ATLAS Offline Software
PoorMansIpAugmenterAlg.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 
7 
10 
14 
16 
17 // NOTE: would be nice to include this to access track parameters, but
18 // it's not in all builds.
19 //
20 // #include "TrkEventPrimitives/ParamDefs.h"
21 
22 namespace Pmt {
23  // copied from
24  // https://acode-browser1.usatlas.bnl.gov/lxr/source/athena/Tracking/TrkEvent/TrkEventPrimitives/TrkEventPrimitives/ParamDefs.h,
25  // which isn't included in some builds.
26  enum ParamDefs {
27  // Track defining parameters
28  d0 = 0,
29  z0 = 1,
30  phi0 = 2,
31  theta = 3,
32  qOverP = 4,
33  // Cartesian
34  x = 0,
35  y = 1,
36  z = 2,
37  };
38 
39  // I'm pretty sure I can get x, y, z, beamspot perigee points from
40  // x = -sin(phi) * d0
41  // y = cos(phi) * d0
42  // z = z0
43  Eigen::Vector3d getPosition(
44  const xAOD::TrackParticle& trk) {
45  double d0 = trk.d0();
46  double phi = trk.phi();
47  return Amg::Vector3D(
48  d0 * -std::sin(phi),
49  d0 * std::cos(phi),
50  trk.z0());
51  }
52 
53  // this one is going to be harder, there's some lead here:
54  // https://acode-browser1.usatlas.bnl.gov/lxr/source/athena/Tracking/TrkVertexFitter/TrkVertexFitterUtils/src/TrackToVertexIPEstimator.cxx
55  double getSigmaD0(const xAOD::TrackParticle& trk,
56  const Eigen::Matrix2d& vtxCov) {
57 
58  // start with the track part
59  double trackComponent = trk.definingParametersCovMatrixDiagVec().at(
60  Pmt::d0);
61 
62  // now do the vertex part
63  double phi = trk.phi();
64  // The sign seems inverted below, but maybe that doesn't
65  // matter. I'm just following TrackToVertexIPEstimator...
66  Eigen::Vector2d vtxJacobian(-std::sin(phi), std::cos(phi));
67  double vertexComponent = vtxJacobian.transpose()*vtxCov*vtxJacobian;
68 
69  return std::sqrt(trackComponent + vertexComponent);
70 
71  }
72 
73  double getSigmaD0(const xAOD::TrackParticle& trk,
74  const xAOD::Vertex& vtx) {
75 
76  // first two elements of the vertex covariance is xy
77  Eigen::Matrix2d vtxCov = vtx.covariancePosition().block<2,2>(0,0);
78  return getSigmaD0(trk, vtxCov);
79 
80  }
81 
83  const xAOD::EventInfo& evt) {
84  Eigen::Matrix2d bsCov;
85  // based on what I read in the beamspot code [1] and some code
86  // that copies it to the EventInfo [2] I'm pretty sure the
87  // beamPosSigmaX and beamPosSigmaY need to be squared in the
88  // covariance matrix, whereas beamPosSigmaXY does not.
89  //
90  // [1]: https://acode-browser1.usatlas.bnl.gov/lxr/source/athena/InnerDetector/InDetConditions/BeamSpotConditionsData/src/BeamSpotData.cxx
91  // [2]: https://acode-browser1.usatlas.bnl.gov/lxr/source/athena/Simulation/BeamEffects/src/BeamSpotFixerAlg.cxx#0068
92  bsCov(0, 0) = std::pow(evt.beamPosSigmaX(),2);
93  bsCov(0, 1) = evt.beamPosSigmaXY();
94  bsCov(1, 0) = evt.beamPosSigmaXY();
95  bsCov(1, 1) = std::pow(evt.beamPosSigmaY(),2);
96  return getSigmaD0(trk, bsCov);
97  }
98 
99 
101  double vxZCov) {
102 
103  // first do the track part
104  const auto& fullCov = trk.definingParametersCovMatrix();
105  Eigen::Matrix2d trkCov;
106  trkCov(0,0)=fullCov(Pmt::z0, Pmt::z0);
107  trkCov(0,1)=fullCov(Pmt::z0, Pmt::theta);
108  trkCov(1,0)=fullCov(Pmt::theta, Pmt::z0);
109  trkCov(1,1)=fullCov(Pmt::theta, Pmt::theta);
110  double theta = trk.theta();
111  Eigen::Vector2d trkJacobian(
112  std::sin(theta),
113  trk.z0()*std::cos(theta));
114  double trackComponent = trkJacobian.transpose()*trkCov*trkJacobian;
115 
116  // now do the vertex part
117  double vertexComponent = std::sin(theta)*vxZCov*std::sin(theta);
118 
119  return std::sqrt(trackComponent + vertexComponent);
120 
121  }
123  const xAOD::Vertex& vtx) {
124  double vxZCov = vtx.covariancePosition()(Pmt::z,Pmt::z);
125  return getSigmaZ0SinTheta(trk, vxZCov);
126  }
127 
128 }
129 
130 namespace FlavorTagDiscriminants {
131 
133  const std::string& name, ISvcLocator* loc )
134  : AthReentrantAlgorithm(name, loc) {}
135 
137  ATH_MSG_INFO( "Inizializing " << name() << "... " );
138 
139  // Initialize Container keys
140  ATH_MSG_DEBUG( "Inizializing containers:" );
141  ATH_MSG_DEBUG( " ** " << m_TrackContainerKey );
142  ATH_MSG_DEBUG( " ** " << m_VertexContainerKey );
143  ATH_MSG_DEBUG( " ** " << m_eventInfoKey );
144 
147 
148  bool use_vertices = !m_VertexContainerKey.empty();
149  ATH_CHECK( m_VertexContainerKey.initialize(use_vertices) );
150 
151  // Prepare decorators
154 
158 
159  // Initialize decorators
160  ATH_MSG_DEBUG( "Inizializing decorators:" );
161  ATH_MSG_DEBUG( " ** " << m_dec_d0_sigma );
162  ATH_MSG_DEBUG( " ** " << m_dec_z0_sigma );
163  ATH_MSG_DEBUG( " ** " << m_dec_track_pos );
164  ATH_MSG_DEBUG( " ** " << m_dec_track_mom );
165  ATH_MSG_DEBUG( " ** " << m_dec_invalid );
166 
172 
174  "Accessors for beamspot:" <<
175  " ** " << m_beam_sigma_x <<
176  " ** " << m_beam_sigma_y <<
177  " ** " << m_beam_sigma_z <<
178  " ** " << m_beam_cov_xy <<
179  "");
180 
181  CHECK( m_beam_sigma_x.initialize() );
182  CHECK( m_beam_sigma_y.initialize() );
183  CHECK( m_beam_sigma_z.initialize() );
184  CHECK( m_beam_cov_xy.initialize() );
185 
186  return StatusCode::SUCCESS;
187  }
188 
189  StatusCode PoorMansIpAugmenterAlg::execute(const EventContext& ctx) const {
190  ATH_MSG_DEBUG( "Executing " << name() << "... " );
191 
193  CHECK( event_info.isValid() );
194 
195  const xAOD::Vertex* primary = nullptr;
196  if (!m_VertexContainerKey.empty()) {
198  m_VertexContainerKey,ctx );
199  CHECK( verteces.isValid() );
200  primary = getPrimaryVertex( *verteces );
201  if ( primary == nullptr ) {
202  ATH_MSG_FATAL("No primary vertex found");
203  return StatusCode::FAILURE;
204  }
205  }
206 
208  m_TrackContainerKey,ctx);
209  CHECK( tracks.isValid() );
210  ATH_MSG_DEBUG( "Retrieved " << tracks->size() << " input tracks..." );
211 
212  using TPC = xAOD::TrackParticleContainer;
213 
216 
218  m_dec_track_pos, ctx);
220  m_dec_track_mom, ctx);
221 
223 
225  m_beam_sigma_x, ctx);
227  m_beam_sigma_y, ctx);
229  m_beam_sigma_z, ctx);
231  m_beam_cov_xy, ctx);
232 
233  // now decorate the tracks
234  for (const xAOD::TrackParticle *trk: *tracks) {
235 
236  const xAOD::EventInfo& evt = *event_info;
237  float d0_sigma2 = trk->definingParametersCovMatrixDiagVec().at(Pmt::d0);
239  trk->phi(),
240  beam_sigma_x(evt),
241  beam_sigma_y(evt),
242  beam_cov_xy(evt));
243  float full_d0_sigma = std::sqrt(d0_sigma2 + bs_d0_sigma2);
244  ATH_MSG_DEBUG("track d0Uncertainty: " << std::sqrt(d0_sigma2));
245  ATH_MSG_DEBUG("beamspot d0Uncertainty: " << std::sqrt(bs_d0_sigma2));
246  ATH_MSG_DEBUG("combined d0Uncertainty: " << full_d0_sigma);
247 
248  decor_d0_sigma(*trk) = full_d0_sigma;
249  if (primary) {
250  decor_z0_sigma(*trk) = Pmt::getSigmaZ0SinTheta(*trk, *primary);
251  } else {
252  // if we don't have a primary we take the beamspot z
253  // uncertainty
254  double vxZCov = std::pow(beam_sigma_z(evt), 2);
255  decor_z0_sigma(*trk) = Pmt::getSigmaZ0SinTheta(*trk, vxZCov);
256  }
257 
258  // the primary vertex position is absolute, whereas the track
259  // perigee parameters are all relative to the beamspot. The x
260  // and y coordinates are zero so that the 2d impact parameter
261  // has no idea about the primary vertex location.
262  const Amg::Vector3D primary_relative_to_beamspot(
263  0, 0,
264  primary ? primary->position().z() - trk->vz() : 0);
265  const Amg::Vector3D position = (
266  Pmt::getPosition(*trk) - primary_relative_to_beamspot);
267 
268  auto trkp4 = trk->p4();
269  const Amg::Vector3D momentum(trkp4.Px(), trkp4.Py(), trkp4.Pz());
270 
272  "track_displacement (x,y,z)= ("
273  << position.x() << ", " << position.y() << ", " << position.z()
274  << ")");
276  "track_momentum (x,y,z)= ("
277  << momentum.x() << ", " << momentum.y() << ", " << momentum.z()
278  << ")");
279 
280  std::vector<float> out_vec_pos(
281  position.data(), position.data() + position.size());
282  std::vector<float> out_vec_mom(
283  momentum.data(), momentum.data() + momentum.size());
284 
285  decor_track_pos (*trk) = out_vec_pos;
286  decor_track_mom (*trk) = out_vec_mom;
287 
288  // all tracks are valid for now, but downstream code requires
289  // this decoration.
290  decor_invalid(*trk) = 0;
291  }
292 
293  return StatusCode::SUCCESS;
294  }
295 
297  return StatusCode::SUCCESS;
298  }
299 
301  const xAOD::VertexContainer& vertexCollection ) const {
302  if ( vertexCollection.size() == 0 ) {
303  ATH_MSG_DEBUG( "Input vertex collection has size 0!" );
304  return nullptr;
305  }
306 
307  for ( const xAOD::Vertex *vertex : vertexCollection ) {
308  if ( vertex->vertexType() == xAOD::VxType::PriVtx ) {
309  return vertex;
310  }
311  }
312 
313  // this is taken from BTagTool, should be the beam spot if nothing
314  // else exists.
315  return vertexCollection.front();
316  }
317 
318 }
319 
320 
FlavorTagDiscriminants::PoorMansIpAugmenterAlg::m_beam_cov_xy
SG::ReadDecorHandleKey< xAOD::EventInfo > m_beam_cov_xy
Definition: PoorMansIpAugmenterAlg.h:81
FlavorTagDiscriminants::PoorMansIpAugmenterAlg::m_VertexContainerKey
SG::ReadHandleKey< xAOD::VertexContainer > m_VertexContainerKey
Definition: PoorMansIpAugmenterAlg.h:40
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
FlavorTagDiscriminants::PoorMansIpAugmenterAlg::m_beam_sigma_x
SG::ReadDecorHandleKey< xAOD::EventInfo > m_beam_sigma_x
Definition: PoorMansIpAugmenterAlg.h:67
FlavorTagDiscriminants::PoorMansIpAugmenterAlg::m_prefix
Gaudi::Property< std::string > m_prefix
Definition: PoorMansIpAugmenterAlg.h:48
TrackParticlexAODHelpers.h
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
FlavorTagDiscriminants
This file contains "getter" functions used for accessing tagger inputs from the EDM.
Definition: AssociationEnums.h:11
Pmt::theta
@ theta
Definition: PoorMansIpAugmenterAlg.cxx:31
Pmt::z
@ z
Definition: PoorMansIpAugmenterAlg.cxx:36
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
FlavorTagDiscriminants::PoorMansIpAugmenterAlg::m_dec_track_pos
SG::WriteDecorHandleKey< xAOD::TrackParticleContainer > m_dec_track_pos
Definition: PoorMansIpAugmenterAlg.h:56
Pmt::z0
@ z0
Definition: PoorMansIpAugmenterAlg.cxx:29
PoorMansIpAugmenterAlg.h
xAOD::TrackParticleContainer
TrackParticleContainer_v1 TrackParticleContainer
Definition of the current "TrackParticle container version".
Definition: Event/xAOD/xAODTracking/xAODTracking/TrackParticleContainer.h:14
xAOD::TrackParticle_v1::z0
float z0() const
Returns the parameter.
Pmt::x
@ x
Definition: PoorMansIpAugmenterAlg.cxx:34
LArG4FSStartPointFilter.evt
evt
Definition: LArG4FSStartPointFilter.py:42
drawFromPickle.cos
cos
Definition: drawFromPickle.py:36
Pmt::getSigmaZ0SinTheta
double getSigmaZ0SinTheta(const xAOD::TrackParticle &trk, double vxZCov)
Definition: PoorMansIpAugmenterAlg.cxx:100
SG::VarHandleKey::key
const std::string & key() const
Return the StoreGate ID for the referenced object.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:141
FlavorTagDiscriminants::PoorMansIpAugmenterAlg::m_dec_d0_sigma
SG::WriteDecorHandleKey< xAOD::TrackParticleContainer > m_dec_d0_sigma
Definition: PoorMansIpAugmenterAlg.h:50
xAOD::TrackParticle_v1::d0
float d0() const
Returns the parameter.
xAOD::TrackingHelpers::d0UncertaintyBeamSpot2
double d0UncertaintyBeamSpot2(double track_phi0, double beam_sigma_x, double beam_sigma_y, double beam_sigma_xy)
calculate the squared d0 uncertainty component due to the size of the beam spot.
Definition: TrackParticlexAODHelpers.h:49
FlavorTagDiscriminants::PoorMansIpAugmenterAlg::finalize
virtual StatusCode finalize() override
Definition: PoorMansIpAugmenterAlg.cxx:296
AthReentrantAlgorithm
An algorithm that can be simultaneously executed in multiple threads.
Definition: AthReentrantAlgorithm.h:83
FlavorTagDiscriminants::PoorMansIpAugmenterAlg::m_dec_invalid
SG::WriteDecorHandleKey< xAOD::TrackParticleContainer > m_dec_invalid
Definition: PoorMansIpAugmenterAlg.h:62
FlavorTagDiscriminants::PoorMansIpAugmenterAlg::m_eventInfoKey
SG::ReadHandleKey< xAOD::EventInfo > m_eventInfoKey
Definition: PoorMansIpAugmenterAlg.h:44
Pmt::getPosition
Eigen::Vector3d getPosition(const xAOD::TrackParticle &trk)
Definition: PoorMansIpAugmenterAlg.cxx:43
FlavorTagDiscriminants::PoorMansIpAugmenterAlg::getPrimaryVertex
const xAOD::Vertex * getPrimaryVertex(const xAOD::VertexContainer &) const
Definition: PoorMansIpAugmenterAlg.cxx:300
StateLessPT_NewConfig.primary
primary
Definition: StateLessPT_NewConfig.py:228
FlavorTagDiscriminants::PoorMansIpAugmenterAlg::m_beam_sigma_y
SG::ReadDecorHandleKey< xAOD::EventInfo > m_beam_sigma_y
Definition: PoorMansIpAugmenterAlg.h:71
SG::ReadDecorHandle
Handle class for reading a decoration on an object.
Definition: StoreGate/StoreGate/ReadDecorHandle.h:94
TrackParticleAuxContainer.h
ParticleGun_EoverP_Config.momentum
momentum
Definition: ParticleGun_EoverP_Config.py:63
FlavorTagDiscriminants::PoorMansIpAugmenterAlg::m_dec_z0_sigma
SG::WriteDecorHandleKey< xAOD::TrackParticleContainer > m_dec_z0_sigma
Definition: PoorMansIpAugmenterAlg.h:52
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
Pmt::getSigmaD0
double getSigmaD0(const xAOD::TrackParticle &trk, const Eigen::Matrix2d &vtxCov)
Definition: PoorMansIpAugmenterAlg.cxx:55
FlavorTagDiscriminants::PoorMansIpAugmenterAlg::m_beam_sigma_z
SG::ReadDecorHandleKey< xAOD::EventInfo > m_beam_sigma_z
Definition: PoorMansIpAugmenterAlg.h:75
WriteDecorHandle.h
Handle class for adding a decoration to an object.
xAOD::VxType::PriVtx
@ PriVtx
Primary vertex.
Definition: TrackingPrimitives.h:571
FlavorTagDiscriminants::PoorMansIpAugmenterAlg::m_dec_track_mom
SG::WriteDecorHandleKey< xAOD::TrackParticleContainer > m_dec_track_mom
Definition: PoorMansIpAugmenterAlg.h:59
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
CHECK
#define CHECK(...)
Evaluate an expression and check for errors.
Definition: Control/AthenaKernel/AthenaKernel/errorcheck.h:422
FlavorTagDiscriminants::PoorMansIpAugmenterAlg::m_TrackContainerKey
SG::ReadHandleKey< xAOD::TrackParticleContainer > m_TrackContainerKey
Definition: PoorMansIpAugmenterAlg.h:37
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
Pmt::qOverP
@ qOverP
Definition: PoorMansIpAugmenterAlg.cxx:32
DataVector
Derived DataVector<T>.
Definition: DataVector.h:794
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
FlavorTagDiscriminants::PoorMansIpAugmenterAlg::initialize
virtual StatusCode initialize() override
Definition: PoorMansIpAugmenterAlg.cxx:136
xAOD::TrackParticle_v1::definingParametersCovMatrix
const ParametersCovMatrix_t definingParametersCovMatrix() const
Returns the 5x5 symmetric matrix containing the defining parameters covariance matrix.
Definition: TrackParticle_v1.cxx:246
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
Pmt::d0
@ d0
Definition: PoorMansIpAugmenterAlg.cxx:28
Amg::Vector3D
Eigen::Matrix< double, 3, 1 > Vector3D
Definition: GeoPrimitives.h:47
xAOD::EventInfo_v1
Class describing the basic event information.
Definition: EventInfo_v1.h:43
Trk::vertex
@ vertex
Definition: MeasurementType.h:21
VertexContainer.h
xAOD::TrackParticle_v1::definingParametersCovMatrixDiagVec
const std::vector< float > & definingParametersCovMatrixDiagVec() const
Returns the diagonal elements of the defining parameters covariance matrix.
Definition: TrackParticle_v1.cxx:375
SG::WriteDecorHandleKey::initialize
StatusCode initialize(bool used=true)
If this object is used as a property, then this should be called during the initialize phase.
xAOD::Vertex_v1
Class describing a Vertex.
Definition: Vertex_v1.h:42
Pmt
Definition: PoorMansIpAugmenterAlg.cxx:22
Pmt::getSigmaD0WithRespectToBeamspot
double getSigmaD0WithRespectToBeamspot(const xAOD::TrackParticle &trk, const xAOD::EventInfo &evt)
Definition: PoorMansIpAugmenterAlg.cxx:82
ReadDecorHandle.h
Handle class for reading a decoration on an object.
Pmt::phi0
@ phi0
Definition: PoorMansIpAugmenterAlg.cxx:30
Pmt::y
@ y
Definition: PoorMansIpAugmenterAlg.cxx:35
FlavorTagDiscriminants::PoorMansIpAugmenterAlg::PoorMansIpAugmenterAlg
PoorMansIpAugmenterAlg(const std::string &name, ISvcLocator *pSvcLocator)
Definition: PoorMansIpAugmenterAlg.cxx:132
xAOD::TrackParticle_v1
Class describing a TrackParticle.
Definition: TrackParticle_v1.h:43
drawFromPickle.sin
sin
Definition: drawFromPickle.py:36
Pmt::ParamDefs
ParamDefs
Definition: PoorMansIpAugmenterAlg.cxx:26
pow
constexpr int pow(int base, int exp) noexcept
Definition: ap_fixedTest.cxx:15
xAOD::TrackParticle_v1::theta
float theta() const
Returns the parameter, which has range 0 to .
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
TrackParticleContainer.h
FlavorTagDiscriminants::PoorMansIpAugmenterAlg::execute
virtual StatusCode execute(const EventContext &) const override
Definition: PoorMansIpAugmenterAlg.cxx:189
xAOD::TrackParticle_v1::phi
virtual double phi() const override final
The azimuthal angle ( ) of the particle (has range to .)