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.12 by tschum, Tue Dec 8 10:04:35 2009 UTC vs.
Revision 1.14 by tschum, Thu Dec 10 18:29:27 2009 UTC

# Line 15 | Line 15 | PlotTool::PlotTool() {
15          showLegend = false;
16          logY = true;
17          addTrackJets = false;
18 +        addEventInfo = false;
19 +        addTower = false;
20          verbose = true;
21  
22          globalCuts="";
23  
24 +        recreateTree = false; //force recreation of friend tree
25 +
26   }
27  
28   //------------------------------------------------------------------
29   //Fill object PlotTool with Chains constructed from files from given source
30  
31   int PlotTool::init(string fileName, string dirPath, string treeName,
32 <                string fileLabel) {
33 <        this->New(this->GetEntries() );
34 <        int currChain = this->GetEntries() - 1;
32 >                   string fileLabel) {
33 >  this->New(this->GetEntries() );
34 >  int currChain = this->GetEntries() - 1;
35 >
36 >  if (currChain < 0)
37 >    return currChain;
38 >
39 >  TStopwatch timer;
40 >  timer.Start();
41 >
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 >
51 >  TList *files = new TList();
52 >  TSystemFile* sysFile = 0;
53 >  if(fileName.find(".") != string::npos) {
54 >    ifstream f(fileName.c_str());
55 >    if( ! f.is_open() ) return -1;
56 >    string line;
57 >
58 >    while (!f.eof()) {
59 >      getline(f,line);
60 >      sysFile = new TSystemFile(line.c_str(),dirPath.c_str());
61 >      files->Add(sysFile);
62 >    }
63 >
64 >
65 >  } else {
66 >    TSystemDirectory dir("sourceDir", dirPath.c_str());
67 >    files = dir.GetListOfFiles();
68 >  }
69 >  if (files->GetEntries()>0) {
70 >    TIter next(files);
71 >    TSystemFile *file;
72 >    TString fname;
73 >    string filePath;
74 >
75 >    if(verbose && fileName.find(".") == string::npos )  cout<<"Open"<<dirPath.c_str()<<" Search for .root files that contain: "
76 >                                                            <<fileName.c_str()<<endl;
77 >    if(verbose && fileName.find(".") != string::npos )  cout<<"Open"<<dirPath.c_str()<<" Search lines with .root in: "
78 >                                                            <<fileName.c_str()<<endl;
79 >
80 >
81 >    while ((file=(TSystemFile*)next())) {
82 >      fname = file->GetName();
83 >      if (!fname.EndsWith(".root"))
84 >        continue;
85 >      if (!fname.Contains(fileName.c_str()) && fileName.find(".") == string::npos )
86 >        continue;
87 >
88 >      filePath = dirPath;
89 >      filePath += fname.Data();
90 >      //fwlite::ChainEvent to lop over events, jets, etc
91 >      fileNames.push_back(filePath.c_str());
92 >
93 >      if (((TChain*) this->At(currChain))->AddFile(filePath.c_str(), -1,
94 >                                                   treeName.c_str()) )
95 >        if(verbose)     cout<<"Chained "<<((TChain*) this->At(currChain))->GetNtrees()<<" file(s) with "<<((TChain*) this->At(currChain))->GetEntries()<<" events."<<endl;
96 >        else
97 >          return -1;
98 >
99 >    }
100 >  } else
101 >    return -1;
102 >
103 >  for (int i=0; i<((TChain*) this->At(currChain))->GetListOfBranches()->GetEntries(); ++i) {
104 >
105 >    string s(((TChain*) this->At(currChain))->GetListOfBranches()->At(i)->GetName());
106 >
107 >    string branch_alias = s;
108 >    string branch_name = s;
109 >    if (s.find(".", s.size()-1) != string::npos)
110 >      branch_name += "obj";
111 >
112 >    size_t a = s.find("_");
113 >    if (a != string::npos) {
114 >      size_t b = s.find("_", a+1);
115 >      if (b != string::npos) {
116 >        size_t c = s.find("_", b+1);
117 >        if (c != string::npos) {
118 >          string _prod     =s.substr(0,a);
119 >          string _label    =s.substr(a+1, b-a-1);
120 >          string _instance =s.substr(b+1, c-b-1);
121 >          branch_alias = _label;
122 >          if(_instance.length() > 0 )  {
123 >            branch_alias += "_";
124 >            branch_alias += _instance;
125 >            ((TChain*) this->At(currChain))->SetAlias(_instance.c_str(),
126 >                                                      branch_name.c_str());
127 >          }
128 >          string branch_alias_full = _prod + "_" + branch_alias;
129 >          ((TChain*) this->At(currChain))->SetAlias(branch_alias_full.c_str(),
130 >                                                    branch_name.c_str());
131 >        }
132 >      }
133 >    }
134  
135 <        if (currChain < 0)
136 <                return currChain;
135 >    ((TChain*) this->At(currChain))->SetAlias(branch_alias.c_str(),
136 >                                              branch_name.c_str());
137  
138 <        TStopwatch timer;
36 <        timer.Start();
138 >  }
139  
140 +  if( addEventInfo || addTrackJets || addTower ) {
141  
142 <        fileNames.clear();
142 >    if(verbose)   cout<<"add friend tree with additional variables..."<<endl;
143  
144 <        //check if alternative label is different from default(searchString)
145 <        if (fileLabel=="") {
146 <                fileLabel=fileName;
147 <        }
148 <        ((TChain*) this->At(currChain))->SetName(fileLabel.c_str());
144 >    //make file for tree friends (adding additional, computed branches)
145 >    string friendFileName("/scratch/hh/current/cms/user/");
146 >    friendFileName += gSystem->GetUserInfo()->fUser;
147 >    friendFileName += "/temp/";
148 >    friendFileName += fileLabel;
149 >    friendFileName += ".root";
150 >    string fileOpt = "update";
151 >    if( recreateTree )    fileOpt = "recreate";
152  
153 <        TList *files = new TList();
48 <        TSystemFile* sysFile = 0;
49 <        if(fileName.find(".") != string::npos) {
50 <          ifstream f(fileName.c_str());
51 <          if( ! f.is_open() ) return -1;
52 <          string line;
153 >    TFile *f = new TFile(friendFileName.c_str(),fileOpt.c_str());
154  
155 <          while (!f.eof()) {
55 <            getline(f,line);
56 <            sysFile = new TSystemFile(line.c_str(),dirPath.c_str());
57 <            files->Add(sysFile);
58 <          }
155 >    if( f->IsZombie() ) return -1;
156  
157 +    string friendTreeName("friendTree");
158 +    //char number = currChain;
159 +    //friendTreeName += number;
160 +    //TTree *friendTree = new TTree("friendTree","friendTree");
161  
162 <        } else {
163 <        TSystemDirectory dir("sourceDir", dirPath.c_str());
63 <        files = dir.GetListOfFiles();
64 <        }
65 <        if (files->GetEntries()>0) {
66 <                TIter next(files);
67 <                TSystemFile *file;
68 <                TString fname;
69 <                string filePath;
70 <
71 <                if(verbose && fileName.find(".") == string::npos )      cout<<"Open"<<dirPath.c_str()<<" Search for .root files that contain: "
72 <                                <<fileName.c_str()<<endl;
73 <                if(verbose && fileName.find(".") != string::npos )      cout<<"Open"<<dirPath.c_str()<<" Search lines with .root in: "
74 <                                <<fileName.c_str()<<endl;
75 <
76 <
77 <                while ((file=(TSystemFile*)next())) {
78 <                        fname = file->GetName();
79 <                        if (!fname.EndsWith(".root"))
80 <                                continue;
81 <                        if (!fname.Contains(fileName.c_str()) && fileName.find(".") == string::npos )
82 <                                continue;
83 <
84 <                        filePath = dirPath;
85 <                        filePath += fname.Data();
86 <                        //fwlite::ChainEvent to lop over events, jets, etc
87 <                        fileNames.push_back(filePath.c_str());
88 <
89 <                        if (((TChain*) this->At(currChain))->AddFile(filePath.c_str(), -1,
90 <                                        treeName.c_str()) )
91 <                if(verbose)     cout<<"Chained "<<((TChain*) this->At(currChain))->GetNtrees()<<" file(s) with "<<((TChain*) this->At(currChain))->GetEntries()<<" events."<<endl;
92 <                        else
93 <                                return -1;
162 >    if(! f->FindKey(friendTreeName.c_str())) {
163 >      if(verbose)   cout<<"calculating additional variables..."<<endl;
164  
95                }
96        } else
97                return -1;
165  
166 <        for (int i=0; i<((TChain*) this->At(currChain))->GetListOfBranches()->GetEntries(); ++i) {
166 >      TTree *friendTree = new TTree(friendTreeName.c_str(),friendTreeName.c_str());
167 >      fwlite::ChainEvent ev(fileNames);
168 >      int _event, _run, _lumi;
169 >      int nJetsKT;
170 >      TrackJetKT = new float [100];
171  
172 <                string s(((TChain*) this->At(currChain))->GetListOfBranches()->At(i)->GetName());
172 >      //tower data
173 >      const int kMAX = 10000;
174 >      int NobjTowCal;
175 >      towet  = new float [ kMAX ];
176 >      toweta = new float [ kMAX ];
177 >      towphi = new float [ kMAX ];
178 >      towen  = new float [ kMAX ];
179 >      towem  = new float [ kMAX ];
180 >      towhd  = new float [ kMAX ];
181 >      towoe  = new float [ kMAX ];
182 >      towid_phi = new int[ kMAX ];
183 >      towid_eta = new int[ kMAX ];
184 >      towid     = new int[ kMAX ];
185 >      
186  
187 <                string branch_alias = s;
188 <                string branch_name = s;
105 <                if (s.find(".", s.size()-1) != string::npos)
106 <                        branch_name += "obj";
107 <
108 <                size_t a = s.find("_");
109 <                if (a != string::npos) {
110 <                  size_t b = s.find("_", a+1);
111 <                  if (b != string::npos) {
112 <                    size_t c = s.find("_", b+1);
113 <                    if (c != string::npos) {
114 <                      string _prod     =s.substr(0,a);
115 <                      string _label    =s.substr(a+1, b-a-1);
116 <                      string _instance =s.substr(b+1, c-b-1);
117 <                      branch_alias = _label;
118 <                      if(_instance.length() > 0 )  {
119 <                        branch_alias += "_";
120 <                        branch_alias += _instance;
121 <                        ((TChain*) this->At(currChain))->SetAlias(_instance.c_str(),
122 <                                                                  branch_name.c_str());
123 <                      }
124 <                      string branch_alias_full = _prod + "_" + branch_alias;
125 <                      ((TChain*) this->At(currChain))->SetAlias(branch_alias_full.c_str(),
126 <                                branch_name.c_str());
127 <                    }
128 <                  }
129 <                }
187 >      if( addEventInfo ) {
188 >
189  
190 <                ((TChain*) this->At(currChain))->SetAlias(branch_alias.c_str(),
191 <                                branch_name.c_str());
190 >        friendTree->Branch("event", &_event, "event/I");
191 >        friendTree->Branch("run", &_run, "run/I");
192 >        friendTree->Branch("lumi", &_lumi, "lumi/I");
193 >
194 >      }
195 >
196 >      if( addTrackJets ) {
197 >
198 >
199 >        friendTree->Branch("nJetsKT", &nJetsKT, "nJetsKT/I");
200 >        friendTree->Branch("TrackJetKT", TrackJetKT, "TrackJetKT[nJetsKT]/F");
201 >      }
202 >
203 >      if( addTower) {
204 >
205 >        // CaloTower branches
206 >        friendTree->Branch( "NobjTowCal",&NobjTowCal,"NobjTowCal/I"            );
207 >        friendTree->Branch( "TowId",     towid,      "TowId[NobjTowCal]/I"     );
208 >        friendTree->Branch( "TowId_phi", towid_phi,  "TowId_phi[NobjTowCal]/I" );
209 >        friendTree->Branch( "TowId_eta", towid_eta,  "TowId_eta[NobjTowCal]/I" );
210 >        friendTree->Branch( "TowEt",     towet,      "TowEt[NobjTowCal]/F"     );
211 >        friendTree->Branch( "TowEta",    toweta,     "TowEta[NobjTowCal]/F"    );
212 >        friendTree->Branch( "TowPhi",    towphi,     "TowPhi[NobjTowCal]/F"    );
213 >        friendTree->Branch( "TowE",      towen,      "TowE[NobjTowCal]/F"      );
214 >        friendTree->Branch( "TowEm",     towem,      "TowEm[NobjTowCal]/F"     );
215 >        friendTree->Branch( "TowHad",    towhd,      "TowHad[NobjTowCal]/F"    );
216 >        friendTree ->Branch( "TowOE",     towoe,      "TowOE[NobjTowCal]/F"     );
217 >
218 >
219 >      }
220 >
221 >
222 >      int tenth = ev.size() / 10;
223 >      int eventNum =0;
224 >      for (ev.toBegin(); !ev.atEnd(); ++ev, ++eventNum) {
225 >
226 >
227 >        if( addEventInfo ) {
228  
134        }
229  
230 <        // add branch with track  Jets
230 >          if(eventNum==0) {
231 >            //    fwlite::Handle<edm::TriggerResults> hltHandle;
232 >            //    hltHandle.getByLabel(ev, "TriggerResults");
233 >
234 >            //    fwlite::Handle<edm::TriggerResults> hTriggerResults;
235 >
236 >            //    hTriggerResults.getByLabel(ev, "TriggerResults","","TEST");
237 >            //    fwlite::TriggerNames const&  triggerNames = ev.triggerNames(*hTriggerResults);
238 >
239 >
240 >            //    //      std::vector<std::string> const& names = triggerNames.triggerNames();
241 >            //    for (unsigned i = 0; i < triggerNames.size(); ++i) {
242 >            //    std::cout << i << "  " << triggerNames.triggerName(i) << std::endl;
243 >            //    }
244 >          }
245 >
246 >          _event = ev.id().event();
247 >          _run   = ev.id().run();
248 >          _lumi = ev.luminosityBlock();
249 >
250 >        }
251  
252          if( addTrackJets ) {
253  
254 <          //make file for tree friends (adding additional, computed branches)
255 <          string friendFileName("/scratch/hh/current/cms/user/");
256 <          friendFileName += gSystem->GetUserInfo()->fUser;
257 <          friendFileName += "/temp/",
258 <          friendFileName += fileName;
259 <          friendFileName += ".root";
260 <          TFile *f = new TFile(friendFileName.c_str(),"recreate");
261 <
262 <          if( f->IsZombie() ) return -1;
263 <
264 <          string friendTreeName("friendTree");
265 <          //char number = currChain;
266 <          //friendTreeName += number;
267 <          //TTree *friendTree = new TTree("friendTree","friendTree");
268 <          TTree *friendTree = new TTree(friendTreeName.c_str(),friendTreeName.c_str());
269 <
270 <          int nJetsKT;
271 <          TrackJetKT = new float [100];
272 <          fwlite::ChainEvent ev(fileNames);
273 <
160 <          friendTree->Branch("nJetsKT", &nJetsKT, "nJetsKT/I");
161 <          friendTree->Branch("TrackJetKT", TrackJetKT, "TrackJetKT[nJetsKT]/F");
162 <
163 <          if(verbose)   cout<<"calculating additional variables..."<<endl;
164 <          
165 <          
166 <
167 <          int tenth = ev.size() / 10;
168 <          int eventNum =0;
169 <          for (ev.toBegin(); !ev.atEnd(); ++ev, ++eventNum) {
170 <
171 <            fwlite::Handle<reco::CaloJetCollection> jets;
172 <            jets.getByLabel(ev, "antikt5CaloJets");
173 <
174 <            fwlite::Handle<reco::TrackCollection> tracks;
175 <            tracks.getByLabel(ev, "generalTracks");
176 <
177 <            if (!jets.isValid())
178 <              continue;
179 <            if (!tracks.isValid())
180 <              continue;
181 <            double trackSum;
182 <            nJetsKT = 0;
183 <            for (reco::CaloJetCollection::const_iterator jet = jets->begin(); jet
184 <                   !=jets->end(); jet++) {
185 <              trackSum = 0;
186 <              for (reco::TrackCollection::const_iterator track = tracks->begin(); track
187 <                     !=tracks->end(); track++) {
188 <                if (deltaR(jet->eta(), jet->phi(), track->eta(), track->phi())
189 <                    < 0.5)
190 <                  trackSum += track->pt();
191 <              }
192 <              TrackJetKT[nJetsKT] = trackSum;
193 <              nJetsKT++;
254 >          fwlite::Handle<reco::CaloJetCollection> jets;
255 >          jets.getByLabel(ev, "ak5CaloJets");
256 >
257 >          fwlite::Handle<reco::TrackCollection> tracks;
258 >          tracks.getByLabel(ev, "generalTracks");
259 >
260 >          if (!jets.isValid())
261 >            continue;
262 >          if (!tracks.isValid())
263 >            continue;
264 >          double trackSum;
265 >          nJetsKT = 0;
266 >          for (reco::CaloJetCollection::const_iterator jet = jets->begin(); jet
267 >                 !=jets->end(); jet++) {
268 >            trackSum = 0;
269 >            for (reco::TrackCollection::const_iterator track = tracks->begin(); track
270 >                   !=tracks->end(); track++) {
271 >              if (deltaR(jet->eta(), jet->phi(), track->eta(), track->phi())
272 >                  < 0.5)
273 >                trackSum += track->pt();
274              }
275 <            friendTree->Fill();
275 >            TrackJetKT[nJetsKT] = trackSum;
276 >            nJetsKT++;
277 >          }
278 >        }
279 >        if( addTower) {
280  
281 <            if( eventNum != 0 && eventNum%tenth == 0) cout<<"Processed "<<eventNum <<" of "<<ev.size()<<" events. "<<endl;
281 >          fwlite::Handle<CaloTowerCollection> towers;
282 >          towers.getByLabel(ev, "towerMaker");
283  
284 +          int jtow = 0;
285 +          NobjTowCal=towers->size();
286 +          for(CaloTowerCollection::const_iterator tow = towers->begin();
287 +              tow != towers->end(); ++tow, ++jtow){
288 +            towet [jtow] = tow->et();
289 +            toweta[jtow] = tow->eta();
290 +            towphi[jtow] = tow->phi();
291 +            towen [jtow] = tow->energy();
292 +            towem [jtow] = tow->emEnergy();
293 +            towhd [jtow] = tow->hadEnergy();
294 +            towoe [jtow] = tow->outerEnergy();
295 +            towid_phi[jtow] = tow->id().iphi();
296 +            towid_eta[jtow] = tow->id().ieta();
297 +            towid [jtow] = tow->id().rawId();
298            }
299 <          f->cd();
300 <          friendTree->Write();
202 <          f->Close();
203 <          ((TChain*) this->At(currChain))->AddFriend(friendTreeName.c_str(),
204 <                                                     friendFileName.c_str());
299 >
300 >
301          }
302 +        friendTree->Fill();
303 +
304 +        if( eventNum != 0 && eventNum%tenth == 0) cout<<"Processed "<<eventNum <<" of "<<ev.size()<<" events. "<<endl;
305 +
306 +      }
307 +      f->cd();
308 +      friendTree->Write();
309 +    }
310 +    f->Close();
311 +    ((TChain*) this->At(currChain))->AddFriend(friendTreeName.c_str(),
312 +                                               friendFileName.c_str());
313 +
314 +
315  
316 <        timer.Stop();
317 <        if(verbose) timer.Print();
316 >  }
317 >
318 >
319 >  timer.Stop();
320 >  if(verbose) timer.Print();
321        
322 <        return this->GetEntries();
322 >  return this->GetEntries();
323  
324   }
325   //------------------------------------------------------------------
# Line 317 | Line 429 | int PlotTool::plot(int chainIndex, strin
429  
430          if(verbose && draw_err > 0) {
431            timer.Stop();
432 <          cout<<"Done: Selected "<<draw_err<<" of total "<< currN <<" events."<<endl;
432 >          cout<<"Done: Selected "<<draw_err<<" objects in "<< currN <<" processed events."<<endl;
433            timer.Print();
434          }
435          return draw_err;
# Line 401 | Line 513 | int PlotTool::loop(string histName, vect
513   int PlotTool::updatePads() {
514  
515          for (map< string, TCanvas* >::iterator it=pads_.begin() ; it != pads_.end(); ++it) {
516 <                if (gROOT->GetListOfCanvases()->FindObject((*it).first.c_str() ) ) {
517 <                        (*it).second->Draw();
518 <                        setCanvas((*it).second);
519 <                } else
520 <                        pads_.erase(it);
516 >                if ( ! gROOT->GetListOfCanvases()->FindObject((*it).first.c_str() ) ) {
517 >                  pads_.erase(it);
518 >                  continue;
519 >                }
520 >                if( (*it).second->GetListOfPrimitives()->GetEntries() == 0 ) {
521 >                  (*it).second->Close();
522 >                  pads_.erase(it);
523 >                  continue;
524 >                }
525 >                (*it).second->Draw();
526 >                setCanvas((*it).second);
527 >
528          }
529  
530          return pads_.size();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines