ATLAS Offline Software
TrackParameterKVU.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 // TrackParametersKVU.cxx, (c) ATLAS Detector software
8 
12 #include <vector>
13 #include <string>
14 
15 // Constructor
17  const std::string& n,
18  const IInterface* p ) :
19  base_class(t, n, p){}
20 
21 // Athena initialize and finalize
23 {
24 
25  if (m_trackContainerKey.empty() || m_vertexContainerKey.empty()) {
26  ATH_MSG_ERROR("No selection variables for the TrackParametersKVU tool!");
27  return StatusCode::FAILURE;
28  }
29 
30  ATH_CHECK(m_trackContainerKey.initialize());
31  ATH_CHECK(m_vertexContainerKey.initialize());
32 
33  m_KVUphiKey = m_trackContainerKey.key() + m_KVUphiKey.key();
34  m_KVUthetaKey = m_trackContainerKey.key() + m_KVUthetaKey.key();
35  m_KVUd0Key = m_trackContainerKey.key() + m_KVUd0Key.key();
36  m_KVUz0Key = m_trackContainerKey.key() + m_KVUz0Key.key();
37  m_KVUqOverPKey = m_trackContainerKey.key() + m_KVUqOverPKey.key();
38  m_KVUChi2Key = m_trackContainerKey.key() + m_KVUChi2Key.key();
39  m_KVUusedPVKey = m_trackContainerKey.key() + m_KVUusedPVKey.key();
40  m_KVUCovMatKey = m_trackContainerKey.key() + m_KVUCovMatKey.key();
41 
42  ATH_CHECK(m_KVUphiKey.initialize());
43  ATH_CHECK(m_KVUthetaKey.initialize());
44  ATH_CHECK(m_KVUd0Key.initialize());
45  ATH_CHECK(m_KVUz0Key.initialize());
46  ATH_CHECK(m_KVUqOverPKey.initialize());
47  ATH_CHECK(m_KVUChi2Key.initialize());
48  ATH_CHECK(m_KVUusedPVKey.initialize());
49  ATH_CHECK(m_KVUCovMatKey.initialize());
50 
51  ATH_CHECK(m_vertexTrackUpdator.retrieve());
52  ATH_CHECK(m_extrapolator.retrieve());
53  ATH_CHECK(m_LinearizedTrackFactory.retrieve());
54  ATH_CHECK(m_IPEstimator.retrieve());
55 
56  ATH_MSG_DEBUG("initialize() ...");
57  ATH_MSG_DEBUG("Successfully retrived the TrackParametersKVU tool" );
58  return StatusCode::SUCCESS;
59 }
60 
61 // Augmentation
63 {
64  const EventContext& ctx = Gaudi::Hive::currentContext();
65  // --- Get the tracks
66  //const xAOD::TrackParticleContainer* tracks = nullptr;
67  SG::ReadHandle<xAOD::TrackParticleContainer> tracks(m_trackContainerKey,ctx);
68  ATH_CHECK( tracks.isValid() );
69 
70  //-- for each track, update track params with vtx considered as extra measurement (choose the closest vtx)
71  if(tracks->size() !=0) {
72  SG::WriteDecorHandle<xAOD::TrackParticleContainer, float> decoratorKVUphi(m_KVUphiKey, ctx);
73  SG::WriteDecorHandle<xAOD::TrackParticleContainer, float> decoratorKVUtheta(m_KVUthetaKey, ctx);
74  SG::WriteDecorHandle<xAOD::TrackParticleContainer, float> decoratorKVUd0(m_KVUd0Key, ctx);
75  SG::WriteDecorHandle<xAOD::TrackParticleContainer, float> decoratorKVUz0(m_KVUz0Key, ctx);
76  SG::WriteDecorHandle<xAOD::TrackParticleContainer, float> decoratorKVUqOverP(m_KVUqOverPKey, ctx);
77  SG::WriteDecorHandle<xAOD::TrackParticleContainer, float> decoratorKVUChi2(m_KVUChi2Key, ctx);
78  SG::WriteDecorHandle<xAOD::TrackParticleContainer, float> decoratorKVUusedPV(m_KVUusedPVKey, ctx);
79  SG::WriteDecorHandle<xAOD::TrackParticleContainer, std::vector<float>> decoratorKVUCovMat(m_KVUCovMatKey, ctx);
80 
81  for (const auto track : *tracks) {
82  if(track){
83  const Trk::TrackParameters *tPerigee = &(track->perigeeParameters());
84  // --- list of new variables that will decorate the track
85  std::unique_ptr<AmgSymMatrix(5)> updateTrackCov = nullptr;
86  float updatephi = -999;
87  float updatetheta = -999;
88  float updated0 = -999;
89  float updatez0 = -999;
90  float updateqOverP = -999;
91  float updateChi2 = -999;
92  bool usedPrimaryVertex = false;
93  std::vector<float> vec;
94 
95  std::unique_ptr<const Trk::TrackParameters> trackParams = nullptr;
96  float minIP = 1000.;
97  //--- retrieve closest vertex to track
98  const xAOD::Vertex* closestVertex = nullptr;
99  int nVtx = 0;
100  SG::ReadHandle<xAOD::VertexContainer> vxContainer(m_vertexContainerKey,ctx);
101  if (vxContainer.isValid()) {
102  for (const xAOD::Vertex* vtx: *vxContainer) {
103  if ( (vtx->vertexType() == xAOD::VxType::PriVtx) ||
104  (vtx->vertexType() == xAOD::VxType::PileUp) ) {
105  Amg::Vector3D vtxPos(vtx->position());
106  auto vtxSurface = std::make_unique<Trk::PerigeeSurface>(vtxPos);
107  trackParams = m_extrapolator->extrapolate(ctx,*tPerigee,*vtxSurface);
108  std::unique_ptr<const Trk::ImpactParametersAndSigma> iPandSigma = nullptr;
109  iPandSigma = m_IPEstimator->estimate(trackParams.get(), vtx);
110  if(sqrt(iPandSigma->IPd0*iPandSigma->IPd0+iPandSigma->IPz0*iPandSigma->IPz0) < minIP){
111  minIP = sqrt(iPandSigma->IPd0*iPandSigma->IPd0+iPandSigma->IPz0*iPandSigma->IPz0);
112  closestVertex = vtx;
113  }
114  ATH_MSG_VERBOSE("n vtx pos(x, y, z) IPd0 IPz0 : " << nVtx << " " << vtx->position().x() << " "<< vtx->position().y() << " "<< vtx->position().z() << " " << iPandSigma->IPd0 << " " << iPandSigma->IPz0);
115  ATH_MSG_VERBOSE(" d0,Delta_z0-TrackParticle:" << track->d0() <<", "<< track->z0() - vtx->z() + track->vz());
116  nVtx++;
117  }
118  }
119  }else{
120  ATH_MSG_ERROR ("Couldn't retrieve vxContainer with key: " << m_vertexContainerKey.key() );
121  return StatusCode::FAILURE;
122  } // --- end retrieve closest vertex to track
123 
124 
125  // update the track params with vtx info after linearization of track around it
126  if(closestVertex){
127  ATH_MSG_VERBOSE("Vertex selected position (x y z): " << closestVertex->position().x()<<" "<<closestVertex->position().y()<<" "<<closestVertex->position().z());
128  ATH_MSG_VERBOSE(" type: " << closestVertex->vertexType());
129  ATH_MSG_VERBOSE(" nParticles: " << closestVertex->nTrackParticles());
130  ATH_MSG_VERBOSE(" quality chi2: " << closestVertex->chiSquared() << " " << closestVertex->numberDoF());
131  Amg::Vector3D globPos(closestVertex->position());
132  auto recVtx = std::make_unique<xAOD::Vertex>();
133  recVtx->makePrivateStore(*closestVertex);
134  auto surface = std::make_unique<const Trk::PerigeeSurface>(globPos);
135  trackParams = m_extrapolator->extrapolate(ctx,*tPerigee,*surface);
136  auto linearTrack = std::make_unique<Trk::VxTrackAtVertex>(0., nullptr, nullptr, trackParams.get(), nullptr);
137  if(linearTrack){
138  ATH_MSG_VERBOSE("Linearizing track");
139  m_LinearizedTrackFactory->linearize(*linearTrack,globPos);
140  ATH_MSG_VERBOSE("Updating linearized track parameters after vertex fit. Track weight = " << linearTrack->weight());
141  m_vertexTrackUpdator->update((*linearTrack), (*recVtx));
142  ATH_MSG_VERBOSE ("track info after vertex track updator !"<<*linearTrack );
143  if(linearTrack->perigeeAtVertex()) {
144  //retrieve & store updated track param qOverP,d0, z0,theta,phi after KVU
145  updateqOverP = linearTrack->perigeeAtVertex()->parameters()[Trk::qOverP];
146  updated0 = linearTrack->perigeeAtVertex()->parameters()[Trk::d0];
147  updatez0 = linearTrack->perigeeAtVertex()->parameters()[Trk::z0];
148  updatephi = linearTrack->perigeeAtVertex()->parameters()[Trk::phi0];
149  updatetheta = linearTrack->perigeeAtVertex()->parameters()[Trk::theta];
150  //retrieve & store updated track cov matrix plus save the KVU Chi2
151  updateTrackCov = std::make_unique<AmgSymMatrix(5)>(*linearTrack->perigeeAtVertex()->covariance());
152  updateChi2 = linearTrack->trackQuality().chiSquared();
153  // store whether this used the PV or not
154  usedPrimaryVertex = closestVertex->vertexType()==xAOD::VxType::PriVtx;
155  }
156  }
157  }// --- end if closest vertex
158 
159  //decorate tracks with new updated track parameters:
160  decoratorKVUqOverP(*track) = updateqOverP;
161  decoratorKVUd0(*track) = updated0;
162  decoratorKVUz0(*track) = updatez0;
163  decoratorKVUphi(*track) = updatephi;
164  decoratorKVUtheta(*track) = updatetheta;
165  decoratorKVUChi2(*track) = updateChi2;
166  decoratorKVUusedPV(*track) = usedPrimaryVertex;
167  if (updateTrackCov){
168  Amg::compress(*updateTrackCov, vec);
169  }else{
170  vec.assign(5, 0.0);
171  }
172  decoratorKVUCovMat(*track) = vec;
173  ATH_MSG_VERBOSE("track updated.");
174  } // --- end if(track)
175  } // --- end loop tracks
176  ATH_MSG_VERBOSE("All tracks updated.");
177  }
178 
179  return StatusCode::SUCCESS;
180 }
DerivationFramework::TrackParametersKVU::initialize
virtual StatusCode initialize() override
Definition: TrackParameterKVU.cxx:22
xAOD::Vertex_v1::nTrackParticles
size_t nTrackParticles() const
Get the number of tracks associated with this vertex.
Definition: Vertex_v1.cxx:270
Amg::compress
void compress(const AmgSymMatrix(N) &covMatrix, std::vector< float > &vec)
Definition: EventPrimitivesHelpers.h:56
TrackParametersKVU.h
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
EventPrimitivesHelpers.h
DerivationFramework::TrackParametersKVU::addBranches
virtual StatusCode addBranches() const override
Check that the current event passes this filter.
Definition: TrackParameterKVU.cxx:62
DerivationFramework::TrackParametersKVU::TrackParametersKVU
TrackParametersKVU(const std::string &t, const std::string &n, const IInterface *p)
Constructor with parameters.
Definition: TrackParameterKVU.cxx:16
Trk::z0
@ z0
Definition: ParamDefs.h:64
Trk::ImpactParametersAndSigma::IPd0
double IPd0
Definition: ITrackToVertexIPEstimator.h:34
xAOD::Vertex_v1::position
const Amg::Vector3D & position() const
Returns the 3-pos.
vec
std::vector< size_t > vec
Definition: CombinationsGeneratorTest.cxx:12
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
xAOD::Vertex_v1::vertexType
VxType::VertexType vertexType() const
The type of the vertex.
DerivationFramework::AmgSymMatrix
AmgSymMatrix(3) BPhysAddMuonBasedInvMass
Definition: BPhysAddMuonBasedInvMass.cxx:601
CxxUtils::vec
typename vecDetail::vec_typedef< T, N >::type vec
Define a nice alias for the vectorized type.
Definition: vec.h:207
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
beamspotman.n
n
Definition: beamspotman.py:731
Trk::theta
@ theta
Definition: ParamDefs.h:66
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
SG::WriteDecorHandle
Handle class for adding a decoration to an object.
Definition: StoreGate/StoreGate/WriteDecorHandle.h:100
WriteDecorHandle.h
Handle class for adding a decoration to an object.
xAOD::VxType::PriVtx
@ PriVtx
Primary vertex.
Definition: TrackingPrimitives.h:571
Trk::ImpactParametersAndSigma::IPz0
double IPz0
Definition: ITrackToVertexIPEstimator.h:35
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
Trk::ParametersBase
Definition: ParametersBase.h:55
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
xAOD::VxType::PileUp
@ PileUp
Pile-up vertex.
Definition: TrackingPrimitives.h:573
Trk::d0
@ d0
Definition: ParamDefs.h:63
Amg::Vector3D
Eigen::Matrix< double, 3, 1 > Vector3D
Definition: GeoPrimitives.h:47
xAOD::Vertex_v1::numberDoF
float numberDoF() const
Returns the number of degrees of freedom of the vertex fit as float.
xAOD::Vertex_v1::chiSquared
float chiSquared() const
Returns the of the vertex fit as float.
xAOD::Vertex_v1
Class describing a Vertex.
Definition: Vertex_v1.h:42
Trk::qOverP
@ qOverP
perigee
Definition: ParamDefs.h:67
xAOD::track
@ track
Definition: TrackingPrimitives.h:512
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
Trk::phi0
@ phi0
Definition: ParamDefs.h:65