ATLAS Offline Software
SegmentDumperAlg.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2025 CERN
3  for the benefit of the ATLAS collaboration
4 */
5 
6 #include "SegmentDumperAlg.h"
7 
8 #include "StoreGate/ReadHandle.h"
10 #include "xAODMuon/MuonSegment.h"
12 #include "xAODMuonPrepData/UtilFunctions.h" // getTruthMatchedParticle
13 
17 
18 #include <set>
19 #include <unordered_map>
20 #include <limits>
21 
22 namespace {
23 struct LocalSegSorter {
24  bool operator()(const xAOD::MuonSegment* a,
25  const xAOD::MuonSegment* b) const {
26  if (a == b) return false;
27  if (a->chamberIndex() != b->chamberIndex())
28  return a->chamberIndex() < b->chamberIndex();
29  if (a->sector() != b->sector())
30  return a->sector() < b->sector();
31  if (a->etaIndex() != b->etaIndex())
32  return a->etaIndex() < b->etaIndex();
33  using namespace MuonR4::SegmentFit;
34  auto la = localSegmentPars(*a);
35  auto lb = localSegmentPars(*b);
36  return la < lb;
37  }
38 };
39 } // namespace
40 
41 namespace MuonR4 {
42 
44  ATH_MSG_INFO("Initializing SegmentDumperAlg (MC)");
45 
46  ATH_CHECK(m_spacePointKeys.initialize());
48 
49  // Truth decoration on MC
50  m_truthDecorKeys.emplace_back(m_segmentKeys, "truthParticleLink");
51  ATH_CHECK(m_truthDecorKeys.initialize());
52 
54  ATH_CHECK(m_tree.init(this));
55  return StatusCode::SUCCESS;
56 }
57 
60  return StatusCode::SUCCESS;
61 }
62 
64  const EventContext& ctx{Gaudi::Hive::currentContext()};
65 
66  using SegmentsPerBucket_t =
67  std::unordered_map<const SpacePointBucket*,
68  std::set<const xAOD::MuonSegment*, LocalSegSorter>>;
69 
70  const ActsTrk::GeometryContext* gctx{nullptr};
71  ATH_CHECK(SG::get(gctx, m_geoCtxKey, ctx));
72 
73  const xAOD::MuonSegmentContainer* segContainer{nullptr};
74  ATH_CHECK(SG::get(segContainer, m_segmentKeys, ctx));
75 
76  for (unsigned iKey = 0; iKey < m_spacePointKeys.size(); ++iKey) {
77  const auto& spKey = m_spacePointKeys[iKey];
78 
79  const SpacePointContainer* spContainer{nullptr};
80  ATH_CHECK(SG::get(spContainer, spKey, ctx));
81 
82  SegmentsPerBucket_t segPerBucket{};
83  if (segContainer) {
84  for (const xAOD::MuonSegment* seg : *segContainer) {
85  const auto* detSeg = MuonR4::detailedSegment(*seg);
86  segPerBucket[detSeg->parent()->parentBucket()].insert(seg);
87  }
88  }
89 
90  for (const SpacePointBucket* bucket : *spContainer) {
91  m_bucket_spacePoints = static_cast<uint16_t>(bucket->size());
92  m_bucket_chamberIdx = static_cast<uint8_t>(bucket->msSector()->chamberIndex());
93  m_bucket_sector = static_cast<uint8_t>(bucket->msSector()->sector());
95 
96  const auto it = segPerBucket.find(bucket);
97  if (it != segPerBucket.end()) {
98  for (const xAOD::MuonSegment* seg : it->second) {
99  // Reco-level outputs (always aligned to segments)
104 
105  // --- Reco η/φ (GLOBAL): direction is already in global coordinates
106  const Amg::Vector3D& globDir = seg->direction();
107  float segEta = static_cast<float>(globDir.eta());
108  float segPhi = static_cast<float>(globDir.phi());
109  m_segmentRecoEta.push_back(segEta);
110  m_segmentRecoPhi.push_back(segPhi);
111 
112  // Truth association
113  int32_t truthIdx = -1;
114  int32_t truthPdg = -1;
116  if (tp) {
117  truthIdx = static_cast<int32_t>(tp->index());
118  truthPdg = static_cast<int32_t>(tp->pdgId());
119  }
120 
121  // Per-segment truth flags/ids remain aligned to segments
122  m_segmentHasTruth.push_back(truthIdx >= 0 ? 1 : 0);
123 
124  // Fill truth kinematics ONLY when a truth match exists (sparse truth arrays)
125  if (truthIdx >= 0) {
126  m_segmentTruthIdx.push_back(truthIdx);
127  m_segmentTruthPDGId.push_back(truthPdg);
128  m_segmentTruthPt.push_back(static_cast<float>(tp->pt())); // MeV
129  m_segmentTruthEta.push_back(static_cast<float>(tp->eta()));
130  m_segmentTruthPhi.push_back(static_cast<float>(tp->phi()));
131  }
132  }
133  }
134 
135  if (!m_tree.fill(ctx)) {
136  ATH_MSG_ERROR("Failed to fill output tree");
137  return StatusCode::FAILURE;
138  }
139  }
140  }
141 
142  return StatusCode::SUCCESS;
143 }
144 
147  std::vector<unsigned int> uniqueLayers;
148  uniqueLayers.reserve(bucket.size());
149 
150  for (const SpacePointBucket::value_type& sp : bucket) {
151  const unsigned layNum = sorter.sectorLayerNum(*sp);
152  if (std::find(uniqueLayers.begin(), uniqueLayers.end(), layNum) == uniqueLayers.end()) {
153  uniqueLayers.push_back(layNum);
154  }
155  }
156  return static_cast<uint16_t>(uniqueLayers.size());
157 }
158 
159 } // namespace MuonR4
MuonR4::SegmentDumperAlg::m_segmentDir
MuonVal::ThreeVectorBranch m_segmentDir
Definition: SegmentDumperAlg.h:65
SegmentDumperAlg.h
xAOD::MuonSegment_v1::numberDoF
float numberDoF() const
Returns the numberDoF.
MuonSimHitHelpers.h
UtilFunctions.h
MuonR4::SegmentDumperAlg::m_segmentTruthIdx
MuonVal::VectorBranch< int32_t > & m_segmentTruthIdx
Definition: SegmentDumperAlg.h:78
MuonR4::SegmentFit
Definition: MuonHoughDefs.h:34
xAOD::MuonSegment_v1::direction
Amg::Vector3D direction() const
Returns the direction as Amg::Vector.
Definition: MuonSegment_v1.cxx:18
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
MuonR4::getTruthMatchedParticle
const xAOD::TruthParticle * getTruthMatchedParticle(const xAOD::MuonSegment &segment)
Returns the particle truth-matched to the segment.
Definition: MuonSimHitHelpers.cxx:107
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
MuonVal::MuonTesterTree::init
StatusCode init(OWNER *instance)
Initialize method.
xAOD::uint8_t
uint8_t
Definition: Muon_v1.cxx:553
find
std::string find(const std::string &s)
return a remapped string
Definition: hcg.cxx:138
MuonR4::SegmentDumperAlg::m_truthDecorKeys
SG::ReadDecorHandleKeyArray< xAOD::MuonSegmentContainer > m_truthDecorKeys
Truth decoration (MC)
Definition: SegmentDumperAlg.h:44
MuonR4::SpacePointPerLayerSorter
The SpacePointPerLayerSorter sort two given space points by their layer Identifier.
Definition: SpacePointPerLayerSorter.h:15
MuonR4::SegmentDumperAlg::m_segmentTruthPt
MuonVal::VectorBranch< float > & m_segmentTruthPt
Definition: SegmentDumperAlg.h:82
xAOD::MuonSegment_v1::chiSquared
float chiSquared() const
MuonR4::SegmentDumperAlg::execute
StatusCode execute() override final
Definition: SegmentDumperAlg.cxx:63
MuonSegment.h
skel.it
it
Definition: skel.GENtoEVGEN.py:407
MuonR4::SegmentDumperAlg::m_tree
MuonVal::MuonTesterTree m_tree
Output tree.
Definition: SegmentDumperAlg.h:51
xAOD::MuonSegment_v1
Class describing a MuonSegment.
Definition: MuonSegment_v1.h:33
ParticleTest.tp
tp
Definition: ParticleTest.py:25
MuonR4::SegmentDumperAlg::m_spacePointKeys
SG::ReadHandleKeyArray< SpacePointContainer > m_spacePointKeys
Inputs.
Definition: SegmentDumperAlg.h:35
postInclude.sorter
sorter
Definition: postInclude.SortInput.py:23
MuonR4::SegmentDumperAlg::finalize
StatusCode finalize() override final
Definition: SegmentDumperAlg.cxx:58
MuonR4::SegmentDumperAlg::initialize
StatusCode initialize() override final
Definition: SegmentDumperAlg.cxx:43
MuonR4::detailedSegment
const Segment * detailedSegment(const xAOD::MuonSegment &seg)
Helper function to navigate from the xAOD::MuonSegment to the MuonR4::Segment.
Definition: TrackingHelpers.cxx:24
MuonR4::SegmentDumperAlg::m_segmentRecoPhi
MuonVal::VectorBranch< float > & m_segmentRecoPhi
Definition: SegmentDumperAlg.h:74
MuonVal::ThreeVectorBranch::push_back
void push_back(const Amg::Vector3D &vec)
interface using the Amg::Vector3D
Definition: ThreeVectorBranch.cxx:23
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
MuonR4::SegmentDumperAlg::m_segment_chiSquared
MuonVal::VectorBranch< float > & m_segment_chiSquared
Definition: SegmentDumperAlg.h:66
python.BunchSpacingUtils.lb
lb
Definition: BunchSpacingUtils.py:86
MuonR4::SegmentDumperAlg::m_bucket_sector
MuonVal::ScalarBranch< uint8_t > & m_bucket_sector
Definition: SegmentDumperAlg.h:60
xAOD::uint16_t
setWord1 uint16_t
Definition: eFexEMRoI_v1.cxx:93
MuonR4::SegmentDumperAlg::m_segment_numberDoF
MuonVal::VectorBranch< float > & m_segment_numberDoF
Definition: SegmentDumperAlg.h:68
SG::get
const T * get(const ReadCondHandleKey< T > &key, const EventContext &ctx)
Convenience function to retrieve an object given a ReadCondHandleKey.
Definition: ReadCondHandle.h:283
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
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
SG::VarHandleKey::initialize
StatusCode initialize(bool used=true)
If this object is used as a property, then this should be called during the initialize phase.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:103
ActsTrk::GeometryContext
Definition: GeometryContext.h:28
MuonR4::SegmentDumperAlg::m_bucket_spacePoints
MuonVal::ScalarBranch< uint16_t > & m_bucket_spacePoints
Definition: SegmentDumperAlg.h:54
DataVector
Derived DataVector<T>.
Definition: DataVector.h:795
MuonVal::VectorBranch::push_back
void push_back(const T &value)
Adds a new element at the end of the vector.
MuonR4::SegmentDumperAlg::m_bucket_chamberIdx
MuonVal::ScalarBranch< uint8_t > & m_bucket_chamberIdx
Definition: SegmentDumperAlg.h:58
xAOD::MuonSegment_v1::position
Amg::Vector3D position() const
Returns the position as Amg::Vector.
Definition: MuonSegment_v1.cxx:15
TrackingHelpers.h
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:76
MuonR4::SegmentDumperAlg::m_geoCtxKey
SG::ReadHandleKey< ActsTrk::GeometryContext > m_geoCtxKey
Definition: SegmentDumperAlg.h:47
MuonR4::SegmentDumperAlg::m_segmentTruthEta
MuonVal::VectorBranch< float > & m_segmentTruthEta
Definition: SegmentDumperAlg.h:84
Amg::Vector3D
Eigen::Matrix< double, 3, 1 > Vector3D
Definition: GeoPrimitives.h:47
MuonR4::SegmentDumperAlg::m_segmentTruthPhi
MuonVal::VectorBranch< float > & m_segmentTruthPhi
Definition: SegmentDumperAlg.h:86
MuonR4
This header ties the generic definitions in this package.
Definition: HoughEventData.h:16
MuonR4::SegmentDumperAlg::m_segmentKeys
SG::ReadHandleKey< xAOD::MuonSegmentContainer > m_segmentKeys
Definition: SegmentDumperAlg.h:39
MuonR4::SegmentDumperAlg::countLayersInBucket
uint16_t countLayersInBucket(const SpacePointBucket &bucket) const
Definition: SegmentDumperAlg.cxx:145
a
TList * a
Definition: liststreamerinfos.cxx:10
SegmentFitterEventData.h
MuonVal::MuonTesterTree::fill
bool fill(const EventContext &ctx)
Fills the tree per call.
Definition: MuonTesterTree.cxx:89
MuonR4::SegmentDumperAlg::m_bucket_layers
MuonVal::ScalarBranch< uint16_t > & m_bucket_layers
Definition: SegmentDumperAlg.h:56
MuonVal::MuonTesterTree::write
StatusCode write()
Finally write the TTree objects.
Definition: MuonTesterTree.cxx:178
MuonR4::SegmentFit::localSegmentPars
Parameters localSegmentPars(const xAOD::MuonSegment &seg)
Returns the localSegPars decoration from a xAODMuon::Segment.
Definition: SegmentFitterEventData.cxx:42
MuonR4::SegmentDumperAlg::m_segmentHasTruth
MuonVal::VectorBranch< uint8_t > & m_segmentHasTruth
Definition: SegmentDumperAlg.h:88
ReadHandle.h
Handle class for reading from StoreGate.
TruthParticle.h
MuonR4::SegmentDumperAlg::m_segmentTruthPDGId
MuonVal::VectorBranch< int32_t > & m_segmentTruthPDGId
Definition: SegmentDumperAlg.h:80
columnar::operator()
decltype(auto) operator()(ObjectId< CI, CM > id) const noexcept
Definition: ColumnAccessor.h:173
SpacePointPerLayerSorter.h
MuonR4::SegmentDumperAlg::m_segmentPos
MuonVal::ThreeVectorBranch m_segmentPos
Definition: SegmentDumperAlg.h:64
MuonR4::SegmentDumperAlg::m_segmentRecoEta
MuonVal::VectorBranch< float > & m_segmentRecoEta
Definition: SegmentDumperAlg.h:72