Belle II Software development
MVA< AFilter > Class Template Reference

Filter based on a mva method. More...

#include <MVAFilter.dcl.h>

Inheritance diagram for MVA< AFilter >:
OnVarSet< AFilter >

Public Types

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

Public Member Functions

 MVA (std::unique_ptr< AVarSet > varSet, const std::string &identifier="", double defaultCut=NAN, const std::string &dbObjectName="")
 Constructor of the filter.
 
virtual ~MVA ()
 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 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.
 
virtual std::vector< float > operator() (const std::vector< Object * > &objs) override
 Evaluate the MVA method over a vector of objects.
 
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 disfunctional 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 = OnVarSet< AFilter >
 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 AFilter>
class Belle2::TrackFindingCDC::MVA< AFilter >

Filter based on a mva method.

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

Member Typedef Documentation

◆ AVarSet

using AVarSet = BaseVarSet<Object>
private

Type of the variable set.

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

◆ Object

using Object = typename AFilter::Object

Type of the object to be analysed.

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

◆ Super

using Super = OnVarSet<AFilter>
private

Type of the super class.

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

Constructor & Destructor Documentation

◆ MVA()

MVA ( std::unique_ptr< AVarSet varSet,
const std::string &  identifier = "",
double  defaultCut = NAN,
const std::string &  dbObjectName = "" 
)
explicit

Constructor of the filter.

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

41 : Super(std::move(varSet)), m_identifier(identifier), m_cutValue(defaultCut), m_DBPayloadName(dbObjectName)
42 {
43 }
OnVarSet< AFilter > Super
Type of the super class.
Definition: MVAFilter.dcl.h:40
std::string m_DBPayloadName
Name of the DB payload.
Definition: MVAFilter.dcl.h:89
double m_cutValue
The cut on the MVA output.
Definition: MVAFilter.dcl.h:86
std::string m_identifier
Database identifier of the expert or weight file name.
Definition: MVAFilter.dcl.h:83

Member Function Documentation

◆ beginRun()

void beginRun
override

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 90 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 }
virtual std::vector< Named< Float_t * > > getNamedVariables(const std::string &prefix)
Getter for the named references to the individual variables Base implementaton returns empty vector.
Definition: BaseVarSet.h:67
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
AVarSet & getVarSet() const
Getter for the set of variables.

◆ exposeParameters()

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

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

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

50 {
51 Super::exposeParameters(moduleParamList, prefix);
52 moduleParamList->addParameter(prefixed(prefix, "cut"),
54 "The cut value of the mva output below which the object is rejected",
56
57 moduleParamList->addParameter(prefixed(prefix, "identifier"),
59 "Database identfier of the expert of weight file name",
61
62 moduleParamList->addParameter(prefixed(prefix, "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.",
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
override

Initialize the expert before event processing.

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

70 {
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 }
void initialize() override
No reassignment of variable set possible for now.

◆ 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)
override

Function to object for its signalness.

Definition at line 114 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)
overridevirtual

Evaluate the MVA method over a vector of objects.

Definition at line 150 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)
virtual

Evaluate the mva method.

Definition at line 121 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 }
Weight operator()(const Object &obj) override
Function extracting the variables of the object into the variable set.

◆ predict() [2/2]

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

Evaluate the MVA method over several inputs simultaneously.

Definition at line 132 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 disfunctional 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
private

The cut on the MVA output.

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

◆ m_DBPayloadName

std::string m_DBPayloadName = ""
private

Name of the DB payload.

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

◆ m_identifier

std::string m_identifier = ""
private

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
private

MVA Expert to examine the object.

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

◆ m_namedVariables

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

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: