ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/tschum/FWlite_Analysis/PlotTool.cc
Revision: 1.5
Committed: Fri Nov 20 10:52:48 2009 UTC (15 years, 5 months ago) by thomsen
Content type: text/plain
Branch: MAIN
Changes since 1.4: +17 -17 lines
Log Message:
bugfix in friend tree

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