CMS 3D CMS Logo

AlCaRecoTriggerBitsRcdRead.cc
Go to the documentation of this file.
1 
18 #include <memory>
19 
20 #include <map>
21 #include <string>
22 //#include <vector>
23 #include <sstream>
24 #include <fstream>
25 
26 // Framework
35 
36 // What I want to read:
39 
40 class AlCaRecoTriggerBitsRcdRead : public edm::one::EDAnalyzer<edm::one::WatchRuns> {
41 public:
43  ~AlCaRecoTriggerBitsRcdRead() override = default;
44 
45  void analyze(const edm::Event &evt, const edm::EventSetup &evtSetup) override {}
46  void beginRun(const edm::Run &run, const edm::EventSetup &evtSetup) override;
47  void endRun(edm::Run const &, edm::EventSetup const &) override {}
48  void endJob() override;
49 
50  static void fillDescriptions(edm::ConfigurationDescriptions &descriptions);
51 
52 private:
53  // types
54  enum OutputType { kText, kTwiki, kPython }; //kHtml};
55 
56  // methods
57  OutputType stringToEnum(const std::string &outputType) const;
59 
60  // members
67  std::unique_ptr<std::ofstream> output_;
68 };
69 
73  desc.setComment("Plugin to read payloads of type AlCaRecoTriggerBits");
74  desc.addUntracked<std::string>("rawFileName", "");
75  desc.addUntracked<std::string>("outputType", "twiki");
76  descriptions.addWithDefaultLabel(desc);
77 }
78 
81  : triggerBitsToken_(esConsumes<edm::Transition::BeginRun>()),
82  outputType_(this->stringToEnum(cfg.getUntrackedParameter<std::string>("outputType"))),
83  firstRun_(0),
84  lastRun_(0) {
85  // edm::LogInfo("") << "@SUB=AlCaRecoTriggerBitsRcdRead"
86  // << cfg.getParameter<std::string>("@module_label");
87 
88  std::string fileName(cfg.getUntrackedParameter<std::string>("rawFileName"));
89  switch (outputType_) { // now append suffix
90  case kText:
91  fileName += ".txt";
92  break;
93  case kPython:
94  fileName += ".py";
95  break;
96  case kTwiki:
97  fileName += ".twiki";
98  break;
99  }
100  if (!fileName.empty()) {
101  output_ = std::make_unique<std::ofstream>(fileName.c_str());
102  if (!output_->good()) {
103  edm::LogError("IOproblem") << "Could not open output file " << fileName << ".";
104  output_.reset();
105  }
106  }
107 }
108 
111  if (outputTypeStr == "text")
112  return kText;
113  if (outputTypeStr == "twiki")
114  return kTwiki;
115  if (outputTypeStr == "python")
116  return kPython;
117  // if (outputTypeStr == "html") return kHtml;
118 
119  throw cms::Exception("BadConfig") << "AlCaRecoTriggerBitsRcdRead: "
120  << "outputType '" << outputTypeStr << "' not known,"
121  << " use 'text', 'twiki' or 'python'\n";
122 
123  return kTwiki; // never reached, to please compiler
124 }
125 
128  if (watcher_.check(iSetup)) { // new IOV for this run
129  edm::LogPrint("AlCaRecoTriggerBitsRcdRead") << "new IOV: " << firstRun_ << "-" << lastRun_;
130  // Print last IOV - if there has already been one:
131  if (lastRun_ != 0)
132  this->printMap(firstRun_, lastRun_, lastTriggerBits_);
133 
134  // Get AlCaRecoTriggerBits from EventSetup:
135  const auto &triggerBits = &iSetup.getData(triggerBitsToken_);
136  lastTriggerBits_ = *triggerBits; // copy for later use
137  firstRun_ = run.run(); // keep track where it started
138  }
139 
140  lastRun_ = run.run(); // keep track of last visited run
141 }
142 
145  // Print for very last IOV, not treated yet in beginRun(..):
147 }
148 
152  const AlCaRecoTriggerBits &triggerBits) const {
153  // Get map of strings to concatenated list of names of HLT paths:
154  typedef std::map<std::string, std::string> TriggerMap;
155  const TriggerMap &triggerMap = triggerBits.m_alcarecoToTrig;
156 
157  // Collect output for given run numbers via ostringstream.
158  // Format depends on outputType_ configuration.
159  std::ostringstream output;
160  switch (outputType_) {
161  case kPython:
162  output << " triggerLists = cms.VPSet(\n";
163  [[fallthrough]];
164  case kText:
165  output << "#\n# AlCaRecoTriggerBits settings for IOV " << firstRun << "-" << lastRun << ":\n#\n";
166  break;
167  case kTwiki:
168  output << "---+++++ *IOV*: " << firstRun << "-" << lastRun << "\n"
169  << "| *TriggerBits list key* | *HLT paths* |\n";
170  break;
171  }
172 
173  // if (outputType_ == kPython) output << " triggerLists = cms.VPSet(\n";
174 
175  // loop over entries in map
176  for (TriggerMap::const_iterator i = triggerMap.begin(); i != triggerMap.end(); ++i) {
177  if (outputType_ == kPython && i != triggerMap.begin())
178  output << ",\n";
179 
180  switch (outputType_) {
181  case kPython:
182  output << " cms.PSet(listName = cms.string('" << i->first << "'),\n"
183  << " hltPaths = cms.vstring(";
184  break;
185  case kText:
186  output << "trigger list key: '" << i->first << "'\npaths:\n";
187  break;
188  case kTwiki:
189  output << "| '" << i->first << "' | ";
190  }
191  // We must avoid a map<string,vector<string> > in DB for performance reason,
192  // so the paths are mapped into one string separated by ';':
193  const std::vector<std::string> paths = triggerBits.decompose(i->second);
194  for (unsigned int iPath = 0; iPath < paths.size(); ++iPath) {
195  if (iPath != 0) {
196  output << ", "; // next path
197  switch (outputType_) {
198  case kPython: // only 2 per line
199  case kText: // only 4 per line
200  if (0 == (iPath % (outputType_ == kPython ? 2 : 4))) {
201  output << "\n";
202  if (outputType_ == kPython)
203  output << " ";
204  }
205  break;
206  case kTwiki: // Twiki will handle that
207  break;
208  }
209  }
210  output << "'" << paths[iPath] << "'";
211  }
212  switch (outputType_) {
213  case kPython:
214  output << ")\n )";
215  break;
216  case kText:
217  output << "\n#\n";
218  break;
219  case kTwiki:
220  output << " |\n";
221  }
222  }
223  if (outputType_ == kPython)
224  output << "\n ) # closing of VPSet triggerLists\n";
225 
226  // Final output - either message logger or output file:
227  if (output_.get())
228  *output_ << output.str();
229  else
230  edm::LogInfo("") << output.str();
231 }
232 
233 //define this as a plug-in
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
edm::ESWatcher< AlCaRecoTriggerBitsRcd > watcher_
void endRun(edm::Run const &, edm::EventSetup const &) override
void analyze(const edm::Event &evt, const edm::EventSetup &evtSetup) override
std::map< std::string, std::string > m_alcarecoToTrig
std::vector< std::string > decompose(const std::string &concatPaths) const
Decompose one value of map from concatenated string.
AlCaRecoTriggerBitsRcdRead(const edm::ParameterSet &cfg)
void beginRun(const edm::Run &run, const edm::EventSetup &evtSetup) override
Log< level::Error, false > LogError
~AlCaRecoTriggerBitsRcdRead() override=default
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
Transition
Definition: Transition.h:12
std::unique_ptr< std::ofstream > output_
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
bool getData(T &iHolder) const
Definition: EventSetup.h:122
Log< level::Warning, true > LogPrint
Log< level::Info, false > LogInfo
bool check(const edm::EventSetup &iSetup)
Definition: ESWatcher.h:57
HLT enums.
void printMap(edm::RunNumber_t firstRun, edm::RunNumber_t lastRun, const AlCaRecoTriggerBits &triggerMap) const
const edm::ESGetToken< AlCaRecoTriggerBits, AlCaRecoTriggerBitsRcd > triggerBitsToken_
firstRun
Definition: dataset.py:940
unsigned int RunNumber_t
Definition: Run.h:45
OutputType stringToEnum(const std::string &outputType) const