20 #include "canvas/Persistency/Common/FindManyP.h" 42 #include "Geant4/G4ThreeVector.hh" 44 #include "nug4/MagneticField/MagneticField.h" 84 int KalmanFit( std::vector<TPCCluster> &TPCClusters,
85 std::vector<int> &TPCClusterlist,
86 std::vector<float> &trackparatend,
90 std::set<int> &unused_TPCClusters,
92 std::vector<TVector3>& trajpts);
97 void updatepar(TVectorF &parvec,
float zcur,
float xh,
float yh,
float zh, TVectorF &predstep,
float &dlength);
124 consumes< std::vector<gar::rec::Track> >(patrecTag);
125 consumes< art::Assns<gar::rec::TPCCluster, gar::rec::Track> >(patrecTag);
131 produces< std::vector<gar::rec::Track> >();
132 produces< art::Assns<gar::rec::TPCCluster, gar::rec::Track> >();
133 produces<std::vector<gar::rec::TrackIoniz>>();
134 produces<art::Assns<rec::TrackIoniz, rec::Track>>();
135 produces<std::vector<gar::rec::TrackTrajectory>>();
136 produces<art::Assns<rec::TrackTrajectory, rec::Track>>();
145 std::unique_ptr< std::vector<gar::rec::Track> > trkCol(
new std::vector<gar::rec::Track>);
146 std::unique_ptr< art::Assns<gar::rec::TPCCluster,gar::rec::Track> > TPCClusterTrkAssns(new ::art::Assns<gar::rec::TPCCluster,gar::rec::Track>);
147 std::unique_ptr< std::vector<rec::TrackIoniz> > ionCol(
new std::vector<rec::TrackIoniz>);
148 std::unique_ptr< art::Assns<rec::TrackIoniz,rec::Track> > ionTrkAssns(new ::art::Assns<rec::TrackIoniz,rec::Track>);
149 std::unique_ptr< std::vector<rec::TrackTrajectory> > trajCol(
new std::vector<rec::TrackTrajectory>);
150 std::unique_ptr< art::Assns<rec::TrackTrajectory,rec::Track> > trajTrkAssns(new ::art::Assns<rec::TrackTrajectory,rec::Track>);
155 auto const& patrecTracks = *patrecTrackHandle;
163 G4ThreeVector zerovec(0,0,0);
164 G4ThreeVector magfield = magFieldService->FieldAtPoint(zerovec);
167 double xtpccent = geo->TPCXCent();
168 double ytpccent = geo->TPCYCent();
169 double ztpccent = geo->TPCZCent();
170 TVector3 tpccent(xtpccent,ytpccent,ztpccent);
171 TVector3 xhat(1,0,0);
173 const art::FindManyP<gar::rec::TPCCluster> TPCClustersFromPatRecTracks(patrecTrackHandle,e,
fPatRecLabel);
175 for (
size_t itrack = 0; itrack < patrecTracks.size(); ++itrack)
177 std::vector<gar::rec::TPCCluster> TPCClusters;
178 for (
size_t iTPCCluster=0; iTPCCluster < TPCClustersFromPatRecTracks.at(itrack).size(); ++iTPCCluster)
180 TPCClusters.push_back(*TPCClustersFromPatRecTracks.at(itrack).at(iTPCCluster));
188 ionCol->push_back(trackions);
189 trajCol->push_back(tracktraj);
190 auto const trackpointer = trackPtrMaker(trkCol->size()-1);
191 auto const ionizpointer = ionizPtrMaker(ionCol->size()-1);
192 auto const trajpointer = trajPtrMaker(trajCol->size()-1);
193 for (
size_t iTPCCluster=0; iTPCCluster<TPCClusters.size(); ++iTPCCluster)
195 TPCClusterTrkAssns->addSingle(TPCClustersFromPatRecTracks.at(itrack).at(iTPCCluster),trackpointer);
197 ionTrkAssns->addSingle(ionizpointer, trackpointer);
198 trajTrkAssns->addSingle(trajpointer, trackpointer);
223 std::vector<int> hlf;
224 std::vector<int> hlb;
229 std::vector<float> tparend(6);
231 float chisqforwards = 0;
232 float lengthforwards = 0;
233 std::set<int> unused_TPCClusters;
234 std::vector<std::pair<float,float>> dSigdXs_FWD;
235 std::vector<TVector3> trajpts_FWD;
237 int retcode =
KalmanFit(TPCClusters,hlf,tparend,chisqforwards,lengthforwards,covmatend,unused_TPCClusters,dSigdXs_FWD,trajpts_FWD);
238 if (retcode != 0)
return 1;
242 std::vector<float> tparbeg(6);
244 float chisqbackwards = 0;
245 float lengthbackwards = 0;
246 std::vector<std::pair<float,float>> dSigdXs_BAK;
247 std::vector<TVector3> trajpts_BAK;
249 retcode =
KalmanFit(TPCClusters,hlb,tparbeg,chisqbackwards,lengthbackwards,covmatbeg,unused_TPCClusters,dSigdXs_BAK,trajpts_BAK);
250 if (retcode != 0)
return 1;
252 size_t nTPCClusters=0;
253 if (TPCClusters.size()>unused_TPCClusters.size())
254 { nTPCClusters = TPCClusters.size()-unused_TPCClusters.size(); }
268 trackions.
setData(dSigdXs_FWD,dSigdXs_BAK);
269 tracktraj.
setData(trajpts_FWD,trajpts_BAK);
299 std::vector<int> &TPCClusterlist,
300 std::vector<float> &trackparatend,
304 std::set<int> &unused_TPCClusters,
306 std::vector<TVector3>& trajpts)
311 size_t nTPCClusters = TPCClusterlist.size();
314 for (
size_t i=0; i<5; ++i) trackparatend[i] = 0;
315 for (
size_t i=0; i<25; ++i) covmat[i] = 0;
320 float curvature_init=0.1;
322 float lambda_init = 0;
326 float x_other_end = 0;
345 float zpos = zpos_init;
350 P[0][0] = TMath::Sq(1);
351 P[1][1] = TMath::Sq(1);
352 P[2][2] = TMath::Sq(.5);
353 P[3][3] = TMath::Sq(.5);
354 P[4][4] = TMath::Sq(.5);
379 parvec[0] = ypos_init;
380 parvec[1] = xpos_init;
381 parvec[2] = curvature_init;
382 parvec[3] = phi_init;
383 parvec[4] = lambda_init;
384 TVectorF predstep(5);
400 for (
int i=0;i<5;++i) I[i][i] = 1;
402 for (
size_t iTPCCluster=1; iTPCCluster<nTPCClusters; ++iTPCCluster)
405 float xh = TPCClusters[TPCClusterlist[iTPCCluster]].Position()[0];
406 float yh = TPCClusters[TPCClusterlist[iTPCCluster]].Position()[1];
407 float zh = TPCClusters[TPCClusterlist[iTPCCluster]].Position()[2];
412 std::cout <<
"Adding a new TPCCluster: " << xh <<
" " << yh <<
" " << zh <<
std::endl;
416 float lambda = parvec[4];
417 float slope = TMath::Tan(lambda);
431 std::cout <<
"z: " << zpos <<
" adding new hit at z: " << zh <<
std::endl;
432 std::cout <<
" Parvec: y " << parvec[0] <<
" z " << parvec[1] <<
" c " << parvec[2] <<
" phi " << parvec[3] <<
" lambda " << parvec[4] <<
std::endl;
438 updatepar(parvec,zpos,xh,yh,zh,predstep,dlength);
442 std::cout <<
" Predstep: y " << predstep[0] <<
" z " << predstep[1] <<
" c " << predstep[2] <<
" phi " << predstep[3] <<
" lambda " << predstep[4] <<
std::endl;
448 TVectorF parvec_plus_dpar(5);
449 TVectorF predstepmod(5);
450 TVectorF derivvec(5);
451 for (
size_t ipar=0;ipar<5;++ipar)
453 float epsilon = 0.001;
454 parvec_plus_dpar = parvec;
455 parvec_plus_dpar[ipar] += epsilon;
457 updatepar(parvec_plus_dpar,zpos,xh,yh,zh,predstepmod,dldummy);
458 derivvec = (predstepmod - predstep);
459 derivvec *= (1.0/epsilon);
460 for (
size_t jpar=0; jpar<5; ++jpar)
462 F[jpar][ipar] = derivvec[jpar];
479 std::cout <<
"PPred Matrix: " <<
std::endl;
483 ytilde[0] = yh - predstep[0];
484 ytilde[1] = xh - predstep[1];
485 float ydistsq = ytilde.Norm2Sqr();
486 if (ydistsq > roadsq)
488 unused_TPCClusters.insert(iTPCCluster);
494 std::cout <<
"ytilde (residuals): " <<
std::endl;
514 std::cout <<
"Inverted S Matrix: " <<
std::endl;
525 parvec = predstep + K*ytilde;
529 trajpts.emplace_back(parvec[1],parvec[0],zpos);
534 float valSig = TPCClusters[TPCClusterlist[iTPCCluster]].Signal();
537 std::pair pushme = std::make_pair(valSig,dlength);
538 dSigdXs.push_back( pushme );
543 if (dSigdXs.size()>0) dSigdXs.pop_back();
547 for (
size_t i=0; i<5; ++i)
549 trackparatend[i] = parvec[i];
551 trackparatend[5] =
zpos;
554 std::cout <<
"Track params at end (y, z, curv, phi, lambda) " << trackparatend[0] <<
" " << trackparatend[1] <<
" " <<
555 trackparatend[2] <<
" " << trackparatend[3] <<
" " << trackparatend[4] <<
std::endl;
571 for (
size_t i=0; i<5; ++i)
573 for (
size_t j=0; j<5; ++j)
575 covmat[icov] = P[i][j];
605 float yc = parvec[0] + r*TMath::Cos(parvec[3]);
606 float zc = zcur - r*TMath::Sin(parvec[3]);
611 float phip1 = TMath::ASin((zh-zc)/r);
612 float phip2 = TMath::Pi() - phip1;
616 float tanlambda = TMath::Tan(parvec[4]);
617 if (tanlambda == 0) tanlambda = 1
E-4;
618 float phip3 = parvec[3] + (xh-parvec[1])/(r*tanlambda);
620 float dphip1n = (phip1-phip3)/TMath::TwoPi();
621 float niphi1 = nearbyint(dphip1n);
622 float dphip1a = TMath::Abs(dphip1n - niphi1);
624 float dphip2n = (phip2-phip3)/TMath::TwoPi();
625 float niphi2 = nearbyint(dphip2n);
626 float dphip2a = TMath::Abs(dphip2n - niphi2);
628 float phiex=phip1 + niphi1*TMath::TwoPi();
629 if (dphip1a > dphip2a)
631 phiex = phip2 + niphi2*TMath::TwoPi();
637 predstep[0] = yc - r*TMath::Cos(phiex);
638 predstep[1] = parvec[1] + r*tanlambda*(phiex-parvec[3]);
641 dlength = TMath::Abs( r * (phiex - parvec[3]) / TMath::Cos(parvec[4]) );
void setNTPCClusters(const size_t nTPCClusters)
void setLengthForwards(const float lengthforwards)
int fPrintLevel
debug printout: 0: none, 1: just track parameters and residuals, 2: all
void updatepar(TVectorF &parvec, float zcur, float xh, float yh, float zh, TVectorF &predstep, float &dlength)
float fTPCClusterResolYZinFit
TPCCluster resolution parameter to use in fit.
dayonetrackfit(fhicl::ParameterSet const &p)
void setCovMatBeg(const float *covmatbeg)
EDProducer(fhicl::ParameterSet const &pset)
float fTPCClusterResolX
drift direction contribution to determine step size (resolution of a TPCCluster)
float fMinIonizGapCut
Don't compute dEdx for this dx or larger.
float fKalPhiStepUncSq
constant uncertainty term on each step of the Kalman fit – squared, for phi
unsigned int fInitialTPNTPCClusters
number of TPCClusters to use for initial trackpar estimate, if present
std::pair< float, std::string > P
void setLengthBackwards(const float lengthbackwards)
float fSortTransWeight
for use in the hit sorting algorithm – transverse distance weight factor
void setTrackParametersBegin(const float *tparbeg)
unsigned int fMinNumTPCClusters
minimum number of TPCClusters to define a track
#define DEFINE_ART_MODULE(klass)
std::string fPatRecLabel
input patrec tracks and associations
float fSortDistBack
for use in the hit sorting algorithm – how far to go back before raising the distance figure of meri...
float fKalCurvStepUncSq
constant uncertainty term on each step of the Kalman fit – squared, for curvature ...
void setXEnd(const float xend)
void setTrackParametersEnd(const float *tparend)
ValidHandle< PROD > getValidHandle(InputTag const &tag) const
ProductID put(std::unique_ptr< PROD > &&edp, std::string const &instance={})
void setChisqBackwards(const float chisqbackwards)
void setData(std::vector< TVector3 > ForwardTrajectory, std::vector< TVector3 > BackwardTrajectory)
gar::rec::Track CreateTrack()
int KalmanFit(std::vector< TPCCluster > &TPCClusters, std::vector< int > &TPCClusterlist, std::vector< float > &trackparatend, float &chisquared, float &length, float *covmat, std::set< int > &unused_TPCClusters, std::vector< std::pair< float, float >> &dSigdX, std::vector< TVector3 > &trajpts)
int KalmanFitBothWays(std::vector< gar::rec::TPCCluster > &TPCClusters, TrackPar &trackpar, TrackIoniz &trackions, TrackTrajectory &tracktraj)
void produce(art::Event &e) override
General GArSoft Utilities.
void setCovMatEnd(const float *covmatend)
void sort_TPCClusters_along_track(const std::vector< gar::rec::TPCCluster > &TPCClusters, std::vector< int > &hlf, std::vector< int > &hlb, int printlevel, float &lengthforwards, float &lengthbackwards, float sorttransweight, float sortdistback)
void setXBeg(const float xbeg)
void setData(std::vector< std::pair< float, float >> dSigdX_FWD, std::vector< std::pair< float, float >> dSigdX_BAK)
int fDumpTracks
0: do not print out tracks, 1: print out tracks
void setChisqForwards(const float chisqforwards)
float fRoadYZinFit
cut in cm for dropping TPCClusters from tracks in fit
float fTPCClusterResolYZ
pad size in cm in YZ to determine step size
LArSoft geometry interface.
art framework interface to geometry description
float fKalLambdaStepUncSq
constant uncertainty term on each step of the Kalman fit – squared, for lambda
void setTime(const double time)
dayonetrackfit & operator=(dayonetrackfit const &)=delete
QTextStream & endl(QTextStream &s)
int initial_trackpar_estimate(const std::vector< gar::rec::TPCCluster > &TPCClusters, std::vector< int > &TPCClusterlist, float &curvature_init, float &lambda_init, float &phi_init, float &xpos, float &ypos, float &zpos, float &x_other_end, unsigned int initialtpnTPCClusters, int printlevel)