Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EventSetupInitTrait can now have state #36633

Merged
merged 1 commit into from
Jan 12, 2022
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
4 changes: 3 additions & 1 deletion CommonTools/CandAlgos/interface/CandCombiner.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ namespace reco {
/// constructor from parameter settypedef
explicit CandCombiner(const edm::ParameterSet& cfg)
: CandCombinerBase(cfg),
combinerInit_(consumesCollector()),
combiner_(reco::modules::make<Selector>(cfg, consumesCollector()),
reco::modules::make<PairSelector>(cfg),
Setup(cfg),
Expand All @@ -146,7 +147,7 @@ namespace reco {
private:
/// process an event
void produce(edm::Event& evt, const edm::EventSetup& es) override {
Init::init(combiner_.setup(), evt, es);
combinerInit_.init(combiner_.setup(), evt, es);
int n = labels_.size();
std::vector<edm::Handle<CandidateView> > colls(n);
for (int i = 0; i < n; ++i)
Expand All @@ -168,6 +169,7 @@ namespace reco {
evt.put(std::move(out));
}
/// combiner utility
Init combinerInit_;
::CandCombiner<Selector, PairSelector, Cloner, OutputCollection, Setup> combiner_;

RoleNames names_;
Expand Down
4 changes: 3 additions & 1 deletion CommonTools/RecoAlgos/interface/CandidateProducer.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class CandidateProducer : public edm::stream::EDProducer<> {
CandidateProducer(const edm::ParameterSet& cfg)
: srcToken_(consumes<TColl>(cfg.template getParameter<edm::InputTag>("src"))),
converter_(cfg, consumesCollector()),
selectorInit_(consumesCollector()),
selector_(reco::modules::make<Selector>(cfg, consumesCollector())),
initialized_(false) {
produces<CColl>();
Expand All @@ -92,7 +93,7 @@ class CandidateProducer : public edm::stream::EDProducer<> {
void produce(edm::Event& evt, const edm::EventSetup& es) override {
edm::Handle<TColl> src;
evt.getByToken(srcToken_, src);
Init::init(selector_, evt, es);
selectorInit_.init(selector_, evt, es);
::helper::MasterCollection<TColl> master(src, evt);
std::unique_ptr<CColl> cands(new CColl);
if (!src->empty()) {
Expand All @@ -110,6 +111,7 @@ class CandidateProducer : public edm::stream::EDProducer<> {
/// converter helper
Conv converter_;
/// selector
Init selectorInit_;
Selector selector_;
/// particles initialized?
bool initialized_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,19 @@ namespace reco {
namespace modules {
template <typename S>
struct AssociatedVariableCollectionSelectorEventSetupInit {
static void init(S& s, const edm::Event& evt, const edm::EventSetup& es) {
typedef typename EventSetupInit<typename S::selector>::type ESI;
ESI::init(s.select_, evt, es);
}
explicit AssociatedVariableCollectionSelectorEventSetupInit(edm::ConsumesCollector iC) : esi_(iC) {}

void init(S& s, const edm::Event& evt, const edm::EventSetup& es) { esi_.init(s.select_, evt, es); }
typedef typename EventSetupInit<typename S::selector>::type ESI;
ESI esi_;
};

template <typename I, typename V, typename S, typename O, typename C, typename R>
struct EventSetupInit<AssociatedVariableCollectionSelector<I, V, S, O, C, R> > {
typedef AssociatedVariableCollectionSelectorEventSetupInit<AssociatedVariableCollectionSelector<I, V, S, O, C, R> >
type;
};

} // namespace modules
} // namespace reco

Expand Down
76 changes: 49 additions & 27 deletions CommonTools/UtilAlgos/interface/EventSetupInitTrait.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define UtilAlgos_EventSetupInitTrait_h
#include "CommonTools/UtilAlgos/interface/AndSelector.h"
#include "CommonTools/UtilAlgos/interface/OrSelector.h"
#include "FWCore/Framework/interface/ConsumesCollector.h"

namespace edm {
class EventSetup;
Expand All @@ -17,14 +18,18 @@ namespace reco {
/// take no action (default)
template <typename T>
struct NoEventSetupInit {
static void init(T&, const edm::Event&, const edm::EventSetup&) {}
explicit NoEventSetupInit(edm::ConsumesCollector) {}
NoEventSetupInit() = delete;
void init(T&, const edm::Event&, const edm::EventSetup&) {}
};

/// implement common interface defined in:
/// https://twiki.cern.ch/twiki/bin/view/CMS/SelectorInterface
struct CommonSelectorEventSetupInit {
explicit CommonSelectorEventSetupInit(edm::ConsumesCollector) {}
CommonSelectorEventSetupInit() = delete;
template <typename Selector>
static void init(Selector& selector, const edm::Event& evt, const edm::EventSetup& es) {
void init(Selector& selector, const edm::Event& evt, const edm::EventSetup& es) {
selector.newEvent(evt, es);
}
};
Expand All @@ -40,51 +45,68 @@ namespace reco {
typename T4 = helpers::NullAndOperand,
typename T5 = helpers::NullAndOperand>
struct CombinedEventSetupInit {
explicit CombinedEventSetupInit(edm::ConsumesCollector iC) : t1_(iC), t2_(iC), t3_(iC), t4_(iC), t5_(iC) {}
template <template <typename, typename, typename, typename, typename> class SelectorT>
static void init(SelectorT<T1, T2, T3, T4, T5>& selector, const edm::Event& evt, const edm::EventSetup& es) {
EventSetupInit<T1>::type::init(selector.s1_, evt, es);
EventSetupInit<T2>::type::init(selector.s2_, evt, es);
EventSetupInit<T3>::type::init(selector.s3_, evt, es);
EventSetupInit<T4>::type::init(selector.s4_, evt, es);
EventSetupInit<T5>::type::init(selector.s5_, evt, es);
void init(SelectorT<T1, T2, T3, T4, T5>& selector, const edm::Event& evt, const edm::EventSetup& es) {
t1_.init(selector.s1_, evt, es);
t2_.init(selector.s2_, evt, es);
t3_.init(selector.s3_, evt, es);
t4_.init(selector.s4_, evt, es);
t5_.init(selector.s5_, evt, es);
}
typename EventSetupInit<T1>::type t1_;
typename EventSetupInit<T2>::type t2_;
typename EventSetupInit<T3>::type t3_;
typename EventSetupInit<T4>::type t4_;
typename EventSetupInit<T5>::type t5_;
};

template <typename T1, typename T2, typename T3, typename T4>
struct CombinedEventSetupInit<T1, T2, T3, T4, helpers::NullAndOperand> {
explicit CombinedEventSetupInit(edm::ConsumesCollector iC) : t1_(iC), t2_(iC), t3_(iC), t4_(iC) {}
template <template <typename, typename, typename, typename, typename> class SelectorT>
static void init(SelectorT<T1, T2, T3, T4, helpers::NullAndOperand>& selector,
const edm::Event& evt,
const edm::EventSetup& es) {
EventSetupInit<T1>::type::init(selector.s1_, evt, es);
EventSetupInit<T2>::type::init(selector.s2_, evt, es);
EventSetupInit<T3>::type::init(selector.s3_, evt, es);
EventSetupInit<T4>::type::init(selector.s4_, evt, es);
void init(SelectorT<T1, T2, T3, T4, helpers::NullAndOperand>& selector,
const edm::Event& evt,
const edm::EventSetup& es) {
t1_.init(selector.s1_, evt, es);
t2_.init(selector.s2_, evt, es);
t3_.init(selector.s3_, evt, es);
t4_.init(selector.s4_, evt, es);
}
typename EventSetupInit<T1>::type t1_;
typename EventSetupInit<T2>::type t2_;
typename EventSetupInit<T3>::type t3_;
typename EventSetupInit<T4>::type t4_;
};

template <typename T1, typename T2, typename T3>
struct CombinedEventSetupInit<T1, T2, T3, helpers::NullAndOperand, helpers::NullAndOperand> {
explicit CombinedEventSetupInit(edm::ConsumesCollector iC) : t1_(iC), t2_(iC), t3_(iC) {}
template <template <typename, typename, typename, typename, typename> class SelectorT>
static void init(SelectorT<T1, T2, T3, helpers::NullAndOperand, helpers::NullAndOperand>& selector,
const edm::Event& evt,
const edm::EventSetup& es) {
EventSetupInit<T1>::type::init(selector.s1_, evt, es);
EventSetupInit<T2>::type::init(selector.s2_, evt, es);
EventSetupInit<T3>::type::init(selector.s3_, evt, es);
void init(SelectorT<T1, T2, T3, helpers::NullAndOperand, helpers::NullAndOperand>& selector,
const edm::Event& evt,
const edm::EventSetup& es) {
t1_.init(selector.s1_, evt, es);
t2_.init(selector.s2_, evt, es);
t3_.init(selector.s3_, evt, es);
}
typename EventSetupInit<T1>::type t1_;
typename EventSetupInit<T2>::type t2_;
typename EventSetupInit<T3>::type t3_;
};

template <typename T1, typename T2>
struct CombinedEventSetupInit<T1, T2, helpers::NullAndOperand, helpers::NullAndOperand, helpers::NullAndOperand> {
explicit CombinedEventSetupInit(edm::ConsumesCollector iC) : t1_(iC), t2_(iC) {}
template <template <typename, typename, typename, typename, typename> class SelectorT>
static void init(
SelectorT<T1, T2, helpers::NullAndOperand, helpers::NullAndOperand, helpers::NullAndOperand>& selector,
const edm::Event& evt,
const edm::EventSetup& es) {
EventSetupInit<T1>::type::init(selector.s1_, evt, es);
EventSetupInit<T2>::type::init(selector.s2_, evt, es);
void init(SelectorT<T1, T2, helpers::NullAndOperand, helpers::NullAndOperand, helpers::NullAndOperand>& selector,
const edm::Event& evt,
const edm::EventSetup& es) {
t1_.init(selector.s1_, evt, es);
t2_.init(selector.s2_, evt, es);
}
typename EventSetupInit<T1>::type t1_;
typename EventSetupInit<T2>::type t2_;
};

template <typename T1, typename T2, typename T3, typename T4, typename T5>
Expand Down
5 changes: 3 additions & 2 deletions CommonTools/UtilAlgos/interface/ObjectSelectorBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class ObjectSelectorBase : public Base {
srcToken_(
this->template consumes<typename Selector::collection>(cfg.template getParameter<edm::InputTag>("src"))),
filter_(false),
selectorInit_(this->consumesCollector()),
selector_(cfg, this->consumesCollector()),
sizeSelector_(reco::modules::make<SizeSelector>(cfg)),
postProcessor_(cfg, this->consumesCollector()) {
Expand All @@ -54,8 +55,7 @@ class ObjectSelectorBase : public Base {
private:
/// process one event
bool filter(edm::Event& evt, const edm::EventSetup& es) override {
Init::init(selector_, evt, es);
using namespace std;
selectorInit_.init(selector_, evt, es);
edm::Handle<typename Selector::collection> source;
evt.getByToken(srcToken_, source);
StoreManager manager(source);
Expand All @@ -71,6 +71,7 @@ class ObjectSelectorBase : public Base {
/// filter event
bool filter_;
/// Object collection selector
Init selectorInit_;
Selector selector_;
/// selected object collection size selector
SizeSelector sizeSelector_;
Expand Down
4 changes: 3 additions & 1 deletion CommonTools/UtilAlgos/interface/ObjectSelectorProducer.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class ObjectSelectorProducer : public Base {
: Base(cfg),
srcToken_(
this->template consumes<typename Selector::collection>(cfg.template getParameter<edm::InputTag>("src"))),
selectorInit_(this->consumesCollector()),
selector_(cfg, this->consumesCollector()),
postProcessor_(cfg, this->consumesCollector()) {
postProcessor_.init(*this);
Expand All @@ -44,7 +45,7 @@ class ObjectSelectorProducer : public Base {
private:
/// process one event
void produce(edm::Event& evt, const edm::EventSetup& es) override {
Init::init(selector_, evt, es);
selectorInit_.init(selector_, evt, es);
edm::Handle<typename Selector::collection> source;
evt.getByToken(srcToken_, source);
StoreManager manager(source);
Expand All @@ -56,6 +57,7 @@ class ObjectSelectorProducer : public Base {
/// source collection label
edm::EDGetTokenT<typename Selector::collection> srcToken_;
/// Object collection selector
Init selectorInit_;
Selector selector_;
/// post processor
PostProcessor postProcessor_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ namespace reco {
namespace modules {
template <typename S>
struct SingleElementCollectionRefSelectorEventSetupInit {
static void init(S& s, const edm::Event& ev, const edm::EventSetup& es) {
typedef typename EventSetupInit<typename S::selector>::type ESI;
ESI::init(s.select_, ev, es);
}
explicit SingleElementCollectionRefSelectorEventSetupInit(edm::ConsumesCollector iC) : esi_(iC) {}
void init(S& s, const edm::Event& ev, const edm::EventSetup& es) { esi_.init(s.select_, ev, es); }
typedef typename EventSetupInit<typename S::selector>::type ESI;
ESI esi_;
};

template <typename I, typename S, typename O, typename C, typename R>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ namespace reco {
namespace modules {
template <typename S>
struct SingleElementCollectionSelectorEventSetupInit {
static void init(S& s, const edm::Event& ev, const edm::EventSetup& es) {
typedef typename EventSetupInit<typename S::selector>::type ESI;
ESI::init(s.select_, ev, es);
}
explicit SingleElementCollectionSelectorEventSetupInit(edm::ConsumesCollector iC) : esi_(iC) {}
typedef typename EventSetupInit<typename S::selector>::type ESI;
void init(S& s, const edm::Event& ev, const edm::EventSetup& es) { esi_.init(s.select_, ev, es); }
ESI esi_;
};

template <typename I, typename S, typename O, typename C, typename R>
Expand Down
4 changes: 2 additions & 2 deletions PhysicsTools/RecoAlgos/interface/MassKinFitterCandProducer.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
*
*/
#include "FWCore/Utilities/interface/InputTag.h"
#include "FWCore/Framework/interface/EDProducer.h"
#include "FWCore/Framework/interface/stream/EDProducer.h"
#include "PhysicsTools/RecoUtils/interface/CandMassKinFitter.h"

#include "DataFormats/Candidate/interface/Candidate.h"

class MassKinFitterCandProducer : public edm::EDProducer {
class MassKinFitterCandProducer : public edm::stream::EDProducer<> {
public:
explicit MassKinFitterCandProducer(const edm::ParameterSet &, CandMassKinFitter * = nullptr);

Expand Down
8 changes: 4 additions & 4 deletions PhysicsTools/RecoAlgos/plugins/CandCommonVertexFitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ namespace reco {
namespace modules {
template <typename Fitter>
struct CandVertexFitterEventSetupInit {
static void init(CandCommonVertexFitter<Fitter>& fitter, const edm::Event& evt, const edm::EventSetup& es) {
edm::ESHandle<MagneticField> h;
es.get<IdealMagneticFieldRecord>().get(h);
fitter.set(h.product());
explicit CandVertexFitterEventSetupInit(edm::ConsumesCollector iC) : magToken_(iC.esConsumes()) {}
void init(CandCommonVertexFitter<Fitter>& fitter, const edm::Event& evt, const edm::EventSetup& es) {
fitter.set(&es.getData(magToken_));
}
edm::ESGetToken<MagneticField, IdealMagneticFieldRecord> magToken_;
};

template <typename Fitter>
Expand Down
16 changes: 9 additions & 7 deletions PhysicsTools/RecoAlgos/plugins/CandKinematicVertexFitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@
namespace reco {
namespace modules {
struct CandKinematicVertexFitterEventSetupInit {
static void init(CandKinematicVertexFitter& fitter, const edm::Event& evt, const edm::EventSetup& es) {
edm::ESHandle<MagneticField> h;
es.get<IdealMagneticFieldRecord>().get(h);
fitter.set(h.product());
edm::ESHandle<ParticleDataTable> pdt;
es.getData(pdt);
fitter.set(pdt.product());
explicit CandKinematicVertexFitterEventSetupInit(edm::ConsumesCollector iC)
: magToken_(iC.esConsumes()), pdtToken_(iC.esConsumes()) {}

void init(CandKinematicVertexFitter& fitter, const edm::Event& evt, const edm::EventSetup& es) {
fitter.set(&es.getData(magToken_));
fitter.set(&es.getData(pdtToken_));
}

edm::ESGetToken<MagneticField, IdealMagneticFieldRecord> magToken_;
edm::ESGetToken<ParticleDataTable, edm::DefaultRecord> pdtToken_;
};

template <>
Expand Down
8 changes: 5 additions & 3 deletions PhysicsTools/RecoAlgos/plugins/ConstrainedFitCandProducer.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* \author Luca Lista, INFN
*
*/
#include "FWCore/Framework/interface/EDProducer.h"
#include "FWCore/Framework/interface/stream/EDProducer.h"
#include "FWCore/Utilities/interface/InputTag.h"
#include "CommonTools/UtilAlgos/interface/ParameterAdapter.h"
#include "CommonTools/UtilAlgos/interface/EventSetupInitTrait.h"
Expand All @@ -17,7 +17,7 @@ template <typename Fitter,
typename InputCollection = reco::CandidateCollection,
typename OutputCollection = InputCollection,
typename Init = typename ::reco::modules::EventSetupInit<Fitter>::type>
class ConstrainedFitCandProducer : public edm::EDProducer {
class ConstrainedFitCandProducer : public edm::stream::EDProducer<> {
public:
explicit ConstrainedFitCandProducer(const edm::ParameterSet&);

Expand All @@ -27,6 +27,7 @@ class ConstrainedFitCandProducer : public edm::EDProducer {
bool setMassConstraint_;
bool setPdgId_;
int pdgId_;
Init fitterInit_;
Fitter fitter_;
void produce(edm::Event&, const edm::EventSetup&) override;
};
Expand All @@ -47,6 +48,7 @@ ConstrainedFitCandProducer<Fitter, InputCollection, OutputCollection, Init>::Con
setLongLived_(false),
setMassConstraint_(false),
setPdgId_(false),
fitterInit_(consumesCollector()),
fitter_(reco::modules::make<Fitter>(cfg)) {
produces<OutputCollection>();
std::string alias(cfg.getParameter<std::string>("@module_label"));
Expand Down Expand Up @@ -92,7 +94,7 @@ namespace reco {
template <typename Fitter, typename InputCollection, typename OutputCollection, typename Init>
void ConstrainedFitCandProducer<Fitter, InputCollection, OutputCollection, Init>::produce(edm::Event& evt,
const edm::EventSetup& es) {
Init::init(fitter_, evt, es);
fitterInit_.init(fitter_, evt, es);
edm::Handle<InputCollection> cands;
evt.getByToken(srcToken_, cands);
auto fitted = std::make_unique<OutputCollection>();
Expand Down