Belle II Software development
MVAFilter< AVarSet > Class Template Reference

Convenience template to create a mva filter for a set of variables. More...

#include <MVAFilter.dcl.h>

Inheritance diagram for MVAFilter< AVarSet >:
MVA< Filter< AVarSet::Object > > OnVarSet< Filter< AVarSet::Object > > Filter< AVarSet::Object > CompositeProcessingSignalListener ProcessingSignalListener

Public Types

using Object = typename AVarSet::Object
 Type of the object to be analysed.
 
using Interface
 Mark this class as the basic interface.
 

Public Member Functions

 MVAFilter (const std::string &defaultTrainingName="", double defaultCut=NAN, const std::string &defaultDBObjectName="")
 Constructor of the filter.
 
 ~MVAFilter ()
 Default destructor.
 
void exposeParameters (ModuleParamList *moduleParamList, const std::string &prefix) override
 Expose the set of parameters of the filter to the module parameter list.
 
void initialize () override
 Initialize the expert before event processing.
 
void beginRun () override
 Signal to load new run parameters.
 
Weight operator() (const Object &obj) override
 Function to object for its signalness.
 
virtual std::vector< float > operator() (const std::vector< Object * > &objs) override
 Evaluate the MVA method over a vector of objects.
 
virtual Weight operator() (const Object &obj)
 Function to evaluate the object.
 
Weight operator() (const Object *obj)
 Function to evaluate the object.
 
virtual std::vector< float > operator() (const std::vector< Object * > &objs)
 Function to evaluate a vector of objects Base implementation applies the function to each object.
 
virtual double predict (const Object &obj)
 Evaluate the mva method.
 
std::vector< float > predict (const std::vector< Object * > &objs)
 Evaluate the MVA method over several inputs simultaneously.
 
bool needsTruthInformation () override
 Checks if any variables need Monte Carlo information.
 
std::unique_ptr< AVarSetreleaseVarSet () &&
 Steal the set of variables form this filter - filter becomes dysfunctional afterwards.
 
void beginEvent () override
 Receive and dispatch signal for the start of a new event.
 
void endRun () override
 Receive and dispatch signal for the end of the run.
 
void terminate () override
 Receive and dispatch Signal for termination of the event processing.
 

Protected Member Functions

AVarSetgetVarSet () const
 Getter for the set of variables.
 
void setVarSet (std::unique_ptr< AVarSet > varSet)
 Setter for the set of variables.
 
void addProcessingSignalListener (ProcessingSignalListener *psl)
 Register a processing signal listener to be notified.
 
int getNProcessingSignalListener ()
 Get the number of currently registered listeners.
 

Private Types

using Super = MVA<Filter<typename AVarSet::Object> >
 Type of the super class.
 
using AVarSet
 Type of the variable set.
 

Private Attributes

std::string m_identifier
 Database identifier of the expert or weight file name.
 
double m_cutValue
 The cut on the MVA output.
 
std::string m_DBPayloadName
 Name of the DB payload.
 
std::unique_ptr< MVAExpertm_mvaExpert
 MVA Expert to examine the object.
 
std::vector< Named< Float_t * > > m_namedVariables
 named variables, ordered as in the weightFile:
 
std::unique_ptr< AVarSetm_varSet
 Instance of the variable set to be used in the filter.
 
std::vector< ProcessingSignalListener * > m_subordinaryProcessingSignalListeners
 References to subordinary signal processing listener contained in this findlet.
 
bool m_initialized
 Flag to keep track whether initialization happened before.
 
bool m_terminated
 Flag to keep track whether termination happened before.
 
std::string m_initializedAs
 Name of the type during initialisation.
 

Detailed Description

template<class AVarSet>
class Belle2::TrackFindingCDC::MVAFilter< AVarSet >

Convenience template to create a mva filter for a set of variables.

Definition at line 100 of file MVAFilter.dcl.h.

Member Typedef Documentation

◆ AVarSet

using AVarSet
privateinherited

Type of the variable set.

Definition at line 48 of file MVAFilter.dcl.h.

◆ Interface

using Interface
inherited

Mark this class as the basic interface.

Definition at line 38 of file Filter.dcl.h.

◆ Object

template<class AVarSet>
using Object = typename AVarSet::Object

Type of the object to be analysed.

Definition at line 108 of file MVAFilter.dcl.h.

◆ Super

template<class AVarSet>
using Super = MVA<Filter<typename AVarSet::Object> >
private

Type of the super class.

Definition at line 104 of file MVAFilter.dcl.h.

Constructor & Destructor Documentation

◆ MVAFilter()

template<class AVarSet>
MVAFilter ( const std::string & defaultTrainingName = "",
double defaultCut = NAN,
const std::string & defaultDBObjectName = "" )
explicit

Constructor of the filter.

Definition at line 160 of file MVAFilter.icc.h.

163 : Super(std::make_unique<AVarSet>(), defaultTrainingName, defaultCut, defaultDBObjectName)
164 {
165 }

Member Function Documentation

◆ addProcessingSignalListener()

void addProcessingSignalListener ( ProcessingSignalListener * psl)
protectedinherited

Register a processing signal listener to be notified.

Definition at line 53 of file CompositeProcessingSignalListener.cc.

56{
58}
Base class for filters on a generic object type.
Definition Filter.dcl.h:31

◆ beginEvent()

void beginEvent ( )
overrideinherited

Receive and dispatch signal for the start of a new event.

Definition at line 36 of file CompositeProcessingSignalListener.cc.

32{
35 psl->beginEvent();
36 }
37}
void beginEvent() override
Receive and dispatch signal for the start of a new event.
virtual void beginEvent()
Receive signal for the start of a new event.

◆ beginRun()

void beginRun ( )
overridevirtualinherited

Signal to load new run parameters.

Make sure that the sequence of columns (features) is correct and follows the one from the weightFile

Reimplemented from CompositeProcessingSignalListener.

Definition at line 67 of file MVAFilter.icc.h.

91 {
92 Super::beginRun();
93 m_mvaExpert->beginRun();
95 const auto& selectedVars = m_mvaExpert->getVariableNames();
96 const std::vector<Named<Float_t*>>& namedVariables = Super::getVarSet().getNamedVariables();
97 m_namedVariables.clear();
98 for (const auto& name : selectedVars) {
99
100 auto itNamedVariable = std::find_if(namedVariables.begin(),
101 namedVariables.end(),
102 [name](const Named<Float_t*>& namedVariable) {
103 return namedVariable.getName() == name;
104 });
105 if (itNamedVariable == namedVariables.end()) {
106 B2ERROR("Variable name " << name << " mismatch for MVA filter. " <<
107 "Could not find expected variable '" << name << "'");
108 }
109 m_namedVariables.push_back(*itNamedVariable);
110 }
111 }

◆ endRun()

void endRun ( )
overrideinherited

Receive and dispatch signal for the end of the run.

Definition at line 39 of file CompositeProcessingSignalListener.cc.

40{
42 psl->endRun();
43 }
45}
void endRun() override
Receive and dispatch signal for the end of the run.
virtual void endRun()
Receive signal for the end of the run.

◆ exposeParameters()

void exposeParameters ( ModuleParamList * moduleParamList,
const std::string & prefix )
overridevirtualinherited

Expose the set of parameters of the filter to the module parameter list.

Reimplemented from OnVarSet< Filter< AVarSet::Object > >.

Definition at line 61 of file MVAFilter.icc.h.

50 {
51 Super::exposeParameters(moduleParamList, prefix);
52 moduleParamList->addParameter(prefixed(prefix, "cut"),
53 m_cutValue,
54 "The cut value of the mva output below which the object is rejected",
55 m_cutValue);
56
57 moduleParamList->addParameter(prefixed(prefix, "identifier"),
58 m_identifier,
59 "Database identifier of the expert of weight file name",
60 m_identifier);
61
62 moduleParamList->addParameter(prefixed(prefix, "DBPayloadName"),
63 m_DBPayloadName,
64 "Name of the DB payload containing weightfile name and the cut value. If a DB payload with both values is available and valid, it will override the values provided by parameters.",
65 m_DBPayloadName);
66 }

◆ getNProcessingSignalListener()

int getNProcessingSignalListener ( )
protectedinherited

Get the number of currently registered listeners.

Definition at line 56 of file CompositeProcessingSignalListener.cc.

61{
63}

◆ getVarSet()

auto getVarSet ( ) const
protectedinherited

Getter for the set of variables.

Definition at line 64 of file FilterOnVarSet.icc.h.

82 {
83 return *m_varSet;
84 }

◆ initialize()

void initialize ( )
overridevirtualinherited

Initialize the expert before event processing.

Reimplemented from CompositeProcessingSignalListener.

Definition at line 64 of file MVAFilter.icc.h.

70 {
71 Super::initialize();
72
73 DBObjPtr<TrackingMVAFilterParameters> mvaParameterPayload(m_DBPayloadName);
74 if (mvaParameterPayload.isValid()) {
75 m_identifier = mvaParameterPayload->getIdentifierName();
76 m_cutValue = mvaParameterPayload->getCutValue();
77 B2DEBUG(20, "MVAFilter: Using DBObject " << m_DBPayloadName << " with weightfile " << m_identifier << " and cut value " <<
78 m_cutValue << ".");
79 } else {
80 B2FATAL("MVAFilter: No valid MVAFilter payload with name " + m_DBPayloadName + " was found.");
81 }
82
83 std::vector<Named<Float_t*>> namedVariables = Super::getVarSet().getNamedVariables();
84
85 m_mvaExpert = std::make_unique<MVAExpert>(m_identifier, std::move(namedVariables));
86 m_mvaExpert->initialize();
87 }

◆ needsTruthInformation()

bool needsTruthInformation ( )
overrideinherited

Checks if any variables need Monte Carlo information.

Definition at line 52 of file FilterOnVarSet.icc.h.

50 {
51 bool result = Super::needsTruthInformation();
52 if (result) return true;
53
54 const std::vector<Named<Float_t*>>& namedVariables = m_varSet->getNamedVariables();
55 for (const Named<Float_t*>& namedVariable : namedVariables) {
56 std::string name = namedVariable.getName();
57 // If the name contains the word truth it is considered to have Monte carlo information.
58 if (name.find("truth") != std::string::npos) {
59 return true;
60 }
61 }
62 return false;
63 }

◆ operator()() [1/5]

Weight operator() ( const Object & obj)
virtualinherited

Function to evaluate the object.

Base implementation accepts all objects.

Parameters
objThe object to be accepted or rejected.
Returns
A finite float value if the object is accepted. NAN if the object is rejected.

Definition at line 59 of file Filter.icc.h.

47 {
48 return 1;
49 }

◆ operator()() [2/5]

Weight operator() ( const Object * obj)
inherited

Function to evaluate the object.

Base implementation accepts all objects, except nullptr.

Parameters
objThe object to be accepted or rejected.
Returns
A finit float value if the object is accepted. NAN if the object is rejected. Nullptr is always rejected.

Definition at line 69 of file Filter.icc.h.

53 {
54 return obj ? operator()(*obj) : NAN;
55 }

◆ operator()() [3/5]

std::vector< float > operator() ( const std::vector< Object * > & objs)
virtualinherited

Function to evaluate a vector of objects Base implementation applies the function to each object.

Can be optimized for MVA filters

Parameters
objsA vector of pointers to objects
Returns
A vector of float or NAN values. See above

Definition at line 77 of file Filter.icc.h.

59 {
60 std::vector<float> out;
61 for (const auto& obj : objs) {
62 out.push_back(operator()(obj));
63 }
64 return out;
65 }

◆ operator()() [4/5]

Weight operator() ( const Object & obj)
overrideinherited

Function to object for its signalness.

Definition at line 71 of file MVAFilter.icc.h.

115 {
116 double prediction = predict(obj);
117 return prediction < m_cutValue ? NAN : prediction;
118 }

◆ operator()() [5/5]

std::vector< float > operator() ( const std::vector< Object * > & objs)
overridevirtualinherited

Evaluate the MVA method over a vector of objects.

Definition at line 80 of file MVAFilter.icc.h.

151 {
152 auto out = predict(objs);
153 for (auto& res : out) {
154 res = res < m_cutValue ? NAN : res;
155 }
156 return out;
157 }

◆ predict() [1/2]

double predict ( const Object & obj)
virtualinherited

Evaluate the mva method.

Definition at line 74 of file MVAFilter.icc.h.

122 {
123 Weight extracted = Super::operator()(obj);
124 if (std::isnan(extracted)) {
125 return NAN;
126 } else {
127 return m_mvaExpert->predict();
128 }
129 }

◆ predict() [2/2]

std::vector< float > predict ( const std::vector< Object * > & objs)
inherited

Evaluate the MVA method over several inputs simultaneously.

Definition at line 77 of file MVAFilter.icc.h.

133 {
134 const int nFeature = m_namedVariables.size();
135 const int nRows = objs.size();
136 auto allFeatures = std::unique_ptr<float[]>(new float[nRows * nFeature]);
137 size_t iRow = 0;
138 for (const auto& obj : objs) {
139 if (Super::getVarSet().extract(obj)) {
140 for (int iFeature = 0; iFeature < nFeature; iFeature += 1) {
141 allFeatures[nFeature * iRow + iFeature] = *m_namedVariables[iFeature];
142 }
143 iRow += 1;
144 }
145 }
146 return m_mvaExpert->predict(allFeatures.get(), nFeature, nRows);
147 }

◆ releaseVarSet()

auto releaseVarSet ( ) &&
inherited

Steal the set of variables form this filter - filter becomes dysfunctional afterwards.

Definition at line 60 of file FilterOnVarSet.icc.h.

76 {
77 return std::move(m_varSet);
78 }

◆ setVarSet()

void setVarSet ( std::unique_ptr< AVarSet > varSet)
protectedinherited

Setter for the set of variables.

Definition at line 67 of file FilterOnVarSet.icc.h.

88 {
89 m_varSet = std::move(varSet);
90 }

◆ terminate()

void terminate ( )
overrideinherited

Receive and dispatch Signal for termination of the event processing.

Definition at line 42 of file CompositeProcessingSignalListener.cc.

48{
50 psl->terminate();
51 }
53}
void terminate() override
Receive and dispatch Signal for termination of the event processing.
virtual void terminate()
Receive Signal for termination of the event processing.

Member Data Documentation

◆ m_cutValue

double m_cutValue
privateinherited

The cut on the MVA output.

Definition at line 86 of file MVAFilter.dcl.h.

◆ m_DBPayloadName

std::string m_DBPayloadName
privateinherited

Name of the DB payload.

Definition at line 89 of file MVAFilter.dcl.h.

◆ m_identifier

std::string m_identifier
privateinherited

Database identifier of the expert or weight file name.

Definition at line 83 of file MVAFilter.dcl.h.

◆ m_initialized

bool m_initialized
privateinherited

Flag to keep track whether initialization happened before.

Definition at line 52 of file ProcessingSignalListener.h.

◆ m_initializedAs

std::string m_initializedAs
privateinherited

Name of the type during initialisation.

Definition at line 58 of file ProcessingSignalListener.h.

◆ m_mvaExpert

std::unique_ptr<MVAExpert> m_mvaExpert
privateinherited

MVA Expert to examine the object.

Definition at line 92 of file MVAFilter.dcl.h.

◆ m_namedVariables

std::vector<Named<Float_t*> > m_namedVariables
privateinherited

named variables, ordered as in the weightFile:

Definition at line 95 of file MVAFilter.dcl.h.

◆ m_subordinaryProcessingSignalListeners

std::vector<ProcessingSignalListener*> m_subordinaryProcessingSignalListeners
privateinherited

References to subordinary signal processing listener contained in this findlet.

Definition at line 60 of file CompositeProcessingSignalListener.h.

◆ m_terminated

bool m_terminated
privateinherited

Flag to keep track whether termination happened before.

Definition at line 55 of file ProcessingSignalListener.h.

◆ m_varSet

std::unique_ptr<AVarSet> m_varSet
privateinherited

Instance of the variable set to be used in the filter.

Definition at line 74 of file FilterOnVarSet.dcl.h.


The documentation for this class was generated from the following files: