ATLAS Offline Software
ScoringPlane.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include "ScoringPlane.h"
6 #include "G4Step.hh"
7 #include "CLHEP/Units/SystemOfUnits.h"
8 #include "TTree.h"
9 #include "TFile.h"
10 
11 
12 namespace G4UA
13 {
14 
15  //----------------------------------------------------------------------------
16  // Constructor
17  //----------------------------------------------------------------------------
19  : m_config(config),
20  m_evt(0), m_ntr(0), m_pdg(0), m_cha(0), m_ene(0),
21  m_vx(0), m_vy(0), m_vz(0),
22  m_x0(0), m_y0(0), m_z0(0), m_t0(0),
23  m_px0(0), m_py0(0), m_pz0(0),
24  m_x1(0), m_y1(0), m_z1(0), m_t1(0),
25  m_px1(0), m_py1(0), m_pz1(0),
26  m_x(0), m_y(0), m_z(0)
27  {
28  }
29 
31  {
32  }
33 
34  void ScoringPlane::BeginOfRunAction(const G4Run*)
35  {
36 
37  m_tree0 = std::make_unique<TTree>("t0", "ATHENA event tree");
38 
39  m_tree0->Branch("evt", &m_evt, "evt/I");
40  m_tree0->Branch("ntr", &m_ntr, "ntr/I");
41 
42  m_tree1 = std::make_unique<TTree>("t1", "ATHENA particle tree");
43 
44  m_tree1->Branch("evt", &m_evt, "evt/I");
45  m_tree1->Branch("ntr", &m_ntr, "ntr/I");
46  m_tree1->Branch("pdg", &m_pdg, "pdg/I");
47  m_tree1->Branch("cha", &m_cha, "cha/D");
48  m_tree1->Branch("ene", &m_ene, "ene/D");
49  m_tree1->Branch("x0", &m_x0, "x0/D");
50  m_tree1->Branch("y0", &m_y0, "y0/D");
51  m_tree1->Branch("z0", &m_z0, "z0/D");
52  m_tree1->Branch("t0", &m_t0, "t0/D");
53  m_tree1->Branch("px0", &m_px0, "px0/D");
54  m_tree1->Branch("py0", &m_py0, "py0/D");
55  m_tree1->Branch("pz0", &m_pz0, "pz0/D");
56  m_tree1->Branch("x1", &m_x1, "x1/D");
57  m_tree1->Branch("y1", &m_y1, "y1/D");
58  m_tree1->Branch("z1", &m_z1, "z1/D");
59  m_tree1->Branch("t1", &m_t1, "t1/D");
60  m_tree1->Branch("px1", &m_px1, "px1/D");
61  m_tree1->Branch("py1", &m_py1, "py1/D");
62  m_tree1->Branch("pz1", &m_pz1, "pz1/D");
63  m_tree1->Branch("vx", &m_vx, "vx/D");
64  m_tree1->Branch("vy", &m_vy, "vy/D");
65  m_tree1->Branch("vz", &m_vz, "vz/D");
66  m_tree1->Branch("x", &m_x, "x/D");
67  m_tree1->Branch("y", &m_y, "y/D");
68  m_tree1->Branch("z", &m_z, "z/D");
69 
70  m_evt = 0;
71 
72  G4cout<<"ScoringPlane: placing scoring plane at [mm]: " << m_config.plane << G4endl;
73  G4cout<<"ScoringPlane: stop and kill particles: " << m_config.pkill << G4endl;
74  G4cout<<"ScoringPlane: output root filename: " << m_config.fname << G4endl;
75  }
76 
77  void ScoringPlane::EndOfRunAction(const G4Run*){
78  TFile* file = new TFile(m_config.fname.c_str(), "RECREATE", "ATHENA ufo simulation");
79 
80  m_tree0->Write();
81  m_tree1->Write();
82 
83  file->Close();
84  }
85 
86  void ScoringPlane::UserSteppingAction(const G4Step* aStep)
87  {
88  m_z0 = aStep->GetPreStepPoint()->GetPosition().z();
89  m_z1 = aStep->GetPostStepPoint()->GetPosition().z();
90 
91  if (m_z0*m_config.plane < 0) return; // take only particles on one side
92  if (m_z1*m_config.plane < 0) return; // take only particles on one side
93  if (std::fabs(m_z0) < std::fabs(m_config.plane)) return; // take only particles flowing towards the IP
94  if (std::fabs(m_z1) > std::fabs(m_config.plane)) return; // take only particles flowing towards the IP
95 
96  m_ntr++;
97 
98  m_pdg = aStep->GetTrack()->GetDefinition()->GetPDGEncoding();
99  m_cha = aStep->GetTrack()->GetDefinition()->GetPDGCharge();
100  m_ene = aStep->GetTrack()->GetTotalEnergy();
101  m_vx = aStep->GetTrack()->GetVertexPosition().x();
102  m_vy = aStep->GetTrack()->GetVertexPosition().y();
103  m_vz = aStep->GetTrack()->GetVertexPosition().z();
104 
105  m_x0 = aStep->GetPreStepPoint()->GetPosition().x();
106  m_y0 = aStep->GetPreStepPoint()->GetPosition().y();
107  m_t0 = aStep->GetPreStepPoint()->GetGlobalTime()/CLHEP::ns;
108  m_px0 = aStep->GetPreStepPoint()->GetMomentum().x();
109  m_py0 = aStep->GetPreStepPoint()->GetMomentum().y();
110  m_pz0 = aStep->GetPreStepPoint()->GetMomentum().z();
111 
112  m_x1 = aStep->GetPostStepPoint()->GetPosition().x();
113  m_y1 = aStep->GetPostStepPoint()->GetPosition().y();
114  m_t1 = aStep->GetPostStepPoint()->GetGlobalTime()/CLHEP::ns;
115  m_px1 = aStep->GetPostStepPoint()->GetMomentum().x();
116  m_py1 = aStep->GetPostStepPoint()->GetMomentum().y();
117  m_pz1 = aStep->GetPostStepPoint()->GetMomentum().z();
118 
119  m_z = m_config.plane;
120  m_x = m_x0 + (m_z0!=m_z1 ? (m_x1-m_x0)/(m_z1-m_z0)*(m_z-m_z0) : (m_x1-m_x0)*0.5);
121  m_y = m_y0 + (m_z0!=m_z1 ? (m_y1-m_y0)/(m_z1-m_z0)*(m_z-m_z0) : (m_y1-m_y0)*0.5);
122 
123  m_tree1->Fill();
124 
125  if (m_config.pkill == 1) aStep->GetTrack()->SetTrackStatus(fStopAndKill);
126  else if (m_config.pkill == 2) aStep->GetTrack()->SetTrackStatus(fKillTrackAndSecondaries);
127 
128  //G4cout<<
129  // " z0: " << std::setw(10) << m_z0
130  // << " z1: " << std::setw(10) << m_z1
131  // << " x0: " << std::setw(10) << m_x0
132  // << " x1: " << std::setw(10) << m_x1
133  // << " x: " << std::setw(10) << m_x
134  // << " y0: " << std::setw(10) << m_y0
135  // << " y1: " << std::setw(10) << m_y1
136  // << " y: " << std::setw(10) << m_y << G4endl;
137 
138  }
139 
140  void ScoringPlane::BeginOfEventAction(const G4Event*)
141  {
142  m_evt++;
143  m_ntr = 0;
144  }
145 
146  void ScoringPlane::EndOfEventAction(const G4Event*)
147  {
148  m_tree0->Fill();
149  }
150 
151 } // namespace G4UA
G4UA::ScoringPlane::m_py1
double m_py1
Definition: ScoringPlane.h:71
G4UA::ScoringPlane::Config::pkill
int pkill
Definition: ScoringPlane.h:33
G4UA::ScoringPlane::m_z1
double m_z1
Definition: ScoringPlane.h:68
G4UA::ScoringPlane::m_x
double m_x
Definition: ScoringPlane.h:73
G4UA::ScoringPlane::m_pdg
int m_pdg
Definition: ScoringPlane.h:53
G4UA::ScoringPlane::EndOfEventAction
virtual void EndOfEventAction(const G4Event *) override
Definition: ScoringPlane.cxx:146
G4UA::ScoringPlane::m_pz1
double m_pz1
Definition: ScoringPlane.h:72
G4UA::ScoringPlane::m_x0
double m_x0
Definition: ScoringPlane.h:59
G4UA::ScoringPlane::~ScoringPlane
~ScoringPlane()
Definition: ScoringPlane.cxx:30
G4UA::ScoringPlane::m_y0
double m_y0
Definition: ScoringPlane.h:60
G4UA::ScoringPlane::m_ene
double m_ene
Definition: ScoringPlane.h:55
G4UA
for nSW
Definition: CalibrationDefaultProcessing.h:19
G4UA::ScoringPlane::m_evt
int m_evt
Definition: ScoringPlane.h:50
G4UA::ScoringPlane::m_ntr
int m_ntr
Definition: ScoringPlane.h:51
G4UA::ScoringPlane::m_vz
double m_vz
Definition: ScoringPlane.h:58
G4UA::ScoringPlane::m_vx
double m_vx
Definition: ScoringPlane.h:56
G4UA::ScoringPlane::m_x1
double m_x1
Definition: ScoringPlane.h:66
G4UA::ScoringPlane::m_px1
double m_px1
Definition: ScoringPlane.h:70
G4UA::ScoringPlane::m_config
Config m_config
Definition: ScoringPlane.h:45
G4UA::ScoringPlane::m_y1
double m_y1
Definition: ScoringPlane.h:67
G4UA::ScoringPlane::m_t0
double m_t0
Definition: ScoringPlane.h:62
G4UA::ScoringPlane::Config::plane
double plane
Definition: ScoringPlane.h:32
G4UA::ScoringPlane::EndOfRunAction
virtual void EndOfRunAction(const G4Run *) override
Definition: ScoringPlane.cxx:77
config
Definition: PhysicsAnalysis/AnalysisCommon/AssociationUtils/python/config.py:1
G4UA::ScoringPlane::BeginOfRunAction
virtual void BeginOfRunAction(const G4Run *) override
Definition: ScoringPlane.cxx:34
G4UA::ScoringPlane::Config
Definition: ScoringPlane.h:31
file
TFile * file
Definition: tile_monitor.h:29
G4UA::ScoringPlane::m_y
double m_y
Definition: ScoringPlane.h:74
G4UA::ScoringPlane::m_tree0
std::unique_ptr< TTree > m_tree0
Definition: ScoringPlane.h:47
G4UA::ScoringPlane::BeginOfEventAction
virtual void BeginOfEventAction(const G4Event *) override
Definition: ScoringPlane.cxx:140
G4UA::ScoringPlane::m_z
double m_z
Definition: ScoringPlane.h:75
G4UA::ScoringPlane::ScoringPlane
ScoringPlane(const Config &config)
Definition: ScoringPlane.cxx:18
G4UA::ScoringPlane::m_z0
double m_z0
Definition: ScoringPlane.h:61
G4UA::ScoringPlane::m_cha
double m_cha
Definition: ScoringPlane.h:54
G4UA::ScoringPlane::m_t1
double m_t1
Definition: ScoringPlane.h:69
G4UA::ScoringPlane::m_px0
double m_px0
Definition: ScoringPlane.h:63
G4UA::ScoringPlane::m_tree1
std::unique_ptr< TTree > m_tree1
Definition: ScoringPlane.h:48
G4UA::ScoringPlane::Config::fname
std::string fname
Definition: ScoringPlane.h:34
python.SystemOfUnits.ns
int ns
Definition: SystemOfUnits.py:130
G4UA::ScoringPlane::UserSteppingAction
virtual void UserSteppingAction(const G4Step *) override
Definition: ScoringPlane.cxx:86
G4UA::ScoringPlane::m_vy
double m_vy
Definition: ScoringPlane.h:57
G4UA::ScoringPlane::m_pz0
double m_pz0
Definition: ScoringPlane.h:65
G4UA::ScoringPlane::m_py0
double m_py0
Definition: ScoringPlane.h:64
ScoringPlane.h