GGS(GenericGEANT4Simulation)Software  2.7.1
 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  G4VPhysicalVolume *postStepVol = step->GetPostStepPoint()->GetPhysicalVolume();
64  if (postStepVol) {
65  newElement->volumeName = postStepVol->GetName();
66  } else {
67  newElement->volumeName = "outOfWorld";
68  }
69  if (_outputProducts) {
70  if (newElement->products) {
71  newElement->products->Clear("C");
72  } else {
73  newElement->products = new TClonesArray("GGSTParticle");
74  }
75  auto *secondaries = step->GetSecondaryInCurrentStep();
76  unsigned int nSecondaries = secondaries->size();
77  for (unsigned int is = 0; is < nSecondaries; is++) {
78  GGSTParticle *sec = new ((*(newElement->products))[is]) GGSTParticle;
79  auto *currentSecondaryTrack = (*secondaries)[is];
80  sec->PDGCode = currentSecondaryTrack->GetDefinition()->GetPDGEncoding();
81  sec->trackID = currentSecondaryTrack->GetTrackID();
82  for (int i = 0; i < 3; i++) {
83  sec->pos[i] = currentSecondaryTrack->GetPosition()[i] / cm;
84  sec->mom[i] = currentSecondaryTrack->GetMomentum()[i] / GeV;
85  }
86  sec->time = currentSecondaryTrack->GetGlobalTime() / ns;
87  }
88 
89  } else {
90  delete newElement->products;
91  newElement->products = nullptr;
92  } // if(_outputProducts)
93 
94  // Delete the currently followed particle from list
95  _tracks.erase(trackIter);
96 
97  } // if (trackIter != _tracks.end())
98 
99  } // if (_tracks.size() > 0)
100 }
101 
102 void GGSPrimaryDisAction::BeginOfEventAction(const G4Event *) { _disInfo.Clear("C"); }
103 
105 
106  if (_outTreeName != "") {
107  // Call fill only if not using the default tree
108  _outRootFile->cd();
109  _outTree->Fill();
110  }
111 }
112 
114 
115  _outRootFile = GGSRootFileService::GetInstance().GetFileForThisRun(_outBase, run);
116  if (_outTreeName == "") {
117  _outTree = GGSRootFileService::GetInstance().GetDefaultTree(_outRootFile);
118  _outTree->SetTitle(TString(_outTree->GetTitle()) + "DisInfo ");
119  } else
120  _outTree = new TTree(_outTreeName.data(), "Disappearance of primary particles");
121 
122  // Create inelastic interactions branch
123  _outTree->Branch("primaryDisInfo", "TClonesArray", &_disInfo);
124 }
125 
127 
128  if (_outTreeName != "") {
129  // Write the tree if we are not using the default one
130  _outRootFile->cd();
131  _outTree->Write();
132  }
134  _outRootFile = nullptr;
135  _outTree = nullptr;
136 }
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.