ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/tschum/FWlite_Analysis/PlotTool.cc
Revision: 1.18
Committed: Wed Feb 10 11:41:57 2010 UTC (15 years, 3 months ago) by tschum
Content type: text/plain
Branch: MAIN
Changes since 1.17: +25 -639 lines
Log Message:
outsourced calculation of extra variables

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