CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
GlobalVariablesTableProducer.cc
Go to the documentation of this file.
6 
7 #include <memory>
8 #include <utility>
9 #include <vector>
10 
12 public:
14  : name_(params.existsAs<std::string>("name") ? params.getParameter<std::string>("name") : ""),
15  extension_(params.existsAs<bool>("extension") ? params.getParameter<bool>("extension") : false) {
16  edm::ParameterSet const& varsPSet = params.getParameter<edm::ParameterSet>("variables");
17  for (const std::string& vname : varsPSet.getParameterNamesForType<edm::ParameterSet>()) {
18  const auto& varPSet = varsPSet.getParameter<edm::ParameterSet>(vname);
19  const std::string& type = varPSet.getParameter<std::string>("type");
20  if (type == "int")
21  vars_.push_back(std::make_unique<IntVar>(vname, varPSet, consumesCollector()));
22  else if (type == "float")
23  vars_.push_back(std::make_unique<FloatVar>(vname, varPSet, consumesCollector()));
24  else if (type == "double")
25  vars_.push_back(std::make_unique<DoubleVar>(vname, varPSet, consumesCollector()));
26  else if (type == "bool")
27  vars_.push_back(std::make_unique<BoolVar>(vname, varPSet, consumesCollector()));
28  else if (type == "candidatescalarsum")
29  vars_.push_back(std::make_unique<CandidateScalarSumVar>(vname, varPSet, consumesCollector()));
30  else if (type == "candidatesize")
31  vars_.push_back(std::make_unique<CandidateSizeVar>(vname, varPSet, consumesCollector()));
32  else if (type == "candidatesummass")
33  vars_.push_back(std::make_unique<CandidateSumMassVar>(vname, varPSet, consumesCollector()));
34  else
35  throw cms::Exception("Configuration", "unsupported type " + type + " for variable " + vname);
36  }
37 
38  produces<nanoaod::FlatTable>();
39  }
40 
42 
43  void produce(edm::Event& iEvent, const edm::EventSetup& iSetup) override {
44  auto out = std::make_unique<nanoaod::FlatTable>(1, this->name_, true, this->extension_);
45 
46  for (const auto& var : vars_)
47  var->fill(iEvent, *out);
48 
49  iEvent.put(std::move(out));
50  }
51 
52 protected:
53  class Variable {
54  public:
55  Variable(const std::string& aname, const edm::ParameterSet& cfg)
56  : name_(aname), doc_(cfg.getParameter<std::string>("doc")) {}
57  virtual void fill(const edm::Event& iEvent, nanoaod::FlatTable& out) const = 0;
58  virtual ~Variable() {}
59  const std::string& name() const { return name_; }
60 
61  protected:
63  };
64  template <typename ValType>
65  class Identity {
66  public:
67  static ValType convert(ValType x) { return x; }
68  };
69  template <typename ValType>
70  class Size {
71  public:
72  static int convert(ValType x) { return x.size(); }
73  };
74 
75  template <typename ColType, typename ValType>
76  class Max {
77  public:
78  static ColType convert(ValType x) {
80  for (const auto& i : x)
81  if (i > v)
82  v = i;
83  return v;
84  }
85  };
86  template <typename ColType, typename ValType>
87  class Min {
88  public:
89  static ColType convert(ValType x) {
91  for (const auto& i : x)
92  if (i < v)
93  v = i;
94  return v;
95  }
96  };
97  template <typename ColType, typename ValType>
98  class ScalarPtSum {
99  public:
100  static ColType convert(ValType x) {
101  ColType v = 0;
102  for (const auto& i : x)
103  v += i.pt();
104  return v;
105  }
106  };
107  template <typename ColType, typename ValType>
108  class MassSum {
109  public:
110  static ColType convert(ValType x) {
111  if (x.empty())
112  return 0;
113  auto v = x[0].p4();
114  for (const auto& i : x)
115  v += i.p4();
116  return v.mass();
117  }
118  };
119  template <typename ColType, typename ValType>
120  class PtVectorSum {
121  public:
122  static ColType convert(ValType x) {
123  if (x.empty())
124  return 0;
125  auto v = x[0].p4();
126  v -= x[0].p4();
127  for (const auto& i : x)
128  v += i.p4();
129  return v.pt();
130  }
131  };
132 
133  template <typename ValType, typename ColType = ValType, typename Converter = Identity<ValType>>
134  class VariableT : public Variable {
135  public:
137  : Variable(aname, cfg), src_(cc.consumes<ValType>(cfg.getParameter<edm::InputTag>("src"))) {}
138  ~VariableT() override {}
139  void fill(const edm::Event& iEvent, nanoaod::FlatTable& out) const override {
140  out.template addColumnValue<ColType>(this->name_, Converter::convert(iEvent.get(src_)), this->doc_);
141  }
142 
143  protected:
145  };
154  std::vector<std::unique_ptr<Variable>> vars_;
156  const bool extension_;
157 };
158 
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:133
tuple cfg
Definition: looper.py:296
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
void fill(const edm::Event &iEvent, nanoaod::FlatTable &out) const override
VariableT< edm::View< reco::Candidate >, int, Size< edm::View< reco::Candidate > > > CandidateSizeVar
std::vector< std::string > getParameterNamesForType(bool trackiness=true) const
Definition: ParameterSet.h:179
virtual void fill(const edm::Event &iEvent, nanoaod::FlatTable &out) const =0
int iEvent
Definition: GenABIO.cc:224
def move
Definition: eostools.py:511
bool get(ProductID const &oid, Handle< PROD > &result) const
Definition: Event.h:346
VariableT(const std::string &aname, const edm::ParameterSet &cfg, edm::ConsumesCollector &&cc)
void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
VariableT< edm::View< reco::Candidate >, float, ScalarPtSum< float, edm::View< reco::Candidate > > > CandidateScalarSumVar
GlobalVariablesTableProducer(edm::ParameterSet const &params)
std::vector< std::unique_ptr< Variable > > vars_
Variable(const std::string &aname, const edm::ParameterSet &cfg)
VariableT< edm::View< reco::Candidate >, float, MassSum< float, edm::View< reco::Candidate > > > CandidateSumMassVar