ATLAS Offline Software
Loading...
Searching...
No Matches
ActsExtrapolationAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3*/
4
6
7// ATHENA
10#include "GaudiKernel/EventContext.h"
11#include "GaudiKernel/ISvcLocator.h"
12
13// ACTS
14#include "Acts/Propagator/MaterialInteractor.hpp"
15#include "Acts/Propagator/detail/SteppingLogger.hpp"
16#include "Acts/Surfaces/PerigeeSurface.hpp"
17#include "Acts/Utilities/Helpers.hpp"
18#include "Acts/Definitions/Units.hpp"
19#include "Acts/Utilities/Logger.hpp"
20
21// PACKAGE
25#include "ActsInterop/Logger.h"
26
27// OTHER
28#include "CLHEP/Random/RandomEngine.h"
29
30// STL
31#include <fstream>
32#include <string>
33
34using namespace Acts::UnitLiterals;
35
37
38 ATH_MSG_DEBUG(name() << "::" << __FUNCTION__);
39
40 ATH_CHECK(m_rndmGenSvc.retrieve());
45
46 return StatusCode::SUCCESS;
47}
48
49StatusCode ActsExtrapolationAlg::execute(const EventContext &ctx) const {
50
51 ATH_MSG_VERBOSE(name() << "::" << __FUNCTION__);
52
53 ATHRNG::RNGWrapper *rngWrapper = m_rndmGenSvc->getEngine(this);
54 rngWrapper->setSeed(name(), ctx);
55 CLHEP::HepRandomEngine *rngEngine = rngWrapper->getEngine(ctx);
56
57 ATH_MSG_VERBOSE("Extrapolating " << m_nParticlePerEvent << " particles");
58
59 // Write to the collection to the EventStore
61
62 // Record the collection once per event if not already there
63 if (!materialTracks.isPresent()) {
64 auto coll = std::make_unique<ActsTrk::RecordedMaterialTrackCollection>();
65 ATH_CHECK(materialTracks.record(std::move(coll)));
66 }
67
68 // Add the track to the recorded collection
69 auto* coll = materialTracks.ptr();
70 if (!coll) {
71 ATH_MSG_ERROR("RecordedMaterialTrackCollection ptr() is null for key "
73 return StatusCode::FAILURE;
74 }
75
76 for (size_t i = 0; i < m_nParticlePerEvent; i++) {
77 double d0 = 0;
78 double z0 = 0;
79 double phi = rngEngine->flat() * 2 * M_PI - M_PI;
80 std::vector<double> etaRange = m_etaRange;
81 double etaMin = etaRange.at(0);
82 double etaMax = etaRange.at(1);
83 double eta = rngEngine->flat() * std::abs(etaMax - etaMin) + etaMin;
84
85 std::vector<double> ptRange = m_ptRange;
86 double ptMin = ptRange.at(0) * 1_GeV;
87 double ptMax = ptRange.at(1) * 1_GeV;
88
89 double pt = rngEngine->flat() * std::abs(ptMax - ptMin) + ptMin;
90
91 Acts::Vector3 momentum(pt * std::cos(phi), pt * std::sin(phi),
92 pt * std::sinh(eta));
93
94 double theta = Acts::VectorHelpers::theta(momentum);
95
96 double charge = rngEngine->flat() > 0.5 ? -1 : 1;
97
98 double qop = charge / momentum.norm();
99
100 std::shared_ptr<Acts::PerigeeSurface> surface =
101 Acts::Surface::makeShared<Acts::PerigeeSurface>(
102 Acts::Vector3(0, 0, 0));
103
104 double t = 0;
105 ATH_MSG_VERBOSE("Pseudo-particle: eta: " << eta << " phi: " << phi);
106
107 Acts::BoundVector pars;
108 // cppcheck-suppress constStatement; will be able to initialize this directly with eigen 3.4
109 pars << d0, z0, phi, theta, qop, t;
110 std::optional<Acts::BoundMatrix> cov = std::nullopt;
111
112 if (charge != 0.) {
113 // Perigee, no alignment -> default geo context
114 Acts::GenericBoundTrackParameters startParameters(std::move(surface), std::move(pars), std::move(cov), Acts::ParticleHypothesis::pion());
115 auto result = m_extrapolationTool->propagationSteps(ctx, startParameters);
116 if (!result.ok()) {
117 ATH_MSG_WARNING("Extrapolation tool failed to extrapolate the track: "
118 << result.error().message());
119 continue;
120 }
121 auto &output = result.value();
122 if(output.first.size() == 0) {
123 ATH_MSG_WARNING("Got ZERO steps from the extrapolation tool");
124 }
125 if (m_writePropStep) {
126 m_propStepWriterSvc->write(output.first);
127 }
128
130 Acts::RecordedMaterialTrack track;
131 track.first.first = Acts::Vector3::Zero();
132 track.first.second = momentum;
133 track.second = std::move(output.second);
134 coll->push_back(track);
135 }
136 }
137
138 ATH_MSG_VERBOSE(name() << " execute done");
139 }
140
141 return StatusCode::SUCCESS;
142}
143
144
146 const std::vector<Acts::detail::Step>& steps) const {
147
148 std::lock_guard<std::mutex> lock(m_writeMutex);
149
150 static std::ofstream out ATLAS_THREAD_SAFE ("steps.obj");
151 std::stringstream lstr;
152 lstr << "l";
153 for (const auto &step : steps) {
154 const auto &pos = step.position;
155 out << "v " << pos.x() << " " << pos.y() << " " << pos.z() << std::endl;
156 lstr << " " << m_objVtxCount;
157 m_objVtxCount++;
158 }
159
160 lstr << std::endl;
161
162 out << lstr.str() << std::endl;
163}
#define M_PI
Scalar eta() const
pseudorapidity method
Scalar phi() const
phi method
Scalar theta() const
theta method
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
double charge(const T &p)
Definition AtlasPID.h:997
A wrapper class for event-slot-local random engines.
Definition RNGWrapper.h:56
void setSeed(const std::string &algName, const EventContext &ctx)
Set the random seed using a string (e.g.
Definition RNGWrapper.h:169
CLHEP::HepRandomEngine * getEngine(const EventContext &ctx) const
Retrieve the random engine corresponding to the provided EventContext.
Definition RNGWrapper.h:134
ToolHandle< ActsTrk::IExtrapolationTool > m_extrapolationTool
Gaudi::Property< size_t > m_nParticlePerEvent
size_t m_objVtxCount ATLAS_THREAD_SAFE
ServiceHandle< IAthRNGSvc > m_rndmGenSvc
Gaudi::Property< bool > m_writePropStep
Gaudi::Property< std::vector< double > > m_etaRange
PublicToolHandle< ActsTrk::ITrackingGeometryTool > m_trackingGeometryTool
Gaudi::Property< bool > m_writeMaterialTracks
Gaudi::Property< std::vector< double > > m_ptRange
ServiceHandle< IActsPropStepRootWriterSvc > m_propStepWriterSvc
StatusCode execute(const EventContext &ctx) const override
SG::WriteHandleKey< ActsTrk::RecordedMaterialTrackCollection > m_materialTrackCollectionKey
The RecordedMaterialTrackCollection to write.
StatusCode initialize() override
void writeStepsObj(const std::vector< Acts::detail::Step > &steps) const
bool isPresent() const
Is the referenced object present in SG?
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
pointer_type ptr()
Dereference the pointer.