CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
SimpleFlatTableProducer.h
Go to the documentation of this file.
7 
10 
11 #include <memory>
12 #include <vector>
13 
14 template <typename T, typename TProd>
16 public:
18  : name_(params.getParameter<std::string>("name")),
19  doc_(params.existsAs<std::string>("doc") ? params.getParameter<std::string>("doc") : ""),
20  extension_(params.existsAs<bool>("extension") ? params.getParameter<bool>("extension") : false),
22  params.existsAs<bool>("skipNonExistingSrc") ? params.getParameter<bool>("skipNonExistingSrc") : false),
23  src_(skipNonExistingSrc_ ? mayConsume<TProd>(params.getParameter<edm::InputTag>("src"))
24  : consumes<TProd>(params.getParameter<edm::InputTag>("src"))) {
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")
30  vars_.push_back(std::make_unique<IntVar>(vname, varPSet));
31  else if (type == "float")
32  vars_.push_back(std::make_unique<FloatVar>(vname, varPSet));
33  else if (type == "uint8")
34  vars_.push_back(std::make_unique<UInt8Var>(vname, varPSet));
35  else if (type == "bool")
36  vars_.push_back(std::make_unique<BoolVar>(vname, varPSet));
37  else
38  throw cms::Exception("Configuration", "unsupported type " + type + " for variable " + vname);
39  }
40 
41  produces<nanoaod::FlatTable>();
42  }
43 
45 
46  // this is to be overriden by the child class
47  virtual std::unique_ptr<nanoaod::FlatTable> fillTable(const edm::Event &iEvent,
48  const edm::Handle<TProd> &prod) const = 0;
49 
50  void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override {
52  iEvent.getByToken(src_, src);
53 
54  std::unique_ptr<nanoaod::FlatTable> out = fillTable(iEvent, src);
55  out->setDoc(doc_);
56 
57  iEvent.put(std::move(out));
58  }
59 
60 protected:
63  const bool extension_;
64  const bool skipNonExistingSrc_;
66 
67  class VariableBase {
68  public:
70  : name_(aname),
71  doc_(cfg.getParameter<std::string>("doc")),
72  precision_(cfg.existsAs<int>("precision") ? cfg.getParameter<int>("precision")
73  : (cfg.existsAs<std::string>("precision") ? -2 : -1)) {}
74  virtual ~VariableBase() {}
75  const std::string &name() const { return name_; }
76 
77  protected:
80  };
81 
82  class Variable : public VariableBase {
83  public:
84  Variable(const std::string &aname, const edm::ParameterSet &cfg) : VariableBase(aname, cfg) {}
85  virtual void fill(std::vector<const T *> selobjs, nanoaod::FlatTable &out) const = 0;
86  };
87 
88  template <typename StringFunctor, typename ValType>
89  class FuncVariable : public Variable {
90  public:
92  : Variable(aname, cfg),
93  func_(cfg.getParameter<std::string>("expr"), true),
94  precisionFunc_(cfg.existsAs<std::string>("precision") ? cfg.getParameter<std::string>("precision") : "23",
95  true) {}
96  ~FuncVariable() override {}
97  void fill(std::vector<const T *> selobjs, nanoaod::FlatTable &out) const override {
98  std::vector<ValType> vals(selobjs.size());
99  for (unsigned int i = 0, n = vals.size(); i < n; ++i) {
100  if constexpr (std::is_same<ValType, float>()) {
101  if (this->precision_ == -2) {
102  vals[i] =
104  } else {
105  vals[i] = func_(*selobjs[i]);
106  }
107  } else {
108  vals[i] = func_(*selobjs[i]);
109  }
110  }
111  out.template addColumn<ValType>(this->name_, vals, this->doc_, this->precision_);
112  }
113 
114  protected:
115  StringFunctor func_;
116  StringFunctor precisionFunc_;
117  };
118  typedef FuncVariable<StringObjectFunction<T>, int> IntVar;
119  typedef FuncVariable<StringObjectFunction<T>, float> FloatVar;
120  typedef FuncVariable<StringObjectFunction<T>, uint8_t> UInt8Var;
121  typedef FuncVariable<StringCutObjectSelector<T>, bool> BoolVar;
122  std::vector<std::unique_ptr<Variable>> vars_;
123 };
124 
125 template <typename T>
126 class SimpleFlatTableProducer : public SimpleFlatTableProducerBase<T, edm::View<T>> {
127 public:
129 
131  : SimpleFlatTableProducerBase<T, edm::View<T>>(params),
132  singleton_(params.getParameter<bool>("singleton")),
133  maxLen_(params.existsAs<unsigned int>("maxLen") ? params.getParameter<unsigned int>("maxLen")
134  : std::numeric_limits<unsigned int>::max()),
135  cut_(!singleton_ ? params.getParameter<std::string>("cut") : "", true) {
136  if (params.existsAs<edm::ParameterSet>("externalVariables")) {
137  edm::ParameterSet const &extvarsPSet = params.getParameter<edm::ParameterSet>("externalVariables");
138  for (const std::string &vname : extvarsPSet.getParameterNamesForType<edm::ParameterSet>()) {
139  const auto &varPSet = extvarsPSet.getParameter<edm::ParameterSet>(vname);
140  const std::string &type = varPSet.getParameter<std::string>("type");
141  if (type == "int")
142  extvars_.push_back(
143  std::make_unique<IntExtVar>(vname, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
144  else if (type == "float")
145  extvars_.push_back(
146  std::make_unique<FloatExtVar>(vname, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
147  else if (type == "double")
148  extvars_.push_back(
149  std::make_unique<DoubleExtVar>(vname, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
150  else if (type == "uint8")
151  extvars_.push_back(
152  std::make_unique<UInt8ExtVar>(vname, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
153  else if (type == "bool")
154  extvars_.push_back(
155  std::make_unique<BoolExtVar>(vname, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
156  else
157  throw cms::Exception("Configuration", "unsupported type " + type + " for variable " + vname);
158  }
159  }
160  }
161 
163 
164  std::unique_ptr<nanoaod::FlatTable> fillTable(const edm::Event &iEvent,
165  const edm::Handle<edm::View<T>> &prod) const override {
166  std::vector<const T *> selobjs;
167  std::vector<edm::Ptr<T>> selptrs; // for external variables
168  if (prod.isValid() || !(this->skipNonExistingSrc_)) {
169  if (singleton_) {
170  assert(prod->size() == 1);
171  selobjs.push_back(&(*prod)[0]);
172  if (!extvars_.empty())
173  selptrs.emplace_back(prod->ptrAt(0));
174  } else {
175  for (unsigned int i = 0, n = prod->size(); i < n; ++i) {
176  const auto &obj = (*prod)[i];
177  if (cut_(obj)) {
178  selobjs.push_back(&obj);
179  if (!extvars_.empty())
180  selptrs.emplace_back(prod->ptrAt(i));
181  }
182  if (selobjs.size() >= maxLen_)
183  break;
184  }
185  }
186  }
187  auto out = std::make_unique<nanoaod::FlatTable>(selobjs.size(), this->name_, singleton_, this->extension_);
188  for (const auto &var : this->vars_)
189  var->fill(selobjs, *out);
190  for (const auto &var : this->extvars_)
191  var->fill(iEvent, selptrs, *out);
192  return out;
193  }
194 
195 protected:
197  const unsigned int maxLen_;
199 
200  class ExtVariable : public base::VariableBase {
201  public:
202  ExtVariable(const std::string &aname, const edm::ParameterSet &cfg) : base::VariableBase(aname, cfg) {}
203  virtual void fill(const edm::Event &iEvent, std::vector<edm::Ptr<T>> selptrs, nanoaod::FlatTable &out) const = 0;
204  };
205  template <typename TIn, typename ValType = TIn>
206  class ValueMapVariable : public ExtVariable {
207  public:
209  const edm::ParameterSet &cfg,
211  bool skipNonExistingSrc = false)
212  : ExtVariable(aname, cfg),
213  skipNonExistingSrc_(skipNonExistingSrc),
214  token_(skipNonExistingSrc_ ? cc.mayConsume<edm::ValueMap<TIn>>(cfg.getParameter<edm::InputTag>("src"))
215  : cc.consumes<edm::ValueMap<TIn>>(cfg.getParameter<edm::InputTag>("src"))) {}
216  void fill(const edm::Event &iEvent, std::vector<edm::Ptr<T>> selptrs, nanoaod::FlatTable &out) const override {
218  iEvent.getByToken(token_, vmap);
219  std::vector<ValType> vals;
220  if (vmap.isValid() || !skipNonExistingSrc_) {
221  vals.resize(selptrs.size());
222  for (unsigned int i = 0, n = vals.size(); i < n; ++i) {
223  vals[i] = (*vmap)[selptrs[i]];
224  }
225  }
226  out.template addColumn<ValType>(this->name_, vals, this->doc_, this->precision_);
227  }
228 
229  protected:
232  };
238  std::vector<std::unique_ptr<ExtVariable>> extvars_;
239 };
240 
241 template <typename T>
243 public:
245 
247 
248  std::unique_ptr<nanoaod::FlatTable> fillTable(const edm::Event &, const edm::Handle<T> &prod) const override {
249  auto out = std::make_unique<nanoaod::FlatTable>(1, this->name_, true, this->extension_);
250  std::vector<const T *> selobjs(1, prod.product());
251  for (const auto &var : this->vars_)
252  var->fill(selobjs, *out);
253  return out;
254  }
255 };
256 
257 template <typename T>
259 public:
261  : SimpleFlatTableProducerBase<T, edm::View<T>>(params) {}
262 
264 
265  std::unique_ptr<nanoaod::FlatTable> fillTable(const edm::Event &iEvent,
266  const edm::Handle<edm::View<T>> &prod) const override {
267  auto out = std::make_unique<nanoaod::FlatTable>(1, this->name_, true, this->extension_);
268  std::vector<const T *> selobjs(1, &(*prod)[0]);
269  for (const auto &var : this->vars_)
270  var->fill(selobjs, *out);
271  return out;
272  }
273 };
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:133
tuple cfg
Definition: looper.py:296
bool existsAs(std::string const &parameterName, bool trackiness=true) const
checks if a parameter exists as a given type
Definition: ParameterSet.h:171
ValueMapVariable< int > IntExtVar
ValueMapVariable(const std::string &aname, const edm::ParameterSet &cfg, edm::ConsumesCollector &&cc, bool skipNonExistingSrc=false)
ValueMapVariable< double, float > DoubleExtVar
void fill(const edm::Event &iEvent, std::vector< edm::Ptr< T >> selptrs, nanoaod::FlatTable &out) const override
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:539
const edm::EDGetTokenT< TProd > src_
edm::EDGetTokenT< edm::ValueMap< TIn > > token_
void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override
std::unique_ptr< nanoaod::FlatTable > fillTable(const edm::Event &iEvent, const edm::Handle< edm::View< T >> &prod) const override
FuncVariable< StringObjectFunction< T >, uint8_t > UInt8Var
assert(be >=bs)
SimpleFlatTableProducerBase(edm::ParameterSet const &params)
ValueMapVariable< float > FloatExtVar
std::vector< std::string > getParameterNamesForType(bool trackiness=true) const
Definition: ParameterSet.h:179
std::vector< std::unique_ptr< ExtVariable > > extvars_
std::unique_ptr< nanoaod::FlatTable > fillTable(const edm::Event &, const edm::Handle< T > &prod) const override
ValueMapVariable< int, uint8_t > UInt8ExtVar
virtual void fill(std::vector< const T * > selobjs, nanoaod::FlatTable &out) const =0
int iEvent
Definition: GenABIO.cc:224
SimpleFlatTableProducerBase< T, edm::View< T > > base
FuncVariable< StringObjectFunction< T >, int > IntVar
list var
if using global norm cols_to_minmax = [&#39;t_delta&#39;, &#39;t_hmaxNearP&#39;,&#39;t_emaxNearP&#39;, &#39;t_hAnnular&#39;, &#39;t_eAnnular&#39;,&#39;t_pt&#39;,&#39;t_nVtx&#39;,&#39;t_ieta&#39;,&#39;t_eHcal10&#39;, &#39;t_eHcal30&#39;,&#39;t_rhoh&#39;,&#39;t_eHcal&#39;] df[cols_to_minmax] = df[cols_to_minmax].apply(lambda x: (x - x.min()) / (x.max() - x.min()) if (x.max() - x.min() &gt; 0) else 1.0/200.0)
def move
Definition: eostools.py:511
SimpleFlatTableProducer(edm::ParameterSet const &params)
bool isValid() const
Definition: HandleBase.h:70
virtual void fill(const edm::Event &iEvent, std::vector< edm::Ptr< T >> selptrs, nanoaod::FlatTable &out) const =0
ValueMapVariable< bool > BoolExtVar
FuncVariable< StringCutObjectSelector< T >, bool > BoolVar
T const * product() const
Definition: Handle.h:70
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
virtual std::unique_ptr< nanoaod::FlatTable > fillTable(const edm::Event &iEvent, const edm::Handle< TProd > &prod) const =0
std::unique_ptr< nanoaod::FlatTable > fillTable(const edm::Event &iEvent, const edm::Handle< edm::View< T >> &prod) const override
static float reduceMantissaToNbitsRounding(const float &f)
Definition: libminifloat.h:79
ExtVariable(const std::string &aname, const edm::ParameterSet &cfg)
FuncVariable(const std::string &aname, const edm::ParameterSet &cfg)
void fill(std::vector< const T * > selobjs, nanoaod::FlatTable &out) const override
FirstObjectSimpleFlatTableProducer(edm::ParameterSet const &params)
EventSingletonSimpleFlatTableProducer(edm::ParameterSet const &params)
std::vector< std::unique_ptr< Variable > > vars_
Variable(const std::string &aname, const edm::ParameterSet &cfg)
FuncVariable< StringObjectFunction< T >, float > FloatVar
VariableBase(const std::string &aname, const edm::ParameterSet &cfg)
long double T
const StringCutObjectSelector< T > cut_