ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/tschum/FWlite_Analysis/PlotTool.cc
Revision: 1.15
Committed: Wed Dec 16 16:33:28 2009 UTC (15 years, 4 months ago) by thomsen
Content type: text/plain
Branch: MAIN
Changes since 1.14: +169 -36 lines
Log Message:
Trigger & TrackDetE

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 thomsen 1.15 addTrackJets = true;
18     addHitDetInfo = true;
19     diTrackMass = true;
20     addTrigger = true;
21 tschum 1.11 addTrackJets = false;
22 tschum 1.13 addEventInfo = false;
23 tschum 1.14 addTower = false;
24 tschum 1.8 verbose = true;
25 tschum 1.1
26 tschum 1.12 globalCuts="";
27    
28 tschum 1.14 recreateTree = false; //force recreation of friend tree
29    
30 tschum 1.1 }
31    
32     //------------------------------------------------------------------
33     //Fill object PlotTool with Chains constructed from files from given source
34 tschum 1.7
35 gebbert 1.6 int PlotTool::init(string fileName, string dirPath, string treeName,
36 tschum 1.13 string fileLabel) {
37     this->New(this->GetEntries() );
38     int currChain = this->GetEntries() - 1;
39 gebbert 1.6
40 tschum 1.13 if (currChain < 0)
41     return currChain;
42 gebbert 1.6
43 tschum 1.13 TStopwatch timer;
44     timer.Start();
45    
46    
47     fileNames.clear();
48    
49     //check if alternative label is different from default(searchString)
50     if (fileLabel=="") {
51     fileLabel=fileName;
52     }
53     ((TChain*) this->At(currChain))->SetName(fileLabel.c_str());
54    
55     TList *files = new TList();
56     TSystemFile* sysFile = 0;
57     if(fileName.find(".") != string::npos) {
58     ifstream f(fileName.c_str());
59     if( ! f.is_open() ) return -1;
60     string line;
61    
62     while (!f.eof()) {
63     getline(f,line);
64     sysFile = new TSystemFile(line.c_str(),dirPath.c_str());
65     files->Add(sysFile);
66     }
67 tschum 1.8
68    
69 tschum 1.13 } else {
70     TSystemDirectory dir("sourceDir", dirPath.c_str());
71     files = dir.GetListOfFiles();
72     }
73     if (files->GetEntries()>0) {
74     TIter next(files);
75     TSystemFile *file;
76     TString fname;
77     string filePath;
78    
79     if(verbose && fileName.find(".") == string::npos ) cout<<"Open"<<dirPath.c_str()<<" Search for .root files that contain: "
80     <<fileName.c_str()<<endl;
81     if(verbose && fileName.find(".") != string::npos ) cout<<"Open"<<dirPath.c_str()<<" Search lines with .root in: "
82     <<fileName.c_str()<<endl;
83    
84    
85     while ((file=(TSystemFile*)next())) {
86     fname = file->GetName();
87     if (!fname.EndsWith(".root"))
88     continue;
89     if (!fname.Contains(fileName.c_str()) && fileName.find(".") == string::npos )
90     continue;
91    
92     filePath = dirPath;
93     filePath += fname.Data();
94     //fwlite::ChainEvent to lop over events, jets, etc
95     fileNames.push_back(filePath.c_str());
96    
97     if (((TChain*) this->At(currChain))->AddFile(filePath.c_str(), -1,
98     treeName.c_str()) )
99     if(verbose) cout<<"Chained "<<((TChain*) this->At(currChain))->GetNtrees()<<" file(s) with "<<((TChain*) this->At(currChain))->GetEntries()<<" events."<<endl;
100     else
101     return -1;
102 gebbert 1.6
103 tschum 1.13 }
104     } else
105     return -1;
106    
107     for (int i=0; i<((TChain*) this->At(currChain))->GetListOfBranches()->GetEntries(); ++i) {
108    
109     string s(((TChain*) this->At(currChain))->GetListOfBranches()->At(i)->GetName());
110    
111     string branch_alias = s;
112     string branch_name = s;
113     if (s.find(".", s.size()-1) != string::npos)
114     branch_name += "obj";
115    
116     size_t a = s.find("_");
117     if (a != string::npos) {
118     size_t b = s.find("_", a+1);
119     if (b != string::npos) {
120     size_t c = s.find("_", b+1);
121     if (c != string::npos) {
122     string _prod =s.substr(0,a);
123     string _label =s.substr(a+1, b-a-1);
124     string _instance =s.substr(b+1, c-b-1);
125     branch_alias = _label;
126     if(_instance.length() > 0 ) {
127     branch_alias += "_";
128     branch_alias += _instance;
129     ((TChain*) this->At(currChain))->SetAlias(_instance.c_str(),
130     branch_name.c_str());
131     }
132     string branch_alias_full = _prod + "_" + branch_alias;
133     ((TChain*) this->At(currChain))->SetAlias(branch_alias_full.c_str(),
134     branch_name.c_str());
135 gebbert 1.6 }
136 tschum 1.13 }
137     }
138    
139     ((TChain*) this->At(currChain))->SetAlias(branch_alias.c_str(),
140     branch_name.c_str());
141    
142     }
143    
144 thomsen 1.15 if( addEventInfo || addTrackJets || addTower || addTrackJets || addHitDetInfo || diTrackMass || addTrigger) {
145 tschum 1.10
146 tschum 1.14 if(verbose) cout<<"add friend tree with additional variables..."<<endl;
147 tschum 1.10
148 tschum 1.13 //make file for tree friends (adding additional, computed branches)
149     string friendFileName("/scratch/hh/current/cms/user/");
150     friendFileName += gSystem->GetUserInfo()->fUser;
151 tschum 1.14 friendFileName += "/temp/";
152     friendFileName += fileLabel;
153 tschum 1.13 friendFileName += ".root";
154 tschum 1.14 string fileOpt = "update";
155     if( recreateTree ) fileOpt = "recreate";
156    
157     TFile *f = new TFile(friendFileName.c_str(),fileOpt.c_str());
158 tschum 1.13
159     if( f->IsZombie() ) return -1;
160    
161     string friendTreeName("friendTree");
162     //char number = currChain;
163     //friendTreeName += number;
164     //TTree *friendTree = new TTree("friendTree","friendTree");
165    
166 tschum 1.14 if(! f->FindKey(friendTreeName.c_str())) {
167     if(verbose) cout<<"calculating additional variables..."<<endl;
168    
169    
170     TTree *friendTree = new TTree(friendTreeName.c_str(),friendTreeName.c_str());
171     fwlite::ChainEvent ev(fileNames);
172     int _event, _run, _lumi;
173    
174     //tower data
175     const int kMAX = 10000;
176     int NobjTowCal;
177     towet = new float [ kMAX ];
178     toweta = new float [ kMAX ];
179     towphi = new float [ kMAX ];
180     towen = new float [ kMAX ];
181     towem = new float [ kMAX ];
182     towhd = new float [ kMAX ];
183     towoe = new float [ kMAX ];
184     towid_phi = new int[ kMAX ];
185     towid_eta = new int[ kMAX ];
186     towid = new int[ kMAX ];
187    
188 thomsen 1.15 int nJetsKT;
189     TrackJetKT = new float [100];
190     int nTracks;
191     TrackDetE = new float [3000];
192     TrackDetEECAL = new float [3000];
193     float DiTrackMass;
194     bool Trigger;
195     bool Trigger1;
196    
197    
198    
199 tschum 1.14 if( addEventInfo ) {
200 tschum 1.13
201    
202 tschum 1.14 friendTree->Branch("event", &_event, "event/I");
203     friendTree->Branch("run", &_run, "run/I");
204     friendTree->Branch("lumi", &_lumi, "lumi/I");
205    
206     }
207    
208     if( addTrackJets ) {
209    
210    
211     friendTree->Branch("nJetsKT", &nJetsKT, "nJetsKT/I");
212     friendTree->Branch("TrackJetKT", TrackJetKT, "TrackJetKT[nJetsKT]/F");
213     }
214    
215     if( addTower) {
216    
217     // CaloTower branches
218     friendTree->Branch( "NobjTowCal",&NobjTowCal,"NobjTowCal/I" );
219     friendTree->Branch( "TowId", towid, "TowId[NobjTowCal]/I" );
220     friendTree->Branch( "TowId_phi", towid_phi, "TowId_phi[NobjTowCal]/I" );
221     friendTree->Branch( "TowId_eta", towid_eta, "TowId_eta[NobjTowCal]/I" );
222     friendTree->Branch( "TowEt", towet, "TowEt[NobjTowCal]/F" );
223     friendTree->Branch( "TowEta", toweta, "TowEta[NobjTowCal]/F" );
224     friendTree->Branch( "TowPhi", towphi, "TowPhi[NobjTowCal]/F" );
225     friendTree->Branch( "TowE", towen, "TowE[NobjTowCal]/F" );
226     friendTree->Branch( "TowEm", towem, "TowEm[NobjTowCal]/F" );
227     friendTree->Branch( "TowHad", towhd, "TowHad[NobjTowCal]/F" );
228     friendTree ->Branch( "TowOE", towoe, "TowOE[NobjTowCal]/F" );
229    
230    
231     }
232    
233 thomsen 1.15 if( addTrackJets){
234     friendTree->Branch("nJetsKT", &nJetsKT, "nJetsKT/I");
235     friendTree->Branch("TrackJetKT", TrackJetKT, "TrackJetKT[nJetsKT]/F");
236     }
237    
238     if( addHitDetInfo){
239     friendTree->Branch("nTracks", &nTracks, "nTracks/I");
240     friendTree->Branch("TrackDetE", TrackDetE, "TrackDetE[nTracks]/F");
241     friendTree->Branch("TrackDetEECAL", TrackDetEECAL, "TrackDetEECAL[nTracks]/F");
242     }
243     if(diTrackMass) friendTree->Branch("DiTrackMass", &DiTrackMass, "DiTrackMass/F");
244     if(addTrigger) {
245     friendTree->Branch("Trigger", &Trigger, "Trigger/O");
246     friendTree->Branch("Trigger1", &Trigger1, "Trigger1/O");
247     }
248 tschum 1.14
249     int tenth = ev.size() / 10;
250     int eventNum =0;
251     for (ev.toBegin(); !ev.atEnd(); ++ev, ++eventNum) {
252    
253    
254     if( addEventInfo ) {
255 tschum 1.10
256    
257 tschum 1.14 if(eventNum==0) {
258     // fwlite::Handle<edm::TriggerResults> hltHandle;
259     // hltHandle.getByLabel(ev, "TriggerResults");
260 gebbert 1.6
261 tschum 1.14 // fwlite::Handle<edm::TriggerResults> hTriggerResults;
262 gebbert 1.6
263 tschum 1.14 // hTriggerResults.getByLabel(ev, "TriggerResults","","TEST");
264     // fwlite::TriggerNames const& triggerNames = ev.triggerNames(*hTriggerResults);
265 gebbert 1.6
266    
267 tschum 1.14 // // std::vector<std::string> const& names = triggerNames.triggerNames();
268     // for (unsigned i = 0; i < triggerNames.size(); ++i) {
269     // std::cout << i << " " << triggerNames.triggerName(i) << std::endl;
270     // }
271     }
272 tschum 1.7
273 tschum 1.14 _event = ev.id().event();
274     _run = ev.id().run();
275     _lumi = ev.luminosityBlock();
276 tschum 1.7
277 tschum 1.14 }
278 gebbert 1.6
279 thomsen 1.15 if( addTrackJets)
280     {
281     fwlite::Handle<reco::CaloJetCollection> jets;
282     jets.getByLabel(ev, "antikt5CaloJets");
283    
284     fwlite::Handle<reco::TrackCollection> tracks;
285     tracks.getByLabel(ev, "generalTracks");
286    
287     if (!jets.isValid())
288     continue;
289     if (!tracks.isValid())
290     continue;
291     double trackSum;
292     nJetsKT = 0;
293     for (reco::CaloJetCollection::const_iterator jet = jets->begin(); jet
294     !=jets->end(); jet++) {
295     trackSum = 0;
296     for (reco::TrackCollection::const_iterator track = tracks->begin(); track
297     !=tracks->end(); track++) {
298     if (deltaR(jet->eta(), jet->phi(), track->eta(), track->phi())
299     < 0.5)
300     trackSum += track->pt();
301     }
302     TrackJetKT[nJetsKT] = trackSum;
303     nJetsKT++;
304     }
305     }
306    
307     if(addHitDetInfo)
308     {
309     fwlite::Handle<reco::TrackCollection> tracks;
310     tracks.getByLabel(ev, "generalTracks");
311    
312     nTracks = 0;
313    
314     if (!tracks.isValid())
315     continue;
316    
317     for (reco::TrackCollection::const_iterator track = tracks->begin(); track
318     !=tracks->end(); track++) {
319     fwlite::Handle<vector <reco::CaloCluster> > CaloClusters;
320     CaloClusters.getByLabel(ev, "hybridSuperClusters"); //add instance
321     //CaloClusters.getByLabel(ev, "multi5x5BasicClusters");
322    
323     //fwlite::Handle<vector <CaloTower> > CaloTowers;
324     //CaloTowers.getByLabel(ev, "towerMaker");
325    
326     double DR = 100;
327     double DetE = 0;
328     double DetEECAL = 0;
329    
330     if (CaloClusters.isValid()){ //always fails
331     //if (CaloTowers.isValid()){
332     for (vector<reco::CaloCluster>::const_iterator CaloCluster = CaloClusters->begin(); CaloCluster
333     !=CaloClusters->end(); CaloCluster++) {
334     // int DetectorId = track->outerDetId(); //unfortuately only the Tracker DetId...
335     // /DetId *dId = new DetId(DetectorId);
336     //cout<<dId->det()<<endl;
337    
338    
339     if(deltaR( CaloCluster->eta(), CaloCluster->phi(), track->outerEta(), track->phi()) < DR)
340     {
341     DetE = CaloCluster->energy();
342     if(CaloCluster->caloID().detector(reco::CaloID::DET_ECAL_BARREL) ||CaloCluster->caloID().detector(reco::CaloID::DET_ECAL_ENDCAP))
343     DetEECAL = CaloCluster->energy();
344     }
345     }
346     }
347    
348     TrackDetE[nTracks] = DetE;
349     TrackDetEECAL[nTracks] = DetEECAL;
350    
351    
352     nTracks++;
353     if(nTracks > 2990) cout<<"To many Tracks!!!!!!!!!"<<endl;
354     }
355     }
356    
357     if(diTrackMass)
358     {
359     fwlite::Handle<reco::TrackCollection> tracks;
360     tracks.getByLabel(ev, "generalTracks");
361    
362     DiTrackMass = 0;
363    
364     if (!tracks.isValid())
365     continue;
366    
367 tschum 1.14 for (reco::TrackCollection::const_iterator track = tracks->begin(); track
368     !=tracks->end(); track++) {
369 thomsen 1.15 if(!(track->d0() < 2 && track->dz() < 10 && track->numberOfValidHits() > 7)) continue;
370     for (reco::TrackCollection::const_iterator track1 = tracks->begin(); track1
371     !=tracks->end(); track1++) {
372     if(!(track1->d0() < 2 && track1->dz() < 10 && track1->numberOfValidHits() > 7) || track1 == track) continue;
373    
374     TLorentzVector tr1(track->px(), track->py(), track->pz(), track->p() );
375     TLorentzVector tr2(track1->px(), track1->py(), track1->pz(), track1->p() );
376    
377     double result = (tr1 + tr2).Mag();
378    
379     if(result > DiTrackMass) DiTrackMass = result;
380    
381     }
382 tschum 1.14 }
383 thomsen 1.15
384     }
385    
386     if(addTrigger)
387     {
388    
389     Trigger = false;
390     Trigger1 = false;
391    
392     fwlite::Handle< L1GlobalTriggerReadoutRecord> gtReadoutRecord;
393     gtReadoutRecord.getByLabel(ev, "gtDigis");
394     //edm::Handle< L1GlobalTriggerReadoutRecord > gtReadoutRecord;
395     //event.getByLabel( edm::InputTag("gtDigis"), gtReadoutRecord);
396    
397     if(!gtReadoutRecord.isValid()) continue;
398    
399    
400     const TechnicalTriggerWord& technicalTriggerWordBeforeMask = gtReadoutRecord->technicalTriggerWord();
401    
402    
403     //std::vector<bool>& technicalTriggerWordBeforeMask = gtReadoutRecord->technicalTriggerWord();
404    
405     bool techTrigger0 = technicalTriggerWordBeforeMask.at(0);
406     bool techTrigger40 = technicalTriggerWordBeforeMask.at(40);
407     bool techTrigger41 = technicalTriggerWordBeforeMask.at(41);
408     bool techTrigger36 = technicalTriggerWordBeforeMask.at(36);
409     bool techTrigger37 = technicalTriggerWordBeforeMask.at(37);
410     bool techTrigger38 = technicalTriggerWordBeforeMask.at(38);
411     bool techTrigger39 = technicalTriggerWordBeforeMask.at(39);
412    
413     if(techTrigger0 && (techTrigger40 || techTrigger41)) Trigger = true;
414     if(Trigger && !techTrigger36 && !techTrigger37 && !techTrigger38 && !techTrigger39) Trigger1 = true;
415 tschum 1.14 }
416 thomsen 1.15
417 tschum 1.14 if( addTower) {
418     fwlite::Handle<CaloTowerCollection> towers;
419     towers.getByLabel(ev, "towerMaker");
420 thomsen 1.15
421 tschum 1.14 int jtow = 0;
422     NobjTowCal=towers->size();
423     for(CaloTowerCollection::const_iterator tow = towers->begin();
424     tow != towers->end(); ++tow, ++jtow){
425     towet [jtow] = tow->et();
426     toweta[jtow] = tow->eta();
427     towphi[jtow] = tow->phi();
428     towen [jtow] = tow->energy();
429     towem [jtow] = tow->emEnergy();
430     towhd [jtow] = tow->hadEnergy();
431     towoe [jtow] = tow->outerEnergy();
432     towid_phi[jtow] = tow->id().iphi();
433     towid_eta[jtow] = tow->id().ieta();
434     towid [jtow] = tow->id().rawId();
435 tschum 1.7 }
436 gebbert 1.6 }
437 tschum 1.14 friendTree->Fill();
438 thomsen 1.15
439 tschum 1.14 if( eventNum != 0 && eventNum%tenth == 0) cout<<"Processed "<<eventNum <<" of "<<ev.size()<<" events. "<<endl;
440 thomsen 1.15
441 tschum 1.14 }
442     f->cd();
443     friendTree->Write();
444 tschum 1.13 }
445     f->Close();
446     ((TChain*) this->At(currChain))->AddFriend(friendTreeName.c_str(),
447     friendFileName.c_str());
448 thomsen 1.15
449 tschum 1.13 }
450    
451 tschum 1.8
452 tschum 1.13 timer.Stop();
453     if(verbose) timer.Print();
454 tschum 1.8
455 tschum 1.13 return this->GetEntries();
456 tschum 1.1
457     }
458     //------------------------------------------------------------------
459     //Draw given Chain with given variable and given cuts
460 gebbert 1.6 int PlotTool::plot(int chainIndex, string histName, string cutName,
461     int nEntries, string drwOpt) {
462 tschum 1.1
463 gebbert 1.6 if (chainIndex >= this->GetEntries() )
464     return -1;
465 tschum 1.1
466 tschum 1.8 TStopwatch timer;
467 tschum 1.11 if(verbose) {
468     cout<<"Plot: "<<histName<<" "<<cutName<<" from chain "<<this->At(chainIndex)->GetName()<<endl;
469     timer.Start();
470     }
471 tschum 1.8
472 gebbert 1.6 int currN = nEntries;
473     if (nEntries < 0)
474     currN = ((TChain*) this->At(chainIndex))->GetEntries(); //nEntries<0 : all entries are plotted!
475    
476     //++++ Create and name Canvases according to global variables +++++++++++++
477     ostringstream currHistName;
478     if (samePad_trees)
479     currHistName<<((TChain*) this->At(chainIndex))->GetName()<<":";
480     if (samePad_vars)
481     currHistName<<histName;
482     if (samePad_cuts)
483     currHistName<<"{"<<cutName<<"}";
484    
485     ostringstream currPadName;
486     if (!samePad_trees)
487     currPadName<<((TChain*) this->At(chainIndex))->GetName()<<":";
488     if (!samePad_vars)
489     currPadName<<histName;
490     if (!samePad_cuts)
491     currPadName<<"{"<<cutName<<"}";
492    
493     ostringstream currCanvName;
494     if (!sameCanv_trees && !samePad_trees)
495     currCanvName<<((TChain*) this->At(chainIndex))->GetName()<<":";
496     if (!sameCanv_vars && !samePad_vars)
497     currCanvName<<histName;
498     if (!sameCanv_cuts && !samePad_cuts)
499     currCanvName<<"{"<<cutName<<"}";
500    
501     if ( (sameCanv_trees || samePad_trees) && (sameCanv_vars || samePad_vars)
502     && (sameCanv_cuts || samePad_cuts))
503     currCanvName<<"All";
504    
505     string currOpt = drwOpt;
506     bool useSubPads = (sameCanv_trees && !samePad_trees) || (sameCanv_vars
507     && !samePad_vars) || (sameCanv_cuts && !samePad_cuts);
508    
509     if (useSubPads) {
510 tschum 1.7 if ( !canvases_[currCanvName.str()] || !gROOT->GetListOfCanvases()->FindObject(currCanvName.str().c_str()) ) {
511 gebbert 1.6 canvases_[currCanvName.str()] = new TCanvas(currCanvName.str().c_str(), currCanvName.str().c_str());
512     } else if (canvases_[currCanvName.str()]->GetNumber() >= 0) {
513     canvases_[currCanvName.str()]->SetNumber(canvases_[currCanvName.str()]->GetNumber()+1);
514     }
515     }
516 tschum 1.1
517 tschum 1.7 if ( !pads_[currPadName.str()] || !gROOT->GetListOfCanvases()->FindObject(currPadName.str().c_str()) ) {
518 gebbert 1.6 pads_[currPadName.str()] = new TCanvas(currPadName.str().c_str(), currPadName.str().c_str());
519 tschum 1.9 // if (logY)
520     // pads_[currPadName.str()]->SetLogy(1);
521 gebbert 1.6 } else {
522     pads_[currPadName.str()]->cd();
523     currOpt += "SAMES";
524     if (useSubPads)
525     canvases_[currCanvName.str()]->SetNumber(canvases_[currCanvName.str()]->GetNumber()-1);
526     }
527     //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
528 tschum 1.1
529    
530 gebbert 1.6 //Draw histogram:
531 tschum 1.12 string cutNameWithGlobal = cutName;
532     if(cutName!="" && globalCuts!="") cutNameWithGlobal += "&&";
533     if(globalCuts!="") cutNameWithGlobal += globalCuts;
534    
535     int draw_err = ((TChain*) this->At(chainIndex))->Draw(histName.c_str(), cutNameWithGlobal.c_str(),
536 gebbert 1.6 currOpt.c_str(), currN);
537     if (draw_err < 0)
538     return draw_err;
539    
540     //++++ Fix for histos with no entries +++++++++++++
541     if (draw_err == 0) {
542     if (currOpt.find("SAMES") == string::npos) {
543     pads_[currPadName.str()]->Close();
544     pads_.erase(currPadName.str() );
545     if (useSubPads)
546     canvases_[currCanvName.str()]->SetNumber(canvases_[currCanvName.str()]->GetNumber()-1);
547     }
548     cout<< "Warning: "<<currHistName.str().c_str()<<" in "<<currPadName.str()
549     <<" has no entries and is not drawn!"<<endl;
550     return draw_err;
551     }
552     //++++++++++++++++++++++++++++++++++++++++++++++++++
553 tschum 1.1
554 gebbert 1.6 ((TH1F*) pads_[currPadName.str()]->GetPrimitive("htemp"))->SetName(currHistName.str().c_str()); // Set Name of histogram
555    
556 tschum 1.8 // Update for 2D histograms
557     if( pads_[currPadName.str()]->GetPrimitive(currHistName.str().c_str())->InheritsFrom("TH2") ) {
558     pads_[currPadName.str()]->Draw();
559     setCanvas( pads_[currPadName.str()] );
560     }
561 gebbert 1.6
562 tschum 1.11
563 tschum 1.12 if(verbose && draw_err > 0) {
564 tschum 1.11 timer.Stop();
565 tschum 1.13 cout<<"Done: Selected "<<draw_err<<" objects in "<< currN <<" processed events."<<endl;
566 tschum 1.8 timer.Print();
567     }
568 gebbert 1.6 return draw_err;
569 tschum 1.1
570     }
571     //------------------------------------------------------------------
572     //Standard loop to draw all chains and multiple variables and cuts
573 gebbert 1.6 int PlotTool::loop(vector<string> _histName, vector<string> _cutName,
574     int nEntries, string drwOpt, bool correspond) {
575 tschum 1.1
576 gebbert 1.6 int numProcessed = 0;
577 tschum 1.1
578 gebbert 1.6 if (correspond == false) {
579 tschum 1.1
580 gebbert 1.6 for (int i=0; i<this->GetEntries(); ++i) {
581     for (vector<string>::iterator it1 =_histName.begin(); it1
582     != _histName.end(); ++it1) {
583     for (vector<string>::iterator it2 =_cutName.begin(); it2
584     != _cutName.end(); ++it2) {
585 tschum 1.1
586 gebbert 1.6 numProcessed += plot(i, *it1, *it2, nEntries, drwOpt);
587 tschum 1.8
588 tschum 1.1
589 gebbert 1.6 }
590     }
591     }
592     } else {
593 tschum 1.1
594 gebbert 1.6 if (_histName.size() != _cutName.size() )
595     return -1;
596 tschum 1.1
597 gebbert 1.6 for (int i=0; i<this->GetEntries(); ++i) {
598     for (vector<string>::iterator it1 =_histName.begin(), it2 =
599     _cutName.begin(); it1 != _cutName.end()&&it2
600     != _cutName.end(); ++it1, ++it2) {
601 tschum 1.1
602 gebbert 1.6 numProcessed += plot(i, *it1, *it2, nEntries, drwOpt);
603 tschum 1.1
604 gebbert 1.6 }
605     }
606     }
607 tschum 1.1
608 tschum 1.8 updatePads();
609 gebbert 1.6 fillCanvases();
610 tschum 1.1
611 gebbert 1.6 return numProcessed;
612 tschum 1.1 }
613    
614     //Helper methods to allow for using simple strings as input
615 gebbert 1.6 int PlotTool::loop(string histName, string cutName, int nEntries, string drwOpt) {
616 tschum 1.1
617 gebbert 1.6 vector<string> _histName;
618     _histName.push_back(histName);
619     vector<string> _cutName;
620     _cutName.push_back(cutName);
621 tschum 1.1
622 gebbert 1.6 return loop(_histName, _cutName, nEntries, drwOpt);
623 tschum 1.1
624     }
625    
626 gebbert 1.6 int PlotTool::loop(vector<string> _histName, string cutName, int nEntries,
627     string drwOpt) {
628 tschum 1.1
629 gebbert 1.6 vector<string> _cutName;
630     _cutName.push_back(cutName);
631 tschum 1.1
632 gebbert 1.6 return loop(_histName, _cutName, nEntries, drwOpt);
633 tschum 1.1
634     }
635    
636 gebbert 1.6 int PlotTool::loop(string histName, vector<string> _cutName, int nEntries,
637     string drwOpt) {
638 tschum 1.1
639 gebbert 1.6 vector<string> _histName;
640     _histName.push_back(histName);
641 tschum 1.1
642 gebbert 1.6 return loop(_histName, _cutName, nEntries, drwOpt);
643 tschum 1.1 }
644     //------------------------------------------------------------------
645     //redraw the canvas to make changes in style visible
646 gebbert 1.6 int PlotTool::updatePads() {
647 tschum 1.1
648 gebbert 1.6 for (map< string, TCanvas* >::iterator it=pads_.begin() ; it != pads_.end(); ++it) {
649 tschum 1.14 if ( ! gROOT->GetListOfCanvases()->FindObject((*it).first.c_str() ) ) {
650     pads_.erase(it);
651     continue;
652     }
653     if( (*it).second->GetListOfPrimitives()->GetEntries() == 0 ) {
654     (*it).second->Close();
655     pads_.erase(it);
656     continue;
657     }
658     (*it).second->Draw();
659     setCanvas((*it).second);
660    
661 gebbert 1.6 }
662 tschum 1.1
663 gebbert 1.6 return pads_.size();
664 tschum 1.1
665     }
666    
667     int PlotTool::fillCanvases() {
668    
669 gebbert 1.6 for (map< string, TCanvas* >::iterator it=canvases_.begin() ; it
670     != canvases_.end(); ++it) {
671 tschum 1.8 string canvName = (*it).first;
672     if (gROOT->GetListOfCanvases()->FindObject(canvName.c_str()) ) {
673 gebbert 1.6
674     int numP = (*it).second->GetNumber()+1;
675    
676     if (numP <= 0)
677     continue;
678    
679     int x = int( sqrt(numP) );
680     int y = x;
681     if (x*y < numP)
682     x += 1;
683     if (x*y < numP)
684     x += 1;
685     if (x*y < numP) {
686     x -= 1;
687     y += 1;
688     }
689    
690 tschum 1.8
691 gebbert 1.6 (*it).second->Divide(x, y);
692     int padIndex = 1;
693     for (map< string, TCanvas* >::iterator it2=pads_.begin() ; it2
694     != pads_.end(); ++it2) {
695     string padName = (*it2).first;
696 tschum 1.7 if (gROOT->GetListOfCanvases()->FindObject(padName.c_str() ) ) {
697 tschum 1.8 string n1 = canvName.substr(0,canvName.find(":"));
698     string n2("");
699     if(canvName.find("{") != string::npos ) n2 = canvName.substr(canvName.find("{"),canvName.find("}"));
700    
701     if ( (padName.find(canvName.c_str()) != string::npos ) || (padName.find(n1) != string::npos && padName.find(n2) != string::npos) || !strcmp( canvName.c_str(), "All") ) {
702 gebbert 1.6 (*it).second->cd(padIndex);
703     (*it2).second->DrawClonePad();
704     (*it2).second->Close();
705     pads_.erase(it2);
706     ++padIndex;
707     }
708     } else
709     pads_.erase(it2);
710     }
711     (*it).second->SetNumber(-1);
712 tschum 1.1
713 gebbert 1.6 } else
714     canvases_.erase(it);
715     }
716 tschum 1.1
717 gebbert 1.6 return canvases_.size();
718 tschum 1.1
719     }
720     //------------------------------------------------------------------
721     void PlotTool::setCanvas(TCanvas* thisCanvas) {
722    
723 gebbert 1.6 TH1* thisHist = 0;
724     TPaveStats* thisStatsBox = 0;
725     TPaletteAxis* palette =0;
726     TLegend* thisLeg = 0;
727     int counter =0;
728     double maxEntry=0;
729 tschum 1.1
730 gebbert 1.6 ((TFrame*) thisCanvas->GetFrame())->Delete();
731     thisCanvas->GetFrame()->SetLineWidth(gStyle->GetLineWidth() );
732 tschum 1.1
733 gebbert 1.6 thisCanvas->SetLeftMargin(0.2);
734     thisCanvas->SetRightMargin(0.06);
735     thisCanvas->SetBottomMargin(0.2);
736     thisCanvas->SetTopMargin(0.1);
737 tschum 1.1
738 gebbert 1.6 if (logY)
739     thisCanvas->SetLogy(1);
740     else
741     thisCanvas->SetLogy(0);
742 tschum 1.1
743 gebbert 1.6 if (showLegend)
744     thisLeg = new TLegend();
745 tschum 1.1
746 gebbert 1.6 for (int i = 0; i != thisCanvas->GetListOfPrimitives()->GetSize(); ++i) {
747 tschum 1.1
748 gebbert 1.6 if ( !thisCanvas->GetListOfPrimitives()->At(i)->InheritsFrom("TH1"))
749     continue;
750 tschum 1.1
751 gebbert 1.6 thisHist = ((TH1*) thisCanvas->GetListOfPrimitives()->At(i));
752     setColor(thisHist, counter);
753     setMathLabels(thisHist);
754 tschum 1.1
755 gebbert 1.6 if (thisHist->GetMaximum() > maxEntry)
756     maxEntry = thisHist->GetMaximum();
757 tschum 1.1
758 gebbert 1.6 thisStatsBox = (TPaveStats*) thisHist->GetListOfFunctions()->FindObject("stats");
759     if (thisStatsBox)
760     setStats(thisCanvas, thisStatsBox, thisHist, counter);
761 tschum 1.1
762 gebbert 1.6 palette = (TPaletteAxis*) thisHist->GetListOfFunctions()->FindObject("palette");
763     if (palette)
764     setPalette(thisCanvas, palette);
765 tschum 1.1
766 gebbert 1.6 if (thisLeg)
767     thisLeg->AddEntry(thisHist,thisHist->GetName())->SetTextSize(0.04);
768 tschum 1.1
769 gebbert 1.6 ++counter;
770 tschum 1.1
771 gebbert 1.6 }
772 tschum 1.1
773 gebbert 1.6 if (maxEntry != 0)
774     setHistMax(thisCanvas, maxEntry);
775 tschum 1.1
776 gebbert 1.6 thisCanvas->cd();
777     thisCanvas->Update();
778 tschum 1.1
779 gebbert 1.6 if (thisLeg)
780     setLegend(thisCanvas, thisLeg, counter);
781 tschum 1.1
782     }
783     //------------------------------------------------------------------
784     //private helper classes to set the canvas and hist style
785 gebbert 1.6 void PlotTool::setStats(TCanvas* thisCanvas, TPaveStats* thisStatsBox,
786     TH1* thisHist, int counter) {
787 tschum 1.1
788 gebbert 1.6 if (thisCanvas->GetRightMargin() < .2)
789     thisCanvas->SetRightMargin(.2);
790 tschum 1.1
791 gebbert 1.6 thisStatsBox->SetLineColor(thisHist->GetLineColor());
792     thisStatsBox->SetX2NDC(1.);
793     thisStatsBox->SetY2NDC(1-thisCanvas->GetTopMargin()-0.16*counter);
794     thisStatsBox->SetX1NDC(1-thisCanvas->GetRightMargin()+0.01);
795     thisStatsBox->SetY1NDC(thisStatsBox->GetY2NDC()-.15);
796 tschum 1.1
797     }
798    
799     void PlotTool::setMathLabels(TH1* thisHist) {
800    
801 gebbert 1.6 string t = thisHist ->GetTitle();
802     string x = thisHist->GetXaxis()->GetTitle();
803     string y = thisHist->GetYaxis()->GetTitle();
804    
805 tschum 1.12 // if (x.find("__") != string::npos)
806     // x = x.substr(0,x.find("__"));
807     // if (y.find("__") != string::npos)
808     // y = y.substr(0,y.find("__"));
809    
810 gebbert 1.6 if (x.find(".phi()") != string::npos)
811     x.replace(x.find(".phi()"), 6, " #phi");
812     if (y.find(".phi()") != string::npos)
813     y.replace(y.find(".phi()"), 6, " #phi");
814    
815     if (x.find(".eta()") != string::npos)
816     x.replace(x.find(".eta()"), 6, " #eta");
817     if (y.find(".eta()") != string::npos)
818     y.replace(y.find(".eta()"), 6, " #eta");
819    
820 tschum 1.12 if (x.find(".theta()") != string::npos)
821     x.replace(x.find(".theta()"), 6, " #theta");
822     if (y.find(".theta()") != string::npos)
823     y.replace(y.find(".theta()"), 6, " #theta");
824    
825     if (x.find(".chi2()") != string::npos)
826     x.replace(x.find(".chi2()"), 7, " #chi^{2}");
827     if (y.find(".chi2()") != string::npos)
828     y.replace(y.find(".chi2()"), 7, " #chi^{2}");
829    
830 gebbert 1.6 if (x.find(".pt()") != string::npos)
831 tschum 1.12 x.replace(x.find(".pt()"), 5, " p_{T} [GeV]");
832 gebbert 1.6 if (y.find(".pt()") != string::npos)
833 tschum 1.12 y.replace(y.find(".pt()"), 5, " p_{T} [GeV]");
834    
835     if (x.find(".et()") != string::npos)
836     x.replace(x.find(".et()"), 5, " E_{T} [GeV]");
837     if (y.find(".et()") != string::npos)
838     y.replace(y.find(".et()"), 5, " E_{T} [GeV]");
839    
840     //splitlines for many cuts
841     string test1= "{" + globalCuts + "}";
842     string test2= "&&" + globalCuts;
843    
844     if(t.find(test1) != string::npos) t.replace(t.find(test1),test1.length(),"");
845     if(t.find(test2) != string::npos) t.replace(t.find(test2),test2.length(),"");
846    
847     if (t.find("{") != string::npos && t.find("#splitline") == string::npos) {
848     t.replace(t.find_last_of("{"), 1, "}{");
849     string t_old = t;
850     t = "#splitline{";
851     t += t_old;
852     }
853 gebbert 1.6
854     thisHist ->SetTitle(t.c_str());
855     thisHist->GetXaxis()->SetTitle(x.c_str());
856     thisHist->GetYaxis()->SetTitle(y.c_str());
857 tschum 1.1
858     }
859    
860     void PlotTool::setColor(TH1* thisHist, int counter) {
861    
862 gebbert 1.6 if (counter == 0) {
863 tschum 1.1
864 gebbert 1.6 thisHist->SetLineColor(kRed+1);
865     //if( gStyle->GetHistFillStyle() ) thisHist->SetFillColor (kRed+1);
866     //else
867     thisHist->SetFillStyle(0);
868     thisHist->SetMarkerColor(kRed+1);
869     } else if (counter == 1) {
870     thisHist->SetLineColor(kBlue+1);
871     //if( gStyle->GetHistFillStyle() ) thisHist->SetFillColor (kBlue+1);
872     //else
873     thisHist->SetFillStyle(0);
874     thisHist->SetMarkerColor(kBlue+1);
875    
876     } else if (counter == 2) {
877     thisHist->SetLineColor(kGreen+2);
878     //if( gStyle->GetHistFillStyle() ) thisHist->SetFillColor (kGreen+2);
879     //else
880     thisHist->SetFillStyle(0);
881     thisHist->SetMarkerColor(kGreen+2);
882    
883     } else if (counter == 3) {
884     thisHist->SetLineColor(kMagenta+2);
885     //if( gStyle->GetHistFillStyle() ) thisHist->SetFillColor (kMagenta+2);
886     //else
887     thisHist->SetFillStyle(0);
888     thisHist->SetMarkerColor(kMagenta+2);
889    
890     } else if (counter == 4) {
891     thisHist->SetLineColor(kCyan+2);
892     //if( gStyle->GetHistFillStyle() ) thisHist->SetFillColor (kCyan+2);
893     //else
894     thisHist->SetFillStyle(0);
895     thisHist->SetMarkerColor(kCyan+2);
896    
897     } else {
898     thisHist->SetLineColor(kBlack);
899     //if( gStyle->GetHistFillStyle() ) thisHist->SetFillColor (kBlack);
900     //else
901     thisHist->SetFillStyle(0);
902     thisHist->SetMarkerColor(kBlack);
903 tschum 1.1
904 gebbert 1.6 }
905 tschum 1.1
906     }
907    
908     void PlotTool::setPalette(TCanvas* thisCanvas, TPaletteAxis* palette) {
909    
910 gebbert 1.6 palette->SetLabelSize(0.045);
911     if (thisCanvas->GetRightMargin() < .15)
912     thisCanvas->SetRightMargin(.15);
913     palette->SetX1NDC(1-thisCanvas->GetRightMargin()+0.01);
914     palette->SetY1NDC(thisCanvas->GetBottomMargin());
915     palette->SetX2NDC(palette->GetX1NDC()+0.05);
916     palette->SetY2NDC(1-thisCanvas->GetTopMargin());
917 tschum 1.1
918     }
919    
920     void PlotTool::setHistMax(TCanvas* thisCanvas, double maxEntry) {
921    
922 gebbert 1.6 TH1* thisHist = 0;
923 tschum 1.1
924 gebbert 1.6 for (int i = 0; i != thisCanvas->GetListOfPrimitives()->GetSize(); ++i) {
925 tschum 1.1
926 gebbert 1.6 if ( !thisCanvas->GetListOfPrimitives()->At(i)->InheritsFrom("TH1") )
927     continue;
928     thisHist = ((TH1*) thisCanvas->GetListOfPrimitives()->At(i));
929     if (thisHist->GetMaximum() < maxEntry) {
930     double minEntry=thisHist->GetBinContent(thisHist->GetMinimumBin());
931     if (logY) {
932     int bin = (thisHist->GetMinimumBin()+1);
933     minEntry=thisHist->GetBinContent(bin);
934     }
935     thisHist->GetYaxis()->SetRangeUser(minEntry, maxEntry*1.2);
936     }
937     break;
938 tschum 1.1
939 gebbert 1.6 }
940 tschum 1.1
941     }
942    
943     void PlotTool::setLegend(TCanvas* thisCanvas, TLegend* thisLeg, int counter) {
944    
945 gebbert 1.6 thisLeg->SetFillStyle(0);
946     thisLeg->SetX1NDC(1-thisCanvas->GetRightMargin()-0.5);
947     thisLeg->SetY1NDC(1-thisCanvas->GetTopMargin()-0.05*counter);
948     thisLeg->SetX2NDC(1-thisCanvas->GetRightMargin());
949     thisLeg->SetY2NDC(1-thisCanvas->GetTopMargin());
950     thisLeg->SetEntrySeparation(0.5);
951     thisLeg->Draw("NDC");
952 tschum 1.1
953     }
954    
955 gebbert 1.6 void PlotTool::createColors() {
956     const Int_t NRGBs = 5;
957     const Int_t NCont = 255;
958    
959     Double_t stops[NRGBs] = { 0.00, 0.34, 0.61, 0.84, 1.00 };
960     Double_t red[NRGBs] = { 0.00, 0.00, 0.87, 1.00, 0.51 };
961     Double_t green[NRGBs] = { 0.00, 0.81, 1.00, 0.20, 0.00 };
962     Double_t blue[NRGBs] = { 0.51, 1.00, 0.12, 0.00, 0.00 };
963     TColor::CreateGradientColorTable(NRGBs, stops, red, green, blue, NCont);
964     gStyle->SetNumberContours(NCont);
965 tschum 1.1
966     }
967    
968     //------------------------------------------------------------------
969    
970 tschum 1.9 int PlotTool::saveCanvases(string name, string path) {
971 tschum 1.1
972 gebbert 1.6 TSystemDirectory d("", path.c_str());
973     if (!d.GetListOfFiles())
974     return -1;
975    
976 tschum 1.9 string namePs = path +"/" + name;
977     string nameRt = path +"/" + name;
978    
979     if (name.find(".ps") == string::npos)
980     namePs += ".ps";
981    
982     if (name.find(".root") == string::npos)
983     nameRt += ".root";
984    
985     int savedCanvs =0;
986    
987 tschum 1.12
988     TIter next(gROOT->GetListOfCanvases());
989     TCanvas* canv;
990     while ((canv=(TCanvas*)next())) {
991     string modName = (*canv).GetName();
992     if(modName.find("{") != string::npos) modName = modName.substr(0,modName.find("{"));
993     (*canv).SetTitle( modName.c_str() );
994    
995     }
996    
997    
998 tschum 1.9 TPostScript ps(namePs.c_str(),112);
999     TFile rt(nameRt.c_str(),"recreate");
1000 gebbert 1.6
1001 tschum 1.12 next.Reset();
1002 gebbert 1.6 while ((canv=(TCanvas*)next())) {
1003 tschum 1.9 ps.NewPage();
1004 tschum 1.12
1005    
1006     (*canv).Write();
1007 tschum 1.9 (*canv).Draw();
1008     (*canv).Update();
1009 tschum 1.12
1010    
1011 tschum 1.9 ++savedCanvs;
1012 gebbert 1.6
1013 tschum 1.9 }
1014 gebbert 1.6
1015 tschum 1.9 ps.Close();
1016     rt.Close();
1017 tschum 1.1
1018 tschum 1.12
1019     if(verbose && savedCanvs) {
1020     cout<<"Saved file "<<rt.GetName()<<" with "<<savedCanvs<<" canvases."<<endl;
1021     cout<<"Saved file "<<ps.GetName()<<" with "<<savedCanvs<<" pages."<<endl;
1022     }
1023 tschum 1.9 return savedCanvs;
1024 tschum 1.1
1025     }
1026     //------------------------------------------------------------------
1027 tschum 1.11
1028     int PlotTool::clearCanvases()
1029 tschum 1.7 {
1030    
1031 tschum 1.11 while(gROOT->GetListOfCanvases()->GetEntries()) ((TCanvas*) gROOT->GetListOfCanvases()->At(0))->Close();
1032     pads_.clear();
1033     autoVars.clear();
1034     return pads_.size() + gROOT->GetListOfCanvases()->GetEntries();
1035    
1036     }
1037     //------------------------------------------------------------------
1038     int PlotTool::setVariables(string label)
1039     {
1040 tschum 1.7
1041     for (int i=0; i< this->GetEntries(); ++i) {
1042 tschum 1.11 cout<<"--------------------------------"<<endl;
1043 tschum 1.7 cout<<((TChain*) this->At(i))->GetName()<<endl;
1044 tschum 1.11 cout<<"------------"<<endl;
1045    
1046     TList* leaves = (TList*) ((TChain*) this->At(i))->GetListOfLeaves();
1047     for (int j=0; j< leaves->GetEntries(); ++j) {
1048     TString leafName ( leaves->At(j)->GetName() );
1049     if(! leafName.EndsWith(".") ) continue;
1050     if(! leafName.Contains(label.c_str() ) ) continue;
1051    
1052     TClass cl( ( (TLeafElement*) leaves->At(j) )->GetTypeName() );
1053     cout<<"++++++"<<endl;
1054     cout<< leafName.Data() <<endl;
1055     for(int k=0;k<cl.GetListOfAllPublicMethods()->GetEntries();++k) {
1056     string typeName ( ((TMethod*) cl.GetListOfAllPublicMethods()->At(k))->GetReturnTypeName() );
1057     string methName ( cl.GetListOfAllPublicMethods()->At(k)->GetName() );
1058     if( methName != "product") continue;
1059     cout<< typeName <<endl;
1060     string _type;
1061     TString testString( typeName.c_str() );
1062     if( testString.BeginsWith("vector<") ) {
1063     _type = typeName.substr(typeName.find("<")+1,typeName.find(">")-typeName.find("<")-1);
1064     string _varSize = leafName.Data();
1065     _varSize += "obj@.size()";
1066     autoVars.push_back( _varSize );
1067     }
1068     else _type = typeName.substr(0,typeName.find("*"));
1069     TClass _cl( _type.c_str() );
1070     for(int l=0;l<_cl.GetListOfAllPublicMethods()->GetEntries();++l) {
1071     string _typeName ( ((TMethod*) _cl.GetListOfAllPublicMethods()->At(l))->GetReturnTypeName() );
1072     string _methName ( _cl.GetListOfAllPublicMethods()->At(l)->GetName() );
1073     // if(_typeName.find("void") != string::npos ) continue;
1074     // cout<<" "<<_typeName<<" "<<_methName<<endl;
1075    
1076     cout<<" "<<_typeName<<" "<<_methName<<endl;
1077     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")) {
1078    
1079     cout<<"--> "<<_typeName<<" "<<_methName<<endl;
1080    
1081     string _varName = leafName.Data();
1082     _varName += "obj.";
1083     _varName += _methName;
1084     _varName += "()";
1085     autoVars.push_back( _varName );
1086     }
1087     }
1088    
1089    
1090     }
1091    
1092     }
1093 tschum 1.7
1094     }
1095    
1096 tschum 1.11 return autoVars.size();
1097 tschum 1.7
1098     }
1099 tschum 1.8 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1100     //Draw efficiencies
1101     int PlotTool::plotEff(int chainIndex, string histName, string cutName, int nEntries, double fitXmin, double fitXmax, string fitFormula)
1102     {
1103    
1104     if( chainIndex >= this->GetEntries() ) return -1;
1105    
1106     int currN = nEntries;
1107     if(nEntries < 0) currN = ((TChain*) this->At(chainIndex))->GetEntries(); //nEntries<0 : all entries are plotted!
1108    
1109     //++++ Create and name Canvases according to global variables +++++++++++++
1110     ostringstream currHistName;
1111     if( samePad_trees) currHistName<<((TChain*) this->At(chainIndex))->GetName()<<":";
1112     if( samePad_vars) currHistName<<histName;
1113     if( samePad_cuts) currHistName<<"{"<<cutName<<"}";
1114    
1115     ostringstream currPadName;
1116     if(! samePad_trees) currPadName<<((TChain*) this->At(chainIndex))->GetName()<<":";
1117     if(! samePad_vars) currPadName<<histName;
1118     if(! samePad_cuts) currPadName<<"{"<<cutName<<"}";
1119    
1120    
1121     //Draw total histogram:
1122     if( ! pads_["total"] || ! gROOT->FindObject("total") ) {
1123     pads_["total"] = new TCanvas("total", "total");
1124     } else {
1125     pads_["total"]->cd();
1126     }
1127     ostringstream bins_total;
1128     bins_total<<histName;
1129     bins_total<<">>total(100,0,1000)";
1130    
1131    
1132     int draw_err = ((TChain*) this->At(chainIndex))->Draw(bins_total.str().c_str(), "", "", currN);
1133     if( draw_err <= 0 ) return draw_err;
1134    
1135    
1136     TH1* total = ((TH1*) pads_["total"]->GetPrimitive("total"));
1137    
1138     ostringstream bins_bkg;
1139     bins_bkg<<histName;
1140     bins_bkg<<">>bkg(";
1141     bins_bkg<<total->GetNbinsX();
1142     bins_bkg<<",";
1143     bins_bkg<<total->GetXaxis()->GetXmin();
1144     bins_bkg<<",";
1145     bins_bkg<<total->GetXaxis()->GetXmax();
1146     bins_bkg<<")";
1147    
1148     //Draw bkg histogram:
1149     if( ! pads_["bkg"] || ! gROOT->FindObject("bkg") ) {
1150     pads_["bkg"] = new TCanvas("bkg", currPadName.str().c_str());
1151     } else {
1152     pads_["bkg"]->cd();
1153     }
1154    
1155     draw_err = ((TChain*) this->At(chainIndex))->Draw(bins_bkg.str().c_str(), cutName.c_str(),"" , currN);
1156     if( draw_err <= 0 ) return draw_err;
1157    
1158    
1159     TH1* bkg = ((TH1*) pads_["bkg"]->GetPrimitive("bkg"));
1160    
1161     //Draw pass histogram:
1162     ostringstream bins_pass;
1163     bins_pass<<histName;
1164     bins_pass<<">>pass(";
1165     bins_pass<<total->GetNbinsX();
1166     bins_pass<<",";
1167     bins_pass<<total->GetXaxis()->GetXmin();
1168     bins_pass<<",";
1169     bins_pass<<total->GetXaxis()->GetXmax();
1170     bins_pass<<")";
1171    
1172     ostringstream cut_pass;
1173     cut_pass<<"!(";
1174     cut_pass<<cutName;
1175     cut_pass<<")";
1176    
1177    
1178     if( ! pads_["pass"] || ! gROOT->FindObject("pass") ) {
1179     pads_["pass"] = new TCanvas("pass", currPadName.str().c_str());
1180     } else {
1181     pads_["pass"]->cd();
1182     }
1183    
1184     draw_err = ((TChain*) this->At(chainIndex))->Draw(bins_pass.str().c_str(),cut_pass.str().c_str() ,"" , currN);
1185     if( draw_err <= 0 ) return draw_err;
1186    
1187    
1188     TH1* pass = ((TH1*) pads_["pass"]->GetPrimitive("pass"));
1189    
1190    
1191     currPadName<<"Eff";
1192     //Draw Efficiency Graph:
1193     if( ! pads_["EffGraph"] || ! gROOT->FindObject("EffGraph") ) {
1194     pads_["EffGraph"] = new TCanvas("EffGraph", currPadName.str().c_str());
1195     } else {
1196     pads_["EffGraph"]->cd();
1197     }
1198    
1199     TGraphAsymmErrors* EffGraph = new TGraphAsymmErrors(bkg, total);
1200     EffGraph->SetName(currHistName.str().c_str());
1201    
1202     TF1* reverse = new TF2("reverse","1/y-1",total->GetXaxis()->GetXmin(),total->GetXaxis()->GetXmax());
1203     EffGraph->Apply(reverse);
1204     EffGraph->Draw("A*");
1205    
1206     TF1* fitfunc = new TF1("fitfunc",fitFormula.c_str(),fitXmin,fitXmax);
1207     EffGraph->Fit("fitfunc","R+");
1208    
1209    
1210     //Save fit function
1211    
1212     ostringstream savefuncName;
1213     savefuncName<<histName;
1214     savefuncName<<"_";
1215    
1216     if(cutName.find("<") != std::string::npos ) cutName.replace(cutName.find("<"),1,"_");
1217     if(cutName.find(">") != std::string::npos ) cutName.replace(cutName.find(">"),1,"_");
1218     savefuncName<<cutName;
1219    
1220    
1221     TFile file("ABCDFunctions.root","UPDATE");
1222     TF1* savefunc = new TF1(savefuncName.str().c_str(), fitfunc->GetExpFormula().Data(),0,10000);
1223     savefunc->SetParameters( fitfunc->GetParameters() );
1224     savefunc->SetParErrors ( fitfunc->GetParErrors() );
1225    
1226     //Show results
1227     if( ! pads_["abcd"] || ! gROOT->FindObject("abcd") ) {
1228     pads_["abcd"] = new TCanvas("abcd", "abcd");
1229     } else {
1230     pads_["abcd"]->cd();
1231     }
1232    
1233     bkg->Multiply(savefunc);
1234     bkg->Draw();
1235    
1236     // total->Add(bkg,-1);
1237     pass->SetLineColor(kRed);
1238     pass->Draw("sames,e");
1239     pads_["abcd"]->SetLogy();
1240    
1241    
1242     savefunc->Write();
1243     // file.Close();
1244    
1245    
1246     return draw_err;
1247    
1248     }
1249     //------------------------------------------------------------------