ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/tschum/FWlite_Analysis/PlotTool.cc
Revision: 1.7
Committed: Fri Nov 27 17:39:46 2009 UTC (15 years, 5 months ago) by tschum
Content type: text/plain
Branch: MAIN
Changes since 1.6: +102 -62 lines
Log Message:
fixed bug with slash in name and label ambiguity

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
19 }
20
21 //------------------------------------------------------------------
22 //Fill object PlotTool with Chains constructed from files from given source
23
24 int PlotTool::init(string fileName, string dirPath, string treeName,
25 string fileLabel) {
26 this->New(this->GetEntries() );
27 int currChain = this->GetEntries() - 1;
28
29 if (currChain < 0)
30 return currChain;
31
32 fileNames.clear();
33
34 //check if alternative label is different from default(searchString)
35 if (fileLabel=="") {
36 fileLabel=fileName;
37 }
38 ((TChain*) this->At(currChain))->SetName(fileLabel.c_str());
39 TSystemDirectory dir("sourceDir", dirPath.c_str());
40 TList *files = dir.GetListOfFiles();
41 if (files) {
42 TIter next(files);
43 TSystemFile *file;
44 TString fname;
45 string filePath;
46
47 cout<<"Open"<<dirPath.c_str()<<" Search for .root files that contain: "
48 <<fileName.c_str()<<endl;
49
50 while ((file=(TSystemFile*)next())) {
51 fname = file->GetName();
52 if (!fname.EndsWith(".root"))
53 continue;
54 if (!fname.Contains(fileName.c_str()))
55 continue;
56
57 filePath = dirPath;
58 filePath += fname.Data();
59 //fwlite::ChainEvent to lop over events, jets, etc
60 fileNames.push_back(filePath.c_str());
61
62 if (((TChain*) this->At(currChain))->AddFile(filePath.c_str(), -1,
63 treeName.c_str()) )
64 cout<<"Chained "<<((TChain*) this->At(currChain))->GetNtrees()<<" file(s) with "<<((TChain*) this->At(currChain))->GetEntries()<<" events."<<endl;
65 else
66 return -1;
67
68 }
69 } else
70 return -1;
71
72 for (int i=0; i<((TChain*) this->At(currChain))->GetListOfBranches()->GetEntries(); ++i) {
73
74 string s(((TChain*) this->At(currChain))->GetListOfBranches()->At(i)->GetName());
75
76 string branch_alias = s;
77 string branch_name = s;
78 if (s.find(".", s.size()-1) != string::npos)
79 branch_name += "obj";
80
81 size_t a = s.find("_");
82 if (a != string::npos) {
83 size_t b = s.find("_", a+1);
84 if (b != string::npos) {
85 size_t c = s.find("_", b+1);
86 if (c != string::npos) {
87 string _label =s.substr(a+1, b-a-1);
88 string _instance =s.substr(b+1, c-b-1);
89 branch_alias = _label;
90 if(_instance.length() > 0 ) {
91 branch_alias += "_";
92 branch_alias += _instance;
93 }
94 }
95 }
96 }
97
98 ((TChain*) this->At(currChain))->SetAlias(branch_alias.c_str(),
99 branch_name.c_str());
100
101 }
102
103 // add branch with track Jets
104
105 if( addTrackJets ) {
106
107 //make file for tree friends (adding additional, computed branches)
108 string friendFileName("/scratch/hh/lustre/cms/user/thomsen/temp/");
109 friendFileName += fileName;
110 friendFileName += ".root";
111 TFile *f = new TFile(friendFileName.c_str(),"recreate");
112 string friendTreeName("friendTree");
113 //char number = currChain;
114 //friendTreeName += number;
115 //TTree *friendTree = new TTree("friendTree","friendTree");
116 TTree *friendTree = new TTree(friendTreeName.c_str(),friendTreeName.c_str());
117
118 int nJetsKT;
119 TrackJetKT = new float [100];
120 fwlite::ChainEvent ev(fileNames);
121
122 friendTree->Branch("nJetsKT", &nJetsKT, "nJetsKT/I");
123 friendTree->Branch("TrackJetKT", TrackJetKT, "TrackJetKT[nJetsKT]/F");
124
125 cout<<"calculating additional variables..."<<endl;
126 for (ev.toBegin(); !ev.atEnd(); ++ev) {
127
128 fwlite::Handle<reco::CaloJetCollection> jets;
129 jets.getByLabel(ev, "antikt5CaloJets");
130
131 fwlite::Handle<reco::TrackCollection> tracks;
132 tracks.getByLabel(ev, "generalTracks");
133
134 if (!jets.isValid())
135 continue;
136 if (!tracks.isValid())
137 continue;
138 double trackSum;
139 nJetsKT = 0;
140 for (reco::CaloJetCollection::const_iterator jet = jets->begin(); jet
141 !=jets->end(); jet++) {
142 trackSum = 0;
143 for (reco::TrackCollection::const_iterator track = tracks->begin(); track
144 !=tracks->end(); track++) {
145 if (deltaR(jet->eta(), jet->phi(), track->eta(), track->phi())
146 < 0.5)
147 trackSum += track->pt();
148 }
149 TrackJetKT[nJetsKT] = trackSum;
150 nJetsKT++;
151 }
152 friendTree->Fill();
153 }
154 f->cd();
155 friendTree->Write();
156 f->Close();
157 ((TChain*) this->At(currChain))->AddFriend(friendTreeName.c_str(),
158 friendFileName.c_str());
159 }
160 return this->GetEntries();
161
162 }
163 //------------------------------------------------------------------
164 //Draw given Chain with given variable and given cuts
165 int PlotTool::plot(int chainIndex, string histName, string cutName,
166 int nEntries, string drwOpt) {
167
168 if (chainIndex >= this->GetEntries() )
169 return -1;
170
171 int currN = nEntries;
172 if (nEntries < 0)
173 currN = ((TChain*) this->At(chainIndex))->GetEntries(); //nEntries<0 : all entries are plotted!
174
175 //++++ Create and name Canvases according to global variables +++++++++++++
176 ostringstream currHistName;
177 if (samePad_trees)
178 currHistName<<((TChain*) this->At(chainIndex))->GetName()<<":";
179 if (samePad_vars)
180 currHistName<<histName;
181 if (samePad_cuts)
182 currHistName<<"{"<<cutName<<"}";
183
184 ostringstream currPadName;
185 if (!samePad_trees)
186 currPadName<<((TChain*) this->At(chainIndex))->GetName()<<":";
187 if (!samePad_vars)
188 currPadName<<histName;
189 if (!samePad_cuts)
190 currPadName<<"{"<<cutName<<"}";
191
192 ostringstream currCanvName;
193 if (!sameCanv_trees && !samePad_trees)
194 currCanvName<<((TChain*) this->At(chainIndex))->GetName()<<":";
195 if (!sameCanv_vars && !samePad_vars)
196 currCanvName<<histName;
197 if (!sameCanv_cuts && !samePad_cuts)
198 currCanvName<<"{"<<cutName<<"}";
199
200 if ( (sameCanv_trees || samePad_trees) && (sameCanv_vars || samePad_vars)
201 && (sameCanv_cuts || samePad_cuts))
202 currCanvName<<"All";
203
204 string currOpt = drwOpt;
205 bool useSubPads = (sameCanv_trees && !samePad_trees) || (sameCanv_vars
206 && !samePad_vars) || (sameCanv_cuts && !samePad_cuts);
207
208 if (useSubPads) {
209 if ( !canvases_[currCanvName.str()] || !gROOT->GetListOfCanvases()->FindObject(currCanvName.str().c_str()) ) {
210 canvases_[currCanvName.str()] = new TCanvas(currCanvName.str().c_str(), currCanvName.str().c_str());
211 } else if (canvases_[currCanvName.str()]->GetNumber() >= 0) {
212 canvases_[currCanvName.str()]->SetNumber(canvases_[currCanvName.str()]->GetNumber()+1);
213 }
214 }
215
216 if ( !pads_[currPadName.str()] || !gROOT->GetListOfCanvases()->FindObject(currPadName.str().c_str()) ) {
217 pads_[currPadName.str()] = new TCanvas(currPadName.str().c_str(), currPadName.str().c_str());
218 if (logY)
219 pads_[currPadName.str()]->SetLogy(1);
220 } else {
221 pads_[currPadName.str()]->cd();
222 currOpt += "SAMES";
223 if (useSubPads)
224 canvases_[currCanvName.str()]->SetNumber(canvases_[currCanvName.str()]->GetNumber()-1);
225 }
226 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
227
228
229 //Draw histogram:
230 int draw_err = ((TChain*) this->At(chainIndex))->Draw(histName.c_str(), cutName.c_str(),
231 currOpt.c_str(), currN);
232 if (draw_err < 0)
233 return draw_err;
234
235 //++++ Fix for histos with no entries +++++++++++++
236 if (draw_err == 0) {
237 if (currOpt.find("SAMES") == string::npos) {
238 pads_[currPadName.str()]->Close();
239 pads_.erase(currPadName.str() );
240 if (useSubPads)
241 canvases_[currCanvName.str()]->SetNumber(canvases_[currCanvName.str()]->GetNumber()-1);
242 }
243 cout<< "Warning: "<<currHistName.str().c_str()<<" in "<<currPadName.str()
244 <<" has no entries and is not drawn!"<<endl;
245 return draw_err;
246 }
247 //++++++++++++++++++++++++++++++++++++++++++++++++++
248
249 ((TH1F*) pads_[currPadName.str()]->GetPrimitive("htemp"))->SetName(currHistName.str().c_str()); // Set Name of histogram
250
251
252 return draw_err;
253
254 }
255 //------------------------------------------------------------------
256 //Standard loop to draw all chains and multiple variables and cuts
257 int PlotTool::loop(vector<string> _histName, vector<string> _cutName,
258 int nEntries, string drwOpt, bool correspond) {
259
260 int numProcessed = 0;
261
262 if (correspond == false) {
263
264 for (int i=0; i<this->GetEntries(); ++i) {
265 for (vector<string>::iterator it1 =_histName.begin(); it1
266 != _histName.end(); ++it1) {
267 for (vector<string>::iterator it2 =_cutName.begin(); it2
268 != _cutName.end(); ++it2) {
269
270 numProcessed += plot(i, *it1, *it2, nEntries, drwOpt);
271 updatePads();
272
273 }
274 }
275 }
276 } else {
277
278 if (_histName.size() != _cutName.size() )
279 return -1;
280
281 for (int i=0; i<this->GetEntries(); ++i) {
282 for (vector<string>::iterator it1 =_histName.begin(), it2 =
283 _cutName.begin(); it1 != _cutName.end()&&it2
284 != _cutName.end(); ++it1, ++it2) {
285
286 numProcessed += plot(i, *it1, *it2, nEntries, drwOpt);
287
288 }
289 }
290 }
291
292 // updatePads();
293 fillCanvases();
294
295 return numProcessed;
296 }
297
298 //Helper methods to allow for using simple strings as input
299 int PlotTool::loop(string histName, string cutName, int nEntries, string drwOpt) {
300
301 vector<string> _histName;
302 _histName.push_back(histName);
303 vector<string> _cutName;
304 _cutName.push_back(cutName);
305
306 return loop(_histName, _cutName, nEntries, drwOpt);
307
308 }
309
310 int PlotTool::loop(vector<string> _histName, string cutName, int nEntries,
311 string drwOpt) {
312
313 vector<string> _cutName;
314 _cutName.push_back(cutName);
315
316 return loop(_histName, _cutName, nEntries, drwOpt);
317
318 }
319
320 int PlotTool::loop(string histName, vector<string> _cutName, int nEntries,
321 string drwOpt) {
322
323 vector<string> _histName;
324 _histName.push_back(histName);
325
326 return loop(_histName, _cutName, nEntries, drwOpt);
327 }
328 //------------------------------------------------------------------
329 //redraw the canvas to make changes in style visible
330 int PlotTool::updatePads() {
331
332 for (map< string, TCanvas* >::iterator it=pads_.begin() ; it != pads_.end(); ++it) {
333 if (gROOT->GetListOfCanvases()->FindObject((*it).first.c_str() ) ) {
334 (*it).second->Draw();
335 setCanvas((*it).second);
336 } else
337 pads_.erase(it);
338 }
339
340 return pads_.size();
341
342 }
343
344 int PlotTool::fillCanvases() {
345
346 for (map< string, TCanvas* >::iterator it=canvases_.begin() ; it
347 != canvases_.end(); ++it) {
348 const char* canvName = (*it).first.c_str();
349 if (gROOT->GetListOfCanvases()->FindObject(canvName) ) {
350
351 int numP = (*it).second->GetNumber()+1;
352
353 if (numP <= 0)
354 continue;
355
356 int x = int( sqrt(numP) );
357 int y = x;
358 if (x*y < numP)
359 x += 1;
360 if (x*y < numP)
361 x += 1;
362 if (x*y < numP) {
363 x -= 1;
364 y += 1;
365 }
366
367 (*it).second->Divide(x, y);
368 int padIndex = 1;
369 for (map< string, TCanvas* >::iterator it2=pads_.begin() ; it2
370 != pads_.end(); ++it2) {
371 string padName = (*it2).first;
372 if (gROOT->GetListOfCanvases()->FindObject(padName.c_str() ) ) {
373 if ( (padName.find(canvName) != string::npos ) || !strcmp(
374 canvName, "All") ) {
375 (*it).second->cd(padIndex);
376 (*it2).second->DrawClonePad();
377 (*it2).second->Close();
378 pads_.erase(it2);
379 ++padIndex;
380 }
381 } else
382 pads_.erase(it2);
383 }
384 (*it).second->SetNumber(-1);
385
386 } else
387 canvases_.erase(it);
388 }
389
390 return canvases_.size();
391
392 }
393 //------------------------------------------------------------------
394 void PlotTool::setCanvas(TCanvas* thisCanvas) {
395
396 TH1* thisHist = 0;
397 TPaveStats* thisStatsBox = 0;
398 TPaletteAxis* palette =0;
399 TLegend* thisLeg = 0;
400 int counter =0;
401 double maxEntry=0;
402
403 ((TFrame*) thisCanvas->GetFrame())->Delete();
404 thisCanvas->GetFrame()->SetLineWidth(gStyle->GetLineWidth() );
405
406 thisCanvas->SetLeftMargin(0.2);
407 thisCanvas->SetRightMargin(0.06);
408 thisCanvas->SetBottomMargin(0.2);
409 thisCanvas->SetTopMargin(0.1);
410
411 if (logY)
412 thisCanvas->SetLogy(1);
413 else
414 thisCanvas->SetLogy(0);
415
416 if (showLegend)
417 thisLeg = new TLegend();
418
419 for (int i = 0; i != thisCanvas->GetListOfPrimitives()->GetSize(); ++i) {
420
421 if ( !thisCanvas->GetListOfPrimitives()->At(i)->InheritsFrom("TH1"))
422 continue;
423
424 thisHist = ((TH1*) thisCanvas->GetListOfPrimitives()->At(i));
425 setColor(thisHist, counter);
426 setMathLabels(thisHist);
427
428 if (thisHist->GetMaximum() > maxEntry)
429 maxEntry = thisHist->GetMaximum();
430
431 thisStatsBox = (TPaveStats*) thisHist->GetListOfFunctions()->FindObject("stats");
432 if (thisStatsBox)
433 setStats(thisCanvas, thisStatsBox, thisHist, counter);
434
435 palette = (TPaletteAxis*) thisHist->GetListOfFunctions()->FindObject("palette");
436 if (palette)
437 setPalette(thisCanvas, palette);
438
439 if (thisLeg)
440 thisLeg->AddEntry(thisHist,thisHist->GetName())->SetTextSize(0.04);
441
442 ++counter;
443
444 }
445
446 if (maxEntry != 0)
447 setHistMax(thisCanvas, maxEntry);
448
449 thisCanvas->cd();
450 thisCanvas->Update();
451
452 if (thisLeg)
453 setLegend(thisCanvas, thisLeg, counter);
454
455 }
456 //------------------------------------------------------------------
457 //private helper classes to set the canvas and hist style
458 void PlotTool::setStats(TCanvas* thisCanvas, TPaveStats* thisStatsBox,
459 TH1* thisHist, int counter) {
460
461 if (thisCanvas->GetRightMargin() < .2)
462 thisCanvas->SetRightMargin(.2);
463
464 thisStatsBox->SetLineColor(thisHist->GetLineColor());
465 thisStatsBox->SetX2NDC(1.);
466 thisStatsBox->SetY2NDC(1-thisCanvas->GetTopMargin()-0.16*counter);
467 thisStatsBox->SetX1NDC(1-thisCanvas->GetRightMargin()+0.01);
468 thisStatsBox->SetY1NDC(thisStatsBox->GetY2NDC()-.15);
469
470 }
471
472 void PlotTool::setMathLabels(TH1* thisHist) {
473
474 string t = thisHist ->GetTitle();
475 string x = thisHist->GetXaxis()->GetTitle();
476 string y = thisHist->GetYaxis()->GetTitle();
477
478 if (t.find(".phi()") != string::npos)
479 t.replace(t.find(".phi()"), 6, " #phi");
480 if (x.find(".phi()") != string::npos)
481 x.replace(x.find(".phi()"), 6, " #phi");
482 if (y.find(".phi()") != string::npos)
483 y.replace(y.find(".phi()"), 6, " #phi");
484
485 if (t.find(".eta()") != string::npos)
486 t.replace(t.find(".eta()"), 6, " #eta");
487 if (x.find(".eta()") != string::npos)
488 x.replace(x.find(".eta()"), 6, " #eta");
489 if (y.find(".eta()") != string::npos)
490 y.replace(y.find(".eta()"), 6, " #eta");
491
492 if (t.find(".pt()") != string::npos)
493 t.replace(t.find(".pt()"), 5, " p_{T}");
494 if (x.find(".pt()") != string::npos)
495 x.replace(x.find(".pt()"), 5, " p_{T}");
496 if (y.find(".pt()") != string::npos)
497 y.replace(y.find(".pt()"), 5, " p_{T}");
498
499 thisHist ->SetTitle(t.c_str());
500 thisHist->GetXaxis()->SetTitle(x.c_str());
501 thisHist->GetYaxis()->SetTitle(y.c_str());
502
503 }
504
505 void PlotTool::setColor(TH1* thisHist, int counter) {
506
507 if (counter == 0) {
508
509 thisHist->SetLineColor(kRed+1);
510 //if( gStyle->GetHistFillStyle() ) thisHist->SetFillColor (kRed+1);
511 //else
512 thisHist->SetFillStyle(0);
513 thisHist->SetMarkerColor(kRed+1);
514 } else if (counter == 1) {
515 thisHist->SetLineColor(kBlue+1);
516 //if( gStyle->GetHistFillStyle() ) thisHist->SetFillColor (kBlue+1);
517 //else
518 thisHist->SetFillStyle(0);
519 thisHist->SetMarkerColor(kBlue+1);
520
521 } else if (counter == 2) {
522 thisHist->SetLineColor(kGreen+2);
523 //if( gStyle->GetHistFillStyle() ) thisHist->SetFillColor (kGreen+2);
524 //else
525 thisHist->SetFillStyle(0);
526 thisHist->SetMarkerColor(kGreen+2);
527
528 } else if (counter == 3) {
529 thisHist->SetLineColor(kMagenta+2);
530 //if( gStyle->GetHistFillStyle() ) thisHist->SetFillColor (kMagenta+2);
531 //else
532 thisHist->SetFillStyle(0);
533 thisHist->SetMarkerColor(kMagenta+2);
534
535 } else if (counter == 4) {
536 thisHist->SetLineColor(kCyan+2);
537 //if( gStyle->GetHistFillStyle() ) thisHist->SetFillColor (kCyan+2);
538 //else
539 thisHist->SetFillStyle(0);
540 thisHist->SetMarkerColor(kCyan+2);
541
542 } else {
543 thisHist->SetLineColor(kBlack);
544 //if( gStyle->GetHistFillStyle() ) thisHist->SetFillColor (kBlack);
545 //else
546 thisHist->SetFillStyle(0);
547 thisHist->SetMarkerColor(kBlack);
548
549 }
550
551 }
552
553 void PlotTool::setPalette(TCanvas* thisCanvas, TPaletteAxis* palette) {
554
555 palette->SetLabelSize(0.045);
556 if (thisCanvas->GetRightMargin() < .15)
557 thisCanvas->SetRightMargin(.15);
558 palette->SetX1NDC(1-thisCanvas->GetRightMargin()+0.01);
559 palette->SetY1NDC(thisCanvas->GetBottomMargin());
560 palette->SetX2NDC(palette->GetX1NDC()+0.05);
561 palette->SetY2NDC(1-thisCanvas->GetTopMargin());
562
563 }
564
565 void PlotTool::setHistMax(TCanvas* thisCanvas, double maxEntry) {
566
567 TH1* thisHist = 0;
568
569 for (int i = 0; i != thisCanvas->GetListOfPrimitives()->GetSize(); ++i) {
570
571 if ( !thisCanvas->GetListOfPrimitives()->At(i)->InheritsFrom("TH1") )
572 continue;
573 thisHist = ((TH1*) thisCanvas->GetListOfPrimitives()->At(i));
574 if (thisHist->GetMaximum() < maxEntry) {
575 double minEntry=thisHist->GetBinContent(thisHist->GetMinimumBin());
576 if (logY) {
577 int bin = (thisHist->GetMinimumBin()+1);
578 minEntry=thisHist->GetBinContent(bin);
579 }
580 thisHist->GetYaxis()->SetRangeUser(minEntry, maxEntry*1.2);
581 }
582 break;
583
584 }
585
586 }
587
588 void PlotTool::setLegend(TCanvas* thisCanvas, TLegend* thisLeg, int counter) {
589
590 thisLeg->SetFillStyle(0);
591 thisLeg->SetX1NDC(1-thisCanvas->GetRightMargin()-0.5);
592 thisLeg->SetY1NDC(1-thisCanvas->GetTopMargin()-0.05*counter);
593 thisLeg->SetX2NDC(1-thisCanvas->GetRightMargin());
594 thisLeg->SetY2NDC(1-thisCanvas->GetTopMargin());
595 thisLeg->SetEntrySeparation(0.5);
596 thisLeg->Draw("NDC");
597
598 }
599
600 void PlotTool::createColors() {
601 const Int_t NRGBs = 5;
602 const Int_t NCont = 255;
603
604 Double_t stops[NRGBs] = { 0.00, 0.34, 0.61, 0.84, 1.00 };
605 Double_t red[NRGBs] = { 0.00, 0.00, 0.87, 1.00, 0.51 };
606 Double_t green[NRGBs] = { 0.00, 0.81, 1.00, 0.20, 0.00 };
607 Double_t blue[NRGBs] = { 0.51, 1.00, 0.12, 0.00, 0.00 };
608 TColor::CreateGradientColorTable(NRGBs, stops, red, green, blue, NCont);
609 gStyle->SetNumberContours(NCont);
610
611 }
612
613 //------------------------------------------------------------------
614
615 int PlotTool::saveCanvases(string type, string path) {
616
617 TSystemDirectory d("", path.c_str());
618 if (!d.GetListOfFiles())
619 return -1;
620 if (type.find(".") == string::npos)
621 return -1;
622
623 int savedFiles =0;
624
625 TIter next(gROOT->GetListOfCanvases());
626 TCanvas* canv;
627 while ((canv=(TCanvas*)next())) {
628
629 string s = "";
630 s += path;
631 s += canv->GetName();
632 s += type;
633
634 canv->SaveAs(s.c_str());
635 ++savedFiles;
636
637 }
638
639 return savedFiles;
640
641 }
642 //------------------------------------------------------------------
643 void PlotTool::showChainInfo()
644 {
645
646
647 cout<<endl;
648 cout<<"****** Show Chain Information:"<<endl;
649
650
651 this->ls();
652
653 cout<<endl;
654 cout<<"We have "<<this->GetEntries()<<" TChains with following aliases set:"<<endl;
655 for (int i=0; i< this->GetEntries(); ++i) {
656 cout<<endl;
657 cout<<((TChain*) this->At(i))->GetName()<<endl;
658 ((TChain*) this->At(i))->GetListOfAliases()->ls();
659
660 }
661
662
663
664 }