ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/cbrown/AnalysisFramework/Plotting/Modules/SampleClass.C
(Generate patch)

Comparing UserCode/cbrown/AnalysisFramework/Plotting/Modules/SampleClass.C (file contents):
Revision 1.4 by buchmann, Fri Jul 8 14:33:58 2011 UTC vs.
Revision 1.11 by buchmann, Mon Aug 15 06:59:32 2011 UTC

# Line 6 | Line 6
6   #include <TFile.h>
7   #include <TTree.h>
8   #include <TH1.h>
9 + #include <TH2.h>
10   #include <TCut.h>
11   #include <THStack.h>
12   #include <TColor.h>
# Line 86 | Line 87 | sample::sample(string m_filename, string
87      this->tfile = new TFile(m_filename.c_str());
88      gErrorIgnoreLevel=currlevel;
89      this->events=(TTree*)(this->tfile)->Get(treename.c_str());
90 <    if(Verbosity>0) cout << "The " << write_mc_or_data(is_data) << " file " << this->filename << " has been added successfully to the list of samples. " << endl;
90 >    if(Verbosity>0) dout << "The " << write_mc_or_data(is_data) << " file " << this->filename << " has been added successfully to the list of samples. " << endl;
91    }
92    else {
93      this->is_active=false;
# Line 100 | Line 101 | void sample::closeFile()
101      (this->tfile)->Close();
102    }
103    else {
104 <    cout << "SAMPLE " << this->samplename << " cannot be closed as the underlying file (" << this->filename << ") does not exist!" << endl;
104 >    dout << "SAMPLE " << this->samplename << " cannot be closed as the underlying file (" << this->filename << ") does not exist!" << endl;
105      this->is_active=false;
106    }
107   }
# Line 113 | Line 114 | bool doesROOTFileExist(string filename)
114    TFile *f = new TFile(filename.c_str());
115    
116    if (f->IsZombie()) {
117 <    cout << "Error opening file" << filename << endl;
117 >    dout << "Error opening file" << filename << endl;
118      return 0;
119    }
120    f->Close();
# Line 123 | Line 124 | bool doesROOTFileExist(string filename)
124   //********************************************************
125  
126   TCut essentialcut("mll>0");
127 + // This will reweight all the events: use "weight" to correct MC for pileUP
128 + TCut cutWeight("weight");
129  
130   void setessentialcut(TCut ess) {
131    essentialcut=ess;
# Line 149 | Line 152 | public:
152    TH1F* Draw(string m_histoname,string m_var, int m_nbins, float m_minx, float m_maxx, string m_xlabel, string m_ylabel, TCut Cut, bool m_is_data, float luminosity, bool drawsignal);
153    TH1F* Draw(string m_histoname,string m_var, int m_nbins, float m_minx, float m_maxx, string m_xlabel, string m_ylabel, TCut Cut, bool m_is_data, float luminosity, vector<int> onlyindex, bool drawsignal);
154    
155 +  TH2F* Draw(string m_histoname,string m_var, vector<float> binningx, vector<float> binningy, string m_xlabel, string m_ylabel, TCut Cut, bool m_is_data, float luminosity, vector<int> onlyindex, bool drawsignal);
156 +  
157    vector<float> get_optimal_binsize(string variable, TCut cut,int nbins, float low, float high);
158      
159    THStack DrawStack(string m_histoname,string m_var, int m_nbins, float m_minx, float m_maxx, string m_xlabel, string m_ylabel, TCut Cut, bool m_is_data, float luminosity, bool drawsignal);
160 +  THStack DrawStack(string m_histoname,string m_var, vector<float> binning, string m_xlabel, string m_ylabel, TCut Cut, bool m_is_data, float luminosity, bool drawsignal);
161    vector<int> FindSample(string what);
162    void ListSamples();
163    bool do_sample(int thissample, vector<int> &selected_samples);
# Line 168 | Line 174 | public:
174   samplecollection::samplecollection(string m_name)
175   {
176    this->name=m_name;
177 <  if(Verbosity>0) cout << "Initiated sample collection " << this->name << " " << endl;
177 >  if(Verbosity>0) dout << "Initiated sample collection " << this->name << " " << endl;
178   }
179  
180   void samplecollection::ListSamples()
181   {
182 <  cout << "---------------------------------------------------------------------------------------------------" << endl;
183 <  cout << "Listing all " << this->nsamples << " sample(s) of the sample collection called " << this->name << " : " << endl;
182 >  dout << "---------------------------------------------------------------------------------------------------" << endl;
183 >  dout << "Listing all " << this->nsamples << " sample(s) of the sample collection called " << this->name << " : " << endl;
184    if(this->ndatasamples>0) {
185 <    cout << "Data sample(s): " << endl;
185 >    dout << "Data sample(s): " << endl;
186      for (int isamp=0;isamp<this->collection.size();isamp++)
187        {
188 <        if((this->collection)[isamp].is_data) cout << " - " << (this->collection)[isamp].samplename << " from " << (this->collection)[isamp].filename << endl;
188 >        if((this->collection)[isamp].is_data) dout << " - " << (this->collection)[isamp].samplename << " from " << (this->collection)[isamp].filename << endl;
189        }
190    }
191    if(this->nmcsamples>0) {
192 <    cout << "MC sample(s): " << endl;
192 >    dout << "MC sample(s): " << endl;
193      for (int isamp=0;isamp<this->collection.size();isamp++)
194        {
195 <        if(!(this->collection)[isamp].is_data) cout << " - " << (this->collection)[isamp].samplename << " from " << (this->collection)[isamp].filename << endl;
195 >        if(!(this->collection)[isamp].is_data) dout << " - " << (this->collection)[isamp].samplename << " from " << (this->collection)[isamp].filename << endl;
196        }
197    }
198 <  cout << "---------------------------------------------------------------------------------------------------" << endl;
198 >  dout << "---------------------------------------------------------------------------------------------------" << endl;
199   }
200      
201   void samplecollection::AddSample(string m_filename,string m_samplename,int m_Nentries, float m_xs,bool is_data, bool m_is_signal, int groupindex, Color_t newcolor)
# Line 236 | Line 242 | TH1F* samplecollection::Draw(string m_hi
242   }
243    
244   TH1F* samplecollection::Draw(string m_histoname,string m_var, vector<float> binning, string m_xlabel, string m_ylabel, TCut Cut, bool m_is_data, float luminosity, vector<int> onlyindex, bool drawsignal=false) {
245 <  if(Verbosity>0) cout << endl << endl;
246 <  if(Verbosity>0) cout << "-------------------------------------------------------------------------------------" << endl;
247 <  if(Verbosity>0) cout << "histoname : " << m_histoname << " , m_var = " << m_var << ", m_xlabel=" << m_xlabel << " m_ylabel=" << m_ylabel << " m_is_data = " << m_is_data << " lumi = " << luminosity << " onlyindex size: " << onlyindex.size() << endl;
248 <  if(HUSH==0) cout << "Drawing histo called " << m_histoname << "... " << endl;
245 >  if(Verbosity>0) dout << endl << endl;
246 >  if(Verbosity>0) dout << "-------------------------------------------------------------------------------------" << endl;
247 >  if(Verbosity>0) dout << "histoname : " << m_histoname << " , m_var = " << m_var << ", m_xlabel=" << m_xlabel << " m_ylabel=" << m_ylabel << " m_is_data = " << m_is_data << " lumi = " << luminosity << " onlyindex size: " << onlyindex.size() << endl;
248 >  if(HUSH==0) dout << "Drawing histo called " << m_histoname << "... " << endl;
249    bool do_only_selected_samples=false;
250    if(onlyindex.size()>0&&onlyindex[0]!=-1) {
251 <    if(Verbosity>0) {cout << "Requested to only draw sample corresponding to the following sample(s) : " << endl;}
251 >    if(Verbosity>0) {dout << "Requested to only draw sample corresponding to the following sample(s) : " << endl;}
252      for(int is=0;is<onlyindex.size();is++) {
253 <      if(Verbosity>0) cout << "   - " << (this->collection)[onlyindex[is]].filename << " (sample index: " << onlyindex[is] << ")" << endl;
253 >      if(Verbosity>0) dout << "   - " << (this->collection)[onlyindex[is]].filename << " (sample index: " << onlyindex[is] << ")" << endl;
254      }
255      do_only_selected_samples=true;
256    }
# Line 275 | Line 281 | TH1F* samplecollection::Draw(string m_hi
281      TH1F *tempdrawhisto = new TH1F("tempdrawhisto","tempdrawhisto",binning.size()-1,binningarray);
282      tempdrawhisto->Sumw2();
283      if(use_this_sample) {
284 <      if(Verbosity>0) cout << "[samplecollection::Draw] : Added contribution from sample " << (this->collection)[isample].samplename << endl;
285 <      (this->collection)[isample].events->Draw(drawthis.str().c_str(),(essentialcut&&Cut)*"weight");//this weight is based on PU etc. not XS
284 >      if(Verbosity>0) dout << "[samplecollection::Draw] : Added contribution from sample " << (this->collection)[isample].samplename << endl;
285 >      (this->collection)[isample].events->Draw(drawthis.str().c_str(),(essentialcut&&Cut)*cutWeight);//this weight is based on PU etc. not XS
286 >      if(addoverunderflowbins) {
287 >        //now also adding the overflow & underflow bins:
288 >        tempdrawhisto->SetBinContent(tempdrawhisto->GetNbinsX(),tempdrawhisto->GetBinContent(tempdrawhisto->GetNbinsX()+1));
289 >        tempdrawhisto->SetBinError(tempdrawhisto->GetNbinsX(),TMath::Sqrt(tempdrawhisto->GetBinContent(tempdrawhisto->GetNbinsX())));
290 >        tempdrawhisto->SetBinContent(1,tempdrawhisto->GetBinContent(0));
291 >        tempdrawhisto->SetBinError(1,TMath::Sqrt(tempdrawhisto->GetBinContent(1)));
292 >      }
293 >
294 >      if(!(this->collection)[isample].is_data) tempdrawhisto->Scale(luminosity*((this->collection)[isample].weight));//weight applied here is XS & N(entries)
295 >      histo->Add(tempdrawhisto);
296 >    }
297 >    tempdrawhisto->Delete();
298 >  }//end of loop over isample
299 >  if(Verbosity>0) dout << "Histo has been filled and now contains " << histo->Integral() << " points (integral)" << endl;
300 >  histo->GetXaxis()->SetTitle(m_xlabel.c_str());
301 >  histo->GetYaxis()->SetTitle(m_ylabel.c_str());
302 >  histo->GetXaxis()->CenterTitle();
303 >  histo->GetYaxis()->CenterTitle();
304 >  if(do_only_selected_samples) histo->SetLineColor(this->GetColor(onlyindex[0]));
305 >  return histo;
306 > }
307 >
308 > TH2F* samplecollection::Draw(string m_histoname,string m_var, vector<float> binningx, vector<float> binningy, string m_xlabel, string m_ylabel, TCut Cut, bool m_is_data, float luminosity, vector<int> onlyindex, bool drawsignal=false) {
309 >  if(Verbosity>0) dout << endl << endl;
310 >  if(Verbosity>0) dout << "-------------------------------------------------------------------------------------" << endl;
311 >  if(Verbosity>0) dout << "histoname : " << m_histoname << " , m_var = " << m_var << ", m_xlabel=" << m_xlabel << " m_ylabel=" << m_ylabel << " m_is_data = " << m_is_data << " lumi = " << luminosity << " onlyindex size: " << onlyindex.size() << endl;
312 >  if(HUSH==0) dout << "Drawing histo called " << m_histoname << "... " << endl;
313 >  bool do_only_selected_samples=false;
314 >  if(onlyindex.size()>0&&onlyindex[0]!=-1) {
315 >    if(Verbosity>0) {dout << "Requested to only draw sample corresponding to the following sample(s) : " << endl;}
316 >    for(int is=0;is<onlyindex.size();is++) {
317 >      if(Verbosity>0) dout << "   - " << (this->collection)[onlyindex[is]].filename << " (sample index: " << onlyindex[is] << ")" << endl;
318 >    }
319 >    do_only_selected_samples=true;
320 >  }
321 >  if(onlyindex.size()==1&&onlyindex[0]==-1) {
322 >    do_only_selected_samples=true; // this is the special case when we request a non-existing sample - we shouldn't draw anything then.
323 >    onlyindex.clear();
324 >  }
325 >  stringstream h_histoname;
326 >  h_histoname<<"h_"<<m_histoname;
327 >  float binningarray[binningx.size()+1];
328 >  float binningyarray[binningy.size()+1];
329 >  for(int i=0;i<binningx.size();i++) {binningarray[i]=binningx[i];binningyarray[i]=binningy[i];}
330 >  TH2F *histo = new TH2F(m_histoname.c_str(),"",binningx.size()-1,binningarray,binningy.size(),binningyarray);
331 >  histo->Sumw2();
332 >  
333 >  stringstream drawthis;
334 >  drawthis<<m_var<<">>tempdrawhisto";
335 >  
336 >  for (unsigned int isample=0;isample<(this->collection).size();isample++) {
337 >    bool use_this_sample=false;
338 >    if(!(this->collection)[isample].is_active) continue; // skip inactive samples right away
339 >    if(((this->collection)[isample].is_data==m_is_data)&&(!do_only_selected_samples)) {
340 >      if(drawsignal==false&&(this->collection)[isample].is_signal==true) continue;
341 >      use_this_sample=true;
342 >    }
343 >    if(do_only_selected_samples&&this->do_sample(isample,onlyindex)) {
344 >      use_this_sample=true;
345 >    }
346 >    TH2F *tempdrawhisto = new TH2F("tempdrawhisto","tempdrawhisto",binningx.size()-1,binningarray,binningy.size()-1,binningyarray);
347 >    tempdrawhisto->Sumw2();
348 >    if(use_this_sample) {
349 >      if(Verbosity>0) dout << "[samplecollection::Draw] : Added contribution from sample " << (this->collection)[isample].samplename << endl;
350 >      (this->collection)[isample].events->Draw(drawthis.str().c_str(),(essentialcut&&Cut)*cutWeight);//this weight is based on PU etc. not XS
351        if(addoverunderflowbins) {
352          //now also adding the overflow & underflow bins:
353          tempdrawhisto->SetBinContent(tempdrawhisto->GetNbinsX(),tempdrawhisto->GetBinContent(tempdrawhisto->GetNbinsX()+1));
# Line 290 | Line 361 | TH1F* samplecollection::Draw(string m_hi
361      }
362      tempdrawhisto->Delete();
363    }//end of loop over isample
364 <  if(Verbosity>0) cout << "Histo has been filled and now contains " << histo->Integral() << " points (integral)" << endl;
364 >  if(Verbosity>0) dout << "Histo has been filled and now contains " << histo->Integral() << " points (integral)" << endl;
365    histo->GetXaxis()->SetTitle(m_xlabel.c_str());
366    histo->GetYaxis()->SetTitle(m_ylabel.c_str());
367    histo->GetXaxis()->CenterTitle();
# Line 311 | Line 382 | TH1F* samplecollection::Draw(string m_hi
382    TH1F *histo = this->Draw(m_histoname,m_var, binning, m_xlabel, m_ylabel, Cut, m_is_data, luminosity, onlyindex,drawsignal);
383    return histo;
384    }
385 < THStack samplecollection::DrawStack(string m_histoname,string m_var, int m_nbins, float m_minx, float m_maxx, string m_xlabel, string m_ylabel, TCut Cut, bool m_is_data, float luminosity, bool drawsignal=false)
386 < {
385 >
386 > THStack samplecollection::DrawStack(string m_histoname,string m_var, vector<float> binning, string m_xlabel, string m_ylabel, TCut Cut, bool m_is_data, float luminosity, bool drawsignal=false) {
387    stringstream h_histoname;
388    h_histoname<<"h_"<<m_histoname;
389    THStack thestack("thestack",m_histoname.c_str());
# Line 324 | Line 395 | THStack samplecollection::DrawStack(stri
395    int bookedhistos[this->ngroups+1];
396    for(int ih=0;ih<=this->ngroups;ih++) bookedhistos[ih]=0;
397    
398 +  float binningarray[binning.size()+1];
399 +  for(int i=0;i<binning.size();i++) {binningarray[i]=binning[i];}
400 +  
401    for (unsigned int isample=0;isample<(this->collection).size();isample++) {
402      if(!drawsignal&&(this->collection)[isample].is_signal) continue;
403      if((this->collection)[isample].is_active&&(this->collection)[isample].is_data==m_is_data) {//fills mc if we want mc, else fills data.
404 <      TH1F *tempdrawhisto = new TH1F("tempdrawhisto","",m_nbins,m_minx,m_maxx);
405 <      (this->collection)[isample].events->Draw(drawthis.str().c_str(),(essentialcut&&Cut)*"weight");
404 >      TH1F *tempdrawhisto = new TH1F("tempdrawhisto","",binning.size()-1,binningarray);
405 >      (this->collection)[isample].events->Draw(drawthis.str().c_str(),(essentialcut&&Cut)*cutWeight);
406        
407        if(addoverunderflowbins) {
408          //now also adding the overflow & underflow bins:
# Line 376 | Line 450 | THStack samplecollection::DrawStack(stri
450    return thestack;
451   }
452  
453 + THStack samplecollection::DrawStack(string m_histoname,string m_var, int m_nbins, float m_minx, float m_maxx, string m_xlabel, string m_ylabel, TCut Cut, bool m_is_data, float luminosity, bool
454 + drawsignal=false)
455 + {
456 +  vector<float> binning;
457 +  for(int i=0;i<=m_nbins;i++)
458 +  {
459 +    binning.push_back(((float)(m_maxx-m_minx)/((float)m_nbins))*i+m_minx);
460 +  }
461 +  return this->DrawStack(m_histoname,m_var, binning, m_xlabel, m_ylabel, Cut, m_is_data, luminosity, drawsignal);
462 +  }
463 +
464  
465   vector<int> samplecollection::FindSample(string what)
466   {
# Line 384 | Line 469 | vector<int> samplecollection::FindSample
469    {
470      if(((this->collection)[isam].filename).find(what)!=string::npos) {
471        hitcollection.push_back(isam);
472 +    } else {
473      }
388    else {
389    }
474    }
475 <  if(hitcollection.size()==0) hitcollection.push_back(-1);
475 >  if(hitcollection.size()==0) {
476 >    hitcollection.push_back(-1);
477 >    write_warning(__FUNCTION__,"Couldn't find sample "+string(what)+": using in sample collection"+string(this->name));
478 >  }
479    return hitcollection;
480   }
481  
# Line 404 | Line 491 | Color_t samplecollection::GetColor(int s
491  
492   TLegend* samplecollection::allbglegend(string title, TH1F *data,float posx=0.65, float posy=0.60) {
493   //  TLegend *leg = new TLegend(0.65,0.60,0.89,0.77);
494 <  TLegend *leg = new TLegend(posx,posy,0.89,0.77);
494 >  TLegend *leg = new TLegend(posx,posy,0.89,0.89);
495    if(title!="") leg->SetHeader(title.c_str());
496    if(data->GetName()!="nothing") leg->AddEntry(data,"Data","p");
497    leg->SetFillColor(kWhite);
# Line 440 | Line 527 | TLegend* samplecollection::allbglegend(s
527  
528  
529   void set_treename(string treen="events") {
530 <  cout << "Treename has been set to " << treen << endl;
530 >  dout << "Treename has been set to " << treen << endl;
531    if(treen=="PFevents"||treen=="events") treename=treen;
532    else write_error("sample::set_treename","Setting the treename failed as you provided an invalid tree name.");
533   }
# Line 477 | Line 564 | void samplecollection::PickUpFromThisFil
564    (this->collection)[isamp].events->SetBranchAddress("eventNum",&eventNum);
565    (this->collection)[isamp].events->SetBranchAddress("lumi",&lumi);
566    (this->collection)[isamp].events->SetBranchAddress("runNum",&runNum);
567 +
568    
569 <  TTreeFormula *select = new TTreeFormula("select", "pfJetGoodNum>=3&&jzb[1]>100&&jzb[1]<150&&id1==id2", (this->collection)[isamp].events);
569 >  TTreeFormula *select = new TTreeFormula("select", cut.c_str()&&essentialcut, (this->collection)[isamp].events);
570 >  int npickedup=0;
571    for (Int_t entry = 0 ; entry < (this->collection)[isamp].events->GetEntries() ; entry++) {
572     (this->collection)[isamp].events->LoadTree(entry);
573     if (select->EvalInstance()) {
574       (this->collection)[isamp].events->GetEntry(entry);
575 <     cout << runNum << ":" << lumi << ":" << eventNum << endl;
575 >     dout << runNum << ":" << lumi << ":" << eventNum << endl;
576 >     npickedup++;
577     }
578    }
579 +  dout << "Printed out a total of " << npickedup << " events" << endl;
580   }
581  
582    
# Line 506 | Line 597 | void samplecollection::PickUpEvents(stri
597  
598   void switch_overunderflow(bool newpos=false) {
599    addoverunderflowbins=newpos;
600 < }
600 > }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines