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.15 by fronga, Thu Aug 25 08:44:43 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, "1.0" otherwise
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 163 | Line 169 | public:
169    
170    void PickUpFromThisFile(int isamp, string cut, vector<string> &output, vector<string> &pickupfile);
171    void PickUpEvents(string cut);
172 +  string find_units(string&);
173   };
174  
175   samplecollection::samplecollection(string m_name)
176   {
177    this->name=m_name;
178 <  if(Verbosity>0) cout << "Initiated sample collection " << this->name << " " << endl;
178 >  if(Verbosity>0) dout << "Initiated sample collection " << this->name << " " << endl;
179   }
180  
181   void samplecollection::ListSamples()
182   {
183 <  cout << "---------------------------------------------------------------------------------------------------" << endl;
184 <  cout << "Listing all " << this->nsamples << " sample(s) of the sample collection called " << this->name << " : " << endl;
183 >  dout << "---------------------------------------------------------------------------------------------------" << endl;
184 >  dout << "Listing all " << this->nsamples << " sample(s) of the sample collection called " << this->name << " : " << endl;
185    if(this->ndatasamples>0) {
186 <    cout << "Data sample(s): " << endl;
186 >    dout << "Data sample(s): " << endl;
187      for (int isamp=0;isamp<this->collection.size();isamp++)
188        {
189 <        if((this->collection)[isamp].is_data) cout << " - " << (this->collection)[isamp].samplename << " from " << (this->collection)[isamp].filename << endl;
189 >        if((this->collection)[isamp].is_data) dout << " - " << (this->collection)[isamp].samplename << " from " << (this->collection)[isamp].filename << endl;
190        }
191    }
192    if(this->nmcsamples>0) {
193 <    cout << "MC sample(s): " << endl;
193 >    dout << "MC sample(s): " << endl;
194      for (int isamp=0;isamp<this->collection.size();isamp++)
195        {
196 <        if(!(this->collection)[isamp].is_data) cout << " - " << (this->collection)[isamp].samplename << " from " << (this->collection)[isamp].filename << endl;
196 >        if(!(this->collection)[isamp].is_data) dout << " - " << (this->collection)[isamp].samplename << " from " << (this->collection)[isamp].filename << endl;
197        }
198    }
199 <  cout << "---------------------------------------------------------------------------------------------------" << endl;
199 >  dout << "---------------------------------------------------------------------------------------------------" << endl;
200   }
201      
202   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 243 | TH1F* samplecollection::Draw(string m_hi
243   }
244    
245   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) {
246 <  if(Verbosity>0) cout << endl << endl;
247 <  if(Verbosity>0) cout << "-------------------------------------------------------------------------------------" << endl;
248 <  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;
249 <  if(HUSH==0) cout << "Drawing histo called " << m_histoname << "... " << endl;
246 >  if(Verbosity>0) dout << endl << endl;
247 >  if(Verbosity>0) dout << "-------------------------------------------------------------------------------------" << endl;
248 >  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;
249 >  if(HUSH==0) dout << "Drawing histo called " << m_histoname << "... " << endl;
250    bool do_only_selected_samples=false;
251    if(onlyindex.size()>0&&onlyindex[0]!=-1) {
252 <    if(Verbosity>0) {cout << "Requested to only draw sample corresponding to the following sample(s) : " << endl;}
252 >    if(Verbosity>0) {dout << "Requested to only draw sample corresponding to the following sample(s) : " << endl;}
253      for(int is=0;is<onlyindex.size();is++) {
254 <      if(Verbosity>0) cout << "   - " << (this->collection)[onlyindex[is]].filename << " (sample index: " << onlyindex[is] << ")" << endl;
254 >      if(Verbosity>0) dout << "   - " << (this->collection)[onlyindex[is]].filename << " (sample index: " << onlyindex[is] << ")" << endl;
255      }
256      do_only_selected_samples=true;
257    }
# Line 275 | Line 282 | TH1F* samplecollection::Draw(string m_hi
282      TH1F *tempdrawhisto = new TH1F("tempdrawhisto","tempdrawhisto",binning.size()-1,binningarray);
283      tempdrawhisto->Sumw2();
284      if(use_this_sample) {
285 <      if(Verbosity>0) cout << "[samplecollection::Draw] : Added contribution from sample " << (this->collection)[isample].samplename << endl;
286 <      (this->collection)[isample].events->Draw(drawthis.str().c_str(),(essentialcut&&Cut)*"weight");//this weight is based on PU etc. not XS
285 >      if(Verbosity>0) dout << "[samplecollection::Draw] : Added contribution from sample " << (this->collection)[isample].samplename << endl;
286 >      (this->collection)[isample].events->Draw(drawthis.str().c_str(),(essentialcut&&Cut)*cutWeight);//this weight is based on PU etc. not XS
287 >      if(addoverunderflowbins) {
288 >        //now also adding the overflow & underflow bins:
289 >        tempdrawhisto->SetBinContent(tempdrawhisto->GetNbinsX(),tempdrawhisto->GetBinContent(tempdrawhisto->GetNbinsX()+1));
290 >        tempdrawhisto->SetBinError(tempdrawhisto->GetNbinsX(),TMath::Sqrt(tempdrawhisto->GetBinContent(tempdrawhisto->GetNbinsX())));
291 >        tempdrawhisto->SetBinContent(1,tempdrawhisto->GetBinContent(0));
292 >        tempdrawhisto->SetBinError(1,TMath::Sqrt(tempdrawhisto->GetBinContent(1)));
293 >      }
294 >
295 >      if(!(this->collection)[isample].is_data) tempdrawhisto->Scale(luminosity*((this->collection)[isample].weight));//weight applied here is XS & N(entries)
296 >      histo->Add(tempdrawhisto);
297 >    }
298 >    tempdrawhisto->Delete();
299 >  }//end of loop over isample
300 >  if(Verbosity>0) dout << "Histo has been filled and now contains " << histo->Integral() << " points (integral)" << endl;
301 >  histo->GetXaxis()->SetTitle(m_xlabel.c_str());
302 >  // Try to add bin width information: look for units in m_xlabel
303 >  string units = find_units(m_xlabel);
304 >  if ( units.length()>0 ) {
305 >    stringstream ylabel;
306 >    ylabel << m_ylabel << " / " << histo->GetBinWidth(1) << " " << units;
307 >    histo->GetYaxis()->SetTitle( ylabel.str().c_str() );
308 >  } else {
309 >    histo->GetYaxis()->SetTitle(m_ylabel.c_str());
310 >  }
311 >  histo->GetXaxis()->CenterTitle();
312 >  histo->GetYaxis()->CenterTitle();
313 >  if(do_only_selected_samples) histo->SetLineColor(this->GetColor(onlyindex[0]));
314 >  return histo;
315 > }
316 >
317 > 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) {
318 >  if(Verbosity>0) dout << endl << endl;
319 >  if(Verbosity>0) dout << "-------------------------------------------------------------------------------------" << endl;
320 >  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;
321 >  if(HUSH==0) dout << "Drawing histo called " << m_histoname << "... " << endl;
322 >  bool do_only_selected_samples=false;
323 >  if(onlyindex.size()>0&&onlyindex[0]!=-1) {
324 >    if(Verbosity>0) {dout << "Requested to only draw sample corresponding to the following sample(s) : " << endl;}
325 >    for(int is=0;is<onlyindex.size();is++) {
326 >      if(Verbosity>0) dout << "   - " << (this->collection)[onlyindex[is]].filename << " (sample index: " << onlyindex[is] << ")" << endl;
327 >    }
328 >    do_only_selected_samples=true;
329 >  }
330 >  if(onlyindex.size()==1&&onlyindex[0]==-1) {
331 >    do_only_selected_samples=true; // this is the special case when we request a non-existing sample - we shouldn't draw anything then.
332 >    onlyindex.clear();
333 >  }
334 >  stringstream h_histoname;
335 >  h_histoname<<"h_"<<m_histoname;
336 >  float binningarray[binningx.size()+1];
337 >  float binningyarray[binningy.size()+1];
338 >  for(int i=0;i<binningx.size();i++) {binningarray[i]=binningx[i];binningyarray[i]=binningy[i];}
339 >  TH2F *histo = new TH2F(m_histoname.c_str(),"",binningx.size()-1,binningarray,binningy.size(),binningyarray);
340 >  histo->Sumw2();
341 >  
342 >  stringstream drawthis;
343 >  drawthis<<m_var<<">>tempdrawhisto";
344 >  
345 >  for (unsigned int isample=0;isample<(this->collection).size();isample++) {
346 >    bool use_this_sample=false;
347 >    if(!(this->collection)[isample].is_active) continue; // skip inactive samples right away
348 >    if(((this->collection)[isample].is_data==m_is_data)&&(!do_only_selected_samples)) {
349 >      if(drawsignal==false&&(this->collection)[isample].is_signal==true) continue;
350 >      use_this_sample=true;
351 >    }
352 >    if(do_only_selected_samples&&this->do_sample(isample,onlyindex)) {
353 >      use_this_sample=true;
354 >    }
355 >    TH2F *tempdrawhisto = new TH2F("tempdrawhisto","tempdrawhisto",binningx.size()-1,binningarray,binningy.size()-1,binningyarray);
356 >    tempdrawhisto->Sumw2();
357 >    if(use_this_sample) {
358 >      if(Verbosity>0) dout << "[samplecollection::Draw] : Added contribution from sample " << (this->collection)[isample].samplename << endl;
359 >      (this->collection)[isample].events->Draw(drawthis.str().c_str(),(essentialcut&&Cut)*cutWeight);//this weight is based on PU etc. not XS
360        if(addoverunderflowbins) {
361          //now also adding the overflow & underflow bins:
362          tempdrawhisto->SetBinContent(tempdrawhisto->GetNbinsX(),tempdrawhisto->GetBinContent(tempdrawhisto->GetNbinsX()+1));
# Line 290 | Line 370 | TH1F* samplecollection::Draw(string m_hi
370      }
371      tempdrawhisto->Delete();
372    }//end of loop over isample
373 <  if(Verbosity>0) cout << "Histo has been filled and now contains " << histo->Integral() << " points (integral)" << endl;
373 >  if(Verbosity>0) dout << "Histo has been filled and now contains " << histo->Integral() << " points (integral)" << endl;
374    histo->GetXaxis()->SetTitle(m_xlabel.c_str());
375    histo->GetYaxis()->SetTitle(m_ylabel.c_str());
376    histo->GetXaxis()->CenterTitle();
# Line 311 | Line 391 | TH1F* samplecollection::Draw(string m_hi
391    TH1F *histo = this->Draw(m_histoname,m_var, binning, m_xlabel, m_ylabel, Cut, m_is_data, luminosity, onlyindex,drawsignal);
392    return histo;
393    }
394 < 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)
395 < {
394 >
395 > 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) {
396    stringstream h_histoname;
397    h_histoname<<"h_"<<m_histoname;
398    THStack thestack("thestack",m_histoname.c_str());
# Line 324 | Line 404 | THStack samplecollection::DrawStack(stri
404    int bookedhistos[this->ngroups+1];
405    for(int ih=0;ih<=this->ngroups;ih++) bookedhistos[ih]=0;
406    
407 +  float binningarray[binning.size()+1];
408 +  for(int i=0;i<binning.size();i++) {binningarray[i]=binning[i];}
409 +  
410    for (unsigned int isample=0;isample<(this->collection).size();isample++) {
411      if(!drawsignal&&(this->collection)[isample].is_signal) continue;
412      if((this->collection)[isample].is_active&&(this->collection)[isample].is_data==m_is_data) {//fills mc if we want mc, else fills data.
413 <      TH1F *tempdrawhisto = new TH1F("tempdrawhisto","",m_nbins,m_minx,m_maxx);
414 <      (this->collection)[isample].events->Draw(drawthis.str().c_str(),(essentialcut&&Cut)*"weight");
413 >      TH1F *tempdrawhisto = new TH1F("tempdrawhisto","",binning.size()-1,binningarray);
414 >      (this->collection)[isample].events->Draw(drawthis.str().c_str(),(essentialcut&&Cut)*cutWeight);
415        
416        if(addoverunderflowbins) {
417          //now also adding the overflow & underflow bins:
# Line 376 | Line 459 | THStack samplecollection::DrawStack(stri
459    return thestack;
460   }
461  
462 + 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
463 + drawsignal=false)
464 + {
465 +  vector<float> binning;
466 +  for(int i=0;i<=m_nbins;i++)
467 +  {
468 +    binning.push_back(((float)(m_maxx-m_minx)/((float)m_nbins))*i+m_minx);
469 +  }
470 +  return this->DrawStack(m_histoname,m_var, binning, m_xlabel, m_ylabel, Cut, m_is_data, luminosity, drawsignal);
471 +  }
472 +
473  
474   vector<int> samplecollection::FindSample(string what)
475   {
# Line 384 | Line 478 | vector<int> samplecollection::FindSample
478    {
479      if(((this->collection)[isam].filename).find(what)!=string::npos) {
480        hitcollection.push_back(isam);
481 +    } else {
482      }
388    else {
389    }
483    }
484 <  if(hitcollection.size()==0) hitcollection.push_back(-1);
484 >  if(hitcollection.size()==0) {
485 >    hitcollection.push_back(-1);
486 >    write_warning(__FUNCTION__,"Couldn't find sample "+string(what)+" using sample collection \""+string(this->name)+"\"");
487 >  }
488    return hitcollection;
489   }
490  
# Line 404 | Line 500 | Color_t samplecollection::GetColor(int s
500  
501   TLegend* samplecollection::allbglegend(string title, TH1F *data,float posx=0.65, float posy=0.60) {
502   //  TLegend *leg = new TLegend(0.65,0.60,0.89,0.77);
503 <  TLegend *leg = new TLegend(posx,posy,0.89,0.77);
503 >  TLegend *leg = new TLegend(posx,posy,0.89,0.89);
504    if(title!="") leg->SetHeader(title.c_str());
505    if(data->GetName()!="nothing") leg->AddEntry(data,"Data","p");
506    leg->SetFillColor(kWhite);
# Line 440 | Line 536 | TLegend* samplecollection::allbglegend(s
536  
537  
538   void set_treename(string treen="events") {
539 <  cout << "Treename has been set to " << treen << endl;
539 >  dout << "Treename has been set to " << treen << endl;
540    if(treen=="PFevents"||treen=="events") treename=treen;
541    else write_error("sample::set_treename","Setting the treename failed as you provided an invalid tree name.");
542   }
# Line 477 | Line 573 | void samplecollection::PickUpFromThisFil
573    (this->collection)[isamp].events->SetBranchAddress("eventNum",&eventNum);
574    (this->collection)[isamp].events->SetBranchAddress("lumi",&lumi);
575    (this->collection)[isamp].events->SetBranchAddress("runNum",&runNum);
576 +
577    
578 <  TTreeFormula *select = new TTreeFormula("select", "pfJetGoodNum>=3&&jzb[1]>100&&jzb[1]<150&&id1==id2", (this->collection)[isamp].events);
578 >  TTreeFormula *select = new TTreeFormula("select", cut.c_str()&&essentialcut, (this->collection)[isamp].events);
579 >  int npickedup=0;
580    for (Int_t entry = 0 ; entry < (this->collection)[isamp].events->GetEntries() ; entry++) {
581     (this->collection)[isamp].events->LoadTree(entry);
582     if (select->EvalInstance()) {
583       (this->collection)[isamp].events->GetEntry(entry);
584 <     cout << runNum << ":" << lumi << ":" << eventNum << endl;
584 >     dout << runNum << ":" << lumi << ":" << eventNum << endl;
585 >     npickedup++;
586     }
587    }
588 +  dout << "Printed out a total of " << npickedup << " events" << endl;
589   }
590  
591    
# Line 503 | Line 603 | void samplecollection::PickUpEvents(stri
603    //do something with output and of course the pickup file!
604   }
605  
606 + //________________________________________________________________________________________
607 + // Find units from histogram x-label (looks for '[...]')
608 + string samplecollection::find_units(string& xlabel) {
609 +  
610 +  string units;
611 +  size_t pos1 = xlabel.find("[");
612 +  if ( pos1 != string::npos ) {
613 +    size_t pos2 = xlabel.find("]");
614 +    units = xlabel.substr(pos1+1,pos2-pos1-1);
615 +  }//  else {
616 + //     size_t pos1 = xlabel.find("(");
617 + //     if ( pos1 != string::npos ) {
618 + //       size_t pos2 = xlabel.find(")");
619 + //       units = xlabel.substr(pos1+1,pos2-pos1-1);
620 + //     }
621 + //   }
622 +  return units;
623 +  
624 + }
625  
626   void switch_overunderflow(bool newpos=false) {
627    addoverunderflowbins=newpos;
628 < }
628 > }
629 >
630 >

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines