ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/cbrown/AnalysisFramework/Plotting/Modules/GeneralToolBox.C
Revision: 1.10
Committed: Thu Jul 14 15:49:37 2011 UTC (13 years, 9 months ago) by buchmann
Content type: text/plain
Branch: MAIN
Changes since 1.9: +2 -2 lines
Log Message:
Adapted the size of the CMS Preliminary text and legends; The CMS Preliminary text is now at the top

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