Skip to content

Commit 7561998

Browse files
authored
Merge pull request #215 from brownd1978/trajptr
Use trajectory ptrs in ClosestApproach
2 parents 91c77cd + 8faca38 commit 7561998

15 files changed

+157
-202
lines changed

Detector/Hit.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace KinKal {
3535
virtual void print(std::ostream& ost=std::cout,int detail=0) const = 0;
3636
// update to a new reference, without changing internal state
3737
virtual void updateReference(PTRAJ const& ptraj) = 0;
38-
virtual KTRAJPTR const& refTrajPtr() const = 0;
38+
virtual KTRAJPTR refTrajPtr() const = 0;
3939
// update the internals of the hit, specific to this meta-iteraion
4040
virtual void updateState(MetaIterConfig const& config,bool first) = 0;
4141
// The following provides the constraint/information content of this hit in the trajectory weight space

Detector/ParameterHit.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ namespace KinKal {
4040
// parameter constraints are absolute and can't be updated
4141
void print(std::ostream& ost=std::cout,int detail=0) const override;
4242
void updateReference(PTRAJ const& ptraj) override { reftraj_ = ptraj.nearestTraj(time()); }
43-
KTRAJPTR const& refTrajPtr() const override { return reftraj_; }
43+
KTRAJPTR refTrajPtr() const override { return reftraj_; }
4444
// ParameterHit-specfic interface
4545
// construct from constraint values, time, and mask of which parameters to constrain
4646
ParameterHit(double time, PTRAJ const& ptraj, Parameters const& params, PMASK const& pmask);

Examples/ScintHit.hh

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,13 @@ namespace KinKal {
1717
using RESIDHIT = ResidualHit<KTRAJ>;
1818
using HIT = Hit<KTRAJ>;
1919
using KTRAJPTR = std::shared_ptr<KTRAJ>;
20+
using STRAJPTR = std::shared_ptr<SensorLine>;
2021
// copy constructor
2122
ScintHit(ScintHit<KTRAJ> const& rhs):
2223
ResidualHit<KTRAJ>(rhs),
23-
saxis_(rhs.sensorAxis()),
2424
tvar_(rhs.timeVariance()),
2525
wvar_(rhs.widthVariance()),
26-
tpca_(
27-
rhs.closestApproach().particleTraj(),
28-
saxis_,
29-
rhs.closestApproach().hint(),
30-
rhs.closestApproach().precision()
31-
),
26+
tpca_(rhs.tpca_),
3227
rresid_(rhs.refResidual()){
3328
/**/
3429
};
@@ -46,20 +41,19 @@ namespace KinKal {
4641
Residual const& refResidual(unsigned ires=0) const override;
4742
double time() const override { return tpca_.particleToca(); }
4843
void updateReference(PTRAJ const& ptraj) override;
49-
KTRAJPTR const& refTrajPtr() const override { return tpca_.particleTrajPtr(); }
44+
KTRAJPTR refTrajPtr() const override { return tpca_.particleTrajPtr(); }
5045
void updateState(MetaIterConfig const& config,bool first) override;
5146
void print(std::ostream& ost=std::cout,int detail=0) const override;
5247
// scintHit explicit interface
5348
ScintHit(PCA const& pca, double tvar, double wvar);
5449
virtual ~ScintHit(){}
5550
// the line encapsulates both the measurement value (through t0), and the light propagation model (through the velocity)
56-
auto const& sensorAxis() const { return saxis_; }
51+
auto sensorAxis() const { return tpca_.sensorTrajPtr(); }
5752
auto const& closestApproach() const { return tpca_; }
5853
double timeVariance() const { return tvar_; }
5954
double widthVariance() const { return wvar_; }
6055
auto precision() const { return tpca_.precision(); }
6156
private:
62-
SensorLine saxis_; // symmetry axis of this sensor
6357
double tvar_; // variance in the time measurement: assumed independent of propagation distance/time
6458
double wvar_; // variance in transverse position of the sensor/measurement in mm. Assumes cylindrical error, could be more general
6559
CA tpca_; // reference time and position of closest approach to the axis
@@ -70,8 +64,8 @@ namespace KinKal {
7064
};
7165

7266
template <class KTRAJ> ScintHit<KTRAJ>::ScintHit(PCA const& pca, double tvar, double wvar) :
73-
saxis_(pca.sensorTraj()), tvar_(tvar), wvar_(wvar),
74-
tpca_(pca.localTraj(),saxis_,pca.precision(),pca.tpData(),pca.dDdP(),pca.dTdP())
67+
tvar_(tvar), wvar_(wvar),
68+
tpca_(static_cast<CA const&>(pca))
7569
{}
7670

7771
template <class KTRAJ> Residual const& ScintHit<KTRAJ>::refResidual(unsigned ires) const {
@@ -81,9 +75,9 @@ namespace KinKal {
8175

8276
template <class KTRAJ> void ScintHit<KTRAJ>::updateReference(PTRAJ const& ptraj) {
8377
// use previous hint, or initialize from the sensor time
84-
CAHint tphint = tpca_.usable() ? tpca_.hint() : CAHint(saxis_.measurementTime(), saxis_.measurementTime());
85-
PCA pca(ptraj,saxis_,tphint,precision());
86-
tpca_ = pca.localClosestApproach();
78+
CAHint tphint = tpca_.usable() ? tpca_.hint() : CAHint(sensorAxis()->measurementTime(), sensorAxis()->measurementTime());
79+
PCA pca(ptraj,sensorAxis(),tphint,precision());
80+
tpca_ = static_cast<CA>(pca);
8781
if(!tpca_.usable())throw std::runtime_error("ScintHit TPOCA failure");
8882
}
8983

@@ -92,26 +86,26 @@ namespace KinKal {
9286
// early in the fit when t0 has very large errors.
9387
// If it is unphysical try to adjust it back using a better hint.
9488
auto ppos = tpca_.particlePoca().Vect();
95-
auto const& sstart = saxis_.start();
96-
auto const& send = saxis_.end();
89+
auto const& sstart = sensorAxis()->start();
90+
auto const& send = sensorAxis()->end();
9791
// tolerance should come from the config. Should also test relative to the error. FIXME
98-
double tol = saxis_.length()*1.0;
99-
double sdist = (ppos - saxis_.middle()).Dot(saxis_.direction());
100-
if( (ppos-sstart).Dot(saxis_.direction()) < -tol || (ppos-send).Dot(saxis_.direction()) > tol) {
92+
double tol = sensorAxis()->length()*1.0;
93+
double sdist = (ppos - sensorAxis()->middle()).Dot(sensorAxis()->direction());
94+
if( (ppos-sstart).Dot(sensorAxis()->direction()) < -tol || (ppos-send).Dot(sensorAxis()->direction()) > tol) {
10195
// adjust hint to the middle and try agian
102-
double sspeed = tpca_.particleTraj().velocity(tpca_.particleToca()).Dot(saxis_.direction());
96+
double sspeed = tpca_.particleTraj().velocity(tpca_.particleToca()).Dot(sensorAxis()->direction());
10397
auto tphint = tpca_.hint();
10498
tphint.particleToca_ -= sdist/sspeed;
105-
tpca_ = CA(tpca_.particleTrajPtr(),saxis_,tphint,precision());
99+
tpca_ = CA(tpca_.particleTrajPtr(),sensorAxis(),tphint,precision());
106100
// should check if this is still unphysical and disable the hit if so FIXME
107-
sdist = (tpca_.particlePoca().Vect() - saxis_.middle()).Dot(saxis_.direction());
101+
sdist = (tpca_.particlePoca().Vect() - sensorAxis()->middle()).Dot(sensorAxis()->direction());
108102
}
109103

110104
// residual is just delta-T at CA.
111105
// the variance includes the measurement variance and the tranvserse size (which couples to the relative direction)
112106
// Might want to do more updating (set activity) based on DOCA in future: TODO
113107
double dd2 = tpca_.dirDot()*tpca_.dirDot();
114-
double totvar = tvar_ + wvar_*dd2/(saxis_.speed(sdist)*saxis_.speed(sdist)*(1.0-dd2));
108+
double totvar = tvar_ + wvar_*dd2/(sensorAxis()->speed(sdist)*sensorAxis()->speed(sdist)*(1.0-dd2));
115109
rresid_ = Residual(tpca_.deltaT(),totvar,0.0,tpca_.dTdP());
116110
this->updateWeight(config);
117111
}
@@ -124,7 +118,7 @@ namespace KinKal {
124118
ost << " ScintHit tvar " << tvar_ << " wvar " << wvar_ << std::endl;
125119
if(detail > 0){
126120
ost << "Line ";
127-
saxis_.print(ost,detail);
121+
sensorAxis()->print(ost,detail);
128122
}
129123
}
130124

Examples/SimpleWireHit.hh

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ namespace KinKal {
2222
using CA = ClosestApproach<KTRAJ,SensorLine>;
2323
using KTRAJPTR = std::shared_ptr<KTRAJ>;
2424
using PTRAJ = ParticleTrajectory<KTRAJ>;
25+
using STRAJPTR = std::shared_ptr<SensorLine>;
26+
2527
enum Dimension { dresid=0, tresid=1}; // residual dimensions
2628

2729
SimpleWireHit(BFieldMap const& bfield, PCA const& pca, WireHitState const& whstate, double mindoca,
@@ -32,13 +34,7 @@ namespace KinKal {
3234
ResidualHit<KTRAJ>(rhs),
3335
bfield_(rhs.bfield()),
3436
whstate_(rhs.hitState()),
35-
wire_(rhs.wire()),
36-
ca_(
37-
rhs.closestApproach().particleTraj(),
38-
wire_,
39-
rhs.closestApproach().hint(),
40-
rhs.closestApproach().precision()
41-
),
37+
tpca_(rhs.tpca_),
4238
rresid_(rhs.residuals()),
4339
mindoca_(rhs.minDOCA()),
4440
dvel_(driftVelocity()),
@@ -57,11 +53,11 @@ namespace KinKal {
5753
rv->setClosestApproach(ca);
5854
return rv;
5955
};
60-
double time() const override { return ca_.particleToca(); }
56+
double time() const override { return tpca_.particleToca(); }
6157
VEC3 dRdX(unsigned ires) const;
6258
Residual const& refResidual(unsigned ires=dresid) const override;
6359
void updateReference(PTRAJ const& ptraj) override;
64-
KTRAJPTR const& refTrajPtr() const override { return ca_.particleTrajPtr(); }
60+
KTRAJPTR refTrajPtr() const override { return tpca_.particleTrajPtr(); }
6561
void print(std::ostream& ost=std::cout,int detail=0) const override;
6662
// Use dedicated updater
6763
void updateState(MetaIterConfig const& config,bool first) override;
@@ -73,24 +69,23 @@ namespace KinKal {
7369
double minDOCA() const { return mindoca_; }
7470
int id() const { return id_; }
7571
CA unbiasedClosestApproach() const;
76-
auto const& closestApproach() const { return ca_; }
72+
auto const& closestApproach() const { return tpca_; }
7773
auto const& hitState() const { return whstate_; }
78-
auto const& wire() const { return wire_; }
7974
auto const& bfield() const { return bfield_; }
80-
auto precision() const { return ca_.precision(); }
75+
auto precision() const { return tpca_.precision(); }
8176
auto const& residuals() const { return rresid_; }
8277
double tot() const { return tot_; }
8378
double totVariance() const { return totvar_; }
79+
auto wire() const { return tpca_.sensorTrajPtr(); }
8480

8581
private:
8682
BFieldMap const& bfield_; // drift calculation requires the BField for ExB effects
8783
WireHitState whstate_; // current state
88-
SensorLine wire_; // local linear approximation to the wire of this hit, encoding all (local) position and time information.
8984
// the start time is the measurement time, the direction is from
9085
// the physical source of the signal (particle) to the measurement recording location (electronics), the direction magnitude
9186
// is the effective signal propagation velocity along the wire, and the time range describes the active wire length
9287
// (when multiplied by the propagation velocity).
93-
CA ca_; // reference time and position of closest approach to the wire; this is generally biased by the hit
88+
CA tpca_; // reference time and position of closest approach to the wire; this is generally biased by the hit
9489
std::array<Residual,2> rresid_; // residuals WRT most recent reference
9590
double mindoca_; // effective minimum DOCA used when assigning LR ambiguity, used to define null hit properties
9691
double dvel_; // constant drift speed
@@ -101,7 +96,7 @@ namespace KinKal {
10196
void updateResiduals();
10297

10398
// modifiers to support cloning
104-
void setClosestApproach(const CA& ca){ ca_ = ca; }
99+
void setClosestApproach(const CA& ca){ tpca_ = ca; }
105100
};
106101

107102
//trivial 'updater' that sets the wire hit state to null
@@ -113,18 +108,18 @@ namespace KinKal {
113108
template <class KTRAJ> SimpleWireHit<KTRAJ>::SimpleWireHit(BFieldMap const& bfield, PCA const& pca, WireHitState const& whstate,
114109
double mindoca, double driftspeed, double tvar, double tot, double totvar, double rcell, int id) :
115110
bfield_(bfield),
116-
whstate_(whstate), wire_(pca.sensorTraj()),
117-
ca_(pca.localTraj(),wire_,pca.precision(),pca.tpData(),pca.dDdP(),pca.dTdP()), // must be explicit to get the right sensor traj reference
111+
whstate_(whstate),
112+
tpca_(static_cast<CA const&>(pca)),
118113
mindoca_(mindoca), dvel_(driftspeed), tvar_(tvar), tot_(tot), totvar_(totvar), rcell_(rcell), id_(id) {
119114
}
120115

121116
template <class KTRAJ> void SimpleWireHit<KTRAJ>::updateReference(PTRAJ const& ptraj) {
122117
// if we already computed PCA in the previous iteration, use that to set the hint. This speeds convergence
123118
// otherwise use the time at the center of the wire
124-
CAHint tphint = ca_.usable() ? ca_.hint() : CAHint(wire_.timeAtMidpoint(),wire_.timeAtMidpoint());
125-
PCA pca(ptraj,wire_,tphint,precision());
126-
ca_ = pca.localClosestApproach();
127-
if(!ca_.usable())throw std::runtime_error("WireHit TPOCA failure");
119+
CAHint tphint = tpca_.usable() ? tpca_.hint() : CAHint(wire()->timeAtMidpoint(),wire()->timeAtMidpoint());
120+
PCA pca(ptraj,wire(),tphint,precision());
121+
tpca_ = static_cast<CA>(pca);
122+
if(!tpca_.usable())throw std::runtime_error("WireHit TPOCA failure");
128123
}
129124

130125
template <class KTRAJ> void SimpleWireHit<KTRAJ>::updateState(MetaIterConfig const& miconfig, bool first) {
@@ -148,16 +143,16 @@ namespace KinKal {
148143
}
149144
rresid_[tresid] = rresid_[dresid] = Residual();
150145
if(whstate_.active()){
151-
rresid_[tresid] = Residual(ca_.deltaT() - tot_, totvar_,0.0,ca_.dTdP()); // always constrain to TOT; this stabilizes the fit
146+
rresid_[tresid] = Residual(tpca_.deltaT() - tot_, totvar_,0.0,tpca_.dTdP()); // always constrain to TOT; this stabilizes the fit
152147
if(whstate_.useDrift()){
153148
// translate PCA to residual. Use ambiguity assignment to convert drift time to a drift radius
154-
double dr = dvel_*whstate_.lrSign()*ca_.deltaT() -ca_.doca();
155-
DVEC dRdP = dvel_*whstate_.lrSign()*ca_.dTdP() -ca_.dDdP();
149+
double dr = dvel_*whstate_.lrSign()*tpca_.deltaT() -tpca_.doca();
150+
DVEC dRdP = dvel_*whstate_.lrSign()*tpca_.dTdP() -tpca_.dDdP();
156151
rresid_[dresid] = Residual(dr,tvar_*dvel_*dvel_,0.0,dRdP);
157152
} else {
158153
// interpret DOCA against the wire directly as a residuals
159-
double nulldvar = dvel_*dvel_*(ca_.deltaT()*ca_.deltaT()+0.8);
160-
rresid_[dresid] = Residual(ca_.doca(),nulldvar,0.0,ca_.dDdP());
154+
double nulldvar = dvel_*dvel_*(tpca_.deltaT()*tpca_.deltaT()+0.8);
155+
rresid_[dresid] = Residual(tpca_.doca(),nulldvar,0.0,tpca_.dDdP());
161156
}
162157
}
163158
// now update the weight
@@ -168,9 +163,9 @@ namespace KinKal {
168163
if (whstate_.active()){
169164
if (ires == dresid){
170165
if (whstate_.useDrift()){
171-
return ca_.lSign()*ca_.delta().Vect().Unit();
166+
return tpca_.lSign()*tpca_.delta().Vect().Unit();
172167
}else{
173-
return -1*ca_.lSign()*ca_.delta().Vect().Unit();
168+
return -1*tpca_.lSign()*tpca_.delta().Vect().Unit();
174169
}
175170
}
176171
}
@@ -186,7 +181,7 @@ namespace KinKal {
186181
// compute the unbiased closest approach; this is brute force, but works
187182
auto const& ca = this->closestApproach();
188183
auto uparams = HIT::unbiasedParameters();
189-
KTRAJ utraj(uparams,ca.particleTraj());
184+
auto utraj = std::make_shared<KTRAJ>(uparams,ca.particleTraj());
190185
return CA(utraj,this->wire(),ca.hint(),ca.precision());
191186
}
192187

@@ -214,7 +209,7 @@ namespace KinKal {
214209
ost << std::endl;
215210
}
216211
if(detail > 1) {
217-
ost << "Approximate Propagation speed " << wire_.speed(100) << " TPOCA " << ca_.tpData() << std::endl;
212+
ost << "Approximate Propagation speed " << wire()->speed(100) << " TPOCA " << tpca_.tpData() << std::endl;
218213
}
219214
}
220215

Examples/StrawXing.hh

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ namespace KinKal {
1515
public:
1616
using PTRAJ = ParticleTrajectory<KTRAJ>;
1717
using KTRAJPTR = std::shared_ptr<KTRAJ>;
18+
using STRAJPTR = std::shared_ptr<SensorLine>;
1819
using EXING = ElementXing<KTRAJ>;
1920
using PCA = PiecewiseClosestApproach<KTRAJ,SensorLine>;
2021
using CA = ClosestApproach<KTRAJ,SensorLine>;
@@ -24,14 +25,8 @@ namespace KinKal {
2425
// copy constructor
2526
StrawXing(StrawXing const& rhs):
2627
ElementXing<KTRAJ>(rhs),
27-
axis_(rhs.axis_),
2828
smat_(rhs.smat_),
29-
tpca_(
30-
rhs.closestApproach().particleTraj(),
31-
axis_,
32-
rhs.closestApproach().hint(),
33-
rhs.closestApproach().precision()
34-
),
29+
tpca_(rhs.tpca_),
3530
toff_(rhs.toff_),
3631
sxconfig_(rhs.sxconfig_),
3732
varscale_(rhs.varscale_),
@@ -60,12 +55,12 @@ namespace KinKal {
6055
bool active() const override { return mxings_.size() > 0; }
6156
// accessors
6257
auto const& closestApproach() const { return tpca_; }
58+
auto axis() const { return tpca_.sensorTrajPtr(); }
6359
auto const& strawMaterial() const { return smat_; }
6460
auto const& config() const { return sxconfig_; }
6561
auto precision() const { return tpca_.precision(); }
6662

6763
private:
68-
SensorLine axis_; // straw axis, expressed as a timeline
6964
StrawMaterial const& smat_;
7065
CA tpca_; // result of most recent TPOCA
7166
double toff_; // small time offset
@@ -79,17 +74,16 @@ namespace KinKal {
7974
};
8075

8176
template <class KTRAJ> StrawXing<KTRAJ>::StrawXing(PCA const& pca, StrawMaterial const& smat) :
82-
axis_(pca.sensorTraj()),
8377
smat_(smat),
84-
tpca_(pca.localTraj(),axis_,pca.precision(),pca.tpData(),pca.dDdP(),pca.dTdP()),
78+
tpca_(static_cast<CA const&>(pca)),
8579
toff_(smat.wireRadius()/pca.particleTraj().speed(pca.particleToca())), // locate the effect to 1 side of the wire to avoid overlap with hits
8680
varscale_(1.0)
8781
{}
8882

8983
template <class KTRAJ> void StrawXing<KTRAJ>::updateReference(PTRAJ const& ptraj) {
90-
CAHint tphint = tpca_.usable() ? tpca_.hint() : CAHint(axis_.timeAtMidpoint(),axis_.timeAtMidpoint());
91-
PCA pca(ptraj,axis_,tphint,precision());
92-
tpca_ = pca.localClosestApproach();
84+
CAHint tphint = tpca_.usable() ? tpca_.hint() : CAHint(axis()->timeAtMidpoint(),axis()->timeAtMidpoint());
85+
PCA pca(ptraj,axis(),tphint,precision());
86+
tpca_ = static_cast<CA>(pca);
9387
if(!tpca_.usable())throw std::runtime_error("StrawXing TPOCA failure");
9488
}
9589

@@ -131,7 +125,7 @@ namespace KinKal {
131125
}
132126
if(detail > 1){
133127
ost << " Axis ";
134-
axis_.print(ost,0);
128+
axis()->print(ost,0);
135129
}
136130
ost << std::endl;
137131
}

0 commit comments

Comments
 (0)