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>
55 (void)featureToolName;
60 template <
typename... Ts>
63 template <
typename... Ts>
89 template <
typename TCONTAINER>
90 static pandora::StatusCode ProduceTrainingExample(
const std::string &trainingOutputFile,
const bool result, TCONTAINER &&featureContainer);
101 template <
typename TCONTAINER>
102 static pandora::StatusCode ProduceTrainingExample(
103 const std::string &trainingOutputFile,
const bool result,
const pandora::StringVector &featureOrder, TCONTAINER &&featureContainer);
113 template <
typename TCONTAINER>
114 static bool Classify(
const MvaInterface &classifier, TCONTAINER &&featureContainer);
125 template <
typename TCONTAINER>
126 static bool Classify(
const MvaInterface &classifier,
const pandora::StringVector &featureOrder, TCONTAINER &&featureContainer);
136 template <
typename TCONTAINER>
137 static double CalculateClassificationScore(
const MvaInterface &classifier, TCONTAINER &&featureContainer);
147 template <
typename TCONTAINER>
148 static double CalculateProbability(
const MvaInterface &classifier, TCONTAINER &&featureContainer);
159 template <
typename TCONTAINER>
160 static double CalculateProbability(
const MvaInterface &classifier,
const pandora::StringVector &featureOrder, TCONTAINER &&featureContainer);
170 template <
typename... Ts,
typename... TARGS>
183 template <
typename... Ts,
typename... TARGS>
184 static MvaFeatureMap CalculateFeatures(
const pandora::StringVector &featureToolOrder,
const MvaFeatureToolMap<Ts...> &featureToolMap,
185 pandora::StringVector &featureOrder, TARGS &&...args);
195 template <
typename T,
typename... Ts,
typename... TARGS>
206 template <
typename... Ts>
207 static pandora::StatusCode AddFeatureToolToVector(pandora::AlgorithmTool *
const pFeatureTool,
MvaFeatureToolVector<Ts...> &featureToolVector);
218 template <
typename... Ts>
219 static pandora::StatusCode AddFeatureToolToMap(
220 pandora::AlgorithmTool *
const pFeatureTool, std::string pFeatureToolName,
MvaFeatureToolMap<Ts...> &featureToolMap);
230 static pandora::StatusCode ProcessAlgorithmToolListToMap(
const pandora::Algorithm &algorithm,
const pandora::TiXmlHandle &xmlHandle,
231 const std::string &listName, pandora::StringVector &algorithToolNameVector, AlgorithmToolMap &algorithmToolMap);
241 template <
typename TLIST,
typename... TLISTS>
242 static MvaFeatureVector ConcatenateFeatureLists(TLIST &&featureList, TLISTS &&...featureLists);
247 static MvaFeatureVector ConcatenateFeatureLists();
255 static std::string GetTimestampString();
266 template <
typename TCONTAINER>
267 static pandora::StatusCode WriteFeaturesToFile(std::ofstream &outfile,
const std::string &delimiter, TCONTAINER &&featureContainer);
278 template <
typename TCONTAINER>
279 static pandora::StatusCode WriteFeaturesToFileImpl(std::ofstream &outfile,
const std::string &delimiter, TCONTAINER &&featureContainer);
284 template <
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(
",");
297 outfile << GetTimestampString() << delimiter;
299 PANDORA_RETURN_RESULT_IF(pandora::STATUS_CODE_SUCCESS, !=, WriteFeaturesToFile(outfile, delimiter, featureContainer));
300 outfile << static_cast<int>(result) <<
'\n';
302 return pandora::STATUS_CODE_SUCCESS;
307 template <
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;
320 throw pandora::StatusCodeException(pandora::STATUS_CODE_NOT_FOUND);
322 featureVector.push_back(featureContainer.at(pFeatureToolName));
325 return ProduceTrainingExample(trainingOutputFile, result, featureVector);
330 template <
typename TCONTAINER>
333 return classifier.
Classify(featureContainer);
338 template <
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;
350 throw pandora::StatusCodeException(pandora::STATUS_CODE_NOT_FOUND);
352 featureVector.push_back(featureContainer.at(pFeatureToolName));
355 return Classify(classifier, featureVector);
360 template <
typename TCONTAINER>
368 template <
typename TCONTAINER>
376 template <
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;
388 throw pandora::StatusCodeException(pandora::STATUS_CODE_NOT_FOUND);
390 featureVector.push_back(featureContainer.at(pFeatureToolName));
393 return CalculateProbability(classifier, featureVector);
398 template <
typename... Ts,
typename... TARGS>
404 pFeatureTool->Run(featureVector, std::forward<TARGS>(args)...);
406 return featureVector;
411 template <
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;
423 throw pandora::StatusCodeException(pandora::STATUS_CODE_NOT_FOUND);
425 featureToolMap.at(pFeatureToolName)->Run(featureMap, featureOrder, pFeatureToolName, std::forward<TARGS>(args)...);
433 template <
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;
450 template <
typename... Ts>
455 featureToolVector.push_back(pCastFeatureTool);
456 return pandora::STATUS_CODE_SUCCESS;
459 return pandora::STATUS_CODE_FAILURE;
464 template <
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();
499 template <
typename TCONTAINER>
503 "LArMvaHelper: Could not write training set example because a passed parameter was not a vector of MvaFeatures");
505 PANDORA_RETURN_RESULT_IF(pandora::STATUS_CODE_SUCCESS, !=, WriteFeaturesToFileImpl(outfile, delimiter, featureContainer));
506 return pandora::STATUS_CODE_SUCCESS;
511 template <
typename TCONTAINER>
514 for (
const MvaFeature &feature : featureContainer)
515 outfile << feature.Get() << delimiter;
517 return pandora::STATUS_CODE_SUCCESS;
522 template <
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;
548 #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 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.
virtual bool Classify(const MvaTypes::MvaFeatureVector &features) const =0
Classify the set of input features based on the trained model.
static MvaFeatureVector CalculateFeaturesOfType(const MvaFeatureToolVector< Ts... > &featureToolVector, TARGS &&...args)
Calculate the features of a given derived feature tool type in a feature tool vector.
InitializedDouble class used to define mva features.
std::map< std::string, double > DoubleMap
std::map< std::string, MvaFeature > MvaFeatureMap
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, MvaFeatureTool< Ts... > * > MvaFeatureToolMap
virtual double CalculateProbability(const MvaTypes::MvaFeatureVector &features) const =0
Calculate the classification probability for a set of input features, based on the trained model...
virtual void Run(MvaTypes::MvaFeatureMap &featureMap, pandora::StringVector &featureOrder, const std::string &featureToolName, Ts...)
std::vector< MvaFeatureTool< Ts... > * > FeatureToolVector
virtual double CalculateClassificationScore(const MvaTypes::MvaFeatureVector &features) const =0
Calculate the classification score for a set of input features, based on the trained model...
static MvaFeatureVector CalculateFeatures(const MvaFeatureToolVector< Ts... > &featureToolVector, TARGS &&...args)
Calculate the features in a given feature tool vector.
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...
static double CalculateProbability(const MvaInterface &classifier, TCONTAINER &&featureContainer)
Use the trained mva to calculate a classification probability for an example.
MvaFeatureTool class template.
std::vector< MvaFeature > MvaFeatureVector
Header file for the lar multivariate analysis interface class.
static pandora::StatusCode AddFeatureToolToMap(pandora::AlgorithmTool *const pFeatureTool, std::string pFeatureToolName, MvaFeatureToolMap< Ts... > &featureToolMap)
Add a feature tool to a map of feature tools.