Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:32:35

0001 // F. Cossutti 
0002 //
0003 // ROOT macro for graphical compariosn of Monitor Elements in a user
0004 // supplied directory between two files with the same histogram content
0005 
0006 #include <iostream.h>
0007 
0008 class HistoCompare {
0009 
0010  public:
0011 
0012   HistoCompare() { std::cout << "Initializing HistoCompare... " << std::endl; } ;
0013 
0014   void PVCompute(TH1 * oldHisto , TH1 * newHisto , TText * te );
0015   void PVCompute(TH2 * oldHisto , TH2 * newHisto , TText * te );
0016   void PVCompute(TProfile * oldHisto , TProfile * newHisto , TText * te );
0017 
0018  private:
0019   
0020   Double_t mypv;
0021 
0022   TH1 * myoldHisto1;
0023   TH1 * mynewHisto1;
0024 
0025   TH2 * myoldHisto2;
0026   TH2 * mynewHisto2;
0027 
0028   TProfile * myoldProfile;
0029   TProfile * mynewProfile;
0030 
0031   TText * myte;
0032 
0033   void printRes(TString theName, Double_t thePV, TText * te);
0034 
0035 };
0036 
0037 HistoCompare::printRes(TString myName, Double_t mypv, TText * myte)
0038 {
0039   std::strstream buf;
0040   std::string value;
0041   buf<<"PV="<<mypv<<std::endl;
0042   buf>>value;
0043   
0044   myte->DrawTextNDC(0.7,0.92, value.c_str());
0045 
0046   std::cout << "[Compatibility test] " << myName << " PV = " << mypv << std::endl;
0047 
0048 }
0049 
0050 
0051 HistoCompare::PVCompute(TH1 * oldHisto , TH1 * newHisto , TText * te )
0052 {
0053 
0054   myoldHisto1 = oldHisto;
0055   mynewHisto1 = newHisto;
0056   myte = te;
0057 
0058   Double_t *res;
0059 
0060   Double_t mypv = myoldHisto1->Chi2Test(mynewHisto1,"WW",res);
0061   TString title = myoldHisto1->GetName();
0062   printRes(title, mypv, myte);
0063   return;
0064 
0065 }
0066 
0067 HistoCompare::PVCompute(TH2 * oldHisto , TH2 * newHisto , TText * te )
0068 {
0069 
0070   myoldHisto2 = oldHisto;
0071   mynewHisto2 = newHisto;
0072   myte = te;
0073 
0074   Double_t *res ;
0075   Double_t mypv = myoldHisto2->Chi2Test(mynewHisto2,"WW",res);
0076   TString title = myoldHisto2->GetName();
0077   printRes(title, mypv, myte);
0078   return;
0079 
0080 }
0081 
0082 
0083 HistoCompare::PVCompute(TProfile * oldHisto , TProfile * newHisto , TText * te )
0084 {
0085 
0086   myoldProfile = oldHisto;
0087   mynewProfile = newHisto;
0088   myte = te;
0089 
0090   Double_t *res ;
0091 
0092   Double_t mypv = myoldProfile->Chi2Test(mynewProfile,"WW",res);
0093   TString title = myoldProfile->GetName();
0094   printRes(title, mypv, myte);
0095   return;
0096 
0097 }
0098 
0099 #include "TObject.h"
0100 #include "TDirectory.h"
0101 #include "TKey.h"
0102 #include "TFile.h"
0103 #include "TTree.h"
0104 #include "TText.h"
0105 
0106 
0107 void MECompare( TString currentfile = "new.root",
0108                 TString referencefile = "ref.root",
0109                 TString theDir = "DQMData/Run 1/Generator/Run summary/MBUEandQCD" )
0110 {
0111   
0112   std::vector<TString> theList =  histoList(currentfile, theDir);
0113   
0114   gROOT ->Reset();
0115   char*  rfilename = referencefile ;
0116   char*  sfilename = currentfile ;
0117   
0118   delete gROOT->GetListOfFiles()->FindObject(rfilename);
0119   delete gROOT->GetListOfFiles()->FindObject(sfilename);
0120   
0121   TFile * rfile = new TFile(rfilename);
0122   TFile * sfile = new TFile(sfilename);
0123   
0124   char* baseDir=theDir;
0125   
0126   rfile->cd(baseDir);
0127   gDirectory->ls();
0128   
0129   sfile->cd(baseDir);
0130   gDirectory->ls();
0131 
0132   for ( unsigned int index = 0; index < theList.size() ; index++ ) {
0133 
0134     std::cout << index << std::endl;
0135 
0136     TString theName = theDir+"/"+theList[index];
0137     std::cout << theName << std::endl;
0138 
0139     TH1* href_;
0140     rfile->GetObject(theName,href_);
0141     href_;
0142     
0143     TH1* hnew_;
0144     sfile->GetObject(theName,hnew_);
0145     hnew_;
0146     
0147     MEComparePlot(href_, hnew_, currentfile, referencefile, theDir, theList[index]); 
0148 
0149   }
0150  
0151 }
0152 
0153 void MEComparePlot(TH1 * href_, TH1 * hnew_, TString currentfile, TString referencefile, TString theDir, TString theHisto )
0154 {
0155 
0156  TString theName = theDir+"/"+theHisto;
0157  std::cout << "Histogram name = " << theName << std::endl;
0158 
0159  HistoCompare * myPV = new HistoCompare();
0160 
0161  int rcolor = 2;
0162  int scolor = 4;
0163  
0164  int rmarker = 21;
0165  int smarker = 20;
0166 
0167  Double_t markerSize = 0.75;
0168  
0169  href_->SetLineColor(rcolor);
0170  href_->SetMarkerStyle(rmarker);
0171  href_->SetMarkerSize(markerSize);
0172  href_->SetMarkerColor(rcolor);
0173 
0174  hnew_->SetLineColor(scolor);
0175  hnew_->SetMarkerStyle(smarker);
0176  hnew_->SetMarkerSize(markerSize);
0177  hnew_->SetMarkerColor(scolor);    
0178 
0179  if ( href_ && hnew_ ) {
0180 
0181  
0182    TCanvas *myPlot = new TCanvas("myPlot","Histogram comparison",200,10,700,900);
0183    TPad *pad1 = new TPad("pad1",
0184                          "The pad with the function",0.03,0.62,0.50,0.92);
0185    TPad *pad2 = new TPad("pad2",
0186                          "The pad with the histogram",0.51,0.62,0.98,0.92);
0187    TPad *pad3 = new TPad("pad3",
0188                          "The pad with the histogram",0.03,0.02,0.97,0.57);
0189    pad1->Draw();
0190    pad2->Draw();
0191    pad3->Draw();
0192 
0193    // Draw a global picture title
0194    TPaveLabel *title = new TPaveLabel(0.1,0.94,0.9,0.98,theName);
0195    title->SetFillColor(16);
0196    title->SetTextFont(52);
0197    title->Draw();
0198 
0199    TText * titte = new TText();
0200 
0201    // Draw reference
0202    pad1->cd();
0203    href_->DrawCopy("e1");
0204    titte->DrawTextNDC(0.,0.02, referencefile);
0205 
0206    // Draw new
0207    pad2->cd();
0208    hnew_->DrawCopy("e1");
0209    titte->DrawTextNDC(0.,0.02, currentfile);
0210    gStyle->SetOptStat("nemruoi");
0211 
0212    // Draw the two overlayed
0213    pad3->cd();
0214    pad3->SetGridx();
0215    pad3->SetGridy();
0216    href_->DrawCopy("e1");
0217    hnew_->DrawCopy("e1same");
0218 
0219    TText* te = new TText();
0220    te->SetTextSize(0.1);
0221    myPV->PVCompute( href_ , hnew_ , te );
0222 
0223    gStyle->SetOptStat(0000000);
0224 
0225  }
0226  TString plotFile = theHisto+".jpg";
0227  myPlot->Print(plotFile); 
0228  
0229  delete myPV;
0230  delete myPlot; 
0231 
0232  }
0233 
0234 std::vector<TString> histoList( TString currentfile, TString theDir )
0235 {
0236 
0237  gROOT ->Reset();
0238  char*  sfilename = currentfile ;
0239 
0240  delete gROOT->GetListOfFiles()->FindObject(sfilename);
0241 
0242  TFile * sfile = new TFile(sfilename);
0243 
0244  char* baseDir=theDir;
0245 
0246  sfile->cd(baseDir);
0247 
0248  TDirectory * d = gDirectory;
0249 
0250  std::vector<TString> theHistList;
0251 
0252  TIter i( d->GetListOfKeys() );
0253  TKey *k;
0254  while( (k = (TKey*)i())) {
0255    TClass * c1 = gROOT->GetClass(k->GetClassName());
0256    if ( !c1->InheritsFrom("TH1")) continue;
0257    theHistList.push_back(k->GetName());
0258  }
0259  
0260  std::cout << "Histograms considered: " << std::endl;
0261  for (unsigned int index = 0; index < theHistList.size() ; index++ ) {
0262    std::cout << index << " " << theHistList[index] << std::endl;
0263  }
0264 
0265  return theHistList;
0266 
0267 }