EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SteppingAction.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file SteppingAction.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2017-2020 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
9 #include "SteppingAction.hpp"
10 
11 #include "Acts/Utilities/Units.hpp"
12 
13 #include <stdexcept>
14 
15 #include <G4Material.hh>
16 #include <G4Step.hh>
17 
18 using namespace ActsExamples;
19 
21 
23  return s_instance;
24 }
25 
26 SteppingAction::SteppingAction() : G4UserSteppingAction() {
27  if (s_instance) {
28  throw std::logic_error(
29  "Attempted to duplicate the SteppingAction singleton");
30  } else {
31  s_instance = this;
32  }
33 }
34 
36  s_instance = nullptr;
37 }
38 
40  // get the material
41  G4Material* material = step->GetPreStepPoint()->GetMaterial();
42 
43  if (material && material->GetName() != "Vacuum" &&
44  material->GetName() != "Air") {
45  // Quantities valid for elemental materials and mixtures
46  double X0 = (material->GetRadlen() / CLHEP::mm) * Acts::UnitConstants::mm;
47  double L0 = (material->GetNuclearInterLength() / CLHEP::mm) *
49  double rho = (material->GetDensity() / (CLHEP::gram / CLHEP::mm3)) *
51  // Get{A,Z} is only meaningful for single-element materials (according to
52  // the Geant4 docs). Need to compute average manually.
53  const G4ElementVector* elements = material->GetElementVector();
54  const G4double* fraction = material->GetFractionVector();
55  size_t nElements = material->GetNumberOfElements();
56  double Ar = 0.;
57  double Z = 0.;
58  if (nElements == 1) {
59  Ar = material->GetA() / (CLHEP::gram / CLHEP::mole);
60  Z = material->GetZ();
61  } else {
62  for (size_t i = 0; i < nElements; i++) {
63  Ar +=
64  elements->at(i)->GetA() * fraction[i] / (CLHEP::gram / CLHEP::mole);
65  Z += elements->at(i)->GetZ() * fraction[i];
66  }
67  }
68  // construct passed material slab for the step
69  const auto slab = Acts::MaterialSlab(
70  Acts::Material::fromMassDensity(X0, L0, Ar, Z, rho),
71  (step->GetStepLength() / CLHEP::mm) * Acts::UnitConstants::mm);
72 
73  /* G4cout << *material << G4endl;
74  G4cout << "----G4StepMaterial----" << G4endl;
76  G4cout << "Material: " << material->GetName() << G4endl;
77  G4cout << "X0: " << X0 << G4endl;
78  G4cout << "L0: " << L0 << G4endl;
79  G4cout << "A: " << A << G4endl;
80  G4cout << "Z: " << Z << G4endl;
81  G4cout << "rho: " << rho << G4endl;
82  G4cout << "steplength: " << steplength << G4endl;*/
83 
84  // create the RecordedMaterialSlab
85  const auto& rawPos = step->GetPreStepPoint()->GetPosition();
86  const auto& rawDir = step->GetPreStepPoint()->GetMomentum();
87  Acts::MaterialInteraction mInteraction;
88  mInteraction.position = Acts::Vector3D(rawPos.x(), rawPos.y(), rawPos.z());
89  mInteraction.direction = Acts::Vector3D(rawDir.x(), rawDir.y(), rawDir.z());
90  mInteraction.direction.normalized();
91  mInteraction.materialSlab = slab;
92  m_materialSteps.push_back(mInteraction);
93 
94  // // Get the track associated to the step
95  // G4Track* track = step->GetTrack();
96  // const auto& trkPos = track->GetPosition();
97  // const auto& trkTime = track->GetGlobalTime();
98  // // Get the particle from the track
99  // const auto& par = track->GetDynamicParticle();
100  // std::string parName = track->GetDefinition()->GetParticleName();
101  // const auto& par4Mom = par->Get4Momentum();
102  //
103  // if (track->GetTrackID() == 1) { // only consider primary tracks
104  // /*G4cout <<
105  // "px: " << par4Mom.px() << "\t" <<
106  // "py: " << par4Mom.py() << "\t" <<
107  // "pz: " << par4Mom.pz() << "\t" <<
108  // "e: " << par4Mom.e() << G4endl;
109  //
110  // G4cout <<
111  // "x: " << trkPos.x() << "\t" <<
112  // "y: " << trkPos.y() << "\t" <<
113  // "z: " << trkPos.z() << "\t" <<
114  // "t: " << trkTime << G4endl;
115  // */
116  //
117  // m_tracksteps.emplace_back(
118  // 0,
119  // 0, // set Acts::GeometryIdentifier = 0 and Barcode = 0
120  // Acts::ActsVectorD<4>(trkPos.x() * Acts::UnitConstants::mm,
121  // trkPos.y() * Acts::UnitConstants::mm,
122  // trkPos.z() * Acts::UnitConstants::mm,
123  // trkTime * Acts::UnitConstants::ns), // pos4
124  // Acts::ActsVectorD<4>(
125  // par4Mom.px() * Acts::UnitConstants::MeV,
126  // par4Mom.py() * Acts::UnitConstants::MeV,
127  // par4Mom.pz() * Acts::UnitConstants::MeV,
128  // par4Mom.e() * Acts::UnitConstants::MeV), // before4
129  // Acts::ActsVectorD<4>(0, 0, 0, 0)); // after4
130  // }
131  }
132 }
133 
135  m_materialSteps.clear();
136  m_trackSteps.clear();
137 }