CMS 3D CMS Logo

SimpleFlatTableProducer.h
Go to the documentation of this file.
14 
17 
18 #include <memory>
19 #include <vector>
20 
21 // Base class for dumped variables
22 class VariableBase {
23 public:
25  : name_(aname),
26  doc_(cfg.getParameter<std::string>("doc")),
27  precision_(cfg.existsAs<int>("precision") ? cfg.getParameter<int>("precision")
28  : (cfg.existsAs<std::string>("precision") ? -2 : -1)) {}
29  virtual ~VariableBase() {}
30  const std::string &name() const { return name_; }
31 
32 protected:
35 };
36 
37 // Object member variables and methods
38 template <typename ObjType>
39 class Variable : public VariableBase {
40 public:
41  Variable(const std::string &aname, const edm::ParameterSet &cfg) : VariableBase(aname, cfg) {}
42  virtual void fill(std::vector<const ObjType *> &selobjs, nanoaod::FlatTable &out) const = 0;
43 };
44 
45 template <typename ObjType, typename StringFunctor, typename ValType>
46 class FuncVariable : public Variable<ObjType> {
47 public:
49  : Variable<ObjType>(aname, cfg),
50  func_(cfg.getParameter<std::string>("expr"), cfg.getUntrackedParameter<bool>("lazyEval")),
51  precisionFunc_(cfg.existsAs<std::string>("precision") ? cfg.getParameter<std::string>("precision") : "23",
52  cfg.getUntrackedParameter<bool>("lazyEval")) {}
53  ~FuncVariable() override {}
54 
55  void fill(std::vector<const ObjType *> &selobjs, nanoaod::FlatTable &out) const override {
56  std::vector<ValType> vals(selobjs.size());
57  for (unsigned int i = 0, n = vals.size(); i < n; ++i) {
58  vals[i] = func_(*selobjs[i]);
59  if constexpr (std::is_same<ValType, float>()) {
60  if (this->precision_ == -2) {
61  auto prec = precisionFunc_(*selobjs[i]);
62  if (prec > 0) {
64  }
65  }
66  }
67  }
68  out.template addColumn<ValType>(this->name_, vals, this->doc_, this->precision_);
69  }
70 
71 protected:
72  StringFunctor func_;
73  StringFunctor precisionFunc_;
74 };
75 
76 // External variables: i.e. variables that are not member or methods of the object
77 template <typename ObjType>
78 class ExtVariable : public VariableBase {
79 public:
80  ExtVariable(const std::string &aname, const edm::ParameterSet &cfg) : VariableBase(aname, cfg) {}
81  virtual void fill(const edm::Event &iEvent,
83  nanoaod::FlatTable &out) const = 0;
84 };
85 
86 template <typename ObjType, typename TIn, typename ValType = TIn>
87 class ValueMapVariableBase : public ExtVariable<ObjType> {
88 public:
90  const edm::ParameterSet &cfg,
92  bool skipNonExistingSrc = false)
93  : ExtVariable<ObjType>(aname, cfg),
95  token_(cc.consumes<edm::ValueMap<TIn>>(cfg.getParameter<edm::InputTag>("src"))) {}
96  virtual ValType eval(const edm::Handle<edm::ValueMap<TIn>> &vmap, const edm::Ptr<ObjType> &op) const = 0;
97  void fill(const edm::Event &iEvent, std::vector<edm::Ptr<ObjType>> selptrs, nanoaod::FlatTable &out) const override {
99  iEvent.getByToken(token_, vmap);
100  std::vector<ValType> vals;
101  if (vmap.isValid() || !skipNonExistingSrc_) {
102  vals.resize(selptrs.size());
103  for (unsigned int i = 0, n = vals.size(); i < n; ++i) {
104  // calls the overloaded method to either get the valuemap value directly, or a function of the object value.
105  vals[i] = this->eval(vmap, selptrs[i]);
106  }
107  }
108  out.template addColumn<ValType>(this->name_, vals, this->doc_, this->precision_);
109  }
110 
111 protected:
114 };
115 
116 template <typename ObjType, typename TIn, typename ValType = TIn>
117 class ValueMapVariable : public ValueMapVariableBase<ObjType, TIn, ValType> {
118 public:
120  const edm::ParameterSet &cfg,
122  bool skipNonExistingSrc = false)
123  : ValueMapVariableBase<ObjType, TIn, ValType>(aname, cfg, std::move(cc), skipNonExistingSrc) {}
124  ValType eval(const edm::Handle<edm::ValueMap<TIn>> &vmap, const edm::Ptr<ObjType> &op) const override {
125  ValType val = (*vmap)[op];
126  return val;
127  }
128 };
129 
130 template <typename ObjType, typename TIn, typename StringFunctor, typename ValType>
131 class TypedValueMapVariable : public ValueMapVariableBase<ObjType, TIn, ValType> {
132 public:
134  const edm::ParameterSet &cfg,
136  bool skipNonExistingSrc = false)
137  : ValueMapVariableBase<ObjType, TIn, ValType>(aname, cfg, std::move(cc), skipNonExistingSrc),
138  func_(cfg.getParameter<std::string>("expr"), true),
139  precisionFunc_(cfg.existsAs<std::string>("precision") ? cfg.getParameter<std::string>("precision") : "23",
140  true) {}
141 
142  ValType eval(const edm::Handle<edm::ValueMap<TIn>> &vmap, const edm::Ptr<ObjType> &op) const override {
143  ValType val = func_((*vmap)[op]);
144  if constexpr (std::is_same<ValType, float>()) {
145  if (this->precision_ == -2) {
146  auto prec = precisionFunc_(*op);
147  if (prec > 0) {
149  }
150  }
151  }
152  return val;
153  }
154 
155 protected:
156  StringFunctor func_;
158 };
159 
160 // Event producers
161 // - ABC
162 // - Singleton
163 // - Collection
164 template <typename T, typename TProd>
166 public:
168  : name_(params.getParameter<std::string>("name")),
169  doc_(params.getParameter<std::string>("doc")),
170  extension_(params.getParameter<bool>("extension")),
171  skipNonExistingSrc_(params.getParameter<bool>("skipNonExistingSrc")),
172  src_(consumes<TProd>(params.getParameter<edm::InputTag>("src"))) {
173  edm::ParameterSet const &varsPSet = params.getParameter<edm::ParameterSet>("variables");
174  for (const std::string &vname : varsPSet.getParameterNamesForType<edm::ParameterSet>()) {
175  const auto &varPSet = varsPSet.getParameter<edm::ParameterSet>(vname);
176  const std::string &type = varPSet.getParameter<std::string>("type");
177  if (type == "int")
178  vars_.push_back(std::make_unique<IntVar>(vname, varPSet));
179  else if (type == "uint")
180  vars_.push_back(std::make_unique<UIntVar>(vname, varPSet));
181  else if (type == "float")
182  vars_.push_back(std::make_unique<FloatVar>(vname, varPSet));
183  else if (type == "double")
184  vars_.push_back(std::make_unique<DoubleVar>(vname, varPSet));
185  else if (type == "uint8")
186  vars_.push_back(std::make_unique<UInt8Var>(vname, varPSet));
187  else if (type == "int16")
188  vars_.push_back(std::make_unique<Int16Var>(vname, varPSet));
189  else if (type == "uint16")
190  vars_.push_back(std::make_unique<UInt16Var>(vname, varPSet));
191  else if (type == "bool")
192  vars_.push_back(std::make_unique<BoolVar>(vname, varPSet));
193  else
194  throw cms::Exception("Configuration", "unsupported type " + type + " for variable " + vname);
195  }
196 
197  produces<nanoaod::FlatTable>();
198  }
199 
201 
205  desc.add<std::string>("name")->setComment("name of the branch in the flat table output for " + classname);
206  desc.add<std::string>("doc", "")->setComment("few words of self documentation");
207  desc.add<bool>("extension", false)->setComment("whether or not to extend an existing same table");
208  desc.add<bool>("skipNonExistingSrc", false)
209  ->setComment("whether or not to skip producing the table on absent input product");
210  desc.add<edm::InputTag>("src")->setComment("input collection to fill the flat table");
211 
213  variable.add<std::string>("expr")->setComment("a function to define the content of the branch in the flat table");
214  variable.add<std::string>("doc")->setComment("few words description of the branch content");
215  variable.addUntracked<bool>("lazyEval", false)
216  ->setComment("if true, can use methods of inheriting classes in `expr`. Can cause problems with threading.");
217  variable.ifValue(
219  "type", "int", true, edm::Comment("the c++ type of the branch in the flat table")),
220  edm::allowedValues<std::string>("int", "uint", "float", "double", "uint8", "int16", "uint16", "bool"));
221  variable.addOptionalNode(
223  "precision", true, edm::Comment("the precision with which to store the value in the flat table")) xor
225  "precision", true, edm::Comment("the precision with which to store the value in the flat table")),
226  false);
227 
229  variables.setComment("a parameters set to define all variable to fill the flat table");
230  variables.addNode(
232  desc.add<edm::ParameterSetDescription>("variables", variables);
233 
234  return desc;
235  }
236  // this is to be overriden by the child class
237  virtual std::unique_ptr<nanoaod::FlatTable> fillTable(const edm::Event &iEvent,
238  const edm::Handle<TProd> &prod) const = 0;
239 
240  void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override {
242  iEvent.getByToken(src_, src);
243 
244  std::unique_ptr<nanoaod::FlatTable> out = fillTable(iEvent, src);
245  out->setDoc(doc_);
246 
247  iEvent.put(std::move(out));
248  }
249 
250 protected:
253  const bool extension_;
256 
265  std::vector<std::unique_ptr<Variable<T>>> vars_;
266 };
267 
268 template <typename T>
269 class SimpleFlatTableProducer : public SimpleFlatTableProducerBase<T, edm::View<T>> {
270 public:
273  singleton_(params.getParameter<bool>("singleton")),
274  maxLen_(params.existsAs<unsigned int>("maxLen") ? params.getParameter<unsigned int>("maxLen")
275  : std::numeric_limits<unsigned int>::max()),
276  cut_(!singleton_ ? params.getParameter<std::string>("cut") : "",
277  !singleton_ ? params.getUntrackedParameter<bool>("lazyEval") : false) {
278  if (params.existsAs<edm::ParameterSet>("externalVariables")) {
279  edm::ParameterSet const &extvarsPSet = params.getParameter<edm::ParameterSet>("externalVariables");
280  for (const std::string &vname : extvarsPSet.getParameterNamesForType<edm::ParameterSet>()) {
281  const auto &varPSet = extvarsPSet.getParameter<edm::ParameterSet>(vname);
282  const std::string &type = varPSet.getParameter<std::string>("type");
283  if (type == "int")
284  extvars_.push_back(
285  std::make_unique<IntExtVar>(vname, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
286  else if (type == "uint")
287  extvars_.push_back(
288  std::make_unique<UIntExtVar>(vname, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
289  else if (type == "float")
290  extvars_.push_back(
291  std::make_unique<FloatExtVar>(vname, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
292  else if (type == "double")
293  extvars_.push_back(
294  std::make_unique<DoubleExtVar>(vname, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
295  else if (type == "uint8")
296  extvars_.push_back(
297  std::make_unique<UInt8ExtVar>(vname, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
298  else if (type == "int16")
299  extvars_.push_back(
300  std::make_unique<Int16ExtVar>(vname, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
301  else if (type == "uint16")
302  extvars_.push_back(
303  std::make_unique<UInt16ExtVar>(vname, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
304  else if (type == "bool")
305  extvars_.push_back(
306  std::make_unique<BoolExtVar>(vname, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
307  else
308  throw cms::Exception("Configuration", "unsupported type " + type + " for variable " + vname);
309  }
310  }
311  }
312 
314 
317 
318  desc.ifValue(
320  "singleton", false, true, edm::Comment("whether or not the input collection is single-element")),
322  "cut", "", true, edm::Comment("selection on the main input collection")) and
324  false,
325  false,
326  edm::Comment("if true, can use methods of inheriting classes. Can "
327  "cause problems when multi-threading."))) or
328  true >> edm::EmptyGroupDescription());
329  desc.addOptional<unsigned int>("maxLen")->setComment(
330  "define the maximum length of the input collection to put in the branch");
331 
332  edm::ParameterSetDescription extvariable;
333  extvariable.add<edm::InputTag>("src")->setComment("valuemap input collection to fill the flat table");
334  extvariable.add<std::string>("doc")->setComment("few words description of the branch content");
335  extvariable.ifValue(
337  "type", "int", true, edm::Comment("the c++ type of the branch in the flat table")),
338  edm::allowedValues<std::string>("int", "uint", "float", "double", "uint8", "int16", "uint16", "bool"));
339  extvariable.addOptionalNode(
341  "precision", true, edm::Comment("the precision with which to store the value in the flat table")) xor
343  true,
344  edm::Comment("the precision with which to store the value in the "
345  "flat table, as a function of the object evaluated")),
346  false);
347 
348  edm::ParameterSetDescription extvariables;
349  extvariables.setComment("a parameters set to define all variable taken form valuemap to fill the flat table");
350  extvariables.addOptionalNode(
352  desc.addOptional<edm::ParameterSetDescription>("externalVariables", extvariables);
353 
354  return desc;
355  }
358  descriptions.addWithDefaultLabel(desc);
359  }
360  std::unique_ptr<nanoaod::FlatTable> fillTable(const edm::Event &iEvent,
361  const edm::Handle<edm::View<T>> &prod) const override {
362  std::vector<const T *> selobjs;
363  std::vector<edm::Ptr<T>> selptrs; // for external variables
364  if (prod.isValid() || !(this->skipNonExistingSrc_)) {
365  if (singleton_) {
366  assert(prod->size() == 1);
367  selobjs.push_back(&(*prod)[0]);
368  if (!extvars_.empty() || !typedextvars_.empty())
369  selptrs.emplace_back(prod->ptrAt(0));
370  } else {
371  for (unsigned int i = 0, n = prod->size(); i < n; ++i) {
372  const auto &obj = (*prod)[i];
373  if (cut_(obj)) {
374  selobjs.push_back(&obj);
375  if (!extvars_.empty() || !typedextvars_.empty())
376  selptrs.emplace_back(prod->ptrAt(i));
377  }
378  if (selobjs.size() >= maxLen_)
379  break;
380  }
381  }
382  }
383  auto out = std::make_unique<nanoaod::FlatTable>(selobjs.size(), this->name_, singleton_, this->extension_);
384  for (const auto &var : this->vars_)
385  var->fill(selobjs, *out);
386  for (const auto &var : this->extvars_)
387  var->fill(iEvent, selptrs, *out);
388  for (const auto &var : this->typedextvars_)
389  var->fill(iEvent, selptrs, *out);
390  return out;
391  }
392 
393 protected:
395  const unsigned int maxLen_;
397 
406  std::vector<std::unique_ptr<ExtVariable<T>>> extvars_;
407  std::vector<std::unique_ptr<ExtVariable<T>>> typedextvars_;
408 };
409 
410 template <typename T, typename V>
412 public:
414  edm::ParameterSet const &extvarsPSet = params.getParameter<edm::ParameterSet>("externalTypedVariables");
415  for (const std::string &vname : extvarsPSet.getParameterNamesForType<edm::ParameterSet>()) {
416  const auto &varPSet = extvarsPSet.getParameter<edm::ParameterSet>(vname);
417  const std::string &type = varPSet.getParameter<std::string>("type");
418  if (type == "int")
419  this->typedextvars_.push_back(
420  std::make_unique<IntTypedExtVar>(vname, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
421  else if (type == "uint")
422  this->typedextvars_.push_back(
423  std::make_unique<UIntTypedExtVar>(vname, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
424  else if (type == "float")
425  this->typedextvars_.push_back(
426  std::make_unique<FloatTypedExtVar>(vname, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
427  else if (type == "double")
428  this->typedextvars_.push_back(
429  std::make_unique<DoubleTypedExtVar>(vname, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
430  else if (type == "uint8")
431  this->typedextvars_.push_back(
432  std::make_unique<UInt8TypedExtVar>(vname, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
433  else if (type == "int16")
434  this->typedextvars_.push_back(
435  std::make_unique<Int16TypedExtVar>(vname, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
436  else if (type == "uint16")
437  this->typedextvars_.push_back(
438  std::make_unique<UInt16TypedExtVar>(vname, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
439  else if (type == "bool")
440  this->typedextvars_.push_back(
441  std::make_unique<BoolTypedExtVar>(vname, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
442  else
443  throw cms::Exception("Configuration", "unsupported type " + type + " for variable " + vname);
444  }
445  }
449  edm::ParameterSetDescription extvariable;
450  extvariable.add<edm::InputTag>("src")->setComment("valuemap input collection to fill the flat table");
451  extvariable.add<std::string>("expr")->setComment(
452  "a function to define the content of the branch in the flat table");
453  extvariable.add<std::string>("doc")->setComment("few words description of the branch content");
454  extvariable.addUntracked<bool>("lazyEval", false)
455  ->setComment("if true, can use methods of inheriting classes in `expr`. Can cause problems with threading.");
456  extvariable.ifValue(
458  "type", "int", true, edm::Comment("the c++ type of the branch in the flat table")),
459  edm::allowedValues<std::string>("int", "uint", "float", "double", "uint8", "int16", "uint16", "bool"));
460  extvariable.addOptionalNode(
462  "precision", true, edm::Comment("the precision with which to store the value in the flat table")) xor
464  true,
465  edm::Comment("the precision with which to store the value in the "
466  "flat table, as a function of the object evaluated")),
467  false);
468 
469  edm::ParameterSetDescription extvariables;
470  extvariables.setComment("a parameters set to define all variable taken form valuemap to fill the flat table");
471  extvariables.addOptionalNode(
473  desc.addOptional<edm::ParameterSetDescription>("externalTypedVariables", extvariables);
474 
475  descriptions.addWithDefaultLabel(desc);
476  }
477 
478 protected:
487 };
488 
489 template <typename T>
491 public:
494  maxLen_(params.existsAs<unsigned int>("maxLen") ? params.getParameter<unsigned int>("maxLen")
495  : std::numeric_limits<unsigned int>::max()),
496  cut_(params.getParameter<std::string>("cut"), false),
497  minBX_(params.getParameter<int>("minBX")),
498  maxBX_(params.getParameter<int>("maxBX")),
499  alwaysWriteBXValue_(params.getParameter<bool>("alwaysWriteBXValue")),
500  bxVarName_("bx") {
501  edm::ParameterSet const &varsPSet = params.getParameter<edm::ParameterSet>("variables");
503  if (std::find(varNames.begin(), varNames.end(), bxVarName_) != varNames.end()) {
504  throw cms::Exception("Configuration",
505  "BXVectorSimpleFlatTableProducer already defines the " + bxVarName_ +
506  "internally and thus you should not specify it yourself");
507  }
508  }
509 
512  desc.add<std::string>("cut", "")->setComment(
513  "selection on the main input collection (but selection can not be bx based)");
514  desc.addOptional<unsigned int>("maxLen")->setComment(
515  "define the maximum length of the input collection to put in the branch");
516  desc.add<int>("minBX", -2)->setComment("min bx (inclusive) to include");
517  desc.add<int>("maxBX", 2)->setComment("max bx (inclusive) to include");
518  desc.add<bool>("alwaysWriteBXValue", true)
519  ->setComment("always write the bx number (event when only one bx can be present, ie minBX==maxBX)");
520  descriptions.addWithDefaultLabel(desc);
521  }
522 
523  std::unique_ptr<nanoaod::FlatTable> fillTable(const edm::Event &iEvent,
524  const edm::Handle<BXVector<T>> &prod) const override {
525  std::vector<const T *> selObjs;
526  std::vector<int> selObjBXs;
527 
528  if (prod.isValid() || !(this->skipNonExistingSrc_)) {
529  const int minBX = std::max(minBX_, prod->getFirstBX());
530  const int maxBX = std::min(maxBX_, prod->getLastBX());
531  for (int bx = minBX; bx <= maxBX; bx++) {
532  for (size_t objNr = 0, nrObjs = prod->size(bx); objNr < nrObjs; ++objNr) {
533  const auto &obj = prod->at(bx, objNr);
534  if (cut_(obj)) {
535  selObjs.push_back(&obj);
536  selObjBXs.push_back(bx);
537  }
538  if (selObjs.size() >= maxLen_)
539  break;
540  }
541  }
542  }
543  auto out = std::make_unique<nanoaod::FlatTable>(selObjs.size(), this->name_, false, this->extension_);
544  for (const auto &var : this->vars_)
545  var->fill(selObjs, *out);
546  if (alwaysWriteBXValue_ || minBX_ != maxBX_) {
547  out->template addColumn<int16_t>(bxVarName_, selObjBXs, "BX of the L1 candidate");
548  }
549  return out;
550  }
551 
552 protected:
553  const unsigned int maxLen_;
555  const int minBX_;
556  const int maxBX_;
559 };
560 
561 template <typename T>
563 public:
565 
567 
570  descriptions.addWithDefaultLabel(desc);
571  }
572 
573  std::unique_ptr<nanoaod::FlatTable> fillTable(const edm::Event &, const edm::Handle<T> &prod) const override {
574  auto out = std::make_unique<nanoaod::FlatTable>(1, this->name_, true, this->extension_);
575  std::vector<const T *> selobjs(1, prod.product());
576  for (const auto &var : this->vars_)
577  var->fill(selobjs, *out);
578  return out;
579  }
580 };
581 
582 template <typename T>
584 public:
587 
589 
592  descriptions.addWithDefaultLabel(desc);
593  }
594 
595  std::unique_ptr<nanoaod::FlatTable> fillTable(const edm::Event &iEvent,
596  const edm::Handle<edm::View<T>> &prod) const override {
597  auto out = std::make_unique<nanoaod::FlatTable>(1, this->name_, true, this->extension_);
598  std::vector<const T *> selobjs(1, &(*prod)[0]);
599  for (const auto &var : this->vars_)
600  var->fill(selobjs, *out);
601  return out;
602  }
603 };
604 
605 // LuminosityBlock producers
606 // - ABC
607 // - Singleton
608 // - Collection
609 template <typename T, typename TProd>
611  : public edm::one::EDProducer<edm::EndLuminosityBlockProducer, edm::LuminosityBlockCache<int>> {
612 public:
614  : name_(params.getParameter<std::string>("name")),
615  doc_(params.existsAs<std::string>("doc") ? params.getParameter<std::string>("doc") : ""),
616  extension_(params.existsAs<bool>("extension") ? params.getParameter<bool>("extension") : false),
618 
619  params.existsAs<bool>("skipNonExistingSrc") ? params.getParameter<bool>("skipNonExistingSrc") : false),
620  src_(consumes<TProd, edm::InLumi>(params.getParameter<edm::InputTag>("src"))) {
621  edm::ParameterSet const &varsPSet = params.getParameter<edm::ParameterSet>("variables");
622  for (const std::string &vname : varsPSet.getParameterNamesForType<edm::ParameterSet>()) {
623  const auto &varPSet = varsPSet.getParameter<edm::ParameterSet>(vname);
624  const std::string &type = varPSet.getParameter<std::string>("type");
625  if (type == "int")
626  vars_.push_back(std::make_unique<IntVar>(vname, varPSet));
627  else if (type == "float")
628  vars_.push_back(std::make_unique<FloatVar>(vname, varPSet));
629  else if (type == "uint8")
630  vars_.push_back(std::make_unique<UInt8Var>(vname, varPSet));
631  else if (type == "bool")
632  vars_.push_back(std::make_unique<BoolVar>(vname, varPSet));
633  else
634  throw cms::Exception("Configuration", "unsupported type " + type + " for variable " + vname);
635  }
636 
637  produces<nanoaod::FlatTable, edm::Transition::EndLuminosityBlock>();
638  }
639 
641 
642  std::shared_ptr<int> globalBeginLuminosityBlock(edm::LuminosityBlock const &,
643  edm::EventSetup const &) const override {
644  return nullptr;
645  }
646 
648 
649  // this is to be overriden by the child class
650  virtual std::unique_ptr<nanoaod::FlatTable> fillTable(const edm::LuminosityBlock &iLumi,
651  const edm::Handle<TProd> &prod) const = 0;
652 
653  void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override {
654  // do nothing
655  }
656 
659  iLumi.getByToken(src_, src);
660 
661  std::unique_ptr<nanoaod::FlatTable> out = fillTable(iLumi, src);
662  out->setDoc(doc_);
663 
664  iLumi.put(std::move(out));
665  }
666 
667 protected:
670  const bool extension_;
673 
678  std::vector<std::unique_ptr<Variable<T>>> vars_;
679 };
680 
681 // Class for singletons like GenFilterInfo
682 template <typename T>
684 public:
687 
689 
692  descriptions.addWithDefaultLabel(desc);
693  }
694 
695  std::unique_ptr<nanoaod::FlatTable> fillTable(const edm::LuminosityBlock &,
696  const edm::Handle<T> &prod) const override {
697  auto out = std::make_unique<nanoaod::FlatTable>(1, this->name_, true, this->extension_);
698  std::vector<const T *> selobjs(1, prod.product());
699  for (const auto &var : this->vars_)
700  var->fill(selobjs, *out);
701  return out;
702  }
703 };
704 
705 // Class for generic collections
706 template <typename T, typename TProd>
708 public:
711  maxLen_(params.existsAs<unsigned int>("maxLen") ? params.getParameter<unsigned int>("maxLen")
712  : std::numeric_limits<unsigned int>::max()),
713  cut_(params.existsAs<std::string>("cut") ? params.getParameter<std::string>("cut") : "", true) {}
714 
716 
719  desc.addOptional<unsigned int>("maxLen")->setComment(
720  "define the maximum length of the input collection to put in the branch");
721  descriptions.addWithDefaultLabel(desc);
722  }
723 
724  std::unique_ptr<nanoaod::FlatTable> fillTable(const edm::LuminosityBlock &iLumi,
725  const edm::Handle<TProd> &prod) const override {
726  std::vector<const T *> selobjs;
727  if (prod.isValid() || !(this->skipNonExistingSrc_)) {
728  for (unsigned int i = 0, n = prod->size(); i < n; ++i) {
729  const auto &obj = (*prod)[i];
730  if (cut_(obj)) {
731  selobjs.push_back(&obj);
732  }
733  if (selobjs.size() >= maxLen_)
734  break;
735  }
736  }
737  auto out = std::make_unique<nanoaod::FlatTable>(selobjs.size(), this->name_, false, this->extension_);
738  for (const auto &var : this->vars_)
739  var->fill(selobjs, *out);
740  return out;
741  }
742 
743 protected:
744  const unsigned int maxLen_;
746 };
747 
748 // Run producers
749 // - ABC
750 // - Singleton
751 // - Collection
752 template <typename T, typename TProd>
753 class SimpleFlatTableProducerBaseRun : public edm::one::EDProducer<edm::EndRunProducer, edm::RunCache<int>> {
754 public:
756  : name_(params.getParameter<std::string>("name")),
757  doc_(params.existsAs<std::string>("doc") ? params.getParameter<std::string>("doc") : ""),
758  extension_(params.existsAs<bool>("extension") ? params.getParameter<bool>("extension") : false),
760 
761  params.existsAs<bool>("skipNonExistingSrc") ? params.getParameter<bool>("skipNonExistingSrc") : false),
762  src_(consumes<TProd, edm::InRun>(params.getParameter<edm::InputTag>("src"))) {
763  edm::ParameterSet const &varsPSet = params.getParameter<edm::ParameterSet>("variables");
764  for (const std::string &vname : varsPSet.getParameterNamesForType<edm::ParameterSet>()) {
765  const auto &varPSet = varsPSet.getParameter<edm::ParameterSet>(vname);
766  const std::string &type = varPSet.getParameter<std::string>("type");
767  if (type == "int")
768  vars_.push_back(std::make_unique<IntVar>(vname, varPSet));
769  else if (type == "float")
770  vars_.push_back(std::make_unique<FloatVar>(vname, varPSet));
771  else if (type == "uint8")
772  vars_.push_back(std::make_unique<UInt8Var>(vname, varPSet));
773  else if (type == "bool")
774  vars_.push_back(std::make_unique<BoolVar>(vname, varPSet));
775  else
776  throw cms::Exception("Configuration", "unsupported type " + type + " for variable " + vname);
777  }
778 
779  produces<nanoaod::FlatTable, edm::Transition::EndRun>();
780  }
781 
783 
784  std::shared_ptr<int> globalBeginRun(edm::Run const &, edm::EventSetup const &) const override { return nullptr; }
785 
786  void globalEndRun(edm::Run const &, edm::EventSetup const &) override {}
787 
788  // this is to be overriden by the child class
789  virtual std::unique_ptr<nanoaod::FlatTable> fillTable(const edm::Run &iRun, const edm::Handle<TProd> &prod) const = 0;
790 
791  void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override {
792  // do nothing
793  }
794 
795  void endRunProduce(edm::Run &iRun, const edm::EventSetup &iSetup) final {
797  iRun.getByToken(src_, src);
798 
799  std::unique_ptr<nanoaod::FlatTable> out = fillTable(iRun, src);
800  out->setDoc(doc_);
801 
802  iRun.put(std::move(out));
803  }
804 
805 protected:
808  const bool extension_;
811 
816  std::vector<std::unique_ptr<Variable<T>>> vars_;
817 };
818 
819 // Class for singletons like GenFilterInfo
820 template <typename T>
822 public:
824 
826 
827  std::unique_ptr<nanoaod::FlatTable> fillTable(const edm::Run &, const edm::Handle<T> &prod) const override {
828  auto out = std::make_unique<nanoaod::FlatTable>(1, this->name_, true, this->extension_);
829  std::vector<const T *> selobjs(1, prod.product());
830  for (const auto &var : this->vars_)
831  var->fill(selobjs, *out);
832  return out;
833  }
834 };
835 
836 // Class for generic collections
837 template <typename T, typename TProd>
839 public:
842  maxLen_(params.existsAs<unsigned int>("maxLen") ? params.getParameter<unsigned int>("maxLen")
843  : std::numeric_limits<unsigned int>::max()),
844  cut_(params.existsAs<std::string>("cut") ? params.getParameter<std::string>("cut") : "", true) {}
845 
847 
848  std::unique_ptr<nanoaod::FlatTable> fillTable(const edm::Run &iRun, const edm::Handle<TProd> &prod) const override {
849  std::vector<const T *> selobjs;
850  if (prod.isValid() || !(this->skipNonExistingSrc_)) {
851  for (unsigned int i = 0, n = prod->size(); i < n; ++i) {
852  const auto &obj = (*prod)[i];
853  if (cut_(obj)) {
854  selobjs.push_back(&obj);
855  }
856  if (selobjs.size() >= maxLen_)
857  break;
858  }
859  }
860  auto out = std::make_unique<nanoaod::FlatTable>(selobjs.size(), this->name_, false, this->extension_);
861  for (const auto &var : this->vars_)
862  var->fill(selobjs, *out);
863  return out;
864  }
865 
866 protected:
867  const unsigned int maxLen_;
869 };
ValueMapVariable< T, double, float > DoubleExtVar
SimpleTypedExternalFlatTableProducer(edm::ParameterSet const &params)
ParameterDescriptionNode * ifValue(ParameterDescription< T > const &switchParameter, std::unique_ptr< ParameterDescriptionCases< T >> cases)
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
Variable(const std::string &aname, const edm::ParameterSet &cfg)
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
TypedValueMapVariable< T, V, StringObjectFunction< V >, double > DoubleTypedExtVar
ValueMapVariable< T, int, uint16_t > UInt16ExtVar
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
ValueMapVariable< T, bool > BoolExtVar
FuncVariable< T, StringObjectFunction< T >, int > IntVar
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
std::vector< std::unique_ptr< Variable< T > > > vars_
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
std::unique_ptr< nanoaod::FlatTable > fillTable(const edm::LuminosityBlock &, const edm::Handle< T > &prod) const override
TypedValueMapVariable< T, V, StringObjectFunction< V >, uint16_t > UInt16TypedExtVar
FuncVariable< T, StringCutObjectSelector< T >, bool > BoolVar
virtual void fill(const edm::Event &iEvent, std::vector< edm::Ptr< ObjType >> selptrs, nanoaod::FlatTable &out) const =0
std::shared_ptr< int > globalBeginRun(edm::Run const &, edm::EventSetup const &) const override
void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override
uint32_t cc[maxCellsPerHit]
Definition: gpuFishbone.h:49
ValueMapVariable< T, int, uint8_t > UInt8ExtVar
const edm::EDGetTokenT< TProd > src_
void globalEndRun(edm::Run const &, edm::EventSetup const &) override
const StringCutObjectSelector< T > cut_
void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override
void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override
std::unique_ptr< nanoaod::FlatTable > fillTable(const edm::Event &iEvent, const edm::Handle< edm::View< T >> &prod) const override
TypedValueMapVariable< T, V, StringObjectFunction< V >, uint32_t > UIntTypedExtVar
FuncVariable< T, StringObjectFunction< T >, uint32_t > UIntVar
FuncVariable< T, StringObjectFunction< T >, uint8_t > UInt8Var
constexpr char const * varNames[]
FuncVariable< T, StringCutObjectSelector< T >, bool > BoolVar
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
assert(be >=bs)
VariableBase(const std::string &aname, const edm::ParameterSet &cfg)
FuncVariable< T, StringObjectFunction< T >, uint16_t > UInt16Var
SimpleFlatTableProducerBase(edm::ParameterSet const &params)
ValueMapVariable< T, uint32_t > UIntExtVar
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
virtual std::unique_ptr< nanoaod::FlatTable > fillTable(const edm::Run &iRun, const edm::Handle< TProd > &prod) const =0
ValueMapVariable< T, int, int16_t > Int16ExtVar
ValType eval(const edm::Handle< edm::ValueMap< TIn >> &vmap, const edm::Ptr< ObjType > &op) const override
TypedValueMapVariable(const std::string &aname, const edm::ParameterSet &cfg, edm::ConsumesCollector &&cc, bool skipNonExistingSrc=false)
std::unique_ptr< nanoaod::FlatTable > fillTable(const edm::Event &, const edm::Handle< T > &prod) const override
FuncVariable< T, StringObjectFunction< T >, uint8_t > UInt8Var
void setComment(std::string const &value)
void globalEndLuminosityBlock(edm::LuminosityBlock const &, edm::EventSetup const &) override
int iEvent
Definition: GenABIO.cc:224
const StringCutObjectSelector< T > cut_
std::unique_ptr< nanoaod::FlatTable > fillTable(const edm::LuminosityBlock &iLumi, const edm::Handle< TProd > &prod) const override
const StringCutObjectSelector< T > cut_
TypedValueMapVariable< T, V, StringObjectFunction< V >, float > FloatTypedExtVar
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< void, edm::EventID const &, edm::Timestamp const & > We also list in braces which AR_WATCH_USING_METHOD_ is used for those or
Definition: Activities.doc:12
FuncVariable< T, StringObjectFunction< T >, int16_t > Int16Var
FuncVariable< T, StringObjectFunction< T >, float > FloatVar
TypedValueMapVariable< T, V, StringObjectFunction< V >, int16_t > Int16TypedExtVar
std::vector< std::string > getParameterNamesForType(bool trackiness=true) const
Definition: ParameterSet.h:180
static const std::string & name()
Definition: ClassName.h:38
std::vector< std::unique_ptr< ExtVariable< T > > > typedextvars_
std::vector< std::unique_ptr< ExtVariable< T > > > extvars_
ParameterDescriptionNode * addOptionalNode(ParameterDescriptionNode const &node, bool writeToCfi)
LumiSingletonSimpleFlatTableProducer(edm::ParameterSet const &params)
RunSingletonSimpleFlatTableProducer(edm::ParameterSet const &params)
static edm::ParameterSetDescription baseDescriptions()
ExtVariable(const std::string &aname, const edm::ParameterSet &cfg)
TypedValueMapVariable< T, V, StringCutObjectSelector< V >, bool > BoolTypedExtVar
SimpleFlatTableProducer(edm::ParameterSet const &params)
ParameterDescriptionBase * add(U const &iLabel, T const &value)
virtual ValType eval(const edm::Handle< edm::ValueMap< TIn >> &vmap, const edm::Ptr< ObjType > &op) const =0
ValueMapVariableBase(const std::string &aname, const edm::ParameterSet &cfg, edm::ConsumesCollector &&cc, bool skipNonExistingSrc=false)
std::unique_ptr< nanoaod::FlatTable > fillTable(const edm::Run &iRun, const edm::Handle< TProd > &prod) const override
edm::EDGetTokenT< edm::ValueMap< TIn > > token_
TypedValueMapVariable< T, V, StringObjectFunction< V >, int32_t > IntTypedExtVar
void endRunProduce(edm::Run &iRun, const edm::EventSetup &iSetup) final
const edm::EDGetTokenT< TProd > src_
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
FuncVariable< T, StringObjectFunction< T >, float > FloatVar
FuncVariable< T, StringObjectFunction< T >, int32_t > IntVar
StringObjectFunction< ObjType > precisionFunc_
void fill(std::vector< const ObjType *> &selobjs, nanoaod::FlatTable &out) const override
BXVectorSimpleFlatTableProducer(edm::ParameterSet const &params)
FuncVariable< T, StringObjectFunction< T >, int > IntVar
StringFunctor precisionFunc_
RunSimpleFlatTableProducer(edm::ParameterSet const &params)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
const std::string & name() const
SimpleFlatTableProducerBaseRun(edm::ParameterSet const &params)
FuncVariable< T, StringObjectFunction< T >, uint8_t > UInt8Var
FuncVariable< T, StringObjectFunction< T >, float > FloatVar
SimpleFlatTableProducerBaseLumi(edm::ParameterSet const &params)
virtual std::unique_ptr< nanoaod::FlatTable > fillTable(const edm::Event &iEvent, const edm::Handle< TProd > &prod) const =0
FuncVariable< T, StringObjectFunction< T >, double > DoubleVar
std::vector< std::unique_ptr< Variable< T > > > vars_
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
std::shared_ptr< int > globalBeginLuminosityBlock(edm::LuminosityBlock const &, edm::EventSetup const &) const override
std::unique_ptr< nanoaod::FlatTable > fillTable(const edm::Event &iEvent, const edm::Handle< edm::View< T >> &prod) const override
bool isValid() const
Definition: HandleBase.h:70
std::unique_ptr< nanoaod::FlatTable > fillTable(const edm::Event &iEvent, const edm::Handle< BXVector< T >> &prod) const override
static float reduceMantissaToNbitsRounding(const float &f)
Definition: libminifloat.h:79
HLT enums.
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
ValueMapVariable(const std::string &aname, const edm::ParameterSet &cfg, edm::ConsumesCollector &&cc, bool skipNonExistingSrc=false)
FirstObjectSimpleFlatTableProducer(edm::ParameterSet const &params)
EventSingletonSimpleFlatTableProducer(edm::ParameterSet const &params)
ValueMapVariable< T, float > FloatExtVar
FuncVariable(const std::string &aname, const edm::ParameterSet &cfg)
virtual void fill(std::vector< const ObjType *> &selobjs, nanoaod::FlatTable &out) const =0
TypedValueMapVariable< T, V, StringObjectFunction< V >, uint8_t > UInt8TypedExtVar
ValueMapVariable< T, int32_t > IntExtVar
std::vector< std::unique_ptr< Variable< T > > > vars_
ValType eval(const edm::Handle< edm::ValueMap< TIn >> &vmap, const edm::Ptr< ObjType > &op) const override
LumiSimpleFlatTableProducer(edm::ParameterSet const &params)
long double T
std::unique_ptr< nanoaod::FlatTable > fillTable(const edm::Run &, const edm::Handle< T > &prod) const override
void endLuminosityBlockProduce(edm::LuminosityBlock &iLumi, const edm::EventSetup &iSetup) final
const StringCutObjectSelector< T > cut_
FuncVariable< T, StringCutObjectSelector< T >, bool > BoolVar
def move(src, dest)
Definition: eostools.py:511
virtual std::unique_ptr< nanoaod::FlatTable > fillTable(const edm::LuminosityBlock &iLumi, const edm::Handle< TProd > &prod) const =0
Definition: Run.h:45
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
const edm::EDGetTokenT< TProd > src_
static edm::ParameterSetDescription baseDescriptions()
void fill(const edm::Event &iEvent, std::vector< edm::Ptr< ObjType >> selptrs, nanoaod::FlatTable &out) const override