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
35
36//================ Initialization =================================================
37
39 // Code entered here will be executed once at program start.
40
41 ATH_MSG_INFO("initialize()");
42
43 // Get Extrapolator from ToolService
44 if (m_extrapolator.retrieve().isFailure()) {
45 ATH_MSG_FATAL("Could not retrieve Tool " << m_extrapolator << ". Exiting.");
46 return StatusCode::FAILURE;
47 }
48 if (m_extraprec.retrieve().isFailure()) {
49 ATH_MSG_FATAL("Could not retrieve Tool " << m_extraprec << ". Exiting.");
50 return StatusCode::FAILURE;
51 }
52
53 ATH_MSG_INFO("initialize() successful");
54 return StatusCode::SUCCESS;
55}
56
57//================ Finalization =================================================
58
60 // Code entered here will be executed once at the end of the program run.
61
62 if (m_chronoStatSvc) m_chronoStatSvc->chronoPrint("MS::scan");
63
64 return StatusCode::SUCCESS;
65}
66
67//================ Execution ====================================================
68
69StatusCode Trk::CETmaterial::execute(const EventContext& ctx) {
70 ATH_MSG_INFO("execute()");
71 // retrieve outer boundary cylinder surface
72 if (!m_outerBoundary) {
73 m_trackingGeometry = m_extrapolator->trackingGeometry();
74 m_outerBoundary = &(m_trackingGeometry->highestTrackingVolume()->boundarySurfaces()[2]->surfaceRepresentation());
75 if (!m_outerBoundary) {
76 ATH_MSG_FATAL("Could not retrieve cylinder boundary from " << m_extrapolator << ". Exiting.");
77 return StatusCode::FAILURE;
78 }
79 ATH_MSG_INFO("boundary retrieved ");
80 }
81
82 if (m_chronoStatSvc) m_chronoStatSvc->chronoStart("MS::scan");
83
84 // scan
85 std::vector<const TrackStateOnSurface*> material;
86 std::vector<const TrackStateOnSurface*> matPrec;
87 double phi = -M_PI;
88 for (unsigned int it = 0; it < m_numScan + 1; it++) {
89 // the initial perigee
90 double z0 = m_minZ0 + (m_maxZ0 - m_minZ0) / m_numScan * it;
91 phi += 1 * Gaudi::Units::deg;
92 if (phi > M_PI) phi -= 2 * M_PI;
93
94 double theta = m_minTheta + (m_maxTheta - m_minTheta) / m_numScan * it;
95 double p = m_minP + (m_maxP - m_minP) / m_numScan * it;
96 Trk::PerigeeSurface surface(Amg::Vector3D(0., 0., 0.));
97 Trk::Perigee initialPerigee(0., z0, phi, theta, m_charge / p, surface);
98
99 const Trk::TrackParameters* seed = initialPerigee.clone();
100 const Trk::PerigeeSurface& pSf = initialPerigee.associatedSurface();
101 material.clear();
102 matPrec.clear();
103 const Trk::TrackParameters* currPar = seed;
104 const Trk::TrackParameters* precPar = seed;
105 if (m_domsentry) {
106 if (!m_msentry) {
107 m_msentry = m_trackingGeometry->trackingVolume("Calo::Containers::Calorimeter");
108 }
109 if (m_msentry) {
110 auto msEntry = m_extrapolator->extrapolateToVolume(
111 ctx,
112 *currPar,
113 *m_msentry,
115 static_cast<Trk::ParticleHypothesis>(m_particleType.value()));
116 if (msEntry) {
117 printMat(theta, phi,currPar->momentum().mag() - msEntry->momentum().mag(),
118 Amg::error(msEntry->covariance()->inverse().eval(), Trk::theta),
119 Amg::error(msEntry->covariance()->inverse().eval(), Trk::phi));
120 const std::vector<const Trk::TrackStateOnSurface*>* mmsentry = m_extrapolator->extrapolateM(ctx,
121 *currPar, msEntry->associatedSurface(), Trk::alongMomentum, false,
122 static_cast<Trk:: ParticleHypothesis>( m_particleType.value()));
123 if (mmsentry) {
124 for (const auto& entry : *mmsentry) {
125 if (entry) {
126 ATH_MSG_DEBUG("position:eloss:"
127 << entry->trackParameters()->position() << ":"
128 << entry->trackParameters()->momentum().mag() - currPar->momentum().mag());
129 }
130 }
131 currPar = (mmsentry->back()) ? mmsentry->back()->trackParameters() : msEntry.release();
132 const std::vector<const Trk::TrackStateOnSurface*>* peri = m_extrapolator->extrapolateM(ctx,
133 *currPar, pSf, Trk::oppositeMomentum, false,
134 static_cast<Trk::ParticleHypothesis>( m_particleType.value()));
135 ATH_MSG_INFO("material scan:backward:");
136 if (peri) {
137 ATH_MSG_DEBUG("trPar vector size:" << peri->size());
138 } else {
139 ATH_MSG_ERROR("Perigee pointer is null in CETmaterial.cxx");
140 return StatusCode::FAILURE;
141 }
142 for (const auto& entry : *peri) {
143 if (entry && entry->trackParameters()) {
144 ATH_MSG_DEBUG("position:eloss:"
145 << entry->trackParameters()->position() << ":"
146 << entry->trackParameters()->momentum().mag() - msEntry->momentum().mag());
147 }
148 }
149
150 if (peri->back() && peri->back()->trackParameters()) {
151 ATH_MSG_INFO("extrapolation to perigee:input: "
152 << initialPerigee.parameters()[0] << ","
153 << initialPerigee.parameters()[1] << ","
154 << initialPerigee.parameters()[2] << ","
155 << initialPerigee.parameters()[3] << ","
156 << initialPerigee.momentum().mag());
157 ATH_MSG_INFO("extrapolation to perigee:output: "
158 << peri->back()->trackParameters()->parameters()[0] << ","
159 << peri->back()->trackParameters()->parameters()[1] << ","
160 << peri->back()->trackParameters()->parameters()[2] << ","
161 << peri->back()->trackParameters()->parameters()[3] << ","
162 << peri->back()->trackParameters()->momentum().mag());
163 } else {
164 ATH_MSG_ERROR("extrapolation to perigee failed for input parameters: " << msEntry->parameters());
165 }
166 delete peri;
167 peri = nullptr;
168 } else {
169 ATH_MSG_ERROR("extrapolation to MSentry failed for input parameters: " << currPar->parameters());
170 printMat(theta, phi, 0.);
171 }
172 }
173 }
174 delete currPar;
175 currPar = nullptr;
176 continue;
177 }
178 if (m_checkStepWise) {
179 double matApp = 0.;
180 while (currPar) {
181 std::pair<std::unique_ptr<Trk::TrackParameters>,const Trk::Layer*> next =
182 m_extrapolator->extrapolateToNextActiveLayerM(
183 ctx, *currPar, Trk::alongMomentum, true, material,
184 static_cast<Trk::ParticleHypothesis>(m_particleType.value()));
185 const Trk::TrackParameters* nextPar = next.first.release();
186 const Trk::Layer* lay = next.second;
187 currPar = nextPar;
188 if (m_doprecision && precPar && currPar) {
189 // try to extrapolate to the same surface
190 const std::vector<const Trk::TrackStateOnSurface*>* nextPrec = m_extraprec->extrapolateM(
191 ctx, *precPar, currPar->associatedSurface(), Trk::alongMomentum, false,
192 static_cast<Trk::ParticleHypothesis>(m_particleType.value()));
193 delete precPar;
194 precPar = nullptr;
195 // collect material
196 if (nextPrec) {
197 for (const auto* i : *nextPrec) {
198 const Trk::MaterialEffectsBase* mEff = i->materialEffectsOnTrack();
199 const Trk::TrackParameters* trPar = i->trackParameters();
200 if (mEff && trPar) {
201 matApp += mEff->thicknessInX0();
202 }
203 }
204 }
205 // stop of extrapolation failed
206 if (!lay || !nextPrec || nextPrec->empty() || !nextPrec->back()) break;
207 precPar = nextPrec->back()->trackParameters();
208 double mat = 0.;
209 if (!material.empty()) for (auto& i : material) {
210 if (i->materialEffectsOnTrack()) mat += i->materialEffectsOnTrack()->thicknessInX0();
211 }
212 if (precPar) printMatComp(theta, phi, currPar,
213 lay->enclosingDetachedTrackingVolume()->name(), mat, matApp,
214 currPar->parameters()[0] - precPar->parameters()[0],
215 currPar->parameters()[1] - precPar->parameters()[1]);
216 else if (currPar) {
217 //precPar is nullptr here
218 ATH_MSG_INFO("expected layer not reached:" << currPar->position());
219 }
220 }
221 if (nextPar && m_printActive) {
222 int id = 0;
223 if (lay) id = lay->layerType();
224 double matc = 0.;
225 if (!material.empty()) for (auto& i : material) {
226 if (i->materialEffectsOnTrack()) matc += i->materialEffectsOnTrack()->thicknessInX0();
227 }
228 else ATH_MSG_INFO("mat & error:" << theta << "," << phi << "," << matc << ","
229 << Amg::error(nextPar->covariance()->inverse().eval(), Trk::theta) << ","
230 << Amg::error(nextPar->covariance()->inverse().eval(), Trk::phi));
231
232 printMatPrec(theta, phi, nextPar, nextPar, matc, id, "unknown");
233 }
234 if (!lay) break;
235 }
236 if (m_printMaterial) {
237 double mat = 0.;
238 if (!material.empty()) for (auto& i : material) {
239 if (i->materialEffectsOnTrack()) {
240 mat += i->materialEffectsOnTrack()->thicknessInX0();
241 }
242 }
243 printMat(theta, phi, mat);
244 }
245 } else {
246 const std::vector<const Trk::TrackStateOnSurface*>* destParameters = m_extrapolator->extrapolateM(
247 ctx, *currPar, *m_outerBoundary, Trk::alongMomentum, false,
248 static_cast<Trk::ParticleHypothesis>(m_particleType.value()));
249
250 if (m_printMaterial) {
251 double mat = 0.;
252 if (destParameters) for (const auto* destParameter : *destParameters) {
253 const Trk::MaterialEffectsBase* mEff = destParameter->materialEffectsOnTrack();
254 const Trk::TrackParameters* trPar = destParameter->trackParameters();
255 if (mEff && trPar) {
256 mat += mEff->thicknessInX0();
257 // find volume
258 std::vector<const Trk::DetachedTrackingVolume*> detVols =
259 m_extrapolator->trackingGeometry()->lowestDetachedTrackingVolumes(trPar->position());
260 if (!detVols.empty()){
261 printMatScan(theta, phi, trPar->position().perp(), trPar->position().z(), mEff->thicknessInX0(), (detVols)[0]->name());
262 } else {
263 printMatScan(theta, phi, trPar->position().perp(), trPar->position().z(),
264 mEff->thicknessInX0(), m_extrapolator->trackingGeometry()->lowestStaticTrackingVolume(
265 trPar->position())->volumeName());
266 }
267 }
268 }
269
270 printMat(theta, phi, mat);
271 }
272
273 if (!destParameters || destParameters->empty()) {
274 ATH_MSG_ERROR("extrapolation to outer boundary failed for input parameters: " << initialPerigee.parameters());
275 } else if (destParameters->back()->trackParameters()) {
276 // forward extrapolation ok
277 if (m_backward) {
278 material.clear();
279 const std::vector<const Trk::TrackStateOnSurface*>* peri = m_extrapolator->extrapolateM(
280 ctx, *(destParameters->back()->trackParameters()),
281 pSf,
283 false,
284 static_cast<Trk::ParticleHypothesis>(m_particleType.value()));
285
286 if (peri) {
287 ATH_MSG_INFO("trPar vector size:" << peri->size());
288 for (unsigned int i = 0; i < peri->size(); i++){
289 ATH_MSG_INFO("position:" << i << "," << (*peri)[i]->trackParameters()->position());
290 }
291 ATH_MSG_INFO("extrapolation to perigee:input: " << initialPerigee.parameters());
292 ATH_MSG_INFO("extrapolation to perigee:output: " << peri->back()->trackParameters()->parameters());
293 } else {
295 "extrapolation to perigee failed for input parameters: " <<
296 destParameters->back()->trackParameters()->parameters());
297 }
298 delete peri;
299 peri = nullptr;
300 }
301 }
302
303 delete destParameters;
304 destParameters = nullptr;
305 }
306 }
307
308 if (m_chronoStatSvc) m_chronoStatSvc->chronoStop("MS::scan");
309
310 return StatusCode::SUCCESS;
311}
312
313//============================================================================================
314
315void Trk::CETmaterial::printMat(double theta, double phi, double mat, double dtheta, double dphi) const {
316 std::ofstream myfilemat;
317 myfilemat.open(m_matTotFile, std::ios::app);
318 myfilemat << theta << " " << phi << " " << mat << " " << dtheta << " " << dphi << std::endl;
319}
320
321void Trk::CETmaterial::printMatScan(double theta, double phi, double r, double z, double mat,
322 const std::string& name) const {
323 std::ofstream myfilemat;
324 myfilemat.open(m_matScanFile, std::ios::app);
325 myfilemat << theta << " " << phi << " " << r << " " << z << " " << mat << " " << name << std::endl;
326}
327
329 const Trk::TrackParameters* mdest, double mat, int id, const std::string& /*name*/) {
330 ; // dummy to get rid of warning message (unused variable name)
331 std::ofstream myfilemat;
332 myfilemat.open(m_matActiveFile, std::ios::app);
333 if (!m_th && !m_ph) {
334 m_th = theta;
335 m_ph = phi;
336 m_id = id;
337 m_matSaved = mat;
338 delete m_next;
339 m_next = nextPar->clone();
340 delete m_err;
341 m_err = nullptr;
342 if (mdest) {
343 m_err = new Amg::MatrixX;
344 *m_err = mdest->covariance()->inverse().eval();
345 }
346 return;
347 }
348
349 if (theta != m_th || phi != m_ph) {
350 if (m_err && m_id > 0) {
351 myfilemat << m_th << " " << m_ph << " " << 1 << " " << m_id << " " << m_matSaved << std::endl;
352 myfilemat << m_next->parameters()[Trk::locX] << " " << m_next->parameters()[Trk::locY] << " " <<
353 m_next->parameters()[Trk::phi]
354 << " " << m_next->parameters()[Trk::theta] << " " << m_next->parameters()[Trk::qOverP] << std::endl;
355 myfilemat << Amg::error(*m_err, Trk::locX) << " " << Amg::error(*m_err, Trk::locY)
356 << " " << Amg::error(*m_err, Trk::phi) << " " << Amg::error(*m_err, Trk::theta)
357 << " " << Amg::error(*m_err, Trk::qOverP) << std::endl;
358 } else {
359 myfilemat << m_th << " " << m_ph << " " << 0 << " " << m_id << std::endl;
360 myfilemat << m_next->parameters()[Trk::locX] << " " << m_next->parameters()[Trk::locY] << " " <<
361 m_next->parameters()[Trk::phi]
362 << " " << m_next->parameters()[Trk::theta] << " " << m_next->parameters()[Trk::qOverP] << std::endl;
363 }
364 m_th = theta;
365 m_ph = phi;
366 m_id = id;
367 m_matSaved = mat;
368 delete m_next;
369 m_next = nextPar->clone();
370 delete m_err;
371 m_err = nullptr;
372
373 if (mdest) {
374 m_err = new Amg::MatrixX;
375 *m_err = mdest->covariance()->inverse().eval();
376 }
377 return;
378 }
379
380 // update data
381 if (id > 1) {
382 m_th = theta;
383 m_ph = phi;
384 m_id = id;
385 m_matSaved = mat;
386 delete m_next;
387 m_next = nextPar->clone();
388 delete m_err;
389 m_err = nullptr;
390 if (mdest) {
391 m_err = new Amg::MatrixX;
392 *m_err = mdest->covariance()->inverse().eval();
393 }
394 }
395}
396
398 const std::string& name, double mat, double matApp, double dx, double dy) const {
399 std::ofstream myfilemat;
400 myfilemat.open(m_matCompFile, std::ios::app);
401 myfilemat << theta << " " << phi << " " << currPar->position().perp() << " " << currPar->position().z() << " " <<
402 name.substr(0, 2)
403 << " " << mat << " " << matApp << " " << dx << " " << dy << std::endl;
404}
#define M_PI
#define ATH_MSG_DEBUG(x,...)
#define ATH_MSG_ERROR(x,...)
#define ATH_MSG_INFO(x,...)
#define ATH_MSG_FATAL(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 =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
Ensure that the ATLAS eigen extensions are properly loaded.
@ 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