8 #ifndef LAR_MVA_HELPER_H 9 #define LAR_MVA_HELPER_H 1 13 #include "Pandora/AlgorithmTool.h" 14 #include "Pandora/StatusCodes.h" 26 template <
typename... Ts>
46 template <
typename... Ts>
68 template <
typename... TLISTS>
69 static pandora::StatusCode ProduceTrainingExample(
const std::string &trainingOutputFile,
const bool result, TLISTS &&... featureLists);
79 template <
typename... TLISTS>
80 static bool Classify(
const MvaInterface &classifier, TLISTS &&... featureLists);
90 template <
typename... TLISTS>
91 static double CalculateClassificationScore(
const MvaInterface &classifier, TLISTS &&... featureLists);
101 template <
typename... TLISTS>
102 static double CalculateProbability(
const MvaInterface &classifier, TLISTS &&... featureLists);
112 template <
typename... Ts,
typename... TARGS>
123 template <
typename T,
typename... Ts,
typename... TARGS>
134 template <
typename... Ts>
135 static pandora::StatusCode AddFeatureToolToVector(pandora::AlgorithmTool *
const pFeatureTool,
MvaFeatureToolVector<Ts...> &featureToolVector);
155 template <
typename TLIST,
typename... TLISTS>
156 static pandora::StatusCode WriteFeaturesToFile(std::ofstream &
outfile,
const std::string &delimiter, TLIST &&featureList, TLISTS &&... featureLists);
163 static pandora::StatusCode WriteFeaturesToFile(std::ofstream &,
const std::string &);
174 template <
typename TLIST>
175 static pandora::StatusCode WriteFeaturesToFileImpl(std::ofstream &
outfile,
const std::string &delimiter, TLIST &&featureList);
185 template <
typename TLIST,
typename... TLISTS>
186 static MvaFeatureVector ConcatenateFeatureLists(TLIST &&featureList, TLISTS &&... featureLists);
191 static MvaFeatureVector ConcatenateFeatureLists();
196 template <
typename... TLISTS>
200 outfile.open(trainingOutputFile, std::ios_base::app);
202 if (!outfile.is_open())
204 std::cout <<
"LArMvaHelper: could not open file for training examples at " << trainingOutputFile <<
std::endl;
205 return pandora::STATUS_CODE_FAILURE;
209 outfile << GetTimestampString() << delimiter;
211 PANDORA_RETURN_RESULT_IF(pandora::STATUS_CODE_SUCCESS, !=, WriteFeaturesToFile(outfile, delimiter, featureLists...));
212 outfile << static_cast<int>(
result) <<
'\n';
214 return pandora::STATUS_CODE_SUCCESS;
219 template <
typename... TLISTS>
222 return classifier.
Classify(ConcatenateFeatureLists(std::forward<TLISTS>(featureLists)...));
227 template <
typename... TLISTS>
235 template <
typename... TLISTS>
238 return classifier.
CalculateProbability(ConcatenateFeatureLists(std::forward<TLISTS>(featureLists)...));
243 template <
typename... Ts,
typename... TARGS>
249 pFeatureTool->Run(featureVector, std::forward<TARGS>(
args)...);
251 return featureVector;
256 template <
typename T,
typename... Ts,
typename... TARGS>
264 if (TD *
const pCastFeatureTool = dynamic_cast<TD *const>(pFeatureTool))
265 pCastFeatureTool->Run(featureVector, std::forward<TARGS>(
args)...);
268 return featureVector;
273 template <
typename... Ts>
278 featureToolVector.push_back(pCastFeatureTool);
279 return pandora::STATUS_CODE_SUCCESS;
282 return pandora::STATUS_CODE_FAILURE;
291 struct tm *pTimeInfo(NULL);
294 pTimeInfo = localtime(×tampNow);
295 strftime(buffer, 80,
"%x_%X", pTimeInfo);
299 if (!timeString.empty() && timeString.back() ==
'\n')
300 timeString.pop_back();
307 template <
typename TLIST,
typename... TLISTS>
309 std::ofstream &
outfile,
const std::string &delimiter, TLIST &&featureList, TLISTS &&... featureLists)
312 "LArMvaHelper: Could not write training set example because a passed parameter was not a vector of MvaFeatures");
314 PANDORA_RETURN_RESULT_IF(pandora::STATUS_CODE_SUCCESS, !=, WriteFeaturesToFileImpl(outfile, delimiter, featureList));
315 return WriteFeaturesToFile(outfile, delimiter, featureLists...);
322 return pandora::STATUS_CODE_SUCCESS;
327 template <
typename TLIST>
331 outfile << feature.Get() << delimiter;
333 return pandora::STATUS_CODE_SUCCESS;
338 template <
typename TLIST,
typename... TLISTS>
342 "LArMvaHelper: Could not concatenate feature lists because one or more lists was not a vector of MvaFeatures");
347 featureVector.push_back(feature);
350 featureVector.insert(featureVector.end(), newFeatureVector.begin(), newFeatureVector.end());
352 return featureVector;
364 #endif // #ifndef LAR_MVA_HELPER_H
static double CalculateProbability(const MvaInterface &classifier, TLISTS &&...featureLists)
Use the trained mva to calculate a classification probability for an example.
static pandora::StatusCode WriteFeaturesToFileImpl(std::ofstream &outfile, const std::string &delimiter, TLIST &&featureList)
Write the features of the given list to file (implementation method)
MvaTypes::MvaFeatureVector MvaFeatureVector
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.
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.
static bool Classify(const MvaInterface &classifier, TLISTS &&...featureLists)
Use the trained classifier to predict the boolean class of an example.
std::vector< MvaFeatureTool< Ts... > * > MvaFeatureToolVector
MvaTypes::MvaFeature MvaFeature
static double CalculateClassificationScore(const MvaInterface &classifier, TLISTS &&...featureLists)
Use the trained classifer to calculate the classification score of an example (>0 means boolean class...
static pandora::StatusCode ProduceTrainingExample(const std::string &trainingOutputFile, const bool result, TLISTS &&...featureLists)
Produce a training example with the given features and result.
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.
static pandora::StatusCode AddFeatureToolToVector(pandora::AlgorithmTool *const pFeatureTool, MvaFeatureToolVector< Ts... > &featureToolVector)
Add a feature tool to a vector 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
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)
MvaFeatureTool class template.
std::vector< MvaFeature > MvaFeatureVector
Header file for the lar multivariate analysis interface class.
QTextStream & endl(QTextStream &s)