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  if (sgKey.empty()){
34  return;
35  }
36  auto newColl = std::make_unique<SegmentVariables>(m_tree, sgKey, outCollName, msgLevel());
37  if (m_isMC) {
39  std::format("{:}_truthLink", outCollName),[this](const SG::AuxElement* aux){
40  const auto* seg = static_cast<const xAOD::MuonSegment*>(aux);
42  m_truthTrks->push_back(truthP);
43  return m_truthTrks->find(truthP);
44  }));
46  std::format("{:}_truthSegLink", outCollName),[this](const SG::AuxElement* aux) -> unsigned short{
47  if (acc_truthSegLink.isAvailable(*aux) && acc_truthSegLink(*aux).isValid()) {
48  const xAOD::MuonSegment* truthSeg {*acc_truthSegLink(*aux)};
49  return m_truthSegs->push_back(*truthSeg);
50  }
51  return -1;
52  }));
53  }
54  m_tree.addBranch(std::move(newColl));
55  };
56 
57  initSegBranch(m_legacySegmentKey, "LegacySegments");
58  initSegBranch(m_r4PatternSegmentKey, "HoughSegments");
59  initSegBranch(m_segmentKeyR4, "SegmentsR4");
60 
61  ATH_CHECK(m_truthSegmentKey.initialize(m_isMC));
62  if (m_isMC) {
63  m_truthSegs = std::make_unique<SegmentVariables>(m_tree, m_truthSegmentKey.key(), "TruthSegments", msgLevel());
64  m_tree.addBranch(m_truthSegs);
65  }
66 
67  ATH_CHECK(m_legacyTrackKey.initialize());
68  ATH_CHECK(m_TrackKeyHoughR4.initialize(m_r4PatternSegmentKey.value().size()));
69  ATH_CHECK(m_TrackKeyR4.initialize());
70  ATH_CHECK(m_spacePointKey.initialize());
71  ATH_CHECK(m_truthKey.initialize(m_isMC));
72 
73  m_legacyTrks = std::make_unique<IParticleFourMomBranch>(m_tree, "LegacyMSTrks");
74  m_legacyTrks->addVariable(std::make_unique<TrackChi2Branch>(*m_legacyTrks));
75 
76  if (!m_TrackKeyHoughR4.empty()){
77  m_TrksHoughR4 = std::make_unique<IParticleFourMomBranch>(m_tree, "HoughMSTrks");
78  m_TrksHoughR4->addVariable(std::make_unique<TrackChi2Branch>(*m_TrksHoughR4));
79  m_tree.addBranch(m_TrksHoughR4);
80 
81  }
82  m_TrksSegmentR4 = std::make_unique<IParticleFourMomBranch>(m_tree, "MSTrksR4");
83  m_TrksSegmentR4->addVariable(std::make_unique<TrackChi2Branch>(*m_TrksSegmentR4));
84 
85  m_tree.addBranch(m_legacyTrks);
86  m_tree.addBranch(m_TrksSegmentR4);
87 
88  if (m_isMC) {
89  m_trkTruthLinks.emplace_back(m_legacyTrackKey, "truthParticleLink");
90  if (!m_TrackKeyHoughR4.empty()) {
91  m_trkTruthLinks.emplace_back(m_TrackKeyHoughR4, "truthParticleLink");
92  m_trkTruthLinks.emplace_back(std::format("{:}.truthParticleLink", m_r4PatternSegmentKey.value()));
93  BilateralLinkerBranch::connectCollections(m_TrksHoughR4, m_truthTrks, [](const xAOD::IParticle* trk){
94  return xAOD::TruthHelpers::getTruthParticle(*trk); }, "truth", "HoughMS");
95  }
96  m_trkTruthLinks.emplace_back(m_TrackKeyR4, "truthParticleLink");
97  m_trkTruthLinks.emplace_back(m_truthSegmentKey, "truthParticleLink");
98 
99  m_trkTruthLinks.emplace_back(std::format("{:}.truthParticleLink", m_segmentKeyR4.value()));
100 
101  m_truthTrks = std::make_unique<IParticleFourMomBranch>(m_tree, "TruthMuons");
102  BilateralLinkerBranch::connectCollections(m_legacyTrks, m_truthTrks, [](const xAOD::IParticle* trk){
103  return xAOD::TruthHelpers::getTruthParticle(*trk); }, "truth", "LegacyMS");
104  BilateralLinkerBranch::connectCollections(m_TrksSegmentR4, m_truthTrks, [](const xAOD::IParticle* trk){
105  return xAOD::TruthHelpers::getTruthParticle(*trk); }, "truth", "MSTrksR4");
106  m_tree.addBranch(m_truthTrks);
107  }
108 
109  ATH_CHECK(m_trkTruthLinks.initialize());
110  ATH_CHECK(m_tree.init(this));
111  return StatusCode::SUCCESS;
112  }
113  void MuonRecoChainTester::fillBucketsPerStation(const MuonR4::SpacePointContainer& spContainer,
114  const StIdx station,
115  MuonVal::ScalarBranch<uint16_t>& outBranch) const{
116  outBranch = std::count_if(spContainer.begin(), spContainer.end(),
117  [station](const MuonR4::SpacePointBucket* bucket){
118  return Muon::MuonStationIndex::toStationIndex(bucket->msSector()->chamberIndex()) == station;
119  });
120  }
121 
123 
124  const EventContext& ctx{Gaudi::Hive::currentContext()};
125  const xAOD::TrackParticleContainer* legacyTrks{nullptr};
126  ATH_CHECK(SG::get(legacyTrks, m_legacyTrackKey, ctx));
127 
128  const xAOD::TrackParticleContainer* trksFromHoughR4{nullptr};
129  ATH_CHECK(SG::get(trksFromHoughR4, m_TrackKeyHoughR4, ctx));
130 
131  const xAOD::TrackParticleContainer* trksR4{nullptr};
132  ATH_CHECK(SG::get(trksR4, m_TrackKeyR4, ctx));
133 
134  ATH_MSG_DEBUG("Fill reconstructed tracks from "<<m_legacyTrackKey.fullKey());
135  for (const xAOD::TrackParticle* trk : *legacyTrks) {
136  m_legacyTrks->push_back(trk);
137  }
138  ATH_MSG_DEBUG("Fill reconstructed tracks from "<<m_TrackKeyR4.fullKey());
139  for (const xAOD::TrackParticle* trk : *trksR4) {
140  m_TrksSegmentR4->push_back(trk);
141  }
142  ATH_MSG_DEBUG("Fill reconstructed tracks from "<<m_TrackKeyHoughR4.fullKey());
143  if (trksFromHoughR4) {
144  for (const xAOD::TrackParticle* trk : *trksFromHoughR4) {
145  m_TrksHoughR4->push_back(trk);
146  }
147  }
148  if (!m_truthKey.empty()) {
149  const xAOD::TruthParticleContainer* truthCont{nullptr};
150  ATH_CHECK(SG::get(truthCont, m_truthKey, ctx));
151  for (const xAOD::TruthParticle* truth : *truthCont) {
152  m_truthTrks->push_back(truth);
153  }
154  const xAOD::MuonSegmentContainer* truthSeg{nullptr};
155  ATH_CHECK(SG::get(truthSeg, m_truthSegmentKey, ctx));
156  for (const xAOD::MuonSegment* seg : *truthSeg) {
157  m_truthSegs->push_back(*seg);
158  }
159  }
161  const MuonR4::SpacePointContainer* spContainer{nullptr};
162  ATH_CHECK(SG::get(spContainer, m_spacePointKey, ctx));
163  m_nBucket = spContainer->size();
164 
165  fillBucketsPerStation(*spContainer, StIdx::BI, m_nBucketBI);
166  fillBucketsPerStation(*spContainer, StIdx::BM, m_nBucketBM);
167  fillBucketsPerStation(*spContainer, StIdx::BO, m_nBucketBO);
168  fillBucketsPerStation(*spContainer, StIdx::BE, m_nBucketBE);
169 
170  fillBucketsPerStation(*spContainer, StIdx::EI, m_nBucketEI);
171  fillBucketsPerStation(*spContainer, StIdx::EM, m_nBucketEM);
172  fillBucketsPerStation(*spContainer, StIdx::EO, m_nBucketEO);
173  fillBucketsPerStation(*spContainer, StIdx::EE, m_nBucketEE);
174 
175 
176  if(!m_tree.fill(ctx)) {
177  return StatusCode::FAILURE;
178  }
179  return StatusCode::SUCCESS;
180  }
182  ATH_CHECK(m_tree.write());
183  return StatusCode::SUCCESS;
184  }
185 }
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:107
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
Abrivation of the link to the reco segment container
Definition: xAODSegmentCnvAlg.cxx:19
DataVector
Derived DataVector<T>.
Definition: DataVector.h:795
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:44
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.