ATLAS Offline Software
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 
14 
15 #include <fstream>
16 #include <TString.h>
17 
18 #include <unordered_map>
19 
20 
21 namespace {
22  constexpr int encodeId(const int8_t stName, const int8_t stEta,
23  const int8_t sector) {
24  return (sector <<16 |stEta << 8| stName);
25  }
26  constexpr double precCutOff(const double value, const double cutOff = 1.e-15) {
27  return std::abs(value) > cutOff ? value : 0.;
28  }
29 
30 }
31 
32 namespace MuonR4{
33 
34 
38  ATH_CHECK(m_idHelperSvc.retrieve());
39  ATH_CHECK(m_edmHelperSvc.retrieve());
41  return StatusCode::SUCCESS;
42 }
43 
45  const auto truthHits = getMatchingSimHits(segment);
46  if (truthHits.size()) {
47  return m_detMgr->getSectorEnvelope((*truthHits.begin())->identify());
48  }
49  if (segment.muonSegment().isValid()) {
50  return m_detMgr->getSectorEnvelope(m_edmHelperSvc->chamberId(static_cast<const Muon::MuonSegment&>(**segment.muonSegment())));
51  }
52  ATH_MSG_WARNING("No matching hits were found. Neither the simulated ones or the reconstructed");
53  return nullptr;
54 }
55 
57  const EventContext & ctx = Gaudi::Hive::currentContext();
58  std::ofstream file{std::string(Form("event%09zu-",++m_event))+"MuonTruthSegment.csv"};
59  constexpr std::string_view delim = ",";
60  file<<"sectorId"<<delim;
61  file<<"globalPositionX"<<delim;
62  file<<"globalPositionY"<<delim;
63  file<<"globalPositionZ"<<delim;
64 
65  file<<"globalDirectionX"<<delim;
66  file<<"globalDirectionY"<<delim;
67  file<<"globalDirectionZ"<<delim;
68 
69 
70  file<<"localPositionX"<<delim;
71  file<<"localPositionY"<<delim;
72  file<<"localPositionZ"<<delim;
73 
74  file<<"localDirectionX"<<delim;
75  file<<"localDirectionY"<<delim;
76  file<<"localDirectionZ"<<delim;
77 
78  file<<"time"<<delim;
79  file<<"timeError"<<delim;
80  file<<"chiSquared"<<delim;
81  file<<"nDoF"<<delim;
82  file<<"precisionHits"<<delim;
83  file<<"phiLayers"<<delim;
84  file<<"trigEtaLayers"<<delim;
85  file<<std::endl;
86 
87  const xAOD::MuonSegmentContainer* readTruthSegment{nullptr};
88  ATH_CHECK(SG::get(readTruthSegment, m_inSegmentKey, ctx));
89 
90  const ActsGeometryContext* gctxHandle{nullptr};
91  ATH_CHECK(SG::get(gctxHandle, m_geoCtxKey, ctx));
92 
93  for (const xAOD::MuonSegment* segment : *readTruthSegment) {
95  if (!sector) {
96  continue;
97  }
98  const Amg::Transform3D globToLoc{sector->globalToLocalTrans(*gctxHandle)};
100  const Amg::Vector3D globPos = segment->position();
101  const Amg::Vector3D globDir = segment->direction();
103  const Amg::Vector3D locPos = globToLoc * globPos;
104  const Amg::Vector3D locDir = globToLoc.linear() * globDir;
105 
106  const int secId = encodeId(static_cast<int8_t>(sector->chamberIndex()),
107  sector->side(),
108  sector->sector());
109  // time information
110  float seg_t0 = segment->t0();
111  float seg_t0error = segment->t0error();
112 
113  // Fit quality information
114  float seg_chiSquared = segment->chiSquared();
115  float seg_numberDoF = segment->numberDoF();
116 
117  // nHits
118  int seg_PrecisionHits = segment->nPrecisionHits();
119  int seg_PhiLayers = segment->nPhiLayers();
120  int seg_TrigEtaLayers = segment->nTrigEtaLayers();
121 
122  // Detector information
123 
124 
125  // verbose some information to check
126  ATH_MSG_VERBOSE("Segment global position "<<Amg::toString(globPos)<<", direction: "<<Amg::toString(globDir));
127  ATH_MSG_VERBOSE("Segment local position: "<<Amg::toString(locPos)<<", direction: "<<Amg::toString(locDir));
128  ATH_MSG_VERBOSE("t0: "<<seg_t0<<" t0error: "<<seg_t0error);
129  ATH_MSG_VERBOSE("chiSquared: "<<seg_chiSquared<<" numberDoF: "<<seg_numberDoF);
130  ATH_MSG_VERBOSE("nPrecisionHits: "<<seg_PrecisionHits<<" nPhiLayers: "<<seg_PhiLayers<<" nTrigEtaLayers: "<<seg_TrigEtaLayers);
131 
132  // save the segment information to the csv file
133  file<<secId<<delim;
134  file<<precCutOff(globPos.x())<<delim;
135  file<<precCutOff(globPos.y())<<delim;
136  file<<precCutOff(globPos.z())<<delim;
137  file<<precCutOff(globDir.x())<<delim;
138  file<<precCutOff(globDir.y())<<delim;
139  file<<precCutOff(globDir.z())<<delim;
140 
141  file<<precCutOff(locPos.x())<<delim;
142  file<<precCutOff(locPos.y())<<delim;
143  file<<precCutOff(locPos.z())<<delim;
144  file<<precCutOff(locDir.x())<<delim;
145  file<<precCutOff(locDir.y())<<delim;
146  file<<precCutOff(locDir.z())<<delim;
147 
148 
149  file<<precCutOff(seg_t0)<<delim;
150  file<<precCutOff(seg_t0error)<<delim;
151  file<<precCutOff(seg_chiSquared)<<delim;
152  file<<seg_numberDoF<<delim;
153  file<<seg_PrecisionHits<<delim;
154  file<<seg_PhiLayers<<delim;
155  file<<seg_TrigEtaLayers<<delim;
156  file<<std::endl;
157  }
158  return StatusCode::SUCCESS;
159 }
160 
161 }
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
MuonSimHitHelpers.h
MuonGMR4::SpectrometerSector::sector
int sector() const
Returns the sector of the MS-sector.
Definition: SpectrometerSector.cxx:64
MuonGMR4::SpectrometerSector::side
int8_t side() const
Returns the side of the MS-sector 1 -> A side ; -1 -> C side.
Definition: SpectrometerSector.cxx:57
MuonGMR4::SpectrometerSector
A spectrometer sector forms the envelope of all chambers that are placed in the same MS sector & laye...
Definition: SpectrometerSector.h:40
MuonR4::TruthSegmentCsvDumperAlg::m_idHelperSvc
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
Definition: TruthSegmentCsvDumperAlg.h:30
Muon::MuonStationIndex::stName
const std::string & stName(StIndex index)
convert StIndex into a string
Definition: MuonStationIndex.cxx:104
MuonR4::TruthSegmentCsvDumperAlg::m_detMgr
const MuonGMR4::MuonDetectorManager * m_detMgr
Definition: TruthSegmentCsvDumperAlg.h:33
xAOD::MuonSegment_v1
Class describing a MuonSegment.
Definition: MuonSegment_v1.h:33
athena.value
value
Definition: athena.py:124
MuonR4::TruthSegmentCsvDumperAlg::msSector
const MuonGMR4::SpectrometerSector * msSector(const xAOD::MuonSegment &segment) const
Definition: TruthSegmentCsvDumperAlg.cxx:44
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
SpectrometerSector.h
MatrixUtils.h
AthCommonDataStore< AthCommonMsg< Algorithm > >::detStore
const ServiceHandle< StoreGateSvc > & detStore() const
The standard StoreGateSvc/DetectorStore Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:95
MuonGMR4::SpectrometerSector::chamberIndex
Muon::MuonStationIndex::ChIndex chamberIndex() const
Returns the chamber index scheme.
Definition: SpectrometerSector.cxx:62
Amg::toString
std::string toString(const Translation3D &translation, int precision=4)
GeoPrimitvesToStringConverter.
Definition: GeoPrimitivesToStringConverter.h:40
MuonR4::TruthSegmentCsvDumperAlg::execute
StatusCode execute() override
Definition: TruthSegmentCsvDumperAlg.cxx:56
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
MuonGMR4::SpectrometerSector::globalToLocalTrans
Amg::Transform3D globalToLocalTrans(const ActsGeometryContext &gctx) const
Returns the global -> local transformation from the ATLAS global.
Definition: SpectrometerSector.cxx:78
Amg::Transform3D
Eigen::Affine3d Transform3D
Definition: GeoPrimitives.h:46
file
TFile * file
Definition: tile_monitor.h:29
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
MuonR4::TruthSegmentCsvDumperAlg::m_inSegmentKey
SG::ReadHandleKey< xAOD::MuonSegmentContainer > m_inSegmentKey
Definition: TruthSegmentCsvDumperAlg.h:28
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
DataVector
Derived DataVector<T>.
Definition: DataVector.h:794
ActsGeometryContext
Include the GeoPrimitives which need to be put first.
Definition: ActsGeometryContext.h:27
MuonR4::TruthSegmentCsvDumperAlg::m_event
size_t m_event
Definition: TruthSegmentCsvDumperAlg.h:34
Amg::Vector3D
Eigen::Matrix< double, 3, 1 > Vector3D
Definition: GeoPrimitives.h:47
python.copyTCTOutput.locDir
locDir
Definition: copyTCTOutput.py:112
MuonR4
This header ties the generic definitions in this package.
Definition: HoughEventData.h:16
TruthSegmentCsvDumperAlg.h
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
MuonR4::TruthSegmentCsvDumperAlg::m_edmHelperSvc
ServiceHandle< Muon::IMuonEDMHelperSvc > m_edmHelperSvc
Definition: TruthSegmentCsvDumperAlg.h:31
MuonSegment.h
MuonR4::TruthSegmentCsvDumperAlg::m_geoCtxKey
SG::ReadHandleKey< ActsGeometryContext > m_geoCtxKey
Definition: TruthSegmentCsvDumperAlg.h:27
Muon::MuonSegment
Definition: MuonSpectrometer/MuonReconstruction/MuonRecEvent/MuonSegment/MuonSegment/MuonSegment.h:45
MuonReadoutElement.h
MuonR4::TruthSegmentCsvDumperAlg::initialize
StatusCode initialize() override
Definition: TruthSegmentCsvDumperAlg.cxx:35
MuonR4::getMatchingSimHits
std::unordered_set< const xAOD::MuonSimHit * > getMatchingSimHits(const xAOD::MuonSegment &segment)
: Returns all sim hits matched to a xAOD::MuonSegment
Definition: MuonSimHitHelpers.cxx:27
MuonGMR4::MuonDetectorManager::getSectorEnvelope
const SpectrometerSector * getSectorEnvelope(const Identifier &channelId) const
Retrieves the spectrometer envelope enclosing the channel's readout element.
Definition: MuonPhaseII/MuonDetDescr/MuonReadoutGeometryR4/src/MuonDetectorManager.cxx:198
NSWL1::PadTriggerAdapter::segment
Muon::NSW_PadTriggerSegment segment(const NSWL1::PadTrigger &data)
Definition: PadTriggerAdapter.cxx:5