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