ATLAS Offline Software
MuonRecoChainTester.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3 */
4 // Framework includes
5 #include "MuonRecoChainTester.h"
6 
15 #include "StoreGate/ReadHandle.h"
16 
17 using namespace MuonVal;
18 using namespace MuonPRDTest;
19 
20 namespace{
22  static const SG::ConstAccessor<SegLink_t> acc_truthSegLink{"truthSegmentLink"};
23 }
24 
25 
26 namespace MuonValR4{
28  int evOpts{0};
29  if (m_isMC) evOpts |= EventInfoBranch::isMC;
30  m_tree.addBranch(std::make_unique<EventInfoBranch>(m_tree, evOpts));
31 
32  auto initSegBranch = [this](const std::string& sgKey, const std::string& outCollName) {
33  auto newColl = std::make_unique<SegmentVariables>(m_tree, sgKey, outCollName, msgLevel());
34  if (m_isMC) {
36  std::format("{:}_truthLink", outCollName),[this](const SG::AuxElement* aux){
37  const auto* seg = static_cast<const xAOD::MuonSegment*>(aux);
39  m_truthTrks->push_back(truthP);
40  return m_truthTrks->find(truthP);
41  }));
43  std::format("{:}_truthSegLink", outCollName),[this](const SG::AuxElement* aux) -> unsigned short{
44  if (acc_truthSegLink.isAvailable(*aux) && acc_truthSegLink(*aux).isValid()) {
45  const xAOD::MuonSegment* truthSeg {*acc_truthSegLink(*aux)};
46  return m_truthSegs->push_back(*truthSeg);
47  }
48  return -1;
49  }));
50  }
51  m_tree.addBranch(std::move(newColl));
52  };
53 
54  initSegBranch(m_legacySegmentKey, "LegacySegments");
55  initSegBranch(m_r4PatternSegmentKey, "HoughSegments");
56  initSegBranch(m_segmentKeyR4, "SegmentsR4");
57 
58  ATH_CHECK(m_truthSegmentKey.initialize(m_isMC));
59  if (m_isMC) {
60  m_truthSegs = std::make_unique<SegmentVariables>(m_tree, m_truthSegmentKey.key(), "TruthSegments", msgLevel());
61  m_tree.addBranch(m_truthSegs);
62  }
63 
64  ATH_CHECK(m_legacyTrackKey.initialize());
65  ATH_CHECK(m_TrackKeyHoughR4.initialize());
66  ATH_CHECK(m_TrackKeyR4.initialize());
67  ATH_CHECK(m_spacePointKey.initialize());
68  ATH_CHECK(m_truthKey.initialize(m_isMC));
69 
70  m_legacyTrks = std::make_unique<IParticleFourMomBranch>(m_tree, "LegacyMSTrks");
71  m_legacyTrks->addVariable(std::make_unique<TrackChi2Branch>(*m_legacyTrks));
72 
73  m_TrksHoughR4 = std::make_unique<IParticleFourMomBranch>(m_tree, "HoughMSTrks");
74  m_TrksHoughR4->addVariable(std::make_unique<TrackChi2Branch>(*m_TrksHoughR4));
75 
76  m_TrksSegmentR4 = std::make_unique<IParticleFourMomBranch>(m_tree, "MSTrksR4");
77  m_TrksSegmentR4->addVariable(std::make_unique<TrackChi2Branch>(*m_TrksSegmentR4));
78 
79  m_tree.addBranch(m_legacyTrks);
80  m_tree.addBranch(m_TrksSegmentR4);
81  m_tree.addBranch(m_TrksHoughR4);
82 
83  if (m_isMC) {
84  m_trkTruthLinks.emplace_back(m_legacyTrackKey, "truthParticleLink");
85  m_trkTruthLinks.emplace_back(m_TrackKeyHoughR4, "truthParticleLink");
86  m_trkTruthLinks.emplace_back(m_TrackKeyR4, "truthParticleLink");
87  m_trkTruthLinks.emplace_back(m_truthSegmentKey, "truthParticleLink");
88  m_trkTruthLinks.emplace_back(std::format("{:}.truthParticleLink", m_r4PatternSegmentKey.value()));
89  m_trkTruthLinks.emplace_back(std::format("{:}.truthParticleLink", m_segmentKeyR4.value()));
90 
91  m_truthTrks = std::make_unique<IParticleFourMomBranch>(m_tree, "TruthMuons");
92  BilateralLinkerBranch::connectCollections(m_legacyTrks, m_truthTrks, [](const xAOD::IParticle* trk){
93  return xAOD::TruthHelpers::getTruthParticle(*trk); }, "truth", "LegacyMS");
94  BilateralLinkerBranch::connectCollections(m_TrksHoughR4, m_truthTrks, [](const xAOD::IParticle* trk){
95  return xAOD::TruthHelpers::getTruthParticle(*trk); }, "truth", "HoughMS");
96  BilateralLinkerBranch::connectCollections(m_TrksSegmentR4, m_truthTrks, [](const xAOD::IParticle* trk){
97  return xAOD::TruthHelpers::getTruthParticle(*trk); }, "truth", "MSTrksR4");
98 
99  m_tree.addBranch(m_truthTrks);
100  }
101 
102  ATH_CHECK(m_trkTruthLinks.initialize());
103  ATH_CHECK(m_tree.init(this));
104  return StatusCode::SUCCESS;
105  }
106  void MuonRecoChainTester::fillBucketsPerStation(const MuonR4::SpacePointContainer& spContainer,
107  const StIdx station,
108  MuonVal::ScalarBranch<uint16_t>& outBranch) const{
109  outBranch = std::count_if(spContainer.begin(), spContainer.end(),
110  [station](const MuonR4::SpacePointBucket* bucket){
111  return Muon::MuonStationIndex::toStationIndex(bucket->msSector()->chamberIndex()) == station;
112  });
113  }
114 
116 
117  const EventContext& ctx{Gaudi::Hive::currentContext()};
118  const xAOD::TrackParticleContainer* legacyTrks{nullptr};
119  ATH_CHECK(SG::get(legacyTrks, m_legacyTrackKey, ctx));
120 
121  const xAOD::TrackParticleContainer* trksFromHoughR4{nullptr};
122  ATH_CHECK(SG::get(trksFromHoughR4, m_TrackKeyHoughR4, ctx));
123 
124  const xAOD::TrackParticleContainer* trksR4{nullptr};
125  ATH_CHECK(SG::get(trksR4, m_TrackKeyR4, ctx));
126 
127  ATH_MSG_DEBUG("Fill reconstructed tracks from "<<m_legacyTrackKey.fullKey());
128  for (const xAOD::TrackParticle* trk : *legacyTrks) {
129  m_legacyTrks->push_back(trk);
130  }
131  ATH_MSG_DEBUG("Fill reconstructed tracks from "<<m_TrackKeyR4.fullKey());
132  for (const xAOD::TrackParticle* trk : *trksR4) {
133  m_TrksSegmentR4->push_back(trk);
134  }
135  ATH_MSG_DEBUG("Fill reconstructed tracks from "<<m_TrackKeyHoughR4.fullKey());
136  for (const xAOD::TrackParticle* trk : *trksFromHoughR4) {
137  m_TrksHoughR4->push_back(trk);
138  }
139 
140  if (!m_truthKey.empty()) {
141  const xAOD::TruthParticleContainer* truthCont{nullptr};
142  ATH_CHECK(SG::get(truthCont, m_truthKey, ctx));
143  for (const xAOD::TruthParticle* truth : *truthCont) {
144  m_truthTrks->push_back(truth);
145  }
146  const xAOD::MuonSegmentContainer* truthSeg{nullptr};
147  ATH_CHECK(SG::get(truthSeg, m_truthSegmentKey, ctx));
148  for (const xAOD::MuonSegment* seg : *truthSeg) {
149  m_truthSegs->push_back(*seg);
150  }
151  }
153  const MuonR4::SpacePointContainer* spContainer{nullptr};
154  ATH_CHECK(SG::get(spContainer, m_spacePointKey, ctx));
155  m_nBucket = spContainer->size();
156 
157  fillBucketsPerStation(*spContainer, StIdx::BI, m_nBucketBI);
158  fillBucketsPerStation(*spContainer, StIdx::BM, m_nBucketBM);
159  fillBucketsPerStation(*spContainer, StIdx::BO, m_nBucketBO);
160  fillBucketsPerStation(*spContainer, StIdx::BE, m_nBucketBE);
161 
162  fillBucketsPerStation(*spContainer, StIdx::EI, m_nBucketEI);
163  fillBucketsPerStation(*spContainer, StIdx::EM, m_nBucketEM);
164  fillBucketsPerStation(*spContainer, StIdx::EO, m_nBucketEO);
165  fillBucketsPerStation(*spContainer, StIdx::EE, m_nBucketEE);
166 
167 
168  if(!m_tree.fill(ctx)) {
169  return StatusCode::FAILURE;
170  }
171  return StatusCode::SUCCESS;
172  }
174  ATH_CHECK(m_tree.write());
175  return StatusCode::SUCCESS;
176  }
177 }
MuonSimHitHelpers.h
python.tests.PyTestsLib.finalize
def finalize(self)
_info( "content of StoreGate..." ) self.sg.dump()
Definition: PyTestsLib.py:50
MuonR4::SpacePointBucket
: The muon space point bucket represents a collection of points that will bre processed together in t...
Definition: MuonSpectrometer/MuonPhaseII/Event/MuonSpacePoint/MuonSpacePoint/SpacePointContainer.h:21
vtune_athena.format
format
Definition: vtune_athena.py:14
MuonR4::getTruthMatchedParticle
const xAOD::TruthParticle * getTruthMatchedParticle(const xAOD::MuonSegment &segment)
Returns the particle truth-matched to the segment.
Definition: MuonSimHitHelpers.cxx:100
MuonRecoChainTester.h
make_unique
std::unique_ptr< T > make_unique(Args &&... args)
Definition: SkimmingToolEXOT5.cxx:23
ConstDataVector.h
DataVector adapter that acts like it holds const pointers.
MuonVal::EventInfoBranch::isMC
@ isMC
Flag determining whether the branch is simulation.
Definition: EventInfoBranch.h:20
TRT_PAI_gasdata::EO
const float EO[NO]
Energy levels for Oxygen.
Definition: TRT_PAI_gasdata.h:301
xAODP4Helpers.h
SG::AuxElement
Base class for elements of a container that can have aux data.
Definition: AuxElement.h:483
MuonVal::GenericAuxDecorationBranch
Generic branch object where the information is evaluated by a std::function instead reading it from t...
Definition: AuxElementBranch.h:33
initialize
void initialize()
Definition: run_EoverP.cxx:894
ParticleVariables.h
MuonTesterTreeDict.h
xAOD::MuonSegment_v1
Class describing a MuonSegment.
Definition: MuonSegment_v1.h:33
SG::ConstAccessor
Helper class to provide constant type-safe access to aux data.
Definition: ConstAccessor.h:55
xAOD::IParticle
Class providing the definition of the 4-vector interface.
Definition: Event/xAOD/xAODBase/xAODBase/IParticle.h:41
xAODTruthHelpers.h
TrackChi2Branch.h
LArG4FSStartPointFilterLegacy.execute
execute
Definition: LArG4FSStartPointFilterLegacy.py:20
Ringer::EM
@ EM
Definition: CaloRingsDefs.h:19
SG::get
const T * get(const ReadCondHandleKey< T > &key, const EventContext &ctx)
Convenience function to retrieve an object given a ReadCondHandleKey.
Definition: ReadCondHandle.h:287
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
xAOD::TruthParticle_v1
Class describing a truth particle in the MC record.
Definition: TruthParticle_v1.h:37
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
MuonR4::SegLink_t
ElementLink< MuonR4::SegmentContainer > SegLink_t
Definition: xAODSegmentCnvAlg.cxx:18
DataVector
Derived DataVector<T>.
Definition: DataVector.h:794
MuonVal::BilateralLinkerBranch::connectCollections
static bool connectCollections(ParticleBranch_ptr primColl, ParticleBranch_ptr secondColl, Linker_t fromPrimToSec, const std::string &altPrimName="", const std::string &altSecName="")
Definition: LinkerBranch.cxx:33
MuonValR4
Lightweight algorithm to read xAOD MDT sim hits and (fast-digitised) drift circles from SG and fill a...
Definition: IPatternVisualizationTool.h:23
Muon::MuonStationIndex::StIndex
StIndex
enum to classify the different station layers in the muon spectrometer
Definition: MuonStationIndex.h:23
MuonVal
Class to store array like branches into the n-tuples.
Definition: HitValAlg.cxx:19
MuonVal::ScalarBranch< uint16_t >
xAOD::TruthHelpers::getTruthParticle
const xAOD::TruthParticle * getTruthParticle(const xAOD::IParticle &p)
Return the truthParticle associated to the given IParticle (if any)
Definition: xAODTruthHelpers.cxx:25
DataVector::end
const_iterator end() const noexcept
Return a const_iterator pointing past the end of the collection.
MuonPRDTest
Definition: MuonHitTesterAlg.h:15
SegmentVariables.h
xAOD::TrackParticle_v1
Class describing a TrackParticle.
Definition: TrackParticle_v1.h:43
ReadHandle.h
Handle class for reading from StoreGate.
DataVector::begin
const_iterator begin() const noexcept
Return a const_iterator pointing at the beginning of the collection.