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<TH2F> {
91  static TH2F *get(MonitorElement *me) { return me->getTH2F(); }
92  template <typename... Args>
93  static MonitorElement *book(DQMStore::IBooker &iBooker, Args &&...args) {
94  return iBooker.book2D(std::forward<Args>(args)...);
95  }
96  };
97  template <>
98  struct HistoTraits<TH2S> {
99  static TH2S *get(MonitorElement *me) { return me->getTH2S(); }
100  template <typename... Args>
101  static MonitorElement *book(DQMStore::IBooker &iBooker, Args &&...args) {
102  return iBooker.book2S(std::forward<Args>(args)...);
103  }
104  };
105  template <>
106  struct HistoTraits<TH2D> {
107  static TH2D *get(MonitorElement *me) { return me->getTH2D(); }
108  template <typename... Args>
109  static MonitorElement *book(DQMStore::IBooker &iBooker, Args &&...args) {
110  return iBooker.book2DD(std::forward<Args>(args)...);
111  }
112  };
113  template <>
114  struct HistoTraits<TH3F> {
115  static TH3F *get(MonitorElement *me) { return me->getTH3F(); }
116  template <typename... Args>
117  static MonitorElement *book(DQMStore::IBooker &iBooker, Args &&...args) {
118  return iBooker.book3D(std::forward<Args>(args)...);
119  }
120  };
121  template <>
122  struct HistoTraits<TProfile> {
123  static TProfile *get(MonitorElement *me) { return me->getTProfile(); }
124  template <typename... Args>
125  static MonitorElement *book(DQMStore::IBooker &iBooker, Args &&...args) {
126  return iBooker.bookProfile(std::forward<Args>(args)...);
127  }
128  };
129  template <>
130  struct HistoTraits<TProfile2D> {
131  static TProfile2D *get(MonitorElement *me) { return me->getTProfile2D(); }
132  template <typename... Args>
133  static MonitorElement *book(DQMStore::IBooker &iBooker, Args &&...args) {
134  return iBooker.bookProfile2D(std::forward<Args>(args)...);
135  }
136  };
137 
138  // Default to histograms and similar, others are specialized
139  template <typename T>
140  struct AddMonitorElement {
141  template <typename MEtoEDMObject_object, typename RunOrLumi>
142  static MonitorElement *call(DQMStore::IBooker &iBooker,
143  DQMStore::IGetter &iGetter,
144  MEtoEDMObject_object *metoedmobject,
145  const std::string &dir,
146  const std::string &name,
147  const RunOrLumi &runOrLumi) {
148  MonitorElement *me = iGetter.get(dir + "/" + metoedmobject->GetName());
149 
150  if (me) {
151  auto histo = HistoTraits<T>::get(me);
152  assert(histo);
153  TList list;
154  list.Add(metoedmobject);
155  if (histo->Merge(&list) == -1)
156  edm::LogError("EDMtoMEConverter") << "ERROR EDMtoMEConverter::getData(): merge failed for '"
157  << metoedmobject->GetName() << "'" << std::endl;
158  return me;
159  } else {
160  iBooker.setCurrentFolder(dir);
161  return HistoTraits<T>::book(iBooker, metoedmobject->GetName(), metoedmobject);
162  }
163  }
164  };
165 
166  template <>
167  struct AddMonitorElement<double> {
168  template <typename MEtoEDMObject_object, typename RunOrLumi>
169  static MonitorElement *call(DQMStore::IBooker &iBooker,
170  DQMStore::IGetter &iGetter,
171  MEtoEDMObject_object *metoedmobject,
172  const std::string &dir,
173  const std::string &name,
174  const RunOrLumi &runOrLumi) {
175  iBooker.setCurrentFolder(dir);
176  MonitorElement *me = iBooker.bookFloat(name);
177  me->Fill(*metoedmobject);
178  return me;
179  }
180  };
181 
182  // long long and int share some commonalities, which are captured here (we can have only one default template definition)
183  template <typename T>
184  struct AddMonitorElementForIntegers {
185  template <typename MEtoEDMObject_object, typename RunOrLumi>
186  static MonitorElement *call(DQMStore::IBooker &iBooker,
187  DQMStore::IGetter &iGetter,
188  MEtoEDMObject_object *metoedmobject,
189  const std::string &dir,
190  const std::string &name,
191  const RunOrLumi &runOrLumi) {
192  iBooker.setCurrentFolder(dir);
193  iGetter.setCurrentFolder(dir);
194  T ival = getProcessedEvents(iGetter, dir, name, runOrLumi);
195  MonitorElement *me = iBooker.bookInt(name);
196  me->Fill(*metoedmobject + ival);
197  return me;
198  }
199 
200  static T getProcessedEvents(DQMStore::IGetter &iGetter,
201  const std::string &dir,
202  const std::string &name,
203  const edm::Run &) {
204  if (name.find("processedEvents") != std::string::npos) {
205  if (const MonitorElement *me = iGetter.get(dir + "/" + name)) {
206  return me->getIntValue();
207  }
208  }
209  return 0;
210  }
211 
212  static T getProcessedEvents(DQMStore::IGetter &iGetter,
213  const std::string &dir,
214  const std::string &name,
215  const edm::LuminosityBlock &) {
216  return 0;
217  }
218  };
219  template <>
220  struct AddMonitorElement<long long> {
221  template <typename... Args>
222  static MonitorElement *call(Args &&...args) {
223  return AddMonitorElementForIntegers<long long>::call(std::forward<Args>(args)...);
224  }
225  };
226  template <>
227  struct AddMonitorElement<int> {
228  template <typename... Args>
229  static MonitorElement *call(Args &&...args) {
230  return AddMonitorElementForIntegers<int>::call(std::forward<Args>(args)...);
231  }
232  };
233 
234  template <>
235  struct AddMonitorElement<TString> {
236  template <typename MEtoEDMObject_object, typename RunOrLumi>
237  static MonitorElement *call(DQMStore::IBooker &iBooker,
238  DQMStore::IGetter &iGetter,
239  MEtoEDMObject_object *metoedmobject,
240  const std::string &dir,
241  const std::string &name,
242  const RunOrLumi &runOrLumi) {
243  iBooker.setCurrentFolder(dir);
244  std::string scont = metoedmobject->Data();
245  return iBooker.bookString(name, scont);
246  }
247  };
248 
249  // TODO: might need re-scoping to JOB here.
250  void adjustScope(DQMStore::IBooker &ibooker, const edm::Run &, MonitorElementData::Scope reScope) {
251  if (reScope == MonitorElementData::Scope::JOB) {
252  ibooker.setScope(MonitorElementData::Scope::JOB);
253  } else {
254  ibooker.setScope(MonitorElementData::Scope::RUN);
255  }
256  }
257  void adjustScope(DQMStore::IBooker &ibooker, const edm::LuminosityBlock &, MonitorElementData::Scope reScope) {
258  // will be LUMI for no reScoping, else the expected scope.
259  ibooker.setScope(reScope);
260  }
261 
262 } // namespace
263 
265  const edm::InputTag &runInputTag = iPSet.getParameter<edm::InputTag>("runInputTag");
266  const edm::InputTag &lumiInputTag = iPSet.getParameter<edm::InputTag>("lumiInputTag");
268 
269  for_each(tokens_, [&](auto &tok) { tok.set(runInputTag, lumiInputTag, iC); });
270 
271  constexpr char MsgLoggerCat[] = "EDMtoMEConverter_EDMtoMEConverter";
272 
273  // get information from parameter set
274  name = iPSet.getUntrackedParameter<std::string>("Name");
275  verbosity = iPSet.getUntrackedParameter<int>("Verbosity");
276  frequency = iPSet.getUntrackedParameter<int>("Frequency");
277 
278  convertOnEndLumi = iPSet.getUntrackedParameter<bool>("convertOnEndLumi", true);
279  convertOnEndRun = iPSet.getUntrackedParameter<bool>("convertOnEndRun", true);
280 
281  auto scopeDecode = std::map<std::string, MonitorElementData::Scope>{{"", MonitorElementData::Scope::LUMI},
282  {"LUMI", MonitorElementData::Scope::LUMI},
283  {"RUN", MonitorElementData::Scope::RUN},
284  {"JOB", MonitorElementData::Scope::JOB}};
285  reScope = scopeDecode[iPSet.getUntrackedParameter<std::string>("reScope", "")];
286 
287  // use value of first digit to determine default output level (inclusive)
288  // 0 is none, 1 is basic, 2 is fill output, 3 is gather output
289  verbosity %= 10;
290 
291  // print out Parameter Set information being used
292  if (verbosity >= 0) {
293  edm::LogInfo(MsgLoggerCat) << "\n===============================\n"
294  << "Initialized as EDAnalyzer with parameter values:\n"
295  << " Name = " << name << "\n"
296  << " Verbosity = " << verbosity << "\n"
297  << " Frequency = " << frequency << "\n"
298  << "===============================\n";
299  }
300 
301  assert(sizeof(int64_t) == sizeof(long long));
302  usesResource("DQMStore");
303 
304  dqmLumiToken_ = produces<DQMToken, edm::Transition::EndLuminosityBlock>("endLumi");
305  dqmRunToken_ = produces<DQMToken, edm::Transition::EndRun>("endRun");
306 } // end constructor
307 
309 
311  if (convertOnEndRun) {
313  store->meBookerGetter([&](DQMStore::IBooker &b, DQMStore::IGetter &g) { getData(b, g, iRun); });
314  }
315 
316  iRun.put(dqmRunToken_, std::make_unique<DQMToken>());
317 }
318 
320  if (convertOnEndLumi) {
322  store->meBookerGetter([&](DQMStore::IBooker &b, DQMStore::IGetter &g) { getData(b, g, iLumi); });
323  }
324 
325  iLumi.put(dqmLumiToken_, std::make_unique<DQMToken>());
326 }
327 
328 template <class T>
329 void EDMtoMEConverter::getData(DQMStore::IBooker &iBooker, DQMStore::IGetter &iGetter, T &iGetFrom) {
330  constexpr char MsgLoggerCat[] = "EDMtoMEConverter_getData";
331 
332  if (verbosity >= 0)
333  edm::LogInfo(MsgLoggerCat) << "\nRestoring MonitorElements.";
334 
335  for_each(tokens_, [&](const auto &tok) {
336  using Tokens_T = typename std::decay<decltype(tok)>::type;
337  using METype = typename Tokens_T::type;
338  using MEtoEDM_T = typename Tokens_T::Product;
339  edm::Handle<MEtoEDM_T> metoedm;
340  tok.getData(iGetFrom, metoedm);
341  if (!metoedm.isValid()) {
342  //edm::LogWarning(MsgLoggerCat)
343  // << "MEtoEDM<TH1F> doesn't exist in run";
344  return;
345  }
346 
347  std::vector<typename MEtoEDM_T::MEtoEDMObject> metoedmobject = metoedm->getMEtoEdmObject();
348 
349  for (unsigned int i = 0; i < metoedmobject.size(); ++i) {
350  // get full path of monitor element
351  const std::string &pathname = metoedmobject[i].name;
352  if (verbosity > 0)
353  std::cout << pathname << std::endl;
354 
356 
357  // deconstruct path from fullpath
358  StringList fulldir = StringOps::split(pathname, "/");
359  std::string name = *(fulldir.end() - 1);
360 
361  for (unsigned j = 0; j < fulldir.size() - 1; ++j) {
362  dir += fulldir[j];
363  if (j != fulldir.size() - 2)
364  dir += "/";
365  }
366 
367  // define new monitor element
368  adjustScope(iBooker, iGetFrom, reScope);
369  AddMonitorElement<METype>::call(iBooker, iGetter, &metoedmobject[i].object, dir, name, iGetFrom);
370 
371  } // end loop thorugh metoedmobject
372  });
373 }
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:399
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:219
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:322
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:632
MonitorElement * book1S(TString const &name, TString const &title, int nchX, double lowX, double highX, FUNC onbooking=NOOP())
Definition: DQMStore.h:133
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_
bool isValid() const
Definition: HandleBase.h:70
edm::EDPutTokenT< DQMToken > dqmLumiToken_
Log< level::Info, false > LogInfo
std::vector< edm::EDGetTokenT< int > > 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:177
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:261
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:290
tuple size
Write out results.
Definition: Run.h:45