Commit 78258ba4 authored by Frank Winklmeier's avatar Frank Winklmeier
Browse files

Merge branch 'refactor/gx2f_internal_memory' into 'master'

Replace the custom memory management infrastructure in GlobalChi2Fitter

See merge request !35388
parents c149e050 dd4b4a6d
......@@ -28,32 +28,41 @@ namespace Trk {
class GXFTrackState {
public:
GXFTrackState();
GXFTrackState() = delete;
GXFTrackState(GXFTrackState &);
~GXFTrackState();
GXFTrackState(const MeasurementBase *, const TrackParameters * trackpar = nullptr, bool ownmb = false);
GXFTrackState(const TrackParameters *);
GXFTrackState(GXFMaterialEffects *, const TrackParameters * trackpar = nullptr);
GXFTrackState & operator=(GXFTrackState & rhs);
GXFTrackState(std::unique_ptr<const MeasurementBase>, std::unique_ptr<const TrackParameters>);
GXFTrackState(const TrackParameters *, TrackState::TrackStateType = TrackState::Hole);
GXFTrackState(GXFMaterialEffects *, const TrackParameters * trackpar);
GXFTrackState & operator=(GXFTrackState &) = delete;
void setMeasurement(std::unique_ptr<const MeasurementBase>);
const MeasurementBase *measurement(void);
const MeasurementBase *takeMeasurement(void);
void setMeasurement(const MeasurementBase *);
const MeasurementBase *measurement(bool takeownership = false);
TrackState::TrackStateType trackStateType();
void setTrackStateType(TrackState::TrackStateType);
void setTrackParameters(const TrackParameters *);
const TrackParameters *trackParameters(bool takeownership = false);
void setMaterialEffects(GXFMaterialEffects *);
void setTrackParameters(std::unique_ptr<const TrackParameters>);
const TrackParameters *trackParameters(void);
const TrackParameters *takeTrackParameters(void);
GXFMaterialEffects *materialEffects();
const Surface *surface();
void setJacobian(TransportJacobian *);
void setJacobian(TransportJacobian &);
Eigen::Matrix<double, 5, 5> & jacobian();
Amg::MatrixX & derivatives();
void setDerivatives(Amg::MatrixX &);
AmgSymMatrix(5) * trackCovariance(bool takeownership = false);
void setTrackCovariance(AmgSymMatrix(5) *);
const FitQualityOnSurface *fitQuality(bool takeownership = false);
void setFitQuality(const FitQualityOnSurface *);
AmgSymMatrix(5) & trackCovariance(void);
bool hasTrackCovariance(void);
void zeroTrackCovariance(void);
void setFitQuality(std::unique_ptr<const FitQualityOnSurface>);
const FitQualityOnSurface *fitQuality(void);
const FitQualityOnSurface *takeFitQuality(void);
TrackState::MeasurementType measurementType();
void setMeasurementType(TrackState::MeasurementType);
......@@ -75,24 +84,26 @@ namespace Trk {
void setMeasuresPhi(bool);
private:
const MeasurementBase *m_measurement; //!< The measurement defining the track state
std::unique_ptr<const MeasurementBase> m_measurement; //!< The measurement defining the track state
TrackState::TrackStateType m_tsType; //!< type of track state, eg Fittable, Outlier, Scatterer, Brem, Hole
const TrackParameters *m_trackpar; //!< track parameters
GXFMaterialEffects *m_materialEffects; //!< Material effects on track (ie scatterer, brem)
std::unique_ptr<const TrackParameters> m_trackpar; //!< track parameters
std::unique_ptr<GXFMaterialEffects> m_materialEffects; //!< Material effects on track (ie scatterer, brem)
Eigen::Matrix<double, 5, 5> m_jacobian; //!< Transport jacobian wrt previous state
Amg::MatrixX * m_derivs; //!< Derivatives of local parameters wrt fit parameters
AmgSymMatrix(5) * m_covariancematrix; //!< Covariance matrix of track parameters at this surface
const FitQualityOnSurface *m_fitqual;
Amg::MatrixX m_derivs; //!< Derivatives of local parameters wrt fit parameters
AmgSymMatrix(5) m_covariancematrix; //!< Covariance matrix of track parameters at this surface
bool m_covariance_set;
std::unique_ptr<const FitQualityOnSurface> m_fitqual;
double m_measerror[5]; //!< Measurement errors (corrected for stereo angle)
double m_sinstereo; //!< stereo angle
TrackState::MeasurementType m_mType; //!< Measurement type, eg pixel, SCT, ...
bool m_recalib; //!< Has this measurement already been recalibrated?
bool m_owntp;
bool m_ownmb;
bool m_ownfq;
bool m_owncov;
bool m_measphi;
Amg::Vector3D m_globpos;
public:
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
};
inline Eigen::Matrix<double, 5, 5> & GXFTrackState::jacobian() {
......@@ -100,25 +111,23 @@ namespace Trk {
}
inline Amg::MatrixX & GXFTrackState::derivatives() {
return *m_derivs;
return m_derivs;
}
inline AmgSymMatrix(5) * GXFTrackState::trackCovariance(bool takeownership) {
AmgSymMatrix(5) * tmpcov = m_covariancematrix;
if (takeownership) {
m_owncov = false;
}
return tmpcov;
inline AmgSymMatrix(5) & GXFTrackState::trackCovariance(void) {
return m_covariancematrix;
}
inline const TrackParameters *GXFTrackState::trackParameters(bool takeownership) {
if (takeownership)
m_owntp = false;
return m_trackpar;
inline const TrackParameters *GXFTrackState::trackParameters(void) {
return m_trackpar.get();
}
inline const TrackParameters *GXFTrackState::takeTrackParameters(void) {
return m_trackpar.get();
}
inline GXFMaterialEffects *GXFTrackState::materialEffects() {
return m_materialEffects;
return m_materialEffects.get();
}
inline TrackState::MeasurementType GXFTrackState::measurementType() {
......
......@@ -6,6 +6,7 @@
#define TRK_GXFTRAJECTORY_H
#include "TrkGlobalChi2Fitter/GXFTrackState.h"
#include "TrkGeometry/MagneticFieldProperties.h"
namespace Trk {
class MeasurementBase;
......@@ -30,8 +31,7 @@ namespace Trk {
GXFTrajectory & operator=(GXFTrajectory & rhs);
bool addMeasurementState(GXFTrackState *, int index = -1);
void addHoleState(const TrackParameters *);
void addMaterialState(GXFTrackState *, int index = -1, bool owntp = false);
void addMaterialState(GXFTrackState *, int index = -1);
void setReferenceParameters(const TrackParameters *);
void setScatteringAngles(std::vector < std::pair < double, double > >&);
......@@ -73,9 +73,9 @@ namespace Trk {
bool converged();
int prefit();
void resetReferenceParameters();
double chi2();
double chi2() const;
double prevchi2();
int nDOF();
int nDOF() const;
Amg::VectorX & residuals();
Amg::VectorX & errors();
......@@ -90,8 +90,11 @@ namespace Trk {
std::vector < std::pair < const Layer *,
const Layer *>>&upstreamMaterialLayers();
void resetCovariances(void);
std::unique_ptr<const FitQuality> quality(void) const;
bool m_straightline;
MagneticFieldProperties *m_fieldprop;
MagneticFieldProperties m_fieldprop = Trk::FullField;
private:
std::vector < GXFTrackState * >m_states; //!< The vector of track states, i.e. measurements, scatterers, brem points, and holes
......
......@@ -526,12 +526,12 @@ namespace Trk {
void calculateTrackErrors(GXFTrajectory &, Amg::SymMatrixX &, bool) const;
TransportJacobian *numericalDerivatives(
std::unique_ptr<TransportJacobian> numericalDerivatives(
const EventContext& ctx,
const TrackParameters *,
const Surface *,
PropDirection,
const MagneticFieldProperties *
const MagneticFieldProperties
) const;
virtual int iterationsOfLastFit() const;
......@@ -604,7 +604,6 @@ namespace Trk {
double m_scalefactor;
bool m_redoderivs;
bool m_reintoutl;
TrackFitInputPreparator *m_inputPreparator;
int m_maxit;
bool m_acceleration;
bool m_numderiv;
......@@ -615,8 +614,6 @@ namespace Trk {
bool m_useCaloTG = false;
bool m_rejectLargeNScat = false;
MagneticFieldProperties *m_fieldpropnofield;
MagneticFieldProperties *m_fieldpropfullfield;
ParticleMasses m_particleMasses;
/*
......
......@@ -19,42 +19,18 @@
// #include <string>
namespace Trk {
GXFTrackState::GXFTrackState():
m_measurement(nullptr),
m_tsType(TrackState::Fittable),
m_trackpar(nullptr),
m_materialEffects(nullptr),
m_jacobian {},
m_derivs(nullptr),
m_covariancematrix(nullptr),
m_fitqual(nullptr),
m_sinstereo(0),
m_mType(TrackState::unidentified),
m_recalib(false),
m_owntp(false),
m_ownmb(false),
m_ownfq(false),
m_owncov(false),
m_measphi(false)
{
m_measerror[0] = m_measerror[1] = m_measerror[2] = m_measerror[3] = m_measerror[4] = -1;
}
GXFTrackState::GXFTrackState(GXFTrackState & rhs):
m_measurement(rhs.m_measurement != nullptr ? (rhs.m_ownmb ? rhs.m_measurement->clone() : rhs.m_measurement) : nullptr),
m_measurement(rhs.m_measurement != nullptr ? std::unique_ptr<const MeasurementBase>(rhs.m_measurement->clone()) : nullptr),
m_tsType(rhs.m_tsType),
m_trackpar(rhs.m_trackpar),
m_materialEffects(rhs.m_materialEffects != nullptr ? new GXFMaterialEffects(*rhs. m_materialEffects) : nullptr),
m_derivs(rhs.m_derivs != nullptr ? new Amg::MatrixX(*rhs.m_derivs) : nullptr),
m_covariancematrix(rhs.m_covariancematrix != nullptr ? (rhs.m_owncov ? new AmgSymMatrix(5) (*rhs.m_covariancematrix) : rhs.m_covariancematrix) : nullptr),
m_fitqual(rhs.m_fitqual != nullptr ? (rhs.m_ownfq ? new FitQualityOnSurface(*rhs.m_fitqual) : rhs.m_fitqual) : nullptr),
m_trackpar(std::unique_ptr<const TrackParameters>(rhs.m_trackpar != nullptr ? rhs.m_trackpar->clone() : nullptr)),
m_materialEffects(std::unique_ptr<GXFMaterialEffects>(rhs.m_materialEffects != nullptr ? new GXFMaterialEffects(*rhs. m_materialEffects) : nullptr)),
m_derivs(rhs.m_derivs),
m_covariancematrix(rhs.m_covariancematrix),
m_covariance_set(rhs.m_covariance_set),
m_fitqual(rhs.m_fitqual ? std::make_unique<const FitQualityOnSurface>(*rhs.m_fitqual) : nullptr),
m_sinstereo(rhs.m_sinstereo),
m_mType(rhs.m_mType),
m_recalib(rhs.m_recalib),
m_owntp(false),
m_ownmb(rhs.m_ownmb),
m_ownfq(rhs.m_ownfq),
m_owncov(rhs.m_owncov),
m_measphi(rhs.m_measphi)
{
for (int i = 0; i < 5; i++) {
......@@ -66,47 +42,41 @@ namespace Trk {
}
GXFTrackState::GXFTrackState(
const MeasurementBase * measurement,
const TrackParameters * trackpar,
bool ownmb
std::unique_ptr<const MeasurementBase> measurement,
std::unique_ptr<const TrackParameters> trackpar
):
m_measurement(measurement),
m_measurement(std::move(measurement)),
m_tsType(TrackState::Fittable),
m_trackpar(trackpar),
m_trackpar(std::move(trackpar)),
m_materialEffects(nullptr),
m_jacobian {},
m_derivs(nullptr),
m_covariancematrix(nullptr),
m_derivs(),
m_covariancematrix(),
m_covariance_set(false),
m_fitqual(nullptr),
m_sinstereo(0),
m_mType(TrackState::unidentified),
m_recalib(false),
m_owntp(false),
m_ownmb(ownmb),
m_ownfq(false),
m_owncov(false),
m_measphi(false) {
m_measerror[0] = m_measerror[1] = m_measerror[2] = m_measerror[3] = m_measerror[4] = -1;
}
GXFTrackState::GXFTrackState(
const TrackParameters * trackpar
const TrackParameters * trackpar,
TrackState::TrackStateType tsType
):
m_measurement(nullptr),
m_tsType(TrackState::Hole),
m_trackpar(trackpar),
m_tsType(tsType),
m_trackpar(std::unique_ptr<const TrackParameters>(trackpar != nullptr ? trackpar->clone() : nullptr)),
m_materialEffects(nullptr),
m_jacobian {},
m_derivs(nullptr),
m_covariancematrix(nullptr),
m_derivs(),
m_covariancematrix(),
m_covariance_set(false),
m_fitqual(nullptr),
m_sinstereo(0),
m_mType(TrackState::unidentified),
m_recalib(false),
m_owntp(false),
m_ownmb(false),
m_ownfq(false),
m_owncov(false),
m_measphi(false)
{
m_measerror[0] = m_measerror[1] = m_measerror[2] = m_measerror[3] = m_measerror[4] = -1;
......@@ -118,19 +88,16 @@ namespace Trk {
):
m_measurement(nullptr),
m_tsType(TrackState::Scatterer),
m_trackpar(trackpar),
m_materialEffects(mef),
m_trackpar(std::unique_ptr<const TrackParameters>(trackpar != nullptr ? trackpar->clone() : nullptr)),
m_materialEffects(std::unique_ptr<GXFMaterialEffects>(mef)),
m_jacobian {},
m_derivs(nullptr),
m_covariancematrix(nullptr),
m_derivs(),
m_covariancematrix(),
m_covariance_set(false),
m_fitqual(nullptr),
m_sinstereo(0),
m_mType(TrackState::unidentified),
m_recalib(false),
m_owntp(false),
m_ownmb(false),
m_ownfq(false),
m_owncov(false),
m_measphi(false)
{
m_measerror[0] = m_measerror[1] = m_measerror[2] = m_measerror[3] = m_measerror[4] = -1;
......@@ -140,130 +107,30 @@ namespace Trk {
}
}
Trk::GXFTrackState::~GXFTrackState() {
if (m_ownmb) {
delete m_measurement;
}
if (m_owntp) {
delete m_trackpar;
}
delete m_materialEffects;
if (m_owncov) {
delete m_covariancematrix;
}
if (m_ownfq) {
delete m_fitqual;
}
delete m_derivs;
}
GXFTrackState & GXFTrackState::operator =(GXFTrackState & rhs) {
if (this != &rhs) {
if (m_ownmb) {
delete m_measurement;
}
if (m_owntp) {
delete m_trackpar;
}
m_measurement = rhs.m_measurement != nullptr ? (rhs.m_ownmb ? rhs.m_measurement->clone() : rhs.m_measurement) : nullptr;
m_tsType = rhs.m_tsType;
m_trackpar = rhs.m_trackpar;
m_materialEffects = rhs.m_materialEffects != nullptr ? new GXFMaterialEffects(*rhs.m_materialEffects) : nullptr;
m_derivs = rhs.m_derivs != nullptr ? new Amg::MatrixX(*rhs.m_derivs) : nullptr;
delete m_covariancematrix;
m_covariancematrix = (
rhs.m_covariancematrix != nullptr ? (
rhs.m_owncov ?
new AmgSymMatrix(5) (*rhs.m_covariancematrix) :
rhs.m_covariancematrix
) : nullptr
);
m_fitqual = rhs.m_fitqual != nullptr ? (rhs.m_ownfq ? new FitQualityOnSurface(*rhs.m_fitqual) : rhs.m_fitqual) : nullptr;
m_sinstereo = rhs.m_sinstereo;
m_mType = rhs.m_mType;
m_recalib = rhs.m_recalib;
m_owntp = false;
m_ownmb = rhs.m_ownmb;
m_ownfq = rhs.m_ownfq;
m_owncov = rhs.m_owncov;
m_measphi = rhs.m_measphi;
for (int i = 0; i < 5; i++) {
m_measerror[i] = rhs.m_measerror[i];
for (int j = 0; j < 5; j++) {
m_jacobian(i, j) = rhs.m_jacobian(i, j);
}
}
}
return *this;
}
void GXFTrackState::setMeasurement(const MeasurementBase * meas) {
if (m_ownmb) {
delete m_measurement;
}
m_measurement = meas;
void GXFTrackState::setMeasurement(std::unique_ptr<const MeasurementBase> meas) {
m_measurement = std::move(meas);
m_recalib = true;
m_ownmb = true;
}
const MeasurementBase *GXFTrackState::measurement(bool takeownership) {
const MeasurementBase *meas = m_measurement;
if ((meas != nullptr) && !m_ownmb && takeownership) {
meas = meas->clone();
}
if (takeownership) {
m_ownmb = false;
}
return meas;
const MeasurementBase *GXFTrackState::measurement(void) {
return m_measurement.get();
}
const MeasurementBase *GXFTrackState::takeMeasurement(void) {
return m_measurement.get();
}
void
GXFTrackState::setTrackParameters(const TrackParameters * par) {
if ((m_trackpar != nullptr) && m_owntp) {
delete m_trackpar;
}
m_trackpar = par;
m_owntp = true;
void GXFTrackState::setTrackParameters(std::unique_ptr<const TrackParameters> par) {
m_trackpar = std::move(par);
}
void
GXFTrackState::setMaterialEffects(GXFMaterialEffects * mef) {
delete m_materialEffects;
m_materialEffects = mef;
}
void
GXFTrackState::setJacobian(TransportJacobian * jac) {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
m_jacobian(i, j) = (*jac)(i, j);
}
}
delete jac;
void GXFTrackState::setJacobian(TransportJacobian & jac) {
m_jacobian = jac;
}
void
GXFTrackState::setDerivatives(Amg::MatrixX & deriv) {
delete m_derivs;
m_derivs = new Amg::MatrixX(deriv);
m_derivs = deriv;
}
void
......@@ -308,29 +175,24 @@ namespace Trk {
void
GXFTrackState::setTrackCovariance(AmgSymMatrix(5) * covmat) {
if ((m_covariancematrix != nullptr) && m_owncov) {
delete m_covariancematrix;
}
m_covariancematrix = covmat;
if (covmat != nullptr) {
m_owncov = true;
if (covmat == nullptr) {
m_covariance_set = false;
} else {
m_covariance_set = true;
m_covariancematrix = *covmat;
}
}
void
GXFTrackState::setFitQuality(const FitQualityOnSurface * fitqual) {
if ((m_fitqual != nullptr) && m_ownfq) {
delete m_fitqual;
}
m_fitqual = fitqual;
m_ownfq = true;
void GXFTrackState::setFitQuality(std::unique_ptr<const FitQualityOnSurface> fitqual) {
m_fitqual = std::move(fitqual);
}
const FitQualityOnSurface *GXFTrackState::fitQuality(bool takeownership) {
if (takeownership) {
m_ownfq = false;
}
return m_fitqual;
const FitQualityOnSurface *GXFTrackState::fitQuality(void) {
return m_fitqual.get();
}
const FitQualityOnSurface *GXFTrackState::takeFitQuality(void) {
return m_fitqual.get();
}
int
......@@ -378,4 +240,13 @@ namespace Trk {
GXFTrackState::setRecalibrated(bool isrecal) {
m_recalib = isrecal;
}
bool GXFTrackState::hasTrackCovariance(void) {
return m_covariance_set;
}
void GXFTrackState::zeroTrackCovariance(void) {
m_covariance_set = true;
m_covariancematrix.setZero();
}
}
......@@ -15,7 +15,6 @@ using CLHEP::HepVector;
namespace Trk {
GXFTrajectory::GXFTrajectory() {
m_straightline = true;
m_fieldprop = nullptr;
m_ndof = 0;
m_nperpars = -1;
m_nscatterers = 0;
......@@ -234,18 +233,9 @@ namespace Trk {
return true;
}
void GXFTrajectory::addHoleState(const TrackParameters * par) {
GXFTrackState *state = new GXFTrackState(par);
m_states.push_back(state);
}
void GXFTrajectory::addMaterialState(GXFTrackState * state, int index, bool owntp) {
void GXFTrajectory::addMaterialState(GXFTrackState * state, int index) {
const TrackParameters *par = state->trackParameters();
GXFMaterialEffects *meff = state->materialEffects();
if (owntp) {
state->setTrackParameters(par);
}
if (index == -1) {
m_states.push_back(state);
......@@ -521,7 +511,7 @@ namespace Trk {
return m_nperpars + numberOfBrems() + 2 * numberOfScatterers();
}
double GXFTrajectory::chi2() {
double GXFTrajectory::chi2() const {
return m_chi2;
}
......@@ -537,7 +527,7 @@ namespace Trk {
m_prevchi2 = chi2;
}
int GXFTrajectory::nDOF() {
int GXFTrajectory::nDOF() const {
return m_ndof;
}