DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
Geant4IsotropeGenerator.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/Geant4Random.h>
19 
20 using namespace dd4hep::sim;
21 
24  : Geant4ParticleGenerator(ctxt, nam)
25 {
27  declareProperty("PhiMin", m_phiMin = 0.0);
28  declareProperty("PhiMax", m_phiMax = 2.0*M_PI);
29  declareProperty("ThetaMin", m_thetaMin = 0.0);
30  declareProperty("ThetaMax", m_thetaMax = M_PI);
31  declareProperty("Distribution", m_distribution = "uniform" );
32 }
33 
37 }
38 
40 void Geant4IsotropeGenerator::getParticleDirectionUniform(int, ROOT::Math::XYZVector& direction, double& momentum) const {
41  Geant4Event& evt = context()->event();
42  Geant4Random& rnd = evt.random();
43  double phi = m_phiMin+(m_phiMax-m_phiMin)*rnd.rndm();
44  double theta = m_thetaMin+(m_thetaMax-m_thetaMin)*rnd.rndm();
45  double x1 = std::sin(theta)*std::cos(phi);
46  double x2 = std::sin(theta)*std::sin(phi);
47  double x3 = std::cos(theta);
48 
49  direction.SetXYZ(x1,x2,x3);
51 }
52 
54 void Geant4IsotropeGenerator::getParticleDirectionCosTheta(int, ROOT::Math::XYZVector& direction, double& momentum) const {
55  Geant4Event& evt = context()->event();
56  Geant4Random& rnd = evt.random();
57  double phi = m_phiMin+(m_phiMax-m_phiMin)*rnd.rndm();
58  double cos_theta = std::cos(m_thetaMin)+(std::cos(m_thetaMax)-std::cos(m_thetaMin))*rnd.rndm();
59  double sin_theta = std::sqrt(1.0-cos_theta*cos_theta);
60  double x1 = sin_theta*std::cos(phi);
61  double x2 = sin_theta*std::sin(phi);
62  double x3 = cos_theta;
63 
64  direction.SetXYZ(x1,x2,x3);
66 }
67 
69 void Geant4IsotropeGenerator::getParticleDirectionEta(int, ROOT::Math::XYZVector& direction, double& momentum) const {
70  struct Distribution {
71  static double eta(double x) { return -1.0*std::log(std::tan(x/2.0)); }
72  };
73  Geant4Event& evt = context()->event();
74  Geant4Random& rnd = evt.random();
75  // See https://en.wikipedia.org/wiki/Pseudorapidity
76  const double dmin = std::numeric_limits<double>::epsilon();
77  double phi = m_phiMin+(m_phiMax-m_phiMin)*rnd.rndm();
78  double eta_max = Distribution::eta(m_thetaMin>dmin ? m_thetaMin : dmin);
79  double eta_min = Distribution::eta(m_thetaMax>(M_PI-dmin) ? M_PI-dmin : m_thetaMax);
80  double eta = eta_min + (eta_max-eta_min)*rnd.rndm();
81  double x1 = std::cos(phi);
82  double x2 = std::sin(phi);
83  double x3 = std::sinh(eta);
84  double r = std::sqrt(1.0+x3*x3);
85  direction.SetXYZ(x1/r,x2/r,x3/r);
87 }
88 
90 void Geant4IsotropeGenerator::getParticleDirectionFFbar(int, ROOT::Math::XYZVector& direction, double& momentum) const {
91  struct Distribution {
92  static double ffbar(double x) { double c = std::cos(x); return 1 + c*c; }
93  //static double integral(double x) { return 1.5*x + sin(2.*x)/4.0; }
94  };
95  Geant4Event& evt = context()->event();
96  Geant4Random& rnd = evt.random();
97  double phi = m_phiMin+(m_phiMax-m_phiMin)*rnd.rndm();
98 
99  // 1 + cos^2(theta) cannot be integrated and then inverted.
100  // We have to throw the dice until it fits.....
101  double v1 = Distribution::ffbar(m_thetaMin), v2 = Distribution::ffbar(m_thetaMax);
102  double vmax = std::max(v1,v2); // ffbar symmetric and monotonic in |x|.
103  while(1) {
104  double dice = rnd.rndm()*vmax;
105  double theta = m_thetaMin+(m_thetaMax-m_thetaMin)*rnd.rndm();
106  double dist = Distribution::ffbar(theta);
107  if ( dice <= dist ) {
108  double x1 = std::sin(theta)*std::cos(phi);
109  double x2 = std::sin(theta)*std::sin(phi);
110  double x3 = std::cos(theta);
111  direction.SetXYZ(x1,x2,x3);
112  getParticleMomentumUniform(momentum);
113  return;
114  }
115  }
116 }
117 
119 void Geant4IsotropeGenerator::getParticleDirection(int num, ROOT::Math::XYZVector& direction, double& momentum) const {
120  switch(::toupper(m_distribution[0])) {
121  case 'C': // cos(theta)
122  return getParticleDirectionCosTheta(num, direction, momentum);
123  case 'F': // ffbar: ~ 1 + cos^2(theta)
124  return getParticleDirectionFFbar(num, direction, momentum);
125  case 'E': // eta, rapidity, pseudorapidity
126  case 'P':
127  case 'R':
128  return getParticleDirectionEta(num, direction, momentum);
129  case 'U': // uniform
130  return getParticleDirectionUniform(num, direction, momentum);
131  default:
132  break;
133  }
134  except("Unknown distribution densitiy: %s. Cannot generate primaries.",
135  m_distribution.c_str());
136 }
dd4hep::sim::Geant4IsotropeGenerator::~Geant4IsotropeGenerator
virtual ~Geant4IsotropeGenerator()
Default destructor.
Definition: Geant4IsotropeGenerator.cpp:35
Geant4IsotropeGenerator.h
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::Geant4IsotropeGenerator::m_phiMax
double m_phiMax
Property: Maximal phi angular value.
Definition: Geant4IsotropeGenerator.h:38
dd4hep::sim::Geant4IsotropeGenerator::getParticleDirectionUniform
void getParticleDirectionUniform(int num, ROOT::Math::XYZVector &direction, double &momentum) const
Uniform particle distribution.
Definition: Geant4IsotropeGenerator.cpp:40
M_PI
#define M_PI
Definition: Handle.h:33
dd4hep::sim::Geant4IsotropeGenerator::m_distribution
std::string m_distribution
Property: Distribution name. Default: "uniform". Allowed: "uniform", "cos(theta)",...
Definition: Geant4IsotropeGenerator.h:34
dd4hep::InstanceCount::increment
static void increment(T *)
Increment count according to type information.
Definition: InstanceCount.h:98
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::Geant4IsotropeGenerator::m_phiMin
double m_phiMin
Property: Minimal phi angular value.
Definition: Geant4IsotropeGenerator.h:36
dd4hep::sim::Geant4IsotropeGenerator::m_thetaMax
double m_thetaMax
Property: Maximal theta angular value.
Definition: Geant4IsotropeGenerator.h:42
dd4hep::sim::Geant4IsotropeGenerator::getParticleDirectionEta
void getParticleDirectionEta(int num, ROOT::Math::XYZVector &direction, double &momentum) const
e+e- --> ffbar particle distribution ~ 1 + cos^2(theta)
Definition: Geant4IsotropeGenerator.cpp:69
dd4hep::sim::Geant4IsotropeGenerator::getParticleDirectionCosTheta
void getParticleDirectionCosTheta(int num, ROOT::Math::XYZVector &direction, double &momentum) const
Particle distribution ~ cos(theta)
Definition: Geant4IsotropeGenerator.cpp:54
dd4hep::sim::Geant4IsotropeGenerator::m_thetaMin
double m_thetaMin
Property: Minimal theta angular value.
Definition: Geant4IsotropeGenerator.h:40
epsilon
const double epsilon
Definition: test_cellid_position_converter.cpp:41
dd4hep::sim::Geant4Action::except
void except(const char *fmt,...) const
Support of exceptions: Print fatal message and throw runtime_error.
Definition: Geant4Action.cpp:256
Geant4Random.h
dd4hep::sim::Geant4ParticleGenerator::getParticleMomentumUniform
void getParticleMomentumUniform(double &momentum) const
Uniform particle momentum.
Definition: Geant4ParticleGenerator.cpp:61
dd4hep::sim::Geant4IsotropeGenerator::getParticleDirectionFFbar
void getParticleDirectionFFbar(int num, ROOT::Math::XYZVector &direction, double &momentum) const
e+e- --> ffbar particle distribution ~ 1 + cos^2(theta)
Definition: Geant4IsotropeGenerator.cpp:90
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::Geant4IsotropeGenerator::Geant4IsotropeGenerator
Geant4IsotropeGenerator()=delete
Inhibit default constructor.
dd4hep::sim::Geant4Random
Mini interface to THE random generator of the application.
Definition: Geant4Random.h:59
dd4hep::sim::Geant4ParticleGenerator
Generate particles isotrop in space around origine (0,0,0)
Definition: Geant4ParticleGenerator.h:38
dd4hep::sim
Namespace for the Geant4 based simulation part of the AIDA detector description toolkit.
Definition: Geant4Output2EDM4hep.cpp:49
dist
double dist(const Position &p0, const Position &p1)
Definition: test_cellid_position_converter.cpp:45
dd4hep::sim::Geant4IsotropeGenerator::getParticleDirection
virtual void getParticleDirection(int num, ROOT::Math::XYZVector &direction, double &momentum) const override
Particle modification. Caller presets defaults to: ( direction = m_direction, momentum = [m_momentumM...
Definition: Geant4IsotropeGenerator.cpp:119
InstanceCount.h
dd4hep::sim::Geant4Event
User event context for DDG4.
Definition: Geant4Context.h:121
Printout.h
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