From 5171c87d7010438c08c397140ed0ff241d388200 Mon Sep 17 00:00:00 2001 From: Luca Ferragina Date: Wed, 15 Apr 2026 16:40:29 +0200 Subject: [PATCH] Fix handling of empty inputs or invalid handles --- .../plugins/TrackingAssocValueMapsProducer.cc | 34 ++++++++++++------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/PhysicsTools/NanoAOD/plugins/TrackingAssocValueMapsProducer.cc b/PhysicsTools/NanoAOD/plugins/TrackingAssocValueMapsProducer.cc index 27009fb88dca3..5cb81de4d261d 100644 --- a/PhysicsTools/NanoAOD/plugins/TrackingAssocValueMapsProducer.cc +++ b/PhysicsTools/NanoAOD/plugins/TrackingAssocValueMapsProducer.cc @@ -120,6 +120,8 @@ TrackingAssocValueMapsProducer::TrackingAssocValueMapsProducer(const edm::Parame } void TrackingAssocValueMapsProducer::produce(edm::Event& iEvent, const edm::EventSetup&) { + const std::string metname = "PhysicsTools/NanoAOD/TrackingAssocValueMapsProducer"; + edm::Handle> tracksH; iEvent.getByToken(tracksToken_, tracksH); @@ -145,19 +147,10 @@ void TrackingAssocValueMapsProducer::produce(edm::Event& iEvent, const edm::Even const size_t nTracks = tracksH->size(); - if (nTracks == 0 || !inputsValid) { - // No tracks or invalid handles, put empty ValueMaps and return - iEvent.put(std::make_unique>(), "matched"); - iEvent.put(std::make_unique>(), "duplicate"); - iEvent.put(std::make_unique>(), "tpPdgId"); - iEvent.put(std::make_unique>(), "tpCharge"); - if (storeTPKinematics_) { - iEvent.put(std::make_unique>(), "tpPt"); - iEvent.put(std::make_unique>(), "tpEta"); - iEvent.put(std::make_unique>(), "tpPhi"); - } - return; - } + LogDebug(metname) << "Retrieved " << nTracks << " tracks and " << tpH->size() + << " tracking particles. Track handle valid: " << tracksH.isValid() + << ", TP handle valid: " << tpH.isValid() << ", associator handle valid: " + << (useMuonAssociators_ ? recoToSimH.isValid() && simToRecoH.isValid() : associatorH.isValid()); std::vector matched(nTracks, 0); std::vector duplicate(nTracks, 0); @@ -171,6 +164,21 @@ void TrackingAssocValueMapsProducer::produce(edm::Event& iEvent, const edm::Even tpPhi.assign(nTracks, -10.f); } + if (nTracks == 0 || !inputsValid) { + LogDebug(metname) << "No tracks or invalid inputs, producing empty ValueMaps."; + // No tracks or invalid handles, put ValueMaps with default values and return + fillAndPut(iEvent, tracksH, std::make_unique>(), matched, "matched"); + fillAndPut(iEvent, tracksH, std::make_unique>(), duplicate, "duplicate"); + fillAndPut(iEvent, tracksH, std::make_unique>(), tpPdgId, "tpPdgId"); + fillAndPut(iEvent, tracksH, std::make_unique>(), tpCharge, "tpCharge"); + if (storeTPKinematics_) { + fillAndPut(iEvent, tracksH, std::make_unique>(), tpPt, "tpPt"); + fillAndPut(iEvent, tracksH, std::make_unique>(), tpEta, "tpEta"); + fillAndPut(iEvent, tracksH, std::make_unique>(), tpPhi, "tpPhi"); + } + return; + } + edm::RefToBaseVector trackRefs; for (edm::View::size_type i = 0; i < nTracks; ++i) { trackRefs.push_back(tracksH->refAt(i));