Belle II Software  release-06-01-15
InitialParticleGeneration.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 
9 #include <generators/utilities/InitialParticleGeneration.h>
10 #include <framework/gearbox/Const.h>
11 #include <framework/logging/Logger.h>
12 
13 namespace Belle2 {
20  {
21  m_event.registerInDataStore();
22  }
23 
24  TVector3 InitialParticleGeneration::generateVertex(const TVector3& initial, const TMatrixDSym& cov,
26  {
27  if (m_event->hasGenerationFlags(BeamParameters::c_smearVertex)) {
28  if (!gen.size()) gen.setMeanCov(initial, cov);
29  return gen.generateVec3();
30  }
31  return initial;
32  }
33 
34  TLorentzVector InitialParticleGeneration::generateBeam(const TLorentzVector& initial, const TMatrixDSym& cov,
36  {
37  if (!(m_event->getGenerationFlags() & BeamParameters::c_smearBeam)) {
38  //no smearing, nothing to do
39  return initial;
40  }
41  if (gen.size() != 3) gen.setMeanCov(TVector3(initial.E(), 0, 0), cov);
42  // generate the beam parameters: energy and horizontal angle and vertical
43  // angle with respect to nominal direction
44  // p[0] = energy
45  // p[1] = horizontal angle
46  // p[2] = vertical angle
47  Eigen::VectorXd p = gen.generate();
48  // if we don't smear beam energy set it to nominal values
49  if (!m_event->hasGenerationFlags(BeamParameters::c_smearBeamEnergy)) {
50  p[0] = initial.E();
51  }
52  // if we don't smear beam direction set angles to 0
53  if (!m_event->hasGenerationFlags(BeamParameters::c_smearBeamDirection)) {
54  p[1] = 0;
55  p[2] = 0;
56  }
57  // calculate TLorentzvector from p
58  const double pz = std::sqrt(p[0] * p[0] - Const::electronMass * Const::electronMass);
59  // Rotate around theta_x and theta_y. Same as result.RotateX(p[1]);
60  // result.RotateY(p[2]) but as we know it's a 0,0,pz vector this can be
61  // optimized.
62  const double sx = sin(p[1]);
63  const double cx = cos(p[1]);
64  const double sy = sin(p[2]);
65  const double cy = cos(p[2]);
66  const double px = sy * cx * pz;
67  const double py = -sx * pz;
68  TLorentzVector result(px, py, cx * cy * pz, p[0]);
69  // And rotate into the direction of the nominal beam
70  result.RotateY(initial.Theta());
71  return result;
72  }
73 
75  {
76  return generate(m_allowedFlags);
77  }
78 
80  {
81  if (!m_event) {
82  m_event.create();
83  }
84  if (!m_beamParams.isValid()) {
85  B2FATAL("Cannot generate beam without valid BeamParameters");
86  }
87  if (m_beamParams.hasChanged()) {
91  }
92  m_event->setGenerationFlags(m_beamParams->getGenerationFlags() & allowedFlags);
93  TLorentzVector her = generateBeam(m_beamParams->getHER(), m_beamParams->getCovHER(), m_generateHER);
94  TLorentzVector ler = generateBeam(m_beamParams->getLER(), m_beamParams->getCovLER(), m_generateLER);
95  TVector3 vtx = generateVertex(m_beamParams->getVertex(), m_beamParams->getCovVertex(), m_generateVertex);
96  m_event->set(her, ler, vtx);
97  //Check if we want to go to CMS, if so boost both
98  if (m_beamParams->hasGenerationFlags(BeamParameters::c_generateCMS)) {
99  m_event->setGenerationFlags(0);
100  her = m_event->getLabToCMS() * her;
101  ler = m_event->getLabToCMS() * ler;
102  m_event->set(her, ler, vtx);
103  m_event->setGenerationFlags(m_beamParams->getGenerationFlags() & allowedFlags);
104  }
105  return *m_event;
106  }
107 
109  {
110  if (!m_beamParams.isValid()) {
111  B2FATAL("Cannot generate beam without valid BeamParameters");
112  }
113  if (!m_event) {
114  // generate a new mc initial particle without smearing except for the vertex
116  return m_event->getVertex();
117  }
118  if (!m_beamParams->hasGenerationFlags(BeamParameters::c_smearVertex) or
119  (m_event->hasGenerationFlags(BeamParameters::c_smearVertex) and not force)) {
120  // already has a smeared vertex or smearing disallowed. nothing to do
121  return {0, 0, 0};
122  }
123  // Ok, now we need to just update the vertex, make sure the parameters are up to date ...
124  if (m_beamParams.hasChanged()) {
128  }
129  // Add the smear vertex flag
130  m_event->setGenerationFlags(m_event->getGenerationFlags() | BeamParameters::c_smearVertex);
131  // And generate a vertex ...
132  auto previous = m_event->getVertex();
133  auto vtx = generateVertex(m_beamParams->getVertex(), m_beamParams->getCovVertex(), m_generateVertex);
134  m_event->setVertex(vtx);
135  // Everything done
136  return vtx - previous;
137  }
139 } //Belle2 namespace
static const double electronMass
electron mass
Definition: Const.h:565
DBObjPtr< BeamParameters > m_beamParams
Datastore object containing the nominal beam parameters.
int m_allowedFlags
Allowed generation flags.
MultivariateNormalGenerator m_generateHER
Generator for HER.
StoreObjPtr< MCInitialParticles > m_event
Datastore object containing the generated event.
MultivariateNormalGenerator m_generateLER
Generator for LER.
MultivariateNormalGenerator m_generateVertex
Generator for Vertex.
This class contains the initial state for the given event.
@ c_smearBeam
smear the full beam momentum (energy and direction)
@ c_generateCMS
generate initial event in CMS instead of lab
@ c_smearBeamEnergy
smear energy of HER and LER (but not direction)
@ c_smearBeamDirection
smear direction of HER and LER (but not energy)
Class to generate normal distributed, correlated random numbers given the mean values and the covaria...
size_t size() const
Return the number of elements to be generated on generate()
bool setMeanCov(int n, const double *mean, const double *cov)
set the mean and covariance for the distribution with array interface: mean and covariance are passed...
Eigen::VectorXd generate() const
Generate a set of correlated random numbers with the previouly set mean and covariance.
void reset()
reset the generator setting the size to 0.
TVector3 generateVec3() const
Generate a set of correlated random numbers with the previouly set mean and covariance and return a T...
TVector3 updateVertex(bool force=false)
Update the vertex position:
void initialize()
function to be executed on initialize()
MCInitialParticles & generate()
Generate a new event.
TVector3 generateVertex(const TVector3 &initial, const TMatrixDSym &cov, MultivariateNormalGenerator &gen)
generate the vertex
TLorentzVector generateBeam(const TLorentzVector &initial, const TMatrixDSym &cov, MultivariateNormalGenerator &gen)
generate 4 vector for one beam
Abstract base class for different kinds of events.