ATLAS Offline Software
Loading...
Searching...
No Matches
CETmaterial.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// CETmaterial.cxx, (c) ATLAS Detector software
8
9
10// Tracking
20
21#include <fstream>
22//================ Constructor =================================================
23
24Trk::CETmaterial::CETmaterial(const std::string& name, ISvcLocator* pSvcLocator)
25 :
26 AthAlgorithm(name,pSvcLocator),
27 m_chronoStatSvc( "ChronoStatSvc", name )
28{ }
29
30//================ Destructor =================================================
31
36
37
38//================ Initialization =================================================
39
41{
42 // Code entered here will be executed once at program start.
43
44 ATH_MSG_INFO( "initialize()" );
45
46 // Get Extrapolator from ToolService
47 if (m_extrapolator.retrieve().isFailure()) {
48 ATH_MSG_FATAL( "Could not retrieve Tool " << m_extrapolator << ". Exiting." );
49 return StatusCode::FAILURE;
50 }
51 if (m_extraprec.retrieve().isFailure()) {
52 ATH_MSG_FATAL( "Could not retrieve Tool " << m_extraprec << ". Exiting." );
53 return StatusCode::FAILURE;
54 }
55
56 ATH_MSG_INFO( "initialize() successful" );
57 return StatusCode::SUCCESS;
58}
59
60//================ Finalization =================================================
61
63{
64 // Code entered here will be executed once at the end of the program run.
65
66 if (m_chronoStatSvc) m_chronoStatSvc->chronoPrint("MS::scan");
67
68 return StatusCode::SUCCESS;
69}
70
71//================ Execution ====================================================
72
73StatusCode Trk::CETmaterial::execute(const EventContext& ctx)
74{
75 ATH_MSG_INFO( "execute()" );
76 // retrieve outer boundary cylinder surface
77 if (!m_outerBoundary) {
78 m_trackingGeometry = m_extrapolator->trackingGeometry();
79 m_outerBoundary = &(m_trackingGeometry->highestTrackingVolume()->boundarySurfaces()[2]->surfaceRepresentation());
80 if (!m_outerBoundary) {
81 ATH_MSG_FATAL( "Could not retrieve cylinder boundary from " << m_extrapolator << ". Exiting." );
82 return StatusCode::FAILURE;
83 }
84 ATH_MSG_INFO( "boundary retrieved " );
85 }
86
87 if (m_chronoStatSvc) m_chronoStatSvc->chronoStart("MS::scan");
88
89 // scan
90 std::vector<const TrackStateOnSurface*> material;
91 std::vector<const TrackStateOnSurface*> matPrec;
92 double phi = -M_PI;
93 for ( unsigned int it = 0; it < m_numScan+1; it++) {
94 // the initial perigee
95 double z0 = m_minZ0 + (m_maxZ0-m_minZ0)/m_numScan *it;
96 phi += 1*Gaudi::Units::deg; if (phi>M_PI) phi -=2*M_PI;
97
99 double p = m_minP + (m_maxP-m_minP)/m_numScan *it;
100 Trk::PerigeeSurface surface( Amg::Vector3D(0.,0.,0.));
101 Trk::Perigee initialPerigee(0., z0, phi, theta, m_charge/p, surface);
102
103 const Trk::TrackParameters* seed = initialPerigee.clone();
104 const Trk::PerigeeSurface& pSf = initialPerigee.associatedSurface();
105 material.clear(); matPrec.clear();
106 const Trk::TrackParameters* currPar = seed;
107 const Trk::TrackParameters* precPar = seed;
108 if (m_domsentry) {
109 if (!m_msentry) {
110 m_msentry = m_trackingGeometry->trackingVolume("Calo::Containers::Calorimeter");
111 }
112 if (m_msentry) {
113 auto msEntry = m_extrapolator->extrapolateToVolume(
114 ctx,
115 *currPar,
116 *m_msentry,
118 static_cast<Trk::ParticleHypothesis>(m_particleType.value()));
119 if (msEntry) {
120 printMat(
121 theta,
122 phi,
123 currPar->momentum().mag() - msEntry->momentum().mag(),
124 Amg::error(msEntry->covariance()->inverse().eval(), Trk::theta),
125 Amg::error(msEntry->covariance()->inverse().eval(), Trk::phi));
126
127 const std::vector<const Trk::TrackStateOnSurface*>* mmsentry = m_extrapolator->extrapolateM(ctx,
128 *currPar,
129 msEntry->associatedSurface(),
131 false,
132 static_cast<Trk::ParticleHypothesis>(m_particleType.value()));
133 if (mmsentry ) {
134 for (const auto& entry : *mmsentry) {
135 if (entry) {
136 ATH_MSG_DEBUG("position:eloss:"
137 << entry->trackParameters()->position() << ":"
138 << entry->trackParameters()->momentum().mag() - currPar->momentum().mag());
139 }
140 }
141
142 currPar = (mmsentry->back()) ? mmsentry->back()->trackParameters() : msEntry.release();
143
144 const std::vector<const Trk::TrackStateOnSurface*>* peri = m_extrapolator->extrapolateM(ctx,
145 *currPar,
146 pSf,
148 false,
149 static_cast<Trk::ParticleHypothesis>(m_particleType.value()));
150 ATH_MSG_INFO ( "material scan:backward:" );
151 if (peri){
152 ATH_MSG_DEBUG ("trPar vector size:" << peri->size() );
153 } else {
154 ATH_MSG_ERROR ("Perigee pointer is null in CETmaterial.cxx");
155 return StatusCode::FAILURE;
156 }
157 for (const auto& entry : *peri) {
158 if (entry && entry->trackParameters()) {
159 ATH_MSG_DEBUG("position:eloss:"
160 << entry->trackParameters()->position() << ":"
161 << entry->trackParameters()->momentum().mag() - msEntry->momentum().mag());
162 }
163 }
164
165 if (peri->back() && peri->back()->trackParameters()) {
166 ATH_MSG_INFO( "extrapolation to perigee:input: "
167 << initialPerigee.parameters()[0] << ","
168 << initialPerigee.parameters()[1] << ","
169 << initialPerigee.parameters()[2] << ","
170 << initialPerigee.parameters()[3] << ","
171 << initialPerigee.momentum().mag() );
172 ATH_MSG_INFO( "extrapolation to perigee:output: "
173 << peri->back()->trackParameters()->parameters()[0] << ","
174 << peri->back()->trackParameters()->parameters()[1] << ","
175 << peri->back()->trackParameters()->parameters()[2] << ","
176 << peri->back()->trackParameters()->parameters()[3] << ","
177 << peri->back()->trackParameters()->momentum().mag() );
178 } else {
179 ATH_MSG_ERROR( "extrapolation to perigee failed for input parameters: " << msEntry->parameters() );
180 }
181 delete peri;
182 peri = nullptr;
183 } else {
184 ATH_MSG_ERROR( "extrapolation to MSentry failed for input parameters: " << currPar->parameters() );
185 printMat(theta,phi,0.);
186 }
187 }
188 }
189 delete currPar;
190 currPar = nullptr;
191 continue;
192 }
193 if (m_checkStepWise) {
194 double matApp = 0.;
195 while (currPar) {
196 std::pair<std::unique_ptr<Trk::TrackParameters>,const Trk::Layer*> next = m_extrapolator->extrapolateToNextActiveLayerM(
197 ctx,
198 *currPar,
200 true,
201 material,
202 static_cast<Trk::ParticleHypothesis>(m_particleType.value()));
203
204
205 const Trk::TrackParameters* nextPar = next.first.release();
206 const Trk::Layer* lay = next.second;
207 currPar = nextPar;
208
209 if (m_doprecision && precPar && currPar ) {
210 // try to extrapolate to the same surface
211 const std::vector<const Trk::TrackStateOnSurface*>* nextPrec = m_extraprec->extrapolateM(
212 ctx,
213 *precPar,currPar->associatedSurface(),
215 false,
216 static_cast<Trk::ParticleHypothesis>(m_particleType.value()));
217 delete precPar;
218 precPar = nullptr;
219 // collect material
220 if (nextPrec) {
221 for (const auto *i : *nextPrec) {
222 const Trk::MaterialEffectsBase* mEff = i->materialEffectsOnTrack();
223 const Trk::TrackParameters* trPar = i->trackParameters();
224 if (mEff && trPar) {
225 matApp += mEff->thicknessInX0();
226 }
227 }
228 }
229 // stop of extrapolation failed
230 if (!lay || !nextPrec || nextPrec->empty() || !nextPrec->back() ) break;
231 precPar = nextPrec->back()->trackParameters();
232 double mat=0.;
233 if (!material.empty()) for (auto & i : material) {
234 if (i->materialEffectsOnTrack()) mat += i->materialEffectsOnTrack()->thicknessInX0();
235 }
236 if ( precPar ) printMatComp(theta,phi,currPar,lay->enclosingDetachedTrackingVolume()->name(),mat,matApp,currPar->parameters()[0]-precPar->parameters()[0],
237 currPar->parameters()[1]-precPar->parameters()[1]);
238 else if (currPar) {
239 //precPar is nullptr here
240 ATH_MSG_INFO( "expected layer not reached:" << currPar->position() );
241 }
242 }
243 if (nextPar && m_printActive) {
244 int id = 0;
245 if (lay) id = lay->layerType();
246 double matc=0.;
247 if (!material.empty()) for (auto & i : material) {
248 if (i->materialEffectsOnTrack()) matc += i->materialEffectsOnTrack()->thicknessInX0();
249 }
250 else ATH_MSG_INFO( "mat & error:" << theta << "," << phi << "," << matc << ","
251 << Amg::error(nextPar->covariance()->inverse().eval(),Trk::theta) << ","
252 << Amg::error(nextPar->covariance()->inverse().eval(),Trk::phi) );
253
254 printMatPrec(theta,phi,nextPar,nextPar,matc,id,"unknown");
255 }
256 if (!lay) break;
257 }
258 if (m_printMaterial) {
259 double mat=0.;
260 if (!material.empty()) for (auto & i : material) {
261 if (i->materialEffectsOnTrack()) {
262 mat += i->materialEffectsOnTrack()->thicknessInX0();
263 }
264 }
265 printMat(theta,phi,mat);
266 }
267 } else {
268 const std::vector<const Trk::TrackStateOnSurface*>* destParameters = m_extrapolator->extrapolateM(
269 ctx,
270 *currPar,
273 false,
274 static_cast<Trk::ParticleHypothesis>(m_particleType.value()));
275
276 if (m_printMaterial) {
277 double mat=0.;
278 if (destParameters) for (const auto *destParameter : *destParameters) {
279 const Trk::MaterialEffectsBase* mEff = destParameter->materialEffectsOnTrack();
280 const Trk::TrackParameters* trPar = destParameter->trackParameters();
281 if (trPar) {
282 //const Trk::MeasuredTrackParameters* mdest = dynamic_cast<const Trk::MeasuredTrackParameters*> (trPar);
283 //if (mdest) ATH_MSG_INFO( "radiation thickness and errors(theta,phi):" << theta << "," << phi << "," << mat << "," <<
284 // mdest->localErrorMatrix().error(Trk::theta) << "," << mdest->localErrorMatrix().error(Trk::phi) );
285 }
286 if (mEff && trPar) {
287 mat += mEff->thicknessInX0();
288 // find volume
289 std::vector<const Trk::DetachedTrackingVolume*> detVols = m_extrapolator->trackingGeometry()->lowestDetachedTrackingVolumes(trPar->position());
290 if (!detVols.empty()) printMatScan(theta,phi,trPar->position().perp(),trPar->position().z(),mEff->thicknessInX0(),(detVols)[0]->name());
291 else printMatScan(theta,phi,trPar->position().perp(),trPar->position().z(),mEff->thicknessInX0(),m_extrapolator->trackingGeometry()->lowestStaticTrackingVolume(trPar->position())->volumeName());
292 }
293 }
294 if (destParameters) {
295 //const Trk::MeasuredTrackParameters* mdest = dynamic_cast<const Trk::MeasuredTrackParameters*> ((*destParameters).back()->trackParameters());
296 //if (mdest) ATH_MSG_INFO( "radiation thickness and errors(theta,phi):" << theta << "," << phi << "," << mat << "," <<
297 //mdest->localErrorMatrix().error(Trk::theta) << "," << mdest->localErrorMatrix().error(Trk::phi) );
298 }
299 printMat(theta,phi,mat);
300 }
301
302 if (!destParameters || destParameters->empty() ) {
303 ATH_MSG_ERROR( "extrapolation to outer boundary failed for input parameters: " << initialPerigee.parameters() );
304 } else if (destParameters->back()->trackParameters()) {
305 // forward extrapolation ok
306 if (m_backward) {
307 material.clear();
308 const std::vector<const Trk::TrackStateOnSurface*>* peri = m_extrapolator->extrapolateM(
309 ctx, *(destParameters->back()->trackParameters()),
310 pSf,
312 false,
313 static_cast<Trk::ParticleHypothesis>(m_particleType.value()));
314
315 if (peri) {
316 ATH_MSG_INFO( "trPar vector size:" << peri->size() );
317 for (unsigned int i=0; i< peri->size(); i++)
318 ATH_MSG_INFO( "position:" << i << "," << (*peri)[i]->trackParameters()->position() );
319 ATH_MSG_INFO( "extrapolation to perigee:input: " << initialPerigee.parameters() );
320 ATH_MSG_INFO( "extrapolation to perigee:output: " << peri->back()->trackParameters()->parameters() );
321 } else {
322 ATH_MSG_ERROR( "extrapolation to perigee failed for input parameters: " << destParameters->back()->trackParameters()->parameters() );
323 }
324 delete peri;
325 peri = nullptr;
326 }
327 }
328
329 delete destParameters;
330 destParameters = nullptr;
331 }
332 }
333
334 if (m_chronoStatSvc) m_chronoStatSvc->chronoStop("MS::scan");
335
336 return StatusCode::SUCCESS;
337}
338
339//============================================================================================
340
341void Trk::CETmaterial::printMat(double theta, double phi, double mat, double dtheta, double dphi) const {
342
343 std::ofstream myfilemat;
344 myfilemat.open (m_matTotFile,std::ios::app);
345 myfilemat<<theta<<" "<<phi<<" "<<mat<<" "<<dtheta<<" "<<dphi<<std::endl;
346}
347
348
349void Trk::CETmaterial::printMatScan(double theta, double phi, double r, double z, double mat, const std::string& name) const {
350
351 std::ofstream myfilemat;
352 myfilemat.open(m_matScanFile,std::ios::app);
353 myfilemat << theta << " " << phi << " " << r << " " << z << " " << mat << " " << name << std::endl;
354}
355
356void Trk::CETmaterial::printMatPrec(double theta, double phi, const Trk::TrackParameters* nextPar, const Trk::TrackParameters* mdest, double mat, int id, const std::string& name) {
357
358 if (name.empty()) {}; // dummy to get rid of warning message (unused variable name)
359 std::ofstream myfilemat;
360 myfilemat.open(m_matActiveFile,std::ios::app);
361
362 if (!m_th && !m_ph) {
363 m_th = theta;
364 m_ph = phi;
365 m_id = id;
366 m_matSaved = mat;
367 delete m_next; m_next=nextPar->clone();
368 delete m_err;
369 m_err=nullptr;
370 if (mdest) {
372 *m_err = mdest->covariance()->inverse().eval();
373 }
374 return;
375 }
376
377 if ( theta!=m_th || phi!=m_ph ) {
378
379 if (m_err && m_id>0) {
380 myfilemat << m_th << " " << m_ph << " " << 1 << " " << m_id << " " << m_matSaved << std::endl;
381 myfilemat << m_next->parameters()[Trk::locX] << " " << m_next->parameters()[Trk::locY] << " " << m_next->parameters()[Trk::phi]
382 << " " << m_next->parameters()[Trk::theta] << " " << m_next->parameters()[Trk::qOverP] << std::endl;
383 myfilemat << Amg::error(*m_err,Trk::locX) << " " << Amg::error(*m_err,Trk::locY)
384 << " " << Amg::error(*m_err,Trk::phi) << " " << Amg::error(*m_err,Trk::theta)
385 << " " << Amg::error(*m_err,Trk::qOverP) << std::endl;
386 } else {
387 myfilemat << m_th << " " << m_ph << " " << 0 << " " << m_id << std::endl;
388 myfilemat << m_next->parameters()[Trk::locX] << " " << m_next->parameters()[Trk::locY] << " " << m_next->parameters()[Trk::phi]
389 << " " << m_next->parameters()[Trk::theta] << " " << m_next->parameters()[Trk::qOverP] << std::endl;
390 }
391 m_th = theta;
392 m_ph = phi;
393 m_id = id;
394 m_matSaved = mat;
395 delete m_next; m_next=nextPar->clone();
396 delete m_err;
397 m_err=nullptr;
398
399 if (mdest) {
401 *m_err = mdest->covariance()->inverse().eval();
402 }
403 return;
404 }
405
406 // update data
407 if (id>1) {
408 m_th = theta;
409 m_ph = phi;
410 m_id = id;
411 m_matSaved = mat;
412 delete m_next; m_next=nextPar->clone();
413 delete m_err;
414 m_err=nullptr;
415 if (mdest) {
417 *m_err = mdest->covariance()->inverse().eval();
418 }
419 }
420 }
421
422void Trk::CETmaterial::printMatComp(double theta, double phi, const Trk::TrackParameters* currPar, const std::string& name, double mat, double matApp,double dx, double dy) const
423{
424 std::ofstream myfilemat;
425 myfilemat.open(m_matCompFile,std::ios::app);
426 myfilemat << theta << " " << phi << " " << currPar->position().perp() << " " << currPar->position().z() << " " << name.substr(0,2)
427 << " " << mat << " " << matApp << " " << dx << " " << dy << std::endl;
428}
#define M_PI
#define ATH_MSG_ERROR(x)
#define ATH_MSG_FATAL(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_DEBUG(x)
AthAlgorithm(const std::string &name, ISvcLocator *pSvcLocator)
Constructor.
void printMat(double th, double ph, double mat, double dtheta=0., double dphi=0.) const
BooleanProperty m_backward
Definition CETmaterial.h:85
StatusCode initialize()
standard Athena-Algorithm method
StatusCode execute(const EventContext &ctx)
standard Athena-Algorithm method
BooleanProperty m_domsentry
Definition CETmaterial.h:86
CETmaterial(const std::string &name, ISvcLocator *pSvcLocator)
Standard Athena-Algorithm Constructor.
Amg::MatrixX * m_err
Definition CETmaterial.h:94
const Trk::TrackingGeometry * m_trackingGeometry
Definition CETmaterial.h:97
StatusCode finalize()
standard Athena-Algorithm method
IntegerProperty m_particleType
void printMatPrec(double theta, double phi, const Trk::TrackParameters *, const Trk::TrackParameters *, double mat, int id, const std::string &name)
const char * m_matCompFile
Definition CETmaterial.h:83
BooleanProperty m_doprecision
Definition CETmaterial.h:87
DoubleProperty m_charge
Definition CETmaterial.h:74
void printMatScan(double theta, double phi, double r, double z, double mat, const std::string &name) const
IChronoStatSvc_t m_chronoStatSvc
DoubleProperty m_minP
Definition CETmaterial.h:72
const char * m_matTotFile
Definition CETmaterial.h:80
BooleanProperty m_printActive
Definition CETmaterial.h:78
~CETmaterial()
Default Destructor.
DoubleProperty m_minZ0
Definition CETmaterial.h:68
BooleanProperty m_checkStepWise
Definition CETmaterial.h:76
ToolHandle< IExtrapolator > m_extraprec
Definition CETmaterial.h:66
UnsignedIntegerProperty m_numScan
Definition CETmaterial.h:75
DoubleProperty m_maxP
Definition CETmaterial.h:73
const Trk::Surface * m_outerBoundary
Definition CETmaterial.h:96
const char * m_matScanFile
Definition CETmaterial.h:81
Trk::TrackParameters * m_next
Definition CETmaterial.h:93
BooleanProperty m_printMaterial
Definition CETmaterial.h:77
void printMatComp(double theta, double phi, const Trk::TrackParameters *currPar, const std::string &name, double mat, double matApp, double dx, double dy) const
DoubleProperty m_minTheta
Definition CETmaterial.h:70
DoubleProperty m_maxTheta
Definition CETmaterial.h:71
const Trk::TrackingVolume * m_msentry
Definition CETmaterial.h:98
ToolHandle< IExtrapolator > m_extrapolator
The Extrapolator(s) to be retrieved.
Definition CETmaterial.h:64
DoubleProperty m_maxZ0
Definition CETmaterial.h:69
const char * m_matActiveFile
Definition CETmaterial.h:82
const std::string & name() const
returns the Name
Base Class for a Detector Layer in the Tracking realm.
Definition Layer.h:72
int layerType() const
get the Layer coding
const DetachedTrackingVolume * enclosingDetachedTrackingVolume() const
get the confining DetachedTrackingVolume
base class to integrate material effects on Trk::Track in a flexible way.
double thicknessInX0() const
returns the actually traversed material .
const Amg::Vector3D & momentum() const
Access method for the momentum.
virtual ParametersBase< DIM, T > * clone() const override=0
clone method for polymorphic deep copy
const Amg::Vector3D & position() const
Access method for the position.
virtual const Surface & associatedSurface() const override=0
Access to the Surface associated to the Parameters.
virtual ParametersT< DIM, T, S > * clone() const override final
Virtual clone.
virtual const S & associatedSurface() const override final
Access to the Surface method.
Class describing the Line to which the Perigee refers to.
int r
Definition globals.cxx:22
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > MatrixX
Dynamic Matrix - dynamic allocation.
double error(const Amg::MatrixX &mat, int index)
return diagonal error of the matrix caller should ensure the matrix is symmetric and the index is in ...
Eigen::Matrix< double, 3, 1 > Vector3D
@ oppositeMomentum
@ alongMomentum
@ next
Definition BinningData.h:35
ParametersT< TrackParametersDim, Charged, PerigeeSurface > Perigee
@ locY
local cartesian
Definition ParamDefs.h:38
@ locX
Definition ParamDefs.h:37
@ z
global position (cartesian)
Definition ParamDefs.h:57
@ theta
Definition ParamDefs.h:66
@ qOverP
perigee
Definition ParamDefs.h:67
@ phi
Definition ParamDefs.h:75
@ z0
Definition ParamDefs.h:64
ParticleHypothesis
Enumeration for Particle hypothesis respecting the interaction with material.
const Amg::Vector3D & position() const
Method to retrieve the position of the Intersection.
ParametersBase< TrackParametersDim, Charged > TrackParameters