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