CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_14/src/Validation/RecoTau/plugins/DQMHistEffProducer.cc

Go to the documentation of this file.
00001 #include "Validation/RecoTau/plugins/DQMHistEffProducer.h"
00002 
00003 #include "Validation/RecoTau/plugins/dqmAuxFunctions.h"
00004 
00005 // framework & common header files
00006 #include "FWCore/Framework/interface/Frameworkfwd.h"
00007 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00008 
00009 //DQM services
00010 #include "DQMServices/Core/interface/DQMStore.h"
00011 #include "FWCore/ServiceRegistry/interface/Service.h"
00012 
00013 
00014 #include <TH1.h>
00015 #include <TCanvas.h>
00016 
00017 #include <iostream>
00018 
00019 TauDQMHistEffProducer::cfgEntryPlot::cfgEntryPlot(const edm::ParameterSet& cfg)
00020 {
00021   //std::cout << "<cfgEntryPlot::cfgEntryPlot>:" << std::endl;
00022 
00023   numerator_ = cfg.getParameter<std::string>("numerator");
00024   //std::cout << " numerator = " << numerator_ << std::endl;
00025 
00026   denominator_ = cfg.getParameter<std::string>("denominator");
00027   //std::cout << " denominator = " << denominator_ << std::endl;
00028 
00029   efficiency_ = cfg.getParameter<std::string>("efficiency");
00030   //std::cout << " efficiency = " << efficiency_ << std::endl;
00031 }
00032 
00033 TauDQMHistEffProducer::cfgEntryPlot::cfgEntryPlot(const std::string& numerator, const std::string& denominator, const std::string& efficiency)
00034   : numerator_(numerator), denominator_(denominator), efficiency_(efficiency)
00035 {
00036   //std::cout << "<cfgEntryPlot::cfgEntryPlot>:" << std::endl;
00037   //std::cout << " numerator = " << numerator_ << std::endl;
00038   //std::cout << " denominator = " << denominator_ << std::endl;
00039   //std::cout << " efficiency = " << efficiency_ << std::endl;
00040 }
00041 
00042 //
00043 //-----------------------------------------------------------------------------------------------------------------------
00044 //
00045 
00046 TauDQMHistEffProducer::TauDQMHistEffProducer(const edm::ParameterSet& cfg)
00047 {
00048   //std::cout << "<TauDQMHistEffProducer::TauDQMHistEffProducer>:" << std::endl;
00049   
00050   edm::ParameterSet plots = cfg.getParameter<edm::ParameterSet>("plots");
00051   std::vector<std::string> plotNames = plots.getParameterNamesForType<edm::ParameterSet>();
00052   for ( std::vector<std::string>::const_iterator plotName = plotNames.begin(); plotName != plotNames.end(); ++plotName ) {
00053     edm::ParameterSet plotConfig = plots.getParameter<edm::ParameterSet>(*plotName);
00054 
00055     typedef std::vector<std::string> vstring;
00056     vstring plotParameter = plotConfig.getParameter<vstring>("parameter");
00057     if ( plotParameter.size() == 0 ) {
00058       cfgEntryPlot_.push_back(cfgEntryPlot(plotConfig));
00059     } else {
00060       std::string numerator = plotConfig.getParameter<std::string>("numerator");
00061       std::string denominator = plotConfig.getParameter<std::string>("denominator");
00062       std::string efficiency = plotConfig.getParameter<std::string>("efficiency");
00063       for ( vstring::const_iterator parameter = plotParameter.begin(); parameter != plotParameter.end(); ++parameter ) {
00064         int errorFlag = 0;
00065         std::string modNumerator = replace_string(numerator, parKeyword, *parameter, 1, 1, errorFlag);
00066         std::string modDenominator = replace_string(denominator, parKeyword, *parameter, 1, 1, errorFlag);
00067         std::string modEfficiency = replace_string(efficiency, parKeyword, *parameter, 1, 1, errorFlag);
00068 
00069         if ( !errorFlag ) {
00070           cfgEntryPlot_.push_back(cfgEntryPlot(modNumerator, modDenominator, modEfficiency));
00071         } else {
00072           edm::LogError("TauDQMHistEffProducer") << " Failed to decode histogram names for plotName = " << (*plotName) 
00073                                               << " --> skipping !!";
00074         }
00075       }
00076     }
00077   }
00078 }
00079 
00080 TauDQMHistEffProducer::~TauDQMHistEffProducer() 
00081 {
00082 //--- nothing to be done yet
00083 }
00084 
00085 void TauDQMHistEffProducer::analyze(const edm::Event&, const edm::EventSetup&)
00086 {
00087 //--- nothing to be done yet
00088 }
00089 
00090 void TauDQMHistEffProducer::endRun(const edm::Run& r, const edm::EventSetup& c)
00091 {
00092   //std::cout << "<TauDQMHistEffProducer::endJob>:" << std::endl;
00093 
00094 //--- check that DQMStore service is available
00095   if ( !edm::Service<DQMStore>().isAvailable() ) {
00096     edm::LogError ("endJob") << " Failed to access dqmStore --> histograms will NOT be plotted !!";
00097     return;
00098   }
00099 
00100   DQMStore& dqmStore = (*edm::Service<DQMStore>());
00101 
00102   for ( std::vector<cfgEntryPlot>::const_iterator plot = cfgEntryPlot_.begin(); plot != cfgEntryPlot_.end(); ++plot ) {
00103     //std::cout << "plot->numerator_ = " << plot->numerator_ << std::endl;
00104     std::string numeratorHistogramName, numeratorHistogramDirectory;
00105     separateHistogramFromDirectoryName(plot->numerator_, numeratorHistogramName, numeratorHistogramDirectory);
00106     //std::cout << "numeratorHistogramName = " << numeratorHistogramName << std::endl;
00107     //std::cout << "numeratorHistogramDirectory = " << numeratorHistogramDirectory << std::endl;
00108     MonitorElement* meNumerator = dqmStore.get(std::string(numeratorHistogramDirectory).append(dqmSeparator).append(numeratorHistogramName));
00109     //std::cout << "meNumerator = " << meNumerator << std::endl;
00110     TH1* histoNumerator = ( meNumerator != NULL ) ? meNumerator->getTH1() : NULL;
00111     
00112     //std::cout << "plot->denominator_ = " << plot->denominator_ << std::endl;
00113     std::string denominatorHistogramName, denominatorHistogramDirectory;
00114     separateHistogramFromDirectoryName(plot->denominator_, denominatorHistogramName, denominatorHistogramDirectory);
00115     //std::cout << "denominatorHistogramName = " << denominatorHistogramName << std::endl;
00116     //std::cout << "denominatorHistogramDirectory = " << denominatorHistogramDirectory << std::endl;
00117     MonitorElement* meDenominator = dqmStore.get(std::string(denominatorHistogramDirectory).append(dqmSeparator).append(denominatorHistogramName));
00118     //std::cout << "meDenominator = " << meDenominator << std::endl;
00119     TH1* histoDenominator = ( meDenominator != NULL ) ? meDenominator->getTH1() : NULL;
00120     
00121     if ( histoNumerator != NULL && histoDenominator != NULL ) {
00122       if ( !histoNumerator->GetSumw2N() ) histoNumerator->Sumw2();
00123       //std::cout << " histoNumerator->GetName = " << histoNumerator->GetName() << std::endl;
00124       
00125       if ( !histoDenominator->GetSumw2N() ) histoDenominator->Sumw2();
00126       //std::cout << " histoDenominator->GetName = " << histoNumerator->GetName() << std::endl;
00127       
00128       std::string effHistogramName, effHistogramDirectory, dummy;
00129       separateHistogramFromDirectoryName(plot->efficiency_, effHistogramName, effHistogramDirectory);
00130       //if ( effHistogramDirectory == "" ) separateHistogramFromDirectoryName(numeratorHistogramName, dummy, effHistogramDirectory);
00131       if ( effHistogramDirectory != "" ) 
00132         {
00133           if(dqmStore.dirExists(effHistogramDirectory))
00134             dqmStore.setCurrentFolder(effHistogramDirectory);
00135           else
00136             std::cout<<"TauDQMHistEffProducer:: Directory: "<<effHistogramDirectory<<" does not exist!"<<std::endl;
00137         }
00138       
00139       MonitorElement* histoEfficiency = dqmStore.book1D(effHistogramName, effHistogramName, 
00140                                                         histoNumerator->GetNbinsX(), histoNumerator->GetXaxis()->GetXmin(), histoNumerator->GetXaxis()->GetXmax());
00141       
00142       histoEfficiency->getTH1F()->Divide(histoNumerator, histoDenominator, 1., 1., "B");
00143 
00144       //to avoid the pointer to go out of scope:
00145       histoEfficiencyVector_.push_back(histoEfficiency);
00146 
00147       /*      std::vector<std::string> mes = dqmStore.getMEs();
00148       std::cout<<dqmStore.pwd()<<std::endl;
00149       for(unsigned int i =0; i<mes.size(); i++)
00150         std::cout<<mes[i]<<std::endl;
00151       */
00152     } else {
00153       edm::LogError("endRun") << " Failed to produce efficiency histogram = " << plot->efficiency_ << " !!";
00154       if ( histoNumerator   == NULL ) edm::LogError("endRun") << "  numerator = " << plot->numerator_ << " does not exist.";
00155       if ( histoDenominator == NULL ) edm::LogError("endRun") << "  denominator = " << plot->denominator_ << " does not exist.";
00156     }
00157   }
00158 }
00159 
00160 #include "FWCore/Framework/interface/MakerMacros.h"
00161 
00162 DEFINE_FWK_MODULE(TauDQMHistEffProducer);