From 10e551d6dcfc78d40a8fa3f79a70e94723cee66f Mon Sep 17 00:00:00 2001
From: Johannes Junggeburth <johannes.josef.junggeburth@cern.ch>
Date: Mon, 17 Feb 2025 12:03:43 +0100
Subject: [PATCH 1/2] Surprising that this tool has been using the evtStore for
 such a long time... jaaa physval

---
 .../src/MuonPhysValMonitoringTool.cxx         | 204 +++++++-----------
 .../src/MuonPhysValMonitoringTool.h           |  71 +++---
 2 files changed, 115 insertions(+), 160 deletions(-)

diff --git a/MuonSpectrometer/MuonValidation/MuonDQA/MuonPhysValMonitoring/src/MuonPhysValMonitoringTool.cxx b/MuonSpectrometer/MuonValidation/MuonDQA/MuonPhysValMonitoring/src/MuonPhysValMonitoringTool.cxx
index 5d6408d149ed..0b2b1568259a 100644
--- a/MuonSpectrometer/MuonValidation/MuonDQA/MuonPhysValMonitoring/src/MuonPhysValMonitoringTool.cxx
+++ b/MuonSpectrometer/MuonValidation/MuonDQA/MuonPhysValMonitoring/src/MuonPhysValMonitoringTool.cxx
@@ -1,5 +1,5 @@
 /*
-  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
+  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
 */
 
 // MuonPhysValMonitoringTool.cxx
@@ -10,24 +10,15 @@
 // PhysVal includes
 #include "MuonPhysValMonitoringTool.h"
 
-
-
-#include "GaudiKernel/IToolSvc.h"
 #include "MuonHistUtils/MuonEnumDefs.h"
 
 #include "xAODBase/IParticleHelpers.h"
 #include "xAODCore/ShallowCopy.h"
-#include "xAODMuon/MuonAuxContainer.h"
-#include "xAODMuon/MuonContainer.h"
-#include "xAODMuon/SlowMuonAuxContainer.h"
-#include "xAODMuon/SlowMuonContainer.h"
-#include "xAODTrigMuon/L2CombinedMuonContainer.h"
-#include "xAODTrigMuon/L2StandAloneMuonContainer.h"
+
+
 #include "xAODTrigger/MuonRoI.h"
-#include "xAODTrigger/MuonRoIContainer.h"
 #include "AthContainers/ConstAccessor.h"
 
-#include "xAODTruth/TruthVertexAuxContainer.h"
 #include "xAODTruth/TruthVertexContainer.h"
 #include "FourMomUtils/xAODP4Helpers.h"
 #include "TruthUtils/HepMCHelpers.h"
@@ -60,14 +51,21 @@ namespace MuonPhysValMonitoring {
                                                      std::numeric_limits<float>::quiet_NaN());
     }
 
-    // Constructors
-    ////////////////
-
-    MuonPhysValMonitoringTool::MuonPhysValMonitoringTool(const std::string& type, const std::string& name, const IInterface* parent) :
-        ManagedMonitorToolBase(type, name, parent),
-        m_counterBits(),
-        m_muonItems(),
-        m_L1Seed() {
+    template <class ContType> StatusCode MuonPhysValMonitoringTool::retrieveContainer(const EventContext& ctx,
+                                                                                      const SG::ReadHandleKey<ContType>& key,
+                                                                                      const ContType* & container) const{
+        container = nullptr;
+        if (key.empty()) {
+            ATH_MSG_DEBUG("No key of type "<<typeid(ContType).name()<<" has been parsed");
+            return StatusCode::FAILURE;
+        }
+        SG::ReadHandle readHandle{key,ctx};
+        if (!readHandle.isPresent()) {
+            ATH_MSG_ERROR("Failed to retrieve "<<key.fullKey()<<". Please check.");
+            return StatusCode::FAILURE;
+        }
+        container = readHandle.cptr();
+        return StatusCode::SUCCESS;
     }
 
     // Athena algtool's Hooks
@@ -76,8 +74,6 @@ namespace MuonPhysValMonitoring {
         ATH_MSG_INFO("Initializing " << name() << "...");
         ATH_CHECK(ManagedMonitorToolBase::initialize());
 
-        if (!m_slowMuonsName.empty()) m_muonsName = m_slowMuonsName;
-
         for (unsigned int i = 0; i < m_selectHLTMuonItems.size(); i++) {
             if (m_selectHLTMuonItems[i][0] == "" || m_selectHLTMuonItems[i][1] == "") continue;
             m_muonItems.emplace_back(m_selectHLTMuonItems[i][0]);
@@ -122,13 +118,13 @@ namespace MuonPhysValMonitoring {
         // no such muons in case of SlowMuon reco
         bool separateSAFMuons = m_slowMuonsName.empty();
         
-        std::string muonContainerName = m_muonsName;
+        std::string muonContainerName = m_muonsName.key();
         for (const auto& category : m_selectMuonCategoriesStr) {
-            std::string categoryPath = m_muonsName + "/" + category + "/";
+            std::string categoryPath = muonContainerName + "/" + category + "/";
 
             m_muonValidationPlots.emplace_back(std::make_unique<MuonValidationPlots>(
                 nullptr, categoryPath, m_selectMuonWPs, m_selectMuonAuthors, m_isData,
-                (category == theMuonCategories.at(ALL) ? false : m_doBinnedResolutionPlots.value()), separateSAFMuons, m_doMuonTree));
+                (category == theMuonCategories.at(ALL) ? false : m_doBinnedResolutionPlots.value()), separateSAFMuons, false));
 
             if (!m_slowMuonsName.empty()) m_slowMuonValidationPlots.emplace_back(std::make_unique<SlowMuonValidationPlots>(nullptr, categoryPath, m_isData));
             if (m_doTrigMuonValidation) {
@@ -221,11 +217,11 @@ namespace MuonPhysValMonitoring {
 
         int nAuth = xAOD::Muon::NumberOfMuonAuthors;
         for (int i = 1; i < 4; i++) {
-            m_h_overview_reco_authors.emplace_back(new TH1F((m_muonsName + "_" + theMuonCategories.at(i) + "_reco_authors").c_str(),
+            m_h_overview_reco_authors.emplace_back(new TH1F((muonContainerName + "_" + theMuonCategories.at(i) + "_reco_authors").c_str(),
                                                          (muonContainerName + "_" + theMuonCategories.at(i) + "_reco_authors").c_str(),
                                                          nAuth + 1, -0.5, nAuth + 0.5));
         }
-        m_h_overview_reco_authors.emplace_back(new TH1F((m_muonsName + "_Overview_Other_reco_authors").c_str(),
+        m_h_overview_reco_authors.emplace_back(new TH1F((muonContainerName + "_Overview_Other_reco_authors").c_str(),
                                                      (muonContainerName + "_Other_reco_authors").c_str(), nAuth + 1, -0.5, nAuth + 0.5));
 
         for (const auto& hist : m_h_overview_reco_authors) {
@@ -288,40 +284,28 @@ namespace MuonPhysValMonitoring {
         m_vRecoMuons_EffDen_CB.clear();
         m_vRecoMuons_EffDen_MS.clear();
 
-        SG::ReadHandle<xAOD::EventInfo> eventInfoHandle(m_eventInfo);
-        if (!eventInfoHandle.isValid()) {
-            ATH_MSG_WARNING("Could not retrieve EventInfo, returning");
-            return StatusCode::SUCCESS;
-        }
-        const xAOD::EventInfo* eventInfo = eventInfoHandle.cptr();
-        m_isData = !eventInfo->eventType(xAOD::EventInfo::IS_SIMULATION);
-        float beamSpotWeight = eventInfo->beamSpotWeight();
+        const EventContext& ctx{Gaudi::Hive::currentContext()};
+        const xAOD::EventInfo* eventInfo{nullptr};
+        ATH_CHECK(retrieveContainer(ctx, m_eventInfo, eventInfo));
 
-        const xAOD::TruthParticleContainer* TruthMuons(nullptr);
+        float beamSpotWeight = eventInfo->beamSpotWeight();
 
+        const xAOD::TruthParticleContainer* TruthMuons{nullptr};
+        ATH_CHECK(retrieveContainer(ctx, m_muonsTruthName, TruthMuons));
+        
         if (!m_isData) {
-            TruthMuons = evtStore()->tryConstRetrieve<xAOD::TruthParticleContainer>(m_muonsTruthName);
-            if (!TruthMuons) {
-                ATH_MSG_ERROR("Couldn't retrieve TruthMuons container with key: " << m_muonsTruthName);
-                return StatusCode::FAILURE;
-            }
-            ATH_MSG_DEBUG("Retrieved truth muons " << TruthMuons->size());
             m_h_overview_nObjects[0]->Fill(TruthMuons->size(), beamSpotWeight);
         }
 
         const xAOD::MuonContainer* Muons = nullptr;
-        const xAOD::SlowMuonContainer* SlowMuons = nullptr;
-        if (!m_slowMuonsName.empty()) {
-            SlowMuons = getContainer<xAOD::SlowMuonContainer>(m_slowMuonsName);
-            if (!SlowMuons) {
-                ATH_MSG_WARNING("Couldn't retrieve SlowMuons container with key: " << m_slowMuonsName);
-                return StatusCode::SUCCESS;
-            }
+        const xAOD::SlowMuonContainer* SlowMuons{nullptr};
+        ATH_CHECK(retrieveContainer(ctx, m_slowMuonsName, SlowMuons));
+        ATH_CHECK(retrieveContainer(ctx, m_muonsName, Muons));
+        if (SlowMuons) {           
             ATH_MSG_DEBUG("Retrieved slow muons " << SlowMuons->size());
             m_h_overview_nObjects[1]->Fill(SlowMuons->size(), beamSpotWeight);
-        } else {
-            Muons = getContainer<xAOD::MuonContainer>(m_muonsName);
-            if (!Muons) { return StatusCode::SUCCESS; }
+        } 
+        if (Muons) {
             ATH_MSG_DEBUG("Retrieved muons " << Muons->size());
             m_h_overview_nObjects[1]->Fill(Muons->size(), beamSpotWeight);
         }
@@ -329,16 +313,7 @@ namespace MuonPhysValMonitoring {
         /////////////////////////////////////////////////////////////////////// @@@
         // @@@ Temp hack to get the MuonSpectrometerTrackParticle (@MS Entry, not extrapolated), needed for eloss plots
         // Remove when the link to the real MuonSpectrometerTrackParticle appears in the xAOD muon
-        if (evtStore()->contains<xAOD::TrackParticleContainer>("MuonSpectrometerTrackParticles")) {
-            m_MSTracks = getContainer<xAOD::TrackParticleContainer>("MuonSpectrometerTrackParticles");
-            if (!m_MSTracks) {
-                ATH_MSG_WARNING("Couldn't retrieve MS Tracks container");
-                return StatusCode::SUCCESS;
-            } else
-                ATH_MSG_DEBUG("Retrieved muon tracks " << m_MSTracks->size());
-        } else
-            ATH_MSG_DEBUG("Couldn't find MS Tracks container");
-
+        ATH_CHECK(retrieveContainer(ctx, m_muonTracksName, m_MSTracks));
         /////////////////////////////////////////////////////////////////////// @@@
 
         // Do resonance selection
@@ -418,56 +393,52 @@ namespace MuonPhysValMonitoring {
             for (const auto mu : *Muons) handleMuon(mu, nullptr, beamSpotWeight);
         }
 
-        if (m_doMuonTree) { handleMuonTrees(eventInfo, m_isData); }
-
-        if (!m_tracksName.empty()) {
-            auto IDTracks = getContainer<xAOD::TrackParticleContainer>(m_tracksName);
-            if (!IDTracks) return StatusCode::FAILURE;
+        const xAOD::TrackParticleContainer* IDTracks{nullptr};
+        ATH_CHECK(retrieveContainer(ctx, m_tracksName, IDTracks));
+        if (IDTracks) {
             ATH_MSG_DEBUG("handling " << IDTracks->size() << " " << m_tracksName);
             for (const auto tp : *IDTracks) handleMuonTrack(tp, xAOD::Muon::InnerDetectorTrackParticle, beamSpotWeight);
         }
-        if (!m_fwdtracksName.empty()) {
-            auto FwdIDTracks = getContainer<xAOD::TrackParticleContainer>(m_fwdtracksName);
-            if (!FwdIDTracks) return StatusCode::FAILURE;
+        const xAOD::TrackParticleContainer* FwdIDTracks{nullptr};
+        ATH_CHECK(retrieveContainer(ctx, m_fwdtracksName, IDTracks));
+        if (FwdIDTracks) {            
             ATH_MSG_DEBUG("handling " << FwdIDTracks->size() << " " << m_fwdtracksName);
             for (const auto tp : *FwdIDTracks) handleMuonTrack(tp, xAOD::Muon::InnerDetectorTrackParticle, beamSpotWeight);
         }
-        if (!m_muonTracksName.empty()) {
-            auto MuonTracks = getContainer<xAOD::TrackParticleContainer>(m_muonTracksName);
-            if (!MuonTracks) return StatusCode::FAILURE;
+        const xAOD::TrackParticleContainer*  MuonTracks{nullptr};
+        ATH_CHECK(retrieveContainer(ctx, m_muonTracksName, MuonTracks));
+        if (MuonTracks) {           
             ATH_MSG_DEBUG("handling " << MuonTracks->size() << " " << m_muonTracksName);
             m_h_overview_nObjects[2]->Fill(MuonTracks->size(), beamSpotWeight);
             for (const auto tp : *MuonTracks) handleMuonTrack(tp, xAOD::Muon::MuonSpectrometerTrackParticle, beamSpotWeight);
         }
-        if (!m_muonExtrapolatedTracksName.empty()) {
-            auto MuonExtrapolatedTracks = getContainer<xAOD::TrackParticleContainer>(m_muonExtrapolatedTracksName);
-            if (!MuonExtrapolatedTracks) return StatusCode::FAILURE;
+        const xAOD::TrackParticleContainer* MuonExtrapolatedTracks{nullptr};
+        ATH_CHECK(retrieveContainer(ctx, m_muonExtrapolatedTracksName, MuonExtrapolatedTracks));
+        if (MuonExtrapolatedTracks) {
             ATH_MSG_DEBUG("handling " << MuonExtrapolatedTracks->size() << " " << m_muonExtrapolatedTracksName);
             for (const auto tp : *MuonExtrapolatedTracks)
                 handleMuonTrack(tp, xAOD::Muon::ExtrapolatedMuonSpectrometerTrackParticle, beamSpotWeight);
         }
+        const xAOD::TrackParticleContainer* MSOnlyMuonExtrapolatedTracks{nullptr};
+        ATH_CHECK(retrieveContainer(ctx, m_muonMSOnlyExtrapolatedTracksName, MSOnlyMuonExtrapolatedTracks));
 
-        if (!m_muonMSOnlyExtrapolatedTracksName.empty() &&
-            evtStore()->contains<xAOD::TrackParticleContainer>(m_muonMSOnlyExtrapolatedTracksName)) {
-            auto MSOnlyMuonExtrapolatedTracks = getContainer<xAOD::TrackParticleContainer>(m_muonMSOnlyExtrapolatedTracksName);
-            if (!MSOnlyMuonExtrapolatedTracks) return StatusCode::FAILURE;
+        if (MSOnlyMuonExtrapolatedTracks) {
             ATH_MSG_DEBUG("handling " << MSOnlyMuonExtrapolatedTracks->size() << " " << m_muonMSOnlyExtrapolatedTracksName);
             for (const auto tp : *MSOnlyMuonExtrapolatedTracks)
                 handleMuonTrack(tp, xAOD::Muon::MSOnlyExtrapolatedMuonSpectrometerTrackParticle, beamSpotWeight);
         }
 
-        if (!m_muonSegmentsName.empty()) {
-            const xAOD::MuonSegmentContainer* TruthMuonSegments(nullptr);
-            if (!m_isData) {
-                TruthMuonSegments = getContainer<xAOD::MuonSegmentContainer>(m_muonSegmentsTruthName);
-                if (!TruthMuonSegments) { return StatusCode::SUCCESS; }
-                m_h_overview_nObjects[3]->Fill(TruthMuonSegments->size(), beamSpotWeight);
-                ATH_MSG_DEBUG("handling " << TruthMuonSegments->size() << " " << m_muonSegmentsTruthName);
-                for (const auto truthMuSeg : *TruthMuonSegments) handleTruthMuonSegment(truthMuSeg, TruthMuons, beamSpotWeight);
-            }
+        const xAOD::MuonSegmentContainer* TruthMuonSegments{nullptr}; 
+        ATH_CHECK(retrieveContainer(ctx, m_muonSegmentsTruthName, TruthMuonSegments));
+        if (TruthMuonSegments) {
+            m_h_overview_nObjects[3]->Fill(TruthMuonSegments->size(), beamSpotWeight);
+            ATH_MSG_DEBUG("handling " << TruthMuonSegments->size() << " " << m_muonSegmentsTruthName);
+            for (const auto truthMuSeg : *TruthMuonSegments) handleTruthMuonSegment(truthMuSeg, TruthMuons, beamSpotWeight);
+        }
 
-            const xAOD::MuonSegmentContainer* MuonSegments = getContainer<xAOD::MuonSegmentContainer>(m_muonSegmentsName);
-            if (!MuonSegments) { return StatusCode::SUCCESS; }
+        const xAOD::MuonSegmentContainer* MuonSegments{nullptr};
+        ATH_CHECK(retrieveContainer(ctx, m_muonSegmentsName, MuonSegments));
+        if (MuonSegments) {
             m_h_overview_nObjects[4]->Fill(MuonSegments->size(), beamSpotWeight);
             ATH_MSG_DEBUG("handling " << MuonSegments->size() << " " << m_muonSegmentsName);
             for (const auto muSeg : *MuonSegments) handleMuonSegment(muSeg, beamSpotWeight);
@@ -503,8 +474,8 @@ namespace MuonPhysValMonitoring {
 
             //@@@@@ L1 @@@@@
             if (m_doTrigMuonL1Validation) {
-                const xAOD::MuonRoIContainer* L1TrigMuons = getContainer<xAOD::MuonRoIContainer>(m_muonL1TrigName);
-                if (!L1TrigMuons) { return StatusCode::SUCCESS; }
+                const xAOD::MuonRoIContainer* L1TrigMuons{nullptr};
+                ATH_CHECK(retrieveContainer(ctx, m_muonL1TrigName, L1TrigMuons));
                 ATH_MSG_DEBUG("Retrieved L1 triggered muons " << L1TrigMuons->size());
                 for (const auto TrigL1mu : *L1TrigMuons) handleMuonL1Trigger(TrigL1mu);
             }
@@ -512,8 +483,8 @@ namespace MuonPhysValMonitoring {
             //@@@@@ L2 @@@@@
             if (m_doTrigMuonL2Validation) {
                 //@@@@@ L2SA @@@@@
-                const xAOD::L2StandAloneMuonContainer* L2SAMuons = getContainer<xAOD::L2StandAloneMuonContainer>(m_muonL2SAName);
-                if (!L2SAMuons) { return StatusCode::SUCCESS; }
+                const xAOD::L2StandAloneMuonContainer* L2SAMuons{nullptr};
+                ATH_CHECK(retrieveContainer(ctx, m_muonL2SAName, L2SAMuons));
                 ATH_MSG_DEBUG("Retrieved L2 StandAlone triggered muons " << L2SAMuons->size());
                 if (L2SAMuons->size() != 0) {
                     for (const auto L2SAmu : *L2SAMuons) {
@@ -572,8 +543,9 @@ namespace MuonPhysValMonitoring {
                 }
 
                 //@@@@@ L2CB @@@@@
-                const xAOD::L2CombinedMuonContainer* L2CBMuons = getContainer<xAOD::L2CombinedMuonContainer>(m_muonL2CBName);
-                if (!L2CBMuons) { return StatusCode::SUCCESS; }
+                
+                const xAOD::L2CombinedMuonContainer* L2CBMuons{nullptr};
+                ATH_CHECK(retrieveContainer(ctx, m_muonL2CBName, L2CBMuons));
                 ATH_MSG_DEBUG("Retrieved L2 Combined triggered muons " << L2CBMuons->size());
                 if (L2CBMuons->size() != 0) {
                     for (const auto L2CBmu : *L2CBMuons) {
@@ -633,9 +605,10 @@ namespace MuonPhysValMonitoring {
 
             //@@@@@ EF @@@@@
             if (m_doTrigMuonEFValidation) {
-                const xAOD::MuonContainer* EFCombTrigMuons = getContainer<xAOD::MuonContainer>(m_muonEFCombTrigName);
-                const xAOD::MuonRoIContainer* L1TrigMuons = getContainer<xAOD::MuonRoIContainer>(m_muonL1TrigName);
-                if (!EFCombTrigMuons) { return StatusCode::SUCCESS; }
+                const xAOD::MuonContainer* EFCombTrigMuons{nullptr};
+                const xAOD::MuonRoIContainer* L1TrigMuons{nullptr};
+                ATH_CHECK(retrieveContainer(ctx, m_muonEFCombTrigName, EFCombTrigMuons));
+                ATH_CHECK(retrieveContainer(ctx, m_muonL1TrigName, L1TrigMuons));
                 ATH_MSG_DEBUG("Retrieved EF triggered muons " << EFCombTrigMuons->size());
                 if (EFCombTrigMuons->size() != 0) {
                     for (const auto Trigmu : *EFCombTrigMuons) {
@@ -1004,20 +977,6 @@ namespace MuonPhysValMonitoring {
             m_oUnmatchedTruthMuonPlots->fill(*truthMu, weight);
     }
 
-    // This method MUST be called after all muon object (reco, truth) related variables (usually, vectors or arrays of vectors) of branches
-    // of tree are already initialized. The method fills the event related branches (basic types like int, float, double, etc), fills the
-    // TTree object and at the end resets all branch variables for the next event.
-    void MuonPhysValMonitoringTool::handleMuonTrees(const xAOD::EventInfo* eventInfo, bool isData) {
-        ATH_MSG_DEBUG("Filling MuonTree " << name() << "...");
-
-        for (unsigned int i = 0; i < m_selectMuonCategories.size(); i++) {
-            m_muonValidationPlots[i]->fillTree(eventInfo, isData);
-
-            ATH_MSG_DEBUG("MuonTree is filled for muon category = " << m_selectMuonCategories[i] << " with event # "
-                                                                    << eventInfo->eventNumber());
-        }
-    }
-
     void MuonPhysValMonitoringTool::handleMuonTrack(const xAOD::TrackParticle* tp, xAOD::Muon::TrackParticleType type, float weight) {
         if (!tp) {
             ATH_MSG_WARNING("No track particle found");
@@ -1305,10 +1264,9 @@ namespace MuonPhysValMonitoring {
     }
 
     const xAOD::SlowMuon* MuonPhysValMonitoringTool::findRecoSlowMuon(const xAOD::TruthParticle* truthMu) {
-        if (m_slowMuonsName.empty()) return nullptr;
-
         const xAOD::SlowMuonContainer* SlowMuons = nullptr;
-        SlowMuons = getContainer<xAOD::SlowMuonContainer>(m_slowMuonsName);
+        retrieveContainer(Gaudi::Hive::currentContext() , m_slowMuonsName, SlowMuons).ignore();
+        if (!SlowMuons) return nullptr;
         for (const auto smu : *SlowMuons) {
             const MuonLink muLink = smu->muonLink();
             if (!muLink.isValid()) continue;
@@ -1350,12 +1308,6 @@ namespace MuonPhysValMonitoring {
         if (!m_isData)
             if (m_oUnmatchedRecoMuonSegmentPlots) m_oUnmatchedRecoMuonSegmentPlots->finalize();
 
-        if (m_doMuonTree) {
-            for (const auto& plots : m_muonValidationPlots) {
-                if (plots->getMuonTree()) { plots->getMuonTree()->getTree()->Write(); }
-            }
-        }
-
         return StatusCode::SUCCESS;
     }//procHistograms*/
 
@@ -1445,7 +1397,7 @@ namespace MuonPhysValMonitoring {
     void MuonPhysValMonitoringTool::modifyHistogram(TH1* hist) {
         std::string histname = hist->GetName();
          
-        if(m_muonsName == "Muons"){
+        if(m_muonsName.key() == "Muons"){
             if (histname.find("parameters_z0") != std::string::npos) { 
                 hist->GetXaxis()->Set(80, -200., 200.);
             }
@@ -1453,9 +1405,7 @@ namespace MuonPhysValMonitoring {
                 hist->GetXaxis()->Set(80, -1., 1.);
                 hist->GetYaxis()->SetTitle("Entries / 0.025 mm");                
             }
-       }
-
-       if(m_muonsName == "MuonsLRT"){
+       } else if(m_muonsName.key() == "MuonsLRT"){
             if (histname.find("parameters_d0") != std::string::npos && histname.find("parameters_d0_small") == std::string::npos) {
                 hist->Rebin(100);
                 hist->GetYaxis()->SetTitle("Entries / 2.5 mm");                
diff --git a/MuonSpectrometer/MuonValidation/MuonDQA/MuonPhysValMonitoring/src/MuonPhysValMonitoringTool.h b/MuonSpectrometer/MuonValidation/MuonDQA/MuonPhysValMonitoring/src/MuonPhysValMonitoringTool.h
index 1efd32694361..d4940e5077c5 100644
--- a/MuonSpectrometer/MuonValidation/MuonDQA/MuonPhysValMonitoring/src/MuonPhysValMonitoringTool.h
+++ b/MuonSpectrometer/MuonValidation/MuonDQA/MuonPhysValMonitoring/src/MuonPhysValMonitoringTool.h
@@ -1,5 +1,5 @@
 /*
-  Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
+  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
 */
 
 // MuonPhysValMonitoringTool.cxx
@@ -24,13 +24,15 @@
 
 #include "TrkToolInterfaces/ITrackSelectorTool.h"
 #include "xAODEventInfo/EventInfo.h"
-#include "xAODMuon/Muon.h" //typedef
-#include "xAODMuon/MuonSegment.h" //typedef
-#include "xAODMuon/SlowMuon.h" //typedef
-#include "xAODTrigMuon/L2CombinedMuon.h" //typedef
 
-#include "xAODTrigMuon/L2StandAloneMuon.h" //typedef
-#include "xAODTruth/TruthParticle.h" //typedef
+#include "xAODTrigger/MuonRoIContainer.h"
+#include "xAODMuon/MuonContainer.h"
+#include "xAODMuon/MuonSegmentContainer.h"
+#include "xAODMuon/SlowMuonContainer.h"
+#include "xAODTrigMuon/L2CombinedMuonContainer.h"
+#include "xAODTrigMuon/L2StandAloneMuonContainer.h"
+
+
 #include "xAODTruth/TruthParticleContainer.h" //typedef
 
 #include "MuonSegmentValidationPlots.h" //needed for unique_ptr  access to deleter
@@ -62,7 +64,7 @@ namespace MuonPhysValMonitoring {
         // Copy constructor:
 
         /// Constructor with parameters:
-        MuonPhysValMonitoringTool(const std::string& type, const std::string& name, const IInterface* parent);
+        using ManagedMonitorToolBase::ManagedMonitorToolBase;
 
         /// Destructor:
         virtual ~MuonPhysValMonitoringTool() = default;
@@ -87,8 +89,6 @@ namespace MuonPhysValMonitoring {
         void handleTruthMuonSegment(const xAOD::MuonSegment* truthMuSeg, const xAOD::TruthParticleContainer* muonTruthContainer,
                                     float weight = 1.0);
 
-        void handleMuonTrees(const xAOD::EventInfo* eventInfo, bool isData);
-
         void handleMuonL1Trigger(const xAOD::MuonRoI* TrigL1mu);
         void handleMuonL2Trigger(const xAOD::L2StandAloneMuon* L2SAMu);
         void handleMuonL2Trigger(const xAOD::L2CombinedMuon* L2CBMu);
@@ -115,23 +115,31 @@ namespace MuonPhysValMonitoring {
         TH1F* findHistogram(const std::vector<HistData>& hists, const std::string& hnameTag, const std::string& hdirTag,
                             const std::string& hNewName);
         void modifyHistogram(TH1* hist);
-
-        Gaudi::Property<std::string> m_tracksName{this, "TrackContainerName", "InDetTrackParticles"};
-        Gaudi::Property<std::string> m_fwdtracksName{this, "FwdTrackContainerName", ""};
-        Gaudi::Property<std::string> m_muonsName{this, "MuonContainerName", "Muons"};
-        Gaudi::Property<std::string> m_slowMuonsName{this, "SlowMuonContainerName", "SlowMuons"};
-        Gaudi::Property<std::string> m_muonsTruthName{this, "MuonTruthParticleContainerName", "MuonTruthParticles"};
-        Gaudi::Property<std::string> m_muonTracksName{this, "MuonTrackContainerName", "MuonSpectrometerTrackParticles"};
-        Gaudi::Property<std::string> m_muonExtrapolatedTracksName{this, "MuonExtrapolatedTrackContainerName",
-                                                                  "ExtrapolatedMuonTrackParticles"};
-        Gaudi::Property<std::string> m_muonMSOnlyExtrapolatedTracksName{this, "MuonOnlyExtrapolatedTrackContainerName",
+        using TrackContKey_t = SG::ReadHandleKey<xAOD::TrackParticleContainer>;
+        using MuonContKey_t = SG::ReadHandleKey<xAOD::MuonContainer>;
+        using SegmentContKey_t = SG::ReadHandleKey<xAOD::MuonSegmentContainer>;
+        using MuonROIContKey_t = SG::ReadHandleKey<xAOD::MuonRoIContainer>;
+        using L2SAContKey_t = SG::ReadHandleKey<xAOD::L2StandAloneMuonContainer>;
+        using L2CBContKey_t = SG::ReadHandleKey<xAOD::L2CombinedMuonContainer>;
+        using SlowContKey_t = SG::ReadHandleKey<xAOD::SlowMuonContainer>;
+        using TruthPartKey_t = SG::ReadHandleKey<xAOD::TruthParticleContainer>;
+        
+        TrackContKey_t m_tracksName{this, "TrackContainerName", "InDetTrackParticles"};
+        TrackContKey_t m_fwdtracksName{this, "FwdTrackContainerName", ""};
+        MuonContKey_t m_muonsName{this, "MuonContainerName", "Muons"};
+        SlowContKey_t m_slowMuonsName{this, "SlowMuonContainerName", "SlowMuons"};
+        TruthPartKey_t m_muonsTruthName{this, "MuonTruthParticleContainerName", "MuonTruthParticles"};
+        TrackContKey_t m_muonTracksName{this, "MuonTrackContainerName", "MuonSpectrometerTrackParticles"};
+        TrackContKey_t m_muonExtrapolatedTracksName{this, "MuonExtrapolatedTrackContainerName", "ExtrapolatedMuonTrackParticles"};
+        TrackContKey_t m_muonMSOnlyExtrapolatedTracksName{this, "MuonOnlyExtrapolatedTrackContainerName",
                                                                         "MSOnlyExtrapolatedMuonTrackParticles"};
-        Gaudi::Property<std::string> m_muonSegmentsName{this, "MuonSegmentContainerName", "MuonSegments"};
-        Gaudi::Property<std::string> m_muonSegmentsTruthName{this, "MuonTruthSegmentContainerName", "MuonTruthSegments"};
-        Gaudi::Property<std::string> m_muonL1TrigName{this, "L1TrigMuonContainerName", "LVL1MuonRoIs"};
-        Gaudi::Property<std::string> m_muonL2SAName{this, "L2SAMuonContainerName", "HLT_xAOD__L2StandAloneMuonContainer_MuonL2SAInfo"};
-        Gaudi::Property<std::string> m_muonL2CBName{this, "L2CBMuonContainerName", "HLT_xAOD__L2CombinedMuonContainer_MuonL2CBInfo"};
-        Gaudi::Property<std::string> m_muonEFCombTrigName{this, "EFCombTrigMuonContainerName", "HLT_xAOD__MuonContainer_MuonEFInfo"};
+        
+        SegmentContKey_t m_muonSegmentsName{this, "MuonSegmentContainerName", "MuonSegments"};
+        SegmentContKey_t m_muonSegmentsTruthName{this, "MuonTruthSegmentContainerName", "MuonTruthSegments"};
+        MuonROIContKey_t m_muonL1TrigName{this, "L1TrigMuonContainerName", "LVL1MuonRoIs"};
+        L2SAContKey_t m_muonL2SAName{this, "L2SAMuonContainerName", "HLT_xAOD__L2StandAloneMuonContainer_MuonL2SAInfo"};
+        L2CBContKey_t m_muonL2CBName{this, "L2CBMuonContainerName", "HLT_xAOD__L2CombinedMuonContainer_MuonL2CBInfo"};
+        MuonContKey_t m_muonEFCombTrigName{this, "EFCombTrigMuonContainerName", "HLT_xAOD__MuonContainer_MuonEFInfo"};
 
         Gaudi::Property<std::vector<int>> m_selectMuonWPs{
             this, "SelectMuonWorkingPoints", {xAOD::Muon::Loose, xAOD::Muon::Medium, xAOD::Muon::Tight}};
@@ -153,7 +161,6 @@ namespace MuonPhysValMonitoring {
         Gaudi::Property<bool> m_doTrigMuonL1Validation{this, "DoTrigMuonL1Validation", false};
         Gaudi::Property<bool> m_doTrigMuonL2Validation{this, "DoTrigMuonL2Validation", false};
         Gaudi::Property<bool> m_doTrigMuonEFValidation{this, "DoTrigMuonEFValidation", false};
-        Gaudi::Property<bool> m_doMuonTree{this, "DoMuonTree", false};
         Gaudi::Property<bool> m_isData{this, "IsData", false};
 
         SG::ReadHandleKey<xAOD::EventInfo> m_eventInfo{this, "EventInfo", "EventInfo", "event info"};
@@ -217,14 +224,12 @@ namespace MuonPhysValMonitoring {
         std::vector<const xAOD::Muon*> m_vRecoMuons_EffDen_MS;
         std::vector<const xAOD::Muon*> m_vRecoMuons_EffDen;
 
-        template <class T> const T* getContainer(const std::string& containerName);
+        template <class ContType> StatusCode retrieveContainer(const EventContext& ctx,
+                                                          const SG::ReadHandleKey<ContType>& key,
+                                                          const ContType* & container) const;
     };
 
-    template <class T> const T* MuonPhysValMonitoringTool::getContainer(const std::string& containerName) {
-        const T* ptr = evtStore()->retrieve<const T>(containerName);
-        if (!ptr) { ATH_MSG_WARNING("Container '" << containerName << "' could not be retrieved"); }
-        return ptr;
-    }
+    
 
 }  // namespace MuonPhysValMonitoring
 
-- 
GitLab


From c10f41c978115c023a5e2295395569687094c0a9 Mon Sep 17 00:00:00 2001
From: Johannes Junggeburth <johannes.josef.junggeburth@cern.ch>
Date: Mon, 17 Feb 2025 13:56:59 +0100
Subject: [PATCH 2/2] Add decoration dependencies

---
 .../src/MuonPhysValMonitoringTool.cxx         | 59 +++++++++++++++----
 .../src/MuonPhysValMonitoringTool.h           |  9 ++-
 2 files changed, 51 insertions(+), 17 deletions(-)

diff --git a/MuonSpectrometer/MuonValidation/MuonDQA/MuonPhysValMonitoring/src/MuonPhysValMonitoringTool.cxx b/MuonSpectrometer/MuonValidation/MuonDQA/MuonPhysValMonitoring/src/MuonPhysValMonitoringTool.cxx
index 0b2b1568259a..c3ce13051be9 100644
--- a/MuonSpectrometer/MuonValidation/MuonDQA/MuonPhysValMonitoring/src/MuonPhysValMonitoringTool.cxx
+++ b/MuonSpectrometer/MuonValidation/MuonDQA/MuonPhysValMonitoring/src/MuonPhysValMonitoringTool.cxx
@@ -13,7 +13,6 @@
 #include "MuonHistUtils/MuonEnumDefs.h"
 
 #include "xAODBase/IParticleHelpers.h"
-#include "xAODCore/ShallowCopy.h"
 
 
 #include "xAODTrigger/MuonRoI.h"
@@ -24,9 +23,10 @@
 #include "TruthUtils/HepMCHelpers.h"
 
 #include "TString.h"
+
 #include <cmath>
 #include <limits>
-
+#include <format>
 
 namespace{
     using TrackLink = ElementLink<xAOD::TrackParticleContainer>;
@@ -43,14 +43,6 @@ namespace MuonPhysValMonitoring {
     ///////////////////////////////////////////////////////////////////
     // Public methods:
     ///////////////////////////////////////////////////////////////////
-
-    // utilities
-    float getMatchingProbability(const xAOD::TrackParticle& trackParticle) {
-        static const SG::ConstAccessor<float> truthMatchProbabilityAcc("truthMatchProbability");
-        return truthMatchProbabilityAcc.withDefault (trackParticle,
-                                                     std::numeric_limits<float>::quiet_NaN());
-    }
-
     template <class ContType> StatusCode MuonPhysValMonitoringTool::retrieveContainer(const EventContext& ctx,
                                                                                       const SG::ReadHandleKey<ContType>& key,
                                                                                       const ContType* & container) const{
@@ -93,6 +85,46 @@ namespace MuonPhysValMonitoring {
         ATH_CHECK(m_trackSelector.retrieve());
         ATH_CHECK(m_isoTool.retrieve());
 
+        ATH_CHECK(m_tracksName.initialize());
+        ATH_CHECK(m_fwdtracksName.initialize(!m_fwdtracksName.empty()));
+        ATH_CHECK(m_muonsName.initialize());
+        ATH_CHECK(m_slowMuonsName.initialize(!m_slowMuonsName.empty()));
+        
+        ATH_CHECK(m_muonTracksName.initialize());
+        ATH_CHECK(m_muonExtrapolatedTracksName.initialize());
+        ATH_CHECK(m_muonMSOnlyExtrapolatedTracksName.initialize());
+        
+        ATH_CHECK(m_muonSegmentsName.initialize());
+        ATH_CHECK(m_muonsTruthName.initialize(!m_isData));
+        ATH_CHECK(m_muonSegmentsTruthName.initialize(!m_isData));
+        ATH_CHECK(m_muonL1TrigName.initialize(m_doTrigMuonL1Validation));
+        ATH_CHECK(m_muonL2SAName.initialize(m_doTrigMuonL2Validation));
+        ATH_CHECK(m_muonL2CBName.initialize(m_doTrigMuonL2Validation));
+        ATH_CHECK(m_muonEFCombTrigName.initialize(m_doTrigMuonEFValidation));
+        
+        if (!m_isData) {
+            const std::vector<std::string> truthDecors{"truthOrigin", "truthType", "truthParticleLink"};
+            const std::vector<std::string> keys{m_tracksName.key(), m_fwdtracksName.key(), m_muonsName.key(), 
+                                                m_slowMuonsName.key(), m_muonTracksName.key(), m_muonExtrapolatedTracksName.key(),
+                                                m_muonMSOnlyExtrapolatedTracksName.key()};
+            for (const std::string& key : keys) {
+                if (key.empty()) continue;
+                for (const std::string& decor : truthDecors){
+                    m_decorDep.emplace_back(std::format("{:}.{:}", key , decor)); 
+                }
+            }
+            const std::vector<std::string> truthPartDecors{"MuonEntryLayer_px", "nprecLayers",
+                                                            "truthOrigin", "truthType"};
+
+            for (const std::string& decor: truthPartDecors) {
+                m_decorDep.emplace_back(m_muonsTruthName, decor);
+            }
+            
+
+
+        }
+        
+        ATH_CHECK(m_decorDep.initialize());
         return StatusCode::SUCCESS;
     }
 
@@ -389,8 +421,11 @@ namespace MuonPhysValMonitoring {
                 if (!link.isValid()) continue;
                 handleMuon(*link, smu, beamSpotWeight);
             }
-        } else if (Muons) {
-            for (const auto mu : *Muons) handleMuon(mu, nullptr, beamSpotWeight);
+        }  
+        if (Muons) {
+            for (const xAOD::Muon* mu : *Muons) {
+                handleMuon(mu, nullptr, beamSpotWeight);
+            }
         }
 
         const xAOD::TrackParticleContainer* IDTracks{nullptr};
diff --git a/MuonSpectrometer/MuonValidation/MuonDQA/MuonPhysValMonitoring/src/MuonPhysValMonitoringTool.h b/MuonSpectrometer/MuonValidation/MuonDQA/MuonPhysValMonitoring/src/MuonPhysValMonitoringTool.h
index d4940e5077c5..165143e2fe17 100644
--- a/MuonSpectrometer/MuonValidation/MuonDQA/MuonPhysValMonitoring/src/MuonPhysValMonitoringTool.h
+++ b/MuonSpectrometer/MuonValidation/MuonDQA/MuonPhysValMonitoring/src/MuonPhysValMonitoringTool.h
@@ -13,13 +13,13 @@
 
 
 #include "AthenaMonitoring/ManagedMonitorToolBase.h"
-#include "GaudiKernel/ServiceHandle.h"
 #include "IsolationSelection/IIsolationSelectionTool.h"
 #include "MuonAnalysisInterfaces/IMuonSelectionTool.h"
 #include "MuonCombinedToolInterfaces/IMuonPrintingTool.h"
 
 
 #include "StoreGate/ReadHandleKey.h"
+#include "StoreGate/ReadDecorHandleKeyArray.h"
 #include "TrigDecisionTool/TrigDecisionTool.h"
 
 #include "TrkToolInterfaces/ITrackSelectorTool.h"
@@ -31,9 +31,7 @@
 #include "xAODMuon/SlowMuonContainer.h"
 #include "xAODTrigMuon/L2CombinedMuonContainer.h"
 #include "xAODTrigMuon/L2StandAloneMuonContainer.h"
-
-
-#include "xAODTruth/TruthParticleContainer.h" //typedef
+#include "xAODTruth/TruthParticleContainer.h" 
 
 #include "MuonSegmentValidationPlots.h" //needed for unique_ptr  access to deleter
 #include "MuonHistUtils/MuonSegmentPlots.h" //needed for unique_ptr  access to deleter
@@ -61,7 +59,6 @@ namespace MuonPhysValMonitoring {
         // Public methods:
         ///////////////////////////////////////////////////////////////////
     public:
-        // Copy constructor:
 
         /// Constructor with parameters:
         using ManagedMonitorToolBase::ManagedMonitorToolBase;
@@ -141,6 +138,8 @@ namespace MuonPhysValMonitoring {
         L2CBContKey_t m_muonL2CBName{this, "L2CBMuonContainerName", "HLT_xAOD__L2CombinedMuonContainer_MuonL2CBInfo"};
         MuonContKey_t m_muonEFCombTrigName{this, "EFCombTrigMuonContainerName", "HLT_xAOD__MuonContainer_MuonEFInfo"};
 
+        SG::ReadDecorHandleKeyArray<xAOD::IParticleContainer> m_decorDep{this, "DecorDependencies", {}};
+        
         Gaudi::Property<std::vector<int>> m_selectMuonWPs{
             this, "SelectMuonWorkingPoints", {xAOD::Muon::Loose, xAOD::Muon::Medium, xAOD::Muon::Tight}};
         Gaudi::Property<std::vector<unsigned int>> m_selectMuonAuthors{
-- 
GitLab