ATLAS Offline Software
Loading...
Searching...
No Matches
ReFitTrack.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
6// ReFitTrack.cxx
7// Implementation file for class ReFitTrack
9// version 1.0 13/05/04 Maria Jose Costa
11
12//SiTBLineFitter includes
14
15// Gaudi includes
16#include "GaudiKernel/TypeNameString.h"
17
29#include "VxVertex/RecVertex.h"
30
31#include <memory>
32
33#include <vector>
34
35// Constructor with parameters:
36Trk::ReFitTrack::ReFitTrack(const std::string &name, ISvcLocator *pSvcLocator) :
37 AthAlgorithm(name,pSvcLocator),
38 m_trackName("Tracks"),
39 m_newTrackName("ReFitted_Tracks"),
40 m_runOutlier(false),
41 m_matEffects(3),
43 m_tracks(nullptr),
44 m_ITrackFitter("TrkKalmanFitter/KalmanFitter"),
49 m_vxContainerName("VxPrimaryCandidate"),
50 m_extrapolator("Trk::Extrapolator/InDetExtrapolator"),
51 m_usetrackhypo(false)
52{
53
54 // Get parameter values from jobOptions file
55 declareProperty("TrackName", m_trackName, "collection name for tracks to be refitted");
56 declareProperty("NewTrackName", m_newTrackName, "collection name for output tracks");
57 declareProperty("FitterTool", m_ITrackFitter, "ToolHandle for track fitter implementation");
58 declareProperty("FitterToolTRT", m_ITrackFitterTRT, "ToolHandle for TRT track fitter implementation");
59 declareProperty("SummaryTool" , m_trkSummaryTool, "ToolHandle for track summary tool");
60 declareProperty("TrackSelectionTool", m_trkSelectorTool, "ToolHandle for track selection tool");
61 declareProperty("runOutlier", m_runOutlier, "switch to control outlier finding in track fit");
62 declareProperty("matEffects", m_matEffects, "type of material interaction in extrapolation");
63 declareProperty("useParticleHypothesisFromTrack", m_usetrackhypo,"fit with particle hypothesis from original track");
64
65 // constrained fitting
66 declareProperty("ConstrainFit", m_constrainFitMode,"mode switch if/how the track is constrained to the BS/Vx");
67 declareProperty("VertexCollection", m_vxContainerName,"Source for vertex to use for constraining tracks");
68 declareProperty("Extrapolator", m_extrapolator, "Extrapolator needed for coherent measurement frame.");
69
70
71}
72
73// Initialize method:
75{
76 ATH_MSG_INFO("ReFitTrack::initialize()");
77
78 if (m_ITrackFitter.retrieve().isFailure()) {
79 msg(MSG::FATAL) << "Failed to retrieve tool "<<m_ITrackFitter.typeAndName()<<endmsg;
80 return StatusCode::FAILURE;
81 } else
82 msg(MSG::INFO) << "Retrieved general fitter " << m_ITrackFitter.typeAndName() << endmsg;
83
84 if (!m_ITrackFitterTRT.name().empty()){
85 if (m_ITrackFitterTRT.retrieve().isFailure()) {
86 msg(MSG::FATAL) << "Failed to retrieve tool " << m_ITrackFitterTRT.typeAndName()<<endmsg;
87 return StatusCode::FAILURE;
88 } else
89 msg(MSG::INFO) << "Retrieved fitter for TRT-only tracks " << m_ITrackFitterTRT.typeAndName() << endmsg;
90 }
91
92 if (!m_trkSummaryTool.name().empty()){
93 if (m_trkSummaryTool.retrieve().isFailure()) {
94 msg(MSG::FATAL) << "Failed to retrieve tool " << m_trkSummaryTool << endmsg;
95 return StatusCode::FAILURE;
96 } else
97 msg(MSG::INFO) << "Retrieved tool " << m_trkSummaryTool.typeAndName() << endmsg;
98 }
99 if ( m_assoTool.retrieve().isFailure() ) {
100 msg(MSG::FATAL) << "Failed to retrieve tool " << m_assoTool << endmsg;
101 return StatusCode::FAILURE;
102 } else ATH_MSG_INFO("Retrieved tool " << m_assoTool);
103
104 if (!m_trkSelectorTool.empty()){
105 if (m_trkSelectorTool.retrieve().isFailure()) {
106 msg(MSG::FATAL) << "Failed to retrieve tool " << m_trkSelectorTool << ". No Track Selection will be done." << endmsg;
107 return StatusCode::FAILURE;
108 } else
109 msg(MSG::INFO) << "Retrieved tool " << m_trkSelectorTool.typeAndName() << endmsg;
110 }
111
112 // beam conditions service
113 if (m_constrainFitMode) {
114 ATH_CHECK(m_beamSpotKey.initialize());
115 }
116
117 // extrapolator
118 if (m_constrainFitMode && m_extrapolator.retrieve().isFailure()){
119 ATH_MSG_FATAL( "Failed to retrieve " << m_extrapolator );
120 return StatusCode::FAILURE;
121 }
122
123 // Configuration of the material effects
125
126 ATH_CHECK( m_trackName.initialize() );
127 ATH_CHECK( m_vxContainerName.initialize() );
128 ATH_CHECK( m_newTrackName.initialize() );
129 ATH_CHECK( m_pixelDetEleCollKey.initialize() );
130 ATH_CHECK( m_SCTDetEleCollKey.initialize() );
131
132 return StatusCode::SUCCESS;
133}
134
135// Execute method:
136StatusCode Trk::ReFitTrack::execute(const EventContext& ctx)
137{
138 ATH_MSG_DEBUG ("ReFitTrack::execute()");
139 std::unique_ptr<Trk::PRDtoTrackMap> prd_to_track_map(m_assoTool->createPRDtoTrackMap());
141
142 if (!tracks.isValid()){
143 msg(MSG::ERROR) <<"Track collection named " << m_trackName.key()
144 << " not found, exit ReFitTrack." << endmsg;
145 return StatusCode::SUCCESS;
146 } else {
147 ATH_MSG_DEBUG ("Tracks collection '" << m_trackName.key() << "' retrieved from EventStore.");
148 }
149
150 ATH_MSG_DEBUG("Track fit configured with constrain option : " << m_constrainFitMode );
151
152 // Handle the various possible constraints.
153 std::unique_ptr<const Trk::PerigeeSurface> constrainSf;
154 std::unique_ptr<const Trk::RecVertex> constrainVx;
155
156 if (m_constrainFitMode > 0){
157 constrainVx.reset( setupConstrainedFit() );
158 }
159
160 // constrain surface (either BS of VTX)
161 if (constrainVx) {
162 constrainSf = std::make_unique<Trk::PerigeeSurface>( constrainVx->position());
163 }
164
166
167 // create new collection of tracks to write in storegate
168 // auto newtracks = std::make_unique<TrackCollection>();
169 std::vector<std::unique_ptr<Trk::Track> > new_tracks;
170
171 // loop over tracks
172 for (TrackCollection::const_iterator itr = (*tracks).begin(); itr < (*tracks).end(); ++itr){
173
174 ATH_MSG_VERBOSE ("input track:" << **itr);
175
176 if (m_usetrackhypo) hypo=(**itr).info().particleHypothesis();
177 std::unique_ptr<Trk::Track> newtrack;
178 bool trtonly=false;
179
180 bool passedSelection = true;
181 // check whether the track passes the selection
182 if (*itr && !m_trkSelectorTool.empty()){
183 passedSelection = m_trkSelectorTool->decision(**itr);
184 }
185
186
187 if (!m_ITrackFitterTRT.name().empty() && !m_trkSummaryTool.name().empty()){
188 ATH_MSG_VERBOSE ("Creating summary");
189 // @TODO does not need PRDtoTrackMap
190 unsigned int n_trt_hits;
191 if ((*itr)->trackSummary()) {
192 n_trt_hits = (*itr)->trackSummary()->get(numberOfTRTHits);
193 }
194 else {
195 std::unique_ptr<const Trk::TrackSummary> summary( m_trkSummaryTool->summaryNoHoleSearch(ctx, **itr) );
196 n_trt_hits = summary->get(numberOfTRTHits);
197 }
198 if ( (**itr).measurementsOnTrack()->size() - n_trt_hits<3 )
199 trtonly=true;
200 }
201
202 if (passedSelection) {
203 ATH_MSG_VERBOSE ("Passed selection");
204
205 const Trk::Perigee* origPerigee = (*itr)->perigeeParameters();
206 if (origPerigee){
207 double od0 = origPerigee->parameters()[Trk::d0];
208 double oz0 = origPerigee->parameters()[Trk::z0];
209 double ophi0 = origPerigee->parameters()[Trk::phi0];
210 double otheta = origPerigee->parameters()[Trk::theta];
211 double oqOverP = origPerigee->parameters()[Trk::qOverP];
212 ATH_MSG_DEBUG ("Original parameters " << od0 << " " << oz0 << " " << ophi0 << " " << otheta << " " << oqOverP);
213 }
214
215 // create a measurement for the beamspot
216 if (constrainVx && origPerigee){
217
218 ATH_MSG_VERBOSE ("Creating measurement for beamspot.");
219 // extrapolate the track to the vertex -- for consistent Measurement frame
220 std::unique_ptr<const Trk::TrackParameters> tp( m_extrapolator->extrapolateTrack(ctx,
221 **itr,
222 *constrainSf,
224 const Trk::Perigee* tpConstrainedSf = dynamic_cast<const Trk::Perigee*>(tp.get());
225 // create the vertex/beamsptOnTrack
226 std::unique_ptr<Trk::VertexOnTrack> bsvxOnTrack( tpConstrainedSf ? new Trk::VertexOnTrack(*constrainVx,*tpConstrainedSf) : nullptr );
227 std::vector<const MeasurementBase*> vec;
228 if (tpConstrainedSf) vec.push_back(bsvxOnTrack.get() );
229 // get the measurmentsOnTrack
230 const DataVector<const MeasurementBase>* measurementsOnTracks = (**itr).measurementsOnTrack();
231 // get the outliersOnTrack, needs sorting
233 // clone measurements and outliers into the track
234 DataVector<const MeasurementBase>::const_iterator measIter = measurementsOnTracks->begin();
235 DataVector<const MeasurementBase>::const_iterator measIterEnd = measurementsOnTracks->end();
236 for ( ; measIter != measIterEnd; ++measIter)
237 vec.push_back((*measIter));
238 // if - protect the outliers ...
239 // const DataVector<const MeasurementBase>* outliersOnTracks = (**itr).outliersOnTrack();
240 // measIter = outliersOnTracks->begin();
241 // measIterEnd = outliersOnTracks->end();
242 // for ( ; measIter != measIterEnd; ++measIter)
243 // vec.push_back((*measIter));
244 // refit with the beamspot / vertex
245 newtrack = ((trtonly ? m_ITrackFitterTRT : m_ITrackFitter)
246 ->fit(ctx,
247 vec,
248 *origPerigee,
250 hypo));
251 // now delete the vsvx
252 } else {
253 newtrack =
254 ((trtonly ? m_ITrackFitterTRT : m_ITrackFitter)
255 ->fit(ctx, **itr, m_runOutlier, hypo));
256 }
257 } // passed selection
258
259 if (msgLvl(MSG::DEBUG)) {
260 if (!newtrack) ATH_MSG_DEBUG ("Refit Failed");
261 else {
262
263 // ATH_MSG_VERBOSE ("re-fitted track:" << *newtrack);
264 const Trk::Perigee *aMeasPer=
265 dynamic_cast<const Trk::Perigee*>(newtrack->perigeeParameters () );
266 if (aMeasPer==nullptr){
267 msg(MSG::ERROR) << "Could not get Trk::MeasuredPerigee" << endmsg;
268 continue;
269 }
270 double d0 = aMeasPer->parameters()[Trk::d0];
271 double z0 = aMeasPer->parameters()[Trk::z0];
272 double phi0 = aMeasPer->parameters()[Trk::phi0];
273 double theta = aMeasPer->parameters()[Trk::theta];
274 double qOverP = aMeasPer->parameters()[Trk::qOverP];
275 ATH_MSG_DEBUG ("Refitted parameters " << d0 << " " << z0 << " " << phi0 << " " << theta << " " << qOverP);
276 }
277 }
278
279 if (newtrack) {
280 new_tracks.push_back(std::move(newtrack));
281 }
282
283 }
284
285 ATH_MSG_VERBOSE ("Add PRDs to assoc tool.");
286
287 // recreate the summaries on the final track collection with correct PRD tool
288 for(const std::unique_ptr<Trk::Track> &new_track : new_tracks ) {
289 if((m_assoTool->addPRDs(*prd_to_track_map, *new_track)).isFailure()) {ATH_MSG_WARNING("Failed to add PRDs to map");}
290 }
291
292 ATH_MSG_VERBOSE ("Recalculate the summary");
293 // and copy tracks from vector of non-const tracks to collection of const tracks
294 std::unique_ptr<TrackCollection> new_track_collection = std::make_unique<TrackCollection>();
295 new_track_collection->reserve(new_tracks.size());
296 for(std::unique_ptr<Trk::Track> &new_track : new_tracks ) {
297 m_trkSummaryTool->computeAndReplaceTrackSummary(ctx, *new_track, false /* DO NOT suppress hole search*/);
298 new_track_collection->push_back(std::move(new_track));
299 }
300
301 ATH_MSG_VERBOSE ("Save tracks");
302 ATH_CHECK(SG::WriteHandle<TrackCollection>(m_newTrackName, ctx).record(std::move(new_track_collection)));
303
304 return StatusCode::SUCCESS;
305}
306
307// Finalize method:
309{
310 msg(MSG::INFO) << "ReFitTrack::finalize()" << endmsg;
311 return StatusCode::SUCCESS;
312}
313
315{
316 // constrainVx
317 const Trk::RecVertex* constrainVx = nullptr;
318 if ( m_constrainFitMode == 2 ){
319 // Beamspot mode
321 // get vertex position and uncertainties from BeamCondSvc
322 constrainVx = new Trk::RecVertex(beamSpotHandle->beamVtx());
323 ATH_MSG_DEBUG("Track fit with BeamSpot constraint (x/y/z) = "
324 << constrainVx->position().x() << ", "
325 << constrainVx->position().y() << ", "
326 << constrainVx->position().z());
327 } else if (m_constrainFitMode ==1){
328 // Vertex mode
330 ATH_MSG_DEBUG("Track fit with vertex from collection '" << m_vxContainerName.key() << "'.");
331 if (!vxContainer.isValid())
332 ATH_MSG_WARNING("Track fit configured to use vertex constraint, but '" << m_vxContainerName.key() << "' could not be retrieved.");
333 else if (vxContainer->size() == 1 ) {
334 ATH_MSG_WARNING("Track fit configured to use vertex constraint, but '" << m_vxContainerName.key() << "' contains only dummy vertex.");
335 } else {
336 // only refit to the 'signal' vertex
337 constrainVx = new Trk::RecVertex( (*vxContainer)[0]->recVertex() ); // Just copy it to simplify ownership.
338 ATH_MSG_DEBUG("Track fit with Vertex constraint (x/y/z) = "
339 << constrainVx->position().x() << ", "
340 << constrainVx->position().y() << ", "
341 << constrainVx->position().z());
342 }
343 } else {
344 ATH_MSG_WARNING("Unknown constraint mode: "<< m_constrainFitMode);
345 }
346 return constrainVx;
347}
#define endmsg
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_FATAL(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
std::vector< size_t > vec
AthAlgorithm(const std::string &name, ISvcLocator *pSvcLocator)
Constructor with parameters:
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T, V, H > &t)
bool msgLvl(const MSG::Level lvl) const
Derived DataVector<T>.
Definition DataVector.h:795
DataModel_detail::const_iterator< DataVector > const_iterator
Definition DataVector.h:838
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.
virtual bool isValid() override final
Can the handle be successfully dereferenced?
ToolHandle< Trk::IExtendedTrackSummaryTool > m_trkSummaryTool
the track summary tool
Definition ReFitTrack.h:80
const TrackCollection * m_tracks
job option for main track collection
Definition ReFitTrack.h:77
ToolHandle< Trk::ITrackSelectorTool > m_trkSelectorTool
the track selector tool
Definition ReFitTrack.h:85
Trk::RunOutlierRemoval m_runOutlier
switch whether to run outlier logics or not
Definition ReFitTrack.h:71
const Trk::RecVertex * setupConstrainedFit()
virtual StatusCode finalize()
ToolHandle< Trk::IPRDtoTrackMapTool > m_assoTool
Tool to create and populate PRD to track association maps e.g.
Definition ReFitTrack.h:82
Trk::ParticleHypothesis m_ParticleHypothesis
nomen est omen
Definition ReFitTrack.h:73
SG::ReadHandleKey< TrackCollection > m_trackName
Definition ReFitTrack.h:67
SG::ReadCondHandleKey< InDet::BeamSpotData > m_beamSpotKey
Definition ReFitTrack.h:89
virtual StatusCode initialize()
SG::ReadCondHandleKey< InDetDD::SiDetectorElementCollection > m_SCTDetEleCollKey
Definition ReFitTrack.h:92
SG::ReadHandleKey< VxContainer > m_vxContainerName
Definition ReFitTrack.h:88
ToolHandle< Trk::ITrackFitter > m_ITrackFitter
the refit tool
Definition ReFitTrack.h:78
SG::ReadCondHandleKey< InDetDD::SiDetectorElementCollection > m_pixelDetEleCollKey
Definition ReFitTrack.h:91
int m_matEffects
type of material interaction in extrapolation
Definition ReFitTrack.h:72
unsigned int m_constrainFitMode
0 - not constrained, 1 - vertex, 2 - beamspot
Definition ReFitTrack.h:87
SG::WriteHandleKey< TrackCollection > m_newTrackName
Definition ReFitTrack.h:68
bool m_usetrackhypo
Fit using particle hypothesis from input track.
Definition ReFitTrack.h:95
virtual StatusCode execute(const EventContext &ctx)
Execute method.
ToolHandle< Trk::IExtrapolator > m_extrapolator
the extrapolator for the consistent measurement frame
Definition ReFitTrack.h:93
ToolHandle< Trk::ITrackFitter > m_ITrackFitterTRT
the TRT refit tool
Definition ReFitTrack.h:79
Trk::RecVertex inherits from Trk::Vertex.
Definition RecVertex.h:44
Class to handle Vertex On Tracks, it inherits from the common MeasurementBase.
const Amg::Vector3D & position() const
return position of vertex
Definition Vertex.cxx:63
constexpr ParticleHypothesis particle[PARTICLEHYPOTHESES]
the array of masses
Ensure that the ATLAS eigen extensions are properly loaded.
@ anyDirection
ParametersT< TrackParametersDim, Charged, PerigeeSurface > Perigee
@ phi0
Definition ParamDefs.h:65
@ theta
Definition ParamDefs.h:66
@ qOverP
perigee
Definition ParamDefs.h:67
@ d0
Definition ParamDefs.h:63
@ z0
Definition ParamDefs.h:64
ParticleHypothesis
Enumeration for Particle hypothesis respecting the interaction with material.
MsgStream & msg
Definition testRead.cxx:32