CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_14/src/HLTrigger/HLTcore/src/HLTConfigProvider.cc

Go to the documentation of this file.
00001 
00012 #include "HLTrigger/HLTcore/interface/HLTConfigProvider.h"
00013 #include "HLTrigger/HLTcore/interface/HLTConfigDataRegistry.h"
00014 #include "FWCore/Utilities/interface/RegexMatch.h"
00015 #include "FWCore/Utilities/interface/ThreadSafeRegistry.h"
00016 #include "FWCore/ParameterSet/interface/Registry.h"
00017 #include "FWCore/Framework/interface/Run.h"
00018 #include "FWCore/Framework/interface/Event.h"
00019 #include "FWCore/Framework/interface/LuminosityBlock.h"
00020 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00021 #include "DataFormats/Provenance/interface/ProcessHistory.h"
00022 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00023 
00024 #include <boost/regex.hpp> 
00025 
00026 static const bool useL1EventSetup(true);
00027 static const bool useL1GtTriggerMenuLite(false);
00028 
00029 // an empty dummy config data used when we fail to initialize 
00030 static const HLTConfigData* s_dummyHLTConfigData()
00031 { static HLTConfigData dummyHLTConfigData;
00032   return &dummyHLTConfigData;
00033 }
00034 
00035 HLTConfigProvider::HLTConfigProvider():
00036   processName_(""),
00037   inited_(false),
00038   changed_(true),
00039   hltConfigData_(s_dummyHLTConfigData()),
00040   l1GtUtils_(new L1GtUtils())
00041 {
00042   //  HLTConfigDataRegistry::instance()->extra().increment();
00043 }
00044 
00045 //HLTConfigProvider::~HLTConfigProvider() {
00046 //  if (HLTConfigDataRegistry::instance()->extra().decrement()==0) {
00047 //    HLTConfigDataRegistry::instance()->data().clear();
00048 //  }
00049 //}
00050 
00051 HLTConfigProvider::HLTConfigCounterSentry::HLTConfigCounterSentry() {
00052   HLTConfigDataRegistry::instance()->extra().increment();
00053 }
00054 
00055 HLTConfigProvider::HLTConfigCounterSentry::HLTConfigCounterSentry(HLTConfigCounterSentry const&) {
00056   HLTConfigDataRegistry::instance()->extra().increment();
00057 }
00058 
00059 HLTConfigProvider::HLTConfigCounterSentry::HLTConfigCounterSentry(HLTConfigCounterSentry &&) {
00060   HLTConfigDataRegistry::instance()->extra().increment();
00061 }
00062 
00063 HLTConfigProvider::HLTConfigCounterSentry::~HLTConfigCounterSentry() {
00064   HLTConfigDataRegistry::instance()->extra().decrement();
00065 }
00066 
00067 bool HLTConfigProvider::init(const edm::Run& iRun, 
00068                              const edm::EventSetup& iSetup, 
00069                              const std::string& processName, 
00070                              bool& changed) {
00071 
00072    using namespace std;
00073    using namespace edm;
00074 
00075    LogInfo("HLTConfigData") << "Called (R) with processName '"
00076                             << processName
00077                             << "' for " << iRun.id() << endl;
00078 
00079    init(iRun.processHistory(),processName);
00080 
00082    l1GtUtils_->getL1GtRunCache(iRun,iSetup,useL1EventSetup,useL1GtTriggerMenuLite);
00083 
00084    processName_=processName;
00085    changed=changed_;
00086    return inited_;
00087 
00088 }
00089 
00090 void HLTConfigProvider::init(const edm::ProcessHistory& iHistory, const std::string& processName) {
00091 
00092    using namespace std;
00093    using namespace edm;
00094 
00095    const ProcessHistory::const_iterator hb(iHistory.begin());
00096    const ProcessHistory::const_iterator he(iHistory.end());
00097 
00098    ProcessConfiguration processConfiguration;
00099    const edm::ParameterSet* processPSet(0);
00100 
00101    processName_=processName;
00102    if (processName_=="*") {
00103      // auto-discovery of process name
00104      for (ProcessHistory::const_iterator hi=hb; hi!=he; ++hi) {
00105        if (iHistory.getConfigurationForProcess(hi->processName(),processConfiguration)) {
00106          processPSet = edm::pset::Registry::instance()->getMapped(processConfiguration.parameterSetID());
00107          if ((processPSet!=0) && (processPSet->exists("hltTriggerSummaryAOD"))) {
00108            processName_=hi->processName();
00109          }       
00110        }
00111      }
00112      LogInfo("HLTConfigData") << "Auto-discovered processName: '"
00113                               << processName_ << "'"
00114                               << endl;
00115    }
00116    if (processName_=="*") {
00117      clear();
00118      LogError("HLTConfigData") << "Auto-discovery of processName failed!"
00119                                << endl;
00120      return;
00121    }
00122 
00124    unsigned int n(0);
00125    for (ProcessHistory::const_iterator hi=hb; hi!=he; ++hi) {
00126      if (hi->processName()==processName_) {n++;}
00127    }
00128    if (n>1) {
00129      clear();
00130      LogError("HLTConfigProvider") << " ProcessName '"<< processName_
00131                                    << " found " << n
00132                                    << " times in history!" << endl;
00133      return;
00134    }
00135 
00137    if (iHistory.getConfigurationForProcess(processName_,processConfiguration)) {
00138      if ((hltConfigData_ !=s_dummyHLTConfigData()) && (processConfiguration.parameterSetID() == hltConfigData_->id())) {
00139        changed_ = false;
00140        inited_  = true;
00141        return;
00142      } else {
00143        getDataFrom(processConfiguration.parameterSetID());
00144      }
00145    } else {
00146      LogError("HLTConfigProvider") << "Falling back to processName-only init!";
00147      clear();
00148      init(processName_);
00149      if (!inited_) {
00150        LogError("HLTConfigProvider") << "ProcessName not found in history!";
00151      }
00152      return;
00153    }
00154 }
00155 
00156 void HLTConfigProvider::getDataFrom(const edm::ParameterSetID& iID)
00157 {
00158   //is it in our registry?
00159   HLTConfigDataRegistry* reg = HLTConfigDataRegistry::instance();
00160   const HLTConfigData* d = reg->getMapped(iID);
00161   if(0 != d) {
00162     changed_ = true;
00163     inited_  = true;
00164     hltConfigData_ = d;
00165   } else {
00166     const edm::ParameterSet* processPSet = 0;
00167     if ( 0 != (processPSet = edm::pset::Registry::instance()->getMapped(iID))) {
00168        if (not processPSet->id().isValid()) {
00169          clear();
00170          edm::LogError("HLTConfigProvider") << "ProcessPSet found is empty!";
00171          changed_ = true; 
00172          inited_  = false; 
00173          hltConfigData_ = s_dummyHLTConfigData();
00174          return; 
00175        } else { 
00176          clear(); 
00177          reg->insertMapped( HLTConfigData(processPSet));
00178          changed_ = true; 
00179          inited_  = true; 
00180          hltConfigData_ = reg->getMapped(processPSet->id());
00181          return;
00182        }
00183      } else {
00184        clear();
00185        edm::LogError("HLTConfigProvider") << "ProcessPSet not found in regsistry!";
00186        changed_ = true;
00187        inited_  = false;
00188        hltConfigData_ = s_dummyHLTConfigData();       
00189        return;
00190      }
00191   }
00192   return;
00193 }
00194 
00195 void HLTConfigProvider::init(const std::string& processName)
00196 {
00197    using namespace std;
00198    using namespace edm;
00199 
00200    // Obtain ParameterSetID for requested process (with name
00201    // processName) from pset registry
00202    string pNames("");
00203    string hNames("");
00204    const ParameterSet*   pset = 0;
00205    ParameterSetID psetID;
00206    unsigned int   nPSets(0);
00207    const edm::pset::Registry * registry_(pset::Registry::instance());
00208    const edm::pset::Registry::const_iterator rb(registry_->begin());
00209    const edm::pset::Registry::const_iterator re(registry_->end());
00210    for (edm::pset::Registry::const_iterator i = rb; i != re; ++i) {
00211      if (i->second.exists("@process_name")) {
00212        const std::string pName(i->second.getParameter<string>("@process_name"));
00213        pNames += pName+" ";
00214        if ( pName == processName ) {
00215          psetID = i->first;
00216          nPSets++;
00217          if ((hltConfigData_ != s_dummyHLTConfigData()) && (hltConfigData_->id()==psetID)) {
00218            hNames += tableName();
00219          } else if ( 0 != (pset = registry_->getMapped(psetID))) {
00220            if (pset->exists("HLTConfigVersion")) {
00221              const ParameterSet& HLTPSet(pset->getParameterSet("HLTConfigVersion"));
00222              if (HLTPSet.exists("tableName")) {
00223                hNames += HLTPSet.getParameter<string>("tableName")+" ";
00224              }
00225            }
00226          }
00227        }
00228      }
00229    }
00230 
00231    LogVerbatim("HLTConfigProvider") << "Unordered list of all process names found: "
00232                                     << pNames << "." << endl;
00233 
00234    LogVerbatim("HLTConfigProvider") << "HLT TableName of each selected process: "
00235                                     << hNames << "." << endl;
00236 
00237    if (nPSets==0) {
00238      clear();
00239      LogError("HLTConfigProvider") << " Process name '"
00240                                    << processName
00241                                    << "' not found in registry!" << endl;
00242      return;
00243    }
00244    if (psetID==ParameterSetID()) {
00245      clear();
00246      LogError("HLTConfigProvider") << " Process name '"
00247                                    << processName
00248                                    << "' found but ParameterSetID invalid!"
00249                                    << endl;
00250      return;
00251    }
00252    if (nPSets>1) {
00253      clear();
00254      LogError("HLTConfigProvider") << " Process name '"
00255                                    << processName
00256                                    << " found " << nPSets
00257                                    << " times in registry!" << endl;
00258      return;
00259    }
00260 
00261    getDataFrom(psetID);
00262 
00263    return;
00264 
00265 }
00266 
00267 void HLTConfigProvider::clear()
00268 {
00269    // clear all data members
00270 
00271    processName_   = "";
00272    inited_        = false;
00273    changed_       = true;
00274    hltConfigData_ = s_dummyHLTConfigData();
00275    *l1GtUtils_    = L1GtUtils();
00276 
00277    return;
00278 }
00279 
00280 
00281 int HLTConfigProvider::prescaleSet(const edm::Event& iEvent, const edm::EventSetup& iSetup) const {
00282   // return hltPrescaleTable_.set();
00283   l1GtUtils_->getL1GtRunCache(iEvent,iSetup,useL1EventSetup,useL1GtTriggerMenuLite);
00284   int errorTech(0);
00285   const int psfsiTech(l1GtUtils_->prescaleFactorSetIndex(iEvent,L1GtUtils::TechnicalTrigger,errorTech));
00286   int errorPhys(0);
00287   const int psfsiPhys(l1GtUtils_->prescaleFactorSetIndex(iEvent,L1GtUtils::AlgorithmTrigger,errorPhys));
00288   assert(psfsiTech==psfsiPhys);
00289   if ( (errorTech==0) && (errorPhys==0) &&
00290        (psfsiTech>=0) && (psfsiPhys>=0) && (psfsiTech==psfsiPhys) ) {
00291     return psfsiPhys;
00292   } else {
00294     edm::LogError("HLTConfigData")
00295       << " Error in determining HLT prescale set index from L1 data using L1GtUtils: "
00296       << " Tech/Phys error = " << errorTech << "/" << errorPhys
00297       << " Tech/Phys psfsi = " << psfsiTech << "/" << psfsiPhys;
00298     return -1;
00299   }
00300 }
00301 
00302 unsigned int HLTConfigProvider::prescaleValue(const edm::Event& iEvent, const edm::EventSetup& iSetup, const std::string& trigger) const {
00303   const int set(prescaleSet(iEvent,iSetup));
00304   if (set<0) {
00305     return 1;
00306   } else {
00307     return prescaleValue(static_cast<unsigned int>(set),trigger);
00308   }
00309 }
00310 
00311 std::pair<int,int>  HLTConfigProvider::prescaleValues(const edm::Event& iEvent, const edm::EventSetup& iSetup, const std::string& trigger) const {
00312 
00313   // start with setting both L1T and HLT prescale values to 0
00314   std::pair<int,int> result(std::pair<int,int>(0,0));
00315 
00316   // get HLT prescale (possible if HLT prescale set index is correctly found)
00317   const int set(prescaleSet(iEvent,iSetup));
00318   if (set<0) {
00319     result.second = -1;
00320   } else {
00321     result.second = static_cast<int>(prescaleValue(static_cast<unsigned int>(set),trigger));
00322   }
00323 
00324   // get L1T prescale - works only for those hlt trigger paths with
00325   // exactly one L1GT seed module which has exactly one L1T name as seed
00326   const unsigned int nL1GTSeedModules(hltL1GTSeeds(trigger).size());
00327   if (nL1GTSeedModules==0) {
00328     // no L1 seed module on path hence no L1 seed hence formally no L1 prescale
00329     result.first=1;
00330   } else if (nL1GTSeedModules==1) {
00331     l1GtUtils_->getL1GtRunCache(iEvent,iSetup,useL1EventSetup,useL1GtTriggerMenuLite);
00332     const std::string l1tname(hltL1GTSeeds(trigger).at(0).second);
00333     int               l1error(0);
00334     result.first = l1GtUtils_->prescaleFactor(iEvent,l1tname,l1error);
00335     if (l1error!=0) {
00336       edm::LogError("HLTConfigData")
00337         << " Error in determining L1T prescale for HLT path: '" << trigger
00338         << "' with L1T seed: '" << l1tname
00339         << "' using L1GtUtils: error code: " << l1error
00340         << ". (Note: only a single L1T name, not a bit number, is allowed as seed for a proper determination of the L1T prescale!)";
00341       result.first = -1;
00342     }
00343   } else {
00345     std::string dump("'"+hltL1GTSeeds(trigger).at(0).second+"'");
00346     for (unsigned int i=1; i!=nL1GTSeedModules; ++i) {
00347       dump += " * '"+hltL1GTSeeds(trigger).at(i).second+"'";
00348     }
00349     edm::LogError("HLTConfigData")
00350       << " Error in determining L1T prescale for HLT path: '" << trigger
00351       << "' has multiple L1GTSeed modules, " << nL1GTSeedModules
00352       << ", with L1 seeds: " << dump
00353       << ". (Note: at most one L1GTSeed module is allowed for a proper determination of the L1T prescale!)";
00354     result.first = -1;
00355   }
00356 
00357   return result;
00358 }
00359 
00360 const std::vector<std::string> HLTConfigProvider::matched(const std::vector<std::string>& inputs, const std::string& pattern) {
00361   std::vector<std::string> matched;
00362   const boost::regex regexp(edm::glob2reg(pattern));
00363   const unsigned int n(inputs.size());
00364   for (unsigned int i=0; i<n; ++i) {
00365     const std::string& input(inputs[i]);
00366     if (boost::regex_match(input,regexp)) matched.push_back(input);
00367   }
00368   return matched;
00369 }
00370 
00371 const std::string HLTConfigProvider::removeVersion(const std::string& trigger) {
00372   const boost::regex regexp("_v[0-9]+$");
00373   return boost::regex_replace(trigger,regexp,"");
00374 }
00375 
00376 const std::vector<std::string> HLTConfigProvider::restoreVersion(const std::vector<std::string>& inputs, const std::string& trigger) {
00377   return matched(inputs,trigger+"_v[0-9]+$");
00378 }