Belle II Software development
FilterMill.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
9#pragma once
10
11#include <framework/logging/Logger.h>
12#include <string>
13#include <vector>
14#include <utility> // std::pair, std::move
15#include <functional>
16
17namespace Belle2 {
24 template <class PointType>
25 class FilterMill {
26 public:
27
28
30 using TwoHitFunction = typename std::function<double(const PointType&, const PointType&)>;
31
32
34 using ThreeHitFunction = typename std::function<double(const PointType&, const PointType&, const PointType&)>;
35
36
38 using FourHitFunction = typename std::function<double(const PointType&, const PointType&, const PointType&, const PointType&)>;
39
40
41
42 protected:
44 std::vector<std::pair<std::string, TwoHitFunction> > m_2Hitfilters;
45
46
48 std::vector<std::pair<std::string, ThreeHitFunction> > m_3Hitfilters;
49
50
52 std::vector<std::pair<std::string, FourHitFunction> > m_4Hitfilters;
53
54
56 bool m_locked = false;
57
58
60 void checkLocked(const std::string& name) const
61 {
62 if (!m_locked) {
63 B2WARNING("FilterMill-checkLocked: Function " << name <<
64 " was executed although the Mill was not locked yet. This might be unintended...");
65 }
66 }
67
68
69
70 public:
71
73 struct HitPair {
74 const PointType* outer;
75 const PointType* inner;
76 };
77
79 struct HitTriplet {
80 const PointType* outer;
81 const PointType* center;
82 const PointType* inner;
83 };
84
87 const PointType* outer;
88 const PointType* outerCenter;
89 const PointType* innerCenter;
90 const PointType* inner;
91 };
92
95 m_locked(false) {}
96
97
99 void lockMill() { m_locked = true; }
100
101
103 void add2HitFilter(std::pair<const std::string, TwoHitFunction> newFilter)
104 {
105 if (m_locked) {
106 B2ERROR("FilterMill-add2HitFilter: someone tried to add filter " << newFilter.first <<
107 " after lockdown! This is unintended behavior - doing nothing instead...");
108 return;
109 }
110 B2DEBUG(20, "FilterMill::add2HitFilter: filter " << newFilter.first << " added");
111 m_2Hitfilters.push_back(std::move(newFilter));
112 }
113
114
116 void add3HitFilter(std::pair<std::string, ThreeHitFunction> newFilter)
117 {
118 if (m_locked) {
119 B2ERROR("FilterMill-add3HitFilter: someone tried to add filter " << newFilter.first <<
120 " after lockdown! This is unintended behavior - doing nothing instead...");
121 return;
122 }
123 B2DEBUG(20, "FilterMill::add3HitFilter: filter " << newFilter.first << " added");
124 m_3Hitfilters.push_back(std::move(newFilter));
125 }
126
127
129 void add4HitFilter(std::pair<std::string, FourHitFunction> newFilter)
130 {
131 if (m_locked) {
132 B2ERROR("FilterMill-add4HitFilter: someone tried to add filter " << newFilter.first <<
133 " after lockdown! This is unintended behavior - doing nothing instead...");
134 return;
135 }
136 B2DEBUG(20, "FilterMill::add4HitFilter: filter " << newFilter.first << " added");
137 m_4Hitfilters.push_back(std::move(newFilter));
138 }
139
140
142 void grindData2Hit(const HitPair& dataSet,
143 std::vector<std::pair<std::string, double> >& collectedData) const
144 {
145 checkLocked("grindData2Hit");
146 if (m_2Hitfilters.empty()) { B2DEBUG(20, "there are no 2-hit-filters stored, skipping grinding"); return; }
147
148 std::vector<std::pair<std::string, double>> tempData;
149 try {
150 for (const auto& filterPack : m_2Hitfilters) {
151
152 double result = filterPack.second(
153 *(dataSet.outer),
154 *(dataSet.inner));
155 tempData.push_back({filterPack.first, result});
156 }
157 } catch (...) {
158 std::string filterNames;
159 for (const auto& filterPack : m_2Hitfilters) {
160 filterNames += filterPack.first + " ";
161 }
162 B2WARNING("FilterMill:grindData2Hit: an exception was thrown by one of the Filters/SelectionVariables, that indicates strange input data, no data collected! Filters "
163 << filterNames << " were used and one of them did not work.");
164 return;
165 }
166
167 collectedData.insert(collectedData.end(), tempData.begin(), tempData.end());
168
169 B2DEBUG(20, "FilterMill::grindData2Hit: collectedData has now " << collectedData.size() << " entries");
170 }
171
172
174 void grindData3Hit(const HitTriplet& dataSet,
175 std::vector<std::pair<std::string, double> >& collectedData) const
176 {
177 checkLocked("grindData3Hit");
178 if (m_3Hitfilters.empty()) { B2DEBUG(20, "there are no 3-hit-filters stored, skipping grinding"); return; }
179
180 std::vector<std::pair<std::string, double>> tempData;
181 try {
182 for (const auto& filterPack : m_3Hitfilters) {
183
184 double result = filterPack.second(
185 *(dataSet.outer),
186 *(dataSet.center),
187 *(dataSet.inner));
188 tempData.push_back({filterPack.first, result});
189 }
190 } catch (...) {
191 std::string filterNames;
192 for (const auto& filterPack : m_2Hitfilters) {
193 filterNames += filterPack.first + " ";
194 }
195 B2WARNING("FilterMill:grindData3Hit: an exception was thrown by one of the Filters/SelectionVariables, that indicates strange input data, no data collected! Filters "
196 << filterNames << " were used and one of them did not work.");
197 return;
198 }
199 collectedData.insert(collectedData.end(), tempData.begin(), tempData.end());
200
201 B2DEBUG(20, "FilterMill::grindData3Hit: collectedData has now " << collectedData.size() << " entries");
202 }
203
204
206 void grindData4Hit(const HitQuadruplet& dataSet,
207 std::vector<std::pair<std::string, double> >& collectedData) const
208 {
209 checkLocked("grindData4Hit");
210 if (m_4Hitfilters.empty()) { B2DEBUG(20, "there are no 4-hit-filters stored, skipping grinding"); return; }
211
212 std::vector<std::pair<std::string, double>> tempData;
213 try {
214 for (const auto& filterPack : m_4Hitfilters) {
215
216 double result = filterPack.second(
217 *(dataSet.outer),
218 *(dataSet.outerCenter),
219 *(dataSet.innerCenter),
220 *(dataSet.inner));
221 tempData.push_back({filterPack.first, result});
222 }
223 } catch (...) {
224 B2WARNING("FilterMill:grindData4Hit: an exception was thrown by one of the Filters/SelectionVariables, that indicates strange input data, no data collected!");
225 return;
226 }
227 collectedData.insert(collectedData.end(), tempData.begin(), tempData.end());
228
229 B2DEBUG(20, "FilterMill::grindData4Hit: collectedData has now " << collectedData.size() << " entries");
230 }
231
232 };
233
235}
236
237
Small class which stores the filters/selectionVariables to be used for a secMap and has an interface ...
Definition: FilterMill.h:25
void grindData3Hit(const HitTriplet &dataSet, std::vector< std::pair< std::string, double > > &collectedData) const
on given dataSet, apply all filters stored in the mill and store the results in collectedData.
Definition: FilterMill.h:174
void grindData4Hit(const HitQuadruplet &dataSet, std::vector< std::pair< std::string, double > > &collectedData) const
on given dataSet, apply all filters stored in the mill and store the results in collectedData.
Definition: FilterMill.h:206
FilterMill()
Constructor.
Definition: FilterMill.h:94
typename std::function< double(const PointType &, const PointType &, const PointType &, const PointType &)> FourHitFunction
typedef for more readable function-type - to be used for 4-hit-selectionVariables.
Definition: FilterMill.h:38
void lockMill()
to block adding new filters, execute this member.
Definition: FilterMill.h:99
std::vector< std::pair< std::string, TwoHitFunction > > m_2Hitfilters
Contains all 2-hit-Filters and their names to be applied.
Definition: FilterMill.h:44
void add3HitFilter(std::pair< std::string, ThreeHitFunction > newFilter)
add new Filter for 3 Hits .
Definition: FilterMill.h:116
bool m_locked
Simple safeguard for not changing any filters after preparing phase.
Definition: FilterMill.h:56
typename std::function< double(const PointType &, const PointType &)> TwoHitFunction
typedef for more readable function-type - to be used for 2-hit-selectionVariables.
Definition: FilterMill.h:30
std::vector< std::pair< std::string, ThreeHitFunction > > m_3Hitfilters
Contains all 3-hit-Filters and their names to be applied.
Definition: FilterMill.h:48
void add2HitFilter(std::pair< const std::string, TwoHitFunction > newFilter)
add new Filter for 2 Hits .
Definition: FilterMill.h:103
void grindData2Hit(const HitPair &dataSet, std::vector< std::pair< std::string, double > > &collectedData) const
on given dataSet, apply all filters stored in the mill and store the results in collectedData.
Definition: FilterMill.h:142
void checkLocked(const std::string &name) const
to be executed by functions which shall work with unchangeable set of filters.
Definition: FilterMill.h:60
void add4HitFilter(std::pair< std::string, FourHitFunction > newFilter)
add new Filter for 4 Hits .
Definition: FilterMill.h:129
typename std::function< double(const PointType &, const PointType &, const PointType &)> ThreeHitFunction
typedef for more readable function-type - to be used for 3-hit-selectionVariables.
Definition: FilterMill.h:34
std::vector< std::pair< std::string, FourHitFunction > > m_4Hitfilters
Contains all 4-hit-Filters and their names to be applied.
Definition: FilterMill.h:52
Abstract base class for different kinds of events.
small struct containing pointers to two hits.
Definition: FilterMill.h:73
const PointType * outer
outer hit.
Definition: FilterMill.h:74
const PointType * inner
inner hit.
Definition: FilterMill.h:75
small struct containing pointers to four hits.
Definition: FilterMill.h:86
const PointType * innerCenter
innerCenter hit.
Definition: FilterMill.h:89
const PointType * outer
outer hit.
Definition: FilterMill.h:87
const PointType * outerCenter
outerCenter hit.
Definition: FilterMill.h:88
const PointType * inner
inner hit.
Definition: FilterMill.h:90
small struct containing pointers to three hits.
Definition: FilterMill.h:79
const PointType * center
center hit.
Definition: FilterMill.h:81
const PointType * outer
outer hit.
Definition: FilterMill.h:80
const PointType * inner
inner hit.
Definition: FilterMill.h:82