ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/tschum/FWlite_Analysis/PlotTool.cc
Revision: 1.13
Committed: Wed Dec 9 13:02:16 2009 UTC (15 years, 5 months ago) by tschum
Content type: text/plain
Branch: MAIN
Changes since 1.12: +185 -158 lines
Log Message:
add EventInfo to friendTree

File Contents

# Content
1 #include "PlotTool.h"
2 //------------------------------------------------------------------
3 //Constructur: Set some global Variables to define canvas output
4 PlotTool::PlotTool() {
5
6 this->SetClass("TChain", 100);
7
8 samePad_trees = true;
9 samePad_vars = false;
10 samePad_cuts = false;
11 sameCanv_trees = false;
12 sameCanv_vars = false;
13 sameCanv_cuts = false;
14
15 showLegend = false;
16 logY = true;
17 addTrackJets = false;
18 addEventInfo = false;
19 verbose = true;
20
21 globalCuts="";
22
23 }
24
25 //------------------------------------------------------------------
26 //Fill object PlotTool with Chains constructed from files from given source
27
28 int PlotTool::init(string fileName, string dirPath, string treeName,
29 string fileLabel) {
30 this->New(this->GetEntries() );
31 int currChain = this->GetEntries() - 1;
32
33 if (currChain < 0)
34 return currChain;
35
36 TStopwatch timer;
37 timer.Start();
38
39
40 fileNames.clear();
41
42 //check if alternative label is different from default(searchString)
43 if (fileLabel=="") {
44 fileLabel=fileName;
45 }
46 ((TChain*) this->At(currChain))->SetName(fileLabel.c_str());
47
48 TList *files = new TList();
49 TSystemFile* sysFile = 0;
50 if(fileName.find(".") != string::npos) {
51 ifstream f(fileName.c_str());
52 if( ! f.is_open() ) return -1;
53 string line;
54
55 while (!f.eof()) {
56 getline(f,line);
57 sysFile = new TSystemFile(line.c_str(),dirPath.c_str());
58 files->Add(sysFile);
59 }
60
61
62 } else {
63 TSystemDirectory dir("sourceDir", dirPath.c_str());
64 files = dir.GetListOfFiles();
65 }
66 if (files->GetEntries()>0) {
67 TIter next(files);
68 TSystemFile *file;
69 TString fname;
70 string filePath;
71
72 if(verbose && fileName.find(".") == string::npos ) cout<<"Open"<<dirPath.c_str()<<" Search for .root files that contain: "
73 <<fileName.c_str()<<endl;
74 if(verbose && fileName.find(".") != string::npos ) cout<<"Open"<<dirPath.c_str()<<" Search lines with .root in: "
75 <<fileName.c_str()<<endl;
76
77
78 while ((file=(TSystemFile*)next())) {
79 fname = file->GetName();
80 if (!fname.EndsWith(".root"))
81 continue;
82 if (!fname.Contains(fileName.c_str()) && fileName.find(".") == string::npos )
83 continue;
84
85 filePath = dirPath;
86 filePath += fname.Data();
87 //fwlite::ChainEvent to lop over events, jets, etc
88 fileNames.push_back(filePath.c_str());
89
90 if (((TChain*) this->At(currChain))->AddFile(filePath.c_str(), -1,
91 treeName.c_str()) )
92 if(verbose) cout<<"Chained "<<((TChain*) this->At(currChain))->GetNtrees()<<" file(s) with "<<((TChain*) this->At(currChain))->GetEntries()<<" events."<<endl;
93 else
94 return -1;
95
96 }
97 } else
98 return -1;
99
100 for (int i=0; i<((TChain*) this->At(currChain))->GetListOfBranches()->GetEntries(); ++i) {
101
102 string s(((TChain*) this->At(currChain))->GetListOfBranches()->At(i)->GetName());
103
104 string branch_alias = s;
105 string branch_name = s;
106 if (s.find(".", s.size()-1) != string::npos)
107 branch_name += "obj";
108
109 size_t a = s.find("_");
110 if (a != string::npos) {
111 size_t b = s.find("_", a+1);
112 if (b != string::npos) {
113 size_t c = s.find("_", b+1);
114 if (c != string::npos) {
115 string _prod =s.substr(0,a);
116 string _label =s.substr(a+1, b-a-1);
117 string _instance =s.substr(b+1, c-b-1);
118 branch_alias = _label;
119 if(_instance.length() > 0 ) {
120 branch_alias += "_";
121 branch_alias += _instance;
122 ((TChain*) this->At(currChain))->SetAlias(_instance.c_str(),
123 branch_name.c_str());
124 }
125 string branch_alias_full = _prod + "_" + branch_alias;
126 ((TChain*) this->At(currChain))->SetAlias(branch_alias_full.c_str(),
127 branch_name.c_str());
128 }
129 }
130 }
131
132 ((TChain*) this->At(currChain))->SetAlias(branch_alias.c_str(),
133 branch_name.c_str());
134
135 }
136
137 if( addEventInfo || addTrackJets) {
138
139 if(verbose) cout<<"calculating additional variables..."<<endl;
140
141 //make file for tree friends (adding additional, computed branches)
142 string friendFileName("/scratch/hh/current/cms/user/");
143 friendFileName += gSystem->GetUserInfo()->fUser;
144 friendFileName += "/temp/",
145 friendFileName += fileName;
146 friendFileName += ".root";
147 TFile *f = new TFile(friendFileName.c_str(),"recreate");
148
149 if( f->IsZombie() ) return -1;
150
151 string friendTreeName("friendTree");
152 //char number = currChain;
153 //friendTreeName += number;
154 //TTree *friendTree = new TTree("friendTree","friendTree");
155 TTree *friendTree = new TTree(friendTreeName.c_str(),friendTreeName.c_str());
156 fwlite::ChainEvent ev(fileNames);
157 int _event, _run, _lumi;
158 int nJetsKT;
159 TrackJetKT = new float [100];
160
161 if( addEventInfo ) {
162
163
164 friendTree->Branch("event", &_event, "event/I");
165 friendTree->Branch("run", &_run, "run/I");
166 friendTree->Branch("lumi", &_lumi, "lumi/I");
167
168 }
169
170 if( addTrackJets ) {
171
172
173 friendTree->Branch("nJetsKT", &nJetsKT, "nJetsKT/I");
174 friendTree->Branch("TrackJetKT", TrackJetKT, "TrackJetKT[nJetsKT]/F");
175 }
176
177
178 int tenth = ev.size() / 10;
179 int eventNum =0;
180 for (ev.toBegin(); !ev.atEnd(); ++ev, ++eventNum) {
181
182
183 if( addEventInfo ) {
184
185 _event = ev.id().event();
186 _run = ev.id().run();
187 _lumi = ev.luminosityBlock();
188
189 }
190
191 if( addTrackJets ) {
192
193 fwlite::Handle<reco::CaloJetCollection> jets;
194 jets.getByLabel(ev, "antikt5CaloJets");
195
196 fwlite::Handle<reco::TrackCollection> tracks;
197 tracks.getByLabel(ev, "generalTracks");
198
199 if (!jets.isValid())
200 continue;
201 if (!tracks.isValid())
202 continue;
203 double trackSum;
204 nJetsKT = 0;
205 for (reco::CaloJetCollection::const_iterator jet = jets->begin(); jet
206 !=jets->end(); jet++) {
207 trackSum = 0;
208 for (reco::TrackCollection::const_iterator track = tracks->begin(); track
209 !=tracks->end(); track++) {
210 if (deltaR(jet->eta(), jet->phi(), track->eta(), track->phi())
211 < 0.5)
212 trackSum += track->pt();
213 }
214 TrackJetKT[nJetsKT] = trackSum;
215 nJetsKT++;
216 }
217 }
218 friendTree->Fill();
219
220 if( eventNum != 0 && eventNum%tenth == 0) cout<<"Processed "<<eventNum <<" of "<<ev.size()<<" events. "<<endl;
221
222 }
223 f->cd();
224 friendTree->Write();
225 f->Close();
226 ((TChain*) this->At(currChain))->AddFriend(friendTreeName.c_str(),
227 friendFileName.c_str());
228
229
230
231 }
232
233
234 timer.Stop();
235 if(verbose) timer.Print();
236
237 return this->GetEntries();
238
239 }
240 //------------------------------------------------------------------
241 //Draw given Chain with given variable and given cuts
242 int PlotTool::plot(int chainIndex, string histName, string cutName,
243 int nEntries, string drwOpt) {
244
245 if (chainIndex >= this->GetEntries() )
246 return -1;
247
248 TStopwatch timer;
249 if(verbose) {
250 cout<<"Plot: "<<histName<<" "<<cutName<<" from chain "<<this->At(chainIndex)->GetName()<<endl;
251 timer.Start();
252 }
253
254 int currN = nEntries;
255 if (nEntries < 0)
256 currN = ((TChain*) this->At(chainIndex))->GetEntries(); //nEntries<0 : all entries are plotted!
257
258 //++++ Create and name Canvases according to global variables +++++++++++++
259 ostringstream currHistName;
260 if (samePad_trees)
261 currHistName<<((TChain*) this->At(chainIndex))->GetName()<<":";
262 if (samePad_vars)
263 currHistName<<histName;
264 if (samePad_cuts)
265 currHistName<<"{"<<cutName<<"}";
266
267 ostringstream currPadName;
268 if (!samePad_trees)
269 currPadName<<((TChain*) this->At(chainIndex))->GetName()<<":";
270 if (!samePad_vars)
271 currPadName<<histName;
272 if (!samePad_cuts)
273 currPadName<<"{"<<cutName<<"}";
274
275 ostringstream currCanvName;
276 if (!sameCanv_trees && !samePad_trees)
277 currCanvName<<((TChain*) this->At(chainIndex))->GetName()<<":";
278 if (!sameCanv_vars && !samePad_vars)
279 currCanvName<<histName;
280 if (!sameCanv_cuts && !samePad_cuts)
281 currCanvName<<"{"<<cutName<<"}";
282
283 if ( (sameCanv_trees || samePad_trees) && (sameCanv_vars || samePad_vars)
284 && (sameCanv_cuts || samePad_cuts))
285 currCanvName<<"All";
286
287 string currOpt = drwOpt;
288 bool useSubPads = (sameCanv_trees && !samePad_trees) || (sameCanv_vars
289 && !samePad_vars) || (sameCanv_cuts && !samePad_cuts);
290
291 if (useSubPads) {
292 if ( !canvases_[currCanvName.str()] || !gROOT->GetListOfCanvases()->FindObject(currCanvName.str().c_str()) ) {
293 canvases_[currCanvName.str()] = new TCanvas(currCanvName.str().c_str(), currCanvName.str().c_str());
294 } else if (canvases_[currCanvName.str()]->GetNumber() >= 0) {
295 canvases_[currCanvName.str()]->SetNumber(canvases_[currCanvName.str()]->GetNumber()+1);
296 }
297 }
298
299 if ( !pads_[currPadName.str()] || !gROOT->GetListOfCanvases()->FindObject(currPadName.str().c_str()) ) {
300 pads_[currPadName.str()] = new TCanvas(currPadName.str().c_str(), currPadName.str().c_str());
301 // if (logY)
302 // pads_[currPadName.str()]->SetLogy(1);
303 } else {
304 pads_[currPadName.str()]->cd();
305 currOpt += "SAMES";
306 if (useSubPads)
307 canvases_[currCanvName.str()]->SetNumber(canvases_[currCanvName.str()]->GetNumber()-1);
308 }
309 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
310
311
312 //Draw histogram:
313 string cutNameWithGlobal = cutName;
314 if(cutName!="" && globalCuts!="") cutNameWithGlobal += "&&";
315 if(globalCuts!="") cutNameWithGlobal += globalCuts;
316
317 int draw_err = ((TChain*) this->At(chainIndex))->Draw(histName.c_str(), cutNameWithGlobal.c_str(),
318 currOpt.c_str(), currN);
319 if (draw_err < 0)
320 return draw_err;
321
322 //++++ Fix for histos with no entries +++++++++++++
323 if (draw_err == 0) {
324 if (currOpt.find("SAMES") == string::npos) {
325 pads_[currPadName.str()]->Close();
326 pads_.erase(currPadName.str() );
327 if (useSubPads)
328 canvases_[currCanvName.str()]->SetNumber(canvases_[currCanvName.str()]->GetNumber()-1);
329 }
330 cout<< "Warning: "<<currHistName.str().c_str()<<" in "<<currPadName.str()
331 <<" has no entries and is not drawn!"<<endl;
332 return draw_err;
333 }
334 //++++++++++++++++++++++++++++++++++++++++++++++++++
335
336 ((TH1F*) pads_[currPadName.str()]->GetPrimitive("htemp"))->SetName(currHistName.str().c_str()); // Set Name of histogram
337
338 // Update for 2D histograms
339 if( pads_[currPadName.str()]->GetPrimitive(currHistName.str().c_str())->InheritsFrom("TH2") ) {
340 pads_[currPadName.str()]->Draw();
341 setCanvas( pads_[currPadName.str()] );
342 }
343
344
345 if(verbose && draw_err > 0) {
346 timer.Stop();
347 cout<<"Done: Selected "<<draw_err<<" objects in "<< currN <<" processed events."<<endl;
348 timer.Print();
349 }
350 return draw_err;
351
352 }
353 //------------------------------------------------------------------
354 //Standard loop to draw all chains and multiple variables and cuts
355 int PlotTool::loop(vector<string> _histName, vector<string> _cutName,
356 int nEntries, string drwOpt, bool correspond) {
357
358 int numProcessed = 0;
359
360 if (correspond == false) {
361
362 for (int i=0; i<this->GetEntries(); ++i) {
363 for (vector<string>::iterator it1 =_histName.begin(); it1
364 != _histName.end(); ++it1) {
365 for (vector<string>::iterator it2 =_cutName.begin(); it2
366 != _cutName.end(); ++it2) {
367
368 numProcessed += plot(i, *it1, *it2, nEntries, drwOpt);
369
370
371 }
372 }
373 }
374 } else {
375
376 if (_histName.size() != _cutName.size() )
377 return -1;
378
379 for (int i=0; i<this->GetEntries(); ++i) {
380 for (vector<string>::iterator it1 =_histName.begin(), it2 =
381 _cutName.begin(); it1 != _cutName.end()&&it2
382 != _cutName.end(); ++it1, ++it2) {
383
384 numProcessed += plot(i, *it1, *it2, nEntries, drwOpt);
385
386 }
387 }
388 }
389
390 updatePads();
391 fillCanvases();
392
393 return numProcessed;
394 }
395
396 //Helper methods to allow for using simple strings as input
397 int PlotTool::loop(string histName, string cutName, int nEntries, string drwOpt) {
398
399 vector<string> _histName;
400 _histName.push_back(histName);
401 vector<string> _cutName;
402 _cutName.push_back(cutName);
403
404 return loop(_histName, _cutName, nEntries, drwOpt);
405
406 }
407
408 int PlotTool::loop(vector<string> _histName, string cutName, int nEntries,
409 string drwOpt) {
410
411 vector<string> _cutName;
412 _cutName.push_back(cutName);
413
414 return loop(_histName, _cutName, nEntries, drwOpt);
415
416 }
417
418 int PlotTool::loop(string histName, vector<string> _cutName, int nEntries,
419 string drwOpt) {
420
421 vector<string> _histName;
422 _histName.push_back(histName);
423
424 return loop(_histName, _cutName, nEntries, drwOpt);
425 }
426 //------------------------------------------------------------------
427 //redraw the canvas to make changes in style visible
428 int PlotTool::updatePads() {
429
430 for (map< string, TCanvas* >::iterator it=pads_.begin() ; it != pads_.end(); ++it) {
431 if (gROOT->GetListOfCanvases()->FindObject((*it).first.c_str() ) ) {
432 (*it).second->Draw();
433 setCanvas((*it).second);
434 } else
435 pads_.erase(it);
436 }
437
438 return pads_.size();
439
440 }
441
442 int PlotTool::fillCanvases() {
443
444 for (map< string, TCanvas* >::iterator it=canvases_.begin() ; it
445 != canvases_.end(); ++it) {
446 string canvName = (*it).first;
447 if (gROOT->GetListOfCanvases()->FindObject(canvName.c_str()) ) {
448
449 int numP = (*it).second->GetNumber()+1;
450
451 if (numP <= 0)
452 continue;
453
454 int x = int( sqrt(numP) );
455 int y = x;
456 if (x*y < numP)
457 x += 1;
458 if (x*y < numP)
459 x += 1;
460 if (x*y < numP) {
461 x -= 1;
462 y += 1;
463 }
464
465
466 (*it).second->Divide(x, y);
467 int padIndex = 1;
468 for (map< string, TCanvas* >::iterator it2=pads_.begin() ; it2
469 != pads_.end(); ++it2) {
470 string padName = (*it2).first;
471 if (gROOT->GetListOfCanvases()->FindObject(padName.c_str() ) ) {
472 string n1 = canvName.substr(0,canvName.find(":"));
473 string n2("");
474 if(canvName.find("{") != string::npos ) n2 = canvName.substr(canvName.find("{"),canvName.find("}"));
475
476 if ( (padName.find(canvName.c_str()) != string::npos ) || (padName.find(n1) != string::npos && padName.find(n2) != string::npos) || !strcmp( canvName.c_str(), "All") ) {
477 (*it).second->cd(padIndex);
478 (*it2).second->DrawClonePad();
479 (*it2).second->Close();
480 pads_.erase(it2);
481 ++padIndex;
482 }
483 } else
484 pads_.erase(it2);
485 }
486 (*it).second->SetNumber(-1);
487
488 } else
489 canvases_.erase(it);
490 }
491
492 return canvases_.size();
493
494 }
495 //------------------------------------------------------------------
496 void PlotTool::setCanvas(TCanvas* thisCanvas) {
497
498 TH1* thisHist = 0;
499 TPaveStats* thisStatsBox = 0;
500 TPaletteAxis* palette =0;
501 TLegend* thisLeg = 0;
502 int counter =0;
503 double maxEntry=0;
504
505 ((TFrame*) thisCanvas->GetFrame())->Delete();
506 thisCanvas->GetFrame()->SetLineWidth(gStyle->GetLineWidth() );
507
508 thisCanvas->SetLeftMargin(0.2);
509 thisCanvas->SetRightMargin(0.06);
510 thisCanvas->SetBottomMargin(0.2);
511 thisCanvas->SetTopMargin(0.1);
512
513 if (logY)
514 thisCanvas->SetLogy(1);
515 else
516 thisCanvas->SetLogy(0);
517
518 if (showLegend)
519 thisLeg = new TLegend();
520
521 for (int i = 0; i != thisCanvas->GetListOfPrimitives()->GetSize(); ++i) {
522
523 if ( !thisCanvas->GetListOfPrimitives()->At(i)->InheritsFrom("TH1"))
524 continue;
525
526 thisHist = ((TH1*) thisCanvas->GetListOfPrimitives()->At(i));
527 setColor(thisHist, counter);
528 setMathLabels(thisHist);
529
530 if (thisHist->GetMaximum() > maxEntry)
531 maxEntry = thisHist->GetMaximum();
532
533 thisStatsBox = (TPaveStats*) thisHist->GetListOfFunctions()->FindObject("stats");
534 if (thisStatsBox)
535 setStats(thisCanvas, thisStatsBox, thisHist, counter);
536
537 palette = (TPaletteAxis*) thisHist->GetListOfFunctions()->FindObject("palette");
538 if (palette)
539 setPalette(thisCanvas, palette);
540
541 if (thisLeg)
542 thisLeg->AddEntry(thisHist,thisHist->GetName())->SetTextSize(0.04);
543
544 ++counter;
545
546 }
547
548 if (maxEntry != 0)
549 setHistMax(thisCanvas, maxEntry);
550
551 thisCanvas->cd();
552 thisCanvas->Update();
553
554 if (thisLeg)
555 setLegend(thisCanvas, thisLeg, counter);
556
557 }
558 //------------------------------------------------------------------
559 //private helper classes to set the canvas and hist style
560 void PlotTool::setStats(TCanvas* thisCanvas, TPaveStats* thisStatsBox,
561 TH1* thisHist, int counter) {
562
563 if (thisCanvas->GetRightMargin() < .2)
564 thisCanvas->SetRightMargin(.2);
565
566 thisStatsBox->SetLineColor(thisHist->GetLineColor());
567 thisStatsBox->SetX2NDC(1.);
568 thisStatsBox->SetY2NDC(1-thisCanvas->GetTopMargin()-0.16*counter);
569 thisStatsBox->SetX1NDC(1-thisCanvas->GetRightMargin()+0.01);
570 thisStatsBox->SetY1NDC(thisStatsBox->GetY2NDC()-.15);
571
572 }
573
574 void PlotTool::setMathLabels(TH1* thisHist) {
575
576 string t = thisHist ->GetTitle();
577 string x = thisHist->GetXaxis()->GetTitle();
578 string y = thisHist->GetYaxis()->GetTitle();
579
580 // if (x.find("__") != string::npos)
581 // x = x.substr(0,x.find("__"));
582 // if (y.find("__") != string::npos)
583 // y = y.substr(0,y.find("__"));
584
585 if (x.find(".phi()") != string::npos)
586 x.replace(x.find(".phi()"), 6, " #phi");
587 if (y.find(".phi()") != string::npos)
588 y.replace(y.find(".phi()"), 6, " #phi");
589
590 if (x.find(".eta()") != string::npos)
591 x.replace(x.find(".eta()"), 6, " #eta");
592 if (y.find(".eta()") != string::npos)
593 y.replace(y.find(".eta()"), 6, " #eta");
594
595 if (x.find(".theta()") != string::npos)
596 x.replace(x.find(".theta()"), 6, " #theta");
597 if (y.find(".theta()") != string::npos)
598 y.replace(y.find(".theta()"), 6, " #theta");
599
600 if (x.find(".chi2()") != string::npos)
601 x.replace(x.find(".chi2()"), 7, " #chi^{2}");
602 if (y.find(".chi2()") != string::npos)
603 y.replace(y.find(".chi2()"), 7, " #chi^{2}");
604
605 if (x.find(".pt()") != string::npos)
606 x.replace(x.find(".pt()"), 5, " p_{T} [GeV]");
607 if (y.find(".pt()") != string::npos)
608 y.replace(y.find(".pt()"), 5, " p_{T} [GeV]");
609
610 if (x.find(".et()") != string::npos)
611 x.replace(x.find(".et()"), 5, " E_{T} [GeV]");
612 if (y.find(".et()") != string::npos)
613 y.replace(y.find(".et()"), 5, " E_{T} [GeV]");
614
615 //splitlines for many cuts
616 string test1= "{" + globalCuts + "}";
617 string test2= "&&" + globalCuts;
618
619 if(t.find(test1) != string::npos) t.replace(t.find(test1),test1.length(),"");
620 if(t.find(test2) != string::npos) t.replace(t.find(test2),test2.length(),"");
621
622 if (t.find("{") != string::npos && t.find("#splitline") == string::npos) {
623 t.replace(t.find_last_of("{"), 1, "}{");
624 string t_old = t;
625 t = "#splitline{";
626 t += t_old;
627 }
628
629 thisHist ->SetTitle(t.c_str());
630 thisHist->GetXaxis()->SetTitle(x.c_str());
631 thisHist->GetYaxis()->SetTitle(y.c_str());
632
633 }
634
635 void PlotTool::setColor(TH1* thisHist, int counter) {
636
637 if (counter == 0) {
638
639 thisHist->SetLineColor(kRed+1);
640 //if( gStyle->GetHistFillStyle() ) thisHist->SetFillColor (kRed+1);
641 //else
642 thisHist->SetFillStyle(0);
643 thisHist->SetMarkerColor(kRed+1);
644 } else if (counter == 1) {
645 thisHist->SetLineColor(kBlue+1);
646 //if( gStyle->GetHistFillStyle() ) thisHist->SetFillColor (kBlue+1);
647 //else
648 thisHist->SetFillStyle(0);
649 thisHist->SetMarkerColor(kBlue+1);
650
651 } else if (counter == 2) {
652 thisHist->SetLineColor(kGreen+2);
653 //if( gStyle->GetHistFillStyle() ) thisHist->SetFillColor (kGreen+2);
654 //else
655 thisHist->SetFillStyle(0);
656 thisHist->SetMarkerColor(kGreen+2);
657
658 } else if (counter == 3) {
659 thisHist->SetLineColor(kMagenta+2);
660 //if( gStyle->GetHistFillStyle() ) thisHist->SetFillColor (kMagenta+2);
661 //else
662 thisHist->SetFillStyle(0);
663 thisHist->SetMarkerColor(kMagenta+2);
664
665 } else if (counter == 4) {
666 thisHist->SetLineColor(kCyan+2);
667 //if( gStyle->GetHistFillStyle() ) thisHist->SetFillColor (kCyan+2);
668 //else
669 thisHist->SetFillStyle(0);
670 thisHist->SetMarkerColor(kCyan+2);
671
672 } else {
673 thisHist->SetLineColor(kBlack);
674 //if( gStyle->GetHistFillStyle() ) thisHist->SetFillColor (kBlack);
675 //else
676 thisHist->SetFillStyle(0);
677 thisHist->SetMarkerColor(kBlack);
678
679 }
680
681 }
682
683 void PlotTool::setPalette(TCanvas* thisCanvas, TPaletteAxis* palette) {
684
685 palette->SetLabelSize(0.045);
686 if (thisCanvas->GetRightMargin() < .15)
687 thisCanvas->SetRightMargin(.15);
688 palette->SetX1NDC(1-thisCanvas->GetRightMargin()+0.01);
689 palette->SetY1NDC(thisCanvas->GetBottomMargin());
690 palette->SetX2NDC(palette->GetX1NDC()+0.05);
691 palette->SetY2NDC(1-thisCanvas->GetTopMargin());
692
693 }
694
695 void PlotTool::setHistMax(TCanvas* thisCanvas, double maxEntry) {
696
697 TH1* thisHist = 0;
698
699 for (int i = 0; i != thisCanvas->GetListOfPrimitives()->GetSize(); ++i) {
700
701 if ( !thisCanvas->GetListOfPrimitives()->At(i)->InheritsFrom("TH1") )
702 continue;
703 thisHist = ((TH1*) thisCanvas->GetListOfPrimitives()->At(i));
704 if (thisHist->GetMaximum() < maxEntry) {
705 double minEntry=thisHist->GetBinContent(thisHist->GetMinimumBin());
706 if (logY) {
707 int bin = (thisHist->GetMinimumBin()+1);
708 minEntry=thisHist->GetBinContent(bin);
709 }
710 thisHist->GetYaxis()->SetRangeUser(minEntry, maxEntry*1.2);
711 }
712 break;
713
714 }
715
716 }
717
718 void PlotTool::setLegend(TCanvas* thisCanvas, TLegend* thisLeg, int counter) {
719
720 thisLeg->SetFillStyle(0);
721 thisLeg->SetX1NDC(1-thisCanvas->GetRightMargin()-0.5);
722 thisLeg->SetY1NDC(1-thisCanvas->GetTopMargin()-0.05*counter);
723 thisLeg->SetX2NDC(1-thisCanvas->GetRightMargin());
724 thisLeg->SetY2NDC(1-thisCanvas->GetTopMargin());
725 thisLeg->SetEntrySeparation(0.5);
726 thisLeg->Draw("NDC");
727
728 }
729
730 void PlotTool::createColors() {
731 const Int_t NRGBs = 5;
732 const Int_t NCont = 255;
733
734 Double_t stops[NRGBs] = { 0.00, 0.34, 0.61, 0.84, 1.00 };
735 Double_t red[NRGBs] = { 0.00, 0.00, 0.87, 1.00, 0.51 };
736 Double_t green[NRGBs] = { 0.00, 0.81, 1.00, 0.20, 0.00 };
737 Double_t blue[NRGBs] = { 0.51, 1.00, 0.12, 0.00, 0.00 };
738 TColor::CreateGradientColorTable(NRGBs, stops, red, green, blue, NCont);
739 gStyle->SetNumberContours(NCont);
740
741 }
742
743 //------------------------------------------------------------------
744
745 int PlotTool::saveCanvases(string name, string path) {
746
747 TSystemDirectory d("", path.c_str());
748 if (!d.GetListOfFiles())
749 return -1;
750
751 string namePs = path +"/" + name;
752 string nameRt = path +"/" + name;
753
754 if (name.find(".ps") == string::npos)
755 namePs += ".ps";
756
757 if (name.find(".root") == string::npos)
758 nameRt += ".root";
759
760 int savedCanvs =0;
761
762
763 TIter next(gROOT->GetListOfCanvases());
764 TCanvas* canv;
765 while ((canv=(TCanvas*)next())) {
766 string modName = (*canv).GetName();
767 if(modName.find("{") != string::npos) modName = modName.substr(0,modName.find("{"));
768 (*canv).SetTitle( modName.c_str() );
769
770 }
771
772
773 TPostScript ps(namePs.c_str(),112);
774 TFile rt(nameRt.c_str(),"recreate");
775
776 next.Reset();
777 while ((canv=(TCanvas*)next())) {
778 ps.NewPage();
779
780
781 (*canv).Write();
782 (*canv).Draw();
783 (*canv).Update();
784
785
786 ++savedCanvs;
787
788 }
789
790 ps.Close();
791 rt.Close();
792
793
794 if(verbose && savedCanvs) {
795 cout<<"Saved file "<<rt.GetName()<<" with "<<savedCanvs<<" canvases."<<endl;
796 cout<<"Saved file "<<ps.GetName()<<" with "<<savedCanvs<<" pages."<<endl;
797 }
798 return savedCanvs;
799
800 }
801 //------------------------------------------------------------------
802
803 int PlotTool::clearCanvases()
804 {
805
806 while(gROOT->GetListOfCanvases()->GetEntries()) ((TCanvas*) gROOT->GetListOfCanvases()->At(0))->Close();
807 pads_.clear();
808 autoVars.clear();
809 return pads_.size() + gROOT->GetListOfCanvases()->GetEntries();
810
811 }
812 //------------------------------------------------------------------
813 int PlotTool::setVariables(string label)
814 {
815
816 for (int i=0; i< this->GetEntries(); ++i) {
817 cout<<"--------------------------------"<<endl;
818 cout<<((TChain*) this->At(i))->GetName()<<endl;
819 cout<<"------------"<<endl;
820
821 TList* leaves = (TList*) ((TChain*) this->At(i))->GetListOfLeaves();
822 for (int j=0; j< leaves->GetEntries(); ++j) {
823 TString leafName ( leaves->At(j)->GetName() );
824 if(! leafName.EndsWith(".") ) continue;
825 if(! leafName.Contains(label.c_str() ) ) continue;
826
827 TClass cl( ( (TLeafElement*) leaves->At(j) )->GetTypeName() );
828 cout<<"++++++"<<endl;
829 cout<< leafName.Data() <<endl;
830 for(int k=0;k<cl.GetListOfAllPublicMethods()->GetEntries();++k) {
831 string typeName ( ((TMethod*) cl.GetListOfAllPublicMethods()->At(k))->GetReturnTypeName() );
832 string methName ( cl.GetListOfAllPublicMethods()->At(k)->GetName() );
833 if( methName != "product") continue;
834 cout<< typeName <<endl;
835 string _type;
836 TString testString( typeName.c_str() );
837 if( testString.BeginsWith("vector<") ) {
838 _type = typeName.substr(typeName.find("<")+1,typeName.find(">")-typeName.find("<")-1);
839 string _varSize = leafName.Data();
840 _varSize += "obj@.size()";
841 autoVars.push_back( _varSize );
842 }
843 else _type = typeName.substr(0,typeName.find("*"));
844 TClass _cl( _type.c_str() );
845 for(int l=0;l<_cl.GetListOfAllPublicMethods()->GetEntries();++l) {
846 string _typeName ( ((TMethod*) _cl.GetListOfAllPublicMethods()->At(l))->GetReturnTypeName() );
847 string _methName ( _cl.GetListOfAllPublicMethods()->At(l)->GetName() );
848 // if(_typeName.find("void") != string::npos ) continue;
849 // cout<<" "<<_typeName<<" "<<_methName<<endl;
850
851 cout<<" "<<_typeName<<" "<<_methName<<endl;
852 if(_methName.find("operator")==string::npos&&(_typeName=="float"||_typeName=="double"||_typeName=="int"||_typeName=="unsigned int"||_typeName=="bool"||_typeName=="unsigned short"||_typeName=="unsigned long"||_typeName=="unsigned long long")) {
853
854 cout<<"--> "<<_typeName<<" "<<_methName<<endl;
855
856 string _varName = leafName.Data();
857 _varName += "obj.";
858 _varName += _methName;
859 _varName += "()";
860 autoVars.push_back( _varName );
861 }
862 }
863
864
865 }
866
867 }
868
869 }
870
871 return autoVars.size();
872
873 }
874 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
875 //Draw efficiencies
876 int PlotTool::plotEff(int chainIndex, string histName, string cutName, int nEntries, double fitXmin, double fitXmax, string fitFormula)
877 {
878
879 if( chainIndex >= this->GetEntries() ) return -1;
880
881 int currN = nEntries;
882 if(nEntries < 0) currN = ((TChain*) this->At(chainIndex))->GetEntries(); //nEntries<0 : all entries are plotted!
883
884 //++++ Create and name Canvases according to global variables +++++++++++++
885 ostringstream currHistName;
886 if( samePad_trees) currHistName<<((TChain*) this->At(chainIndex))->GetName()<<":";
887 if( samePad_vars) currHistName<<histName;
888 if( samePad_cuts) currHistName<<"{"<<cutName<<"}";
889
890 ostringstream currPadName;
891 if(! samePad_trees) currPadName<<((TChain*) this->At(chainIndex))->GetName()<<":";
892 if(! samePad_vars) currPadName<<histName;
893 if(! samePad_cuts) currPadName<<"{"<<cutName<<"}";
894
895
896 //Draw total histogram:
897 if( ! pads_["total"] || ! gROOT->FindObject("total") ) {
898 pads_["total"] = new TCanvas("total", "total");
899 } else {
900 pads_["total"]->cd();
901 }
902 ostringstream bins_total;
903 bins_total<<histName;
904 bins_total<<">>total(100,0,1000)";
905
906
907 int draw_err = ((TChain*) this->At(chainIndex))->Draw(bins_total.str().c_str(), "", "", currN);
908 if( draw_err <= 0 ) return draw_err;
909
910
911 TH1* total = ((TH1*) pads_["total"]->GetPrimitive("total"));
912
913 ostringstream bins_bkg;
914 bins_bkg<<histName;
915 bins_bkg<<">>bkg(";
916 bins_bkg<<total->GetNbinsX();
917 bins_bkg<<",";
918 bins_bkg<<total->GetXaxis()->GetXmin();
919 bins_bkg<<",";
920 bins_bkg<<total->GetXaxis()->GetXmax();
921 bins_bkg<<")";
922
923 //Draw bkg histogram:
924 if( ! pads_["bkg"] || ! gROOT->FindObject("bkg") ) {
925 pads_["bkg"] = new TCanvas("bkg", currPadName.str().c_str());
926 } else {
927 pads_["bkg"]->cd();
928 }
929
930 draw_err = ((TChain*) this->At(chainIndex))->Draw(bins_bkg.str().c_str(), cutName.c_str(),"" , currN);
931 if( draw_err <= 0 ) return draw_err;
932
933
934 TH1* bkg = ((TH1*) pads_["bkg"]->GetPrimitive("bkg"));
935
936 //Draw pass histogram:
937 ostringstream bins_pass;
938 bins_pass<<histName;
939 bins_pass<<">>pass(";
940 bins_pass<<total->GetNbinsX();
941 bins_pass<<",";
942 bins_pass<<total->GetXaxis()->GetXmin();
943 bins_pass<<",";
944 bins_pass<<total->GetXaxis()->GetXmax();
945 bins_pass<<")";
946
947 ostringstream cut_pass;
948 cut_pass<<"!(";
949 cut_pass<<cutName;
950 cut_pass<<")";
951
952
953 if( ! pads_["pass"] || ! gROOT->FindObject("pass") ) {
954 pads_["pass"] = new TCanvas("pass", currPadName.str().c_str());
955 } else {
956 pads_["pass"]->cd();
957 }
958
959 draw_err = ((TChain*) this->At(chainIndex))->Draw(bins_pass.str().c_str(),cut_pass.str().c_str() ,"" , currN);
960 if( draw_err <= 0 ) return draw_err;
961
962
963 TH1* pass = ((TH1*) pads_["pass"]->GetPrimitive("pass"));
964
965
966 currPadName<<"Eff";
967 //Draw Efficiency Graph:
968 if( ! pads_["EffGraph"] || ! gROOT->FindObject("EffGraph") ) {
969 pads_["EffGraph"] = new TCanvas("EffGraph", currPadName.str().c_str());
970 } else {
971 pads_["EffGraph"]->cd();
972 }
973
974 TGraphAsymmErrors* EffGraph = new TGraphAsymmErrors(bkg, total);
975 EffGraph->SetName(currHistName.str().c_str());
976
977 TF1* reverse = new TF2("reverse","1/y-1",total->GetXaxis()->GetXmin(),total->GetXaxis()->GetXmax());
978 EffGraph->Apply(reverse);
979 EffGraph->Draw("A*");
980
981 TF1* fitfunc = new TF1("fitfunc",fitFormula.c_str(),fitXmin,fitXmax);
982 EffGraph->Fit("fitfunc","R+");
983
984
985 //Save fit function
986
987 ostringstream savefuncName;
988 savefuncName<<histName;
989 savefuncName<<"_";
990
991 if(cutName.find("<") != std::string::npos ) cutName.replace(cutName.find("<"),1,"_");
992 if(cutName.find(">") != std::string::npos ) cutName.replace(cutName.find(">"),1,"_");
993 savefuncName<<cutName;
994
995
996 TFile file("ABCDFunctions.root","UPDATE");
997 TF1* savefunc = new TF1(savefuncName.str().c_str(), fitfunc->GetExpFormula().Data(),0,10000);
998 savefunc->SetParameters( fitfunc->GetParameters() );
999 savefunc->SetParErrors ( fitfunc->GetParErrors() );
1000
1001 //Show results
1002 if( ! pads_["abcd"] || ! gROOT->FindObject("abcd") ) {
1003 pads_["abcd"] = new TCanvas("abcd", "abcd");
1004 } else {
1005 pads_["abcd"]->cd();
1006 }
1007
1008 bkg->Multiply(savefunc);
1009 bkg->Draw();
1010
1011 // total->Add(bkg,-1);
1012 pass->SetLineColor(kRed);
1013 pass->Draw("sames,e");
1014 pads_["abcd"]->SetLogy();
1015
1016
1017 savefunc->Write();
1018 // file.Close();
1019
1020
1021 return draw_err;
1022
1023 }
1024 //------------------------------------------------------------------