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.14 by tschum, Thu Dec 10 18:29:27 2009 UTC vs.
Revision 1.18 by tschum, Wed Feb 10 11:41:57 2010 UTC

# Line 14 | Line 14 | PlotTool::PlotTool() {
14  
15          showLegend = false;
16          logY = true;
17        addTrackJets = false;
18        addEventInfo = false;
19        addTower = false;
17          verbose = true;
18  
19          globalCuts="";
20  
21 +        varBlockNames = "";   //Set to "ALL" or add Names, i.e. varBlockNames += "CaloTower"
22          recreateTree = false; //force recreation of friend tree
23  
24   }
# Line 91 | Line 89 | int PlotTool::init(string fileName, stri
89        fileNames.push_back(filePath.c_str());
90  
91        if (((TChain*) this->At(currChain))->AddFile(filePath.c_str(), -1,
92 <                                                   treeName.c_str()) )
92 >                                                   treeName.c_str()) ) {
93          if(verbose)     cout<<"Chained "<<((TChain*) this->At(currChain))->GetNtrees()<<" file(s) with "<<((TChain*) this->At(currChain))->GetEntries()<<" events."<<endl;
94 <        else
95 <          return -1;
94 >      } else {
95 >        return -1;
96 >      }
97  
98      }
99    } else
# Line 137 | Line 136 | int PlotTool::init(string fileName, stri
136  
137    }
138  
140  if( addEventInfo || addTrackJets || addTower ) {
141
142    if(verbose)   cout<<"add friend tree with additional variables..."<<endl;
139  
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    TFile *f = new TFile(friendFileName.c_str(),fileOpt.c_str());
154
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    if(! f->FindKey(friendTreeName.c_str())) {
163      if(verbose)   cout<<"calculating additional variables..."<<endl;
164
165
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      //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      
140  
187      if( addEventInfo ) {
141  
142 +    
143 +  string friendTreeName("friendTree");
144  
145 <        friendTree->Branch("event", &_event, "event/I");
146 <        friendTree->Branch("run", &_run, "run/I");
147 <        friendTree->Branch("lumi", &_lumi, "lumi/I");
148 <
149 <      }
150 <
151 <      if( addTrackJets ) {
152 <
153 <
154 <        friendTree->Branch("nJetsKT", &nJetsKT, "nJetsKT/I");
155 <        friendTree->Branch("TrackJetKT", TrackJetKT, "TrackJetKT[nJetsKT]/F");
156 <      }
157 <
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 <
229 <
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 <          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 <            TrackJetKT[nJetsKT] = trackSum;
276 <            nJetsKT++;
277 <          }
278 <        }
279 <        if( addTower) {
280 <
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 <
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();
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 >  FWliteVariables fwVars(friendTreeName, friendFileName, fileOpt);
154 >
155 >  if(varBlockNames.length() > 1 ) {
156 >    fwVars.loop(fileNames, varBlockNames);
157 >    if(verbose)   cout<<"add friend tree with additional variables: "<<varBlockNames<<endl;
158      ((TChain*) this->At(currChain))->AddFriend(friendTreeName.c_str(),
159                                                 friendFileName.c_str());
160 <
161 <
315 <
160 >  } else {
161 >    if(verbose)   cout<<"No additional variables added."<<endl;
162    }
163  
164 +  
165 +    
166 +
167  
168    timer.Stop();
169    if(verbose) timer.Print();
# Line 963 | Line 812 | int PlotTool::setVariables(string label)
812          return autoVars.size();
813  
814   }
966 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
967 //Draw efficiencies
968 int  PlotTool::plotEff(int chainIndex, string histName, string cutName, int nEntries, double fitXmin, double fitXmax, string fitFormula)
969 {
970
971  if( chainIndex >= this->GetEntries() ) return -1;
972
973  int currN = nEntries;
974  if(nEntries < 0) currN = ((TChain*) this->At(chainIndex))->GetEntries(); //nEntries<0 : all entries are plotted!
975
976  //++++ Create and name Canvases according to global variables +++++++++++++
977  ostringstream currHistName;
978  if( samePad_trees) currHistName<<((TChain*) this->At(chainIndex))->GetName()<<":";
979  if( samePad_vars)  currHistName<<histName;
980  if( samePad_cuts)  currHistName<<"{"<<cutName<<"}";
981
982  ostringstream currPadName;
983  if(! samePad_trees) currPadName<<((TChain*) this->At(chainIndex))->GetName()<<":";
984  if(! samePad_vars)  currPadName<<histName;
985  if(! samePad_cuts)  currPadName<<"{"<<cutName<<"}";
986
987
988  //Draw total histogram:
989  if( ! pads_["total"] || ! gROOT->FindObject("total") ) {
990    pads_["total"] = new TCanvas("total", "total");
991  } else {
992    pads_["total"]->cd();
993  }
994  ostringstream bins_total;
995  bins_total<<histName;
996  bins_total<<">>total(100,0,1000)";
997
998
999  int draw_err = ((TChain*) this->At(chainIndex))->Draw(bins_total.str().c_str(), "", "", currN);
1000  if( draw_err <= 0 ) return draw_err;
1001
1002
1003  TH1* total = ((TH1*) pads_["total"]->GetPrimitive("total"));
1004
1005  ostringstream bins_bkg;
1006  bins_bkg<<histName;
1007  bins_bkg<<">>bkg(";
1008  bins_bkg<<total->GetNbinsX();
1009  bins_bkg<<",";
1010  bins_bkg<<total->GetXaxis()->GetXmin();
1011  bins_bkg<<",";
1012  bins_bkg<<total->GetXaxis()->GetXmax();
1013  bins_bkg<<")";
1014
1015  //Draw bkg histogram:
1016  if( ! pads_["bkg"] || ! gROOT->FindObject("bkg") ) {
1017    pads_["bkg"] = new TCanvas("bkg", currPadName.str().c_str());
1018  } else {
1019    pads_["bkg"]->cd();
1020  }
1021
1022  draw_err = ((TChain*) this->At(chainIndex))->Draw(bins_bkg.str().c_str(), cutName.c_str(),"" , currN);
1023  if( draw_err <= 0 ) return draw_err;
1024
1025
1026  TH1* bkg = ((TH1*) pads_["bkg"]->GetPrimitive("bkg"));
1027
1028  //Draw pass histogram:
1029  ostringstream bins_pass;
1030  bins_pass<<histName;
1031  bins_pass<<">>pass(";
1032  bins_pass<<total->GetNbinsX();
1033  bins_pass<<",";
1034  bins_pass<<total->GetXaxis()->GetXmin();
1035  bins_pass<<",";
1036  bins_pass<<total->GetXaxis()->GetXmax();
1037  bins_pass<<")";
1038
1039  ostringstream cut_pass;
1040  cut_pass<<"!(";
1041  cut_pass<<cutName;
1042  cut_pass<<")";
1043  
1044
1045  if( ! pads_["pass"] || ! gROOT->FindObject("pass") ) {
1046    pads_["pass"] = new TCanvas("pass", currPadName.str().c_str());
1047  } else {
1048    pads_["pass"]->cd();
1049  }
1050
1051  draw_err = ((TChain*) this->At(chainIndex))->Draw(bins_pass.str().c_str(),cut_pass.str().c_str() ,"" , currN);
1052  if( draw_err <= 0 ) return draw_err;
1053
1054
1055  TH1* pass = ((TH1*) pads_["pass"]->GetPrimitive("pass"));
815  
1057
1058  currPadName<<"Eff";
1059  //Draw Efficiency Graph:
1060  if( ! pads_["EffGraph"] || ! gROOT->FindObject("EffGraph") ) {
1061    pads_["EffGraph"] = new TCanvas("EffGraph", currPadName.str().c_str());
1062  } else {
1063    pads_["EffGraph"]->cd();
1064  }
1065
1066  TGraphAsymmErrors* EffGraph = new TGraphAsymmErrors(bkg, total);
1067  EffGraph->SetName(currHistName.str().c_str());
1068
1069  TF1* reverse = new TF2("reverse","1/y-1",total->GetXaxis()->GetXmin(),total->GetXaxis()->GetXmax());
1070  EffGraph->Apply(reverse);
1071  EffGraph->Draw("A*");
1072
1073  TF1* fitfunc = new TF1("fitfunc",fitFormula.c_str(),fitXmin,fitXmax);
1074  EffGraph->Fit("fitfunc","R+");
1075
1076
1077  //Save fit function
1078
1079  ostringstream savefuncName;
1080  savefuncName<<histName;
1081  savefuncName<<"_";
1082
1083  if(cutName.find("<") != std::string::npos ) cutName.replace(cutName.find("<"),1,"_");
1084  if(cutName.find(">") != std::string::npos ) cutName.replace(cutName.find(">"),1,"_");
1085  savefuncName<<cutName;
1086
1087
1088  TFile file("ABCDFunctions.root","UPDATE");
1089  TF1* savefunc = new TF1(savefuncName.str().c_str(), fitfunc->GetExpFormula().Data(),0,10000);
1090  savefunc->SetParameters( fitfunc->GetParameters() );
1091  savefunc->SetParErrors ( fitfunc->GetParErrors() );
1092
1093  //Show results
1094  if( ! pads_["abcd"] || ! gROOT->FindObject("abcd") ) {
1095    pads_["abcd"] = new TCanvas("abcd", "abcd");
1096  } else {
1097    pads_["abcd"]->cd();
1098  }
1099
1100  bkg->Multiply(savefunc);
1101  bkg->Draw();
1102
1103  //  total->Add(bkg,-1);
1104  pass->SetLineColor(kRed);
1105  pass->Draw("sames,e");
1106  pads_["abcd"]->SetLogy();
1107
1108
1109  savefunc->Write();
1110  //  file.Close();
1111
1112
1113  return draw_err;
1114
1115 }
816   //------------------------------------------------------------------

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines