ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/tschum/FWlite_Analysis/PlotTool.cc
Revision: 1.10
Committed: Thu Dec 3 22:29:16 2009 UTC (15 years, 5 months ago) by tschum
Content type: text/plain
Branch: MAIN
Changes since 1.9: +24 -4 lines
Log Message:
possible init from text file

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