ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/cbrown/AnalysisFramework/Plotting/Modules/GeneralToolBox.C
Revision: 1.21
Committed: Tue Aug 16 07:36:26 2011 UTC (13 years, 8 months ago) by buchmann
Content type: text/plain
Branch: MAIN
Changes since 1.20: +4 -0 lines
Log Message:
Now providing the option to run ./Create_All_Plots.exec 1 to calculate peak positions, observed and predicted yields, and save all information in a file that can then be used for the susy model scans; this means that all these steps are just once and the data can then be used, instead of having each job redo this.

File Contents

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