10#include <mva/modules/MVAExpert/MVAExpertModule.h>
12#include <analysis/DecayDescriptor/DecayDescriptorParticle.h>
14#include <analysis/dataobjects/Particle.h>
15#include <analysis/dataobjects/ParticleList.h>
16#include <analysis/dataobjects/ParticleExtraInfoMap.h>
17#include <framework/dataobjects/EventExtraInfo.h>
19#include <mva/interface/Interface.h>
21#include <boost/algorithm/string/predicate.hpp>
23#include <framework/logging/Logger.h>
32 setDescription(
"Adds an ExtraInfo to the Particle objects in given ParticleLists which is calcuated by an expert defined by a weightfile.");
35 std::vector<std::string> empty;
37 "Particles from these ParticleLists are used as input. If no name is given the expert is applied to every event once, and one can only use variables which accept nullptr as Particle*. Decay descriptor functionality is supported, which allows to run the module on daughter particles, e.g. Lambda0:my_list -> ^p+ pi-. One has to provide full name of the mother particle list and only one selected daughter is supported.",
40 "Name under which the output of the expert is stored in the ExtraInfo of the Particle object. If the expert returns multiple values, the index of the value is appended to the name in the form '_0', '_1', ...");
41 addParam(
"identifier",
m_identifier,
"The database identifier which is used to load the weights during the training.");
43 "signalFraction to calculate probability (if -1 the signalFraction of the training data is used)", -1.0);
45 "-1/0/1/2: Overwrite if lower / don't overwrite / overwrite if higher / always overwrite, in case the extra info with given name already exists",
54 bool valid = dd.
init(name);
56 B2ERROR(
"Decay string " << name <<
" is invalid.");
60 if (nSelectedDaughters > 1) {
61 B2ERROR(
"More than one daughter is selected in the decay string " << name <<
".");
71 extraInfo.registerInDataStore();
74 extraInfo.registerInDataStore();
91 std::stringstream ss((*m_weightfile_representation)->m_data);
113 m_expert = supported_interfaces[general_options.m_method]->getExpert();
119 B2FATAL(
"One or more feature variables could not be loaded via the Variable::Manager. Check the names!");
122 std::vector<float> dummy;
124 m_dataset = std::make_unique<MVA::SingleDataset>(general_options, dummy, 0);
132 if (std::holds_alternative<double>(var_result)) {
133 m_dataset->m_input[i] = std::get<double>(var_result);
134 }
else if (std::holds_alternative<int>(var_result)) {
135 m_dataset->m_input[i] = std::get<int>(var_result);
136 }
else if (std::holds_alternative<bool>(var_result)) {
137 m_dataset->m_input[i] = std::get<bool>(var_result);
145 B2ERROR(
"MVA Expert is not loaded! I will return 0");
155 B2ERROR(
"MVA Expert is not loaded! I will return 0");
165 if (particle->
getExtraInfo(extraInfoName) != responseValue) {
169 if (responseValue < current) particle->
setExtraInfo(extraInfoName, responseValue);
173 if (responseValue > current) particle->
setExtraInfo(extraInfoName, responseValue);
188 if (eventExtraInfo->hasExtraInfo(extraInfoName)) {
190 double current = eventExtraInfo->getExtraInfo(extraInfoName);
192 if (responseValue < current) eventExtraInfo->setExtraInfo(extraInfoName, responseValue);
196 if (responseValue > current) eventExtraInfo->setExtraInfo(extraInfoName, responseValue);
198 eventExtraInfo->setExtraInfo(extraInfoName, responseValue);
203 eventExtraInfo->addExtraInfo(extraInfoName, responseValue);
212 for (
unsigned i = 0; i < list->getListSize(); ++i) {
214 unsigned int nSelectedDaughters = dd.getSelectionNames().size();
215 const Particle* particle = (nSelectedDaughters > 0) ? dd.getSelectionParticles(list->getParticle(i))[0] : list->getParticle(i);
217 float responseValue =
analyse(particle);
222 B2ERROR(
"Size of results returned by MVA Expert applyMulticlass (" << responseValues.size() <<
223 ") does not match the declared number of classes (" <<
m_nClasses <<
").");
225 for (
unsigned int iClass = 0; iClass <
m_nClasses; iClass++) {
229 B2ERROR(
"Received a value of " <<
m_nClasses <<
230 " for the number of classes considered by the MVA Expert. This value should be >=2.");
236 if (not eventExtraInfo.isValid())
237 eventExtraInfo.create();
240 float responseValue =
analyse(
nullptr);
245 B2ERROR(
"Size of results returned by MVA Expert applyMulticlass (" << responseValues.size() <<
246 ") does not match the declared number of classes (" <<
m_nClasses <<
").");
248 for (
unsigned int iClass = 0; iClass <
m_nClasses; iClass++) {
252 B2ERROR(
"Received a value of " <<
m_nClasses <<
253 " for the number of classes considered by the MVA Expert. This value should be >=2.");
266 " has already been set! It was overwritten by this module if the new value was lower than the previous!");
269 " has already been set! The original value was kept and this module did not overwrite it!");
272 " has already been set! It was overwritten by this module if the new value was higher than the previous!");
274 B2WARNING(
"The extraInfo " <<
m_extraInfoName <<
" 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...
Represents a particle in the DecayDescriptor.
std::string getFullName() const
returns the full name of the particle full_name = name:label
The DecayDescriptor stores information about a decay tree or parts of a decay tree.
bool init(const std::string &str)
Initialise the DecayDescriptor from given string.
std::vector< std::string > getSelectionNames()
Return list of human readable names of selected particles.
const DecayDescriptorParticle * getMother() const
return mother.
std::unordered_map< std::string, DecayDescriptor > m_decaydescriptors
Decay descriptor of decays to look for.
std::unique_ptr< MVA::SingleDataset > m_dataset
Pointer to the current dataset.
std::vector< const Variable::Manager::Var * > m_feature_variables
Pointers to the feature variables.
virtual void initialize() override
Initialize the module.
virtual void event() override
Called for each event.
void setEventExtraInfoField(StoreObjPtr< EventExtraInfo >, std::string, float)
Set the event extra info field.
StoreArray< Particle > m_particles
StoreArray of Particles.
virtual void terminate() override
Called at the end of the event processing.
std::unique_ptr< MVA::Expert > m_expert
Pointer to the current MVA Expert.
double m_signal_fraction_override
Signal Fraction which should be used.
std::vector< std::string > m_targetListNames
input particle list names after decay descriptor
std::unique_ptr< DBObjPtr< DatabaseRepresentationOfWeightfile > > m_weightfile_representation
Database pointer to the Database representation of the weightfile.
std::vector< std::string > m_listNames
input particle list names
virtual void beginRun() override
Called at the beginning of a new run.
MVAExpertModule()
Constructor.
float analyse(const Particle *)
Calculates expert output for given Particle pointer.
bool m_existGivenExtraInfo
check if the given extraInfo is already defined.
int m_overwriteExistingExtraInfo
-1/0/1/2: overwrite if lower/ don't overwrite / overwrite if higher/ always overwrite,...
std::vector< float > analyseMulticlass(const Particle *)
Calculates expert output for given Particle pointer.
void init_mva(MVA::Weightfile &weightfile)
Initialize mva expert, dataset and features Called every time the weightfile in the database changes ...
std::string m_extraInfoName
Name under which the SignalProbability is stored in the extraInfo of the Particle object.
unsigned int m_nClasses
number of classes (~outputs) of the current MVA Expert.
void setExtraInfoField(Particle *, std::string, float)
Set the extra info field.
void fillDataset(const Particle *)
Evaluate the variables and fill the Dataset to be used by the expert.
std::string m_identifier
weight-file
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.
int getArrayIndex() const
Returns this object's array index (in StoreArray), or -1 if not found.
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.