CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
EDMtoMEConverter.cc
Go to the documentation of this file.
1 
8 #include <cassert>
9 
11 
14 
15 using namespace lat;
18 
19 template <typename T>
21  const edm::InputTag &lumiInputTag,
23  runToken = iC.mayConsume<MEtoEDM<T>, edm::InRun>(runInputTag);
24  lumiToken = iC.mayConsume<MEtoEDM<T>, edm::InLumi>(lumiInputTag);
25 }
26 
27 template <typename T>
29  iRun.getByToken(runToken, handle);
30 }
31 
32 template <typename T>
34  iLumi.getByToken(lumiToken, handle);
35 }
36 
37 namespace {
38  // general
39  template <size_t I, size_t N>
40  struct ForEachHelper {
41  template <typename Tuple, typename Func>
42  static void call(Tuple &&tpl, Func &&func) {
43  func(std::get<I - 1>(tpl));
44  ForEachHelper<I + 1, N>::call(std::forward<Tuple>(tpl), std::forward<Func>(func));
45  }
46  };
47  // break recursion
48  template <size_t N>
49  struct ForEachHelper<N, N> {
50  template <typename Tuple, typename Func>
51  static void call(Tuple &&tpl, Func &&func) {
52  func(std::get<N - 1>(tpl));
53  }
54  };
55 
56  // helper function to provide nice interface
57  template <typename Tuple, typename Func>
58  void for_each(Tuple &&tpl, Func &&func) {
60  ForEachHelper<1, size>::call(std::forward<Tuple>(tpl), std::forward<Func>(func));
61  }
62 
63  template <typename T>
64  struct HistoTraits;
65  template <>
66  struct HistoTraits<TH1F> {
67  static TH1F *get(MonitorElement *me) { return me->getTH1F(); }
68  template <typename... Args>
69  static MonitorElement *book(DQMStore::IBooker &iBooker, Args &&...args) {
70  return iBooker.book1D(std::forward<Args>(args)...);
71  }
72  };
73  template <>
74  struct HistoTraits<TH1S> {
75  static TH1S *get(MonitorElement *me) { return me->getTH1S(); }
76  template <typename... Args>
77  static MonitorElement *book(DQMStore::IBooker &iBooker, Args &&...args) {
78  return iBooker.book1S(std::forward<Args>(args)...);
79  }
80  };
81  template <>
82  struct HistoTraits<TH1D> {
83  static TH1D *get(MonitorElement *me) { return me->getTH1D(); }
84  template <typename... Args>
85  static MonitorElement *book(DQMStore::IBooker &iBooker, Args &&...args) {
86  return iBooker.book1DD(std::forward<Args>(args)...);
87  }
88  };
89  template <>
90  struct HistoTraits<TH1I> {
91  static TH1I *get(MonitorElement *me) { return me->getTH1I(); }
92  template <typename... Args>
93  static MonitorElement *book(DQMStore::IBooker &iBooker, Args &&...args) {
94  return iBooker.book1I(std::forward<Args>(args)...);
95  }
96  };
97  template <>
98  struct HistoTraits<TH2F> {
99  static TH2F *get(MonitorElement *me) { return me->getTH2F(); }
100  template <typename... Args>
101  static MonitorElement *book(DQMStore::IBooker &iBooker, Args &&...args) {
102  return iBooker.book2D(std::forward<Args>(args)...);
103  }
104  };
105  template <>
106  struct HistoTraits<TH2S> {
107  static TH2S *get(MonitorElement *me) { return me->getTH2S(); }
108  template <typename... Args>
109  static MonitorElement *book(DQMStore::IBooker &iBooker, Args &&...args) {
110  return iBooker.book2S(std::forward<Args>(args)...);
111  }
112  };
113  template <>
114  struct HistoTraits<TH2D> {
115  static TH2D *get(MonitorElement *me) { return me->getTH2D(); }
116  template <typename... Args>
117  static MonitorElement *book(DQMStore::IBooker &iBooker, Args &&...args) {
118  return iBooker.book2DD(std::forward<Args>(args)...);
119  }
120  };
121  template <>
122  struct HistoTraits<TH2I> {
123  static TH2I *get(MonitorElement *me) { return me->getTH2I(); }
124  template <typename... Args>
125  static MonitorElement *book(DQMStore::IBooker &iBooker, Args &&...args) {
126  return iBooker.book2I(std::forward<Args>(args)...);
127  }
128  };
129  template <>
130  struct HistoTraits<TH3F> {
131  static TH3F *get(MonitorElement *me) { return me->getTH3F(); }
132  template <typename... Args>
133  static MonitorElement *book(DQMStore::IBooker &iBooker, Args &&...args) {
134  return iBooker.book3D(std::forward<Args>(args)...);
135  }
136  };
137  template <>
138  struct HistoTraits<TProfile> {
139  static TProfile *get(MonitorElement *me) { return me->getTProfile(); }
140  template <typename... Args>
141  static MonitorElement *book(DQMStore::IBooker &iBooker, Args &&...args) {
142  return iBooker.bookProfile(std::forward<Args>(args)...);
143  }
144  };
145  template <>
146  struct HistoTraits<TProfile2D> {
147  static TProfile2D *get(MonitorElement *me) { return me->getTProfile2D(); }
148  template <typename... Args>
149  static MonitorElement *book(DQMStore::IBooker &iBooker, Args &&...args) {
150  return iBooker.bookProfile2D(std::forward<Args>(args)...);
151  }
152  };
153 
154  // Default to histograms and similar, others are specialized
155  template <typename T>
156  struct AddMonitorElement {
157  template <typename MEtoEDMObject_object, typename RunOrLumi>
158  static MonitorElement *call(DQMStore::IBooker &iBooker,
159  DQMStore::IGetter &iGetter,
160  MEtoEDMObject_object *metoedmobject,
161  const std::string &dir,
162  const std::string &name,
163  const RunOrLumi &runOrLumi) {
164  MonitorElement *me = iGetter.get(dir + "/" + metoedmobject->GetName());
165 
166  if (me) {
167  auto histo = HistoTraits<T>::get(me);
168  assert(histo);
169  TList list;
170  list.Add(metoedmobject);
171  if (histo->Merge(&list) == -1)
172  edm::LogError("EDMtoMEConverter") << "ERROR EDMtoMEConverter::getData(): merge failed for '"
173  << metoedmobject->GetName() << "'" << std::endl;
174  return me;
175  } else {
176  iBooker.setCurrentFolder(dir);
177  return HistoTraits<T>::book(iBooker, metoedmobject->GetName(), metoedmobject);
178  }
179  }
180  };
181 
182  template <>
183  struct AddMonitorElement<double> {
184  template <typename MEtoEDMObject_object, typename RunOrLumi>
185  static MonitorElement *call(DQMStore::IBooker &iBooker,
186  DQMStore::IGetter &iGetter,
187  MEtoEDMObject_object *metoedmobject,
188  const std::string &dir,
189  const std::string &name,
190  const RunOrLumi &runOrLumi) {
191  iBooker.setCurrentFolder(dir);
192  MonitorElement *me = iBooker.bookFloat(name);
193  me->Fill(*metoedmobject);
194  return me;
195  }
196  };
197 
198  // long long and int share some commonalities, which are captured here (we can have only one default template definition)
199  template <typename T>
200  struct AddMonitorElementForIntegers {
201  template <typename MEtoEDMObject_object, typename RunOrLumi>
202  static MonitorElement *call(DQMStore::IBooker &iBooker,
203  DQMStore::IGetter &iGetter,
204  MEtoEDMObject_object *metoedmobject,
205  const std::string &dir,
206  const std::string &name,
207  const RunOrLumi &runOrLumi) {
208  iBooker.setCurrentFolder(dir);
209  iGetter.setCurrentFolder(dir);
210  T ival = getProcessedEvents(iGetter, dir, name, runOrLumi);
211  MonitorElement *me = iBooker.bookInt(name);
212  me->Fill(*metoedmobject + ival);
213  return me;
214  }
215 
216  static T getProcessedEvents(DQMStore::IGetter &iGetter,
217  const std::string &dir,
218  const std::string &name,
219  const edm::Run &) {
220  if (name.find("processedEvents") != std::string::npos) {
221  if (const MonitorElement *me = iGetter.get(dir + "/" + name)) {
222  return me->getIntValue();
223  }
224  }
225  return 0;
226  }
227 
228  static T getProcessedEvents(DQMStore::IGetter &iGetter,
229  const std::string &dir,
230  const std::string &name,
231  const edm::LuminosityBlock &) {
232  return 0;
233  }
234  };
235  template <>
236  struct AddMonitorElement<long long> {
237  template <typename... Args>
238  static MonitorElement *call(Args &&...args) {
239  return AddMonitorElementForIntegers<long long>::call(std::forward<Args>(args)...);
240  }
241  };
242  template <>
243  struct AddMonitorElement<int> {
244  template <typename... Args>
245  static MonitorElement *call(Args &&...args) {
246  return AddMonitorElementForIntegers<int>::call(std::forward<Args>(args)...);
247  }
248  };
249 
250  template <>
251  struct AddMonitorElement<TString> {
252  template <typename MEtoEDMObject_object, typename RunOrLumi>
253  static MonitorElement *call(DQMStore::IBooker &iBooker,
254  DQMStore::IGetter &iGetter,
255  MEtoEDMObject_object *metoedmobject,
256  const std::string &dir,
257  const std::string &name,
258  const RunOrLumi &runOrLumi) {
259  iBooker.setCurrentFolder(dir);
260  std::string scont = metoedmobject->Data();
261  return iBooker.bookString(name, scont);
262  }
263  };
264 
265  // TODO: might need re-scoping to JOB here.
266  void adjustScope(DQMStore::IBooker &ibooker, const edm::Run &, MonitorElementData::Scope reScope) {
267  if (reScope == MonitorElementData::Scope::JOB) {
268  ibooker.setScope(MonitorElementData::Scope::JOB);
269  } else {
270  ibooker.setScope(MonitorElementData::Scope::RUN);
271  }
272  }
273  void adjustScope(DQMStore::IBooker &ibooker, const edm::LuminosityBlock &, MonitorElementData::Scope reScope) {
274  // will be LUMI for no reScoping, else the expected scope.
275  ibooker.setScope(reScope);
276  }
277 
278 } // namespace
279 
281  const edm::InputTag &runInputTag = iPSet.getParameter<edm::InputTag>("runInputTag");
282  const edm::InputTag &lumiInputTag = iPSet.getParameter<edm::InputTag>("lumiInputTag");
284 
285  for_each(tokens_, [&](auto &tok) { tok.set(runInputTag, lumiInputTag, iC); });
286 
287  constexpr char MsgLoggerCat[] = "EDMtoMEConverter_EDMtoMEConverter";
288 
289  // get information from parameter set
290  name = iPSet.getUntrackedParameter<std::string>("Name");
291  verbosity = iPSet.getUntrackedParameter<int>("Verbosity");
292  frequency = iPSet.getUntrackedParameter<int>("Frequency");
293 
294  convertOnEndLumi = iPSet.getUntrackedParameter<bool>("convertOnEndLumi", true);
295  convertOnEndRun = iPSet.getUntrackedParameter<bool>("convertOnEndRun", true);
296 
297  auto scopeDecode = std::map<std::string, MonitorElementData::Scope>{{"", MonitorElementData::Scope::LUMI},
298  {"LUMI", MonitorElementData::Scope::LUMI},
299  {"RUN", MonitorElementData::Scope::RUN},
300  {"JOB", MonitorElementData::Scope::JOB}};
301  reScope = scopeDecode[iPSet.getUntrackedParameter<std::string>("reScope", "")];
302 
303  // use value of first digit to determine default output level (inclusive)
304  // 0 is none, 1 is basic, 2 is fill output, 3 is gather output
305  verbosity %= 10;
306 
307  // print out Parameter Set information being used
308  if (verbosity >= 0) {
309  edm::LogInfo(MsgLoggerCat) << "\n===============================\n"
310  << "Initialized as EDAnalyzer with parameter values:\n"
311  << " Name = " << name << "\n"
312  << " Verbosity = " << verbosity << "\n"
313  << " Frequency = " << frequency << "\n"
314  << "===============================\n";
315  }
316 
317  assert(sizeof(int64_t) == sizeof(long long));
318  usesResource("DQMStore");
319 
320  dqmLumiToken_ = produces<DQMToken, edm::Transition::EndLuminosityBlock>("endLumi");
321  dqmRunToken_ = produces<DQMToken, edm::Transition::EndRun>("endRun");
322 } // end constructor
323 
325 
327  if (convertOnEndRun) {
329  store->meBookerGetter([&](DQMStore::IBooker &b, DQMStore::IGetter &g) { getData(b, g, iRun); });
330  }
331 
332  iRun.put(dqmRunToken_, std::make_unique<DQMToken>());
333 }
334 
336  if (convertOnEndLumi) {
338  store->meBookerGetter([&](DQMStore::IBooker &b, DQMStore::IGetter &g) { getData(b, g, iLumi); });
339  }
340 
341  iLumi.put(dqmLumiToken_, std::make_unique<DQMToken>());
342 }
343 
344 template <class T>
345 void EDMtoMEConverter::getData(DQMStore::IBooker &iBooker, DQMStore::IGetter &iGetter, T &iGetFrom) {
346  constexpr char MsgLoggerCat[] = "EDMtoMEConverter_getData";
347 
348  if (verbosity >= 0)
349  edm::LogInfo(MsgLoggerCat) << "\nRestoring MonitorElements.";
350 
351  for_each(tokens_, [&](const auto &tok) {
352  using Tokens_T = typename std::decay<decltype(tok)>::type;
353  using METype = typename Tokens_T::type;
354  using MEtoEDM_T = typename Tokens_T::Product;
355  edm::Handle<MEtoEDM_T> metoedm;
356  tok.getData(iGetFrom, metoedm);
357  if (!metoedm.isValid()) {
358  //edm::LogWarning(MsgLoggerCat)
359  // << "MEtoEDM<TH1F> doesn't exist in run";
360  return;
361  }
362 
363  std::vector<typename MEtoEDM_T::MEtoEDMObject> metoedmobject = metoedm->getMEtoEdmObject();
364 
365  for (unsigned int i = 0; i < metoedmobject.size(); ++i) {
366  // get full path of monitor element
367  const std::string &pathname = metoedmobject[i].name;
368  if (verbosity > 0)
369  std::cout << pathname << std::endl;
370 
372 
373  // deconstruct path from fullpath
374  StringList fulldir = StringOps::split(pathname, "/");
375  std::string name = *(fulldir.end() - 1);
376 
377  for (unsigned j = 0; j < fulldir.size() - 1; ++j) {
378  dir += fulldir[j];
379  if (j != fulldir.size() - 2)
380  dir += "/";
381  }
382 
383  // define new monitor element
384  adjustScope(iBooker, iGetFrom, reScope);
385  AddMonitorElement<METype>::call(iBooker, iGetter, &metoedmobject[i].object, dir, name, iGetFrom);
386 
387  } // end loop thorugh metoedmobject
388  });
389 }
T getUntrackedParameter(std::string const &, T const &) const
MonitorElement * bookFloat(TString const &name, FUNC onbooking=NOOP())
Definition: DQMStore.h:80
MonitorElement * bookProfile2D(TString const &name, TString const &title, int nchX, double lowX, double highX, int nchY, double lowY, double highY, double lowZ, double highZ, char const *option="s", FUNC onbooking=NOOP())
Definition: DQMStore.h:476
MonitorElement * book1I(TString const &name, TString const &title, int const nchX, double const lowX, double const highX, FUNC onbooking=NOOP())
Definition: DQMStore.h:177
MonitorElement * book2S(TString const &name, TString const &title, int nchX, double lowX, double highX, int nchY, double lowY, double highY, FUNC onbooking=NOOP())
Definition: DQMStore.h:254
EDGetTokenT< ProductType > mayConsume(edm::InputTag const &tag)
virtual void setCurrentFolder(std::string const &fullpath)
Definition: DQMStore.cc:32
void endLuminosityBlockProduce(edm::LuminosityBlock &, edm::EventSetup const &) override
bool getByToken(EDGetToken token, Handle< PROD > &result) const
EDMtoMEConverter(const edm::ParameterSet &)
virtual MonitorElementData::Scope setScope(MonitorElementData::Scope newscope)
Definition: DQMStore.cc:46
virtual int64_t getIntValue() const
uint32_t T const *__restrict__ uint32_t const *__restrict__ int32_t int Histo::index_type cudaStream_t Func __host__ __device__ V int Func func
assert(be >=bs)
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e g
Definition: Activities.doc:4
MonitorElement * bookString(TString const &name, TString const &value, FUNC onbooking=NOOP())
Definition: DQMStore.h:87
~EDMtoMEConverter() override
dqm::reco::DQMStore DQMStore
void Fill(long long x)
void endRunProduce(edm::Run &run, edm::EventSetup const &setup) override
MonitorElement * book1DD(TString const &name, TString const &title, int nchX, double lowX, double highX, FUNC onbooking=NOOP())
Definition: DQMStore.h:155
MonitorElement * bookProfile(TString const &name, TString const &title, int nchX, double lowX, double highX, int, double lowY, double highY, char const *option="s", FUNC onbooking=NOOP())
Definition: DQMStore.h:399
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Run.h:318
ConsumesCollector consumesCollector()
Use a ConsumesCollector to gather consumes information from helper functions.
void put(std::unique_ptr< PROD > product)
Put a new product.
edm::EDPutTokenT< DQMToken > dqmRunToken_
virtual MonitorElement * get(std::string const &fullpath) const
Definition: DQMStore.cc:673
tuple handle
Definition: patZpeak.py:25
void meBookerGetter(iFunc f)
Definition: DQMStore.h:709
MonitorElement * book1S(TString const &name, TString const &title, int nchX, double lowX, double highX, FUNC onbooking=NOOP())
Definition: DQMStore.h:133
bool isValid() const
Definition: HandleBase.h:70
edm::EDPutTokenT< DQMToken > dqmLumiToken_
Log< level::Info, false > LogInfo
std::vector< edm::EDGetTokenT< int > > tokens_
std::tuple< Tokens< TH1F >, Tokens< TH1S >, Tokens< TH1D >, Tokens< TH1I >, Tokens< TH2F >, Tokens< TH2S >, Tokens< TH2D >, Tokens< TH2I >, Tokens< TH3F >, Tokens< TProfile >, Tokens< TProfile2D >, Tokens< double >, Tokens< int >, Tokens< long long >, Tokens< TString > > tokens_
#define N
Definition: blowfish.cc:9
dqm::legacy::MonitorElement MonitorElement
void set(const edm::InputTag &runInputTag, const edm::InputTag &lumiInputTag, edm::ConsumesCollector &iC)
MonitorElement * book2D(TString const &name, TString const &title, int nchX, double lowX, double highX, int nchY, double lowY, double highY, FUNC onbooking=NOOP())
Definition: DQMStore.h:212
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
MonitorElement * bookInt(TString const &name, FUNC onbooking=NOOP())
Definition: DQMStore.h:73
double b
Definition: hdecay.h:118
void put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Run.h:109
MonitorElement * book2DD(TString const &name, TString const &title, int nchX, double lowX, double highX, int nchY, double lowY, double highY, FUNC onbooking=NOOP())
Definition: DQMStore.h:338
tuple cout
Definition: gather_cfg.py:144
#define get
void getData(DQMStore::IBooker &iBooker, DQMStore::IGetter &iGetter, T &iGetFrom)
void getData(const edm::Run &iRun, edm::Handle< Product > &handle) const
MonitorElement * book1D(TString const &name, TString const &title, int const nchX, double const lowX, double const highX, FUNC onbooking=NOOP())
Definition: DQMStore.h:98
long double T
MonitorElement * book3D(TString const &name, TString const &title, int nchX, double lowX, double highX, int nchY, double lowY, double highY, int nchZ, double lowZ, double highZ, FUNC onbooking=NOOP())
Definition: DQMStore.h:367
MonitorElement * book2I(TString const &name, TString const &title, int nchX, double lowX, double highX, int nchY, double lowY, double highY, FUNC onbooking=NOOP())
Definition: DQMStore.h:296
tuple size
Write out results.
Definition: Run.h:45