CMS 3D CMS Logo

/data/git/CMSSW_5_3_11_patch5/src/GeneratorInterface/RivetInterface/plugins/DQMRivetClient.cc

Go to the documentation of this file.
00001 /*
00002  *  Class:DQMRivetClient 
00003  *
00004  *
00005  *  $Date: 2012/06/22 08:47:17 $
00006  *  $Revision: 1.1 $
00007  * 
00008  *  \author Junghwan Goh - SungKyunKwan University
00009  */
00010 
00011 #include "GeneratorInterface/RivetInterface/interface/DQMRivetClient.h"
00012 
00013 #include "DQMServices/ClientConfig/interface/FitSlicesYTool.h"
00014 #include "DQMServices/Core/interface/DQMStore.h"
00015 #include "DQMServices/Core/interface/MonitorElement.h"
00016 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00017 #include "FWCore/ServiceRegistry/interface/Service.h"
00018 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00019 
00020 #include <TH1F.h>
00021 #include <TClass.h>
00022 #include <TString.h>
00023 #include <TPRegexp.h>
00024 
00025 #include <cmath>
00026 #include <boost/tokenizer.hpp>
00027 
00028 using namespace std;
00029 using namespace edm;
00030 
00031 typedef MonitorElement ME;
00032 
00033 TPRegexp metacharacters("[\\^\\$\\.\\*\\+\\?\\|\\(\\)\\{\\}\\[\\]]");
00034 
00035 DQMRivetClient::DQMRivetClient(const ParameterSet& pset)
00036 {
00037   
00038   typedef std::vector<edm::ParameterSet> VPSet;
00039   typedef std::vector<std::string> vstring;
00040   typedef boost::escaped_list_separator<char> elsc;
00041 
00042   elsc commonEscapes("\\", " \t", "\'"); 
00043 
00044   // Parse Normalization commands
00045   vstring normCmds = pset.getUntrackedParameter<vstring>("normalizationToIntegral", vstring());
00046   for ( vstring::const_iterator normCmd = normCmds.begin();
00047         normCmd != normCmds.end(); ++normCmd )
00048   {
00049     if ( normCmd->empty() ) continue;
00050     boost::tokenizer<elsc> tokens(*normCmd, commonEscapes);
00051 
00052     vector<string> args;
00053     for(boost::tokenizer<elsc>::const_iterator iToken = tokens.begin();
00054         iToken != tokens.end(); ++iToken) {
00055       if ( iToken->empty() ) continue;
00056       args.push_back(*iToken);
00057     }
00058 
00059     if ( args.empty() or args.size() > 2 ) {
00060       LogInfo("DQMRivetClient") << "Wrong input to normCmds\n";
00061       continue;
00062     }
00063 
00064     DQMGenericClient::NormOption opt;
00065     opt.name = args[0];
00066     opt.normHistName = args.size() == 2 ? args[1] : args[0];
00067 
00068     normOptions_.push_back(opt);
00069   }
00070 
00071   VPSet normSets = pset.getUntrackedParameter<VPSet>("normalizationToIntegralSets", VPSet());
00072   for ( VPSet::const_iterator normSet = normSets.begin();
00073         normSet != normSets.end(); ++normSet )
00074   {
00075     DQMGenericClient::NormOption opt;
00076     opt.name = normSet->getUntrackedParameter<string>("name");
00077     opt.normHistName = normSet->getUntrackedParameter<string>("normalizedTo", opt.name);
00078 
00079     normOptions_.push_back(opt);
00080   }
00081 
00082   //normalize to lumi 
00083   vstring lumiCmds = pset.getUntrackedParameter<vstring>("normalizationToLumi", vstring());
00084   for ( vstring::const_iterator lumiCmd = lumiCmds.begin();
00085         lumiCmd != lumiCmds.end(); ++lumiCmd )
00086   {
00087     if ( lumiCmd->empty() ) continue;
00088     boost::tokenizer<elsc> tokens(*lumiCmd, commonEscapes);
00089 
00090     vector<string> args;
00091     for(boost::tokenizer<elsc>::const_iterator iToken = tokens.begin();
00092         iToken != tokens.end(); ++iToken) {
00093       if ( iToken->empty() ) continue;
00094       args.push_back(*iToken);
00095     }
00096 
00097     if ( args.size() != 2 ) {
00098       LogInfo("DQMRivetClient") << "Wrong input to lumiCmds\n";
00099       continue;
00100     }
00101 
00102     DQMRivetClient::LumiOption opt;
00103     opt.name = args[0];
00104     opt.normHistName = args[1] ;
00105     opt.xsection = pset.getUntrackedParameter<double>("xsection", -1.);
00106     //opt.xsection = atof(args[2].c_str());
00107 
00108     //std::cout << opt.name << " " << opt.normHistName << " " << opt.xsection << std::endl; 
00109     lumiOptions_.push_back(opt);
00110   } 
00111 
00112   //multiply by a number
00113   vstring scaleCmds = pset.getUntrackedParameter<vstring>("scaleBy", vstring());
00114   for ( vstring::const_iterator scaleCmd = scaleCmds.begin();
00115         scaleCmd != scaleCmds.end(); ++scaleCmd )
00116   {
00117     if ( scaleCmd->empty() ) continue;
00118     boost::tokenizer<elsc> tokens(*scaleCmd, commonEscapes);
00119 
00120     vector<string> args;
00121     for(boost::tokenizer<elsc>::const_iterator iToken = tokens.begin();
00122         iToken != tokens.end(); ++iToken) {
00123       if ( iToken->empty() ) continue;
00124       args.push_back(*iToken);
00125     }
00126 
00127     if ( args.empty() or args.size() > 2 ) {
00128       LogInfo("DQMRivetClient") << "Wrong input to normCmds\n";
00129       continue;
00130     }
00131 
00132     ScaleFactorOption opt;
00133     opt.name = args[0];
00134     opt.scale = atof(args[1].c_str());
00135     scaleOptions_.push_back(opt);
00136   }  
00137    
00138 
00139   outputFileName_ = pset.getUntrackedParameter<string>("outputFileName", "");
00140   subDirs_ = pset.getUntrackedParameter<vstring>("subDirs");
00141 
00142 }
00143 
00144 void DQMRivetClient::endRun(const edm::Run& r, const edm::EventSetup& c) {
00145 
00146   typedef vector<string> vstring;
00147 
00148   // Update 2009-09-23
00149   // Migrated all code from endJob to this function
00150   // endJob is not necessarily called in the proper sequence
00151   // and does not necessarily book histograms produced in
00152   // that step.
00153   // It more robust to do the histogram manipulation in
00154   // this endRun function
00155 
00156 
00157   
00158   theDQM = 0;
00159   theDQM = Service<DQMStore>().operator->();
00160 
00161   if ( ! theDQM ) {
00162     LogInfo("DQMRivetClient") << "Cannot create DQMStore instance\n";
00163     return;
00164   }
00165 
00166   // Process wildcard in the sub-directory
00167   set<string> subDirSet;
00168 
00169   for(vstring::const_iterator iSubDir = subDirs_.begin();
00170       iSubDir != subDirs_.end(); ++iSubDir) {
00171     string subDir = *iSubDir;
00172 
00173     if ( subDir[subDir.size()-1] == '/' ) subDir.erase(subDir.size()-1);
00174 
00175     subDirSet.insert(subDir);
00176   }
00177 
00178   for(set<string>::const_iterator iSubDir = subDirSet.begin();
00179       iSubDir != subDirSet.end(); ++iSubDir) {
00180     const string& dirName = *iSubDir;
00181     for ( vector<DQMGenericClient::NormOption>::const_iterator normOption = normOptions_.begin();
00182           normOption != normOptions_.end(); ++normOption ){
00183       normalizeToIntegral(dirName, normOption->name, normOption->normHistName);
00184     }
00185   }
00186 
00187   for(set<string>::const_iterator iSubDir = subDirSet.begin();
00188       iSubDir != subDirSet.end(); ++iSubDir) {
00189     const string& dirName = *iSubDir;
00190     for ( vector<LumiOption>::const_iterator lumiOption = lumiOptions_.begin();
00191           lumiOption != lumiOptions_.end(); ++lumiOption ){
00192       normalizeToLumi(dirName, lumiOption->name, lumiOption->normHistName, lumiOption->xsection);
00193     }
00194   }
00195 
00196   for(set<string>::const_iterator iSubDir = subDirSet.begin();
00197       iSubDir != subDirSet.end(); ++iSubDir) {
00198     const string& dirName = *iSubDir;
00199     for ( vector<ScaleFactorOption>::const_iterator scaleOption = scaleOptions_.begin();
00200           scaleOption != scaleOptions_.end(); ++scaleOption ){
00201       scaleByFactor(dirName, scaleOption->name, scaleOption->scale);
00202     }
00203   }
00204 
00205 
00206   if ( ! outputFileName_.empty() ) theDQM->save(outputFileName_);
00207   
00208 }
00209 
00210 void DQMRivetClient::endJob()
00211 {
00212 
00213   // Update 2009-09-23
00214   // Migrated all code from here to endRun
00215 
00216   LogTrace ("DQMRivetClient") << "inside of DQMGenericClient::endJob()"
00217                                 << endl;
00218 
00219 }
00220 
00221 void DQMRivetClient::normalizeToIntegral(const std::string& startDir, const std::string& histName, const std::string& normHistName) 
00222 {
00223   if ( ! theDQM->dirExists(startDir) ) {
00224     LogInfo("DQMRivetClient") << "normalizeToEntries() : "
00225                                      << "Cannot find sub-directory " << startDir << endl;
00226     return;
00227   }
00228 
00229   theDQM->cd();
00230 
00231   ME* element = theDQM->get(startDir+"/"+histName);
00232   ME* normME = theDQM->get(startDir+"/"+normHistName);
00233 
00234   if ( !element ) {
00235     LogInfo("DQMRivetClient") << "normalizeToEntries() : "
00236                                      << "No such element '" << histName << "' found\n";
00237     return;
00238   }
00239 
00240   if ( !normME ) {
00241     LogInfo("DQMRivetClient") << "normalizeToEntries() : "
00242                                      << "No such element '" << normHistName << "' found\n";
00243     return;
00244   }
00245 
00246   TH1F* hist  = element->getTH1F();
00247   if ( !hist) {
00248     LogInfo("DQMRivetClient") << "normalizeToEntries() : "
00249                                      << "Cannot create TH1F from ME\n";
00250     return;
00251   }
00252 
00253   TH1F* normHist = normME->getTH1F();
00254   if ( !normHist ) {
00255     LogInfo("DQMRivetClient") << "normalizeToEntries() : "
00256                                      << "Cannot create TH1F from ME\n";
00257     return;
00258   }
00259 
00260   const double entries = normHist->Integral();
00261   if ( entries != 0 ) {
00262     hist->Scale(1./entries, "width");
00263   }
00264   else {
00265     LogInfo("DQMRivetClient") << "normalizeToEntries() : " 
00266                                    << "Zero entries in histogram\n";
00267   }
00268 
00269   return;
00270 }
00271 
00272 void DQMRivetClient::normalizeToLumi(const std::string& startDir, const std::string& histName, const std::string& normHistName, double xsection){
00273   normalizeToIntegral(startDir, histName, normHistName);
00274   theDQM->cd();
00275   ME* element = theDQM->get(startDir+"/"+histName);
00276   TH1F* hist  = element->getTH1F();
00277   if ( !hist) {
00278     LogInfo("DQMRivetClient") << "normalizeToEntries() : "
00279                               << "Cannot create TH1F from ME\n";
00280     return;
00281   }
00282   hist->Scale(xsection);
00283   return;
00284 }
00285 
00286 void DQMRivetClient::scaleByFactor(const std::string& startDir, const std::string& histName, double factor){
00287   if ( ! theDQM->dirExists(startDir) ) {
00288     LogInfo("DQMRivetClient") << "normalizeToEntries() : "
00289                                      << "Cannot find sub-directory " << startDir << endl;
00290     return;
00291   }
00292 
00293   theDQM->cd();
00294 
00295   ME* element = theDQM->get(startDir+"/"+histName);
00296 
00297   if ( !element ) {
00298     LogInfo("DQMRivetClient") << "normalizeToEntries() : "
00299                                      << "No such element '" << histName << "' found\n";
00300     return;
00301   }
00302 
00303   TH1F* hist  = element->getTH1F();
00304   if ( !hist) {
00305     LogInfo("DQMRivetClient") << "normalizeToEntries() : "
00306                                      << "Cannot create TH1F from ME\n";
00307     return;
00308   }
00309   hist->Scale(factor);
00310 }
00311 
00312 #include "FWCore/Framework/interface/MakerMacros.h"
00313 
00314 DEFINE_FWK_MODULE(DQMRivetClient);