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