ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/cbrown/AnalysisFramework/Plotting/Modules/GeneralToolBox.C
Revision: 1.14
Committed: Wed Jul 20 12:26:53 2011 UTC (13 years, 9 months ago) by buchmann
Content type: text/plain
Branch: MAIN
Changes since 1.13: +3 -1 lines
Log Message:
Error log file now in LOGerr.txt

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