All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DlVertexingAlgorithm.h
Go to the documentation of this file.
1 /**
2  * @file larpandoradlcontent/LArVertexing/DlVertexingAlgorithm.h
3  *
4  * @brief Header file for the deep learning vertexing algorithm.
5  *
6  * $Log: $
7  */
8 #ifndef LAR_DL_VERTEXING_ALGORITHM_H
9 #define LAR_DL_VERTEXING_ALGORITHM_H 1
10 
11 #include "Pandora/Algorithm.h"
12 #include "Pandora/AlgorithmHeaders.h"
13 
15 
17 
18 #include <random>
19 
20 using namespace lar_content;
21 
22 namespace lar_dl_content
23 {
24 /**
25  * @brief DeepLearningTrackShowerIdAlgorithm class
26  */
27 class DlVertexingAlgorithm : public pandora::Algorithm
28 {
29 public:
30  typedef std::map<std::pair<int, int>, std::vector<const pandora::CaloHit *>> PixelToCaloHitsMap;
31 
32  /**
33  * @brief Default constructor
34  */
36 
37  virtual ~DlVertexingAlgorithm();
38 
39 private:
41  {
42  public:
43  VertexTuple(const pandora::Pandora &pandora, const pandora::CartesianVector &vertexU, const pandora::CartesianVector &vertexV,
44  const pandora::CartesianVector &vertexW);
45 
46  VertexTuple(const pandora::Pandora &pandora, const pandora::CartesianVector &vertex1, const pandora::CartesianVector &vertex2,
47  const pandora::HitType view1, const pandora::HitType view2);
48 
49  const pandora::CartesianVector &GetPosition() const;
50  float GetChi2() const;
51  std::string ToString() const;
52 
53  private:
54  pandora::CartesianVector m_pos; ///< Calculated 3D position
55  float m_chi2; ///< Chi squared of calculated position
56  };
57 
58  typedef std::pair<int, int> Pixel; // A Pixel is a row, column pair
59  typedef std::vector<Pixel> PixelVector;
60 
61  pandora::StatusCode Run();
62  pandora::StatusCode ReadSettings(const pandora::TiXmlHandle xmlHandle);
63  pandora::StatusCode PrepareTrainingSample();
64  pandora::StatusCode Infer();
65 
66  /*
67  * @brief Create input for the network from a calo hit list
68  *
69  * @param caloHits The CaloHitList from which the input should be made
70  * @param networkInput The TorchInput object to populate
71  * @param pixelVector The output vector of populated pixels
72  *
73  * @return The StatusCode resulting from the function
74  **/
75  pandora::StatusCode MakeNetworkInputFromHits(
76  const pandora::CaloHitList &caloHits, LArDLHelper::TorchInput &networkInput, PixelVector &pixelVector) const;
77 
78  /*
79  * @brief Create a list of wire plane-space coordinates from a list of Pixels
80  *
81  * @param caloHits The CaloHitList from which the pixel vector was originally constructed
82  * @param pixelVector The input vector of populated pixels
83  * @param positionVector The output vector of wire plane positions
84  *
85  * @return The StatusCode resulting from the function
86  **/
87  pandora::StatusCode MakeWirePlaneCoordinatesFromPixels(
88  const pandora::CaloHitList &caloHits, const PixelVector &pixelVector, pandora::CartesianPointVector &positionVector) const;
89 
90  /*
91  * @brief Create a list of wire plane-space coordinates from a canvas
92  *
93  * @param caloHits The CaloHitList from which the pixel vector was originally constructed
94  * @param canvas The input canvas
95  * @param canvasWidth The width of the canvas
96  * @param canvasHeight The height of the canvas
97  * @param columnOffset The column offset used when populating the canvas
98  * @param rowOffset The row offset used when populating the canvas
99  * @param positionVector The output vector of wire plane positions
100  *
101  * @return The StatusCode resulting from the function
102  **/
103  pandora::StatusCode MakeWirePlaneCoordinatesFromCanvas(const pandora::CaloHitList &caloHits, float **canvas, const int canvasWidth,
104  const int canvasHeight, const int columnOffset, const int rowOffset, pandora::CartesianPointVector &positionVector) const;
105 
106  /**
107  * @brief Determines the parameters of the canvas for extracting the vertex location.
108  * The network predicts the distance that each pixel associated with a hit is located from the vertex, but says nothing about
109  * the direction. As a result, the ring describing the potential vertices associated with that hit can extend beyond the
110  * original canvas size. This function returns the size of the required canvas and the offset for the bottom left corner.
111  *
112  * @param networkOutput The TorchOutput object populated by the network inference step
113  * @param pixelVector The vector of populated pixels
114  * @param columnOffset The output column offset for the canvas
115  * @param rowOffset The output row offset for the canvas
116  * @param width The output width for the canvas
117  * @param height The output height for the canvas
118  */
119  void GetCanvasParameters(const LArDLHelper::TorchOutput &networkOutput, const PixelVector &pixelVector, int &columnOffset,
120  int &rowOffset, int &width, int &height) const;
121 
122  /**
123  * @brief Add a filled ring to the specified canvas.
124  * The ring has an inner radius based on the minimum predicted distance to the vertex and an outer radius based on the maximum
125  * predicted distance to the vertex. The centre of the ring is the location of the hit used to predict the distance to the
126  * vertex. Each pixel to be filled is augmented by the specified weight. In this way, once all hits have been considered, a
127  * consensus view emerges of the likely vertex location based on the overlap of various rings centred at different locations.
128  *
129  * The underlying implementation is a variant of the Bresenham midpoint circle algorithm and therefore only computes pixel
130  * coordinates for one octant of each circle (one of radius 'inner', one of radius 'outer') and interpolates the fill between
131  * points using integer arithmetic, guaranteeing each pixel of the ring is filled once and only once, and then mirrored to the
132  * remaining seven octants.
133  *
134  * @param networkOutput The TorchOutput object populated by the network inference step
135  * @param pixelVector The vector of populated pixels
136  * @param thresholds The fractional distance thresholds representing the classes predicted by the network
137  * @param columnOffset The output column offset for the canvas
138  * @param rowOffset The output row offset for the canvas
139  * @param width The output width for the canvas
140  * @param height The output height for the canvas
141  */
142  void DrawRing(float **canvas, const int row, const int col, const int inner, const int outer, const float weight) const;
143 
144  /**
145  * @brief Update the coordinates along the loci of a circle.
146  * When drawing the ring we need an efficient means to determine the next pixel defining the inner and outer loci of the ring.
147  * This update function uses the Bresenham midpoint circle update function to determine this location. The row position is
148  * always incremented by 1 pixel, the column position is left unchanged, or decremented by 1 pixel to best follow the arc of
149  * the true underlying circle.
150  *
151  * @param radius2 The squared radius of the circle under consideration
152  * @param col The input/output column position to (potentially) update
153  * @param row The input/output row position to update
154  */
155  void Update(const int radius, int &col, int &row) const;
156 
157  /*
158  * @brief Retrieve the map from MC to calo hits for reconstructable particles
159  *
160  * @param mcToHitsMap The map to populate
161  *
162  * @return The StatusCode resulting from the function
163  **/
164  pandora::StatusCode GetMCToHitsMap(LArMCParticleHelper::MCContributionMap &mcToHitsMap) const;
165 
166  /*
167  * @brief Construct a list of the MC particles from the MC to calo hits map, completing the interaction hierarchy with the invisible
168  * upstream particles.
169  *
170  * @param mcToHitsMap The map of reconstructible MC particles to calo hits
171  * @param mcHierarchy The output list of MC particles representing the interaction
172  *
173  * @return The StatusCode resulting from the function
174  **/
175  pandora::StatusCode CompleteMCHierarchy(const LArMCParticleHelper::MCContributionMap &mcToHitsMap, pandora::MCParticleList &mcHierarchy) const;
176 
177  /*
178  * @brief Determine the physical bounds associated with a CaloHitList.
179  *
180  * @param caloHitList The CaloHitList for which bounds should be determined
181  * @param xMin The output minimum x value
182  * @param xMax The output maximum x value
183  * @param zMin The output minimum z value
184  * @param zMax The output maximum z value
185  *
186  * @return The StatusCode resulting from the function
187  */
188  void GetHitRegion(const pandora::CaloHitList &caloHitList, float &xMin, float &xMax, float &zMin, float &zMax) const;
189 
190  /**
191  * @brief Create a vertex list from the candidate vertices.
192  *
193  * @param candidates The candidate positions with which to create the list.
194  *
195  * @return The StatusCode resulting from the function
196  */
197  pandora::StatusCode MakeCandidateVertexList(const pandora::CartesianPointVector &positions);
198 
199  /**
200  * @brief Retrieve the true neutrino vertex position.
201  */
202  void GetTrueVertexPosition(float &x, float &y, float &z) const;
203 
204  /**
205  * @brief Retrieve the true neutrino vertex position.
206  */
207  void GetTrueVertexPosition(float &x, float &u, float &v, float &w) const;
208 
209  /**
210  * @brief Retrieve the true neutrino vertex.
211  */
212  const pandora::CartesianVector &GetTrueVertex() const;
213 
214  /**
215  * @brief Populate a root true with vertex information.
216  */
217  void PopulateRootTree(const std::vector<VertexTuple> &vertexTuples, const pandora::CartesianPointVector &vertexCandidatesU,
218  const pandora::CartesianPointVector &vertexCandidatesV, const pandora::CartesianPointVector &vertexCandidatesW) const;
219 
220  bool m_trainingMode; ///< Training mode
221  std::string m_trainingOutputFile; ///< Output file name for training examples
222  std::string m_inputVertexListName; ///< Input vertex list name if 2nd pass
223  std::string m_outputVertexListName; ///< Output vertex list name
224  pandora::StringVector m_caloHitListNames; ///< Names of input calo hit lists
225  LArDLHelper::TorchModel m_modelU; ///< The model for the U view
226  LArDLHelper::TorchModel m_modelV; ///< The model for the V view
227  LArDLHelper::TorchModel m_modelW; ///< The model for the W view
228  int m_event; ///< The current event number
229  int m_pass; ///< The pass of the train/infer step
230  int m_nClasses; ///< The number of distance classes
231  int m_height; ///< The height of the images
232  int m_width; ///< The width of the images
233  float m_maxHitAdc; ///< Maximum ADC value to allow
234  float m_regionSize; ///< The half width/height of the event region to consider in cm
235  bool m_visualise; ///< Whether or not to visualise the candidate vertices
236  bool m_writeTree; ///< Whether or not to write validation details to a ROOT tree
237  std::string m_rootTreeName; ///< The ROOT tree name
238  std::string m_rootFileName; ///< The ROOT file name
239  std::mt19937 m_rng; ///< The random number generator
240  std::vector<double> m_thresholds; ///< Distance class thresholds
241  const double PY_EPSILON{1.1920929e-7}; ///< The value of epsilon in Python
242 };
243 
244 } // namespace lar_dl_content
245 
246 #endif // LAR_DL_VERTEXING_ALGORITHM_H
pandora::CartesianVector m_pos
Calculated 3D position.
LArDLHelper::TorchModel m_modelU
The model for the U view.
process_name opflash particleana ie ie ie z
std::vector< double > m_thresholds
Distance class thresholds.
std::unordered_map< const pandora::MCParticle *, pandora::CaloHitList > MCContributionMap
process_name opflash particleana ie x
bool m_visualise
Whether or not to visualise the candidate vertices.
int m_event
The current event number.
pandora::StringVector m_caloHitListNames
Names of input calo hit lists.
torch::jit::script::Module TorchModel
Definition: LArDLHelper.h:25
LArDLHelper::TorchModel m_modelV
The model for the V view.
float m_regionSize
The half width/height of the event region to consider in cm.
float m_maxHitAdc
Maximum ADC value to allow.
std::string m_outputVertexListName
Output vertex list name.
std::mt19937 m_rng
The random number generator.
int m_nClasses
The number of distance classes.
process_name opflash particleana ie ie y
std::string m_rootTreeName
The ROOT tree name.
Header file for the lar monte carlo particle helper helper class.
DeepLearningTrackShowerIdAlgorithm class.
bool m_writeTree
Whether or not to write validation details to a ROOT tree.
Header file for the lar deep learning helper helper class.
int m_height
The height of the images.
int m_pass
The pass of the train/infer step.
std::map< std::pair< int, int >, std::vector< const pandora::CaloHit * > > PixelToCaloHitsMap
std::string m_inputVertexListName
Input vertex list name if 2nd pass.
LArDLHelper::TorchModel m_modelW
The model for the W view.
required by fuzzyCluster table::sbnd_g4_services gaushitTruthMatch pandora
Definition: reco_sbnd.fcl:182
std::string m_trainingOutputFile
Output file name for training examples.
std::string m_rootFileName
The ROOT file name.
std::string ToString(const T &value)
Parse flashmatch::PSet configuration file content.
Definition: Parser.h:66
int m_width
The width of the images.
float m_chi2
Chi squared of calculated position.
bool Update(detinfo::DetectorClocksData const &clockData, detinfo::DetectorPropertiesData const &detProp, const TCSlice &slc, PFPStruct &pfp, bool prt)
Definition: PFPUtils.cxx:1057