ATLAS Offline Software
Loading...
Searching...
No Matches
FastCaloSimCaloTransportation.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3*/
4
5/* Athena includes */
7#include "GaudiKernel/ToolHandle.h"
8#include "GaudiKernel/IPartPropSvc.h"
9#include "GaudiKernel/ThreadLocalContext.h"
10
12/* Header include */
14
15/* ISF includes */
19
20/* Tracking includes */
22
23/* Geometry primitives */
25
26/* Transport steps will be return as G4FieldTracks*/
27#include "G4FieldTrack.hh"
29
31
32FastCaloSimCaloTransportation::FastCaloSimCaloTransportation(const std::string& t, const std::string& n, const IInterface* p)
33 : base_class(t,n,p)
34{
35
36}
37
39{
40 ATH_MSG_INFO( "Initializing FastCaloSimCaloTransportation" );
41
42 // Get TimedExtrapolator
43 if(!m_extrapolator.empty()){
44 ATH_CHECK(m_extrapolator.retrieve());
45 ATH_MSG_INFO("Extrapolator retrieved "<< m_extrapolator);
46 }
47
48 return StatusCode::SUCCESS;
49
50}
51
52
54 ATH_MSG_INFO( "Finalizing FastCaloSimCaloTransportation" );
55 return StatusCode::SUCCESS;
56}
57
58
59std::vector<G4FieldTrack> FastCaloSimCaloTransportation::transport(const TFCSTruthState* truth, bool forceNeutral) const{
60 if (auto* eventInfo = AtlasG4EventUserInfo::GetEventUserInfo()) {
61 Gaudi::Hive::setCurrentContext(eventInfo->GetEventContext());
62 }
63
64 // Start calo extrapolation
65 ATH_MSG_DEBUG ("[ fastCaloSim transport ] processing particle "<<truth->pdgid() );
66
67 auto hitVector = std::make_unique<std::vector<Trk::HitInfo>>();
68
69 int pdgId = truth->pdgid();
70 double charge = MC::charge(pdgId);
71 if (forceNeutral) charge = 0.;
72
73 // particle Hypothesis for the extrapolation
74 Trk::ParticleHypothesis pHypothesis = m_pdgToParticleHypothesis.convert(pdgId, charge);
75
76 ATH_MSG_DEBUG ("particle hypothesis "<< pHypothesis);
77
78 // geantinos not handled by PdgToParticleHypothesis - fix there
79 if (pdgId == 999) pHypothesis = Trk::geantino;
80
81 Amg::Vector3D pos = Amg::Vector3D(truth->vertex().X(), truth->vertex().Y(), truth->vertex().Z());
82
83 Amg::Vector3D mom(truth->X(), truth->Y(), truth->Z());
84
85 ATH_MSG_DEBUG( "[ fastCaloSim transport ] x from position eta="<<pos.eta()<<" phi="<<pos.phi()<<" d="<<pos.mag()<<" pT="<<mom.perp() );
86
87 // input parameters : curvilinear parameters
88 Trk::CurvilinearParameters inputPar(pos,mom,charge);
89
90 double freepath = -1.;
91 double tDec = freepath > 0. ? freepath : -1.;
92 int decayProc = 0;
93
94 Trk::TimeLimit timeLim(tDec, 0., decayProc);
95 Trk::PathLimit pathLim(-1., 0);
97
98 // first extrapolation to reach the ID boundary
99 ATH_MSG_DEBUG( "[ fastCaloSim transport ] before calo entrance ");
100
101 // get CaloEntrance if not done already
102 if(!m_caloEntrance.get()){
103 m_caloEntrance.set(m_extrapolator->trackingGeometry()->trackingVolume(m_caloEntranceName));
104 if(!m_caloEntrance.get())
105 ATH_MSG_WARNING("CaloEntrance not found");
106 else
107 ATH_MSG_DEBUG("CaloEntrance found");
108 }
109
110 ATH_MSG_DEBUG( "[ fastCaloSim transport ] after calo entrance ");
111
112 std::unique_ptr<const Trk::TrackParameters> caloEntry = nullptr;
113
114 if(m_caloEntrance.get() && m_caloEntrance.get()->inside(pos, 0.001) && !m_extrapolator->trackingGeometry()->atVolumeBoundary(pos,m_caloEntrance.get(), 0.001)){
115 std::vector<Trk::HitInfo>* dummyHitVector = nullptr;
116 if (charge == 0){
117 caloEntry =
118 m_extrapolator->transportNeutralsWithPathLimit(inputPar,
119 pathLim,
120 timeLim,
122 pHypothesis,
123 dummyHitVector,
124 nextGeoID,
125 m_caloEntrance.get());
126 }else{
127 caloEntry = m_extrapolator->extrapolateWithPathLimit(inputPar,
128 pathLim,
129 timeLim,
131 pHypothesis,
132 dummyHitVector,
133 nextGeoID,
134 m_caloEntrance.get());
135 }
136 } else
137 caloEntry = inputPar.uniqueClone();
138
139 ATH_MSG_DEBUG( "[ fastCaloSim transport ] after calo caloEntry ");
140
141 if(caloEntry){
142 std::unique_ptr<const Trk::TrackParameters> eParameters = nullptr;
143
144 // save Calo entry hit (fallback info)
145 hitVector->push_back(Trk::HitInfo(caloEntry->uniqueClone(), timeLim.time, nextGeoID, 0.));
146
148 "[ fastCaloSim transport ] starting Calo transport from position eta="
149 << caloEntry->position().eta() << " phi=" << caloEntry->position().phi()
150 << " d=" << caloEntry->position().mag());
151
152 std::vector<Trk::HitInfo>* rawHitVector = hitVector.get();
153 if (charge == 0){
154 eParameters =
155 m_extrapolator->transportNeutralsWithPathLimit(*caloEntry,
156 pathLim,
157 timeLim,
159 pHypothesis,
160 rawHitVector,
161 nextGeoID);
162 }else{
163 eParameters =
164 m_extrapolator->extrapolateWithPathLimit(*caloEntry,
165 pathLim,
166 timeLim,
168 pHypothesis,
169 rawHitVector,
170 nextGeoID);
171 }
172 // save Calo exit hit (fallback info)
173 if(eParameters) hitVector->push_back(Trk::HitInfo(std::move(eParameters), timeLim.time, nextGeoID, 0.));
174 } //if caloEntry
175
176 //used to identify ID-Calo boundary in tracking tools
177 int IDCaloBoundary = 3000;
178
179 if(msgLvl(MSG::DEBUG)){
180 std::vector<Trk::HitInfo>::iterator it = hitVector->begin();
181 while (it < hitVector->end()){
182 int sample=(*it).detID;
183 Amg::Vector3D hitPos = (*it).trackParms->position();
184 ATH_MSG_DEBUG(" HIT: layer="<<sample<<" sample="<<sample-IDCaloBoundary<<" eta="<<hitPos.eta()<<" phi="<<hitPos.phi()<<" d="<<hitPos.mag());
185 ++it;
186 }
187 }
188
189 std::vector<Trk::HitInfo>::iterator it2 = hitVector->begin();
190 while(it2 < hitVector->end()){
191 int sample=(*it2).detID;
192 Amg::Vector3D hitPos = (*it2).trackParms->position();
193 ATH_MSG_DEBUG(" HIT: layer="<<sample<<" sample="<<sample-IDCaloBoundary<<" eta="<<hitPos.eta()<<" phi="<<hitPos.phi()<<" r="<<hitPos.perp()<<" z="<<hitPos[Amg::z]);
194 ++it2;
195 }
196
197 // Extrapolation may fail for very low pT charged particles. Enforce charge 0 to prevent this
198 if (!forceNeutral && hitVector->empty()){
199 ATH_MSG_DEBUG("forcing neutral charge in FastCaloSimCaloTransportation::caloHits");
200 transport(truth, true);
201 }
202 // Don't expect this ever to happen. Nevertheless, error handling should be improved.
203 // This may require changes in periphery (adjustments after setting function type to StatusCode)
204 else if(hitVector->empty()) ATH_MSG_ERROR("Empty hitVector even after forcing neutral charge. This may cause a segfault soon.");
205
206
207 std::vector<G4FieldTrack> caloSteps = convertToFieldTrack(*hitVector);
208
209 return caloSteps;
210
211}
212
213
214std::vector<G4FieldTrack> FastCaloSimCaloTransportation::convertToFieldTrack(const std::vector<Trk::HitInfo>& vec) const{
215
216 std::vector<G4FieldTrack> caloSteps;
217 for (auto& step : vec){
218 G4FieldTrack track = G4FieldTrack(' ');
219 track.SetPosition(Amg::EigenToHep3Vector(step.trackParms->position()));
220 track.SetMomentum(Amg::EigenToHep3Vector(step.trackParms->momentum()));
221 track.SetChargeAndMoments(step.trackParms->charge());
222 caloSteps.push_back(track);
223 }
224
225 return caloSteps;
226
227}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
double charge(const T &p)
Definition AtlasPID.h:997
std::vector< size_t > vec
std::vector< FPGATrackSimHit > hitVector
ATLAS-specific HepMC functions.
static AtlasG4EventUserInfo * GetEventUserInfo()
virtual std::vector< G4FieldTrack > transport(const TFCSTruthState *truth, bool forceNeutral=false) const override final
Trk::PdgToParticleHypothesis m_pdgToParticleHypothesis
virtual StatusCode initialize() override final
CxxUtils::CachedPointer< const Trk::TrackingVolume > m_caloEntrance
FastCaloSimCaloTransportation(const std::string &t, const std::string &n, const IInterface *p)
virtual StatusCode finalize() override final
PublicToolHandle< Trk::ITimedExtrapolator > m_extrapolator
std::vector< G4FieldTrack > convertToFieldTrack(const std::vector< Trk::HitInfo > &vec) const
int pdgid() const
const TLorentzVector & vertex() const
std::unique_ptr< ParametersBase< DIM, T > > uniqueClone() const
clone method for polymorphic deep copy returning unique_ptr; it is not overriden, but uses the existi...
CLHEP::Hep3Vector EigenToHep3Vector(const Amg::Vector3D &eigenvector)
Converts an Eigen-based Amg::Vector3D into a CLHEP-based CLHEP::Hep3Vector.
Eigen::Matrix< double, 3, 1 > Vector3D
double charge(const T &p)
@ alongMomentum
CurvilinearParametersT< TrackParametersDim, Charged, PlaneSurface > CurvilinearParameters
ParticleHypothesis
Enumeration for Particle hypothesis respecting the interaction with material.