Loading [MathJax]/extensions/tex2jax.js
ATLAS Offline Software
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 
5 #include "TruthTrackRecordsAlg.h"
7 
10 #include "xAODTruth/TruthVertex.h"
14 
15 namespace {
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  }
30 
31  std::string record_name{};
32  size_t idx{0};
33  const Trk::TrackingVolume* volume{nullptr};
34  };
35 
36 } // namespace
37 
38 namespace 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:
53  ATH_CHECK(m_trackRecords.initialize());
54 
70 
71  ATH_CHECK(m_extrapolator.retrieve());
72  return StatusCode::SUCCESS;
73  }
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;
82  myDecors.xDecor = makeHandles<float>(ctx, m_muonSnapShotX);
83  myDecors.yDecor = makeHandles<float>(ctx, m_muonSnapShotY);
84  myDecors.zDecor = makeHandles<float>(ctx, m_muonSnapShotZ);
85  myDecors.pxDecor = makeHandles<float>(ctx, m_muonSnapShotPx);
86  myDecors.pyDecor = makeHandles<float>(ctx, m_muonSnapShotPy);
87  myDecors.pzDecor = makeHandles<float>(ctx, m_muonSnapShotPz);
88  myDecors.matchedDecor = makeHandles<char>(ctx, m_muonSnapShotMtc);
89  myDecors.exDecor = makeHandles<float>(ctx, m_muonSnapShotEX);
90  myDecors.eyDecor = makeHandles<float>(ctx, m_muonSnapShotEY);
91  myDecors.ezDecor = makeHandles<float>(ctx, m_muonSnapShotEZ);
92  myDecors.epxDecor = makeHandles<float>(ctx, m_muonSnapShotEPx);
93  myDecors.epyDecor = makeHandles<float>(ctx, m_muonSnapShotEPy);
94  myDecors.epzDecor = makeHandles<float>(ctx, m_muonSnapShotEPz);
95  myDecors.ecovDecor = makeHandles<std::vector<float>>(ctx, m_muonSnapShotEcov);
96  myDecors.eisDecor = makeHandles<char>(ctx, m_muonSnapShotEis);
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
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
make_hlt_rep.pars
pars
Definition: make_hlt_rep.py:90
Muon::TruthTrackRecordsAlg::initialize
virtual StatusCode initialize() override
Definition: TruthTrackRecordsAlg.cxx:51
Amg::hasPositiveDiagElems
bool hasPositiveDiagElems(const AmgSymMatrix(N) &mat)
Returns true if all diagonal elements of the covariance matrix are finite aka sane in the above defin...
Definition: EventPrimitivesCovarianceHelpers.h:96
test_pyathena.px
px
Definition: test_pyathena.py:18
Muon::TruthTrackRecordsAlg::SummaryDecors::eyDecor
WriteDecorArray_f eyDecor
Definition: TruthTrackRecordsAlg.h:58
Amg::compress
void compress(const AmgSymMatrix(N) &covMatrix, std::vector< float > &vec)
Definition: EventPrimitivesHelpers.h:56
Muon::TruthTrackRecordsAlg::m_muonSnapShotEis
DecorKey_t m_muonSnapShotEis
Definition: TruthTrackRecordsAlg.h:121
Muon::TruthTrackRecordsAlg::m_muonSnapShotMtc
DecorKey_t m_muonSnapShotMtc
Definition: TruthTrackRecordsAlg.h:113
TrackParameters.h
xAOD::TruthParticle_v1::pz
float pz() const
The z component of the particle's momentum.
Muon::TruthTrackRecordsAlg::m_muonSnapShotEX
DecorKey_t m_muonSnapShotEX
Definition: TruthTrackRecordsAlg.h:114
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:67
Muon::TruthTrackRecordsAlg::execute
virtual StatusCode execute(const EventContext &ctx) const override
Definition: TruthTrackRecordsAlg.cxx:76
Muon::TruthTrackRecordsAlg::SummaryDecors::epzDecor
WriteDecorArray_f epzDecor
Definition: TruthTrackRecordsAlg.h:62
Muon::TruthTrackRecordsAlg::m_trackRecords
SG::ReadHandleKeyArray< TrackRecordCollection > m_trackRecords
Keys of the containers of track records in different detector positions.
Definition: TruthTrackRecordsAlg.h:96
EventPrimitivesHelpers.h
Muon::TruthTrackRecordsAlg::addTrackRecords
StatusCode addTrackRecords(const EventContext &ctx, const xAOD::TruthParticle &truthParticle, SummaryDecors &myDecors) const
addTrackRecords is the actual function that decorates muons with track records.
Definition: TruthTrackRecordsAlg.cxx:105
plotBeamSpotVxVal.cov
cov
Definition: plotBeamSpotVxVal.py:201
xAOD::TruthParticle_v1::px
float px() const
The x component of the particle's momentum.
Muon::TruthTrackRecordsAlg::SummaryDecors::epxDecor
WriteDecorArray_f epxDecor
Definition: TruthTrackRecordsAlg.h:60
SG::HandleKeyArray
Definition: StoreGate/StoreGate/HandleKeyArray.h:38
Trk::alongMomentum
@ alongMomentum
Definition: PropDirection.h:20
xAOD::TruthParticle_v1::py
float py() const
The y component of the particle's momentum.
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
SG::ReadHandleKey
Property holding a SG store/key/clid from which a ReadHandle is made.
Definition: StoreGate/StoreGate/ReadHandleKey.h:39
Muon::TruthTrackRecordsAlg::m_muonSnapShotEcov
DecorKey_t m_muonSnapShotEcov
Definition: TruthTrackRecordsAlg.h:120
Muon::TruthTrackRecordsAlg::m_muonSnapShotPz
DecorKey_t m_muonSnapShotPz
Definition: TruthTrackRecordsAlg.h:112
Muon
NRpcCablingAlg reads raw condition data and writes derived condition data to the condition store.
Definition: TrackSystemController.h:45
x
#define x
AmgSymMatrix
#define AmgSymMatrix(dim)
Definition: EventPrimitives.h:50
Muon::TruthTrackRecordsAlg::m_muonSnapShotEPx
DecorKey_t m_muonSnapShotEPx
Definition: TruthTrackRecordsAlg.h:117
Muon::TruthTrackRecordsAlg::m_muonSnapShotZ
DecorKey_t m_muonSnapShotZ
Definition: TruthTrackRecordsAlg.h:109
Muon::TruthTrackRecordsAlg::m_muonSnapShotPx
DecorKey_t m_muonSnapShotPx
Definition: TruthTrackRecordsAlg.h:110
Muon::TruthTrackRecordsAlg::m_muonSnapShotX
DecorKey_t m_muonSnapShotX
Definition: TruthTrackRecordsAlg.h:107
Muon::TruthTrackRecordsAlg::m_muonSnapShotEPy
DecorKey_t m_muonSnapShotEPy
Definition: TruthTrackRecordsAlg.h:118
ParticleGun_EoverP_Config.mom
mom
Definition: ParticleGun_EoverP_Config.py:63
DecorUtils.h
Muon::TruthTrackRecordsAlg::fillWriteDecorator
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.
Definition: TruthTrackRecordsAlg.cxx:41
Muon::TruthTrackRecordsAlg::SummaryDecors::exDecor
WriteDecorArray_f exDecor
Definition: TruthTrackRecordsAlg.h:57
Muon::TruthTrackRecordsAlg::SummaryDecors::eisDecor
WriteDecorArray_b eisDecor
Definition: TruthTrackRecordsAlg.h:64
lumiFormat.i
int i
Definition: lumiFormat.py:85
z
#define z
Muon::TruthTrackRecordsAlg::SummaryDecors::ezDecor
WriteDecorArray_f ezDecor
Definition: TruthTrackRecordsAlg.h:59
Muon::TruthTrackRecordsAlg::m_muonTruth
SG::ReadHandleKey< xAOD::TruthParticleContainer > m_muonTruth
Key of the container of truth muons that will be decorated.
Definition: TruthTrackRecordsAlg.h:92
Trk::TrackingGeometry
Definition: TrackingGeometry.h:67
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
Muon::TruthTrackRecordsAlg::SummaryDecors::zDecor
WriteDecorArray_f zDecor
Definition: TruthTrackRecordsAlg.h:52
xAOD::TruthParticle_v1
Class describing a truth particle in the MC record.
Definition: TruthParticle_v1.h:37
Muon::TruthTrackRecordsAlg::SummaryDecors::pxDecor
WriteDecorArray_f pxDecor
Definition: TruthTrackRecordsAlg.h:53
Amg::pz
@ pz
Definition: GeoPrimitives.h:40
Muon::TruthTrackRecordsAlg::m_muonSnapShotEZ
DecorKey_t m_muonSnapShotEZ
Definition: TruthTrackRecordsAlg.h:116
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
plotmaker.keyName
keyName
Definition: plotmaker.py:145
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
Trk::CurvilinearParametersT
Definition: CurvilinearParametersT.h:48
Trk::muon
@ muon
Definition: ParticleHypothesis.h:28
Muon::TruthTrackRecordsAlg::SummaryDecors::matchedDecor
WriteDecorArray_b matchedDecor
Definition: TruthTrackRecordsAlg.h:56
Muon::TruthTrackRecordsAlg::m_muonSnapShotEY
DecorKey_t m_muonSnapShotEY
Definition: TruthTrackRecordsAlg.h:115
TruthVertex.h
xAOD::TruthParticle_v1::prodVtx
const TruthVertex_v1 * prodVtx() const
The production vertex of this particle.
Definition: TruthParticle_v1.cxx:80
Muon::TruthTrackRecordsAlg::m_muonSnapShotPy
DecorKey_t m_muonSnapShotPy
Definition: TruthTrackRecordsAlg.h:111
xAOD::TruthVertex_v1
Class describing a truth vertex in the MC record.
Definition: TruthVertex_v1.h:37
Amg::py
@ py
Definition: GeoPrimitives.h:39
Muon::TruthTrackRecordsAlg::m_muonSnapShotY
DecorKey_t m_muonSnapShotY
Definition: TruthTrackRecordsAlg.h:108
SG::VarHandleKeyArrayCommon< T_HandleKey >::initialize
StatusCode initialize(bool used=true)
forward the initialization to the member VarHandleKeys
Amg::error
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 ...
Definition: EventPrimitivesHelpers.h:40
Muon::TruthTrackRecordsAlg::SummaryDecors::pzDecor
WriteDecorArray_f pzDecor
Definition: TruthTrackRecordsAlg.h:55
Amg::Vector3D
Eigen::Matrix< double, 3, 1 > Vector3D
Definition: GeoPrimitives.h:47
TrackRecord
Definition: TrackRecord.h:12
python.LumiBlobConversion.pos
pos
Definition: LumiBlobConversion.py:18
Muon::TruthTrackRecordsAlg::SummaryDecors::yDecor
WriteDecorArray_f yDecor
Definition: TruthTrackRecordsAlg.h:51
EventPrimitivesCovarianceHelpers.h
TrackingVolume.h
Muon::TruthTrackRecordsAlg::SummaryDecors::ecovDecor
WriteDecorArray_fvec ecovDecor
Definition: TruthTrackRecordsAlg.h:63
y
#define y
Muon::TruthTrackRecordsAlg::SummaryDecors::epyDecor
WriteDecorArray_f epyDecor
Definition: TruthTrackRecordsAlg.h:61
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
TruthTrackRecordsAlg.h
Muon::TruthTrackRecordsAlg::m_extrapolator
ToolHandle< Trk::IExtrapolator > m_extrapolator
Extrapolation tool handle.
Definition: TruthTrackRecordsAlg.h:124
Muon::TruthTrackRecordsAlg::m_muonSnapShotEPz
DecorKey_t m_muonSnapShotEPz
Definition: TruthTrackRecordsAlg.h:119
Trk::qOverP
@ qOverP
perigee
Definition: ParamDefs.h:67
Trk::TrackingGeometry::trackingVolume
const TrackingVolume * trackingVolume(const std::string &name) const
return the tracking Volume by name, 0 if it doesn't exist
LArNewCalib_DelayDump_OFC_Cali.idx
idx
Definition: LArNewCalib_DelayDump_OFC_Cali.py:69
physics_parameters.parameters
parameters
Definition: physics_parameters.py:144
Muon::TruthTrackRecordsAlg::SummaryDecors::xDecor
WriteDecorArray_f xDecor
Definition: TruthTrackRecordsAlg.h:50
TrackingGeometry.h
SG::VarHandleBase::isPresent
bool isPresent() const
Is the referenced object present in SG?
Definition: StoreGate/src/VarHandleBase.cxx:400
HepMC::is_sim_descendant
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,...
Definition: MagicNumbers.h:373
xAOD::TruthParticle_v1::p4
virtual FourMom_t p4() const override final
The full 4-momentum of the particle.
Definition: TruthParticle_v1.cxx:196
Trk::TrackingVolume
Definition: TrackingVolume.h:121
Muon::TruthTrackRecordsAlg::SummaryDecors
Definition: TruthTrackRecordsAlg.h:49
Muon::TruthTrackRecordsAlg::SummaryDecors::pyDecor
WriteDecorArray_f pyDecor
Definition: TruthTrackRecordsAlg.h:54
xAOD::TruthParticle_v1::charge
double charge() const
Physical charge.
generate::Zero
void Zero(TH1D *hin)
Definition: generate.cxx:32