All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Types | Static Public Member Functions | Static Private Member Functions | List of all members
lar_content::LArMvaHelper Class Reference

LArMvaHelper class. More...

#include <LArMvaHelper.h>

Public Types

typedef MvaTypes::MvaFeature MvaFeature
 
typedef MvaTypes::MvaFeatureVector MvaFeatureVector
 
typedef std::map< std::string,
double > 
DoubleMap
 
typedef MvaTypes::MvaFeatureMap MvaFeatureMap
 
typedef std::map< std::string,
pandora::AlgorithmTool * > 
AlgorithmToolMap
 

Static Public Member Functions

template<typename TCONTAINER >
static pandora::StatusCode ProduceTrainingExample (const std::string &trainingOutputFile, const bool result, TCONTAINER &&featureContainer)
 Produce a training example with the given features and result. More...
 
template<typename TCONTAINER >
static pandora::StatusCode ProduceTrainingExample (const std::string &trainingOutputFile, const bool result, const pandora::StringVector &featureOrder, TCONTAINER &&featureContainer)
 Produce a training example with the given features and result - using a map. More...
 
template<typename TCONTAINER >
static bool Classify (const MvaInterface &classifier, TCONTAINER &&featureContainer)
 Use the trained classifier to predict the boolean class of an example. More...
 
template<typename TCONTAINER >
static bool Classify (const MvaInterface &classifier, const pandora::StringVector &featureOrder, TCONTAINER &&featureContainer)
 Use the trained classifier to predict the boolean class of an example – using a map. More...
 
template<typename TCONTAINER >
static double CalculateClassificationScore (const MvaInterface &classifier, TCONTAINER &&featureContainer)
 Use the trained classifer to calculate the classification score of an example (>0 means boolean class true) More...
 
template<typename TCONTAINER >
static double CalculateProbability (const MvaInterface &classifier, TCONTAINER &&featureContainer)
 Use the trained mva to calculate a classification probability for an example. More...
 
template<typename TCONTAINER >
static double CalculateProbability (const MvaInterface &classifier, const pandora::StringVector &featureOrder, TCONTAINER &&featureContainer)
 Use the trained mva to calculate a classification probability for an example – using a map. 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... Ts, typename... TARGS>
static MvaFeatureMap CalculateFeatures (const pandora::StringVector &featureToolOrder, const MvaFeatureToolMap< Ts...> &featureToolMap, pandora::StringVector &featureOrder, TARGS &&...args)
 Calculate the features in a given feature tool map, and fill an MvaFeatureMap and vector with feature order. 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...
 
template<typename... Ts>
static pandora::StatusCode AddFeatureToolToMap (pandora::AlgorithmTool *const pFeatureTool, std::string pFeatureToolName, MvaFeatureToolMap< Ts...> &featureToolMap)
 Add a feature tool to a map of feature tools. More...
 
static pandora::StatusCode ProcessAlgorithmToolListToMap (const pandora::Algorithm &algorithm, const pandora::TiXmlHandle &xmlHandle, const std::string &listName, pandora::StringVector &algorithToolNameVector, AlgorithmToolMap &algorithmToolMap)
 Process a list of algorithms tools in an xml file, using a map. Idea is for this to go to XmlHelper in PandoraSDK eventually as an overload to ProcessAlgorithmToolList. 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...
 

Static Private Member Functions

static std::string GetTimestampString ()
 Get a timestamp string for this point in time. More...
 
template<typename TCONTAINER >
static pandora::StatusCode WriteFeaturesToFile (std::ofstream &outfile, const std::string &delimiter, TCONTAINER &&featureContainer)
 Write the features of the given lists to file. More...
 
template<typename TCONTAINER >
static pandora::StatusCode WriteFeaturesToFileImpl (std::ofstream &outfile, const std::string &delimiter, TCONTAINER &&featureContainer)
 Write the features of the given list to file (implementation method) More...
 

Detailed Description

LArMvaHelper class.

Definition at line 68 of file LArMvaHelper.h.

Member Typedef Documentation

typedef std::map<std::string, pandora::AlgorithmTool *> lar_content::LArMvaHelper::AlgorithmToolMap

Definition at line 76 of file LArMvaHelper.h.

typedef std::map<std::string, double> lar_content::LArMvaHelper::DoubleMap

Definition at line 73 of file LArMvaHelper.h.

Definition at line 71 of file LArMvaHelper.h.

Definition at line 75 of file LArMvaHelper.h.

Definition at line 72 of file LArMvaHelper.h.

Member Function Documentation

template<typename... Ts>
pandora::StatusCode lar_content::LArMvaHelper::AddFeatureToolToMap ( pandora::AlgorithmTool *const  pFeatureTool,
std::string  pFeatureToolName,
MvaFeatureToolMap< Ts...> &  featureToolMap 
)
static

Add a feature tool to a map of feature tools.

Parameters
pFeatureToolthe feature tool
pFeatureToolNamethe name of the feature tool
featureToolMapthe map to append
Returns
success

Definition at line 462 of file LArMvaHelper.h.

464 {
465  if (MvaFeatureTool<Ts...> *const pCastFeatureTool = dynamic_cast<MvaFeatureTool<Ts...> *const>(pFeatureTool))
466  {
467  featureToolMap[pFeatureToolName] = pCastFeatureTool;
468  return pandora::STATUS_CODE_SUCCESS;
469  }
470 
471  return pandora::STATUS_CODE_FAILURE;
472 }
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 448 of file LArMvaHelper.h.

449 {
450  if (MvaFeatureTool<Ts...> *const pCastFeatureTool = dynamic_cast<MvaFeatureTool<Ts...> *const>(pFeatureTool))
451  {
452  featureToolVector.push_back(pCastFeatureTool);
453  return pandora::STATUS_CODE_SUCCESS;
454  }
455 
456  return pandora::STATUS_CODE_FAILURE;
457 }
template<typename TCONTAINER >
double lar_content::LArMvaHelper::CalculateClassificationScore ( const MvaInterface classifier,
TCONTAINER &&  featureContainer 
)
static

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

Parameters
classifierthe classifier
featureContainerthe container of features
Returns
the classification score

Definition at line 358 of file LArMvaHelper.h.

359 {
360  return classifier.CalculateClassificationScore(featureContainer);
361 }
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 396 of file LArMvaHelper.h.

397 {
398  LArMvaHelper::MvaFeatureVector featureVector;
399 
400  for (MvaFeatureTool<Ts...> *const pFeatureTool : featureToolVector)
401  pFeatureTool->Run(featureVector, std::forward<TARGS>(args)...);
402 
403  return featureVector;
404 }
MvaTypes::MvaFeatureVector MvaFeatureVector
Definition: LArMvaHelper.h:72
template<typename... Ts, typename... TARGS>
LArMvaHelper::MvaFeatureMap lar_content::LArMvaHelper::CalculateFeatures ( const pandora::StringVector &  featureToolOrder,
const MvaFeatureToolMap< Ts...> &  featureToolMap,
pandora::StringVector &  featureOrder,
TARGS &&...  args 
)
static

Calculate the features in a given feature tool map, and fill an MvaFeatureMap and vector with feature order.

Parameters
featureToolOrdervector of strings of the ordered keys
featureToolMapthe feature tool map
featureOrdera vector that is to be filled with the order of features in the function
argsarguments to pass to the tool
Returns
the map of features

Definition at line 409 of file LArMvaHelper.h.

411 {
412  LArMvaHelper::MvaFeatureMap featureMap;
413 
414  for (auto const &pFeatureToolName : featureToolOrder)
415  {
416  if (featureToolMap.find(pFeatureToolName) == featureToolMap.end())
417  {
418  std::cout << "LArMvaHelper::CalculateFeatures "
419  << "- Error: feature tool " << pFeatureToolName << " not found." << std::endl;
420  throw pandora::StatusCodeException(pandora::STATUS_CODE_NOT_FOUND);
421  }
422  featureToolMap.at(pFeatureToolName)->Run(featureMap, featureOrder, pFeatureToolName, std::forward<TARGS>(args)...);
423  }
424 
425  return featureMap;
426 }
MvaTypes::MvaFeatureMap MvaFeatureMap
Definition: LArMvaHelper.h:75
BEGIN_PROLOG could also be cout
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 431 of file LArMvaHelper.h.

432 {
433  using TD = typename std::decay<T>::type;
434  LArMvaHelper::MvaFeatureVector featureVector;
435 
436  for (MvaFeatureTool<Ts...> *const pFeatureTool : featureToolVector)
437  {
438  if (TD *const pCastFeatureTool = dynamic_cast<TD *const>(pFeatureTool))
439  pCastFeatureTool->Run(featureVector, std::forward<TARGS>(args)...);
440  }
441 
442  return featureVector;
443 }
MvaTypes::MvaFeatureVector MvaFeatureVector
Definition: LArMvaHelper.h:72
template<typename TCONTAINER >
double lar_content::LArMvaHelper::CalculateProbability ( const MvaInterface classifier,
TCONTAINER &&  featureContainer 
)
static

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

Parameters
classifierthe classifier
featureContainerthe container of features
Returns
the classification probability

Definition at line 366 of file LArMvaHelper.h.

367 {
368  return classifier.CalculateProbability(featureContainer);
369 }
template<typename TCONTAINER >
double lar_content::LArMvaHelper::CalculateProbability ( const MvaInterface classifier,
const pandora::StringVector &  featureOrder,
TCONTAINER &&  featureContainer 
)
static

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

Parameters
classifierthe classifier
featureOrderthe vector of strings corresponding to ordered list of keys
featureContainerthe container of features
Returns
the classification probability

Definition at line 374 of file LArMvaHelper.h.

375 {
376  // Make a feature vector from the map and calculate the features
377  LArMvaHelper::MvaFeatureVector featureVector;
378 
379  for (auto const &pFeatureToolName : featureOrder)
380  {
381  if (featureContainer.find(pFeatureToolName) == featureContainer.end())
382  {
383  std::cout << "LArMvaHelper::CalculateProbability "
384  << "- Error: feature tool " << pFeatureToolName << " not found." << std::endl;
385  throw pandora::StatusCodeException(pandora::STATUS_CODE_NOT_FOUND);
386  }
387  featureVector.push_back(featureContainer.at(pFeatureToolName));
388  }
389 
390  return CalculateProbability(classifier, featureVector);
391 }
MvaTypes::MvaFeatureVector MvaFeatureVector
Definition: LArMvaHelper.h:72
static double CalculateProbability(const MvaInterface &classifier, TCONTAINER &&featureContainer)
Use the trained mva to calculate a classification probability for an example.
Definition: LArMvaHelper.h:366
BEGIN_PROLOG could also be cout
template<typename TCONTAINER >
bool lar_content::LArMvaHelper::Classify ( const MvaInterface classifier,
TCONTAINER &&  featureContainer 
)
static

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

Parameters
classifierthe classifier
featureContainerthe container of features
Returns
the predicted boolean class of the example

Definition at line 328 of file LArMvaHelper.h.

329 {
330  return classifier.Classify(featureContainer);
331 }
template<typename TCONTAINER >
bool lar_content::LArMvaHelper::Classify ( const MvaInterface classifier,
const pandora::StringVector &  featureOrder,
TCONTAINER &&  featureContainer 
)
static

Use the trained classifier to predict the boolean class of an example – using a map.

Parameters
classifierthe classifier
featureOrderthe vector of strings corresponding to ordered list of keys
featureContainerthe container of features
Returns
the predicted boolean class of the example

Definition at line 336 of file LArMvaHelper.h.

337 {
338  // Make a feature vector from the map and calculate the features
339  LArMvaHelper::MvaFeatureVector featureVector;
340 
341  for (auto const &pFeatureToolName : featureOrder)
342  {
343  if (featureContainer.find(pFeatureToolName) == featureContainer.end())
344  {
345  std::cout << "LArMvaHelper::Classify "
346  << "- Error: feature tool " << pFeatureToolName << " not found." << std::endl;
347  throw pandora::StatusCodeException(pandora::STATUS_CODE_NOT_FOUND);
348  }
349  featureVector.push_back(featureContainer.at(pFeatureToolName));
350  }
351 
352  return Classify(classifier, featureVector);
353 }
static bool Classify(const MvaInterface &classifier, TCONTAINER &&featureContainer)
Use the trained classifier to predict the boolean class of an example.
Definition: LArMvaHelper.h:328
MvaTypes::MvaFeatureVector MvaFeatureVector
Definition: LArMvaHelper.h:72
BEGIN_PROLOG could also be cout
template<typename TLIST , typename... TLISTS>
LArMvaHelper::MvaFeatureVector lar_content::LArMvaHelper::ConcatenateFeatureLists ( TLIST &&  featureList,
TLISTS &&...  featureLists 
)
static

Recursively concatenate vectors of features.

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

Definition at line 520 of file LArMvaHelper.h.

521 {
522  static_assert(std::is_same<typename std::decay<TLIST>::type, LArMvaHelper::MvaFeatureVector>::value,
523  "LArMvaHelper: Could not concatenate feature lists because one or more lists was not a vector of MvaFeatures");
524 
525  LArMvaHelper::MvaFeatureVector featureVector;
526 
527  for (const MvaFeature &feature : featureList)
528  featureVector.push_back(feature);
529 
530  LArMvaHelper::MvaFeatureVector newFeatureVector = ConcatenateFeatureLists(std::forward<TLISTS>(featureLists)...);
531  featureVector.insert(featureVector.end(), newFeatureVector.begin(), newFeatureVector.end());
532 
533  return featureVector;
534 }
MvaTypes::MvaFeatureVector MvaFeatureVector
Definition: LArMvaHelper.h:72
MvaTypes::MvaFeature MvaFeature
Definition: LArMvaHelper.h:71
static MvaFeatureVector ConcatenateFeatureLists()
Recursively concatenate vectors of features (terminating method)
Definition: LArMvaHelper.h:538
temporary value
LArMvaHelper::MvaFeatureVector lar_content::LArMvaHelper::ConcatenateFeatureLists ( )
inlinestatic

Recursively concatenate vectors of features (terminating method)

Definition at line 538 of file LArMvaHelper.h.

539 {
541 }
MvaTypes::MvaFeatureVector MvaFeatureVector
Definition: LArMvaHelper.h:72
std::string lar_content::LArMvaHelper::GetTimestampString ( )
inlinestaticprivate

Get a timestamp string for this point in time.

Returns
a timestamp string

Definition at line 476 of file LArMvaHelper.h.

477 {
478  std::time_t timestampNow = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
479 
480  struct tm *pTimeInfo(NULL);
481  char buffer[80];
482 
483  pTimeInfo = localtime(&timestampNow);
484  strftime(buffer, 80, "%x_%X", pTimeInfo);
485 
486  std::string timeString(buffer);
487 
488  if (!timeString.empty() && timeString.back() == '\n') // last char is always a newline
489  timeString.pop_back();
490 
491  return timeString;
492 }
StatusCode lar_content::LArMvaHelper::ProcessAlgorithmToolListToMap ( const pandora::Algorithm &  algorithm,
const pandora::TiXmlHandle &  xmlHandle,
const std::string &  listName,
pandora::StringVector &  algorithToolNameVector,
AlgorithmToolMap algorithmToolMap 
)
static

Process a list of algorithms tools in an xml file, using a map. Idea is for this to go to XmlHelper in PandoraSDK eventually as an overload to ProcessAlgorithmToolList.

Parameters
algorithmthe parent algorithm calling this function
xmlHandlethe relevant xml handle
listNamethe name of the algorithm tool list
algorithmToolMapto receive the vector of addresses of the algorithm tool instances, but also keep the name

Definition at line 16 of file LArMvaHelper.cc.

18 {
19  // Fill a vector with names in the desired run order as well as the map
20 
21  if ("algorithm" != xmlHandle.ToNode()->ValueStr())
22  return STATUS_CODE_NOT_ALLOWED;
23 
24  const TiXmlHandle algorithmListHandle = TiXmlHandle(xmlHandle.FirstChild(listName).Element());
25 
26  for (TiXmlElement *pXmlElement = algorithmListHandle.FirstChild("tool").Element(); nullptr != pXmlElement;
27  pXmlElement = pXmlElement->NextSiblingElement("tool"))
28  {
29  AlgorithmTool *pAlgorithmTool(nullptr);
30  PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, PandoraContentApi::CreateAlgorithmTool(algorithm, pXmlElement, pAlgorithmTool));
31  std::string toolName = pXmlElement->Attribute("type");
32  // If already exists, then make the second have the instance name attached
33  if (algorithmToolMap.find(toolName) != algorithmToolMap.end())
34  toolName += "_" + pAlgorithmTool->GetInstanceName();
35  algorithmToolMap[toolName] = pAlgorithmTool;
36  algorithmToolNameVector.push_back(toolName);
37  }
38 
39  return STATUS_CODE_SUCCESS;
40 }
template<typename TCONTAINER >
pandora::StatusCode lar_content::LArMvaHelper::ProduceTrainingExample ( const std::string &  trainingOutputFile,
const bool  result,
TCONTAINER &&  featureContainer 
)
static

Produce a training example with the given features and result.

Parameters
trainingOutputFilethe file to which to append the example
featureContainerthe container of features
Returns
success

Definition at line 282 of file LArMvaHelper.h.

283 {
284  std::ofstream outfile;
285  outfile.open(trainingOutputFile, std::ios_base::app); // always append to the output file
286 
287  if (!outfile.is_open())
288  {
289  std::cout << "LArMvaHelper: could not open file for training examples at " << trainingOutputFile << std::endl;
290  return pandora::STATUS_CODE_FAILURE;
291  }
292 
293  std::string delimiter(",");
294  outfile << GetTimestampString() << delimiter;
295 
296  PANDORA_RETURN_RESULT_IF(pandora::STATUS_CODE_SUCCESS, !=, WriteFeaturesToFile(outfile, delimiter, featureContainer));
297  outfile << static_cast<int>(result) << '\n';
298 
299  return pandora::STATUS_CODE_SUCCESS;
300 }
static std::string GetTimestampString()
Get a timestamp string for this point in time.
Definition: LArMvaHelper.h:476
static pandora::StatusCode WriteFeaturesToFile(std::ofstream &outfile, const std::string &delimiter, TCONTAINER &&featureContainer)
Write the features of the given lists to file.
Definition: LArMvaHelper.h:497
BEGIN_PROLOG could also be cout
template<typename TCONTAINER >
pandora::StatusCode lar_content::LArMvaHelper::ProduceTrainingExample ( const std::string &  trainingOutputFile,
const bool  result,
const pandora::StringVector &  featureOrder,
TCONTAINER &&  featureContainer 
)
static

Produce a training example with the given features and result - using a map.

Parameters
trainingOutputFilethe file to which to append the example
featureOrderthe vector of strings corresponding to ordered list of keys
featureContainerthe container of features
Returns
success

Definition at line 305 of file LArMvaHelper.h.

307 {
308  // Make a feature vector from the map and calculate the features
309  LArMvaHelper::MvaFeatureVector featureVector;
310 
311  for (auto const &pFeatureToolName : featureOrder)
312  {
313  if (featureContainer.find(pFeatureToolName) == featureContainer.end())
314  {
315  std::cout << "LArMvaHelper::ProduceTrainingExample "
316  << "- Error: feature tool " << pFeatureToolName << " not found." << std::endl;
317  throw pandora::StatusCodeException(pandora::STATUS_CODE_NOT_FOUND);
318  }
319  featureVector.push_back(featureContainer.at(pFeatureToolName));
320  }
321 
322  return ProduceTrainingExample(trainingOutputFile, result, featureVector);
323 }
MvaTypes::MvaFeatureVector MvaFeatureVector
Definition: LArMvaHelper.h:72
static pandora::StatusCode ProduceTrainingExample(const std::string &trainingOutputFile, const bool result, TCONTAINER &&featureContainer)
Produce a training example with the given features and result.
Definition: LArMvaHelper.h:282
BEGIN_PROLOG could also be cout
template<typename TCONTAINER >
pandora::StatusCode lar_content::LArMvaHelper::WriteFeaturesToFile ( std::ofstream &  outfile,
const std::string &  delimiter,
TCONTAINER &&  featureContainer 
)
inlinestaticprivate

Write the features of the given lists to file.

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

Definition at line 497 of file LArMvaHelper.h.

498 {
499  static_assert(std::is_same<typename std::decay<TCONTAINER>::type, LArMvaHelper::MvaFeatureVector>::value,
500  "LArMvaHelper: Could not write training set example because a passed parameter was not a vector of MvaFeatures");
501 
502  PANDORA_RETURN_RESULT_IF(pandora::STATUS_CODE_SUCCESS, !=, WriteFeaturesToFileImpl(outfile, delimiter, featureContainer));
503  return pandora::STATUS_CODE_SUCCESS;
504 }
static pandora::StatusCode WriteFeaturesToFileImpl(std::ofstream &outfile, const std::string &delimiter, TCONTAINER &&featureContainer)
Write the features of the given list to file (implementation method)
Definition: LArMvaHelper.h:509
MvaTypes::MvaFeatureVector MvaFeatureVector
Definition: LArMvaHelper.h:72
temporary value
template<typename TCONTAINER >
pandora::StatusCode lar_content::LArMvaHelper::WriteFeaturesToFileImpl ( std::ofstream &  outfile,
const std::string &  delimiter,
TCONTAINER &&  featureContainer 
)
staticprivate

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

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

Definition at line 509 of file LArMvaHelper.h.

510 {
511  for (const MvaFeature &feature : featureContainer)
512  outfile << feature.Get() << delimiter;
513 
514  return pandora::STATUS_CODE_SUCCESS;
515 }
MvaTypes::MvaFeature MvaFeature
Definition: LArMvaHelper.h:71

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