GGS(GenericGEANT4Simulation)Software  2.7.0
 All Data Structures Namespaces Files Functions Variables Typedefs Macros
GGSHadrIntAction.cpp
Go to the documentation of this file.
1 /*
2  * GGSHadrIntAction.cpp
3  *
4  * Created on: 31 May 2011
5  * Author: Nicola mori
6  */
7 
11 #include "montecarlo/dataobjs/GGSTHadrIntInfo.h"
12 #include "montecarlo/dataobjs/GGSTParticle.h"
15 
16 #include "G4HadronicProcess.hh"
17 #include "G4SystemOfUnits.hh"
18 
19 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
20 
22 
23 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
24 
26  : _energyFraction(0.9), _outputProducts(false), _outBase(""), _outTreeName(""), _outRootFile(NULL), _outTree(NULL) {
27 
28  _messenger = new GGSHadrIntMessenger(this);
29  _quasiElasticInteractions = new TClonesArray("GGSTHadrIntInfo", 10);
30  _inelasticInteractions = new TClonesArray("GGSTHadrIntInfo", 1);
31 }
32 
33 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
34 
36 
37  delete _messenger;
38  delete _quasiElasticInteractions;
39  delete _inelasticInteractions;
40 }
41 
42 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
43 
44 void GGSHadrIntAction::PreUserTrackingAction(const G4Track *track) {
45 
46  // Check whether it is a primary particle
47  G4PrimaryParticle *primary = track->GetDynamicParticle()->GetPrimaryParticle();
48  if (primary != NULL)
49  // Check whether it is a baryon
50  if (primary->GetParticleDefinition()->GetBaryonNumber() > 0)
51  _tracks[track] = track->GetTrackID();
52 }
53 
54 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
55 
56 void GGSHadrIntAction::PostUserTrackingAction(const G4Track *track) {
57 
58  if (_tracks.size() > 0) {
59  // See if the track is in the tracking list
60  std::map<const G4Track *, int>::iterator trackIter = _tracks.find(track);
61  if (trackIter != _tracks.end()) {
62  // Check process type
63  const G4Step *step = track->GetStep();
64  int pType = step->GetPostStepPoint()->GetProcessDefinedStep()->GetProcessType();
65  if (pType == fHadronic) {
66  int primaryPDG = track->GetDefinition()->GetPDGEncoding();
67 
68  // Check if interaction is quasi-elastic
69  float maxSecondaryEnergy = step->GetPreStepPoint()->GetKineticEnergy() * _energyFraction;
70  auto *secondaries = step->GetSecondaryInCurrentStep();
71  unsigned int nSecondaries = secondaries->size();
72  bool inelastic = true;
73  GGSTHadrIntInfo *newElement = NULL;
74  for (unsigned int is = 0; is < nSecondaries; is++) {
75  auto *currentSecondaryTrack = (*secondaries)[is];
76  if (currentSecondaryTrack->GetDefinition()->GetPDGEncoding() == primaryPDG &&
77  currentSecondaryTrack->GetKineticEnergy() > maxSecondaryEnergy) {
78 
79  // Quasi-elastic interaction
80  newElement =
81  (GGSTHadrIntInfo *)(_quasiElasticInteractions->ConstructedAt(_quasiElasticInteractions->GetEntries()));
82  newElement->Clear();
83 
84  // Add an entry in the track list for the new "primary"
85  _tracks[currentSecondaryTrack] = trackIter->second;
86  // Set flag to avoid treating it as inelastic
87  inelastic = false;
88  // End loop
89  break;
90  }
91  }
92 
93  if (inelastic) {
94  // Inelastic interaction
95  newElement = (GGSTHadrIntInfo *)(_inelasticInteractions->ConstructedAt(_inelasticInteractions->GetEntries()));
96  newElement->Clear();
97  }
98 
99  // Fill the new element
100  newElement->primary.PDGCode = primaryPDG;
101  newElement->primary.trackID = step->GetTrack()->GetTrackID();
102  for (int i = 0; i < 3; i++) {
103  newElement->primary.pos[i] = track->GetPosition()[i] / cm;
104  newElement->primary.mom[i] = track->GetMomentum()[i] / GeV;
105  }
106  newElement->primary.time = track->GetGlobalTime() / ns;
107  const G4Isotope *target =
108  ((G4HadronicProcess *)(step->GetPostStepPoint()->GetProcessDefinedStep()))->GetTargetIsotope();
109  newElement->targetPDGCode = 1000000000 + target->GetZ() * 10000 + target->GetN() * 10;
110  newElement->originalTrackID = trackIter->second;
111  newElement->processName = step->GetPostStepPoint()->GetProcessDefinedStep()->GetProcessName().data();
112  newElement->volumeName = step->GetPostStepPoint()->GetPhysicalVolume()->GetName();
113  if (_outputProducts) {
114  if (newElement->products) {
115  newElement->products->Clear("C");
116  } else {
117  newElement->products = new TClonesArray("GGSTParticle");
118  }
119  for (unsigned int is = 0; is < nSecondaries; is++) {
120  GGSTParticle *sec = new ((*(newElement->products))[is]) GGSTParticle;
121  auto *currentSecondaryTrack = (*secondaries)[is];
122  sec->PDGCode = currentSecondaryTrack->GetDefinition()->GetPDGEncoding();
123  sec->trackID = currentSecondaryTrack->GetTrackID();
124  for (int i = 0; i < 3; i++) {
125  sec->pos[i] = currentSecondaryTrack->GetPosition()[i] / cm;
126  sec->mom[i] = currentSecondaryTrack->GetMomentum()[i] / GeV;
127  }
128  sec->time = currentSecondaryTrack->GetGlobalTime() / ns;
129  }
130 
131  } else {
132  delete newElement->products;
133  newElement->products = NULL;
134  } // if(_outputProducts)
135  } // if (pType == fHadronic)
136 
137  // Delete the currently followed particle from list
138  _tracks.erase(trackIter);
139  } // if (trackIter != _tracks.end())
140  }
141 }
142 
143 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
144 
146 
147  _inelasticInteractions->Clear("C");
148  _quasiElasticInteractions->Clear("C");
149 }
150 
151 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
152 
153 void GGSHadrIntAction::EndOfEventAction(const G4Event *) {
154 
155  if (_outTreeName != "") {
156  // Call fill only if not using the default tree
157  _outRootFile->cd();
158  _outTree->Fill();
159  }
160 }
161 
162 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
163 
164 void GGSHadrIntAction::BeginOfRunAction(const G4Run *run) {
165 
166  _outRootFile = GGSRootFileService::GetInstance().GetFileForThisRun(_outBase, run);
167  if (_outTreeName == "") {
168  _outTree = GGSRootFileService::GetInstance().GetDefaultTree(_outRootFile);
169  _outTree->SetTitle(TString(_outTree->GetTitle()) + "HadrInt ");
170  } else
171  _outTree = new TTree(_outTreeName.data(), "Hadronic interactions of primary particles");
172 
173  // Create inelastic interactions branch
174  _outTree->Branch("inelastic", "TClonesArray", &_inelasticInteractions);
175 
176  // Create quasi-elastic interactions branch
177  _outTree->Branch("quasiElastic", "TClonesArray", &_quasiElasticInteractions);
178 }
179 
180 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
181 
182 void GGSHadrIntAction::EndOfRunAction(const G4Run *) {
183 
184  if (_outTreeName != "") {
185  // Write the tree if we are not using the default one
186  _outRootFile->cd();
187  _outTree->Write();
188  }
190  _outRootFile = NULL;
191  _outTree = NULL;
192 }
Float_t mom[3]
Momentum at generation [GeV].
Definition: GGSTParticle.h:24
Int_t originalTrackID
Track ID of the original primary particle.
TFile * GetFileForThisRun(const path &baseName, const G4Run *run)
Opens a file for a given run and returns a pointer to it.
GGSTParticle primary
The particle originating the interaction.
void BeginOfEventAction(const G4Event *event)
Clears the interaction arrays.
GGSHadrIntAction()
Constructor.
void BeginOfRunAction(const G4Run *run)
Opens the output file for the current run and prepares the output tree.
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 PostUserTrackingAction(const G4Track *track)
Check the track end for a hadronic interaction.
Float_t time
Time of generation [ns].
Definition: GGSTParticle.h:25
Int_t trackID
Track ID.
Definition: GGSTParticle.h:22
TClonesArray * products
Particles produced in the interaction.
TString volumeName
The name of the physical volume where the interaction took place.
Action which finds the hadronic interaction points for each primary particle.
Float_t pos[3]
Point of generation [cm].
Definition: GGSTParticle.h:23
void EndOfRunAction(const G4Run *run)
Closes the output file for the current run.
A simple class to carry informations about hadronic interactions.
Class to store G4 particles.
Definition: GGSTParticle.h:19
A control messenger for GGSHadrIntAction.
TTree * GetDefaultTree(TFile *file)
Gets the default tree for this file.
void PreUserTrackingAction(const G4Track *track)
Initialization of primary track.
static GGSRootFileService & GetInstance()
Get reference to GGSRootFileService unique instance.
Int_t targetPDGCode
PDG code of target nucleus.
void EndOfEventAction(const G4Event *event)
Fills the output tree.
#define RegisterUA(uaClassName)
Macro for registration of user actions classes.
void Clear(Option_t *="")
Resets all the members.
TString processName
Hadronic process name.
virtual ~GGSHadrIntAction()
Destructor.