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