ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/cbrown/AnalysisFramework/Plotting/Modules/GeneralToolBox.C
Revision: 1.15
Committed: Fri Jul 22 10:16:02 2011 UTC (13 years, 9 months ago) by buchmann
Content type: text/plain
Branch: MAIN
Changes since 1.14: +8 -2 lines
Log Message:
Added function to detect infinities; adapted titlebox to fit the new style

File Contents

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