ATLAS Offline Software
Loading...
Searching...
No Matches
TruthTrackRecordsAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
7
14
15namespace {
16
17 constexpr float dummy_val = -1.;
18
19 struct RecordPars {
20 RecordPars() = default;
21 RecordPars(Amg::Vector3D&& pos_, Amg::Vector3D&& mom_):
22 pos{std::move(pos_)},
23 mom{std::move(mom_)}{}
24 RecordPars(const CLHEP::Hep3Vector& pos_, const CLHEP::Hep3Vector& mom_):
25 pos{pos_.x(), pos_.y(), pos_.z()},
26 mom{mom_.x(), mom_.y(), mom_.z()}{
27 }
28 const Amg::Vector3D pos{Amg::Vector3D::Zero()};
29 const Amg::Vector3D mom{Amg::Vector3D::Zero()};
30
31 std::string record_name{};
32 size_t idx{0};
33 const Trk::TrackingVolume* volume{nullptr};
34 };
35
36} // namespace
37
38namespace Muon {
39
40 template <typename T>
42 const std::string& keyName) const{
44 const std::string fullKey = recordKey.key() + "_" + keyName;
45 writeKey.emplace_back(m_muonTruth, fullKey);
46 }
47 return writeKey.initialize();
48 }
49
50 // Initialize method:
74
75 // Execute method:
76 StatusCode TruthTrackRecordsAlg::execute(const EventContext& ctx) const {
77 // skip if no input data found
78 SG::ReadHandle muonTruthContainer(m_muonTruth, ctx);
79 ATH_CHECK(muonTruthContainer.isPresent());
80
81 SummaryDecors myDecors;
97
98 // loop over truth coll
99 for (const xAOD::TruthParticle* truthParticle : *muonTruthContainer) {
100 ATH_CHECK(addTrackRecords(ctx, *truthParticle, myDecors));
101 }
102 return StatusCode::SUCCESS;
103 }
104
105 StatusCode TruthTrackRecordsAlg::addTrackRecords(const EventContext& ctx, const xAOD::TruthParticle& truthParticle, SummaryDecors& myDecors) const {
106
107 // first loop over track records, store parameters at the different positions
108 const xAOD::TruthVertex* vertex = truthParticle.prodVtx();
109 const Trk::TrackingGeometry* trackingGeometry = m_extrapolator->trackingGeometry();
110
111 std::vector<RecordPars> parameters;
112 if (vertex) {
113 parameters.emplace_back(Amg::Vector3D{vertex->x(), vertex->y(), vertex->z()},
114 Amg::Vector3D{truthParticle.px(), truthParticle.py(), truthParticle.pz()});
115 }
116 else {parameters.emplace_back();}
117
118 size_t idx{0};
119 for (SG::ReadHandle<TrackRecordCollection>& trackRecordCollection : m_trackRecords.makeHandles(ctx)) {
120 ATH_CHECK(trackRecordCollection.isPresent());
121
122 const std::string r_name = trackRecordCollection.key();
123 float& x = (*myDecors.xDecor[idx])(truthParticle);
124 float& y = (*myDecors.yDecor[idx])(truthParticle);
125 float& z = (*myDecors.zDecor[idx])(truthParticle);
126 float& px = (*myDecors.pxDecor[idx])(truthParticle);
127 float& py = (*myDecors.pyDecor[idx])(truthParticle);
128 float& pz = (*myDecors.pzDecor[idx])(truthParticle);
129
130 char& found_truth = (*myDecors.matchedDecor[idx])(truthParticle);
131
132 x = y = z = px = py = pz = dummy_val;
133 found_truth = false;
134
135 // Need to always make these, to avoid crashes later
136 float& ex = (*myDecors.exDecor[idx])(truthParticle);
137 float& ey = (*myDecors.eyDecor[idx])(truthParticle);
138 float& ez = (*myDecors.ezDecor[idx])(truthParticle);
139 float& epx = (*myDecors.epxDecor[idx])(truthParticle);
140 float& epy = (*myDecors.epyDecor[idx])(truthParticle);
141 float& epz = (*myDecors.epzDecor[idx])(truthParticle);
142
143 ex = ey = ez = epx = epy = epz = dummy_val;
144
146 for (const TrackRecord& trackRecord : *trackRecordCollection) {
147 if (!HepMC::is_sim_descendant(&trackRecord,&truthParticle)) continue;
148 CLHEP::Hep3Vector pos = trackRecord.GetPosition();
149 CLHEP::Hep3Vector mom = trackRecord.GetMomentum();
150 ATH_MSG_VERBOSE("Found associated " << r_name << " pt " << mom.perp() << " position: r " << pos.perp() << " z " << pos.z());
151 x = pos.x(); y = pos.y(); z = pos.z();
152 px = mom.x(); py = mom.y(); pz = mom.z();
153 parameters.emplace_back(pos, mom);
154 found_truth = true;
155 break;
156 }
157
159 if (!found_truth) {parameters.emplace_back();}
161 RecordPars& r_pars = parameters.back();
162 r_pars.record_name = r_name;
163 r_pars.idx = idx++;
164
166 if (!found_truth) continue;
167 const Trk::TrackingVolume* volume = nullptr;
168 if (r_name == "CaloEntryLayer")
169 volume = trackingGeometry->trackingVolume("InDet::Containers::InnerDetector");
170 else if (r_name == "MuonEntryLayer")
171 volume = trackingGeometry->trackingVolume("Calo::Container");
172 else if (r_name == "MuonExitLayer")
173 volume = trackingGeometry->trackingVolume("Muon::Containers::MuonSystem");
174 else {
175 ATH_MSG_WARNING("no destination surface for track record "<<r_name);
176 }
177 r_pars.volume = volume;
178 }
179
181 AmgSymMatrix(5) cov{AmgSymMatrix(5)::Identity()};
182 cov(0, 0) = cov(1, 1) = 1e-3;
183 cov(2, 2) = cov(3, 3) = 1e-6;
184 cov(4, 4) = 1e-3 / truthParticle.p4().P();
185 for (unsigned int i = 0; i < parameters.size() - 1; ++i) {
186 const RecordPars& start_pars = parameters[i];
187 const RecordPars& end_pars = parameters[i+1];
189 if ( (!start_pars.record_name.empty() && !start_pars.volume) || !end_pars.volume) continue;
190 Trk::CurvilinearParameters pars(start_pars.pos, start_pars.mom, truthParticle.charge(), cov);
191
192 const Trk::TrackingVolume* volume = end_pars.volume;
193 const std::string& r_name = end_pars.record_name;
194
195 float& ex = (*myDecors.exDecor[end_pars.idx])(truthParticle);
196 float& ey = (*myDecors.eyDecor[end_pars.idx])(truthParticle);
197 float& ez = (*myDecors.ezDecor[end_pars.idx])(truthParticle);
198 float& epx = (*myDecors.epxDecor[end_pars.idx])(truthParticle);
199 float& epy = (*myDecors.epyDecor[end_pars.idx])(truthParticle);
200 float& epz = (*myDecors.epzDecor[end_pars.idx])(truthParticle);
201
202 std::vector<float>& covMat = (*myDecors.ecovDecor[end_pars.idx])(truthParticle);
203
204 std::unique_ptr<Trk::TrackParameters> exPars{
205 m_extrapolator->extrapolateToVolume(ctx, pars, *volume, Trk::alongMomentum, Trk::muon)};
206 if (! exPars || !exPars->covariance() || !Amg::hasPositiveDiagElems(*exPars->covariance())) {
207 ATH_MSG_VERBOSE("Extrapolation to "<<r_name<<" failed. ");
208 continue;
209 }
210 (*myDecors.eisDecor[end_pars.idx])(truthParticle) = true;
211 ex = exPars->position().x();
212 ey = exPars->position().y();
213 ez = exPars->position().z();
214 epx = exPars->momentum().x();
215 epy = exPars->momentum().y();
216 epz = exPars->momentum().z();
217 Amg::compress(*exPars->covariance(), covMat);
218
219 const double errorp = Amg::error(*exPars->covariance(), Trk::qOverP);
220 ATH_MSG_VERBOSE( " Extrapolated to " << r_name << std::endl
221 << " truth: r " << end_pars.pos.perp() << " z "
222 << end_pars.pos.z() << " p "
223 << end_pars.mom.mag() << std::endl
224 << " extrp: r " << exPars->position().perp() << " z "
225 << exPars->position().z() << " p "
226 << exPars->momentum().mag() << " res p "
227 << (end_pars.mom.mag() -
228 exPars->momentum().mag())
229 << " error " << errorp << " cov "
230 << (*exPars->covariance())(Trk::qOverP, Trk::qOverP)
231 << " pull p "
232 << (end_pars.mom.mag() -
233 exPars->momentum().mag()) /
234 errorp);
235 }
236 return StatusCode::SUCCESS;
237 }
238
239} // namespace Muon
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_WARNING(x)
#define AmgSymMatrix(dim)
#define y
#define x
#define z
virtual StatusCode initialize() override
SG::ReadHandleKey< xAOD::TruthParticleContainer > m_muonTruth
Key of the container of truth muons that will be decorated.
StatusCode fillWriteDecorator(SG::WriteDecorHandleKeyArray< xAOD::TruthParticleContainer, T > &writeKey, const std::string &keyName) const
This function fills the arrays of write-decorator keys and initializes them.
StatusCode addTrackRecords(const EventContext &ctx, const xAOD::TruthParticle &truthParticle, SummaryDecors &myDecors) const
addTrackRecords is the actual function that decorates muons with track records.
virtual StatusCode execute(const EventContext &ctx) const override
ToolHandle< Trk::IExtrapolator > m_extrapolator
Extrapolation tool handle.
SG::ReadHandleKeyArray< TrackRecordCollection > m_trackRecords
Keys of the containers of track records in different detector positions.
Property holding a SG store/key/clid from which a ReadHandle is made.
bool isPresent() const
Is the referenced object present in SG?
StatusCode initialize(bool used=true)
forward the initialization to the member VarHandleKeys
The TrackingGeometry class is the owner of the constructed TrackingVolumes.
const TrackingVolume * trackingVolume(const std::string &name) const
return the tracking Volume by name, 0 if it doesn't exist
Full Volume description used in Tracking, it inherits from Volume to get the geometrical structure,...
float px() const
The x component of the particle's momentum.
const TruthVertex_v1 * prodVtx() const
The production vertex of this particle.
float py() const
The y component of the particle's momentum.
double charge() const
Physical charge.
virtual FourMom_t p4() const override final
The full 4-momentum of the particle.
float pz() const
The z component of the particle's momentum.
void compress(const AmgSymMatrix(N) &covMatrix, std::vector< float > &vec)
double error(const Amg::MatrixX &mat, int index)
return diagonal error of the matrix caller should ensure the matrix is symmetric and the index is in ...
bool hasPositiveDiagElems(const AmgSymMatrix(N) &mat)
Returns true if all diagonal elements of the covariance matrix are finite aka sane in the above defin...
Eigen::Matrix< double, 3, 1 > Vector3D
bool is_sim_descendant(const T1 &p1, const T2 &p2)
Method to check if the first particle is a descendant of the second in the simulation,...
NRpcCablingAlg reads raw condition data and writes derived condition data to the condition store.
DecorHandlePtrVec_t< ContType, DataType > makeHandles(const EventContext &ctx, const SG::WriteDecorHandleKeyArray< ContType > &keys, const DataType defVal={})
Definition DecorUtils.h:36
DecorHandleKeyArray< WriteDecorHandle< T, S >, WriteDecorHandleKey< T >, Gaudi::DataHandle::Writer > WriteDecorHandleKeyArray
@ alongMomentum
CurvilinearParametersT< TrackParametersDim, Charged, PlaneSurface > CurvilinearParameters
@ qOverP
perigee
Definition ParamDefs.h:67
TruthVertex_v1 TruthVertex
Typedef to implementation.
Definition TruthVertex.h:15
TruthParticle_v1 TruthParticle
Typedef to implementation.