CMS 3D CMS Logo

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 
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),
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:
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 };
SimpleFlatTableProducerBase::BoolVar
FuncVariable< StringCutObjectSelector< T >, bool > BoolVar
Definition: SimpleFlatTableProducer.h:121
SimpleFlatTableProducer::ExtVariable::ExtVariable
ExtVariable(const std::string &aname, const edm::ParameterSet &cfg)
Definition: SimpleFlatTableProducer.h:202
electrons_cff.bool
bool
Definition: electrons_cff.py:366
mps_fire.i
i
Definition: mps_fire.py:428
SimpleFlatTableProducerBase::produce
void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override
Definition: SimpleFlatTableProducer.h:50
funct::false
false
Definition: Factorize.h:29
dqmiodumpmetadata.n
n
Definition: dqmiodumpmetadata.py:28
SimpleFlatTableProducer::ValueMapVariable::token_
edm::EDGetTokenT< edm::ValueMap< TIn > > token_
Definition: SimpleFlatTableProducer.h:231
sistrip::View
View
Definition: ConstantsForView.h:26
CalibrationSummaryClient_cfi.params
params
Definition: CalibrationSummaryClient_cfi.py:14
SimpleFlatTableProducer::ExtVariable
Definition: SimpleFlatTableProducer.h:200
FirstObjectSimpleFlatTableProducer
Definition: SimpleFlatTableProducer.h:258
SimpleFlatTableProducerBase::FuncVariable::func_
StringFunctor func_
Definition: SimpleFlatTableProducer.h:115
edm::EDGetTokenT< TProd >
edm
HLT enums.
Definition: AlignableModifier.h:19
SimpleFlatTableProducer::IntExtVar
ValueMapVariable< int > IntExtVar
Definition: SimpleFlatTableProducer.h:233
SimpleFlatTableProducer::ValueMapVariable::ValueMapVariable
ValueMapVariable(const std::string &aname, const edm::ParameterSet &cfg, edm::ConsumesCollector &&cc, bool skipNonExistingSrc=false)
Definition: SimpleFlatTableProducer.h:208
HLT_FULL_cff.InputTag
InputTag
Definition: HLT_FULL_cff.py:89285
SimpleFlatTableProducerBase::VariableBase::name
const std::string & name() const
Definition: SimpleFlatTableProducer.h:75
SimpleFlatTableProducerBase::Variable
Definition: SimpleFlatTableProducer.h:82
cms::cuda::assert
assert(be >=bs)
EDProducer.h
SimpleFlatTableProducer::UInt8ExtVar
ValueMapVariable< int, uint8_t > UInt8ExtVar
Definition: SimpleFlatTableProducer.h:237
SimpleFlatTableProducerBase::FuncVariable::precisionFunc_
StringFunctor precisionFunc_
Definition: SimpleFlatTableProducer.h:116
SimpleFlatTableProducer::base
SimpleFlatTableProducerBase< T, edm::View< T > > base
Definition: SimpleFlatTableProducer.h:128
edm::Handle
Definition: AssociativeIterator.h:50
SimpleFlatTableProducerBase::SimpleFlatTableProducerBase
SimpleFlatTableProducerBase(edm::ParameterSet const &params)
Definition: SimpleFlatTableProducer.h:17
SimpleFlatTableProducerBase::VariableBase::~VariableBase
virtual ~VariableBase()
Definition: SimpleFlatTableProducer.h:74
SimpleFlatTableProducer::singleton_
bool singleton_
Definition: SimpleFlatTableProducer.h:196
SimpleFlatTableProducer::FloatExtVar
ValueMapVariable< float > FloatExtVar
Definition: SimpleFlatTableProducer.h:234
trigObjTnPSource_cfi.var
var
Definition: trigObjTnPSource_cfi.py:21
SimpleFlatTableProducerBase::Variable::Variable
Variable(const std::string &aname, const edm::ParameterSet &cfg)
Definition: SimpleFlatTableProducer.h:84
EventSingletonSimpleFlatTableProducer
Definition: SimpleFlatTableProducer.h:242
SimpleFlatTableProducerBase::UInt8Var
FuncVariable< StringObjectFunction< T >, uint8_t > UInt8Var
Definition: SimpleFlatTableProducer.h:120
SimpleFlatTableProducerBase::FloatVar
FuncVariable< StringObjectFunction< T >, float > FloatVar
Definition: SimpleFlatTableProducer.h:119
dumpMFGeometry_cfg.prod
prod
Definition: dumpMFGeometry_cfg.py:24
SimpleFlatTableProducer::maxLen_
const unsigned int maxLen_
Definition: SimpleFlatTableProducer.h:197
SimpleFlatTableProducerBase::~SimpleFlatTableProducerBase
~SimpleFlatTableProducerBase() override
Definition: SimpleFlatTableProducer.h:44
SimpleFlatTableProducerBase::VariableBase
Definition: SimpleFlatTableProducer.h:67
EventSingletonSimpleFlatTableProducer::EventSingletonSimpleFlatTableProducer
EventSingletonSimpleFlatTableProducer(edm::ParameterSet const &params)
Definition: SimpleFlatTableProducer.h:244
FirstObjectSimpleFlatTableProducer::FirstObjectSimpleFlatTableProducer
FirstObjectSimpleFlatTableProducer(edm::ParameterSet const &params)
Definition: SimpleFlatTableProducer.h:260
SimpleFlatTableProducerBase::FuncVariable::fill
void fill(std::vector< const T * > selobjs, nanoaod::FlatTable &out) const override
Definition: SimpleFlatTableProducer.h:97
getGTfromDQMFile.obj
obj
Definition: getGTfromDQMFile.py:32
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
FirstObjectSimpleFlatTableProducer::fillTable
std::unique_ptr< nanoaod::FlatTable > fillTable(const edm::Event &iEvent, const edm::Handle< edm::View< T >> &prod) const override
Definition: SimpleFlatTableProducer.h:265
SimpleFlatTableProducerBase::skipNonExistingSrc_
const bool skipNonExistingSrc_
Definition: SimpleFlatTableProducer.h:64
edm::View
Definition: CaloClusterFwd.h:14
funct::true
true
Definition: Factorize.h:173
SimpleFlatTableProducerBase::Variable::fill
virtual void fill(std::vector< const T * > selobjs, nanoaod::FlatTable &out) const =0
SimpleFlatTableProducer::extvars_
std::vector< std::unique_ptr< ExtVariable > > extvars_
Definition: SimpleFlatTableProducer.h:238
MiniFloatConverter::reduceMantissaToNbitsRounding
static float reduceMantissaToNbitsRounding(const float &f)
Definition: libminifloat.h:102
edm::ParameterSet
Definition: ParameterSet.h:47
SimpleFlatTableProducerBase::name_
const std::string name_
Definition: SimpleFlatTableProducer.h:61
TrackRefitter_38T_cff.src
src
Definition: TrackRefitter_38T_cff.py:24
SiStripPI::max
Definition: SiStripPayloadInspectorHelper.h:169
FirstObjectSimpleFlatTableProducer::~FirstObjectSimpleFlatTableProducer
~FirstObjectSimpleFlatTableProducer() override
Definition: SimpleFlatTableProducer.h:263
type
type
Definition: SiPixelVCal_PayloadInspector.cc:37
SimpleFlatTableProducer::~SimpleFlatTableProducer
~SimpleFlatTableProducer() override
Definition: SimpleFlatTableProducer.h:162
protons_cff.skipNonExistingSrc
skipNonExistingSrc
Definition: protons_cff.py:33
createfilelist.int
int
Definition: createfilelist.py:10
SimpleFlatTableProducer::ExtVariable::fill
virtual void fill(const edm::Event &iEvent, std::vector< edm::Ptr< T >> selptrs, nanoaod::FlatTable &out) const =0
edm::ParameterSet::getParameterNamesForType
std::vector< std::string > getParameterNamesForType(bool trackiness=true) const
Definition: ParameterSet.h:179
iEvent
int iEvent
Definition: GenABIO.cc:224
SimpleFlatTableProducer::BoolExtVar
ValueMapVariable< bool > BoolExtVar
Definition: SimpleFlatTableProducer.h:236
trackerHitRTTI::vector
Definition: trackerHitRTTI.h:21
edm::stream::EDProducer
Definition: EDProducer.h:38
SimpleFlatTableProducerBase::FuncVariable
Definition: SimpleFlatTableProducer.h:89
edm::EventSetup
Definition: EventSetup.h:58
SimpleFlatTableProducerBase::VariableBase::name_
std::string name_
Definition: SimpleFlatTableProducer.h:78
SimpleFlatTableProducerBase::VariableBase::VariableBase
VariableBase(const std::string &aname, const edm::ParameterSet &cfg)
Definition: SimpleFlatTableProducer.h:69
SimpleFlatTableProducerBase::src_
const edm::EDGetTokenT< TProd > src_
Definition: SimpleFlatTableProducer.h:65
cc
FlatTable.h
edm::Ptr
Definition: AssociationVector.h:31
looper.cfg
cfg
Definition: looper.py:297
ValueMap.h
SimpleFlatTableProducer::SimpleFlatTableProducer
SimpleFlatTableProducer(edm::ParameterSet const &params)
Definition: SimpleFlatTableProducer.h:130
nanoaod::FlatTable
Definition: FlatTable.h:38
eostools.move
def move(src, dest)
Definition: eostools.py:511
std
Definition: JetResolutionObject.h:76
SimpleFlatTableProducerBase::VariableBase::precision_
int precision_
Definition: SimpleFlatTableProducer.h:79
StringCutObjectSelector.h
SimpleFlatTableProducerBase::VariableBase::doc_
std::string doc_
Definition: SimpleFlatTableProducer.h:78
T
long double T
Definition: Basic3DVectorLD.h:48
SimpleFlatTableProducer::ValueMapVariable
Definition: SimpleFlatTableProducer.h:206
StringCutObjectSelector< T >
Exception
Definition: hltDiff.cc:245
SimpleFlatTableProducerBase::fillTable
virtual std::unique_ptr< nanoaod::FlatTable > fillTable(const edm::Event &iEvent, const edm::Handle< TProd > &prod) const =0
SimpleFlatTableProducer
Definition: SimpleFlatTableProducer.h:126
edm::ParameterSet::getParameter
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
MillePedeFileConverter_cfg.out
out
Definition: MillePedeFileConverter_cfg.py:31
SimpleFlatTableProducerBase::FuncVariable::~FuncVariable
~FuncVariable() override
Definition: SimpleFlatTableProducer.h:96
ConsumesCollector.h
SimpleFlatTableProducer::cut_
const StringCutObjectSelector< T > cut_
Definition: SimpleFlatTableProducer.h:198
View.h
EventSingletonSimpleFlatTableProducer::~EventSingletonSimpleFlatTableProducer
~EventSingletonSimpleFlatTableProducer() override
Definition: SimpleFlatTableProducer.h:246
ParameterSet.h
SimpleFlatTableProducerBase
Definition: SimpleFlatTableProducer.h:15
SimpleFlatTableProducer::fillTable
std::unique_ptr< nanoaod::FlatTable > fillTable(const edm::Event &iEvent, const edm::Handle< edm::View< T >> &prod) const override
Definition: SimpleFlatTableProducer.h:164
edm::HandleBase::isValid
bool isValid() const
Definition: HandleBase.h:70
edm::Event
Definition: Event.h:73
SimpleFlatTableProducerBase::FuncVariable::FuncVariable
FuncVariable(const std::string &aname, const edm::ParameterSet &cfg)
Definition: SimpleFlatTableProducer.h:91
EventSingletonSimpleFlatTableProducer::fillTable
std::unique_ptr< nanoaod::FlatTable > fillTable(const edm::Event &, const edm::Handle< T > &prod) const override
Definition: SimpleFlatTableProducer.h:248
StringObjectFunction.h
SimpleFlatTableProducerBase::doc_
const std::string doc_
Definition: SimpleFlatTableProducer.h:62
edm::ConsumesCollector
Definition: ConsumesCollector.h:45
SimpleFlatTableProducerBase::IntVar
FuncVariable< StringObjectFunction< T >, int > IntVar
Definition: SimpleFlatTableProducer.h:118
SimpleFlatTableProducerBase::vars_
std::vector< std::unique_ptr< Variable > > vars_
Definition: SimpleFlatTableProducer.h:122
SimpleFlatTableProducer::DoubleExtVar
ValueMapVariable< double, float > DoubleExtVar
Definition: SimpleFlatTableProducer.h:235
SimpleFlatTableProducer::ValueMapVariable::fill
void fill(const edm::Event &iEvent, std::vector< edm::Ptr< T >> selptrs, nanoaod::FlatTable &out) const override
Definition: SimpleFlatTableProducer.h:216
SimpleFlatTableProducerBase::extension_
const bool extension_
Definition: SimpleFlatTableProducer.h:63
SimpleFlatTableProducer::ValueMapVariable::skipNonExistingSrc_
const bool skipNonExistingSrc_
Definition: SimpleFlatTableProducer.h:230