ATLAS Offline Software
TruthTrackRecordToTrack.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 
7 #include <cmath>
8 #include <memory>
9 
10 #include "GaudiKernel/IPartPropSvc.h"
11 
12 #include "AtlasHepMC/GenParticle.h"
13 #include "AtlasHepMC/GenVertex.h"
14 
16 #include "xAODTruth/TruthVertex.h"
17 
18 #include "HepPDT/ParticleDataTable.hh"
21 
22 
23 //================================================================
24 Trk::TruthTrackRecordToTrack::TruthTrackRecordToTrack(const std::string& type, const std::string& name,
25  const IInterface* parent)
27  m_particleDataTable(nullptr),
28  m_extrapolator("Trk::Extrapolator/AtlasExtrapolator")
29 {
30  declareInterface<ITruthToTrack>(this);
31 
32  declareProperty("Extrapolator", m_extrapolator);
33  declareProperty("TrackRecordKey",m_reccollkey="CosmicPerigee");
34 }
35 
36 //================================================================
38 
39  // get the Particle Properties Service
40  IPartPropSvc* partPropSvc = nullptr;
41  StatusCode sc = service("PartPropSvc", partPropSvc, true);
42  if (sc.isFailure()) {
43  ATH_MSG_ERROR ("Could not initialize Particle Properties Service");
44  return StatusCode::FAILURE;
45  }
46  m_particleDataTable = partPropSvc->PDT();
47 
48  if ( m_extrapolator.retrieve().isFailure() ) {
49  ATH_MSG_FATAL ("Failed to retrieve tool " << m_extrapolator );
50  return StatusCode::FAILURE;
51  }
52 
53  ATH_CHECK( m_reccollkey.initialize() );
54 
55  return StatusCode::SUCCESS;
56 }
57 
58 //================================================================
60 
61  if (part == nullptr || m_particleDataTable==nullptr) return nullptr;
62 
63  Trk::TrackParameters *result = nullptr;
64  Amg::Vector3D prodVertexVector;
65  prodVertexVector.setZero();
66  Amg::Vector3D globalPos;
67  Amg::Vector3D globalMom;
68  int id=0;
69  double charge = 0.0;
70  const HepPDT::ParticleData* pd = nullptr;
71 
72 
73  SG::ReadHandle<TrackRecordCollection> trackRecordCollection(m_reccollkey);
74 
75  if (trackRecordCollection.isValid()) {
76  ATH_MSG_ERROR ("Could not get track record!");
77  return nullptr;
78  }
79  ATH_MSG_DEBUG("reading from track record, size=" << trackRecordCollection->size());
80 
81  if (trackRecordCollection->empty()) ATH_MSG_WARNING ("action required but TrackRecordCollection size is 0");
82 
83  const int barcodepart = HepMC::barcode(part);
84  for (const auto & trackRecord : *trackRecordCollection){
85 
86  if ( HepMC::barcode(trackRecord) != barcodepart ) continue; // FIXME barcode-based - currently TrackRecords read in from input files will have invalid ID values, so stick with barcodes
87 
88  id = trackRecord.GetPDGCode();
89  pd = m_particleDataTable->particle(std::abs(id));
90  if (!pd) {
91  ATH_MSG_WARNING ("Found particle with problematic PDG ID " << part << " , " << id);
92  continue;
93  }
94 
95  CLHEP::Hep3Vector tv = trackRecord.GetPosition();
96  prodVertexVector = Amg::Vector3D(tv.x(),tv.y(),tv.z());
97  globalPos = prodVertexVector;
98 
99  Amg::Vector3D hv2(trackRecord.GetMomentum().x(), trackRecord.GetMomentum().y(),
100  trackRecord.GetMomentum().z());
101  globalMom = hv2;
102 
103  ATH_MSG_DEBUG("Found particle " << part << ", momentum " << hv2 << " production " << globalPos);
104 
105 
106  } // loop over G4 records
107 
108  if (pd) {
109  charge = (id>0) ? pd->charge() : -pd->charge();
110 
111  Amg::Translation3D prodSurfaceCentre( prodVertexVector.x(),
112  prodVertexVector.y(),
113  prodVertexVector.z() );
114 
115  Amg::Transform3D tmpTransf = prodSurfaceCentre * Amg::RotationMatrix3D::Identity();
116 
117  Trk::PlaneSurface planeSurface(tmpTransf, 5., 5. );
118  result = new Trk::AtaPlane(globalPos, globalMom, charge, planeSurface);
119 
120  } else {
121  ATH_MSG_WARNING ("Could not get particle data for particle ID="<<id);
122  }
123  return result;
124 }
125 
126 
127 
128 //================================================================
130 
131  if (part == nullptr || m_particleDataTable==nullptr) return nullptr;
132 
133  Trk::TrackParameters *result = nullptr;
134  Amg::Vector3D prodVertexVector;
135  Amg::Vector3D globalPos;
136  Amg::Vector3D globalMom;
137  int id=0;
138  double charge = 0.0;
139  const HepPDT::ParticleData* pd = nullptr;
140 
141  SG::ReadHandle<TrackRecordCollection> trackRecordCollection(m_reccollkey);
142 
143  if (trackRecordCollection.isValid()) {
144  ATH_MSG_ERROR ("Could not get track record!");
145  return nullptr;
146  }
147 
148  ATH_MSG_DEBUG("reading from track record, size=" << trackRecordCollection->size());
149 
150  if (trackRecordCollection->empty()) ATH_MSG_WARNING ("action required but TrackRecordCollection size is 0");
151 
152  for (const auto & trackRecord : *trackRecordCollection){
153 
154  if ( HepMC::barcode(trackRecord) == HepMC::barcode(part) ) { // FIXME barcode-based - currently TrackRecords read in from input files will have invalid ID values and xAOD::TruthParticle only supports barcodes, so stick with barcodes
155 
156  id = trackRecord.GetPDGCode();
157  pd = m_particleDataTable->particle(std::abs(id));
158  if (!pd) {
159  ATH_MSG_WARNING ("found particle with problematic PDG ID" << part << " , " << id);
160  continue;
161  }
162 
163  CLHEP::Hep3Vector tv = trackRecord.GetPosition();
164  prodVertexVector = Amg::Vector3D(tv.x(),tv.y(),tv.z());
165  globalPos = prodVertexVector;
166 
167  Amg::Vector3D hv2(trackRecord.GetMomentum().x(), trackRecord.GetMomentum().y(), trackRecord.GetMomentum().z());
168  globalMom = hv2;
169 
170  ATH_MSG_DEBUG("found particle " << part << ", momentum " << hv2 << " production " << globalPos);
171 
172 
173  }
174  } // loop over G4 records
175 
176  if (pd) {
177  charge = (id>0) ? pd->charge() : -pd->charge();
178 
179  Amg::Translation3D prodSurfaceCentre( prodVertexVector.x(),
180  prodVertexVector.y(),
181  prodVertexVector.z() );
182 
183  Amg::Transform3D tmpTransf = prodSurfaceCentre * Amg::RotationMatrix3D::Identity();
184 
185  Trk::PlaneSurface planeSurface(tmpTransf, 5., 5. );
186  result = new Trk::AtaPlane(globalPos, globalMom, charge, planeSurface);
187 
188  } else {
189  ATH_MSG_WARNING ("Could not get particle data for particle ID="<<id);
190  }
191  return result;
192 }
193 
194 
195 
196 //================================================================
198  const Trk::TrackParameters* generatedTrackPerigee = nullptr;
199 
200  if(part && part->production_vertex() && m_particleDataTable && m_extrapolator) {
201 
202  MsgStream log(msgSvc(), name());
203 
204  std::unique_ptr<const Trk::TrackParameters> productionVertexTrackParams( makeProdVertexParameters(part) );
205  if(productionVertexTrackParams) {
206 
207  // Extrapolate the TrackParameters object to the perigee. Direct extrapolation,
208  // no material effects.
209  generatedTrackPerigee =
210  m_extrapolator->extrapolateDirectly(Gaudi::Hive::currentContext(),
211  *productionVertexTrackParams,
214  false,
215  Trk::nonInteracting).release();
216  }
217  }
218 
219  return generatedTrackPerigee;
220 }
221 
222 //================================================================
224  const Trk::TrackParameters* generatedTrackPerigee = nullptr;
225 
226  if(part && part->hasProdVtx() && m_particleDataTable && m_extrapolator) {
227 
228  MsgStream log(msgSvc(), name());
229 
230  std::unique_ptr<const Trk::TrackParameters> productionVertexTrackParams( makeProdVertexParameters(part) );
231  if(productionVertexTrackParams) {
232 
233  // Extrapolate the TrackParameters object to the perigee. Direct extrapolation,
234  // no material effects.
235  generatedTrackPerigee =
236  m_extrapolator->extrapolateDirectly(Gaudi::Hive::currentContext(),
237  *productionVertexTrackParams,
240  false,
241  Trk::nonInteracting).release();
242  }
243  }
244 
245  return generatedTrackPerigee;
246 }
247 //================================================================
LArG4FSStartPointFilter.part
part
Definition: LArG4FSStartPointFilter.py:21
Trk::anyDirection
@ anyDirection
Definition: PropDirection.h:22
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
Trk::TruthTrackRecordToTrack::TruthTrackRecordToTrack
TruthTrackRecordToTrack(const std::string &type, const std::string &name, const IInterface *parent)
Definition: TruthTrackRecordToTrack.cxx:24
get_generator_info.result
result
Definition: get_generator_info.py:21
Trk::PerigeeSurface
Definition: PerigeeSurface.h:43
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
AthCommonDataStore< AthCommonMsg< AlgTool > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
GenVertex.h
Trk::TruthTrackRecordToTrack::makeProdVertexParameters
virtual const Trk::TrackParameters * makeProdVertexParameters(HepMC::ConstGenParticlePtr part) const
This function produces a Trk::TrackParameters object corresponding to the HepMC::GenParticle at the p...
Definition: TruthTrackRecordToTrack.cxx:59
Trk::TruthTrackRecordToTrack::initialize
virtual StatusCode initialize()
Definition: TruthTrackRecordToTrack.cxx:37
IExtrapolator.h
GenParticle.h
AthenaPoolTestRead.sc
sc
Definition: AthenaPoolTestRead.py:27
AtlasHitsVector::empty
bool empty() const
Definition: AtlasHitsVector.h:129
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
StdJOSetup.msgSvc
msgSvc
Provide convenience handles for various services.
Definition: StdJOSetup.py:36
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
HepMC::barcode
int barcode(const T *p)
Definition: Barcode.h:16
xAOD::TruthParticle_v1
Class describing a truth particle in the MC record.
Definition: TruthParticle_v1.h:41
Amg::Transform3D
Eigen::Affine3d Transform3D
Definition: GeoPrimitives.h:46
test_pyathena.parent
parent
Definition: test_pyathena.py:15
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?
TruthVertex.h
TrackRecord.h
HepMC::ConstGenParticlePtr
const GenParticle * ConstGenParticlePtr
Definition: GenParticle.h:38
Trk::TruthTrackRecordToTrack::m_reccollkey
SG::ReadHandleKey< TrackRecordCollection > m_reccollkey
Definition: TruthTrackRecordToTrack.h:68
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:192
Trk::nonInteracting
@ nonInteracting
Definition: ParticleHypothesis.h:25
charge
double charge(const T &p)
Definition: AtlasPID.h:494
Amg::Vector3D
Eigen::Matrix< double, 3, 1 > Vector3D
Definition: GeoPrimitives.h:47
Trk::TruthTrackRecordToTrack::m_extrapolator
ToolHandle< Trk::IExtrapolator > m_extrapolator
Definition: TruthTrackRecordToTrack.h:67
Trk::AtaPlane
ParametersT< 5, Charged, PlaneSurface > AtaPlane
Definition: Tracking/TrkEvent/TrkParameters/TrkParameters/TrackParameters.h:30
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
Trk::PlaneSurface
Definition: PlaneSurface.h:64
Trk::TruthTrackRecordToTrack::makePerigeeParameters
virtual const Trk::TrackParameters * makePerigeeParameters(HepMC::ConstGenParticlePtr part) const
This function extrapolates track to the perigee, and returns perigee parameters.
Definition: TruthTrackRecordToTrack.cxx:197
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
Amg::Translation3D
Eigen::Translation< double, 3 > Translation3D
Definition: GeoPrimitives.h:44
python.CaloCondTools.log
log
Definition: CaloCondTools.py:20
AtlasHitsVector::size
size_type size() const
Definition: AtlasHitsVector.h:143
AthAlgTool
Definition: AthAlgTool.h:26
TruthParticle.h
TruthTrackRecordToTrack.h