ATLAS Offline Software
Loading...
Searching...
No Matches
SPIdDumperAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
5#include "SPIdDumperAlg.h"
6
8#include "Identifier/Identifier.h"
14
16
17#include <fstream>
18
19namespace MuonR4 {
20
22 ATH_CHECK(m_readKey.initialize());
23 ATH_CHECK(m_inSegmentKey.initialize());
24 ATH_CHECK(m_tree.init(this));
25 ATH_CHECK(m_graphFilterTool.retrieve());
26
27 ATH_MSG_DEBUG("Successfully initialized SPIdDumperAlg with ONNX model filtering.");
28 return StatusCode::SUCCESS;
29 }
30
31 StatusCode SPIdDumperAlg::execute(const EventContext& ctx) {
32
34 if (!readHandle.isValid()) {
35 ATH_MSG_ERROR("Failed to retrieve SpacePointContainer from StoreGate.");
36 return StatusCode::FAILURE;
37 }
38
39 MuonML::GraphRawData graphData;
40 ATH_CHECK(m_graphFilterTool->runGraphInference(ctx, graphData));
41
42 if (graphData.graph->dataTensor.size() < 3) {
43 ATH_MSG_DEBUG("ONNX inference output tensor is missing.");
44 return StatusCode::SUCCESS;
45 }
46
47 const float* predictions = graphData.graph->dataTensor[2].GetTensorMutableData<float>();
48 size_t predictionIndex = 0;
49
50 size_t totalNodes = graphData.graph->dataTensor[2].GetTensorTypeAndShapeInfo().GetElementCount();
51 size_t totalFeatureElements = graphData.graph->dataTensor[0].GetTensorTypeAndShapeInfo().GetElementCount();
52
53 if (totalNodes == 0) {
54 ATH_MSG_ERROR("Total number of nodes is zero! Cannot divide.");
55 return StatusCode::FAILURE;
56 }
57
58 size_t numFeaturesPerNode = totalFeatureElements / totalNodes;
59 ATH_MSG_DEBUG("Inferred " << numFeaturesPerNode << " features per node from tensor of size "
60 << totalFeatureElements << " and " << totalNodes << " nodes.");
61
62 std::unordered_map <const SpacePointBucket*, std::vector<const MuonR4::Segment*>> segmentMap;
63 SG::ReadHandle readSegment(m_inSegmentKey, ctx);
64 ATH_CHECK(readSegment.isPresent());
65 for (const MuonR4::Segment* segment : *readSegment) {
66 segmentMap[segment->parent()->parentBucket()].push_back(segment);
67 }
68
69 for (const MuonR4::SpacePointBucket* bucket : *readHandle) {
70
71 std::unordered_map<const SpacePoint*, std::vector<int16_t>> spacePointToSegment;
72 auto match_itr = segmentMap.find(bucket);
73 if (match_itr != segmentMap.end()) {
74 unsigned int segIdx{0};
75 for (const MuonR4::Segment* segment : match_itr->second) {
76 for (const auto& meas : segment->measurements()) {
77 spacePointToSegment[meas->spacePoint()].push_back(segIdx);
78 }
79 ++segIdx;
80 }
81 }
82
83 SpacePointPerLayerSplitter splitter{*bucket};
84 unsigned int layer{0};
85
86 for (const auto& hitsInLay : splitter.mdtHits()) {
87 for (const auto sp : hitsInLay){
88
89 const auto* dc = static_cast<const xAOD::MdtDriftCircle*>(sp->primaryMeasurement());
90 if (dc->status() != Muon::MdtDriftCircleStatus::MdtStatusDriftTime){
91 continue;
92 }
93 m_spoint_x.push_back(sp->localPosition().x());
94 m_spoint_y.push_back(sp->localPosition().y());
95 m_spoint_z.push_back(sp->localPosition().z());
96 m_spoint_driftR.push_back(sp->driftRadius());
97 m_spoint_station.push_back(m_idHelperSvc->stationName(sp->identify()));
98 m_spoint_layer.push_back(layer);
99
100 if (spacePointToSegment.count(sp) > 0) {
101 m_spoint_label.push_back(1);
102 } else {
103 m_spoint_label.push_back(0);
104 }
105
106 if (predictionIndex < graphData.graph->dataTensor[2].GetTensorTypeAndShapeInfo().GetElementCount()) {
107 m_spoint_predictions.push_back(predictions[predictionIndex]);
108 predictionIndex++;
109 } else {
110 ATH_MSG_WARNING("Prediction index exceeded ONNX output size.");
111 m_spoint_predictions.push_back(-999); // Default invalid value
112 }
113
114
115 }
116 ++layer;
117
118 }
119
120 for (const auto& hitsInLay : splitter.stripHits()) {
121
122 for (const auto sp : hitsInLay){
123
124 m_spoint_x.push_back(sp->localPosition().x());
125 m_spoint_y.push_back(sp->localPosition().y());
126 m_spoint_z.push_back(sp->localPosition().z());
127 m_spoint_driftR.push_back(sp->driftRadius());
128 m_spoint_station.push_back(m_idHelperSvc->stationName(sp->identify()));
129 m_spoint_layer.push_back(layer);
130
131 if (spacePointToSegment.count(sp) > 0) {
132 m_spoint_label.push_back(1);
133 } else {
134 m_spoint_label.push_back(0);
135 }
136
137 if (predictionIndex < graphData.graph->dataTensor[2].GetTensorTypeAndShapeInfo().GetElementCount()) {
138 m_spoint_predictions.push_back(predictions[predictionIndex]);
139 predictionIndex++;
140 } else {
141 ATH_MSG_WARNING("Prediction index exceeded ONNX output size.");
142 m_spoint_predictions.push_back(-999); // Default invalid value
143 }
144
145 }
146 ++layer;
147
148 }
149
150 }
151
152 if (!m_tree.fill(ctx)) return StatusCode::FAILURE;
153 return StatusCode::SUCCESS;
154 }
155
157 ATH_CHECK(m_tree.write());
158 return StatusCode::SUCCESS;
159 }
160
161}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
static Double_t sp
Handle class for reading from StoreGate.
SG::ReadHandleKey< MuonR4::SpacePointContainer > m_readKey
MuonVal::VectorBranch< float > & m_spoint_driftR
MuonVal::VectorBranch< float > & m_spoint_x
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
MuonVal::MuonTesterTree m_tree
MuonVal::VectorBranch< uint16_t > & m_spoint_layer
MuonVal::VectorBranch< uint8_t > & m_spoint_station
MuonVal::VectorBranch< float > & m_spoint_z
virtual StatusCode finalize() override
virtual StatusCode initialize() override
SG::ReadHandleKey< MuonR4::SegmentContainer > m_inSegmentKey
virtual StatusCode execute(const EventContext &ctx) override
Execute method.
ToolHandle< MuonML::IGraphInferenceTool > m_graphFilterTool
MuonVal::VectorBranch< uint8_t > & m_spoint_label
MuonVal::VectorBranch< float > & m_spoint_y
MuonVal::VectorBranch< float > & m_spoint_predictions
Placeholder for what will later be the muon segment EDM representation.
const MeasVec & measurements() const
Returns the associated measurements.
: The muon space point bucket represents a collection of points that will bre processed together in t...
The SpacePointPerLayerSplitter takes a set of spacepoints already sorted by layer Identifier (see Muo...
const HitLayVec & mdtHits() const
Returns the sorted Mdt hits.
const HitLayVec & stripHits() const
Returns the sorted strip hits.
virtual bool isValid() override final
Can the handle be successfully dereferenced?
bool isPresent() const
Is the referenced object present in SG?
This header ties the generic definitions in this package.
MdtDriftCircle_v1 MdtDriftCircle
Helper struct to ship the Graph from the space point buckets to ONNX.
Definition GraphData.h:25
std::unique_ptr< InferenceGraph > graph
Pointer to the graph to be parsed to ONNX.
Definition GraphData.h:46