GGS(GenericGEANT4Simulation)Software  2.6.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
13 #include "montecarlo/dataobjs/GGSTMCTruthInfo.h"
19 
20 #include "G4SystemOfUnits.hh"
21 
22 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
23 
25 
26 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
27 
29  _outBase(""), _outTreeName(""), _outRootFile(NULL), _outTree(NULL) {
30 
31  _messenger = new GGSMCTruthMessenger(this);
32  _mcTruthInfo = new GGSTMCTruthInfo;
33  _primaryParticle = new GGSTParticle;
34 }
35 
36 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
37 
39 
40  delete _messenger;
41  delete _mcTruthInfo;
42  delete _primaryParticle;
43 }
44 
45 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
46 
47 void GGSMCTruthAction::EndOfEventAction(const G4Event *event) {
48 
49  _Convert(event, _mcTruthInfo);
50 
51  if (_outTreeName != "") {
52  // Call fill only if not using the default tree
53  _outRootFile->cd();
54  _outTree->Fill();
55  }
56 }
57 
58 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
59 
60 void GGSMCTruthAction::BeginOfRunAction(const G4Run *run) {
61 
62  _outRootFile = GGSRootFileService::GetInstance().GetFileForThisRun(_outBase, run);
63  if (_outTreeName == "") {
64  _outTree = GGSRootFileService::GetInstance().GetDefaultTree(_outRootFile);
65  _outTree->SetTitle(TString(_outTree->GetTitle()) + "MCTruth ");
66  }
67  else
68  _outTree = new TTree(_outTreeName.data(), "MC truth");
69 
70  // Create MC truth info branch
71  _outTree->Branch("mcTruthInfo", "GGSTMCTruthInfo", &_mcTruthInfo);
72 
73 }
74 
75 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
76 
77 void GGSMCTruthAction::EndOfRunAction(const G4Run *) {
78 
79  if (_outTreeName != "") {
80  // Write the tree if we are not using the default one
81  _outRootFile->cd();
82  _outTree->Write();
83  }
85  _outRootFile = NULL;
86  _outTree = NULL;
87 }
88 
89 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
90 
91 void GGSMCTruthAction::_Convert(const G4Event *event, GGSTMCTruthInfo *&mcTruth) {
92 
93  // -----------------------------------------------
94  // check if GGSTMCTruthInfo exists. if not, create it
95  // -----------------------------------------------
96  if (!mcTruth)
97  mcTruth = new GGSTMCTruthInfo();
98  else
99  mcTruth->Reset();
100 
101  mcTruth->eventID = event->GetEventID();
102 
103  // Obtain the number of discarded events
105  // Add killed events to discarded ones
107 
108  G4int n_vertex = event->GetNumberOfPrimaryVertex();
109  //-----------------------
110  // Loop over all vertexes
111  //-----------------------
112  for (G4int iv = 0; iv < n_vertex; iv++) {
113  G4PrimaryVertex* pv = event->GetPrimaryVertex(iv);
114  //-----------------------------------
115  // Loop over primaries in this vertex
116  //-----------------------------------
117  for (G4int ipp = 0; ipp < pv->GetNumberOfParticle(); ipp++) {
118 
119  G4PrimaryParticle* pp = pv->GetPrimary(ipp);
120 
121  GGSTParticle *tPP = new GGSTParticle();
122  tPP->pos[0] = pv->GetX0() / cm;
123  tPP->pos[1] = pv->GetY0() / cm;
124  tPP->pos[2] = pv->GetZ0() / cm;
125  tPP->time = pv->GetT0() / ns;
126 
127  //------------------------------
128  // Extract primary particle info
129  //------------------------------
130 
131  tPP->PDGCode = pp->GetPDGcode();
132  tPP->trackID = pp->GetTrackID();
133  tPP->mom[0] = pp->GetPx() / GeV;
134  tPP->mom[1] = pp->GetPy() / GeV;
135  tPP->mom[2] = pp->GetPz() / GeV;
136 
137  GGSPrimaryParticleInfo *partInfo = (GGSPrimaryParticleInfo*) (pp->GetUserInformation());
138  if (partInfo) {
139  tPP->isTrackKilled = partInfo->isTrackKilled;
140  }
141  else {
142  tPP->isTrackKilled = 2;
143  }
144 
145  //-----------------------------
146  // Record primary particle info
147  //-----------------------------
148  mcTruth->AddPrimaryParticle(tPP);
149  delete tPP;
150  }
151  }
152 
153 }
154 
155 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
Float_t mom[3]
Momentum at generation [GeV].
Definition: GGSTParticle.h:25
TFile * GetFileForThisRun(const path &baseName, const G4Run *run)
Opens a file for a given run and returns a pointer to it.
UShort_t isTrackKilled
Flag to signal if particle&#39;s track have been killed(0: not killed, 1: killed, 2: info not available)...
Definition: GGSTParticle.h:27
~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:22
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:26
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:23
GGSMCTruthAction()
Constructor.
Float_t pos[3]
Point of generation [cm].
Definition: GGSTParticle.h:24
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:86
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:99