Belle II Software  release-05-02-19
TrackBuilder.cc
1 #include <tracking/trackFitting/trackBuilder/factories/TrackBuilder.h>
2 
3 
4 #include <framework/datastore/StoreArray.h>
5 #include <mdst/dataobjects/MCParticle.h>
6 #include <mdst/dataobjects/HitPatternVXD.h>
7 #include <mdst/dataobjects/HitPatternCDC.h>
8 #include <mdst/dataobjects/Track.h>
9 #include <mdst/dataobjects/TrackFitResult.h>
10 #include <cdc/dataobjects/CDCRecoHit.h>
11 #include <pxd/reconstruction/PXDRecoHit.h>
12 #include <svd/reconstruction/SVDRecoHit.h>
13 #include <svd/reconstruction/SVDRecoHit2D.h>
14 #include <tracking/dataobjects/RecoTrack.h>
15 
16 #include <TVector3.h>
17 #include <TMatrixDSym.h>
18 
19 #include <genfit/FitStatus.h>
20 #include <genfit/KalmanFitterInfo.h>
21 #include <genfit/Track.h>
22 #include <genfit/TrackPoint.h>
23 #include <genfit/MeasuredStateOnPlane.h>
24 #include <genfit/FieldManager.h>
25 
26 using namespace Belle2;
27 
29  const bool useClosestHitToIP, const bool useBFieldAtHit)
30 {
33 
34  const auto& trackReps = recoTrack.getRepresentations();
35  B2DEBUG(100, trackReps.size() << " track representations available.");
36  Track newTrack(recoTrack.getQualityIndicator());
37 
38  bool repAlreadySet = false;
39  unsigned int repIDPlusOne = 0;
40  for (const auto& trackRep : trackReps) {
41  repIDPlusOne++;
42 
43  // Check if the fitted particle type is in our charged stable set.
44  const Const::ParticleType particleType(std::abs(trackRep->getPDG()));
45  if (not Const::chargedStableSet.contains(particleType)) {
46  B2DEBUG(100, "Track fitted with hypothesis that is not a ChargedStable (PDG code = " << particleType.getPDGCode() << ")");
47  continue;
48  }
49 
50  // Check if the fit worked.
51  if (not recoTrack.wasFitSuccessful(trackRep)) {
52  B2DEBUG(100, "The fit with the given track representation (" << std::abs(trackRep->getPDG()) <<
53  ") was not successful. Skipping ...");
54  continue;
55  }
56 
57  if (not repAlreadySet) {
58  RecoTrackGenfitAccess::getGenfitTrack(recoTrack).setCardinalRep(repIDPlusOne - 1);
59  repAlreadySet = true;
60  }
61 
62  // Extrapolate the tracks to the perigee.
64  try {
65  if (useClosestHitToIP) {
66  msop = recoTrack.getMeasuredStateOnPlaneClosestTo(TVector3(0, 0, 0), trackRep);
67  } else {
68  msop = recoTrack.getMeasuredStateOnPlaneFromFirstHit(trackRep);
69  }
70  } catch (genfit::Exception& exception) {
71  B2WARNING(exception.what());
72  continue;
73  }
74 
75  genfit::MeasuredStateOnPlane extrapolatedMSoP = msop;
76  try {
77  extrapolatedMSoP.extrapolateToLine(m_beamSpot, m_beamAxis);
78  } catch (...) {
79  B2WARNING("Could not extrapolate the fit result for pdg " << particleType.getPDGCode() <<
80  " to the perigee point. Why, I don't know.");
81  continue;
82  }
83 
84  // Build track fit result.
85 
86  TVector3 poca(0., 0., 0.);
87  TVector3 dirInPoca(0., 0., 0.);
88  TMatrixDSym cov(6);
89  extrapolatedMSoP.getPosMomCov(poca, dirInPoca, cov);
90  B2DEBUG(149, "Point of closest approach: " << poca.x() << " " << poca.y() << " " << poca.z());
91  B2DEBUG(149, "Track direction in POCA: " << dirInPoca.x() << " " << dirInPoca.y() << " " << dirInPoca.z());
92 
93  const int charge = recoTrack.getTrackFitStatus(trackRep)->getCharge();
94  const double pValue = recoTrack.getTrackFitStatus(trackRep)->getPVal();
95  const int nDF = recoTrack.getTrackFitStatus(trackRep)->getNdf();
96 
97  double Bx, By, Bz; // In cgs units
98  if (useBFieldAtHit) {
99  const TVector3& hitPosition = msop.getPos();
100  genfit::FieldManager::getInstance()->getFieldVal(hitPosition.X(), hitPosition.Y(), hitPosition.Z(), Bx, By, Bz);
101  } else {
102  genfit::FieldManager::getInstance()->getFieldVal(poca.X(), poca.Y(), poca.Z(), Bx, By, Bz);
103  }
104  Bz = Bz / 10.; // In SI-Units
105 
106  const uint64_t hitPatternCDCInitializer = getHitPatternCDCInitializer(recoTrack);
107  const uint32_t hitPatternVXDInitializer = getHitPatternVXDInitializer(recoTrack);
108 
109  const auto newTrackFitResult = trackFitResults.appendNew(
110  poca, dirInPoca, cov, charge, particleType, pValue, Bz,
111  hitPatternCDCInitializer, hitPatternVXDInitializer, nDF
112  );
113 
114  const int newTrackFitResultArrayIndex = newTrackFitResult->getArrayIndex();
115  newTrack.setTrackFitResultIndex(particleType, newTrackFitResultArrayIndex);
116  }
117 
118  B2DEBUG(100, "Number of fitted hypothesis = " << newTrack.getNumberOfFittedHypotheses());
119  if (newTrack.getNumberOfFittedHypotheses() > 0) {
120  Track* addedTrack = tracks.appendNew(newTrack);
121  addedTrack->addRelationTo(&recoTrack);
122  const auto& mcParticleWithWeight = recoTrack.getRelatedToWithWeight<MCParticle>(m_mcParticleColName);
123  const MCParticle* mcParticle = mcParticleWithWeight.first;
124  if (mcParticle) {
125  B2DEBUG(200, "Relation to MCParticle set.");
126  addedTrack->addRelationTo(mcParticle, mcParticleWithWeight.second);
127  } else {
128  B2DEBUG(200, "Relation to MCParticle not set. No related MCParticle to RecoTrack.");
129  }
130  return true;
131  } else {
132  B2DEBUG(200, "Relation to MCParticle not set. No related MCParticle to RecoTrack.");
133  }
134  return true;
135 }
136 
137 
139 {
140  HitPatternVXD hitPatternVXD;
141 
142  const auto& hitPointsWithMeasurements = recoTrack.getHitPointsWithMeasurement();
143  int nNotFittedVXDhits = 0;
144 
145  for (const auto& trackPoint : hitPointsWithMeasurements) {
146 
147  for (size_t measurementId = 0; measurementId < trackPoint->getNumRawMeasurements(); measurementId++) {
148 
149  genfit::AbsMeasurement* absMeas = trackPoint->getRawMeasurement(measurementId);
150  genfit::KalmanFitterInfo* kalmanInfo = trackPoint->getKalmanFitterInfo();
151 
152  if (kalmanInfo) {
153  const double weight = kalmanInfo->getWeights().at(measurementId);
154  if (weight == 0)
155  continue;
156  } else {
157  ++nNotFittedVXDhits;
158  continue;
159  }
160 
161  PXDRecoHit* pxdHit = dynamic_cast<PXDRecoHit*>(absMeas);
162  if (pxdHit) {
163  const int layerNumber = pxdHit->getSensorID().getLayerNumber();
164  const int currentHits = hitPatternVXD.getPXDLayer(layerNumber, HitPatternVXD::PXDMode::normal);
165  hitPatternVXD.setPXDLayer(layerNumber, currentHits + 1, HitPatternVXD::PXDMode::normal);
166  }
167 
168  SVDRecoHit* svdHit = dynamic_cast<SVDRecoHit*>(absMeas);
169  SVDRecoHit2D* svdHit2D = dynamic_cast<SVDRecoHit2D*>(absMeas);
170  if (svdHit2D) {
171  const int layerNumber = svdHit2D->getSensorID().getLayerNumber();
172  const auto& currentHits = hitPatternVXD.getSVDLayer(layerNumber);
173  hitPatternVXD.setSVDLayer(layerNumber, currentHits.first + 1, currentHits.second + 1);
174  } else if (svdHit) {
175  const int layerNumber = svdHit->getSensorID().getLayerNumber();
176  const auto& currentHits = hitPatternVXD.getSVDLayer(layerNumber);
177 
178  if (svdHit->isU())
179  hitPatternVXD.setSVDLayer(layerNumber, currentHits.first + 1, currentHits.second);
180  else
181  hitPatternVXD.setSVDLayer(layerNumber, currentHits.first , currentHits.second + 1);
182  }
183 
184  }
185  }
186 
187  if (nNotFittedVXDhits > 0) {
188  B2DEBUG(100, " No KalmanFitterInfo associated to some TrackPoints with VXD hits, not filling the HitPatternVXD");
189  B2DEBUG(100, nNotFittedVXDhits << " had no FitterInfo");
190  }
191  return hitPatternVXD.getInteger();
192 }
193 
194 
196 {
197  HitPatternCDC hitPatternCDC;
198 
199  int nCDChits = 0;
200  int nNotFittedCDChits = 0;
201 
202  const auto& hitPointsWithMeasurements = recoTrack.getHitPointsWithMeasurement();
203 
204  for (const auto& trackPoint : hitPointsWithMeasurements) {
205 
206  for (size_t measurementId = 0; measurementId < trackPoint->getNumRawMeasurements(); measurementId++) {
207 
208  genfit::AbsMeasurement* absMeas = trackPoint->getRawMeasurement(measurementId);
209  genfit::KalmanFitterInfo* kalmanInfo = trackPoint->getKalmanFitterInfo();
210 
211  if (kalmanInfo) {
212  const double weight = kalmanInfo->getWeights().at(measurementId);
213  if (weight == 0)
214  continue;
215  } else {
216  ++nNotFittedCDChits;
217  continue;
218  }
219 
220  CDCRecoHit* cdcHit = dynamic_cast<CDCRecoHit*>(absMeas);
221 
222  if (cdcHit) {
223  WireID wire = cdcHit->getWireID();
224  hitPatternCDC.setLayer(wire.getICLayer());
225  nCDChits++;
226  }
227  }
228 
229  }
230  if (nNotFittedCDChits > 0) {
231  B2DEBUG(100, " No KalmanFitterInfo associated to some TrackPoints with CDC hits, not filling the HitPatternCDC");
232  B2DEBUG(100, nNotFittedCDChits << " out of " << nCDChits << " had no FitterInfo");
233  }
234  hitPatternCDC.setNHits(nCDChits);
235 
236  return hitPatternCDC.getInteger();
237 
238 }
239 
240 
genfit::FitStatus::getCharge
double getCharge() const
Get the fitted charge.
Definition: FitStatus.h:118
Belle2::StoreArray::appendNew
T * appendNew()
Construct a new T object at the end of the array.
Definition: StoreArray.h:256
Belle2::TrackBuilder::m_mcParticleColName
std::string m_mcParticleColName
MCParticleColName (input, optional).
Definition: TrackBuilder.h:104
genfit::Exception
Exception class for error handling in GENFIT (provides storage for diagnostic information)
Definition: Exception.h:48
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::WireID
Class to identify a wire inside the CDC.
Definition: WireID.h:44
Belle2::SVDRecoHit
SVDRecoHit - an extended form of SVDHit containing geometry information.
Definition: SVDRecoHit.h:57
Belle2::RecoTrack::getQualityIndicator
float getQualityIndicator() const
Get the quality index attached to this RecoTrack given by one of the reconstruction algorithms....
Definition: RecoTrack.h:730
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::Track::getNumberOfFittedHypotheses
unsigned int getNumberOfFittedHypotheses() const
Returns the number of fitted hypothesis which are stored in this track.
Definition: Track.cc:31
genfit::MeasuredStateOnPlane
#StateOnPlane with additional covariance matrix.
Definition: MeasuredStateOnPlane.h:39
Belle2::RecoTrackGenfitAccess::getGenfitTrack
static genfit::Track & getGenfitTrack(RecoTrack &recoTrack)
Give access to the RecoTrack's genfit::Track.
Definition: RecoTrack.cc:389
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::TrackBuilder::m_trackFitResultColName
std::string m_trackFitResultColName
TrackFitResultColName (output).
Definition: TrackBuilder.h:102
genfit::FieldManager::getFieldVal
TVector3 getFieldVal(const TVector3 &position)
This does NOT use the cache!
Definition: FieldManager.h:63
Belle2::Const::chargedStableSet
static const ParticleSet chargedStableSet
set of charged stable particles
Definition: Const.h:494
Belle2::RelationsInterface::addRelationTo
void addRelationTo(const RelationsInterface< BASE > *object, float weight=1.0, const std::string &namedRelation="") const
Add a relation from this object to another object (with caching).
Definition: RelationsObject.h:144
Belle2::Const::ParticleType::getPDGCode
int getPDGCode() const
PDG code.
Definition: Const.h:349
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::PXDRecoHit
PXDRecoHit - an extended form of PXDCluster containing geometry information.
Definition: PXDRecoHit.h:61
Belle2::CDCRecoHit
This class is used to transfer CDC information to the track fit.
Definition: CDCRecoHit.h:43
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
Belle2::TrackBuilder::m_beamAxis
TVector3 m_beamAxis
Extrapolation target, positive z direction.
Definition: TrackBuilder.h:108
genfit::AbsMeasurement
Contains the measurement and covariance in raw detector coordinates.
Definition: AbsMeasurement.h:42
Belle2::RecoTrack
This is the Reconstruction Event-Data Model Track.
Definition: RecoTrack.h:78
Belle2::TrackBuilder::getHitPatternCDCInitializer
static uint64_t getHitPatternCDCInitializer(const RecoTrack &recoTrack)
Get the HitPattern in the CDC.
Definition: TrackBuilder.cc:195
genfit::FitStatus::getPVal
virtual double getPVal() const
Get the p value of the fit.
Definition: FitStatus.h:128
Belle2::HitPatternVXD::getSVDLayer
std::pair< const unsigned short, const unsigned short > getSVDLayer(const unsigned short layerId) const
Get the number of hits in a specific layer of the SVD.
Definition: HitPatternVXD.cc:65
Belle2::CDCRecoHit::getWireID
WireID getWireID() const
Getter for WireID object.
Definition: CDCRecoHit.h:60
Belle2::RelationsInterface::getRelatedToWithWeight
std::pair< TO *, float > getRelatedToWithWeight(const std::string &name="", const std::string &namedRelation="") const
Get first related object & weight of relation pointing to an array.
Definition: RelationsObject.h:299
genfit::KalmanFitterInfo
Collects information needed and produced by a AbsKalmanFitter implementations and is specific to one ...
Definition: KalmanFitterInfo.h:44
Belle2::SVDRecoHit2D::getSensorID
VxdID getSensorID() const
Get the compact ID.
Definition: SVDRecoHit2D.h:109
Belle2::Track::setTrackFitResultIndex
void setTrackFitResultIndex(const Const::ChargedStable &chargedStable, short index)
Set an index (for positive values) or unavailability-code (with negative values) for a specific mass ...
Definition: Track.h:104
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::HitPatternVXD::setPXDLayer
void setPXDLayer(const unsigned short layerId, unsigned short nHits, const PXDMode &mode=PXDMode::normal)
Set the number of hits in a specific layer of the PXD.
Definition: HitPatternVXD.cc:122
Belle2::HitPatternCDC::setNHits
void setNHits(unsigned short nHits)
Sets the 8 MSBs to the total number of hits in the CDC.
Definition: HitPatternCDC.cc:55
Belle2::SVDRecoHit::isU
bool isU() const
Is the coordinate u or v?
Definition: SVDRecoHit.h:100
Belle2::HitPatternVXD::setSVDLayer
void setSVDLayer(const unsigned short layerId, unsigned short uHits, unsigned short vHits)
Set the number of hits in a specific layer of the SVD.
Definition: HitPatternVXD.cc:51
Belle2::TrackBuilder::m_trackColName
std::string m_trackColName
TrackColName (output).
Definition: TrackBuilder.h:100
Belle2::TrackBuilder::getHitPatternVXDInitializer
static uint32_t getHitPatternVXDInitializer(const RecoTrack &recoTrack)
Get the HitPattern in the VXD.
Definition: TrackBuilder.cc:138
Belle2::HitPatternVXD::getPXDLayer
unsigned short getPXDLayer(const unsigned short layerId, const PXDMode &mode=PXDMode::normal) const
Get the number of hits in a specific layer of the PXD.
Definition: HitPatternVXD.cc:133
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::PXDRecoHit::getSensorID
VxdID getSensorID() const
Get the compact ID.
Definition: PXDRecoHit.h:111
Belle2::RelationsInterface::getArrayIndex
int getArrayIndex() const
Returns this object's array index (in StoreArray), or -1 if not found.
Definition: RelationsObject.h:387
genfit::FitStatus::getNdf
double getNdf() const
Get the degrees of freedom of the fit.
Definition: FitStatus.h:122
Belle2::SVDRecoHit2D
SVDRecoHit - an extended form of SVDHit containing geometry information.
Definition: SVDRecoHit2D.h:56
Belle2::HitPatternCDC::setLayer
void setLayer(const unsigned short layer)
Set bit corresponding to layer to true.
Definition: HitPatternCDC.cc:80
Belle2::TrackBuilder::m_beamSpot
TVector3 m_beamSpot
Extrapolation target, origin.
Definition: TrackBuilder.h:106
Belle2::Const::ParticleType
The ParticleType class for identifying different particle types.
Definition: Const.h:284
Belle2::HitPatternCDC::getInteger
ULong64_t getInteger() const
Getter for underlying integer type.
Definition: HitPatternCDC.cc:71
Belle2::Track
Class that bundles various TrackFitResults.
Definition: Track.h:35
Belle2::MCParticle
A Class to store the Monte Carlo particle information.
Definition: MCParticle.h:43
genfit::KalmanFitterInfo::getWeights
std::vector< double > getWeights() const
Get weights of measurements.
Definition: KalmanFitterInfo.cc:168
Belle2::StoreArray
Accessor to arrays stored in the data store.
Definition: ECLMatchingPerformanceExpertModule.h:33
Belle2::VxdID::getLayerNumber
baseType getLayerNumber() const
Get the layer id.
Definition: VxdID.h:106
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
genfit::FieldManager::getInstance
static FieldManager * getInstance()
Get singleton instance.
Definition: FieldManager.h:119
Belle2::WireID::getICLayer
unsigned short getICLayer() const
Getter for continuous layer numbering.
Definition: WireID.cc:26
Belle2::HitPatternVXD::getInteger
unsigned int getInteger() const
Getter for the underlying integer.
Definition: HitPatternVXD.h:65
Belle2::SVDRecoHit::getSensorID
VxdID getSensorID() const
Get the compact ID.
Definition: SVDRecoHit.h:91
Belle2::TrackBuilder::storeTrackFromRecoTrack
bool storeTrackFromRecoTrack(RecoTrack &recoTrack, const bool useClosestHitToIP=false, const bool useBFieldAtHit=false)
Stores a Belle2 Track from a Reco Track.
Definition: TrackBuilder.cc:28
Belle2::HitPatternVXD
Hit pattern of the VXD within a track.
Definition: HitPatternVXD.h:44
Belle2::HitPatternCDC
Hit pattern of CDC hits within a track.
Definition: HitPatternCDC.h:45