ATLAS Offline Software
Loading...
Searching...
No Matches
TruthSegmentWriter.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3 */
5
10
11#include "Acts/Surfaces/PlaneSurface.hpp"
12#include "Acts/Definitions/Units.hpp"
13#include "Acts/Geometry/GeometryIdentifier.hpp"
14#include "Acts/Surfaces/Surface.hpp"
15
16using namespace Acts::UnitLiterals;
17using namespace MuonVal;
18using namespace MuonR4;
19namespace MuonValR4{
20 constexpr float inDeg(const float rad) {
21 return rad / 1._degree;
22 }
23
25 ATH_CHECK(m_segmentKey.initialize());
26 ATH_CHECK(m_truthLinkKey.initialize());
27 ATH_CHECK(m_idHelperSvc.retrieve());
28 ATH_CHECK(detStore()->retrieve(m_detMgr));
30
31 m_truthTrks = std::make_unique<IParticleFourMomBranch>(m_tree, "Muons");
32 m_truthTrks->addVariable<int>(-1, "truthOrigin");
33 m_truthTrks->addVariable<int>(-1, "truthType");
34
36 m_truthTrks->addVariable(
38 std::vector<unsigned short>>>(m_tree.tree(),
39 std::format("{:}_truthSegLinks", m_truthTrks->name()), [&] (const xAOD::TruthParticle& p){
40 std::vector<unsigned short> idx{};
41 for (const xAOD::MuonSegment* truthSeg: getTruthSegments(p)){
42 idx.push_back(m_segmentBranches->push_back(*truthSeg));
43 }
44 return idx;
45 }));
47 m_truthTrks->addVariable(
49 std::format("{:}_nTruthSegments", m_truthTrks->name()), [&] (const xAOD::TruthParticle& p) -> unsigned short {
50 return getTruthSegments(p).size();
51 }));
52
53 m_tree.addBranch(m_truthTrks);
54
55 const std::string segName = "Segments";
56 m_segmentBranches = std::make_unique<MuonPRDTest::SegmentVariables>(m_tree, m_segmentKey.key(),
57 segName, msgLevel());
58
59 m_segmentFrames = std::make_unique<MuonVal::CoordSystemsBranch>(m_tree, std::format("{:}_localToGlobal", segName));
60 m_tree.addBranch(m_segmentFrames);
61 m_segmentBranches->addVariable(std::make_unique<MuonVal::GenericAuxDecorationBranch<unsigned short>>(m_tree,
62 std::format("{:}_truthLink", segName),[this](const SG::AuxElement* aux){
63 const auto* seg = static_cast<const xAOD::MuonSegment*>(aux);
65 m_truthTrks->push_back(truthP);
66 unsigned short linkIdx = m_truthTrks->find(truthP);
67 return linkIdx;
68 }));
69
70 m_segmentBranches->addVariable(std::make_unique<MuonVal::GenericAuxDecorationBranch<
71 std::vector<Acts::GeometryIdentifier::Value>>>(m_tree,
72 std::format("{:}_hitGeoIds", segName),[this](const SG::AuxElement* aux){
73 const auto* seg = static_cast<const xAOD::MuonSegment*>(aux);
74 std::vector<Acts::GeometryIdentifier::Value> geoIds{};
75 for (const xAOD::MuonSimHit* hit : MuonR4::getMatchingSimHits(*seg)) {
76 geoIds.push_back(getSurface(hit->identify()).geometryId().value());
77 }
78 std::ranges::sort(geoIds);
79 return geoIds;
80 }));
81
82 m_segmentBranches->addVariable(std::make_unique<MuonVal::GenericAuxDecorationBranch<
83 std::vector<float>>>(m_tree,
84 std::format("{:}_localSegPars", segName),[](const SG::AuxElement* aux){
85 using namespace MuonR4::SegmentFit;
86 const auto* seg = static_cast<const xAOD::MuonSegment*>(aux);
88 std::vector<float> ret{pars.begin(), pars.end()};
89 ret[Acts::toUnderlying(ParamDefs::phi)] = inDeg(ret[Acts::toUnderlying(ParamDefs::phi)]);
90 ret[Acts::toUnderlying(ParamDefs::theta)] = inDeg(ret[Acts::toUnderlying(ParamDefs::theta)]);
91 return ret;
92 }));
93
94 m_tree.addBranch(m_segmentBranches);
95 ATH_CHECK(m_tree.init(this));
96 return StatusCode::SUCCESS;
97 }
98 StatusCode TruthSegmentWriter::execute(const EventContext& ctx) {
99 m_eventId = ctx.eventID().event_number();
100 const xAOD::MuonSegmentContainer* segments{nullptr};
101 ATH_CHECK(SG::get(segments, m_segmentKey, ctx));
102 const Acts::GeometryContext tgContext = m_trackingGeometryTool->getGeometryContext(ctx).context();
103 for (const xAOD::MuonSegment* segment: *segments) {
104 m_segmentBranches->push_back(*segment);
105 m_segmentFrames->push_back(getSurface(*segment).localToGlobalTransform(tgContext));
106 }
107
108 ATH_CHECK(m_tree.fill(ctx));
109 return StatusCode::SUCCESS;
110 }
112 ATH_CHECK(m_tree.write());
113 return StatusCode::SUCCESS;
114 }
115 const Acts::Surface& TruthSegmentWriter::getSurface(const Identifier& simHitId) const {
116 const MuonGMR4::MuonReadoutElement* re = m_detMgr->getReadoutElement(simHitId);
117 const IdentifierHash surfHash = re->detectorType() == ActsTrk::DetectorType::Mdt ?
118 re->measurementHash(simHitId) : re->layerHash(simHitId);
119 return re->surface(surfHash);
120 }
121 const Acts::Surface& TruthSegmentWriter::getSurface(const xAOD::MuonSegment& segment) const {
122 return m_detMgr->getSectorEnvelope(segment.chamberIndex(), segment.sector(),
123 segment.etaIndex())->surface();
124 }
125}
const std::regex re(r_e)
#define ATH_CHECK
Evaluate an expression and check for errors.
const ServiceHandle< StoreGateSvc > & detStore() const
StatusCode addVariable(const std::string &name, T *&ptr, const std::string &docstring="")
Add a variable to the tuple.
This is a "hash" representation of an Identifier.
MuonReadoutElement is an abstract class representing the geometry of a muon detector.
virtual StatusCode initialize() override final
virtual StatusCode execute(const EventContext &ctx) override final
Execute method.
virtual StatusCode finalize() override final
std::shared_ptr< MuonVal::IParticleFourMomBranch > m_truthTrks
The pointer to the associated truth particles.
SG::ReadHandleKey< xAOD::MuonSegmentContainer > m_segmentKey
Data dependency on the truth segment container.
PublicToolHandle< ActsTrk::ITrackingGeometryTool > m_trackingGeometryTool
The tool handle of the tracking geometry tool.
std::shared_ptr< MuonVal::CoordSystemsBranch > m_segmentFrames
Pointer to the associated track vari.
MuonVal::ScalarBranch< std::uint32_t > & m_eventId
The event number in this event.
const MuonGMR4::MuonDetectorManager * m_detMgr
The muon detector manager.
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
Service handle towards the IdHelper svc.
MuonVal::MuonTesterTree m_tree
instance to the Event tree
std::shared_ptr< MuonPRDTest::SegmentVariables > m_segmentBranches
The pointer to the segment variables.
const Acts::Surface & getSurface(const Identifier &simHitId) const
Retrieves the surface associated with a sim hit identifier.
SG::ReadDecorHandleKey< xAOD::MuonSegmentContainer > m_truthLinkKey
Data dependency on the truth particle link.
Generic branch object where the information is evaluated by a std::function instead reading it from t...
::Muon::MuonStationIndex::ChIndex chamberIndex() const
Returns the chamber index.
int etaIndex() const
Returns the eta index, which corresponds to stationEta in the offline identifiers (and the ).
@ Mdt
MuonSpectrometer.
ID3PD * m_tree
Pointer to the ID3PD object used.
Parameters localSegmentPars(const xAOD::MuonSegment &seg)
Returns the localSegPars decoration from a xAODMuon::Segment.
This header ties the generic definitions in this package.
const xAOD::TruthParticle * getTruthMatchedParticle(const xAOD::MuonSegment &segment)
Returns the particle truth-matched to the segment.
std::unordered_set< const xAOD::MuonSimHit * > getMatchingSimHits(const xAOD::MuonSegment &segment)
: Returns all sim hits matched to a xAOD::MuonSegment
std::vector< const xAOD::MuonSegment * > getTruthSegments(const xAOD::TruthParticle &truthMuon)
Returns the segments associated to the truth muon.
Lightweight algorithm to read xAOD MDT sim hits and (fast-digitised) drift circles from SG and fill a...
constexpr float inDeg(const float rad)
Class to store array like branches into the n-tuples.
Definition HitValAlg.cxx:19
AuxElement(SG::AuxVectorData *container, size_t index)
Base class for elements of a container that can have aux data.
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".
MuonSimHit_v1 MuonSimHit
Defined the version of the MuonSimHit.
Definition MuonSimHit.h:12
TruthParticle_v1 TruthParticle
Typedef to implementation.
MuonSegment_v1 MuonSegment
Reference the current persistent version: