ATLAS Offline Software
Loading...
Searching...
No Matches
TrackPropagationHelper.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
5
7 // //
8 // Implementation of class TrackPropagationHelper //
9 // //
10 // Author: Thomas H. Kittelmann (Thomas.Kittelmann@cern.ch) //
11 // Initial extrapolation code by Thijs Cornelissen //
12 // Initial version: February 2008 //
13 // //
15
17#include "GaudiKernel/ThreadLocalContext.h"
19#include "TrkTrack/Track.h"
22#include <iostream>
23#include <sstream>
24#include <stdexcept>
27#include "TrkVolumes/Volume.h"
30
31#include "CLHEP/Units/PhysicalConstants.h"
32
33#include "VP1Base/VP1QtUtils.h"
34#include "VP1Base/VP1Msg.h"
35#include <limits>
36#include <memory>
37
38//____________________________________________________________________
40public:
42 : theclass(tc),
43 tracksanity(new VP1TrackSanity(tc?tc->systemBase():nullptr)),
44 maxInDetr(1.1*CLHEP::m),
46 maxz(3.5*CLHEP::m),
48 showExtrapSurfaces(VP1QtUtils::environmentVariableIsOn("VP1_TRKSYS_SHOWEXTRAPSURFACES"))
49 {
50 }
51
53 std::unique_ptr<VP1TrackSanity> tracksanity{};
54 const double maxInDetr{};
55 const double maxInDetrsq{};
56 const double maxz{};
57 const double fallback_flydist{};
58
59 bool outsideIDVolume(const Amg::Vector3D& p) const {
60 return fabs(p.z())>maxz||p.perp2()>maxInDetrsq;
61 }
62
63 //Fixme: These are similar to internal methods in InDetProjHelper in guideline system!!:
64 void movePoint1ToZPlaneAndPoint2( Amg::Vector3D& p1, const Amg::Vector3D& p2, const double& z ) const;
65 void movePoint1ToInfiniteCylinderAndPoint2( Amg::Vector3D&p1, const Amg::Vector3D&p2, const double& r ) const;
66
67 bool makePointsNeutral_SinglePar( std::vector<Amg::Vector3D >& points, const Trk::Track* );
68 bool makePointsCharged_SinglePar( std::vector<Amg::Vector3D >& points, const Trk::Track*,
69 Trk::IExtrapolator * extrapolator, Trk::ParticleHypothesis hypo );
70 bool addPointsBetweenParameters_Charged( std::vector<Amg::Vector3D >& points, const Trk::Track*,
71 const Trk::TrackParameters * par1, const Trk::TrackParameters * par2,
72 Trk::IExtrapolator * extrapolator, Trk::ParticleHypothesis hypo );
73
74 //Will create new track par:
76 const Trk::Track* trk,
77 const Trk::TrackParameters * prevpars,
79 const double& dist );
80 //Granularity:
81 static double maxPointDistSq(const Amg::Vector3D& /* p */){
82 //Vertex region:
92 return 2*CLHEP::cm;//Outside ID
93 }
94
95 std::vector<Trk::PlaneSurface> surfaces;
97};
98
99std::vector<Trk::PlaneSurface>& TrackPropagationHelper::getExtrapolationSurfaces() const {
100 return m_d->surfaces;
101}
102
103//____________________________________________________________________
105{
106 double dx(p2.x()-p1.x()), dy(p2.y()-p1.y()), dz(p2.z()-p1.z());
107 if (dz==0.0) {
108 theclass->message("movePoint1ToZPlaneAndPoint2 Error: Points have same z!!");
109 return;
110 }
111 double s( (z-p1.z())/dz );
112 // p1.set( p1.x()+dx*s, p1.y()+dy*s, z );
113 p1[0]= p1.x()+dx*s;
114 p1[1]= p1.y()+dy*s;
115 p1[2]= z;
116
117}
118
119 //____________________________________________________________________
121{
122 //Fixme: what happens here if we don't cross? And how can we be sure
123 //that we don't move FURTHER than the other point? (i.e. if the
124 //infinite line with p1 and p2 crosses, but the segment p1p2 does
125 //not!?
126 double p1r(p1.perp());
127 double dr(p2.perp()-p1r);
128 if (dr==0.0) {
129 theclass->message("movePoint1ToInfiniteCylinderAndPoint2 Error: Points have same r!!");
130 return;
131 }
132 double s((r-p1r)/dr);
133 double t(1.0-s);
134 // p1.set( p1.x()*t + p2.x()*s, p1.y()*t + p2.y()*s, p1.z()*t + p2.z()*s );
135 p1[0]= p1.x()*t + p2.x()*s;
136 //p1[1]= p2.y()*s, p1.z()*t; original
137 p1[1]= p1.y()*t + p2.y()*s; //sroe, compiler warning fix
138 p1[2]= p1.z()*t + p2.z()*s;
139}
140
141 //____________________________________________________________________
143 : VP1HelperClassBase(sys,"TrackPropagationHelper"), m_d(new Imp(this))
144{
145}
146
147 //____________________________________________________________________
152
153 //____________________________________________________________________
154bool TrackPropagationHelper::makePointsNeutral( std::vector<Amg::Vector3D >& points, const Trk::Track* track )
155{
156 if (VP1Msg::verbose())
157 messageVerbose("makePointsNeutral start");
158
159 points.clear();
160 if (!track) {
161 message("makePointsNeutral called with null track pointer");
162 return false;
163 }
164 const unsigned npars = track->trackParameters()->size();
165 if (npars==0) {
166 message("makePointsNeutral Error: No TrackParameters on track!");
167 return false;
168 }
169 if (npars==1)
170 return m_d->makePointsNeutral_SinglePar(points,track);
171
172 points.reserve(track->trackParameters()->size());
173
174
175 Trk::TrackStates::const_iterator tsos_iter = track->trackStateOnSurfaces()->begin();
176 Trk::TrackStates::const_iterator tsos_end = track->trackStateOnSurfaces()->end();
177 bool problems(false);
178 for (; tsos_iter != tsos_end; ++tsos_iter) {
179 if (!m_d->tracksanity->isSafe(*tsos_iter)) {
180 problems = true;
181 continue;
182 }
183 //if ((*tsos_iter)->materialEffectsOnTrack())
184 // continue;
185 const Trk::TrackParameters* trackParam = (*tsos_iter)->trackParameters();
186 if (!m_d->tracksanity->isSafe(trackParam)) {
187 problems = true;
188 continue;
189 }
190 points.push_back(trackParam->position());
191 }
192 if (problems) {
193 messageDebug("makePointsNeutral WARNING: Track had one or more track parameters which is unsafe to use in job.");
194 if (points.size()<2) {
195 messageDebug("makePointsNeutral ERROR: Track did not have at least two safe parameters.");
196 points.clear();
197 return false;
198 }
199 }
200
201 if (VP1Msg::verbose())
202 messageVerbose("makePointsNeutral_SinglePar end");
203 return true;
204}
205
206 //____________________________________________________________________
207bool TrackPropagationHelper::Imp::makePointsNeutral_SinglePar( std::vector<Amg::Vector3D >& points, const Trk::Track* track )
208{
209 if (VP1Msg::verbose())
210 theclass->messageVerbose("makePointsNeutral_SinglePar start");
211 points.clear();
212 const Trk::TrackParameters * par = *(track->trackParameters()->begin());
213 if (!tracksanity->isSafe(par)) {
214 theclass->messageDebug("makePointsNeutral_SinglePar called with unsafe track parameter");
215 return false;
216 }
217 Amg::Vector3D u(par->momentum().unit());
218 Amg::Vector3D a(par->position()),b;
219 if (outsideIDVolume(a)) {
220 //We start outside. We just add fallback_flydist. (fixme: warn?)
221 b = a +fallback_flydist*u;
222 } else {
223 //Make a second point, where trajectory hits maxInDetr,maxz
224 b = a +999*CLHEP::m*u;
225 if (b.z()<-maxz)
227 else
228 if (b.z()>maxz)
230 if (b.perp2()>maxInDetrsq)
232 }
233 points.reserve(2);
234 points.push_back(a);
235 points.push_back(b);
236 if (VP1Msg::verbose())
237 theclass->messageVerbose("makePointsNeutral_SinglePar (single track parameter) end");
238 return true;
239}
240
241 //____________________________________________________________________
242bool TrackPropagationHelper::makePointsCharged( std::vector<Amg::Vector3D >& points, const Trk::Track* track,
243 Trk::IExtrapolator * extrapolator, Trk::ParticleHypothesis hypo, bool useMEOT,const Trk::Volume* volume )
244{
245 if (VP1Msg::verbose())
246 messageVerbose("makePointsCharged start with hypo="+str(hypo)+", useMEOT="+str(useMEOT)+", volume=" +str(volume));
247 // if (volume) std::cout<<volume->volumeBounds()<<std::endl;
249 // Clear input points, check/sanitise input and initialise //
251
252 points.clear();
253 if (!extrapolator) {
254 message("makePointsCharged ERROR: Null extrapolator tool provided!");
255 return false;
256 }
257 if (!track) {
258 message("makePointsCharged ERROR: Called with null track pointer");
259 return false;
260 }
261 const unsigned npars = track->trackParameters()->size();
262 if (npars==0) {
263 message("makePointsCharged ERROR: No TrackParameters on track!");
264 return false;
265 }
266 if (npars==1)
267 return m_d->makePointsCharged_SinglePar(points,track,extrapolator,hypo);
268
269 points.reserve(npars);//At least we need this.
270 const EventContext& ctx = Gaudi::Hive::currentContext();
271 //Add a point for each parameter, and add extra points between them where appropriate.
272 Trk::TrackStates::const_iterator tsos_iter = track->trackStateOnSurfaces()->begin();
273 Trk::TrackStates::const_iterator tsos_end = track->trackStateOnSurfaces()->end();
274 const Trk::TrackParameters* prevpar(nullptr);
275 const Trk::TrackParameters* trackParam(nullptr);
276 bool problems(false);
277 for (; tsos_iter != tsos_end; ++tsos_iter) {
278 if (!m_d->tracksanity->isSafe(*tsos_iter))
279 continue;
280 if ((*tsos_iter)->measurementOnTrack()==nullptr && ( (*tsos_iter)->materialEffectsOnTrack()&&!useMEOT ) )
281 continue;
282 trackParam = (*tsos_iter)->trackParameters();
283 if (!m_d->tracksanity->isSafe(trackParam))
284 continue;
285 if (!prevpar) {
286 //first time.
287 prevpar = trackParam;
288 points.push_back(prevpar->position());
289 continue;
290 }
291 if (!m_d->addPointsBetweenParameters_Charged(points,track,prevpar,trackParam,extrapolator,hypo))
292 problems = true;
293 points.push_back(trackParam->position());
294 prevpar = trackParam;
295 }
296
297 if (problems)
298 messageDebug("WARNING: Problems encountered adding point(s) between track parameters");
299
300 // Only tracks ending inside the inner detector are eligible for extension.
301 if (volume && prevpar && !m_d->outsideIDVolume(prevpar->position())) {
302 messageVerbose("Extending to Volume");
303 const auto surfaces = const_cast<Trk::VolumeBounds&>(volume->volumeBounds())
304 .decomposeToSurfaces(volume->transform());
305 std::size_t intersections = 0;
306 bool extended = false;
307 QStringList surfaceErrors;
308 for (std::size_t i = 0; i < surfaces.size(); ++i) {
309 try {
310 const auto target = extrapolator->extrapolate(ctx, *prevpar, *surfaces[i],
311 Trk::alongMomentum, true, hypo);
312 if (!target)
313 continue;
314
315 ++intersections;
316 messageVerbose("Extrapolation succeeded");
317 const auto pointCount = points.size();
318 const bool sampled = m_d->addPointsBetweenParameters_Charged(
319 points, track, prevpar, target.get(), extrapolator, hypo);
320 if (sampled && points.size() > pointCount)
321 extended = true;
322 } catch (const std::runtime_error& error) {
323 surfaceErrors << QStringLiteral("surface %1: %2").arg(i + 1).arg(error.what());
324 }
325 }
326
327 if (!extended) {
328 const QString warning = QStringLiteral("WARNING: The extrapolator could not extend a track to the selected destination.");
329 message(warning + QStringLiteral(" See the terminal for details."));
330
331 std::ostringstream details;
332 details << VP1Msg::prefix_msg() << " [TrackPropagationHelper]: " << warning.toStdString()
333 << " Target bounds: " << volume->volumeBounds()
334 << " | Start (x, y, z) [mm]: " << prevpar->position().transpose()
335 << " | Particle hypothesis: " << hypo
336 << " | Target intersections: " << intersections << '/' << surfaces.size() << ". ";
337 if (surfaces.empty())
338 details << "The target volume has no boundary surfaces.";
339 else if (intersections == 0)
340 details << "No boundary surface could be reached along the track momentum.";
341 else
342 details << "An intersection was found, but no extension segment was sampled successfully.";
343 if (!surfaceErrors.empty())
344 details << " Extrapolator exceptions: " << surfaceErrors.join(QStringLiteral("; ")).toStdString();
345 std::cerr << details.str() << std::endl;
346 }
347 }
348 if (VP1Msg::verbose())
349 messageVerbose("makePointsCharged end with "+str(points.size())+"points");
350 return true;
351}
352
353 //____________________________________________________________________
355 const Trk::Track* trk,
356 const Trk::TrackParameters * prevpars,
358 const double& dist )
359{
360 if (!trk||!prevpars||!extrapolator)
361 return nullptr;
362
363 Trk::CurvilinearUVT uvt(prevpars->momentum().unit());
364
365
366 Amg::Transform3D t(uvt.curvU(),uvt.curvV(),uvt.curvT(), prevpars->position()+(prevpars->momentum().unit()*dist));
367
368 Trk::PlaneSurface surf(t);
369
370
371 if (showExtrapSurfaces) surfaces.push_back(surf);
372
373
374 const Trk::TrackParameters *newpars(nullptr);
375 try {
376 newpars = extrapolator->extrapolate(Gaudi::Hive::currentContext(),
377 *prevpars,
378 surf,
379 Trk::alongMomentum,false,hypo).release(); // change this to extrapolate current param to surface.
380 } catch (const std::runtime_error& e) {
381 theclass->message("Failure trying to use extrapolator for track (Exception thrown: " + QString(e.what())+")");
382 return nullptr;
383 }
384 if (!newpars) {
385 theclass->message("Failure trying to use extrapolator for track");
386 return nullptr;
387 }
388 if (!newpars) {
389 theclass->message("Failure trying to use extrapolator for track");
390 return nullptr;
391 }
392 return newpars;
393}
394
396 return m_d->showExtrapSurfaces;
397}
398
399
400 //____________________________________________________________________
401bool TrackPropagationHelper::Imp::makePointsCharged_SinglePar( std::vector<Amg::Vector3D >& points, const Trk::Track* track,
402 Trk::IExtrapolator * extrapolator, Trk::ParticleHypothesis hypo )
403{
404 if (VP1Msg::verbose())
405 theclass->messageVerbose("makePointsCharged_SinglePar start");
406 points.clear();
407 if (!extrapolator) {
408 theclass->message("makePointsCharged_SinglePar ERROR: Null extrapolator tool provided!");
409 return false;
410 }
411 const Trk::TrackParameters * par = *(track->trackParameters()->begin());
412 if (!tracksanity->isSafe(par)) {
413 theclass->messageDebug("makePointsCharged_SinglePar called with unsafe track parameter");
414 return false;
415 }
416
417 Amg::Vector3D p0(par->position());
418 points.push_back(p0);
419 const bool startoutsideID(outsideIDVolume(p0));
420
421 double distadded(0);
422 const double maxdistadded = 2*CLHEP::m;
423 points.reserve(20);//skip a few reallocs
424 const Trk::TrackParameters * prevpars = par;
425 const Trk::TrackParameters * temppars(nullptr);
426
427 while (distadded<maxdistadded) {
428 temppars = extrapolateToNewPar( extrapolator, track, prevpars, hypo, maxPointDistSq(prevpars->position()));
429 if (temppars)
430 distadded += (temppars->position()-prevpars->position()).mag();
431 if (prevpars!=par)
432 delete prevpars;
433 prevpars = nullptr;
434 if (!temppars) {
435 theclass->messageDebug("makePointsCharged_SinglePar ERROR: Failed to use extrapolator for next point");
436 if (points.size()<2) {
437 points.clear();
438 return false;
439 }
440 return true;//Fixme??
441 }
442 Amg::Vector3D p(temppars->position());
443 points.push_back(p);
444 prevpars = temppars;
445 //temppars is overwritten on the next loop
446 if (!startoutsideID && outsideIDVolume(p)) {
447 if (prevpars!=par)
448 delete prevpars;
449 prevpars = nullptr;
450 break;
451 }
452 }
453 if (prevpars!=par)
454 delete prevpars;
455
456 if (VP1Msg::verbose())
457 theclass->messageVerbose("makePointsCharged_SinglePar end");
458 return true;
459
460}
461
462 //____________________________________________________________________
463bool TrackPropagationHelper::Imp::addPointsBetweenParameters_Charged( std::vector<Amg::Vector3D >& points, const Trk::Track* trk,
464 const Trk::TrackParameters * par1, const Trk::TrackParameters * par2,
465 Trk::IExtrapolator * extrapolator, Trk::ParticleHypothesis hypo )
466{
467 double distbetween = sqrt((par2->position()-par1->position()).mag2());
468 if (distbetween<0.001) {
469 theclass->messageVerbose("TrackPropagationHelper::Imp::addPointsBetweenParameters_Charged: parameters on top of each other. Skip, but no error.");
470 return true;
471 }
472 // theclass->messageVerbose("TrackPropagationHelper::Imp::addPointsBetweenParameters_Charged: par1="
473 // +str(par1->position())+", par2=" +str(par2->position())+", dist="+str(distbetween));
474
475 Amg::Vector3D p2(par2->position());
476 const Trk::TrackParameters * prevpars = par1;
477
478 double olddistsq(1.0e99);
479 double distadded(0);
480 const double maxdistadded = std::max(2*CLHEP::m,(par1->position()-p2).mag()*1.5);
481 while ( (prevpars->position()-p2).mag()> maxPointDistSq(prevpars->position()) && distadded<maxdistadded ) {
482
483 // theclass->messageVerbose("distadded: "+str(distadded)+", distance left="+str(sqrt((prevpars->position()-p2).mag2()))+", jump="+str(maxPointDistSq(prevpars->position())));
484 const Trk::TrackParameters * newpars = extrapolateToNewPar( extrapolator, trk, prevpars, hypo, maxPointDistSq(prevpars->position()) );
485 if (!newpars){
486 if (prevpars!=par1)
487 delete prevpars;
488 if (VP1Msg::verbose())
489 theclass->messageVerbose("TrackPropagationHelper::Imp::addPointsBetweenParameters_Charged: Extrapolation failed.");
490 return false;
491 }
492 const double distsq = (par2->position()-newpars->position()).mag2();
493 if (distsq>olddistsq) {
494 if (prevpars!=par1)
495 delete prevpars;
496 delete newpars;
497 if (VP1Msg::verbose())
498 theclass->messageVerbose("TrackPropagationHelper::Imp::addPointsBetweenParameters_Charged: distq("+str(distsq)+")>olddistsq ("+str(olddistsq)+") so overshot?");
499 return false;
500 }
501 olddistsq = distsq;
502 // if ((prevpars->position()-newpars->position()).mag2() > 4*maxpointdistsq) {
503 // delete newpars;
504 // return false;
505 // }
506 points.push_back(newpars->position());
507 distadded += (newpars->position()-prevpars->position()).mag();
508 if (prevpars!=par1)
509 delete prevpars;
510 prevpars = newpars;
511 }
512 if (prevpars!=par1)
513 delete prevpars;
514 return (distadded<maxdistadded);
515}
static Double_t a
static Double_t tc
#define z
DataModel_detail::const_iterator< DataVector > const_iterator
Definition DataVector.h:861
const Trk::TrackParameters * extrapolateToNewPar(Trk::IExtrapolator *extrapolator, const Trk::Track *trk, const Trk::TrackParameters *prevpars, Trk::ParticleHypothesis hypo, const double &dist)
std::vector< Trk::PlaneSurface > surfaces
For debugging.
bool addPointsBetweenParameters_Charged(std::vector< Amg::Vector3D > &points, const Trk::Track *, const Trk::TrackParameters *par1, const Trk::TrackParameters *par2, Trk::IExtrapolator *extrapolator, Trk::ParticleHypothesis hypo)
bool makePointsCharged_SinglePar(std::vector< Amg::Vector3D > &points, const Trk::Track *, Trk::IExtrapolator *extrapolator, Trk::ParticleHypothesis hypo)
TrackPropagationHelper * theclass
void movePoint1ToZPlaneAndPoint2(Amg::Vector3D &p1, const Amg::Vector3D &p2, const double &z) const
bool outsideIDVolume(const Amg::Vector3D &p) const
Imp(TrackPropagationHelper *tc)
std::unique_ptr< VP1TrackSanity > tracksanity
bool makePointsNeutral_SinglePar(std::vector< Amg::Vector3D > &points, const Trk::Track *)
static double maxPointDistSq(const Amg::Vector3D &)
void movePoint1ToInfiniteCylinderAndPoint2(Amg::Vector3D &p1, const Amg::Vector3D &p2, const double &r) const
bool makePointsCharged(std::vector< Amg::Vector3D > &points, const Trk::Track *, Trk::IExtrapolator *extrapolator, Trk::ParticleHypothesis hypo=Trk::nonInteracting, bool useMEOT=false, const Trk::Volume *volume=0)
bool makePointsNeutral(std::vector< Amg::Vector3D > &points, const Trk::Track *)
std::vector< Trk::PlaneSurface > & getExtrapolationSurfaces() const
simple class that constructs the curvilinear vectors curvU and curvV from a given momentum direction ...
const Amg::Vector3D & curvU() const
Access methods.
const Amg::Vector3D & curvT() const
const Amg::Vector3D & curvV() const
Interface class for the extrapolation AlgTool, it inherits from IAlgTool Detailed information about p...
virtual std::unique_ptr< NeutralParameters > extrapolate(const NeutralParameters &parameters, const Surface &sf, PropDirection dir=anyDirection, const BoundaryCheck &bcheck=true) const =0
Main extrapolation Interface starting from neutral parameters and aiming at surface.
const Amg::Vector3D & momentum() const
Access method for the momentum.
const Amg::Vector3D & position() const
Access method for the position.
Class for a planaer rectangular or trapezoidal surface in the ATLAS detector.
Pure Absract Base Class for Volume bounds.
virtual std::vector< std::unique_ptr< Trk::Surface > > decomposeToSurfaces(const Amg::Transform3D &transform)=0
Method to decompose the Bounds into Surfaces, the Volume can turn them into BoundarySurfaces.
Base class for all volumes inside the tracking realm, it defines the interface for inherited Volume c...
Definition Volume.h:36
const Amg::Transform3D & transform() const
Return methods for geometry transform.
Definition Volume.h:83
const VolumeBounds & volumeBounds() const
returns the volumeBounds()
Definition Volume.h:96
VP1HelperClassBase(IVP1System *sys=0, QString helpername="")
void messageVerbose(const QString &) const
void message(const QString &) const
IVP1System * systemBase() const
void messageDebug(const QString &) const
static bool verbose()
Definition VP1Msg.h:31
static const char * prefix_msg()
Definition VP1Msg.h:56
int r
Definition globals.cxx:22
Eigen::Affine3d Transform3D
Eigen::Matrix< double, 3, 1 > Vector3D
@ alongMomentum
ParticleHypothesis
Enumeration for Particle hypothesis respecting the interaction with material.
ParametersBase< TrackParametersDim, Charged > TrackParameters