CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
AlCaRecoTriggerBitsRcdRead.cc
Go to the documentation of this file.
1 
18 #include <string>
19 #include <map>
20 //#include <vector>
21 #include <sstream>
22 #include <fstream>
23 
24 // Framework
34 
35 // What I want to read:
38 
39 
41 public:
42  explicit AlCaRecoTriggerBitsRcdRead(const edm::ParameterSet &cfg);
44 
45  virtual void analyze(const edm::Event &evt, const edm::EventSetup &evtSetup) override {}
46  virtual void beginRun(const edm::Run &run, const edm::EventSetup &evtSetup) override;
47  virtual void endJob() override;
48 
49 
50 private:
51  // types
52  enum OutputType {kText, kTwiki, kPython}; //kHtml};
53 
54  // methods
55  OutputType stringToEnum(const std::string &outputType) const;
57  const AlCaRecoTriggerBits &triggerMap) const;
58 
59  // members
65  std::auto_ptr<std::ofstream> output_;
66 };
67 
71 
73  : outputType_(this->stringToEnum(cfg.getUntrackedParameter<std::string>("outputType"))),
74  firstRun_(0), lastRun_(0)
75 {
76  // edm::LogInfo("") << "@SUB=AlCaRecoTriggerBitsRcdRead"
77  // << cfg.getParameter<std::string>("@module_label");
78 
80  switch (outputType_) { // now append suffix
81  case kText: fileName += ".txt"; break;
82  case kPython: fileName += ".py"; break;
83  case kTwiki: fileName += ".twiki"; break;
84  }
85  if (fileName.size()) {
86  output_.reset(new std::ofstream(fileName.c_str()));
87  if (!output_->good()) {
88  edm::LogError("IOproblem") << "Could not open output file " << fileName << ".";
89  output_.reset();
90  }
91  }
92 
93 }
94 
98 {
99  if (outputTypeStr == "text") return kText;
100  if (outputTypeStr == "twiki") return kTwiki;
101  if (outputTypeStr == "python") return kPython;
102  // if (outputTypeStr == "html") return kHtml;
103 
104  throw cms::Exception("BadConfig") << "AlCaRecoTriggerBitsRcdRead: "
105  << "outputType '" << outputTypeStr << "' not known,"
106  << " use 'text', 'twiki' or 'python'\n";
107 
108  return kTwiki; // never reached, to please compiler
109 }
110 
113 {
114  if (watcher_.check(iSetup)) { // new IOV for this run
115  // Print last IOV - if there has already been one:
117 
118  // Get AlCaRecoTriggerBits from EventSetup:
120  iSetup.get<AlCaRecoTriggerBitsRcd>().get(triggerBits);
121  lastTriggerBits_ = *triggerBits; // copy for later use
122  firstRun_ = run.run(); // keep track where it started
123  }
124 
125  lastRun_ = run.run(); // keep track of last visited run
126 }
127 
130 {
131  // Print for very last IOV, not treated yet in beginRun(..):
133 }
134 
138  const AlCaRecoTriggerBits &triggerBits) const
139 {
140  // Get map of strings to concatenated list of names of HLT paths:
141  typedef std::map<std::string, std::string> TriggerMap;
142  const TriggerMap &triggerMap = triggerBits.m_alcarecoToTrig;
143 
144  // Collect output for given run numbers via ostringstream.
145  // Format depends on outputType_ configuration.
146  std::ostringstream output;
147  switch (outputType_) {
148  case kPython:
149  output << " triggerLists = cms.VPSet(\n";
150  // no 'break;'!
151  case kText:
152  output << "#\n# AlCaRecoTriggerBits settings for IOV "
153  << firstRun << "-" << lastRun << ":\n#\n";
154  break;
155  case kTwiki:
156  output << "---+++++ *IOV*: " << firstRun << "-" << lastRun << "\n"
157  << "| *TriggerBits list key* | *HLT paths* |\n";
158  break;
159  }
160 
161  // if (outputType_ == kPython) output << " triggerLists = cms.VPSet(\n";
162 
163  // loop over entries in map
164  for (TriggerMap::const_iterator i = triggerMap.begin(); i != triggerMap.end(); ++i) {
165 
166  if (outputType_ == kPython && i != triggerMap.begin()) output << ",\n";
167 
168  switch (outputType_) {
169  case kPython:
170  output << " cms.PSet(listName = cms.string('" << i->first << "'),\n"
171  << " hltPaths = cms.vstring(";
172  break;
173  case kText:
174  output << "trigger list key: '" << i->first << "'\npaths:\n";
175  break;
176  case kTwiki:
177  output << "| '" << i->first << "' | ";
178  }
179  // We must avoid a map<string,vector<string> > in DB for performance reason,
180  // so the paths are mapped into one string separated by ';':
181  const std::vector<std::string> paths = triggerBits.decompose(i->second);
182  for (unsigned int iPath = 0; iPath < paths.size(); ++iPath) {
183  if (iPath != 0) {
184  output << ", "; // next path
185  switch (outputType_) {
186  case kPython: // only 2 per line
187  case kText: // only 4 per line
188  if (0 == (iPath % (outputType_ == kPython ? 2 : 4))) {
189  output << "\n";
190  if (outputType_ == kPython) output << " ";
191  }
192  break;
193  case kTwiki: // Twiki will handle that
194  break;
195  }
196  }
197  output << "'" << paths[iPath] << "'";
198  }
199  switch (outputType_) {
200  case kPython:
201  output << ")\n )";
202  break;
203  case kText:
204  output << "\n#\n";
205  break;
206  case kTwiki:
207  output << " |\n";
208  }
209  }
210  if (outputType_ == kPython) output << "\n ) # closing of VPSet triggerLists\n";
211 
212  // Final output - either message logger or output file:
213  if (output_.get()) *output_ << output.str();
214  else edm::LogInfo("") << output.str();
215 }
216 
217 
218 
219 //define this as a plug-in
std::map< std::string, std::string > m_alcarecoToTrig
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
edm::ESWatcher< AlCaRecoTriggerBitsRcd > watcher_
RunNumber_t run() const
Definition: RunBase.h:42
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
AlCaRecoTriggerBitsRcdRead(const edm::ParameterSet &cfg)
std::vector< std::string > decompose(const std::string &concatPaths) const
Decompose one value of map from concatenated string.
void printMap(edm::RunNumber_t firstRun, edm::RunNumber_t lastRun, const AlCaRecoTriggerBits &triggerMap) const
string firstRun
Definition: dataset.py:395
virtual void beginRun(const edm::Run &run, const edm::EventSetup &evtSetup) override
std::auto_ptr< std::ofstream > output_
const T & get() const
Definition: EventSetup.h:55
bool check(const edm::EventSetup &iSetup)
Definition: ESWatcher.h:58
virtual void analyze(const edm::Event &evt, const edm::EventSetup &evtSetup) override
OutputType stringToEnum(const std::string &outputType) const
unsigned int RunNumber_t
Definition: EventRange.h:32
Definition: Run.h:41