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 
32 public:
35 
36  void analyze(const edm::Event &evt, const edm::EventSetup &evtSetup) override;
37 
38 private:
39  typedef std::map<std::string, std::string> TriggerMap;
41  bool removeKeysFromMap(const std::vector<std::string> &keys, TriggerMap &triggerMap) const;
42  bool replaceKeysFromMap(const std::vector<edm::ParameterSet> &alcarecoReplace, TriggerMap &triggerMap) const;
43  bool addTriggerLists(const std::vector<edm::ParameterSet> &triggerListsAdd, AlCaRecoTriggerBits &bits) const;
45  void writeBitsToDB(AlCaRecoTriggerBits *bitsToWrite) const;
46 
47  unsigned int nEventCalls_;
48  const unsigned int firstRunIOV_;
49  const int lastRunIOV_;
50  const bool startEmpty_;
51  const std::vector<std::string> listNamesRemove_;
52  const std::vector<edm::ParameterSet> triggerListsAdd_;
53  const std::vector<edm::ParameterSet> alcarecoReplace_;
54 };
55 
59 
61  : nEventCalls_(0),
62  firstRunIOV_(cfg.getParameter<unsigned int>("firstRunIOV")),
63  lastRunIOV_(cfg.getParameter<int>("lastRunIOV")),
64  startEmpty_(cfg.getParameter<bool>("startEmpty")),
65  listNamesRemove_(cfg.getParameter<std::vector<std::string> >("listNamesRemove")),
66  triggerListsAdd_(cfg.getParameter<std::vector<edm::ParameterSet> >("triggerListsAdd")),
67  alcarecoReplace_(cfg.getParameter<std::vector<edm::ParameterSet> >("alcarecoToReplace")) {}
68 
71  if (nEventCalls_++ > 0) { // postfix increment!
72  edm::LogWarning("BadConfig") << "@SUB=analyze"
73  << "Writing to DB to be done only once, set\n"
74  << "'process.maxEvents = cms.untracked.PSet(input = cms.untracked.int32(1))'\n"
75  << " next time. But your writing is fine.)";
76  return;
77  }
78 
79  // create what to write - starting from empty or existing list (auto_ptr?)
80  AlCaRecoTriggerBits *bitsToWrite = this->createStartTriggerBits(startEmpty_, iSetup);
81 
82  // remove some existing entries in map
84 
85  // now add new entries
86  this->addTriggerLists(triggerListsAdd_, *bitsToWrite);
87 
88  // now replace keys
90 
91  // finally write to DB
92  this->writeBitsToDB(bitsToWrite);
93 }
94 
96 AlCaRecoTriggerBits * // auto_ptr?
98  if (startEmpty) {
99  return new AlCaRecoTriggerBits;
100  } else {
102  evtSetup.get<AlCaRecoTriggerBitsRcd>().get(triggerBits);
103  return new AlCaRecoTriggerBits(*triggerBits); // copy old one
104  }
105 }
106 
108 bool AlCaRecoTriggerBitsRcdUpdate::removeKeysFromMap(const std::vector<std::string> &keys,
109  TriggerMap &triggerMap) const {
110  for (std::vector<std::string>::const_iterator iKey = keys.begin(), endKey = keys.end(); iKey != endKey; ++iKey) {
111  if (triggerMap.find(*iKey) != triggerMap.end()) {
112  // remove
113  // edm::LogError("Temp") << "@SUB=removeKeysFromMap" << "Cannot yet remove '" << *iKey
114  // << "' from map.";
115  // FIXME: test next line@
116  triggerMap.erase(*iKey);
117  } else { // not in list ==> misconfiguartion!
118  throw cms::Exception("BadConfig") << "[AlCaRecoTriggerBitsRcdUpdate::removeKeysFromMap] "
119  << "Cannot remove key '" << *iKey << "' since not in "
120  << "list - typo in configuration?\n";
121  return false;
122  }
123  }
124  return true;
125 }
126 
128 bool AlCaRecoTriggerBitsRcdUpdate::replaceKeysFromMap(const std::vector<edm::ParameterSet> &alcarecoReplace,
129  TriggerMap &triggerMap) const {
130  std::vector<std::pair<std::string, std::string> > keyPairs;
131  keyPairs.reserve(alcarecoReplace.size());
132 
133  for (auto &iSet : alcarecoReplace) {
134  const std::string oldKey(iSet.getParameter<std::string>("oldKey"));
135  const std::string newKey(iSet.getParameter<std::string>("newKey"));
136  keyPairs.push_back(std::make_pair(oldKey, newKey));
137  }
138 
139  for (auto &iKey : keyPairs) {
140  if (triggerMap.find(iKey.first) != triggerMap.end()) {
141  std::string bitsToReplace = triggerMap[iKey.first];
142  triggerMap.erase(iKey.first);
143  triggerMap[iKey.second] = bitsToReplace;
144  } else { // not in list ==> misconfiguration!
145  edm::LogWarning("AlCaRecoTriggerBitsRcdUpdate")
146  << "[AlCaRecoTriggerBitsRcdUpdate::replaceKeysFromMap] "
147  << "Cannot replace key '" << iKey.first << "with " << iKey.second << " since not in "
148  << "list - typo in configuration?\n";
149  return false;
150  }
151  }
152  return true;
153 }
154 
156 bool AlCaRecoTriggerBitsRcdUpdate::addTriggerLists(const std::vector<edm::ParameterSet> &triggerListsAdd,
157  AlCaRecoTriggerBits &bits) const {
158  TriggerMap &triggerMap = bits.m_alcarecoToTrig;
159 
160  // loop on PSets, each containing the key (filter name) and a vstring with triggers
161  for (std::vector<edm::ParameterSet>::const_iterator iSet = triggerListsAdd.begin(); iSet != triggerListsAdd.end();
162  ++iSet) {
163  const std::vector<std::string> paths(iSet->getParameter<std::vector<std::string> >("hltPaths"));
164  // We must avoid a map<string,vector<string> > in DB for performance reason,
165  // so we have to merge the paths into one string that will be decoded when needed:
166  const std::string mergedPaths = bits.compose(paths);
167 
168  const std::string filter(iSet->getParameter<std::string>("listName"));
169  if (triggerMap.find(filter) != triggerMap.end()) {
170  throw cms::Exception("BadConfig") << "List name '" << filter << "' already in map, either "
171  << "remove from 'triggerListsAdd' or "
172  << " add to 'listNamesRemove'.\n";
173  }
174  triggerMap[filter] = mergedPaths;
175  }
176 
177  return true;
178 }
179 
182  edm::LogInfo("") << "Uploading to the database...";
183 
185  if (!poolDbService.isAvailable()) {
186  throw cms::Exception("NotAvailable") << "PoolDBOutputService not available.\n";
187  }
188 
189  // ownership of bitsToWrite transferred
190  // FIXME: Have to check that timetype is run number! How?
191  const std::string recordName("AlCaRecoTriggerBitsRcd");
192  if (poolDbService->isNewTagRequest(recordName)) { // tag not yet existing
193  // lastRunIOV_ = -1 means infinity:
194  const cond::Time_t lastRun = (lastRunIOV_ < 0 ? poolDbService->endOfTime() : lastRunIOV_);
195  poolDbService->createNewIOV(bitsToWrite, firstRunIOV_, lastRun, recordName);
196  } else { // tag exists, can only append
197  if (lastRunIOV_ >= 0) {
198  throw cms::Exception("BadConfig") << "Tag already exists, can only append until infinity,"
199  << " but lastRunIOV = " << lastRunIOV_ << ".\n";
200  }
201  poolDbService->appendSinceTime(bitsToWrite, firstRunIOV_, recordName);
202  }
203 
204  edm::LogInfo("") << "...done for runs " << firstRunIOV_ << " to " << lastRunIOV_ << " (< 0 meaning infinity)!";
205 }
206 
207 //define this as a plug-in
AlCaRecoTriggerBits * createStartTriggerBits(bool startEmpty, const edm::EventSetup &evtSetup) const
std::map< std::string, std::string > m_alcarecoToTrig
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)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
void analyze(const edm::Event &evt, const edm::EventSetup &evtSetup) override
unsigned long long Time_t
Definition: Time.h:14
const std::vector< edm::ParameterSet > alcarecoReplace_
bool isNewTagRequest(const std::string &recordName)
bool isAvailable() const
Definition: Service.h:40
void createNewIOV(T *firstPayloadObj, cond::Time_t firstSinceTime, cond::Time_t firstTillTime, const std::string &recordName, bool withlogging=false)
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_
T get() const
Definition: EventSetup.h:73
void writeBitsToDB(AlCaRecoTriggerBits *bitsToWrite) const
Takes over memory uresponsibility for &#39;bitsToWrite&#39;.
bool replaceKeysFromMap(const std::vector< edm::ParameterSet > &alcarecoReplace, TriggerMap &triggerMap) const