Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 150 additions & 18 deletions PhysicsTools/NanoAOD/interface/SimpleFlatTableProducer.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,18 @@ class FuncVariable : public Variable<ObjType> {
precisionFunc_(cfg.existsAs<std::string>("precision") ? cfg.getParameter<std::string>("precision") : "23",
true) {}
~FuncVariable() override {}

void fill(std::vector<const ObjType *> &selobjs, nanoaod::FlatTable &out) const override {
std::vector<ValType> vals(selobjs.size());
for (unsigned int i = 0, n = vals.size(); i < n; ++i) {
ValType val = func_(*selobjs[i]);
vals[i] = func_(*selobjs[i]);
if constexpr (std::is_same<ValType, float>()) {
if (this->precision_ == -2) {
auto prec = precisionFunc_(*selobjs[i]);
vals[i] = prec > 0 ? MiniFloatConverter::reduceMantissaToNbitsRounding(val, prec) : val;
} else
vals[i] = val;
} else {
vals[i] = val;
if (prec > 0) {
vals[i] = MiniFloatConverter::reduceMantissaToNbitsRounding(vals[i], prec);
}
}
}
}
out.template addColumn<ValType>(this->name_, vals, this->doc_, this->precision_);
Expand All @@ -82,24 +82,27 @@ class ExtVariable : public VariableBase {
std::vector<edm::Ptr<ObjType>> selptrs,
nanoaod::FlatTable &out) const = 0;
};

template <typename ObjType, typename TIn, typename ValType = TIn>
class ValueMapVariable : public ExtVariable<ObjType> {
class ValueMapVariableBase : public ExtVariable<ObjType> {
public:
ValueMapVariable(const std::string &aname,
const edm::ParameterSet &cfg,
edm::ConsumesCollector &&cc,
bool skipNonExistingSrc = false)
ValueMapVariableBase(const std::string &aname,
const edm::ParameterSet &cfg,
edm::ConsumesCollector &&cc,
bool skipNonExistingSrc = false)
: ExtVariable<ObjType>(aname, cfg),
skipNonExistingSrc_(skipNonExistingSrc),
token_(cc.consumes<edm::ValueMap<TIn>>(cfg.getParameter<edm::InputTag>("src"))) {}
virtual ValType eval(const edm::Handle<edm::ValueMap<TIn>> &vmap, const edm::Ptr<ObjType> &op) const = 0;
void fill(const edm::Event &iEvent, std::vector<edm::Ptr<ObjType>> selptrs, nanoaod::FlatTable &out) const override {
edm::Handle<edm::ValueMap<TIn>> vmap;
iEvent.getByToken(token_, vmap);
std::vector<ValType> vals;
if (vmap.isValid() || !skipNonExistingSrc_) {
vals.resize(selptrs.size());
for (unsigned int i = 0, n = vals.size(); i < n; ++i) {
vals[i] = (*vmap)[selptrs[i]];
// calls the overloade method to either get the valuemap value directly, or a function of the object value.
vals[i] = this->eval(vmap, selptrs[i]);
}
}
out.template addColumn<ValType>(this->name_, vals, this->doc_, this->precision_);
Expand All @@ -110,6 +113,50 @@ class ValueMapVariable : public ExtVariable<ObjType> {
edm::EDGetTokenT<edm::ValueMap<TIn>> token_;
};

template <typename ObjType, typename TIn, typename ValType = TIn>
class ValueMapVariable : public ValueMapVariableBase<ObjType, TIn, ValType> {
public:
ValueMapVariable(const std::string &aname,
const edm::ParameterSet &cfg,
edm::ConsumesCollector &&cc,
bool skipNonExistingSrc = false)
: ValueMapVariableBase<ObjType, TIn, ValType>(aname, cfg, std::move(cc), skipNonExistingSrc) {}
ValType eval(const edm::Handle<edm::ValueMap<TIn>> &vmap, const edm::Ptr<ObjType> &op) const {
ValType val = (*vmap)[op];
return val;
}
};

template <typename ObjType, typename TIn, typename StringFunctor, typename ValType>
class TypedValueMapVariable : public ValueMapVariableBase<ObjType, TIn, ValType> {
public:
TypedValueMapVariable(const std::string &aname,
const edm::ParameterSet &cfg,
edm::ConsumesCollector &&cc,
bool skipNonExistingSrc = false)
: ValueMapVariableBase<ObjType, TIn, ValType>(aname, cfg, std::move(cc), skipNonExistingSrc),
func_(cfg.getParameter<std::string>("expr"), true),
precisionFunc_(cfg.existsAs<std::string>("precision") ? cfg.getParameter<std::string>("precision") : "23",
true) {}

ValType eval(const edm::Handle<edm::ValueMap<TIn>> &vmap, const edm::Ptr<ObjType> &op) const {
ValType val = func_((*vmap)[op]);
if constexpr (std::is_same<ValType, float>()) {
if (this->precision_ == -2) {
auto prec = precisionFunc_(*op);
if (prec > 0) {
val = MiniFloatConverter::reduceMantissaToNbitsRounding(val, prec);
}
}
}
return val;
}

protected:
StringFunctor func_;
StringObjectFunction<ObjType> precisionFunc_;
};

// Event producers
// - ABC
// - Singleton
Expand Down Expand Up @@ -262,7 +309,7 @@ class SimpleFlatTableProducer : public SimpleFlatTableProducerBase<T, edm::View<

~SimpleFlatTableProducer() override {}

static void fillDescriptions(edm::ConfigurationDescriptions &descriptions) {
static edm::ParameterSetDescription baseDescriptions() {
edm::ParameterSetDescription desc = SimpleFlatTableProducerBase<T, edm::View<T>>::baseDescriptions();

desc.ifValue(edm::ParameterDescription<bool>(
Expand All @@ -283,8 +330,10 @@ class SimpleFlatTableProducer : public SimpleFlatTableProducerBase<T, edm::View<
extvariable.addOptionalNode(
edm::ParameterDescription<int>(
"precision", true, edm::Comment("the precision with which to store the value in the flat table")) xor
edm::ParameterDescription<std::string>(
"precision", true, edm::Comment("the precision with which to store the value in the flat table")),
edm::ParameterDescription<std::string>("precision",
true,
edm::Comment("the precision with which to store the value in the "
"flat table, as a fucntion of the object evaluated")),
false);

edm::ParameterSetDescription extvariables;
Expand All @@ -293,9 +342,12 @@ class SimpleFlatTableProducer : public SimpleFlatTableProducerBase<T, edm::View<
edm::ParameterWildcard<edm::ParameterSetDescription>("*", edm::RequireZeroOrMore, true, extvariable), false);
desc.addOptional<edm::ParameterSetDescription>("externalVariables", extvariables);

return desc;
}
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions) {
edm::ParameterSetDescription desc = SimpleFlatTableProducer<T>::baseDescriptions();
descriptions.addWithDefaultLabel(desc);
}

std::unique_ptr<nanoaod::FlatTable> fillTable(const edm::Event &iEvent,
const edm::Handle<edm::View<T>> &prod) const override {
std::vector<const T *> selobjs;
Expand All @@ -304,14 +356,14 @@ class SimpleFlatTableProducer : public SimpleFlatTableProducerBase<T, edm::View<
if (singleton_) {
assert(prod->size() == 1);
selobjs.push_back(&(*prod)[0]);
if (!extvars_.empty())
if (!extvars_.empty() || !typedextvars_.empty())
selptrs.emplace_back(prod->ptrAt(0));
} else {
for (unsigned int i = 0, n = prod->size(); i < n; ++i) {
const auto &obj = (*prod)[i];
if (cut_(obj)) {
selobjs.push_back(&obj);
if (!extvars_.empty())
if (!extvars_.empty() || !typedextvars_.empty())
selptrs.emplace_back(prod->ptrAt(i));
}
if (selobjs.size() >= maxLen_)
Expand All @@ -324,6 +376,8 @@ class SimpleFlatTableProducer : public SimpleFlatTableProducerBase<T, edm::View<
var->fill(selobjs, *out);
for (const auto &var : this->extvars_)
var->fill(iEvent, selptrs, *out);
for (const auto &var : this->typedextvars_)
var->fill(iEvent, selptrs, *out);
return out;
}

Expand All @@ -341,6 +395,84 @@ class SimpleFlatTableProducer : public SimpleFlatTableProducerBase<T, edm::View<
typedef ValueMapVariable<T, int, int16_t> Int16ExtVar;
typedef ValueMapVariable<T, int, uint16_t> UInt16ExtVar;
std::vector<std::unique_ptr<ExtVariable<T>>> extvars_;
std::vector<std::unique_ptr<ExtVariable<T>>> typedextvars_;
};

template <typename T, typename V>
class SimpleTypedExternalFlatTableProducer : public SimpleFlatTableProducer<T> {
public:
SimpleTypedExternalFlatTableProducer(edm::ParameterSet const &params) : SimpleFlatTableProducer<T>(params) {
edm::ParameterSet const &extvarsPSet = params.getParameter<edm::ParameterSet>("externalTypedVariables");
for (const std::string &vname : extvarsPSet.getParameterNamesForType<edm::ParameterSet>()) {
const auto &varPSet = extvarsPSet.getParameter<edm::ParameterSet>(vname);
const std::string &type = varPSet.getParameter<std::string>("type");
if (type == "int")
this->typedextvars_.push_back(
std::make_unique<IntTypedExtVar>(vname, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
else if (type == "uint")
this->typedextvars_.push_back(
std::make_unique<UIntTypedExtVar>(vname, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
else if (type == "float")
this->typedextvars_.push_back(
std::make_unique<FloatTypedExtVar>(vname, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
else if (type == "double")
this->typedextvars_.push_back(
std::make_unique<DoubleTypedExtVar>(vname, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
else if (type == "uint8")
this->typedextvars_.push_back(
std::make_unique<UInt8TypedExtVar>(vname, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
else if (type == "int16")
this->typedextvars_.push_back(
std::make_unique<Int16TypedExtVar>(vname, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
else if (type == "uint16")
this->typedextvars_.push_back(
std::make_unique<UInt16TypedExtVar>(vname, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
else if (type == "bool")
this->typedextvars_.push_back(
std::make_unique<BoolTypedExtVar>(vname, varPSet, this->consumesCollector(), this->skipNonExistingSrc_));
else
throw cms::Exception("Configuration", "unsupported type " + type + " for variable " + vname);
}
}
~SimpleTypedExternalFlatTableProducer() override {}
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions) {
edm::ParameterSetDescription desc = SimpleFlatTableProducer<T>::baseDescriptions();
edm::ParameterSetDescription extvariable;
extvariable.add<edm::InputTag>("src")->setComment("valuemap input collection to fill the flat table");
extvariable.add<std::string>("expr")->setComment(
"a function to define the content of the branch in the flat table");
extvariable.add<std::string>("doc")->setComment("few words description of the branch content");
extvariable.ifValue(
edm::ParameterDescription<std::string>(
"type", "int", true, edm::Comment("the c++ type of the branch in the flat table")),
edm::allowedValues<std::string>("int", "uint", "float", "double", "uint8", "int16", "uint16", "bool"));
extvariable.addOptionalNode(
edm::ParameterDescription<int>(
"precision", true, edm::Comment("the precision with which to store the value in the flat table")) xor
edm::ParameterDescription<std::string>("precision",
true,
edm::Comment("the precision with which to store the value in the "
"flat table, as a fucntion of the object evaluated")),
false);

edm::ParameterSetDescription extvariables;
extvariables.setComment("a parameters set to define all variable taken form valuemap to fill the flat table");
extvariables.addOptionalNode(
edm::ParameterWildcard<edm::ParameterSetDescription>("*", edm::RequireZeroOrMore, true, extvariable), false);
desc.addOptional<edm::ParameterSetDescription>("externalTypedVariables", extvariables);

descriptions.addWithDefaultLabel(desc);
}

protected:
typedef TypedValueMapVariable<T, V, StringObjectFunction<V>, int32_t> IntTypedExtVar;
typedef TypedValueMapVariable<T, V, StringObjectFunction<V>, uint32_t> UIntTypedExtVar;
typedef TypedValueMapVariable<T, V, StringObjectFunction<V>, float> FloatTypedExtVar;
typedef TypedValueMapVariable<T, V, StringObjectFunction<V>, double> DoubleTypedExtVar;
typedef TypedValueMapVariable<T, V, StringCutObjectSelector<V>, bool> BoolTypedExtVar;
typedef TypedValueMapVariable<T, V, StringObjectFunction<V>, uint8_t> UInt8TypedExtVar;
typedef TypedValueMapVariable<T, V, StringObjectFunction<V>, int16_t> Int16TypedExtVar;
typedef TypedValueMapVariable<T, V, StringObjectFunction<V>, uint16_t> UInt16TypedExtVar;
};

template <typename T>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#include "DataFormats/Candidate/interface/Candidate.h"
typedef SimpleFlatTableProducer<reco::Candidate> SimpleCandidateFlatTableProducer;

typedef SimpleTypedExternalFlatTableProducer<reco::Candidate, reco::Candidate>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a dummy producer that would have to be removed in the final PR

SimpleCandidate2CandidateFlatTableProducer;

#include "SimDataFormats/GeneratorProducts/interface/GenEventInfoProduct.h"
typedef EventSingletonSimpleFlatTableProducer<GenEventInfoProduct> SimpleGenEventFlatTableProducer;

Expand Down Expand Up @@ -59,6 +62,7 @@ typedef SimpleFlatTableProducer<Run3ScoutingTrack> SimpleRun3ScoutingTrackFlatTa

#include "FWCore/Framework/interface/MakerMacros.h"
DEFINE_FWK_MODULE(SimpleCandidateFlatTableProducer);
DEFINE_FWK_MODULE(SimpleCandidate2CandidateFlatTableProducer);
DEFINE_FWK_MODULE(SimpleGenEventFlatTableProducer);
DEFINE_FWK_MODULE(SimpleGenFilterFlatTableProducerLumi);
DEFINE_FWK_MODULE(SimpleHTXSFlatTableProducer);
Expand Down