Belle II Software prerelease-11-00-00a
TrackBuilder Class Reference

TrackBuilder class to create the Track/TrackFitResult mdst output from the RecoTrack. More...

#include <TrackBuilder.h>

Collaboration diagram for TrackBuilder:

Public Member Functions

 TrackBuilder (const std::string &trackColName, const std::string &trackFitResultColName, const B2Vector3D &beamSpot=B2Vector3D(0., 0., 0.), const B2Vector3D &beamAxis=B2Vector3D(0., 0., 1.))
 Constructor of the class.
 
bool storeTrackFromRecoTrack (RecoTrack &recoTrack, const bool useClosestHitToIP=false, const bool useBFieldAtHit=false)
 Stores a Belle2 Track from a Reco Track.
 

Static Public Member Functions

static uint32_t getHitPatternVXDInitializer (const RecoTrack &recoTrack, const genfit::AbsTrackRep *representation=nullptr)
 Get the HitPattern in the VXD.
 
static uint64_t getHitPatternCDCInitializer (const RecoTrack &recoTrack, const genfit::AbsTrackRep *representation=nullptr)
 Get the HitPattern in the CDC.
 

Private Attributes

std::string m_trackColName
 TrackColName (output).
 
std::string m_trackFitResultColName
 TrackFitResultColName (output).
 
B2Vector3D m_beamSpot
 Extrapolation target, origin.
 
B2Vector3D m_beamAxis
 Extrapolation target, positive z direction.
 

Detailed Description

TrackBuilder class to create the Track/TrackFitResult mdst output from the RecoTrack.

To use this class, iterate over all Reco tracks and call trackBuilder.storeTrackFromRecoTrack. All fitted hypotheses will be stored into one Track with the TrackFitResult array indices stored in the Track. for (auto& recoTrack : recoTracks) { trackBuilder.storeTrackFromRecoTrack(recoTrack); }

If you want to make sure that all hypotheses are fitted and stored, call the fitter again. If the tracks are fitted already, this produces no overhead. TrackFitter trackFitter; TrackBuilder trackBuilder; for (auto& recoTrack : recoTracks) { for (const auto& pdg : m_pdgCodes) { trackFitter.fit(recoTrack, Const::ParticleType(abs(pdg))); } trackBuilder.storeTrackFromRecoTrack(recoTrack); }

Definition at line 41 of file TrackBuilder.h.

Constructor & Destructor Documentation

◆ TrackBuilder()

TrackBuilder ( const std::string & trackColName,
const std::string & trackFitResultColName,
const B2Vector3D & beamSpot = B2Vector3D(0., 0., 0.),
const B2Vector3D & beamAxis = B2Vector3D(0., 0., 1.) )
inline

Constructor of the class.

Parameters
trackColNameName of the store array for tracks (output).
trackFitResultColNameName of the store array for track fit results (output).
beamSpotOrigin.
beamAxisPositive z-direction.

Definition at line 50 of file TrackBuilder.h.

55 :
56 m_trackColName(trackColName),
57 m_trackFitResultColName(trackFitResultColName),
58 m_beamSpot(beamSpot),
59 m_beamAxis(beamAxis)
60 {};

Member Function Documentation

◆ getHitPatternCDCInitializer()

uint64_t getHitPatternCDCInitializer ( const RecoTrack & recoTrack,
const genfit::AbsTrackRep * representation = nullptr )
static

Get the HitPattern in the CDC.

Definition at line 226 of file TrackBuilder.cc.

227{
228 HitPatternCDC hitPatternCDC;
229
230 int nCDChits = 0;
231 int nNotFittedCDChits = 0;
232
233 const auto& hitPointsWithMeasurements = recoTrack.getHitPointsWithMeasurement();
234
235 for (const auto& trackPoint : hitPointsWithMeasurements) { // Loop on TrackPoint
236
237 genfit::KalmanFitterInfo* kalmanInfo = trackPoint->getKalmanFitterInfo(representation);
238
239 for (size_t measurementId = 0; measurementId < trackPoint->getNumRawMeasurements(); measurementId++) { //Loop on raw measurement
240
241 genfit::AbsMeasurement* absMeas = trackPoint->getRawMeasurement(measurementId);
242 CDCRecoHit* cdcHit = dynamic_cast<CDCRecoHit*>(absMeas);
243
244 if (!cdcHit)
245 continue; // consider only CDC hits
246
247 if (kalmanInfo) {
248 bool isValidWeight = false;
249 for (unsigned int kfinfoId = 0; kfinfoId < kalmanInfo->getNumMeasurements(); kfinfoId++) { // Loop on KalmanFitterInfo
250 const double weight = kalmanInfo->getWeights().at(kfinfoId);
251 if (weight < 1.e-9)
252 continue; // skip kfinfo with negligible weight
253 else {
254 isValidWeight = true;
255 B2DEBUG(27, "CDC: " << nCDChits << "\t" << cdcHit->getWireID().getEWire() << "\t" << kfinfoId << "\t" << weight);
256 break;
257 }
258 } // end of KalmanFitterInfo loop
259
260 if (isValidWeight) { // fill nCDChits only one time per each raw measurement
261 WireID wire = cdcHit->getWireID();
262 hitPatternCDC.setLayer(wire.getICLayer());
263 nCDChits++; // counts CDC hits where there is KalmanFitterInfo and not negligible weight
264 }
265
266 } // end of if kalmanInfo
267 else {
268 // i.e. if !kalmanInfo)
269 ++nNotFittedCDChits; // counts TrackPoints with CDC hits without KalmanFitterInfo
270 continue;
271 }
272
273 } // end of loop on raw measurements
274 } // end of loop on TrackPoint
275
276 if (nNotFittedCDChits > 0) {
277 B2DEBUG(27, " No KalmanFitterInfo associated to some TrackPoints with CDC hits, not filling the HitPatternCDC");
278 B2DEBUG(27, nNotFittedCDChits << " out of " << nCDChits << " had no FitterInfo");
279 }
280 hitPatternCDC.setNHits(nCDChits);
281
282 return hitPatternCDC.getInteger();
283
284}
WireID getWireID() const
Getter for WireID object.
Definition CDCRecoHit.h:49
void setNHits(unsigned short nHits)
Sets the 8 MSBs to the total number of hits in the CDC.
void setLayer(const unsigned short layer)
Set bit corresponding to layer to true.
ULong64_t getInteger() const
Getter for underlying integer type.
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:708
unsigned short getICLayer() const
Getter for continuous layer numbering.
Definition WireID.cc:24
unsigned short getEWire() const
Getter for encoded wire number.
Definition WireID.h:154

◆ getHitPatternVXDInitializer()

uint32_t getHitPatternVXDInitializer ( const RecoTrack & recoTrack,
const genfit::AbsTrackRep * representation = nullptr )
static

Get the HitPattern in the VXD.

Definition at line 157 of file TrackBuilder.cc.

158{
159 HitPatternVXD hitPatternVXD;
160
161 const auto& hitPointsWithMeasurements = recoTrack.getHitPointsWithMeasurement();
162 int nNotFittedVXDhits = 0;
163
164 for (const auto& trackPoint : hitPointsWithMeasurements) { // Loop on TrackPoint
165
166 genfit::KalmanFitterInfo* kalmanInfo = trackPoint->getKalmanFitterInfo(representation);
167
168 for (size_t measurementId = 0; measurementId < trackPoint->getNumRawMeasurements(); measurementId++) { //Loop on raw measurement
169
170 genfit::AbsMeasurement* absMeas = trackPoint->getRawMeasurement(measurementId);
171
172 PXDRecoHit* pxdHit = dynamic_cast<PXDRecoHit*>(absMeas);
173 SVDRecoHit* svdHit = dynamic_cast<SVDRecoHit*>(absMeas);
174 SVDRecoHit2D* svdHit2D = dynamic_cast<SVDRecoHit2D*>(absMeas);
175
176 if (!pxdHit && !svdHit2D && !svdHit)
177 continue; // consider only VXD hits
178
179 if (kalmanInfo) {
180
181 if (kalmanInfo->getNumMeasurements() > 1)
182 B2WARNING("VXD TrackPoint contains more than one KalmanFitterInfo: only the first will be considered");
183
184 const double weight = kalmanInfo->getWeights().at(0); // only 1st KalmanFitterInfo considered
185 if (weight < 1.e-9)
186 continue; // skip kfinfo with negligible weight
187
188 if (pxdHit) {
189 const int layerNumber = pxdHit->getSensorID().getLayerNumber();
190 const int currentHits = hitPatternVXD.getPXDLayer(layerNumber, HitPatternVXD::PXDMode::normal);
191 hitPatternVXD.setPXDLayer(layerNumber, currentHits + 1, HitPatternVXD::PXDMode::normal);
192 }
193
194 if (svdHit2D) {
195 const int layerNumber = svdHit2D->getSensorID().getLayerNumber();
196 const auto& currentHits = hitPatternVXD.getSVDLayer(layerNumber);
197 hitPatternVXD.setSVDLayer(layerNumber, currentHits.first + 1, currentHits.second + 1);
198 } else if (svdHit) {
199 const int layerNumber = svdHit->getSensorID().getLayerNumber();
200 const auto& currentHits = hitPatternVXD.getSVDLayer(layerNumber);
201
202 if (svdHit->isU())
203 hitPatternVXD.setSVDLayer(layerNumber, currentHits.first + 1, currentHits.second);
204 else
205 hitPatternVXD.setSVDLayer(layerNumber, currentHits.first, currentHits.second + 1);
206 }
207
208 } // end of if kalmanInfo
209 else {
210 // i.e. if !kalmanInfo)
211 ++nNotFittedVXDhits; // counts TrackPoints with VXD hits without KalmanFitterInfo
212 continue;
213 }
214
215 } // end of loop on raw measurements
216 } // end of loop on TrackPoint
217
218 if (nNotFittedVXDhits > 0) {
219 B2DEBUG(27, " No KalmanFitterInfo associated to some TrackPoints with VXD hits, not filling the HitPatternVXD");
220 B2DEBUG(27, nNotFittedVXDhits << " had no FitterInfo");
221 }
222 return hitPatternVXD.getInteger();
223}
unsigned int getInteger() const
Getter for the underlying integer.
void setSVDLayer(const unsigned short layerId, unsigned short uHits, unsigned short vHits)
Set the number of hits in a specific layer of the SVD.
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.
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.
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.
VxdID getSensorID() const
Get the compact ID.
Definition PXDRecoHit.h:105
VxdID getSensorID() const
Get the compact ID.
bool isU() const
Is the coordinate u or v?
Definition SVDRecoHit.h:91
VxdID getSensorID() const
Get the compact ID.
Definition SVDRecoHit.h:82
baseType getLayerNumber() const
Get the layer id.
Definition VxdID.h:95

◆ storeTrackFromRecoTrack()

bool storeTrackFromRecoTrack ( RecoTrack & recoTrack,
const bool useClosestHitToIP = false,
const bool useBFieldAtHit = false )

Stores a Belle2 Track from a Reco Track.

Every fitted hypothesis will be extrapolated to the perigee and stored as a TrackFitResult when the fit and the extrapolation were successful. We will only create a Track mdst object, when we are sure that we have at least one valid hypothesis available. If we do not have this, we discard the track no matter what.

The StoreArrayIndex is stored in the Belle2 Track, no relation is set.

Parameters
recoTrackThe reco track the fit results are stored for
useClosestHitToIPFlag to turn on special handling which measurement to choose; especially useful for Cosmics
useBFieldAtHitFlag to calculate the BField at the used hit (closest to IP or first one), instead of the one at the POCA. This is also useful for cosmics only.
Returns

Definition at line 35 of file TrackBuilder.cc.

37{
38 StoreArray<Track> tracks(m_trackColName);
39 StoreArray<TrackFitResult> trackFitResults(m_trackFitResultColName);
40
41 const auto& trackReps = recoTrack.getRepresentations();
42 B2DEBUG(27, trackReps.size() << " track representations available.");
43 std::optional<Track> ownedTrack;
44 Track* relatedTrack = recoTrack.getRelatedFrom<Track>();
45 bool newTrackCreated = false;
46 if (!relatedTrack) {
47 ownedTrack.emplace(recoTrack.getQualityIndicator());
48 relatedTrack = &*ownedTrack;
49 newTrackCreated = true;
50 }
51
52 bool repAlreadySet = false;
53 unsigned int repIDPlusOne = 0;
54 for (const auto& trackRep : trackReps) {
55 repIDPlusOne++;
56
57 // Check if the fitted particle type is in our charged stable set.
58 const Const::ParticleType particleType(std::abs(trackRep->getPDG()));
59 if (not Const::chargedStableSet.contains(particleType)) {
60 B2DEBUG(27, "Track fitted with hypothesis that is not a ChargedStable (PDG code = " << particleType.getPDGCode() << ")");
61 continue;
62 }
63
64 // Check if the fit worked.
65 if (not recoTrack.wasFitSuccessful(trackRep)) {
66 B2DEBUG(27, "The fit with the given track representation (" << std::abs(trackRep->getPDG()) <<
67 ") was not successful. Skipping ...");
68 continue;
69 }
70
71 // Skip hypotheses already stored by a previous TrackBuilder call (e.g. from a prior TrackCreator module).
72 if (not newTrackCreated && relatedTrack->getTrackFitResult(particleType)) {
73 continue;
74 }
75
76 if (not repAlreadySet) {
77 RecoTrackGenfitAccess::getGenfitTrack(recoTrack).setCardinalRep(repIDPlusOne - 1);
78 repAlreadySet = true;
79 }
80
81 // Extrapolate the tracks to the perigee.
82 genfit::MeasuredStateOnPlane msop;
83 try {
84 if (useClosestHitToIP) {
85 msop = recoTrack.getMeasuredStateOnPlaneClosestTo(ROOT::Math::XYZVector(0, 0, 0), trackRep);
86 } else {
87 msop = recoTrack.getMeasuredStateOnPlaneFromFirstHit(trackRep);
88 }
89 } catch (genfit::Exception& exception) {
90 B2WARNING(exception.what());
91 continue;
92 } catch (const std::runtime_error& er) {
93 B2WARNING("Runtime error encountered: " << er.what());
94 continue;
95 } catch (...) {
96 B2WARNING("Undefined exception encountered.");
97 continue;
98 }
99
100 genfit::MeasuredStateOnPlane extrapolatedMSoP = msop;
101 try {
102 extrapolatedMSoP.extrapolateToLine(m_beamSpot, m_beamAxis);
103 } catch (...) {
104 B2DEBUG(29, "Could not extrapolate the fit result for pdg " << particleType.getPDGCode() <<
105 " to the perigee point.");
106 continue;
107 }
108
109 // Build track fit result.
110
111 TVector3 poca(0., 0., 0.);
112 TVector3 dirInPoca(0., 0., 0.);
113 TMatrixDSym cov(6);
114 extrapolatedMSoP.getPosMomCov(poca, dirInPoca, cov);
115 B2DEBUG(29, "Point of closest approach: " << poca.x() << " " << poca.y() << " " << poca.z());
116 B2DEBUG(29, "Track direction in POCA: " << dirInPoca.x() << " " << dirInPoca.y() << " " << dirInPoca.z());
117
118 const int charge = recoTrack.getTrackFitStatus(trackRep)->getCharge();
119 const double pValue = recoTrack.getTrackFitStatus(trackRep)->getPVal();
120 const double nDF = recoTrack.getTrackFitStatus(trackRep)->getNdf();
121
122 double Bx, By, Bz; // In cgs units
123 if (useBFieldAtHit) {
124 const B2Vector3D& hitPosition = msop.getPos();
125 genfit::FieldManager::getInstance()->getFieldVal(hitPosition.X(), hitPosition.Y(), hitPosition.Z(), Bx, By, Bz);
126 } else {
127 genfit::FieldManager::getInstance()->getFieldVal(poca.X(), poca.Y(), poca.Z(), Bx, By, Bz);
128 }
129 Bz = Bz / 10.; // In SI-Units
130
131 const uint64_t hitPatternCDCInitializer = getHitPatternCDCInitializer(recoTrack, trackRep);
132 const uint32_t hitPatternVXDInitializer = getHitPatternVXDInitializer(recoTrack, trackRep);
133
134 const auto newTrackFitResult = trackFitResults.appendNew(
135 ROOT::Math::XYZVector(poca), ROOT::Math::XYZVector(dirInPoca), cov, charge, particleType, pValue, Bz,
136 hitPatternCDCInitializer, hitPatternVXDInitializer, nDF
137 );
138
139 const int newTrackFitResultArrayIndex = newTrackFitResult->getArrayIndex();
140 relatedTrack->setTrackFitResultIndex(particleType, newTrackFitResultArrayIndex);
141 }
142
143 B2DEBUG(27, "Number of fitted hypothesis = " << relatedTrack->getNumberOfFittedHypotheses());
144 if (relatedTrack->getNumberOfFittedHypotheses() > 0) {
145 if (newTrackCreated) {
146 Track* addedTrack = tracks.appendNew(*relatedTrack);
147 addedTrack->addRelationTo(&recoTrack);
148 }
149 return true;
150 } else {
151 B2DEBUG(28, "No valid fit for any given hypothesis. No Track is added to the Tracks StoreArray.");
152 }
153 return true;
154}
DataType Z() const
access variable Z (= .at(2) without boundary check)
Definition B2Vector3.h:435
DataType X() const
access variable X (= .at(0) without boundary check)
Definition B2Vector3.h:431
DataType Y() const
access variable Y (= .at(1) without boundary check)
Definition B2Vector3.h:433
static const ParticleSet chargedStableSet
set of charged stable particles
Definition Const.h:618
static genfit::Track & getGenfitTrack(RecoTrack &recoTrack)
Give access to the RecoTrack's genfit::Track.
Definition RecoTrack.cc:404
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:638
bool wasFitSuccessful(const genfit::AbsTrackRep *representation=nullptr) const
Returns true if the last fit with the given representation was successful.
Definition RecoTrack.cc:336
const genfit::MeasuredStateOnPlane & getMeasuredStateOnPlaneClosestTo(const ROOT::Math::XYZVector &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:426
float getQualityIndicator() const
Get the quality index attached to this RecoTrack given by one of the reconstruction algorithms....
Definition RecoTrack.h:841
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:605
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:621
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).
FROM * getRelatedFrom(const std::string &name="", const std::string &namedRelation="") const
Get the object from which this object has a relation.
static uint32_t getHitPatternVXDInitializer(const RecoTrack &recoTrack, const genfit::AbsTrackRep *representation=nullptr)
Get the HitPattern in the VXD.
B2Vector3D m_beamSpot
Extrapolation target, origin.
std::string m_trackColName
TrackColName (output).
static uint64_t getHitPatternCDCInitializer(const RecoTrack &recoTrack, const genfit::AbsTrackRep *representation=nullptr)
Get the HitPattern in the CDC.
std::string m_trackFitResultColName
TrackFitResultColName (output).
B2Vector3D m_beamAxis
Extrapolation target, positive z direction.
const TrackFitResult * getTrackFitResult(const Const::ChargedStable &chargedStable) const
Default Access to TrackFitResults.
Definition Track.cc:30
void setTrackFitResultIndex(const Const::ChargedStable &chargedStable, short index)
Set an index (for positive values) or unavailability-code (index = -1) for a specific mass hypothesis...
Definition Track.h:188
unsigned int getNumberOfFittedHypotheses() const
Returns the number of fitted hypotheses which are stored in this track.
Definition Track.cc:36
B2Vector3< double > B2Vector3D
typedef for common usage with double
Definition B2Vector3.h:516
ROOT::Math::XYZVector poca(ROOT::Math::XYZVector const &trackPos, ROOT::Math::XYZVector const &trackP, ROOT::Math::XYZVector const &vtxPos)
Returns the Point Of Closest Approach of a track to a vertex.

Member Data Documentation

◆ m_beamAxis

B2Vector3D m_beamAxis
private

Extrapolation target, positive z direction.

Definition at line 93 of file TrackBuilder.h.

◆ m_beamSpot

B2Vector3D m_beamSpot
private

Extrapolation target, origin.

Definition at line 91 of file TrackBuilder.h.

◆ m_trackColName

std::string m_trackColName
private

TrackColName (output).

Definition at line 87 of file TrackBuilder.h.

◆ m_trackFitResultColName

std::string m_trackFitResultColName
private

TrackFitResultColName (output).

Definition at line 89 of file TrackBuilder.h.


The documentation for this class was generated from the following files: