All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BlurredClusteringAlg.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////
2 // Implementation of the Blurred Clustering algorithm
3 //
4 // Converts a hit map into a 2D image of the hits before convoling
5 // with a Gaussian function to introduce a weighted blurring.
6 // Clustering proceeds on this blurred image to create more
7 // complete clusters.
8 //
9 // M Wallbank (m.wallbank@sheffield.ac.uk), May 2015
10 ////////////////////////////////////////////////////////////////////
11 
12 #ifndef BlurredClustering_h
13 #define BlurredClustering_h
14 
15 // Framework includes
16 #include "art/Framework/Services/Registry/ServiceHandle.h"
17 #include "canvas/Persistency/Common/Ptr.h"
18 #include "canvas/Persistency/Common/PtrVector.h"
19 
20 // LArSoft includes
24 namespace detinfo {
25  class DetectorProperties;
26 }
27 namespace fhicl {
28  class ParameterSet;
29 }
30 namespace lariov {
31  class ChannelStatusProvider;
32 }
33 namespace geo {
34  struct WireID;
35 }
36 
37 // ROOT
38 #include "TString.h"
39 class TCanvas;
40 class TH2F;
41 
42 // c++
43 #include <array>
44 #include <string>
45 #include <vector>
46 
47 namespace cluster {
48  class BlurredClusteringAlg;
49 }
50 
52 public:
53  BlurredClusteringAlg(fhicl::ParameterSet const& pset);
55 
56  /// Create the PDF to save debug images
57  void CreateDebugPDF(int run, int subrun, int event);
58 
59  /// Takes a vector of clusters (itself a vector of hits) and turns them into clusters using the initial hit selection
60  void ConvertBinsToClusters(std::vector<std::vector<double>> const& image,
61  std::vector<std::vector<int>> const& allClusterBins,
62  std::vector<art::PtrVector<recob::Hit>>& clusters) const;
63 
64  /// Takes hit map and returns a 2D vector representing wire and tick, filled with the charge
65  std::vector<std::vector<double>> ConvertRecobHitsToVector(
66  std::vector<art::Ptr<recob::Hit>> const& hits,
67  int readoutWindowSize);
68 
69  /// Find clusters in the histogram
70  int FindClusters(std::vector<std::vector<double>> const& image,
71  std::vector<std::vector<int>>& allcluster) const;
72 
73  /// Find the global wire position
74  int GlobalWire(geo::WireID const& wireID) const;
75 
76  /// Applies Gaussian blur to image
77  std::vector<std::vector<double>> GaussianBlur(
78  std::vector<std::vector<double>> const& image) const;
79 
80  /// Minimum size of cluster to save
81  unsigned int
82  GetMinSize() const noexcept
83  {
84  return fMinSize;
85  }
86 
87  /// Converts a 2D vector in a histogram for the debug pdf
88  TH2F* MakeHistogram(std::vector<std::vector<double>> const& image, TString name) const;
89 
90  /// Save the images for debugging
91  /// This version takes the final clusters and overlays on the hit map
92  void SaveImage(TH2F* image,
93  std::vector<art::PtrVector<recob::Hit>> const& allClusters,
94  int pad,
95  int tpc,
96  int plane);
97 
98  /// Save the images for debugging
99  void SaveImage(TH2F* image, int pad, int tpc, int plane);
100 
101  /// Save the images for debugging
102  /// This version takes a vector of bins and overlays the relevant bins on the hit map
103  void SaveImage(TH2F* image,
104  std::vector<std::vector<int>> const& allClusterBins,
105  int pad,
106  int tpc,
107  int plane);
108 
109 private:
110  /// Converts a vector of bins into a hit selection - not all the hits in the bins vector are real hits
111  art::PtrVector<recob::Hit> ConvertBinsToRecobHits(std::vector<std::vector<double>> const& image,
112  std::vector<int> const& bins) const;
113 
114  /// Converts a bin into a recob::Hit (not all of these bins correspond to recob::Hits - some are fake hits created by the blurring)
115  art::Ptr<recob::Hit> ConvertBinToRecobHit(std::vector<std::vector<double>> const& image,
116  int bin) const;
117 
118  /// Converts an xbin and a ybin to a global bin number
119  int ConvertWireTickToBin(std::vector<std::vector<double>> const& image, int xbin, int ybin) const;
120 
121  /// Returns the charge stored in the global bin value
122  double ConvertBinToCharge(std::vector<std::vector<double>> const& image, int bin) const;
123 
124  /// Count how many dead wires there are in the blurring region for a particular hit
125  /// Returns a pair of counters representing how many dead wires there are below and above the hit respectively
126  std::pair<int, int> DeadWireCount(int wire_bin, int width) const;
127 
128  /// Dynamically find the blurring radii and Gaussian sigma in each dimension
129  std::array<int, 4> FindBlurringParameters() const;
130 
131  /// Returns the hit time of a hit in a particular bin
132  double GetTimeOfBin(std::vector<std::vector<double>> const& image, int bin) const;
133 
134  /// Makes all the kernels which could be required given the tuned parameters
135  std::vector<std::vector<std::vector<double>>> MakeKernels() const;
136 
137  /// Determines the number of clustered neighbours of a hit
138  unsigned int NumNeighbours(int nx, std::vector<bool> const& used, int bin) const;
139 
140  /// Determine if a hit is within a time threshold of any other hits in a cluster
141  bool PassesTimeCut(std::vector<double> const& times, double time) const;
142 
143  bool fDebug;
144  std::string fDetector;
145 
146  // Parameters used in the Blurred Clustering algorithm
147  int fBlurWire; // blur radius for Gauss kernel in the wire direction
148  int fBlurTick; // blur radius for Gauss kernel in the tick direction
149  double fSigmaWire; // sigma for Gaussian kernel in the wire direction
150  double fSigmaTick; // sigma for Gaussian kernel in the tick direction
151  int fMaxTickWidthBlur; // maximum distance to blur a hit based on its natural width in time
152  int fClusterWireDistance; // how far to cluster from seed in wire direction
153  int fClusterTickDistance; // how far to cluster from seed in tick direction
154  unsigned int fNeighboursThreshold; // min. number of neighbors to add to cluster
155  unsigned int fMinNeighbours; // minumum number of neighbors to keep in the cluster
156  unsigned int fMinSize; // minimum size for cluster
157  double fMinSeed; // minimum seed after blurring needed before clustering proceeds
158  double fTimeThreshold; // time threshold for clustering
159  double fChargeThreshold; // charge threshold for clustering
160 
161  // Blurring stuff
163  std::vector<std::vector<std::vector<double>>> fAllKernels;
164 
165  // Hit containers
166  std::vector<std::vector<art::Ptr<recob::Hit>>> fHitMap;
167  std::vector<bool> fDeadWires;
168 
171 
172  // For the debug pdf
173  TCanvas* fDebugCanvas{nullptr};
174  std::string fDebugPDFName{};
175 
176  // art service handles
177  art::ServiceHandle<geo::Geometry const> fGeom;
179  art::ServiceHandle<lariov::ChannelStatusService const>()->GetProvider()};
180 };
181 
182 #endif
BlurredClusteringAlg(fhicl::ParameterSet const &pset)
TH2F * MakeHistogram(std::vector< std::vector< double >> const &image, TString name) const
Converts a 2D vector in a histogram for the debug pdf.
std::vector< std::vector< art::Ptr< recob::Hit > > > fHitMap
std::vector< std::vector< std::vector< double > > > MakeKernels() const
Makes all the kernels which could be required given the tuned parameters.
process_name cluster
Definition: cheaterreco.fcl:51
double GetTimeOfBin(std::vector< std::vector< double >> const &image, int bin) const
Returns the hit time of a hit in a particular bin.
bool PassesTimeCut(std::vector< double > const &times, double time) const
Determine if a hit is within a time threshold of any other hits in a cluster.
Declaration of signal hit object.
unsigned int NumNeighbours(int nx, std::vector< bool > const &used, int bin) const
Determines the number of clustered neighbours of a hit.
std::array< int, 4 > FindBlurringParameters() const
Dynamically find the blurring radii and Gaussian sigma in each dimension.
int FindClusters(std::vector< std::vector< double >> const &image, std::vector< std::vector< int >> &allcluster) const
Find clusters in the histogram.
constexpr details::BinObj< T > bin(T value)
Returns a wrapper to print the specified data in binary format.
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:265
art::PtrVector< recob::Hit > ConvertBinsToRecobHits(std::vector< std::vector< double >> const &image, std::vector< int > const &bins) const
Converts a vector of bins into a hit selection - not all the hits in the bins vector are real hits...
unsigned int GetMinSize() const noexcept
Minimum size of cluster to save.
void CreateDebugPDF(int run, int subrun, int event)
Create the PDF to save debug images.
std::pair< int, int > DeadWireCount(int wire_bin, int width) const
double ConvertBinToCharge(std::vector< std::vector< double >> const &image, int bin) const
Returns the charge stored in the global bin value.
IDparameter< geo::WireID > WireID
Member type of validated geo::WireID parameter.
int ConvertWireTickToBin(std::vector< std::vector< double >> const &image, int xbin, int ybin) const
Converts an xbin and a ybin to a global bin number.
art::Ptr< recob::Hit > ConvertBinToRecobHit(std::vector< std::vector< double >> const &image, int bin) const
Converts a bin into a recob::Hit (not all of these bins correspond to recob::Hits - some are fake hit...
int GlobalWire(geo::WireID const &wireID) const
Find the global wire position.
std::vector< std::vector< double > > GaussianBlur(std::vector< std::vector< double >> const &image) const
Applies Gaussian blur to image.
void SaveImage(TH2F *image, std::vector< art::PtrVector< recob::Hit >> const &allClusters, int pad, int tpc, int plane)
Class providing information about the quality of channels.
art::ServiceHandle< geo::Geometry const > fGeom
void ConvertBinsToClusters(std::vector< std::vector< double >> const &image, std::vector< std::vector< int >> const &allClusterBins, std::vector< art::PtrVector< recob::Hit >> &clusters) const
Takes a vector of clusters (itself a vector of hits) and turns them into clusters using the initial h...
std::vector< std::vector< double > > ConvertRecobHitsToVector(std::vector< art::Ptr< recob::Hit >> const &hits, int readoutWindowSize)
Takes hit map and returns a 2D vector representing wire and tick, filled with the charge...
then echo fcl name
std::vector< std::vector< std::vector< double > > > fAllKernels
Interface for experiment-specific service for channel quality info.
lariov::ChannelStatusProvider const & fChanStatus
art framework interface to geometry description