8 #ifndef LAR_MVA_HELPER_H
9 #define LAR_MVA_HELPER_H 1
13 #include "Api/PandoraContentApi.h"
15 #include "Helpers/XmlHelper.h"
17 #include "Pandora/Algorithm.h"
18 #include "Pandora/AlgorithmTool.h"
19 #include "Pandora/PandoraInternal.h"
20 #include "Pandora/StatusCodes.h"
32 template <
typename... Ts>
57 template <
typename... Ts>
60 template <
typename... Ts>
86 template <
typename TCONTAINER>
87 static pandora::StatusCode
ProduceTrainingExample(
const std::string &trainingOutputFile,
const bool result, TCONTAINER &&featureContainer);
98 template <
typename TCONTAINER>
100 const std::string &trainingOutputFile,
const bool result,
const pandora::StringVector &featureOrder, TCONTAINER &&featureContainer);
110 template <
typename TCONTAINER>
122 template <
typename TCONTAINER>
123 static bool Classify(
const MvaInterface &classifier,
const pandora::StringVector &featureOrder, TCONTAINER &&featureContainer);
133 template <
typename TCONTAINER>
144 template <
typename TCONTAINER>
156 template <
typename TCONTAINER>
167 template <
typename... Ts,
typename... TARGS>
180 template <
typename... Ts,
typename... TARGS>
182 pandora::StringVector &featureOrder, TARGS &&...
args);
192 template <
typename T,
typename... Ts,
typename... TARGS>
203 template <
typename... Ts>
215 template <
typename... Ts>
217 pandora::AlgorithmTool *
const pFeatureTool, std::string pFeatureToolName,
MvaFeatureToolMap<Ts...> &featureToolMap);
228 const std::string &listName, pandora::StringVector &algorithToolNameVector,
AlgorithmToolMap &algorithmToolMap);
238 template <
typename TLIST,
typename... TLISTS>
263 template <
typename TCONTAINER>
264 static pandora::StatusCode
WriteFeaturesToFile(std::ofstream &outfile,
const std::string &delimiter, TCONTAINER &&featureContainer);
275 template <
typename TCONTAINER>
276 static pandora::StatusCode
WriteFeaturesToFileImpl(std::ofstream &outfile,
const std::string &delimiter, TCONTAINER &&featureContainer);
281 template <
typename TCONTAINER>
284 std::ofstream outfile;
285 outfile.open(trainingOutputFile, std::ios_base::app);
287 if (!outfile.is_open())
289 std::cout <<
"LArMvaHelper: could not open file for training examples at " << trainingOutputFile << std::endl;
290 return pandora::STATUS_CODE_FAILURE;
293 std::string delimiter(
",");
296 PANDORA_RETURN_RESULT_IF(pandora::STATUS_CODE_SUCCESS, !=,
WriteFeaturesToFile(outfile, delimiter, featureContainer));
297 outfile << static_cast<int>(result) <<
'\n';
299 return pandora::STATUS_CODE_SUCCESS;
304 template <
typename TCONTAINER>
306 const std::string &trainingOutputFile,
const bool result,
const pandora::StringVector &featureOrder, TCONTAINER &&featureContainer)
311 for (
auto const &pFeatureToolName : featureOrder)
313 if (featureContainer.find(pFeatureToolName) == featureContainer.end())
315 std::cout <<
"LArMvaHelper::ProduceTrainingExample "
316 <<
"- Error: feature tool " << pFeatureToolName <<
" not found." << std::endl;
317 throw pandora::StatusCodeException(pandora::STATUS_CODE_NOT_FOUND);
319 featureVector.push_back(featureContainer.at(pFeatureToolName));
327 template <
typename TCONTAINER>
330 return classifier.
Classify(featureContainer);
335 template <
typename TCONTAINER>
341 for (
auto const &pFeatureToolName : featureOrder)
343 if (featureContainer.find(pFeatureToolName) == featureContainer.end())
346 <<
"- Error: feature tool " << pFeatureToolName <<
" not found." << std::endl;
347 throw pandora::StatusCodeException(pandora::STATUS_CODE_NOT_FOUND);
349 featureVector.push_back(featureContainer.at(pFeatureToolName));
352 return Classify(classifier, featureVector);
357 template <
typename TCONTAINER>
365 template <
typename TCONTAINER>
373 template <
typename TCONTAINER>
379 for (
auto const &pFeatureToolName : featureOrder)
381 if (featureContainer.find(pFeatureToolName) == featureContainer.end())
383 std::cout <<
"LArMvaHelper::CalculateProbability "
384 <<
"- Error: feature tool " << pFeatureToolName <<
" not found." << std::endl;
385 throw pandora::StatusCodeException(pandora::STATUS_CODE_NOT_FOUND);
387 featureVector.push_back(featureContainer.at(pFeatureToolName));
395 template <
typename... Ts,
typename... TARGS>
401 pFeatureTool->Run(featureVector, std::forward<TARGS>(
args)...);
403 return featureVector;
408 template <
typename... Ts,
typename... TARGS>
414 for (
auto const &pFeatureToolName : featureToolOrder)
416 if (featureToolMap.find(pFeatureToolName) == featureToolMap.end())
418 std::cout <<
"LArMvaHelper::CalculateFeatures "
419 <<
"- Error: feature tool " << pFeatureToolName <<
" not found." << std::endl;
420 throw pandora::StatusCodeException(pandora::STATUS_CODE_NOT_FOUND);
422 featureToolMap.at(pFeatureToolName)->Run(featureMap, featureOrder, pFeatureToolName, std::forward<TARGS>(
args)...);
430 template <
typename T,
typename... Ts,
typename... TARGS>
438 if (TD *
const pCastFeatureTool = dynamic_cast<TD *const>(pFeatureTool))
439 pCastFeatureTool->Run(featureVector, std::forward<TARGS>(
args)...);
442 return featureVector;
447 template <
typename... Ts>
452 featureToolVector.push_back(pCastFeatureTool);
453 return pandora::STATUS_CODE_SUCCESS;
456 return pandora::STATUS_CODE_FAILURE;
461 template <
typename... Ts>
467 featureToolMap[pFeatureToolName] = pCastFeatureTool;
468 return pandora::STATUS_CODE_SUCCESS;
471 return pandora::STATUS_CODE_FAILURE;
480 struct tm *pTimeInfo(NULL);
483 pTimeInfo = localtime(×tampNow);
484 strftime(buffer, 80,
"%x_%X", pTimeInfo);
486 std::string timeString(buffer);
488 if (!timeString.empty() && timeString.back() ==
'\n')
489 timeString.pop_back();
496 template <
typename TCONTAINER>
500 "LArMvaHelper: Could not write training set example because a passed parameter was not a vector of MvaFeatures");
502 PANDORA_RETURN_RESULT_IF(pandora::STATUS_CODE_SUCCESS, !=,
WriteFeaturesToFileImpl(outfile, delimiter, featureContainer));
503 return pandora::STATUS_CODE_SUCCESS;
508 template <
typename TCONTAINER>
511 for (
const MvaFeature &feature : featureContainer)
512 outfile << feature.Get() << delimiter;
514 return pandora::STATUS_CODE_SUCCESS;
519 template <
typename TLIST,
typename... TLISTS>
523 "LArMvaHelper: Could not concatenate feature lists because one or more lists was not a vector of MvaFeatures");
528 featureVector.push_back(feature);
531 featureVector.insert(featureVector.end(), newFeatureVector.begin(), newFeatureVector.end());
533 return featureVector;
545 #endif // #ifndef LAR_MVA_HELPER_H
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 bool Classify(const MvaInterface &classifier, TCONTAINER &&featureContainer)
Use the trained classifier to predict the boolean class of an example.
std::map< std::string, pandora::AlgorithmTool * > AlgorithmToolMap
MvaTypes::MvaFeatureVector MvaFeatureVector
static pandora::StatusCode ProduceTrainingExample(const std::string &trainingOutputFile, const bool result, TCONTAINER &&featureContainer)
Produce a training example with the given features and result.
MvaFeatureTool()=default
Default constructor.
virtual void Run(MvaTypes::MvaFeatureVector &featureVector, Ts...args)=0
Run the algorithm tool.
static MvaFeatureVector CalculateFeatures(const MvaFeatureToolVector< Ts...> &featureToolVector, TARGS &&...args)
Calculate the features in a given feature tool vector.
virtual bool Classify(const MvaTypes::MvaFeatureVector &features) const =0
Classify the set of input features based on the trained model.
static std::string GetTimestampString()
Get a timestamp string for this point in time.
std::vector< MvaFeatureTool< Ts...> * > MvaFeatureToolVector
MvaTypes::MvaFeatureMap MvaFeatureMap
MvaTypes::MvaFeature MvaFeature
std::map< std::string, MvaFeatureTool< Ts...> * > FeatureToolMap
static pandora::StatusCode WriteFeaturesToFile(std::ofstream &outfile, const std::string &delimiter, TCONTAINER &&featureContainer)
Write the features of the given lists to file.
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
InitializedDouble class used to define mva features.
std::map< std::string, double > DoubleMap
static pandora::StatusCode AddFeatureToolToVector(pandora::AlgorithmTool *const pFeatureTool, MvaFeatureToolVector< Ts...> &featureToolVector)
Add a feature tool to a vector of feature tools.
std::map< std::string, MvaFeature > MvaFeatureMap
std::map< std::string, MvaFeatureTool< Ts...> * > MvaFeatureToolMap
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 pandora::StatusCode AddFeatureToolToMap(pandora::AlgorithmTool *const pFeatureTool, std::string pFeatureToolName, MvaFeatureToolMap< Ts...> &featureToolMap)
Add a feature tool to a map of feature tools.
virtual double CalculateProbability(const MvaTypes::MvaFeatureVector &features) const =0
Calculate the classification probability for a set of input features, based on the trained model...
std::vector< MvaFeatureTool< Ts...> * > FeatureToolVector
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 ConcatenateFeatureLists()
Recursively concatenate vectors of features (terminating method)
virtual void Run(MvaTypes::MvaFeatureMap &featureMap, pandora::StringVector &featureOrder, const std::string &featureToolName, Ts...args)
static double CalculateClassificationScore(const MvaInterface &classifier, TCONTAINER &&featureContainer)
Use the trained classifer to calculate the classification score of an example (>0 means boolean class...
static double CalculateProbability(const MvaInterface &classifier, TCONTAINER &&featureContainer)
Use the trained mva to calculate a classification probability for an example.
MvaFeatureTool class template.
virtual double CalculateClassificationScore(const MvaTypes::MvaFeatureVector &features) const =0
Calculate the classification score for a set of input features, based on the trained model...
std::vector< MvaFeature > MvaFeatureVector
BEGIN_PROLOG could also be cout
Header file for the lar multivariate analysis interface class.