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"), true),
51  precisionFunc_(cfg.existsAs<std::string>("precision") ? cfg.getParameter<std::string>("precision") : "23",
52  true) {}
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.ifValue(
217  "type", "int", true, edm::Comment("the c++ type of the branch in the flat table")),
218  edm::allowedValues<std::string>("int", "uint", "float", "double", "uint8", "int16", "uint16", "bool"));
219  variable.addOptionalNode(
221  "precision", true, edm::Comment("the precision with which to store the value in the flat table")) xor
223  "precision", true, edm::Comment("the precision with which to store the value in the flat table")),
224  false);
225 
227  variables.setComment("a parameters set to define all variable to fill the flat table");
228  variables.addNode(
230  desc.add<edm::ParameterSetDescription>("variables", variables);
231 
232  return desc;
233  }
234  // this is to be overriden by the child class
235  virtual std::unique_ptr<nanoaod::FlatTable> fillTable(const edm::Event &iEvent,
236  const edm::Handle<TProd> &prod) const = 0;
237 
238  void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override {
240  iEvent.getByToken(src_, src);
241 
242  std::unique_ptr<nanoaod::FlatTable> out = fillTable(iEvent, src);
243  out->setDoc(doc_);
244 
245  iEvent.put(std::move(out));
246  }
247 
248 protected:
251  const bool extension_;
254 
263  std::vector<std::unique_ptr<Variable<T>>> vars_;
264 };
265 
266 template <typename T>
267 class SimpleFlatTableProducer : public SimpleFlatTableProducerBase<T, edm::View<T>> {
268 public:
271  singleton_(params.getParameter<bool>("singleton")),
272  maxLen_(params.existsAs<unsigned int>("maxLen") ? params.getParameter<unsigned int>("maxLen")
273  : std::numeric_limits<unsigned int>::max()),
274  cut_(!singleton_ ? params.getParameter<std::string>("cut") : "", true) {
275  if (params.existsAs<edm::ParameterSet>("externalVariables")) {
276  edm::ParameterSet const &extvarsPSet = params.getParameter<edm::ParameterSet>("externalVariables");
277  for (const std::string &vname : extvarsPSet.getParameterNamesForType<edm::ParameterSet>()) {
278  const auto &varPSet = extvarsPSet.getParameter<edm::ParameterSet>(vname);
279  const std::string &type = varPSet.getParameter<std::string>("type");
280  if (type == "int")
281  extvars_.push_back(
282  std::make_unique<IntExtVar>(vname, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
283  else if (type == "uint")
284  extvars_.push_back(
285  std::make_unique<UIntExtVar>(vname, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
286  else if (type == "float")
287  extvars_.push_back(
288  std::make_unique<FloatExtVar>(vname, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
289  else if (type == "double")
290  extvars_.push_back(
291  std::make_unique<DoubleExtVar>(vname, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
292  else if (type == "uint8")
293  extvars_.push_back(
294  std::make_unique<UInt8ExtVar>(vname, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
295  else if (type == "int16")
296  extvars_.push_back(
297  std::make_unique<Int16ExtVar>(vname, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
298  else if (type == "uint16")
299  extvars_.push_back(
300  std::make_unique<UInt16ExtVar>(vname, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
301  else if (type == "bool")
302  extvars_.push_back(
303  std::make_unique<BoolExtVar>(vname, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
304  else
305  throw cms::Exception("Configuration", "unsupported type " + type + " for variable " + vname);
306  }
307  }
308  }
309 
311 
314 
316  "singleton", false, true, edm::Comment("whether or not the input collection is single-element")),
318  "cut", "", true, edm::Comment("selection on the main input collection")) or
319  true >> edm::EmptyGroupDescription());
320  desc.addOptional<unsigned int>("maxLen")->setComment(
321  "define the maximum length of the input collection to put in the branch");
322 
323  edm::ParameterSetDescription extvariable;
324  extvariable.add<edm::InputTag>("src")->setComment("valuemap input collection to fill the flat table");
325  extvariable.add<std::string>("doc")->setComment("few words description of the branch content");
326  extvariable.ifValue(
328  "type", "int", true, edm::Comment("the c++ type of the branch in the flat table")),
329  edm::allowedValues<std::string>("int", "uint", "float", "double", "uint8", "int16", "uint16", "bool"));
330  extvariable.addOptionalNode(
332  "precision", true, edm::Comment("the precision with which to store the value in the flat table")) xor
334  true,
335  edm::Comment("the precision with which to store the value in the "
336  "flat table, as a function of the object evaluated")),
337  false);
338 
339  edm::ParameterSetDescription extvariables;
340  extvariables.setComment("a parameters set to define all variable taken form valuemap to fill the flat table");
341  extvariables.addOptionalNode(
343  desc.addOptional<edm::ParameterSetDescription>("externalVariables", extvariables);
344 
345  return desc;
346  }
349  descriptions.addWithDefaultLabel(desc);
350  }
351  std::unique_ptr<nanoaod::FlatTable> fillTable(const edm::Event &iEvent,
352  const edm::Handle<edm::View<T>> &prod) const override {
353  std::vector<const T *> selobjs;
354  std::vector<edm::Ptr<T>> selptrs; // for external variables
355  if (prod.isValid() || !(this->skipNonExistingSrc_)) {
356  if (singleton_) {
357  assert(prod->size() == 1);
358  selobjs.push_back(&(*prod)[0]);
359  if (!extvars_.empty() || !typedextvars_.empty())
360  selptrs.emplace_back(prod->ptrAt(0));
361  } else {
362  for (unsigned int i = 0, n = prod->size(); i < n; ++i) {
363  const auto &obj = (*prod)[i];
364  if (cut_(obj)) {
365  selobjs.push_back(&obj);
366  if (!extvars_.empty() || !typedextvars_.empty())
367  selptrs.emplace_back(prod->ptrAt(i));
368  }
369  if (selobjs.size() >= maxLen_)
370  break;
371  }
372  }
373  }
374  auto out = std::make_unique<nanoaod::FlatTable>(selobjs.size(), this->name_, singleton_, this->extension_);
375  for (const auto &var : this->vars_)
376  var->fill(selobjs, *out);
377  for (const auto &var : this->extvars_)
378  var->fill(iEvent, selptrs, *out);
379  for (const auto &var : this->typedextvars_)
380  var->fill(iEvent, selptrs, *out);
381  return out;
382  }
383 
384 protected:
386  const unsigned int maxLen_;
388 
397  std::vector<std::unique_ptr<ExtVariable<T>>> extvars_;
398  std::vector<std::unique_ptr<ExtVariable<T>>> typedextvars_;
399 };
400 
401 template <typename T, typename V>
403 public:
405  edm::ParameterSet const &extvarsPSet = params.getParameter<edm::ParameterSet>("externalTypedVariables");
406  for (const std::string &vname : extvarsPSet.getParameterNamesForType<edm::ParameterSet>()) {
407  const auto &varPSet = extvarsPSet.getParameter<edm::ParameterSet>(vname);
408  const std::string &type = varPSet.getParameter<std::string>("type");
409  if (type == "int")
410  this->typedextvars_.push_back(
411  std::make_unique<IntTypedExtVar>(vname, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
412  else if (type == "uint")
413  this->typedextvars_.push_back(
414  std::make_unique<UIntTypedExtVar>(vname, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
415  else if (type == "float")
416  this->typedextvars_.push_back(
417  std::make_unique<FloatTypedExtVar>(vname, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
418  else if (type == "double")
419  this->typedextvars_.push_back(
420  std::make_unique<DoubleTypedExtVar>(vname, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
421  else if (type == "uint8")
422  this->typedextvars_.push_back(
423  std::make_unique<UInt8TypedExtVar>(vname, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
424  else if (type == "int16")
425  this->typedextvars_.push_back(
426  std::make_unique<Int16TypedExtVar>(vname, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
427  else if (type == "uint16")
428  this->typedextvars_.push_back(
429  std::make_unique<UInt16TypedExtVar>(vname, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
430  else if (type == "bool")
431  this->typedextvars_.push_back(
432  std::make_unique<BoolTypedExtVar>(vname, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
433  else
434  throw cms::Exception("Configuration", "unsupported type " + type + " for variable " + vname);
435  }
436  }
440  edm::ParameterSetDescription extvariable;
441  extvariable.add<edm::InputTag>("src")->setComment("valuemap input collection to fill the flat table");
442  extvariable.add<std::string>("expr")->setComment(
443  "a function to define the content of the branch in the flat table");
444  extvariable.add<std::string>("doc")->setComment("few words description of the branch content");
445  extvariable.ifValue(
447  "type", "int", true, edm::Comment("the c++ type of the branch in the flat table")),
448  edm::allowedValues<std::string>("int", "uint", "float", "double", "uint8", "int16", "uint16", "bool"));
449  extvariable.addOptionalNode(
451  "precision", true, edm::Comment("the precision with which to store the value in the flat table")) xor
453  true,
454  edm::Comment("the precision with which to store the value in the "
455  "flat table, as a function of the object evaluated")),
456  false);
457 
458  edm::ParameterSetDescription extvariables;
459  extvariables.setComment("a parameters set to define all variable taken form valuemap to fill the flat table");
460  extvariables.addOptionalNode(
462  desc.addOptional<edm::ParameterSetDescription>("externalTypedVariables", extvariables);
463 
464  descriptions.addWithDefaultLabel(desc);
465  }
466 
467 protected:
476 };
477 
478 template <typename T>
480 public:
483  maxLen_(params.existsAs<unsigned int>("maxLen") ? params.getParameter<unsigned int>("maxLen")
484  : std::numeric_limits<unsigned int>::max()),
485  cut_(params.getParameter<std::string>("cut"), true),
486  minBX_(params.getParameter<int>("minBX")),
487  maxBX_(params.getParameter<int>("maxBX")),
488  alwaysWriteBXValue_(params.getParameter<bool>("alwaysWriteBXValue")),
489  bxVarName_("bx") {
490  edm::ParameterSet const &varsPSet = params.getParameter<edm::ParameterSet>("variables");
492  if (std::find(varNames.begin(), varNames.end(), bxVarName_) != varNames.end()) {
493  throw cms::Exception("Configuration",
494  "BXVectorSimpleFlatTableProducer already defines the " + bxVarName_ +
495  "internally and thus you should not specify it yourself");
496  }
497  }
498 
501  desc.add<std::string>("cut", "")->setComment(
502  "selection on the main input collection (but selection can not be bx based)");
503  desc.addOptional<unsigned int>("maxLen")->setComment(
504  "define the maximum length of the input collection to put in the branch");
505  desc.add<int>("minBX", -2)->setComment("min bx (inclusive) to include");
506  desc.add<int>("maxBX", 2)->setComment("max bx (inclusive) to include");
507  desc.add<bool>("alwaysWriteBXValue", true)
508  ->setComment("always write the bx number (event when only one bx can be present, ie minBX==maxBX)");
509  descriptions.addWithDefaultLabel(desc);
510  }
511 
512  std::unique_ptr<nanoaod::FlatTable> fillTable(const edm::Event &iEvent,
513  const edm::Handle<BXVector<T>> &prod) const override {
514  std::vector<const T *> selObjs;
515  std::vector<int> selObjBXs;
516 
517  if (prod.isValid() || !(this->skipNonExistingSrc_)) {
518  const int minBX = std::max(minBX_, prod->getFirstBX());
519  const int maxBX = std::min(maxBX_, prod->getLastBX());
520  for (int bx = minBX; bx <= maxBX; bx++) {
521  for (size_t objNr = 0, nrObjs = prod->size(bx); objNr < nrObjs; ++objNr) {
522  const auto &obj = prod->at(bx, objNr);
523  if (cut_(obj)) {
524  selObjs.push_back(&obj);
525  selObjBXs.push_back(bx);
526  }
527  if (selObjs.size() >= maxLen_)
528  break;
529  }
530  }
531  }
532  auto out = std::make_unique<nanoaod::FlatTable>(selObjs.size(), this->name_, false, this->extension_);
533  for (const auto &var : this->vars_)
534  var->fill(selObjs, *out);
535  if (alwaysWriteBXValue_ || minBX_ != maxBX_) {
536  out->template addColumn<int16_t>(bxVarName_, selObjBXs, "BX of the L1 candidate");
537  }
538  return out;
539  }
540 
541 protected:
542  const unsigned int maxLen_;
544  const int minBX_;
545  const int maxBX_;
548 };
549 
550 template <typename T>
552 public:
554 
556 
559  descriptions.addWithDefaultLabel(desc);
560  }
561 
562  std::unique_ptr<nanoaod::FlatTable> fillTable(const edm::Event &, const edm::Handle<T> &prod) const override {
563  auto out = std::make_unique<nanoaod::FlatTable>(1, this->name_, true, this->extension_);
564  std::vector<const T *> selobjs(1, prod.product());
565  for (const auto &var : this->vars_)
566  var->fill(selobjs, *out);
567  return out;
568  }
569 };
570 
571 template <typename T>
573 public:
576 
578 
581  descriptions.addWithDefaultLabel(desc);
582  }
583 
584  std::unique_ptr<nanoaod::FlatTable> fillTable(const edm::Event &iEvent,
585  const edm::Handle<edm::View<T>> &prod) const override {
586  auto out = std::make_unique<nanoaod::FlatTable>(1, this->name_, true, this->extension_);
587  std::vector<const T *> selobjs(1, &(*prod)[0]);
588  for (const auto &var : this->vars_)
589  var->fill(selobjs, *out);
590  return out;
591  }
592 };
593 
594 // LuminosityBlock producers
595 // - ABC
596 // - Singleton
597 // - Collection
598 template <typename T, typename TProd>
600  : public edm::one::EDProducer<edm::EndLuminosityBlockProducer, edm::LuminosityBlockCache<int>> {
601 public:
603  : name_(params.getParameter<std::string>("name")),
604  doc_(params.existsAs<std::string>("doc") ? params.getParameter<std::string>("doc") : ""),
605  extension_(params.existsAs<bool>("extension") ? params.getParameter<bool>("extension") : false),
607 
608  params.existsAs<bool>("skipNonExistingSrc") ? params.getParameter<bool>("skipNonExistingSrc") : false),
609  src_(consumes<TProd, edm::InLumi>(params.getParameter<edm::InputTag>("src"))) {
610  edm::ParameterSet const &varsPSet = params.getParameter<edm::ParameterSet>("variables");
611  for (const std::string &vname : varsPSet.getParameterNamesForType<edm::ParameterSet>()) {
612  const auto &varPSet = varsPSet.getParameter<edm::ParameterSet>(vname);
613  const std::string &type = varPSet.getParameter<std::string>("type");
614  if (type == "int")
615  vars_.push_back(std::make_unique<IntVar>(vname, varPSet));
616  else if (type == "float")
617  vars_.push_back(std::make_unique<FloatVar>(vname, varPSet));
618  else if (type == "uint8")
619  vars_.push_back(std::make_unique<UInt8Var>(vname, varPSet));
620  else if (type == "bool")
621  vars_.push_back(std::make_unique<BoolVar>(vname, varPSet));
622  else
623  throw cms::Exception("Configuration", "unsupported type " + type + " for variable " + vname);
624  }
625 
626  produces<nanoaod::FlatTable, edm::Transition::EndLuminosityBlock>();
627  }
628 
630 
631  std::shared_ptr<int> globalBeginLuminosityBlock(edm::LuminosityBlock const &,
632  edm::EventSetup const &) const override {
633  return nullptr;
634  }
635 
637 
638  // this is to be overriden by the child class
639  virtual std::unique_ptr<nanoaod::FlatTable> fillTable(const edm::LuminosityBlock &iLumi,
640  const edm::Handle<TProd> &prod) const = 0;
641 
642  void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override {
643  // do nothing
644  }
645 
648  iLumi.getByToken(src_, src);
649 
650  std::unique_ptr<nanoaod::FlatTable> out = fillTable(iLumi, src);
651  out->setDoc(doc_);
652 
653  iLumi.put(std::move(out));
654  }
655 
656 protected:
659  const bool extension_;
662 
667  std::vector<std::unique_ptr<Variable<T>>> vars_;
668 };
669 
670 // Class for singletons like GenFilterInfo
671 template <typename T>
673 public:
676 
678 
681  descriptions.addWithDefaultLabel(desc);
682  }
683 
684  std::unique_ptr<nanoaod::FlatTable> fillTable(const edm::LuminosityBlock &,
685  const edm::Handle<T> &prod) const override {
686  auto out = std::make_unique<nanoaod::FlatTable>(1, this->name_, true, this->extension_);
687  std::vector<const T *> selobjs(1, prod.product());
688  for (const auto &var : this->vars_)
689  var->fill(selobjs, *out);
690  return out;
691  }
692 };
693 
694 // Class for generic collections
695 template <typename T, typename TProd>
697 public:
700  maxLen_(params.existsAs<unsigned int>("maxLen") ? params.getParameter<unsigned int>("maxLen")
701  : std::numeric_limits<unsigned int>::max()),
702  cut_(params.existsAs<std::string>("cut") ? params.getParameter<std::string>("cut") : "", true) {}
703 
705 
708  desc.addOptional<unsigned int>("maxLen")->setComment(
709  "define the maximum length of the input collection to put in the branch");
710  descriptions.addWithDefaultLabel(desc);
711  }
712 
713  std::unique_ptr<nanoaod::FlatTable> fillTable(const edm::LuminosityBlock &iLumi,
714  const edm::Handle<TProd> &prod) const override {
715  std::vector<const T *> selobjs;
716  if (prod.isValid() || !(this->skipNonExistingSrc_)) {
717  for (unsigned int i = 0, n = prod->size(); i < n; ++i) {
718  const auto &obj = (*prod)[i];
719  if (cut_(obj)) {
720  selobjs.push_back(&obj);
721  }
722  if (selobjs.size() >= maxLen_)
723  break;
724  }
725  }
726  auto out = std::make_unique<nanoaod::FlatTable>(selobjs.size(), this->name_, false, this->extension_);
727  for (const auto &var : this->vars_)
728  var->fill(selobjs, *out);
729  return out;
730  }
731 
732 protected:
733  const unsigned int maxLen_;
735 };
736 
737 // Run producers
738 // - ABC
739 // - Singleton
740 // - Collection
741 template <typename T, typename TProd>
742 class SimpleFlatTableProducerBaseRun : public edm::one::EDProducer<edm::EndRunProducer, edm::RunCache<int>> {
743 public:
745  : name_(params.getParameter<std::string>("name")),
746  doc_(params.existsAs<std::string>("doc") ? params.getParameter<std::string>("doc") : ""),
747  extension_(params.existsAs<bool>("extension") ? params.getParameter<bool>("extension") : false),
749 
750  params.existsAs<bool>("skipNonExistingSrc") ? params.getParameter<bool>("skipNonExistingSrc") : false),
751  src_(consumes<TProd, edm::InRun>(params.getParameter<edm::InputTag>("src"))) {
752  edm::ParameterSet const &varsPSet = params.getParameter<edm::ParameterSet>("variables");
753  for (const std::string &vname : varsPSet.getParameterNamesForType<edm::ParameterSet>()) {
754  const auto &varPSet = varsPSet.getParameter<edm::ParameterSet>(vname);
755  const std::string &type = varPSet.getParameter<std::string>("type");
756  if (type == "int")
757  vars_.push_back(std::make_unique<IntVar>(vname, varPSet));
758  else if (type == "float")
759  vars_.push_back(std::make_unique<FloatVar>(vname, varPSet));
760  else if (type == "uint8")
761  vars_.push_back(std::make_unique<UInt8Var>(vname, varPSet));
762  else if (type == "bool")
763  vars_.push_back(std::make_unique<BoolVar>(vname, varPSet));
764  else
765  throw cms::Exception("Configuration", "unsupported type " + type + " for variable " + vname);
766  }
767 
768  produces<nanoaod::FlatTable, edm::Transition::EndRun>();
769  }
770 
772 
773  std::shared_ptr<int> globalBeginRun(edm::Run const &, edm::EventSetup const &) const override { return nullptr; }
774 
775  void globalEndRun(edm::Run const &, edm::EventSetup const &) override {}
776 
777  // this is to be overriden by the child class
778  virtual std::unique_ptr<nanoaod::FlatTable> fillTable(const edm::Run &iRun, const edm::Handle<TProd> &prod) const = 0;
779 
780  void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override {
781  // do nothing
782  }
783 
784  void endRunProduce(edm::Run &iRun, const edm::EventSetup &iSetup) final {
786  iRun.getByToken(src_, src);
787 
788  std::unique_ptr<nanoaod::FlatTable> out = fillTable(iRun, src);
789  out->setDoc(doc_);
790 
791  iRun.put(std::move(out));
792  }
793 
794 protected:
797  const bool extension_;
800 
805  std::vector<std::unique_ptr<Variable<T>>> vars_;
806 };
807 
808 // Class for singletons like GenFilterInfo
809 template <typename T>
811 public:
813 
815 
816  std::unique_ptr<nanoaod::FlatTable> fillTable(const edm::Run &, const edm::Handle<T> &prod) const override {
817  auto out = std::make_unique<nanoaod::FlatTable>(1, this->name_, true, this->extension_);
818  std::vector<const T *> selobjs(1, prod.product());
819  for (const auto &var : this->vars_)
820  var->fill(selobjs, *out);
821  return out;
822  }
823 };
824 
825 // Class for generic collections
826 template <typename T, typename TProd>
828 public:
831  maxLen_(params.existsAs<unsigned int>("maxLen") ? params.getParameter<unsigned int>("maxLen")
832  : std::numeric_limits<unsigned int>::max()),
833  cut_(params.existsAs<std::string>("cut") ? params.getParameter<std::string>("cut") : "", true) {}
834 
836 
837  std::unique_ptr<nanoaod::FlatTable> fillTable(const edm::Run &iRun, const edm::Handle<TProd> &prod) const override {
838  std::vector<const T *> selobjs;
839  if (prod.isValid() || !(this->skipNonExistingSrc_)) {
840  for (unsigned int i = 0, n = prod->size(); i < n; ++i) {
841  const auto &obj = (*prod)[i];
842  if (cut_(obj)) {
843  selobjs.push_back(&obj);
844  }
845  if (selobjs.size() >= maxLen_)
846  break;
847  }
848  }
849  auto out = std::make_unique<nanoaod::FlatTable>(selobjs.size(), this->name_, false, this->extension_);
850  for (const auto &var : this->vars_)
851  var->fill(selobjs, *out);
852  return out;
853  }
854 
855 protected:
856  const unsigned int maxLen_;
858 };
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
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