19 #include "art/Framework/Services/Registry/ServiceHandle.h"
20 #include "canvas/Persistency/Common/PtrVector.h"
21 #include "cetlib_except/exception.h"
22 #include "fhiclcpp/ParameterSet.h"
38 #include "messagefacility/MessageLogger/MessageLogger.h"
40 #include <type_traits>
50 calcMagnitude(
const double*
x)
52 return std::hypot(x[0], x[1], x[2]);
66 if (used_hits.size() > 0) {
68 std::stable_sort(hits.begin(), hits.end());
69 std::stable_sort(used_hits.begin(), used_hits.end());
72 trkf::Hits::iterator it = std::set_difference(
73 hits.begin(), hits.end(), used_hits.begin(), used_hits.end(), hits.begin());
76 hits.erase(it, hits.end());
85 : fDoDedx{pset.get<
bool>(
"DoDedx")}
87 ,
fMaxTcut{pset.get<
double>(
"MaxTcut")}
95 ,
fKFAlg(pset.get<fhicl::ParameterSet>(
"KalmanFilterAlg"))
99 mf::LogInfo(
"Track3DKalmanHitAlg") <<
"Track3DKalmanHitAlg instantiated.";
104 std::vector<trkf::KalmanOutput>
109 std::vector<KalmanOutput>
outputs(inputs.size());
111 for (
size_t i = 0; i < inputs.size(); ++i) {
112 Hits& hits = inputs[i].hits;
120 Hits unusedhits = hits;
124 std::vector<Hits> hitsperseed;
125 std::vector<recob::Seed>
seeds;
130 auto const seedsize = inputs[i].seeds.size();
131 if (first && seedsize > 0 && inputs[i].seedhits.size() > 0) {
132 seeds.reserve(seedsize);
139 if (unusedhits.size() > 0) {
142 seeds.emplace_back(
makeSeed(detProp, unusedhits));
143 hitsperseed.emplace_back();
144 hitsperseed.back().insert(
145 hitsperseed.back().end(), unusedhits.begin(), unusedhits.end());
153 assert(seeds.size() == hitsperseed.size());
156 if (
empty(seeds))
break;
169 const std::vector<Hits>& pfseedhits,
170 std::vector<recob::Seed>&
seeds,
171 std::vector<Hits>& hitsperseed)
const
173 for (
const auto& pseed : pfseeds) {
174 seeds.push_back(*pseed);
176 hitsperseed.insert(hitsperseed.end(), pfseedhits.begin(), pfseedhits.end());
185 const std::vector<recob::Seed>&
seeds,
186 const std::vector<Hits>& hitsperseed,
189 std::deque<KGTrack>& kgtracks)
192 if (seeds.size() != hitsperseed.size()) {
193 throw cet::exception(
"Track3DKalmanHitAlg")
194 <<
"Different size containers for Seeds and Hits/Seed.\n";
196 for (
size_t i = 0; i < seeds.size(); ++i) {
197 growSeedIntoTracks(detProp, pfseed, seeds[i], hitsperseed[i], unusedhits, hits, kgtracks);
209 std::deque<KGTrack>& kgtracks)
219 size_t initial_unusedhits = unusedhits.size();
220 filterHits(unusedhits, trimmedhits);
224 if (!(trimmedhits.size() + unusedhits.size() == initial_unusedhits))
return;
229 std::shared_ptr<Surface> psurf =
makeSurface(seed, dir);
241 const bool build_both =
fDoDedx;
244 auto ntracks = kgtracks.size();
246 if ((!ok || build_both) && ninit == 2) {
253 for (
unsigned int itrk = ntracks; itrk < kgtracks.size(); ++itrk) {
254 const KGTrack& trg = kgtracks[itrk];
262 std::shared_ptr<trkf::Surface>
269 if (mf::isDebugEnabled()) {
270 mf::LogDebug(
"Track3DKalmanHit")
272 <<
"(x,y,z) = " << xyz[0] <<
", " << xyz[1] <<
", " << xyz[2] <<
"\n"
273 <<
"(dx,dy,dz) = " << dir[0] <<
", " << dir[1] <<
", " << dir[2] <<
"\n";
276 return std::make_shared<SurfXYZPlane>(xyz[0], xyz[1], xyz[2], dir[0], dir[1], dir[2]);
283 const std::shared_ptr<trkf::Surface> psurf,
287 std::deque<KGTrack>& kgtracks)
302 KTrack initial_track(psurf, vec, trkdir, pdg);
306 std::unique_ptr<KHitContainer> pseedcont =
fillHitContainer(detProp, seedhits);
309 unsigned int prefplane = pseedcont->getPreferredPlane();
311 if (mf::isDebugEnabled())
312 mf::LogDebug(
"Track3DKalmanHit") <<
"Preferred plane = " << prefplane <<
"\n";
322 if (mf::isDebugEnabled())
323 mf::LogDebug(
"Track3DKalmanHit")
324 << (ok ?
"Find track succeeded." :
"Find track failed.") <<
"\n";
341 Hits& seederhits)
const
343 Hits track_used_hits;
344 std::vector<unsigned int> hittpindex;
345 trg.
fillHits(track_used_hits, hittpindex);
346 filterHits(hits, track_used_hits);
347 filterHits(seederhits, track_used_hits);
352 std::unique_ptr<trkf::KHitContainer>
354 const Hits& hits)
const
359 hitcont->fill(detProp, hits, -1);
375 double dxds0 = mom0[0] / calcMagnitude(mom0);
376 double dxds1 = mom1[0] / calcMagnitude(mom1);
396 Hits::const_iterator itb = hpsit.begin();
397 Hits::const_iterator ite = hpsit.end();
400 seedhits.reserve(hpsit.size());
401 for (Hits::const_iterator it = itb; it != ite; ++it)
402 seedhits.push_back(*it);
412 unsigned int prefplane,
413 std::deque<KGTrack>& kalman_tracks)
433 Hits trackhits = hits;
441 if (!ok)
return false;
444 if (!ok)
return false;
451 kalman_tracks.push_back(trg1);
462 unsigned int prefplane,
463 Hits& trackhits)
const
468 for (
int ix = 0; ok && ix < niter && nfail < 2; ++ix) {
473 std::vector<unsigned int> hittpindex;
474 trg1.
fillHits(goodhits, hittpindex);
477 filterHits(trackhits, goodhits);
480 std::unique_ptr<KHitContainer> ptrackcont =
fillHitContainer(detProp, trackhits);
522 const Hits& hits)
const
524 art::ServiceHandle<geo::Geometry const> geom;
537 for (
auto const& phit : hits) {
563 for (
auto const& phit : hits) {
575 double phi = TMath::PiOver2() - wgeom.
ThetaZ();
576 double sphi = std::sin(phi);
577 double cphi = std::cos(phi);
578 double w = -xyz[1] * sphi + xyz[2] * cphi;
587 sm(0, 0) += sphi * sphi;
588 sm(1, 0) -= sphi * cphi;
589 sm(1, 1) += cphi * cphi;
590 sm(2, 0) += sphi * sphi * dx;
591 sm(2, 1) -= sphi * cphi * dx;
592 sm(2, 2) += sphi * sphi * dx * dx;
593 sm(3, 0) -= sphi * cphi * dx;
594 sm(3, 1) += cphi * cphi * dx;
595 sm(3, 2) -= sphi * cphi * dx * dx;
596 sm(3, 3) += cphi * cphi * dx * dx;
600 sv(2) -= sphi * w * dx;
601 sv(3) += cphi * w * dx;
613 double dydx = par(2);
614 double dzdx = par(3);
618 double dsdx = std::hypot(1., std::hypot(dydx, dzdx));
620 double pos[3] = {x0, y0, z0};
621 double dir[3] = {1. / dsdx, dydx / dsdx, dzdx / dsdx};
622 double poserr[3] = {0., 0., 0.};
623 double direrr[3] = {0., 0., 0.};
ublas::symmetric_matrix< double, ublas::lower, ublas::row_major, ublas::bounded_array< double, N *(N+1)/2 > > type
Geometry description of a TPC wireThe wire is a single straight segment on a wire plane...
void growSeedIntoTracks(detinfo::DetectorPropertiesData const &detProp, const bool pfseed, const recob::Seed &seed, const Hits &hpsit, Hits &unusedhits, Hits &hits, std::deque< KGTrack > &kgtracks)
TrackDirection
Track direction enum.
Basic Kalman filter track class, plus one measurement on same surface.
Utilities related to art service access.
void fillHits(art::PtrVector< recob::Hit > &hits, std::vector< unsigned int > &hittpindex) const
Fill a PtrVector of Hits.
ClusterModuleLabel join with tracks
A KHitContainer for KHitWireLine type measurements.
A KHitContainer for KHitWireX type measurements.
process_name opflash particleana ie x
const KHitTrack & endTrack() const
Track at end point.
size_t fMinSeedHits
Minimum number of hits per track seed.
geo::WireID WireID() const
Declaration of signal hit object.
void filterHitsOnKalmanTrack(const KGTrack &trg, Hits &hits, Hits &seederhits) const
Filter hits that are on kalman tracks.
EResult err(const char *call)
void GetPoint(double *Pt, double *Err) const
std::vector< trkf::KalmanOutput > makeTracks(detinfo::DetectorClocksData const &clockData, detinfo::DetectorPropertiesData const &detProp, KalmanInputs &kalman_inputs)
bool fDoDedx
Global dE/dx enable flag.
double fMaxSeedChiDF
Maximum seed track chisquare/dof.
Track3DKalmanHitAlg(const fhicl::ParameterSet &pset)
Constructor.
int fMinSeedChopHits
Potentially chop seeds that exceed this length.
Track3DKalmanHit Algorithm.
double ThetaZ() const
Returns angle of wire with respect to z axis in the Y-Z plane in radians.
void chopHitsOffSeeds(Hits const &hpsit, bool pfseed, Hits &seedhits) const
Chop hits off of the end of seeds.
double fInitialMomentum
Initial (or constant) momentum.
recob::Seed makeSeed(detinfo::DetectorPropertiesData const &detProp, const Hits &hits) const
Make seed method.
bool makeKalmanTracks(detinfo::DetectorPropertiesData const &detProp, const std::shared_ptr< trkf::Surface > psurf, const Surface::TrackDirection trkdir, Hits &seedhits, Hits &hits, std::deque< KGTrack > &kalman_tracks)
bool buildTrack(const KTrack &trk, KGTrack &trg, const Propagator &prop, const Propagator::PropDirection dir, KHitContainer &hits, bool linear) const
Make a new track.
void growSeedsIntoTracks(detinfo::DetectorPropertiesData const &detProp, const bool pfseed, const std::vector< recob::Seed > &seeds, const std::vector< Hits > &hitsperseed, Hits &unusedhits, Hits &hits, std::deque< KGTrack > &kalman_tracks)
Grow Seeds method.
ublas::vector< double, ublas::bounded_array< double, N > > type
bool syminvert(ublas::symmetric_matrix< T, TRI, L, A > &m)
bool extendTrack(KGTrack &trg, const Propagator &prop, KHitContainer &hits) const
Add hits to existing track.
std::unique_ptr< KHitContainer > fillHitContainer(detinfo::DetectorPropertiesData const &detProp, const Hits &hits) const
Fill hit container with either seedhits or filtered hits i.e. recob::Hit.
void fetchPFParticleSeeds(const art::PtrVector< recob::Seed > &pfseeds, const std::vector< Hits > &pfseedhits, std::vector< recob::Seed > &seeds, std::vector< Hits > &hitsperseed) const
Fetch Seeds method.
KVector< 5 >::type TrackVector
Track state vector, dimension 5.
Definition of data types for geometry description.
bool smoothTrack(KGTrack &trg, KGTrack *trg1, const Propagator &prop) const
Smooth track.
KalmanFilterAlg fKFAlg
Kalman filter algorithm.
Kalman filter linear algebra typedefs.
bool smoothandextendTrack(detinfo::DetectorPropertiesData const &detProp, Propagator const &propagator, KGTrack &trg0, const Hits hits, unsigned int prefplane, std::deque< KGTrack > &kalman_tracks)
SMooth and extend track.
std::vector< recob::Seed > GetSeedsFromUnSortedHits(detinfo::DetectorClocksData const &clockData, detinfo::DetectorPropertiesData const &detProp, art::PtrVector< recob::Hit > const &, std::vector< art::PtrVector< recob::Hit >> &, unsigned int StopAfter=0) const
Encapsulate the geometry of a wire.
double ConvertTicksToX(double ticks, int p, int t, int c) const
std::vector< TrajPoint > seeds
art::PtrVector< recob::Hit > Hits
void fitnupdateMomentum(Propagator const &propagator, KGTrack &trg1, KGTrack &trg2) const
fit and update method, used twice.
float PeakTime() const
Time of the signal peak, in tick units.
Contains all timing reference information for the detector.
int fNumTrack
Number of tracks produced.
bool testSeedSlope(const double *dir) const
void getMomentum(double mom[3]) const
Get momentum vector of track.
void setPlane(int plane)
Set preferred view plane.
bool fLineSurface
Line surface flag.
size_t numHits() const
Number of measurements in track.
SeedFinderAlgorithm fSeedFinderAlg
Seed finder.
Basic Kalman filter track class, with error.
2D representation of charge deposited in the TDC/wire plane
bool qualityCutsOnSeedTrack(const KGTrack &trg0) const
Quality cuts on seed track.
void GetCenter(double *xyz, double localz=0.0) const
Fills the world coordinate of a point on the wire.
int fMaxChopHits
Maximum number of hits to chop from each end of seed.
bool empty(FixedBins< T, C > const &) noexcept
bool fitMomentum(const KGTrack &trg, const Propagator &prop, KETrack &tremom) const
Estimate track momentum using either range or multiple scattering.
bool updateMomentum(const KETrack &tremom, const Propagator &prop, KGTrack &trg) const
Set track momentum at each track surface.
std::vector< KalmanInput > KalmanInputs
Basic Kalman filter track class, without error.
void GetDirection(double *Dir, double *Err) const
double getChisq() const
Fit chisquare.
bool fSelfSeed
Self seed flag.
BEGIN_PROLOG sequence::SlidingWindowTriggerPatternsWindowPair END_PROLOG trigslidewindowOR6m output outputs
const KHitTrack & startTrack() const
Track at start point.
art framework interface to geometry description
double fMinSeedSlope
Minimum seed slope (dx/dz).
bool extendandsmoothLoop(detinfo::DetectorPropertiesData const &detProp, Propagator const &propagator, KGTrack &trg1, unsigned int prefplane, Hits &trackhits) const
SMooth and extend a track in a loop.
double fMaxTcut
Maximum delta ray energy in MeV for restricted dE/dx.
std::shared_ptr< Surface > makeSurface(const recob::Seed &seed, double *dir) const
method to return a seed to surface.