DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
Geant4ParticleGenerator.cpp
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation
3 //--------------------------------------------------------------------------
4 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
5 // All rights reserved.
6 //
7 // For the licensing terms see $DD4hepINSTALL/LICENSE.
8 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
9 //
10 // Author : M.Frank
11 //
12 //==========================================================================
13 
14 // Framework include files
15 #include <DD4hep/Printout.h>
16 #include <DD4hep/InstanceCount.h>
17 #include <DDG4/Geant4Context.h>
18 #include <DDG4/Geant4Primary.h>
20 #include <DDG4/Geant4Random.h>
21 #include <CLHEP/Units/SystemOfUnits.h>
22 
23 // Geant4 include files
24 #include <G4ParticleTable.hh>
25 #include <G4ParticleDefinition.hh>
26 
27 // C/C++ include files
28 #include <stdexcept>
29 #include <cmath>
30 
31 using namespace dd4hep::sim;
32 
35  : Geant4GeneratorAction(ctxt, nam), m_direction(0,1,0), m_position(0,0,0), m_particle(0)
36 {
38  m_needsControl = true;
39  declareProperty("Particle", m_particleName = "e-");
40  declareProperty("Energy", m_energy = -1);
41  declareProperty("energy", m_energy = -1);
42  declareProperty("MomentumMin", m_momentumMin = 0.0);
43  declareProperty("MomentumMax", m_momentumMax = 50 * CLHEP::MeV);
44  declareProperty("Multiplicity", m_multiplicity = 1);
45  declareProperty("Mask", m_mask = 0);
46  declareProperty("Position", m_position = ROOT::Math::XYZVector(0.,0.,0.));
47  declareProperty("Direction", m_direction = ROOT::Math::XYZVector(1.,1.,1.));
48 }
49 
53 }
54 
56 void Geant4ParticleGenerator::getParticleDirection(int , ROOT::Math::XYZVector& , double& momentum) const {
58 }
59 
62  if (m_energy != -1) {
63  momentum = m_energy;
64  return;
65  }
66  Geant4Event& evt = context()->event();
67  Geant4Random& rnd = evt.random();
69  // We no longer set m_momentumMax to -1 so, not entirely sure a) this will still happen, b) actually work
70  momentum = m_momentumMin+(momentum-m_momentumMin)*rnd.rndm();
71  else
72  momentum = m_momentumMin+(m_momentumMax-m_momentumMin)*rnd.rndm();
73 }
74 
77 }
78 
80 void Geant4ParticleGenerator::getVertexPosition(ROOT::Math::XYZVector& ) const {
81 }
82 
86  if ( !prim ) {
87  warning("printInteraction: Bad primary event [NULL-Pointer].");
88  return;
89  }
90  Geant4PrimaryInteraction* inter = prim->get(mask);
91  if ( !inter ) {
92  warning("printInteraction: Bad interaction identifier 0x%08X [Unknown Mask].",mask);
93  return;
94  }
95  printInteraction(inter);
96 }
97 
100  int count = 0;
101  if ( !inter ) {
102  warning("printInteraction: Invalid interaction pointer [NULL-Pointer].");
103  return;
104  }
105  for(const auto& iv : inter->vertices ) {
106  for( Geant4Vertex* v : iv.second ){
107  print("+-> Interaction [%d] [%.3f , %.3f] GeV %s pos:(%.3f %.3f %.3f)[mm]",
108  count, m_momentumMin/CLHEP::GeV, m_momentumMax/CLHEP::GeV, m_particleName.c_str(),
109  v->x/CLHEP::mm, v->y/CLHEP::mm, v->z/CLHEP::mm);
110  ++count;
111  for ( int i : v->out ) {
112  Geant4ParticleHandle p = inter->particles[i];
113  p.dumpWithVertex(outputLevel(),name()," +->");
114  }
115  }
116  }
117 }
118 
121  typedef Geant4Particle Particle;
122 
123  if (0 == m_particle || m_particle->GetParticleName() != m_particleName.c_str()) {
124  G4ParticleTable* particleTable = G4ParticleTable::GetParticleTable();
125  m_particle = particleTable->FindParticle(m_particleName);
126  if (0 == m_particle) {
127  except("Geant4ParticleGenerator: Bad particle type: %s!", m_particleName.c_str());
128  }
129  }
130  Geant4Event& evt = context()->event();
133  prim->add(m_mask, inter);
134 
135  Geant4Vertex* vtx = new Geant4Vertex();
136  int multiplicity = m_multiplicity;
137  ROOT::Math::XYZVector unit_direction, position = m_position;
138  getVertexPosition(position);
139  getParticleMultiplicity(multiplicity);
140  vtx->mask = m_mask;
141  vtx->x = position.X();
142  vtx->y = position.Y();
143  vtx->z = position.Z();
144  inter->vertices[m_mask].emplace_back( vtx );
145  for(int i=0; i<m_multiplicity; ++i) {
146  double momentum = 0.0;
147  ROOT::Math::XYZVector direction = m_direction;
148  Particle* p = new Particle();
149  getParticleDirection(i, direction, momentum);
150  unit_direction = direction.unit();
151  p->id = inter->nextPID();
152  p->status |= G4PARTICLE_GEN_STABLE;
153  p->mask = m_mask;
154  p->pdgID = m_particle->GetPDGEncoding();
155 
156  p->psx = unit_direction.X()*momentum;
157  p->psy = unit_direction.Y()*momentum;
158  p->psz = unit_direction.Z()*momentum;
159  p->mass = m_particle->GetPDGMass();
160  p->charge = 3 * m_particle->GetPDGCharge();
161  p->spin[0] = 0;
162  p->spin[1] = 0;
163  p->spin[2] = 0;
164  p->colorFlow[0] = 0;
165  p->colorFlow[1] = 0;
166  p->vsx = vtx->x;
167  p->vsy = vtx->y;
168  p->vsz = vtx->z;
169  //fg: do not set the endpoint to the start point of the particle
170  // p->vex = vtx->x;
171  // p->vey = vtx->y;
172  // p->vez = vtx->z;
173  inter->particles.emplace(p->id,p);
174  vtx->out.insert(p->id);
175  printout(INFO,name(),"Particle [%d] %-12s Mom:%.3f GeV vertex:(%6.3f %6.3f %6.3f)[mm] direction:(%6.3f %6.3f %6.3f)",
176  p->id, m_particleName.c_str(), momentum/CLHEP::GeV,
177  vtx->x/CLHEP::mm, vtx->y/CLHEP::mm, vtx->z/CLHEP::mm,
178  direction.X(), direction.Y(), direction.Z());
179  }
180 }
dd4hep::sim::Geant4ParticleGenerator::m_multiplicity
int m_multiplicity
Property: Desired multiplicity of the particles to be shot.
Definition: Geant4ParticleGenerator.h:56
dd4hep::sim::Geant4ParticleGenerator::~Geant4ParticleGenerator
virtual ~Geant4ParticleGenerator()
Default destructor.
Definition: Geant4ParticleGenerator.cpp:51
dd4hep::sim::Geant4PrimaryEvent
Class modelling a complete primary event with multiple interactions.
Definition: Geant4Primary.h:143
dd4hep::sim::Geant4Action::m_needsControl
bool m_needsControl
Default property: Flag to create control instance.
Definition: Geant4Action.h:123
dd4hep::sim::Geant4Random::rndm
double rndm(int i=0)
Create flat distributed random numbers in the interval ]0,1].
Definition: Geant4Random.cpp:268
dd4hep::sim::G4PARTICLE_GEN_STABLE
@ G4PARTICLE_GEN_STABLE
Definition: Geant4Particle.h:71
dd4hep::sim::Geant4Vertex::y
double y
Definition: Geant4Vertex.h:53
v
View * v
Definition: MultiView.cpp:28
dd4hep::sim::Geant4ParticleGenerator::m_mask
int m_mask
Property: User mask passed to all particles in the generated interaction.
Definition: Geant4ParticleGenerator.h:58
dd4hep::sim::Geant4ParticleHandle::dumpWithVertex
void dumpWithVertex(int level, const std::string &src, const char *tag) const
Output type 3:+++ "tag" ID: 0 e- status:00000014 type: 11 Vertex:(+0.00e+00,+0.00e+00,...
Definition: Geant4Particle.cpp:308
Geant4ParticleGenerator.h
dd4hep::sim::Geant4ParticleGenerator::Geant4ParticleGenerator
Geant4ParticleGenerator(Geant4Context *context, const std::string &name)
Standard constructor.
Definition: Geant4ParticleGenerator.cpp:34
dd4hep::sim::Geant4PrimaryInteraction
Class modelling a single interaction with multiple primary vertices and particles.
Definition: Geant4Primary.h:95
dd4hep::sim::Geant4Vertex::mask
int mask
Vertex mask to associate particles from collision.
Definition: Geant4Vertex.h:51
dd4hep::sim::Geant4PrimaryInteraction::vertices
VertexMap vertices
The map of primary vertices for the particles.
Definition: Geant4Primary.h:110
dd4hep::sim::Geant4ParticleGenerator::m_momentumMin
double m_momentumMin
Property: Minimal momentum value.
Definition: Geant4ParticleGenerator.h:52
dd4hep::InstanceCount::increment
static void increment(T *)
Increment count according to type information.
Definition: InstanceCount.h:98
dd4hep::sim::Geant4ParticleGenerator::getParticleMultiplicity
virtual void getParticleMultiplicity(int &multiplicity) const
Particle modification. Caller presets defaults to: (multiplicity=m_multiplicity)
Definition: Geant4ParticleGenerator.cpp:76
dd4hep::sim::Geant4PrimaryInteraction::particles
ParticleMap particles
The map of particles participating in this primary interaction.
Definition: Geant4Primary.h:112
dd4hep::sim::Geant4Context::event
Geant4Event & event() const
Access the geant4 event – valid only between BeginEvent() and EndEvent()!
Definition: Geant4Context.cpp:84
dd4hep::sim::Geant4Event::random
Geant4Random & random() const
Access the random number generator.
Definition: Geant4Context.h:137
dd4hep::sim::Geant4Action::warning
void warning(const char *fmt,...) const
Support of warning messages.
Definition: Geant4Action.cpp:223
dd4hep::sim::Geant4ParticleGenerator::m_energy
double m_energy
Property: Fixed momentum value, overwrites momentumMin and momentumMax if set.
Definition: Geant4ParticleGenerator.h:50
dd4hep::sim::Geant4Event::extension
T * extension(bool alert=true)
Access to type safe extension object. Exception is thrown if the object is invalid.
Definition: Geant4Context.h:151
dd4hep::sim::Geant4Action::except
void except(const char *fmt,...) const
Support of exceptions: Print fatal message and throw runtime_error.
Definition: Geant4Action.cpp:256
dd4hep::sim::Geant4ParticleGenerator::m_particle
G4ParticleDefinition * m_particle
Pointer to geant4 particle definition.
Definition: Geant4ParticleGenerator.h:48
Geant4Random.h
dd4hep::sim::Geant4ParticleGenerator::m_direction
ROOT::Math::XYZVector m_direction
Property: Shooting direction of the gun.
Definition: Geant4ParticleGenerator.h:42
dd4hep::sim::Geant4ParticleGenerator::getVertexPosition
virtual void getVertexPosition(ROOT::Math::XYZVector &position) const
Particle's vertex modification. Caller presets defaults to: (multiplicity=m_multiplicity)
Definition: Geant4ParticleGenerator.cpp:80
dd4hep::sim::Geant4ParticleGenerator::getParticleMomentumUniform
void getParticleMomentumUniform(double &momentum) const
Uniform particle momentum.
Definition: Geant4ParticleGenerator.cpp:61
dd4hep::sim::Geant4Vertex::x
double x
Vertex data.
Definition: Geant4Vertex.h:53
dd4hep::sim::Geant4ParticleGenerator::operator()
virtual void operator()(G4Event *event) override
Callback to generate primary particles.
Definition: Geant4ParticleGenerator.cpp:120
dd4hep::sim::Geant4ParticleGenerator::m_position
ROOT::Math::XYZVector m_position
Property: Position of the gun in space.
Definition: Geant4ParticleGenerator.h:44
dd4hep::sim::Geant4ParticleGenerator::m_momentumMax
double m_momentumMax
Property: Maximal momentum value.
Definition: Geant4ParticleGenerator.h:54
dd4hep::sim::Geant4Action::declareProperty
Geant4Action & declareProperty(const std::string &nam, T &val)
Declare property.
Definition: Geant4Action.h:366
dd4hep::InstanceCount::decrement
static void decrement(T *)
Decrement count according to type information.
Definition: InstanceCount.h:102
dd4hep::sim::Geant4GeneratorAction
Concrete implementation of the Geant4 generator action base class.
Definition: Geant4GeneratorAction.h:47
dd4hep::sim::Geant4Random
Mini interface to THE random generator of the application.
Definition: Geant4Random.h:59
dd4hep::sim::Geant4Action::name
const std::string & name() const
Access name of the action.
Definition: Geant4Action.h:280
dd4hep::sim::Geant4Action::outputLevel
PrintLevel outputLevel() const
Access the output level.
Definition: Geant4Action.h:296
dd4hep::sim::Geant4Vertex::out
Particles out
The list of outgoing particles.
Definition: Geant4Vertex.h:55
dd4hep::sim::Geant4Action::print
void print(const char *fmt,...) const
Support for messages with variable output level using output level.
Definition: Geant4Action.cpp:144
dd4hep::sim
Namespace for the Geant4 based simulation part of the AIDA detector description toolkit.
Definition: Geant4Output2EDM4hep.cpp:49
dd4hep::sim::Geant4ParticleGenerator::m_particleName
std::string m_particleName
Property: Particle name.
Definition: Geant4ParticleGenerator.h:46
Geant4Primary.h
dd4hep::sim::Geant4ParticleHandle
Data structure to access derived MC particle information.
Definition: Geant4Particle.h:181
dd4hep::sim::Geant4Vertex::z
double z
Definition: Geant4Vertex.h:53
dd4hep::sim::Geant4Particle
Data structure to store the MC particle information.
Definition: Geant4Particle.h:103
dd4hep::sim::Geant4Vertex
Data structure to store the MC vertex information.
Definition: Geant4Vertex.h:45
InstanceCount.h
dd4hep::sim::Geant4ParticleGenerator::getParticleDirection
virtual void getParticleDirection(int num, ROOT::Math::XYZVector &direction, double &momentum) const
Particle modification. Caller presets defaults to: ( direction = m_direction, momentum = m_energy)
Definition: Geant4ParticleGenerator.cpp:56
dd4hep::sim::Geant4ParticleGenerator::printInteraction
virtual void printInteraction(int mask) const
Print single particle interaction identified by its mask.
Definition: Geant4ParticleGenerator.cpp:84
dd4hep::sim::Geant4Event
User event context for DDG4.
Definition: Geant4Context.h:121
Printout.h
Geant4Context.h
dd4hep::sim::Geant4PrimaryEvent::add
void add(int id, Geant4PrimaryInteraction *interaction)
Add a new interaction object to the event.
Definition: Geant4Primary.cpp:92
dd4hep::sim::Geant4Context
Generic context to extend user, run and event information.
Definition: Geant4Context.h:201
dd4hep::sim::Geant4Action::context
Geant4Context * context() const
Access the context.
Definition: Geant4Action.h:270
dd4hep::sim::Geant4PrimaryEvent::get
Geant4PrimaryInteraction * get(int id) const
Retrieve an interaction by its ID.
Definition: Geant4Primary.cpp:107