ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/tschum/FWlite_Analysis/PlotTool.cc
Revision: 1.12
Committed: Tue Dec 8 10:04:35 2009 UTC (15 years, 5 months ago) by tschum
Content type: text/plain
Branch: MAIN
Changes since 1.11: +67 -22 lines
Log Message:
fixed bug in ps with long name, added globalCuts option

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