ATLAS Offline Software
Loading...
Searching...
No Matches
AlignAlg.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
8
11
12#include "TFile.h"
13
14namespace Trk {
15
16//___________________________________________________________________________
18{
19
20 if (m_ntuple && m_solveOnly) {
21 ATH_MSG_DEBUG("closing file");
22 m_ntuple->Close();
23 ATH_MSG_DEBUG("file closed");
24 }
25}
26
27//___________________________________________________________________________
29{
30 ATH_MSG_INFO("AlignAlg::initialize()");
31
32 // Get GeometryManagerTool
33 if ( m_geometryManagerTool.retrieve().isFailure() ) {
34 msg(MSG::FATAL)<<"Failed to retrieve tool " << m_geometryManagerTool
35 << endmsg;
36 return StatusCode::FAILURE;
37 }
38 else
39 ATH_MSG_INFO("Retrieved tool " << m_geometryManagerTool);
40
41 // some tools are not needed if only solving is done
42 if ( !m_solveOnly ) {
43
44 // get TrackCollectionProvider
45 if (m_trackCollectionProvider.retrieve().isSuccess())
47 else{
48 msg(MSG::FATAL) << "Could not get " << m_trackCollectionProvider << endmsg;
49 return StatusCode::FAILURE;
50 }
51
52 // get AlignTrackPreProcessor
53 if (m_alignTrackPreProcessor.retrieve().isSuccess())
55 else{
56 msg(MSG::FATAL) << "Could not get " << m_alignTrackPreProcessor << endmsg;
57 return StatusCode::FAILURE;
58 }
59
60 // Get AlignTrackCreator tool
61 if ( m_alignTrackCreator.retrieve().isFailure() ) {
62 msg(MSG::FATAL)<<"Failed to retrieve tool "<<m_alignTrackCreator<<endmsg;
63 return StatusCode::FAILURE;
64 }
65 else
66 ATH_MSG_INFO("Retrieved tool "<<m_alignTrackCreator);
67
68 // Get AlignTrackDresser tool
69 if ( m_alignTrackDresser.retrieve().isFailure() ) {
70 msg(MSG::FATAL)<<"Failed to retrieve tool "<<m_alignTrackDresser<<endmsg;
71 return StatusCode::FAILURE;
72 }
73 else
74 ATH_MSG_INFO("Retrieved tool "<<m_alignTrackDresser);
75
76 // Get TrkAlign::FillTrack
77 if (m_writeNtuple) {
78 if ( m_fillNtupleTool.retrieve().isFailure() ) {
79 msg(MSG::FATAL)<<"Failed to retrieve tool "<<m_fillNtupleTool<<endmsg;
80 return StatusCode::FAILURE;
81 }
82 else
83 ATH_MSG_INFO("Retrieved tool "<<m_fillNtupleTool);
84 }
85 }
86
87 // Get AlignTool tool
88 if ( m_alignTool.retrieve().isFailure() ) {
89 msg(MSG::FATAL)<<"Failed to retrieve tool "<<m_alignTool<<endmsg;
90 return StatusCode::FAILURE;
91 }
92 else
93 ATH_MSG_INFO("Retrieved tool "<<m_alignTool);
94
95 // Get TrkAlignDataBaseTool
96 if ( m_trkAlignDBTool.retrieve().isFailure() ) {
97 msg(MSG::FATAL)<<"Failed to retrieve tool "<<m_trkAlignDBTool<<endmsg;
98 return StatusCode::FAILURE;
99 }
100 else
101 ATH_MSG_INFO("Retrieved tool "<<m_trkAlignDBTool);
102
103 // Alignment Level
104 // Number of layers in the superstructures
106 msg(MSG::FATAL)<<"AlignSolveLevel could be 1, 2 or 3"<<endmsg;
107 return StatusCode::FAILURE;
108 }
109
110 // open logfile
111 if(m_writeLogfile) {
112 std::ostream * out = &std::cout;
113 std::ofstream * ofile = new std::ofstream(m_logfileName.value().c_str());
114 if(!ofile->is_open()) {
115 msg(MSG::ERROR)<<"Couldn't open logfile. Writing to standard output."<<endmsg;
116 delete ofile;
117 }
118 else
119 out = ofile;
120
121 m_logStream = out;
122
123 // set logStream in the tools
124 m_geometryManagerTool->setLogStream(m_logStream);
125 m_alignTool->setLogStream(m_logStream);
126 m_trkAlignDBTool->setLogStream(m_logStream);
127 if( !m_solveOnly ) {
130 m_alignTrackCreator->setLogStream(m_logStream);
131 m_alignTrackDresser->setLogStream(m_logStream);
132 if (m_writeNtuple)
133 m_fillNtupleTool->setLogStream(m_logStream);
134 }
135 }
136
137 return StatusCode::SUCCESS;
138}
139
140//___________________________________________________________________________
141StatusCode AlignAlg::start()
142{
143 ATH_MSG_DEBUG("AlignAlg::start()");
144
145 if (m_writeNtuple) {
146 m_ntuple = new TFile((m_filepath.value()+m_filename.value()).c_str(),"RECREATE");
147 // set the ntuple points of the tools
151 m_alignTool->setNtuple(m_ntuple);
152 m_trkAlignDBTool->setNtuple(m_ntuple);
153 m_fillNtupleTool->setNtuple(m_ntuple);
154 }
155
156 ATH_MSG_DEBUG("reading geometry");
157 int NDoF = m_geometryManagerTool->ReadGeometry(m_alignSolveLevel);
158 ATH_MSG_DEBUG("read geometry, "<<NDoF<<" alignment parameters");
159
160 m_geometryManagerTool->PrintModPositions();
161
162 // fix pathname (used to be m_pathbin+m+prefixName)
163 // m_geometryManagerTool->StoreModPositions(pathname);
164
165 StatusCode sc = m_alignTool->firstEventInitialize();
166
167 if (sc != StatusCode::SUCCESS) {
168 msg(MSG::FATAL)<<"problem with alignTool->firstEventInitialize()"<<endmsg;
169 return StatusCode::FAILURE;
170 }
171
172 if(m_solveOnly) {
173 ATH_MSG_DEBUG("getting data from files");
174 m_alignTool->accumulateFromFiles();
175 }
176
177 return StatusCode::SUCCESS;
178}
179
180//___________________________________________________________________________
181StatusCode AlignAlg::execute()
182{
183 ATH_MSG_DEBUG("AlignAlg::execute()");
184
185 const EventContext& ctx = Gaudi::Hive::currentContext();
186
187 // increment number of events
188 m_nevents++;
189
190 if(m_solveOnly) {
191 if (m_nevents<=1)
192 return StatusCode::SUCCESS;
193 else {
194 // return FAILURE to make sure that the job ends after 1st event
195 msg(MSG::INFO)<<"Only doing the solving. Aborting event processing after first event."<<endmsg;
196 msg(MSG::INFO)<<"Following ERROR message is not a problem..."<<endmsg;
197 return StatusCode::FAILURE;
198 }
199 }
200
201
203 // 1. apply event selection, select hits, refit tracks
205 // m_trackSelector->cleanTracks();
206
207 // loop over tracks
208 TrackCollection * alignTracks;
209 const TrackCollection* originalTracks;
210
211 StatusCode sc = m_trackCollectionProvider->trackCollection(originalTracks);
212 if (!originalTracks || sc.isFailure()) return sc;
213
214 if(originalTracks->empty()) {
215 ATH_MSG_DEBUG("found no tracks");
216 return StatusCode::SUCCESS;
217 }
218
219 // increment number of processed tracks
220 m_ntracks += originalTracks->size();
221 ATH_MSG_DEBUG("nTracks: "<<m_ntracks);
222
223 // process track collection and create collection of AlignTracks
224 alignTracks = m_alignTrackPreProcessor->processTrackCollection(originalTracks);
225
226 // method returns zero or empty collection
227 if (!alignTracks || alignTracks->empty()) {
228 ATH_MSG_DEBUG("processTrackCollection() returned empty Track collection.");
229 return StatusCode::SUCCESS;
230 }
231
232 // increment number of selected tracks
233 m_ntracksSel += alignTracks->size();
234
235 // loop over AlignTracks and pass to AlignTrackCreator and Dresser for more processing
236 int itrk = 0;
237 for (Track* trk : *alignTracks) { // first loop over AlignTracks
238
239 ATH_MSG_DEBUG("1st loop: Processing track "<<itrk);
240
241 // each track in the collection HAS to be an AlignTrack
242 AlignTrack * alignTrack = dynamic_cast<AlignTrack*>(trk);
243 if(!alignTrack) {
244 msg(MSG::FATAL)<<"Track is not an AlignTrack. PreProcessor returns TrackCollection but"
245 <<"it has to be filled with AlignTracks. Otherwise the code doesn't work."<<endmsg;
246 return StatusCode::FAILURE;
247 }
248
249 // process AlignTrack
250 bool success = m_alignTrackCreator->processAlignTrack(alignTrack);
251 if (!success) {
252 ATH_MSG_DEBUG("processAlignTrack() returned FALSE, Skipping the track.");
253 continue;
254 }
255
257
258 // dress AlignTrack with extra info
259 success = m_alignTrackDresser->dressAlignTrack(alignTrack);
260 ATH_MSG_DEBUG("back from dressing AlignTrack");
261
262 if (!success) {
263 ATH_MSG_DEBUG("dressAlignTrack() returned FALSE, Skipping the track.");
264 continue;
265 }
266
268
269 // accumulate vertex fit information:
270 m_alignTrackPreProcessor->accumulateVTX(alignTrack);
271
272 ATH_MSG_DEBUG("back from accumulateVTX");
273
274 } // end of the first loop over AlignTracks
275
276
277 // solve existing vertices:
278 m_alignTrackPreProcessor->solveVTX();
279
280
281 for (Track* trk : *alignTracks) { // second loop over AlignTracks
282
283 ATH_MSG_DEBUG("2nd loop: Processing tracks");
284
285 // each track in the collection HAS to be an AlignTrack
286 AlignTrack * alignTrack = dynamic_cast<AlignTrack*>(trk);
287 if(!alignTrack) {
288 msg(MSG::FATAL)<<"Track is not an AlignTrack. PreProcessor returns TrackCollection but"
289 <<"it has to be filled with AlignTracks. Otherwise the code doesn't work."<<endmsg;
290 return StatusCode::FAILURE;
291 }
292
293
294 // check if the dressing was successful:
295 const Amg::SymMatrixX * ptrWeights = alignTrack->weightMatrix(); //Symmetric Matrix
296 if ( !ptrWeights ) {
297 ATH_MSG_DEBUG("dressAlignTrack failed, Skipping the track.");
298 continue;
299 }
300
301 // check if the eventual VTX solving was successful:
302 const AlignVertex * ptrVertex = alignTrack->getVtx();
303 if ( ptrVertex && (ptrVertex->type() < AlignVertex::Refitted) ) {
304 ATH_MSG_DEBUG("Vertex fit failed for this track. Skipping the track.");
305 continue;
306 }
307
308
309 // pass to AlignTool for processing of track information
310 bool success = m_alignTool->accumulate(alignTrack);
311 ATH_MSG_DEBUG("back from accumulate");
312
313 if (!success) {
314 ATH_MSG_DEBUG("accumulate() returned FALSE, Skipping the track.");
315 continue;
316 }
317
318 ATH_MSG_DEBUG("calling fillntuple::dumptrack");
319
321
322 if (m_writeNtuple)
323 m_fillNtupleTool->dumpTrack(ctx, itrk,alignTrack);
324 ATH_MSG_DEBUG("done with track "<<itrk);
325
326 } // end of the second loop over AlignTracks
327
328
329
330
331 // record the collection in the StoreGate
332 // the ownership is transferred to StoreGate so we don't delete the collection here
333 if(evtStore()->record(alignTracks,m_alignTracksName,false).isFailure()) {
334 ATH_MSG_ERROR("Unable to record "<<m_alignTracksName<<" in the StoreGate");
335 delete alignTracks;
336 alignTracks = nullptr;
337 }
338 if (alignTracks) ATH_MSG_DEBUG("Recorded \'"<<m_alignTracksName<<"\' with size "<<alignTracks->size()<<" to StoreGate");
339
340 ATH_MSG_DEBUG("returning SUCCESS");
341 return StatusCode::SUCCESS;
342}
343
344//___________________________________________________________________________
345StatusCode AlignAlg::stop()
346{
347 ATH_MSG_INFO("AlignAlg::stop()");
348
349 if(m_logStream && !m_solveOnly) {
350 *m_logStream<<"*************************************************************"<<std::endl;
351 *m_logStream<<"****** Alignment Job summary ******"<<std::endl;
352 *m_logStream<<"*************************************************************"<<std::endl;
353 *m_logStream<<"*"<<std::endl;
354 *m_logStream<<"* number of processed events: "<<m_nevents<<std::endl;
355 *m_logStream<<"* number of processed tracks: "<<m_ntracks<<std::endl;
356 *m_logStream<<"* number of selected tracks: "<<m_ntracksSel<<std::endl;
357 *m_logStream<<"* number of tracks processed: "<<m_ntracksProc<<std::endl;
358 *m_logStream<<"* number of tracks dressed: "<<m_ntracksDress<<std::endl;
359 *m_logStream<<"* number of tracks accumulated: "<<m_ntracksAccum<<std::endl;
360
361 *m_logStream<<"*"<<std::endl;
362
363 m_alignTrackPreProcessor->printSummary();
364 m_trackCollectionProvider->printSummary();
365 }
366
367 // fill ntuple for all tools
368 if (m_writeNtuple) {
369 ATH_MSG_INFO("calling alignTool->fillNtuple()");
370 if(m_alignTool->fillNtuple().isFailure()) {
371 msg(MSG::ERROR)<<"problem with alignTool fillNtuple()!"<<endmsg;
372 return StatusCode::FAILURE;
373 }
374 ATH_MSG_INFO("calling fillNtupleTool->fillNtuple()");
375 if(m_fillNtupleTool->fillNtuple().isFailure()) {
376 msg(MSG::ERROR)<<"problem with fillNtupleTool fillNtuple()!"<<endmsg;
377 return StatusCode::FAILURE;
378 }
379
380 ATH_MSG_INFO("calling alignTrackPreProcessor->fillNtuple()");
381 if(m_alignTrackPreProcessor->fillNtuple().isFailure()) {
382 msg(MSG::ERROR)<<"problem with alignTrackPreProcessor fillNtuple()!"<<endmsg;
383 return StatusCode::FAILURE;
384 }
385
386 ATH_MSG_INFO("calling trackCollectionProvider->fillNtuple()");
387 if(m_trackCollectionProvider->fillNtuple().isFailure()) {
388 msg(MSG::ERROR)<<"problem with trackCollectionProvider fillNtuple()!"<<endmsg;
389 return StatusCode::FAILURE;
390 }
391 }
392
393 if(!m_solveOnly && m_ntracksSel==0) {
394 msg(MSG::WARNING)<<"No tracks passed track selection. No solving."<<endmsg;
395 return StatusCode::SUCCESS;
396 }
397
398 ATH_MSG_INFO("calling trkAlignDBTool->preSolve()");
399 if (m_trkAlignDBTool->preSolve().isFailure()) {
400 msg(MSG::FATAL)<<"failure in ITrkAlignDBTool->preSolve()"<<endmsg;
401 return StatusCode::FAILURE;
402 }
403
404 ATH_MSG_INFO("calling alignTool->solve()");
405 if (m_alignTool->solve().isFailure()) {
406 msg(MSG::FATAL)<<"failure in IAlignTool->solve()"<<endmsg;
407 return StatusCode::FAILURE;
408 }
409
410 ATH_MSG_INFO("calling trkAlignDBTool->writeAlignPar()");
411 m_trkAlignDBTool->writeAlignPar();
412
413 return StatusCode::SUCCESS;
414}
415
416//___________________________________________________________________________
418{
419 ATH_MSG_INFO("AlignAlg::finalize()");
420
421 // close the logfile
422 if(m_writeLogfile) {
423 if(std::ofstream * ofs = dynamic_cast<std::ofstream *>(m_logStream)) {
424 if(ofs->is_open()) {
425 *ofs<<std::flush;
426 ofs->close();
427 }
428 delete m_logStream;
429 }
430 }
431
432 if (m_writeNtuple) {
433 ATH_MSG_INFO("closing "<<m_ntuple->GetName());
434 m_ntuple->Close();
435 }
436
437 return StatusCode::SUCCESS;
438}
439
440
441}
#define endmsg
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_DEBUG(x)
static Double_t sc
DataVector< Trk::Track > TrackCollection
This typedef represents a collection of Trk::Track objects.
MsgStream & msg() const
size_type size() const noexcept
Returns the number of elements in the collection.
bool empty() const noexcept
Returns true if the collection is empty.
virtual StatusCode finalize()
finalize method
Definition AlignAlg.cxx:417
StringProperty m_filename
Definition AlignAlg.h:109
std::ostream * m_logStream
logfile output stream
Definition AlignAlg.h:125
virtual StatusCode initialize()
initialize method
Definition AlignAlg.cxx:28
virtual StatusCode start()
set up geometry and prepare the tools
Definition AlignAlg.cxx:141
virtual StatusCode stop()
processes information accumulated in execute method to determine alignment parameters
Definition AlignAlg.cxx:345
BooleanProperty m_writeLogfile
Definition AlignAlg.h:121
StringProperty m_filepath
Definition AlignAlg.h:110
ToolHandle< ITrackCollectionProvider > m_trackCollectionProvider
Definition AlignAlg.h:70
StringProperty m_alignTracksName
Definition AlignAlg.h:134
int m_ntracksProc
number of tracks successfully processed
Definition AlignAlg.h:130
ToolHandle< IAlignTool > m_alignTool
Pointer to alignTool.
Definition AlignAlg.h:90
virtual ~AlignAlg()
destructor
Definition AlignAlg.cxx:17
int m_ntracksAccum
number of tracks successfully accumulated
Definition AlignAlg.h:132
int m_nevents
number of processed events
Definition AlignAlg.h:127
virtual StatusCode execute()
loops over tracks in event, and accumulates information necessary for alignmnet
Definition AlignAlg.cxx:181
int m_ntracksSel
number of selected tracks
Definition AlignAlg.h:129
BooleanProperty m_writeNtuple
Definition AlignAlg.h:114
int m_ntracks
number of processed tracks
Definition AlignAlg.h:128
StringProperty m_logfileName
Definition AlignAlg.h:123
ToolHandle< IAlignTrackPreProcessor > m_alignTrackPreProcessor
Pointer to AlignTrackPreProcessor, used to select hits on tracks and/or tracks before passing to Alig...
Definition AlignAlg.h:75
BooleanProperty m_solveOnly
Definition AlignAlg.h:112
PublicToolHandle< IGeometryManagerTool > m_geometryManagerTool
Pointer to GeometryManagerTool, used to get lists of chambers for which alignment parameters will be ...
Definition AlignAlg.h:95
ToolHandle< IAlignTrackDresser > m_alignTrackDresser
Pointer to alignTrackDresser, used to add residuals, derivatives, etc.
Definition AlignAlg.h:85
ToolHandle< IFillNtupleTool > m_fillNtupleTool
Pointer to FillNtupleTool, used to write track information to ntuple.
Definition AlignAlg.h:104
int m_ntracksDress
number of tracks successfully dressed
Definition AlignAlg.h:131
TFile * m_ntuple
output ntuple
Definition AlignAlg.h:120
ToolHandle< ITrkAlignDBTool > m_trkAlignDBTool
Pointer to TrkAlignDBTool, used for reading/writing alignment parameters from/to the database.
Definition AlignAlg.h:100
ToolHandle< IAlignTrackCreator > m_alignTrackCreator
Pointer to alignTrackCreator, used to convert Trk::Track to vector of AlignTrack.
Definition AlignAlg.h:80
IntegerProperty m_alignSolveLevel
Definition AlignAlg.h:117
const AlignVertex * getVtx() const
set and get pointer to the associated vertex
Definition AlignTrack.h:188
const Amg::SymMatrixX * weightMatrix() const
Weight matrix is W from Si alignment (see Eqn.
Definition AlignTrack.h:156
@ Refitted
normally refitted, without adding any pseudo-measurement
Definition AlignVertex.h:46
AlignVertexType type() const
get and set the refit type
Definition AlignVertex.h:85
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > SymMatrixX
Ensure that the ATLAS eigen extensions are properly loaded.