Belle II Software development
FilterVarSet.icc.h
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8#pragma once
9
10#include <tracking/trackFindingCDC/filters/base/FilterVarSet.dcl.h>
11
12#include <framework/core/ModuleParamList.templateDetails.h>
13#include <framework/core/ModuleParam.h>
14
15#include <RtypesCore.h>
16
17#include <algorithm>
18#include <string>
19#include <memory>
20#include <cmath>
21#include <cstddef>
22
23namespace Belle2 {
28 namespace TrackFindingCDC {
29
30 constexpr
31 static char const* const filterVarNames[] = {
32 "accept",
33 "weight",
34 "positive",
35 };
36
37 template<class AFilter>
38 const std::size_t FilterVarNames<AFilter>::nVars = size(filterVarNames);
39
40 template<class AFilter>
41 constexpr char const* FilterVarNames<AFilter>::getName(int iName)
42 {
43 return filterVarNames[iName];
44 }
45
46 template <class AFilter>
47 FilterVarSet<AFilter>::FilterVarSet(const std::string& filterName)
48 : Super()
49 , m_filterName(filterName)
50 , m_filterNamePrefix(filterName + '_')
51 , m_ptrFilter(std::make_unique<Filter>())
52 {
53 }
54
55 template <class AFilter>
56 FilterVarSet<AFilter>::FilterVarSet(const std::string& filterName,
57 std::unique_ptr<Filter> ptrFilter)
58 : Super()
59 , m_filterName(filterName)
60 , m_filterNamePrefix(filterName + '_')
61 , m_ptrFilter(std::move(ptrFilter))
62 {
63 }
64
65 template <class AFilter>
67 {
68 bool extracted = Super::extract(obj);
69 if (m_ptrFilter and obj) {
70 Weight weight = (*m_ptrFilter)(*obj);
71 this->template var<named("weight")>() = weight;
72 this->template var<named("accept")>() = not std::isnan(weight) and not(weight < m_cut);
73 this->template var<named("positive")>() = weight > 0 and not(weight < m_cut);
74
75 // Forward the nested result.
76 return extracted;
77 } else {
78 return false;
79 }
80 }
81
82 template <class AFilter>
84 {
85 ModuleParamList moduleParamList;
86 const std::string prefix = "";
87 m_ptrFilter->exposeParameters(&moduleParamList, prefix);
88
89 // try to find the MVAFilter cut parameter and reset it such that we can set it
90 std::vector<std::string> paramNames = moduleParamList.getParameterNames();
91 if (std::count(paramNames.begin(), paramNames.end(), "cut")) {
92 ModuleParam<double>& cutParam = moduleParamList.getParameter<double>("cut");
93 m_cut = cutParam.getValue();
94 cutParam.setDefaultValue(NAN);
95 }
96
97 this->addProcessingSignalListener(m_ptrFilter.get());
98 Super::initialize();
99 }
100
101 template <class AFilter>
102 std::vector<Named<Float_t*>> FilterVarSet<AFilter>::getNamedVariables(const std::string& prefix)
103 {
104 return Super::getNamedVariables(prefix + m_filterNamePrefix);
105 }
106
107 template <class AFilter>
108 MayBePtr<Float_t> FilterVarSet<AFilter>::find(const std::string& varName)
109 {
110 if (varName.find(m_filterNamePrefix) == 0) {
111 std::string varNameWithoutPrefix = varName.substr(m_filterNamePrefix.size());
112 MayBePtr<Float_t> found = Super::find(varNameWithoutPrefix);
113 if (found) return found;
114 }
115 return nullptr;
116 }
117 }
119}
The Module parameter list class.
std::vector< std::string > getParameterNames() const
Returns the names of all parameters in this parameter list.
A single parameter of the module.
Definition: ModuleParam.h:34
std::vector< Named< Float_t * > > getNamedVariables()
Getter for the named references to the individual variables.
Definition: BaseVarSet.h:73
FilterVarSet(const std::string &filterName="")
Construct the varset making an instance of the template filter.
void initialize() final
Initialize the filter before event processing.
bool extract(const Object *obj) final
Generate filter weight variable from the object.
typename Filter::Object Object
Type of the object from which the filter judgement should be extracted.
AFilter Filter
Type of the filter.
MayBePtr< Float_t > find(const std::string &varName) override
Pointer to the variable with the given name.
ModuleParam< T > & getParameter(const std::string &name) const
Returns a reference to a parameter.
T & getValue()
Returns the value of the parameter.
void setDefaultValue(const T &defaultValue)
Sets the default value of a parameter.
Abstract base class for different kinds of events.
STL namespace.
static const size_t nVars
Number of variables to be generated.
static constexpr char const * getName(int iName)
Getter for the name at the given index.