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 <boost/algorithm/string/predicate.hpp>
20#include <framework/logging/Logger.h>
29 setDescription(
"Adds ExtraInfos to the Particle objects in given ParticleLists which is calcuated by multiple experts defined by the given weightfiles.");
32 std::vector<std::string> empty;
34 "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*",
37 "Names under which the output of the experts is stored in the ExtraInfo of the Particle object.");
38 addParam(
"identifiers",
m_identifiers,
"The database identifiers which is used to load the weights during the training.");
40 "signalFraction to calculate probability (if -1 the signalFraction of the training data is used)", -1.0);
41 std::vector<int> empty_vec;
43 "If true, when the given extraInfo has already defined, the old extraInfo value is overwritten. If false, the original value is kept.",
57 extraInfo.isRequired();
60 extraInfo.isRequired();
64 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!");
103 }
else B2FATAL(
"No m_identifiers given. At least one is needed!");
120 m_experts[i] = supported_interfaces[general_options.m_method]->getExpert();
126 B2FATAL(
"One or more feature variables could not be loaded via the Variable::Manager. Check the names!");
135 std::vector<float> dummy;
137 m_datasets[i] = std::make_unique<MVA::SingleDataset>(general_options, dummy, 0);
146 if (iVariable.first->variabletype == Variable::Manager::VariableDataType::c_double) {
147 m_feature_variables[iVariable.first] = std::get<double>(iVariable.first->function(particle));
148 }
else if (iVariable.first->variabletype == Variable::Manager::VariableDataType::c_int) {
150 }
else if (iVariable.first->variabletype == Variable::Manager::VariableDataType::c_bool) {
151 m_feature_variables[iVariable.first] = std::get<bool>(iVariable.first->function(particle));
164 std::vector<std::vector<float>> responseValues;
173 B2ERROR(
"Received a value of " <<
m_nClasses[i] <<
174 " for the number of classes considered by the MVA Expert. This value should be >=2.");
177 return responseValues;
182 if (particle->hasExtraInfo(extraInfoName)) {
183 if (particle->getExtraInfo(extraInfoName) != responseValue) {
185 double current = particle->getExtraInfo(extraInfoName);
187 if (responseValue < current) particle->setExtraInfo(extraInfoName, responseValue);
191 if (responseValue > current) particle->setExtraInfo(extraInfoName, responseValue);
193 particle->setExtraInfo(extraInfoName, responseValue);
199 particle->addExtraInfo(extraInfoName, responseValue);
204 float responseValue,
unsigned int i)
206 if (eventExtraInfo->hasExtraInfo(extraInfoName)) {
208 double current = eventExtraInfo->getExtraInfo(extraInfoName);
210 if (responseValue < current) eventExtraInfo->setExtraInfo(extraInfoName, responseValue);
214 if (responseValue > current) eventExtraInfo->setExtraInfo(extraInfoName, responseValue);
216 eventExtraInfo->setExtraInfo(extraInfoName, responseValue);
221 eventExtraInfo->addExtraInfo(extraInfoName, responseValue);
230 for (
unsigned i = 0; i < list->getListSize(); ++i) {
231 Particle* particle = list->getParticle(i);
232 std::vector<std::vector<float>> responseValues =
analyse(particle);
237 if (responseValues[j].size() !=
m_nClasses[j]) {
238 B2ERROR(
"Size of results returned by MVA Expert applyMulticlass (" << responseValues[j].size() <<
239 ") does not match the declared number of classes (" <<
m_nClasses[j] <<
").");
241 for (
unsigned int iClass = 0; iClass <
m_nClasses[j]; iClass++) {
245 B2ERROR(
"Received a value of " <<
m_nClasses[j] <<
246 " for the number of classes considered by the MVA Expert. This value should be >=2.");
253 if (not eventExtraInfo.isValid())
254 eventExtraInfo.create();
255 std::vector<std::vector<float>> responseValues =
analyse(
nullptr);
260 if (responseValues[j].size() !=
m_nClasses[j]) {
261 B2ERROR(
"Size of results returned by MVA Expert applyMulticlass (" << responseValues[j].size() <<
262 ") does not match the declared number of classes (" <<
m_nClasses[j] <<
").");
264 for (
unsigned int iClass = 0; iClass <
m_nClasses[j]; iClass++) {
268 B2ERROR(
"Received a value of " <<
m_nClasses[j] <<
269 " for the number of classes considered by the MVA Expert. This value should be >=2.");
284 " has already been set! It was overwritten by this module if the new value was lower than the previous!");
287 " has already been set! The original value was kept and this module did not overwrite it!");
290 " has already been set! It was overwritten by this module if the new value was higher than the previous!");
292 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.
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.