GGS(GenericGEANT4Simulation)Software  2.7.0
 All Data Structures Namespaces Files Functions Variables Typedefs 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
14 #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(NULL), _outTree(NULL) {
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  // Create MC truth info branch
68  _outTree->Branch("mcTruthInfo", "GGSTMCTruthInfo", &_mcTruthInfo);
69 }
70 
71 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
72 
73 void GGSMCTruthAction::EndOfRunAction(const G4Run *) {
74 
75  if (_outTreeName != "") {
76  // Write the tree if we are not using the default one
77  _outRootFile->cd();
78  _outTree->Write();
79  }
81  _outRootFile = NULL;
82  _outTree = NULL;
83 }
84 
85 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
86 
87 void GGSMCTruthAction::_Convert(const G4Event *event, GGSTMCTruthInfo *&mcTruth) {
88 
89  // -----------------------------------------------
90  // check if GGSTMCTruthInfo exists. if not, create it
91  // -----------------------------------------------
92  if (!mcTruth)
93  mcTruth = new GGSTMCTruthInfo();
94  else
95  mcTruth->Reset();
96 
97  mcTruth->eventID = event->GetEventID();
98 
99  // Obtain the number of discarded events
101  // Add killed events to discarded ones
103 
104  G4int n_vertex = event->GetNumberOfPrimaryVertex();
105  //-----------------------
106  // Loop over all vertexes
107  //-----------------------
108  for (G4int iv = 0; iv < n_vertex; iv++) {
109  G4PrimaryVertex *pv = event->GetPrimaryVertex(iv);
110  //-----------------------------------
111  // Loop over primaries in this vertex
112  //-----------------------------------
113  for (G4int ipp = 0; ipp < pv->GetNumberOfParticle(); ipp++) {
114 
115  G4PrimaryParticle *pp = pv->GetPrimary(ipp);
116 
117  GGSTParticle *tPP = new GGSTParticle();
118  tPP->pos[0] = pv->GetX0() / cm;
119  tPP->pos[1] = pv->GetY0() / cm;
120  tPP->pos[2] = pv->GetZ0() / cm;
121  tPP->time = pv->GetT0() / ns;
122 
123  //------------------------------
124  // Extract primary particle info
125  //------------------------------
126 
127  tPP->PDGCode = pp->GetPDGcode();
128  tPP->trackID = pp->GetTrackID();
129  tPP->mom[0] = pp->GetPx() / GeV;
130  tPP->mom[1] = pp->GetPy() / GeV;
131  tPP->mom[2] = pp->GetPz() / GeV;
132 
133  GGSPrimaryParticleInfo *partInfo = (GGSPrimaryParticleInfo *)(pp->GetUserInformation());
134  if (partInfo) {
135  tPP->isTrackKilled = partInfo->isTrackKilled;
136  } else {
137  tPP->isTrackKilled = 2;
138  }
139 
140  //-----------------------------
141  // Record primary particle info
142  //-----------------------------
143  mcTruth->AddPrimaryParticle(tPP);
144  delete tPP;
145  }
146  }
147 }
148 
149 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
Float_t mom[3]
Momentum at generation [GeV].
Definition: GGSTParticle.h:24
TFile * GetFileForThisRun(const path &baseName, const G4Run *run)
Opens a file for a given run and returns a pointer to it.
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.
void CloseFileForThisRun(const path &baseName)
Closes the ROOT output file.
Int_t PDGCode
PDG code of particle (see http://www3.nd.edu/~avillano/geant4/geant4_pid.html).
Definition: GGSTParticle.h:21
void EndOfEventAction(const G4Event *event)
Fills MC truth informations for current event.
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_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
static GGSRunManager * GetRunManager()
Static getter function the run manager.
int GetNDiscardedEvents()
Getter method for number of discarded events.
Definition: GGSRunManager.h:85
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...
int GetNKilledEvents()
Getter method for number of killed events.
Definition: GGSRunManager.h:98