8#ifndef LAR_MVA_HELPER_H
9#define LAR_MVA_HELPER_H 1
32template <
typename... Ts>
55 (void)featureToolName;
60template <
typename... Ts>
63template <
typename... Ts>
89 template <
typename TCONTAINER>
101 template <
typename TCONTAINER>
103 const std::string &trainingOutputFile,
const bool result,
const pandora::StringVector &featureOrder, TCONTAINER &&featureContainer);
113 template <
typename TCONTAINER>
125 template <
typename TCONTAINER>
136 template <
typename TCONTAINER>
147 template <
typename TCONTAINER>
159 template <
typename TCONTAINER>
170 template <
typename... Ts,
typename... TARGS>
183 template <
typename... Ts,
typename... TARGS>
195 template <
typename T,
typename... Ts,
typename... TARGS>
206 template <
typename... Ts>
218 template <
typename... Ts>
241 template <
typename TLIST,
typename... TLISTS>
266 template <
typename TCONTAINER>
278 template <
typename TCONTAINER>
284template <
typename TCONTAINER>
287 std::ofstream outfile;
288 outfile.open(trainingOutputFile, std::ios_base::app);
290 if (!outfile.is_open())
292 std::cout <<
"LArMvaHelper: could not open file for training examples at " << trainingOutputFile << std::endl;
293 return pandora::STATUS_CODE_FAILURE;
296 std::string delimiter(
",");
300 outfile << static_cast<int>(result) <<
'\n';
302 return pandora::STATUS_CODE_SUCCESS;
307template <
typename TCONTAINER>
309 const std::string &trainingOutputFile,
const bool result,
const pandora::StringVector &featureOrder, TCONTAINER &&featureContainer)
314 for (
auto const &pFeatureToolName : featureOrder)
316 if (featureContainer.find(pFeatureToolName) == featureContainer.end())
318 std::cout <<
"LArMvaHelper::ProduceTrainingExample "
319 <<
"- Error: feature tool " << pFeatureToolName <<
" not found." << std::endl;
322 featureVector.push_back(featureContainer.at(pFeatureToolName));
330template <
typename TCONTAINER>
333 return classifier.
Classify(featureContainer);
338template <
typename TCONTAINER>
344 for (
auto const &pFeatureToolName : featureOrder)
346 if (featureContainer.find(pFeatureToolName) == featureContainer.end())
348 std::cout <<
"LArMvaHelper::Classify "
349 <<
"- Error: feature tool " << pFeatureToolName <<
" not found." << std::endl;
352 featureVector.push_back(featureContainer.at(pFeatureToolName));
355 return Classify(classifier, featureVector);
360template <
typename TCONTAINER>
368template <
typename TCONTAINER>
376template <
typename TCONTAINER>
382 for (
auto const &pFeatureToolName : featureOrder)
384 if (featureContainer.find(pFeatureToolName) == featureContainer.end())
386 std::cout <<
"LArMvaHelper::CalculateProbability "
387 <<
"- Error: feature tool " << pFeatureToolName <<
" not found." << std::endl;
390 featureVector.push_back(featureContainer.at(pFeatureToolName));
398template <
typename... Ts,
typename... TARGS>
404 pFeatureTool->Run(featureVector, std::forward<TARGS>(args)...);
406 return featureVector;
411template <
typename... Ts,
typename... TARGS>
417 for (
auto const &pFeatureToolName : featureToolOrder)
419 if (featureToolMap.find(pFeatureToolName) == featureToolMap.end())
421 std::cout <<
"LArMvaHelper::CalculateFeatures "
422 <<
"- Error: feature tool " << pFeatureToolName <<
" not found." << std::endl;
425 featureToolMap.at(pFeatureToolName)->Run(featureMap, featureOrder, pFeatureToolName, std::forward<TARGS>(args)...);
433template <
typename T,
typename... Ts,
typename... TARGS>
436 using TD =
typename std::decay<T>::type;
441 if (TD *
const pCastFeatureTool =
dynamic_cast<TD *const
>(pFeatureTool))
442 pCastFeatureTool->Run(featureVector, std::forward<TARGS>(args)...);
445 return featureVector;
450template <
typename... Ts>
455 featureToolVector.push_back(pCastFeatureTool);
456 return pandora::STATUS_CODE_SUCCESS;
459 return pandora::STATUS_CODE_FAILURE;
464template <
typename... Ts>
470 featureToolMap[pFeatureToolName] = pCastFeatureTool;
471 return pandora::STATUS_CODE_SUCCESS;
474 return pandora::STATUS_CODE_FAILURE;
481 std::time_t timestampNow = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
483 struct tm *pTimeInfo(NULL);
486 pTimeInfo = localtime(×tampNow);
487 strftime(buffer, 80,
"%x_%X", pTimeInfo);
489 std::string timeString(buffer);
491 if (!timeString.empty() && timeString.back() ==
'\n')
492 timeString.pop_back();
499template <
typename TCONTAINER>
503 "LArMvaHelper: Could not write training set example because a passed parameter was not a vector of MvaFeatures");
506 return pandora::STATUS_CODE_SUCCESS;
511template <
typename TCONTAINER>
514 for (
const MvaFeature &feature : featureContainer)
515 outfile << feature.Get() << delimiter;
517 return pandora::STATUS_CODE_SUCCESS;
522template <
typename TLIST,
typename... TLISTS>
526 "LArMvaHelper: Could not concatenate feature lists because one or more lists was not a vector of MvaFeatures");
531 featureVector.push_back(feature);
534 featureVector.insert(featureVector.end(), newFeatureVector.begin(), newFeatureVector.end());
536 return featureVector;
Header file for the algorithm class.
Header file for the lar multivariate analysis interface class.
Header file for the pandora content api class.
Header file defining relevant internal typedefs, sort and string conversion functions.
Header file defining status codes and relevant preprocessor macros.
#define PANDORA_RETURN_RESULT_IF(StatusCode1, Operator, Command)
Header file for the xml helper class.
MvaTypes::MvaFeature MvaFeature
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 i...
static std::string GetTimestampString()
Get a timestamp string for this point in time.
MvaTypes::MvaFeatureVector MvaFeatureVector
static pandora::StatusCode WriteFeaturesToFileImpl(std::ofstream &outfile, const std::string &delimiter, TCONTAINER &&featureContainer)
Write the features of the given list to file (implementation method)
static pandora::StatusCode WriteFeaturesToFile(std::ofstream &outfile, const std::string &delimiter, TCONTAINER &&featureContainer)
Write the features of the given lists to file.
static double CalculateProbability(const MvaInterface &classifier, TCONTAINER &&featureContainer)
Use the trained mva to calculate a classification probability for an example.
static MvaFeatureVector ConcatenateFeatureLists()
Recursively concatenate vectors of features (terminating method)
static double CalculateClassificationScore(const MvaInterface &classifier, TCONTAINER &&featureContainer)
Use the trained classifer to calculate the classification score of an example (>0 means boolean class...
std::map< std::string, pandora::AlgorithmTool * > AlgorithmToolMap
static bool Classify(const MvaInterface &classifier, TCONTAINER &&featureContainer)
Use the trained classifier to predict the boolean class of an example.
static pandora::StatusCode AddFeatureToolToVector(pandora::AlgorithmTool *const pFeatureTool, MvaFeatureToolVector< Ts... > &featureToolVector)
Add a feature tool to a vector of feature tools.
static MvaFeatureVector CalculateFeaturesOfType(const MvaFeatureToolVector< Ts... > &featureToolVector, TARGS &&... args)
Calculate the features of a given derived feature tool type in a feature tool vector.
static MvaFeatureVector CalculateFeatures(const MvaFeatureToolVector< Ts... > &featureToolVector, TARGS &&... args)
Calculate the features in a given feature tool vector.
static pandora::StatusCode AddFeatureToolToMap(pandora::AlgorithmTool *const pFeatureTool, std::string pFeatureToolName, MvaFeatureToolMap< Ts... > &featureToolMap)
Add a feature tool to a map of feature tools.
static pandora::StatusCode ProduceTrainingExample(const std::string &trainingOutputFile, const bool result, TCONTAINER &&featureContainer)
Produce a training example with the given features and result.
MvaTypes::MvaFeatureMap MvaFeatureMap
std::map< std::string, double > DoubleMap
MvaFeatureTool class template.
std::vector< MvaFeatureTool< Ts... > * > FeatureToolVector
virtual void Run(MvaTypes::MvaFeatureMap &featureMap, pandora::StringVector &featureOrder, const std::string &featureToolName, Ts...)
virtual void Run(MvaTypes::MvaFeatureVector &featureVector, Ts... args)=0
Run the algorithm tool.
MvaFeatureTool()=default
Default constructor.
std::map< std::string, MvaFeatureTool< Ts... > * > FeatureToolMap
virtual double CalculateClassificationScore(const MvaTypes::MvaFeatureVector &features) const =0
Calculate the classification score for a set of input features, based on the trained model.
virtual bool Classify(const MvaTypes::MvaFeatureVector &features) const =0
Classify the set of input features based on the trained model.
virtual double CalculateProbability(const MvaTypes::MvaFeatureVector &features) const =0
Calculate the classification probability for a set of input features, based on the trained model.
InitializedDouble class used to define mva features.
Algorithm class. Algorithm addresses are held only by the algorithm manager. They have a fully define...
StatusCodeException class.
std::map< std::string, MvaFeatureTool< Ts... > * > MvaFeatureToolMap
std::vector< MvaFeatureTool< Ts... > * > MvaFeatureToolVector
std::vector< MvaFeature > MvaFeatureVector
std::map< std::string, MvaFeature > MvaFeatureMap
std::vector< std::string > StringVector
StatusCode
The StatusCode enum.