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),
21  src_(consumes<TProd>(params.getParameter<edm::InputTag>("src"))) {
22  edm::ParameterSet const &varsPSet = params.getParameter<edm::ParameterSet>("variables");
23  for (const std::string &vname : varsPSet.getParameterNamesForType<edm::ParameterSet>()) {
24  const auto &varPSet = varsPSet.getParameter<edm::ParameterSet>(vname);
25  const std::string &type = varPSet.getParameter<std::string>("type");
26  if (type == "int")
27  vars_.push_back(std::make_unique<IntVar>(vname, varPSet));
28  else if (type == "float")
29  vars_.push_back(std::make_unique<FloatVar>(vname, varPSet));
30  else if (type == "uint8")
31  vars_.push_back(std::make_unique<UInt8Var>(vname, varPSet));
32  else if (type == "bool")
33  vars_.push_back(std::make_unique<BoolVar>(vname, varPSet));
34  else
35  throw cms::Exception("Configuration", "unsupported type " + type + " for variable " + vname);
36  }
37 
38  produces<nanoaod::FlatTable>();
39  }
40 
42 
43  // this is to be overriden by the child class
44  virtual std::unique_ptr<nanoaod::FlatTable> fillTable(const edm::Event &iEvent,
45  const edm::Handle<TProd> &prod) const = 0;
46 
47  void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override {
49  iEvent.getByToken(src_, src);
50 
51  std::unique_ptr<nanoaod::FlatTable> out = fillTable(iEvent, src);
52  out->setDoc(doc_);
53 
54  iEvent.put(std::move(out));
55  }
56 
57 protected:
60  const bool extension_;
62 
63  class VariableBase {
64  public:
66  : name_(aname),
67  doc_(cfg.getParameter<std::string>("doc")),
68  precision_(cfg.existsAs<int>("precision") ? cfg.getParameter<int>("precision")
69  : (cfg.existsAs<std::string>("precision") ? -2 : -1)) {}
70  virtual ~VariableBase() {}
71  const std::string &name() const { return name_; }
72 
73  protected:
76  };
77 
78  class Variable : public VariableBase {
79  public:
80  Variable(const std::string &aname, const edm::ParameterSet &cfg) : VariableBase(aname, cfg) {}
81  virtual void fill(std::vector<const T *> selobjs, nanoaod::FlatTable &out) const = 0;
82  };
83 
84  template <typename StringFunctor, typename ValType>
85  class FuncVariable : public Variable {
86  public:
88  : Variable(aname, cfg),
89  func_(cfg.getParameter<std::string>("expr"), true),
90  precisionFunc_(cfg.existsAs<std::string>("precision") ? cfg.getParameter<std::string>("precision") : "23",
91  true) {}
92  ~FuncVariable() override {}
93  void fill(std::vector<const T *> selobjs, nanoaod::FlatTable &out) const override {
94  std::vector<ValType> vals(selobjs.size());
95  for (unsigned int i = 0, n = vals.size(); i < n; ++i) {
96  if constexpr (std::is_same<ValType, float>()) {
97  if (this->precision_ == -2) {
98  vals[i] =
100  } else {
101  vals[i] = func_(*selobjs[i]);
102  }
103  } else {
104  vals[i] = func_(*selobjs[i]);
105  }
106  }
107  out.template addColumn<ValType>(this->name_, vals, this->doc_, this->precision_);
108  }
109 
110  protected:
111  StringFunctor func_;
112  StringFunctor precisionFunc_;
113  };
114  typedef FuncVariable<StringObjectFunction<T>, int> IntVar;
115  typedef FuncVariable<StringObjectFunction<T>, float> FloatVar;
116  typedef FuncVariable<StringObjectFunction<T>, uint8_t> UInt8Var;
117  typedef FuncVariable<StringCutObjectSelector<T>, bool> BoolVar;
118  std::vector<std::unique_ptr<Variable>> vars_;
119 };
120 
121 template <typename T>
122 class SimpleFlatTableProducer : public SimpleFlatTableProducerBase<T, edm::View<T>> {
123 public:
125 
128  singleton_(params.getParameter<bool>("singleton")),
129  maxLen_(params.existsAs<unsigned int>("maxLen") ? params.getParameter<unsigned int>("maxLen")
130  : std::numeric_limits<unsigned int>::max()),
131  cut_(!singleton_ ? params.getParameter<std::string>("cut") : "", true) {
132  if (params.existsAs<edm::ParameterSet>("externalVariables")) {
133  edm::ParameterSet const &extvarsPSet = params.getParameter<edm::ParameterSet>("externalVariables");
134  for (const std::string &vname : extvarsPSet.getParameterNamesForType<edm::ParameterSet>()) {
135  const auto &varPSet = extvarsPSet.getParameter<edm::ParameterSet>(vname);
136  const std::string &type = varPSet.getParameter<std::string>("type");
137  if (type == "int")
138  extvars_.push_back(std::make_unique<IntExtVar>(vname, varPSet, this->consumesCollector()));
139  else if (type == "float")
140  extvars_.push_back(std::make_unique<FloatExtVar>(vname, varPSet, this->consumesCollector()));
141  else if (type == "double")
142  extvars_.push_back(std::make_unique<DoubleExtVar>(vname, varPSet, this->consumesCollector()));
143  else if (type == "uint8")
144  extvars_.push_back(std::make_unique<UInt8ExtVar>(vname, varPSet, this->consumesCollector()));
145  else if (type == "bool")
146  extvars_.push_back(std::make_unique<BoolExtVar>(vname, varPSet, this->consumesCollector()));
147  else
148  throw cms::Exception("Configuration", "unsupported type " + type + " for variable " + vname);
149  }
150  }
151  }
152 
154 
155  std::unique_ptr<nanoaod::FlatTable> fillTable(const edm::Event &iEvent,
156  const edm::Handle<edm::View<T>> &prod) const override {
157  std::vector<const T *> selobjs;
158  std::vector<edm::Ptr<T>> selptrs; // for external variables
159  if (singleton_) {
160  assert(prod->size() == 1);
161  selobjs.push_back(&(*prod)[0]);
162  if (!extvars_.empty())
163  selptrs.emplace_back(prod->ptrAt(0));
164  } else {
165  for (unsigned int i = 0, n = prod->size(); i < n; ++i) {
166  const auto &obj = (*prod)[i];
167  if (cut_(obj)) {
168  selobjs.push_back(&obj);
169  if (!extvars_.empty())
170  selptrs.emplace_back(prod->ptrAt(i));
171  }
172  if (selobjs.size() >= maxLen_)
173  break;
174  }
175  }
176  auto out = std::make_unique<nanoaod::FlatTable>(selobjs.size(), this->name_, singleton_, this->extension_);
177  for (const auto &var : this->vars_)
178  var->fill(selobjs, *out);
179  for (const auto &var : this->extvars_)
180  var->fill(iEvent, selptrs, *out);
181  return out;
182  }
183 
184 protected:
186  const unsigned int maxLen_;
188 
189  class ExtVariable : public base::VariableBase {
190  public:
191  ExtVariable(const std::string &aname, const edm::ParameterSet &cfg) : base::VariableBase(aname, cfg) {}
192  virtual void fill(const edm::Event &iEvent, std::vector<edm::Ptr<T>> selptrs, nanoaod::FlatTable &out) const = 0;
193  };
194  template <typename TIn, typename ValType = TIn>
195  class ValueMapVariable : public ExtVariable {
196  public:
198  : ExtVariable(aname, cfg), token_(cc.consumes<edm::ValueMap<TIn>>(cfg.getParameter<edm::InputTag>("src"))) {}
199  void fill(const edm::Event &iEvent, std::vector<edm::Ptr<T>> selptrs, nanoaod::FlatTable &out) const override {
201  iEvent.getByToken(token_, vmap);
202  std::vector<ValType> vals(selptrs.size());
203  for (unsigned int i = 0, n = vals.size(); i < n; ++i) {
204  vals[i] = (*vmap)[selptrs[i]];
205  }
206  out.template addColumn<ValType>(this->name_, vals, this->doc_, this->precision_);
207  }
208 
209  protected:
211  };
217  std::vector<std::unique_ptr<ExtVariable>> extvars_;
218 };
219 
220 template <typename T>
222 public:
224 
226 
227  std::unique_ptr<nanoaod::FlatTable> fillTable(const edm::Event &, const edm::Handle<T> &prod) const override {
228  auto out = std::make_unique<nanoaod::FlatTable>(1, this->name_, true, this->extension_);
229  std::vector<const T *> selobjs(1, prod.product());
230  for (const auto &var : this->vars_)
231  var->fill(selobjs, *out);
232  return out;
233  }
234 };
235 
236 template <typename T>
238 public:
241 
243 
244  std::unique_ptr<nanoaod::FlatTable> fillTable(const edm::Event &iEvent,
245  const edm::Handle<edm::View<T>> &prod) const override {
246  auto out = std::make_unique<nanoaod::FlatTable>(1, this->name_, true, this->extension_);
247  std::vector<const T *> selobjs(1, &(*prod)[0]);
248  for (const auto &var : this->vars_)
249  var->fill(selobjs, *out);
250  return out;
251  }
252 };
SimpleFlatTableProducerBase::BoolVar
FuncVariable< StringCutObjectSelector< T >, bool > BoolVar
Definition: SimpleFlatTableProducer.h:117
SimpleFlatTableProducer::ExtVariable::ExtVariable
ExtVariable(const std::string &aname, const edm::ParameterSet &cfg)
Definition: SimpleFlatTableProducer.h:191
electrons_cff.bool
bool
Definition: electrons_cff.py:393
mps_fire.i
i
Definition: mps_fire.py:428
SimpleFlatTableProducerBase::produce
void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override
Definition: SimpleFlatTableProducer.h:47
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:210
sistrip::View
View
Definition: ConstantsForView.h:26
CalibrationSummaryClient_cfi.params
params
Definition: CalibrationSummaryClient_cfi.py:14
SimpleFlatTableProducer::ExtVariable
Definition: SimpleFlatTableProducer.h:189
FirstObjectSimpleFlatTableProducer
Definition: SimpleFlatTableProducer.h:237
SimpleFlatTableProducerBase::FuncVariable::func_
StringFunctor func_
Definition: SimpleFlatTableProducer.h:111
edm::EDGetTokenT< TProd >
edm
HLT enums.
Definition: AlignableModifier.h:19
SimpleFlatTableProducer::IntExtVar
ValueMapVariable< int > IntExtVar
Definition: SimpleFlatTableProducer.h:212
HLT_FULL_cff.InputTag
InputTag
Definition: HLT_FULL_cff.py:89353
SimpleFlatTableProducerBase::VariableBase::name
const std::string & name() const
Definition: SimpleFlatTableProducer.h:71
SimpleFlatTableProducerBase::Variable
Definition: SimpleFlatTableProducer.h:78
cms::cuda::assert
assert(be >=bs)
EDProducer.h
SimpleFlatTableProducer::UInt8ExtVar
ValueMapVariable< int, uint8_t > UInt8ExtVar
Definition: SimpleFlatTableProducer.h:216
SimpleFlatTableProducerBase::FuncVariable::precisionFunc_
StringFunctor precisionFunc_
Definition: SimpleFlatTableProducer.h:112
SimpleFlatTableProducer::base
SimpleFlatTableProducerBase< T, edm::View< T > > base
Definition: SimpleFlatTableProducer.h:124
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:70
SimpleFlatTableProducer::singleton_
bool singleton_
Definition: SimpleFlatTableProducer.h:185
SimpleFlatTableProducer::FloatExtVar
ValueMapVariable< float > FloatExtVar
Definition: SimpleFlatTableProducer.h:213
trigObjTnPSource_cfi.var
var
Definition: trigObjTnPSource_cfi.py:21
SimpleFlatTableProducerBase::Variable::Variable
Variable(const std::string &aname, const edm::ParameterSet &cfg)
Definition: SimpleFlatTableProducer.h:80
EventSingletonSimpleFlatTableProducer
Definition: SimpleFlatTableProducer.h:221
SimpleFlatTableProducerBase::UInt8Var
FuncVariable< StringObjectFunction< T >, uint8_t > UInt8Var
Definition: SimpleFlatTableProducer.h:116
SimpleFlatTableProducerBase::FloatVar
FuncVariable< StringObjectFunction< T >, float > FloatVar
Definition: SimpleFlatTableProducer.h:115
dumpMFGeometry_cfg.prod
prod
Definition: dumpMFGeometry_cfg.py:24
SimpleFlatTableProducer::maxLen_
const unsigned int maxLen_
Definition: SimpleFlatTableProducer.h:186
SimpleFlatTableProducerBase::~SimpleFlatTableProducerBase
~SimpleFlatTableProducerBase() override
Definition: SimpleFlatTableProducer.h:41
SimpleFlatTableProducerBase::VariableBase
Definition: SimpleFlatTableProducer.h:63
EventSingletonSimpleFlatTableProducer::EventSingletonSimpleFlatTableProducer
EventSingletonSimpleFlatTableProducer(edm::ParameterSet const &params)
Definition: SimpleFlatTableProducer.h:223
FirstObjectSimpleFlatTableProducer::FirstObjectSimpleFlatTableProducer
FirstObjectSimpleFlatTableProducer(edm::ParameterSet const &params)
Definition: SimpleFlatTableProducer.h:239
SimpleFlatTableProducerBase::FuncVariable::fill
void fill(std::vector< const T * > selobjs, nanoaod::FlatTable &out) const override
Definition: SimpleFlatTableProducer.h:93
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:244
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::ValueMapVariable::ValueMapVariable
ValueMapVariable(const std::string &aname, const edm::ParameterSet &cfg, edm::ConsumesCollector &&cc)
Definition: SimpleFlatTableProducer.h:197
SimpleFlatTableProducer::extvars_
std::vector< std::unique_ptr< ExtVariable > > extvars_
Definition: SimpleFlatTableProducer.h:217
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:58
TrackRefitter_38T_cff.src
src
Definition: TrackRefitter_38T_cff.py:24
SiStripPI::max
Definition: SiStripPayloadInspectorHelper.h:169
FirstObjectSimpleFlatTableProducer::~FirstObjectSimpleFlatTableProducer
~FirstObjectSimpleFlatTableProducer() override
Definition: SimpleFlatTableProducer.h:242
type
type
Definition: SiPixelVCal_PayloadInspector.cc:37
SimpleFlatTableProducer::~SimpleFlatTableProducer
~SimpleFlatTableProducer() override
Definition: SimpleFlatTableProducer.h:153
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:215
trackerHitRTTI::vector
Definition: trackerHitRTTI.h:21
edm::stream::EDProducer
Definition: EDProducer.h:38
SimpleFlatTableProducerBase::FuncVariable
Definition: SimpleFlatTableProducer.h:85
edm::EventSetup
Definition: EventSetup.h:57
SimpleFlatTableProducerBase::VariableBase::name_
std::string name_
Definition: SimpleFlatTableProducer.h:74
SimpleFlatTableProducerBase::VariableBase::VariableBase
VariableBase(const std::string &aname, const edm::ParameterSet &cfg)
Definition: SimpleFlatTableProducer.h:65
SimpleFlatTableProducerBase::src_
const edm::EDGetTokenT< TProd > src_
Definition: SimpleFlatTableProducer.h:61
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:126
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:75
StringCutObjectSelector.h
SimpleFlatTableProducerBase::VariableBase::doc_
std::string doc_
Definition: SimpleFlatTableProducer.h:74
T
long double T
Definition: Basic3DVectorLD.h:48
SimpleFlatTableProducer::ValueMapVariable
Definition: SimpleFlatTableProducer.h:195
StringCutObjectSelector< T >
Exception
Definition: hltDiff.cc:246
SimpleFlatTableProducerBase::fillTable
virtual std::unique_ptr< nanoaod::FlatTable > fillTable(const edm::Event &iEvent, const edm::Handle< TProd > &prod) const =0
SimpleFlatTableProducer
Definition: SimpleFlatTableProducer.h:122
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:92
ConsumesCollector.h
SimpleFlatTableProducer::cut_
const StringCutObjectSelector< T > cut_
Definition: SimpleFlatTableProducer.h:187
View.h
EventSingletonSimpleFlatTableProducer::~EventSingletonSimpleFlatTableProducer
~EventSingletonSimpleFlatTableProducer() override
Definition: SimpleFlatTableProducer.h:225
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:155
edm::Event
Definition: Event.h:73
SimpleFlatTableProducerBase::FuncVariable::FuncVariable
FuncVariable(const std::string &aname, const edm::ParameterSet &cfg)
Definition: SimpleFlatTableProducer.h:87
EventSingletonSimpleFlatTableProducer::fillTable
std::unique_ptr< nanoaod::FlatTable > fillTable(const edm::Event &, const edm::Handle< T > &prod) const override
Definition: SimpleFlatTableProducer.h:227
StringObjectFunction.h
SimpleFlatTableProducerBase::doc_
const std::string doc_
Definition: SimpleFlatTableProducer.h:59
edm::ConsumesCollector
Definition: ConsumesCollector.h:45
SimpleFlatTableProducerBase::IntVar
FuncVariable< StringObjectFunction< T >, int > IntVar
Definition: SimpleFlatTableProducer.h:114
SimpleFlatTableProducerBase::vars_
std::vector< std::unique_ptr< Variable > > vars_
Definition: SimpleFlatTableProducer.h:118
SimpleFlatTableProducer::DoubleExtVar
ValueMapVariable< double, float > DoubleExtVar
Definition: SimpleFlatTableProducer.h:214
SimpleFlatTableProducer::ValueMapVariable::fill
void fill(const edm::Event &iEvent, std::vector< edm::Ptr< T >> selptrs, nanoaod::FlatTable &out) const override
Definition: SimpleFlatTableProducer.h:199
SimpleFlatTableProducerBase::extension_
const bool extension_
Definition: SimpleFlatTableProducer.h:60