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