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< AFilter >

Public Types

using Object = typename AVarSet::Object
 Type of the object to be analysed.
 

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 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.
 

Protected Member Functions

AVarSetgetVarSet () const
 Getter for the set of variables.
 
void setVarSet (std::unique_ptr< AVarSet > varSet)
 Setter for the set of variables.
 

Private Types

using Super = MVA< Filter< typename AVarSet::Object > >
 Type of the super class.
 
using AVarSet = BaseVarSet< Object >
 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.
 

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 = BaseVarSet<Object>
privateinherited

Type of the variable set.

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

◆ Object

using Object = typename AVarSet::Object

Type of the object to be analysed.

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

◆ Super

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()

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 }
MVA< Filter< typename AVarSet::Object > > Super
Type of the super class.

Member Function Documentation

◆ beginRun()

void beginRun
overrideinherited

Signal to load new run parameters.

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

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 }
std::unique_ptr< MVAExpert > m_mvaExpert
MVA Expert to examine the object.
Definition: MVAFilter.dcl.h:92
std::vector< Named< Float_t * > > m_namedVariables
named variables, ordered as in the weightFile:
Definition: MVAFilter.dcl.h:95

◆ exposeParameters()

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

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

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 }

◆ getVarSet()

auto getVarSet
protectedinherited

Getter for the set of variables.

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

81 {
82 return *m_varSet;
83 }
std::unique_ptr< AVarSet > m_varSet
Instance of the variable set to be used in the filter.

◆ initialize()

void initialize
overrideinherited

Initialize the expert before event processing.

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 }
std::string m_identifier
Database identifier of the expert or weight file name.
Definition: MVAFilter.dcl.h:83

◆ needsTruthInformation()

bool needsTruthInformation
overrideinherited

Checks if any variables need Monte Carlo information.

Definition at line 49 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/2]

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 }
virtual double predict(const Object &obj)
Evaluate the mva method.

◆ operator()() [2/2]

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 75 of file FilterOnVarSet.icc.h.

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

◆ setVarSet()

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

Setter for the set of variables.

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

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

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_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_varSet

std::unique_ptr<AVarSet> m_varSet
privateinherited

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

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


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