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

Comparing UserCode/cbrown/Development/Plotting/Modules/SampleClass.C (file contents):
Revision 1.2 by buchmann, Thu Feb 9 15:59:08 2012 UTC vs.
Revision 1.16 by buchmann, Thu Sep 13 09:44:16 2012 UTC

# Line 45 | Line 45 | public:
45    bool is_data;
46    bool is_signal;
47    bool is_active;
48 +  float weightrenormalization;
49    int groupindex;
50    Color_t samplecolor;
51    
# Line 72 | Line 73 | sample::sample(string m_filename, string
73    this->groupindex=m_groupindex;
74    this->is_active=true;
75    this->samplecolor=mycolor;
76 +  this->weightrenormalization=1.0;
77    if(!doesROOTFileExist(this->filename)) {
78      stringstream message;
79      message << "The " << write_mc_or_data(is_data) << " sample " << this->samplename << " is invalid because the associated file path, " << this->filename << " is incorrect.";
# Line 87 | Line 89 | sample::sample(string m_filename, string
89      gErrorIgnoreLevel=currlevel;
90      this->events=(TTree*)(this->tfile)->Get(treename.c_str());
91      if(Verbosity>0) dout << "The " << write_mc_or_data(is_data) << " file " << this->filename << " has been added successfully to the list of samples. " << endl;
92 <    long long measured_nevents=(this->events)->GetEntries();
93 <    if(((this->Nentries>1)||(this->Nentries==0))&measured_nevents!=this->Nentries) {
92 >    long long measured_nevents=(long)1;
93 >    TH1F *weight_histo = (TH1F*)(this->tfile)->Get("weight_histo");
94 >    float average_weight = 1.0;
95 >    if(weight_histo) {
96 >      average_weight = weight_histo->Integral()/weight_histo->GetEntries();
97 >      measured_nevents = (long)weight_histo->GetEntries();
98 >    } else {
99 >      measured_nevents=(this->events)->GetEntries();
100 >    }
101 >
102 >    if(((this->Nentries>1)||(this->Nentries==0))&&measured_nevents!=this->Nentries) {
103        //special cases: m_Nentries=1 : we want to give each event the full weight (->scans!)
104        //               m_Nentries=0: detect the number of events and set the nevents automatically
105 +      //               m_Nentries<0: set manually (see below)
106  
107        stringstream warning;
108        warning << "Detected incorrect number of events in sample initialization of sample " << m_filename << " (detected Nevents: " << measured_nevents << " , definition claims: " << this->Nentries << "; will use measured number of events. If you want to use this algorithm to set the number of events anyway, set the number of events to 0.";
109        if(m_Nentries>1) write_warning(__FUNCTION__,warning.str());
110        this->Nentries=measured_nevents;
111 +    } else if ( this->Nentries<0 ) {
112 +      this->Nentries = -this->Nentries;
113 +    }
114 +    if ( average_weight>0 ) {
115 +      this->weight=(xs/(float)Nentries)/average_weight;
116 +      this->weightrenormalization=(1.0/average_weight);
117 +    } else {
118 +      if(average_weight>0) {
119 +      this->weight=(xs/(float)Nentries)/average_weight;
120 +      this->weightrenormalization=(1.0/average_weight);
121 +      } else {
122 +        this->weight = (xs/(float)Nentries);
123 +        this->weightrenormalization=1;
124 +      }
125      }
100    this->weight=(xs/(float)Nentries);
126    }
127    else {
128      this->is_active=false;
# Line 136 | Line 161 | bool doesROOTFileExist(string filename)
161   TCut essentialcut("mll>0");
162   // The following cut (cutWeight) will reweight all the events: use "weight" to correct MC for pileUP, "1.0" otherwise
163   TCut cutWeight("weight");
164 + //TCut cutWeight("(weight*(((id1==id2&&id1==0)*1.2)+((id1==id2&&id1==1)/1.2)+(id1!=id2)))");
165  
166   void setessentialcut(TCut ess) {
167    essentialcut=ess;
# Line 169 | Line 195 | public:
195    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);
196    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);
197    vector<int> FindSample(string what);
198 +  vector<int> FindSampleBySampleName(string what);
199    void ListSamples();
200    bool do_sample(int thissample, vector<int> &selected_samples);
201    Color_t GetColor(string filename);
# Line 196 | Line 223 | void samplecollection::ListSamples()
223    dout << "Listing all " << this->nsamples << " sample(s) of the sample collection called " << this->name << " : " << endl;
224    if(this->ndatasamples>0) {
225      dout << "Data sample(s): " << endl;
226 <    for (int isamp=0;isamp<this->collection.size();isamp++)
226 >    for (int isamp=0;isamp<(int)this->collection.size();isamp++)
227        {
228          if((this->collection)[isamp].is_data) dout << " - " << (this->collection)[isamp].samplename << " from " << (this->collection)[isamp].filename << endl;
229        }
230    }
231    if(this->nmcsamples>0) {
232      dout << "MC sample(s): " << endl;
233 <    for (int isamp=0;isamp<this->collection.size();isamp++)
233 >    for (int isamp=0;isamp<(int)this->collection.size();isamp++)
234        {
235 <        if(!(this->collection)[isamp].is_data) dout << " - " << (this->collection)[isamp].samplename << " (" << (this->collection)[isamp].filename << ") xs=" << (this->collection)[isamp].xs << " pb, N(events)=" << (this->collection)[isamp].Nentries << endl;
235 >        if(!(this->collection)[isamp].is_data) dout << " - " << (this->collection)[isamp].samplename << " (" << (this->collection)[isamp].filename << ") xs=" << (this->collection)[isamp].xs << " pb, N(events)=" << (this->collection)[isamp].Nentries << " , RN = " << (this->collection)[isamp].weightrenormalization << endl;
236        }
237    }
238    dout << "---------------------------------------------------------------------------------------------------" << endl;
# Line 230 | Line 257 | void samplecollection::AddSample(string
257   bool samplecollection::do_sample(int thissample, vector<int> &selected_samples)
258   {
259    bool drawit=false;
260 <  for(int isel=0;isel<selected_samples.size();isel++)
260 >  for(int isel=0;isel<(int)selected_samples.size();isel++)
261    {
262      if(selected_samples[isel]==thissample) {
263        drawit=true;
# Line 258 | Line 285 | TH1F* samplecollection::Draw(string m_hi
285    if(Verbosity>0) dout << endl << endl;
286    if(Verbosity>0) dout << "-------------------------------------------------------------------------------------" << endl;
287    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;
288 +  
289    if(HUSH==0) dout << "Drawing histo called " << m_histoname << "... " << endl;
290    bool do_only_selected_samples=false;
291    if(onlyindex.size()>0&&onlyindex[0]!=-1) {
292      if(Verbosity>0) {dout << "Requested to only draw sample corresponding to the following sample(s) : " << endl;}
293 <    for(int is=0;is<onlyindex.size();is++) {
293 >    for(int is=0;is<(int)onlyindex.size();is++) {
294        if(Verbosity>0) dout << "   - " << (this->collection)[onlyindex[is]].filename << " (sample index: " << onlyindex[is] << ")" << endl;
295      }
296      do_only_selected_samples=true;
# Line 274 | Line 302 | TH1F* samplecollection::Draw(string m_hi
302    stringstream h_histoname;
303    h_histoname<<"h_"<<m_histoname;
304    float binningarray[binning.size()+1];
305 <  for(int i=0;i<binning.size();i++) {binningarray[i]=binning[i];}
305 >  for(int i=0;i<(int)binning.size();i++) {binningarray[i]=binning[i];}
306    TH1F *histo = new TH1F(m_histoname.c_str(),"",binning.size()-1,binningarray);
307    histo->Sumw2();
308    
# Line 294 | Line 322 | TH1F* samplecollection::Draw(string m_hi
322      TH1F *tempdrawhisto = new TH1F("tempdrawhisto","tempdrawhisto",binning.size()-1,binningarray);
323      tempdrawhisto->Sumw2();
324      if(use_this_sample) {
297      if(Verbosity>0) dout << "[samplecollection::Draw] : Added contribution from sample " << (this->collection)[isample].samplename << endl;
325        (this->collection)[isample].events->Draw(drawthis.str().c_str(),(essentialcut&&Cut)*cutWeight);//this weight is based on PU etc. not XS
326 + //   cout << "\033[1;33m Drawing " << drawthis.str() << "   with cut   " << (const char*) (essentialcut&&Cut)*cutWeight << " for sample " <<  (this->collection)[isample].filename << "\033[0m   (overflow: " << addoverunderflowbins << ")" << endl;
327        if(addoverunderflowbins) {
328          //now also adding the overflow & underflow bins:
329          tempdrawhisto->SetBinContent(tempdrawhisto->GetNbinsX(),tempdrawhisto->GetBinContent(tempdrawhisto->GetNbinsX()+1)+tempdrawhisto->GetBinContent(tempdrawhisto->GetNbinsX()));
330          tempdrawhisto->SetBinError(tempdrawhisto->GetNbinsX(),TMath::Sqrt(tempdrawhisto->GetBinContent(tempdrawhisto->GetNbinsX())));
331 <        tempdrawhisto->SetBinContent(1,tempdrawhisto->GetBinContent(0)+tempdrawhisto->GetBinContent(1));
332 <        tempdrawhisto->SetBinError(1,TMath::Sqrt(tempdrawhisto->GetBinContent(1)));
331 > //      tempdrawhisto->SetBinContent(1,tempdrawhisto->GetBinContent(0)+tempdrawhisto->GetBinContent(1));
332 > //      tempdrawhisto->SetBinError(1,TMath::Sqrt(tempdrawhisto->GetBinContent(1)));
333 >        // Delete over- and under-flow bins
334 >        tempdrawhisto->SetBinContent(tempdrawhisto->GetNbinsX()+1,0);
335 > //        tempdrawhisto->SetBinContent(0,0);
336        }
337  
338        if(!(this->collection)[isample].is_data) tempdrawhisto->Scale(luminosity*((this->collection)[isample].weight));//weight applied here is XS & N(entries)
339 +      if(Verbosity>0) dout << "[samplecollection::Draw] : Added contribution from sample " << (this->collection)[isample].samplename << ": " <<tempdrawhisto->Integral() << endl;
340        histo->Add(tempdrawhisto);
341      }
342      tempdrawhisto->Delete();
# Line 334 | Line 366 | TH2F* samplecollection::Draw(string m_hi
366    bool do_only_selected_samples=false;
367    if(onlyindex.size()>0&&onlyindex[0]!=-1) {
368      if(Verbosity>0) {dout << "Requested to only draw sample corresponding to the following sample(s) : " << endl;}
369 <    for(int is=0;is<onlyindex.size();is++) {
369 >    for(int is=0;is<(int)onlyindex.size();is++) {
370        if(Verbosity>0) dout << "   - " << (this->collection)[onlyindex[is]].filename << " (sample index: " << onlyindex[is] << ")" << endl;
371      }
372      do_only_selected_samples=true;
# Line 347 | Line 379 | TH2F* samplecollection::Draw(string m_hi
379    h_histoname<<"h_"<<m_histoname;
380    float binningarray[binningx.size()+1];
381    float binningyarray[binningy.size()+1];
382 <  for(int i=0;i<binningx.size();i++) {binningarray[i]=binningx[i];binningyarray[i]=binningy[i];}
383 <  TH2F *histo = new TH2F(m_histoname.c_str(),"",binningx.size()-1,binningarray,binningy.size(),binningyarray);
382 >  for(int i=0;i<(int)binningx.size();i++) {binningarray[i]=binningx[i];binningyarray[i]=binningy[i];}
383 >  TH2F *histo = new TH2F(m_histoname.c_str(),"",binningx.size()-1,binningarray,binningy.size()-1,binningyarray);
384    histo->Sumw2();
385    
386    stringstream drawthis;
# Line 369 | Line 401 | TH2F* samplecollection::Draw(string m_hi
401      if(use_this_sample) {
402        if(Verbosity>0) dout << "[samplecollection::Draw] : Added contribution from sample " << (this->collection)[isample].samplename << endl;
403        (this->collection)[isample].events->Draw(drawthis.str().c_str(),(essentialcut&&Cut)*cutWeight);//this weight is based on PU etc. not XS
404 + //       cout << "About to draw : " << drawthis.str() << "  with cut   " << (const char*) (essentialcut&&Cut)*cutWeight << endl;
405        if(addoverunderflowbins) {
406          //now also adding the overflow & underflow bins:
407          tempdrawhisto->SetBinContent(tempdrawhisto->GetNbinsX(),tempdrawhisto->GetBinContent(tempdrawhisto->GetNbinsX()+1)+tempdrawhisto->GetBinContent(tempdrawhisto->GetNbinsX()));
# Line 399 | Line 432 | TH1F* samplecollection::Draw(string m_hi
432    {
433      binning.push_back(((float)(m_maxx-m_minx)/((float)m_nbins))*i+m_minx);
434    }
435 <  
435 >
436    TH1F *histo = this->Draw(m_histoname,m_var, binning, m_xlabel, m_ylabel, Cut, m_is_data, luminosity, onlyindex,drawsignal);
437    return histo;
438    }
# Line 417 | Line 450 | THStack samplecollection::DrawStack(stri
450    for(int ih=0;ih<=this->ngroups;ih++) bookedhistos[ih]=0;
451    
452    float binningarray[binning.size()+1];
453 <  for(int i=0;i<binning.size();i++) {binningarray[i]=binning[i];}
453 >  for(int i=0;i<(int)binning.size();i++) {binningarray[i]=binning[i];}
454    
455    for (unsigned int isample=0;isample<(this->collection).size();isample++) {
456      if(!drawsignal&&(this->collection)[isample].is_signal) continue;
# Line 436 | Line 469 | THStack samplecollection::DrawStack(stri
469        if(!(this->collection)[isample].is_data) tempdrawhisto->Scale(luminosity*((this->collection)[isample].weight));
470        tempdrawhisto->SetFillColor(this->GetColor(isample));
471        if(bookedhistos[(this->collection)[isample].groupindex]==0) {
472 <        tempdrawhisto->SetName(GetNumericHistoName().c_str());
472 >        tempdrawhisto->SetName((removefunnystring((this->collection)[isample].samplename)+"__"+GetNumericHistoName()).c_str());
473          histogroups[(this->collection)[isample].groupindex]=(TH1F*)tempdrawhisto->Clone();
474          bookedhistos[(this->collection)[isample].groupindex]=1;
475        }
# Line 465 | Line 498 | THStack samplecollection::DrawStack(stri
498      thestack.Add(histogroups[ordered_indices[index]]);
499    }
500    */
501 <  for(int index=0;index<ordered_indices.size();index++) {
501 >  for(int index=0;index<(int)ordered_indices.size();index++) {
502      thestack.Add(histogroups[ordered_indices[index]]);
503    }
504    return thestack;
# Line 500 | Line 533 | vector<int> samplecollection::FindSample
533    return hitcollection;
534   }
535  
536 + vector<int> samplecollection::FindSampleBySampleName(string what)
537 + {
538 +  vector<int> hitcollection;
539 +  for(unsigned int isam=0;isam<(this->collection).size();isam++)
540 +  {
541 +    if(((this->collection)[isam].samplename).find(what)!=string::npos) {
542 +      hitcollection.push_back(isam);
543 +    } else {
544 +    }
545 +  }
546 +  if(hitcollection.size()==0) {
547 +    hitcollection.push_back(-1);
548 +    write_warning(__FUNCTION__,"Couldn't find sample "+string(what)+" using sample collection \""+string(this->name)+"\"");
549 +  }
550 +  return hitcollection;
551 + }
552 +
553   Color_t samplecollection::GetColor(string filename) {
554    vector<int> corresponding_samples = this->FindSample(filename);
555    if(corresponding_samples.size()>0) return this->GetColor(corresponding_samples[0]);
# Line 514 | Line 564 | TLegend* samplecollection::allbglegend(s
564   //  TLegend *leg = new TLegend(0.65,0.60,0.89,0.77);
565    TLegend *leg = new TLegend(posx,posy,0.89,0.89);
566    if(title!="") leg->SetHeader(title.c_str());
567 <  if(data->GetName()!="nothing") leg->AddEntry(data,"Data","p");
567 >  if(data->GetName()!="nothing") leg->AddEntry(data,"Data","lp");
568    leg->SetFillColor(kWhite);
569    leg->SetBorderSize(0);
570    leg->SetLineColor(kWhite);
571    
572    TH1F *fakehistos[(this->collection).size()];
573    bool donealready[(this->collection).size()];
574 <  for(int i=0;i<(this->collection).size();i++) donealready[i]=false;
575 <  for(int isample=0;isample<(this->collection).size();isample++) {
574 >  for(int i=0;i<(int)(this->collection).size();i++) donealready[i]=false;
575 >  for(int isample=0;isample<(int)(this->collection).size();isample++) {
576      if((this->collection)[isample].is_data||(this->collection)[isample].is_signal) continue;
577  
578      if(!donealready[(this->collection)[isample].groupindex]) {
# Line 605 | Line 655 | void samplecollection::PickUpFromThisFil
655   void samplecollection::PickUpEvents(string cut) {
656    vector<string> output;
657    vector<string> pickupfile;
658 <  for (int isamp=0;isamp<this->collection.size();isamp++)
658 >  for (int isamp=0;isamp<(int)this->collection.size();isamp++)
659    {
660      if((this->collection)[isamp].is_data) {
661        //we have a data sample !

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines