LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
lar_content::LArMvaHelper Class Reference

LArMvaHelper class. More...

#include "LArMvaHelper.h"

Public Types

typedef MvaTypes::MvaFeature MvaFeature
 
typedef MvaTypes::MvaFeatureVector MvaFeatureVector
 

Static Public Member Functions

template<typename... TLISTS>
static pandora::StatusCode ProduceTrainingExample (const std::string &trainingOutputFile, const bool result, TLISTS &&...featureLists)
 Produce a training example with the given features and result. More...
 
template<typename... TLISTS>
static bool Classify (const MvaInterface &classifier, TLISTS &&...featureLists)
 Use the trained classifier to predict the boolean class of an example. More...
 
template<typename... TLISTS>
static double CalculateClassificationScore (const MvaInterface &classifier, TLISTS &&...featureLists)
 Use the trained classifer to calculate the classification score of an example (>0 means boolean class true) More...
 
template<typename... TLISTS>
static double CalculateProbability (const MvaInterface &classifier, TLISTS &&...featureLists)
 Use the trained mva to calculate a classification probability for an example. More...
 
template<typename... Ts, typename... TARGS>
static MvaFeatureVector CalculateFeatures (const MvaFeatureToolVector< Ts... > &featureToolVector, TARGS &&...args)
 Calculate the features in a given feature tool vector. More...
 
template<typename T , typename... Ts, typename... TARGS>
static MvaFeatureVector CalculateFeaturesOfType (const MvaFeatureToolVector< Ts... > &featureToolVector, TARGS &&...args)
 Calculate the features of a given derived feature tool type in a feature tool vector. More...
 
template<typename... Ts>
static pandora::StatusCode AddFeatureToolToVector (pandora::AlgorithmTool *const pFeatureTool, MvaFeatureToolVector< Ts... > &featureToolVector)
 Add a feature tool to a vector of feature tools. More...
 

Static Private Member Functions

static std::string GetTimestampString ()
 Get a timestamp string for this point in time. More...
 
template<typename TLIST , typename... TLISTS>
static pandora::StatusCode WriteFeaturesToFile (std::ofstream &outfile, const std::string &delimiter, TLIST &&featureList, TLISTS &&...featureLists)
 Recursively write the features of the given lists to file. More...
 
static pandora::StatusCode WriteFeaturesToFile (std::ofstream &, const std::string &)
 Recursively write the features of the given lists to file (terminating method) More...
 
template<typename TLIST >
static pandora::StatusCode WriteFeaturesToFileImpl (std::ofstream &outfile, const std::string &delimiter, TLIST &&featureList)
 Write the features of the given list to file (implementation method) More...
 
template<typename TLIST , typename... TLISTS>
static MvaFeatureVector ConcatenateFeatureLists (TLIST &&featureList, TLISTS &&...featureLists)
 Recursively concatenate vectors of features. More...
 
static MvaFeatureVector ConcatenateFeatureLists ()
 Recursively concatenate vectors of features (terminating method) More...
 

Detailed Description

LArMvaHelper class.

Definition at line 54 of file LArMvaHelper.h.

Member Typedef Documentation

Member Function Documentation

template<typename... Ts>
pandora::StatusCode lar_content::LArMvaHelper::AddFeatureToolToVector ( pandora::AlgorithmTool *const  pFeatureTool,
MvaFeatureToolVector< Ts... > &  featureToolVector 
)
static

Add a feature tool to a vector of feature tools.

Parameters
pFeatureToolthe feature tool
featureToolVectorthe vector to append
Returns
success

Definition at line 274 of file LArMvaHelper.h.

Referenced by lar_content::SvmPfoCharacterisationAlgorithm::ReadSettings(), lar_content::EnergyKickVertexSelectionAlgorithm::ReadSettings(), and lar_content::SvmVertexSelectionAlgorithm::ReadSettings().

275 {
276  if (MvaFeatureTool<Ts...> *const pCastFeatureTool = dynamic_cast<MvaFeatureTool<Ts...> *const>(pFeatureTool))
277  {
278  featureToolVector.push_back(pCastFeatureTool);
279  return pandora::STATUS_CODE_SUCCESS;
280  }
281 
282  return pandora::STATUS_CODE_FAILURE;
283 }
template<typename... TLISTS>
double lar_content::LArMvaHelper::CalculateClassificationScore ( const MvaInterface classifier,
TLISTS &&...  featureLists 
)
static

Use the trained classifer to calculate the classification score of an example (>0 means boolean class true)

Parameters
classifierthe classifier
featureListsthe lists of features
Returns
the classification score

Definition at line 228 of file LArMvaHelper.h.

References lar_content::MvaInterface::CalculateClassificationScore().

Referenced by lar_content::BdtBeamParticleIdTool::SliceFeatures::GetAdaBoostDecisionTreeScore().

229 {
230  return classifier.CalculateClassificationScore(ConcatenateFeatureLists(std::forward<TLISTS>(featureLists)...));
231 }
static MvaFeatureVector ConcatenateFeatureLists()
Recursively concatenate vectors of features (terminating method)
Definition: LArMvaHelper.h:356
template<typename... Ts, typename... TARGS>
LArMvaHelper::MvaFeatureVector lar_content::LArMvaHelper::CalculateFeatures ( const MvaFeatureToolVector< Ts... > &  featureToolVector,
TARGS &&...  args 
)
static

Calculate the features in a given feature tool vector.

Parameters
featureToolVectorthe feature tool vector
argsarguments to pass to the tool
Returns
the vector of features

Definition at line 244 of file LArMvaHelper.h.

Referenced by lar_content::SvmPfoCharacterisationAlgorithm::IsClearTrack(), and lar_content::SvmPfoCharacterisationAlgorithm::SvmPfoCharacterisationAlgorithm().

245 {
246  LArMvaHelper::MvaFeatureVector featureVector;
247 
248  for (MvaFeatureTool<Ts...> *const pFeatureTool : featureToolVector)
249  pFeatureTool->Run(featureVector, std::forward<TARGS>(args)...);
250 
251  return featureVector;
252 }
MvaTypes::MvaFeatureVector MvaFeatureVector
Definition: LArMvaHelper.h:58
template<typename T , typename... Ts, typename... TARGS>
LArMvaHelper::MvaFeatureVector lar_content::LArMvaHelper::CalculateFeaturesOfType ( const MvaFeatureToolVector< Ts... > &  featureToolVector,
TARGS &&...  args 
)
static

Calculate the features of a given derived feature tool type in a feature tool vector.

Parameters
featureToolVectorthe feature tool vector
argsarguments to pass to the tool
Returns
the vector of features

Definition at line 257 of file LArMvaHelper.h.

258 {
259  using TD = typename std::decay<T>::type;
260  LArMvaHelper::MvaFeatureVector featureVector;
261 
262  for (MvaFeatureTool<Ts...> *const pFeatureTool : featureToolVector)
263  {
264  if (TD *const pCastFeatureTool = dynamic_cast<TD *const>(pFeatureTool))
265  pCastFeatureTool->Run(featureVector, std::forward<TARGS>(args)...);
266  }
267 
268  return featureVector;
269 }
MvaTypes::MvaFeatureVector MvaFeatureVector
Definition: LArMvaHelper.h:58
template<typename... TLISTS>
double lar_content::LArMvaHelper::CalculateProbability ( const MvaInterface classifier,
TLISTS &&...  featureLists 
)
static

Use the trained mva to calculate a classification probability for an example.

Parameters
classifierthe classifier
featureListsthe lists of features
Returns
the classification probability

Definition at line 236 of file LArMvaHelper.h.

References lar_content::MvaInterface::CalculateProbability().

Referenced by lar_content::NeutrinoIdTool::SliceFeatures::GetNeutrinoProbability(), lar_content::SvmPfoCharacterisationAlgorithm::IsClearTrack(), and lar_content::SvmPfoCharacterisationAlgorithm::SvmPfoCharacterisationAlgorithm().

237 {
238  return classifier.CalculateProbability(ConcatenateFeatureLists(std::forward<TLISTS>(featureLists)...));
239 }
static MvaFeatureVector ConcatenateFeatureLists()
Recursively concatenate vectors of features (terminating method)
Definition: LArMvaHelper.h:356
template<typename... TLISTS>
bool lar_content::LArMvaHelper::Classify ( const MvaInterface classifier,
TLISTS &&...  featureLists 
)
static

Use the trained classifier to predict the boolean class of an example.

Parameters
classifierthe classifier
featureListsthe lists of features
Returns
the predicted boolean class of the example

Definition at line 220 of file LArMvaHelper.h.

References lar_content::MvaInterface::Classify().

Referenced by lar_content::SvmVertexSelectionAlgorithm::CompareVertices(), lar_content::SvmPfoCharacterisationAlgorithm::IsClearTrack(), and lar_content::SvmPfoCharacterisationAlgorithm::SvmPfoCharacterisationAlgorithm().

221 {
222  return classifier.Classify(ConcatenateFeatureLists(std::forward<TLISTS>(featureLists)...));
223 }
static MvaFeatureVector ConcatenateFeatureLists()
Recursively concatenate vectors of features (terminating method)
Definition: LArMvaHelper.h:356
template<typename TLIST , typename... TLISTS>
LArMvaHelper::MvaFeatureVector lar_content::LArMvaHelper::ConcatenateFeatureLists ( TLIST &&  featureList,
TLISTS &&...  featureLists 
)
staticprivate

Recursively concatenate vectors of features.

Parameters
featureLista list of features
featureListsoptional further lists of features
Returns
the concatenated vector of features

Definition at line 338 of file LArMvaHelper.h.

References fhicl::detail::atom::value().

339 {
340  static_assert(std::is_same<typename std::decay<TLIST>::type, LArMvaHelper::MvaFeatureVector>::value,
341  "LArMvaHelper: Could not concatenate feature lists because one or more lists was not a vector of MvaFeatures");
342 
343  LArMvaHelper::MvaFeatureVector featureVector;
344 
345  for (const MvaFeature feature : featureList)
346  featureVector.push_back(feature);
347 
348  LArMvaHelper::MvaFeatureVector newFeatureVector = ConcatenateFeatureLists(std::forward<TLISTS>(featureLists)...);
349  featureVector.insert(featureVector.end(), newFeatureVector.begin(), newFeatureVector.end());
350 
351  return featureVector;
352 }
MvaTypes::MvaFeatureVector MvaFeatureVector
Definition: LArMvaHelper.h:58
MvaTypes::MvaFeature MvaFeature
Definition: LArMvaHelper.h:57
std::string value(boost::any const &)
static MvaFeatureVector ConcatenateFeatureLists()
Recursively concatenate vectors of features (terminating method)
Definition: LArMvaHelper.h:356
LArMvaHelper::MvaFeatureVector lar_content::LArMvaHelper::ConcatenateFeatureLists ( )
inlinestaticprivate

Recursively concatenate vectors of features (terminating method)

Definition at line 356 of file LArMvaHelper.h.

357 {
359 }
MvaTypes::MvaFeatureVector MvaFeatureVector
Definition: LArMvaHelper.h:58
std::string lar_content::LArMvaHelper::GetTimestampString ( )
inlinestaticprivate

Get a timestamp string for this point in time.

Returns
a timestamp string

Definition at line 287 of file LArMvaHelper.h.

288 {
289  std::time_t timestampNow = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
290 
291  struct tm *pTimeInfo(NULL);
292  char buffer[80];
293 
294  pTimeInfo = localtime(&timestampNow);
295  strftime(buffer, 80, "%x_%X", pTimeInfo);
296 
297  std::string timeString(buffer);
298 
299  if (!timeString.empty() && timeString.back() == '\n') // last char is always a newline
300  timeString.pop_back();
301 
302  return timeString;
303 }
template<typename... TLISTS>
pandora::StatusCode lar_content::LArMvaHelper::ProduceTrainingExample ( const std::string &  trainingOutputFile,
const bool  result,
TLISTS &&...  featureLists 
)
static

Produce a training example with the given features and result.

Parameters
trainingOutputFilethe file to which to append the example
featureListsthe lists of features
Returns
success

Definition at line 197 of file LArMvaHelper.h.

Referenced by lar_content::SvmPfoCharacterisationAlgorithm::IsClearTrack(), lar_content::SvmVertexSelectionAlgorithm::ProduceTrainingExamples(), lar_content::NeutrinoIdTool::SelectOutputPfos(), lar_content::BdtBeamParticleIdTool::SelectOutputPfos(), and lar_content::SvmPfoCharacterisationAlgorithm::SvmPfoCharacterisationAlgorithm().

198 {
199  std::ofstream outfile;
200  outfile.open(trainingOutputFile, std::ios_base::app); // always append to the output file
201 
202  if (!outfile.is_open())
203  {
204  std::cout << "LArMvaHelper: could not open file for training examples at " << trainingOutputFile << std::endl;
205  return pandora::STATUS_CODE_FAILURE;
206  }
207 
208  std::string delimiter(",");
209  outfile << GetTimestampString() << delimiter;
210 
211  PANDORA_RETURN_RESULT_IF(pandora::STATUS_CODE_SUCCESS, !=, WriteFeaturesToFile(outfile, delimiter, featureLists...));
212  outfile << static_cast<int>(result) << '\n';
213 
214  return pandora::STATUS_CODE_SUCCESS;
215 }
static std::string GetTimestampString()
Get a timestamp string for this point in time.
Definition: LArMvaHelper.h:287
static pandora::StatusCode WriteFeaturesToFile(std::ofstream &outfile, const std::string &delimiter, TLIST &&featureList, TLISTS &&...featureLists)
Recursively write the features of the given lists to file.
Definition: LArMvaHelper.h:308
template<typename TLIST , typename... TLISTS>
pandora::StatusCode lar_content::LArMvaHelper::WriteFeaturesToFile ( std::ofstream &  outfile,
const std::string &  delimiter,
TLIST &&  featureList,
TLISTS &&...  featureLists 
)
inlinestaticprivate

Recursively write the features of the given lists to file.

Parameters
outfilethe std::ofstream object to use
delimiterthe delimiter string
featureLista list of features to write
featureListsoptional further lists of features to write
Returns
success

Definition at line 308 of file LArMvaHelper.h.

References fhicl::detail::atom::value().

309 {
310  static_assert(std::is_same<typename std::decay<TLIST>::type, LArMvaHelper::MvaFeatureVector>::value,
311  "LArMvaHelper: Could not write training set example because a passed parameter was not a vector of MvaFeatures");
312 
313  PANDORA_RETURN_RESULT_IF(pandora::STATUS_CODE_SUCCESS, !=, WriteFeaturesToFileImpl(outfile, delimiter, featureList));
314  return WriteFeaturesToFile(outfile, delimiter, featureLists...);
315 }
static pandora::StatusCode WriteFeaturesToFileImpl(std::ofstream &outfile, const std::string &delimiter, TLIST &&featureList)
Write the features of the given list to file (implementation method)
Definition: LArMvaHelper.h:327
MvaTypes::MvaFeatureVector MvaFeatureVector
Definition: LArMvaHelper.h:58
static pandora::StatusCode WriteFeaturesToFile(std::ofstream &outfile, const std::string &delimiter, TLIST &&featureList, TLISTS &&...featureLists)
Recursively write the features of the given lists to file.
Definition: LArMvaHelper.h:308
std::string value(boost::any const &)
pandora::StatusCode lar_content::LArMvaHelper::WriteFeaturesToFile ( std::ofstream &  ,
const std::string &   
)
inlinestaticprivate

Recursively write the features of the given lists to file (terminating method)

Returns
success

Definition at line 319 of file LArMvaHelper.h.

320 {
321  return pandora::STATUS_CODE_SUCCESS;
322 }
template<typename TLIST >
pandora::StatusCode lar_content::LArMvaHelper::WriteFeaturesToFileImpl ( std::ofstream &  outfile,
const std::string &  delimiter,
TLIST &&  featureList 
)
staticprivate

Write the features of the given list to file (implementation method)

Parameters
outfilethe std::ofstream object to use
delimiterthe delimiter string
featureLista list of features to write
Returns
success

Definition at line 327 of file LArMvaHelper.h.

328 {
329  for (const MvaFeature feature : featureList)
330  outfile << feature.Get() << delimiter;
331 
332  return pandora::STATUS_CODE_SUCCESS;
333 }
MvaTypes::MvaFeature MvaFeature
Definition: LArMvaHelper.h:57

The documentation for this class was generated from the following file: