10 #define ProofSimple_cxx
36 ProofSimple::ProofSimple()
54 ProofSimple::~ProofSimple()
66 void ProofSimple::Begin(TTree * )
72 TString option = GetOption();
76 if (fInput->FindObject(
"ProofSimple_NHist")) {
77 TParameter<Long_t> *p =
78 dynamic_cast<TParameter<Long_t>*
>(fInput->FindObject(
"ProofSimple_NHist"));
79 fNhist = (p) ? (Int_t) p->GetVal() : fNhist;
80 }
else if ((iopt = option.Index(
"nhist=")) != kNPOS) {
82 Ssiz_t from = iopt + strlen(
"nhist=");
83 if (option.Tokenize(s, from,
";") && s.IsDigit()) fNhist = s.Atoi();
86 Abort(
"fNhist must be > 0! Hint: proof->SetParameter(\"ProofSimple_NHist\","
87 " (Long_t) <nhist>)", kAbortProcess);
91 if (fInput->FindObject(
"ProofSimple_NHist3")) {
92 TParameter<Long_t> *p =
93 dynamic_cast<TParameter<Long_t>*
>(fInput->FindObject(
"ProofSimple_NHist3"));
94 fNhist3 = (p) ? (Int_t) p->GetVal() : fNhist3;
95 }
else if ((iopt = option.Index(
"nhist3=")) != kNPOS) {
97 Ssiz_t from = iopt + strlen(
"nhist3=");
98 if (option.Tokenize(s, from,
";") && s.IsDigit()) fNhist3 = s.Atoi();
102 TNamed *nm =
dynamic_cast<TNamed *
>(fInput->FindObject(
"ProofSimple_Ntuple"));
116 TString ontp(nm->GetTitle());
117 if (ontp.Contains(
"|plot") || ontp ==
"plot") {
119 ontp.ReplaceAll(
"|plot",
"");
120 if (ontp ==
"plot") ontp =
"";
122 if (ontp.BeginsWith(
"dataset")) fHasNtuple = 2;
127 void ProofSimple::SlaveBegin(TTree * )
133 TString option = GetOption();
137 if (fInput->FindObject(
"ProofSimple_NHist")) {
138 TParameter<Long_t> *p =
139 dynamic_cast<TParameter<Long_t>*
>(fInput->FindObject(
"ProofSimple_NHist"));
140 fNhist = (p) ? (Int_t) p->GetVal() : fNhist;
141 }
else if ((iopt = option.Index(
"nhist=")) != kNPOS) {
143 Ssiz_t from = iopt + strlen(
"nhist=");
144 if (option.Tokenize(s, from,
";") && s.IsDigit()) fNhist = s.Atoi();
147 Abort(
"fNhist must be > 0! Hint: proof->SetParameter(\"ProofSimple_NHist\","
148 " (Long_t) <nhist>)", kAbortProcess);
151 fHist =
new TH1F*[fNhist];
155 for (Int_t i=0; i < fNhist; i++) {
157 fHist[i] =
new TH1F(hn.Data(), hn.Data(), 100, -3., 3.);
158 fHist[i]->SetFillColor(kRed);
159 fOutput->Add(fHist[i]);
163 if (fInput->FindObject(
"ProofSimple_NHist3")) {
164 TParameter<Long_t> *p =
165 dynamic_cast<TParameter<Long_t>*
>(fInput->FindObject(
"ProofSimple_NHist3"));
166 fNhist3 = (p) ? (Int_t) p->GetVal() : fNhist3;
167 }
else if ((iopt = option.Index(
"nhist3=")) != kNPOS) {
169 Ssiz_t from = iopt + strlen(
"nhist3=");
170 if (option.Tokenize(s, from,
";") && s.IsDigit()) fNhist3 = s.Atoi();
173 fHist3 =
new TH3F*[fNhist3];
174 Info(
"Begin",
"%d 3D histograms requested", fNhist3);
176 for (Int_t i=0; i < fNhist3; i++) {
178 fHist3[i] =
new TH3F(hn.Data(), hn.Data(),
179 100, -3., 3., 100, -3., 3., 100, -3., 3.);
180 fOutput->Add(fHist3[i]);
185 if (fInput->FindObject(
"ProofSimple_TestLabelMerging")) {
186 fHLab =
new TH1F(
"hlab",
"Test merging of histograms with automatic labels", 10, 0., 10.);
191 TNamed *nm =
dynamic_cast<TNamed *
>(fInput->FindObject(
"ProofSimple_Ntuple"));
205 TString ontp(nm->GetTitle());
206 if (ontp.Contains(
"|plot") || ontp ==
"plot") {
208 ontp.ReplaceAll(
"|plot",
"");
209 if (ontp ==
"plot") ontp =
"";
211 TString locfn(
"SimpleNtuple.root");
212 if (ontp.BeginsWith(
"merge")) {
213 ontp.Replace(0,5,
"");
214 fProofFile =
new TProofOutputFile(locfn,
"M");
216 Ssiz_t iret = ontp.Index(
"|retrieve");
218 fProofFile->SetRetrieve(kTRUE);
219 TString rettag(
"|retrieve");
220 if ((iret = ontp.Index(
"|retrieve=")) != kNPOS) {
222 fn = ontp(iret + rettag.Length(), ontp.Length() - iret - rettag.Length());
223 if ((iret = fn.Index(
'|')) != kNPOS) fn.Remove(iret);
226 ontp.ReplaceAll(rettag,
"");
228 Ssiz_t iof = ontp.Index(
'|');
229 if (iof != kNPOS) ontp.Remove(0, iof + 1);
230 if (!ontp.IsNull()) {
231 fProofFile->SetOutputFileName(ontp.Data());
232 if (fn.IsNull()) fn = gSystem->BaseName(TUrl(ontp.Data(), kTRUE).GetFile());
234 if (fn.IsNull()) fn = locfn;
236 fProofFile->SetTitle(fn);
237 }
else if (ontp.BeginsWith(
"dataset")) {
238 ontp.Replace(0,7,
"");
239 Ssiz_t iof = ontp.Index(
"|");
240 if (iof != kNPOS) ontp.Remove(0, iof + 1);
241 TString dsname = (!ontp.IsNull()) ? ontp.Data() :
"dataset_ntuple";
242 UInt_t opt = TProofOutputFile::kRegister | TProofOutputFile::kOverwrite | TProofOutputFile::kVerify;
243 fProofFile =
new TProofOutputFile(
"SimpleNtuple.root",
244 TProofOutputFile::kDataset, opt, dsname.Data());
246 }
else if (!ontp.IsNull()) {
247 Warning(
"SlaveBegin",
"ntuple options unknown: ignored (%s)", ontp.Data());
253 fFile = fProofFile->OpenFile(
"RECREATE");
254 if (fFile && fFile->IsZombie()) SafeDelete(fFile);
258 Info(
"SlaveBegin",
"could not create '%s': instance is invalid!", fProofFile->GetName());
264 fNtp =
new TNtuple(
"ntuple",
"Demo ntuple",
"px:py:pz:random:i");
267 fNtp->SetDirectory(fFile);
275 fRandom =
new TRandom3(0);
279 Bool_t ProofSimple::Process(Long64_t entry)
299 for (Int_t i=0; i < fNhist; i++) {
300 if (fRandom && fHist[i]) {
301 Double_t x = fRandom->Gaus(0.,1.);
305 for (Int_t i=0; i < fNhist3; i++) {
306 if (fRandom && fHist3[i]) {
307 Double_t x = fRandom->Gaus(0.,1.);
308 fHist3[i]->Fill(x,x,x);
311 if (fHLab && fRandom) {
314 fRandom->RndmArray(10, rr);
315 for (Int_t i=0; i < 10; i++) {
316 sortl.Add(
new TParameter<Int_t>(TString::Format(
"%f",rr[i]), i));
319 TParameter<Int_t> *pi = 0;
320 while ((pi = (TParameter<Int_t> *) nxe())) {
321 fHLab->Fill(TString::Format(
"hl%d", pi->GetVal()), pi->GetVal());
324 if (fNtp) FillNtuple(entry);
330 void ProofSimple::FillNtuple(Long64_t entry)
353 Float_t px, py, random;
355 fRandom->Rannor(px,py);
356 random = fRandom->Rndm();
358 Abort(
"no way to get random numbers! Stop processing", kAbortProcess);
361 Float_t pz = px*px + py*py;
362 Int_t i = (Int_t) entry;
363 fNtp->Fill(px,py,pz,random,i);
370 void ProofSimple::SlaveTerminate()
379 Error(
"SlaveTerminate",
"'ntuple' is undefined!");
382 Bool_t cleanup = kFALSE;
383 TDirectory *savedir = gDirectory;
384 if (fNtp->GetEntries() > 0) {
388 fOutput->Add(fProofFile);
392 fNtp->SetDirectory(0);
393 gDirectory = savedir;
397 TUrl uf(*(fFile->GetEndpointUrl()));
399 gSystem->Unlink(uf.GetFile());
400 SafeDelete(fProofFile);
406 void ProofSimple::Terminate()
415 TCanvas *c1 = (TCanvas *) gDirectory->FindObject(
"c1");
417 gDirectory->Remove(c1);
420 c1 =
new TCanvas(
"c1",
"Proof ProofSimple canvas",200,10,700,700);
421 Int_t nside = (Int_t)TMath::Sqrt((Float_t)fNhist);
422 nside = (nside*nside < fNhist) ? nside+1 : nside;
423 c1->Divide(nside,nside,0,0);
425 Bool_t tryfc = kFALSE;
427 for (Int_t i=0; i < fNhist; i++) {
428 if (!(h = dynamic_cast<TH1F *>(TProof::GetOutput(Form(
"h%d",i), fOutput)))) {
438 if (tryfc && GetHistosFromFC(c1) != 0) {
439 Warning(
"Terminate",
"histograms not found");
447 if (fHLab && !gROOT->IsBatch()) {
449 Int_t nb = fHLab->GetNbinsX();
451 Double_t entb = fHLab->GetEntries() / nb;
453 for (Int_t i = 0; i < nb; i++) {
454 TString lab = TString::Format(
"hl%d", i);
455 Int_t ib = fHLab->GetXaxis()->FindBin(lab);
456 Info(
"Terminate",
" %s [%d]:\t%f", lab.Data(), ib, fHLab->GetBinContent(ib)/entb);
459 Warning(
"Terminate",
"no entries in the hlab histogram!");
464 if (fHasNtuple != 1 || !fPlotNtuple)
return;
466 if (!(fNtp = dynamic_cast<TNtuple *>(TProof::GetOutput(
"ntuple", fOutput)))) {
469 dynamic_cast<TProofOutputFile*>(fOutput->FindObject(
"SimpleNtuple.root")))) {
471 TString outputFile(fProofFile->GetOutputFileName());
472 TString outputName(fProofFile->GetName());
473 outputName +=
".root";
474 Printf(
"outputFile: %s", outputFile.Data());
477 fFile = TFile::Open(outputFile);
479 Printf(
"Managed to open file: %s", outputFile.Data());
480 fNtp = (TNtuple *) fFile->Get(
"ntuple");
482 Error(
"Terminate",
"could not open file: %s", outputFile.Data());
487 Error(
"Terminate",
"TProofOutputFile not found");
492 if (fNtp) PlotNtuple(fNtp,
"proof ntuple");
496 void ProofSimple::PlotNtuple(TNtuple *ntp,
const char *ntptitle)
503 TCanvas *c1 =
new TCanvas(Form(
"cv-%s", ntp->GetName()), ntptitle,800,10,700,780);
505 TPad *pad1 = (TPad *) c1->GetPad(1);
506 TPad *pad2 = (TPad *) c1->GetPad(2);
513 pad1->GetFrame()->SetFillColor(15);
514 ntp->SetLineColor(1);
515 ntp->SetFillStyle(1001);
516 ntp->SetFillColor(45);
517 ntp->Draw(
"3*px+2",
"px**2+py**2>1");
518 ntp->SetFillColor(38);
519 ntp->Draw(
"2*px+2",
"pz>2",
"same");
520 ntp->SetFillColor(5);
521 ntp->Draw(
"1.3*px+2",
"(px^2+py^2>4) && py>0",
"same");
527 ntp->Draw(
"pz:py:px",
"(pz<10 && pz>6)+(pz<4 && pz>3)");
528 ntp->SetMarkerColor(4);
529 ntp->Draw(
"pz:py:px",
"pz<6 && pz>4",
"same");
530 ntp->SetMarkerColor(5);
531 ntp->Draw(
"pz:py:px",
"pz<4 && pz>3",
"same");
532 TPaveText *l2 =
new TPaveText(0.,0.6,0.9,0.95);
533 l2->SetFillColor(42);
534 l2->SetTextAlign(12);
535 l2->AddText(
"You can interactively rotate this view in 2 ways:");
536 l2->AddText(
" - With the RotateCube in clicking in this pad");
537 l2->AddText(
" - Selecting View with x3d in the View menu");
546 Int_t ProofSimple::GetHistosFromFC(TCanvas *cv)
551 TFileCollection *fc = 0;
552 Bool_t fc_found = kFALSE, hs_found = kFALSE;
553 while ((fc = (TFileCollection *) nxo())) {
554 if (strcmp(fc->ClassName(),
"TFileCollection"))
continue;
557 fHist =
new TH1F*[fNhist];
558 for (Int_t i = 0; i < fNhist; i++) { fHist[i] = 0; }
560 for (Int_t i = 0; i < fNhist; i++) { SafeDelete(fHist[i]); }
563 TIter nxf(fc->GetList());
565 while ((fi = (TFileInfo *) nxf())) {
566 TFile *f = TFile::Open(fi->GetCurrentUrl()->GetUrl());
568 for (Int_t i = 0; i < fNhist; i++) {
569 TString hn = TString::Format(
"h%d", i);
570 TH1F *h = (TH1F *) f->Get(hn);
574 fHist[i] = (TH1F *) h->Clone();
575 fHist[i]->SetDirectory(0);
580 Error(
"GetHistosFromFC",
"histo '%s' not found in file '%s'",
581 hn.Data(), fi->GetCurrentUrl()->GetUrl());
586 Error(
"GetHistosFromFC",
"file '%s' could not be open", fi->GetCurrentUrl()->GetUrl());
591 if (!fc_found)
return -1;
592 if (!hs_found)
return -1;
594 for (Int_t i = 0; i < fNhist; i++) {
597 fHist[i]->DrawCopy();
600 Info(
"GetHistosFromFC",
"histograms read from %d files in TFileCollection '%s'",
601 fc->GetList()->GetSize(), fc->GetName());