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