ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/cbrown/AnalysisFramework/Plotting/Modules/GeneralToolBox.C
Revision: 1.12
Committed: Tue Jul 19 15:45:28 2011 UTC (13 years, 9 months ago) by buchmann
Content type: text/plain
Branch: MAIN
Changes since 1.11: +54 -0 lines
Log Message:
New feature (currently disabled) : Simultaneous output to file and screen

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