ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/tschum/FWlite_Analysis/PlotTool.cc
Revision: 1.19
Committed: Fri Feb 12 15:21:17 2010 UTC (15 years, 2 months ago) by tschum
Content type: text/plain
Branch: MAIN
Changes since 1.18: +48 -18 lines
Log Message:
improved jetContent vars, plot utils

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