Skip to content

Commit

Permalink
Merge pull request #44040 from mbluj/CMSSW_14_0_X_httNanoAddons_rebase
Browse files Browse the repository at this point in the history
Lepton time-life info for NanoAOD and Nano branches from ValueMap of elaborated types (14_0_X)
  • Loading branch information
cmsbuild authored Feb 29, 2024
2 parents a552a1a + f0ab643 commit 4177026
Show file tree
Hide file tree
Showing 12 changed files with 1,009 additions and 23 deletions.
3 changes: 2 additions & 1 deletion DataFormats/VertexReco/BuildFile.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<use name="DataFormats/Common"/>
<use name="DataFormats/TrackReco"/>
<use name="DataFormats/CLHEP"/>
<use name="DataFormats/Math"/>
<use name="DataFormats/TrackReco"/>
<use name="DataFormats/TrajectorySeed"/>
<export>
<lib name="1"/>
Expand Down
81 changes: 81 additions & 0 deletions DataFormats/VertexReco/interface/TrackTimeLifeInfo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#ifndef DataFormats_VertexReco_TrackTimeLifeInfo_h
#define DataFormats_VertexReco_TrackTimeLifeInfo_h

/**
\class TrackTimeLifeInfo
\brief Structure to hold time-life information
\author Michal Bluj, NCBJ, Warsaw
*/

#include "DataFormats/GeometryCommonDetAlgo/interface/GlobalError.h"
#include "DataFormats/GeometryCommonDetAlgo/interface/Measurement1D.h"
#include "DataFormats/GeometryVector/interface/GlobalPoint.h"
#include "DataFormats/GeometryVector/interface/GlobalVector.h"
#include "DataFormats/TrackReco/interface/Track.h"
#include "DataFormats/VertexReco/interface/Vertex.h"

class TrackTimeLifeInfo {
public:
TrackTimeLifeInfo();
~TrackTimeLifeInfo() {}

// Secondary vertex
void setSV(reco::Vertex sv) {
sv_ = sv;
hasSV_ = true;
}
const reco::Vertex& sv() const { return sv_; }
bool hasSV() const { return hasSV_; }
void setFlightVector(GlobalVector flight_vec, GlobalError flight_cov) {
flight_vec_ = flight_vec;
flight_cov_ = flight_cov;
}
// Flight-path
const GlobalVector& flightVector() const { return flight_vec_; }
const GlobalError& flightCovariance() const { return flight_cov_; }
void setFlightLength(Measurement1D flightLength) { flightLength_ = flightLength; }
const Measurement1D& flightLength() const { return flightLength_; }
// Point of closest approach
void setPCA(GlobalPoint pca, GlobalError pca_cov) {
pca_ = pca;
pca_cov_ = pca_cov;
}
const GlobalPoint& pca() const { return pca_; }
const GlobalError& pcaCovariance() const { return pca_cov_; }
// Impact parameter
void setIP(GlobalVector ip_vec, GlobalError ip_cov) {
ip_vec_ = ip_vec;
ip_cov_ = ip_cov;
}
const GlobalVector& ipVector() const { return ip_vec_; }
const GlobalError& ipCovariance() const { return ip_cov_; }
void setIPLength(Measurement1D ipLength) { ipLength_ = ipLength; }
const Measurement1D& ipLength() const { return ipLength_; }
// Track
void setTrack(const reco::Track* track) {
if (track != nullptr) {
track_ = *track;
hasTrack_ = true;
} else {
track_ = reco::Track();
hasTrack_ = false;
}
}
const reco::Track* track() const { return &track_; }
bool hasTrack() const { return hasTrack_; }
void setBField_z(float bField_z) { bField_z_ = bField_z; }
float bField_z() const { return bField_z_; }

private:
bool hasSV_, hasTrack_;
reco::Vertex sv_;
GlobalVector flight_vec_, ip_vec_;
GlobalPoint pca_;
GlobalError flight_cov_, pca_cov_, ip_cov_;
Measurement1D flightLength_, ipLength_;
reco::Track track_;
float bField_z_;
};

#endif
16 changes: 16 additions & 0 deletions DataFormats/VertexReco/src/TrackTimeLifeInfo.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "DataFormats/VertexReco/interface/TrackTimeLifeInfo.h"

TrackTimeLifeInfo::TrackTimeLifeInfo()
: hasSV_(false),
hasTrack_(false),
sv_(reco::Vertex()),
flight_vec_(GlobalVector()),
ip_vec_(GlobalVector()),
pca_(GlobalPoint()),
flight_cov_(GlobalError()),
pca_cov_(GlobalError()),
ip_cov_(GlobalError()),
flightLength_(Measurement1D()),
ipLength_(Measurement1D()),
track_(reco::Track()),
bField_z_(0.){};
2 changes: 2 additions & 0 deletions DataFormats/VertexReco/src/classes.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@
#include "DataFormats/TrackReco/interface/Track.h"
#include "DataFormats/TrackReco/interface/TrackFwd.h"

#include "DataFormats/VertexReco/interface/TrackTimeLifeInfo.h"

#include <vector>
#include <utility>
5 changes: 5 additions & 0 deletions DataFormats/VertexReco/src/classes_def.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,9 @@
<class name="std::vector<std::pair<edm::Ref<std::vector<reco::Vertex>,reco::Vertex,edm::refhelper::FindUsingAdvance<std::vector<reco::Vertex>,reco::Vertex> >,int> >" />
<class name="std::pair<edm::Ref<std::vector<reco::Vertex>,reco::Vertex,edm::refhelper::FindUsingAdvance<std::vector<reco::Vertex>,reco::Vertex> >,int>" />

<class name="TrackTimeLifeInfo" />
<class name="std::vector<TrackTimeLifeInfo>"/>
<class name="edm::Wrapper<std::vector<TrackTimeLifeInfo> >"/>
<class name="edm::ValueMap<TrackTimeLifeInfo>"/>
<class name="edm::Wrapper<edm::ValueMap<TrackTimeLifeInfo> >"/>
</lcgdict>
170 changes: 151 additions & 19 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 overloaded 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 override {
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 override {
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 function 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 function 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 Expand Up @@ -445,7 +577,7 @@ class FirstObjectSimpleFlatTableProducer : public SimpleFlatTableProducerBase<T,
~FirstObjectSimpleFlatTableProducer() override {}

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

Expand Down
Loading

0 comments on commit 4177026

Please sign in to comment.