ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/cbrown/AnalysisFramework/Plotting/Modules/SampleClass.C
Revision: 1.9
Committed: Wed Jul 27 13:07:42 2011 UTC (13 years, 9 months ago) by buchmann
Content type: text/plain
Branch: MAIN
Changes since 1.8: +18 -3 lines
Log Message:
Sample collection class can now also draw thstack with a variable binning via a vector; the previous type (min,max,nbins) gets converted to a vector automatically and is still accepted

File Contents

# User Rev Content
1 buchmann 1.1 #include <iostream>
2     #include <sstream>
3     #include <vector>
4     #include <utility>
5    
6     #include <TFile.h>
7     #include <TTree.h>
8     #include <TH1.h>
9 buchmann 1.5 #include <TH2.h>
10 buchmann 1.1 #include <TCut.h>
11     #include <THStack.h>
12     #include <TColor.h>
13     #include <TCanvas.h>
14     #include <TError.h>
15     #include <TText.h>
16     #include <TLegend.h>
17     #include <TError.h>
18 buchmann 1.4 #include <TTreeFormula.h>
19 buchmann 1.1
20     #define SampleClassLoaded
21 buchmann 1.4
22 buchmann 1.1 #ifndef Verbosity
23     #define Verbosity 0
24     #endif
25     #ifndef HUSH
26     #define HUSH 1
27     #endif
28     #ifndef GeneralToolBoxLoaded
29     #include "GeneralToolBox.C"
30     #endif
31     using namespace std;
32    
33     bool doesROOTFileExist(string filename);
34 buchmann 1.2 bool addoverunderflowbins=false;
35 buchmann 1.1 string treename="PFevents";
36    
37     class sample
38     {
39     public:
40     string filename;
41     string samplename;
42     int Nentries;
43     float xs;//cross section
44     float weight;
45     bool is_data;
46     bool is_signal;
47     bool is_active;
48     int groupindex;
49     Color_t samplecolor;
50    
51     TFile *tfile;
52     TTree *events;
53    
54     sample(string m_filename,string m_samplename,int m_Nentries, float m_xs,bool is_data, bool m_is_signal, int m_groupindex, Color_t color);
55     void closeFile();
56     };
57    
58     string write_mc_or_data(bool is_data)
59     {
60     if(is_data) return "data";
61     return "MC";
62     }
63    
64     sample::sample(string m_filename, string m_samplename, int m_Nentries, float m_xs,bool m_is_data, bool m_is_signal, int m_groupindex, Color_t mycolor)
65     {
66     this->filename=m_filename;
67     this->samplename=m_samplename;
68     this->Nentries=m_Nentries;
69     this->xs=m_xs;
70     this->is_data=m_is_data;
71     this->is_signal=m_is_signal;
72     this->weight=(xs/(float)Nentries);
73     this->groupindex=m_groupindex;
74     this->is_active=true;
75     this->samplecolor=mycolor;
76     if(!doesROOTFileExist(this->filename)) {
77     stringstream message;
78     message << "The " << write_mc_or_data(is_data) << " sample " << this->samplename << " is invalid because the associated file path, " << this->filename << " is incorrect.";
79     write_error("Sample::Sample",message.str());
80     this->is_active=false;
81     }
82    
83     if(doesROOTFileExist(this->filename)) {
84     //suppressing stupid 64/32 errors here (Warning in <TFile::ReadStreamerInfo>: /scratch/buchmann/MC_Spring11_PU_PF/TToBLNu_TuneZ2_t-channel_7TeV-madgraph.root: not a TStreamerInfo object)
85     Int_t currlevel=gErrorIgnoreLevel;
86     gErrorIgnoreLevel=5000;
87     this->tfile = new TFile(m_filename.c_str());
88     gErrorIgnoreLevel=currlevel;
89     this->events=(TTree*)(this->tfile)->Get(treename.c_str());
90 buchmann 1.8 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 buchmann 1.1 }
92     else {
93     this->is_active=false;
94     }
95    
96     }
97    
98     void sample::closeFile()
99     {
100     if(doesROOTFileExist(this->filename)) {
101     (this->tfile)->Close();
102     }
103     else {
104 buchmann 1.8 dout << "SAMPLE " << this->samplename << " cannot be closed as the underlying file (" << this->filename << ") does not exist!" << endl;
105 buchmann 1.1 this->is_active=false;
106     }
107     }
108    
109     bool doesROOTFileExist(string filename)
110     {
111     //suppressing stupid 64/32 errors here (Warning in <TFile::ReadStreamerInfo>: /scratch/buchmann/MC_Spring11_PU_PF/TToBLNu_TuneZ2_t-channel_7TeV-madgraph.root: not a TStreamerInfo object)
112     Int_t currlevel=gErrorIgnoreLevel;
113     gErrorIgnoreLevel=5000;
114     TFile *f = new TFile(filename.c_str());
115    
116     if (f->IsZombie()) {
117 buchmann 1.8 dout << "Error opening file" << filename << endl;
118 buchmann 1.1 return 0;
119     }
120     f->Close();
121     gErrorIgnoreLevel=currlevel;
122     return 1;
123     }
124     //********************************************************
125    
126     TCut essentialcut("mll>0");
127    
128     void setessentialcut(TCut ess) {
129     essentialcut=ess;
130     }
131    
132     class samplecollection
133     {
134     public:
135     string name;
136     vector<sample> collection;
137     int nsamples;
138     int ndatasamples;
139     int nmcsamples;
140     int ngroups;
141    
142     samplecollection(string m_name);
143     void AddSample(string m_filename,string m_samplename,int m_Nentries, float m_xs,bool is_data, bool m_is_signal, int groupindex, Color_t color);
144    
145     //vector quantities
146     TH1F* 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);
147     TH1F* Draw(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);
148    
149     //minx,maxs quantities
150     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);
151     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);
152    
153 buchmann 1.5 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);
154    
155 buchmann 1.2 vector<float> get_optimal_binsize(string variable, TCut cut,int nbins, float low, float high);
156    
157 buchmann 1.1 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);
158 buchmann 1.9 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);
159 buchmann 1.1 vector<int> FindSample(string what);
160     void ListSamples();
161     bool do_sample(int thissample, vector<int> &selected_samples);
162     Color_t GetColor(string filename);
163     Color_t GetColor(int sampleindex);
164    
165 buchmann 1.4 TLegend* allbglegend(string title,float x, float y);
166     TLegend* allbglegend(string title, TH1F *data, float x, float y);
167    
168     void PickUpFromThisFile(int isamp, string cut, vector<string> &output, vector<string> &pickupfile);
169     void PickUpEvents(string cut);
170 buchmann 1.1 };
171    
172     samplecollection::samplecollection(string m_name)
173     {
174     this->name=m_name;
175 buchmann 1.8 if(Verbosity>0) dout << "Initiated sample collection " << this->name << " " << endl;
176 buchmann 1.1 }
177    
178     void samplecollection::ListSamples()
179     {
180 buchmann 1.8 dout << "---------------------------------------------------------------------------------------------------" << endl;
181     dout << "Listing all " << this->nsamples << " sample(s) of the sample collection called " << this->name << " : " << endl;
182 buchmann 1.1 if(this->ndatasamples>0) {
183 buchmann 1.8 dout << "Data sample(s): " << endl;
184 buchmann 1.1 for (int isamp=0;isamp<this->collection.size();isamp++)
185     {
186 buchmann 1.8 if((this->collection)[isamp].is_data) dout << " - " << (this->collection)[isamp].samplename << " from " << (this->collection)[isamp].filename << endl;
187 buchmann 1.1 }
188     }
189     if(this->nmcsamples>0) {
190 buchmann 1.8 dout << "MC sample(s): " << endl;
191 buchmann 1.1 for (int isamp=0;isamp<this->collection.size();isamp++)
192     {
193 buchmann 1.8 if(!(this->collection)[isamp].is_data) dout << " - " << (this->collection)[isamp].samplename << " from " << (this->collection)[isamp].filename << endl;
194 buchmann 1.1 }
195     }
196 buchmann 1.8 dout << "---------------------------------------------------------------------------------------------------" << endl;
197 buchmann 1.1 }
198    
199     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)
200     {
201     sample NewSample(m_filename,m_samplename,m_Nentries,m_xs,is_data,m_is_signal,groupindex,newcolor);
202     (this->collection).push_back(NewSample);
203     if((this->collection).size()==1) {
204     this->nmcsamples=0;
205     this->ndatasamples=0;
206     this->nsamples=0;
207     this->ngroups=0;
208     }
209     if(groupindex>this->ngroups) this->ngroups=groupindex;
210     if(is_data) this->ndatasamples++;
211     else this->nmcsamples++;
212     this->nsamples=this->collection.size();
213     }
214    
215     bool samplecollection::do_sample(int thissample, vector<int> &selected_samples)
216     {
217     bool drawit=false;
218     for(int isel=0;isel<selected_samples.size();isel++)
219     {
220     if(selected_samples[isel]==thissample) {
221     drawit=true;
222     break;
223     }
224     }
225     return drawit;
226     }
227    
228     TH1F* samplecollection::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=false)
229     {
230     vector<int> emptyvector;
231     TH1F *histo = this->Draw(m_histoname,m_var, m_nbins, m_minx, m_maxx, m_xlabel, m_ylabel, Cut, m_is_data, luminosity,emptyvector,drawsignal);
232     return histo;
233     }
234    
235     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, bool drawsignal=false)
236     {
237     vector<int> emptyvector;
238     TH1F *histo = this->Draw(m_histoname,m_var, binning, m_xlabel, m_ylabel, Cut, m_is_data, luminosity,emptyvector,drawsignal);
239     return histo;
240     }
241    
242     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) {
243 buchmann 1.8 if(Verbosity>0) dout << endl << endl;
244     if(Verbosity>0) dout << "-------------------------------------------------------------------------------------" << endl;
245     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;
246     if(HUSH==0) dout << "Drawing histo called " << m_histoname << "... " << endl;
247 buchmann 1.1 bool do_only_selected_samples=false;
248     if(onlyindex.size()>0&&onlyindex[0]!=-1) {
249 buchmann 1.8 if(Verbosity>0) {dout << "Requested to only draw sample corresponding to the following sample(s) : " << endl;}
250 buchmann 1.1 for(int is=0;is<onlyindex.size();is++) {
251 buchmann 1.8 if(Verbosity>0) dout << " - " << (this->collection)[onlyindex[is]].filename << " (sample index: " << onlyindex[is] << ")" << endl;
252 buchmann 1.1 }
253     do_only_selected_samples=true;
254     }
255     if(onlyindex.size()==1&&onlyindex[0]==-1) {
256     do_only_selected_samples=true; // this is the special case when we request a non-existing sample - we shouldn't draw anything then.
257     onlyindex.clear();
258     }
259     stringstream h_histoname;
260     h_histoname<<"h_"<<m_histoname;
261     float binningarray[binning.size()+1];
262     for(int i=0;i<binning.size();i++) {binningarray[i]=binning[i];}
263     TH1F *histo = new TH1F(m_histoname.c_str(),"",binning.size()-1,binningarray);
264     histo->Sumw2();
265    
266     stringstream drawthis;
267     drawthis<<m_var<<">>tempdrawhisto";
268    
269     for (unsigned int isample=0;isample<(this->collection).size();isample++) {
270     bool use_this_sample=false;
271     if(!(this->collection)[isample].is_active) continue; // skip inactive samples right away
272     if(((this->collection)[isample].is_data==m_is_data)&&(!do_only_selected_samples)) {
273     if(drawsignal==false&&(this->collection)[isample].is_signal==true) continue;
274     use_this_sample=true;
275     }
276     if(do_only_selected_samples&&this->do_sample(isample,onlyindex)) {
277     use_this_sample=true;
278     }
279     TH1F *tempdrawhisto = new TH1F("tempdrawhisto","tempdrawhisto",binning.size()-1,binningarray);
280     tempdrawhisto->Sumw2();
281     if(use_this_sample) {
282 buchmann 1.8 if(Verbosity>0) dout << "[samplecollection::Draw] : Added contribution from sample " << (this->collection)[isample].samplename << endl;
283 buchmann 1.2 (this->collection)[isample].events->Draw(drawthis.str().c_str(),(essentialcut&&Cut)*"weight");//this weight is based on PU etc. not XS
284     if(addoverunderflowbins) {
285     //now also adding the overflow & underflow bins:
286     tempdrawhisto->SetBinContent(tempdrawhisto->GetNbinsX(),tempdrawhisto->GetBinContent(tempdrawhisto->GetNbinsX()+1));
287     tempdrawhisto->SetBinError(tempdrawhisto->GetNbinsX(),TMath::Sqrt(tempdrawhisto->GetBinContent(tempdrawhisto->GetNbinsX())));
288     tempdrawhisto->SetBinContent(1,tempdrawhisto->GetBinContent(0));
289     tempdrawhisto->SetBinError(1,TMath::Sqrt(tempdrawhisto->GetBinContent(1)));
290     }
291    
292     if(!(this->collection)[isample].is_data) tempdrawhisto->Scale(luminosity*((this->collection)[isample].weight));//weight applied here is XS & N(entries)
293 buchmann 1.1 histo->Add(tempdrawhisto);
294     }
295     tempdrawhisto->Delete();
296     }//end of loop over isample
297 buchmann 1.8 if(Verbosity>0) dout << "Histo has been filled and now contains " << histo->Integral() << " points (integral)" << endl;
298 buchmann 1.1 histo->GetXaxis()->SetTitle(m_xlabel.c_str());
299     histo->GetYaxis()->SetTitle(m_ylabel.c_str());
300     histo->GetXaxis()->CenterTitle();
301     histo->GetYaxis()->CenterTitle();
302     if(do_only_selected_samples) histo->SetLineColor(this->GetColor(onlyindex[0]));
303     return histo;
304     }
305    
306 buchmann 1.5 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) {
307 buchmann 1.8 if(Verbosity>0) dout << endl << endl;
308     if(Verbosity>0) dout << "-------------------------------------------------------------------------------------" << endl;
309     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;
310     if(HUSH==0) dout << "Drawing histo called " << m_histoname << "... " << endl;
311 buchmann 1.5 bool do_only_selected_samples=false;
312     if(onlyindex.size()>0&&onlyindex[0]!=-1) {
313 buchmann 1.8 if(Verbosity>0) {dout << "Requested to only draw sample corresponding to the following sample(s) : " << endl;}
314 buchmann 1.5 for(int is=0;is<onlyindex.size();is++) {
315 buchmann 1.8 if(Verbosity>0) dout << " - " << (this->collection)[onlyindex[is]].filename << " (sample index: " << onlyindex[is] << ")" << endl;
316 buchmann 1.5 }
317     do_only_selected_samples=true;
318     }
319     if(onlyindex.size()==1&&onlyindex[0]==-1) {
320     do_only_selected_samples=true; // this is the special case when we request a non-existing sample - we shouldn't draw anything then.
321     onlyindex.clear();
322     }
323     stringstream h_histoname;
324     h_histoname<<"h_"<<m_histoname;
325     float binningarray[binningx.size()+1];
326     float binningyarray[binningy.size()+1];
327     for(int i=0;i<binningx.size();i++) {binningarray[i]=binningx[i];binningyarray[i]=binningy[i];}
328     TH2F *histo = new TH2F(m_histoname.c_str(),"",binningx.size()-1,binningarray,binningy.size(),binningyarray);
329     histo->Sumw2();
330    
331     stringstream drawthis;
332     drawthis<<m_var<<">>tempdrawhisto";
333    
334     for (unsigned int isample=0;isample<(this->collection).size();isample++) {
335     bool use_this_sample=false;
336     if(!(this->collection)[isample].is_active) continue; // skip inactive samples right away
337     if(((this->collection)[isample].is_data==m_is_data)&&(!do_only_selected_samples)) {
338     if(drawsignal==false&&(this->collection)[isample].is_signal==true) continue;
339     use_this_sample=true;
340     }
341     if(do_only_selected_samples&&this->do_sample(isample,onlyindex)) {
342     use_this_sample=true;
343     }
344     TH2F *tempdrawhisto = new TH2F("tempdrawhisto","tempdrawhisto",binningx.size()-1,binningarray,binningy.size()-1,binningyarray);
345     tempdrawhisto->Sumw2();
346     if(use_this_sample) {
347 buchmann 1.8 if(Verbosity>0) dout << "[samplecollection::Draw] : Added contribution from sample " << (this->collection)[isample].samplename << endl;
348 buchmann 1.5 (this->collection)[isample].events->Draw(drawthis.str().c_str(),(essentialcut&&Cut)*"weight");//this weight is based on PU etc. not XS
349     if(addoverunderflowbins) {
350     //now also adding the overflow & underflow bins:
351     tempdrawhisto->SetBinContent(tempdrawhisto->GetNbinsX(),tempdrawhisto->GetBinContent(tempdrawhisto->GetNbinsX()+1));
352     tempdrawhisto->SetBinError(tempdrawhisto->GetNbinsX(),TMath::Sqrt(tempdrawhisto->GetBinContent(tempdrawhisto->GetNbinsX())));
353     tempdrawhisto->SetBinContent(1,tempdrawhisto->GetBinContent(0));
354     tempdrawhisto->SetBinError(1,TMath::Sqrt(tempdrawhisto->GetBinContent(1)));
355     }
356    
357     if(!(this->collection)[isample].is_data) tempdrawhisto->Scale(luminosity*((this->collection)[isample].weight));//weight applied here is XS & N(entries)
358     histo->Add(tempdrawhisto);
359     }
360     tempdrawhisto->Delete();
361     }//end of loop over isample
362 buchmann 1.8 if(Verbosity>0) dout << "Histo has been filled and now contains " << histo->Integral() << " points (integral)" << endl;
363 buchmann 1.5 histo->GetXaxis()->SetTitle(m_xlabel.c_str());
364     histo->GetYaxis()->SetTitle(m_ylabel.c_str());
365     histo->GetXaxis()->CenterTitle();
366     histo->GetYaxis()->CenterTitle();
367     if(do_only_selected_samples) histo->SetLineColor(this->GetColor(onlyindex[0]));
368     return histo;
369     }
370    
371 buchmann 1.1
372     TH1F* samplecollection::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=false)
373     {
374     vector<float> binning;
375     for(int i=0;i<=m_nbins;i++)
376     {
377     binning.push_back(((float)(m_maxx-m_minx)/((float)m_nbins))*i+m_minx);
378     }
379    
380     TH1F *histo = this->Draw(m_histoname,m_var, binning, m_xlabel, m_ylabel, Cut, m_is_data, luminosity, onlyindex,drawsignal);
381     return histo;
382     }
383 buchmann 1.9
384     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) {
385 buchmann 1.1 stringstream h_histoname;
386     h_histoname<<"h_"<<m_histoname;
387     THStack thestack("thestack",m_histoname.c_str());
388    
389     stringstream drawthis;
390     drawthis<<m_var<<">>tempdrawhisto";
391    
392     TH1F *histogroups[this->ngroups+1];
393     int bookedhistos[this->ngroups+1];
394     for(int ih=0;ih<=this->ngroups;ih++) bookedhistos[ih]=0;
395    
396 buchmann 1.9 float binningarray[binning.size()+1];
397     for(int i=0;i<binning.size();i++) {binningarray[i]=binning[i];}
398    
399 buchmann 1.1 for (unsigned int isample=0;isample<(this->collection).size();isample++) {
400     if(!drawsignal&&(this->collection)[isample].is_signal) continue;
401     if((this->collection)[isample].is_active&&(this->collection)[isample].is_data==m_is_data) {//fills mc if we want mc, else fills data.
402 buchmann 1.9 TH1F *tempdrawhisto = new TH1F("tempdrawhisto","",binning.size()-1,binningarray);
403 buchmann 1.1 (this->collection)[isample].events->Draw(drawthis.str().c_str(),(essentialcut&&Cut)*"weight");
404 buchmann 1.2
405     if(addoverunderflowbins) {
406     //now also adding the overflow & underflow bins:
407     tempdrawhisto->SetBinContent(tempdrawhisto->GetNbinsX(),tempdrawhisto->GetBinContent(tempdrawhisto->GetNbinsX()+1));
408     tempdrawhisto->SetBinError(tempdrawhisto->GetNbinsX(),TMath::Sqrt(tempdrawhisto->GetBinContent(tempdrawhisto->GetNbinsX())));
409     tempdrawhisto->SetBinContent(1,tempdrawhisto->GetBinContent(0));
410     tempdrawhisto->SetBinError(1,TMath::Sqrt(tempdrawhisto->GetBinContent(1)));
411     }
412    
413 buchmann 1.1 if(!(this->collection)[isample].is_data) tempdrawhisto->Scale(luminosity*((this->collection)[isample].weight));
414     tempdrawhisto->SetFillColor(this->GetColor(isample));
415     if(bookedhistos[(this->collection)[isample].groupindex]==0) {
416     tempdrawhisto->SetName(GetNumericHistoName().c_str());
417     histogroups[(this->collection)[isample].groupindex]=(TH1F*)tempdrawhisto->Clone();
418     bookedhistos[(this->collection)[isample].groupindex]=1;
419     }
420     else
421     {
422     histogroups[(this->collection)[isample].groupindex]->Add(tempdrawhisto);
423     bookedhistos[(this->collection)[isample].groupindex]++;
424     }
425     tempdrawhisto->Delete();
426     }
427     }
428     vector<int> ordered_indices;
429     vector<float> ordered_integrals;
430    
431     for(int ig=0;ig<=this->ngroups;ig++) {
432     if(bookedhistos[ig]>0) {
433     ordered_indices.push_back(ig);
434     ordered_integrals.push_back(histogroups[ig]->Integral());
435     }
436     }
437    
438     /*
439     bubbleSort(ordered_integrals,ordered_indices);
440     // for(int index=ordered_indices.size()-1;index>=0;index--) {
441     for(int index=0;index<ordered_indices.size();index++) {
442     thestack.Add(histogroups[ordered_indices[index]]);
443     }
444     */
445     for(int index=0;index<ordered_indices.size();index++) {
446     thestack.Add(histogroups[ordered_indices[index]]);
447     }
448     return thestack;
449     }
450    
451 buchmann 1.9 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
452     drawsignal=false)
453     {
454     vector<float> binning;
455     for(int i=0;i<=m_nbins;i++)
456     {
457     binning.push_back(((float)(m_maxx-m_minx)/((float)m_nbins))*i+m_minx);
458     }
459     return this->DrawStack(m_histoname,m_var, binning, m_xlabel, m_ylabel, Cut, m_is_data, luminosity, drawsignal);
460     }
461    
462 buchmann 1.1
463     vector<int> samplecollection::FindSample(string what)
464     {
465     vector<int> hitcollection;
466     for(unsigned int isam=0;isam<(this->collection).size();isam++)
467     {
468     if(((this->collection)[isam].filename).find(what)!=string::npos) {
469     hitcollection.push_back(isam);
470     }
471     else {
472     }
473     }
474     if(hitcollection.size()==0) hitcollection.push_back(-1);
475     return hitcollection;
476     }
477    
478     Color_t samplecollection::GetColor(string filename) {
479     vector<int> corresponding_samples = this->FindSample(filename);
480     if(corresponding_samples.size()>0) return this->GetColor(corresponding_samples[0]);
481     return kBlack;
482     }
483    
484     Color_t samplecollection::GetColor(int sampleindex) {
485     return this->collection[sampleindex].samplecolor;
486     }
487    
488 buchmann 1.4 TLegend* samplecollection::allbglegend(string title, TH1F *data,float posx=0.65, float posy=0.60) {
489     // TLegend *leg = new TLegend(0.65,0.60,0.89,0.77);
490 buchmann 1.6 TLegend *leg = new TLegend(posx,posy,0.89,0.89);
491 buchmann 1.1 if(title!="") leg->SetHeader(title.c_str());
492     if(data->GetName()!="nothing") leg->AddEntry(data,"Data","p");
493     leg->SetFillColor(kWhite);
494     leg->SetBorderSize(0);
495     leg->SetLineColor(kWhite);
496    
497     TH1F *fakehistos[(this->collection).size()];
498     bool donealready[(this->collection).size()];
499     for(int i=0;i<(this->collection).size();i++) donealready[i]=false;
500     for(int isample=0;isample<(this->collection).size();isample++) {
501     if((this->collection)[isample].is_data||(this->collection)[isample].is_signal) continue;
502    
503     if(!donealready[(this->collection)[isample].groupindex]) {
504     fakehistos[(this->collection)[isample].groupindex] = new TH1F(GetNumericHistoName().c_str(),"",1,0,1);
505     fakehistos[(this->collection)[isample].groupindex]->Fill(1);
506     fakehistos[(this->collection)[isample].groupindex]->SetFillColor(this->GetColor(isample));
507     leg->AddEntry(fakehistos[(this->collection)[isample].groupindex],((this->collection)[isample].samplename).c_str(),"f");
508     donealready[(this->collection)[isample].groupindex]=true;
509     }
510     }
511 buchmann 1.4 DrawPrelim();
512 buchmann 1.1 leg->SetTextFont(42);
513     return leg;
514     }
515    
516 buchmann 1.4 TLegend* samplecollection::allbglegend(string title="",float posx=0.65, float posy=0.60) {
517 buchmann 1.1 Int_t currlevel=gErrorIgnoreLevel;
518     gErrorIgnoreLevel=5000;
519     TH1F *blub = new TH1F("nothing","nothing",1,0,1);
520     gErrorIgnoreLevel=currlevel;//we know this possibly replaces a previous histo, but we don't care since it's fake anyway.
521 buchmann 1.4 return this->allbglegend(title,blub,posx,posy);
522 buchmann 1.1 }
523    
524 buchmann 1.4
525 buchmann 1.1 void set_treename(string treen="events") {
526 buchmann 1.8 dout << "Treename has been set to " << treen << endl;
527 buchmann 1.1 if(treen=="PFevents"||treen=="events") treename=treen;
528     else write_error("sample::set_treename","Setting the treename failed as you provided an invalid tree name.");
529     }
530 buchmann 1.2
531     vector<float> samplecollection::get_optimal_binsize(string variable, TCut cut,int nbins, float low, float high) {
532     TH1F *histo = this->Draw("histo",variable,5000,low,high, "finding_optimal_binsize", "events", cut,0,1000);
533     float eventsperbin=histo->Integral()/nbins;
534     vector<float> binning;
535     binning.push_back(high);
536     float runningsum=0;
537     for(int i=histo->GetNbinsX();i>0;i--) {
538     runningsum+=histo->GetBinContent(i);
539     if(TMath::Abs(runningsum-eventsperbin)<0.05*eventsperbin||runningsum>eventsperbin) {
540     binning.push_back(histo->GetBinLowEdge(i));
541     runningsum=0;
542     }
543     }
544     if(runningsum>0) binning.push_back(low);
545     for(int i=0;i<int(binning.size()/2);i++) {
546     float temp=binning[i];
547     binning[i]=binning[binning.size()-1-i];
548     binning[binning.size()-1-i]=temp;
549     }
550    
551     return binning;
552    
553 buchmann 1.3 }
554    
555 buchmann 1.4 void samplecollection::PickUpFromThisFile(int isamp, string cut, vector<string> &output, vector<string> &pickupfile) {
556     int lumi,eventNum,runNum;
557     float jzb[30];
558     (this->collection)[isamp].events->SetBranchAddress("jzb",&jzb);
559     (this->collection)[isamp].events->SetBranchAddress("lumi",&runNum);
560     (this->collection)[isamp].events->SetBranchAddress("eventNum",&eventNum);
561     (this->collection)[isamp].events->SetBranchAddress("lumi",&lumi);
562     (this->collection)[isamp].events->SetBranchAddress("runNum",&runNum);
563    
564 buchmann 1.7 TTreeFormula *select = new TTreeFormula("select", cut.c_str()&&essentialcut, (this->collection)[isamp].events);
565     int npickedup=0;
566 buchmann 1.4 for (Int_t entry = 0 ; entry < (this->collection)[isamp].events->GetEntries() ; entry++) {
567     (this->collection)[isamp].events->LoadTree(entry);
568     if (select->EvalInstance()) {
569     (this->collection)[isamp].events->GetEntry(entry);
570 buchmann 1.8 dout << runNum << ":" << lumi << ":" << eventNum << endl;
571 buchmann 1.7 npickedup++;
572 buchmann 1.4 }
573     }
574 buchmann 1.8 dout << "Printed out a total of " << npickedup << " events" << endl;
575 buchmann 1.4 }
576    
577    
578    
579     void samplecollection::PickUpEvents(string cut) {
580     vector<string> output;
581     vector<string> pickupfile;
582     for (int isamp=0;isamp<this->collection.size();isamp++)
583     {
584     if((this->collection)[isamp].is_data) {
585     //we have a data sample !
586     this->PickUpFromThisFile(isamp,cut,output,pickupfile);
587     }
588     }
589     //do something with output and of course the pickup file!
590     }
591    
592    
593 buchmann 1.3 void switch_overunderflow(bool newpos=false) {
594     addoverunderflowbins=newpos;
595 buchmann 1.2 }