CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_8_patch3/src/FWCore/PrescaleService/src/PrescaleService.cc

Go to the documentation of this file.
00001 
00002 //
00003 // PrescaleService
00004 // ---------------
00005 //
00006 //            04/25/2008 Philipp Schieferdecker <philipp.schieferdecker@cern.ch>
00008 
00009 
00010 #include "FWCore/PrescaleService/interface/PrescaleService.h"
00011 #include "FWCore/ServiceRegistry/interface/ActivityRegistry.h"
00012 #include "FWCore/ParameterSet/interface/Registry.h"
00013 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
00014 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
00015 #include "FWCore/Utilities/interface/Exception.h"
00016 
00017 #include <set>
00018 #include <algorithm>
00019 
00020 
00021 namespace edm {
00022   namespace service {
00023 
00025     // construction/destruction
00027 
00028     //______________________________________________________________________________
00029     PrescaleService::PrescaleService(ParameterSet const& iPS,ActivityRegistry& iReg)
00030       : configured_(false)
00031       , forceDefault_(iPS.getParameter<bool>("forceDefault"))
00032       , lvl1Labels_(iPS.getParameter<std::vector<std::string> >("lvl1Labels"))
00033       , nLvl1Index_(lvl1Labels_.size())
00034       , iLvl1IndexDefault_(findDefaultIndex(iPS.getParameter<std::string>("lvl1DefaultLabel"), lvl1Labels_))
00035       , vpsetPrescales_(iPS.getParameterSetVector("prescaleTable"))
00036       , prescaleTable_()
00037     {
00038       iReg.watchPostBeginJob(this, &PrescaleService::postBeginJob);
00039       iReg.watchPostEndJob(this, &PrescaleService::postEndJob);
00040       
00041       iReg.watchPreProcessEvent(this, &PrescaleService::preEventProcessing);
00042       iReg.watchPostProcessEvent(this, &PrescaleService::postEventProcessing);
00043       
00044       iReg.watchPreModule(this, &PrescaleService::preModule);
00045       iReg.watchPostModule(this, &PrescaleService::postModule);
00046     }
00047       
00048     //______________________________________________________________________________
00049     PrescaleService::~PrescaleService() {
00050     }
00051 
00053     // implementation of member functions
00055 
00056     void PrescaleService::reconfigure(ParameterSet const& iPS) {
00057       vpsetPrescales_.clear();
00058       prescaleTable_.clear();
00059       lvl1Labels_ = iPS.getParameter<std::vector<std::string> >("lvl1Labels");
00060       nLvl1Index_ = lvl1Labels_.size();
00061       iLvl1IndexDefault_ = findDefaultIndex(iPS.getParameter<std::string>("lvl1DefaultLabel"), lvl1Labels_);
00062       vpsetPrescales_ = iPS.getParameterSetVector("prescaleTable");
00063       configure();
00064     }
00065 
00066     void PrescaleService::postBeginJob() {
00067       if (!configured_) {
00068         configure();
00069       }
00070     }
00071 
00072     //______________________________________________________________________________
00073     void PrescaleService::configure()
00074     {
00075       configured_ = true;
00076 
00077       ParameterSet prcPS = getProcessParameterSet();
00078       
00079       // find all HLTPrescaler modules
00080       std::set<std::string> prescalerModules;
00081       std::vector<std::string> allModules=prcPS.getParameter<std::vector<std::string> >("@all_modules");
00082       for(unsigned int i = 0; i < allModules.size(); ++i) {
00083         ParameterSet const& pset  = prcPS.getParameterSet(allModules[i]);
00084         std::string moduleLabel = pset.getParameter<std::string>("@module_label");
00085         std::string moduleType  = pset.getParameter<std::string>("@module_type");
00086         if (moduleType == "HLTPrescaler") prescalerModules.insert(moduleLabel);
00087       }
00088       
00089       // find all paths with an HLTPrescaler and check for <=1
00090       std::set<std::string> prescaledPathSet;
00091       std::vector<std::string> allPaths = prcPS.getParameter<std::vector<std::string> >("@paths");
00092       for (unsigned int iP = 0; iP < allPaths.size(); ++iP) {
00093         std::string pathName = allPaths[iP];
00094         std::vector<std::string> modules = prcPS.getParameter<std::vector<std::string> >(pathName);
00095         for (unsigned int iM = 0; iM < modules.size(); ++iM) {
00096           std::string moduleLabel = modules[iM];
00097           if (prescalerModules.erase(moduleLabel)>0) {
00098             std::set<std::string>::const_iterator itPath=prescaledPathSet.find(pathName);
00099             if (itPath==prescaledPathSet.end()) {
00100               prescaledPathSet.insert(pathName);
00101             } else {
00102               throw cms::Exception("DuplicatePrescaler")
00103                 <<"path '"<<pathName<<"' has more than one HLTPrescaler!";
00104             }
00105           }
00106         }
00107       }
00108 
00109       std::vector<std::string> prescaledPaths;
00110       for (unsigned int iVPSet=0; iVPSet < vpsetPrescales_.size(); ++iVPSet) {
00111         ParameterSet psetPrescales = vpsetPrescales_[iVPSet];
00112         std::string pathName = psetPrescales.getParameter<std::string>("pathName");
00113         if (prescaledPathSet.erase(pathName) > 0) {
00114           std::vector<unsigned int> prescales =
00115             psetPrescales.getParameter<std::vector<unsigned int> >("prescales");
00116           if (prescales.size()!=nLvl1Index_) {
00117             throw cms::Exception("PrescaleTableMismatch")
00118               << "path '" << pathName << "' has unexpected number of prescales";
00119           }
00120           prescaleTable_[pathName] = prescales;
00121         }
00122         else {
00123           throw cms::Exception("PrescaleTableUnknownPath")
00124             <<"path '"<<pathName<<"' is invalid or does not "
00125             <<"contain any HLTPrescaler";
00126         }
00127       }      
00128     }
00129 
00130     //______________________________________________________________________________
00131     unsigned int PrescaleService::getPrescale(std::string const& prescaledPath)
00132     {
00133       return getPrescale(iLvl1IndexDefault_, prescaledPath);
00134     }
00135     
00136     //______________________________________________________________________________
00137     unsigned int PrescaleService::getPrescale(unsigned int lvl1Index,
00138                                               std::string const& prescaledPath)
00139     {
00140       if (forceDefault_)
00141         lvl1Index = iLvl1IndexDefault_;
00142 
00143       if (lvl1Index >= nLvl1Index_) {
00144         throw cms::Exception("InvalidLvl1Index")
00145           <<"lvl1Index '"<<lvl1Index<<"' exceeds number of prescale columns";
00146       }
00147 
00148       if (!configured_) {
00149         configure();
00150       }
00151       
00152       PrescaleTable_t::const_iterator it = prescaleTable_.find(prescaledPath);
00153       return (it == prescaleTable_.end()) ? 1 : it->second[lvl1Index];
00154     }
00155     
00156     //______________________________________________________________________________
00157     unsigned int PrescaleService::findDefaultIndex(std::string const & label, std::vector<std::string> const & labels) {
00158       for (unsigned int i = 0; i < labels.size(); ++i) {
00159         if (labels[i] == label) {
00160           return i;
00161         }
00162       }
00163       // FIXME add a LogWarning if the default is not found ?
00164       return 0;
00165     }
00166     
00167     //______________________________________________________________________________
00168     void PrescaleService::fillDescriptions(edm::ConfigurationDescriptions & descriptions) {
00169       edm::ParameterSetDescription desc;
00170 
00171       std::vector<std::string> defaultVector;
00172       defaultVector.push_back(std::string("default"));
00173       desc.add<std::vector<std::string> >("lvl1Labels", defaultVector);
00174 
00175       // This default vector<ParameterSet> will be used when
00176       // the configuration does not include this parameter and
00177       // it also gets written into the generated cfi file.
00178       std::vector<edm::ParameterSet> defaultVPSet;
00179       edm::ParameterSet pset0;
00180       pset0.addParameter<std::string>("pathName", std::string("HLTPath"));
00181       std::vector<unsigned> defaultVectorU;
00182       defaultVectorU.push_back(1u);
00183       pset0.addParameter<std::vector<unsigned> >("prescales", defaultVectorU);
00184       defaultVPSet.push_back(pset0);
00185 
00186       edm::ParameterSetDescription validator;
00187       validator.add<std::string>("pathName");
00188       validator.add<std::vector<unsigned int> >("prescales");
00189 
00190       desc.addVPSet("prescaleTable", validator, defaultVPSet);
00191 
00192       desc.add<std::string>("lvl1DefaultLabel", std::string("default"));
00193       desc.add<bool>       ("forceDefault",     false);
00194 
00195       descriptions.add("PrescaleService", desc);
00196     }
00197 
00198   } // namespace service
00199 } // namespace edm