8#include <tracking/trackFitting/trackBuilder/factories/TrackBuilder.h>
11#include <framework/datastore/StoreArray.h>
12#include <mdst/dataobjects/MCParticle.h>
13#include <mdst/dataobjects/HitPatternVXD.h>
14#include <mdst/dataobjects/HitPatternCDC.h>
15#include <mdst/dataobjects/Track.h>
16#include <mdst/dataobjects/TrackFitResult.h>
17#include <cdc/dataobjects/CDCRecoHit.h>
18#include <pxd/reconstruction/PXDRecoHit.h>
19#include <svd/reconstruction/SVDRecoHit.h>
20#include <svd/reconstruction/SVDRecoHit2D.h>
21#include <tracking/dataobjects/RecoTrack.h>
23#include <TMatrixDSym.h>
26#include <genfit/FitStatus.h>
27#include <genfit/KalmanFitterInfo.h>
28#include <genfit/Track.h>
29#include <genfit/TrackPoint.h>
30#include <genfit/MeasuredStateOnPlane.h>
31#include <genfit/FieldManager.h>
36 const bool useClosestHitToIP,
const bool useBFieldAtHit)
42 B2DEBUG(27, trackReps.size() <<
" track representations available.");
43 std::optional<Track> ownedTrack;
45 bool newTrackCreated =
false;
48 relatedTrack = &*ownedTrack;
49 newTrackCreated =
true;
52 bool repAlreadySet =
false;
53 unsigned int repIDPlusOne = 0;
54 for (
const auto& trackRep : trackReps) {
60 B2DEBUG(27,
"Track fitted with hypothesis that is not a ChargedStable (PDG code = " << particleType.
getPDGCode() <<
")");
66 B2DEBUG(27,
"The fit with the given track representation (" << std::abs(trackRep->getPDG()) <<
67 ") was not successful. Skipping ...");
76 if (not repAlreadySet) {
82 genfit::MeasuredStateOnPlane msop;
84 if (useClosestHitToIP) {
89 }
catch (genfit::Exception& exception) {
90 B2WARNING(exception.what());
92 }
catch (
const std::runtime_error& er) {
93 B2WARNING(
"Runtime error encountered: " << er.what());
96 B2WARNING(
"Undefined exception encountered.");
100 genfit::MeasuredStateOnPlane extrapolatedMSoP = msop;
104 B2DEBUG(29,
"Could not extrapolate the fit result for pdg " << particleType.
getPDGCode() <<
105 " to the perigee point.");
111 TVector3 poca(0., 0., 0.);
112 TVector3 dirInPoca(0., 0., 0.);
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());
123 if (useBFieldAtHit) {
124 const B2Vector3D& hitPosition = msop.getPos();
125 genfit::FieldManager::getInstance()->getFieldVal(hitPosition.
X(), hitPosition.
Y(), hitPosition.
Z(), Bx, By, Bz);
127 genfit::FieldManager::getInstance()->getFieldVal(poca.X(), poca.Y(), poca.Z(), Bx, By, Bz);
134 const auto newTrackFitResult = trackFitResults.
appendNew(
135 ROOT::Math::XYZVector(poca), ROOT::Math::XYZVector(dirInPoca), cov, charge, particleType, pValue, Bz,
136 hitPatternCDCInitializer, hitPatternVXDInitializer, nDF
139 const int newTrackFitResultArrayIndex = newTrackFitResult->getArrayIndex();
145 if (newTrackCreated) {
146 Track* addedTrack = tracks.appendNew(*relatedTrack);
151 B2DEBUG(28,
"No valid fit for any given hypothesis. No Track is added to the Tracks StoreArray.");
162 int nNotFittedVXDhits = 0;
164 for (
const auto& trackPoint : hitPointsWithMeasurements) {
166 genfit::KalmanFitterInfo* kalmanInfo = trackPoint->getKalmanFitterInfo(representation);
168 for (
size_t measurementId = 0; measurementId < trackPoint->getNumRawMeasurements(); measurementId++) {
170 genfit::AbsMeasurement* absMeas = trackPoint->getRawMeasurement(measurementId);
176 if (!pxdHit && !svdHit2D && !svdHit)
181 if (kalmanInfo->getNumMeasurements() > 1)
182 B2WARNING(
"VXD TrackPoint contains more than one KalmanFitterInfo: only the first will be considered");
184 const double weight = kalmanInfo->getWeights().at(0);
190 const int currentHits = hitPatternVXD.
getPXDLayer(layerNumber, HitPatternVXD::PXDMode::normal);
191 hitPatternVXD.
setPXDLayer(layerNumber, currentHits + 1, HitPatternVXD::PXDMode::normal);
196 const auto& currentHits = hitPatternVXD.
getSVDLayer(layerNumber);
197 hitPatternVXD.
setSVDLayer(layerNumber, currentHits.first + 1, currentHits.second + 1);
200 const auto& currentHits = hitPatternVXD.
getSVDLayer(layerNumber);
203 hitPatternVXD.
setSVDLayer(layerNumber, currentHits.first + 1, currentHits.second);
205 hitPatternVXD.
setSVDLayer(layerNumber, currentHits.first, currentHits.second + 1);
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");
231 int nNotFittedCDChits = 0;
235 for (
const auto& trackPoint : hitPointsWithMeasurements) {
237 genfit::KalmanFitterInfo* kalmanInfo = trackPoint->getKalmanFitterInfo(representation);
239 for (
size_t measurementId = 0; measurementId < trackPoint->getNumRawMeasurements(); measurementId++) {
241 genfit::AbsMeasurement* absMeas = trackPoint->getRawMeasurement(measurementId);
248 bool isValidWeight =
false;
249 for (
unsigned int kfinfoId = 0; kfinfoId < kalmanInfo->getNumMeasurements(); kfinfoId++) {
250 const double weight = kalmanInfo->getWeights().at(kfinfoId);
254 isValidWeight =
true;
255 B2DEBUG(27,
"CDC: " << nCDChits <<
"\t" << cdcHit->
getWireID().
getEWire() <<
"\t" << kfinfoId <<
"\t" << weight);
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");
DataType Z() const
access variable Z (= .at(2) without boundary check)
DataType X() const
access variable X (= .at(0) without boundary check)
DataType Y() const
access variable Y (= .at(1) without boundary check)
This class is used to transfer CDC information to the track fit.
WireID getWireID() const
Getter for WireID object.
The ParticleType class for identifying different particle types.
int getPDGCode() const
PDG code.
static const ParticleSet chargedStableSet
set of charged stable particles
Hit pattern of CDC hits within a track.
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.
Hit pattern of the VXD within a track.
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.
PXDRecoHit - an extended form of PXDCluster containing geometry information.
VxdID getSensorID() const
Get the compact ID.
static genfit::Track & getGenfitTrack(RecoTrack &recoTrack)
Give access to the RecoTrack's genfit::Track.
This is the Reconstruction Event-Data Model Track.
const std::vector< genfit::AbsTrackRep * > & getRepresentations() const
Return a list of track representations. You are not allowed to modify or delete them!
bool wasFitSuccessful(const genfit::AbsTrackRep *representation=nullptr) const
Returns true if the last fit with the given representation was successful.
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...
float getQualityIndicator() const
Get the quality index attached to this RecoTrack given by one of the reconstruction algorithms....
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...
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...
const std::vector< genfit::TrackPoint * > & getHitPointsWithMeasurement() const
Return a list of measurements and track points, which can be used e.g. to extrapolate....
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.
SVDRecoHit - an extended form of SVDHit containing geometry information.
VxdID getSensorID() const
Get the compact ID.
SVDRecoHit - an extended form of SVDHit containing geometry information.
bool isU() const
Is the coordinate u or v?
VxdID getSensorID() const
Get the compact ID.
Accessor to arrays stored in the data store.
T * appendNew()
Construct a new T object at the end of the array.
static uint32_t getHitPatternVXDInitializer(const RecoTrack &recoTrack, const genfit::AbsTrackRep *representation=nullptr)
Get the HitPattern in the VXD.
bool storeTrackFromRecoTrack(RecoTrack &recoTrack, const bool useClosestHitToIP=false, const bool useBFieldAtHit=false)
Stores a Belle2 Track from a Reco Track.
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.
Class that bundles various TrackFitResults.
const TrackFitResult * getTrackFitResult(const Const::ChargedStable &chargedStable) const
Default Access to TrackFitResults.
void setTrackFitResultIndex(const Const::ChargedStable &chargedStable, short index)
Set an index (for positive values) or unavailability-code (index = -1) for a specific mass hypothesis...
unsigned int getNumberOfFittedHypotheses() const
Returns the number of fitted hypotheses which are stored in this track.
baseType getLayerNumber() const
Get the layer id.
Class to identify a wire inside the CDC.
unsigned short getICLayer() const
Getter for continuous layer numbering.
unsigned short getEWire() const
Getter for encoded wire number.
B2Vector3< double > B2Vector3D
typedef for common usage with double
Abstract base class for different kinds of events.