GGS(GenericGEANT4Simulation)Software  2.6.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 = new G4GenericMessenger(this, "/GGS/generatorActions/multiParticle/");
20  _messenger->DeclareMethod("file", &GGSMultiParticleGeneratorAction::SetEventsFile,
21  "Set the name of the file containing the definitions of the events").SetStates(G4State_Idle);
22 
23 }
24 
26  if (_inputFile.is_open()) {
27  _inputFile.close();
28  }
29  delete _messenger;
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 >> VHEP3
73  >> 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  Particle* hepParticle = new Particle;
84  hepParticle->theParticle = particle;
85  hepParticle->ISTHEP = ISTHEP;
86  hepParticle->JDAHEP1 = JDAHEP1;
87  hepParticle->JDAHEP2 = JDAHEP2;
88  hepParticle->theVertex = vertex;
89 
90  // Store
91  _particles.push_back(hepParticle);
92  }
93 
94 // check if there is at least one particle
95  if (_particles.size() == 0)
96  return;
97 
98 // make connection between daughter particles decayed from
99 // the same mother
100  for (size_t i = 0; i < _particles.size(); i++) {
101  if (_particles[i]->JDAHEP1 > 0) // it has daughters
102  {
103  G4int jda1 = _particles[i]->JDAHEP1 - 1; // FORTRAN index starts from 1
104  G4int jda2 = _particles[i]->JDAHEP2 - 1; // but C++ starts from 0.
105  G4PrimaryParticle* mother = _particles[i]->theParticle;
106  for (G4int j = jda1; j <= jda2; j++) {
107  G4PrimaryParticle* daughter = _particles[j]->theParticle;
108  if (_particles[j]->ISTHEP > 0) {
109  mother->SetDaughter(daughter);
110  _particles[j]->ISTHEP *= -1;
111  ;
112  }
113  }
114  }
115  }
116 
117 // put initial particles to the vertex and the vertex to G4Event object
118  for (size_t ii = 0; ii < _particles.size(); ii++) {
119  if (_particles[ii]->ISTHEP > 0) // ISTHEP of daughters had been
120  // set to negative
121  {
122  _particles[ii]->theVertex->SetPrimary(_particles[ii]->theParticle);
123  anEvent->AddPrimaryVertex(_particles[ii]->theVertex);
124  }
125  }
126 
127 // clear G4HEPEvtParticles
128 //_particles.clearAndDestroy();
129  for (size_t iii = 0; iii < _particles.size(); iii++) {
130  delete _particles[iii];
131  }
132  _particles.clear();
133 
134 //evt->AddPrimaryVertex(vertex);
135 
136 }
A multi-particle generator action.
#define RegisterGA(gaClassName, gaName)
Macro for registration of user generator actions.
void GeneratePrimaries(G4Event *anEvent)
Override of GeneratePrimaries method.