Belle II Software development
FilterFactory.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/FilterFactory.dcl.h>
11
12#include <tracking/trackFindingCDC/filters/base/AndFilter.icc.h>
13#include <tracking/trackFindingCDC/filters/base/NotFilter.icc.h>
14
15#include <tracking/trackFindingCDC/utilities/StringManipulation.h>
16
17#include <framework/core/ModuleParamList.h>
18
19#include <framework/logging/Logger.h>
20
21#include <map>
22#include <vector>
23#include <string>
24#include <sstream>
25#include <memory>
26
27namespace Belle2 {
32 namespace TrackFindingCDC {
34 template <class AFilter>
35 FilterFactory<AFilter>::FilterFactory(const std::string& defaultFilterName)
36 : m_defaultFilterName(defaultFilterName)
37 {
38 }
39
40 template <class AFilter>
42
43 template <class AFilter>
44 std::unique_ptr<AFilter> FilterFactory<AFilter>::create(const std::string& filterName) const
45 {
46 // Check whether false positive or false negative filter are requested
47 while (filterName == "false_positive" or filterName == "false_negative") {
48 std::string truthFilterName = "truth";
49 std::unique_ptr<AFilter> truthFilter = this->create(truthFilterName);
50 std::unique_ptr<AFilter> defaultFilter = this->create(m_defaultFilterName);
51
52 if (not truthFilter or not defaultFilter) break;
54 if (filterName == "false_positive") {
55 std::unique_ptr<AFilter> notTruthFilter =
56 std::make_unique<NotFilter<AFilter>>(std::move(truthFilter));
57 return std::make_unique<AndFilter<AFilter>>(std::move(notTruthFilter),
58 std::move(defaultFilter));
59 }
60
61 if (filterName == "false_negative") {
62 std::unique_ptr<AFilter> notDefaultFilter =
63 std::make_unique<NotFilter<AFilter>>(std::move(defaultFilter));
64 return std::make_unique<AndFilter<AFilter>>(std::move(notDefaultFilter),
65 std::move(truthFilter));
66 }
67 break;
68 }
69
70 // Filter not valid
71 B2ERROR("Could not create filter with name " << filterName);
72 std::ostringstream message;
73 message << "Known filter names are: ";
74 std::vector<std::string> quotedFilterNames;
75 for (const auto& filterNameAndDescription : getValidFilterNamesAndDescriptions()) {
76 const std::string& validFilterName = filterNameAndDescription.first;
77 quotedFilterNames.push_back(quoted(validFilterName));
78 }
79 message << join(", ", quotedFilterNames);
80 message << ".";
81 B2ERROR(message.str());
82 return std::unique_ptr<AFilter>(nullptr);
83 }
84
85 template<class AFilter>
87 {
88 // Compose description for the filter names
89 std::ostringstream oss;
90 oss << this->getFilterPurpose();
91 oss << "Allowed values are: ";
92 std::vector<std::string> filterNameAndDescriptions;
93 for (const auto& filterNameAndDescription : this->getValidFilterNamesAndDescriptions()) {
94 const std::string& filterName = filterNameAndDescription.first;
95 const std::string& filterDescription = filterNameAndDescription.second;
96 filterNameAndDescriptions.push_back(quoted(filterName) + " " + bracketed(filterDescription));
97 }
98 oss << join(", ", filterNameAndDescriptions);
99 return oss.str();
100 }
101
102 template<class AFilter>
104 {
105 // Compose description for the filter parameters
106 std::ostringstream oss;
107 oss << "Key -- value pairs depending on the filter." << std::endl;
108 for (const auto& filterNameAndDescription : this->getValidFilterNamesAndDescriptions()) {
109
110 const std::string& filterName = filterNameAndDescription.first;
111 // const std::string& filterDescription = filterNameAndDescription.second;
112
113 std::unique_ptr<AFilter> filter = this->create(filterName);
114 if (not filter) {
115 B2WARNING("Could not create a filter for name " << filterName);
116 continue;
117 }
118
119 ModuleParamList moduleParamList;
120 const std::string prefix = "";
121 filter->exposeParameters(&moduleParamList, prefix);
122
123 std::map<std::string, std::string> filterParameters;
124 for (auto && name : moduleParamList.getParameterNames()) {
125 filterParameters[name] = moduleParamList.getParameterDescription(name);
126 }
127
128 oss << quoted(filterName) << " :\n";
129 if (filterParameters.empty()) {
130 oss << "(no parameters)";
131 } else {
132 std::vector<std::string> parameterDescriptions;
133 for (const auto& parameterNameAndDescription : filterParameters) {
134 const std::string& parameterName = parameterNameAndDescription.first;
135 const std::string& parameterDescription = parameterNameAndDescription.second;
136 parameterDescriptions.push_back(parameterName + " -- " + parameterDescription);
137 }
138 oss << join(",\n", parameterDescriptions);
139 }
140 oss << ";\n";
141 }
142 return oss.str();
143 }
144
145 template<class AFilter>
147 {
148 return m_defaultFilterName;
149 }
150 }
152}
The Module parameter list class.
std::string getParameterDescription(const std::string &name) const
Returns the description of a parameter given by its name.
std::vector< std::string > getParameterNames() const
Returns the names of all parameters in this parameter list.
virtual std::string createFiltersParametersDescription() const
Create a string with a description mentioning the parameters of the chooseable filter.
virtual std::unique_ptr< AFilter > create(const std::string &filterName) const
Create a filter with the given name, does not set filter specific parameters.
virtual ~FilterFactory()
Make destructor of interface class virtual.
FilterFactory(const std::string &defaultFilterName="")
Fill the default filter name values.
const std::string & getDefaultFilterName() const
Legacy - Return the default filter suggested by the factory.
virtual std::string createFiltersNameDescription() const
Create a string with a description mentioning the names of the chooseable filter.
std::map< ExpRun, std::pair< double, double > > filter(const std::map< ExpRun, std::pair< double, double > > &runs, double cut, std::map< ExpRun, std::pair< double, double > > &runsRemoved)
filter events to remove runs shorter than cut, it stores removed runs in runsRemoved
Definition: Splitter.cc:38
Abstract base class for different kinds of events.