ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/tschum/PlotTool.cc
Revision: 1.1.1.1 (vendor branch)
Committed: Wed Nov 11 15:34:20 2009 UTC (15 years, 5 months ago) by tschum
Content type: text/plain
Branch: FWlite_Analysis, MAIN
CVS Tags: START, HEAD
Changes since 1.1: +0 -0 lines
Log Message:
plotting tool ---first commit---

File Contents

# User Rev Content
1 tschum 1.1 #include "PlotTool.h"
2     //------------------------------------------------------------------
3     //Constructur: Set some global Variables to define canvas output
4     PlotTool::PlotTool()
5     {
6    
7     this->SetClass("TChain",100);
8    
9     samePad_trees = true;
10     samePad_vars = false;
11     samePad_cuts = false;
12     sameCanv_trees = false;
13     sameCanv_vars = false;
14     sameCanv_cuts = false;
15    
16     showLegend = false;
17    
18     }
19    
20     //------------------------------------------------------------------
21     //Fill object PlotTool with Chains constructed from files from given source
22     int PlotTool::init(string fileName, string dirPath, string treeName)
23     {
24     this->New( this->GetEntries() );
25     int currChain = this->GetEntries() - 1;
26    
27     if(currChain < 0 ) return currChain;
28    
29    
30     ((TChain*) this->At(currChain))->SetName(fileName.c_str());
31     TSystemDirectory dir("sourceDir",dirPath.c_str());
32     TList *files = dir.GetListOfFiles();
33     if (files) {
34     TIter next(files);
35     TSystemFile *file;
36     TString fname;
37     string filePath;
38    
39     cout<<"Open"<<dirPath.c_str()<<" Search for .root files that contain: "<<fileName.c_str()<<endl;
40    
41     while ((file=(TSystemFile*)next())) {
42     fname = file->GetName();
43     if (!fname.EndsWith(".root")) continue;
44     if (!fname.Contains(fileName.c_str())) continue;
45    
46     filePath = dirPath;
47     filePath += fname.Data();
48     if( ((TChain*) this->At(currChain))->AddFile(filePath.c_str(), -1, treeName.c_str()) ) cout<<"Chained "<<((TChain*) this->At(currChain))->GetNtrees()<<" file(s) with "<<((TChain*) this->At(currChain))->GetEntries()<<" events."<<endl;
49     else return -1;
50    
51     }
52     } else return -1;
53    
54     for(int i=0;i<((TChain*) this->At(currChain))->GetListOfBranches()->GetEntries();++i){
55    
56     string s(((TChain*) this->At(currChain))->GetListOfBranches()->At(i)->GetName());
57     size_t a = s.find("_");
58     if(a == string::npos) a = 0;
59     size_t b = s.find("_", a+1);
60     if(b == string::npos) b = 50;
61     string branch_alias = s.substr(a+1,b-a-1);
62     string branch_name = s;
63     if(s.find(".",s.size()-1) != string::npos ) branch_name += "obj";
64     ((TChain*) this->At(currChain))->SetAlias(branch_alias.c_str(),branch_name.c_str());
65    
66     }
67    
68     return this->GetEntries();
69    
70     }
71     //------------------------------------------------------------------
72     //Draw given Chain with given variable and given cuts
73     int PlotTool::plot(int chainIndex, string histName, string cutName, int nEntries, string drwOpt)
74     {
75    
76     if( chainIndex >= this->GetEntries() ) return -1;
77    
78     int currN = nEntries;
79     if(nEntries < 0) currN = ((TChain*) this->At(chainIndex))->GetEntries(); //nEntries<0 : all entries are plotted!
80    
81     //++++ Create and name Canvases according to global variables +++++++++++++
82     ostringstream currHistName;
83     if( samePad_trees) currHistName<<((TChain*) this->At(chainIndex))->GetName()<<":";
84     if( samePad_vars) currHistName<<histName;
85     if( samePad_cuts) currHistName<<"{"<<cutName<<"}";
86    
87     ostringstream currPadName;
88     if(! samePad_trees) currPadName<<((TChain*) this->At(chainIndex))->GetName()<<":";
89     if(! samePad_vars) currPadName<<histName;
90     if(! samePad_cuts) currPadName<<"{"<<cutName<<"}";
91    
92     ostringstream currCanvName;
93     if(! sameCanv_trees && ! samePad_trees ) currCanvName<<((TChain*) this->At(chainIndex))->GetName()<<":";
94     if(! sameCanv_vars && ! samePad_vars) currCanvName<<histName;
95     if(! sameCanv_cuts && ! samePad_cuts) currCanvName<<"{"<<cutName<<"}";
96    
97     if( (sameCanv_trees || samePad_trees) && (sameCanv_vars || samePad_vars) && (sameCanv_cuts || samePad_cuts) ) currCanvName<<"All";
98    
99    
100     string currOpt = drwOpt;
101     bool useSubPads = (sameCanv_trees && !samePad_trees) || (sameCanv_vars && !samePad_vars) || (sameCanv_cuts && !samePad_cuts);
102    
103     if( useSubPads ) {
104     if( ! canvases_[currCanvName.str()] || ! gROOT->FindObject(currCanvName.str().c_str()) ) {
105     canvases_[currCanvName.str()] = new TCanvas(currCanvName.str().c_str(), currCanvName.str().c_str());
106     } else if ( canvases_[currCanvName.str()]->GetNumber() >= 0 ) {
107     canvases_[currCanvName.str()]->SetNumber(canvases_[currCanvName.str()]->GetNumber()+1);
108     }
109     }
110    
111     if( ! pads_[currPadName.str()] || ! gROOT->FindObject(currPadName.str().c_str()) ) {
112     pads_[currPadName.str()] = new TCanvas(currPadName.str().c_str(), currPadName.str().c_str());
113     } else {
114     pads_[currPadName.str()]->cd();
115     currOpt += "SAMES";
116     if(useSubPads) canvases_[currCanvName.str()]->SetNumber(canvases_[currCanvName.str()]->GetNumber()-1);
117     }
118     //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
119    
120    
121     //Draw histogram:
122     int draw_err = ((TChain*) this->At(chainIndex))->Draw(histName.c_str(), cutName.c_str(), currOpt.c_str(), currN);
123     if( draw_err < 0 ) return draw_err;
124    
125     //++++ Fix for histos with no entries +++++++++++++
126     if( draw_err == 0 ) {
127     if( currOpt.find("SAMES") == string::npos ) {
128     pads_[currPadName.str()]->Close();
129     pads_.erase( currPadName.str() );
130     if(useSubPads) canvases_[currCanvName.str()]->SetNumber(canvases_[currCanvName.str()]->GetNumber()-1);
131     }
132     cout<< "Warning: "<<currHistName.str().c_str()<<" in "<<currPadName.str()<<" has no entries and is not drawn!"<<endl;
133     return draw_err;
134     }
135     //++++++++++++++++++++++++++++++++++++++++++++++++++
136    
137     ((TH1F*) pads_[currPadName.str()]->GetPrimitive("htemp"))->SetName(currHistName.str().c_str()); // Set Name of histogram
138    
139    
140     return draw_err;
141    
142     }
143     //------------------------------------------------------------------
144     //Standard loop to draw all chains and multiple variables and cuts
145     int PlotTool::loop(vector<string> _histName, vector<string> _cutName, int nEntries, string drwOpt, bool correspond)
146     {
147    
148    
149    
150     int numProcessed = 0;
151    
152     if( correspond == false ) {
153    
154     for(int i=0; i<this->GetEntries(); ++i) {
155     for(vector<string>::iterator it1 =_histName.begin(); it1 != _histName.end(); ++it1) {
156     for(vector<string>::iterator it2 =_cutName.begin(); it2 != _cutName.end(); ++it2) {
157    
158     numProcessed += plot(i, *it1, *it2, nEntries, drwOpt);
159    
160     }
161     }
162     }
163     } else {
164    
165     if( _histName.size() != _cutName.size() ) return -1;
166    
167     for(int i=0; i<this->GetEntries(); ++i) {
168     for(vector<string>::iterator it1 =_histName.begin(),it2 =_cutName.begin(); it1 != _cutName.end()&&it2 != _cutName.end(); ++it1, ++it2) {
169    
170     numProcessed += plot(i, *it1, *it2, nEntries, drwOpt);
171    
172    
173     }
174     }
175     }
176    
177     updatePads();
178     fillCanvases();
179    
180     return numProcessed;
181     }
182    
183     //Helper methods to allow for using simple strings as input
184     int PlotTool::loop(string histName, string cutName, int nEntries, string drwOpt)
185     {
186    
187     vector<string> _histName;
188     _histName.push_back( histName );
189     vector<string> _cutName;
190     _cutName.push_back( cutName );
191    
192     return loop(_histName, _cutName, nEntries, drwOpt);
193    
194     }
195    
196     int PlotTool::loop(vector<string> _histName, string cutName, int nEntries, string drwOpt)
197     {
198    
199     vector<string> _cutName;
200     _cutName.push_back( cutName );
201    
202     return loop(_histName, _cutName, nEntries, drwOpt);
203    
204     }
205    
206     int PlotTool::loop(string histName, vector<string> _cutName, int nEntries, string drwOpt)
207     {
208    
209     vector<string> _histName;
210     _histName.push_back( histName );
211    
212     return loop(_histName, _cutName, nEntries, drwOpt);
213     }
214     //------------------------------------------------------------------
215     //redraw the canvas to make changes in style visible
216     int PlotTool::updatePads() {
217    
218     for(map< string, TCanvas* >::iterator it=pads_.begin() ; it != pads_.end(); ++it ) {
219     if( gROOT->FindObject( (*it).first.c_str() ) ) {
220     (*it).second->Draw();
221     setCanvas( (*it).second );
222     } else pads_.erase( it );
223     }
224    
225     return pads_.size();
226    
227     }
228    
229     int PlotTool::fillCanvases() {
230    
231    
232     for(map< string, TCanvas* >::iterator it=canvases_.begin() ; it != canvases_.end(); ++it ) {
233     const char* canvName = (*it).first.c_str();
234     if( gROOT->FindObject( canvName ) ) {
235    
236     int numP = (*it).second->GetNumber()+1;
237    
238     if( numP <= 0 ) continue;
239    
240     int x = int( sqrt(numP) );
241     int y = x;
242     if( x*y < numP ) x += 1;
243     if( x*y < numP ) x += 1;
244     if( x*y < numP ) {
245     x -= 1;
246     y += 1;
247     }
248    
249     (*it).second->Divide(x,y);
250     int padIndex = 1;
251     for(map< string, TCanvas* >::iterator it2=pads_.begin() ; it2 != pads_.end(); ++it2 ) {
252     string padName = (*it2).first;
253     if( gROOT->FindObject( padName.c_str() ) ) {
254     if( ( padName.find(canvName) != string::npos ) || !strcmp(canvName,"All") ) {
255     (*it).second->cd(padIndex);
256     (*it2).second->DrawClonePad();
257     (*it2).second->Close();
258     pads_.erase( it2 );
259     ++padIndex;
260     }
261     } else pads_.erase( it2 );
262     }
263     (*it).second->SetNumber(-1);
264    
265     } else canvases_.erase( it );
266     }
267    
268     return canvases_.size();
269    
270     }
271     //------------------------------------------------------------------
272     void PlotTool::setCanvas(TCanvas* thisCanvas) {
273    
274    
275     TH1* thisHist = 0;
276     TPaveStats* thisStatsBox = 0;
277     TPaletteAxis* palette =0;
278     TLegend* thisLeg = 0;
279     int counter =0;
280     double maxEntry=0;
281    
282    
283     ((TFrame*) thisCanvas->GetFrame())->Delete();
284     thisCanvas->GetFrame()->SetLineWidth( gStyle->GetLineWidth() );
285    
286     thisCanvas->SetLeftMargin(0.2);
287     thisCanvas->SetRightMargin(0.06);
288     thisCanvas->SetBottomMargin(0.2);
289     thisCanvas->SetTopMargin(0.1);
290    
291    
292     if(showLegend) thisLeg = new TLegend();
293    
294    
295     for(int i = 0; i != thisCanvas->GetListOfPrimitives()->GetSize(); ++i){
296    
297    
298     if( ! thisCanvas->GetListOfPrimitives()->At(i)->InheritsFrom("TH1")) continue;
299    
300     thisHist = ((TH1*) thisCanvas->GetListOfPrimitives()->At(i));
301     setColor(thisHist, counter);
302     setMathLabels(thisHist);
303    
304     if(thisHist->GetMaximum() > maxEntry) maxEntry = thisHist->GetMaximum();
305    
306     thisStatsBox = (TPaveStats*) thisHist->GetListOfFunctions()->FindObject("stats");
307     if(thisStatsBox) setStats(thisCanvas, thisStatsBox, thisHist, counter);
308    
309     palette = (TPaletteAxis*) thisHist->GetListOfFunctions()->FindObject("palette");
310     if(palette) setPalette(thisCanvas, palette);
311    
312     if(thisLeg) thisLeg->AddEntry(thisHist,thisHist->GetName())->SetTextSize(0.04);
313    
314    
315     ++counter;
316    
317     }
318    
319     if( maxEntry != 0) setHistMax(thisCanvas, maxEntry);
320    
321    
322     thisCanvas->cd();
323     thisCanvas->Update();
324    
325     if(thisLeg) setLegend( thisCanvas, thisLeg, counter);
326    
327    
328     }
329     //------------------------------------------------------------------
330     //private helper classes to set the canvas and hist style
331     void PlotTool::setStats(TCanvas* thisCanvas, TPaveStats* thisStatsBox, TH1* thisHist, int counter) {
332    
333    
334     if(thisCanvas->GetRightMargin() < .2) thisCanvas->SetRightMargin(.2);
335    
336     thisStatsBox->SetLineColor(thisHist->GetLineColor());
337     thisStatsBox->SetX2NDC(1.);
338     thisStatsBox->SetY2NDC(1-thisCanvas->GetTopMargin()-0.16*counter);
339     thisStatsBox->SetX1NDC(1-thisCanvas->GetRightMargin()+0.01);
340     thisStatsBox->SetY1NDC(thisStatsBox->GetY2NDC()-.15);
341    
342    
343    
344     }
345    
346     void PlotTool::setMathLabels(TH1* thisHist) {
347    
348    
349     string t = thisHist ->GetTitle();
350     string x = thisHist->GetXaxis()->GetTitle();
351     string y = thisHist->GetYaxis()->GetTitle();
352    
353     if(t.find(".phi()") != string::npos ) t.replace(t.find(".phi()"),6," #phi");
354     if(x.find(".phi()") != string::npos ) x.replace(x.find(".phi()"),6," #phi");
355     if(y.find(".phi()") != string::npos ) y.replace(y.find(".phi()"),6," #phi");
356    
357     if(t.find(".eta()") != string::npos ) t.replace(t.find(".eta()"),6," #eta");
358     if(x.find(".eta()") != string::npos ) x.replace(x.find(".eta()"),6," #eta");
359     if(y.find(".eta()") != string::npos ) y.replace(y.find(".eta()"),6," #eta");
360    
361     if(t.find(".pt()") != string::npos ) t.replace(t.find(".pt()"),5," p_{T}");
362     if(x.find(".pt()") != string::npos ) x.replace(x.find(".pt()"),5," p_{T}");
363     if(y.find(".pt()") != string::npos ) y.replace(y.find(".pt()"),5," p_{T}");
364    
365     thisHist ->SetTitle(t.c_str());
366     thisHist->GetXaxis()->SetTitle(x.c_str());
367     thisHist->GetYaxis()->SetTitle(y.c_str());
368    
369    
370     }
371    
372    
373     void PlotTool::setColor(TH1* thisHist, int counter) {
374    
375    
376     if(counter == 0) {
377    
378     thisHist->SetLineColor (kRed+1);
379     if( gStyle->GetHistFillStyle() ) thisHist->SetFillColor (kRed+1);
380     else thisHist->SetFillStyle(0);
381     thisHist->SetMarkerColor(kRed+1);
382     } else if(counter == 1) {
383     thisHist->SetLineColor (kBlue+1);
384     if( gStyle->GetHistFillStyle() ) thisHist->SetFillColor (kBlue+1);
385     else thisHist->SetFillStyle(0);
386     thisHist->SetMarkerColor(kBlue+1);
387    
388     } else if(counter == 2) {
389     thisHist->SetLineColor (kGreen+2);
390     if( gStyle->GetHistFillStyle() ) thisHist->SetFillColor (kGreen+2);
391     else thisHist->SetFillStyle(0);
392     thisHist->SetMarkerColor(kGreen+2);
393    
394     } else if(counter == 3) {
395     thisHist->SetLineColor (kMagenta+2);
396     if( gStyle->GetHistFillStyle() ) thisHist->SetFillColor (kMagenta+2);
397     else thisHist->SetFillStyle(0);
398     thisHist->SetMarkerColor(kMagenta+2);
399    
400     } else if(counter == 4) {
401     thisHist->SetLineColor (kCyan+2);
402     if( gStyle->GetHistFillStyle() ) thisHist->SetFillColor (kCyan+2);
403     else thisHist->SetFillStyle(0);
404     thisHist->SetMarkerColor(kCyan+2);
405    
406     } else {
407     thisHist->SetLineColor (kBlack);
408     if( gStyle->GetHistFillStyle() ) thisHist->SetFillColor (kBlack);
409     else thisHist->SetFillStyle(0);
410     thisHist->SetMarkerColor(kBlack);
411    
412     }
413    
414     }
415    
416     void PlotTool::setPalette(TCanvas* thisCanvas, TPaletteAxis* palette) {
417    
418    
419     palette->SetLabelSize(0.045);
420     if(thisCanvas->GetRightMargin() < .15) thisCanvas->SetRightMargin(.15);
421     palette->SetX1NDC(1-thisCanvas->GetRightMargin()+0.01);
422     palette->SetY1NDC(thisCanvas->GetBottomMargin());
423     palette->SetX2NDC(palette->GetX1NDC()+0.05);
424     palette->SetY2NDC(1-thisCanvas->GetTopMargin());
425    
426    
427     }
428    
429     void PlotTool::setHistMax(TCanvas* thisCanvas, double maxEntry) {
430    
431     TH1* thisHist = 0;
432    
433     for(int i = 0; i != thisCanvas->GetListOfPrimitives()->GetSize(); ++i){
434    
435     if( ! thisCanvas->GetListOfPrimitives()->At(i)->InheritsFrom("TH1") ) continue;
436     thisHist = ((TH1*) thisCanvas->GetListOfPrimitives()->At(i));
437     if( thisHist->GetMaximum() < maxEntry ) thisHist->GetYaxis()->SetRangeUser(thisHist->GetMinimum(),maxEntry*1.2);
438     break;
439    
440     }
441    
442    
443     }
444    
445     void PlotTool::setLegend(TCanvas* thisCanvas, TLegend* thisLeg, int counter) {
446    
447     thisLeg->SetFillStyle(0);
448     thisLeg->SetX1NDC(1-thisCanvas->GetRightMargin()-0.5);
449     thisLeg->SetY1NDC(1-thisCanvas->GetTopMargin()-0.05*counter);
450     thisLeg->SetX2NDC(1-thisCanvas->GetRightMargin());
451     thisLeg->SetY2NDC(1-thisCanvas->GetTopMargin());
452     thisLeg->SetEntrySeparation(0.5);
453     thisLeg->Draw("NDC");
454    
455     }
456    
457     void PlotTool::createColors()
458     {
459     const Int_t NRGBs = 5;
460     const Int_t NCont = 255;
461    
462     Double_t stops[NRGBs] = { 0.00, 0.34, 0.61, 0.84, 1.00 };
463     Double_t red[NRGBs] = { 0.00, 0.00, 0.87, 1.00, 0.51 };
464     Double_t green[NRGBs] = { 0.00, 0.81, 1.00, 0.20, 0.00 };
465     Double_t blue[NRGBs] = { 0.51, 1.00, 0.12, 0.00, 0.00 };
466     TColor::CreateGradientColorTable(NRGBs, stops, red, green, blue, NCont);
467     gStyle->SetNumberContours(NCont);
468    
469     }
470    
471    
472     //------------------------------------------------------------------
473    
474     int PlotTool::saveCanvases(string type, string path) {
475    
476    
477     TSystemDirectory d("",path.c_str());
478     if(!d.GetListOfFiles()) return -1;
479     if(type.find(".") == string::npos) return -1;
480    
481     int savedFiles =0;
482    
483     TIter next(gROOT->GetListOfCanvases());
484     TCanvas* canv;
485     while ((canv=(TCanvas*)next())) {
486    
487     string s = "";
488     s += path;
489     s += canv->GetName();
490     s += type;
491    
492     canv->SaveAs(s.c_str());
493     ++savedFiles;
494    
495     }
496    
497     return savedFiles;
498    
499     }
500     //------------------------------------------------------------------