CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Tools.cc
Go to the documentation of this file.
2 
3 #include "TROOT.h"
4 #include "TSystem.h"
5 #include "TStyle.h"
6 #include <math.h>
7 
8 using namespace std;
9 using namespace RecoBTag;
10 
11 //
12 //
13 // TOOLS
14 //
15 //
16 
17 
19 double RecoBTag::HistoBinWidth ( const TH1F * theHisto , const int& iBin ) {
20  const int& nBins = theHisto->GetSize() ; // includes underflow/overflow
21  // return 0.0 , if invalid bin
22  if ( iBin < 0 || iBin >= nBins ) return 0.0 ;
23  // return first binwidth, if underflow bin
24  if ( iBin == 0 ) return theHisto->GetBinWidth ( 1 ) ;
25  // return last real binwidth, if overflow bin
26  if ( iBin == nBins - 1 ) return theHisto->GetBinWidth ( nBins - 2 ) ;
27  // return binwidth from histo, if within range
28  return theHisto->GetBinWidth ( iBin ) ;
29 }
31 
32 
34 double RecoBTag::IntegrateHistogram ( const TH1F * theHisto ) {
35  // include underflow and overflow: assign binwidth of first/last bin to them!!
36  // integral = sum ( entry_i * binwidth_i )
37  //
38  double histoIntegral = 0.0 ;
39  const int& nBins = theHisto->GetSize() ;
40  //
41  // loop over bins:
42  // bin 0 : underflow
43  // bin nBins-1 : overflow
44  for ( int iBin = 0 ; iBin != nBins ; ++iBin ) {
45  const double& binWidth = HistoBinWidth ( theHisto , iBin ) ;
46  histoIntegral += (*theHisto)[iBin] * binWidth ;
47  }
48  //
49  return histoIntegral ;
50 }
52 
53 
54 
55 
57 void RecoBTag::HistoToNormalizedArrays ( const TH1F * theHisto , TArrayF & theNormalizedArray , TArrayF & theLeftOfBinArray , TArrayF & theBinWidthArray ) {
58 
59  const int& nBins = theHisto->GetSize() ;
60 
61  // check that all arrays/histo have the same size
62  if ( nBins == theNormalizedArray.GetSize() &&
63  nBins == theLeftOfBinArray .GetSize() &&
64  nBins == theBinWidthArray .GetSize() ) {
65 
66  const double& histoIntegral = IntegrateHistogram ( theHisto ) ;
67 
68  for ( int iBin = 0 ; iBin != nBins ; ++iBin ) {
69  theNormalizedArray[iBin] = (*theHisto)[iBin] / histoIntegral ;
70  theLeftOfBinArray [iBin] = theHisto->GetBinLowEdge(iBin) ;
71  theBinWidthArray [iBin] = HistoBinWidth ( theHisto , iBin ) ;
72  }
73 
74  }
75  else {
76  cout << "============>>>>>>>>>>>>>>>>" << endl
77  << "============>>>>>>>>>>>>>>>>" << endl
78  << "============>>>>>>>>>>>>>>>>" << endl
79  << "============>>>>>>>>>>>>>>>>" << endl
80  << "============>>>>>>>>>>>>>>>> HistoToNormalizedArrays failed: not equal sizes of all arrays!!" << endl
81  << "============>>>>>>>>>>>>>>>>" << endl
82  << "============>>>>>>>>>>>>>>>>" << endl
83  << "============>>>>>>>>>>>>>>>>" << endl
84  << "============>>>>>>>>>>>>>>>>" << endl ;
85  }
86 
87 }
89 
90 
91 
93 double RecoBTag::IntegrateArray ( const TArrayF & theArray , const TArrayF & theBinWidth ) {
94 
95  double arrayIntegral = 0.0 ;
96  const int& nBins = theArray.GetSize() ;
97  //
98  for ( int iBin = 0 ; iBin != nBins ; ++iBin ) {
99  arrayIntegral += theArray[iBin] * theBinWidth[iBin] ;
100  }
101  //
102  return arrayIntegral ;
103 }
105 
106 
107 
109 void RecoBTag::PrintCanvasHistos ( TCanvas * canvas , const std::string& psFile , const std::string& epsFile , const std::string& gifFile ) {
110  //
111  //
112  // to create gif in 'batch mode' (non-interactive) see
113  // http://root.cern.ch/cgi-bin/print_hit_bold.pl/root/roottalk/roottalk00/0402.html?gifbatch#first_hit
114  //
115  // ROOT 4 can do it!!??
116  //
117  // if string = "" don't print to corresponding file
118  //
119  if ( psFile != "" ) canvas->Print ( psFile.c_str() ) ;
120  if ( epsFile != "" ) canvas->Print ( epsFile.c_str() , "eps" ) ;
121  // if in batch: use a converter tool
122  const std::string& rootVersion ( gROOT->GetVersion() ) ;
123  const bool& rootCanGif = rootVersion.find("4") == 0 || rootVersion.find("5") == 0 ;
124  if ( gifFile != "" ) {
125  if ( !(gROOT->IsBatch()) || rootCanGif ) { // to find out if running in batch mode
126  cout << "--> Print directly gif!" << endl ;
127  canvas->Print ( gifFile.c_str() , "gif" ) ;
128  }
129  else {
130  if ( epsFile != "" ) { // eps file must have been created before
131  cout << "--> Print gif via scripts!" << endl ;
132  const std::string& executeString1 = "pstopnm -ppm -xborder 0 -yborder 0 -portrait " + epsFile ;
133  gSystem->Exec(executeString1.c_str()) ;
134  const std::string& ppmFile = epsFile + "001.ppm" ;
135  const std::string& executeString2 = "ppmtogif " + ppmFile + " > " + gifFile ;
136  gSystem->Exec(executeString2.c_str()) ;
137  const std::string& executeString3 = "rm " + ppmFile ;
138  gSystem->Exec(executeString3.c_str()) ; // delete the intermediate file
139  }
140  }
141  }
142 }
144 
145 
147 TObjArray RecoBTag::getHistArray ( TFile * histoFile , const std::string& baseName ) {
148  //
149  // return the TObjArray built from the basename
150  //
151  //
152  TObjArray histos (3) ; // reserve 3
153  //
154  const std::string nameB ( baseName + "B" ) ;
155  const std::string nameC ( baseName + "C" ) ;
156  const std::string nameDUSG ( baseName + "DUSG" ) ;
157  //
158  histos.Add ( (TH1F*)histoFile->Get( nameB.c_str() ) ) ;
159  histos.Add ( (TH1F*)histoFile->Get( nameC.c_str() ) ) ;
160  histos.Add ( (TH1F*)histoFile->Get( nameDUSG.c_str() ) ) ;
161  //
162  return histos ;
163 }
165 
166 
168 std::string RecoBTag::flavour ( const int& flav ) {
169  switch(flav) {
170  case 1 :
171  return "d";
172  case 2 :
173  return "u";
174  case 3 :
175  return "s";
176  case 4 :
177  return "c";
178  case 5 :
179  return "b";
180  case 21 :
181  return "g";
182  default :
183  return "";
184  }
185 }
187 
188 
190 bool RecoBTag::flavourIsD ( const int & flav ) { return flav == 1 ; }
191 bool RecoBTag::flavourIsU ( const int & flav ) { return flav == 2 ; }
192 bool RecoBTag::flavourIsS ( const int & flav ) { return flav == 3 ; }
193 bool RecoBTag::flavourIsC ( const int & flav ) { return flav == 4 ; }
194 bool RecoBTag::flavourIsB ( const int & flav ) { return flav == 5 ; }
195 bool RecoBTag::flavourIsG ( const int & flav ) { return flav == 21 ; }
196 
197 bool RecoBTag::flavourIsDUS ( const int & flav ) { return ( flavourIsD(flav) || flavourIsU(flav) || flavourIsS(flav) ) ; }
198 bool RecoBTag::flavourIsDUSG ( const int & flav ) { return ( flavourIsDUS(flav) || flavourIsG(flav) ) ; }
199 
200 bool RecoBTag::flavourIsNI ( const int & flav ) { return !( flavourIsD(flav) || flavourIsU(flav) || flavourIsS(flav) ||
201  flavourIsC(flav) || flavourIsB(flav) || flavourIsG(flav) ) ; }
203 
206  cout << "====>>>> ToolsC:checkCreateDirectory() : " << endl ;
207  int exists = gSystem->Exec ( ("ls -d " + directory).c_str() ) ;
208  // create it if it doesn't exist
209  if ( exists != 0 ) {
210  cout << "====>>>> ToolsC:checkCreateDirectory() : The directory does not exist : " << directory << endl ;
211  cout << "====>>>> ToolsC:checkCreateDirectory() : I'll try to create it" << endl ;
212  const int& create = gSystem->Exec ( ("mkdir " + directory).c_str() ) ;
213  if ( create != 0 ) {
214  cout << "====>>>> ToolsC:checkCreateDirectory() : Creation of directory failed : " << directory << endl
215  << "====>>>> ToolsC:checkCreateDirectory() : Please check your write permissions!" << endl ;
216  }
217  else {
218  cout << "====>>>> ToolsC:checkCreateDirectory() : Creation of directory successful!" << endl ;
219  // check again if it exists now
220  cout << "====>>>> ToolsC:checkCreateDirectory() : " << endl ;
221  exists = gSystem->Exec ( ("ls -d " + directory).c_str() ) ;
222  if ( exists != 0 ) cout << "ToolsC:checkCreateDirectory() : However, it still doesn't exist!?" << endl ;
223  }
224  }
225  cout << endl ;
226  return exists ;
227 }
229 
230 
231 
233 int RecoBTag::findBinClosestYValue ( const TH1F * histo , const float& yVal , const float& yLow , const float& yHigh ) {
234  //
235  // Find the bin in a 1-dim. histogram which has its y-value closest to
236  // the given value yVal where the value yVal has to be in the range yLow < yVal < yHigh.
237  // If it is outside this range the corresponding bin number is returned as negative value.
238  // Currently, there is no protection if there are many bins with the same value!
239  // Teh user has to take care to interpret the output correctly.
240  //
241 
242  // init
243  const int& nBins = histo->GetNbinsX() - 2 ; // -2 because we don't include under/overflow alos in this loop
244  int iBinClosestInit = 0 ;
245  // init start value properly: must avoid that the real one is not filled
246  float yClosestInit ;
247  //
248  const float& maxInHisto = histo->GetMaximum() ;
249  const float& minInHisto = histo->GetMinimum() ;
250  //
251  // if yVal is smaller than max -> take any value well above the maximum
252  if ( yVal <= maxInHisto ) {
253  yClosestInit = maxInHisto + 1 ; }
254  else {
255  // if yVal is greater than max value -> take a value < minimum
256  yClosestInit = minInHisto - 1.0 ;
257  }
258 
260  float yClosest = yClosestInit ;
261 
262  // loop over bins of histogram
263  for ( int iBin = 1 ; iBin <= nBins ; ++iBin ) {
264  const float& yBin = histo->GetBinContent(iBin) ;
265  if ( fabs(yBin-yVal) < fabs(yClosest-yVal) ) {
266  yClosest = yBin ;
267  iBinClosest = iBin ;
268  }
269  }
270 
271  // check if in interval
272  if ( yClosest < yLow || yClosest > yHigh ) {
273  iBinClosest *= -1 ;
274  }
275 
276  // check that not the initialization bin (would mean that init value was the closest)
277  if ( iBinClosest == iBinClosestInit ) {
278  cout << "====>>>> ToolsC=>findBinClosestYValue() : WARNING: returned bin is the initialization bin!!" << endl ;
279  }
280 
281  return iBinClosest ;
282 }
284 
285 
286 
288  TStyle *tdrStyle = new TStyle("tdrStyle","Style for P-TDR");
289 
290 // For the canvas:
291  tdrStyle->SetCanvasBorderMode(0);
292  tdrStyle->SetCanvasColor(kWhite);
293  tdrStyle->SetCanvasDefH(600); //Height of canvas
294  tdrStyle->SetCanvasDefW(600); //Width of canvas
295  tdrStyle->SetCanvasDefX(0); //POsition on screen
296  tdrStyle->SetCanvasDefY(0);
297 
298 // For the Pad:
299  tdrStyle->SetPadBorderMode(0);
300  // tdrStyle->SetPadBorderSize(Width_t size = 1);
301  tdrStyle->SetPadColor(kWhite);
302  tdrStyle->SetPadGridX(false);
303  tdrStyle->SetPadGridY(false);
304  tdrStyle->SetGridColor(0);
305  tdrStyle->SetGridStyle(3);
306  tdrStyle->SetGridWidth(1);
307 
308 // For the frame:
309  tdrStyle->SetFrameBorderMode(0);
310  tdrStyle->SetFrameBorderSize(1);
311  tdrStyle->SetFrameFillColor(0);
312  tdrStyle->SetFrameFillStyle(0);
313  tdrStyle->SetFrameLineColor(1);
314  tdrStyle->SetFrameLineStyle(1);
315  tdrStyle->SetFrameLineWidth(1);
316 
317 // For the histo:
318  // tdrStyle->SetHistFillColor(1);
319  // tdrStyle->SetHistFillStyle(0);
320  tdrStyle->SetHistLineColor(1);
321  tdrStyle->SetHistLineStyle(0);
322  tdrStyle->SetHistLineWidth(1);
323  // tdrStyle->SetLegoInnerR(Float_t rad = 0.5);
324  // tdrStyle->SetNumberContours(Int_t number = 20);
325 
326  tdrStyle->SetEndErrorSize(15);
327 // tdrStyle->SetErrorMarker(20);
328  tdrStyle->SetErrorX(1);
329 
330  tdrStyle->SetMarkerStyle(21);
331  tdrStyle->SetMarkerSize(1.);
332 
333 //For the fit/function:
334  tdrStyle->SetOptFit(0);
335  tdrStyle->SetFitFormat("5.4g");
336  tdrStyle->SetFuncColor(2);
337  tdrStyle->SetFuncStyle(1);
338  tdrStyle->SetFuncWidth(1);
339 
340 //For the date:
341  tdrStyle->SetOptDate(0);
342  // tdrStyle->SetDateX(Float_t x = 0.01);
343  // tdrStyle->SetDateY(Float_t y = 0.01);
344 
345 // For the statistics box:
346  tdrStyle->SetOptFile(1111);
347  tdrStyle->SetOptStat(0); // To display the mean and RMS: SetOptStat("mr");
348  tdrStyle->SetStatColor(kWhite);
349  tdrStyle->SetStatFont(42);
350  tdrStyle->SetStatFontSize(0.025);
351  tdrStyle->SetStatTextColor(1);
352  tdrStyle->SetStatFormat("6.4g");
353  tdrStyle->SetStatBorderSize(1);
354  tdrStyle->SetStatH(0.2);
355  tdrStyle->SetStatW(0.15);
356  // tdrStyle->SetStatStyle(Style_t style = 1001);
357  // tdrStyle->SetStatX(Float_t x = 0);
358  // tdrStyle->SetStatY(Float_t y = 0);
359 
360 // Margins:
361  tdrStyle->SetPadTopMargin(0.05);
362  tdrStyle->SetPadBottomMargin(0.13);
363  tdrStyle->SetPadLeftMargin(0.16);
364  tdrStyle->SetPadRightMargin(0.02);
365 
366 // For the Global title:
367 
368  tdrStyle->SetOptTitle(0);
369  tdrStyle->SetTitleW(0.8); // Set the width of the title box
370 
371  tdrStyle->SetTitleFont(42);
372  tdrStyle->SetTitleColor(1);
373  tdrStyle->SetTitleTextColor(1);
374  tdrStyle->SetTitleFillColor(10);
375  tdrStyle->SetTitleFontSize(0.05);
376  // tdrStyle->SetTitleH(0); // Set the height of the title box
377  // tdrStyle->SetTitleX(0); // Set the position of the title box
378  // tdrStyle->SetTitleY(0.985); // Set the position of the title box
379  // tdrStyle->SetTitleStyle(Style_t style = 1001);
380  // tdrStyle->SetTitleBorderSize(2);
381 
382 // For the axis titles:
383 
384  tdrStyle->SetTitleColor(1, "XYZ");
385  tdrStyle->SetTitleFont(42, "XYZ");
386  tdrStyle->SetTitleSize(0.06, "XYZ");
387  // tdrStyle->SetTitleXSize(Float_t size = 0.02); // Another way to set the size?
388  // tdrStyle->SetTitleYSize(Float_t size = 0.02);
389  tdrStyle->SetTitleXOffset(0.75);
390  tdrStyle->SetTitleYOffset(0.75);
391  // tdrStyle->SetTitleOffset(1.1, "Y"); // Another way to set the Offset
392 
393 // For the axis labels:
394 
395  tdrStyle->SetLabelColor(1, "XYZ");
396  tdrStyle->SetLabelFont(42, "XYZ");
397  tdrStyle->SetLabelOffset(0.007, "XYZ");
398  tdrStyle->SetLabelSize(0.05, "XYZ");
399 
400 // For the axis:
401 
402  tdrStyle->SetAxisColor(1, "XYZ");
403  tdrStyle->SetStripDecimals(kTRUE);
404  tdrStyle->SetTickLength(0.03, "XYZ");
405  tdrStyle->SetNdivisions(510, "XYZ");
406  tdrStyle->SetPadTickX(1); // To get tick marks on the opposite side of the frame
407  tdrStyle->SetPadTickY(1);
408 
409 // Change for log plots:
410  tdrStyle->SetOptLogx(0);
411  tdrStyle->SetOptLogy(0);
412  tdrStyle->SetOptLogz(0);
413 
414 // Postscript options:
415  tdrStyle->SetPaperSize(21.,28.);
416 // tdrStyle->SetPaperSize(20.,20.);
417  // tdrStyle->SetLineScalePS(Float_t scale = 3);
418  // tdrStyle->SetLineStyleString(Int_t i, const char* text);
419  // tdrStyle->SetHeaderPS(const char* header);
420  // tdrStyle->SetTitlePS(const char* pstitle);
421 
422  // tdrStyle->SetBarOffset(Float_t baroff = 0.5);
423  // tdrStyle->SetBarWidth(Float_t barwidth = 0.5);
424  // tdrStyle->SetPaintTextFormat(const char* format = "g");
425  // tdrStyle->SetPalette(Int_t ncolors = 0, Int_t* colors = 0);
426  // tdrStyle->SetTimeOffset(Double_t toffset);
427  // tdrStyle->SetHistMinimumZero(kTRUE);
428 
429  tdrStyle->cd();
430  return tdrStyle;
431 }
432 // tdrGrid: Turns the grid lines on (true) or off (false)
433 
434 void RecoBTag::tdrGrid(const bool& gridOn) {
435  TStyle *tdrStyle = setTDRStyle();
436  tdrStyle->SetPadGridX(gridOn);
437  tdrStyle->SetPadGridY(gridOn);
438  tdrStyle->cd();
439 }
440 
441 // fixOverlay: Redraws the axis
442 
444  gPad->RedrawAxis();
445 }
446 
447 
448 string RecoBTag::itos(const int& i) // convert int to string
449 {
450  ostringstream s;
451  s << i;
452  return s.str();
453 }
454 
455 
456 
457 
458 
459 
TFile * histoFile
Definition: hcalCalib.cc:44
int i
Definition: DBlmapReader.cc:9
void PrintCanvasHistos(TCanvas *canvas, const std::string &psFile, const std::string &epsFile, const std::string &gifFile)
Definition: Tools.cc:109
tuple maxInHisto
Definition: cuy.py:881
bool flavourIsG(const int &flav)
Definition: Tools.cc:195
tuple yBin
Definition: cuy.py:891
def setTDRStyle
Definition: plotscripts.py:87
bool flavourIsNI(const int &flav)
Definition: Tools.cc:200
double IntegrateHistogram(const TH1F *theHisto)
Definition: Tools.cc:34
TStyle * setTDRStyle()
Definition: Tools.cc:287
bool flavourIsD(const int &flav)
Definition: Tools.cc:190
int findBinClosestYValue(const TH1F *, const float &yVal, const float &yLow, const float &yHigh)
Definition: Tools.cc:233
bool flavourIsC(const int &flav)
Definition: Tools.cc:193
bool flavourIsU(const int &flav)
Definition: Tools.cc:191
int checkCreateDirectory(const std::string &)
Definition: Tools.cc:205
def canvas
Definition: svgfig.py:481
TObjArray getHistArray(TFile *histoFile, const std::string &baseName)
Definition: Tools.cc:147
void tdrGrid(const bool &gridOn)
Definition: Tools.cc:434
int iBinClosestInit
Definition: cuy.py:885
double IntegrateArray(const TArrayF &theArray, const TArrayF &theBinWidth)
Definition: Tools.cc:93
double HistoBinWidth(const TH1F *theHisto, const int &iBin)
Definition: Tools.cc:19
iBinClosest
Definition: cuy.py:888
void fixOverlay()
Definition: Tools.cc:443
int yClosestInit
Definition: cuy.py:884
std::string flavour(const int &flav)
Definition: Tools.cc:168
bool flavourIsB(const int &flav)
Definition: Tools.cc:194
tuple minInHisto
Definition: cuy.py:882
bool flavourIsDUSG(const int &flav)
Definition: Tools.cc:198
void HistoToNormalizedArrays(const TH1F *theHisto, TArrayF &theNormalizedArray, TArrayF &theLeftOfBinArray, TArrayF &theBinWidthArray)
Definition: Tools.cc:57
bool flavourIsS(const int &flav)
Definition: Tools.cc:192
bool flavourIsDUS(const int &flav)
Definition: Tools.cc:197
std::string itos(const int &i)
Definition: Tools.cc:448
yClosest
Definition: cuy.py:889
tuple cout
Definition: gather_cfg.py:121
SurfaceDeformation * create(int type, const std::vector< double > &params)