CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_7/src/CondTools/HLT/src/AlCaRecoTriggerBitsRcdUpdate.cc

Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 #include <string>
00011 #include <map>
00012 #include <vector>
00013 
00014 // Framework
00015 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00016 #include "FWCore/ServiceRegistry/interface/Service.h"
00017 #include "FWCore/Framework/interface/EventSetup.h"
00018 #include "FWCore/Framework/interface/ESHandle.h"
00019 #include "FWCore/Framework/interface/EDAnalyzer.h"
00020 #include "FWCore/Framework/interface/MakerMacros.h"
00021 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00022 
00023 // Database
00024 #include "CondCore/DBOutputService/interface/PoolDBOutputService.h"
00025 
00026 // What I want to write:
00027 #include "CondFormats/HLTObjects/interface/AlCaRecoTriggerBits.h"
00028 // Rcd for reading old one:
00029 #include "CondFormats/DataRecord/interface/AlCaRecoTriggerBitsRcd.h"
00030 
00031 
00032 class  AlCaRecoTriggerBitsRcdUpdate : public edm::EDAnalyzer {
00033 public:
00034   explicit  AlCaRecoTriggerBitsRcdUpdate(const edm::ParameterSet& cfg);
00035   ~AlCaRecoTriggerBitsRcdUpdate() {}
00036   
00037   virtual void analyze(const edm::Event& evt, const edm::EventSetup& evtSetup);
00038   
00039 private:
00040   typedef std::map<std::string, std::string> TriggerMap;
00041   AlCaRecoTriggerBits* createStartTriggerBits(bool startEmpty, const edm::EventSetup& evtSetup) const;
00042   bool removeKeysFromMap(const std::vector<std::string> &keys, TriggerMap &triggerMap) const;
00043   bool addTriggerLists(const std::vector<edm::ParameterSet> &triggerListsAdd,
00044                        AlCaRecoTriggerBits &bits) const;
00046   void writeBitsToDB(AlCaRecoTriggerBits *bitsToWrite) const;
00047 
00048   unsigned int nEventCalls_;
00049   const unsigned int firstRunIOV_;
00050   const int lastRunIOV_;
00051   const bool startEmpty_; 
00052   const std::vector<std::string> listNamesRemove_;
00053   const std::vector<edm::ParameterSet> triggerListsAdd_;
00054 };
00055 
00059 
00060 AlCaRecoTriggerBitsRcdUpdate::AlCaRecoTriggerBitsRcdUpdate(const edm::ParameterSet& cfg)
00061   : nEventCalls_(0), 
00062     firstRunIOV_(cfg.getParameter<unsigned int>("firstRunIOV")),
00063     lastRunIOV_(cfg.getParameter<int>("lastRunIOV")),
00064     startEmpty_(cfg.getParameter<bool>("startEmpty")),
00065     listNamesRemove_(cfg.getParameter<std::vector<std::string> >("listNamesRemove")),
00066     triggerListsAdd_(cfg.getParameter<std::vector<edm::ParameterSet> >("triggerListsAdd"))
00067 {
00068 }
00069   
00071 void AlCaRecoTriggerBitsRcdUpdate::analyze(const edm::Event& evt, const edm::EventSetup& iSetup)
00072 {
00073   if (nEventCalls_++ > 0) { // postfix increment!
00074     edm::LogWarning("BadConfig")
00075       << "@SUB=analyze" << "Writing to DB to be done only once, set\n"
00076       << "'process.maxEvents = cms.untracked.PSet(input = cms.untracked.int32(1))'\n"
00077       << " next time. But your writing is fine.)";
00078     return;
00079   }
00080 
00081   // create what to write - starting from empty or existing list (auto_ptr?)
00082   AlCaRecoTriggerBits *bitsToWrite = this->createStartTriggerBits(startEmpty_, iSetup);
00083 
00084   // remove some existing entries in map 
00085   this->removeKeysFromMap(listNamesRemove_, bitsToWrite->m_alcarecoToTrig);
00086 
00087   // now add new entries
00088   this->addTriggerLists(triggerListsAdd_, *bitsToWrite);
00089 
00090   // finally write to DB
00091   this->writeBitsToDB(bitsToWrite);
00092 
00093 }
00094 
00096 AlCaRecoTriggerBits*  // auto_ptr?
00097 AlCaRecoTriggerBitsRcdUpdate::createStartTriggerBits(bool startEmpty,
00098                                                      const edm::EventSetup& evtSetup) const
00099 {
00100   if (startEmpty) {
00101     return new AlCaRecoTriggerBits;
00102   } else {
00103     edm::ESHandle<AlCaRecoTriggerBits> triggerBits;
00104     evtSetup.get<AlCaRecoTriggerBitsRcd>().get(triggerBits);
00105     return new AlCaRecoTriggerBits(*triggerBits); // copy old one
00106   }
00107 }
00108 
00110 bool AlCaRecoTriggerBitsRcdUpdate::removeKeysFromMap(const std::vector<std::string> &keys,
00111                                                      TriggerMap &triggerMap) const
00112 {
00113   for (std::vector<std::string>::const_iterator iKey = keys.begin(), endKey = keys.end(); 
00114        iKey != endKey; ++iKey) {
00115     if (triggerMap.find(*iKey) != triggerMap.end()) {
00116       // remove
00117 //      edm::LogError("Temp") << "@SUB=removeKeysFromMap" << "Cannot yet remove '" << *iKey
00118 //                          << "' from map.";
00119 // FIXME: test next line@
00120       triggerMap.erase(*iKey);
00121     } else { // not in list ==> misconfiguartion!
00122       throw cms::Exception("BadConfig") << "[AlCaRecoTriggerBitsRcdUpdate::removeKeysFromMap] "
00123                                         << "Cannot remove key '" << *iKey << "' since not in "
00124                                         << "list - typo in configuration?\n";
00125       return false;
00126     }
00127   }
00128   return true;
00129 }
00130 
00132 bool AlCaRecoTriggerBitsRcdUpdate::addTriggerLists(const std::vector<edm::ParameterSet> &triggerListsAdd,
00133                                                    AlCaRecoTriggerBits &bits) const
00134 {
00135   TriggerMap &triggerMap = bits.m_alcarecoToTrig;
00136 
00137   // loop on PSets, each containing the key (filter name) and a vstring with triggers
00138   for (std::vector<edm::ParameterSet>::const_iterator iSet = triggerListsAdd.begin();
00139        iSet != triggerListsAdd.end(); ++iSet) {
00140     
00141     const std::vector<std::string> paths(iSet->getParameter<std::vector<std::string> >("hltPaths"));
00142     // We must avoid a map<string,vector<string> > in DB for performance reason,
00143     // so we have to merge the paths into one string that will be decoded when needed:
00144     const std::string mergedPaths = bits.compose(paths);
00145     
00146     const std::string filter(iSet->getParameter<std::string>("listName"));
00147     if (triggerMap.find(filter) != triggerMap.end()) {
00148       throw cms::Exception("BadConfig") << "List name '" << filter << "' already in map, either " 
00149                                         << "remove from 'triggerListsAdd' or "
00150                                         << " add to 'listNamesRemove'.\n";
00151     }
00152     triggerMap[filter] = mergedPaths;
00153   }
00154 
00155   return true;
00156 }
00157 
00159 void AlCaRecoTriggerBitsRcdUpdate::writeBitsToDB(AlCaRecoTriggerBits *bitsToWrite) const
00160 {
00161   edm::LogInfo("") << "Uploading to the database...";
00162   
00163   edm::Service<cond::service::PoolDBOutputService> poolDbService;
00164   if (!poolDbService.isAvailable()) {
00165     throw cms::Exception("NotAvailable") << "PoolDBOutputService not available.\n";
00166   }
00167 
00168   // ownership of bitsToWrite transferred
00169   // FIXME: Have to check that timetype is run number! How?
00170   const std::string recordName("AlCaRecoTriggerBitsRcd");
00171   if (poolDbService->isNewTagRequest(recordName)) { // tag not yet existing
00172     // lastRunIOV_ = -1 means infinity:
00173     const cond::Time_t lastRun = (lastRunIOV_ < 0 ? poolDbService->endOfTime() : lastRunIOV_);
00174     poolDbService->createNewIOV(bitsToWrite, firstRunIOV_, lastRun, recordName);
00175   } else { // tag exists, can only append
00176     if (lastRunIOV_ >= 0) {
00177       throw cms::Exception("BadConfig") << "Tag already exists, can only append until infinity,"
00178                                         << " but lastRunIOV = " << lastRunIOV_ << ".\n";
00179     }
00180     poolDbService->appendSinceTime(bitsToWrite, firstRunIOV_, recordName);
00181   }  
00182   
00183   edm::LogInfo("") << "...done for runs " << firstRunIOV_ << " to " << lastRunIOV_ 
00184                    << " (< 0 meaning infinity)!";
00185 }
00186 
00187 //define this as a plug-in
00188 DEFINE_FWK_MODULE(AlCaRecoTriggerBitsRcdUpdate);