CMS 3D CMS Logo

GlobalVariablesTableProducer.cc
Go to the documentation of this file.
6 
7 #include <utility>
8 #include <vector>
9 #include <boost/ptr_container/ptr_vector.hpp>
10 
12 public:
14  edm::ParameterSet const& varsPSet = params.getParameter<edm::ParameterSet>("variables");
15  for (const std::string& vname : varsPSet.getParameterNamesForType<edm::ParameterSet>()) {
16  const auto& varPSet = varsPSet.getParameter<edm::ParameterSet>(vname);
17  const std::string& type = varPSet.getParameter<std::string>("type");
18  if (type == "int")
19  vars_.push_back(new IntVar(vname, nanoaod::FlatTable::IntColumn, varPSet, consumesCollector()));
20  else if (type == "float")
21  vars_.push_back(new FloatVar(vname, nanoaod::FlatTable::FloatColumn, varPSet, consumesCollector()));
22  else if (type == "double")
23  vars_.push_back(new DoubleVar(vname, nanoaod::FlatTable::FloatColumn, varPSet, consumesCollector()));
24  else if (type == "bool")
25  vars_.push_back(new BoolVar(vname, nanoaod::FlatTable::BoolColumn, varPSet, consumesCollector()));
26  else if (type == "candidatescalarsum")
27  vars_.push_back(
28  new CandidateScalarSumVar(vname, nanoaod::FlatTable::FloatColumn, varPSet, consumesCollector()));
29  else if (type == "candidatesize")
30  vars_.push_back(new CandidateSizeVar(vname, nanoaod::FlatTable::IntColumn, varPSet, consumesCollector()));
31  else if (type == "candidatesummass")
32  vars_.push_back(new CandidateSumMassVar(vname, nanoaod::FlatTable::FloatColumn, varPSet, consumesCollector()));
33  else
34  throw cms::Exception("Configuration", "unsupported type " + type + " for variable " + vname);
35  }
36 
37  produces<nanoaod::FlatTable>();
38  }
39 
41 
42  void produce(edm::Event& iEvent, const edm::EventSetup& iSetup) override {
43  auto out = std::make_unique<nanoaod::FlatTable>(1, "", true);
44 
45  for (const auto& var : vars_)
46  var.fill(iEvent, *out);
47 
48  iEvent.put(std::move(out));
49  }
50 
51 protected:
52  class Variable {
53  public:
55  : name_(aname), doc_(cfg.getParameter<std::string>("doc")), type_(atype) {}
56  virtual void fill(const edm::Event& iEvent, nanoaod::FlatTable& out) const = 0;
57  virtual ~Variable() {}
58  const std::string& name() const { return name_; }
59  const nanoaod::FlatTable::ColumnType& type() const { return type_; }
60 
61  protected:
64  };
65  template <typename ValType>
66  class Identity {
67  public:
68  static ValType convert(ValType x) { return x; }
69  };
70  template <typename ValType>
71  class Size {
72  public:
73  static int convert(ValType x) { return x.size(); }
74  };
75 
76  template <typename ColType, typename ValType>
77  class Max {
78  public:
79  static ColType convert(ValType x) {
81  for (const auto& i : x)
82  if (i > v)
83  v = i;
84  return v;
85  }
86  };
87  template <typename ColType, typename ValType>
88  class Min {
89  public:
90  static ColType convert(ValType x) {
92  for (const auto& i : x)
93  if (i < v)
94  v = i;
95  return v;
96  }
97  };
98  template <typename ColType, typename ValType>
99  class ScalarPtSum {
100  public:
101  static ColType convert(ValType x) {
102  ColType v = 0;
103  for (const auto& i : x)
104  v += i.pt();
105  return v;
106  }
107  };
108  template <typename ColType, typename ValType>
109  class MassSum {
110  public:
111  static ColType convert(ValType x) {
112  if (x.empty())
113  return 0;
114  auto v = x[0].p4();
115  for (const auto& i : x)
116  v += i.p4();
117  return v.mass();
118  }
119  };
120  template <typename ColType, typename ValType>
121  class PtVectorSum {
122  public:
123  static ColType convert(ValType x) {
124  if (x.empty())
125  return 0;
126  auto v = x[0].p4();
127  v -= x[0].p4();
128  for (const auto& i : x)
129  v += i.p4();
130  return v.pt();
131  }
132  };
133 
134  template <typename ValType, typename ColType = ValType, typename Converter = Identity<ValType>>
135  class VariableT : public Variable {
136  public:
137  VariableT(const std::string& aname,
139  const edm::ParameterSet& cfg,
141  : Variable(aname, atype, cfg), src_(cc.consumes<ValType>(cfg.getParameter<edm::InputTag>("src"))) {}
142  ~VariableT() override {}
143  void fill(const edm::Event& iEvent, nanoaod::FlatTable& out) const override {
145  iEvent.getByToken(src_, handle);
146  out.template addColumnValue<ColType>(this->name_, Converter::convert(*handle), this->doc_, this->type_);
147  }
148 
149  protected:
151  };
158  typedef VariableT<edm::View<reco::Candidate>, float, MassSum<float, edm::View<reco::Candidate>>> CandidateSumMassVar;
159  typedef VariableT<edm::View<reco::Candidate>, int, Size<edm::View<reco::Candidate>>> CandidateSizeVar;
160  boost::ptr_vector<Variable> vars_;
161 };
162 
type
Definition: HCALResponse.h:21
T getParameter(std::string const &) const
VariableT(const std::string &aname, nanoaod::FlatTable::ColumnType atype, const edm::ParameterSet &cfg, edm::ConsumesCollector &&cc)
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:131
void fill(const edm::Event &iEvent, nanoaod::FlatTable &out) const override
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:525
VariableT< edm::View< reco::Candidate >, int, Size< edm::View< reco::Candidate > > > CandidateSizeVar
std::vector< std::string > getParameterNamesForType(bool trackiness=true) const
Definition: ParameterSet.h:168
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
const nanoaod::FlatTable::ColumnType & type() const
T min(T a, T b)
Definition: MathUtil.h:58
def convert(infile, ofile)
void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override
VariableT< edm::View< reco::Candidate >, float, ScalarPtSum< float, edm::View< reco::Candidate > > > CandidateScalarSumVar
HLT enums.
boost::ptr_vector< Variable > vars_
Variable(const std::string &aname, nanoaod::FlatTable::ColumnType atype, const edm::ParameterSet &cfg)
GlobalVariablesTableProducer(edm::ParameterSet const &params)
virtual void fill(const edm::Event &iEvent, nanoaod::FlatTable &out) const =0
def move(src, dest)
Definition: eostools.py:511
VariableT< edm::View< reco::Candidate >, float, MassSum< float, edm::View< reco::Candidate > > > CandidateSumMassVar