Belle II Software  release-05-02-19
RecoTrack.cc
1 #include <tracking/dataobjects/RecoTrack.h>
2 #include <mdst/dataobjects/MCParticle.h>
3 
4 #include <genfit/TrackCand.h>
5 #include <genfit/AbsTrackRep.h>
6 #include <genfit/KalmanFitterInfo.h>
7 #include <genfit/KalmanFitStatus.h>
8 #include <genfit/WireTrackCandHit.h>
9 #include <genfit/RKTrackRep.h>
10 #include <genfit/MplTrackRep.h>
11 #include <simulation/monopoles/MonopoleConstants.h>
12 
13 using namespace Belle2;
14 
15 RecoTrack::RecoTrack(const TVector3& seedPosition, const TVector3& seedMomentum, const short int seedCharge,
16  const std::string& storeArrayNameOfCDCHits,
17  const std::string& storeArrayNameOfSVDHits,
18  const std::string& storeArrayNameOfPXDHits,
19  const std::string& storeArrayNameOfBKLMHits,
20  const std::string& storeArrayNameOfEKLMHits,
21  const std::string& storeArrayNameOfRecoHitInformation) :
22  m_charge(seedCharge),
23  m_storeArrayNameOfCDCHits(storeArrayNameOfCDCHits),
24  m_storeArrayNameOfSVDHits(storeArrayNameOfSVDHits),
25  m_storeArrayNameOfPXDHits(storeArrayNameOfPXDHits),
26  m_storeArrayNameOfBKLMHits(storeArrayNameOfBKLMHits),
27  m_storeArrayNameOfEKLMHits(storeArrayNameOfEKLMHits),
28  m_storeArrayNameOfRecoHitInformation(storeArrayNameOfRecoHitInformation)
29 {
30  m_genfitTrack.setStateSeed(seedPosition, seedMomentum);
31  // TODO Set the covariance seed (that should be done by the tracking package)
32  TMatrixDSym covSeed(6);
33  covSeed(0, 0) = 1e-3;
34  covSeed(1, 1) = 1e-3;
35  covSeed(2, 2) = 4e-3;
36  covSeed(3, 3) = 0.01e-3;
37  covSeed(4, 4) = 0.01e-3;
38  covSeed(5, 5) = 0.04e-3;
39  m_genfitTrack.setCovSeed(covSeed);
40 }
41 
43  StoreArray<RecoTrack>& recoTracks,
44  std::string const& pxdHitsStoreArrayName,
45  std::string const& svdHitsStoreArrayName,
46  std::string const& cdcHitsStoreArrayName,
47  std::string const& bklmHitsStoreArrayName,
48  std::string const& eklmHitsStoreArrayName,
49  std::string const& recoHitInformationStoreArrayName)
50 {
51  StoreArray<RecoHitInformation> recoHitInformations(recoHitInformationStoreArrayName);
52  recoHitInformations.registerInDataStore();
53  recoTracks.registerRelationTo(recoHitInformations);
54 
55  StoreArray<RecoHitInformation::UsedCDCHit> cdcHits(cdcHitsStoreArrayName);
56  if (cdcHits.isOptional()) {
57  cdcHits.registerRelationTo(recoTracks);
58  recoHitInformations.registerRelationTo(cdcHits);
59  }
60 
61  StoreArray<RecoHitInformation::UsedSVDHit> svdHits(svdHitsStoreArrayName);
62  if (svdHits.isOptional()) {
63  svdHits.registerRelationTo(recoTracks);
64  recoHitInformations.registerRelationTo(svdHits);
65  }
66 
67  StoreArray<RecoHitInformation::UsedPXDHit> pxdHits(pxdHitsStoreArrayName);
68  if (pxdHits.isOptional()) {
69  pxdHits.registerRelationTo(recoTracks);
70  recoHitInformations.registerRelationTo(pxdHits);
71  }
72 
73  StoreArray<RecoHitInformation::UsedBKLMHit> bklmHits(bklmHitsStoreArrayName);
74  if (bklmHits.isOptional()) {
75  bklmHits.registerRelationTo(recoTracks);
76  recoHitInformations.registerRelationTo(bklmHits);
77  }
78 
79  StoreArray<RecoHitInformation::UsedEKLMHit> eklmHits(eklmHitsStoreArrayName);
80  if (eklmHits.isOptional()) {
81  eklmHits.registerRelationTo(recoTracks);
82  recoHitInformations.registerRelationTo(eklmHits);
83  }
84 }
85 
87  const std::string& storeArrayNameOfRecoTracks,
88  const std::string& storeArrayNameOfCDCHits,
89  const std::string& storeArrayNameOfSVDHits,
90  const std::string& storeArrayNameOfPXDHits,
91  const std::string& storeArrayNameOfBKLMHits,
92  const std::string& storeArrayNameOfEKLMHits,
93  const std::string& storeArrayNameOfRecoHitInformation,
94  const bool recreateSortingParameters
95  )
96 {
97 
98  StoreArray<RecoTrack> recoTracks(storeArrayNameOfRecoTracks);
99  StoreArray<RecoHitInformation> recoHitInformations(storeArrayNameOfRecoHitInformation);
100  StoreArray<UsedCDCHit> cdcHits(storeArrayNameOfCDCHits);
101  StoreArray<UsedSVDHit> svdHits(storeArrayNameOfSVDHits);
102  StoreArray<UsedPXDHit> pxdHits(storeArrayNameOfPXDHits);
103  StoreArray<UsedBKLMHit> bklmHits(storeArrayNameOfBKLMHits);
104  StoreArray<UsedEKLMHit> eklmHits(storeArrayNameOfEKLMHits);
105 
106  // Set the tracking parameters
107  const TVector3& position = trackCand.getPosSeed();
108  const TVector3& momentum = trackCand.getMomSeed();
109  const short int charge = trackCand.getChargeSeed();
110  const double time = trackCand.getTimeSeed();
111 
112  RecoTrack* newRecoTrack = recoTracks.appendNew(position, momentum, charge,
113  cdcHits.getName(), svdHits.getName(),
114  pxdHits.getName(),
115  bklmHits.getName(), eklmHits.getName(),
116  recoHitInformations.getName());
117  newRecoTrack->setTimeSeed(time);
118 
119  // TODO Set the covariance seed (that should be done by the tracking package)
120  TMatrixDSym covSeed(6);
121  covSeed(0, 0) = 1e-3;
122  covSeed(1, 1) = 1e-3;
123  covSeed(2, 2) = 4e-3;
124  covSeed(3, 3) = 0.01e-3;
125  covSeed(4, 4) = 0.01e-3;
126  covSeed(5, 5) = 0.04e-3;
127  newRecoTrack->m_genfitTrack.setCovSeed(covSeed);
128 
129  for (unsigned int hitIndex = 0; hitIndex < trackCand.getNHits(); hitIndex++) {
130  genfit::TrackCandHit* trackCandHit = trackCand.getHit(hitIndex);
131  const int detID = trackCandHit->getDetId();
132  const int hitID = trackCandHit->getHitId();
133  const unsigned int sortingParameter = recreateSortingParameters ? hitIndex : static_cast<unsigned int>
134  (trackCandHit->getSortingParameter());
135  if (detID == Const::CDC) {
136  UsedCDCHit* cdcHit = cdcHits[hitID];
137  // Special case for CDC hits, we add a right-left information
138  const genfit::WireTrackCandHit* wireHit = dynamic_cast<const genfit::WireTrackCandHit*>(trackCandHit);
139  if (not wireHit) {
140  B2FATAL("CDC hit is not a wire hit. The RecoTrack can not handle such a case.");
141  }
142  if (wireHit->getLeftRightResolution() > 0) {
143  newRecoTrack->addCDCHit(cdcHit, sortingParameter, RecoHitInformation::RightLeftInformation::c_right);
144  } else if (wireHit->getLeftRightResolution() < 0) {
145  newRecoTrack->addCDCHit(cdcHit, sortingParameter, RecoHitInformation::RightLeftInformation::c_left);
146  } else {
147  newRecoTrack->addCDCHit(cdcHit, sortingParameter, RecoHitInformation::RightLeftInformation::c_undefinedRightLeftInformation);
148  }
149 
150  } else if (detID == Const::SVD) {
151  UsedSVDHit* svdHit = svdHits[hitID];
152  newRecoTrack->addSVDHit(svdHit, sortingParameter);
153  } else if (detID == Const::PXD) {
154  UsedPXDHit* pxdHit = pxdHits[hitID];
155  newRecoTrack->addPXDHit(pxdHit, sortingParameter);
156  } else if (detID == Const::BKLM) {
157  UsedBKLMHit* bklmHit = bklmHits[hitID];
158  newRecoTrack->addBKLMHit(bklmHit, sortingParameter);
159  } else if (detID == Const::EKLM) {
160  UsedEKLMHit* eklmHit = eklmHits[hitID];
161  newRecoTrack->addEKLMHit(eklmHit, sortingParameter);
162  }
163  }
164 
165  return newRecoTrack;
166 }
167 
169 {
170  genfit::TrackCand createdTrackCand;
171 
172  // Set the trajectory parameters
174  createdTrackCand.setCovSeed(getSeedCovariance());
175  createdTrackCand.setTimeSeed(getTimeSeed());
176 
177  // Add the hits
178  mapOnHits<UsedCDCHit>(m_storeArrayNameOfCDCHits, [&createdTrackCand](const RecoHitInformation & hitInformation,
179  const UsedCDCHit * const hit) {
180  if (hitInformation.getRightLeftInformation() == RecoHitInformation::c_left) {
181  createdTrackCand.addHit(new genfit::WireTrackCandHit(Const::CDC, hit->getArrayIndex(), -1,
182  hitInformation.getSortingParameter(), -1));
183  } else if (hitInformation.getRightLeftInformation() == RecoHitInformation::c_right) {
184  createdTrackCand.addHit(new genfit::WireTrackCandHit(Const::CDC, hit->getArrayIndex(), -1,
185  hitInformation.getSortingParameter(), 1));
186  } else {
187  createdTrackCand.addHit(new genfit::WireTrackCandHit(Const::CDC, hit->getArrayIndex(), -1,
188  hitInformation.getSortingParameter(), 0));
189  }
190  });
191  mapOnHits<UsedSVDHit>(m_storeArrayNameOfSVDHits, [&createdTrackCand](const RecoHitInformation & hitInformation,
192  const UsedSVDHit * const hit) {
193  createdTrackCand.addHit(Const::SVD, hit->getArrayIndex(), -1, hitInformation.getSortingParameter());
194  });
195  mapOnHits<UsedPXDHit>(m_storeArrayNameOfPXDHits, [&createdTrackCand](const RecoHitInformation & hitInformation,
196  const UsedPXDHit * const hit) {
197  createdTrackCand.addHit(Const::PXD, hit->getArrayIndex(), -1, hitInformation.getSortingParameter());
198  });
199  mapOnHits<UsedBKLMHit>(m_storeArrayNameOfBKLMHits, [&createdTrackCand](const RecoHitInformation & hitInformation,
200  const UsedBKLMHit * const hit) {
201  createdTrackCand.addHit(Const::BKLM, hit->getArrayIndex(), -1, hitInformation.getSortingParameter());
202  });
203  mapOnHits<UsedEKLMHit>(m_storeArrayNameOfEKLMHits, [&createdTrackCand](const RecoHitInformation & hitInformation,
204  const UsedEKLMHit * const hit) {
205  createdTrackCand.addHit(Const::EKLM, hit->getArrayIndex(), -1, hitInformation.getSortingParameter());
206  });
207 
208  createdTrackCand.sortHits();
209 
210  // Set the MC Particle
211  const MCParticle* relatedMCParticle = getRelatedTo<MCParticle>();
212  if (relatedMCParticle) {
213  createdTrackCand.setMcTrackId(relatedMCParticle->getArrayIndex());
214  }
215 
216  // Add the hits
217  return createdTrackCand;
218 }
219 
221 {
222  int createdTrackPointID = recoHitInformation->getCreatedTrackPointID();
223  if (createdTrackPointID == -1) {
224  return nullptr;
225  }
226 
227  return m_genfitTrack.getPoint(createdTrackPointID);
228 }
229 
230 size_t RecoTrack::addHitsFromRecoTrack(const RecoTrack* recoTrack, unsigned int sortingParameterOffset, bool reversed,
231  boost::optional<double> optionalMinimalWeight)
232 {
233  size_t hitsCopied = 0;
234 
235  unsigned int maximalSortingParameter = 0;
236 
237  if (reversed) {
238  const auto& recoHitInformations = recoTrack->getRecoHitInformations();
239  const auto sortBySP = [](const RecoHitInformation * lhs, const RecoHitInformation * rhs) {
240  return lhs->getSortingParameter() < rhs->getSortingParameter();
241  };
242  const auto& maximalElement = std::max_element(recoHitInformations.begin(), recoHitInformations.end(), sortBySP);
243  if (maximalElement != recoHitInformations.end()) {
244  maximalSortingParameter = (*maximalElement)->getSortingParameter();
245  }
246  }
247 
248  // Helper function to add the sorting parameter offset (or reverse the sign of the sorting parameter)
249  const auto calculateSortingParameter = [maximalSortingParameter, sortingParameterOffset](unsigned int sortingParameters) {
250  if (maximalSortingParameter > 0) {
251  return maximalSortingParameter - sortingParameters + sortingParameterOffset;
252  }
253  return sortingParameters + sortingParameterOffset;
254  };
255 
256  const auto testHitWeight = [recoTrack, optionalMinimalWeight](const RecoHitInformation * recoHitInformation) {
257  if (not optionalMinimalWeight) {
258  return true;
259  }
260  double minimalWeight = *optionalMinimalWeight;
261  const genfit::TrackPoint* trackPoint = recoTrack->getCreatedTrackPoint(recoHitInformation);
262  if (trackPoint) {
263  genfit::KalmanFitterInfo* kalmanFitterInfo = trackPoint->getKalmanFitterInfo();
264  if (not kalmanFitterInfo) {
265  return false;
266  }
267  const std::vector<double>& weights = kalmanFitterInfo->getWeights();
268  const auto checkWeight = [minimalWeight](const double weight) {
269  return weight >= minimalWeight;
270  };
271  return std::any_of(weights.begin(), weights.end(), checkWeight);
272  }
273  return true;
274  };
275 
276  for (auto* pxdHit : recoTrack->getPXDHitList()) {
277  auto recoHitInfo = recoTrack->getRecoHitInformation(pxdHit);
278  assert(recoHitInfo);
279  if (testHitWeight(recoHitInfo)) {
280  hitsCopied += addPXDHit(pxdHit, calculateSortingParameter(recoHitInfo->getSortingParameter()),
281  recoHitInfo->getFoundByTrackFinder());
282  }
283  }
284 
285  for (auto* svdHit : recoTrack->getSVDHitList()) {
286  auto recoHitInfo = recoTrack->getRecoHitInformation(svdHit);
287  assert(recoHitInfo);
288  if (testHitWeight(recoHitInfo)) {
289  hitsCopied += addSVDHit(svdHit, calculateSortingParameter(recoHitInfo->getSortingParameter()),
290  recoHitInfo->getFoundByTrackFinder());
291  }
292  }
293 
294  for (auto* cdcHit : recoTrack->getCDCHitList()) {
295  auto recoHitInfo = recoTrack->getRecoHitInformation(cdcHit);
296  assert(recoHitInfo);
297  if (testHitWeight(recoHitInfo)) {
298  hitsCopied += addCDCHit(cdcHit, calculateSortingParameter(recoHitInfo->getSortingParameter()),
299  recoHitInfo->getRightLeftInformation(),
300  recoHitInfo->getFoundByTrackFinder());
301  }
302  }
303 
304  for (auto* bklmHit : recoTrack->getBKLMHitList()) {
305  auto recoHitInfo = recoTrack->getRecoHitInformation(bklmHit);
306  assert(recoHitInfo);
307  if (testHitWeight(recoHitInfo)) {
308  hitsCopied += addBKLMHit(bklmHit, calculateSortingParameter(recoHitInfo->getSortingParameter()),
309  recoHitInfo->getFoundByTrackFinder());
310  }
311  }
312 
313  for (auto* eklmHit : recoTrack->getEKLMHitList()) {
314  auto recoHitInfo = recoTrack->getRecoHitInformation(eklmHit);
315  assert(recoHitInfo);
316  if (testHitWeight(recoHitInfo)) {
317  hitsCopied += addEKLMHit(eklmHit, calculateSortingParameter(recoHitInfo->getSortingParameter()),
318  recoHitInfo->getFoundByTrackFinder());
319  }
320  }
321 
322  return hitsCopied;
323 }
324 
325 
326 bool RecoTrack::wasFitSuccessful(const genfit::AbsTrackRep* representation) const
327 {
328  checkDirtyFlag();
329 
330  if (getRepresentations().empty()) {
331  return false;
332  }
333 
334  if (not hasTrackFitStatus(representation)) {
335  return false;
336  }
337 
338  const genfit::FitStatus* fs = getTrackFitStatus(representation);
339  if (not fs) {
340  return false;
341  }
342  if (not fs->isFitConverged()) {
343  return false;
344  }
345 
346  // make sure we only consider fitted if the Kalman method was used
347  if (not dynamic_cast<const genfit::KalmanFitStatus*>(fs)) {
348  return false;
349  }
350 
351  // make sure there is at least one hit with a valid mSoP
352  const unsigned int trackSize = m_genfitTrack.getNumPoints();
353  for (unsigned int i = 0; i < trackSize; i++) {
354  try {
355  m_genfitTrack.getFittedState(i, representation);
356  return true;
357  } catch (const genfit::Exception& exception) {
358  B2DEBUG(100, "Can not get mSoP because of: " << exception.what());
359  }
360  }
361 
362  return false;
363 }
364 
366 {
367  // "Delete" all RecoHitInfromation but the first and the last.
368  // Copy is intended!
369  std::vector<RelationEntry> relatedRecoHitInformations = getRelationsWith<RecoHitInformation>
371  std::sort(relatedRecoHitInformations.begin(), relatedRecoHitInformations.end() , [](const RelationEntry & lhs,
372  const RelationEntry & rhs) {
373  return dynamic_cast<RecoHitInformation*>(lhs.object)->getSortingParameter() > dynamic_cast<RecoHitInformation*>
374  (rhs.object)->getSortingParameter();
375  });
376 
377  // "Prune" all RecoHitInformation but the first and the last.
378  for (unsigned int i = 1; i < relatedRecoHitInformations.size() - 1; ++i) {
379  dynamic_cast<RecoHitInformation*>(relatedRecoHitInformations[i].object)->setFlag(RecoHitInformation::RecoHitFlag::c_pruned);
380  dynamic_cast<RecoHitInformation*>(relatedRecoHitInformations[i].object)->setCreatedTrackPointID(-1);
381  }
382 
383  // Genfits prune method fails, if the number of hits is too small.
384  if (getHitPointsWithMeasurement().size() >= 2) {
385  m_genfitTrack.prune("FL");
386  }
387 }
388 
390 {
391  return recoTrack.m_genfitTrack;
392 }
393 
395 {
396  // try to get the trackRep, if it has already been added
397  genfit::AbsTrackRep* trackRepresentation = recoTrack.getTrackRepresentationForPDG(std::abs(PDGcode));
398 
399  // not available? create one
400  if (trackRepresentation == nullptr) {
401  if (PDGcode == Monopoles::c_monopolePDGCode) {
402  trackRepresentation = new genfit::MplTrackRep(PDGcode, Monopoles::monopoleMagCharge);
403  } else {
404  trackRepresentation = new genfit::RKTrackRep(PDGcode);
405  }
406  RecoTrackGenfitAccess::getGenfitTrack(recoTrack).addTrackRep(trackRepresentation);
407  }
408  return trackRepresentation;
409 }
410 
412  const genfit::AbsTrackRep* representation)
413 {
414  checkDirtyFlag();
415  const unsigned int numberOfPoints = m_genfitTrack.getNumPointsWithMeasurement();
416 
417  assert(numberOfPoints > 0);
418 
419  const genfit::MeasuredStateOnPlane* nearestStateOnPlane = nullptr;
420  double minimalDistance2 = 0;
421  for (unsigned int hitIndex = 0; hitIndex < numberOfPoints; hitIndex++) {
422  try {
423  const genfit::MeasuredStateOnPlane& measuredStateOnPlane = m_genfitTrack.getFittedState(hitIndex, representation);
424 
425  const double currentDistance2 = (measuredStateOnPlane.getPos() - closestPoint).Mag2();
426 
427  if (not nearestStateOnPlane or currentDistance2 < minimalDistance2) {
428  nearestStateOnPlane = &measuredStateOnPlane;
429  minimalDistance2 = currentDistance2;
430  }
431  } catch (const genfit::Exception& exception) {
432  B2DEBUG(50, "Can not get mSoP because of: " << exception.what());
433  continue;
434  }
435  }
436  return *nearestStateOnPlane;
437 }
438 
439 
441 {
442  // Delete all fitted information for all representations
443  for (const genfit::AbsTrackRep* rep : getRepresentations()) {
445  }
446 }
447 
449 {
451 }
452 
454 {
455  if (pdgCode < 0) {
456  B2FATAL("Only positive pdgCode is possible when calling getTrackRepresentationForPDG, got " << pdgCode);
457  }
458 
459  const std::vector<genfit::AbsTrackRep*>& trackRepresentations = getRepresentations();
460 
461  for (genfit::AbsTrackRep* trackRepresentation : trackRepresentations) {
462  // Check if the track representation is a RKTrackRep.
463  const genfit::RKTrackRep* rkTrackRepresenation = dynamic_cast<const genfit::RKTrackRep*>(trackRepresentation);
464  if (rkTrackRepresenation != nullptr) {
465  // take the aboslute value of the PDG code as the TrackRep holds the PDG code including the charge (so -13 or 13)
466  if (std::abs(rkTrackRepresenation->getPDG()) == pdgCode) {
467  return trackRepresentation;
468  }
469  }
470  }
471 
472  return nullptr;
473 }
474 
475 
477 std::tuple<TVector3, TVector3, short> RecoTrack::extractTrackState() const
478 {
479  if (not wasFitSuccessful()) {
480  return std::make_tuple(getPositionSeed(), getMomentumSeed(), getChargeSeed());
481  } else {
482  const auto& measuredStateOnPlane = getMeasuredStateOnPlaneFromFirstHit();
483  return std::make_tuple(measuredStateOnPlane.getPos(), measuredStateOnPlane.getMom(), measuredStateOnPlane.getCharge());
484  }
485 }
486 
488  const TVector3& position, const TVector3& momentum, short charge,
489  const TMatrixDSym& covariance, double timeSeed) const
490 {
491  RecoTrack* newRecoTrack = storeArray.appendNew(position, momentum, charge,
494 
495  newRecoTrack->setTimeSeed(timeSeed);
496  newRecoTrack->setSeedCovariance(covariance);
497 
498  return newRecoTrack;
499 }
500 
502 {
504 }
505 
507 {
508  if (wasFitSuccessful()) {
509  const auto& mSoP = getMeasuredStateOnPlaneFromFirstHit();
510  return copyToStoreArrayUsing(storeArray, mSoP.getPos(), mSoP.getMom(), static_cast<short>(mSoP.getCharge()),
511  mSoP.get6DCov(), mSoP.getTime());
512  } else {
513  return copyToStoreArrayUsingSeeds(storeArray);
514  }
515 }
516 
517 bool RecoTrack::hasTrackFitStatus(const genfit::AbsTrackRep* representation) const
518 {
519  checkDirtyFlag();
520 
521  // there might be the case, where the genfit track has no trackreps, even not the cardinal
522  // one because no fit attempt was performed. In this case, the "hasFitStatus" call to genfit
523  // will fail with an access violation. To prevent that, check for the number of reps here before
524  // actually calling genfit's hasFitStatus(...)
525  if (m_genfitTrack.getNumReps() == 0)
526  return false;
527 
528  return m_genfitTrack.hasFitStatus(representation);
529 }
530 
531 std::vector<RecoHitInformation*> RecoTrack::getRecoHitInformations(bool getSorted) const
532 {
533  std::vector<RecoHitInformation*> hitList;
534  RelationVector<RecoHitInformation> recoHitInformations = getRelationsTo<RecoHitInformation>
536 
537  hitList.reserve(recoHitInformations.size());
538  for (auto& recoHit : recoHitInformations) {
539  // cppcheck-suppress useStlAlgorithm
540  hitList.push_back(&recoHit);
541  }
542 
543  // sort the returned vector if requested
544  if (getSorted) {
545  std::sort(hitList.begin(), hitList.end(), [](const RecoHitInformation * a,
546  const RecoHitInformation * b) -> bool {
547  return a->getSortingParameter() < b->getSortingParameter();
548  });
549  }
550 
551  return hitList;
552 }
553 
555  const genfit::AbsTrackRep* representation) const
556 {
557  checkDirtyFlag();
558 
559  if (!hasTrackFitStatus(representation)) {
560  B2FATAL("MeasuredStateOnPlane can not be retrieved for RecoTracks where no fit has been attempted.");
561  }
562 
563  if (!recoHitInfo->useInFit()) {
564  B2FATAL("MeasuredStateOnPlane cannot be provided for RecoHit which was not used in the fit.");
565  }
566 
567  const auto* hitTrackPoint = getCreatedTrackPoint(recoHitInfo);
568  if (not hitTrackPoint) {
569  B2FATAL("TrackPoint was requested which has not been created");
570  }
571 
572  const auto* fittedResult = hitTrackPoint->getFitterInfo(representation);
573  if (not fittedResult) {
574  throw NoTrackFitResult();
575  }
576 
577  return fittedResult->getFittedState();
578 }
579 
581 {
582  const unsigned int trackSize = m_genfitTrack.getNumPoints();
583  for (unsigned int i = 0; i < trackSize; i++) {
584  try {
585  return m_genfitTrack.getFittedState(i, representation);
586  } catch (const genfit::Exception& exception) {
587  B2DEBUG(50, "Can not get mSoP because of: " << exception.what());
588  }
589  }
590 
591  B2FATAL("There is no single hit with a valid mSoP in this track! Check if the fit failed with wasFitSuccessful before");
592 }
593 
595 {
596  int trackSize = m_genfitTrack.getNumPoints();
597  for (int i = -1; i >= -trackSize; i--) {
598  try {
599  return m_genfitTrack.getFittedState(i, representation);
600  } catch (const genfit::Exception& exception) {
601  B2DEBUG(50, "Can not get mSoP because of: " << exception.what());
602  }
603  }
604 
605  B2FATAL("There is no single hit with a valid mSoP in this track!");
606 }
607 
608 std::string RecoTrack::getInfoHTML() const
609 {
610  std::stringstream out;
611 
612  out << "<b>Charge seed</b>=" << getChargeSeed();
613 
614  out << "<b>pT seed</b>=" << getMomentumSeed().Pt();
615  out << ", <b>pZ seed</b>=" << getMomentumSeed().Z();
616  out << "<br>";
617  out << "<b>position seed</b>=" << getMomentumSeed().X() << ", " << getMomentumSeed().Y() << ", " << getMomentumSeed().Z();
618  out << "<br>";
619 
620  for (const genfit::AbsTrackRep* rep : getRepresentations()) {
621  out << "<b>was fitted with " << rep->getPDG() << "</b>=" << wasFitSuccessful() << ", ";
622  }
623  out << "<br>";
624 
625  return out.str();
626 }
Belle2::RelationVector::size
size_t size() const
Get number of relations.
Definition: RelationVector.h:98
Belle2::StoreArray::appendNew
T * appendNew()
Construct a new T object at the end of the array.
Definition: StoreArray.h:256
genfit::Exception
Exception class for error handling in GENFIT (provides storage for diagnostic information)
Definition: Exception.h:48
genfit::TrackPoint
Object containing AbsMeasurement and AbsFitterInfo objects.
Definition: TrackPoint.h:46
Belle2::RecoTrack::m_storeArrayNameOfEKLMHits
std::string m_storeArrayNameOfEKLMHits
Store array name of added EKLM hits.
Definition: RecoTrack.h:776
Belle2::RecoTrack::registerRequiredRelations
static void registerRequiredRelations(StoreArray< RecoTrack > &recoTracks, std::string const &pxdHitsStoreArrayName="", std::string const &svdHitsStoreArrayName="", std::string const &cdcHitsStoreArrayName="", std::string const &bklmHitsStoreArrayName="", std::string const &eklmHitsStoreArrayName="", std::string const &recoHitInformationStoreArrayName="")
Convenience method which registers all relations required to fully use a RecoTrack.
Definition: RecoTrack.cc:42
Belle2::RecoTrack::getTrackFitStatus
const genfit::FitStatus * getTrackFitStatus(const genfit::AbsTrackRep *representation=nullptr) const
Return the track fit status for the given representation or for the cardinal one. You are not allowed...
Definition: RecoTrack.h:537
Belle2::RecoTrack::createGenfitTrackCand
genfit::TrackCand createGenfitTrackCand() const
Create a genfit::TrackCand out of this reco track and copy all information to the track candidate.
Definition: RecoTrack.cc:168
Belle2::RecoTrack::addHitsFromRecoTrack
size_t addHitsFromRecoTrack(const RecoTrack *recoTrack, unsigned int sortingParameterOffset=0, bool reversed=false, boost::optional< double > optionalMinimalWeight=boost::none)
Add all hits from another RecoTrack to this RecoTrack.
Definition: RecoTrack.cc:230
Belle2::RecoTrack::copyToStoreArrayUsingSeeds
RecoTrack * copyToStoreArrayUsingSeeds(StoreArray< RecoTrack > &storeArray) const
Append a new RecoTrack to the given store array and copy its general properties, but not the hits the...
Definition: RecoTrack.cc:501
Belle2::RecoTrack::setTimeSeed
void setTimeSeed(const double timeSeed)
Set the time seed. ATTENTION: This is not the fitted time.
Definition: RecoTrack.h:520
Belle2::RecoTrack::wasFitSuccessful
bool wasFitSuccessful(const genfit::AbsTrackRep *representation=nullptr) const
Returns true if the last fit with the given representation was successful.
Definition: RecoTrack.cc:326
Belle2::StoreArray::registerRelationTo
bool registerRelationTo(const StoreArray< TO > &toArray, DataStore::EDurability durability=DataStore::c_Event, DataStore::EStoreFlags storeFlags=DataStore::c_WriteOut, const std::string &namedRelation="") const
Register a relation to the given StoreArray.
Definition: StoreArray.h:150
Belle2::RecoTrack::hasTrackFitStatus
bool hasTrackFitStatus(const genfit::AbsTrackRep *representation=nullptr) const
Check, if there is a fit status for the given representation or for the cardinal one.
Definition: RecoTrack.cc:517
Belle2::RecoTrack::extractTrackState
std::tuple< TVector3, TVector3, short > extractTrackState() const
Return the position, the momentum and the charge of the first measured state on plane or - if unfitte...
Definition: RecoTrack.cc:477
genfit::Track::deleteFittedState
void deleteFittedState(const genfit::AbsTrackRep *rep)
Delete the fit status and all the FitStates of the TrackPoints for the given hypothesis.
Definition: Track.cc:509
genfit::MeasuredStateOnPlane
#StateOnPlane with additional covariance matrix.
Definition: MeasuredStateOnPlane.h:39
genfit::TrackCand
Track candidate – seed values and indices.
Definition: TrackCand.h:69
Belle2::RecoTrackGenfitAccess::getGenfitTrack
static genfit::Track & getGenfitTrack(RecoTrack &recoTrack)
Give access to the RecoTrack's genfit::Track.
Definition: RecoTrack.cc:389
Belle2::RecoTrack::setSeedCovariance
void setSeedCovariance(const TMatrixDSym &seedCovariance)
Set the covariance of the seed. ATTENTION: This is not the fitted covariance.
Definition: RecoTrack.h:530
genfit::KalmanFitStatus
FitStatus for use with AbsKalmanFitter implementations.
Definition: KalmanFitStatus.h:36
Belle2::RecoTrack::getMeasuredStateOnPlaneFromRecoHit
const genfit::MeasuredStateOnPlane & getMeasuredStateOnPlaneFromRecoHit(const RecoHitInformation *recoHitInfo, const genfit::AbsTrackRep *representation=nullptr) const
Return genfit's MeasuredStateOnPlane on plane for associated with one RecoHitInformation.
Definition: RecoTrack.cc:554
Belle2::RecoTrack::getSVDHitList
std::vector< Belle2::RecoTrack::UsedSVDHit * > getSVDHitList() const
Return an unsorted list of svd hits.
Definition: RecoTrack.h:449
Belle2::RecoTrack::createFromTrackCand
static RecoTrack * createFromTrackCand(const genfit::TrackCand &trackCand, const std::string &storeArrayNameOfRecoTracks="", const std::string &storeArrayNameOfCDCHits="", const std::string &storeArrayNameOfSVDHits="", const std::string &storeArrayNameOfPXDHits="", const std::string &storeArrayNameOfBKLMHits="", const std::string &storeArrayNameOfEKLMHits="", const std::string &storeArrayNameOfRecoHitInformation="", const bool recreateSortingParameters=false)
Create a reco track from a genfit::TrackCand and save it to the given store array.
Definition: RecoTrack.cc:86
genfit::AbsTrackRep::getPDG
int getPDG() const
Get the pdg code.
Definition: AbsTrackRep.h:272
Belle2::CDCHit
Class containing the result of the unpacker in raw data and the result of the digitizer in simulation...
Definition: CDCHit.h:51
Belle2::RecoTrack::getMeasuredStateOnPlaneFromFirstHit
const genfit::MeasuredStateOnPlane & getMeasuredStateOnPlaneFromFirstHit(const genfit::AbsTrackRep *representation=nullptr) const
Return genfit's MeasuredStateOnPlane for the first hit in a fit useful for extrapolation of measureme...
Definition: RecoTrack.cc:580
Belle2::RecoTrack::getCDCHitList
std::vector< Belle2::RecoTrack::UsedCDCHit * > getCDCHitList() const
Return an unsorted list of cdc hits.
Definition: RecoTrack.h:446
genfit::TrackCand::getTimeSeed
double getTimeSeed() const
Get the time at which the seed state is defined.
Definition: TrackCand.h:122
genfit::TrackCand::setCovSeed
void setCovSeed(const TMatrixDSym &cov6D)
set the covariance matrix seed (6D).
Definition: TrackCand.h:175
Belle2::RecoTrack::getTrackRepresentationForPDG
genfit::AbsTrackRep * getTrackRepresentationForPDG(int pdgCode)
Return an already created track representation of the given reco track for the PDG.
Definition: RecoTrack.cc:453
Belle2::RecoHitInformation::getRightLeftInformation
RightLeftInformation getRightLeftInformation() const
Get the right-left-information.
Definition: RecoHitInformation.h:236
Belle2::RecoTrack::getInfoHTML
virtual std::string getInfoHTML() const
Get useful information on EventDisplay.
Definition: RecoTrack.cc:608
genfit::Track
Collection of TrackPoint objects, AbsTrackRep objects and FitStatus objects.
Definition: Track.h:71
genfit::TrackPoint::getKalmanFitterInfo
KalmanFitterInfo * getKalmanFitterInfo(const AbsTrackRep *rep=nullptr) const
Helper to avoid casting.
Definition: TrackPoint.cc:180
Belle2::RecoTrack::getBKLMHitList
std::vector< Belle2::RecoTrack::UsedBKLMHit * > getBKLMHitList() const
Return an unsorted list of bklm hits.
Definition: RecoTrack.h:455
genfit::TrackCandHit
Hit object for use in TrackCand.
Definition: TrackCandHit.h:34
genfit::AbsTrackRep
Abstract base class for a track representation.
Definition: AbsTrackRep.h:66
Belle2::RecoTrack::m_storeArrayNameOfCDCHits
std::string m_storeArrayNameOfCDCHits
Store array name of added CDC hits.
Definition: RecoTrack.h:768
Belle2::RecoTrack::getStoreArrayNameOfPXDHits
const std::string & getStoreArrayNameOfPXDHits() const
Name of the store array of the pxd hits.
Definition: RecoTrack.h:637
Belle2::RecoTrack::getRepresentations
const std::vector< genfit::AbsTrackRep * > & getRepresentations() const
Return a list of track representations. You are not allowed to modify or delete them!
Definition: RecoTrack.h:554
Belle2::RecoTrack::copyToStoreArray
RecoTrack * copyToStoreArray(StoreArray< RecoTrack > &storeArray) const
Append a new RecoTrack to the given store array and copy its general properties, but not the hits the...
Definition: RecoTrack.cc:506
Belle2::MCParticle::getArrayIndex
int getArrayIndex() const
Get 0-based index of the particle in the corresponding MCParticle list.
Definition: MCParticle.h:255
Belle2::EKLMAlignmentHit
This dataobject is used only for EKLM alignment.
Definition: EKLMAlignmentHit.h:39
Belle2::RecoTrack::m_genfitTrack
genfit::Track m_genfitTrack
Internal storage for the genfit track.
Definition: RecoTrack.h:764
Belle2::RecoTrack::RecoTrack
RecoTrack()
Empty constructor for ROOT. Do not use!
Definition: RecoTrack.h:137
Belle2::RecoTrack::m_storeArrayNameOfRecoHitInformation
std::string m_storeArrayNameOfRecoHitInformation
Store array of added RecoHitInformation.
Definition: RecoTrack.h:778
Belle2::RecoTrack::addCDCHit
bool addCDCHit(const UsedCDCHit *cdcHit, const unsigned int sortingParameter, RightLeftInformation rightLeftInformation=RightLeftInformation::c_undefinedRightLeftInformation, OriginTrackFinder foundByTrackFinder=OriginTrackFinder::c_undefinedTrackFinder)
Adds a cdc hit with the given information to the reco track.
Definition: RecoTrack.h:240
genfit::TrackCand::getPosSeed
TVector3 getPosSeed() const
get the seed value for track: pos.
Definition: TrackCand.h:125
Belle2::RecoTrack::getCreatedTrackPoint
const genfit::TrackPoint * getCreatedTrackPoint(const RecoHitInformation *recoHitInformation) const
Get a pointer to the TrackPoint that was created from this hit.
Definition: RecoTrack.cc:220
Belle2::RelationEntry
Struct for relations.
Definition: RelationEntry.h:26
Belle2::RecoTrack::getHitPointsWithMeasurement
const std::vector< genfit::TrackPoint * > & getHitPointsWithMeasurement() const
Return a list of measurements and track points, which can be used e.g. to extrapolate....
Definition: RecoTrack.h:607
genfit::RKTrackRep
AbsTrackRep with 5D track parameterization in plane coordinates: (q/p, u', v', u, v)
Definition: RKTrackRep.h:72
Belle2::RecoTrack::addEKLMHit
bool addEKLMHit(const UsedEKLMHit *eklmHit, const unsigned int sortingParameter, OriginTrackFinder foundByTrackFinder=OriginTrackFinder::c_undefinedTrackFinder)
Adds an eklm hit with the given information to the reco track.
Definition: RecoTrack.h:297
Belle2::RecoTrack::copyToStoreArrayUsing
RecoTrack * copyToStoreArrayUsing(StoreArray< RecoTrack > &storeArray, const TVector3 &position, const TVector3 &momentum, short charge, const TMatrixDSym &covariance, double timeSeed) const
Append a new RecoTrack to the given store array and copy its general properties, but not the hits the...
Definition: RecoTrack.cc:487
Belle2::RecoTrack::getTimeSeed
double getTimeSeed() const
Return the time seed stored in the reco track. ATTENTION: This is not the fitted time.
Definition: RecoTrack.h:500
Belle2::RecoTrack::m_storeArrayNameOfSVDHits
std::string m_storeArrayNameOfSVDHits
Store array name of added SVD hits.
Definition: RecoTrack.h:770
Belle2::RecoTrack
This is the Reconstruction Event-Data Model Track.
Definition: RecoTrack.h:78
Belle2::RecoTrack::m_storeArrayNameOfPXDHits
std::string m_storeArrayNameOfPXDHits
Store array name of added PXD hits.
Definition: RecoTrack.h:772
Belle2::RecoTrack::deleteFittedInformation
void deleteFittedInformation()
Delete all fitted information for all representations.
Definition: RecoTrack.cc:440
Belle2::RecoTrack::getPositionSeed
TVector3 getPositionSeed() const
Return the position seed stored in the reco track. ATTENTION: This is not the fitted position.
Definition: RecoTrack.h:477
Belle2::RelationVector
Class for type safe access to objects that are referred to in relations.
Definition: DataStore.h:38
genfit::KalmanFitterInfo
Collects information needed and produced by a AbsKalmanFitter implementations and is specific to one ...
Definition: KalmanFitterInfo.h:44
Belle2::RecoTrack::getStoreArrayNameOfCDCHits
const std::string & getStoreArrayNameOfCDCHits() const
Name of the store array of the cdc hits.
Definition: RecoTrack.h:631
Belle2::RecoTrack::getRecoHitInformation
RecoHitInformation * getRecoHitInformation(HitType *hit) const
Return the reco hit information for a generic hit from the storeArray.
Definition: RecoTrack.h:309
Belle2::RecoTrack::addSVDHit
bool addSVDHit(const UsedSVDHit *svdHit, const unsigned int sortingParameter, OriginTrackFinder foundByTrackFinder=OriginTrackFinder::c_undefinedTrackFinder)
Adds a svd hit with the given information to the reco track.
Definition: RecoTrack.h:269
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::RecoTrack::addBKLMHit
bool addBKLMHit(const UsedBKLMHit *bklmHit, const unsigned int sortingParameter, OriginTrackFinder foundByTrackFinder=OriginTrackFinder::c_undefinedTrackFinder)
Adds a bklm hit with the given information to the reco track.
Definition: RecoTrack.h:283
genfit::TrackCand::sortHits
void sortHits()
Sort the hits that were already added to the trackCand using the sorting parameters.
Definition: TrackCand.cc:222
genfit::Track::getFittedState
const MeasuredStateOnPlane & getFittedState(int id=0, const AbsTrackRep *rep=nullptr, bool biased=true) const
Shortcut to get FittedStates.
Definition: Track.cc:285
Belle2::RecoHitInformation
This class stores additional information to every CDC/SVD/PXD hit stored in a RecoTrack.
Definition: RecoHitInformation.h:48
Belle2::RecoTrack::getMeasuredStateOnPlaneClosestTo
const genfit::MeasuredStateOnPlane & getMeasuredStateOnPlaneClosestTo(const TVector3 &closestPoint, const genfit::AbsTrackRep *representation=nullptr)
Return genfit's MasuredStateOnPlane, that is closest to the given point useful for extrapolation of m...
Definition: RecoTrack.cc:411
Belle2::PXDCluster
The PXD Cluster class This class stores all information about reconstructed PXD clusters The position...
Definition: PXDCluster.h:41
Belle2::RecoHitInformation::useInFit
bool useInFit() const
Get the flag, whether this his should be used in a fit or not.
Definition: RecoHitInformation.h:254
genfit::MplTrackRep
Monopole track representation.
Definition: MplTrackRep.h:33
Belle2::RecoTrack::prune
void prune()
Prune the genfit track, e.g.
Definition: RecoTrack.cc:365
genfit::TrackCand::setTimeSeed
void setTimeSeed(double time)
Set the time at which the seed is defined.
Definition: TrackCand.h:172
Belle2::RecoTrackGenfitAccess::createOrReturnRKTrackRep
static genfit::AbsTrackRep * createOrReturnRKTrackRep(RecoTrack &recoTrack, int PDGcode)
Checks if a TrackRap for the PDG id of the RecoTrack (and its charge conjugate) does already exit and...
Definition: RecoTrack.cc:394
Belle2::SVDCluster
The SVD Cluster class This class stores all information about reconstructed SVD clusters.
Definition: SVDCluster.h:38
Belle2::RecoTrack::getRecoHitInformations
std::vector< RecoHitInformation * > getRecoHitInformations(bool getSorted=false) const
Return a list of all RecoHitInformations associated with the RecoTrack.
Definition: RecoTrack.cc:531
Belle2::RecoTrack::getMomentumSeed
TVector3 getMomentumSeed() const
Return the momentum seed stored in the reco track. ATTENTION: This is not the fitted momentum.
Definition: RecoTrack.h:484
genfit::Track::prune
void prune(const Option_t *="CFLWRMIU")
Delete unneeded information from the Track.
Definition: Track.cc:1045
Belle2::RecoTrack::getEKLMHitList
std::vector< Belle2::RecoTrack::UsedEKLMHit * > getEKLMHitList() const
Return an unsorted list of eklm hits.
Definition: RecoTrack.h:458
Belle2::RecoHitInformation::getSortingParameter
unsigned int getSortingParameter() const
Get the sorting parameter.
Definition: RecoHitInformation.h:224
Belle2::MCParticle
A Class to store the Monte Carlo particle information.
Definition: MCParticle.h:43
Belle2::RecoTrack::getChargeSeed
short int getChargeSeed() const
Return the charge seed stored in the reco track. ATTENTION: This is not the fitted charge.
Definition: RecoTrack.h:497
Belle2::StoreArray
Accessor to arrays stored in the data store.
Definition: ECLMatchingPerformanceExpertModule.h:33
genfit::Exception::what
virtual const char * what() const noexcept
Standard error message handling for exceptions. use like "std::cerr << e.what();".
Definition: Exception.cc:52
Belle2::RecoTrack::getStoreArrayNameOfRecoHitInformation
const std::string & getStoreArrayNameOfRecoHitInformation() const
Name of the store array of the reco hit informations.
Definition: RecoTrack.h:646
Belle2::RecoTrack::deleteFittedInformationForRepresentation
void deleteFittedInformationForRepresentation(const genfit::AbsTrackRep *rep)
Delete all fitted information for the given representations.
Definition: RecoTrack.cc:448
genfit::TrackCand::setMcTrackId
void setMcTrackId(int i)
Set the MCT track id, for MC simulations.
Definition: TrackCand.h:151
Belle2::RecoTrack::m_storeArrayNameOfBKLMHits
std::string m_storeArrayNameOfBKLMHits
Store array name of added BKLM hits.
Definition: RecoTrack.h:774
Belle2::RecoTrack::getStoreArrayNameOfEKLMHits
const std::string & getStoreArrayNameOfEKLMHits() const
Name of the store array of the eklm hits.
Definition: RecoTrack.h:643
genfit::FitStatus
Class where important numbers and properties of a fit can be stored.
Definition: FitStatus.h:80
genfit::Track::hasFitStatus
bool hasFitStatus(const AbsTrackRep *rep=nullptr) const
Check if track has a FitStatus for given AbsTrackRep. Per default, check for cardinal rep.
Definition: Track.cc:311
Belle2::RecoHitInformation::getCreatedTrackPointID
int getCreatedTrackPointID() const
Get the id of the TrackPoint related to this reco hit information in the genfit::Track.
Definition: RecoHitInformation.h:281
genfit::TrackCand::setPosMomSeed
void setPosMomSeed(const TVector3 &pos, const TVector3 &mom, const double charge)
sets the state to seed the track fitting.
Definition: TrackCand.cc:258
Belle2::BKLMHit2d
Store one BKLM strip hit as a ROOT object.
Definition: BKLMHit2d.h:42
genfit::WireTrackCandHit
Hit object for use in TrackCand.
Definition: WireTrackCandHit.h:34
genfit::TrackCand::getMomSeed
TVector3 getMomSeed() const
get the seed value for track: mom.
Definition: TrackCand.h:128
Belle2::RecoTrack::addPXDHit
bool addPXDHit(const UsedPXDHit *pxdHit, const unsigned int sortingParameter, OriginTrackFinder foundByTrackFinder=OriginTrackFinder::c_undefinedTrackFinder)
Adds a pxd hit with the given information to the reco track.
Definition: RecoTrack.h:255
Belle2::RecoTrack::checkDirtyFlag
void checkDirtyFlag() const
Helper: Check the dirty flag and produce a warning, whenever a fit result is accessed.
Definition: RecoTrack.h:896
Belle2::RecoTrack::getStoreArrayNameOfBKLMHits
const std::string & getStoreArrayNameOfBKLMHits() const
Name of the store array of the bklm hits.
Definition: RecoTrack.h:640
Belle2::RecoTrack::getSeedCovariance
const TMatrixDSym & getSeedCovariance() const
Return the covariance matrix of the seed. ATTENTION: This is not the fitted covariance.
Definition: RecoTrack.h:527
Belle2::RecoTrack::getStoreArrayNameOfSVDHits
const std::string & getStoreArrayNameOfSVDHits() const
Name of the store array of the svd hits.
Definition: RecoTrack.h:634
Belle2::RecoTrack::getMeasuredStateOnPlaneFromLastHit
const genfit::MeasuredStateOnPlane & getMeasuredStateOnPlaneFromLastHit(const genfit::AbsTrackRep *representation=nullptr) const
Return genfit's MeasuredStateOnPlane for the last hit in a fit useful for extrapolation of measuremen...
Definition: RecoTrack.cc:594
Belle2::RecoTrack::getPXDHitList
std::vector< Belle2::RecoTrack::UsedPXDHit * > getPXDHitList() const
Return an unsorted list of pxd hits.
Definition: RecoTrack.h:452