GGS(GenericGEANT4Simulation)Software  2.99.0
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Macros
GGSMCTruthAction.cpp
Go to the documentation of this file.
1 /*
2  * GGSMCTruthAction.cpp
3  *
4  * Created on: 17 Aug 2011
5  * Author: Nicola mori
6  */
7 
10 // GGS headers
13 #include "montecarlo/dataobjs/GGSTMCTruthInfo.h"
18 
19 #include "G4SystemOfUnits.hh"
20 
21 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
22 
24 
25 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
26 
27 GGSMCTruthAction::GGSMCTruthAction() : _outBase(""), _outTreeName(""), _outRootFile(nullptr), _outTree(nullptr) {
28 
29  _messenger = new GGSMCTruthMessenger(this);
30  _mcTruthInfo = new GGSTMCTruthInfo;
31  _primaryParticle = new GGSTParticle;
32 }
33 
34 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
35 
37 
38  delete _messenger;
39  delete _mcTruthInfo;
40  delete _primaryParticle;
41 }
42 
43 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
44 
45 void GGSMCTruthAction::EndOfEventAction(const G4Event *event) {
46 
47  _Convert(event, _mcTruthInfo);
48 
49  if (_outTreeName != "") {
50  // Call fill only if not using the default tree
51  _outRootFile->cd();
52  _outTree->Fill();
53  }
54 }
55 
56 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
57 
58 void GGSMCTruthAction::BeginOfRunAction(const G4Run *run) {
59 
60  _outRootFile = GGSRootFileService::GetInstance().GetFileForThisRun(_outBase, run);
61  if (_outTreeName == "") {
62  _outTree = GGSRootFileService::GetInstance().GetDefaultTree(_outRootFile);
63  _outTree->SetTitle(TString(_outTree->GetTitle()) + "MCTruth ");
64  } else {
65  _outTree = new TTree(_outTreeName.data(), "MC truth");
66  }
67 
68  // Create MC truth info branch
69  _outTree->Branch("mcTruthInfo", "GGSTMCTruthInfo", &_mcTruthInfo);
70 }
71 
72 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
73 
74 void GGSMCTruthAction::EndOfRunAction(const G4Run *run) {
75 
76  if (_outTreeName != "") {
77  // Write the tree if we are not using the default one
78  _outRootFile->cd();
79  _outTree->Write();
80  }
82  _outRootFile = nullptr;
83  _outTree = nullptr;
84 }
85 
86 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
87 
88 void GGSMCTruthAction::_Convert(const G4Event *event, GGSTMCTruthInfo *&mcTruth) {
89 
90  // -----------------------------------------------
91  // check if GGSTMCTruthInfo exists. if not, create it
92  // -----------------------------------------------
93  if (!mcTruth) {
94  mcTruth = new GGSTMCTruthInfo();
95  } else {
96  mcTruth->Reset();
97  }
98 
99  mcTruth->eventID = event->GetEventID();
100 
101  // Obtain the number of discarded events
102  GGSRunManagerExtensions &rmExt = dynamic_cast<GGSRunManagerExtensions &>(*(G4RunManager::GetRunManager()));
103  mcTruth->nDiscarded = rmExt.GetNDiscardedEvents();
104  // Add killed events to discarded ones
105  mcTruth->nDiscarded += rmExt.GetNKilledEvents();
106 
107  G4int n_vertex = event->GetNumberOfPrimaryVertex();
108  //-----------------------
109  // Loop over all vertexes
110  //-----------------------
111  for (G4int iv = 0; iv < n_vertex; iv++) {
112  G4PrimaryVertex *pv = event->GetPrimaryVertex(iv);
113  //-----------------------------------
114  // Loop over primaries in this vertex
115  //-----------------------------------
116  for (G4int ipp = 0; ipp < pv->GetNumberOfParticle(); ipp++) {
117 
118  G4PrimaryParticle *pp = pv->GetPrimary(ipp);
119 
120  GGSTParticle *tPP = new GGSTParticle();
121  tPP->pos[0] = pv->GetX0() / cm;
122  tPP->pos[1] = pv->GetY0() / cm;
123  tPP->pos[2] = pv->GetZ0() / cm;
124  tPP->time = pv->GetT0() / ns;
125 
126  //------------------------------
127  // Extract primary particle info
128  //------------------------------
129 
130  tPP->PDGCode = pp->GetPDGcode();
131  tPP->trackID = pp->GetTrackID();
132  tPP->mom[0] = pp->GetPx() / GeV;
133  tPP->mom[1] = pp->GetPy() / GeV;
134  tPP->mom[2] = pp->GetPz() / GeV;
135 
136  GGSPrimaryParticleInfo *partInfo = (GGSPrimaryParticleInfo *)(pp->GetUserInformation());
137  if (partInfo) {
138  tPP->isTrackKilled = partInfo->isTrackKilled;
139  } else {
140  tPP->isTrackKilled = 2;
141  }
142 
143  //-----------------------------
144  // Record primary particle info
145  //-----------------------------
146  mcTruth->AddPrimaryParticle(tPP);
147  delete tPP;
148  }
149  }
150 }
151 
152 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
Float_t mom[3]
Momentum at generation [GeV].
Definition: GGSTParticle.h:24
UShort_t isTrackKilled
Definition: GGSTParticle.h:26
~GGSMCTruthAction()
Destructor.
void EndOfRunAction(const G4Run *run)
Closes the output file for the current run.
Data class to store informations about the primary particle.
TFile * GetFileForThisRun(const std::filesystem::path &baseName, const G4Run *run)
Opens a file for a given run and returns a pointer to it.
Int_t PDGCode
PDG code of particle (see http://www3.nd.edu/~avillano/geant4/geant4_pid.html).
Definition: GGSTParticle.h:21
void CloseFileForThisRun(const std::filesystem::path &baseName, const G4Run *run)
Closes the ROOT output file.
void EndOfEventAction(const G4Event *event)
Fills MC truth informations for current event.
Class with additional functionalities for run managers.
bool isTrackKilled
True if the particle&#39;s track has been killed.
Float_t time
Time of generation [ns].
Definition: GGSTParticle.h:25
void BeginOfRunAction(const G4Run *run)
Opens the output file for the current run and prepares the output tree.
int GetNDiscardedEvents()
Getter method for number of discarded events.
Int_t trackID
Track ID.
Definition: GGSTParticle.h:22
GGSMCTruthAction()
Constructor.
Float_t pos[3]
Point of generation [cm].
Definition: GGSTParticle.h:23
void Reset()
Resets the object&#39;s properties.
Class to store G4 particles.
Definition: GGSTParticle.h:19
int GetNKilledEvents()
Getter method for number of killed events.
TTree * GetDefaultTree(TFile *file)
Gets the default tree for this file.
static GGSRootFileService & GetInstance()
Get reference to GGSRootFileService unique instance.
void AddPrimaryParticle(GGSTParticle *primaryParticle)
Adds a primary particle to the particles&#39; array.
Saves MC truth for each event.
A class to store MC truth informations on ROOT files.
Int_t eventID
The event ID.
A control messenger for GGSMCTruthAction.
#define RegisterUA(uaClassName)
Macro for registration of user actions classes.
UInt_t nDiscarded
The number of discarded + killed events the current event and the previous event saved in file...