CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
AlCaRecoTriggerBitsRcdUpdate.cc
Go to the documentation of this file.
1 
10 #include <string>
11 #include <map>
12 #include <vector>
13 
14 // Framework
21 
22 // Database
24 
25 // What I want to write:
27 // Rcd for reading old one:
29 
31 public:
33  ~AlCaRecoTriggerBitsRcdUpdate() override = default;
34 
35  void analyze(const edm::Event &evt, const edm::EventSetup &evtSetup) override;
36  static void fillDescriptions(edm::ConfigurationDescriptions &descriptions);
37 
38 private:
39  typedef std::map<std::string, std::string> TriggerMap;
40  bool removeKeysFromMap(const std::vector<std::string> &keys, TriggerMap &triggerMap) const;
41  bool replaceKeysFromMap(const std::vector<edm::ParameterSet> &alcarecoReplace, TriggerMap &triggerMap) const;
42  bool addTriggerLists(const std::vector<edm::ParameterSet> &triggerListsAdd, AlCaRecoTriggerBits &bits) const;
43  void writeBitsToDB(const AlCaRecoTriggerBits &bitsToWrite) const;
44 
46  unsigned int nEventCalls_;
47  const unsigned int firstRunIOV_;
48  const int lastRunIOV_;
49  const bool startEmpty_;
50  const std::vector<std::string> listNamesRemove_;
51  const std::vector<edm::ParameterSet> triggerListsAdd_;
52  const std::vector<edm::ParameterSet> alcarecoReplace_;
53 };
54 
58  desc.setComment("Plugin to write payloads of type AlCaRecoTriggerBits");
59  desc.add<unsigned int>("firstRunIOV", 1);
60  desc.add<int>("lastRunIOV", -1);
61  desc.add<bool>("startEmpty", true);
62  desc.add<std::vector<std::string>>("listNamesRemove", {});
63 
64  edm::ParameterSetDescription desc_triggeListsToAdd;
65  desc_triggeListsToAdd.add<std::string>("listName");
66  desc_triggeListsToAdd.add<std::vector<std::string>>("hltPaths");
67  std::vector<edm::ParameterSet> default_triggerListsToAdd;
68  desc.addVPSet("triggerListsAdd", desc_triggeListsToAdd, default_triggerListsToAdd);
69 
70  edm::ParameterSetDescription desc_alcarecoToReplace;
71  desc_alcarecoToReplace.add<std::string>("oldKey");
72  desc_alcarecoToReplace.add<std::string>("newKey");
73  std::vector<edm::ParameterSet> default_alcarecoToReplace;
74  desc.addVPSet("alcarecoToReplace", desc_alcarecoToReplace, default_alcarecoToReplace);
75 
76  descriptions.addWithDefaultLabel(desc);
77 }
78 
81  : triggerBitsToken_(esConsumes()),
82  nEventCalls_(0),
83  firstRunIOV_(cfg.getParameter<unsigned int>("firstRunIOV")),
84  lastRunIOV_(cfg.getParameter<int>("lastRunIOV")),
85  startEmpty_(cfg.getParameter<bool>("startEmpty")),
86  listNamesRemove_(cfg.getParameter<std::vector<std::string>>("listNamesRemove")),
87  triggerListsAdd_(cfg.getParameter<std::vector<edm::ParameterSet>>("triggerListsAdd")),
88  alcarecoReplace_(cfg.getParameter<std::vector<edm::ParameterSet>>("alcarecoToReplace")) {}
89 
92  if (nEventCalls_++ > 0) { // postfix increment!
93  edm::LogWarning("BadConfig") << "@SUB=analyze"
94  << "Writing to DB to be done only once, set\n"
95  << "'process.maxEvents = cms.untracked.PSet(input = cms.untracked.int32(1))'\n"
96  << " next time. But your writing is fine.)";
97  return;
98  }
99 
100  // create what to write - starting from empty or existing list
101  std::unique_ptr<AlCaRecoTriggerBits> bitsToWrite;
102  if (startEmpty_) {
103  bitsToWrite = std::make_unique<AlCaRecoTriggerBits>();
104  } else {
105  bitsToWrite = std::make_unique<AlCaRecoTriggerBits>(iSetup.getData(triggerBitsToken_));
106  }
107 
108  // remove some existing entries in map
109  this->removeKeysFromMap(listNamesRemove_, bitsToWrite->m_alcarecoToTrig);
110 
111  // now add new entries
112  this->addTriggerLists(triggerListsAdd_, *bitsToWrite);
113 
114  // now replace keys
115  this->replaceKeysFromMap(alcarecoReplace_, bitsToWrite->m_alcarecoToTrig);
116 
117  // finally write to DB
118  this->writeBitsToDB(*bitsToWrite);
119 }
120 
122 bool AlCaRecoTriggerBitsRcdUpdate::removeKeysFromMap(const std::vector<std::string> &keys,
123  TriggerMap &triggerMap) const {
124  for (std::vector<std::string>::const_iterator iKey = keys.begin(), endKey = keys.end(); iKey != endKey; ++iKey) {
125  if (triggerMap.find(*iKey) != triggerMap.end()) {
126  // remove
127  // edm::LogError("Temp") << "@SUB=removeKeysFromMap" << "Cannot yet remove '" << *iKey
128  // << "' from map.";
129  // FIXME: test next line@
130  triggerMap.erase(*iKey);
131  } else { // not in list ==> misconfiguartion!
132  throw cms::Exception("BadConfig") << "[AlCaRecoTriggerBitsRcdUpdate::removeKeysFromMap] "
133  << "Cannot remove key '" << *iKey << "' since not in "
134  << "list - typo in configuration?\n";
135  return false;
136  }
137  }
138  return true;
139 }
140 
142 bool AlCaRecoTriggerBitsRcdUpdate::replaceKeysFromMap(const std::vector<edm::ParameterSet> &alcarecoReplace,
143  TriggerMap &triggerMap) const {
144  std::vector<std::pair<std::string, std::string>> keyPairs;
145  keyPairs.reserve(alcarecoReplace.size());
146 
147  for (auto &iSet : alcarecoReplace) {
148  const std::string oldKey(iSet.getParameter<std::string>("oldKey"));
149  const std::string newKey(iSet.getParameter<std::string>("newKey"));
150  keyPairs.push_back(std::make_pair(oldKey, newKey));
151  }
152 
153  for (auto &iKey : keyPairs) {
154  if (triggerMap.find(iKey.first) != triggerMap.end()) {
155  std::string bitsToReplace = triggerMap[iKey.first];
156  triggerMap.erase(iKey.first);
157  triggerMap[iKey.second] = bitsToReplace;
158  } else { // not in list ==> misconfiguration!
159  edm::LogWarning("AlCaRecoTriggerBitsRcdUpdate")
160  << "[AlCaRecoTriggerBitsRcdUpdate::replaceKeysFromMap] "
161  << "Cannot replace key '" << iKey.first << "with " << iKey.second << " since not in "
162  << "list - typo in configuration?\n";
163  return false;
164  }
165  }
166  return true;
167 }
168 
170 bool AlCaRecoTriggerBitsRcdUpdate::addTriggerLists(const std::vector<edm::ParameterSet> &triggerListsAdd,
171  AlCaRecoTriggerBits &bits) const {
172  TriggerMap &triggerMap = bits.m_alcarecoToTrig;
173 
174  // loop on PSets, each containing the key (filter name) and a vstring with triggers
175  for (std::vector<edm::ParameterSet>::const_iterator iSet = triggerListsAdd.begin(); iSet != triggerListsAdd.end();
176  ++iSet) {
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  edm::LogInfo("") << "Uploading to the database...";
197 
199  if (!poolDbService.isAvailable()) {
200  throw cms::Exception("NotAvailable") << "PoolDBOutputService not available.\n";
201  }
202 
203  poolDbService->writeOneIOV(bitsToWrite, firstRunIOV_, "AlCaRecoTriggerBitsRcd");
204 
205  edm::LogInfo("") << "...done for runs " << firstRunIOV_ << " to " << lastRunIOV_ << " (< 0 meaning infinity)!";
206 }
207 
208 //define this as a plug-in
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
tuple cfg
Definition: looper.py:296
const edm::ESGetToken< AlCaRecoTriggerBits, AlCaRecoTriggerBitsRcd > triggerBitsToken_
ParameterDescriptionBase * addVPSet(U const &iLabel, ParameterSetDescription const &validator, std::vector< ParameterSet > const &defaults)
std::map< std::string, std::string > m_alcarecoToTrig
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
bool addTriggerLists(const std::vector< edm::ParameterSet > &triggerListsAdd, AlCaRecoTriggerBits &bits) const
std::map< std::string, std::string > TriggerMap
const std::vector< edm::ParameterSet > triggerListsAdd_
bool getData(T &iHolder) const
Definition: EventSetup.h:122
void setComment(std::string const &value)
void analyze(const edm::Event &evt, const edm::EventSetup &evtSetup) override
const std::vector< edm::ParameterSet > alcarecoReplace_
bool isAvailable() const
Definition: Service.h:40
Hash writeOneIOV(const T &payload, Time_t time, const std::string &recordName)
ParameterDescriptionBase * add(U const &iLabel, T const &value)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
void writeBitsToDB(const AlCaRecoTriggerBits &bitsToWrite) const
~AlCaRecoTriggerBitsRcdUpdate() override=default
Log< level::Info, false > LogInfo
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)
const std::vector< std::string > listNamesRemove_
Log< level::Warning, false > LogWarning
ESGetTokenH3DDVariant esConsumes(std::string const &Reccord, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
bool replaceKeysFromMap(const std::vector< edm::ParameterSet > &alcarecoReplace, TriggerMap &triggerMap) const