MvaVertexSelectionAlgorithm.cc
Go to the documentation of this file.
1 /**
2  * @file larpandoracontent/LArVertex/MvaSelectionBaseAlgorithm.cc
3  *
4  * @brief Implementation of the mva vertex selection algorithm class.
5  *
6  * $Log: $
7  */
8 
9 #include "Pandora/AlgorithmHeaders.h"
10 
17 
23 
25 
27 
28 #include <random>
29 
30 using namespace pandora;
31 
32 namespace lar_content
33 {
34 
35 template <typename T>
38  m_filePathEnvironmentVariable("FW_SEARCH_PATH")
39 {
40 }
41 
42 //------------------------------------------------------------------------------------------------------------------------------------------
43 
44 template <typename T>
45 void MvaVertexSelectionAlgorithm<T>::GetVertexScoreList(const VertexVector &vertexVector, const BeamConstants &beamConstants,
46  HitKDTree2D &kdTreeU, HitKDTree2D &kdTreeV, HitKDTree2D &kdTreeW, VertexScoreList &vertexScoreList) const
47 {
48  ClusterList clustersU, clustersV, clustersW;
49  this->GetClusterLists(m_inputClusterListNames, clustersU, clustersV, clustersW);
50 
51  SlidingFitDataList slidingFitDataListU, slidingFitDataListV, slidingFitDataListW;
52  this->CalculateClusterSlidingFits(clustersU, m_minClusterCaloHits, m_slidingFitWindow, slidingFitDataListU);
53  this->CalculateClusterSlidingFits(clustersV, m_minClusterCaloHits, m_slidingFitWindow, slidingFitDataListV);
54  this->CalculateClusterSlidingFits(clustersW, m_minClusterCaloHits, m_slidingFitWindow, slidingFitDataListW);
55 
56  ShowerClusterList showerClusterListU, showerClusterListV, showerClusterListW;
57  this->CalculateShowerClusterList(clustersU, showerClusterListU);
58  this->CalculateShowerClusterList(clustersV, showerClusterListV);
59  this->CalculateShowerClusterList(clustersW, showerClusterListW);
60 
61  // Create maps from hit types to objects for passing to feature tools.
62  const ClusterListMap clusterListMap{{TPC_VIEW_U, clustersU}, {TPC_VIEW_V, clustersV}, {TPC_VIEW_W, clustersW}};
63 
64  const SlidingFitDataListMap slidingFitDataListMap{
65  {TPC_VIEW_U, slidingFitDataListU}, {TPC_VIEW_V, slidingFitDataListV}, {TPC_VIEW_W, slidingFitDataListW}};
66 
67  const ShowerClusterListMap showerClusterListMap{{TPC_VIEW_U, showerClusterListU}, {TPC_VIEW_V, showerClusterListV}, {TPC_VIEW_W, showerClusterListW}};
68 
69  const KDTreeMap kdTreeMap{{TPC_VIEW_U, kdTreeU}, {TPC_VIEW_V, kdTreeV}, {TPC_VIEW_W, kdTreeW}};
70 
71  // Calculate the event feature list and the vertex feature map.
72  EventFeatureInfo eventFeatureInfo(this->CalculateEventFeatures(clustersU, clustersV, clustersW, vertexVector));
73 
74  LArMvaHelper::MvaFeatureVector eventFeatureList;
75  this->AddEventFeaturesToVector(eventFeatureInfo, eventFeatureList);
76 
77  VertexFeatureInfoMap vertexFeatureInfoMap;
78  for (const Vertex *const pVertex : vertexVector)
79  {
81  beamConstants, clusterListMap, slidingFitDataListMap, showerClusterListMap, kdTreeMap, pVertex, vertexFeatureInfoMap);
82  }
83 
84  // Use a simple score to get the list of vertices representing good regions.
85  VertexScoreList initialScoreList;
86  for (const Vertex *const pVertex : vertexVector)
87  PopulateInitialScoreList(vertexFeatureInfoMap, pVertex, initialScoreList);
88 
89  VertexVector bestRegionVertices;
90  this->GetBestRegionVertices(initialScoreList, bestRegionVertices);
91 
93  this->ProduceTrainingSets(vertexVector, bestRegionVertices, vertexFeatureInfoMap, eventFeatureList, kdTreeMap);
94 
95  if ((!m_trainingSetMode || m_allowClassifyDuringTraining) && !bestRegionVertices.empty())
96  {
97  // Use mva to choose the region.
98  const Vertex *const pBestRegionVertex(
99  this->CompareVertices(bestRegionVertices, vertexFeatureInfoMap, eventFeatureList, kdTreeMap, m_mvaRegion, m_useRPhiFeatureForRegion));
100 
101  // Get all the vertices in the best region.
102  VertexVector regionalVertices{pBestRegionVertex};
103  for (const Vertex *const pVertex : vertexVector)
104  {
105  if (pVertex == pBestRegionVertex)
106  continue;
107 
108  if ((pBestRegionVertex->GetPosition() - pVertex->GetPosition()).GetMagnitude() < m_regionRadius)
109  regionalVertices.push_back(pVertex);
110  }
111 
112  this->CalculateRPhiScores(regionalVertices, vertexFeatureInfoMap, kdTreeMap);
113 
114  if (!regionalVertices.empty())
115  {
116  // Use mva to choose the vertex and then fine-tune using the RPhi score.
117  const Vertex *const pBestVertex(
118  this->CompareVertices(regionalVertices, vertexFeatureInfoMap, eventFeatureList, kdTreeMap, m_mvaVertex, true));
119  this->PopulateFinalVertexScoreList(vertexFeatureInfoMap, pBestVertex, vertexVector, vertexScoreList);
120  }
121  }
122 }
123 
124 //------------------------------------------------------------------------------------------------------------------------------------------
125 
126 template <typename T>
128  const LArMvaHelper::MvaFeatureVector &eventFeatureList, const KDTreeMap &kdTreeMap, const T &t, const bool useRPhi) const
129 {
130  const Vertex *pBestVertex(vertexVector.front());
131  LArMvaHelper::MvaFeatureVector chosenFeatureList;
132 
133  VertexFeatureInfo chosenVertexFeatureInfo(vertexFeatureInfoMap.at(pBestVertex));
134  this->AddVertexFeaturesToVector(chosenVertexFeatureInfo, chosenFeatureList, useRPhi);
135 
136  for (const Vertex *const pVertex : vertexVector)
137  {
138  if (pVertex == pBestVertex)
139  continue;
140 
141  LArMvaHelper::MvaFeatureVector featureList;
142  VertexFeatureInfo vertexFeatureInfo(vertexFeatureInfoMap.at(pVertex));
143  this->AddVertexFeaturesToVector(vertexFeatureInfo, featureList, useRPhi);
144 
145  if (!m_legacyVariables)
146  {
147  LArMvaHelper::MvaFeatureVector sharedFeatureList;
148  float separation(0.f), axisHits(0.f);
149  this->GetSharedFeatures(pVertex, pBestVertex, kdTreeMap, separation, axisHits);
150  VertexSharedFeatureInfo sharedFeatureInfo(separation, axisHits);
151  this->AddSharedFeaturesToVector(sharedFeatureInfo, sharedFeatureList);
152 
153  if (LArMvaHelper::Classify(t, eventFeatureList, featureList, chosenFeatureList, sharedFeatureList))
154  {
155  pBestVertex = pVertex;
156  chosenFeatureList = featureList;
157  }
158  }
159  else
160  {
161  if (LArMvaHelper::Classify(t, eventFeatureList, featureList, chosenFeatureList))
162  {
163  pBestVertex = pVertex;
164  chosenFeatureList = featureList;
165  }
166  }
167  }
168 
169  return pBestVertex;
170 }
171 
172 //------------------------------------------------------------------------------------------------------------------------------------------
173 
174 template <typename T>
175 StatusCode MvaVertexSelectionAlgorithm<T>::ReadSettings(const TiXmlHandle xmlHandle)
176 {
177  PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=,
178  XmlHelper::ReadValue(xmlHandle, "FilePathEnvironmentVariable", m_filePathEnvironmentVariable));
179 
180  PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle, "MvaFileName", m_mvaFileName));
181 
182  PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle, "RegionMvaName", m_regionMvaName));
183 
184  PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle, "VertexMvaName", m_vertexMvaName));
185 
186  // ATTN : Need access to base class member variables at this point, so call read settings prior to end of this function
187  PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, TrainedVertexSelectionAlgorithm::ReadSettings(xmlHandle));
188 
190  {
191  if (m_mvaFileName.empty() || m_regionMvaName.empty() || m_vertexMvaName.empty())
192  {
193  std::cout << "MvaVertexSelectionAlgorithm: MvaFileName, RegionMvaName and VertexMvaName must be set if training set mode is"
194  << "off or we allow classification during training" << std::endl;
195  return STATUS_CODE_INVALID_PARAMETER;
196  }
197 
199  m_mvaRegion.Initialize(fullMvaFileName, m_regionMvaName);
200  m_mvaVertex.Initialize(fullMvaFileName, m_vertexMvaName);
201  }
202 
203  return STATUS_CODE_SUCCESS;
204 }
205 
208 
209 } // namespace lar_content
void AddEventFeaturesToVector(const EventFeatureInfo &eventFeatureInfo, LArMvaHelper::MvaFeatureVector &featureVector) const
Add the event features to a vector in the correct order.
bool m_useRPhiFeatureForRegion
Whether to use the r/phi feature for the region vertex.
Header file for the kd tree linker algo template class.
void GetBestRegionVertices(VertexScoreList &initialScoreList, pandora::VertexVector &bestRegionVertices) const
Get the list of top-N separated vertices.
MvaTypes::MvaFeatureVector MvaFeatureVector
Definition: LArMvaHelper.h:58
unsigned int m_minClusterCaloHits
The min number of hits parameter in the energy score.
Header file for the interaction type helper class.
void PopulateInitialScoreList(VertexFeatureInfoMap &vertexFeatureInfoMap, const pandora::Vertex *const pVertex, VertexScoreList &initialScoreList) const
Populate the initial vertex score list for a given vertex.
std::string string
Definition: nybbler.cc:12
std::string m_filePathEnvironmentVariable
The environment variable providing a list of paths to mva files.
void GetClusterLists(const pandora::StringVector &inputClusterListNames, pandora::ClusterList &clusterListU, pandora::ClusterList &clusterListV, pandora::ClusterList &clusterListW) const
Get the cluster lists.
void CalculateClusterSlidingFits(const pandora::ClusterList &inputClusterList, const unsigned int minClusterCaloHits, const unsigned int slidingFitWindow, SlidingFitDataList &slidingFitDataList) const
Calculate the cluster sliding fits.
void PopulateFinalVertexScoreList(const VertexFeatureInfoMap &vertexFeatureInfoMap, const pandora::Vertex *const pFavouriteVertex, const pandora::VertexVector &vertexVector, VertexScoreList &finalVertexScoreList) const
Populate the final vertex score list using the r/phi score to find the best vertex in the vicinity...
static bool Classify(const MvaInterface &classifier, TLISTS &&...featureLists)
Use the trained classifier to predict the boolean class of an example.
Definition: LArMvaHelper.h:220
Header file for the mva vertex selection algorithm class.
void AddVertexFeaturesToVector(const VertexFeatureInfo &vertexFeatureInfo, LArMvaHelper::MvaFeatureVector &featureVector, const bool useRPhi) const
Add the vertex features to a vector in the correct order.
const pandora::Vertex * CompareVertices(const pandora::VertexVector &vertexVector, const VertexFeatureInfoMap &vertexFeatureInfoMap, const LArMvaHelper::MvaFeatureVector &eventFeatureList, const KDTreeMap &kdTreeMap, const T &t, const bool useRPhi) const
Used a binary classifier to compare a set of vertices and pick the best one.
pandora::StatusCode ReadSettings(const pandora::TiXmlHandle xmlHandle)
void CalculateRPhiScores(pandora::VertexVector &vertexVector, VertexFeatureInfoMap &vertexFeatureInfoMap, const KDTreeMap &kdTreeMap) const
Calculate the r/phi scores for the vertices in a vector, possibly erasing those that fail the fast sc...
void GetVertexScoreList(const pandora::VertexVector &vertexVector, const BeamConstants &beamConstants, HitKDTree2D &kdTreeU, HitKDTree2D &kdTreeV, HitKDTree2D &kdTreeW, VertexScoreList &vertexScoreList) const
Get the vertex score list.
Header file for the geometry helper class.
void ProduceTrainingSets(const pandora::VertexVector &vertexVector, const pandora::VertexVector &bestRegionVertices, VertexFeatureInfoMap &vertexFeatureInfoMap, const LArMvaHelper::MvaFeatureVector &eventFeatureList, const KDTreeMap &kdTreeMap) const
Produce the region and vertex training sets.
std::map< pandora::HitType, const ShowerClusterList > ShowerClusterListMap
Map of shower cluster lists for passing to tools.
void PopulateVertexFeatureInfoMap(const BeamConstants &beamConstants, const ClusterListMap &clusterListMap, const SlidingFitDataListMap &slidingFitDataListMap, const ShowerClusterListMap &showerClusterListMap, const KDTreeMap &kdTreeMap, const pandora::Vertex *const pVertex, VertexFeatureInfoMap &vertexFeatureInfoMap) const
Populate the vertex feature info map for a given vertex.
Header file for the lar monte carlo particle helper helper class.
Header file for the cluster helper class.
EventFeatureInfo CalculateEventFeatures(const pandora::ClusterList &clusterListU, const pandora::ClusterList &clusterListV, const pandora::ClusterList &clusterListW, const pandora::VertexVector &vertexVector) const
Calculate the event parameters.
std::map< pandora::HitType, const pandora::ClusterList & > ClusterListMap
Map array of cluster lists for passing to tools.
std::string m_regionMvaName
The name of the region mva to find.
Header file for the local asymmetry feature tool class.
static std::string FindFileInPath(const std::string &unqualifiedFileName, const std::string &environmentVariable, const std::string &delimiter=":")
Find the fully-qualified file name by searching through a list of delimiter-separated paths in a name...
Header file for the file helper class.
std::string m_vertexMvaName
The name of the vertex mva to find.
bool m_allowClassifyDuringTraining
Whether classification is allowed during training.
void CalculateShowerClusterList(const pandora::ClusterList &inputClusterList, ShowerClusterList &showerClusterList) const
Calculate the shower cluster map for a cluster list.
void AddSharedFeaturesToVector(const VertexSharedFeatureInfo &vertexSharedFeatureInfo, LArMvaHelper::MvaFeatureVector &featureVector) const
Add the shared features to a vector in the correct order.
void GetSharedFeatures(const pandora::Vertex *const pVertex1, const pandora::Vertex *const pVertex2, const KDTreeMap &kdTreeMap, float &separation, float &axisHits) const
Calculates the shared features of a pair of vertex candidates.
std::map< pandora::HitType, const SlidingFitDataList > SlidingFitDataListMap
Map of sliding fit data lists for passing to tools.
bool m_legacyVariables
Whether to only use the old variables.
unsigned int m_slidingFitWindow
The layer window for the sliding linear fits.
std::vector< art::Ptr< recob::Vertex > > VertexVector
Header file for the shower asymmetry feature tool class.
boost::graph_traits< ModuleGraph >::vertex_descriptor Vertex
Definition: ModuleGraph.h:25
std::map< pandora::HitType, const std::reference_wrapper< HitKDTree2D > > KDTreeMap
Map array of hit kd trees for passing to tools.
pandora::StatusCode ReadSettings(const pandora::TiXmlHandle xmlHandle)
Header file for the energy kick feature tool class.
pandora::StringVector m_inputClusterListNames
The list of cluster list names.
std::map< const pandora::Vertex *const, VertexFeatureInfo > VertexFeatureInfoMap
QTextStream & endl(QTextStream &s)
Header file for the r/phi feature tool class.
Header file for the global asymmetry feature tool class.