Skip to content

Commit

Permalink
Merge pull request #32270 from makortel/throwEsConsumesOutsideConstru…
Browse files Browse the repository at this point in the history
…ctor

Throw an exception if esConsumes() is called outside EDModule constructors
  • Loading branch information
cmsbuild authored Nov 25, 2020
2 parents dd492f7 + 4dd952a commit aacba62
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
3 changes: 3 additions & 0 deletions FWCore/Framework/interface/EDConsumerBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,9 @@ namespace edm {
void throwBranchMismatch(BranchType, EDGetToken) const;
void throwBadToken(edm::TypeID const& iType, EDGetToken iToken) const;
void throwConsumesCallAfterFrozen(TypeToGet const&, InputTag const&) const;
void throwESConsumesCallAfterFrozen(eventsetup::EventSetupRecordKey const&,
eventsetup::heterocontainer::HCTypeTag const&,
edm::ESInputTag const&) const;
void throwESConsumesInProcessBlock() const;

edm::InputTag const& checkIfEmpty(edm::InputTag const& tag);
Expand Down
17 changes: 17 additions & 0 deletions FWCore/Framework/src/EDConsumerBase.cc
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,10 @@ void EDConsumerBase::updateLookup(BranchType iBranchType,
}

void EDConsumerBase::updateLookup(eventsetup::ESRecordsToProxyIndices const& iPI) {
// temporarily unfreeze to allow late EventSetup consumes registration
frozen_ = false;
registerLateConsumes(iPI);
frozen_ = true;

unsigned int index = 0;
for (auto it = m_esTokenInfo.begin<kESLookupInfo>(); it != m_esTokenInfo.end<kESLookupInfo>(); ++it, ++index) {
Expand Down Expand Up @@ -210,6 +213,10 @@ ESTokenIndex EDConsumerBase::recordESConsumes(Transition iTrans,
eventsetup::EventSetupRecordKey const& iRecord,
eventsetup::heterocontainer::HCTypeTag const& iDataType,
edm::ESInputTag const& iTag) {
if (frozen_) {
throwESConsumesCallAfterFrozen(iRecord, iDataType, iTag);
}

//m_tokenLabels first entry is a null. Since most ES data requests have
// empty labels we will assume we can reuse the first entry
unsigned int startOfComponentName = 0;
Expand Down Expand Up @@ -392,6 +399,16 @@ void EDConsumerBase::throwConsumesCallAfterFrozen(TypeToGet const& typeToGet, In
<< "and " << inputTag << "\n";
}

void EDConsumerBase::throwESConsumesCallAfterFrozen(eventsetup::EventSetupRecordKey const& iRecord,
eventsetup::heterocontainer::HCTypeTag const& iDataType,
edm::ESInputTag const& iTag) const {
throw cms::Exception("LogicError") << "A module declared it consumes an EventSetup product after its constructor.\n"
<< "This must be done in the contructor\n"
<< "The product type was: " << iDataType.name() << " in record "
<< iRecord.type().name() << "\n"
<< "and ESInputTag was " << iTag << "\n";
}

void EDConsumerBase::throwESConsumesInProcessBlock() const {
throw cms::Exception("LogicError")
<< "A module declared it consumes an EventSetup product during a ProcessBlock transition.\n"
Expand Down
16 changes: 16 additions & 0 deletions FWCore/Integration/test/ESTestAnalyzers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,21 @@ namespace edmtest {
<< ": ED value " << intData.value << ": ES value = " << dataJ.value();
}

class ESTestAnalyzerIncorrectConsumes : public edm::stream::EDAnalyzer<> {
public:
explicit ESTestAnalyzerIncorrectConsumes(edm::ParameterSet const& iConfig){};
void analyze(const edm::Event&, const edm::EventSetup&) override;

private:
edm::ESGetToken<ESTestDataJ, ESTestRecordJ> esToken_;
};

void ESTestAnalyzerIncorrectConsumes::analyze(edm::Event const& ev, edm::EventSetup const& es) {
esToken_ = esConsumes();
edm::LogAbsolute("ESTestAnalyzerIncorrectConsumes")
<< "Succeeded to call esConsumes() in analyze(), should not happen!";
}

} // namespace edmtest
using namespace edmtest;
DEFINE_FWK_MODULE(ESTestAnalyzerA);
Expand All @@ -240,3 +255,4 @@ DEFINE_FWK_MODULE(ESTestAnalyzerK);
DEFINE_FWK_MODULE(ESTestAnalyzerAZ);
DEFINE_FWK_MODULE(ESTestAnalyzerJ);
DEFINE_FWK_MODULE(ESTestAnalyzerL);
DEFINE_FWK_MODULE(ESTestAnalyzerIncorrectConsumes);
3 changes: 3 additions & 0 deletions FWCore/Integration/test/eventSetupTest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,7 @@ cmsRun --parameter-set ${LOCAL_TEST_DIR}/testConcurrentIOVsESSource_cfg.py || di
echo EventSetupTestCurrentProcess_cfg.py
cmsRun ${LOCAL_TEST_DIR}/EventSetupTestCurrentProcess_cfg.py || die 'Failed in EventSetupTestCurrentProcess_cfg.py' $?

echo EventSetupIncorrectConsumes_cfg.py
cmsRun ${LOCAL_TEST_DIR}/EventSetupIncorrectConsumes_cfg.py && die 'Failed in EventSetupIncorrectConsumes_cfg.py' 1

popd

0 comments on commit aacba62

Please sign in to comment.