CMS 3D CMS Logo

Tools.cc
Go to the documentation of this file.
2 
3 #include "TROOT.h"
4 #include "TSystem.h"
5 #include "TStyle.h"
6 #include <cmath>
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  // The 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 
286 vector<int> RecoBTag::findBinClosestYValueAtFixedZ ( const TH2F * histoY , const float& yVal , const float& yLow , const float& yHigh , const TH2F * histoZ , const vector<double>& zVal ) {
287  //
288  // Find the bin in a 2-dim. histogram which has its y-value closest to
289  // the given value yVal where the value yVal has to be in the range yLow < yVal < yHigh.
290  // If it is outside this range the corresponding bin number is returned as negative value.
291  // The bin should also correspond to a value of z=zVal within the same precision as yVal.
292  // Currently, there is no protection if there are many bins with the same value!
293  // The user has to take care to interpret the output correctly.
294  //
295 
296  // init
297  const int& nBinsX = histoY->GetNbinsX() - 2 ; // -2 because we don't include under/overflow alos in this loop
298  const int& nBinsY = histoY->GetNbinsY() - 2 ; // -2 because we don't include under/overflow alos in this loop
299  int iBinClosestInit = 0 ;
300  // init start value properly: must avoid that the real one is not filled
301  vector<float> yClosestInit(zVal.size()) ;
302  //
303  const float& maxInHisto = histoY->GetMaximum() ;
304  const float& minInHisto = histoY->GetMinimum() ;
305  //
306  // if yVal is smaller than max -> take any value well above the maximum
307  for (unsigned int i=0; i<yClosestInit.size(); i++){
308  if ( yVal <= maxInHisto ) {
309  yClosestInit[i] = maxInHisto + 1 ; }
310  else {
311  // if yVal is greater than max value -> take a value < minimum
312  yClosestInit[i] = minInHisto - 1.0 ;
313  }
314  }
315 
316  vector<int> iBinClosest(zVal.size(), iBinClosestInit);
317  vector<float> yClosest(yClosestInit);
318 
319  // loop over bins of histogram
320  for ( int iBinX = 1 ; iBinX <= nBinsX ; ++iBinX ) {
321  for ( int iBinY = 1 ; iBinY <= nBinsY ; ++iBinY ) {
322  const float& yBin = histoY->GetBinContent(iBinX,iBinY);
323  for (unsigned int i = 0; i < zVal.size(); i++){
324  if ( fabs(yBin-yVal) < fabs(yClosest[i]-yVal) ) {
325  const float& zLow = zVal[i] - (yVal - yLow);
326  const float& zHigh = zVal[i] + (yHigh - yVal);
327  const float& zBin = histoZ->GetBinContent(iBinX,iBinY) ;
328  if(zBin < zLow || zBin > zHigh) continue;
329  yClosest[i] = yBin ;
330  iBinClosest[i] = histoY->GetBin(iBinX,iBinY) ;
331  }
332  }
333  }
334  }
335  // check if in interval
336  for (unsigned int i = 0; i < yClosest.size(); i++){
337  if ( yClosest[i] < yLow || yClosest[i] > yHigh )
338  iBinClosest[i] *= -1 ;
339  }
340 
341  return iBinClosest ;
342 }
344 
345 
347  TStyle *tdrStyle = new TStyle("tdrStyle","Style for P-TDR");
348 
349 // For the canvas:
350  tdrStyle->SetCanvasBorderMode(0);
351  tdrStyle->SetCanvasColor(kWhite);
352  tdrStyle->SetCanvasDefH(600); //Height of canvas
353  tdrStyle->SetCanvasDefW(600); //Width of canvas
354  tdrStyle->SetCanvasDefX(0); //POsition on screen
355  tdrStyle->SetCanvasDefY(0);
356 
357 // For the Pad:
358  tdrStyle->SetPadBorderMode(0);
359  // tdrStyle->SetPadBorderSize(Width_t size = 1);
360  tdrStyle->SetPadColor(kWhite);
361  tdrStyle->SetPadGridX(false);
362  tdrStyle->SetPadGridY(false);
363  tdrStyle->SetGridColor(0);
364  tdrStyle->SetGridStyle(3);
365  tdrStyle->SetGridWidth(1);
366 
367 // For the frame:
368  tdrStyle->SetFrameBorderMode(0);
369  tdrStyle->SetFrameBorderSize(1);
370  tdrStyle->SetFrameFillColor(0);
371  tdrStyle->SetFrameFillStyle(0);
372  tdrStyle->SetFrameLineColor(1);
373  tdrStyle->SetFrameLineStyle(1);
374  tdrStyle->SetFrameLineWidth(1);
375 
376 // For the histo:
377  // tdrStyle->SetHistFillColor(1);
378  // tdrStyle->SetHistFillStyle(0);
379  tdrStyle->SetHistLineColor(1);
380  tdrStyle->SetHistLineStyle(0);
381  tdrStyle->SetHistLineWidth(1);
382  // tdrStyle->SetLegoInnerR(Float_t rad = 0.5);
383  // tdrStyle->SetNumberContours(Int_t number = 20);
384 
385  tdrStyle->SetEndErrorSize(15);
386 // tdrStyle->SetErrorMarker(20);
387  tdrStyle->SetErrorX(1);
388 
389  tdrStyle->SetMarkerStyle(21);
390  tdrStyle->SetMarkerSize(1.);
391 
392 //For the fit/function:
393  tdrStyle->SetOptFit(0);
394  tdrStyle->SetFitFormat("5.4g");
395  tdrStyle->SetFuncColor(2);
396  tdrStyle->SetFuncStyle(1);
397  tdrStyle->SetFuncWidth(1);
398 
399 //For the date:
400  tdrStyle->SetOptDate(0);
401  // tdrStyle->SetDateX(Float_t x = 0.01);
402  // tdrStyle->SetDateY(Float_t y = 0.01);
403 
404 // For the statistics box:
405  tdrStyle->SetOptFile(1111);
406  tdrStyle->SetOptStat(0); // To display the mean and RMS: SetOptStat("mr");
407  tdrStyle->SetStatColor(kWhite);
408  tdrStyle->SetStatFont(42);
409  tdrStyle->SetStatFontSize(0.025);
410  tdrStyle->SetStatTextColor(1);
411  tdrStyle->SetStatFormat("6.4g");
412  tdrStyle->SetStatBorderSize(1);
413  tdrStyle->SetStatH(0.2);
414  tdrStyle->SetStatW(0.15);
415  // tdrStyle->SetStatStyle(Style_t style = 1001);
416  // tdrStyle->SetStatX(Float_t x = 0);
417  // tdrStyle->SetStatY(Float_t y = 0);
418 
419 // Margins:
420  tdrStyle->SetPadTopMargin(0.05);
421  tdrStyle->SetPadBottomMargin(0.13);
422  tdrStyle->SetPadLeftMargin(0.16);
423  tdrStyle->SetPadRightMargin(0.02);
424 
425 // For the Global title:
426 
427  tdrStyle->SetOptTitle(0);
428  tdrStyle->SetTitleW(0.8); // Set the width of the title box
429 
430  tdrStyle->SetTitleFont(42);
431  tdrStyle->SetTitleColor(1);
432  tdrStyle->SetTitleTextColor(1);
433  tdrStyle->SetTitleFillColor(10);
434  tdrStyle->SetTitleFontSize(0.05);
435  // tdrStyle->SetTitleH(0); // Set the height of the title box
436  // tdrStyle->SetTitleX(0); // Set the position of the title box
437  // tdrStyle->SetTitleY(0.985); // Set the position of the title box
438  // tdrStyle->SetTitleStyle(Style_t style = 1001);
439  // tdrStyle->SetTitleBorderSize(2);
440 
441 // For the axis titles:
442 
443  tdrStyle->SetTitleColor(1, "XYZ");
444  tdrStyle->SetTitleFont(42, "XYZ");
445  tdrStyle->SetTitleSize(0.06, "XYZ");
446  // tdrStyle->SetTitleXSize(Float_t size = 0.02); // Another way to set the size?
447  // tdrStyle->SetTitleYSize(Float_t size = 0.02);
448  tdrStyle->SetTitleXOffset(0.75);
449  tdrStyle->SetTitleYOffset(0.75);
450  // tdrStyle->SetTitleOffset(1.1, "Y"); // Another way to set the Offset
451 
452 // For the axis labels:
453 
454  tdrStyle->SetLabelColor(1, "XYZ");
455  tdrStyle->SetLabelFont(42, "XYZ");
456  tdrStyle->SetLabelOffset(0.007, "XYZ");
457  tdrStyle->SetLabelSize(0.05, "XYZ");
458 
459 // For the axis:
460 
461  tdrStyle->SetAxisColor(1, "XYZ");
462  tdrStyle->SetStripDecimals(kTRUE);
463  tdrStyle->SetTickLength(0.03, "XYZ");
464  tdrStyle->SetNdivisions(510, "XYZ");
465  tdrStyle->SetPadTickX(1); // To get tick marks on the opposite side of the frame
466  tdrStyle->SetPadTickY(1);
467 
468 // Change for log plots:
469  tdrStyle->SetOptLogx(0);
470  tdrStyle->SetOptLogy(0);
471  tdrStyle->SetOptLogz(0);
472 
473 // Postscript options:
474  tdrStyle->SetPaperSize(21.,28.);
475 // tdrStyle->SetPaperSize(20.,20.);
476  // tdrStyle->SetLineScalePS(Float_t scale = 3);
477  // tdrStyle->SetLineStyleString(Int_t i, const char* text);
478  // tdrStyle->SetHeaderPS(const char* header);
479  // tdrStyle->SetTitlePS(const char* pstitle);
480 
481  // tdrStyle->SetBarOffset(Float_t baroff = 0.5);
482  // tdrStyle->SetBarWidth(Float_t barwidth = 0.5);
483  // tdrStyle->SetPaintTextFormat(const char* format = "g");
484  // tdrStyle->SetPalette(Int_t ncolors = 0, Int_t* colors = 0);
485  // tdrStyle->SetTimeOffset(Double_t toffset);
486  // tdrStyle->SetHistMinimumZero(kTRUE);
487 
488  tdrStyle->cd();
489  return tdrStyle;
490 }
491 // tdrGrid: Turns the grid lines on (true) or off (false)
492 
493 void RecoBTag::tdrGrid(const bool& gridOn) {
494  TStyle *tdrStyle = setTDRStyle();
495  tdrStyle->SetPadGridX(gridOn);
496  tdrStyle->SetPadGridY(gridOn);
497  tdrStyle->cd();
498 }
499 
500 // fixOverlay: Redraws the axis
501 
503  gPad->RedrawAxis();
504 }
505 
506 
507 string RecoBTag::itos(const int& i) // convert int to string
508 {
509  ostringstream s;
510  s << i;
511  return s.str();
512 }
513 
514 
515 
516 
517 
518 
TFile * histoFile
Definition: hcalCalib.cc:43
void PrintCanvasHistos(TCanvas *canvas, const std::string &psFile, const std::string &epsFile, const std::string &gifFile)
Definition: Tools.cc:109
bool flavourIsG(const int &flav)
Definition: Tools.cc:195
def create(alignables, pedeDump, additionalData, outputFile, config)
bool flavourIsNI(const int &flav)
Definition: Tools.cc:200
double IntegrateHistogram(const TH1F *theHisto)
Definition: Tools.cc:34
TStyle * setTDRStyle()
Definition: Tools.cc:346
bool flavourIsD(const int &flav)
Definition: Tools.cc:190
iBinClosestInit
Definition: cuy.py:887
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
iBinClosest
Definition: cuy.py:890
TObjArray getHistArray(TFile *histoFile, const std::string &baseName)
Definition: Tools.cc:147
void tdrGrid(const bool &gridOn)
Definition: Tools.cc:493
yClosestInit
Definition: cuy.py:886
def setTDRStyle()
Definition: plotscripts.py:89
double IntegrateArray(const TArrayF &theArray, const TArrayF &theBinWidth)
Definition: Tools.cc:93
minInHisto
Definition: cuy.py:884
double HistoBinWidth(const TH1F *theHisto, const int &iBin)
Definition: Tools.cc:19
void fixOverlay()
Definition: Tools.cc:502
std::string flavour(const int &flav)
Definition: Tools.cc:168
bool flavourIsB(const int &flav)
Definition: Tools.cc:194
bool flavourIsDUSG(const int &flav)
Definition: Tools.cc:198
Definition: Tools.h:23
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:507
yClosest
Definition: cuy.py:891
def canvas(sub, attr)
Definition: svgfig.py:482
std::vector< int > findBinClosestYValueAtFixedZ(const TH2F *, const float &yVal, const float &yLow, const float &yHigh, const TH2F *, const std::vector< double > &zVal)
Definition: Tools.cc:286
yBin
Definition: cuy.py:893
maxInHisto
Definition: cuy.py:883