ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/tschum/FWlite_Analysis/PlotTool.cc
(Generate patch)

Comparing UserCode/tschum/FWlite_Analysis/PlotTool.cc (file contents):
Revision 1.7 by tschum, Fri Nov 27 17:39:46 2009 UTC vs.
Revision 1.15 by thomsen, Wed Dec 16 16:33:28 2009 UTC

# Line 15 | Line 15 | PlotTool::PlotTool() {
15          showLegend = false;
16          logY = true;
17          addTrackJets = true;
18 +        addHitDetInfo = true;
19 +        diTrackMass = true;
20 +        addTrigger = true;
21 +        addTrackJets = false;
22 +        addEventInfo = false;
23 +        addTower = false;
24 +        verbose = true;
25 +
26 +        globalCuts="";
27 +
28 +        recreateTree = false; //force recreation of friend tree
29  
30   }
31  
# Line 22 | Line 33 | PlotTool::PlotTool() {
33   //Fill object PlotTool with Chains constructed from files from given source
34  
35   int PlotTool::init(string fileName, string dirPath, string treeName,
36 <                string fileLabel) {
37 <        this->New(this->GetEntries() );
38 <        int currChain = this->GetEntries() - 1;
39 <
40 <        if (currChain < 0)
41 <                return currChain;
42 <
43 <        fileNames.clear();
44 <
45 <        //check if alternative label is different from default(searchString)
46 <        if (fileLabel=="") {
47 <                fileLabel=fileName;
48 <        }
49 <        ((TChain*) this->At(currChain))->SetName(fileLabel.c_str());
50 <        TSystemDirectory dir("sourceDir", dirPath.c_str());
51 <        TList *files = dir.GetListOfFiles();
52 <        if (files) {
53 <                TIter next(files);
54 <                TSystemFile *file;
55 <                TString fname;
56 <                string filePath;
57 <
58 <                cout<<"Open"<<dirPath.c_str()<<" Search for .root files that contain: "
59 <                                <<fileName.c_str()<<endl;
60 <
61 <                while ((file=(TSystemFile*)next())) {
62 <                        fname = file->GetName();
63 <                        if (!fname.EndsWith(".root"))
64 <                                continue;
65 <                        if (!fname.Contains(fileName.c_str()))
66 <                                continue;
67 <
68 <                        filePath = dirPath;
69 <                        filePath += fname.Data();
70 <                        //fwlite::ChainEvent to lop over events, jets, etc
71 <                        fileNames.push_back(filePath.c_str());
72 <
73 <                        if (((TChain*) this->At(currChain))->AddFile(filePath.c_str(), -1,
74 <                                        treeName.c_str()) )
75 <                                cout<<"Chained "<<((TChain*) this->At(currChain))->GetNtrees()<<" file(s) with "<<((TChain*) this->At(currChain))->GetEntries()<<" events."<<endl;
76 <                        else
77 <                                return -1;
78 <
79 <                }
80 <        } else
81 <                return -1;
82 <
83 <        for (int i=0; i<((TChain*) this->At(currChain))->GetListOfBranches()->GetEntries(); ++i) {
84 <
85 <                string s(((TChain*) this->At(currChain))->GetListOfBranches()->At(i)->GetName());
86 <
87 <                string branch_alias = s;
88 <                string branch_name = s;
89 <                if (s.find(".", s.size()-1) != string::npos)
90 <                        branch_name += "obj";
91 <
92 <                size_t a = s.find("_");
93 <                if (a != string::npos) {
94 <                  size_t b = s.find("_", a+1);
95 <                  if (b != string::npos) {
96 <                    size_t c = s.find("_", b+1);
97 <                    if (c != string::npos) {
98 <                      string _label    =s.substr(a+1, b-a-1);
99 <                      string _instance =s.substr(b+1, c-b-1);
100 <                      branch_alias = _label;
101 <                      if(_instance.length() > 0 )  {
91 <                        branch_alias += "_";
92 <                        branch_alias += _instance;
93 <                      }
94 <                    }
95 <                  }
96 <                }
97 <
98 <                ((TChain*) this->At(currChain))->SetAlias(branch_alias.c_str(),
99 <                                branch_name.c_str());
36 >                   string fileLabel) {
37 >  this->New(this->GetEntries() );
38 >  int currChain = this->GetEntries() - 1;
39 >
40 >  if (currChain < 0)
41 >    return currChain;
42 >
43 >  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 >
68 >
69 >  } 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  
103 +    }
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          }
136 +      }
137 +    }
138  
139 <        // add branch with track  Jets
140 <
105 <        if( addTrackJets ) {
139 >    ((TChain*) this->At(currChain))->SetAlias(branch_alias.c_str(),
140 >                                              branch_name.c_str());
141  
142 <          //make file for tree friends (adding additional, computed branches)
108 <          string friendFileName("/scratch/hh/lustre/cms/user/thomsen/temp/");
109 <          friendFileName += fileName;
110 <          friendFileName += ".root";
111 <          TFile *f = new TFile(friendFileName.c_str(),"recreate");
112 <          string friendTreeName("friendTree");
113 <          //char number = currChain;
114 <          //friendTreeName += number;
115 <          //TTree *friendTree = new TTree("friendTree","friendTree");
116 <          TTree *friendTree = new TTree(friendTreeName.c_str(),friendTreeName.c_str());
142 >  }
143  
144 <          int nJetsKT;
145 <          TrackJetKT = new float [100];
146 <          fwlite::ChainEvent ev(fileNames);
144 >  if( addEventInfo || addTrackJets || addTower || addTrackJets || addHitDetInfo || diTrackMass || addTrigger) {
145 >
146 >    if(verbose)   cout<<"add friend tree with additional variables..."<<endl;
147 >
148 >    //make file for tree friends (adding additional, computed branches)
149 >    string friendFileName("/scratch/hh/current/cms/user/");
150 >    friendFileName += gSystem->GetUserInfo()->fUser;
151 >    friendFileName += "/temp/";
152 >    friendFileName += fileLabel;
153 >    friendFileName += ".root";
154 >    string fileOpt = "update";
155 >    if( recreateTree )    fileOpt = "recreate";
156 >
157 >    TFile *f = new TFile(friendFileName.c_str(),fileOpt.c_str());
158 >
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 >    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 >      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 >      if( addEventInfo ) {
200 >
201 >
202 >        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 >      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 >
249 >      int tenth = ev.size() / 10;
250 >      int eventNum =0;
251 >      for (ev.toBegin(); !ev.atEnd(); ++ev, ++eventNum) {
252 >
253 >
254 >        if( addEventInfo ) {
255 >
256 >
257 >          if(eventNum==0) {
258 >            //    fwlite::Handle<edm::TriggerResults> hltHandle;
259 >            //    hltHandle.getByLabel(ev, "TriggerResults");
260 >
261 >            //    fwlite::Handle<edm::TriggerResults> hTriggerResults;
262 >
263 >            //    hTriggerResults.getByLabel(ev, "TriggerResults","","TEST");
264 >            //    fwlite::TriggerNames const&  triggerNames = ev.triggerNames(*hTriggerResults);
265 >
266 >
267 >            //    //      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  
273 <          friendTree->Branch("nJetsKT", &nJetsKT, "nJetsKT/I");
274 <          friendTree->Branch("TrackJetKT", TrackJetKT, "TrackJetKT[nJetsKT]/F");
273 >          _event = ev.id().event();
274 >          _run   = ev.id().run();
275 >          _lumi = ev.luminosityBlock();
276  
277 <          cout<<"calculating additional variables..."<<endl;
126 <          for (ev.toBegin(); !ev.atEnd(); ++ev) {
277 >        }
278  
279 +        if( addTrackJets)
280 +          {
281              fwlite::Handle<reco::CaloJetCollection> jets;
282              jets.getByLabel(ev, "antikt5CaloJets");
283 <
283 >            
284              fwlite::Handle<reco::TrackCollection> tracks;
285              tracks.getByLabel(ev, "generalTracks");
286 <
286 >            
287              if (!jets.isValid())
288                continue;
289              if (!tracks.isValid())
# Line 149 | Line 302 | int PlotTool::init(string fileName, stri
302                TrackJetKT[nJetsKT] = trackSum;
303                nJetsKT++;
304              }
152            friendTree->Fill();
305            }
306 <          f->cd();
307 <          friendTree->Write();
308 <          f->Close();
309 <          ((TChain*) this->At(currChain))->AddFriend(friendTreeName.c_str(),
310 <                                                     friendFileName.c_str());
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 >            for (reco::TrackCollection::const_iterator track = tracks->begin(); track
368 >                   !=tracks->end(); track++) {
369 >              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 >            }
383 >            
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 >          }
416 >
417 >        if( addTower) {
418 >          fwlite::Handle<CaloTowerCollection> towers;
419 >          towers.getByLabel(ev, "towerMaker");
420 >          
421 >          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 >          }
436          }
437 <        return this->GetEntries();
437 >        friendTree->Fill();
438 >        
439 >        if( eventNum != 0 && eventNum%tenth == 0) cout<<"Processed "<<eventNum <<" of "<<ev.size()<<" events. "<<endl;
440 >        
441 >      }
442 >      f->cd();
443 >      friendTree->Write();
444 >    }
445 >    f->Close();
446 >    ((TChain*) this->At(currChain))->AddFriend(friendTreeName.c_str(),
447 >                                               friendFileName.c_str());
448 >    
449 >  }
450 >
451 >
452 >  timer.Stop();
453 >  if(verbose) timer.Print();
454 >      
455 >  return this->GetEntries();
456  
457   }
458   //------------------------------------------------------------------
# Line 168 | Line 463 | int PlotTool::plot(int chainIndex, strin
463          if (chainIndex >= this->GetEntries() )
464                  return -1;
465  
466 +        TStopwatch timer;
467 +        if(verbose) {
468 +          cout<<"Plot: "<<histName<<" "<<cutName<<" from chain "<<this->At(chainIndex)->GetName()<<endl;
469 +          timer.Start();
470 +        }
471 +
472          int currN = nEntries;
473          if (nEntries < 0)
474                  currN = ((TChain*) this->At(chainIndex))->GetEntries(); //nEntries<0 : all entries are plotted!
# Line 215 | Line 516 | int PlotTool::plot(int chainIndex, strin
516  
517          if ( !pads_[currPadName.str()] || !gROOT->GetListOfCanvases()->FindObject(currPadName.str().c_str()) ) {
518                  pads_[currPadName.str()] = new TCanvas(currPadName.str().c_str(), currPadName.str().c_str());
519 <                if (logY)
520 <                        pads_[currPadName.str()]->SetLogy(1);
519 >                //              if (logY)
520 >                //                      pads_[currPadName.str()]->SetLogy(1);
521          } else {
522                  pads_[currPadName.str()]->cd();
523                  currOpt += "SAMES";
# Line 227 | Line 528 | int PlotTool::plot(int chainIndex, strin
528  
529  
530          //Draw histogram:
531 <        int draw_err = ((TChain*) this->At(chainIndex))->Draw(histName.c_str(), cutName.c_str(),
531 >        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                          currOpt.c_str(), currN);
537          if (draw_err < 0)
538                  return draw_err;
# Line 248 | Line 553 | int PlotTool::plot(int chainIndex, strin
553  
554          ((TH1F*) pads_[currPadName.str()]->GetPrimitive("htemp"))->SetName(currHistName.str().c_str()); // Set Name of histogram
555  
556 +        // 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 +
562  
563 +        if(verbose && draw_err > 0) {
564 +          timer.Stop();
565 +          cout<<"Done: Selected "<<draw_err<<" objects in "<< currN <<" processed events."<<endl;
566 +          timer.Print();
567 +        }
568          return draw_err;
569  
570   }
# Line 268 | Line 584 | int PlotTool::loop(vector<string> _histN
584                                                  != _cutName.end(); ++it2) {
585  
586                                          numProcessed += plot(i, *it1, *it2, nEntries, drwOpt);
587 <                                        updatePads();
587 >                                
588  
589                                  }
590                          }
# Line 289 | Line 605 | int PlotTool::loop(vector<string> _histN
605                  }
606          }
607  
608 <        //  updatePads();
608 >        updatePads();
609          fillCanvases();
610  
611          return numProcessed;
# Line 330 | Line 646 | int PlotTool::loop(string histName, vect
646   int PlotTool::updatePads() {
647  
648          for (map< string, TCanvas* >::iterator it=pads_.begin() ; it != pads_.end(); ++it) {
649 <                if (gROOT->GetListOfCanvases()->FindObject((*it).first.c_str() ) ) {
650 <                        (*it).second->Draw();
651 <                        setCanvas((*it).second);
652 <                } else
653 <                        pads_.erase(it);
649 >                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          }
662  
663          return pads_.size();
# Line 345 | Line 668 | int PlotTool::fillCanvases() {
668  
669          for (map< string, TCanvas* >::iterator it=canvases_.begin() ; it
670                          != canvases_.end(); ++it) {
671 <                const char* canvName = (*it).first.c_str();
672 <                if (gROOT->GetListOfCanvases()->FindObject(canvName) ) {
671 >                string canvName = (*it).first;
672 >                if (gROOT->GetListOfCanvases()->FindObject(canvName.c_str()) ) {
673  
674                          int numP = (*it).second->GetNumber()+1;
675  
# Line 364 | Line 687 | int PlotTool::fillCanvases() {
687                                  y += 1;
688                          }
689  
690 +
691                          (*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                                  if (gROOT->GetListOfCanvases()->FindObject(padName.c_str() ) ) {
697 <                                        if ( (padName.find(canvName) != string::npos ) || !strcmp(
698 <                                                        canvName, "All") ) {
697 >                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                                                  (*it).second->cd(padIndex);
703                                                  (*it2).second->DrawClonePad();
704                                                  (*it2).second->Close();
# Line 475 | Line 802 | void PlotTool::setMathLabels(TH1* thisHi
802          string x = thisHist->GetXaxis()->GetTitle();
803          string y = thisHist->GetYaxis()->GetTitle();
804  
805 <        if (t.find(".phi()") != string::npos)
806 <                t.replace(t.find(".phi()"), 6, " #phi");
805 > //      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          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  
485        if (t.find(".eta()") != string::npos)
486                t.replace(t.find(".eta()"), 6, " #eta");
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 <        if (t.find(".pt()") != string::npos)
821 <                t.replace(t.find(".pt()"), 5, " p_{T}");
820 >        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          if (x.find(".pt()") != string::npos)
831 <                x.replace(x.find(".pt()"), 5, " p_{T}");
831 >                x.replace(x.find(".pt()"), 5, " p_{T} [GeV]");
832          if (y.find(".pt()") != string::npos)
833 <                y.replace(y.find(".pt()"), 5, " p_{T}");
833 >                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  
854          thisHist ->SetTitle(t.c_str());
855          thisHist->GetXaxis()->SetTitle(x.c_str());
# Line 612 | Line 967 | void PlotTool::createColors() {
967  
968   //------------------------------------------------------------------
969  
970 < int PlotTool::saveCanvases(string type, string path) {
970 > int PlotTool::saveCanvases(string name, string path) {
971  
972          TSystemDirectory d("", path.c_str());
973          if (!d.GetListOfFiles())
974                  return -1;
620        if (type.find(".") == string::npos)
621                return -1;
975  
976 <        int savedFiles =0;
976 >        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  
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  
629                string s = "";
630                s += path;
631                s += canv->GetName();
632                s += type;
997  
998 <                canv->SaveAs(s.c_str());
999 <                ++savedFiles;
998 >        TPostScript ps(namePs.c_str(),112);
999 >        TFile rt(nameRt.c_str(),"recreate");
1000 >
1001 >        next.Reset();
1002 >        while ((canv=(TCanvas*)next())) {
1003 >          ps.NewPage();
1004 >
1005 >
1006 >          (*canv).Write();
1007 >          (*canv).Draw();
1008 >          (*canv).Update();
1009 >
1010 >
1011 >          ++savedCanvs;
1012  
1013          }
1014  
1015 <        return savedFiles;
1015 >        ps.Close();
1016 >        rt.Close();
1017  
1018 +
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 < //------------------------------------------------------------------
643 < void PlotTool::showChainInfo()
644 < {
1023 >        return savedCanvs;
1024  
1025 + }
1026 + //------------------------------------------------------------------
1027  
1028 <  cout<<endl;
1029 <  cout<<"****** Show Chain Information:"<<endl;
1028 > int PlotTool::clearCanvases()
1029 > {
1030  
1031 +  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 <  this->ls();
1036 > }
1037 > //------------------------------------------------------------------
1038 > int PlotTool::setVariables(string label)
1039 > {
1040  
653  cout<<endl;
654  cout<<"We have "<<this->GetEntries()<<" TChains with following aliases set:"<<endl;
1041          for (int i=0; i< this->GetEntries(); ++i) {
1042 <          cout<<endl;
1042 >          cout<<"--------------------------------"<<endl;
1043            cout<<((TChain*) this->At(i))->GetName()<<endl;
1044 <          ((TChain*) this->At(i))->GetListOfAliases()->ls();
1044 >          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  
1094          }
1095  
1096 +        return autoVars.size();
1097 +
1098 + }
1099 + //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
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 + //------------------------------------------------------------------

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines