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") : (cfg.existsAs<std::string>("precision") ? -2 : -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), precisionFunc_(cfg.existsAs<std::string>("precision") ? cfg.getParameter<std::string>("precision") : "23",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  if(this->precision_ == -2){
91  vals[i] = MiniFloatConverter::reduceMantissaToNbitsRounding(func_(*selobjs[i]),precisionFunc_(*selobjs[i]));
92  }
93  else vals[i] = func_(*selobjs[i]);
94  }
95  out.template addColumn<ValType>(this->name_, vals, this->doc_, this->type_,this->precision_);
96  }
97  protected:
98  StringFunctor func_;
99  StringFunctor precisionFunc_;
100 
101  };
102  typedef FuncVariable<StringObjectFunction<T>,int> IntVar;
103  typedef FuncVariable<StringObjectFunction<T>,float> FloatVar;
104  typedef FuncVariable<StringObjectFunction<T>,uint8_t> UInt8Var;
105  typedef FuncVariable<StringCutObjectSelector<T>,uint8_t> BoolVar;
106  boost::ptr_vector<Variable> vars_;
107 };
108 
109 template<typename T>
110 class SimpleFlatTableProducer : public SimpleFlatTableProducerBase<T, edm::View<T>> {
111  public:
113 
115  SimpleFlatTableProducerBase<T, edm::View<T>>(params),
116  singleton_(params.getParameter<bool>("singleton")),
117  maxLen_(params.existsAs<unsigned int>("maxLen") ? params.getParameter<unsigned int>("maxLen") : std::numeric_limits<unsigned int>::max()),
118  cut_(!singleton_ ? params.getParameter<std::string>("cut") : "", true)
119  {
120  if (params.existsAs<edm::ParameterSet>("externalVariables")) {
121  edm::ParameterSet const & extvarsPSet = params.getParameter<edm::ParameterSet>("externalVariables");
122  for (const std::string & vname : extvarsPSet.getParameterNamesForType<edm::ParameterSet>()) {
123  const auto & varPSet = extvarsPSet.getParameter<edm::ParameterSet>(vname);
124  const std::string & type = varPSet.getParameter<std::string>("type");
125  if (type == "int") extvars_.push_back(new IntExtVar(vname, nanoaod::FlatTable::IntColumn, varPSet, this->consumesCollector()));
126  else if (type == "float") extvars_.push_back(new FloatExtVar(vname, nanoaod::FlatTable::FloatColumn, varPSet, this->consumesCollector()));
127  else if (type == "double") extvars_.push_back(new DoubleExtVar(vname, nanoaod::FlatTable::FloatColumn, varPSet, this->consumesCollector()));
128  else if (type == "uint8") extvars_.push_back(new UInt8ExtVar(vname, nanoaod::FlatTable::UInt8Column, varPSet, this->consumesCollector()));
129  else if (type == "bool") extvars_.push_back(new BoolExtVar(vname, nanoaod::FlatTable::BoolColumn, varPSet, this->consumesCollector()));
130  else throw cms::Exception("Configuration", "unsupported type "+type+" for variable "+vname);
131  }
132  }
133  }
134 
136 
137  std::unique_ptr<nanoaod::FlatTable> fillTable(const edm::Event &iEvent, const edm::Handle<edm::View<T>> & prod) const override {
138  std::vector<const T *> selobjs;
139  std::vector<edm::Ptr<T>> selptrs; // for external variables
140  if (singleton_) {
141  assert(prod->size() == 1);
142  selobjs.push_back(& (*prod)[0] );
143  if (!extvars_.empty()) selptrs.emplace_back(prod->ptrAt(0));
144  } else {
145  for (unsigned int i = 0, n = prod->size(); i < n; ++i) {
146  const auto & obj = (*prod)[i];
147  if (cut_(obj)) {
148  selobjs.push_back(&obj);
149  if (!extvars_.empty()) selptrs.emplace_back(prod->ptrAt(i));
150  }
151  if(selobjs.size()>=maxLen_) break;
152  }
153  }
154  auto out = std::make_unique<nanoaod::FlatTable>(selobjs.size(), this->name_, singleton_, this->extension_);
155  for (const auto & var : this->vars_) var.fill(selobjs, *out);
156  for (const auto & var : this->extvars_) var.fill(iEvent, selptrs, *out);
157  return out;
158  }
159 
160  protected:
162  const unsigned int maxLen_;
164 
165  class ExtVariable : public base::VariableBase {
166  public:
168  base::VariableBase(aname, atype, cfg) {}
169  virtual void fill(const edm::Event & iEvent, std::vector<edm::Ptr<T>> selptrs, nanoaod::FlatTable & out) const = 0;
170  };
171  template<typename TIn, typename ValType=TIn>
172  class ValueMapVariable : public ExtVariable {
173  public:
175  ExtVariable(aname, atype, cfg), token_(cc.consumes<edm::ValueMap<TIn>>(cfg.getParameter<edm::InputTag>("src"))) {}
176  void fill(const edm::Event & iEvent, std::vector<edm::Ptr<T>> selptrs, nanoaod::FlatTable & out) const override {
178  iEvent.getByToken(token_, vmap);
179  std::vector<ValType> vals(selptrs.size());
180  for (unsigned int i = 0, n = vals.size(); i < n; ++i) {
181  vals[i] = (*vmap)[selptrs[i]];
182  }
183  out.template addColumn<ValType>(this->name_, vals, this->doc_, this->type_, this->precision_);
184  }
185  protected:
187  };
193  boost::ptr_vector<ExtVariable> extvars_;
194 
195 };
196 
197 template<typename T>
199  public:
201  SimpleFlatTableProducerBase<T,T>(params) {}
202 
204 
205  std::unique_ptr<nanoaod::FlatTable> fillTable(const edm::Event &, const edm::Handle<T> & prod) const override {
206  auto out = std::make_unique<nanoaod::FlatTable>(1, this->name_, true, this->extension_);
207  std::vector<const T *> selobjs(1, prod.product());
208  for (const auto & var : this->vars_) var.fill(selobjs, *out);
209  return out;
210  }
211 };
212 
213 template<typename T>
215  public:
217  SimpleFlatTableProducerBase<T, edm::View<T>>(params) {}
218 
220 
221  std::unique_ptr<nanoaod::FlatTable> fillTable(const edm::Event &iEvent, const edm::Handle<edm::View<T>> & prod) const override {
222  auto out = std::make_unique<nanoaod::FlatTable>(1, this->name_, true, this->extension_);
223  std::vector<const T *> selobjs(1, & (*prod)[0]);
224  for (const auto & var : this->vars_) var.fill(selobjs, *out);
225  return out;
226  }
227 };
228 
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:137
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:186
ValueMapVariable< int > IntExtVar
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:579
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:194
int iEvent
Definition: GenABIO.cc:230
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:81
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
static float reduceMantissaToNbitsRounding(const float &f)
Definition: libminifloat.h:86
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:510
ValueMapVariable< int, uint8_t > UInt8ExtVar
std::unique_ptr< nanoaod::FlatTable > fillTable(const edm::Event &, const edm::Handle< T > &prod) const override