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>
16#include <mva/interface/Interface.h>
18#include <framework/logging/Logger.h>
27 setDescription(
"Adds ExtraInfos to the Particle objects in given ParticleLists which is calculated by multiple experts defined by the given weightfiles.");
30 std::vector<std::string> empty;
32 "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*",
35 "Names under which the output of the experts is stored in the ExtraInfo of the Particle object.");
36 addParam(
"identifiers",
m_identifiers,
"The database identifiers which is used to load the weights during the training.");
38 "signalFraction to calculate probability (if -1 the signalFraction of the training data is used)", -1.0);
39 std::vector<int> empty_vec;
41 "If true, when the given extraInfo has already defined, the old extraInfo value is overwritten. If false, the original value is kept.",
55 extraInfo.isRequired();
58 extraInfo.isRequired();
62 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!");
101 }
else B2FATAL(
"No m_identifiers given. At least one is needed!");
112 weightfile.getOptions(general_options);
118 m_experts[i] = supported_interfaces[general_options.m_method]->getExpert();
124 B2FATAL(
"One or more feature variables could not be loaded via the Variable::Manager. Check the names!");
133 std::vector<float> dummy;
135 m_datasets[i] = std::make_unique<MVA::SingleDataset>(general_options, dummy, 0);
144 if (iVariable.first->variabletype == Variable::Manager::VariableDataType::c_double) {
145 m_feature_variables[iVariable.first] = std::get<double>(iVariable.first->function(particle));
146 }
else if (iVariable.first->variabletype == Variable::Manager::VariableDataType::c_int) {
148 }
else if (iVariable.first->variabletype == Variable::Manager::VariableDataType::c_bool) {
149 m_feature_variables[iVariable.first] = std::get<bool>(iVariable.first->function(particle));
162 std::vector<std::vector<float>> responseValues;
171 B2ERROR(
"Received a value of " <<
m_nClasses[i] <<
172 " for the number of classes considered by the MVA Expert. This value should be >=2.");
175 return responseValues;
181 if (particle->hasExtraInfo(extraInfoName)) {
182 if (particle->getExtraInfo(extraInfoName) != responseValue) {
184 double current = particle->getExtraInfo(extraInfoName);
186 if (responseValue < current) particle->setExtraInfo(extraInfoName, responseValue);
190 if (responseValue > current) particle->setExtraInfo(extraInfoName, responseValue);
192 particle->setExtraInfo(extraInfoName, responseValue);
198 particle->addExtraInfo(extraInfoName, responseValue);
203 float responseValue,
unsigned int i)
205 if (eventExtraInfo->hasExtraInfo(extraInfoName)) {
207 double current = eventExtraInfo->getExtraInfo(extraInfoName);
209 if (responseValue < current) eventExtraInfo->setExtraInfo(extraInfoName, responseValue);
213 if (responseValue > current) eventExtraInfo->setExtraInfo(extraInfoName, responseValue);
215 eventExtraInfo->setExtraInfo(extraInfoName, responseValue);
220 eventExtraInfo->addExtraInfo(extraInfoName, responseValue);
229 for (
unsigned i = 0; i < list->getListSize(); ++i) {
230 Particle* particle = list->getParticle(i);
231 std::vector<std::vector<float>> responseValues =
analyse(particle);
236 if (responseValues[j].size() !=
m_nClasses[j]) {
237 B2ERROR(
"Size of results returned by MVA Expert applyMulticlass (" << responseValues[j].size() <<
238 ") does not match the declared number of classes (" <<
m_nClasses[j] <<
").");
240 for (
unsigned int iClass = 0; iClass <
m_nClasses[j]; iClass++) {
244 B2ERROR(
"Received a value of " <<
m_nClasses[j] <<
245 " for the number of classes considered by the MVA Expert. This value should be >=2.");
252 if (not eventExtraInfo.isValid())
253 eventExtraInfo.create();
254 std::vector<std::vector<float>> responseValues =
analyse(
nullptr);
259 if (responseValues[j].size() !=
m_nClasses[j]) {
260 B2ERROR(
"Size of results returned by MVA Expert applyMulticlass (" << responseValues[j].size() <<
261 ") does not match the declared number of classes (" <<
m_nClasses[j] <<
").");
263 for (
unsigned int iClass = 0; iClass <
m_nClasses[j]; iClass++) {
267 B2ERROR(
"Received a value of " <<
m_nClasses[j] <<
268 " for the number of classes considered by the MVA Expert. This value should be >=2.");
283 " has already been set! It was overwritten by this module if the new value was lower than the previous!");
286 " has already been set! The original value was kept and this module did not overwrite it!");
289 " has already been set! It was overwritten by this module if the new value was higher than the previous!");
291 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.
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.
void setExtraInfoField(Particle *, const std::string &, float, unsigned int)
Set the extra info field.
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 initializes all supported interfaces, has to be called once before getSupported...
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.
static Weightfile loadFromFile(const std::string &filename)
Static function which loads a Weightfile from a file.
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.
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.