All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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  {
80  this->PopulateVertexFeatureInfoMap(
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 
92  if (m_trainingSetMode)
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>
127 const pandora::Vertex *MvaVertexSelectionAlgorithm<T>::CompareVertices(const VertexVector &vertexVector, const VertexFeatureInfoMap &vertexFeatureInfoMap,
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, LArMvaHelper::ConcatenateFeatureLists(eventFeatureList, featureList, chosenFeatureList, sharedFeatureList)))
154  {
155  pBestVertex = pVertex;
156  chosenFeatureList = featureList;
157  }
158  }
159  else
160  {
161  if (LArMvaHelper::Classify(t, LArMvaHelper::ConcatenateFeatureLists(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 
189  if ((!m_trainingSetMode || m_allowClassifyDuringTraining))
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 
198  const std::string fullMvaFileName(LArFileHelper::FindFileInPath(m_mvaFileName, m_filePathEnvironmentVariable));
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
static bool Classify(const MvaInterface &classifier, TCONTAINER &&featureContainer)
Use the trained classifier to predict the boolean class of an example.
Definition: LArMvaHelper.h:328
Header file for the kd tree linker algo template class.
MvaTypes::MvaFeatureVector MvaFeatureVector
Definition: LArMvaHelper.h:72
Header file for the interaction type helper class.
Header file for the mva vertex selection algorithm class.
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 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.
std::map< pandora::HitType, const ShowerClusterList > ShowerClusterListMap
Map of shower cluster lists for passing to tools.
Header file for the lar monte carlo particle helper helper class.
Header file for the cluster helper class.
std::map< pandora::HitType, const pandora::ClusterList & > ClusterListMap
Map array of cluster lists for passing to tools.
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.
required by fuzzyCluster table::sbnd_g4_services gaushitTruthMatch pandora
Definition: reco_sbnd.fcl:182
std::map< pandora::HitType, const SlidingFitDataList > SlidingFitDataListMap
Map of sliding fit data lists for passing to tools.
Header file for the shower asymmetry feature tool class.
static MvaFeatureVector ConcatenateFeatureLists()
Recursively concatenate vectors of features (terminating method)
Definition: LArMvaHelper.h:538
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.
std::array< float, 2 > VertexVector(const recob::Vertex &vert, const geo::PlaneID &plane, const geo::GeometryCore *geo, const detinfo::DetectorPropertiesData &dprop)
BEGIN_PROLOG could also be cout
std::map< const pandora::Vertex *const, VertexFeatureInfo > VertexFeatureInfoMap
Header file for the r/phi feature tool class.
Header file for the global asymmetry feature tool class.