CMS 3D CMS Logo

AlCaRecoTriggerBitsRcdUpdate.cc
Go to the documentation of this file.
1 
10 #include <string>
11 #include <map>
12 #include <vector>
13 
14 // Framework
22 
23 // Database
25 
26 // What I want to write:
28 // Rcd for reading old one:
30 
31 
33 public:
36 
37  virtual void analyze(const edm::Event& evt, const edm::EventSetup& evtSetup) override;
38 
39 private:
40  typedef std::map<std::string, std::string> TriggerMap;
42  bool removeKeysFromMap(const std::vector<std::string> &keys, TriggerMap &triggerMap) const;
43  bool replaceKeysFromMap(const std::vector<edm::ParameterSet> &alcarecoReplace,
44  TriggerMap &triggerMap) const;
45  bool addTriggerLists(const std::vector<edm::ParameterSet> &triggerListsAdd,
46  AlCaRecoTriggerBits &bits) const;
48  void writeBitsToDB(AlCaRecoTriggerBits *bitsToWrite) const;
49 
50  unsigned int nEventCalls_;
51  const unsigned int firstRunIOV_;
52  const int lastRunIOV_;
53  const bool startEmpty_;
54  const std::vector<std::string> listNamesRemove_;
55  const std::vector<edm::ParameterSet> triggerListsAdd_;
56  const std::vector<edm::ParameterSet> alcarecoReplace_;
57 };
58 
62 
64  : nEventCalls_(0),
65  firstRunIOV_(cfg.getParameter<unsigned int>("firstRunIOV")),
66  lastRunIOV_(cfg.getParameter<int>("lastRunIOV")),
67  startEmpty_(cfg.getParameter<bool>("startEmpty")),
68  listNamesRemove_(cfg.getParameter<std::vector<std::string> >("listNamesRemove")),
69  triggerListsAdd_(cfg.getParameter<std::vector<edm::ParameterSet> >("triggerListsAdd")),
70  alcarecoReplace_(cfg.getParameter<std::vector<edm::ParameterSet> >("alcarecoToReplace"))
71 {
72 }
73 
76 {
77  if (nEventCalls_++ > 0) { // postfix increment!
78  edm::LogWarning("BadConfig")
79  << "@SUB=analyze" << "Writing to DB to be done only once, set\n"
80  << "'process.maxEvents = cms.untracked.PSet(input = cms.untracked.int32(1))'\n"
81  << " next time. But your writing is fine.)";
82  return;
83  }
84 
85  // create what to write - starting from empty or existing list (auto_ptr?)
86  AlCaRecoTriggerBits *bitsToWrite = this->createStartTriggerBits(startEmpty_, iSetup);
87 
88  // remove some existing entries in map
90 
91  // now add new entries
92  this->addTriggerLists(triggerListsAdd_, *bitsToWrite);
93 
94  // now replace keys
96 
97  // finally write to DB
98  this->writeBitsToDB(bitsToWrite);
99 
100 }
101 
103 AlCaRecoTriggerBits* // auto_ptr?
105  const edm::EventSetup& evtSetup) const
106 {
107  if (startEmpty) {
108  return new AlCaRecoTriggerBits;
109  } else {
111  evtSetup.get<AlCaRecoTriggerBitsRcd>().get(triggerBits);
112  return new AlCaRecoTriggerBits(*triggerBits); // copy old one
113  }
114 }
115 
117 bool AlCaRecoTriggerBitsRcdUpdate::removeKeysFromMap(const std::vector<std::string> &keys,
118  TriggerMap &triggerMap) const
119 {
120  for (std::vector<std::string>::const_iterator iKey = keys.begin(), endKey = keys.end();
121  iKey != endKey; ++iKey) {
122  if (triggerMap.find(*iKey) != triggerMap.end()) {
123  // remove
124 // edm::LogError("Temp") << "@SUB=removeKeysFromMap" << "Cannot yet remove '" << *iKey
125 // << "' from map.";
126 // FIXME: test next line@
127  triggerMap.erase(*iKey);
128  } else { // not in list ==> misconfiguartion!
129  throw cms::Exception("BadConfig") << "[AlCaRecoTriggerBitsRcdUpdate::removeKeysFromMap] "
130  << "Cannot remove key '" << *iKey << "' since not in "
131  << "list - typo in configuration?\n";
132  return false;
133  }
134  }
135  return true;
136 }
137 
139 bool AlCaRecoTriggerBitsRcdUpdate::replaceKeysFromMap(const std::vector<edm::ParameterSet> &alcarecoReplace,
140  TriggerMap &triggerMap) const
141 {
142 
143  std::vector<std::pair<std::string,std::string> > keyPairs;
144  keyPairs.reserve(alcarecoReplace.size());
145 
146  for(auto &iSet : alcarecoReplace ){
147  const std::string oldKey(iSet.getParameter<std::string>("oldKey"));
148  const std::string newKey(iSet.getParameter<std::string>("newKey"));
149  keyPairs.push_back(std::make_pair(oldKey,newKey));
150  }
151 
152  for(auto& iKey : keyPairs){
153  if(triggerMap.find(iKey.first) != triggerMap.end() ){
154  std::string bitsToReplace = triggerMap[iKey.first];
155  triggerMap.erase(iKey.first);
156  triggerMap[iKey.second] = bitsToReplace;
157  } else { // not in list ==> misconfiguration!
158  edm::LogWarning("AlCaRecoTriggerBitsRcdUpdate") << "[AlCaRecoTriggerBitsRcdUpdate::replaceKeysFromMap] "
159  << "Cannot replace key '" << iKey.first << "with " << iKey.second << " since not in "
160  << "list - typo in configuration?\n";
161  return false;
162  }
163  }
164  return true;
165 }
166 
168 bool AlCaRecoTriggerBitsRcdUpdate::addTriggerLists(const std::vector<edm::ParameterSet> &triggerListsAdd,
169  AlCaRecoTriggerBits &bits) const
170 {
171  TriggerMap &triggerMap = bits.m_alcarecoToTrig;
172 
173  // loop on PSets, each containing the key (filter name) and a vstring with triggers
174  for (std::vector<edm::ParameterSet>::const_iterator iSet = triggerListsAdd.begin();
175  iSet != triggerListsAdd.end(); ++iSet) {
176 
177  const std::vector<std::string> paths(iSet->getParameter<std::vector<std::string> >("hltPaths"));
178  // We must avoid a map<string,vector<string> > in DB for performance reason,
179  // so we have to merge the paths into one string that will be decoded when needed:
180  const std::string mergedPaths = bits.compose(paths);
181 
182  const std::string filter(iSet->getParameter<std::string>("listName"));
183  if (triggerMap.find(filter) != triggerMap.end()) {
184  throw cms::Exception("BadConfig") << "List name '" << filter << "' already in map, either "
185  << "remove from 'triggerListsAdd' or "
186  << " add to 'listNamesRemove'.\n";
187  }
188  triggerMap[filter] = mergedPaths;
189  }
190 
191  return true;
192 }
193 
196 {
197  edm::LogInfo("") << "Uploading to the database...";
198 
200  if (!poolDbService.isAvailable()) {
201  throw cms::Exception("NotAvailable") << "PoolDBOutputService not available.\n";
202  }
203 
204  // ownership of bitsToWrite transferred
205  // FIXME: Have to check that timetype is run number! How?
206  const std::string recordName("AlCaRecoTriggerBitsRcd");
207  if (poolDbService->isNewTagRequest(recordName)) { // tag not yet existing
208  // lastRunIOV_ = -1 means infinity:
209  const cond::Time_t lastRun = (lastRunIOV_ < 0 ? poolDbService->endOfTime() : lastRunIOV_);
210  poolDbService->createNewIOV(bitsToWrite, firstRunIOV_, lastRun, recordName);
211  } else { // tag exists, can only append
212  if (lastRunIOV_ >= 0) {
213  throw cms::Exception("BadConfig") << "Tag already exists, can only append until infinity,"
214  << " but lastRunIOV = " << lastRunIOV_ << ".\n";
215  }
216  poolDbService->appendSinceTime(bitsToWrite, firstRunIOV_, recordName);
217  }
218 
219  edm::LogInfo("") << "...done for runs " << firstRunIOV_ << " to " << lastRunIOV_
220  << " (< 0 meaning infinity)!";
221 }
222 
223 //define this as a plug-in
AlCaRecoTriggerBits * createStartTriggerBits(bool startEmpty, const edm::EventSetup &evtSetup) const
std::map< std::string, std::string > m_alcarecoToTrig
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
bool addTriggerLists(const std::vector< edm::ParameterSet > &triggerListsAdd, AlCaRecoTriggerBits &bits) const
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision bits
std::map< std::string, std::string > TriggerMap
const std::vector< edm::ParameterSet > triggerListsAdd_
void appendSinceTime(T *payloadObj, cond::Time_t sinceTime, const std::string &recordName, bool withlogging=false)
virtual void analyze(const edm::Event &evt, const edm::EventSetup &evtSetup) override
unsigned long long Time_t
Definition: Time.h:16
const std::vector< edm::ParameterSet > alcarecoReplace_
bool isNewTagRequest(const std::string &recordName)
bool isAvailable() const
Definition: Service.h:46
void createNewIOV(T *firstPayloadObj, cond::Time_t firstSinceTime, cond::Time_t firstTillTime, const std::string &recordName, bool withlogging=false)
const T & get() const
Definition: EventSetup.h:55
std::string compose(const std::vector< std::string > &paths) const
Compose several paths into one string :
bool removeKeysFromMap(const std::vector< std::string > &keys, TriggerMap &triggerMap) const
AlCaRecoTriggerBitsRcdUpdate(const edm::ParameterSet &cfg)
HLT enums.
const std::vector< std::string > listNamesRemove_
void writeBitsToDB(AlCaRecoTriggerBits *bitsToWrite) const
Takes over memory uresponsibility for &#39;bitsToWrite&#39;.
bool replaceKeysFromMap(const std::vector< edm::ParameterSet > &alcarecoReplace, TriggerMap &triggerMap) const