LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
ClusterCrawlerAlg.h
Go to the documentation of this file.
1 // ClusterCrawlerAlg.h
3 //
4 // ClusterCrawlerAlg class
5 //
6 // Bruce Baller
7 //
9 #ifndef CLUSTERCRAWLERALG_H
10 #define CLUSTERCRAWLERALG_H
11 
12 
13 // C/C++ standard libraries
14 #include <array>
15 #include <map>
16 #include <vector>
17 #include <memory> // std::move()
18 #include <utility> // std::pair<>
19 
20 // framework libraries
21 #include "fhiclcpp/ParameterSet.h"
23 
24 // LArSoft libraries
32 
33 
34 namespace cluster {
35 
37  public:
38 
39  // some functions to handle the CTP_t type
40  typedef unsigned int CTP_t;
41 
42  static constexpr unsigned int CTPpad = 1000; // alignment for CTP sub-items
43  static CTP_t EncodeCTP
44  (unsigned int cryo, unsigned int tpc, unsigned int plane)
45  { return cryo * CTPpad*CTPpad + tpc * CTPpad + plane; }
46  static CTP_t EncodeCTP(const geo::PlaneID& planeID)
47  { return EncodeCTP(planeID.Cryostat, planeID.TPC, planeID.Plane); }
48  static geo::PlaneID DecodeCTP(CTP_t CTP)
49  { return { CTP / (CTPpad*CTPpad), CTP / CTPpad % CTPpad, CTP % CTPpad }; }
50 
53 
55  struct ClusterStore {
56  short ID; // Cluster ID. ID < 0 = abandoned cluster
57  short ProcCode; // Processor code for debugging
58  short StopCode; // code for the reason for stopping cluster tracking
59  CTP_t CTP; // Cryostat/TPC/Plane code
60  float BeginSlp; // beginning slope (= DS end = high wire number)
61  float BeginSlpErr; // error
62  float BeginAng;
63  unsigned int BeginWir; // begin wire
64  float BeginTim; // begin time
65  float BeginChg; // beginning average charge
66  float BeginChgNear; // charge near the cluster Begin
67  short BeginVtx; // ID of the Begin vertex
68  float EndSlp; // end slope (= US end = low wire number)
69  float EndAng;
70  float EndSlpErr; // error
71  unsigned int EndWir; // end wire
72  float EndTim; // ending time
73  float EndChg; // ending average charge
74  float EndChgNear; // charge near the cluster End
75  short EndVtx; // ID of the end vertex
76  std::vector<unsigned int> tclhits; // hits on the cluster
77  }; // ClusterStore
78 
80  struct VtxStore {
81  float Wire;
82  float WireErr;
83  float Time;
84  float TimeErr;
85  unsigned short NClusters; // = 0 for abandoned vertices
86  float ChiDOF;
87  short Topo; // 1 = US-US, 2 = US-DS, 3 = DS-US, 4 = DS-DS, 5 = Star,
88  // 6 = hammer, 7 = vtx3clustermatch, 8 = vtx3clustersplit, 9 = FindTrajVertices
89  CTP_t CTP;
90  bool Fixed; // Vertex position fixed (should not be re-fit)
91  };
92 
94  struct Vtx3Store {
95  std::array<short, 3> Ptr2D; // pointers to 2D vertices in each plane
96  float X; // x position
97  float XErr; // x position error
98  float Y; // y position
99  float YErr; // y position error
100  float Z; // z position
101  float ZErr; // z position error
102  short Wire; // wire number for an incomplete 3D vertex
103  unsigned short CStat;
104  unsigned short TPC;
105  unsigned short ProcCode;
106  };
107 
109 
110  virtual void reconfigure(fhicl::ParameterSet const& pset);
111  void RunCrawler(std::vector<recob::Hit> const& srchits);
112 
115 
116  std::vector<short> const& GetinClus() const {return inClus; }
117 
119  std::vector<recob::Hit>&& YieldHits() { return std::move(fHits); }
120 
122  std::vector<recob::Hit> GetHits() { return fHits; }
123 
125  std::vector<ClusterStore> const& GetClusters() const { return tcl; }
126 
128  std::vector<VtxStore> const& GetEndPoints() const { return vtx; }
129 
131  std::vector<Vtx3Store> const& GetVertices() const { return vtx3; }
132 
133 
136  void ClearResults();
137 
139 
141  static bool SortByMultiplet(recob::Hit const& a, recob::Hit const& b);
142 
144 
145  private:
146 
147  unsigned short fNumPass;
148  std::vector<unsigned short> fMaxHitsFit;
149  std::vector<unsigned short> fMinHits;
150  std::vector<unsigned short> fNHitsAve;
151  std::vector<float> fChiCut;
153  std::vector<float> fKinkChiRat;
154  std::vector<float> fKinkAngCut;
156  std::vector<float> fChgCut;
157  std::vector<unsigned short> fMaxWirSkip;
158  std::vector<unsigned short> fMinWirAfterSkip;
159  std::vector<bool> fDoMerge;
161  std::vector<float> fTimeDelta;
162  std::vector<float> fMergeChgCut;
163  std::vector<bool> fFindVertices;
164  std::vector<bool> fLACrawl;
166  // bool fFindVLAClusters; ///< look for Very Large Angle clusters
168 
169  std::vector<float> fMinAmp;
170 
175  // bool fFindTrajVertices;
176 
177  // global cuts and parameters
178  float fHitErrFac;
179  float fHitMinAmp;
181  float fMinHitFrac;
183  unsigned short fLAClusMaxHitsFit;
187  float fMergeOverlapAngCut;
189  unsigned short fAllowNoHitWire;
190  float fVertex2DCut;
192  float fVertex3DCut;
193 
196  int fDebugHit;
197 
198  // Wires that have been determined by some filter (e.g. NoiseFilter) to be good
199  std::vector<geo::WireID> fFilteredWires;
200 
201  // these variables define the cluster used during crawling
202  float clpar[3];
203  float clparerr[2];
205  float clChisq;
206  float fAveChg;
207  // float fChgRMS; ///< average charge RMS at leading edge of cluster
208  float fChgSlp;
209  float fAveHitWidth;
210 
211  bool prt;
212  bool vtxprt;
213  unsigned short NClusters;
214 
216 
217  std::vector<recob::Hit> fHits;
218  std::vector<short> inClus;
219  std::vector<bool> mergeAvailable;
220  std::vector< ClusterStore > tcl;
221  std::vector< VtxStore > vtx;
222  std::vector< Vtx3Store > vtx3;
223 
225 
226  float clBeginSlp;
227  float clBeginAng;
229  unsigned int clBeginWir;
230  float clBeginTim;
231  float clBeginChg;
233  float clEndSlp;
234  float clEndAng;
235  float clEndSlpErr;
236  unsigned int clEndWir;
237  float clEndTim;
238  float clEndChg;
239  float clEndChgNear;
240  short clStopCode;
241  short clProcCode;
251  CTP_t clCTP;
263  bool clLA;
264 
265  unsigned int fFirstWire;
266  unsigned int fFirstHit;
267  unsigned int fLastWire;
268  unsigned int cstat; // the current cryostat
269  unsigned int tpc; // the current TPC
270  unsigned int plane; // the current plane
271  unsigned int fNumWires; // number of wires in the current plane
272  unsigned int fMaxTime; // number of time samples in the current plane
273  float fScaleF;
274 
275  unsigned short pass;
276 
277  // vector of pairs of first (.first) and last+1 (.second) hit on each wire
278  // in the range fFirstWire to fLastWire. A value of -2 indicates that there
279  // are no hits on the wire. A value of -1 indicates that the wire is dead
280  std::vector< std::pair<int, int> > WireHitRange;
281 
282  std::vector<unsigned int> fcl2hits;
283  std::vector<float> chifits;
284  std::vector<short> hitNear;
285 
287  std::vector<float> chgNear;
289  float fChgNearCut;
290 
292  std::string fhitsModuleLabel;
293  // ******** crawling routines *****************
294 
295  // Loops over wires looking for seed clusters
296  void ClusterLoop();
297  // Returns true if the hits on a cluster have a consistent width
298  bool ClusterHitsOK(short nHitChk);
299  // Finds a hit on wire kwire, adds it to the cluster and re-fits it
300  void AddHit(unsigned int kwire, bool& HitOK, bool& SigOK);
301  // Finds a hit on wire kwire, adds it to a LargeAngle cluster and re-fits it
302  void AddLAHit(unsigned int kwire, bool& ChkCharge, bool& HitOK, bool& SigOK);
303  // Fits the cluster hits in fcl2hits to a straight line
304  void FitCluster();
305  // Fits the charge of the cluster hits in fcl2hits
306  void FitClusterChg();
307  // Fits the middle of a temporary cluster it1 using hits iht to iht + nhit
308  void FitClusterMid(unsigned short it1, unsigned int iht, short nhit);
309  void FitClusterMid(std::vector<unsigned int>& hitVec, unsigned int iht, short nhit);
310  // Crawls along a trail of hits UpStream
311  void CrawlUS();
312  // Crawls along a trail of hits UpStream - Large Angle version
313  void LACrawlUS();
314 
315  void KillGarbageClusters();
316 
317  // ************** cluster merging routines *******************
318 
319  // Compares two cluster combinations to see if they should be merged
320  void ChkMerge();
321  // Checks merge for cluster cl2 within the bounds of cl1
322  void ChkMerge12(unsigned short it1, unsigned short it2, bool& didit);
323  // Merges clusters cl1 and cl2
324  void DoMerge(unsigned short it1, unsigned short it2, short ProcCode);
325 
326  // ************** hit merging routines *******************
327  // check the number of nearby hits to the ones added to the current cluster.
328  // If there are too many, merge the hits and re-fit
329  void ChkClusterNearbyHits(bool prt);
330  // merge the hits in a multiplet into one hit
331  void MergeHits(const unsigned int theHit, bool& didMerge);
334  (size_t begin, size_t end, short int multiplicity = -1);
335 
336  // ************** cluster finish routines *******************
337 
338  // Check the fraction of wires with hits
339  void CheckClusterHitFrac(bool prt);
340 
341  // Try to extend clusters downstream
342  void ChkClusterDS();
343 
344  // Try to merge overlapping clusters
345  void MergeOverlap();
346 
348  void MakeClusterObsolete(unsigned short icl);
350  void RestoreObsoleteCluster(unsigned short icl);
351 
353  void RemoveObsoleteHits();
354 
355  // ************** 2D vertex routines *******************
356 
357  // Find 2D vertices
358  void FindVertices();
359  // Find 2D star topology vertices
360  void FindStarVertices();
361  // check a vertex (vw, fvt) made with clusters it1, and it2 against the
362  // vector of existing clusters
363  void ChkVertex(float fvw, float fvt, unsigned short it1, unsigned short it2, short topo);
364  // try to attach a cluster to an existing vertex
365  void ClusterVertex(unsigned short it2);
366  // try to attach a cluster to a specified vertex
367  void VertexCluster(unsigned short ivx);
368  // Refine cluster ends near vertices
369  void RefineVertexClusters(unsigned short ivx);
370  // Split clusters that cross a vertex
371  bool VtxClusterSplit();
372  // returns true if a vertex is encountered while crawling
373  bool CrawlVtxChk(unsigned int kwire);
374  // returns true if this cluster is between a vertex and another
375  // cluster that is associated with the vertex
376  bool CrawlVtxChk2();
377  // use a vertex constraint to start a cluster
378  void VtxConstraint(unsigned int iwire, unsigned int ihit, unsigned int jwire, unsigned int& useHit, bool& doConstrain);
379  // fit the vertex position
380  void FitVtx(unsigned short iv);
381  // weight and fit all vertices
382  void FitAllVtx(CTP_t inCTP);
383 
384  // ************** 3D vertex routines *******************
385 
386  // match vertices between planes
387  void VtxMatch(geo::TPCID const& tpcid);
388  // Match clusters to endpoints using 3D vertex information
389  void Vtx3ClusterMatch(geo::TPCID const& tpcid);
390  // split clusters using 3D vertex information
391  void Vtx3ClusterSplit(geo::TPCID const& tpcid);
392  // look for a long cluster that stops at a short cluster in two views
393  void FindHammerClusters();
394 
395  // ************** utility routines *******************
396 
397  // inits everything
398  void CrawlInit();
399  // inits the cluster stuff
400  void ClusterInit();
401  // fills the wirehitrange vector for the supplied Cryostat/TPC/Plane code
402  void GetHitRange(CTP_t CTP);
403  // Stores cluster information in a temporary vector
404  bool TmpStore();
405  // Gets a temp cluster and puts it into the working cluster variables
406  void TmpGet(unsigned short it1);
407  // Does just what it says
408  void CalculateAveHitWidth();
409  // Shortens the fcl2hits, chifits, etc vectors by the specified amount
410  void FclTrimUS(unsigned short nTrim);
411  // Splits a cluster into two clusters at position pos. Associates the
412  // new clusters with a vertex
413  bool SplitCluster(unsigned short icl, unsigned short pos, unsigned short ivx);
414  // Counts the number of dead wires in the range spanned by fcl2hits
415  unsigned int DeadWireCount();
416  unsigned int DeadWireCount(unsigned int inWire1, unsigned int inWire2);
417  // return true if the pre-merged it1 and it2 clusters will meet the quality requirement
418  bool ChkMergedClusterHitFrac(unsigned short it1, unsigned short it2);
419  // Prints cluster information to the screen
420  void PrintClusters();
421  void PrintVertices();
422  // check for a signal on all wires between two points
423  bool ChkSignal(unsigned int iht, unsigned int jht);
424  bool ChkSignal(unsigned int wire1, float time1, unsigned int wire2, float time2);
425  // returns an angle-dependent scale factor for weighting fits, etc
426  float AngleFactor(float slope);
427  // calculate the kink angle between hits 0-2 and 3 - 5 on the leading edge of
428  // the cluster under construction
429  float EndKinkAngle();
431  bool CheckHitDuplicates(std::string location, std::string marker = "") const;
432  // Find the distance of closest approach between the end of a cluster and a (wire,tick) position
433  float DoCA(short icl, unsigned short end, float vwire, float vtick);
434  // Find the Chisq/DOF between the end of a cluster and a (wire,tick) position
435  float ClusterVertexChi(short icl, unsigned short end, unsigned short ivx);
436  // Find the Chisq/DOF between a point and a vertex
437  float PointVertexChi(float wire, float tick, unsigned short ivx);
438  std::string PrintHit(unsigned int iht);
439 
442  std::pair<size_t, size_t> FindHitMultiplet(size_t iHit) const;
443 
445 
447  static bool areInSameMultiplet(recob::Hit const& first_hit, recob::Hit const& second_hit);
448 
449 
450  }; // class ClusterCrawlerAlg
451 
452 
453 } // namespace cluster
454 
455 #endif // ifndef CLUSTERCRAWLERALG_H
float fScaleF
scale factor from Tick/Wire to dx/du
std::vector< short > hitNear
bool ClusterHitsOK(short nHitChk)
float fAveChg
average charge at leading edge of cluster
std::vector< Vtx3Store > vtx3
the 3D vertices we are reconstructing
void FitVtx(unsigned short iv)
void RemoveObsoleteHits()
Removes obsolete hits from hits, updating the indices.
void FitClusterMid(unsigned short it1, unsigned int iht, short nhit)
virtual void reconfigure(fhicl::ParameterSet const &pset)
bool ChkSignal(unsigned int iht, unsigned int jht)
std::vector< float > fTimeDelta
max time difference for matching
struct of temporary 2D vertices (end points)
float clparerr[2]
cluster parameter errors
static bool areInSameMultiplet(recob::Hit const &first_hit, recob::Hit const &second_hit)
Returns whether the two hits belong to the same multiplet.
std::vector< float > fChgCut
charge difference cut for adding a hit to a cluster
float clBeginChg
begin average charge
std::vector< unsigned short > fMinWirAfterSkip
after skipping
std::vector< recob::Hit > GetHits()
Returns the collection of reconstructed hits.
Declaration of signal hit object.
int fDebugWire
set to the Begin Wire and Hit of a cluster to print
The data type to uniquely identify a Plane.
Definition: geo_types.h:250
std::vector< ClusterStore > const & GetClusters() const
Returns a constant reference to the clusters found.
float clEndSlp
slope at the end (= US end = low wire number)
static bool SortByMultiplet(recob::Hit const &a, recob::Hit const &b)
Comparison for sorting hits by wire and hit multiplet.
std::vector< unsigned short > fMaxWirSkip
max number of wires that can be skipped while crawling
CryostatID_t Cryostat
Index of cryostat.
Definition: geo_types.h:130
void MergeHits(const unsigned int theHit, bool &didMerge)
void FixMultipletLocalIndices(size_t begin, size_t end, short int multiplicity=-1)
Resets the local index and multiplicity of all the hits in [begin;end[.
std::vector< float > fMinAmp
expected minimum signal in each wire plane
art::ServiceHandle< geo::Geometry > geom
Cluster finding and building.
CTP_t clCTP
Cryostat/TPC/Plane code.
std::vector< float > fMergeChgCut
max charge ratio for matching
float DoCA(short icl, unsigned short end, float vwire, float vtick)
float fHitErrFac
hit time error = fHitErrFac * hit RMS used for cluster fit
void AddLAHit(unsigned int kwire, bool &ChkCharge, bool &HitOK, bool &SigOK)
struct of temporary 3D vertices
float fAveHitWidth
average width (EndTick - StartTick) of hits
void FclTrimUS(unsigned short nTrim)
bool CrawlVtxChk(unsigned int kwire)
static geo::PlaneID DecodeCTP(CTP_t CTP)
void DoMerge(unsigned short it1, unsigned short it2, short ProcCode)
float fHitMinAmp
< ignore hits with Amp < this value
int fDebugHit
out detailed information while crawling
std::vector< bool > fLACrawl
Crawl Large Angle clusters on pass?
void Vtx3ClusterMatch(geo::TPCID const &tpcid)
std::vector< short > inClus
Hit used in cluster (-1 = obsolete, 0 = free)
unsigned int fLastWire
the last wire with a hit
std::vector< float > fChiCut
stop adding hits to clusters if chisq too high
float ClusterVertexChi(short icl, unsigned short end, unsigned short ivx)
float fChgNearWindow
window (ticks) for finding nearby charge
void VtxMatch(geo::TPCID const &tpcid)
static constexpr unsigned int CTPpad
std::vector< ClusterStore > tcl
the clusters we are creating
unsigned short fNumPass
number of passes over the hit collection
std::vector< recob::Hit > && YieldHits()
Returns (and loses) the collection of reconstructed hits.
std::vector< VtxStore > const & GetEndPoints() const
Returns a constant reference to the 2D end points found.
float fMergeOverlapAngCut
angle cut for merging overlapping clusters
unsigned int fFirstWire
the first wire with a hit
static CTP_t EncodeCTP(const geo::PlaneID &planeID)
unsigned int clEndWir
begin wire
float fVertex2DCut
2D vtx -> cluster matching cut (chisq/dof)
std::vector< VtxStore > vtx
the endpoints we are reconstructing
void RunCrawler(std::vector< recob::Hit > const &srchits)
std::vector< evd::details::RawDigitInfo_t >::const_iterator begin(RawDigitCacheDataClass const &cache)
std::vector< float > fKinkAngCut
kink angle cut made after fKinkChiRat
float fVertex3DCut
2D vtx -> 3D vtx matching cut (chisq/dof)
void VtxConstraint(unsigned int iwire, unsigned int ihit, unsigned int jwire, unsigned int &useHit, bool &doConstrain)
unsigned short fLAClusMaxHitsFit
max hits fitted on a Large Angle cluster
void MakeClusterObsolete(unsigned short icl)
Marks the cluster as obsolete and frees hits still associated with it.
std::vector< float > fKinkChiRat
void RestoreObsoleteCluster(unsigned short icl)
Restores an obsolete cluster.
void TmpGet(unsigned short it1)
std::vector< bool > fFindVertices
run vertexing code after clustering?
The data type to uniquely identify a TPC.
Definition: geo_types.h:195
PlaneID_t Plane
Index of the plane within its TPC.
Definition: geo_types.h:258
float clBeginSlp
begin slope (= DS end = high wire number)
Definition of data types for geometry description.
std::pair< size_t, size_t > FindHitMultiplet(size_t iHit) const
std::vector< unsigned short > fMaxHitsFit
Max number of hits fitted.
std::string PrintHit(unsigned int iht)
short clStopCode
8 = SPECIAL CODE FOR STEP CRAWLING
static CTP_t EncodeCTP(unsigned int cryo, unsigned int tpc, unsigned int plane)
float clChisq
chisq of the current fit
std::vector< std::pair< int, int > > WireHitRange
bool clLA
using Large Angle crawling code
std::vector< recob::Hit > fHits
our version of the hits
std::vector< unsigned short > fMinHits
Min number of hits to make a cluster.
float clEndChg
end average charge
std::vector< float > chifits
fit chisq for monitoring kinks, etc
std::vector< Vtx3Store > const & GetVertices() const
Returns a constant reference to the 3D vertices found.
unsigned int clBeginWir
begin wire
void ClusterVertex(unsigned short it2)
void Vtx3ClusterSplit(geo::TPCID const &tpcid)
float fLAClusAngleCut
call Large Angle Clustering code if > 0
std::vector< bool > mergeAvailable
set true if hit is with HitMergeChiCut of a neighbor hit
bool SplitCluster(unsigned short icl, unsigned short pos, unsigned short ivx)
float fClProjErrFac
cluster projection error factor
void AddHit(unsigned int kwire, bool &HitOK, bool &SigOK)
unsigned int fFirstHit
first hit used
float clBeginChgNear
nearby charge
void ChkMerge12(unsigned short it1, unsigned short it2, bool &didit)
float fHitMergeChiCut
is < cut. Set < 0 for no merging
std::vector< float > chgNear
charge near a cluster on each wire
bool fFindHammerClusters
look for hammer type clusters
std::vector< evd::details::RawDigitInfo_t >::const_iterator end(RawDigitCacheDataClass const &cache)
std::vector< short > const & GetinClus() const
Returns (and loses) the collection of reconstructed hits.
float fChgNearCut
to define a shower-like cluster
2D representation of charge deposited in the TDC/wire plane
Definition: Hit.h:49
bool ChkMergedClusterHitFrac(unsigned short it1, unsigned short it2)
float clEndChgNear
nearby charge
TPCID_t TPC
Index of the TPC within its cryostat.
Definition: geo_types.h:203
void VertexCluster(unsigned short ivx)
std::vector< unsigned int > fcl2hits
vector of hits used in the cluster
float fChgSlp
slope of the charge vs wire
void RefineVertexClusters(unsigned short ivx)
void ChkVertex(float fvw, float fvt, unsigned short it1, unsigned short it2, short topo)
float PointVertexChi(float wire, float tick, unsigned short ivx)
art framework interface to geometry description
std::vector< geo::WireID > fFilteredWires
std::vector< unsigned short > fNHitsAve
set to > 2 to do a charge fit using fNHitsAve hits
std::vector< bool > fDoMerge
try to merge clusters?
bool CheckHitDuplicates(std::string location, std::string marker="") const
Returns true if there are no duplicates in the hit list for next cluster.
ClusterCrawlerAlg(fhicl::ParameterSet const &pset)