CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10_patch1/src/DQMOffline/RecoB/src/Tools.cc

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 // TOOLS
00014 //
00015 //
00016 
00017 
00019 double RecoBTag::HistoBinWidth ( const TH1F * theHisto , const int& iBin ) {
00020   const int& nBins = theHisto->GetSize() ; // includes underflow/overflow
00021   // return 0.0 , if invalid bin
00022   if ( iBin < 0 || iBin >= nBins ) return 0.0 ;
00023   // return first binwidth, if underflow bin
00024   if ( iBin == 0 ) return theHisto->GetBinWidth ( 1 ) ;
00025   // return last real binwidth, if overflow bin
00026   if ( iBin == nBins - 1 ) return theHisto->GetBinWidth ( nBins - 2 ) ;
00027   // return binwidth from histo, if within range
00028   return theHisto->GetBinWidth ( iBin ) ;
00029 }
00031 
00032 
00034 double RecoBTag::IntegrateHistogram ( const TH1F * theHisto ) {
00035   // include underflow and overflow: assign binwidth of first/last bin to them!!
00036   // integral = sum ( entry_i * binwidth_i )
00037   //
00038   double histoIntegral = 0.0 ;
00039   const int&    nBins = theHisto->GetSize() ;
00040   //
00041   // loop over bins:
00042   // bin 0       : underflow
00043   // bin nBins-1 : overflow
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   // check that all arrays/histo have the same size
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   // to create gif in 'batch mode' (non-interactive) see
00113   // http://root.cern.ch/cgi-bin/print_hit_bold.pl/root/roottalk/roottalk00/0402.html?gifbatch#first_hit
00114   //
00115   // ROOT 4 can do it!!??
00116   //
00117   // if string = "" don't print to corresponding file
00118   //
00119   if ( psFile  != "" ) canvas->Print ( psFile.c_str() ) ;
00120   if ( epsFile != "" ) canvas->Print ( epsFile.c_str() , "eps" ) ;
00121   // if in batch: use a converter tool
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 )  { // to find out if running in batch mode
00126       cout << "--> Print directly gif!" << endl ;
00127       canvas->Print ( gifFile.c_str() , "gif" ) ;
00128     }
00129     else {
00130       if ( epsFile != "" ) {   // eps file must have been created before
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()) ; // delete the intermediate file
00139       }
00140     }
00141   }
00142 }
00144 
00145 
00147 TObjArray RecoBTag::getHistArray ( TFile * histoFile , const std::string& baseName ) {
00148   //
00149   // return the TObjArray built from the basename
00150   //
00151   //
00152   TObjArray histos (3) ;  // reserve 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   // create it if it doesn't exist
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       // check again if it exists now
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   // Find the bin in a 1-dim. histogram which has its y-value closest to
00236   // the given value yVal where the value yVal has to be in the range yLow < yVal < yHigh.
00237   // If it is outside this range the corresponding bin number is returned as negative value.
00238   // Currently, there is no protection if there are many bins with the same value!
00239   // Teh user has to take care to interpret the output correctly.
00240   //
00241 
00242   // init
00243   const int&   nBins = histo->GetNbinsX() - 2 ; // -2 because we don't include under/overflow alos in this loop
00244   int   iBinClosestInit = 0    ;
00245   // init start value properly: must avoid that the real one is not filled
00246   float yClosestInit ;
00247   //
00248   const float& maxInHisto = histo->GetMaximum() ;
00249   const float& minInHisto = histo->GetMinimum() ;
00250   //
00251   // if yVal is smaller than max -> take any value well above the maximum
00252   if ( yVal <= maxInHisto ) {
00253     yClosestInit = maxInHisto + 1 ; }
00254   else {
00255     // if yVal is greater than max value -> take a value < minimum
00256     yClosestInit = minInHisto - 1.0 ;
00257   }
00258 
00259   int   iBinClosest = iBinClosestInit ;
00260   float yClosest    = yClosestInit ;
00261 
00262   // loop over bins of histogram
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   // check if in interval
00272   if ( yClosest < yLow  || yClosest > yHigh ) {
00273     iBinClosest *= -1 ;
00274   }
00275 
00276   // check that not the initialization bin (would mean that init value was the closest)
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 // For the canvas:
00291   tdrStyle->SetCanvasBorderMode(0);
00292   tdrStyle->SetCanvasColor(kWhite);
00293   tdrStyle->SetCanvasDefH(600); //Height of canvas
00294   tdrStyle->SetCanvasDefW(600); //Width of canvas
00295   tdrStyle->SetCanvasDefX(0);   //POsition on screen
00296   tdrStyle->SetCanvasDefY(0);
00297 
00298 // For the Pad:
00299   tdrStyle->SetPadBorderMode(0);
00300   // tdrStyle->SetPadBorderSize(Width_t size = 1);
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 // For the frame:
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 // For the histo:
00318   // tdrStyle->SetHistFillColor(1);
00319   // tdrStyle->SetHistFillStyle(0);
00320   tdrStyle->SetHistLineColor(1);
00321   tdrStyle->SetHistLineStyle(0);
00322   tdrStyle->SetHistLineWidth(1);
00323   // tdrStyle->SetLegoInnerR(Float_t rad = 0.5);
00324   // tdrStyle->SetNumberContours(Int_t number = 20);
00325 
00326   tdrStyle->SetEndErrorSize(15);
00327 //   tdrStyle->SetErrorMarker(20);
00328   tdrStyle->SetErrorX(1);
00329   
00330   tdrStyle->SetMarkerStyle(21);
00331   tdrStyle->SetMarkerSize(1.);
00332 
00333 //For the fit/function:
00334   tdrStyle->SetOptFit(0);
00335   tdrStyle->SetFitFormat("5.4g");
00336   tdrStyle->SetFuncColor(2);
00337   tdrStyle->SetFuncStyle(1);
00338   tdrStyle->SetFuncWidth(1);
00339 
00340 //For the date:
00341   tdrStyle->SetOptDate(0);
00342   // tdrStyle->SetDateX(Float_t x = 0.01);
00343   // tdrStyle->SetDateY(Float_t y = 0.01);
00344 
00345 // For the statistics box:
00346   tdrStyle->SetOptFile(1111);
00347   tdrStyle->SetOptStat(0); // To display the mean and RMS:   SetOptStat("mr");
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   // tdrStyle->SetStatStyle(Style_t style = 1001);
00357   // tdrStyle->SetStatX(Float_t x = 0);
00358   // tdrStyle->SetStatY(Float_t y = 0);
00359 
00360 // Margins:
00361   tdrStyle->SetPadTopMargin(0.05);
00362   tdrStyle->SetPadBottomMargin(0.13);
00363   tdrStyle->SetPadLeftMargin(0.16);
00364   tdrStyle->SetPadRightMargin(0.02);
00365 
00366 // For the Global title:
00367 
00368   tdrStyle->SetOptTitle(0);
00369   tdrStyle->SetTitleW(0.8); // Set the width of the title box
00370 
00371   tdrStyle->SetTitleFont(42);
00372   tdrStyle->SetTitleColor(1);
00373   tdrStyle->SetTitleTextColor(1);
00374   tdrStyle->SetTitleFillColor(10);
00375   tdrStyle->SetTitleFontSize(0.05);
00376   // tdrStyle->SetTitleH(0); // Set the height of the title box
00377   // tdrStyle->SetTitleX(0); // Set the position of the title box
00378   // tdrStyle->SetTitleY(0.985); // Set the position of the title box
00379   // tdrStyle->SetTitleStyle(Style_t style = 1001);
00380   // tdrStyle->SetTitleBorderSize(2);
00381 
00382 // For the axis titles:
00383 
00384   tdrStyle->SetTitleColor(1, "XYZ");
00385   tdrStyle->SetTitleFont(42, "XYZ");
00386   tdrStyle->SetTitleSize(0.06, "XYZ");
00387   // tdrStyle->SetTitleXSize(Float_t size = 0.02); // Another way to set the size?
00388   // tdrStyle->SetTitleYSize(Float_t size = 0.02);
00389   tdrStyle->SetTitleXOffset(0.75);
00390   tdrStyle->SetTitleYOffset(0.75);
00391   // tdrStyle->SetTitleOffset(1.1, "Y"); // Another way to set the Offset
00392 
00393 // For the axis labels:
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 // For the axis:
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);  // To get tick marks on the opposite side of the frame
00407   tdrStyle->SetPadTickY(1);
00408 
00409 // Change for log plots:
00410   tdrStyle->SetOptLogx(0);
00411   tdrStyle->SetOptLogy(0);
00412   tdrStyle->SetOptLogz(0);
00413 
00414 // Postscript options:
00415    tdrStyle->SetPaperSize(21.,28.);
00416 //  tdrStyle->SetPaperSize(20.,20.);
00417   // tdrStyle->SetLineScalePS(Float_t scale = 3);
00418   // tdrStyle->SetLineStyleString(Int_t i, const char* text);
00419   // tdrStyle->SetHeaderPS(const char* header);
00420   // tdrStyle->SetTitlePS(const char* pstitle);
00421 
00422   // tdrStyle->SetBarOffset(Float_t baroff = 0.5);
00423   // tdrStyle->SetBarWidth(Float_t barwidth = 0.5);
00424   // tdrStyle->SetPaintTextFormat(const char* format = "g");
00425   // tdrStyle->SetPalette(Int_t ncolors = 0, Int_t* colors = 0);
00426   // tdrStyle->SetTimeOffset(Double_t toffset);
00427   // tdrStyle->SetHistMinimumZero(kTRUE);
00428 
00429   tdrStyle->cd();
00430   return tdrStyle;
00431 }
00432 // tdrGrid: Turns the grid lines on (true) or off (false)
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 // fixOverlay: Redraws the axis
00442 
00443 void RecoBTag::fixOverlay() {
00444   gPad->RedrawAxis();
00445 }
00446 
00447 
00448 string RecoBTag::itos(const int& i)     // convert int to string
00449 {
00450         ostringstream s;
00451         s << i;
00452         return s.str();
00453 }
00454 
00455 
00456 
00457 
00458 
00459