GGS(GenericGEANT4Simulation)Software  2.7.0
 All Data Structures Namespaces Files Functions Variables Typedefs Macros
GGSMultiParticleGeneratorAction.cpp
Go to the documentation of this file.
1 /*
2  * GGSMultiParticleGeneratorAction.cpp
3  *
4  * Created on: 09/may/2011
5  * Author: Nicola mori
6  */
7 
12 
13 #include "G4Event.hh"
14 #include "G4SystemOfUnits.hh"
15 
17 
19  : _messenger(this, "/GGS/generatorActions/multiParticle/") {
20 
21  _messenger
22  .DeclareMethod("file", &GGSMultiParticleGeneratorAction::SetEventsFile,
23  "Set the name of the file containing the definitions of the events")
24  .SetStates(G4State_Idle);
25 }
26 
28  if (_inputFile.is_open()) {
29  _inputFile.close();
30  }
31 }
32 
33 void GGSMultiParticleGeneratorAction::SetEventsFile(const G4String &evFile) {
34  if (_inputFile.is_open()) {
35  _inputFile.close();
36  }
37  _inputFile.open(evFile.data());
38  if (!_inputFile) {
39  G4Exception("GGSMultiParticleGeneratorAction::GGSMultiParticleGeneratorAction", "mp-nofile", FatalException,
40  "Cannot open file");
41  }
42 }
43 
44 // This member function is largely copied from G4HEPEvtInterface::GeneratePrimaryVertex
45 // Instead of relying on a G4VParticleGenerator, generate the primaries directly here
47 
48  G4int NHEP; // number of entries
49  _inputFile >> NHEP;
50  if (_inputFile.eof()) {
51  G4Exception("GGSMultiParticleGeneratorAction::GeneratePrimaryVertex", "mp-eof", JustWarning,
52  "End-Of-File: multi-particle input file");
53  return;
54  }
55 
56  G4int ISTHEP; // status code
57  G4int IDHEP; // PDG code
58  G4int JDAHEP1; // first daughter
59  G4int JDAHEP2; // last daughter
60  G4double PHEP1; // px in GeV
61  G4double PHEP2; // py in GeV
62  G4double PHEP3; // pz in GeV
63  G4double PHEP5; // mass in GeV
64  G4double VHEP1; // x position in cm
65  G4double VHEP2; // y position in cm
66  G4double VHEP3; // z position in cm
67  // WARNING: in standard HEPEVT, VHEP4 is in mm/c. Here we use ns since it's more comfortable within Geant4
68  G4double VHEP4; // production time in ns
69 
70  for (G4int IHEP = 0; IHEP < NHEP; IHEP++) {
71 
72  _inputFile >> ISTHEP >> IDHEP >> JDAHEP1 >> JDAHEP2 >> PHEP1 >> PHEP2 >> PHEP3 >> PHEP5 >> VHEP1 >> VHEP2 >>
73  VHEP3 >> VHEP4;
74 
75  // create G4PrimaryParticle object
76  G4PrimaryParticle *particle = new G4PrimaryParticle(IDHEP, PHEP1 * GeV, PHEP2 * GeV, PHEP3 * GeV);
77  particle->SetMass(PHEP5 * GeV);
78 
79  // create G4PrimaryVertex object
80  G4PrimaryVertex *vertex = new G4PrimaryVertex(G4ThreeVector(VHEP1 * cm, VHEP2 * cm, VHEP3 * cm), VHEP4 * ns);
81 
82  // Create Particle object
83  _particles.emplace_back(Particle());
84  Particle &hepParticle = _particles.back();
85  hepParticle.theParticle = particle;
86  hepParticle.ISTHEP = ISTHEP;
87  hepParticle.JDAHEP1 = JDAHEP1;
88  hepParticle.JDAHEP2 = JDAHEP2;
89  hepParticle.theVertex = vertex;
90  }
91 
92  // check if there is at least one particle
93  if (_particles.size() == 0)
94  return;
95 
96  // make connection between daughter particles decayed from
97  // the same mother
98  for (size_t iPart = 0; iPart < _particles.size(); ++iPart) {
99  if (_particles[iPart].JDAHEP1 > 0) // it has daughters
100  {
101  G4int jda1 = _particles[iPart].JDAHEP1 - 1; // FORTRAN index starts from 1
102  G4int jda2 = _particles[iPart].JDAHEP2 - 1; // but C++ starts from 0.
103  G4PrimaryParticle *mother = _particles[iPart].theParticle;
104  for (G4int jPart = jda1; jPart <= jda2; ++jPart) {
105  G4PrimaryParticle *daughter = _particles[jPart].theParticle;
106  if (_particles[jPart].ISTHEP > 0) {
107  mother->SetDaughter(daughter);
108  _particles[jPart].ISTHEP *= -1;
109  }
110  }
111  }
112  }
113 
114  // put initial particles to the vertex and the vertex to G4Event object
115  for (size_t iPart = 0; iPart < _particles.size(); ++iPart) {
116  if (_particles[iPart].ISTHEP > 0) { // ISTHEP of daughters had been set to negative
117  _particles[iPart].theVertex->SetPrimary(_particles[iPart].theParticle);
118  anEvent->AddPrimaryVertex(_particles[iPart].theVertex);
119  }
120  }
121 
122  // clear G4HEPEvtParticles
123  _particles.clear();
124 }
125 
127  GGSParameters params;
128  params.SetParam("generator", std::string("multiParticle"));
129  return params;
130 }
A multi-particle generator action.
void SetParam(const std::string &name, T value)
Sets a parameter.
Class for storing parameters.
Definition: GGSParameters.h:29
GGSParameters GetParameters() const
Gets the generation parameters.
#define RegisterGA(gaClassName, gaName)
Macro for registration of user generator actions.
void GeneratePrimaries(G4Event *anEvent)
Override of GeneratePrimaries method.