ATLAS Offline Software
GeantFollowerHelper.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 // GeantFollowerHelper.cxx, (c) ATLAS Detector Software
8 
9 // StoreGate
11 #include "TTree.h"
12 #include "GaudiKernel/ITHistSvc.h"
13 // CLHEP
14 #include "CLHEP/Units/SystemOfUnits.h"
15 #include "CLHEP/Geometry/Transform3D.h"
16 // Trk
19 // Amg
21 //other
22 
23 // constructor
24 Trk::GeantFollowerHelper::GeantFollowerHelper(const std::string& t, const std::string& n, const IInterface* p) :
25  base_class(t,n,p),
26  m_extrapolator(""),
27  m_extrapolateDirectly(true),
28  m_extrapolateIncrementally(true),
29  m_parameterCache(nullptr),
30  m_tX0Cache(0.),
31  m_validationTreeName("G4Follower_"+n),
32  m_validationTreeDescription("Output of the G4Follower_"),
33  m_validationTreeFolder("/val/G4Follower_"+n),
34  m_validationTree(nullptr)
35 {
36  // properties
37  declareProperty("Extrapolator", m_extrapolator);
38  declareProperty("ExtrapolateDirectly", m_extrapolateDirectly);
39  declareProperty("ExtrapolateIncrementally", m_extrapolateIncrementally);
40 }
41 
42 // destructor
44 = default;
45 
46 // Athena standard methods
47 // initialize
49 {
50  m_treeData = std::make_unique<TreeData>();
51 
52  if (m_extrapolator.retrieve().isFailure()){
53  ATH_MSG_ERROR("Could not retrieve Extrapolator " << m_extrapolator << " . Abort.");
54  return StatusCode::FAILURE;
55  }
56 
57  // create the new Tree
58  m_validationTree = new TTree(m_validationTreeName.c_str(), m_validationTreeDescription.c_str());
59 
60  m_validationTree->Branch("InitX", &m_treeData->m_t_x, "initX/F");
61  m_validationTree->Branch("InitY", &m_treeData->m_t_y, "initY/F");
62  m_validationTree->Branch("InitZ", &m_treeData->m_t_z, "initZ/F");
63  m_validationTree->Branch("InitTheta", &m_treeData->m_t_theta, "initTheta/F");
64  m_validationTree->Branch("InitEta", &m_treeData->m_t_eta, "initEta/F");
65  m_validationTree->Branch("InitPhi", &m_treeData->m_t_phi, "initPhi/F");
66  m_validationTree->Branch("InitP", &m_treeData->m_t_p, "initP/F");
67  m_validationTree->Branch("InitPdg", &m_treeData->m_t_pdg, "initPdg/I");
68  m_validationTree->Branch("InitCharge", &m_treeData->m_t_charge, "initQ/F");
69 
70  m_validationTree->Branch("G4Steps", &m_treeData->m_g4_steps, "g4steps/I");
71  m_validationTree->Branch("G4StepP", m_treeData->m_g4_p, "g4stepP[g4steps]/F");
72  m_validationTree->Branch("G4StepEta", m_treeData->m_g4_eta, "g4stepEta[g4steps]/F");
73  m_validationTree->Branch("G4StepTheta", m_treeData->m_g4_theta, "g4stepTheta[g4steps]/F");
74  m_validationTree->Branch("G4StepPhi", m_treeData->m_g4_phi, "g4stepPhi[g4steps]/F");
75  m_validationTree->Branch("G4StepX", m_treeData->m_g4_x, "g4stepX[g4steps]/F");
76  m_validationTree->Branch("G4StepY", m_treeData->m_g4_y, "g4stepY[g4steps]/F");
77  m_validationTree->Branch("G4StepZ", m_treeData->m_g4_z, "g4stepZ[g4steps]/F");
78  m_validationTree->Branch("G4AccumTX0", m_treeData->m_g4_tX0, "g4stepAccTX0[g4steps]/F");
79  m_validationTree->Branch("G4StepT", m_treeData->m_g4_t, "g4stepTX[g4steps]/F");
80  m_validationTree->Branch("G4StepX0", m_treeData->m_g4_X0, "g4stepX0[g4steps]/F");
81 
82  m_validationTree->Branch("TrkStepStatus",m_treeData->m_trk_status, "trkstepStatus[g4steps]/I");
83  m_validationTree->Branch("TrkStepP", m_treeData->m_trk_p, "trkstepP[g4steps]/F");
84  m_validationTree->Branch("TrkStepEta", m_treeData->m_trk_eta, "trkstepEta[g4steps]/F");
85  m_validationTree->Branch("TrkStepTheta", m_treeData->m_trk_theta, "trkstepTheta[g4steps]/F");
86  m_validationTree->Branch("TrkStepPhi", m_treeData->m_trk_phi, "trkstepPhi[g4steps]/F");
87  m_validationTree->Branch("TrkStepX", m_treeData->m_trk_x, "trkstepX[g4steps]/F");
88  m_validationTree->Branch("TrkStepY", m_treeData->m_trk_y, "trkstepY[g4steps]/F");
89  m_validationTree->Branch("TrkStepZ", m_treeData->m_trk_z, "trkstepZ[g4steps]/F");
90  m_validationTree->Branch("TrkStepLocX", m_treeData->m_trk_lx, "trkstepLX[g4steps]/F");
91  m_validationTree->Branch("TrkStepLocY", m_treeData->m_trk_ly, "trkstepLY[g4steps]/F");
92 
93  // now register the Tree
94  SmartIF<ITHistSvc> tHistSvc{Gaudi::svcLocator()->service("THistSvc")};
95  if ( !tHistSvc ) {
96  ATH_MSG_ERROR( "Could not find Hist Service -> Switching ValidationMode Off !" );
97  delete m_validationTree; m_validationTree = nullptr;
98  }
99  if ((tHistSvc->regTree(m_validationTreeFolder, m_validationTree)).isFailure()) {
100  ATH_MSG_ERROR( "Could not register the validation Tree -> Switching ValidationMode Off !" );
101  delete m_validationTree; m_validationTree = nullptr;
102  }
103 
104  ATH_MSG_INFO("initialize() successful" );
105  return StatusCode::SUCCESS;
106 }
107 
109 {
110  return StatusCode::SUCCESS;
111 }
112 
114 {
115  m_treeData->m_t_x = 0.;
116  m_treeData->m_t_y = 0.;
117  m_treeData->m_t_z = 0.;
118  m_treeData->m_t_theta = 0.;
119  m_treeData->m_t_eta = 0.;
120  m_treeData->m_t_phi = 0.;
121  m_treeData->m_t_p = 0.;
122  m_treeData->m_t_charge = 0.;
123  m_treeData->m_t_pdg = 0;
124  m_treeData->m_g4_steps = 0;
125  m_tX0Cache = 0.;
126 }
127 
129  const G4ThreeVector& mom,
130  int pdg, double charge,
131  float t, float X0)
132 {
133  // construct the initial parameters
134  Amg::Vector3D npos(pos.x(),pos.y(),pos.z());
135  Amg::Vector3D nmom(mom.x(),mom.y(),mom.z());
136  if (!m_treeData->m_g4_steps){
137  ATH_MSG_INFO("Initial step ... preparing event cache.");
138  m_treeData->m_t_x = pos.x();
139  m_treeData->m_t_y = pos.y();
140  m_treeData->m_t_z = pos.z();
141  m_treeData->m_t_theta = mom.theta();
142  m_treeData->m_t_eta = mom.eta();
143  m_treeData->m_t_phi = mom.phi();
144  m_treeData->m_t_p = mom.mag();
145  m_treeData->m_t_charge = charge;
146  m_treeData->m_t_pdg = pdg;
147  m_treeData->m_g4_steps = -1;
148  m_tX0Cache = 0.;
149  m_parameterCache = new Trk::CurvilinearParameters(npos, nmom, charge);
150  return;
151  }
152 
153  // jumping over inital step
154  m_treeData->m_g4_steps = (m_treeData->m_g4_steps == -1) ? 0 : m_treeData->m_g4_steps;
155 
156  if (!m_parameterCache){
157  ATH_MSG_WARNING("No Parameters available. Bailing out.");
158  return;
159  }
160 
161  if ( m_treeData->m_g4_steps >= MAXPROBES) {
162  ATH_MSG_WARNING("Maximum number of " << MAXPROBES << " reached, step is ignored.");
163  return;
164  }
165  // parameters of the G4 step point
166  Trk::CurvilinearParameters* g4Parameters = new Trk::CurvilinearParameters(npos, nmom, m_treeData->m_t_charge);
167  // destination surface
168  const Trk::PlaneSurface& destinationSurface = g4Parameters->associatedSurface();
169  // extrapolate to the destination surface
170  const EventContext& ctx = Gaudi::Hive::currentContext();
171  const Trk::TrackParameters* trkParameters = m_extrapolateDirectly ?
172  m_extrapolator->extrapolateDirectly(ctx,
173  *m_parameterCache,
174  destinationSurface,
175  Trk::alongMomentum,false).release() :
176  m_extrapolator->extrapolate(ctx,
177  *m_parameterCache,
178  destinationSurface,
179  Trk::alongMomentum,false).release();
180  // fill the geant information and the trk information
181  m_treeData->m_g4_p[m_treeData->m_g4_steps] = mom.mag();
182  m_treeData->m_g4_eta[m_treeData->m_g4_steps] = mom.eta();
183  m_treeData->m_g4_theta[m_treeData->m_g4_steps] = mom.theta();
184  m_treeData->m_g4_phi[m_treeData->m_g4_steps] = mom.phi();
185  m_treeData->m_g4_x[m_treeData->m_g4_steps] = pos.x();
186  m_treeData->m_g4_y[m_treeData->m_g4_steps] = pos.y();
187  m_treeData->m_g4_z[m_treeData->m_g4_steps] = pos.z();
188  float tX0 = X0 > 10e-5 ? t/X0 : 0.;
189  m_tX0Cache += tX0;
190  m_treeData->m_g4_tX0[m_treeData->m_g4_steps] = m_tX0Cache;
191  m_treeData->m_g4_t[m_treeData->m_g4_steps] = t;
192  m_treeData->m_g4_X0[m_treeData->m_g4_steps] = X0;
193 
194  m_treeData->m_trk_status[m_treeData->m_g4_steps] = trkParameters ? 1 : 0;
195  m_treeData->m_trk_p[m_treeData->m_g4_steps] = trkParameters ? trkParameters->momentum().mag() : 0.;
196  m_treeData->m_trk_eta[m_treeData->m_g4_steps] = trkParameters ? trkParameters->momentum().eta() : 0.;
197  m_treeData->m_trk_theta[m_treeData->m_g4_steps] = trkParameters ? trkParameters->momentum().theta() : 0.;
198  m_treeData->m_trk_phi[m_treeData->m_g4_steps] = trkParameters ? trkParameters->momentum().phi() : 0.;
199  m_treeData->m_trk_x[m_treeData->m_g4_steps] = trkParameters ? trkParameters->position().x() : 0.;
200  m_treeData->m_trk_y[m_treeData->m_g4_steps] = trkParameters ? trkParameters->position().y() : 0.;
201  m_treeData->m_trk_z[m_treeData->m_g4_steps] = trkParameters ? trkParameters->position().z() : 0.;
202  m_treeData->m_trk_lx[m_treeData->m_g4_steps] = trkParameters ? trkParameters->parameters()[Trk::locX] : 0.;
203  m_treeData->m_trk_ly[m_treeData->m_g4_steps] = trkParameters ? trkParameters->parameters()[Trk::locY] : 0.;
204 
205  // update the parameters if needed/configured
206  if (m_extrapolateIncrementally && trkParameters) {
207  delete m_parameterCache;
208  m_parameterCache = trkParameters;
209  }
210  // delete cache and increment
211  delete g4Parameters;
212  ++m_treeData->m_g4_steps;
213 }
214 
216 {
217  // fill the validation tree
218  m_validationTree->Fill();
219  delete m_parameterCache;
220 }
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
Trk::GeantFollowerHelper::m_extrapolateIncrementally
bool m_extrapolateIncrementally
Definition: GeantFollowerHelper.h:51
Trk::GeantFollowerHelper::beginEvent
virtual void beginEvent() override
Definition: GeantFollowerHelper.cxx:113
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
Trk::locX
@ locX
Definition: ParamDefs.h:37
Trk::locY
@ locY
local cartesian
Definition: ParamDefs.h:38
Trk::ParametersBase::position
const Amg::Vector3D & position() const
Access method for the position.
Trk::GeantFollowerHelper::trackParticle
virtual void trackParticle(const G4ThreeVector &pos, const G4ThreeVector &mom, int pdg, double charge, float t, float X0) override
Definition: GeantFollowerHelper.cxx:128
Trk::CurvilinearParameters
CurvilinearParametersT< TrackParametersDim, Charged, PlaneSurface > CurvilinearParameters
Definition: Tracking/TrkEvent/TrkParameters/TrkParameters/TrackParameters.h:29
Trk::GeantFollowerHelper::finalize
virtual StatusCode finalize() override
Definition: GeantFollowerHelper.cxx:108
IExtrapolator.h
Trk::GeantFollowerHelper::initialize
virtual StatusCode initialize() override
Definition: GeantFollowerHelper.cxx:48
Trk::alongMomentum
@ alongMomentum
Definition: PropDirection.h:20
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
Trk::GeantFollowerHelper::m_extrapolateDirectly
bool m_extrapolateDirectly
Definition: GeantFollowerHelper.h:50
GeantFollowerHelper.h
pdg_comparison.X0
X0
Definition: pdg_comparison.py:314
GeoPrimitives.h
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
ParticleGun_EoverP_Config.mom
mom
Definition: ParticleGun_EoverP_Config.py:63
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
beamspotman.n
n
Definition: beamspotman.py:731
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
Trk::GeantFollowerHelper::m_extrapolator
ToolHandle< IExtrapolator > m_extrapolator
Definition: GeantFollowerHelper.h:49
Trk::GeantFollowerHelper::~GeantFollowerHelper
virtual ~GeantFollowerHelper()
Trk::ParametersBase
Definition: ParametersBase.h:55
Trk::CurvilinearParametersT
Definition: CurvilinearParametersT.h:48
Trk::CurvilinearParametersT::associatedSurface
virtual const S & associatedSurface() const override final
Access to the Surface method.
charge
double charge(const T &p)
Definition: AtlasPID.h:538
Amg::Vector3D
Eigen::Matrix< double, 3, 1 > Vector3D
Definition: GeoPrimitives.h:47
python.LumiBlobConversion.pos
pos
Definition: LumiBlobConversion.py:18
Trk::ParametersBase::momentum
const Amg::Vector3D & momentum() const
Access method for the momentum.
MAXPROBES
#define MAXPROBES
Definition: ActsGeantFollowerHelper.h:22
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
Trk::PlaneSurface
Definition: PlaneSurface.h:64
PlaneSurface.h
Trk::GeantFollowerHelper::GeantFollowerHelper
GeantFollowerHelper(const std::string &, const std::string &, const IInterface *)
Definition: GeantFollowerHelper.cxx:24
if
if(febId1==febId2)
Definition: LArRodBlockPhysicsV0.cxx:567
Trk::GeantFollowerHelper::endEvent
virtual void endEvent() override
Definition: GeantFollowerHelper.cxx:215