Belle II Software development
TOPTrack.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 <top/reconstruction_cpp/TOPTrack.h>
10#include <top/reconstruction_cpp/TOPRecoManager.h>
11#include <top/geometry/TOPGeometryPar.h>
12#include <framework/geometry/BFieldManager.h>
13#include <framework/logging/Logger.h>
14#include <mdst/dataobjects/Track.h>
15#include <mdst/dataobjects/TrackFitResult.h>
16#include <mdst/dataobjects/HitPatternCDC.h>
17#include <tracking/dataobjects/ExtHit.h>
18#include <top/dataobjects/TOPBarHit.h>
19#include <top/dataobjects/TOPDigit.h>
20#include <framework/datastore/StoreArray.h>
21#include <cmath>
22#include <vector>
23#include <algorithm>
24
25using namespace std;
26using namespace ROOT::Math;
27
28namespace Belle2 {
33 namespace TOP {
34
35 TOPTrack::TrackAngles::TrackAngles(const ROOT::Math::XYZVector& dir):
36 cosTh(dir.Z()), sinTh(sqrt(1 - cosTh * cosTh)), cosFi(dir.X() / sinTh), sinFi(dir.Y() / sinTh)
37 {}
38
39
40 TOPTrack::TOPTrack(const Track& track, const std::string& digitsName, const Const::ChargedStable& chargedStable)
41 {
42 // find extrapolated track hit at TOP
43
44 Const::EDetector myDetID = Const::EDetector::TOP;
45 const auto* geo = TOPGeometryPar::Instance()->getGeometry();
46 int numModules = geo->getNumModules();
47 int pdgCode = std::abs(chargedStable.getPDGCode());
48
49 RelationVector<ExtHit> extHits = track.getRelationsWith<ExtHit>();
50 double tmin = 1e10; // some large time
51 for (const auto& extHit : extHits) {
52 if (std::abs(extHit.getPdgCode()) != pdgCode) continue;
53 if (extHit.getDetectorID() != myDetID) continue;
54 if (extHit.getCopyID() < 1 or extHit.getCopyID() > numModules) continue;
55 if (extHit.getTOF() < tmin) {
56 tmin = extHit.getTOF();
57 m_extHit = &extHit;
58 }
59 }
60 if (not m_extHit) return;
61
62 // set the object
63
64 set(track, digitsName, chargedStable);
65 }
66
67
68 TOPTrack::TOPTrack(const ExtHit* extHit, const std::string& digitsName)
69 {
70 if (not extHit) return;
71 m_extHit = extHit;
72
73 const auto* track = extHit->getRelated<Track>();
74 if (not track) {
75 B2ERROR("TOPTrack: no related Track found for valid ExtHit");
76 return;
77 }
78
79 auto chargedStable = Const::chargedStableSet.find(std::abs(extHit->getPdgCode()));
80 if (chargedStable == Const::invalidParticle) {
81 B2ERROR("TOPTrack: extrapolation hypothesis of ExtHit is not ChargedStable");
82 return;
83 }
84
85 // set the object
86
87 set(*track, digitsName, chargedStable);
88 }
89
90
91 void TOPTrack::set(const Track& track, const std::string& digitsName, const Const::ChargedStable& chargedStable)
92 {
93 // require fitResult
94
95 const auto* fitResult = track.getTrackFitResultWithClosestMass(chargedStable);
96 if (not fitResult) {
97 B2ERROR("TOPTrack: no TrackFitResult available for PDGCode = " << chargedStable.getPDGCode());
98 return;
99 }
100 m_pT = fitResult->getTransverseMomentum();
101
102 // require hits in CDC
103
104 if (fitResult->getHitPatternCDC().getNHits() == 0) return;
105
106 // set pointers
107
108 m_track = &track;
109 m_mcParticle = track.getRelated<MCParticle>();
110 if (m_mcParticle) {
111 const auto barHits = m_mcParticle->getRelationsWith<TOPBarHit>();
112 for (const auto& barHit : barHits) {
113 if (barHit.getModuleID() == m_extHit->getCopyID()) m_barHit = &barHit;
114 }
115 }
116
117 // set track parameters and helix
118
119 m_moduleID = m_extHit->getCopyID();
120 m_momentum = m_extHit->getMomentum().R();
121 m_charge = fitResult->getChargeSign();
122 m_TOFLength = m_extHit->getTOF() * Const::speedOfLight * getBeta(chargedStable);
123 m_valid = setHelix(m_alignment->getTransformation(m_moduleID));
124 if (not m_valid) return;
125
126 // selection of photon hits belonging to this track
127
128 unsigned numHitsOtherSlots = 0;
129 StoreArray<TOPDigit> digits(digitsName);
130 for (const auto& digit : digits) {
131 if (digit.getHitQuality() != TOPDigit::c_Good) continue;
132 if (digit.getModuleID() == m_moduleID) {
133 m_selectedHits.push_back(SelectedHit(digit.getPixelID(), digit.getTime(), digit.getTimeError()));
134 } else {
135 numHitsOtherSlots++;
136 }
137 }
138
139 // background rate estimation
140
141 const auto* geo = TOPGeometryPar::Instance()->getGeometry();
142 m_tts = &geo->getTTS(0); // PMT independent TTS should be fine in isScanRequired
143 const auto& tdc = geo->getNominalTDC();
144 double timeWindow = m_feSetting->getReadoutWindows() * tdc.getSyncTimeBase() / static_cast<double>(TOPNominalTDC::c_syncWindows);
145
146 const auto& backgroundPDFs = TOPRecoManager::getBackgroundPDFs();
147 unsigned k = m_moduleID - 1;
148 double effi = (k < backgroundPDFs.size()) ? backgroundPDFs[k].getEfficiency() : 0;
149 double effiSum = 0;
150 for (const auto& bkg : backgroundPDFs) effiSum += bkg.getEfficiency();
151 m_bkgRate = (effiSum > effi) ? numHitsOtherSlots * effi / (effiSum - effi) / timeWindow : 0;
152
153 // selected photon hits mapped to pixel columns
154
155 const auto* yScanner = TOPRecoManager::getYScanner(m_moduleID);
156 if (not yScanner) {
157 B2ERROR("TOPTrack: YScanner for given module not found (must be a bug!)" << LogVar("slot", m_moduleID));
158 m_valid = false;
159 return;
160 }
161 unsigned numCols = yScanner->getPixelPositions().getNumPixelColumns();
162 for (const auto& hit : m_selectedHits) {
163 unsigned col = (hit.pixelID - 1) % numCols;
164 m_columnHits.emplace(col, &hit);
165 }
166
167 m_valid = effi > 0; // no sense to provide PID for the track if the module is fully inefficient
168 }
169
170
171 bool TOPTrack::setHelix(const Transform3D& transform)
172 {
173 m_emissionPoints.clear();
174 m_emissionPoint0.isSet = false;
175
176 // helix in module nominal frame (z-axis still parallel to magnetic field)
177
178 const auto* geo = TOPGeometryPar::Instance()->getGeometry();
179 const auto& module = geo->getModule(m_moduleID);
180 auto globalPosition = m_extHit->getPosition();
181 auto position = module.pointGlobalToNominal(static_cast<XYZPoint>(globalPosition));
182 auto momentum = module.momentumGlobalToNominal(m_extHit->getMomentum());
183 double Bz = BFieldManager::getField(globalPosition).Z();
184 m_helix.set(position, momentum, m_charge, Bz);
185 m_helix.setTransformation(transform);
186
187 // geometry data
188
189 const RaytracerBase::BarSegment bar(module);
190 const RaytracerBase::Mirror mirror(module);
191
192 // bar surfaces in module nominal frame
193
194 std::vector<XYZPoint> points;
195 std::vector<XYZVector> normals;
196 points.push_back(transform * XYZPoint(0, -bar.B / 2, 0)); // lower
197 normals.push_back(transform * XYZVector(0, -1, 0));
198
199 points.push_back(transform * XYZPoint(0, bar.B / 2, 0)); // upper
200 normals.push_back(transform * XYZVector(0, 1, 0));
201
202 points.push_back(transform * XYZPoint(-bar.A / 2, 0, 0)); // left side
203 normals.push_back(transform * XYZVector(-1, 0, 0));
204
205 points.push_back(transform * XYZPoint(bar.A / 2, 0, 0)); // right side
206 normals.push_back(transform * XYZVector(1, 0, 0));
207
208 // intersection with quartz bar
209
210 std::vector<double> lengths; // w.r.t extHit position
211 std::vector<XYZPoint> positions; // in module local frame
212 for (size_t i = 0; i < 2; i++) {
213 double t = m_helix.getDistanceToPlane(points[i], normals[i]);
214 if (isnan(t)) return false;
215 auto r = m_helix.getPosition(t);
216 if (std::abs(r.X()) > bar.A / 2) {
217 auto k = (r.X() > 0) ? 3 : 2;
218 t = m_helix.getDistanceToPlane(points[k], normals[k]);
219 if (isnan(t)) return false;
220 r = m_helix.getPosition(t);
221 if (r.Z() >= bar.zL and std::abs(r.Y()) > bar.B / 2) return false;
222 }
223 lengths.push_back(t);
224 positions.push_back(r);
225 }
226
227 // crossing prism?
228
229 if (positions[0].Z() < bar.zL or positions[1].Z() < bar.zL) {
230 const RaytracerBase::Prism prism(module);
231 bool ok = xsecPrism(lengths, positions, prism, transform);
232 if (not ok) return false;
233 }
234
235 // crossing mirror surface?
236
237 std::vector<bool> outOfBar;
238 XYZPoint rc(mirror.xc, mirror.yc, mirror.zc);
239 double Rsq = mirror.R * mirror.R;
240 for (const auto& r : positions) {
241 outOfBar.push_back((r - rc).Mag2() > Rsq);
242 }
243 if (outOfBar[0] and outOfBar[1]) return false;
244
245 if (outOfBar[0] or outOfBar[1]) { // track crosses mirror surface, where?
246 if (outOfBar[0]) std::reverse(lengths.begin(), lengths.end());
247 double t1 = lengths[0];
248 double t2 = lengths[1];
249 for (int i = 0; i < 20; i++) {
250 double t = (t1 + t2) / 2;
251 auto r = m_helix.getPosition(t);
252 if ((r - rc).Mag2() > Rsq) {
253 t2 = t;
254 } else {
255 t1 = t;
256 }
257 }
258 lengths[1] = (t1 + t2) / 2;
259 }
260
261 // set track length in quartz and full length from IP to the average emission point
262
263 m_length = std::abs(lengths[1] - lengths[0]);
264 double length = (lengths[0] + lengths[1]) / 2;
265 m_trackLength = m_TOFLength + length;
266
267 // finally move reference position to average emission point
268
269 m_helix.moveReferencePosition(length);
270
271 return m_length > bar.B / 2; // require minimal track length inside quartz (more than half of bar thickness)
272 }
273
274
275 bool TOPTrack::xsecPrism(std::vector<double>& lengths, std::vector<XYZPoint>& positions,
276 const RaytracerBase::Prism& prism, const Transform3D& transform)
277 {
278 std::vector<XYZPoint> points;
279 std::vector<XYZVector> normals;
280
281 points.push_back(transform * XYZPoint(0, prism.yDown, 0)); // lower-most surface
282 normals.push_back(transform * XYZVector(0, -1, 0));
283
284 points.push_back(transform * XYZPoint(0, prism.yUp, 0)); // upper surface
285 normals.push_back(transform * XYZVector(0, 1, 0));
286
287 points.push_back(transform * XYZPoint(-prism.A / 2, 0, 0)); // left-side surface
288 normals.push_back(transform * XYZVector(-1, 0, 0));
289
290 points.push_back(transform * XYZPoint(prism.A / 2, 0, 0)); // right-side surface
291 normals.push_back(transform * XYZVector(1, 0, 0));
292
293 for (size_t i = 0; i < 2; i++) {
294 if (positions[i].Z() < prism.zR) {
295 double t = m_helix.getDistanceToPlane(points[i], normals[i]);
296 if (isnan(t)) return false;
297 auto r = m_helix.getPosition(t);
298 if (i == 0 and r.Z() > prism.zFlat) { // intersection with slanted surface -> find it using bisection
299 auto point = transform * XYZPoint(0, -prism.B / 2, 0);
300 double t1 = m_helix.getDistanceToPlane(point, normals[0]);
301 if (isnan(t1)) return false;
302 double t2 = t;
303 for (int iter = 0; iter < 20; iter++) {
304 t = (t1 + t2) / 2;
305 r = m_helix.getPosition(t);
306 double ySlanted = prism.yDown + prism.slope * (r.Z() - prism.zFlat);
307 if (r.Y() < ySlanted) t2 = t;
308 else t1 = t;
309 }
310 t = (t1 + t2) / 2;
311 }
312 if (std::abs(r.X()) > prism.A / 2) { // intersection on the side surface
313 auto k = (r.X() > 0) ? 3 : 2;
314 t = m_helix.getDistanceToPlane(points[k], normals[k]);
315 if (isnan(t)) return false;
316 r = m_helix.getPosition(t);
317 if (r.Z() < prism.zR) { // yes, it's on the prism side
318 if (r.Y() > prism.yUp or r.Y() < prism.yDown) return false;
319 double ySlanted = prism.yDown + prism.slope * (r.Z() - prism.zFlat);
320 if (r.Y() < ySlanted) return false;
321 } else { // no, it's on the prism entrance but outside the bar exit window -> find it using bisection
322 double t1 = lengths[i];
323 double t2 = t;
324 for (int iter = 0; iter < 20; iter++) {
325 t = (t1 + t2) / 2;
326 r = m_helix.getPosition(t);
327 if (r.Z() < prism.zR) t1 = t;
328 else t2 = t;
329 }
330 t = (t1 + t2) / 2;
331 }
332 }
333 lengths[i] = t;
334 positions[i] = m_helix.getPosition(t);
335 }
336 }
337
338 if (positions[0].Z() < prism.zL and positions[1].Z() < prism.zL) return false;
339
340 if (positions[0].Z() < prism.zL or positions[1].Z() < prism.zL) { // intersection is on exit window
341 int i0 = (positions[0].Z() < prism.zL) ? 0 : 1;
342 double t1 = lengths[i0];
343 double t2 = lengths[(i0 + 1) % 2];
344 for (int iter = 0; iter < 20; iter++) {
345 double t = (t1 + t2) / 2;
346 auto r = m_helix.getPosition(t);
347 if (r.Z() < prism.zL) t1 = t;
348 else t2 = t;
349 }
350 double t = (t1 + t2) / 2;
351 lengths[i0] = t;
352 positions[i0] = m_helix.getPosition(t);
353 }
354
355 return true;
356 }
357
358
360 {
361 if (m_emissionPoints.size() > 1000) m_emissionPoints.clear(); // prevent blow-up
362
363 // dL = 0 is by far the most frequent argument, so it is kept out of the map
364 auto& emissionPoint = (dL == 0) ? m_emissionPoint0 : m_emissionPoints[dL];
365 if (not emissionPoint.isSet) {
366 emissionPoint.position = m_helix.getPosition(dL);
367 emissionPoint.trackAngles = TrackAngles(m_helix.getDirection(dL));
368 emissionPoint.isSet = true;
369 }
370 return emissionPoint;
371 }
372
373 bool TOPTrack::isScanRequired(unsigned col, double time, double wid) const
374 {
375 const auto& tts = *m_tts;
376 const auto& range = m_columnHits.equal_range(col);
377 for (auto it = range.first; it != range.second; ++it) {
378 const auto hit = it->second;
379 for (const auto& gaus : tts.getTTS()) {
380 double sigsq = wid + pow(gaus.sigma, 2) + pow(hit->timeErr, 2);
381 double x = pow(hit->time - time - gaus.position, 2) / sigsq;
382 if (x < 10) return true;
383 }
384 }
385
386 return false;
387 }
388
390 {
391 if (not m_valid) return;
392 if (not tofCorrections.isValid()) return;
393 if (not tofCorrections->isCalibrated()) return;
394
395 const auto* geo = TOPGeometryPar::Instance()->getGeometry();
396 const auto& topModule = geo->getModule(m_moduleID);
397 const auto globalPosition = m_extHit->getPosition();
398 const auto position = topModule.pointGlobalToNominal(static_cast<XYZPoint>(globalPosition));
399
400 m_TOFCorrection = tofCorrections->get(position.Z());
401 }
402
403
404 } // end top namespace
406} // end Belle2 namespace
407
Provides a type-safe way to pass members of the chargedStableSet set.
Definition Const.h:590
int getPDGCode() const
PDG code.
Definition Const.h:474
static const ParticleSet chargedStableSet
set of charged stable particles
Definition Const.h:619
EDetector
Enum for identifying the detector components (detector and subdetector).
Definition Const.h:42
static const double speedOfLight
[cm/ns]
Definition Const.h:696
static const ParticleType invalidParticle
Invalid particle, used internally.
Definition Const.h:682
bool isValid() const
Check whether a valid object was obtained from the database.
Class for accessing objects in the database.
Definition DBObjPtr.h:21
Store one Ext hit as a ROOT object.
Definition ExtHit.h:31
int getPdgCode() const
Get PDG code of this extrapolation's hypothesis.
Definition ExtHit.h:117
A Class to store the Monte Carlo particle information.
Definition MCParticle.h:32
Class for type safe access to objects that are referred to in relations.
T * getRelated(const std::string &name="", const std::string &namedRelation="") const
Get the object to or from which this object has a relation.
Accessor to arrays stored in the data store.
Definition StoreArray.h:113
Class to store track parameters of incoming MC particles relation to MCParticle filled in top/simulat...
Definition TOPBarHit.h:27
unsigned getNumModules() const
Returns number of modules.
@ c_syncWindows
number of windows corresponding to syncTimeBase
const TOPGeometry * getGeometry() const
Returns pointer to geometry object using basf2 units.
static TOPGeometryPar * Instance()
Static method to obtain the pointer to its instance.
static const std::vector< BackgroundPDF > & getBackgroundPDFs()
Returns background PDF's of all modules.
static const YScanner * getYScanner(int moduleID)
Returns y-scanner of a given module.
void setTOFCorrection(const DBObjPtr< TOPCalTOFCorrection > &tofCorrections)
Sets time-of-flight correction value.
Definition TOPTrack.cc:389
double m_bkgRate
estimated background hit rate
Definition TOPTrack.h:316
DBObjPtr< TOPFrontEndSetting > m_feSetting
front-end settings
Definition TOPTrack.h:307
DBObjPtr< TOPCalModuleAlignment > m_alignment
module alignment constants
Definition TOPTrack.h:306
bool isScanRequired(unsigned col, double time, double wid) const
Checks if scan method of YScanner is needed to construct PDF for a given pixel column.
Definition TOPTrack.cc:373
const TOPTrack::AssumedEmission & getEmissionPoint(double dL=0) const
Returns assumed photon emission position and track direction.
Definition TOPTrack.cc:359
std::unordered_multimap< unsigned, const SelectedHit * > m_columnHits
selected hits mapped to pixel columns
Definition TOPTrack.h:318
const TOPNominalTTS * m_tts
PMT independent TTS, used by isScanRequired.
Definition TOPTrack.h:317
TOPTrack()
Default constructor.
Definition TOPTrack.h:104
bool xsecPrism(std::vector< double > &lengths, std::vector< ROOT::Math::XYZPoint > &positions, const RaytracerBase::Prism &prism, const ROOT::Math::Transform3D &transform)
Calculates intersection of trajectory with prism.
Definition TOPTrack.cc:275
int m_moduleID
slot ID
Definition TOPTrack.h:297
double m_TOFCorrection
time-of-flight correction
Definition TOPTrack.h:302
double m_momentum
track momentum magnitude at TOP
Definition TOPTrack.h:298
std::map< double, TOPTrack::AssumedEmission > m_emissionPoints
assumed emission points in module local frame, for dL != 0
Definition TOPTrack.h:321
bool m_valid
true for properly defined track
Definition TOPTrack.h:313
double m_pT
transverse momentum at POCA
Definition TOPTrack.h:299
bool setHelix(const ROOT::Math::Transform3D &transform)
Sets helix (helix is given in nominal frame)
Definition TOPTrack.cc:171
double m_trackLength
trajectory length from IP to average photon emission point
Definition TOPTrack.h:303
double m_charge
track charge in units of elementary charge
Definition TOPTrack.h:300
std::vector< SelectedHit > m_selectedHits
selected photon hits from TOPDigits belonging to this slot ID
Definition TOPTrack.h:315
const MCParticle * m_mcParticle
MC particle.
Definition TOPTrack.h:311
TOP::HelixSwimmer m_helix
trajectory helix in nominal slot frame
Definition TOPTrack.h:305
double m_length
trajectory length within quartz
Definition TOPTrack.h:304
void set(const Track &track, const std::string &digitsName, const Const::ChargedStable &chargedStable)
Sets the object (called by constructors)
Definition TOPTrack.cc:91
const ExtHit * m_extHit
extrapolated hit
Definition TOPTrack.h:310
TOPTrack::AssumedEmission m_emissionPoint0
assumed emission point in module local frame, at dL = 0
Definition TOPTrack.h:324
const Track * m_track
mdst track
Definition TOPTrack.h:309
double getBeta(const Const::ChargedStable &particle, double overrideMass=0) const
Returns particle beta.
Definition TOPTrack.h:329
double m_TOFLength
trajectory length corresponding to TOF of extrapolated hit
Definition TOPTrack.h:301
const TOPBarHit * m_barHit
bar hit
Definition TOPTrack.h:312
Class that bundles various TrackFitResults.
Definition Track.h:25
Class to store variables with their name which were sent to the logging service.
static void getField(const double *pos, double *field)
return the magnetic field at a given position.
double sqrt(double a)
sqrt for double
Definition beamHelpers.h:28
const TOPGeoModule & getModule(int moduleID) const
Returns module.
Abstract base class for different kinds of events.
STL namespace.
bar segment data in module local frame.
double A
width (dimension in x)
double B
thickness (dimension in y)
spherical mirror data in module local frame.
double yc
center of curvature in y
double xc
center of curvature in x
double zc
center of curvature in z
prism data in module local frame.
double A
width (dimension in x)
double yDown
minimal y of exit window
double slope
slope of slanted surface (dy/dz)
double yUp
maximal y of exit window
double zFlat
z where flat continues to slanted surface
double zR
maximal z, i.e position of prism-bar joint
double B
thickness at bar (dimension in y)
assumed photon emission point in local frame
Definition TOPTrack.h:76
selected photon hit from TOPDigits
Definition TOPTrack.h:85
Sine and cosine of track polar and azimuthal angles at assumed photon emission.
Definition TOPTrack.h:48
double sinFi
sine of azimuthal angle
Definition TOPTrack.h:52
TrackAngles()
Default constructor.
Definition TOPTrack.h:57
double cosFi
cosine of azimuthal angle
Definition TOPTrack.h:51
double cosTh
cosine of polar angle
Definition TOPTrack.h:49
double sinTh
sine of polar angle
Definition TOPTrack.h:50