Go to the documentation of this file.00001 #include "DQMOffline/RecoB/interface/Tools.h"
00002
00003 #include "TROOT.h"
00004 #include "TSystem.h"
00005 #include "TStyle.h"
00006 #include <math.h>
00007
00008 using namespace std;
00009 using namespace RecoBTag;
00010
00011
00012
00013
00014
00015
00016
00017
00019 double RecoBTag::HistoBinWidth ( const TH1F * theHisto , const int& iBin ) {
00020 const int& nBins = theHisto->GetSize() ;
00021
00022 if ( iBin < 0 || iBin >= nBins ) return 0.0 ;
00023
00024 if ( iBin == 0 ) return theHisto->GetBinWidth ( 1 ) ;
00025
00026 if ( iBin == nBins - 1 ) return theHisto->GetBinWidth ( nBins - 2 ) ;
00027
00028 return theHisto->GetBinWidth ( iBin ) ;
00029 }
00031
00032
00034 double RecoBTag::IntegrateHistogram ( const TH1F * theHisto ) {
00035
00036
00037
00038 double histoIntegral = 0.0 ;
00039 const int& nBins = theHisto->GetSize() ;
00040
00041
00042
00043
00044 for ( int iBin = 0 ; iBin != nBins ; ++iBin ) {
00045 const double& binWidth = HistoBinWidth ( theHisto , iBin ) ;
00046 histoIntegral += (*theHisto)[iBin] * binWidth ;
00047 }
00048
00049 return histoIntegral ;
00050 }
00052
00053
00054
00055
00057 void RecoBTag::HistoToNormalizedArrays ( const TH1F * theHisto , TArrayF & theNormalizedArray , TArrayF & theLeftOfBinArray , TArrayF & theBinWidthArray ) {
00058
00059 const int& nBins = theHisto->GetSize() ;
00060
00061
00062 if ( nBins == theNormalizedArray.GetSize() &&
00063 nBins == theLeftOfBinArray .GetSize() &&
00064 nBins == theBinWidthArray .GetSize() ) {
00065
00066 const double& histoIntegral = IntegrateHistogram ( theHisto ) ;
00067
00068 for ( int iBin = 0 ; iBin != nBins ; ++iBin ) {
00069 theNormalizedArray[iBin] = (*theHisto)[iBin] / histoIntegral ;
00070 theLeftOfBinArray [iBin] = theHisto->GetBinLowEdge(iBin) ;
00071 theBinWidthArray [iBin] = HistoBinWidth ( theHisto , iBin ) ;
00072 }
00073
00074 }
00075 else {
00076 cout << "============>>>>>>>>>>>>>>>>" << endl
00077 << "============>>>>>>>>>>>>>>>>" << endl
00078 << "============>>>>>>>>>>>>>>>>" << endl
00079 << "============>>>>>>>>>>>>>>>>" << endl
00080 << "============>>>>>>>>>>>>>>>> HistoToNormalizedArrays failed: not equal sizes of all arrays!!" << endl
00081 << "============>>>>>>>>>>>>>>>>" << endl
00082 << "============>>>>>>>>>>>>>>>>" << endl
00083 << "============>>>>>>>>>>>>>>>>" << endl
00084 << "============>>>>>>>>>>>>>>>>" << endl ;
00085 }
00086
00087 }
00089
00090
00091
00093 double RecoBTag::IntegrateArray ( const TArrayF & theArray , const TArrayF & theBinWidth ) {
00094
00095 double arrayIntegral = 0.0 ;
00096 const int& nBins = theArray.GetSize() ;
00097
00098 for ( int iBin = 0 ; iBin != nBins ; ++iBin ) {
00099 arrayIntegral += theArray[iBin] * theBinWidth[iBin] ;
00100 }
00101
00102 return arrayIntegral ;
00103 }
00105
00106
00107
00109 void RecoBTag::PrintCanvasHistos ( TCanvas * canvas , const std::string& psFile , const std::string& epsFile , const std::string& gifFile ) {
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119 if ( psFile != "" ) canvas->Print ( psFile.c_str() ) ;
00120 if ( epsFile != "" ) canvas->Print ( epsFile.c_str() , "eps" ) ;
00121
00122 const std::string& rootVersion ( gROOT->GetVersion() ) ;
00123 const bool& rootCanGif = rootVersion.find("4") == 0 || rootVersion.find("5") == 0 ;
00124 if ( gifFile != "" ) {
00125 if ( !(gROOT->IsBatch()) || rootCanGif ) {
00126 cout << "--> Print directly gif!" << endl ;
00127 canvas->Print ( gifFile.c_str() , "gif" ) ;
00128 }
00129 else {
00130 if ( epsFile != "" ) {
00131 cout << "--> Print gif via scripts!" << endl ;
00132 const std::string& executeString1 = "pstopnm -ppm -xborder 0 -yborder 0 -portrait " + epsFile ;
00133 gSystem->Exec(executeString1.c_str()) ;
00134 const std::string& ppmFile = epsFile + "001.ppm" ;
00135 const std::string& executeString2 = "ppmtogif " + ppmFile + " > " + gifFile ;
00136 gSystem->Exec(executeString2.c_str()) ;
00137 const std::string& executeString3 = "rm " + ppmFile ;
00138 gSystem->Exec(executeString3.c_str()) ;
00139 }
00140 }
00141 }
00142 }
00144
00145
00147 TObjArray RecoBTag::getHistArray ( TFile * histoFile , const std::string& baseName ) {
00148
00149
00150
00151
00152 TObjArray histos (3) ;
00153
00154 const std::string nameB ( baseName + "B" ) ;
00155 const std::string nameC ( baseName + "C" ) ;
00156 const std::string nameDUSG ( baseName + "DUSG" ) ;
00157
00158 histos.Add ( (TH1F*)histoFile->Get( nameB.c_str() ) ) ;
00159 histos.Add ( (TH1F*)histoFile->Get( nameC.c_str() ) ) ;
00160 histos.Add ( (TH1F*)histoFile->Get( nameDUSG.c_str() ) ) ;
00161
00162 return histos ;
00163 }
00165
00166
00168 std::string RecoBTag::flavour ( const int& flav ) {
00169 switch(flav) {
00170 case 1 :
00171 return "d";
00172 case 2 :
00173 return "u";
00174 case 3 :
00175 return "s";
00176 case 4 :
00177 return "c";
00178 case 5 :
00179 return "b";
00180 case 21 :
00181 return "g";
00182 default :
00183 return "";
00184 }
00185 }
00187
00188
00190 bool RecoBTag::flavourIsD ( const int & flav ) { return flav == 1 ; }
00191 bool RecoBTag::flavourIsU ( const int & flav ) { return flav == 2 ; }
00192 bool RecoBTag::flavourIsS ( const int & flav ) { return flav == 3 ; }
00193 bool RecoBTag::flavourIsC ( const int & flav ) { return flav == 4 ; }
00194 bool RecoBTag::flavourIsB ( const int & flav ) { return flav == 5 ; }
00195 bool RecoBTag::flavourIsG ( const int & flav ) { return flav == 21 ; }
00196
00197 bool RecoBTag::flavourIsDUS ( const int & flav ) { return ( flavourIsD(flav) || flavourIsU(flav) || flavourIsS(flav) ) ; }
00198 bool RecoBTag::flavourIsDUSG ( const int & flav ) { return ( flavourIsDUS(flav) || flavourIsG(flav) ) ; }
00199
00200 bool RecoBTag::flavourIsNI ( const int & flav ) { return !( flavourIsD(flav) || flavourIsU(flav) || flavourIsS(flav) ||
00201 flavourIsC(flav) || flavourIsB(flav) || flavourIsG(flav) ) ; }
00203
00205 int RecoBTag::checkCreateDirectory ( const std::string& directory ) {
00206 cout << "====>>>> ToolsC:checkCreateDirectory() : " << endl ;
00207 int exists = gSystem->Exec ( ("ls -d " + directory).c_str() ) ;
00208
00209 if ( exists != 0 ) {
00210 cout << "====>>>> ToolsC:checkCreateDirectory() : The directory does not exist : " << directory << endl ;
00211 cout << "====>>>> ToolsC:checkCreateDirectory() : I'll try to create it" << endl ;
00212 const int& create = gSystem->Exec ( ("mkdir " + directory).c_str() ) ;
00213 if ( create != 0 ) {
00214 cout << "====>>>> ToolsC:checkCreateDirectory() : Creation of directory failed : " << directory << endl
00215 << "====>>>> ToolsC:checkCreateDirectory() : Please check your write permissions!" << endl ;
00216 }
00217 else {
00218 cout << "====>>>> ToolsC:checkCreateDirectory() : Creation of directory successful!" << endl ;
00219
00220 cout << "====>>>> ToolsC:checkCreateDirectory() : " << endl ;
00221 exists = gSystem->Exec ( ("ls -d " + directory).c_str() ) ;
00222 if ( exists != 0 ) cout << "ToolsC:checkCreateDirectory() : However, it still doesn't exist!?" << endl ;
00223 }
00224 }
00225 cout << endl ;
00226 return exists ;
00227 }
00229
00230
00231
00233 int RecoBTag::findBinClosestYValue ( const TH1F * histo , const float& yVal , const float& yLow , const float& yHigh ) {
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243 const int& nBins = histo->GetNbinsX() - 2 ;
00244 int iBinClosestInit = 0 ;
00245
00246 float yClosestInit ;
00247
00248 const float& maxInHisto = histo->GetMaximum() ;
00249 const float& minInHisto = histo->GetMinimum() ;
00250
00251
00252 if ( yVal <= maxInHisto ) {
00253 yClosestInit = maxInHisto + 1 ; }
00254 else {
00255
00256 yClosestInit = minInHisto - 1.0 ;
00257 }
00258
00259 int iBinClosest = iBinClosestInit ;
00260 float yClosest = yClosestInit ;
00261
00262
00263 for ( int iBin = 1 ; iBin <= nBins ; ++iBin ) {
00264 const float& yBin = histo->GetBinContent(iBin) ;
00265 if ( fabs(yBin-yVal) < fabs(yClosest-yVal) ) {
00266 yClosest = yBin ;
00267 iBinClosest = iBin ;
00268 }
00269 }
00270
00271
00272 if ( yClosest < yLow || yClosest > yHigh ) {
00273 iBinClosest *= -1 ;
00274 }
00275
00276
00277 if ( iBinClosest == iBinClosestInit ) {
00278 cout << "====>>>> ToolsC=>findBinClosestYValue() : WARNING: returned bin is the initialization bin!!" << endl ;
00279 }
00280
00281 return iBinClosest ;
00282 }
00284
00285
00286
00287 TStyle* RecoBTag::setTDRStyle() {
00288 TStyle *tdrStyle = new TStyle("tdrStyle","Style for P-TDR");
00289
00290
00291 tdrStyle->SetCanvasBorderMode(0);
00292 tdrStyle->SetCanvasColor(kWhite);
00293 tdrStyle->SetCanvasDefH(600);
00294 tdrStyle->SetCanvasDefW(600);
00295 tdrStyle->SetCanvasDefX(0);
00296 tdrStyle->SetCanvasDefY(0);
00297
00298
00299 tdrStyle->SetPadBorderMode(0);
00300
00301 tdrStyle->SetPadColor(kWhite);
00302 tdrStyle->SetPadGridX(false);
00303 tdrStyle->SetPadGridY(false);
00304 tdrStyle->SetGridColor(0);
00305 tdrStyle->SetGridStyle(3);
00306 tdrStyle->SetGridWidth(1);
00307
00308
00309 tdrStyle->SetFrameBorderMode(0);
00310 tdrStyle->SetFrameBorderSize(1);
00311 tdrStyle->SetFrameFillColor(0);
00312 tdrStyle->SetFrameFillStyle(0);
00313 tdrStyle->SetFrameLineColor(1);
00314 tdrStyle->SetFrameLineStyle(1);
00315 tdrStyle->SetFrameLineWidth(1);
00316
00317
00318
00319
00320 tdrStyle->SetHistLineColor(1);
00321 tdrStyle->SetHistLineStyle(0);
00322 tdrStyle->SetHistLineWidth(1);
00323
00324
00325
00326 tdrStyle->SetEndErrorSize(15);
00327
00328 tdrStyle->SetErrorX(1);
00329
00330 tdrStyle->SetMarkerStyle(21);
00331 tdrStyle->SetMarkerSize(1.);
00332
00333
00334 tdrStyle->SetOptFit(0);
00335 tdrStyle->SetFitFormat("5.4g");
00336 tdrStyle->SetFuncColor(2);
00337 tdrStyle->SetFuncStyle(1);
00338 tdrStyle->SetFuncWidth(1);
00339
00340
00341 tdrStyle->SetOptDate(0);
00342
00343
00344
00345
00346 tdrStyle->SetOptFile(1111);
00347 tdrStyle->SetOptStat(0);
00348 tdrStyle->SetStatColor(kWhite);
00349 tdrStyle->SetStatFont(42);
00350 tdrStyle->SetStatFontSize(0.025);
00351 tdrStyle->SetStatTextColor(1);
00352 tdrStyle->SetStatFormat("6.4g");
00353 tdrStyle->SetStatBorderSize(1);
00354 tdrStyle->SetStatH(0.2);
00355 tdrStyle->SetStatW(0.15);
00356
00357
00358
00359
00360
00361 tdrStyle->SetPadTopMargin(0.05);
00362 tdrStyle->SetPadBottomMargin(0.13);
00363 tdrStyle->SetPadLeftMargin(0.16);
00364 tdrStyle->SetPadRightMargin(0.02);
00365
00366
00367
00368 tdrStyle->SetOptTitle(0);
00369 tdrStyle->SetTitleW(0.8);
00370
00371 tdrStyle->SetTitleFont(42);
00372 tdrStyle->SetTitleColor(1);
00373 tdrStyle->SetTitleTextColor(1);
00374 tdrStyle->SetTitleFillColor(10);
00375 tdrStyle->SetTitleFontSize(0.05);
00376
00377
00378
00379
00380
00381
00382
00383
00384 tdrStyle->SetTitleColor(1, "XYZ");
00385 tdrStyle->SetTitleFont(42, "XYZ");
00386 tdrStyle->SetTitleSize(0.06, "XYZ");
00387
00388
00389 tdrStyle->SetTitleXOffset(0.75);
00390 tdrStyle->SetTitleYOffset(0.75);
00391
00392
00393
00394
00395 tdrStyle->SetLabelColor(1, "XYZ");
00396 tdrStyle->SetLabelFont(42, "XYZ");
00397 tdrStyle->SetLabelOffset(0.007, "XYZ");
00398 tdrStyle->SetLabelSize(0.05, "XYZ");
00399
00400
00401
00402 tdrStyle->SetAxisColor(1, "XYZ");
00403 tdrStyle->SetStripDecimals(kTRUE);
00404 tdrStyle->SetTickLength(0.03, "XYZ");
00405 tdrStyle->SetNdivisions(510, "XYZ");
00406 tdrStyle->SetPadTickX(1);
00407 tdrStyle->SetPadTickY(1);
00408
00409
00410 tdrStyle->SetOptLogx(0);
00411 tdrStyle->SetOptLogy(0);
00412 tdrStyle->SetOptLogz(0);
00413
00414
00415 tdrStyle->SetPaperSize(21.,28.);
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429 tdrStyle->cd();
00430 return tdrStyle;
00431 }
00432
00433
00434 void RecoBTag::tdrGrid(const bool& gridOn) {
00435 TStyle *tdrStyle = setTDRStyle();
00436 tdrStyle->SetPadGridX(gridOn);
00437 tdrStyle->SetPadGridY(gridOn);
00438 tdrStyle->cd();
00439 }
00440
00441
00442
00443 void RecoBTag::fixOverlay() {
00444 gPad->RedrawAxis();
00445 }
00446
00447
00448 string RecoBTag::itos(const int& i)
00449 {
00450 ostringstream s;
00451 s << i;
00452 return s.str();
00453 }
00454
00455
00456
00457
00458
00459