10#include <mva/modules/MVAExpert/MVAMultipleExpertsModule.h>
12#include <analysis/dataobjects/Particle.h>
13#include <analysis/dataobjects/ParticleList.h>
14#include <analysis/dataobjects/ParticleExtraInfoMap.h>
15#include <framework/dataobjects/EventExtraInfo.h>
17#include <mva/interface/Interface.h>
19#include <boost/algorithm/string/predicate.hpp>
21#include <framework/logging/Logger.h>
30 setDescription(
"Adds ExtraInfos to the Particle objects in given ParticleLists which is calcuated by multiple experts defined by the given weightfiles.");
33 std::vector<std::string> empty;
35 "Particles from these ParticleLists are used as input. If no name is given the experts are applied to every event once, and one can only use variables which accept nullptr as Particle*",
38 "Names under which the output of the experts is stored in the ExtraInfo of the Particle object.");
39 addParam(
"identifiers",
m_identifiers,
"The database identifiers which is used to load the weights during the training.");
41 "signalFraction to calculate probability (if -1 the signalFraction of the training data is used)", -1.0);
42 std::vector<int> empty_vec;
44 "If true, when the given extraInfo has already defined, the old extraInfo value is overwritten. If false, the original value is kept.",
58 extraInfo.registerInDataStore();
61 extraInfo.registerInDataStore();
65 B2FATAL(
"The number of given m_extraInfoNames is not equal to the number of m_identifiers. The output the ith method in m_identifiers is saved as extraInfo under the ith name in m_extraInfoNames! Set also different names for each method!");
104 }
else B2FATAL(
"No m_identifiers given. At least one is needed!");
121 m_experts[i] = supported_interfaces[general_options.m_method]->getExpert();
127 B2FATAL(
"One or more feature variables could not be loaded via the Variable::Manager. Check the names!");
136 std::vector<float> dummy;
138 m_datasets[i] = std::make_unique<MVA::SingleDataset>(general_options, dummy, 0);
147 if (iVariable.first->variabletype == Variable::Manager::VariableDataType::c_double) {
148 m_feature_variables[iVariable.first] = std::get<double>(iVariable.first->function(particle));
149 }
else if (iVariable.first->variabletype == Variable::Manager::VariableDataType::c_int) {
151 }
else if (iVariable.first->variabletype == Variable::Manager::VariableDataType::c_bool) {
152 m_feature_variables[iVariable.first] = std::get<bool>(iVariable.first->function(particle));
165 std::vector<std::vector<float>> responseValues;
174 B2ERROR(
"Received a value of " <<
m_nClasses[i] <<
175 " for the number of classes considered by the MVA Expert. This value should be >=2.");
178 return responseValues;
184 if (particle->
getExtraInfo(extraInfoName) != responseValue) {
188 if (responseValue < current) particle->
setExtraInfo(extraInfoName, responseValue);
192 if (responseValue > current) particle->
setExtraInfo(extraInfoName, responseValue);
205 float responseValue,
unsigned int i)
207 if (eventExtraInfo->hasExtraInfo(extraInfoName)) {
209 double current = eventExtraInfo->getExtraInfo(extraInfoName);
211 if (responseValue < current) eventExtraInfo->setExtraInfo(extraInfoName, responseValue);
215 if (responseValue > current) eventExtraInfo->setExtraInfo(extraInfoName, responseValue);
217 eventExtraInfo->setExtraInfo(extraInfoName, responseValue);
222 eventExtraInfo->addExtraInfo(extraInfoName, responseValue);
231 for (
unsigned i = 0; i < list->getListSize(); ++i) {
232 Particle* particle = list->getParticle(i);
233 std::vector<std::vector<float>> responseValues =
analyse(particle);
238 if (responseValues[j].size() !=
m_nClasses[j]) {
239 B2ERROR(
"Size of results returned by MVA Expert applyMulticlass (" << responseValues[j].size() <<
240 ") does not match the declared number of classes (" <<
m_nClasses[j] <<
").");
242 for (
unsigned int iClass = 0; iClass <
m_nClasses[j]; iClass++) {
246 B2ERROR(
"Received a value of " <<
m_nClasses[j] <<
247 " for the number of classes considered by the MVA Expert. This value should be >=2.");
254 if (not eventExtraInfo.isValid())
255 eventExtraInfo.create();
256 std::vector<std::vector<float>> responseValues =
analyse(
nullptr);
261 if (responseValues[j].size() !=
m_nClasses[j]) {
262 B2ERROR(
"Size of results returned by MVA Expert applyMulticlass (" << responseValues[j].size() <<
263 ") does not match the declared number of classes (" <<
m_nClasses[j] <<
").");
265 for (
unsigned int iClass = 0; iClass <
m_nClasses[j]; iClass++) {
269 B2ERROR(
"Received a value of " <<
m_nClasses[j] <<
270 " for the number of classes considered by the MVA Expert. This value should be >=2.");
285 " has already been set! It was overwritten by this module if the new value was lower than the previous!");
288 " has already been set! The original value was kept and this module did not overwrite it!");
291 " has already been set! It was overwritten by this module if the new value was higher than the previous!");
293 B2WARNING(
"The extraInfo " <<
m_extraInfoNames[i] <<
" has already been set! It was overwritten by this module!");
@ c_Event
Different object in each event, all objects/arrays are invalidated after event() function has been ca...
void init_mva(MVA::Weightfile &weightfile, unsigned int i)
Initialize mva expert, dataset and features Called every time the weightfile in the database changes ...
std::vector< int > m_overwriteExistingExtraInfo
vector of -1/0/1/2: overwrite if lower/ don't overwrite / overwrite if higher/ always overwrite,...
std::vector< std::unique_ptr< MVA::Expert > > m_experts
Vector of pointers to the current MVA Experts.
void setExtraInfoField(Particle *, std::string, float, unsigned int)
Set the extra info field.
std::vector< unsigned int > m_nClasses
number of classes (~outputs) of the MVA Experts.
virtual void initialize() override
Initialize the module.
std::vector< std::unique_ptr< MVA::SingleDataset > > m_datasets
Vector of pointers to the current input datasets.
virtual void event() override
Called for each event.
std::vector< bool > m_existGivenExtraInfo
check if the given extraInfo is already defined.
std::vector< std::vector< const Variable::Manager::Var * > > m_individual_feature_variables
Vector of pointers to the feature variables for each expert.
virtual void terminate() override
Called at the end of the event processing.
double m_signal_fraction_override
Signal Fraction which should be used.
std::vector< std::string > m_identifiers
weight-files
std::vector< std::string > m_listNames
input particle list names
MVAMultipleExpertsModule()
Constructor.
void fillDatasets(Particle *)
Evaluate the variables and fill the Datasets to be used by the experts.
virtual void beginRun() override
Called at the beginning of a new run.
std::map< const Variable::Manager::Var *, float > m_feature_variables
Map containing the values of all needed feature variables.
std::vector< std::vector< float > > analyse(Particle *)
Calculates expert output for given Particle pointer.
void setEventExtraInfoField(StoreObjPtr< EventExtraInfo >, std::string, float, unsigned int)
Set the event extra info field.
std::vector< std::string > m_extraInfoNames
Names under which the SignalProbability is stored in the extraInfo of the Particle object.
std::vector< std::unique_ptr< DBObjPtr< DatabaseRepresentationOfWeightfile > > > m_weightfile_representations
Vector of database pointers to the Database representation of the weightfile.
static void initSupportedInterfaces()
Static function which initliazes all supported interfaces, has to be called once before getSupportedI...
static std::map< std::string, AbstractInterface * > getSupportedInterfaces()
Returns interfaces supported by the MVA Interface.
General options which are shared by all MVA trainings.
The Weightfile class serializes all information about a training into an xml tree.
static Weightfile loadFromStream(std::istream &stream)
Static function which deserializes a Weightfile from a stream.
void getOptions(Options &options) const
Fills an Option object from the xml tree.
static Weightfile loadFromFile(const std::string &filename)
Static function which loads a Weightfile from a file.
void addSignalFraction(float signal_fraction)
Saves the signal fraction in the xml tree.
void setDescription(const std::string &description)
Sets the description of the module.
void setPropertyFlags(unsigned int propertyFlags)
Sets the flags for the module properties.
@ c_ParallelProcessingCertified
This module can be run in parallel processing mode safely (All I/O must be done through the data stor...
Class to store reconstructed particles.
void setExtraInfo(const std::string &name, double value)
Sets the user-defined data of given name to the given value.
bool hasExtraInfo(const std::string &name) const
Return whether the extra info with the given name is set.
void addExtraInfo(const std::string &name, double value)
Sets the user-defined data of given name to the given value.
double getExtraInfo(const std::string &name) const
Return given value if set.
bool isRequired(const std::string &name="")
Ensure this array/object has been registered previously.
Type-safe access to single objects in the data store.
Global list of available variables.
static Manager & Instance()
get singleton instance.
void addParam(const std::string &name, T ¶mVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Abstract base class for different kinds of events.