From 60c921b95d0e9648e8ff498852c62a4c1573fbc9 Mon Sep 17 00:00:00 2001 From: orduna Date: Thu, 10 Dec 2015 10:05:28 -0600 Subject: [PATCH 1/2] Little modifications to remove all warnings while loading on a ROOT session --- CMSDAS2016/GenExercise/macros/MECompare.C | 568 ++++++++++------------ 1 file changed, 248 insertions(+), 320 deletions(-) diff --git a/CMSDAS2016/GenExercise/macros/MECompare.C b/CMSDAS2016/GenExercise/macros/MECompare.C index d1a3629..7afe9e0 100644 --- a/CMSDAS2016/GenExercise/macros/MECompare.C +++ b/CMSDAS2016/GenExercise/macros/MECompare.C @@ -1,367 +1,295 @@ -// F. Cossutti +// F. Cossutti // $Date: 2012/01/10 15:51:23 $ // $Revision: 1.3 $ // +// J. Orduna: This version should not give any warnings when .L MECompare.C+ +// inside a ROOT session. 2015/12/10 09:55:00 +// +// // ROOT macro for graphical compariosn of Monitor Elements in a user // supplied directory between two files with the same histogram content +#include +#include -#include "TObject.h" -#include "TDirectory.h" -#include "TKey.h" -#include "TFile.h" -#include "TTree.h" +#include "TH1.h" +#include "TH2.h" #include "TText.h" +#include "TProfile.h" +#include "TCanvas.h" +#include "TPad.h" +#include "TPaveLabel.h" #include "TStyle.h" -#include -#include -#include -#include #include "TROOT.h" -//#include -#include "TH1F.h" -#include "TH2F.h" -#include "TProfile.h" +#include "TClass.h" #include "TFile.h" -#include "TSystem.h" -#include "TLegend.h" -#include "TText.h" -#include "TPaveText.h" -#include "TPaveLabel.h" -#include "TCanvas.h" #include "TKey.h" -#include -//#include -#include - - class HistoCompare { - - public: - - HistoCompare() : weightOption("UU") { std::cout << "Initializing HistoCompare... " << std::endl; } ; - - void PVCompute(TH1 * oldHisto , TH1 * newHisto , TText * te ); - void PVCompute(TH2 * oldHisto , TH2 * newHisto , TText * te ); - void PVCompute(TProfile * oldHisto , TProfile * newHisto , TText * te ); - - private: - - Double_t mypv; - - TH1 * myoldHisto1; - TH1 * mynewHisto1; - - TH2 * myoldHisto2; - TH2 * mynewHisto2; - - TProfile * myoldProfile; - TProfile * mynewProfile; - - TText * myte; - std::string weightOption; - - void printRes(TString theName, Double_t thePV, TText * te); - std::vector histoList( TString currentfile, TString theDir ); - void MEComparePlot(TH1 * href_, TH1 * hnew_, TString currentfile, TString referencefile, TString theDir, TString theHisto); - + +public: + + HistoCompare() : weightOption("UU") { std::cout << "Initializing HistoCompare... " << std::endl; } ; + + void PVCompute(TH1 * oldHisto , TH1 * newHisto , TText * te ); + void PVCompute(TH2 * oldHisto , TH2 * newHisto , TText * te ); + void PVCompute(TProfile * oldHisto , TProfile * newHisto , TText * te ); + +private: + + Double_t mypv; + + TH1 * myoldHisto1; + TH1 * mynewHisto1; + + TH2 * myoldHisto2; + TH2 * mynewHisto2; + + TProfile * myoldProfile; + TProfile * mynewProfile; + + TText * myte; + std::string weightOption; + + void printRes(TString theName, Double_t thePV, TText * te); + std::vector histoList( TString currentfile, TString theDir ); + void MEComparePlot(TH1 * href_, TH1 * hnew_, TString currentfile, TString referencefile, TString theDir, TString theHisto); + }; -void HistoCompare::printRes(TString myName, Double_t mypv, TText * myte) +void HistoCompare::printRes(TString myName, Double_t mypv1, TText * myte1) { -// std::strstream buf; -// strstream buf; -// iostream buf; - std::stringstream buf; - std::string value; - buf<<"PV="<>value; - - myte->DrawTextNDC(0.3,0.82, value.c_str()); - - std::cout << "[Compatibility test] " << myName << " PV = " << mypv << std::endl; - + std::stringstream buf; + std::string value; + buf << "PV = " << mypv1 << std::endl; + buf >> value; + + myte->DrawTextNDC(0.3,0.82, value.c_str()); + + std::cout << "[Compatibility test] " << myName << " PV = " << mypv1 << std::endl; + myte1->Print(); } - void HistoCompare::PVCompute(TH1 * oldHisto , TH1 * newHisto , TText * te ) { - - myoldHisto1 = oldHisto; - mynewHisto1 = newHisto; - myte = te; - - Double_t *res; - - Double_t mypv = myoldHisto1->Chi2Test(mynewHisto1,weightOption.c_str(),res); - TString title = myoldHisto1->GetName(); - printRes(title, mypv, myte); - return; - + myoldHisto1 = oldHisto; + mynewHisto1 = newHisto; + myte = te; + + Double_t *res = 0; + + Double_t mypv1 = myoldHisto1->Chi2Test(mynewHisto1, weightOption.c_str(), res); + TString title = myoldHisto1->GetName(); + printRes(title, mypv1, myte); + return; } void HistoCompare::PVCompute(TH2 * oldHisto , TH2 * newHisto , TText * te ) { - - myoldHisto2 = oldHisto; - mynewHisto2 = newHisto; - myte = te; - - Double_t *res ; - Double_t mypv = myoldHisto2->Chi2Test(mynewHisto2,weightOption.c_str(),res); - TString title = myoldHisto2->GetName(); - printRes(title, mypv, myte); - return; - + myoldHisto2 = oldHisto; + mynewHisto2 = newHisto; + myte = te; + + Double_t *res = NULL; + Double_t mypv1 = myoldHisto2->Chi2Test(mynewHisto2, weightOption.c_str(), res); + TString title = myoldHisto2->GetName(); + printRes(title, mypv1, myte); + return; } void HistoCompare::PVCompute(TProfile * oldHisto , TProfile * newHisto , TText * te ) { - - myoldProfile = oldHisto; - mynewProfile = newHisto; - myte = te; - - Double_t *res ; - - Double_t mypv = myoldProfile->Chi2Test(mynewProfile,weightOption.c_str(),res); - TString title = myoldProfile->GetName(); - printRes(title, mypv, myte); - return; - + myoldProfile = oldHisto; + mynewProfile = newHisto; + myte = te; + + Double_t *res = NULL; + + Double_t mypv1 = myoldProfile->Chi2Test(mynewProfile, weightOption.c_str(), res); + TString title = myoldProfile->GetName(); + printRes(title, mypv1, myte); + return; } void MEComparePlot(TH1 * href_, TH1 * hnew_, TString currentfile, TString referencefile, TString theDir, TString tHisto) { - std::string theHisto(tHisto); - bool logScale = false; - std::string logScaleIdentifier = "_logscale"; - std::string poundSign = "#"; - std::string underLine = "_"; - std::string emptyLine = ""; - if(theHisto.find(logScaleIdentifier,0) != std::string::npos) - logScale=true; -// remove [] from names - int n1 = theHisto.find('[',0); - if( n1 != std::string::npos ) theHisto.replace(n1,1,underLine); - n1 = theHisto.find(']',0); - if( n1 != std::string::npos ) theHisto.replace(n1,1,underLine); - n1 = theHisto.find('#',0); - if( n1 != std::string::npos ) theHisto.replace(n1,1,emptyLine); - n1 = theHisto.find(' ',0); - if( n1 != std::string::npos ) theHisto.replace(n1,1,emptyLine); - - - TString theName = theDir+"/"+theHisto; - std::cout << "Log Scale = " << logScale << std::endl; - std::cout << "Histogram name = " << theName << std::endl; - - HistoCompare * myPV = new HistoCompare(); - - int rcolor = 2; - int scolor = 4; - - int rmarker = 21; - int smarker = 20; - - Double_t markerSize = 0.75; - - href_->SetLineColor(rcolor); - href_->SetMarkerStyle(rmarker); - href_->SetMarkerSize(markerSize); - href_->SetMarkerColor(rcolor); - - hnew_->SetLineColor(scolor); - hnew_->SetMarkerStyle(smarker); - hnew_->SetMarkerSize(markerSize); - hnew_->SetMarkerColor(scolor); - - - if ( href_ && hnew_ ) { - -// TCanvas *myPlot = new TCanvas("myPlot","Histogram comparison",200,10,700,900); - TCanvas *myPlot = new TCanvas("myPlot","Histogram comparison",200,10,700,900); -// myPlot -> SetLogy(logScale); -// myPlot -> Update(); - TPad *pad1 = new TPad("pad1", - "The pad with the function",0.03,0.62,0.50,0.92); - TPad *pad2 = new TPad("pad2", - "The pad with the histogram",0.51,0.62,0.98,0.92); - TPad *pad3 = new TPad("pad3", - "The pad with the histogram",0.03,0.02,0.97,0.57); - pad1->Draw(); - pad2->Draw(); - pad3->Draw(); - pad1-> SetLogy(logScale); - pad1-> Update(); - pad2-> SetLogy(logScale); - pad2-> Update(); - pad3-> SetLogy(logScale); - pad3-> Update(); - - // Draw a global picture title - TPaveLabel *title = new TPaveLabel(0.1,0.94,0.9,0.98,theName); - title->SetFillColor(16); - title->SetTextFont(52); - title->Draw(); - - TText * titte = new TText(); - - // Draw reference - pad1->cd(); - href_->DrawCopy("e1"); - titte->DrawTextNDC(0.,0.02, referencefile); - - // Draw new - pad2->cd(); - hnew_->DrawCopy("e1"); - titte->DrawTextNDC(0.,0.02, currentfile); - gStyle->SetOptStat("nemruoi"); - - // Draw the two overlayed - pad3->cd(); - pad3->SetGridx(); - pad3->SetGridy(); - href_->DrawCopy("e1"); - hnew_->DrawCopy("e1same"); - - TText* te = new TText(); - te->SetTextSize(0.1); - myPV->PVCompute( href_ , hnew_ , te ); - - gStyle->SetOptStat(0000000); - - - TString plotFile = theHisto+".ps"; - std::cout << "plotFile name = " << plotFile << std::endl; -// myPlot->Print(plotFile); -// myPlot -> SetLogy(logScale); -// myPlot -> Update(); - myPlot->SaveAs(plotFile); - -// myPlot->Print("file.ps"); - - delete myPV; - delete myPlot; - } + std::string theHisto(tHisto); + bool logScale = false; + std::string logScaleIdentifier = "_logscale"; + std::string poundSign = "#"; + std::string underLine = "_"; + std::string emptyLine = ""; + if(theHisto.find(logScaleIdentifier,0) != std::string::npos) + logScale=true; + // remove [] from names + unsigned long int n1 = theHisto.find('[',0); + if( n1 != std::string::npos ) theHisto.replace(n1, 1, underLine); + n1 = theHisto.find(']',0); + if( n1 != std::string::npos ) theHisto.replace(n1, 1, underLine); + n1 = theHisto.find('#',0); + if( n1 != std::string::npos ) theHisto.replace(n1, 1, emptyLine); + n1 = theHisto.find(' ',0); + if( n1 != std::string::npos ) theHisto.replace(n1, 1, emptyLine); + + + TString theName = theDir+"/"+theHisto; + std::cout << "Log Scale = " << logScale << std::endl; + std::cout << "Histogram name = " << theName << std::endl; + + HistoCompare * myPV = new HistoCompare(); + + int rcolor = 2; + int scolor = 4; + + int rmarker = 21; + int smarker = 20; + + Double_t markerSize = 0.75; + + href_->SetLineColor(rcolor); + href_->SetMarkerStyle(rmarker); + href_->SetMarkerSize(markerSize); + href_->SetMarkerColor(rcolor); + + hnew_->SetLineColor(scolor); + hnew_->SetMarkerStyle(smarker); + hnew_->SetMarkerSize(markerSize); + hnew_->SetMarkerColor(scolor); + + if ( href_ && hnew_ ) { + // TCanvas *myPlot = new TCanvas("myPlot","Histogram comparison",200,10,700,900); + TCanvas *myPlot = new TCanvas("myPlot", "Histogram comparison", 200, 10, 700, 900); + // myPlot -> SetLogy(logScale); + // myPlot -> Update(); + TPad *pad1 = new TPad("pad1", "The pad with the function", 0.03, 0.62, 0.50, 0.92); + TPad *pad2 = new TPad("pad2", "The pad with the histogram", 0.51, 0.62, 0.98, 0.92); + TPad *pad3 = new TPad("pad3", "The pad with the histogram", 0.03, 0.02, 0.97, 0.57); + pad1->Draw(); + pad2->Draw(); + pad3->Draw(); + pad1-> SetLogy(logScale); + pad1-> Update(); + pad2-> SetLogy(logScale); + pad2-> Update(); + pad3-> SetLogy(logScale); + pad3-> Update(); + + // Draw a global picture title + TPaveLabel *title = new TPaveLabel(0.1, 0.94, 0.9, 0.98, theName); + title->SetFillColor(16); + title->SetTextFont(52); + title->Draw(); + + TText * titte = new TText(); + + // Draw reference + pad1->cd(); + href_->DrawCopy("e1"); + titte->DrawTextNDC(0., 0.02, referencefile); + + // Draw new + pad2->cd(); + hnew_->DrawCopy("e1"); + titte->DrawTextNDC(0., 0.02, currentfile); + gStyle->SetOptStat("nemruoi"); + + // Draw the two overlayed + pad3->cd(); + pad3->SetGridx(); + pad3->SetGridy(); + href_->DrawCopy("e1"); + hnew_->DrawCopy("e1same"); + + TText* te = new TText(); + te->SetTextSize(0.1); + myPV->PVCompute( href_ , hnew_ , te ); + + gStyle->SetOptStat(0000000); + + TString plotFile = theHisto+".ps"; + std::cout << "plotFile name = " << plotFile << std::endl; + myPlot->SaveAs(plotFile); + + delete myPV; + delete myPlot; + } +} - } std::vector histoList( TString currentfile, TString theDir ) { - - gROOT ->Reset(); -// char* sfilename = currentfile ; - std::string sfilename = (std::string) currentfile; - -// delete gROOT->GetListOfFiles()->FindObject(sfilename); - delete gROOT->GetListOfFiles()->FindObject(sfilename.c_str()); - -// TFile * sfile = new TFile(sfilename); - TFile * sfile = new TFile(sfilename.c_str()); - -// char* baseDir=theDir; - -// sfile->cd(baseDir); - std::string baseDir = (std::string) theDir; - - sfile->cd(baseDir.c_str()); - - TDirectory * d = gDirectory; - - std::vector theHistList; - - TIter i( d->GetListOfKeys() ); - TKey *k; - while( (k = (TKey*)i())) { - TClass * c1 = gROOT->GetClass(k->GetClassName()); - if ( !c1->InheritsFrom("TH1")) continue; - theHistList.push_back(k->GetName()); - } - - std::cout << "Histograms considered: " << std::endl; - for (unsigned int index = 0; index < theHistList.size() ; index++ ) { - std::cout << index << " " << theHistList[index] << std::endl; - } - - return theHistList; - + gROOT->Reset(); + std::string sfilename = (std::string) currentfile; + + delete gROOT->GetListOfFiles()->FindObject(sfilename.c_str()); + + TFile * sfile = new TFile(sfilename.c_str()); + + std::string baseDir = (std::string) theDir; + + sfile->cd(baseDir.c_str()); + + TDirectory * d = gDirectory; + + std::vector theHistList; + + TIter i( d->GetListOfKeys() ); + TKey *k; + while( (k = (TKey*)i())) { + TClass * c1 = gROOT->GetClass(k->GetClassName()); + if ( !c1->InheritsFrom("TH1")) continue; + theHistList.push_back(k->GetName()); + } + + std::cout << "Histograms considered: " << std::endl; + for (unsigned int index = 0; index < theHistList.size() ; index++ ) { + std::cout << index << " " << theHistList[index] << std::endl; + } + + return theHistList; + } void MECompare( TString currentfile = "new.root", - TString referencefile = "ref.root", - TString theDir = "DQMData/Run 1/Generator/Run summary/MBUEandQCD" ) + TString referencefile = "ref.root", + TString theDir = "DQMData/Run 1/Generator/Run summary/MBUEandQCD" ) { - - std::vector theList = histoList(currentfile, theDir); - - gROOT ->Reset(); -/* char* rfilename = referencefile ; - char* sfilename = currentfile ; - - delete gROOT->GetListOfFiles()->FindObject(rfilename); - delete gROOT->GetListOfFiles()->FindObject(sfilename); - - TFile * rfile = new TFile(rfilename); - TFile * sfile = new TFile(sfilename); */ - - std::string rfilename = (std::string) referencefile ; - std::string sfilename = (std::string) currentfile ; - - delete gROOT->GetListOfFiles()->FindObject(rfilename.c_str()); - delete gROOT->GetListOfFiles()->FindObject(sfilename.c_str()); - - TFile * rfile = new TFile(rfilename.c_str()); - TFile * sfile = new TFile(sfilename.c_str()); - -// char* baseDir=theDir; - std::string baseDir= (std::string) theDir; - -// rfile->cd(baseDir); - rfile->cd(baseDir.c_str()); - gDirectory->ls(); - -// sfile->cd(baseDir); - sfile->cd(baseDir.c_str()); - gDirectory->ls(); - - - - for ( unsigned int index = 0; index < theList.size() ; index++ ) { - - std::cout << index << std::endl; - - TString theName = theDir+"/"+theList[index]; - std::cout << theName << std::endl; - - TH1* href_; - rfile->GetObject(theName,href_); - href_; + std::vector theList = histoList(currentfile, theDir); - TH1* hnew_; - sfile->GetObject(theName,hnew_); - hnew_; - - /* double integral = href_ -> Integral()/10000.; - - if (integral) - { - href_->Scale( 1. / integral ); - } - - integral = hnew_ -> Integral()/10000.; - - if (integral) - { - hnew_->Scale( 1. / integral ); - } */ + gROOT->Reset(); + std::string rfilename = (std::string) referencefile ; + std::string sfilename = (std::string) currentfile ; - MEComparePlot(href_, hnew_, currentfile, referencefile, theDir, theList[index]); - - } - + delete gROOT->GetListOfFiles()->FindObject(rfilename.c_str()); + delete gROOT->GetListOfFiles()->FindObject(sfilename.c_str()); + + TFile * rfile = new TFile(rfilename.c_str()); + TFile * sfile = new TFile(sfilename.c_str()); + + std::string baseDir = (std::string) theDir; + + rfile->cd(baseDir.c_str()); + gDirectory->ls(); + + sfile->cd(baseDir.c_str()); + gDirectory->ls(); + + for ( unsigned int index = 0; index < theList.size() ; index++ ) { + + std::cout << index << std::endl; + + TString theName = theDir+"/"+theList[index]; + std::cout << theName << std::endl; + + TH1* href_; + rfile->GetObject(theName, href_); + + TH1* hnew_; + sfile->GetObject(theName, hnew_); + + MEComparePlot(href_, hnew_, currentfile, referencefile, theDir, theList[index]); + } } From ba08c042ac40e56649cc0c00e5f7aaf6d7c47c67 Mon Sep 17 00:00:00 2001 From: orduna Date: Thu, 10 Dec 2015 10:06:36 -0600 Subject: [PATCH 2/2] Added -f flag to rm command to remove error message when no files are present --- CMSDAS2016/GenExercise/macros/plot.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CMSDAS2016/GenExercise/macros/plot.sh b/CMSDAS2016/GenExercise/macros/plot.sh index cdb88ba..83b6ae3 100755 --- a/CMSDAS2016/GenExercise/macros/plot.sh +++ b/CMSDAS2016/GenExercise/macros/plot.sh @@ -6,9 +6,9 @@ hist2=$2 echo $hist1 echo $hist2 -rm root.sh +rm -fv root.sh touch root.sh -rm *.ps +rm -fv *.ps echo " .L MECompare.C+