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:
13 
15  {
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") vars_.push_back(new IntVar(vname, nanoaod::FlatTable::IntColumn, varPSet, consumesCollector()));
21  else if (type == "float") vars_.push_back(new FloatVar(vname, nanoaod::FlatTable::FloatColumn, varPSet, consumesCollector()));
22  else if (type == "double") vars_.push_back(new DoubleVar(vname, nanoaod::FlatTable::FloatColumn, varPSet, consumesCollector()));
23  else if (type == "bool") vars_.push_back(new BoolVar(vname, nanoaod::FlatTable::UInt8Column, varPSet, consumesCollector()));
24  else if (type == "candidatescalarsum") vars_.push_back(new CandidateScalarSumVar(vname, nanoaod::FlatTable::FloatColumn, varPSet, consumesCollector()));
25  else if (type == "candidatesize") vars_.push_back(new CandidateSizeVar(vname, nanoaod::FlatTable::IntColumn, varPSet, consumesCollector()));
26  else if (type == "candidatesummass") vars_.push_back(new CandidateSumMassVar(vname, nanoaod::FlatTable::FloatColumn, varPSet, consumesCollector()));
27  else throw cms::Exception("Configuration", "unsupported type "+type+" for variable "+vname);
28  }
29 
30  produces<nanoaod::FlatTable>();
31  }
32 
34 
35  void produce(edm::Event& iEvent, const edm::EventSetup& iSetup) override {
36  auto out = std::make_unique<nanoaod::FlatTable>(1, "", true);
37 
38  for (const auto & var : vars_) var.fill(iEvent, *out);
39 
40  iEvent.put(std::move(out));
41  }
42 
43  protected:
44  class Variable {
45  public:
47  name_(aname), doc_(cfg.getParameter<std::string>("doc")), type_(atype) {}
48  virtual void fill(const edm::Event &iEvent, nanoaod::FlatTable & out) const = 0;
49  virtual ~Variable() {}
50  const std::string & name() const { return name_; }
51  const nanoaod::FlatTable::ColumnType & type() const { return type_; }
52  protected:
55  };
56  template <typename ValType>
57  class Identity {
58  public:
59  static ValType convert(ValType x){return x;}
60 
61  };
62  template <typename ValType>
63  class Size {
64  public:
65  static int convert(ValType x){return x.size();}
66 
67  };
68 
69  template <typename ColType,typename ValType>
70  class Max {
71  public:
72  static ColType convert(ValType x){
74  for(const auto & i : x) if(i>v) v=i;
75  return v;
76  }
77  };
78  template <typename ColType,typename ValType>
79  class Min {
80  public:
81  static ColType convert(ValType x){
83  for(const auto & i : x) if(i<v) v=i;
84  return v;
85  }
86  };
87  template <typename ColType,typename ValType>
88  class ScalarPtSum {
89  public:
90  static ColType convert(ValType x){
91  ColType v=0;
92  for(const auto & i : x) v+=i.pt();
93  return v;
94  }
95  };
96  template <typename ColType,typename ValType>
97  class MassSum {
98  public:
99  static ColType convert(ValType x){
100  if(x.empty()) return 0;
101  auto v=x[0].p4();
102  for(const auto & i : x) v+=i.p4();
103  return v.mass();
104  }
105  };
106  template <typename ColType,typename ValType>
107  class PtVectorSum {
108  public:
109  static ColType convert(ValType x){
110  if(x.empty()) return 0;
111  auto v=x[0].p4();
112  v-=x[0].p4();
113  for(const auto & i : x) v+=i.p4();
114  return v.pt();
115  }
116  };
117 
118 
119 
120  template<typename ValType, typename ColType=ValType, typename Converter=Identity<ValType> >
121  class VariableT : public Variable {
122  public:
124  Variable(aname, atype, cfg), src_(cc.consumes<ValType>(cfg.getParameter<edm::InputTag>("src"))) {}
125  ~VariableT() override {}
126  void fill(const edm::Event &iEvent, nanoaod::FlatTable & out) const override {
128  iEvent.getByToken(src_, handle);
129  out.template addColumnValue<ColType>(this->name_, Converter::convert(*handle), this->doc_, this->type_);
130  }
131  protected:
133  };
139  typedef VariableT<edm::View<reco::Candidate>,float,MassSum<float,edm::View<reco::Candidate>>> CandidateSumMassVar;
140  typedef VariableT<edm::View<reco::Candidate>,int,Size<edm::View<reco::Candidate>>> CandidateSizeVar;
141  boost::ptr_vector<Variable> vars_;
142 };
143 
146 
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:137
void fill(const edm::Event &iEvent, nanoaod::FlatTable &out) const override
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:579
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
VariableT< edm::View< reco::Candidate >, int, Size< edm::View< reco::Candidate > > > CandidateSizeVar
std::vector< std::string > getParameterNamesForType(bool trackiness=true) const
Definition: ParameterSet.h:193
int iEvent
Definition: GenABIO.cc:230
const nanoaod::FlatTable::ColumnType & type() const
VariableT< edm::View< reco::Candidate >, float, ScalarPtSum< float, edm::View< reco::Candidate > > > CandidateScalarSumVar
T min(T a, T b)
Definition: MathUtil.h:58
def convert(infile, ofile)
void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override
HLT enums.
boost::ptr_vector< Variable > vars_
VariableT< edm::View< reco::Candidate >, float, MassSum< float, edm::View< reco::Candidate > > > CandidateSumMassVar
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