Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:20:56

0001 /// saveDQMHistograms.C
0002 /// This is a simple macro to make plots of the histograms
0003 /// produced by the hltExoticaPostProcessor_cfg.py file
0004 /// Author: Thiago R. F. P. Tomei
0005 
0006 #include "TFile.h"
0007 #include "TH1.h"
0008 #include "TString.h"
0009 #include "TCanvas.h"
0010 #include "TStyle.h"
0011 #include "TKey.h"
0012 #include "TList.h"
0013 #include "TSystem.h"
0014 
0015 // *******************************************
0016 // Variables
0017 
0018 TString drawOptions1D("");   // Drawing options for 
0019                              // 1D histograms.
0020 TString drawOptions2D("box");// Drawing options for
0021                              // 2D histograms.
0022 
0023 int lineColor = 1; // 1 = Black, 2 = Red, 3 = Green, 4 = Blue
0024 int lineWidth = 2; // Line width for 1D histograms.
0025 int rebinFactor = 1; // Rebinning factor
0026 
0027 double histoxmin = 0.0;
0028 double histoxmax = 1.2;
0029 double titleSize = 0.03;
0030 double titleOffset = 1.5;
0031 
0032 Bool_t displayStatsBox = 0;  // 0 = false, 1 = true
0033 Bool_t autoLogYaxis    = 0;
0034 Bool_t forceLogYaxis   = 0;
0035 
0036 // End of Variables
0037 // *******************************************
0038 
0039 void saveDQMHistograms(const TString fileName="histos.root", 
0040                TString imageType="pdf",
0041                double outputWidth=600, 
0042                double outputHeight=600)
0043 {
0044   TString outputFolder = fileName;
0045   TFile* fin = new TFile(fileName.Data()) ;
0046 
0047   TCanvas* canvasDefault;
0048   TString outputType = "."+imageType;
0049   TH1* h = 0;
0050 
0051   if (!fin->IsOpen()) {
0052     printf("<E> Cannot open input file %s\n",fileName.Data()) ;
0053     exit(1) ;
0054   }
0055   
0056   outputFolder = fileName+"/"; // Blank to use current directory,
0057                                           // or, for a specific dir type
0058                                           // something like "images/"
0059   outputFolder.ReplaceAll(".root","");
0060   outputFolder.ReplaceAll("__","_"); // Just a bit of cleanup here
0061   gSystem->MakeDirectory(outputFolder);
0062 
0063   canvasDefault = new TCanvas("canvasDefault","testCanvas",outputWidth,outputHeight);
0064   canvasDefault->SetGridx();
0065   canvasDefault->SetGridy();
0066   
0067   // Change settings on plotting histograms
0068   gStyle->SetOptStat(111111);  // This will cause overflow and underflow to be shown
0069   gStyle->SetHistLineWidth(lineWidth);
0070   gStyle->SetHistLineColor(lineColor);
0071   gStyle->SetTitleSize(titleSize,"X");
0072   gStyle->SetTitleSize(titleSize,"Y");
0073   gStyle->SetTitleXOffset(titleOffset);
0074   gStyle->SetTitleYOffset(titleOffset);
0075 
0076   // FIXME - this should also be configurable... right?
0077   fin->cd("DQMData/Run 1/HLT/Run summary/Exotica");
0078   TDirectory* baseDir = gDirectory;
0079   TDirectory* subDir;
0080 
0081   TList* thelist = baseDir->GetListOfKeys() ;
0082   if (!thelist) { printf("<E> No keys found in file\n") ; exit(1) ; }
0083 
0084   TIter next(thelist) ;
0085   TKey* key ;
0086   TObject* obj ;
0087       
0088   while ( (key = (TKey*)next()) ) {
0089     obj = key->ReadObj() ;
0090     printf("%s\n",obj->IsA()->GetName());
0091 
0092     if (strcmp(obj->IsA()->GetName(),"TDirectoryFile")==0) {
0093 
0094       // Okay, it is a directory, let's make a subfolder
0095       // and them loop over it
0096       printf("<W> Found subdirectory %s\n",obj->GetName()) ;
0097       TString path = outputFolder+"/"+obj->GetName();
0098       gSystem->MakeDirectory(outputFolder+"/"+obj->GetName());
0099       subDir = (TDirectory*)obj;
0100       TList* thesublist = subDir->GetListOfKeys() ;
0101       TIter subnext(thesublist) ;
0102       
0103       while ( (key = (TKey*)subnext()) ) {
0104     obj = key->ReadObj();
0105     
0106     if ( (strcmp(obj->IsA()->GetName(),"TProfile")==0)
0107          || (!obj->InheritsFrom("TH2"))
0108          || (!obj->InheritsFrom("TH1")) 
0109          ) {
0110       
0111       // Okay, it is a histogram, let's draw it
0112       printf("Histo name:%s title:%s\n",obj->GetName(),obj->GetTitle());
0113       h = (TH1*)obj;
0114 
0115       // But only if it is an efficiency
0116       TString histName = h->GetName(); 
0117       if(!histName.Contains("Eff")) continue;
0118       
0119       h->SetStats(displayStatsBox);
0120       if(rebinFactor!=1)
0121         h->Rebin(rebinFactor);
0122       
0123       ////////////////////////////////////////////////////////
0124       // A trick to see if we want logscale in y-axis or not
0125       if (autoLogYaxis) {
0126         Double_t testYvalue = h->GetMaximum();
0127         //cout << testYvalue << endl;
0128         
0129         if (testYvalue > 1.0) {
0130           Double_t maxy = log10(testYvalue);
0131           Double_t miny = log10(h->GetMinimum(1.0));
0132         
0133           // log scale if more than 2 powers of 10 between low and high bins
0134           if ( (maxy-miny) > 2.0 ) {
0135         canvasDefault->SetLogy(1);
0136           }
0137         }
0138       }
0139       
0140       // or, alternatively, do it unconditionally.
0141       if (forceLogYaxis) {
0142         canvasDefault->SetLogy(1);
0143       } 
0144       // End of log or no-log y axis decision
0145       ////////////////////////////////////////////////////////
0146 
0147       h->Draw(drawOptions1D);
0148       h->GetYaxis()->SetRangeUser(histoxmin, histoxmax);
0149       canvasDefault->Modified();
0150       canvasDefault->Update();
0151       
0152       canvasDefault->Print(path+"/"+histName+outputType);
0153       canvasDefault->SetLogy(0); // reset to no-log - prevents errors
0154 
0155     } // Closes "if is a histogram" conditional
0156       } // Closes loop in keys of subdir
0157       
0158     } // Closes "if is a subdir" conditional
0159   } // Closes loop in base dir
0160   
0161   fin->Close();
0162 }