GGS(GenericGEANT4Simulation)Software  2.7.0
 All Data Structures Namespaces Files Functions Variables Typedefs Macros
GGSPrimaryDisAction.cpp
Go to the documentation of this file.
1 /*
2  * GGSPrimaryDisAction.cpp
3  *
4  * Created on: 1 Jun 2020
5  * Author: Nicola Mori
6  */
7 
11 #include "montecarlo/dataobjs/GGSTParticle.h"
12 #include "montecarlo/dataobjs/GGSTPrimaryDisInfo.h"
14 
15 #include "G4HadronicProcess.hh"
16 #include "G4SystemOfUnits.hh"
17 
19 
21  : _disInfo("GGSTPrimaryDisInfo", 1), _outputProducts(false), _outBase(""), _outTreeName(""), _outRootFile(nullptr),
22  _outTree(nullptr),
23  _messenger(this, "/GGS/userActions/primaryDisAction/", "Commands for primary disappearance action") {
24 
25  _messenger.DeclareProperty("fileBase", _outBase, "Sets the base name for ROOT output file")
26  .SetGuidance(" Can be with or without extension (.root will be used automatically)")
27  .SetStates(G4State_PreInit, G4State_Idle);
28  _messenger.DeclareProperty("treeName", _outTreeName, "Set the name of the TTree object in the ROOT output file.")
29  .SetStates(G4State_PreInit, G4State_Idle);
30  _messenger.DeclareProperty("outProducts", _outputProducts, "Toggles on and off saving interaction products.")
31  .SetParameterName("outProducts", false)
32  .SetStates(G4State_PreInit, G4State_Idle);
33 }
34 
35 void GGSPrimaryDisAction::PreUserTrackingAction(const G4Track *track) {
36 
37  // Check whether it is a primary particle
38  G4PrimaryParticle *primary = track->GetDynamicParticle()->GetPrimaryParticle();
39  if (primary != nullptr) {
40  _tracks[track] = track->GetTrackID();
41  }
42 }
43 
44 void GGSPrimaryDisAction::PostUserTrackingAction(const G4Track *track) {
45 
46  if (_tracks.size() > 0) {
47  // See if the track is in the tracking list
48  std::map<const G4Track *, int>::iterator trackIter = _tracks.find(track);
49  if (trackIter != _tracks.end()) {
50  GGSTPrimaryDisInfo *newElement = (GGSTPrimaryDisInfo *)(_disInfo.ConstructedAt(_disInfo.GetEntries()));
51  newElement->Clear();
52 
53  // Fill the new element
54  newElement->primary.PDGCode = track->GetDefinition()->GetPDGEncoding();
55  const G4Step *step = track->GetStep();
56  newElement->primary.trackID = step->GetTrack()->GetTrackID();
57  for (int i = 0; i < 3; i++) {
58  newElement->primary.pos[i] = track->GetPosition()[i] / cm;
59  newElement->primary.mom[i] = track->GetMomentum()[i] / GeV;
60  }
61  newElement->primary.time = track->GetGlobalTime() / ns;
62  newElement->processName = step->GetPostStepPoint()->GetProcessDefinedStep()->GetProcessName().data();
63  newElement->volumeName = step->GetPostStepPoint()->GetPhysicalVolume()->GetName();
64  if (_outputProducts) {
65  if (newElement->products) {
66  newElement->products->Clear("C");
67  } else {
68  newElement->products = new TClonesArray("GGSTParticle");
69  }
70  auto *secondaries = step->GetSecondaryInCurrentStep();
71  unsigned int nSecondaries = secondaries->size();
72  for (unsigned int is = 0; is < nSecondaries; is++) {
73  GGSTParticle *sec = new ((*(newElement->products))[is]) GGSTParticle;
74  auto *currentSecondaryTrack = (*secondaries)[is];
75  sec->PDGCode = currentSecondaryTrack->GetDefinition()->GetPDGEncoding();
76  sec->trackID = currentSecondaryTrack->GetTrackID();
77  for (int i = 0; i < 3; i++) {
78  sec->pos[i] = currentSecondaryTrack->GetPosition()[i] / cm;
79  sec->mom[i] = currentSecondaryTrack->GetMomentum()[i] / GeV;
80  }
81  sec->time = currentSecondaryTrack->GetGlobalTime() / ns;
82  }
83 
84  } else {
85  delete newElement->products;
86  newElement->products = nullptr;
87  } // if(_outputProducts)
88 
89  // Delete the currently followed particle from list
90  _tracks.erase(trackIter);
91 
92  } // if (trackIter != _tracks.end())
93 
94  } // if (_tracks.size() > 0)
95 }
96 
97 void GGSPrimaryDisAction::BeginOfEventAction(const G4Event *) { _disInfo.Clear("C"); }
98 
100 
101  if (_outTreeName != "") {
102  // Call fill only if not using the default tree
103  _outRootFile->cd();
104  _outTree->Fill();
105  }
106 }
107 
109 
110  _outRootFile = GGSRootFileService::GetInstance().GetFileForThisRun(_outBase, run);
111  if (_outTreeName == "") {
112  _outTree = GGSRootFileService::GetInstance().GetDefaultTree(_outRootFile);
113  _outTree->SetTitle(TString(_outTree->GetTitle()) + "DisInfo ");
114  } else
115  _outTree = new TTree(_outTreeName.data(), "Disappearance of primary particles");
116 
117  // Create inelastic interactions branch
118  _outTree->Branch("primaryDisInfo", "TClonesArray", &_disInfo);
119 }
120 
122 
123  if (_outTreeName != "") {
124  // Write the tree if we are not using the default one
125  _outRootFile->cd();
126  _outTree->Write();
127  }
129  _outRootFile = nullptr;
130  _outTree = nullptr;
131 }
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.
TString volumeName
The name of the physical volume where the primary disappeared.
void EndOfEventAction(const G4Event *event)
Fills the output tree.
GGSTParticle primary
Descriptor for the primary particle.
void Clear(Option_t *="")
Resets all the members.
void CloseFileForThisRun(const path &baseName)
Closes the ROOT output file.
A simple class to carry informations about the disappearance of the primary particle.
Int_t PDGCode
PDG code of particle (see http://www3.nd.edu/~avillano/geant4/geant4_pid.html).
Definition: GGSTParticle.h:21
void PreUserTrackingAction(const G4Track *track)
Initialization of primary track.
Float_t time
Time of generation [ns].
Definition: GGSTParticle.h:25
void EndOfRunAction(const G4Run *run)
Closes the output file for the current run.
Int_t trackID
Track ID.
Definition: GGSTParticle.h:22
void BeginOfEventAction(const G4Event *event)
Clears the interaction arrays.
Float_t pos[3]
Point of generation [cm].
Definition: GGSTParticle.h:23
void BeginOfRunAction(const G4Run *run)
Opens the output file for the current run and prepares the output tree.
TString processName
Name of the process that made the primary disappear.
Class to store G4 particles.
Definition: GGSTParticle.h:19
void PostUserTrackingAction(const G4Track *track)
Check the track end .
TTree * GetDefaultTree(TFile *file)
Gets the default tree for this file.
static GGSRootFileService & GetInstance()
Get reference to GGSRootFileService unique instance.
GGSPrimaryDisAction()
Constructor.
TClonesArray * products
Particles produced by the disappearance of the primary.
Action which finds the point of disappearance points for each primary particle.
#define RegisterUA(uaClassName)
Macro for registration of user actions classes.