Belle II Software  release-08-01-10
TouschekReaderTURTLE.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/touschek/TouschekReaderTURTLE.h>
10 
11 #include <framework/logging/Logger.h>
12 #include <framework/gearbox/Unit.h>
13 #include <mdst/dataobjects/MCParticle.h>
14 
15 #include <boost/lexical_cast.hpp>
16 #include <boost/algorithm/string.hpp>
17 #include <boost/tokenizer.hpp>
18 
19 using namespace std;
20 using namespace Belle2;
21 
22 
23 TouschekReaderTURTLE::TouschekReaderTURTLE(TGeoHMatrix* transMatrix, int pdg): m_transMatrix(transMatrix), m_pdg(pdg),
24  m_lineNum(0)
25 {
26 }
27 
28 
30 {
31  if (m_input) m_input.close();
32 }
33 
34 
35 void TouschekReaderTURTLE::open(const string& filename)
36 {
37  m_lineNum = 0;
38  m_input.open(filename.c_str());
39  if (!m_input) throw(TouschekCouldNotOpenFileError() << filename);
40 }
41 
42 
44 {
45  if (m_transMatrix == NULL) {
46  B2ERROR("The transformation matrix is NULL !");
47  }
48 
49  int numParticles = 0;
50  string currLine;
51  typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
52  boost::char_separator<char> sep(",; \t");
53  double fields[7];
54  double particlePosTouschek[3] = {0.0, 0.0, 0.0};
55  double particlePosGeant4[3] = {0.0, 0.0, 0.0};
56  double particleMomTouschek[3] = {0.0, 0.0, 0.0};
57  double particleMomGeant4[3] = {0.0, 0.0, 0.0};
58 
59  do {
60  getline(m_input, currLine);
61  m_lineNum++;
62 
63  boost::trim(currLine);
64  if (currLine == "") continue;
65 
66  tokenizer tokens(currLine, sep);
67 
68  int index = 0;
69  for (const string& tok : tokens) {
70  try {
71  if (index >= 7) {
72  B2WARNING("This Touschek file has more than 7 fields ! Only the first 7 fields were read.");
73  break;
74  }
75  fields[index] = boost::lexical_cast<double>(tok);
76  index++;
77  } catch (boost::bad_lexical_cast&) {
78  throw (TouschekConvertFieldError() << m_lineNum << index << tok);
79  }
80  }
81 
82  //Convert the position of the particle from local Touschek plane space to global geant4 space.
83  //Flip the sign for the y and z component to go from the accelerator to the detector coordinate system
84  particlePosTouschek[0] = fields[2] * Unit::m;
85  particlePosTouschek[1] = -fields[3] * Unit::m;
86  if (m_pdg == -11) particlePosTouschek[2] = 400.0; //forLER
87  else if (m_pdg == 11) particlePosTouschek[2] = -400.0; //forHER
88  else { B2WARNING("m_pdg is not 11/-11"); break;}
89 
90  m_transMatrix->LocalToMaster(particlePosTouschek, particlePosGeant4);
91 
92  //Convert the momentum of the particle from local Touschek plane space to global geant4 space.
93  //Flip the sign for the y and z component to go from the accelerator to the detector coordinate system
94  particleMomTouschek[0] = fields[4];
95  particleMomTouschek[1] = -fields[5];
96  if (m_pdg == -11) particleMomTouschek[2] = -fields[6]; //forLER
97  else if (m_pdg == 11) particleMomTouschek[2] = fields[6]; //forHER
98  else { B2WARNING("m_pdg is not 11/-11"); break;}
99 
100  m_transMatrix->LocalToMasterVect(particleMomTouschek, particleMomGeant4);
101 
102  double totalMom2 = particleMomGeant4[0] * particleMomGeant4[0];
103  totalMom2 += particleMomGeant4[1] * particleMomGeant4[1];
104  totalMom2 += particleMomGeant4[2] * particleMomGeant4[2];
105 
106  //Add particles to MCParticle collection
107  MCParticleGraph::GraphParticle& particle = graph.addParticle();
108  particle.setStatus(MCParticle::c_PrimaryParticle);
109  particle.setPDG(m_pdg);
110  particle.setMassFromPDG();
111  particle.setMomentum(ROOT::Math::XYZVector(particleMomGeant4[0], particleMomGeant4[1], particleMomGeant4[2]));
112  particle.setProductionVertex(ROOT::Math::XYZVector(particlePosGeant4[0], particlePosGeant4[1], particlePosGeant4[2]));
113  particle.setEnergy(sqrt(totalMom2 + particle.getMass()*particle.getMass()));
114  particle.setProductionTime(0.0);
115  particle.setValidVertex(true);
116 
117  numParticles++;
118  } while ((!m_input.eof()) && ((number > -1) && (numParticles < number)));
119 
120  return numParticles;
121 }
Class to represent Particle data in graph.
Class to build, validate and sort a particle decay chain.
@ c_PrimaryParticle
bit 0: Particle is primary particle.
Definition: MCParticle.h:47
int m_lineNum
The line number in the ascii file of the last particle which was read.
TGeoHMatrix * m_transMatrix
Transformation matrix from local Touschek to global geant4 space.
int m_pdg
The pdg value of the type of particle that is read (e.g.
void open(const std::string &filename)
Opens an ascii file and prepares it for reading.
std::ifstream m_input
The input stream of the ascii file.
int getParticles(int number, MCParticleGraph &graph)
Reads the specified number of particles from the file and stores the result in the given MCParticle g...
static const double m
[meters]
Definition: Unit.h:69
double sqrt(double a)
sqrt for double
Definition: beamHelpers.h:28
GraphParticle & addParticle()
Add new particle to the graph.
Abstract base class for different kinds of events.