ATLAS Offline Software
Loading...
Searching...
No Matches
TruthSegmentCsvDumperAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
6
9#include "GaudiKernel/SystemOfUnits.h"
10
13
14#include <fstream>
15
16#include <unordered_map>
17
18
19namespace {
20 constexpr int encodeId(const int8_t stName, const int8_t stEta,
21 const int8_t sector) {
22 return (sector <<16 |stEta << 8| stName);
23 }
24 constexpr double precCutOff(const double value, const double cutOff = 1.e-15) {
25 return Acts::abs(value) > cutOff ? value : 0.;
26 }
27
28}
29
30namespace MuonR4{
31
32
34 ATH_CHECK(m_inSegmentKey.initialize());
35 ATH_CHECK(m_geoCtxKey.initialize());
36 ATH_CHECK(m_idHelperSvc.retrieve());
37 ATH_CHECK(m_edmHelperSvc.retrieve());
38 ATH_CHECK(detStore()->retrieve(m_detMgr));
39 return StatusCode::SUCCESS;
40}
41
43 const auto truthHits = getMatchingSimHits(segment);
44 if (truthHits.size()) {
45 return m_detMgr->getSectorEnvelope((*truthHits.begin())->identify());
46 }
47 if (segment.muonSegment().isValid()) {
48 return m_detMgr->getSectorEnvelope(m_edmHelperSvc->chamberId(static_cast<const Muon::MuonSegment&>(**segment.muonSegment())));
49 }
50 ATH_MSG_WARNING("No matching hits were found. Neither the simulated ones or the reconstructed");
51 return nullptr;
52}
53
55 const EventContext & ctx = Gaudi::Hive::currentContext();
56 std::ofstream file{std::format("event-{:09}-MuonTruthSegment.csv", ++m_event)};
57 constexpr std::string_view delim = ",";
58 file<<"sectorId"<<delim;
59 file<<"globalPositionX"<<delim;
60 file<<"globalPositionY"<<delim;
61 file<<"globalPositionZ"<<delim;
62
63 file<<"globalDirectionX"<<delim;
64 file<<"globalDirectionY"<<delim;
65 file<<"globalDirectionZ"<<delim;
66
67
68 file<<"localPositionX"<<delim;
69 file<<"localPositionY"<<delim;
70 file<<"localPositionZ"<<delim;
71
72 file<<"localDirectionX"<<delim;
73 file<<"localDirectionY"<<delim;
74 file<<"localDirectionZ"<<delim;
75
76 file<<"time"<<delim;
77 file<<"timeError"<<delim;
78 file<<"chiSquared"<<delim;
79 file<<"nDoF"<<delim;
80 file<<"precisionHits"<<delim;
81 file<<"phiLayers"<<delim;
82 file<<"trigEtaLayers"<<delim;
83 file<<std::endl;
84
85 const xAOD::MuonSegmentContainer* readTruthSegment{nullptr};
86 ATH_CHECK(SG::get(readTruthSegment, m_inSegmentKey, ctx));
87
88 const ActsTrk::GeometryContext* gctxHandle{nullptr};
89 ATH_CHECK(SG::get(gctxHandle, m_geoCtxKey, ctx));
90
91 for (const xAOD::MuonSegment* segment : *readTruthSegment) {
92 const MuonGMR4::SpectrometerSector* sector = msSector(*segment);
93 if (!sector) {
94 continue;
95 }
96 const Amg::Transform3D globToLoc{sector->globalToLocalTrans(*gctxHandle)};
98 const Amg::Vector3D globPos = segment->position();
99 const Amg::Vector3D globDir = segment->direction();
101 const Amg::Vector3D locPos = globToLoc * globPos;
102 const Amg::Vector3D locDir = globToLoc.linear() * globDir;
103
104 const int secId = encodeId(static_cast<int8_t>(sector->chamberIndex()),
105 sector->side(),
106 sector->sector());
107 // time information
108 float seg_t0 = segment->t0();
109 float seg_t0error = segment->t0error();
110
111 // Fit quality information
112 float seg_chiSquared = segment->chiSquared();
113 float seg_numberDoF = segment->numberDoF();
114
115 // nHits
116 int seg_PrecisionHits = segment->nPrecisionHits();
117 int seg_PhiLayers = segment->nPhiLayers();
118 int seg_TrigEtaLayers = segment->nTrigEtaLayers();
119
120 // Detector information
121
122
123 // verbose some information to check
124 ATH_MSG_VERBOSE("Segment global position "<<Amg::toString(globPos)<<", direction: "<<Amg::toString(globDir));
125 ATH_MSG_VERBOSE("Segment local position: "<<Amg::toString(locPos)<<", direction: "<<Amg::toString(locDir));
126 ATH_MSG_VERBOSE("t0: "<<seg_t0<<" t0error: "<<seg_t0error);
127 ATH_MSG_VERBOSE("chiSquared: "<<seg_chiSquared<<" numberDoF: "<<seg_numberDoF);
128 ATH_MSG_VERBOSE("nPrecisionHits: "<<seg_PrecisionHits<<" nPhiLayers: "<<seg_PhiLayers<<" nTrigEtaLayers: "<<seg_TrigEtaLayers);
129
130 // save the segment information to the csv file
131 file<<secId<<delim;
132 file<<precCutOff(globPos.x())<<delim;
133 file<<precCutOff(globPos.y())<<delim;
134 file<<precCutOff(globPos.z())<<delim;
135 file<<precCutOff(globDir.x())<<delim;
136 file<<precCutOff(globDir.y())<<delim;
137 file<<precCutOff(globDir.z())<<delim;
138
139 file<<precCutOff(locPos.x())<<delim;
140 file<<precCutOff(locPos.y())<<delim;
141 file<<precCutOff(locPos.z())<<delim;
142 file<<precCutOff(locDir.x())<<delim;
143 file<<precCutOff(locDir.y())<<delim;
144 file<<precCutOff(locDir.z())<<delim;
145
146
147 file<<precCutOff(seg_t0)<<delim;
148 file<<precCutOff(seg_t0error)<<delim;
149 file<<precCutOff(seg_chiSquared)<<delim;
150 file<<seg_numberDoF<<delim;
151 file<<seg_PrecisionHits<<delim;
152 file<<seg_PhiLayers<<delim;
153 file<<seg_TrigEtaLayers<<delim;
154 file<<std::endl;
155 }
156 return StatusCode::SUCCESS;
157}
158
159}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_WARNING(x)
const ServiceHandle< StoreGateSvc > & detStore() const
A spectrometer sector forms the envelope of all chambers that are placed in the same MS sector & laye...
int8_t side() const
Returns the side of the MS-sector 1 -> A side ; -1 -> C side.
Amg::Transform3D globalToLocalTrans(const ActsTrk::GeometryContext &gctx) const
Returns the global -> local transformation from the ATLAS global.
int sector() const
Returns the sector of the MS-sector.
Muon::MuonStationIndex::ChIndex chamberIndex() const
Returns the chamber index scheme.
const MuonGMR4::SpectrometerSector * msSector(const xAOD::MuonSegment &segment) const
SG::ReadHandleKey< xAOD::MuonSegmentContainer > m_inSegmentKey
SG::ReadHandleKey< ActsTrk::GeometryContext > m_geoCtxKey
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
ServiceHandle< Muon::IMuonEDMHelperSvc > m_edmHelperSvc
const MuonGMR4::MuonDetectorManager * m_detMgr
This is the common class for 3D segments used in the muon spectrometer.
const ElementLink< ::Trk::SegmentCollection > & muonSegment() const
std::string toString(const Translation3D &translation, int precision=4)
GeoPrimitvesToStringConverter.
Eigen::Affine3d Transform3D
Eigen::Matrix< double, 3, 1 > Vector3D
This header ties the generic definitions in this package.
std::unordered_set< const xAOD::MuonSimHit * > getMatchingSimHits(const xAOD::MuonSegment &segment)
: Returns all sim hits matched to a xAOD::MuonSegment
const T * get(const ReadCondHandleKey< T > &key, const EventContext &ctx)
Convenience function to retrieve an object given a ReadCondHandleKey.
MuonSegmentContainer_v1 MuonSegmentContainer
Definition of the current "MuonSegment container version".
MuonSegment_v1 MuonSegment
Reference the current persistent version:
TFile * file