Skip to content

Commit f046846

Browse files
committed
Conditionally produce/consume in GeneratorSmearedProducer
Only call produces and consumes if the data products the module want appear to be in the job. This solves the case where the module has been added to a job just on the remote possibility it might be needed but infact the values which are actually wanted come from the source.
1 parent e9f46cc commit f046846

File tree

1 file changed

+57
-16
lines changed

1 file changed

+57
-16
lines changed

GeneratorInterface/Core/plugins/GeneratorSmearedProducer.cc

Lines changed: 57 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,39 +27,80 @@ class GeneratorSmearedProducer : public edm::global::EDProducer<> {
2727
static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
2828

2929
private:
30-
const edm::EDGetTokenT<edm::HepMCProduct> newToken_;
31-
const edm::EDGetTokenT<edm::HepMCProduct> oldToken_;
32-
const edm::EDGetTokenT<edm::HepMC3Product> Token3_;
30+
edm::EDGetTokenT<edm::HepMCProduct> newToken_;
31+
edm::EDGetTokenT<edm::HepMCProduct> oldToken_;
32+
edm::EDGetTokenT<edm::HepMC3Product> Token3_;
3333
};
3434

35-
GeneratorSmearedProducer::GeneratorSmearedProducer(edm::ParameterSet const& ps)
36-
: newToken_(consumes<edm::HepMCProduct>(ps.getUntrackedParameter<edm::InputTag>("currentTag"))),
37-
oldToken_(consumes<edm::HepMCProduct>(ps.getUntrackedParameter<edm::InputTag>("previousTag"))),
38-
Token3_(consumes<edm::HepMC3Product>(ps.getUntrackedParameter<edm::InputTag>("currentTag"))) {
35+
namespace {
36+
template <typename T>
37+
bool match(edm::InputTag const& iTag, edm::ProductDescription const& iDesc) {
38+
if (iDesc.unwrappedTypeID() == edm::TypeID(typeid(T))) {
39+
if (iDesc.moduleLabel() == iTag.label() and iDesc.productInstanceName() == iTag.instance()) {
40+
if (iTag.process().empty() or iTag.willSkipCurrentProcess() or
41+
iTag.process() == edm::InputTag::kCurrentProcess) {
42+
return true;
43+
}
44+
} else {
45+
return iTag.process() == iDesc.processName();
46+
}
47+
}
48+
return false;
49+
}
50+
} // namespace
51+
52+
GeneratorSmearedProducer::GeneratorSmearedProducer(edm::ParameterSet const& ps) {
3953
// This producer produces a HepMCProduct, a copy of the original one
4054
// It is used for backward compatibility
4155
// If HepMC3Product exists, it produces its copy
4256
// It adds "generatorSmeared" to description, which is needed for further processing
43-
produces<edm::HepMCProduct>();
44-
produces<edm::HepMC3Product>();
57+
auto currentTag = ps.getUntrackedParameter<edm::InputTag>("currentTag");
58+
auto previousTag = ps.getUntrackedParameter<edm::InputTag>("previousTag");
59+
callWhenNewProductsRegistered([this, currentTag, previousTag](edm::ProductDescription const& desc) {
60+
bool oldHep = false;
61+
if (match<edm::HepMCProduct>(currentTag, desc)) {
62+
if (newToken_.isUninitialized()) {
63+
newToken_ = consumes<edm::HepMCProduct>(currentTag);
64+
oldHep = true;
65+
}
66+
}
67+
if (match<edm::HepMCProduct>(previousTag, desc)) {
68+
if (oldToken_.isUninitialized()) {
69+
oldToken_ = consumes<edm::HepMCProduct>(previousTag);
70+
oldHep = true;
71+
}
72+
}
73+
if (oldHep) {
74+
produces<edm::HepMCProduct>();
75+
}
76+
if (match<edm::HepMC3Product>(currentTag, desc)) {
77+
Token3_ = consumes<edm::HepMC3Product>(currentTag);
78+
produces<edm::HepMC3Product>();
79+
}
80+
});
4581
}
4682

4783
void GeneratorSmearedProducer::produce(edm::StreamID, edm::Event& iEvent, const edm::EventSetup& es) const {
4884
edm::Handle<edm::HepMCProduct> theHepMCProduct;
49-
bool found = iEvent.getByToken(newToken_, theHepMCProduct);
50-
if (!found) {
85+
bool found = false;
86+
if (not newToken_.isUninitialized()) {
87+
found = iEvent.getByToken(newToken_, theHepMCProduct);
88+
}
89+
if (!found and not oldToken_.isUninitialized()) {
5190
found = iEvent.getByToken(oldToken_, theHepMCProduct);
5291
}
5392
if (found) {
5493
std::unique_ptr<edm::HepMCProduct> theCopy(new edm::HepMCProduct(*theHepMCProduct));
5594
iEvent.put(std::move(theCopy));
5695
}
5796

58-
edm::Handle<edm::HepMC3Product> theHepMC3Product;
59-
found = iEvent.getByToken(Token3_, theHepMC3Product);
60-
if (found) {
61-
std::unique_ptr<edm::HepMC3Product> theCopy3(new edm::HepMC3Product(*theHepMC3Product));
62-
iEvent.put(std::move(theCopy3));
97+
if (not Token3_.isUninitialized()) {
98+
edm::Handle<edm::HepMC3Product> theHepMC3Product;
99+
found = iEvent.getByToken(Token3_, theHepMC3Product);
100+
if (found) {
101+
std::unique_ptr<edm::HepMC3Product> theCopy3(new edm::HepMC3Product(*theHepMC3Product));
102+
iEvent.put(std::move(theCopy3));
103+
}
63104
}
64105
}
65106

0 commit comments

Comments
 (0)