ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/tschum/FWlite_Analysis/PlotTool.cc
Revision: 1.8
Committed: Wed Dec 2 15:57:44 2009 UTC (15 years, 5 months ago) by tschum
Content type: text/plain
Branch: MAIN
Changes since 1.7: +202 -11 lines
Log Message:
add time info, event info in start.C

File Contents

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