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  skipNonExistingSrc_(params.existsAs<bool>("skipNonExistingSrc") ? params.getParameter<bool>("skipNonExistingSrc") : false),
23  src_(skipNonExistingSrc_ ? mayConsume<TProd>(params.getParameter<edm::InputTag>("src")) : consumes<TProd>(params.getParameter<edm::InputTag>("src")))
24  {
25  edm::ParameterSet const & varsPSet = params.getParameter<edm::ParameterSet>("variables");
26  for (const std::string & vname : varsPSet.getParameterNamesForType<edm::ParameterSet>()) {
27  const auto & varPSet = varsPSet.getParameter<edm::ParameterSet>(vname);
28  const std::string & type = varPSet.getParameter<std::string>("type");
29  if (type == "int") vars_.push_back(new IntVar(vname, nanoaod::FlatTable::IntColumn, varPSet));
30  else if (type == "float") vars_.push_back(new FloatVar(vname, nanoaod::FlatTable::FloatColumn, varPSet));
31  else if (type == "uint8") vars_.push_back(new UInt8Var(vname, nanoaod::FlatTable::UInt8Column, varPSet));
32  else if (type == "bool") vars_.push_back(new BoolVar(vname, nanoaod::FlatTable::BoolColumn, varPSet));
33  else throw cms::Exception("Configuration", "unsupported type "+type+" for variable "+vname);
34  }
35 
36  produces<nanoaod::FlatTable>();
37  }
38 
40 
41  // this is to be overriden by the child class
42  virtual std::unique_ptr<nanoaod::FlatTable> fillTable(const edm::Event &iEvent, const edm::Handle<TProd> & prod) const = 0;
43 
44 
45  void produce(edm::Event& iEvent, const edm::EventSetup& iSetup) override {
47  iEvent.getByToken(src_, src);
48 
49  std::unique_ptr<nanoaod::FlatTable> out = fillTable(iEvent, src);
50  out->setDoc(doc_);
51 
52  iEvent.put(std::move(out));
53  }
54 
55  protected:
58  const bool extension_;
59  const bool skipNonExistingSrc_;
61 
62  class VariableBase {
63  public:
65  name_(aname), doc_(cfg.getParameter<std::string>("doc")), type_(atype),
66  precision_(cfg.existsAs<int>("precision") ? cfg.getParameter<int>("precision") : (cfg.existsAs<std::string>("precision") ? -2 : -1))
67  {
68  }
69  virtual ~VariableBase() {}
70  const std::string & name() const { return name_; }
71  const nanoaod::FlatTable::ColumnType & type() const { return type_; }
72  protected:
76  };
77  class Variable : public VariableBase {
78  public:
80  VariableBase(aname, atype, cfg) {}
81  virtual void fill(std::vector<const T *> selobjs, nanoaod::FlatTable & out) const = 0;
82  };
83  template<typename StringFunctor, typename ValType>
84  class FuncVariable : public Variable {
85  public:
87  Variable(aname, atype, cfg), func_(cfg.getParameter<std::string>("expr"), true), precisionFunc_(cfg.existsAs<std::string>("precision") ? cfg.getParameter<std::string>("precision") : "23",true){}
88  ~FuncVariable() override {}
89  void fill(std::vector<const T *> selobjs, nanoaod::FlatTable & out) const override {
90  std::vector<ValType> vals(selobjs.size());
91  for (unsigned int i = 0, n = vals.size(); i < n; ++i) {
92  if(this->precision_ == -2){
93  vals[i] = MiniFloatConverter::reduceMantissaToNbitsRounding(func_(*selobjs[i]),precisionFunc_(*selobjs[i]));
94  }
95  else vals[i] = func_(*selobjs[i]);
96  }
97  out.template addColumn<ValType>(this->name_, vals, this->doc_, this->type_,this->precision_);
98  }
99  protected:
100  StringFunctor func_;
101  StringFunctor precisionFunc_;
102 
103  };
104  typedef FuncVariable<StringObjectFunction<T>,int> IntVar;
105  typedef FuncVariable<StringObjectFunction<T>,float> FloatVar;
106  typedef FuncVariable<StringObjectFunction<T>,uint8_t> UInt8Var;
107  typedef FuncVariable<StringCutObjectSelector<T>,uint8_t> BoolVar;
108  boost::ptr_vector<Variable> vars_;
109 };
110 
111 template<typename T>
112 class SimpleFlatTableProducer : public SimpleFlatTableProducerBase<T, edm::View<T>> {
113  public:
115 
117  SimpleFlatTableProducerBase<T, edm::View<T>>(params),
118  singleton_(params.getParameter<bool>("singleton")),
119  maxLen_(params.existsAs<unsigned int>("maxLen") ? params.getParameter<unsigned int>("maxLen") : std::numeric_limits<unsigned int>::max()),
120  cut_(!singleton_ ? params.getParameter<std::string>("cut") : "", true)
121  {
122  if (params.existsAs<edm::ParameterSet>("externalVariables")) {
123  edm::ParameterSet const & extvarsPSet = params.getParameter<edm::ParameterSet>("externalVariables");
124  for (const std::string & vname : extvarsPSet.getParameterNamesForType<edm::ParameterSet>()) {
125  const auto & varPSet = extvarsPSet.getParameter<edm::ParameterSet>(vname);
126  const std::string & type = varPSet.getParameter<std::string>("type");
127  if (type == "int") extvars_.push_back(new IntExtVar(vname, nanoaod::FlatTable::IntColumn, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
128  else if (type == "float") extvars_.push_back(new FloatExtVar(vname, nanoaod::FlatTable::FloatColumn, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
129  else if (type == "double") extvars_.push_back(new DoubleExtVar(vname, nanoaod::FlatTable::FloatColumn, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
130  else if (type == "uint8") extvars_.push_back(new UInt8ExtVar(vname, nanoaod::FlatTable::UInt8Column, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
131  else if (type == "bool") extvars_.push_back(new BoolExtVar(vname, nanoaod::FlatTable::BoolColumn, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
132  else throw cms::Exception("Configuration", "unsupported type "+type+" for variable "+vname);
133  }
134  }
135  }
136 
138 
139  std::unique_ptr<nanoaod::FlatTable> fillTable(const edm::Event &iEvent, const edm::Handle<edm::View<T>> & prod) const override {
140  std::vector<const T *> selobjs;
141  std::vector<edm::Ptr<T>> selptrs; // for external variables
142  if (prod.isValid() || !(this->skipNonExistingSrc_)) {
143  if (singleton_) {
144  assert(prod->size() == 1);
145  selobjs.push_back(&(*prod)[0]);
146  if (!extvars_.empty()) selptrs.emplace_back(prod->ptrAt(0));
147  } else {
148  for (unsigned int i = 0, n = prod->size(); i < n; ++i) {
149  const auto &obj = (*prod)[i];
150  if (cut_(obj)) {
151  selobjs.push_back(&obj);
152  if (!extvars_.empty()) selptrs.emplace_back(prod->ptrAt(i));
153  }
154  if (selobjs.size() >= maxLen_) break;
155  }
156  }
157  }
158  auto out = std::make_unique<nanoaod::FlatTable>(selobjs.size(), this->name_, singleton_, this->extension_);
159  for (const auto & var : this->vars_) var.fill(selobjs, *out);
160  for (const auto & var : this->extvars_) var.fill(iEvent, selptrs, *out);
161  return out;
162  }
163 
164  protected:
166  const unsigned int maxLen_;
168 
169  class ExtVariable : public base::VariableBase {
170  public:
172  base::VariableBase(aname, atype, cfg) {}
173  virtual void fill(const edm::Event & iEvent, std::vector<edm::Ptr<T>> selptrs, nanoaod::FlatTable & out) const = 0;
174  };
175  template<typename TIn, typename ValType=TIn>
176  class ValueMapVariable : public ExtVariable {
177  public:
179  ExtVariable(aname, atype, cfg), skipNonExistingSrc_(skipNonExistingSrc), token_(skipNonExistingSrc_ ? cc.mayConsume<edm::ValueMap<TIn>>(cfg.getParameter<edm::InputTag>("src")) : cc.consumes<edm::ValueMap<TIn>>(cfg.getParameter<edm::InputTag>("src"))) {}
180  void fill(const edm::Event & iEvent, std::vector<edm::Ptr<T>> selptrs, nanoaod::FlatTable & out) const override {
182  iEvent.getByToken(token_, vmap);
183  std::vector<ValType> vals;
184  if (vmap.isValid() || !skipNonExistingSrc_) {
185  vals.resize(selptrs.size());
186  for (unsigned int i = 0, n = vals.size(); i < n; ++i) {
187  vals[i] = (*vmap)[selptrs[i]];
188  }
189  }
190  out.template addColumn<ValType>(this->name_, vals, this->doc_, this->type_, this->precision_);
191  }
192  protected:
195  };
201  boost::ptr_vector<ExtVariable> extvars_;
202 
203 };
204 
205 template<typename T>
207  public:
209  SimpleFlatTableProducerBase<T,T>(params) {}
210 
212 
213  std::unique_ptr<nanoaod::FlatTable> fillTable(const edm::Event &, const edm::Handle<T> & prod) const override {
214  auto out = std::make_unique<nanoaod::FlatTable>(1, this->name_, true, this->extension_);
215  std::vector<const T *> selobjs(1, prod.product());
216  for (const auto & var : this->vars_) var.fill(selobjs, *out);
217  return out;
218  }
219 };
220 
221 template<typename T>
223  public:
225  SimpleFlatTableProducerBase<T, edm::View<T>>(params) {}
226 
228 
229  std::unique_ptr<nanoaod::FlatTable> fillTable(const edm::Event &iEvent, const edm::Handle<edm::View<T>> & prod) const override {
230  auto out = std::make_unique<nanoaod::FlatTable>(1, this->name_, true, this->extension_);
231  std::vector<const T *> selobjs(1, & (*prod)[0]);
232  for (const auto & var : this->vars_) var.fill(selobjs, *out);
233  return out;
234  }
235 };
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
ExtVariable(const std::string &aname, nanoaod::FlatTable::ColumnType atype, const edm::ParameterSet &cfg)
ValueMapVariable(const std::string &aname, nanoaod::FlatTable::ColumnType atype, const edm::ParameterSet &cfg, edm::ConsumesCollector &&cc, bool skipNonExistingSrc=false)
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)
bool isValid() const
Definition: HandleBase.h:74
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
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:511
ValueMapVariable< int, uint8_t > UInt8ExtVar
std::unique_ptr< nanoaod::FlatTable > fillTable(const edm::Event &, const edm::Handle< T > &prod) const override