ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/cbrown/AnalysisFramework/Plotting/Modules/GeneralToolBox.C
Revision: 1.17
Committed: Mon Aug 8 14:21:16 2011 UTC (13 years, 9 months ago) by fronga
Content type: text/plain
Branch: MAIN
Changes since 1.16: +5 -2 lines
Log Message:
Small touches to the legend and luminosity labels.

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     basedirectory=(string)currentpath+"/"+basedir;
205     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     void CompleteSave(TCanvas *can, string filename, bool feedback=false) {
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     ensure_directory_exists(extract_directory(basedirectory+filename));
229     if(dopng) can->SaveAs((basedirectory+filename+".png").c_str());
230     if(doeps) can->SaveAs((basedirectory+filename+".eps").c_str());
231 buchmann 1.5 if(dopdf) can->SaveAs((basedirectory+filename+".pdf").c_str());
232 buchmann 1.1 if(doC) can->SaveAs((basedirectory+filename+".C").c_str());
233     gErrorIgnoreLevel=currlevel;
234 buchmann 1.13 dout << "Saved " << filename << " in all requested formats" << endl;
235 buchmann 1.1 }
236 buchmann 1.3
237     void CompleteSave(TVirtualPad *can, string filename, bool feedback=false) {
238     Int_t currlevel=gErrorIgnoreLevel;
239     if(!feedback) gErrorIgnoreLevel=1001;
240     ensure_directory_exists(extract_directory(basedirectory+filename));
241     if(dopng) can->SaveAs((basedirectory+filename+".png").c_str());
242     if(doeps) can->SaveAs((basedirectory+filename+".eps").c_str());
243     if(doC) can->SaveAs((basedirectory+filename+".C").c_str());
244     gErrorIgnoreLevel=currlevel;
245 buchmann 1.13 dout << "Saved " << filename << " in all requested formats" << endl;
246 buchmann 1.3 }
247    
248 buchmann 1.1
249     void setlumi(float l) {
250     lumi=l;
251     }
252    
253     int write_first_line(vector<vector<string> > &entries) {
254     if(entries.size()>0) {
255     vector<string> firstline = entries[0];
256     int ncolumns=firstline.size();
257     int ndividers=ncolumns+1;
258     int cellwidth=(int)(((float)(60-ndividers))/(ncolumns));
259 buchmann 1.13 dout << " |";
260 buchmann 1.1 for(int idiv=0;idiv<ncolumns;idiv++) {
261 buchmann 1.13 for(int isig=0;isig<cellwidth;isig++) dout << "-";
262     dout << "|";
263 buchmann 1.1 }
264 buchmann 1.13 dout << endl;
265 buchmann 1.1 return ncolumns;
266     } else {
267     return 0;
268     }
269     }
270    
271     void write_entry(string entry,int width,int iline=0,int ientry=0) {
272     int currwidth=entry.size();
273     while(currwidth<width) {
274     entry=" "+entry;
275     if(entry.size()<width) entry=entry+" ";
276     currwidth=entry.size();
277     }
278     bool do_special=false;
279 buchmann 1.13 if(iline==1&&ientry==1) { dout << "\033[1;32m" << entry << "\033[0m|";do_special=true;}//observed
280     if(iline==1&&ientry==2) { dout << "\033[1;34m" << entry << "\033[0m|";do_special=true;}//predicted (1)
281     if(iline==2&&ientry==1) { dout << "\033[1;34m" << entry << "\033[0m|";do_special=true;}//predicted (1)
282     if(iline==2&&ientry==2) { dout << "\033[1;34m" << entry << "\033[0m|";do_special=true;}//predicted (1)
283     if(!do_special) dout << entry << "|";
284 buchmann 1.1 }
285    
286     void make_nice_table(vector<vector <string> > &entries) {
287     int ncolumns=write_first_line(entries);
288     int cellwidth=(int)(((float)(60-(ncolumns+1)))/(ncolumns));
289     for(int iline=0;iline<entries.size();iline++) {
290     vector<string> currline = entries[iline];
291 buchmann 1.13 dout << " |";
292 buchmann 1.1 for(int ientry=0;ientry<currline.size();ientry++) {
293     write_entry(currline[ientry],cellwidth);
294     }
295 buchmann 1.13 dout << endl;
296 buchmann 1.1 if(iline==0) write_first_line(entries);
297     }
298     write_first_line(entries);
299     }
300    
301     void make_nice_jzb_table(vector<vector <string> > &entries) {
302     int ncolumns=write_first_line(entries);
303     int cellwidth=(int)(((float)(60-(ncolumns+1)))/(ncolumns));
304     for(int iline=0;iline<entries.size();iline++) {
305     vector<string> currline = entries[iline];
306 buchmann 1.13 dout << " |";
307 buchmann 1.1 for(int ientry=0;ientry<currline.size();ientry++) {
308     write_entry(currline[ientry],cellwidth,iline,ientry);
309     }
310 buchmann 1.13 dout << endl;
311 buchmann 1.1 if(iline==0) write_first_line(entries);
312     }
313     write_first_line(entries);
314     }
315    
316    
317     void write_warning(string funcname, string text) {
318 buchmann 1.13 eout << endl << endl;
319     eout << "\033[1;33m" << " _ " << endl;
320     eout << "\033[1;33m" << " (_) " << endl;
321     eout << "\033[1;33m" << "__ ____ _ _ __ _ __ _ _ __ __ _ " << endl;
322     eout << "\033[1;33m" << "\\ \\ /\\ / / _` | '__| '_ \\| | '_ \\ / _` |" << endl;
323     eout << "\033[1;33m" << " \\ V V / (_| | | | | | | | | | | (_| |" << endl;
324     eout << "\033[1;33m" << " \\_/\\_/ \\__,_|_| |_| |_|_|_| |_|\\__, |" << endl;
325     eout << "\033[1;33m" << " __/ |" << endl;
326     eout << "\033[1;33m" << " |___/ " << endl;
327     eout << endl;
328     eout << "\033[1;33m [" << funcname << "] " << text << " \033[0m" << endl;
329     eout << endl << endl;
330 buchmann 1.1 }
331     void write_error(string funcname, string text) {
332 buchmann 1.13 eout << endl << endl;
333     eout << "\033[1;31m ___ _ __ _ __ ___ _ __ " << endl;
334     eout << "\033[1;31m / _ \\ __| __/ _ \\| '__|" << endl;
335     eout << "\033[1;31m| __/ | | | | (_) | | " << endl;
336     eout << "\033[1;31m \\___|_| |_| \\___/|_| " << endl;
337     eout << endl;
338     eout << "\033[1;31m [" << funcname << "] " << text << " \033[0m" << endl;
339     eout << endl << endl;
340 buchmann 1.1 }
341    
342     void write_info(string funcname, string text) {
343 buchmann 1.13 dout << endl << 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 << "\033[1;34m |_____|_| |_|_| \\___/ " << endl;
350     dout << endl;
351     dout << "\033[1;34m [" << funcname << "] " << text << " \033[0m" << endl;
352     dout << endl << endl;
353 buchmann 1.1 }
354    
355     TText* write_text(float xpos,float ypos,string title)
356     {
357     TLatex* titlebox = new TLatex (xpos,ypos,title.c_str());
358     titlebox->SetNDC(true);
359     titlebox->SetTextFont(42);
360     titlebox->SetTextSize(0.04);
361     titlebox->SetTextAlign(21);
362     return titlebox;
363     }
364    
365     TText* write_title(string title)
366     {
367 buchmann 1.15 TText* titlebox = write_text(0.5,0.945,title);
368 buchmann 1.1 return titlebox;
369     }
370    
371 buchmann 1.7 TText* write_cut_on_canvas(string cut) {
372     // TLatex *normbox = new TLatex(0.96,0.5,cut.c_str());
373     TLatex *normbox = new TLatex(0.96,0.5,"");//currently deactivated
374     normbox->SetNDC(true);
375     normbox->SetTextFont(42);
376     normbox->SetTextSize(0.01);
377     normbox->SetTextAlign(21);
378     normbox->SetTextAngle(270);
379     return normbox;
380     }
381    
382 buchmann 1.1 TText* write_title_low(string title)
383     {
384     TText* titlebox = write_text(0.5,0.94,title);
385     return titlebox;
386     }
387    
388 fronga 1.17 TLegend* make_legend(string title="", float posx=0.6, float posy=0.55)
389 buchmann 1.1 {
390     // TLegend *leg = new TLegend(0.65,0.65,0.89,0.89);
391     gStyle->SetTextFont(42);
392 buchmann 1.7 TLegend *leg = new TLegend(posx,posy,0.89,0.89);
393 buchmann 1.1 if(title!="") leg->SetHeader(title.c_str());
394     leg->SetTextFont(42);
395 fronga 1.17 leg->SetTextSize(0.04);
396 buchmann 1.1 leg->SetFillColor(kWhite);
397     leg->SetBorderSize(0);
398     leg->SetLineColor(kWhite);
399 buchmann 1.5 DrawPrelim();
400     /* TText *writeline1 = write_text(0.77,0.87,"CMS Preliminary 2011");
401 buchmann 1.1 stringstream lumitext;
402 buchmann 1.5 lumitext<<"#sqrt{s}=7, L = "<<lumi<<" pb^{-1}";
403 buchmann 1.1 TText *writeline2 = write_text(0.77,0.83,lumitext.str().c_str());
404     writeline1->SetTextSize(0.03);
405     writeline2->SetTextSize(0.03);
406     writeline1->Draw();
407     writeline2->Draw();
408 buchmann 1.5 */
409 buchmann 1.1 return leg;
410     }
411    
412     TGraph* make_nice_ratio(int nbins,float binning[],TH1F* histo)
413     {
414     float errorsquared[nbins];
415     float errors[nbins];
416     float bincontent[nbins];
417     for (int i=0;i<nbins;i++) {
418     errorsquared[i]=0;
419     bincontent[i]=0;
420     errors[i]=0;
421     }
422     float currlimit=binning[0];
423     int currtoplim=1;
424     for(int ibin=1;ibin<=histo->GetNbinsX();ibin++)
425     {
426     if(binning[currtoplim]<histo->GetBinCenter(ibin)) currtoplim++;
427 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;
428 buchmann 1.1
429     }
430    
431     return 0;
432     }
433    
434     float statErrorN(float x){return x - 0.5*TMath::ChisquareQuantile(0.3173/2,2*x);}
435     float statErrorP(float x){return 0.5*TMath::ChisquareQuantile(1-0.3173/2,2*(x+1))-x;}
436     float lowLimit(float a, float x){return 0.5*TMath::ChisquareQuantile(a,2*x);}
437     float highLimit(float a,float x){return 0.5*TMath::ChisquareQuantile(1-a,2*(x+1));}
438    
439     float computeRatioError(float a, float da, float b, float db)
440     {
441     float val=0.;
442     float errorSquare = (a/b)*(a/b)*( (da/a)*(da/a) + (db/b)*(db/b));
443     val = TMath::Sqrt(errorSquare);
444     return val;
445    
446     }
447     float computeProductError(float a, float da, float b, float db)
448     {
449     float val=0.;
450     float errorSquare = (a*b)*(a*b)*( (da/a)*(da/a) + (db/b)*(db/b));
451     val = TMath::Sqrt(errorSquare);
452     return val;
453     }
454    
455     TGraphAsymmErrors *histRatio(TH1F *h1,TH1F *h2, int id, vector<float>binning)
456     {
457     int absJZBbinsNumber = binning.size()-1;
458     TGraphAsymmErrors* graph = new TGraphAsymmErrors(absJZBbinsNumber);
459    
460     for(unsigned int i=0;i<absJZBbinsNumber;i++)
461     {
462     float xCenter=h1->GetBinCenter(i+1);
463     float xWidth=(h1->GetBinWidth(i+1))*0.5;
464     float nominatorError = h1->GetBinError(i+1);
465     float nominator=h1->GetBinContent(i+1);
466     float denominatorError=h2->GetBinError(i+1);
467     float denominator=h2->GetBinContent(i+1);
468     float errorN = 0;
469     float errorP = computeRatioError(nominator,nominatorError,denominator,denominatorError);
470     if(id==1) // (is data)
471     {
472     errorP = computeRatioError(nominator,statErrorP(nominator),denominator,statErrorP(denominator));
473     errorN = errorP; // symmetrize using statErrorP
474     } else {
475     errorN = computeRatioError(nominator,nominatorError,denominator,denominatorError);
476     errorP = computeRatioError(nominator,nominatorError,denominator,denominatorError);
477     }
478     if(denominator!=0) {
479     graph->SetPoint(i, xCenter, nominator/denominator);
480     graph->SetPointError(i,xWidth,xWidth,errorN,errorP);
481     }
482     else {
483     graph->SetPoint(i, xCenter, -999);
484     graph->SetPointError(i,xWidth,xWidth,errorN,errorP);
485     }
486     }
487     return graph;
488     }
489    
490     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!
491     float uperr=0,downerr=0;
492     if(down>up&&down>cent) uperr=down-cent;
493     if(up>down&&up>cent) uperr=up-cent;
494     if(down<cent&&down<up) downerr=cent-down;
495     if(up<cent&&up<down) downerr=cent-up;
496     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!");
497     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!");
498     stringstream result;
499     result << cent << " + " << uperr << " - " << downerr;
500     return result.str();
501     }
502    
503     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")
504     {
505     int last = size - 2;
506     int isChanged = 1;
507    
508     while ( last >= 0 && isChanged )
509     {
510     isChanged = 0;
511     for ( int k = 0; k <= last; k++ )
512     if ( arr[k] > arr[k+1] )
513     {
514     swap ( arr[k], arr[k+1] );
515     isChanged = 1;
516     int bkp=order[k];
517     order[k]=order[k+1];
518     order[k+1]=bkp;
519     }
520     last--;
521     }
522     }
523    
524     void swapvec(vector<float> &vec,int j, int k) {
525     float bkp=vec[j];
526     vec[j]=vec[k];
527     vec[k]=bkp;
528     }
529    
530     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")
531     {
532     int last = arr.size() - 2;
533     int isChanged = 1;
534    
535     while ( last >= 0 && isChanged )
536     {
537     isChanged = 0;
538     for ( int k = 0; k <= last; k++ )
539     if ( arr[k] > arr[k+1] )
540     {
541     swapvec (arr,k,k+1);
542     isChanged = 1;
543     int bkp=order[k];
544     order[k]=order[k+1];
545     order[k+1]=bkp;
546     }
547     last--;
548     }
549     }
550    
551     int numerichistoname=0;
552 buchmann 1.16 bool givingnumber=false;
553 buchmann 1.1 string GetNumericHistoName() {
554 buchmann 1.16 while(givingnumber) sleep(1);
555     givingnumber=true;
556 buchmann 1.1 stringstream b;
557     b << "h_" << numerichistoname;
558     numerichistoname++;
559 buchmann 1.16 givingnumber=false;
560 buchmann 1.1 return b.str();
561     }
562    
563     //********************** BELOW : CUT INTERPRETATION **************************//
564     void splitupcut(string incut, vector<string> &partvector)
565     {
566     //idea: go thru the string called incut; if a parantheses is opened, then the cut cannot be split up until the parantheses is closed.
567     //ok anyway screw the parantheses.
568     int paranthesis_open=0;
569     int substr_start=0;
570     string currchar="";
571     for (int ichar=0;ichar<incut.length();ichar++)
572     {
573     currchar=incut.substr(ichar,1);
574     // if(currchar=="(") paranthesis_open++;
575     // if(currchar==")") paranthesis_open--;
576     if(currchar=="&"&&incut.substr(ichar+1,1)=="&"&&paranthesis_open==0) {
577     partvector.push_back(incut.substr(substr_start,ichar-substr_start));
578     substr_start=ichar+2;
579     }
580     }
581     partvector.push_back(incut.substr(substr_start,incut.length()-substr_start));
582     if(Verbosity>1) {
583 buchmann 1.13 dout << "[ splitupcut() ] : The cut vector now contains the following elements: "<< endl;
584 buchmann 1.1 for (int ipart=0;ipart<partvector.size();ipart++)
585     {
586 buchmann 1.13 dout << " - " << partvector[ipart] << endl;
587 buchmann 1.1 }
588     }
589     }
590    
591     int atleastvalue(string expression, int &morethanlessthan) // takes in an expression such as ">2" or ">=3" and returns e.g. 3 (in both examples)
592     {
593     int retval=0;
594     if(expression.substr(0,1)==">"&&expression.substr(1,1)=="=") {
595 buchmann 1.13 // dout << "The expression " << expression << " is saying that we have at least " << atoi(expression.substr(2,1).c_str()) << " jets" << endl;
596 buchmann 1.1 morethanlessthan=1;
597     return atoi(expression.substr(2,1).c_str());
598     }
599     if(expression.substr(0,1)=="="&&expression.substr(1,1)=="=") {
600 buchmann 1.13 // dout << "The expression " << expression << " is saying that we have at least " << atoi(expression.substr(1,1).c_str())+1 << " jets" << endl;
601 buchmann 1.1 morethanlessthan=0;
602     return atoi(expression.substr(1,1).c_str());
603     }
604     if(expression.substr(0,1)=="<"&&expression.substr(1,1)=="=") {
605 buchmann 1.13 // dout << "The expression " << expression << " is saying that we have at least " << atoi(expression.substr(1,1).c_str())+1 << " jets" << endl;
606 buchmann 1.1 morethanlessthan=-1;
607     return 1+atoi(expression.substr(1,1).c_str());
608     }
609     if(expression.substr(0,1)==">") {
610 buchmann 1.13 // dout << "The expression " << expression << " is saying that we have at least " << atoi(expression.substr(2,1).c_str()) << " jets" << endl;
611 buchmann 1.1 morethanlessthan=1;
612     return 1+atoi(expression.substr(2,1).c_str());
613     }
614     if(expression.substr(0,1)=="<"&&expression.substr(1,1)=="=") {
615 buchmann 1.13 // dout << "The expression " << expression << " is saying that we have at least " << atoi(expression.substr(2,1).c_str()) << " jets" << endl;
616 buchmann 1.1 morethanlessthan=-1;
617     return 1+atoi(expression.substr(2,1).c_str());
618     }
619     }
620    
621     int do_jet_cut(string incut, int *nJets) {
622     string expression=(incut.substr(12,incut.size()-12));
623 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;
624 buchmann 1.1 if(expression.substr(0,1)=="<"&&expression.substr(1,1)=="=") {
625     int nJet=atoi(expression.substr(2,1).c_str());
626     for(int i=nJet+1;i<20;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<20&&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)==">"&&expression.substr(1,1)=="=") {
637     int nJet=atoi(expression.substr(2,1).c_str());
638     for(int i=0;i<nJet&&i!=nJet;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=nJet;i<20;i++) nJets[i]=0;
645 buchmann 1.13 dout << "Is of type <" << endl;
646 buchmann 1.1 return 0;
647     }
648     if(expression.substr(0,1)==">") {
649     int nJet=atoi(expression.substr(1,1).c_str());
650     for(int i=0;i<nJet+1&&i!=nJet;i++) nJets[i]=0;
651 buchmann 1.13 dout << "Is of type >" << endl;
652 buchmann 1.1 return 0;
653     }
654     }
655    
656     string interpret_cut(string incut, bool &isJetCut, int *permittednJets)
657     {
658     // isJetCut=false;nJets=-1;
659     if(incut=="()") return "";
660     while(incut.substr(0,1)=="(") incut=incut.substr(1,incut.length()-1);
661     while(incut.length()>0&&incut.substr(incut.length()-1,1)==")") incut=incut.substr(0,incut.length()-1);
662     // 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.
663    
664     if(Verbosity>0) {
665 buchmann 1.13 dout << "Now interpreting cut " << incut << endl;
666 buchmann 1.1 }
667     /*
668     if(incut=="ch1*ch2<0") return "OS";
669     if(incut=="id1==id2") return "SF";
670     if(incut=="id1!=id2") return "OF";
671     */
672     if(incut=="ch1*ch2<0") return "";
673 buchmann 1.5 if(incut=="(mll>55&&mll<70)||(mll>112&&mll<160)") return "SB";
674     if(incut=="(mll>61&&mll<70)||(mll>112&&mll<190)") return "SB'";
675 buchmann 1.1 if(incut=="id1==id2") return "";
676     if(incut=="id1!=id2") return "";
677     if(incut=="mll>2") return "";
678    
679     if(incut=="mll>0") return ""; // my typical "fake cut"
680    
681     if(incut=="passed_triggers||!is_data") return "Triggers";
682     if(incut=="pfjzb[0]>-998") return "";
683    
684    
685     if(incut=="id1==0") return "ee";
686     if(incut=="id1==1") return "#mu#mu";
687     if(incut=="abs(mll-91.2)<20") return "|m_{l^{+}l^{-}}-m_{Z}|<20";
688     if(incut=="pfJetGoodID[0]") return "";
689     if(incut=="pfJetGoodID[1]") return "";
690     if((int)incut.find("pfJetGoodNum")>-1) {
691     //do_jet_cut(incut,permittednJets);
692     stringstream result;
693     result << "nJets" << incut.substr(12,incut.size()-12);
694 buchmann 1.13 /* dout << "Dealing with a jet cut: " << incut << endl;
695 buchmann 1.1 stringstream result;
696     result << "nJets" << incut.substr(12,incut.size()-12);
697     isJetCut=true;
698     if(exactjetcut(incut,nJets))
699     // nJets=atleastvalue((incut.substr(12,incut.size()-12)),morethanlessthan);
700     return result.str();*/
701     return result.str();
702     }
703     return incut;
704     }
705    
706     string interpret_nJet_range(int *nJets) {
707 buchmann 1.13 for (int i=0;i<20;i++) dout << i << " : " << nJets[i] << endl;
708 buchmann 1.1 return "hello";
709     }
710    
711     string interpret_cuts(vector<string> &cutparts)
712     {
713     stringstream nicecut;
714     int nJets;
715     bool isJetCut;
716     int finalJetCut=-1;
717     int permittednJets[20];
718     for(int ijet=0;ijet<20;ijet++) permittednJets[ijet]=1;
719     int morethanlessthan=0;//-1: less than, 0: exactly, 1: more than
720     for(int icut=0;icut<cutparts.size();icut++)
721     {
722     if(icut==0) nicecut<<interpret_cut(cutparts[icut],isJetCut,permittednJets);
723     else {
724     string nice_this_cut = interpret_cut(cutparts[icut],isJetCut,permittednJets);//blublu
725     if(nice_this_cut.length()>0&&nicecut.str().length()>0) {
726     if(!isJetCut) nicecut<<"&&"<<nice_this_cut;
727     else {
728     if(nJets>finalJetCut) finalJetCut=nJets;
729     }
730     }
731     if(nice_this_cut.length()>0&&nicecut.str().length()==0) {
732     if(!isJetCut) {
733     nicecut<<nice_this_cut;
734     }
735     else {
736     if(nJets>finalJetCut) finalJetCut=nJets;
737     }
738     }
739     }
740     }
741     if(finalJetCut>-1) {
742     if(nicecut.str().length()==0) {
743     nicecut << "nJets#geq" << finalJetCut;
744     }
745     else
746     {
747     nicecut << "&&nJets#geq " << finalJetCut;
748     }
749     }
750    
751 buchmann 1.13 // dout << "The nJet allowed range is given by: " << interpret_nJet_range(permittednJets) << endl;
752 buchmann 1.1
753     return nicecut.str();
754     }
755    
756     string decipher_cut(TCut originalcut,TCut ignorethispart)
757     {
758     string incut=(const char*)originalcut;
759     string ignore=(const char*)ignorethispart;
760    
761     if(ignore.length()>0 && incut.find(ignore)!=string::npos) incut=incut.replace(incut.find(ignore),ignore.length(),"");
762    
763     vector<string>cutparts;
764     splitupcut(incut,cutparts);
765     string write_cut=interpret_cuts(cutparts);
766     return write_cut;
767     }
768    
769     //********************** ABOVE : CUT INTERPRETATION **************************//
770 buchmann 1.2
771     Double_t GausRandom(Double_t mu, Double_t sigma) {
772     return gRandom->Gaus(mu,sigma);// real deal
773     //return mu;//debugging : no smearing.
774     }
775    
776 buchmann 1.3 int functionalhistocounter=0;
777 buchmann 1.2 TH1F * makehistofromfunction(TF1 *f1,TH1F *model) {
778     TH1F *histo = (TH1F*)model->Clone();
779 buchmann 1.3 functionalhistocounter++;
780     stringstream histoname;
781     histoname << "histo_based_on_function_" << f1->GetName() << "__"<<functionalhistocounter;
782     histo->SetTitle(histoname.str().c_str());
783     histo->SetName(histoname.str().c_str());
784 buchmann 1.2 int nbins=histo->GetNbinsX();
785     float low=histo->GetBinLowEdge(1);
786     float hi=histo->GetBinLowEdge(histo->GetNbinsX())+histo->GetBinWidth(histo->GetNbinsX());
787    
788     for(int i=0;i<=nbins;i++) {
789 buchmann 1.4 histo->SetBinContent(i,(f1->Integral(histo->GetBinLowEdge(i),histo->GetBinLowEdge(i)+histo->GetBinWidth(i)))/histo->GetBinWidth(i));
790     histo->SetBinError(i,TMath::Sqrt(histo->GetBinContent(i)));
791 buchmann 1.2 }
792    
793     return histo;
794 buchmann 1.4 }
795    
796     float hintegral(TH1 *histo, float low, float high) {
797     float sum=0;
798     for(int i=1;i<histo->GetNbinsX();i++) {
799     if((histo->GetBinLowEdge(i)>=low)&&(histo->GetBinLowEdge(i)+histo->GetBinWidth(i))<=high) sum+=histo->GetBinContent(i);
800     //now on to the less clear cases!
801     if(histo->GetBinLowEdge(i)<low&&(histo->GetBinLowEdge(i)+histo->GetBinWidth(i))>low) {
802     //need to consider this case still ... the bin is kind of in range but not sooooo much.
803     }
804     if(histo->GetBinLowEdge(i)<high&&(histo->GetBinLowEdge(i)+histo->GetBinWidth(i))>high) {
805     //need to consider this case still ... the bin is kind of in range but not sooooo much.
806     }
807    
808     }
809     return sum;
810 buchmann 1.5 }
811    
812 buchmann 1.7 void DrawPrelim_OLD() {
813 buchmann 1.5 TText *writeline1 = write_text(0.77,0.87,"CMS Preliminary 2011");
814     stringstream lumitext;
815     lumitext<<"#sqrt{s}=7, L="<< lumi/1000<<" fb^{-1}";
816     TText *writeline2 = write_text(0.77,0.83,lumitext.str().c_str());
817     writeline1->SetTextSize(0.03);
818     writeline2->SetTextSize(0.03);
819     writeline1->Draw();
820     writeline2->Draw();
821     }
822 buchmann 1.6
823 buchmann 1.7 void DrawPrelim() {
824     string barn="pb";
825     float writelumi=lumi;
826     if(writelumi>=1000)
827     {
828     writelumi/=1000;
829     barn="fb";
830     }
831    
832 buchmann 1.8 stringstream prelimtext;
833 buchmann 1.9 //prelimtext << "CMS Preliminary 2011 , #sqrt{s}= 7 TeV, L= O(1) fb^{-1}"; //temporary replacement
834 fronga 1.17 prelimtext << "CMS Preliminary 2011 , #sqrt{s}= 7 TeV, L= "
835     << std::setprecision(2) <<writelumi<<" "<<barn<<"^{-1}";
836 buchmann 1.10 TPaveText *eventSelectionPaveText = new TPaveText(0.27, 0.93,0.77, 1.0,"blNDC");
837 buchmann 1.8 eventSelectionPaveText->SetFillStyle(4000);
838     eventSelectionPaveText->SetBorderSize(0.1);
839     eventSelectionPaveText->SetFillColor(kWhite);
840     eventSelectionPaveText->SetTextFont(42);
841 buchmann 1.10 eventSelectionPaveText->SetTextSize(0.0415);
842 buchmann 1.8 eventSelectionPaveText->AddText(prelimtext.str().c_str());
843     eventSelectionPaveText->Draw();
844 buchmann 1.7 }
845    
846 buchmann 1.6 string newjzbexpression(string oldexpression,float shift) {
847     stringstream ss;
848     if(shift>0) ss<<"("<<oldexpression<<"+"<<shift<<")";
849     if(shift<0) ss<<"("<<oldexpression<<shift<<")";
850     if(shift==0) ss<<oldexpression;
851     return ss.str();
852     }
853    
854 buchmann 1.11 double Round(double num, unsigned int dig)
855     {
856     num *= pow(10, dig);
857     if (num >= 0)
858     num = floor(num + 0.5);
859     else
860     num = ceil(num - 0.5);
861     num/= pow(10, dig);
862     return num;
863 buchmann 1.16 }
864    
865     // The two functions below are for distributed processing
866    
867     int get_job_number(float ipoint, float Npoints,float Njobs) {
868     float pointposition=(ipoint/Npoints);
869     int njob=floor(pointposition*Njobs);
870     if(njob>=Njobs) njob--;
871     // cout << "Looking at point " << ipoint << " out of " << Npoints << " which is at position " << pointposition << " corresponding to " << pointposition*Njobs << " --> JOB " << njob << endl;
872     return njob;
873     }
874    
875    
876     bool do_this_point(int ipoint, int Npoints, int jobnumber, int Njobs) {
877     if(get_job_number(ipoint,Npoints,Njobs)==jobnumber) return true;
878     return false;
879     }