CMS 3D CMS Logo

SimpleFlatTableProducer.h
Go to the documentation of this file.
7 
10 
11 #include <vector>
12 #include <boost/ptr_container/ptr_vector.hpp>
13 
14 template<typename T, typename TProd>
16  public:
17 
19  name_( params.getParameter<std::string>("name") ),
20  doc_(params.existsAs<std::string>("doc") ? params.getParameter<std::string>("doc") : ""),
21  extension_(params.existsAs<bool>("extension") ? params.getParameter<bool>("extension") : false),
22  src_(consumes<TProd>( params.getParameter<edm::InputTag>("src") ))
23  {
24  edm::ParameterSet const & varsPSet = params.getParameter<edm::ParameterSet>("variables");
25  for (const std::string & vname : varsPSet.getParameterNamesForType<edm::ParameterSet>()) {
26  const auto & varPSet = varsPSet.getParameter<edm::ParameterSet>(vname);
27  const std::string & type = varPSet.getParameter<std::string>("type");
28  if (type == "int") vars_.push_back(new IntVar(vname, nanoaod::FlatTable::IntColumn, varPSet));
29  else if (type == "float") vars_.push_back(new FloatVar(vname, nanoaod::FlatTable::FloatColumn, varPSet));
30  else if (type == "uint8") vars_.push_back(new UInt8Var(vname, nanoaod::FlatTable::UInt8Column, varPSet));
31  else if (type == "bool") vars_.push_back(new BoolVar(vname, nanoaod::FlatTable::BoolColumn, varPSet));
32  else throw cms::Exception("Configuration", "unsupported type "+type+" for variable "+vname);
33  }
34 
35  produces<nanoaod::FlatTable>();
36  }
37 
39 
40  // this is to be overriden by the child class
41  virtual std::unique_ptr<nanoaod::FlatTable> fillTable(const edm::Event &iEvent, const edm::Handle<TProd> & prod) const = 0;
42 
43 
44  void produce(edm::Event& iEvent, const edm::EventSetup& iSetup) override {
46  iEvent.getByToken(src_, src);
47 
48  std::unique_ptr<nanoaod::FlatTable> out = fillTable(iEvent, src);
49  out->setDoc(doc_);
50 
51  iEvent.put(std::move(out));
52  }
53 
54  protected:
57  const bool extension_;
59 
60  class VariableBase {
61  public:
63  name_(aname), doc_(cfg.getParameter<std::string>("doc")), type_(atype),
64  precision_(cfg.existsAs<int>("precision") ? cfg.getParameter<int>("precision") : -1)
65  {
66  }
67  virtual ~VariableBase() {}
68  const std::string & name() const { return name_; }
69  const nanoaod::FlatTable::ColumnType & type() const { return type_; }
70  protected:
74  };
75  class Variable : public VariableBase {
76  public:
78  VariableBase(aname, atype, cfg) {}
79  virtual void fill(std::vector<const T *> selobjs, nanoaod::FlatTable & out) const = 0;
80  };
81  template<typename StringFunctor, typename ValType>
82  class FuncVariable : public Variable {
83  public:
85  Variable(aname, atype, cfg), func_(cfg.getParameter<std::string>("expr"), true) {}
86  ~FuncVariable() override {}
87  void fill(std::vector<const T *> selobjs, nanoaod::FlatTable & out) const override {
88  std::vector<ValType> vals(selobjs.size());
89  for (unsigned int i = 0, n = vals.size(); i < n; ++i) {
90  vals[i] = func_(*selobjs[i]);
91  }
92  out.template addColumn<ValType>(this->name_, vals, this->doc_, this->type_,this->precision_);
93  }
94  protected:
95  StringFunctor func_;
96 
97  };
98  typedef FuncVariable<StringObjectFunction<T>,int> IntVar;
99  typedef FuncVariable<StringObjectFunction<T>,float> FloatVar;
100  typedef FuncVariable<StringObjectFunction<T>,uint8_t> UInt8Var;
101  typedef FuncVariable<StringCutObjectSelector<T>,uint8_t> BoolVar;
102  boost::ptr_vector<Variable> vars_;
103 };
104 
105 template<typename T>
106 class SimpleFlatTableProducer : public SimpleFlatTableProducerBase<T, edm::View<T>> {
107  public:
109 
111  SimpleFlatTableProducerBase<T, edm::View<T>>(params),
112  singleton_(params.getParameter<bool>("singleton")),
113  maxLen_(params.existsAs<unsigned int>("maxLen") ? params.getParameter<unsigned int>("maxLen") : std::numeric_limits<unsigned int>::max()),
114  cut_(!singleton_ ? params.getParameter<std::string>("cut") : "", true)
115  {
116  if (params.existsAs<edm::ParameterSet>("externalVariables")) {
117  edm::ParameterSet const & extvarsPSet = params.getParameter<edm::ParameterSet>("externalVariables");
118  for (const std::string & vname : extvarsPSet.getParameterNamesForType<edm::ParameterSet>()) {
119  const auto & varPSet = extvarsPSet.getParameter<edm::ParameterSet>(vname);
120  const std::string & type = varPSet.getParameter<std::string>("type");
121  if (type == "int") extvars_.push_back(new IntExtVar(vname, nanoaod::FlatTable::IntColumn, varPSet, this->consumesCollector()));
122  else if (type == "float") extvars_.push_back(new FloatExtVar(vname, nanoaod::FlatTable::FloatColumn, varPSet, this->consumesCollector()));
123  else if (type == "double") extvars_.push_back(new DoubleExtVar(vname, nanoaod::FlatTable::FloatColumn, varPSet, this->consumesCollector()));
124  else if (type == "uint8") extvars_.push_back(new UInt8ExtVar(vname, nanoaod::FlatTable::UInt8Column, varPSet, this->consumesCollector()));
125  else if (type == "bool") extvars_.push_back(new BoolExtVar(vname, nanoaod::FlatTable::BoolColumn, varPSet, this->consumesCollector()));
126  else throw cms::Exception("Configuration", "unsupported type "+type+" for variable "+vname);
127  }
128  }
129  }
130 
132 
133  std::unique_ptr<nanoaod::FlatTable> fillTable(const edm::Event &iEvent, const edm::Handle<edm::View<T>> & prod) const override {
134  std::vector<const T *> selobjs;
135  std::vector<edm::Ptr<T>> selptrs; // for external variables
136  if (singleton_) {
137  assert(prod->size() == 1);
138  selobjs.push_back(& (*prod)[0] );
139  if (!extvars_.empty()) selptrs.emplace_back(prod->ptrAt(0));
140  } else {
141  for (unsigned int i = 0, n = prod->size(); i < n; ++i) {
142  const auto & obj = (*prod)[i];
143  if (cut_(obj)) {
144  selobjs.push_back(&obj);
145  if (!extvars_.empty()) selptrs.emplace_back(prod->ptrAt(i));
146  }
147  if(selobjs.size()>=maxLen_) break;
148  }
149  }
150  auto out = std::make_unique<nanoaod::FlatTable>(selobjs.size(), this->name_, singleton_, this->extension_);
151  for (const auto & var : this->vars_) var.fill(selobjs, *out);
152  for (const auto & var : this->extvars_) var.fill(iEvent, selptrs, *out);
153  return out;
154  }
155 
156  protected:
158  const unsigned int maxLen_;
160 
161  class ExtVariable : public base::VariableBase {
162  public:
164  base::VariableBase(aname, atype, cfg) {}
165  virtual void fill(const edm::Event & iEvent, std::vector<edm::Ptr<T>> selptrs, nanoaod::FlatTable & out) const = 0;
166  };
167  template<typename TIn, typename ValType=TIn>
168  class ValueMapVariable : public ExtVariable {
169  public:
171  ExtVariable(aname, atype, cfg), token_(cc.consumes<edm::ValueMap<TIn>>(cfg.getParameter<edm::InputTag>("src"))) {}
172  void fill(const edm::Event & iEvent, std::vector<edm::Ptr<T>> selptrs, nanoaod::FlatTable & out) const override {
174  iEvent.getByToken(token_, vmap);
175  std::vector<ValType> vals(selptrs.size());
176  for (unsigned int i = 0, n = vals.size(); i < n; ++i) {
177  vals[i] = (*vmap)[selptrs[i]];
178  }
179  out.template addColumn<ValType>(this->name_, vals, this->doc_, this->type_, this->precision_);
180  }
181  protected:
183  };
189  boost::ptr_vector<ExtVariable> extvars_;
190 
191 };
192 
193 template<typename T>
195  public:
197  SimpleFlatTableProducerBase<T,T>(params) {}
198 
200 
201  std::unique_ptr<nanoaod::FlatTable> fillTable(const edm::Event &, const edm::Handle<T> & prod) const override {
202  auto out = std::make_unique<nanoaod::FlatTable>(1, this->name_, true, this->extension_);
203  std::vector<const T *> selobjs(1, prod.product());
204  for (const auto & var : this->vars_) var.fill(selobjs, *out);
205  return out;
206  }
207 };
208 
209 template<typename T>
211  public:
213  SimpleFlatTableProducerBase<T, edm::View<T>>(params) {}
214 
216 
217  std::unique_ptr<nanoaod::FlatTable> fillTable(const edm::Event &iEvent, const edm::Handle<edm::View<T>> & prod) const override {
218  auto out = std::make_unique<nanoaod::FlatTable>(1, this->name_, true, this->extension_);
219  std::vector<const T *> selobjs(1, & (*prod)[0]);
220  for (const auto & var : this->vars_) var.fill(selobjs, *out);
221  return out;
222  }
223 };
224 
type
Definition: HCALResponse.h:21
T getParameter(std::string const &) const
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:125
virtual std::unique_ptr< nanoaod::FlatTable > fillTable(const edm::Event &iEvent, const edm::Handle< TProd > &prod) const =0
bool existsAs(std::string const &parameterName, bool trackiness=true) const
checks if a parameter exists as a given type
Definition: ParameterSet.h:161
ValueMapVariable< int > IntExtVar
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
Variable(const std::string &aname, nanoaod::FlatTable::ColumnType atype, const edm::ParameterSet &cfg)
VariableBase(const std::string &aname, nanoaod::FlatTable::ColumnType atype, const edm::ParameterSet &cfg)
const edm::EDGetTokenT< TProd > src_
std::unique_ptr< nanoaod::FlatTable > fillTable(const edm::Event &iEvent, const edm::Handle< edm::View< T >> &prod) const override
void fill(std::vector< const T * > selobjs, nanoaod::FlatTable &out) const override
std::unique_ptr< nanoaod::FlatTable > fillTable(const edm::Event &iEvent, const edm::Handle< edm::View< T >> &prod) const override
edm::EDGetTokenT< edm::ValueMap< TIn > > token_
void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override
ValueMapVariable(const std::string &aname, nanoaod::FlatTable::ColumnType atype, const edm::ParameterSet &cfg, edm::ConsumesCollector &&cc)
ExtVariable(const std::string &aname, nanoaod::FlatTable::ColumnType atype, const edm::ParameterSet &cfg)
SimpleFlatTableProducerBase(edm::ParameterSet const &params)
ValueMapVariable< float > FloatExtVar
std::vector< std::string > getParameterNamesForType(bool trackiness=true) const
Definition: ParameterSet.h:169
int iEvent
Definition: GenABIO.cc:224
SimpleFlatTableProducerBase< T, edm::View< T > > base
FuncVariable< StringCutObjectSelector< T >, uint8_t > BoolVar
FuncVariable< StringObjectFunction< T >, uint8_t > UInt8Var
SimpleFlatTableProducer(edm::ParameterSet const &params)
ValueMapVariable< bool, uint8_t > BoolExtVar
boost::ptr_vector< ExtVariable > extvars_
FuncVariable< StringObjectFunction< T >, int > IntVar
T const * product() const
Definition: Handle.h:74
boost::ptr_vector< Variable > vars_
ValueMapVariable< double, float > DoubleExtVar
void fill(const edm::Event &iEvent, std::vector< edm::Ptr< T >> selptrs, nanoaod::FlatTable &out) const override
HLT enums.
FirstObjectSimpleFlatTableProducer(edm::ParameterSet const &params)
EventSingletonSimpleFlatTableProducer(edm::ParameterSet const &params)
FuncVariable< StringObjectFunction< T >, float > FloatVar
FuncVariable(const std::string &aname, nanoaod::FlatTable::ColumnType atype, const edm::ParameterSet &cfg)
long double T
const StringCutObjectSelector< T > cut_
const nanoaod::FlatTable::ColumnType & type() const
def move(src, dest)
Definition: eostools.py:511
ValueMapVariable< int, uint8_t > UInt8ExtVar
std::unique_ptr< nanoaod::FlatTable > fillTable(const edm::Event &, const edm::Handle< T > &prod) const override