LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
SvmPfoCharacterisationAlgorithm.cc
Go to the documentation of this file.
1 
8 #include "Pandora/AlgorithmHeaders.h"
9 
14 
16 
17 using namespace pandora;
18 
19 namespace lar_content
20 {
21 
22 SvmPfoCharacterisationAlgorithm::SvmPfoCharacterisationAlgorithm() :
23  m_trainingSetMode(false),
24  m_enableProbability(true),
25  m_useThreeDInformation(true),
26  m_minProbabilityCut(0.5f),
27  m_minCaloHitsCut(5),
28  m_filePathEnvironmentVariable("FW_SEARCH_PATH")
29 {
30 }
31 
32 //------------------------------------------------------------------------------------------------------------------------------------------
33 
34 bool SvmPfoCharacterisationAlgorithm::IsClearTrack(const Cluster *const pCluster) const
35 {
36  if (pCluster->GetNCaloHits() < m_minCaloHitsCut)
37  return false;
38 
40 
42  {
43  bool isTrueTrack(false);
44 
45  try
46  {
47  const MCParticle *const pMCParticle(MCParticleHelper::GetMainMCParticle(pCluster));
48  isTrueTrack = ((PHOTON != pMCParticle->GetParticleId()) && (E_MINUS != std::abs(pMCParticle->GetParticleId())));
49  }
50  catch (const StatusCodeException &) {}
51 
53  return isTrueTrack;
54  }
55 
57  {
58  return LArMvaHelper::Classify(m_supportVectorMachine, featureVector);
59  }
60  else
61  {
63  }
64 }
65 
66 //------------------------------------------------------------------------------------------------------------------------------------------
67 
68 bool SvmPfoCharacterisationAlgorithm::IsClearTrack(const pandora::ParticleFlowObject *const pPfo) const
69 {
70 
71  if (!LArPfoHelper::IsThreeD(pPfo))
72  {
74  {
75  object_creation::ParticleFlowObject::Metadata metadata;
76  metadata.m_propertiesToAdd["TrackScore"] = -1.f;
77  PANDORA_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, PandoraContentApi::ParticleFlowObject::AlterMetadata(*this, pPfo, metadata));
78  }
79  return (pPfo->GetParticleId() == MU_MINUS);
80  }
81  //charge related features are only calculated using hits in W view
82  ClusterList wClusterList;
83  LArPfoHelper::GetClusters(pPfo, TPC_VIEW_W, wClusterList);
85 
87  {
88  bool isTrueTrack(false);
89  bool isMainMCParticleSet(false);
90 
91  try
92  {
93  const MCParticle *const pMCParticle(LArMCParticleHelper::GetMainMCParticle(pPfo));
94  isTrueTrack = ((PHOTON != pMCParticle->GetParticleId()) && (E_MINUS != std::abs(pMCParticle->GetParticleId())));
95  isMainMCParticleSet = (pMCParticle->GetParticleId() != 0);
96  }
97  catch (const StatusCodeException &) {}
98 
99  if (isMainMCParticleSet)
100  {
101  std::string outputFile;
102  outputFile.append(m_trainingOutputFile);
103  const std::string end=((wClusterList.empty()) ? "noChargeInfo.txt" : ".txt");
104  outputFile.append(end);
105  LArMvaHelper::ProduceTrainingExample(outputFile, isTrueTrack, featureVector);
106  }
107  return isTrueTrack;
108  }// training mode
109 
110  //check for failures in the calculation of features, i.e. not initialized features
111  for (const LArMvaHelper::MvaFeature &featureValue : featureVector)
112  {
113  if (!featureValue.IsInitialized())
114  {
116  {
117  object_creation::ParticleFlowObject::Metadata metadata;
118  metadata.m_propertiesToAdd["TrackScore"] = -1.f;
119  PANDORA_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, PandoraContentApi::ParticleFlowObject::AlterMetadata(*this, pPfo, metadata));
120  }
121  return (pPfo->GetParticleId() == MU_MINUS);
122  }
123  }
124 
125  //if no failures, proceed with svm classification
126  if (!m_enableProbability)
127  {
128  return LArMvaHelper::Classify((wClusterList.empty() ? m_supportVectorMachineNoChargeInfo : m_supportVectorMachine), featureVector);
129  }
130  else
131  {
132  const double score(LArMvaHelper::CalculateProbability((wClusterList.empty() ? m_supportVectorMachineNoChargeInfo : m_supportVectorMachine), featureVector));
133  object_creation::ParticleFlowObject::Metadata metadata;
134  metadata.m_propertiesToAdd["TrackScore"] = score;
135  PANDORA_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, PandoraContentApi::ParticleFlowObject::AlterMetadata(*this, pPfo, metadata));
136  return (m_minProbabilityCut <= score);
137  }
138 }
139 
140 //------------------------------------------------------------------------------------------------------------------------------------------
141 
142 StatusCode SvmPfoCharacterisationAlgorithm::ReadSettings(const TiXmlHandle xmlHandle)
143 {
144  PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle,
145  "TrainingSetMode", m_trainingSetMode));
146 
147  PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle,
148  "MinCaloHitsCut", m_minCaloHitsCut));
149 
150  PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle,
151  "UseThreeDInformation", m_useThreeDInformation));
152 
153  PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle,
154  "FilePathEnvironmentVariable", m_filePathEnvironmentVariable));
155 
156  PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle,
157  "SvmFileName", m_svmFileName));
158 
159  PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle,
160  "SvmName", m_svmName));
161 
163  {
164  PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle,
165  "SvmFileNameNoChargeInfo", m_svmFileNameNoChargeInfo));
166 
167  PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle,
168  "SvmNameNoChargeInfo", m_svmNameNoChargeInfo));
169  }
170 
171  PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle,
172  "EnableProbability", m_enableProbability));
173 
174  PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle,
175  "MinProbabilityCut", m_minProbabilityCut));
176 
177  if (m_trainingSetMode)
178  {
179  PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, XmlHelper::ReadValue(xmlHandle, "TrainingOutputFileName", m_trainingOutputFile));
180  }
181  else
182  {
183  if (m_svmFileName.empty() || m_svmName.empty())
184  {
185  std::cout << "SvmPfoCharacterisationAlgorithm: SvmFileName and SvmName must be set if in classification mode " << std::endl;
186  return STATUS_CODE_INVALID_PARAMETER;
187  }
188 
190  m_supportVectorMachine.Initialize(fullSvmFileName, m_svmName);
191 
193  {
194  if (m_svmFileNameNoChargeInfo.empty() || m_svmNameNoChargeInfo.empty())
195  {
196  std::cout << "SvmPfoCharacterisationAlgorithm: SvmFileName and SvmName must be set if in classification mode for no charge info in 3D mode " << std::endl;
197  return STATUS_CODE_INVALID_PARAMETER;
198  }
199  const std::string fullSvmFileNameNoChargeInfo(LArFileHelper::FindFileInPath(m_svmFileNameNoChargeInfo, m_filePathEnvironmentVariable));
201  }
202  }
203 
204  AlgorithmToolVector algorithmToolVector;
205  PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, XmlHelper::ProcessAlgorithmToolList(*this, xmlHandle, "FeatureTools", algorithmToolVector));
206 
208  {
209  AlgorithmToolVector algorithmToolVectorNoChargeInfo;
210  PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, XmlHelper::ProcessAlgorithmToolList(*this, xmlHandle, "FeatureToolsNoChargeInfo", algorithmToolVectorNoChargeInfo));
211  for (AlgorithmTool *const pAlgorithmTool : algorithmToolVector)
212  PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, LArMvaHelper::AddFeatureToolToVector(pAlgorithmTool, m_featureToolVectorThreeD));
213  for (AlgorithmTool *const pAlgorithmTool : algorithmToolVectorNoChargeInfo)
214  PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, LArMvaHelper::AddFeatureToolToVector(pAlgorithmTool, m_featureToolVectorNoChargeInfo));
215  }
216  else
217  {
218  for (AlgorithmTool *const pAlgorithmTool : algorithmToolVector)
219  PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, LArMvaHelper::AddFeatureToolToVector(pAlgorithmTool, m_featureToolVector));
220  }
221 
223 }
224 
225 } // namespace lar_content
std::string m_svmFileNameNoChargeInfo
The svm input file for PFOs missing the W view, and thus charge info.
bool m_useThreeDInformation
Whether to use 3D information.
static double CalculateProbability(const MvaInterface &classifier, TLISTS &&...featureLists)
Use the trained mva to calculate a classification probability for an example.
Definition: LArMvaHelper.h:236
static bool IsThreeD(const pandora::ParticleFlowObject *const pPfo)
Does Pfo contain 3D clusters.
Header file for the pfo helper class.
MvaTypes::MvaFeatureVector MvaFeatureVector
Definition: LArMvaHelper.h:58
static void GetClusters(const pandora::PfoList &pfoList, const pandora::HitType &hitType, pandora::ClusterList &clusterList)
Get a list of clusters of a particular hit type from a list of pfos.
std::string m_trainingOutputFile
The training output file.
virtual bool IsClearTrack(const pandora::ParticleFlowObject *const pPfo) const
Whether pfo is identified as a clear track.
static bool Classify(const MvaInterface &classifier, TLISTS &&...featureLists)
Use the trained classifier to predict the boolean class of an example.
Definition: LArMvaHelper.h:220
PfoCharacterisationFeatureTool::FeatureToolVector m_featureToolVectorNoChargeInfo
The feature tool map for missing W view.
std::string m_filePathEnvironmentVariable
The environment variable providing a list of paths to svm files.
SupportVectorMachine m_supportVectorMachineNoChargeInfo
The support vector machine for missing W view.
static const pandora::MCParticle * GetMainMCParticle(const pandora::ParticleFlowObject *const pPfo)
Find the mc particle making the largest contribution to 2D clusters in a specified pfo...
Header file for the svm pfo characterisation algorithm class.
std::string m_svmNameNoChargeInfo
The name of the svm to find for PFOs missing the W view, and thus charge info.
PfoCharacterisationFeatureTool::FeatureToolVector m_featureToolVectorThreeD
The feature tool map for 3D info.
TFile f
Definition: plotHisto.C:6
SupportVectorMachine m_supportVectorMachine
The support vector machine.
static pandora::StatusCode ProduceTrainingExample(const std::string &trainingOutputFile, const bool result, TLISTS &&...featureLists)
Produce a training example with the given features and result.
Definition: LArMvaHelper.h:197
InitializedDouble class used to define mva features.
std::string m_svmName
The name of the svm to find.
Header file for the lar monte carlo particle helper helper class.
unsigned int m_minCaloHitsCut
The minimum number of calo hits to qualify as a track.
pandora::StatusCode ReadSettings(const pandora::TiXmlHandle xmlHandle)
static pandora::StatusCode AddFeatureToolToVector(pandora::AlgorithmTool *const pFeatureTool, MvaFeatureToolVector< Ts... > &featureToolVector)
Add a feature tool to a vector of feature tools.
Definition: LArMvaHelper.h:274
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.
pandora::StatusCode ReadSettings(const pandora::TiXmlHandle xmlHandle)
pandora::StatusCode Initialize(const std::string &parameterLocation, const std::string &svmName)
Initialize the svm using a serialized model.
bool m_enableProbability
Whether to use probabilities instead of binary classification.
static MvaFeatureVector CalculateFeatures(const MvaFeatureToolVector< Ts... > &featureToolVector, TARGS &&...args)
Calculate the features in a given feature tool vector.
Definition: LArMvaHelper.h:244
std::vector< evd::details::RawDigitInfo_t >::const_iterator end(RawDigitCacheDataClass const &cache)
float m_minProbabilityCut
The minimum probability to label a cluster as track-like.
ClusterCharacterisationFeatureTool::FeatureToolVector m_featureToolVector
The feature tool map.