CMS 3D CMS Logo

GlobalVariablesTableProducer.cc
Go to the documentation of this file.
9 
10 #include <memory>
11 #include <utility>
12 #include <vector>
13 
15 public:
17  : name_(params.getParameter<std::string>("name")), extension_(params.getParameter<bool>("extension")) {
18  edm::ParameterSet const& varsPSet = params.getParameter<edm::ParameterSet>("variables");
19  for (const std::string& vname : varsPSet.getParameterNamesForType<edm::ParameterSet>()) {
20  const auto& varPSet = varsPSet.getParameter<edm::ParameterSet>(vname);
21  const std::string& type = varPSet.getParameter<std::string>("type");
22  if (type == "int")
23  vars_.push_back(std::make_unique<IntVar>(vname, varPSet, consumesCollector()));
24  else if (type == "float")
25  vars_.push_back(std::make_unique<FloatVar>(vname, varPSet, consumesCollector()));
26  else if (type == "double")
27  vars_.push_back(std::make_unique<DoubleVar>(vname, varPSet, consumesCollector()));
28  else if (type == "bool")
29  vars_.push_back(std::make_unique<BoolVar>(vname, varPSet, consumesCollector()));
30  else if (type == "candidatescalarsum")
31  vars_.push_back(std::make_unique<CandidateScalarSumVar>(vname, varPSet, consumesCollector()));
32  else if (type == "candidatesize")
33  vars_.push_back(std::make_unique<CandidateSizeVar>(vname, varPSet, consumesCollector()));
34  else if (type == "candidatesummass")
35  vars_.push_back(std::make_unique<CandidateSumMassVar>(vname, varPSet, consumesCollector()));
36  else
37  throw cms::Exception("Configuration", "unsupported type " + type + " for variable " + vname);
38  }
39 
40  produces<nanoaod::FlatTable>();
41  }
42 
44 
45  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
47  desc.add<std::string>("name", "")->setComment("name of the branch in the flat table output");
48  desc.add<bool>("extension", false)->setComment("whether or not to extend an existing same table");
51  "type", "int", true, edm::Comment("the c++ type of the branch in the flat table")),
52  edm::allowedValues<std::string>(
53  "int", "float", "double", "bool", "candidatescalarsum", "candidatesize", "candidatesummass"));
54  variable.add<edm::InputTag>("src")->setComment("input collection for the branch");
55  variable.add<std::string>("doc")->setComment("few words description of the branch content");
56  variable.add<int>("precision", -1)->setComment("precision to store the information");
58  variables.setComment("a parameters set to define variable to fill the flat table");
59  variables.addNode(
61  desc.add<edm::ParameterSetDescription>("variables", variables);
62 
63  descriptions.addWithDefaultLabel(desc);
64  }
65  void produce(edm::Event& iEvent, const edm::EventSetup& iSetup) override {
66  auto out = std::make_unique<nanoaod::FlatTable>(1, this->name_, true, this->extension_);
67 
68  for (const auto& var : vars_)
69  var->fill(iEvent, *out);
70 
71  iEvent.put(std::move(out));
72  }
73 
74 protected:
75  class Variable {
76  public:
77  Variable(const std::string& aname, const edm::ParameterSet& cfg)
78  : name_(aname), doc_(cfg.getParameter<std::string>("doc")), precision_(cfg.getParameter<int>("precision")) {}
79  virtual void fill(const edm::Event& iEvent, nanoaod::FlatTable& out) const = 0;
80  virtual ~Variable() {}
81  const std::string& name() const { return name_; }
82 
83  protected:
86  };
87  template <typename ValType>
88  class Identity {
89  public:
90  static ValType convert(ValType x) { return x; }
91  };
92  template <typename ValType>
93  class Size {
94  public:
95  static int convert(ValType x) { return x.size(); }
96  };
97 
98  template <typename ColType, typename ValType>
99  class Max {
100  public:
101  static ColType convert(ValType x) {
103  for (const auto& i : x)
104  if (i > v)
105  v = i;
106  return v;
107  }
108  };
109  template <typename ColType, typename ValType>
110  class Min {
111  public:
112  static ColType convert(ValType x) {
114  for (const auto& i : x)
115  if (i < v)
116  v = i;
117  return v;
118  }
119  };
120  template <typename ColType, typename ValType>
121  class ScalarPtSum {
122  public:
123  static ColType convert(ValType x) {
124  ColType v = 0;
125  for (const auto& i : x)
126  v += i.pt();
127  return v;
128  }
129  };
130  template <typename ColType, typename ValType>
131  class MassSum {
132  public:
133  static ColType convert(ValType x) {
134  if (x.empty())
135  return 0;
136  auto v = x[0].p4();
137  for (const auto& i : x)
138  v += i.p4();
139  return v.mass();
140  }
141  };
142  template <typename ColType, typename ValType>
143  class PtVectorSum {
144  public:
145  static ColType convert(ValType x) {
146  if (x.empty())
147  return 0;
148  auto v = x[0].p4();
149  v -= x[0].p4();
150  for (const auto& i : x)
151  v += i.p4();
152  return v.pt();
153  }
154  };
155 
156  template <typename ValType, typename ColType = ValType, typename Converter = Identity<ValType>>
157  class VariableT : public Variable {
158  public:
160  : Variable(aname, cfg), src_(cc.consumes<ValType>(cfg.getParameter<edm::InputTag>("src"))) {}
161  ~VariableT() override {}
162  void fill(const edm::Event& iEvent, nanoaod::FlatTable& out) const override {
163  out.template addColumnValue<ColType>(
164  this->name_, Converter::convert(iEvent.get(src_)), this->doc_, this->precision_);
165  }
166 
167  protected:
169  };
178  std::vector<std::unique_ptr<Variable>> vars_;
180  const bool extension_;
181 };
182 
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
bool get(ProductID const &oid, Handle< PROD > &result) const
Definition: Event.h:344
uint32_t cc[maxCellsPerHit]
Definition: gpuFishbone.h:49
void fill(const edm::Event &iEvent, nanoaod::FlatTable &out) const override
VariableT< edm::View< reco::Candidate >, int, Size< edm::View< reco::Candidate > > > CandidateSizeVar
virtual void fill(const edm::Event &iEvent, nanoaod::FlatTable &out) const =0
int iEvent
Definition: GenABIO.cc:224
std::vector< std::string > getParameterNamesForType(bool trackiness=true) const
Definition: ParameterSet.h:180
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
VariableT(const std::string &aname, const edm::ParameterSet &cfg, edm::ConsumesCollector &&cc)
def convert(infile, ofile)
void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
VariableT< edm::View< reco::Candidate >, float, ScalarPtSum< float, edm::View< reco::Candidate > > > CandidateScalarSumVar
HLT enums.
GlobalVariablesTableProducer(edm::ParameterSet const &params)
std::vector< std::unique_ptr< Variable > > vars_
Variable(const std::string &aname, const edm::ParameterSet &cfg)
def move(src, dest)
Definition: eostools.py:511
VariableT< edm::View< reco::Candidate >, float, MassSum< float, edm::View< reco::Candidate > > > CandidateSumMassVar