ATLAS Offline Software
Loading...
Searching...
No Matches
GaussianDensityTestAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3*/
4
5// GaussianDensityTestAlg.cxx
6// Implementation file for class GaussianDensityTestAlg
7// Author: Dave Casper <dcasper@uci.edu>
9
10// TrkVertexSeedFinderUtils includes
12
13// STL includes
14
15// FrameWork includes
16#include "Gaudi/Property.h"
17
19#include "xAODTracking/Vertex.h"
20
21#include <limits>
22
23namespace Trk
24{
26// Public methods:
28
29// Constructors
32 ISvcLocator* pSvcLocator ) :
33 ::AthAlgorithm( name, pSvcLocator ),
35 m_firstEvent(true),
36 m_iTHistSvc("THistSvc", name)
37{
38 //
39 // Property declaration
40 //
41}
42
43// Destructor
46= default;
47
48// Athena Algorithm's Hooks
51{
52 ATH_MSG_INFO ("Initializing " << name() << "...");
53
54 ATH_CHECK( m_trackParticlesKey.initialize() );
55 ATH_CHECK( m_truthEventsKey.initialize() );
56 ATH_CHECK( m_pileupEventsKey.initialize() );
57
58 ATH_CHECK( m_estimator.retrieve() );
59 ATH_CHECK( m_trackFilter.retrieve() );
60 ATH_CHECK( m_ipEstimator.retrieve() );
61 ATH_CHECK(m_beamSpotKey.initialize());
62 // setup histograms/trees
63 m_h_density = new TH1F("Density", "Density", 800, -200.0, 200.0);
64 m_h_truthDensity = new TH1F("Truth", "Truth", 800, -200.0, 200.0);
65 m_h_truthVertices = new TH1F("TruthVertices", "TruthVertices", 800, -200.0, 200.0);
66 m_h_modeCheck = new TH1F("ModeOffset", "ModeOffset", 100, -2.0, 2.0);
67
68 CHECK( m_iTHistSvc->regHist("/file1/h/density", m_h_density) );
69 CHECK( m_iTHistSvc->regHist("/file1/h/truth", m_h_truthDensity) );
70 CHECK( m_iTHistSvc->regHist("/file1/h/truthvertices", m_h_truthVertices) );
71 CHECK( m_iTHistSvc->regHist("/file1/h/modeoffset", m_h_modeCheck) );
72
73 return StatusCode::SUCCESS;
74}
75
77{
78 ATH_MSG_INFO ("Finalizing " << name() << "...");
79 return StatusCode::SUCCESS;
80}
81
83{
84 ATH_MSG_DEBUG ("Executing " << name() << "...");
85
87
88 ATH_MSG_VERBOSE("Selecting tracks");
89 std::vector<Trk::ITrackLink*> trackVector;
90 selectTracks(trackParticles.cptr(), trackVector);
91
92 std::vector<const Trk::TrackParameters*> perigeeList;
93 analyzeTracks(trackVector, perigeeList);
94
95 ATH_MSG_VERBOSE("Using density estimator");
96 std::unique_ptr<Trk::IVertexTrackDensityEstimator::ITrackDensity> dens;
97 double mode = m_estimator->globalMaximum (perigeeList, dens);
98
99 if (m_firstEvent)
100 {
101 for (int i = 0; i < 800; i++)
102 {
103 double z = -200.0 + 0.25 + i*0.5;
104 double density = dens->trackDensity(z);
105 m_h_density->Fill((float) z, (float) density);
106 }
107 }
108 ATH_MSG_VERBOSE("Analyzing MC truth");
109 std::vector<Amg::Vector3D> truth;
110 ATH_CHECK( findTruth(mode, trackVector, truth, m_h_truthDensity, m_h_modeCheck) );
111 if (m_firstEvent)
112 {
113 ATH_MSG_VERBOSE("Filling truth vertex histogram");
114 for (auto& v : truth) m_h_truthVertices->Fill( v[2] );
115 }
116 m_firstEvent = false;
117 return StatusCode::SUCCESS;
118}
119
121// Non-const methods:
123
124void GaussianDensityTestAlg::analyzeTracks(const std::vector<Trk::ITrackLink*>& trackVector,
125 std::vector<const Trk::TrackParameters*>& perigeeList)
126{
127 for (auto *seedtrkAtVtxIter : trackVector)
128 {
129 perigeeList.push_back( seedtrkAtVtxIter->parameters() );
130 }
131}
132
134 std::vector<Trk::ITrackLink*>& trackVector)
135{
136 bool selectionPassed{false};
137 const InDet::BeamSpotData* beamspot = nullptr;
140 if(beamSpotHandle.isValid()) beamspot = beamSpotHandle.retrieve();
141 }
142 for (auto itr = trackParticles->begin(); itr != trackParticles->end(); ++itr) {
144 xAOD::Vertex beamposition;
145 beamposition.makePrivateStore();
146 beamposition.setPosition(beamspot->beamVtx().position());
147 beamposition.setCovariancePosition(beamspot->beamVtx().covariancePosition());
148 selectionPassed=static_cast<bool>(m_trackFilter->accept(**itr,&beamposition));
149 }
150 else
151 {
152 xAOD::Vertex null;
153 null.makePrivateStore();
154 null.setPosition(Amg::Vector3D(0,0,0));
155 AmgSymMatrix(3) vertexError;
156 vertexError.setZero();
157 null.setCovariancePosition(vertexError);
158 selectionPassed=static_cast<bool>(m_trackFilter->accept(**itr,&null));
159 }
160 if (selectionPassed)
161 {
163 link.setElement(*itr);
165 linkTT->setStorableObject(*trackParticles);
166 trackVector.push_back(linkTT);
167 }
168 }
169}
170
172// Const methods:
174StatusCode
176 const std::vector<Trk::ITrackLink*>& trackVector,
177 std::vector<Amg::Vector3D>& truth,
178 TH1* h_truthDensity,
179 TH1* h_modeCheck) const
180{
181 double modeClosestDistance = std::numeric_limits<double>::max();
182
184
186 if ( signalEvents.isValid() )
187 {
188 ATH_MSG_VERBOSE("Found signalEvents");
189 for (const xAOD::TruthEventBase* evt : *signalEvents)
190 {
191 const xAOD::TruthVertex* vLink = *(evt->truthVertexLink(0));
192 if (vLink == nullptr){
193 ATH_MSG_ERROR("Invalid truthVertexLink from signalEvents in GaussianDensityTestAlg::findTruth");
194 return StatusCode::FAILURE;
195 }
196 Amg::Vector3D vTruth(vLink->x(),vLink->y(),vLink->z());
197 int nGoodTracks = 0;
198 for (auto *trk : trackVector)
199 {
201 if (lxtp)
202 {
203 bool isAssoc = truthParticleAssoc(**(*lxtp)).isValid();
204 if (isAssoc)
205 {
206 const auto& assocParticle = truthParticleAssoc(**(*lxtp));
207 ATH_MSG_VERBOSE("Found associated truth particle");
208 for (const auto& truthParticle : evt->truthParticleLinks())
209 {
210 if (!truthParticle.isValid()) continue;
211 if (assocParticle == truthParticle)
212 {
213 ATH_MSG_VERBOSE("Calling ipSignificance");
214 double significance = ipSignificance(trk->parameters(), &vTruth);
215 ATH_MSG_VERBOSE("ipSignificance returned " << significance);
216 if (significance <= m_significanceTruthCut)
217 {
218 ATH_MSG_VERBOSE("Adding good track");
219 nGoodTracks++;
220 const Trk::Perigee* perigee = dynamic_cast<const Trk::Perigee*>(trk->parameters());
221 if (perigee == nullptr) ATH_MSG_ERROR("Invalid Perigee");
222 if (m_firstEvent)
223 {
224 h_truthDensity->Fill(vLink->z());
225 ATH_MSG_VERBOSE("Filled truth density histogram");
226 }
227 }
228 break;
229 }
230 }
231 }
232 else
233 {
234 ATH_MSG_VERBOSE("No associated truth particle found");
235 }
236 }
237 }
238 if (nGoodTracks >= m_truthVertexTracks)
239 {
240 truth.push_back(vTruth);
241 if (abs(modeClosestDistance) > abs(mode - vTruth[2]))
242 modeClosestDistance = mode - vTruth[2];
243 }
244 }
245 }
246 else
247 {
248 ATH_MSG_WARNING("No TruthEventContainer found");
249 }
250
252 if ( pileupEvents.isValid() )
253 {
254 ATH_MSG_VERBOSE("Found pileupEvents");
255 for (const xAOD::TruthEventBase* evt : *pileupEvents)
256 {
257 const xAOD::TruthVertex* vLink = *(evt->truthVertexLink(0));
258 if (vLink == nullptr) {
259 ATH_MSG_ERROR("Invalid truthVertexLink from pileupEvents");
260 return StatusCode::FAILURE;
261 }
262 Amg::Vector3D vTruth(vLink->x(),vLink->y(),vLink->z());
263 int nGoodTracks = 0;
264 for (auto *trk : trackVector)
265 {
267 if (lxtp)
268 {
269 bool isAssoc = truthParticleAssoc(**(*lxtp)).isValid();
270 if (isAssoc)
271 {
272 const auto& assocParticle = truthParticleAssoc(**(*lxtp));
273 ATH_MSG_VERBOSE("Found associated truth particle");
274 for (const auto& truthParticle : evt->truthParticleLinks())
275 {
276 if (!truthParticle.isValid()) continue;
277 if (assocParticle == truthParticle)
278 {
279 ATH_MSG_VERBOSE("Calling ipSignificance");
280 double significance = ipSignificance(trk->parameters(), &vTruth);
281 ATH_MSG_VERBOSE("ipSignificance returned " << significance);
282 if (significance <= m_significanceTruthCut)
283 {
284 ATH_MSG_VERBOSE("Adding good track");
285 nGoodTracks++;
286 const Trk::Perigee* perigee = dynamic_cast<const Trk::Perigee*>(trk->parameters());
287 if (perigee == nullptr) ATH_MSG_ERROR("Invalid Perigee");
288 if (m_firstEvent)
289 {
290 h_truthDensity->Fill(vLink->z());
291 ATH_MSG_VERBOSE("Filled truth density histogram");
292 }
293 }
294 break;
295 }
296 }
297 }
298 else
299 {
300 ATH_MSG_VERBOSE("No associated truth particle found");
301 }
302 }
303 }
304 if (nGoodTracks >= m_truthVertexTracks)
305 {
306 truth.push_back(vTruth);
307 if (abs(modeClosestDistance) > abs(mode - vTruth[2]))
308 modeClosestDistance = mode - vTruth[2];
309 }
310 }
311 }
312 else
313 {
314 ATH_MSG_WARNING("No TruthPileupEventContainer found");
315 }
316
317 h_modeCheck->Fill( modeClosestDistance );
318 return StatusCode::SUCCESS;
319 }
320
322 const Amg::Vector3D * vertex ) const
323 {
325 v.makePrivateStore();
326 v.setPosition(*vertex);
327 v.setCovariancePosition(AmgSymMatrix(3)::Zero(3,3));
328 v.setFitQuality(0., 0.);
329
330 double significance = 0.0;
331 std::unique_ptr<ImpactParametersAndSigma> ipas = m_ipEstimator->estimate( params, &v );
332 if ( ipas != nullptr )
333 {
334 if ( ipas->sigmad0 > 0 && ipas->sigmaz0 > 0)
335 {
336 significance = sqrt( pow(ipas->IPd0/ipas->sigmad0,2) + pow(ipas->IPz0/ipas->sigmaz0,2) );
337 }
338 }
339 return significance;
340 }
341}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
#define CHECK(...)
Evaluate an expression and check for errors.
#define AmgSymMatrix(dim)
constexpr int pow(int base, int exp) noexcept
AthAlgorithm(const std::string &name, ISvcLocator *pSvcLocator)
Constructor with parameters:
const_iterator end() const noexcept
Return a const_iterator pointing past the end of the collection.
const_iterator begin() const noexcept
Return a const_iterator pointing at the beginning of the collection.
const Trk::RecVertex & beamVtx() const noexcept
void makePrivateStore()
Create a new (empty) private store for this object.
SG::ConstAccessor< T, ALLOC > ConstAccessor
Helper class to provide type-safe access to aux data.
Definition AuxElement.h:131
const_pointer_type retrieve()
virtual bool isValid() override final
Can the handle be successfully dereferenced?
const_pointer_type cptr()
Dereference the pointer.
TH1 * m_h_density
Histograms and trees.
SG::ReadHandleKey< xAOD::TrackParticleContainer > m_trackParticlesKey
Data handle keys.
ToolHandle< Trk::IVertexTrackDensityEstimator > m_estimator
virtual ~GaussianDensityTestAlg()
Destructor:
StatusCode findTruth(double mode, const std::vector< Trk::ITrackLink * > &trackVector, std::vector< Amg::Vector3D > &truth, TH1 *h_truthDensity, TH1 *h_modeCheck) const
ToolHandle< InDet::IInDetTrackSelectionTool > m_trackFilter
SG::ReadHandleKey< xAOD::TruthPileupEventContainer > m_pileupEventsKey
SG::ReadCondHandleKey< InDet::BeamSpotData > m_beamSpotKey
ToolHandle< Trk::ITrackToVertexIPEstimator > m_ipEstimator
Gaudi::Property< int > m_truthVertexTracks
void selectTracks(const xAOD::TrackParticleContainer *trackParticles, std::vector< Trk::ITrackLink * > &trackVector)
SG::ReadHandleKey< xAOD::TruthEventContainer > m_truthEventsKey
static void analyzeTracks(const std::vector< Trk::ITrackLink * > &trackVector, std::vector< const Trk::TrackParameters * > &perigeeList)
ServiceHandle< ITHistSvc > m_iTHistSvc
Gaudi::Property< double > m_significanceTruthCut
double ipSignificance(const Trk::TrackParameters *params, const Amg::Vector3D *vertex) const
Element link to XAOD TrackParticle.
const Amg::Vector3D & position() const
return position of vertex
Definition Vertex.cxx:63
float z() const
Vertex longitudinal distance along the beam line form the origin.
float y() const
Vertex y displacement.
float x() const
Vertex x displacement.
void setCovariancePosition(const AmgSymMatrix(3)&covariancePosition)
Sets the vertex covariance matrix.
void setPosition(const Amg::Vector3D &position)
Sets the 3-position.
Eigen::Matrix< double, 3, 1 > Vector3D
Ensure that the ATLAS eigen extensions are properly loaded.
ParametersT< TrackParametersDim, Charged, PerigeeSurface > Perigee
@ z
global position (cartesian)
Definition ParamDefs.h:57
@ v
Definition ParamDefs.h:78
ParametersBase< TrackParametersDim, Charged > TrackParameters
TruthEventBase_v1 TruthEventBase
Typedef to implementation.
TruthVertex_v1 TruthVertex
Typedef to implementation.
Definition TruthVertex.h:15
Vertex_v1 Vertex
Define the latest version of the vertex class.
TrackParticleContainer_v1 TrackParticleContainer
Definition of the current "TrackParticle container version".