ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/cbrown/AnalysisFramework/Plotting/Modules/GeneralToolBox.C
Revision: 1.5
Committed: Fri Jul 8 14:52:00 2011 UTC (13 years, 10 months ago) by buchmann
Content type: text/plain
Branch: MAIN
Changes since 1.4: +25 -6 lines
Log Message:
Changed the way CMS Preliminary XYZ is drawn

File Contents

# User Rev Content
1 buchmann 1.1 #include <iostream>
2     #include <sstream>
3     #include <vector>
4     #include <stdio.h>
5     #include <stdlib.h>
6     #include <sys/types.h>
7     #include <sys/stat.h>
8    
9     #include <TFile.h>
10     #include <TTree.h>
11     #include <TCut.h>
12     #include <TLegend.h>
13     #include <TLatex.h>
14     #include <TText.h>
15     #include <TGraph.h>
16     #include <TH1.h>
17 buchmann 1.2 #include <TF1.h>
18 buchmann 1.1 #include <TMath.h>
19     #include <TStyle.h>
20     #include <TCanvas.h>
21     #include <TError.h>
22 buchmann 1.3 #include <TVirtualPad.h>
23 buchmann 1.1 #include <TGraphAsymmErrors.h>
24     #include <TRandom.h>
25     #ifndef Verbosity
26     #define Verbosity 0
27     #endif
28    
29     /*
30     #ifndef SampleClassLoaded
31     #include "SampleClass.C"
32     #endif
33     */
34     #define GeneralToolBoxLoaded
35    
36     using namespace std;
37    
38     bool dopng=false;
39     bool doC=false;
40     bool doeps=false;
41 buchmann 1.5 bool dopdf=false;
42 buchmann 1.1 string basedirectory="";
43    
44 buchmann 1.5 TLegend* make_legend(string title,float posx,float posy);
45 buchmann 1.1 TText* write_title(string title);
46     TText* write_title_low(string title);
47    
48     TText* write_text(float xpos,float ypos,string title);
49     float computeRatioError(float a, float da, float b, float db);
50     float computeProductError(float a, float da, float b, float db);
51     TGraphAsymmErrors *histRatio(TH1F *h1,TH1F *h2, int id, vector<float>binning);
52     void setlumi(float l);
53     void CompleteSave(TCanvas *can, string filename, bool feedback);
54 buchmann 1.3 void CompleteSave(TVirtualPad *can, string filename, bool feedback);
55 buchmann 1.1 void write_warning(string funcname, string text);
56     void write_error(string funcname, string text);
57     void write_info(string funcname, string text);
58 buchmann 1.5 void DrawPrelim();
59 buchmann 1.1 //-------------------------------------------------------------------------------------
60     float lumi;
61     template<class A>
62     string any2string(const A& a){
63     ostringstream out;
64     out << a;
65     return out.str();
66     }
67    
68     void do_png(bool s) { dopng=s;}
69     void do_eps(bool s) { doeps=s;}
70     void do_C(bool s) { doC=s;}
71 buchmann 1.5 void do_pdf(bool s) { dopdf=s;}
72 buchmann 1.1
73     string topdir(string child) {
74     string tempdirectory=child;
75     if(tempdirectory.substr(tempdirectory.length()-1,1)=="/") tempdirectory=tempdirectory.substr(0,tempdirectory.length());
76     //we now have a directory without the trailing slash so we can just look for the last non-slash character :-)
77     for(int ichar=tempdirectory.length()-1;ichar>=0;ichar--) {
78     if(tempdirectory.substr(ichar,1)=="/") {
79     return tempdirectory.substr(0,ichar);
80     }
81     }
82     }
83    
84     void ensure_directory_exists(string thisdirectory) {
85     struct stat st;
86     if(stat(thisdirectory.c_str(),&st) == 0) {
87     if(Verbosity>0) cout << "Directory " << thisdirectory << " exists!" << endl;
88     }
89     else {
90     if(Verbosity>0) cout << "Directory " << thisdirectory << " does not exist. Need to create it!" << endl;
91     ensure_directory_exists(topdir(thisdirectory));
92     if (mkdir(thisdirectory.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH))
93     if(Verbosity>0) cout << "Created the directory " << thisdirectory << endl;
94     }
95     }
96    
97     void set_directory(string basedir="") {
98     if(basedir.substr(0,1)=="/") basedir=basedir.substr(1,basedir.length()-1);
99     if(basedir.substr(basedir.length()-1,1)!="/") basedir+="/";
100     char currentpath[1024];
101     getcwd(currentpath,1024);
102     basedirectory=(string)currentpath+"/"+basedir;
103     ensure_directory_exists(basedirectory);
104     }
105    
106     string extract_directory(string savethis) {
107     bool foundslash=false;
108     int position=savethis.length();
109     while(!foundslash&&position>0) {
110     position--;
111     if(savethis.substr(position,1)=="/") foundslash=true;
112     }
113     if(position>0) return savethis.substr(0,position+1);
114     else return "";
115     }
116    
117     void CompleteSave(TCanvas *can, string filename, bool feedback=false) {
118 buchmann 1.3 //any change you make here should also be done below in the CompleteSave function for virtual pads
119 buchmann 1.1 Int_t currlevel=gErrorIgnoreLevel;
120     if(!feedback) gErrorIgnoreLevel=1001;
121     ensure_directory_exists(extract_directory(basedirectory+filename));
122     if(dopng) can->SaveAs((basedirectory+filename+".png").c_str());
123     if(doeps) can->SaveAs((basedirectory+filename+".eps").c_str());
124 buchmann 1.5 if(dopdf) can->SaveAs((basedirectory+filename+".pdf").c_str());
125 buchmann 1.1 if(doC) can->SaveAs((basedirectory+filename+".C").c_str());
126     gErrorIgnoreLevel=currlevel;
127 buchmann 1.3 cout << "Saved " << filename << " in all requested formats" << endl;
128 buchmann 1.1 }
129 buchmann 1.3
130     void CompleteSave(TVirtualPad *can, string filename, bool feedback=false) {
131     Int_t currlevel=gErrorIgnoreLevel;
132     if(!feedback) gErrorIgnoreLevel=1001;
133     ensure_directory_exists(extract_directory(basedirectory+filename));
134     if(dopng) can->SaveAs((basedirectory+filename+".png").c_str());
135     if(doeps) can->SaveAs((basedirectory+filename+".eps").c_str());
136     if(doC) can->SaveAs((basedirectory+filename+".C").c_str());
137     gErrorIgnoreLevel=currlevel;
138     cout << "Saved " << filename << " in all requested formats" << endl;
139     }
140    
141 buchmann 1.1
142     void setlumi(float l) {
143     lumi=l;
144     }
145    
146     int write_first_line(vector<vector<string> > &entries) {
147     if(entries.size()>0) {
148     vector<string> firstline = entries[0];
149     int ncolumns=firstline.size();
150     int ndividers=ncolumns+1;
151     int cellwidth=(int)(((float)(60-ndividers))/(ncolumns));
152     cout << " |";
153     for(int idiv=0;idiv<ncolumns;idiv++) {
154     for(int isig=0;isig<cellwidth;isig++) cout << "-";
155     cout << "|";
156     }
157     cout << endl;
158     return ncolumns;
159     } else {
160     return 0;
161     }
162     }
163    
164     void write_entry(string entry,int width,int iline=0,int ientry=0) {
165     int currwidth=entry.size();
166     while(currwidth<width) {
167     entry=" "+entry;
168     if(entry.size()<width) entry=entry+" ";
169     currwidth=entry.size();
170     }
171     bool do_special=false;
172     if(iline==1&&ientry==1) { cout << "\033[1;32m" << entry << "\033[0m|";do_special=true;}//observed
173     if(iline==1&&ientry==2) { cout << "\033[1;34m" << entry << "\033[0m|";do_special=true;}//predicted (1)
174     if(iline==2&&ientry==1) { cout << "\033[1;34m" << entry << "\033[0m|";do_special=true;}//predicted (1)
175     if(iline==2&&ientry==2) { cout << "\033[1;34m" << entry << "\033[0m|";do_special=true;}//predicted (1)
176     if(!do_special) cout << entry << "|";
177     }
178    
179     void make_nice_table(vector<vector <string> > &entries) {
180     int ncolumns=write_first_line(entries);
181     int cellwidth=(int)(((float)(60-(ncolumns+1)))/(ncolumns));
182     for(int iline=0;iline<entries.size();iline++) {
183     vector<string> currline = entries[iline];
184     cout << " |";
185     for(int ientry=0;ientry<currline.size();ientry++) {
186     write_entry(currline[ientry],cellwidth);
187     }
188     cout << endl;
189     if(iline==0) write_first_line(entries);
190     }
191     write_first_line(entries);
192     }
193    
194     void make_nice_jzb_table(vector<vector <string> > &entries) {
195     int ncolumns=write_first_line(entries);
196     int cellwidth=(int)(((float)(60-(ncolumns+1)))/(ncolumns));
197     for(int iline=0;iline<entries.size();iline++) {
198     vector<string> currline = entries[iline];
199     cout << " |";
200     for(int ientry=0;ientry<currline.size();ientry++) {
201     write_entry(currline[ientry],cellwidth,iline,ientry);
202     }
203     cout << endl;
204     if(iline==0) write_first_line(entries);
205     }
206     write_first_line(entries);
207     }
208    
209    
210     void write_warning(string funcname, string text) {
211     cout << endl << endl;
212     cout << "\033[1;33m" << " _ " << endl;
213     cout << "\033[1;33m" << " (_) " << endl;
214     cout << "\033[1;33m" << "__ ____ _ _ __ _ __ _ _ __ __ _ " << endl;
215     cout << "\033[1;33m" << "\\ \\ /\\ / / _` | '__| '_ \\| | '_ \\ / _` |" << endl;
216     cout << "\033[1;33m" << " \\ V V / (_| | | | | | | | | | | (_| |" << endl;
217     cout << "\033[1;33m" << " \\_/\\_/ \\__,_|_| |_| |_|_|_| |_|\\__, |" << endl;
218     cout << "\033[1;33m" << " __/ |" << endl;
219     cout << "\033[1;33m" << " |___/ " << endl;
220     cout << endl;
221     cout << "\033[1;33m [" << funcname << "] " << text << " \033[0m" << endl;
222     cout << endl << endl;
223     }
224     void write_error(string funcname, string text) {
225     cout << endl << endl;
226     cout << "\033[1;31m ___ _ __ _ __ ___ _ __ " << endl;
227     cout << "\033[1;31m / _ \\ __| __/ _ \\| '__|" << endl;
228     cout << "\033[1;31m| __/ | | | | (_) | | " << endl;
229     cout << "\033[1;31m \\___|_| |_| \\___/|_| " << endl;
230     cout << endl;
231     cout << "\033[1;31m [" << funcname << "] " << text << " \033[0m" << endl;
232     cerr << "\033[1;31m [" << funcname << "] " << text << " \033[0m" << endl;
233     cout << endl << endl;
234     }
235    
236     void write_info(string funcname, string text) {
237     cout << endl << endl;
238     cout << "\033[1;34m _____ __ " << endl;
239     cout << "\033[1;34m |_ _| / _| " << endl;
240     cout << "\033[1;34m | | _ __ | |_ ___ " << endl;
241     cout << "\033[1;34m | | | '_ \\| _/ _ \\ " << endl;
242     cout << "\033[1;34m _| |_| | | | || (_) | " << endl;
243     cout << "\033[1;34m |_____|_| |_|_| \\___/ " << endl;
244     cout << endl;
245     cout << "\033[1;34m [" << funcname << "] " << text << " \033[0m" << endl;
246     cerr << "\033[1;34m [" << funcname << "] " << text << " \033[0m" << endl;
247     cout << endl << endl;
248     }
249    
250     TText* write_text(float xpos,float ypos,string title)
251     {
252     TLatex* titlebox = new TLatex (xpos,ypos,title.c_str());
253     titlebox->SetNDC(true);
254     titlebox->SetTextFont(42);
255     titlebox->SetTextSize(0.04);
256     titlebox->SetTextAlign(21);
257     return titlebox;
258     }
259    
260     TText* write_title(string title)
261     {
262     TText* titlebox = write_text(0.5,0.96,title);
263     return titlebox;
264     }
265    
266     TText* write_title_low(string title)
267     {
268     TText* titlebox = write_text(0.5,0.94,title);
269     return titlebox;
270     }
271    
272 buchmann 1.5 TLegend* make_legend(string title="", float posx=0.65, float posy=0.65)
273 buchmann 1.1 {
274     // TLegend *leg = new TLegend(0.65,0.65,0.89,0.89);
275     gStyle->SetTextFont(42);
276 buchmann 1.5 TLegend *leg = new TLegend(posx,posy,0.89,0.77);
277 buchmann 1.1 if(title!="") leg->SetHeader(title.c_str());
278     leg->SetTextFont(42);
279     leg->SetFillColor(kWhite);
280     leg->SetBorderSize(0);
281     leg->SetLineColor(kWhite);
282 buchmann 1.5 DrawPrelim();
283     /* TText *writeline1 = write_text(0.77,0.87,"CMS Preliminary 2011");
284 buchmann 1.1 stringstream lumitext;
285 buchmann 1.5 lumitext<<"#sqrt{s}=7, L = "<<lumi<<" pb^{-1}";
286 buchmann 1.1 TText *writeline2 = write_text(0.77,0.83,lumitext.str().c_str());
287     writeline1->SetTextSize(0.03);
288     writeline2->SetTextSize(0.03);
289     writeline1->Draw();
290     writeline2->Draw();
291 buchmann 1.5 */
292 buchmann 1.1 return leg;
293     }
294    
295     TGraph* make_nice_ratio(int nbins,float binning[],TH1F* histo)
296     {
297     float errorsquared[nbins];
298     float errors[nbins];
299     float bincontent[nbins];
300     for (int i=0;i<nbins;i++) {
301     errorsquared[i]=0;
302     bincontent[i]=0;
303     errors[i]=0;
304     }
305     float currlimit=binning[0];
306     int currtoplim=1;
307     for(int ibin=1;ibin<=histo->GetNbinsX();ibin++)
308     {
309     if(binning[currtoplim]<histo->GetBinCenter(ibin)) currtoplim++;
310     cout << "Bin i=" << ibin << " with bin center " << histo->GetBinCenter(ibin) << " contains " << histo->GetBinContent(ibin) << " is within " << binning[currtoplim-1] << " and " << binning[currtoplim] << endl;
311    
312     }
313    
314     return 0;
315     }
316    
317     float statErrorN(float x){return x - 0.5*TMath::ChisquareQuantile(0.3173/2,2*x);}
318     float statErrorP(float x){return 0.5*TMath::ChisquareQuantile(1-0.3173/2,2*(x+1))-x;}
319     float lowLimit(float a, float x){return 0.5*TMath::ChisquareQuantile(a,2*x);}
320     float highLimit(float a,float x){return 0.5*TMath::ChisquareQuantile(1-a,2*(x+1));}
321    
322     float computeRatioError(float a, float da, float b, float db)
323     {
324     float val=0.;
325     float errorSquare = (a/b)*(a/b)*( (da/a)*(da/a) + (db/b)*(db/b));
326     val = TMath::Sqrt(errorSquare);
327     return val;
328    
329     }
330     float computeProductError(float a, float da, float b, float db)
331     {
332     float val=0.;
333     float errorSquare = (a*b)*(a*b)*( (da/a)*(da/a) + (db/b)*(db/b));
334     val = TMath::Sqrt(errorSquare);
335     return val;
336     }
337    
338     TGraphAsymmErrors *histRatio(TH1F *h1,TH1F *h2, int id, vector<float>binning)
339     {
340     int absJZBbinsNumber = binning.size()-1;
341     TGraphAsymmErrors* graph = new TGraphAsymmErrors(absJZBbinsNumber);
342    
343     for(unsigned int i=0;i<absJZBbinsNumber;i++)
344     {
345     float xCenter=h1->GetBinCenter(i+1);
346     float xWidth=(h1->GetBinWidth(i+1))*0.5;
347     float nominatorError = h1->GetBinError(i+1);
348     float nominator=h1->GetBinContent(i+1);
349     float denominatorError=h2->GetBinError(i+1);
350     float denominator=h2->GetBinContent(i+1);
351     float errorN = 0;
352     float errorP = computeRatioError(nominator,nominatorError,denominator,denominatorError);
353     if(id==1) // (is data)
354     {
355     errorP = computeRatioError(nominator,statErrorP(nominator),denominator,statErrorP(denominator));
356     errorN = errorP; // symmetrize using statErrorP
357     } else {
358     errorN = computeRatioError(nominator,nominatorError,denominator,denominatorError);
359     errorP = computeRatioError(nominator,nominatorError,denominator,denominatorError);
360     }
361     if(denominator!=0) {
362     graph->SetPoint(i, xCenter, nominator/denominator);
363     graph->SetPointError(i,xWidth,xWidth,errorN,errorP);
364     }
365     else {
366     graph->SetPoint(i, xCenter, -999);
367     graph->SetPointError(i,xWidth,xWidth,errorN,errorP);
368     }
369     }
370     return graph;
371     }
372    
373     string print_range(float cent, float down, float up) {//note that up&down can be flipped, we don't care, but the central value needs to come 1st!
374     float uperr=0,downerr=0;
375     if(down>up&&down>cent) uperr=down-cent;
376     if(up>down&&up>cent) uperr=up-cent;
377     if(down<cent&&down<up) downerr=cent-down;
378     if(up<cent&&up<down) downerr=cent-up;
379     if(cent>up&&cent>down&&(up!=0&&down!=0)) write_error("print_range"," WATCH OUT: THE CENTRAL VALUE SEEMS TO BE LARGER THAN BOTH UP&DOWN!");
380     if(cent<up&&cent<down&&(up!=0&&down!=0)) write_error("print_range"," WATCH OUT: THE CENTRAL VALUE SEEMS TO BE SMALLER THAN BOTH UP&DOWN!");
381     stringstream result;
382     result << cent << " + " << uperr << " - " << downerr;
383     return result.str();
384     }
385    
386     void bubbleSort ( int arr [ ], int size, int order [ ]) // nice way to sort an array (called arr) which is currently in a random order (indices in (order")
387     {
388     int last = size - 2;
389     int isChanged = 1;
390    
391     while ( last >= 0 && isChanged )
392     {
393     isChanged = 0;
394     for ( int k = 0; k <= last; k++ )
395     if ( arr[k] > arr[k+1] )
396     {
397     swap ( arr[k], arr[k+1] );
398     isChanged = 1;
399     int bkp=order[k];
400     order[k]=order[k+1];
401     order[k+1]=bkp;
402     }
403     last--;
404     }
405     }
406    
407     void swapvec(vector<float> &vec,int j, int k) {
408     float bkp=vec[j];
409     vec[j]=vec[k];
410     vec[k]=bkp;
411     }
412    
413     void bubbleSort ( vector<float> &arr , vector<int> &order) // nice way to sort an array (called arr) which is currently in a random order (indices in (order")
414     {
415     int last = arr.size() - 2;
416     int isChanged = 1;
417    
418     while ( last >= 0 && isChanged )
419     {
420     isChanged = 0;
421     for ( int k = 0; k <= last; k++ )
422     if ( arr[k] > arr[k+1] )
423     {
424     swapvec (arr,k,k+1);
425     isChanged = 1;
426     int bkp=order[k];
427     order[k]=order[k+1];
428     order[k+1]=bkp;
429     }
430     last--;
431     }
432     }
433    
434     int numerichistoname=0;
435     string GetNumericHistoName() {
436     stringstream b;
437     b << "h_" << numerichistoname;
438     numerichistoname++;
439     return b.str();
440     }
441    
442     //********************** BELOW : CUT INTERPRETATION **************************//
443     void splitupcut(string incut, vector<string> &partvector)
444     {
445     //idea: go thru the string called incut; if a parantheses is opened, then the cut cannot be split up until the parantheses is closed.
446     //ok anyway screw the parantheses.
447     int paranthesis_open=0;
448     int substr_start=0;
449     string currchar="";
450     for (int ichar=0;ichar<incut.length();ichar++)
451     {
452     currchar=incut.substr(ichar,1);
453     // if(currchar=="(") paranthesis_open++;
454     // if(currchar==")") paranthesis_open--;
455     if(currchar=="&"&&incut.substr(ichar+1,1)=="&"&&paranthesis_open==0) {
456     partvector.push_back(incut.substr(substr_start,ichar-substr_start));
457     substr_start=ichar+2;
458     }
459     }
460     partvector.push_back(incut.substr(substr_start,incut.length()-substr_start));
461     if(Verbosity>1) {
462     cout << "[ splitupcut() ] : The cut vector now contains the following elements: "<< endl;
463     for (int ipart=0;ipart<partvector.size();ipart++)
464     {
465     cout << " - " << partvector[ipart] << endl;
466     }
467     }
468     }
469    
470     int atleastvalue(string expression, int &morethanlessthan) // takes in an expression such as ">2" or ">=3" and returns e.g. 3 (in both examples)
471     {
472     int retval=0;
473     if(expression.substr(0,1)==">"&&expression.substr(1,1)=="=") {
474     // cout << "The expression " << expression << " is saying that we have at least " << atoi(expression.substr(2,1).c_str()) << " jets" << endl;
475     morethanlessthan=1;
476     return atoi(expression.substr(2,1).c_str());
477     }
478     if(expression.substr(0,1)=="="&&expression.substr(1,1)=="=") {
479     // cout << "The expression " << expression << " is saying that we have at least " << atoi(expression.substr(1,1).c_str())+1 << " jets" << endl;
480     morethanlessthan=0;
481     return atoi(expression.substr(1,1).c_str());
482     }
483     if(expression.substr(0,1)=="<"&&expression.substr(1,1)=="=") {
484     // cout << "The expression " << expression << " is saying that we have at least " << atoi(expression.substr(1,1).c_str())+1 << " jets" << endl;
485     morethanlessthan=-1;
486     return 1+atoi(expression.substr(1,1).c_str());
487     }
488     if(expression.substr(0,1)==">") {
489     // cout << "The expression " << expression << " is saying that we have at least " << atoi(expression.substr(2,1).c_str()) << " jets" << endl;
490     morethanlessthan=1;
491     return 1+atoi(expression.substr(2,1).c_str());
492     }
493     if(expression.substr(0,1)=="<"&&expression.substr(1,1)=="=") {
494     // cout << "The expression " << expression << " is saying that we have at least " << atoi(expression.substr(2,1).c_str()) << " jets" << endl;
495     morethanlessthan=-1;
496     return 1+atoi(expression.substr(2,1).c_str());
497     }
498     }
499    
500     int do_jet_cut(string incut, int *nJets) {
501     string expression=(incut.substr(12,incut.size()-12));
502     cout << "Going to analyze the jet cut : " << expression << " with 0,1 being " << expression.substr(0,1) << " and 1,1 being " << expression.substr(1,1) << endl;
503     if(expression.substr(0,1)=="<"&&expression.substr(1,1)=="=") {
504     int nJet=atoi(expression.substr(2,1).c_str());
505     for(int i=nJet+1;i<20;i++) nJets[i]=0;
506     cout << "Is of type <=" << endl;
507     return 0;
508     }
509     if(expression.substr(0,1)=="="&&expression.substr(1,1)=="=") {
510     int nJet=atoi(expression.substr(2,1).c_str());
511     for(int i=0;i<20&&i!=nJet;i++) nJets[i]=0;
512     cout << "Is of type ==" << endl;
513     return 0;
514     }
515     if(expression.substr(0,1)==">"&&expression.substr(1,1)=="=") {
516     int nJet=atoi(expression.substr(2,1).c_str());
517     for(int i=0;i<nJet&&i!=nJet;i++) nJets[i]=0;
518     cout << "Is of type >=" << endl;
519     return 0;
520     }
521     if(expression.substr(0,1)=="<") {
522     int nJet=atoi(expression.substr(1,1).c_str());
523     for(int i=nJet;i<20;i++) nJets[i]=0;
524     cout << "Is of type <" << endl;
525     return 0;
526     }
527     if(expression.substr(0,1)==">") {
528     int nJet=atoi(expression.substr(1,1).c_str());
529     for(int i=0;i<nJet+1&&i!=nJet;i++) nJets[i]=0;
530     cout << "Is of type >" << endl;
531     return 0;
532     }
533     }
534    
535     string interpret_cut(string incut, bool &isJetCut, int *permittednJets)
536     {
537     // isJetCut=false;nJets=-1;
538     if(incut=="()") return "";
539     while(incut.substr(0,1)=="(") incut=incut.substr(1,incut.length()-1);
540     while(incut.length()>0&&incut.substr(incut.length()-1,1)==")") incut=incut.substr(0,incut.length()-1);
541     // if(incut.substr(0,1)=="("&&incut.substr(incut.length()-1,1)==")") incut=incut.substr(1,incut.length()-2); //this is to make (cut) to cut.
542    
543     if(Verbosity>0) {
544     cout << "Now interpreting cut " << incut << endl;
545     }
546     /*
547     if(incut=="ch1*ch2<0") return "OS";
548     if(incut=="id1==id2") return "SF";
549     if(incut=="id1!=id2") return "OF";
550     */
551     if(incut=="ch1*ch2<0") return "";
552 buchmann 1.5 if(incut=="(mll>55&&mll<70)||(mll>112&&mll<160)") return "SB";
553     if(incut=="(mll>61&&mll<70)||(mll>112&&mll<190)") return "SB'";
554 buchmann 1.1 if(incut=="id1==id2") return "";
555     if(incut=="id1!=id2") return "";
556     if(incut=="mll>2") return "";
557    
558     if(incut=="mll>0") return ""; // my typical "fake cut"
559    
560     if(incut=="passed_triggers||!is_data") return "Triggers";
561     if(incut=="pfjzb[0]>-998") return "";
562    
563    
564     if(incut=="id1==0") return "ee";
565     if(incut=="id1==1") return "#mu#mu";
566     if(incut=="abs(mll-91.2)<20") return "|m_{l^{+}l^{-}}-m_{Z}|<20";
567     if(incut=="pfJetGoodID[0]") return "";
568     if(incut=="pfJetGoodID[1]") return "";
569     if((int)incut.find("pfJetGoodNum")>-1) {
570     //do_jet_cut(incut,permittednJets);
571     stringstream result;
572     result << "nJets" << incut.substr(12,incut.size()-12);
573     /* cout << "Dealing with a jet cut: " << incut << endl;
574     stringstream result;
575     result << "nJets" << incut.substr(12,incut.size()-12);
576     isJetCut=true;
577     if(exactjetcut(incut,nJets))
578     // nJets=atleastvalue((incut.substr(12,incut.size()-12)),morethanlessthan);
579     return result.str();*/
580     return result.str();
581     }
582     return incut;
583     }
584    
585     string interpret_nJet_range(int *nJets) {
586     for (int i=0;i<20;i++) cout << i << " : " << nJets[i] << endl;
587     return "hello";
588     }
589    
590     string interpret_cuts(vector<string> &cutparts)
591     {
592     stringstream nicecut;
593     int nJets;
594     bool isJetCut;
595     int finalJetCut=-1;
596     int permittednJets[20];
597     for(int ijet=0;ijet<20;ijet++) permittednJets[ijet]=1;
598     int morethanlessthan=0;//-1: less than, 0: exactly, 1: more than
599     for(int icut=0;icut<cutparts.size();icut++)
600     {
601     if(icut==0) nicecut<<interpret_cut(cutparts[icut],isJetCut,permittednJets);
602     else {
603     string nice_this_cut = interpret_cut(cutparts[icut],isJetCut,permittednJets);//blublu
604     if(nice_this_cut.length()>0&&nicecut.str().length()>0) {
605     if(!isJetCut) nicecut<<"&&"<<nice_this_cut;
606     else {
607     if(nJets>finalJetCut) finalJetCut=nJets;
608     }
609     }
610     if(nice_this_cut.length()>0&&nicecut.str().length()==0) {
611     if(!isJetCut) {
612     nicecut<<nice_this_cut;
613     }
614     else {
615     if(nJets>finalJetCut) finalJetCut=nJets;
616     }
617     }
618     }
619     }
620     if(finalJetCut>-1) {
621     if(nicecut.str().length()==0) {
622     nicecut << "nJets#geq" << finalJetCut;
623     }
624     else
625     {
626     nicecut << "&&nJets#geq " << finalJetCut;
627     }
628     }
629    
630     // cout << "The nJet allowed range is given by: " << interpret_nJet_range(permittednJets) << endl;
631    
632     return nicecut.str();
633     }
634    
635     string decipher_cut(TCut originalcut,TCut ignorethispart)
636     {
637     string incut=(const char*)originalcut;
638     string ignore=(const char*)ignorethispart;
639    
640     if(ignore.length()>0 && incut.find(ignore)!=string::npos) incut=incut.replace(incut.find(ignore),ignore.length(),"");
641    
642     vector<string>cutparts;
643     splitupcut(incut,cutparts);
644     string write_cut=interpret_cuts(cutparts);
645     return write_cut;
646     }
647    
648     //********************** ABOVE : CUT INTERPRETATION **************************//
649 buchmann 1.2
650     Double_t GausRandom(Double_t mu, Double_t sigma) {
651     return gRandom->Gaus(mu,sigma);// real deal
652     //return mu;//debugging : no smearing.
653     }
654    
655 buchmann 1.3 int functionalhistocounter=0;
656 buchmann 1.2 TH1F * makehistofromfunction(TF1 *f1,TH1F *model) {
657     TH1F *histo = (TH1F*)model->Clone();
658 buchmann 1.3 functionalhistocounter++;
659     stringstream histoname;
660     histoname << "histo_based_on_function_" << f1->GetName() << "__"<<functionalhistocounter;
661     histo->SetTitle(histoname.str().c_str());
662     histo->SetName(histoname.str().c_str());
663 buchmann 1.2 int nbins=histo->GetNbinsX();
664     float low=histo->GetBinLowEdge(1);
665     float hi=histo->GetBinLowEdge(histo->GetNbinsX())+histo->GetBinWidth(histo->GetNbinsX());
666    
667     for(int i=0;i<=nbins;i++) {
668 buchmann 1.4 histo->SetBinContent(i,(f1->Integral(histo->GetBinLowEdge(i),histo->GetBinLowEdge(i)+histo->GetBinWidth(i)))/histo->GetBinWidth(i));
669     histo->SetBinError(i,TMath::Sqrt(histo->GetBinContent(i)));
670 buchmann 1.2 }
671    
672     return histo;
673 buchmann 1.4 }
674    
675     float hintegral(TH1 *histo, float low, float high) {
676     float sum=0;
677     for(int i=1;i<histo->GetNbinsX();i++) {
678     if((histo->GetBinLowEdge(i)>=low)&&(histo->GetBinLowEdge(i)+histo->GetBinWidth(i))<=high) sum+=histo->GetBinContent(i);
679     //now on to the less clear cases!
680     if(histo->GetBinLowEdge(i)<low&&(histo->GetBinLowEdge(i)+histo->GetBinWidth(i))>low) {
681     //need to consider this case still ... the bin is kind of in range but not sooooo much.
682     }
683     if(histo->GetBinLowEdge(i)<high&&(histo->GetBinLowEdge(i)+histo->GetBinWidth(i))>high) {
684     //need to consider this case still ... the bin is kind of in range but not sooooo much.
685     }
686    
687     }
688     return sum;
689 buchmann 1.5 }
690    
691     void DrawPrelim() {
692     TText *writeline1 = write_text(0.77,0.87,"CMS Preliminary 2011");
693     stringstream lumitext;
694     lumitext<<"#sqrt{s}=7, L="<< lumi/1000<<" fb^{-1}";
695     TText *writeline2 = write_text(0.77,0.83,lumitext.str().c_str());
696     writeline1->SetTextSize(0.03);
697     writeline2->SetTextSize(0.03);
698     writeline1->Draw();
699     writeline2->Draw();
700     }