Belle II Software  release-05-02-19
TOPtrack.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2010 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Marko Staric *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #include <iostream>
12 #include <iomanip>
13 #include <math.h>
14 #include <stdlib.h>
15 #include <TRandom.h>
16 // top
17 #include <top/reconstruction/TOPtrack.h>
18 #include <top/geometry/TOPGeometryPar.h>
19 // framework aux
20 #include <framework/gearbox/Const.h>
21 #include <framework/logging/Logger.h>
22 // DataStore classes
23 #include <mdst/dataobjects/Track.h>
24 #include <mdst/dataobjects/TrackFitResult.h>
25 #include <mdst/dataobjects/HitPatternCDC.h>
26 #include <mdst/dataobjects/MCParticle.h>
27 #include <tracking/dataobjects/ExtHit.h>
28 #include <top/dataobjects/TOPBarHit.h>
29 
30 extern "C" {
31  void track2top_(float*, float*, float*, float*, int*); // from top_geo.F
32 }
33 
34 namespace Belle2 {
39  namespace TOP {
40 
41  TOPtrack::TOPtrack(double x, double y, double z,
42  double px, double py, double pz,
43  double Tlen, int Q, int pdg):
44  m_valid(true), m_position(x, y, z), m_momentum(px, py, pz),
45  m_trackLength(Tlen), m_charge(Q), m_pdg(pdg),
46  m_atTop(false), m_moduleID(0),
47  m_track(0), m_extHit(0), m_mcParticle(0), m_barHit(0)
48  {
50  }
51 
52 
53  TOPtrack::TOPtrack(const Track* track,
54  const Const::ChargedStable& chargedStable)
55  {
56 
57  if (!track) return;
58 
59  const auto* fitResult = track->getTrackFitResultWithClosestMass(chargedStable);
60  if (!fitResult) {
61  B2ERROR("No TrackFitResult available."
62  << LogVar("PDG code", chargedStable.getPDGCode()));
63  return;
64  }
65 
66  // require hits in CDC
67  if (fitResult->getHitPatternCDC().getNHits() == 0) return;
68 
69  // set pointers first
70 
71  m_track = track;
72 
73  Const::EDetector myDetID = Const::EDetector::TOP;
74  const auto* geo = TOPGeometryPar::Instance()->getGeometry();
75  int numModules = geo->getNumModules();
76  int pdgCode = abs(chargedStable.getPDGCode());
77 
78  RelationVector<ExtHit> extHits = track->getRelationsWith<ExtHit>();
79  double tmin = 1e10; // some large time
80  for (unsigned i = 0; i < extHits.size(); i++) {
81  const ExtHit* extHit = extHits[i];
82  if (abs(extHit->getPdgCode()) != pdgCode) continue;
83  if (extHit->getDetectorID() != myDetID) continue;
84  if (extHit->getCopyID() < 1 or extHit->getCopyID() > numModules) continue;
85  if (extHit->getTOF() < tmin) {
86  tmin = extHit->getTOF();
87  m_extHit = extHit;
88  }
89  }
90  if (!m_extHit) return;
91 
92  m_mcParticle = track->getRelated<MCParticle>();
93  if (m_mcParticle) {
94  const auto barHits = m_mcParticle->getRelationsWith<TOPBarHit>();
95  for (const auto& barHit : barHits) {
96  if (barHit.getModuleID() == m_extHit->getCopyID()) m_barHit = &barHit;
97  }
98  }
99 
100  // set track parameters
101 
104  setTrackLength(m_extHit->getTOF(), chargedStable);
105  m_charge = fitResult->getChargeSign();
108  m_valid = true;
109  }
110 
111 
112  TOPtrack::TOPtrack(const Track* track, int moduleID,
113  const Const::ChargedStable& chargedStable)
114  {
115 
116  if (!track) return;
117 
118  // set pointers first
119 
120  m_track = track;
121 
122  Const::EDetector myDetID = Const::EDetector::TOP;
123  int pdgCode = abs(chargedStable.getPDGCode());
124 
125  RelationVector<ExtHit> extHits = track->getRelationsWith<ExtHit>();
126  double tmin = 1e10; // some large time
127  for (const auto& extHit : extHits) {
128  if (abs(extHit.getPdgCode()) != pdgCode) continue;
129  if (extHit.getDetectorID() != myDetID) continue;
130  if (extHit.getCopyID() != moduleID) continue;
131  if (extHit.getTOF() < tmin) {
132  tmin = extHit.getTOF();
133  m_extHit = &extHit;
134  }
135  }
136  if (!m_extHit) return;
137 
138  m_mcParticle = track->getRelated<MCParticle>();
139  if (m_mcParticle) {
140  const auto barHits = m_mcParticle->getRelationsWith<TOPBarHit>();
141  for (const auto& barHit : barHits) {
142  if (barHit.getModuleID() == m_extHit->getCopyID()) m_barHit = &barHit;
143  }
144  }
145 
146  // set track parameters
149  setTrackLength(m_extHit->getTOF(), chargedStable);
150  const auto* fitResult = track->getTrackFitResultWithClosestMass(chargedStable);
151  if (!fitResult) {
152  B2ERROR("No TrackFitResult available."
153  << LogVar("PDG code", chargedStable.getPDGCode()));
154  return;
155  }
156  m_charge = fitResult->getChargeSign();
158  m_moduleID = moduleID;
159  m_valid = true;
160  }
161 
162 
163  double TOPtrack::getTOF(double mass) const
164  {
165  double pmom = getP();
166  double beta = pmom / sqrt(pmom * pmom + mass * mass);
167  return m_trackLength / beta / Const::speedOfLight;
168  }
169 
170  void TOPtrack::setTrackLength(double tof, double mass)
171  {
172  double pmom = getP();
173  double beta = pmom / sqrt(pmom * pmom + mass * mass);
174  m_trackLength = tof * Const::speedOfLight * beta;
175  }
176 
177  int TOPtrack::getHypID() const
178  {
179  int lundc[6] = {11, 13, 211, 321, 2212, 1000010020};
180  for (int i = 0; i < 6; i++) {
181  if (abs(m_pdg) == lundc[i]) return i + 1;
182  }
183  return 0;
184  }
185 
187  {
188  if (m_atTop) return m_moduleID;
189 
190  float r[3] = {(float) m_position.X(),
191  (float) m_position.Y(),
192  (float) m_position.Z()
193  };
194  float p[3] = {(float) m_momentum.X(),
195  (float) m_momentum.Y(),
196  (float) m_momentum.Z()
197  };
198  float q = (float) m_charge;
199  float t; int m;
200  track2top_(r, p, &q, &t, &m);
201  m_position.SetXYZ(r[0], r[1], r[2]);
202  m_momentum.SetXYZ(p[0], p[1], p[2]);
204  m_atTop = true;
205  m_moduleID = m + 1;
206  return m_moduleID;
207  }
208 
209  void TOPtrack::smear(double sig_x, double sig_z,
210  double sig_theta, double sig_phi)
211  {
212  double p = getP();
213  if (p == 0) return;
214  double theta = getTheta() + gRandom->Gaus(0., sig_theta);
215  double phi = getPhi() + gRandom->Gaus(0., sig_phi);
216  m_momentum.SetX(p * cos(phi) * sin(theta));
217  m_momentum.SetY(p * sin(phi) * sin(theta));
218  m_momentum.SetZ(p * cos(theta));
219 
220  double rho = gRandom->Gaus(0., sig_x);
221  if (m_position.Perp() != 0) phi = atan2(m_position.Y(), m_position.X());
222  double x = m_position.X() + rho * sin(phi);
223  double y = m_position.Y() - rho * cos(phi);
224  double z = m_position.Z() + gRandom->Gaus(0., sig_z);
225  m_position.SetXYZ(x, y, z);
226 
227  }
228 
229  void TOPtrack::dump() const
230  {
231  double pi = 4 * atan(1);
232  using namespace std;
233  cout << "TOPtrack::dump(): ";
234  cout << " PDG=" << m_pdg;
235  cout << " charge=" << m_charge << endl;
236  cout << " p=" << setprecision(3) << getP() << " GeV/c";
237  cout << " theta=" << setprecision(3) << getTheta() / pi * 180;
238  cout << " phi=" << setprecision(3) << getPhi() / pi * 180 << endl;
239  cout << " x=" << getX() << " cm";
240  cout << " y=" << getY() << " cm";
241  cout << " z=" << getZ() << " cm\n";
242  cout << " trackLength=" << setprecision(4) << m_trackLength << " cm";
243  cout << " atTop=" << m_atTop;
244  cout << " moduleID=" << m_moduleID;
245  cout << endl;
246  }
247 
249  {
250  float r[3] = {(float) m_position.X(),
251  (float) m_position.Y(),
252  (float) m_position.Z()
253  };
254  float p[3] = {(float) m_momentum.X(),
255  (float) m_momentum.Y(),
256  (float) m_momentum.Z()
257  };
258  float q = (float) m_charge;
259  float t;
260  int m;
261  track2top_(r, p, &q, &t, &m);
262  return m + 1;
263  }
264 
265  } // end top namespace
267 } // end Belle2 namespace
268 
Belle2::RelationVector::size
size_t size() const
Get number of relations.
Definition: RelationVector.h:98
Belle2::TOP::TOPtrack::setTrackLength
void setTrackLength(double tof, double mass)
Set track length from time-of-flight and particle mass.
Definition: TOPtrack.cc:170
Belle2::ExtHit::getPdgCode
int getPdgCode() const
Get PDG code of this extrapolation's hypothesis.
Definition: ExtHit.h:126
Belle2::RelationsInterface::getRelationsWith
RelationVector< T > getRelationsWith(const std::string &name="", const std::string &namedRelation="") const
Get the relations between this object and another store array.
Definition: RelationsObject.h:232
Belle2::ExtHit::getPosition
TVector3 getPosition() const
Get position of this extrapolation hit.
Definition: ExtHit.h:153
Belle2::ExtHit::getDetectorID
Const::EDetector getDetectorID() const
Get detector ID of this extrapolation hit.
Definition: ExtHit.h:130
Belle2::TOP::TOPtrack::findModule
int findModule()
Finds moduleID the track is crossing.
Definition: TOPtrack.cc:248
Belle2::Const::ParticleType::getPDGCode
int getPDGCode() const
PDG code.
Definition: Const.h:349
Belle2::ExtHit::getTOF
double getTOF() const
Get time of flight from the point of closest approach near the origin to this hit.
Definition: ExtHit.h:145
Belle2::TOP::TOPtrack::toTop
int toTop()
Propagate track to TOP counter (assuming uniform B field along z)
Definition: TOPtrack.cc:186
Belle2::TOP::TOPtrack::getP
double getP() const
Return momentum magnitude.
Definition: TOPtrack.h:181
Belle2::Const::EDetector
EDetector
Enum for identifying the detector components (detector and subdetector).
Definition: Const.h:44
Belle2::TOP::TOPtrack::m_valid
bool m_valid
true for properly defined track
Definition: TOPtrack.h:278
Belle2::TOP::TOPtrack::m_barHit
const TOPBarHit * m_barHit
pointer to bar hit or NULL
Definition: TOPtrack.h:289
Belle2::TOP::TOPtrack::TOPtrack
TOPtrack()
Default constructor.
Definition: TOPtrack.h:47
Belle2::TOP::TOPGeometryPar::Instance
static TOPGeometryPar * Instance()
Static method to obtain the pointer to its instance.
Definition: TOPGeometryPar.cc:45
Belle2::Const::speedOfLight
static const double speedOfLight
[cm/ns]
Definition: Const.h:568
Belle2::TOP::TOPtrack::getY
double getY() const
Return position component.
Definition: TOPtrack.h:111
Belle2::ExtHit
Store one Ext hit as a ROOT object.
Definition: ExtHit.h:40
Belle2::TOP::TOPtrack::getX
double getX() const
Return position component.
Definition: TOPtrack.h:105
Belle2::TOPBarHit
Class to store track parameters of incoming MC particles relation to MCParticle filled in top/simulat...
Definition: TOPBarHit.h:36
Belle2::RelationVector
Class for type safe access to objects that are referred to in relations.
Definition: DataStore.h:38
Belle2::TOP::TOPtrack::getTheta
double getTheta() const
Return momentum polar angle.
Definition: TOPtrack.h:187
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::TOP::TOPtrack::getTOF
double getTOF(double mass) const
Return time-of-flight from IP to current position for given particle mass.
Definition: TOPtrack.cc:163
Belle2::TOP::TOPtrack::dump
void dump() const
Print track parameters to std output.
Definition: TOPtrack.cc:229
Belle2::TOP::TOPtrack::m_atTop
bool m_atTop
true, if toTop() called
Definition: TOPtrack.h:284
Belle2::TOP::TOPtrack::m_momentum
TVector3 m_momentum
momentum vector
Definition: TOPtrack.h:280
Belle2::TOPGeometry::getNumModules
unsigned getNumModules() const
Returns number of modules.
Definition: TOPGeometry.h:146
LogVar
Class to store variables with their name which were sent to the logging service.
Definition: LogVariableStream.h:24
Belle2::MCParticle::getPDG
int getPDG() const
Return PDG code of particle.
Definition: MCParticle.h:123
Belle2::TOP::TOPtrack::getHypID
int getHypID() const
Return internal particle code.
Definition: TOPtrack.cc:177
Belle2::TOP::TOPtrack::m_mcParticle
const MCParticle * m_mcParticle
pointer to MC particle or NULL
Definition: TOPtrack.h:288
Belle2::TOP::TOPtrack::m_pdg
int m_pdg
PDG code (optional)
Definition: TOPtrack.h:283
Belle2::TOP::TOPtrack::smear
void smear(double sig_x, double sig_z, double sig_theta, double sig_phi)
Smear track.
Definition: TOPtrack.cc:209
Belle2::TOP::TOPtrack::m_position
TVector3 m_position
position vector
Definition: TOPtrack.h:279
Belle2::ExtHit::getCopyID
int getCopyID() const
Get detector-element ID of sensitive element within detector.
Definition: ExtHit.h:134
Belle2::TOP::TOPtrack::m_charge
int m_charge
charge
Definition: TOPtrack.h:282
Belle2::Const::ChargedStable
Provides a type-safe way to pass members of the chargedStableSet set.
Definition: Const.h:465
Belle2::TOP::TOPtrack::m_track
const Track * m_track
pointer to mdst track or NULL
Definition: TOPtrack.h:286
Belle2::ExtHit::getMomentum
TVector3 getMomentum() const
Get momentum at this extrapolation hit.
Definition: ExtHit.h:157
Belle2::TOP::TOPGeometryPar::getGeometry
const TOPGeometry * getGeometry() const
Returns pointer to geometry object using Basf2 units.
Definition: TOPGeometryPar.cc:167
Belle2::Track
Class that bundles various TrackFitResults.
Definition: Track.h:35
Belle2::MCParticle
A Class to store the Monte Carlo particle information.
Definition: MCParticle.h:43
Belle2::TOP::TOPtrack::m_trackLength
double m_trackLength
track length from IP to point
Definition: TOPtrack.h:281
Belle2::TOP::TOPtrack::getZ
double getZ() const
Return position component.
Definition: TOPtrack.h:117
Belle2::TOP::TOPtrack::getPhi
double getPhi() const
Return momentum azimuthal angle.
Definition: TOPtrack.h:193
Belle2::TOP::TOPtrack::m_extHit
const ExtHit * m_extHit
pointer to extrapolated hit or NULL
Definition: TOPtrack.h:287
Belle2::TOP::TOPtrack::m_moduleID
int m_moduleID
module ID or 0
Definition: TOPtrack.h:285