ATLAS Offline Software
ReFitterAlg.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include "ReFitterAlg.h"
6 
7 // ATHENA
8 #include "GaudiKernel/ListItem.h"
9 
12 
13 
15 #include "Identifier/Identifier.h"
16 // Event includes
20 #include "TrkTrack/Track.h"
21 
22 // STL
23 #include <memory>
24 #include <vector>
25 #include <fstream>
26 #include <string>
27 
28 using namespace Acts::UnitLiterals;
29 
30 namespace ActsTrk {
31 
32 ReFitterAlg::ReFitterAlg(const std::string &name,
33  ISvcLocator *pSvcLocator)
34  : AthReentrantAlgorithm(name, pSvcLocator){}
35 
37 
38  ATH_MSG_DEBUG(name() << "::" << __FUNCTION__);
39  ATH_CHECK(m_actsFitter.retrieve());
40  ATH_CHECK(m_trackName.initialize());
41  ATH_CHECK(m_newTrackName.initialize());
42 
43  return StatusCode::SUCCESS;
44 }
45 
46 StatusCode ReFitterAlg::execute(const EventContext &ctx) const {
47 
48  ATH_MSG_DEBUG ("ReFitterAlg::execute()");
50 
51  if (!tracks.isValid()){
52  ATH_MSG_ERROR("Track collection named " << m_trackName.key() << " not found, exit ReFitTrack.");
53  return StatusCode::SUCCESS;
54  } else {
55  ATH_MSG_DEBUG ("Tracks collection '" << m_trackName.key() << "' retrieved from EventStore.");
56  }
57 
58  // Prepare the output data with MultiTrajectory
59  std::vector<std::unique_ptr<Trk::Track> > new_tracks;
60  new_tracks.reserve((*tracks).size());
61 
62  //Prepare the output for uncalibrated measurments; only used if m_doReFitFromPRD=True.
63  std::vector<const Trk::PrepRawData*> PrepRawDataSet;
64 
65  // Perform the fit for each input track
66  for (TrackCollection::const_iterator track = (*tracks).begin(); track < (*tracks).end(); ++track){
67 
68  if (m_doReFitFromPRD){ //Fit from PrepRawData measurments
69  const Trk::Track* trackPtr = *track;
70  const Trk::TrackParameters* trkPar_perigee = trackPtr->perigeeParameters() ;
71 
72  for (const Trk::TrackStateOnSurface* tsos : *trackPtr->trackStateOnSurfaces() ) {
73  //skipping outliers
74  if (!tsos->type(Trk::TrackStateOnSurface::Measurement)) continue;
75  const Trk::MeasurementBase* mesh = tsos->measurementOnTrack();
76  if (mesh == nullptr) continue;
77  const Trk::RIO_OnTrack* hit = dynamic_cast <const Trk::RIO_OnTrack*>(mesh);
78  if (hit == nullptr) continue;
79 
80  const Trk::PrepRawData* prd = hit->prepRawData() ;
81  PrepRawDataSet.push_back(prd);
82  }//end of tsos loop
83 
84  auto newtrack = m_actsFitter->fit(ctx, PrepRawDataSet , *trkPar_perigee );
85 
86  if (newtrack) {
87  if (msgLvl(MSG::VERBOSE)) {
88  msg(MSG::VERBOSE) << "ATLAS param : " << endmsg;
89  msg(MSG::VERBOSE) << *((**track).perigeeParameters()) << endmsg;
90  msg(MSG::VERBOSE) << *((**track).perigeeParameters()->covariance()) << endmsg;
91  msg(MSG::VERBOSE) << "ACTS param : " << endmsg;
92  msg(MSG::VERBOSE) << *(newtrack->perigeeParameters()) << endmsg;
93  msg(MSG::VERBOSE) << *(newtrack->perigeeParameters()->covariance()) << endmsg;
94 
95  msg(MSG::VERBOSE) << "ATLAS INFO : " << endmsg;
96  msg(MSG::VERBOSE) << *((**track).trackSummary()) << endmsg;
97  msg(MSG::VERBOSE) << "ACTS INFO : " << endmsg;
98  msg(MSG::VERBOSE) << *(newtrack->trackSummary()) << endmsg;
99  msg(MSG::VERBOSE) << "==========================" << endmsg;
100  }
101  new_tracks.push_back(std::move(newtrack));
102  }
103  else if (msgLvl(MSG::DEBUG)) { // newtrack might be equal to a nullptr
104  msg(MSG::DEBUG) << "The Acts Refitting (KF or GSF) has returned a nullptr. Below is information on the offending track." << endmsg; // TODO: solve the cases where we return a nullptr
105  msg(MSG::DEBUG) << "ATLAS param : " << endmsg;
106  msg(MSG::DEBUG) << *((**track).perigeeParameters()) << endmsg;
107  msg(MSG::DEBUG) << *((**track).perigeeParameters()->covariance()) << endmsg;
108 
109  msg(MSG::DEBUG) << "ATLAS INFO : " << endmsg;
110  msg(MSG::DEBUG) << *((**track).trackSummary()) << endmsg;
111  msg(MSG::DEBUG) << "==========================" << endmsg;
112  }
113  }
114 
115  else { //Fit from Rio_OnTrack measurments
116  auto newtrack = m_actsFitter->fit(ctx, (**track));
117 
118  if (newtrack) {
119  if (msgLvl(MSG::VERBOSE)) {
120  msg(MSG::VERBOSE) << "ATLAS param : " << endmsg;
121  msg(MSG::VERBOSE) << *((**track).perigeeParameters()) << endmsg;
122  msg(MSG::VERBOSE) << *((**track).perigeeParameters()->covariance()) << endmsg;
123  msg(MSG::VERBOSE) << "ACTS param : " << endmsg;
124  msg(MSG::VERBOSE) << *(newtrack->perigeeParameters()) << endmsg;
125  msg(MSG::VERBOSE) << *(newtrack->perigeeParameters()->covariance()) << endmsg;
126 
127  msg(MSG::VERBOSE) << "ATLAS INFO : " << endmsg;
128  msg(MSG::VERBOSE) << *((**track).trackSummary()) << endmsg;
129  msg(MSG::VERBOSE) << "ACTS INFO : " << endmsg;
130  msg(MSG::VERBOSE) << *(newtrack->trackSummary()) << endmsg;
131  msg(MSG::VERBOSE) << "==========================" << endmsg;
132  }
133  new_tracks.push_back(std::move(newtrack));
134  }
135  else if (msgLvl(MSG::DEBUG)) { // newtrack might be equal to a nullptr
136  msg(MSG::DEBUG) << "The Acts Refitting (KF or GSF) has returned a nullptr. Below is information on the offending track." << endmsg; // TODO: solve the cases where we return a nullptr
137  msg(MSG::DEBUG) << "ATLAS param : " << endmsg;
138  msg(MSG::DEBUG) << *((**track).perigeeParameters()) << endmsg;
139  msg(MSG::DEBUG) << *((**track).perigeeParameters()->covariance()) << endmsg;
140 
141  msg(MSG::DEBUG) << "ATLAS INFO : " << endmsg;
142  msg(MSG::DEBUG) << *((**track).trackSummary()) << endmsg;
143  msg(MSG::DEBUG) << "==========================" << endmsg;
144  }
145  }
146 
147  }
148 
149  // Create a new track collection with the refitted tracks
150  std::unique_ptr<TrackCollection> new_track_collection = std::make_unique<TrackCollection>();
151 
152  new_track_collection->reserve(new_tracks.size());
153  for(std::unique_ptr<Trk::Track> &new_track : new_tracks ) {
154  new_track_collection->push_back(std::move(new_track));
155  }
156 
157  ATH_MSG_DEBUG ("Saving tracks");
158  ATH_CHECK(SG::WriteHandle<TrackCollection>(m_newTrackName, ctx).record(std::move(new_track_collection)));
159  return StatusCode::SUCCESS;
160 }
161 
162 }
DataModel_detail::const_iterator
Const iterator class for DataVector/DataList.
Definition: DVLIterator.h:82
Trk::PrepRawDataSet
std::vector< const PrepRawData * > PrepRawDataSet
vector of clusters and drift circles
Definition: FitterTypes.h:26
TrackParameters.h
ReFitterAlg.h
ActsTrk::ReFitterAlg::m_newTrackName
SG::WriteHandleKey< TrackCollection > m_newTrackName
Definition: ReFitterAlg.h:45
Trk::Track
The ATLAS Track class.
Definition: Tracking/TrkEvent/TrkTrack/TrkTrack/Track.h:73
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
Trk::Track::trackStateOnSurfaces
const Trk::TrackStates * trackStateOnSurfaces() const
return a pointer to a const DataVector of const TrackStateOnSurfaces.
AthCommonMsg< Gaudi::Algorithm >::msgLvl
bool msgLvl(const MSG::Level lvl) const
Definition: AthCommonMsg.h:30
Trk::RIO_OnTrack
Definition: RIO_OnTrack.h:70
PrepRawData.h
AthReentrantAlgorithm
An algorithm that can be simultaneously executed in multiple threads.
Definition: AthReentrantAlgorithm.h:83
Track.h
ActsTrk::ReFitterAlg::m_trackName
SG::ReadHandleKey< TrackCollection > m_trackName
Definition: ReFitterAlg.h:44
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
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
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
TrackSummary.h
Trk::ParametersBase
Definition: ParametersBase.h:55
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
Trk::PrepRawData
Definition: PrepRawData.h:62
Trk::MeasurementBase
Definition: MeasurementBase.h:58
Trk::Track::perigeeParameters
const Perigee * perigeeParameters() const
return Perigee.
Definition: Tracking/TrkEvent/TrkTrack/src/Track.cxx:163
Trk::TrackStateOnSurface
represents the track state (measurement, material, fit parameters and quality) at a surface.
Definition: TrackStateOnSurface.h:71
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
RIO_OnTrack.h
PixelClusterContainer.h
ActsTrk::ReFitterAlg::m_doReFitFromPRD
Gaudi::Property< bool > m_doReFitFromPRD
Definition: ReFitterAlg.h:50
Trk::RIO_OnTrack::prepRawData
virtual const Trk::PrepRawData * prepRawData() const =0
returns the PrepRawData (also known as RIO) object to which this RIO_OnTrack is associated.
ActsTrk::ReFitterAlg::execute
virtual StatusCode execute(const EventContext &ctx) const override
Definition: ReFitterAlg.cxx:46
SG::WriteHandle
Definition: StoreGate/StoreGate/WriteHandle.h:76
ActsTrk::ReFitterAlg::initialize
virtual StatusCode initialize() override
Definition: ReFitterAlg.cxx:36
DEBUG
#define DEBUG
Definition: page_access.h:11
AthCommonMsg< Gaudi::Algorithm >::msg
MsgStream & msg() const
Definition: AthCommonMsg.h:24
ActsTrk
The AlignStoreProviderAlg loads the rigid alignment corrections and pipes them through the readout ge...
Definition: MuonDetectorBuilderTool.cxx:34
xAOD::track
@ track
Definition: TrackingPrimitives.h:512
python.Constants.VERBOSE
int VERBOSE
Definition: Control/AthenaCommon/python/Constants.py:14
ITrackFitter.h
ActsTrk::ReFitterAlg::m_actsFitter
ToolHandle< Trk::ITrackFitter > m_actsFitter
Definition: ReFitterAlg.h:42
Trk::TrackStateOnSurface::Measurement
@ Measurement
This is a measurement, and will at least contain a Trk::MeasurementBase.
Definition: TrackStateOnSurface.h:101