CMS 3D CMS Logo

EDMtoMEConverter.cc
Go to the documentation of this file.
1 
8 #include <cassert>
9 
11 
13 
14 using namespace lat;
15 
16 template<typename T>
17 void EDMtoMEConverter::Tokens<T>::set(const edm::InputTag& runInputTag, const edm::InputTag& lumiInputTag, edm::ConsumesCollector& iC) {
18  runToken = iC.mayConsume<MEtoEDM<T>, edm::InRun>(runInputTag);
19  lumiToken = iC.mayConsume<MEtoEDM<T>, edm::InLumi>(lumiInputTag);
20 }
21 
22 template<typename T>
24  iRun.getByToken(runToken, handle);
25 }
26 
27 template<typename T>
29  iLumi.getByToken(lumiToken, handle);
30 }
31 
32 namespace {
33  // general
34  template <size_t I, size_t N>
35  struct ForEachHelper {
36  template <typename Tuple, typename Func>
37  static
38  void call(Tuple&& tpl, Func&& func) {
39  func(std::get<I-1>(tpl));
40  ForEachHelper<I+1, N>::call(std::forward<Tuple>(tpl), std::forward<Func>(func));
41  }
42  };
43  // break recursion
44  template <size_t N>
45  struct ForEachHelper<N, N> {
46  template <typename Tuple, typename Func>
47  static
48  void call(Tuple&& tpl, Func&& func) {
49  func(std::get<N-1>(tpl));
50  }
51  };
52 
53  // helper function to provide nice interface
54  template <typename Tuple, typename Func>
55  void for_each(Tuple&& tpl, Func&& func) {
57  ForEachHelper<1, size>::call(std::forward<Tuple>(tpl), std::forward<Func>(func));
58  }
59 
60 
61  template <typename T> struct HistoTraits;
62  template <> struct HistoTraits<TH1F> {
63  static TH1F *get(MonitorElement *me) { return me->getTH1F(); }
64  template <typename ...Args> static MonitorElement *book(DQMStore *dbe, Args&&... args) {
65  return dbe->book1D(std::forward<Args>(args)...);
66  }
67  };
68  template <> struct HistoTraits<TH1S> {
69  static TH1S *get(MonitorElement *me) { return me->getTH1S(); }
70  template <typename ...Args> static MonitorElement *book(DQMStore *dbe, Args&&... args) {
71  return dbe->book1S(std::forward<Args>(args)...);
72  }
73  };
74  template <> struct HistoTraits<TH1D> {
75  static TH1D *get(MonitorElement *me) { return me->getTH1D(); }
76  template <typename ...Args> static MonitorElement *book(DQMStore *dbe, Args&&... args) {
77  return dbe->book1DD(std::forward<Args>(args)...);
78  }
79  };
80  template <> struct HistoTraits<TH2F> {
81  static TH2F *get(MonitorElement *me) { return me->getTH2F(); }
82  template <typename ...Args> static MonitorElement *book(DQMStore *dbe, Args&&... args) {
83  return dbe->book2D(std::forward<Args>(args)...);
84  }
85  };
86  template <> struct HistoTraits<TH2S> {
87  static TH2S *get(MonitorElement *me) { return me->getTH2S(); }
88  template <typename ...Args> static MonitorElement *book(DQMStore *dbe, Args&&... args) {
89  return dbe->book2S(std::forward<Args>(args)...);
90  }
91  };
92  template <> struct HistoTraits<TH2D> {
93  static TH2D *get(MonitorElement *me) { return me->getTH2D(); }
94  template <typename ...Args> static MonitorElement *book(DQMStore *dbe, Args&&... args) {
95  return dbe->book2DD(std::forward<Args>(args)...);
96  }
97  };
98  template <> struct HistoTraits<TH3F> {
99  static TH3F *get(MonitorElement *me) { return me->getTH3F(); }
100  template <typename ...Args> static MonitorElement *book(DQMStore *dbe, Args&&... args) {
101  return dbe->book3D(std::forward<Args>(args)...);
102  }
103  };
104  template <> struct HistoTraits<TProfile>{
105  static TProfile *get(MonitorElement *me) { return me->getTProfile(); }
106  template <typename ...Args> static MonitorElement *book(DQMStore *dbe, Args&&... args) {
107  return dbe->bookProfile(std::forward<Args>(args)...);
108  }
109  };
110  template <> struct HistoTraits<TProfile2D> {
111  static TProfile2D *get(MonitorElement *me) { return me->getTProfile2D(); }
112  template <typename ...Args> static MonitorElement *book(DQMStore *dbe, Args&&... args) {
113  return dbe->bookProfile2D(std::forward<Args>(args)...);
114  }
115  };
116 
117  // Default to histograms and similar, others are specialized
118  template <typename T>
119  struct AddMonitorElement {
120  template <typename MEtoEDMObject_object, typename RunOrLumi>
121  static
122  MonitorElement *call(DQMStore *dbe, MEtoEDMObject_object *metoedmobject, const std::string& dir, const std::string& name, const RunOrLumi& runOrLumi) {
123  MonitorElement *me = dbe->get(dir+"/"+metoedmobject->GetName());
124  if(me) {
125  auto histo = HistoTraits<T>::get(me);
126  if(histo && me->getTH1()->CanExtendAllAxes()) {
127  TList list;
128  list.Add(metoedmobject);
129  if (histo->Merge(&list) == -1)
130  std::cout << "ERROR EDMtoMEConverter::getData(): merge failed for '"
131  << metoedmobject->GetName() << "'" << std::endl;
132  return me;
133  }
134  }
135 
136  dbe->setCurrentFolder(dir);
137  return HistoTraits<T>::book(dbe, metoedmobject->GetName(), metoedmobject);
138  }
139  };
140 
141  template <>
142  struct AddMonitorElement<double> {
143  template <typename MEtoEDMObject_object, typename RunOrLumi>
144  static
145  MonitorElement *call(DQMStore *dbe, MEtoEDMObject_object *metoedmobject, const std::string& dir, const std::string& name, const RunOrLumi& runOrLumi) {
146  dbe->setCurrentFolder(dir);
147  MonitorElement *me = dbe->bookFloat(name);
148  me->Fill(*metoedmobject);
149  return me;
150  }
151  };
152 
153  // long long and int share some commonalities, which are captured here (we can have only one default template definition)
154  template <typename T>
155  struct AddMonitorElementForIntegers {
156  template <typename MEtoEDMObject_object, typename RunOrLumi>
157  static
158  MonitorElement *call(DQMStore *dbe, MEtoEDMObject_object *metoedmobject, const std::string& dir, const std::string& name, const RunOrLumi& runOrLumi) {
159  dbe->setCurrentFolder(dir);
160  T ival = getProcessedEvents(dbe, dir, name, runOrLumi);
161  MonitorElement *me = dbe->bookInt(name);
162  me->Fill(*metoedmobject + ival);
163  return me;
164  }
165 
166  static
167  T getProcessedEvents(const DQMStore *dbe, const std::string& dir, const std::string& name, const edm::Run&) {
168  if(name.find("processedEvents") != std::string::npos) {
169  if(const MonitorElement *me = dbe->get(dir+"/"+name)) {
170  return me->getIntValue();
171  }
172  }
173  return 0;
174  }
175 
176  static
177  T getProcessedEvents(const DQMStore *dbe, const std::string& dir, const std::string& name, const edm::LuminosityBlock&) {
178  return 0;
179  }
180  };
181  template <>
182  struct AddMonitorElement<long long> {
183  template <typename ...Args>
184  static
185  MonitorElement *call(Args&&... args) {
186  return AddMonitorElementForIntegers<long long>::call(std::forward<Args>(args)...);
187  }
188  };
189  template <>
190  struct AddMonitorElement<int> {
191  template <typename ...Args>
192  static
193  MonitorElement *call(Args&&... args) {
194  return AddMonitorElementForIntegers<int>::call(std::forward<Args>(args)...);
195  }
196  };
197 
198 
199  template <>
200  struct AddMonitorElement<TString> {
201  template <typename MEtoEDMObject_object, typename RunOrLumi>
202  static
203  MonitorElement *call(DQMStore *dbe, MEtoEDMObject_object *metoedmobject, const std::string& dir, const std::string& name, const RunOrLumi& runOrLumi) {
204  dbe->setCurrentFolder(dir);
205  std::string scont = metoedmobject->Data();
206  return dbe->bookString(name, scont);
207  }
208  };
209 
210  void maybeSetLumiFlag(MonitorElement *me, const edm::Run&) {
211  }
212  void maybeSetLumiFlag(MonitorElement *me, const edm::LuminosityBlock&) {
213  me->setLumiFlag();
214  }
215 
216 }
217 
219  verbosity(0), frequency(0)
220 {
221  const edm::InputTag& runInputTag = iPSet.getParameter<edm::InputTag>("runInputTag");
222  const edm::InputTag& lumiInputTag = iPSet.getParameter<edm::InputTag>("lumiInputTag");
224 
225  for_each(tokens_, [&](auto& tok) {
226  tok.set(runInputTag, lumiInputTag, iC);
227  });
228 
229  constexpr char MsgLoggerCat[] = "EDMtoMEConverter_EDMtoMEConverter";
230 
231  // get information from parameter set
232  name = iPSet.getUntrackedParameter<std::string>("Name");
233  verbosity = iPSet.getUntrackedParameter<int>("Verbosity");
234  frequency = iPSet.getUntrackedParameter<int>("Frequency");
235 
236  convertOnEndLumi = iPSet.getUntrackedParameter<bool>("convertOnEndLumi",true);
237  convertOnEndRun = iPSet.getUntrackedParameter<bool>("convertOnEndRun",true);
238 
239  // use value of first digit to determine default output level (inclusive)
240  // 0 is none, 1 is basic, 2 is fill output, 3 is gather output
241  verbosity %= 10;
242 
243  // get dqm info
244  dbe = 0;
246 
247  // print out Parameter Set information being used
248  if (verbosity >= 0) {
249  edm::LogInfo(MsgLoggerCat)
250  << "\n===============================\n"
251  << "Initialized as EDAnalyzer with parameter values:\n"
252  << " Name = " << name << "\n"
253  << " Verbosity = " << verbosity << "\n"
254  << " Frequency = " << frequency << "\n"
255  << "===============================\n";
256  }
257 
258  iCountf = 0;
259  iCount.clear();
260 
261  assert(sizeof(int64_t) == sizeof(long long));
262 
263 } // end constructor
264 
266 
268 {
269 }
270 
272 {
273  constexpr char MsgLoggerCat[] = "EDMtoMEConverter_endJob";
274  if (verbosity >= 0)
275  edm::LogInfo(MsgLoggerCat)
276  << "Terminating having processed " << iCount.size() << " runs across "
277  << iCountf << " files.";
278  return;
279 }
280 
282 {
283  ++iCountf;
284  return;
285 }
286 
287 void EDMtoMEConverter::beginRun(const edm::Run& iRun, const edm::EventSetup& iSetup)
288 {
289  constexpr char MsgLoggerCat[] = "EDMtoMEConverter_beginRun";
290 
291  int nrun = iRun.run();
292 
293  // keep track of number of unique runs processed
294  ++iCount[nrun];
295 
296  if (verbosity > 0) {
297  edm::LogInfo(MsgLoggerCat)
298  << "Processing run " << nrun << " (" << iCount.size() << " runs total)";
299  } else if (verbosity == 0) {
300  if (nrun%frequency == 0 || iCount.size() == 1) {
301  edm::LogInfo(MsgLoggerCat)
302  << "Processing run " << nrun << " (" << iCount.size() << " runs total)";
303  }
304  }
305 }
306 
307 void EDMtoMEConverter::endRun(const edm::Run& iRun, const edm::EventSetup& iSetup)
308 {
309  if (convertOnEndRun) {
310  getData(iRun);
311  }
312 }
313 
315 {
316 }
317 
319 {
320  if (convertOnEndLumi) {
321  getData(iLumi);
322  }
323 }
324 
326 {
327 }
328 
329 template <class T>
330 void
332 {
333  constexpr char MsgLoggerCat[] = "EDMtoMEConverter_getData";
334 
335  if (verbosity >= 0)
336  edm::LogInfo (MsgLoggerCat) << "\nRestoring MonitorElements.";
337 
338  for_each(tokens_, [&](const auto& tok) {
339  using Tokens_T = typename std::decay<decltype(tok)>::type;
340  using METype = typename Tokens_T::type;
341  using MEtoEDM_T = typename Tokens_T::Product;
342  edm::Handle<MEtoEDM_T> metoedm;
343  tok.getData(iGetFrom, metoedm);
344  if(!metoedm.isValid()) {
345  //edm::LogWarning(MsgLoggerCat)
346  // << "MEtoEDM<TH1F> doesn't exist in run";
347  return;
348  }
349 
350  std::vector<typename MEtoEDM_T::MEtoEDMObject> metoedmobject =
351  metoedm->getMEtoEdmObject();
352 
353  for (unsigned int i = 0; i < metoedmobject.size(); ++i) {
354  // get full path of monitor element
355  const std::string& pathname = metoedmobject[i].name;
356  if (verbosity > 0) std::cout << pathname << std::endl;
357 
359 
360  // deconstruct path from fullpath
361  StringList fulldir = StringOps::split(pathname,"/");
362  std::string name = *(fulldir.end() - 1);
363 
364  for (unsigned j = 0; j < fulldir.size() - 1; ++j) {
365  dir += fulldir[j];
366  if (j != fulldir.size() - 2) dir += "/";
367  }
368 
369  // define new monitor element
370  MonitorElement *me = AddMonitorElement<METype>::call(dbe, &metoedmobject[i].object, dir, name, iGetFrom);
371  maybeSetLumiFlag(me, iGetFrom);
372 
373  // attach taglist
374  for(const auto& tag: metoedmobject[i].tags) {
375  dbe->tag(me->getFullname(), tag);
376  }
377  } // end loop thorugh metoedmobject
378  });
379 
380  // verify tags stored properly
381  if (verbosity > 0) {
382  std::vector<std::string> stags;
383  dbe->getAllTags(stags);
384  for (unsigned int i = 0; i < stags.size(); ++i) {
385  std::cout << "Tags: " << stags[i] << std::endl;
386  }
387  }
388 }
389 
size
Write out results.
virtual void analyze(const edm::Event &, const edm::EventSetup &)
type
Definition: HCALResponse.h:21
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
RunNumber_t run() const
Definition: RunBase.h:40
EDGetTokenT< ProductType > mayConsume(edm::InputTag const &tag)
bool getByToken(EDGetToken token, Handle< PROD > &result) const
EDMtoMEConverter(const edm::ParameterSet &)
virtual void endRun(const edm::Run &, const edm::EventSetup &)
Definition: DQMStore.h:25
#define constexpr
void Fill(long long x)
std::tuple< Tokens< TH1F >, Tokens< TH1S >, Tokens< TH1D >, Tokens< TH2F >, Tokens< TH2S >, Tokens< TH2D >, Tokens< TH3F >, Tokens< TProfile >, Tokens< TProfile2D >, Tokens< double >, Tokens< int >, Tokens< long long >, Tokens< TString > > tokens_
int iEvent
Definition: GenABIO.cc:230
virtual void beginRun(const edm::Run &, const edm::EventSetup &)
virtual void endLuminosityBlock(const edm::LuminosityBlock &, const edm::EventSetup &)
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Run.h:240
ConsumesCollector consumesCollector()
Use a ConsumesCollector to gather consumes information from helper functions.
virtual void endJob()
virtual void beginJob()
TH1 * getTH1(void) const
bool isValid() const
Definition: HandleBase.h:74
const std::string getFullname(void) const
get full name of ME including Pathname
void set(const edm::InputTag &runInputTag, const edm::InputTag &lumiInputTag, edm::ConsumesCollector &iC)
virtual void respondToOpenInputFile(const edm::FileBlock &)
int64_t getIntValue(void) const
virtual void beginLuminosityBlock(const edm::LuminosityBlock &, const edm::EventSetup &)
virtual ~EDMtoMEConverter()
dbl *** dir
Definition: mlp_gen.cc:35
void getData(const edm::Run &iRun, edm::Handle< Product > &handle) const
void setLumiFlag(void)
this ME is meant to be stored for each luminosity section
long double T
unsigned int iCountf
double split
Definition: MVATrainer.cc:139
void getData(T &iGetFrom)
T get(const Candidate &c)
Definition: component.h:55
Definition: Run.h:42
std::map< int, int > iCount
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 and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger list("!*","!HLTx*"if it matches 2 triggers or more) will accept the event if all the matching triggers are FAIL.It will reject the event if any of the triggers are PASS or EXCEPTION(this matches the behavior of"!*"before the partial wildcard feature was incorporated).Triggers which are in the READY state are completely ignored.(READY should never be returned since the trigger paths have been run