Belle II Software  release-05-01-25
filters.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2014 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Eugenio Paoloni (eugenio.paoloni@pi.infn.it *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #include <gtest/gtest.h>
12 
13 
14 #include <tracking/trackFindingVXD/filterMap/filterFramework/Shortcuts.h>
15 #include <tuple>
16 #include <iostream>
17 #include <math.h>
18 
19 using namespace std;
20 
21 using namespace Belle2;
22 
23 namespace VXDTFfilterTest {
24 
26  class spacePoint: public tuple<float, float, float> {
27  public:
29  spacePoint(float x, float y, float z): tuple<float, float, float>(x, y, z)
30  {};
31  private:
34  spacePoint(const spacePoint&) = delete;
35  };
36 
37 
39  class SquaredDistance3D : public SelectionVariable< spacePoint , 2, float > {
40  public:
42  static const std::string name(void) {return "SquaredDistance3D"; };
43 
45  static float value(const spacePoint& p1, const spacePoint& p2)
46  {
47  return
48  pow(get<0>(p1) - get<0>(p2) , 2) +
49  pow(get<1>(p1) - get<1>(p2) , 2) +
50  pow(get<2>(p1) - get<2>(p2) , 2) ;
51  }
52  };
53 
54 
56  class SquaredDistance2Dxy : public SelectionVariable< spacePoint , 2, float > {
57  public:
59  static const std::string name(void) {return "SquaredDistance2Dxy"; };
60 
62  static float value(const spacePoint& p1, const spacePoint& p2)
63  {
64  return
65  pow(get<0>(p1) - get<0>(p2) , 2) +
66  pow(get<1>(p1) - get<1>(p2) , 2) ;
67  }
68  };
69 
70 
72  class SquaredDistance1Dx : public SelectionVariable< spacePoint , 2, float > {
73  public:
75  static const std::string name(void) {return "SquaredDistance1Dx"; };
76 
78  static float value(const spacePoint& p1, const spacePoint& p2)
79  {
80  return
81  pow(get<0>(p1) - get<0>(p2) , 2);
82  }
83  };
84 
85 
87  class BooleanVariable : public SelectionVariable< spacePoint , 2, bool > {
88  public:
90  static const std::string name(void) {return "BooleanVariable"; };
91 
93  static float value(const spacePoint& p1, const spacePoint& p2)
94  {
95  return
96  get<0>(p1) - get<0>(p2) == 0.;
97  }
98  };
99 
100 
102  template < class T> class counter {
103  public:
104  static int N;
105  counter() {};
106  ~counter() {};
107  };
108 
109 
111  template<>
112  int counter< SquaredDistance3D >::N(0);
113 
114 
116  template<>
117  int counter< SquaredDistance2Dxy >::N(0);
118 
119 
121  template<>
122  int counter< SquaredDistance1Dx >::N(0);
123 
124 
126  class Observer : public VoidObserver {
127  public:
129  template<class Var, typename ... otherTypes>
130  static void notify(const Var&,
131  const otherTypes& ...)
132  {
133  counter<Var>::N ++ ;
134  }
135  };
136 
137 
138 
140  class FilterTest : public ::testing::Test {
141  protected:
142  };
143 
144 
147  {
148 
149  Range<double, double> range(0. , 1.);
150  EXPECT_TRUE(range.contains(0.5));
151  EXPECT_FALSE(range.contains(-1.));
152  EXPECT_FALSE(range.contains(0.));
153  EXPECT_FALSE(range.contains(1.));
154  EXPECT_FALSE(range.contains(2.));
155  EXPECT_EQ(0. , range.getInf());
156  EXPECT_EQ(1. , range.getSup());
157  }
158 
160  TEST_F(FilterTest, ClosedRange)
161  {
162 
163  ClosedRange<double, double> range(0. , 1.);
164  EXPECT_TRUE(range.contains(0.5));
165  EXPECT_FALSE(range.contains(-1.));
166  EXPECT_TRUE(range.contains(0.));
167  EXPECT_TRUE(range.contains(1.));
168  EXPECT_FALSE(range.contains(2.));
169  EXPECT_EQ(0. , range.getInf());
170  EXPECT_EQ(1. , range.getSup());
171  }
172 
173 
175  TEST_F(FilterTest, UpperBoundedSet)
176  {
177 
178  UpperBoundedSet<double> upperBoundedSet(0.);
179  EXPECT_TRUE(upperBoundedSet.contains(-1.));
180  EXPECT_FALSE(upperBoundedSet.contains(0.));
181  EXPECT_FALSE(upperBoundedSet.contains(1.));
182  EXPECT_EQ(0. , upperBoundedSet.getSup());
183  }
184 
186  TEST_F(FilterTest, ClosedUpperBoundedSet)
187  {
188 
189  ClosedUpperBoundedSet<double> upperBoundedSet(0.);
190  EXPECT_TRUE(upperBoundedSet.contains(-1.));
191  EXPECT_TRUE(upperBoundedSet.contains(0.));
192  EXPECT_FALSE(upperBoundedSet.contains(1.));
193  EXPECT_EQ(0. , upperBoundedSet.getSup());
194  }
195 
196 
198  TEST_F(FilterTest, LowerBoundedSet)
199  {
200 
201  LowerBoundedSet<double> lowerBoundedSet(0.);
202  EXPECT_TRUE(lowerBoundedSet.contains(1.));
203  EXPECT_FALSE(lowerBoundedSet.contains(0.));
204  EXPECT_FALSE(lowerBoundedSet.contains(-1.));
205  EXPECT_EQ(0. , lowerBoundedSet.getInf());
206  }
207 
209  TEST_F(FilterTest, ClosedLowerBoundedSet)
210  {
211 
212  ClosedLowerBoundedSet<double> lowerBoundedSet(0.);
213  EXPECT_TRUE(lowerBoundedSet.contains(1.));
214  EXPECT_TRUE(lowerBoundedSet.contains(0.));
215  EXPECT_FALSE(lowerBoundedSet.contains(-1.));
216  EXPECT_EQ(0. , lowerBoundedSet.getInf());
217  }
218 
219 
221  TEST_F(FilterTest, SelectionVariableName)
222  {
223 
224  EXPECT_EQ("SquaredDistance3D" , SquaredDistance3D().name());
225  }
226 
227 
229  TEST_F(FilterTest, BasicFilter)
230  {
231  // Very verbose declaration, see below for convenient shortcuts
233 
234  spacePoint x1(0.0f , 0.0f, 0.0f);
235  spacePoint x2(0.5f , 0.0f, 0.0f);
236  spacePoint x3(2.0f , 0.0f, 0.0f);
237 
238  EXPECT_TRUE(filter.accept(x1, x2));
239  EXPECT_FALSE(filter.accept(x1, x3));
240 
241  }
242 
243 
245  TEST_F(FilterTest, ObservedFilter)
246  {
247  // Very verbose declaration, see below for convenient shortcuts
249 
251  spacePoint x1(0.0f , 0.0f, 0.0f);
252  spacePoint x2(0.5f , 0.0f, 0.0f);
253  spacePoint x3(2.0f , 0.0f, 0.0f);
254  counter< SquaredDistance3D >::N = 0;
255 
256  EXPECT_TRUE(filter.accept(x1, x2));
257  EXPECT_FALSE(filter.accept(x1, x3));
258  EXPECT_EQ(2 , counter< SquaredDistance3D >::N);
259  }
260 
262  TEST_F(FilterTest, SwitchingObservers)
263  {
264  //build an dummy filter which is unobserved (VoidObserver)
265  auto dummyFilter = (
266  // cppcheck-suppress compareBoolExpressionWithInt
267  (-10. <= SquaredDistance3D() <= 10.) &&
268  // cppcheck-suppress compareBoolExpressionWithInt
269  ((-100. <= SquaredDistance2Dxy() <= -10.) || // put 2nd pair of parentheses to silence warning
270  // cppcheck-suppress compareBoolExpressionWithInt
271  (-10. <= SquaredDistance1Dx() <= 10.)) &&
272  // cppcheck-suppress compareBoolExpressionWithInt
273  !(-10. <= SquaredDistance1Dx() <= -10.)
274  );
275 
276  // values are chosen in that way that all sub-filters of dummyFilter have to be called (see comment below)
277  // and so that dummyFilter is always true
278  spacePoint x1(0.0f , 0.0f, 0.0f);
279  spacePoint x2(0.5f , 0.0f, 0.0f);
280  spacePoint x3(2.0f , 0.0f, 0.0f);
281 
282  counter< SquaredDistance3D >::N = 0;
283  counter< SquaredDistance1Dx >::N = 0;
284  counter< SquaredDistance2Dxy >::N = 0;
285 
286  dummyFilter.accept(x1, x2);
287  dummyFilter.accept(x1, x3);
288 
289  // useless test as it tests if realy unobserved
290  EXPECT_EQ(0 , counter< SquaredDistance3D >::N);
291  EXPECT_EQ(0 , counter< SquaredDistance2Dxy >::N);
292  EXPECT_EQ(0 , counter< SquaredDistance1Dx >::N);
293 
294  // Now switch observer, this is done recursively for each of the underlying filters
295  // One cannot switch the observer of an existing filter (as it would mean to switch type) so one has to create a new one!
296  // Note that if one filter is evaluated as false the other filter are not evaluated anymore and thus not observed!
297  auto observedDummyFilter = dummyFilter.observe(Observer());
298  observedDummyFilter.accept(x1, x2);
299  observedDummyFilter.accept(x1, x3);
300 
301  EXPECT_EQ(2 , counter< SquaredDistance3D >::N);
302  EXPECT_EQ(2 , counter< SquaredDistance2Dxy >::N);
303  //Note SquaredDistance1D is used twice in the filter so it is evaluated twice!
304  EXPECT_EQ(4 , counter< SquaredDistance1Dx >::N);
305  }
306 
307 
309  TEST_F(FilterTest, BypassableFilter)
310  {
311  bool bypassControl(false);
312  // Very verbose declaration, see below for convenient shortcuts
314  auto filter = nonBypassableFilter.bypass(bypassControl);
315  spacePoint x1(0.0f , 0.0f, 0.0f);
316  spacePoint x2(2.0f , 0.0f, 0.0f);
317  counter< SquaredDistance3D >::N = 0;
318 
319  EXPECT_FALSE(filter.accept(x1, x2));
320  EXPECT_EQ(1 , counter< SquaredDistance3D >::N);
321 
322  bypassControl = true;
323  EXPECT_TRUE(filter.accept(x1, x2));
324  EXPECT_EQ(2 , counter< SquaredDistance3D >::N);
325 
326  }
327 
328 
330  TEST_F(FilterTest, Shortcuts)
331  {
332 
333  spacePoint x1(0.0f , 0.0f, 0.0f);
334  spacePoint x2(0.5f , 0.0f, 0.0f);
335  spacePoint x3(2.0f , 0.0f, 0.0f);
336  spacePoint x4(1.0f , 0.0f, 0.0f);
337 
338  auto filterSup = (SquaredDistance3D() < 1.) ;
339  EXPECT_TRUE(filterSup.accept(x1, x2));
340  EXPECT_FALSE(filterSup.accept(x1, x4));
341  EXPECT_FALSE(filterSup.accept(x1, x3));
342 
343  auto filterMax = (SquaredDistance3D() <= 1.) ;
344  EXPECT_TRUE(filterMax.accept(x1, x2));
345  EXPECT_TRUE(filterMax.accept(x1, x4));
346  EXPECT_FALSE(filterMax.accept(x1, x3));
347 
348 
349  auto filterSup2 = (1 > SquaredDistance3D()) ;
350  EXPECT_TRUE(filterSup2.accept(x1, x2));
351  EXPECT_FALSE(filterSup2.accept(x1, x3));
352  EXPECT_FALSE(filterSup2.accept(x1, x4));
353 
354  auto filterMax2 = (1 >= SquaredDistance3D()) ;
355  EXPECT_TRUE(filterMax2.accept(x1, x2));
356  EXPECT_FALSE(filterMax2.accept(x1, x3));
357  EXPECT_TRUE(filterMax2.accept(x1, x4));
358 
359  auto filterInf = (SquaredDistance3D() > 1.) ;
360  EXPECT_TRUE(filterInf.accept(x1, x3));
361  EXPECT_FALSE(filterInf.accept(x1, x2));
362  EXPECT_FALSE(filterInf.accept(x1, x4));
363 
364  auto filterMin = (SquaredDistance3D() >= 1.) ;
365  EXPECT_TRUE(filterMin.accept(x1, x3));
366  EXPECT_FALSE(filterMin.accept(x1, x2));
367  EXPECT_TRUE(filterMin.accept(x1, x4));
368 
369  auto filterInf2 = (1 < SquaredDistance3D()) ;
370  EXPECT_TRUE(filterInf2.accept(x1, x3));
371  EXPECT_FALSE(filterInf2.accept(x1, x2));
372  EXPECT_FALSE(filterInf2.accept(x1, x4));
373 
374  auto filterMin2 = (1 <= SquaredDistance3D()) ;
375  EXPECT_TRUE(filterMin2.accept(x1, x3));
376  EXPECT_FALSE(filterMin2.accept(x1, x2));
377  EXPECT_TRUE(filterMin2.accept(x1, x4));
378 
379  auto filterRange = (0. < SquaredDistance3D() < 1);
380  EXPECT_FALSE(filterRange.accept(x1, x1));
381  EXPECT_TRUE(filterRange.accept(x1, x2));
382  EXPECT_FALSE(filterRange.accept(x1, x3));
383  EXPECT_FALSE(filterRange.accept(x1, x4));
384 
385  // cppcheck-suppress compareBoolExpressionWithInt
386  auto filterClosedRange = (0. <= SquaredDistance3D() <= 1);
387  EXPECT_TRUE(filterClosedRange.accept(x1, x1));
388  EXPECT_TRUE(filterClosedRange.accept(x1, x2));
389  EXPECT_FALSE(filterClosedRange.accept(x1, x3));
390  EXPECT_TRUE(filterClosedRange.accept(x1, x4));
391 
392  }
393 
394 
396  TEST_F(FilterTest, BooleanOperations)
397  {
398 
399 
400  spacePoint x1(0.0f , 0.0f, 0.0f);
401  spacePoint x2(1.0f , 0.0f, 0.0f);
402  spacePoint x3(2.0f , 0.0f, 0.0f);
403 
404  auto filter = !(SquaredDistance3D() > 1.);
405  EXPECT_TRUE(filter.accept(x1, x2));
406  EXPECT_TRUE(filter.accept(x1, x1));
407  EXPECT_FALSE(filter.accept(x1, x3));
408 
409  auto filter2 =
410  !(SquaredDistance3D() > 1.) &&
411  !(SquaredDistance3D() < 1);
412  // i.e. SquaredDistance3D == 1
413  EXPECT_TRUE(filter2.accept(x1, x2));
414  EXPECT_FALSE(filter2.accept(x1, x1));
415  EXPECT_FALSE(filter2.accept(x1, x3));
416 
417 
418  auto filter3 =
419  (SquaredDistance3D() > 1.) ||
420  (SquaredDistance3D() < 1);
421  // i.e. SquaredDistance3D != 1
422  EXPECT_FALSE(filter3.accept(x1, x2));
423  EXPECT_TRUE(filter3.accept(x1, x1));
424  EXPECT_TRUE(filter3.accept(x1, x3));
425 
426 
427  }
428 
429 
431  TEST_F(FilterTest, ShortCircuitsEvaluation)
432  {
433  auto filter(
434  (SquaredDistance2Dxy() < 1).observeLeaf(Observer()) &&
435  (SquaredDistance3D() < 1).observeLeaf(Observer())
436  );
437 
438  spacePoint x1(0.0f , 0.0f, 0.0f);
439  spacePoint x2(1.0f , 0.0f, 0.0f);
440  spacePoint x3(2.0f , 0.0f, 0.0f);
441 
442  counter< SquaredDistance3D >::N = 0;
443  counter< SquaredDistance2Dxy >::N = 0;
444 
445  EXPECT_FALSE(filter.accept(x1, x3));
446  // since the pair x1, x3 does not satisfy the SquaredDistance2Dxy
447  // requirement, we do expect SquaredDistance2Dxy evaluated once:
448  EXPECT_EQ(1 , counter< SquaredDistance2Dxy >::N);
449  // and SquaredDistance3D not evaluated at all
450  EXPECT_EQ(0 , counter< SquaredDistance3D >::N);
451 
452  EXPECT_TRUE(filter.accept(x1, x1));
453  // in this case Distance2Dxy is satisfied
454  EXPECT_EQ(2 , counter< SquaredDistance2Dxy >::N);
455  // and Distance3D is evaluated
456  EXPECT_EQ(1 , counter< SquaredDistance3D >::N);
457 
458  }
459 
460 
462  TEST_F(FilterTest, BooleanVariableShortcuts)
463  {
464  auto filter1(BooleanVariable() == true);
465  auto filter2(false == BooleanVariable());
466  spacePoint x1(0.0f , 0.0f, 0.0f);
467  spacePoint x2(1.0f , 0.0f, 0.0f);
468 
469  EXPECT_TRUE(filter1.accept(x1, x1));
470  EXPECT_FALSE(filter1.accept(x1, x2));
471 
472 
473  EXPECT_FALSE(filter2.accept(x1, x1));
474  EXPECT_TRUE(filter2.accept(x1, x2));
475 
476 
477 
478  }
479 
480 }
VXDTFfilterTest::counter::~counter
~counter()
constructor.
Definition: filters.cc:106
VXDTFfilterTest::FilterTest
Test class for Filter object.
Definition: filters.cc:140
Belle2::Observer
Observer base class which can be used to evaluate the VXDTF2's Filters.
Definition: Observer.h:29
VXDTFfilterTest::counter::N
static int N
counter.
Definition: filters.cc:104
Belle2::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:43
VXDTFfilterTest::SquaredDistance1Dx::name
static const std::string name(void)
return name of the class
Definition: filters.cc:75
Belle2::ClosedRange
Represents a closed set of arithmetic types.
Definition: ClosedRange.h:42
Belle2::ClosedUpperBoundedSet
Represents an upper bounded set of arithmetic types.
Definition: ClosedUpperBoundedSet.h:40
VXDTFfilterTest::SquaredDistance1Dx
a small filter illustrating the behavior of a distance1D-filter in X
Definition: filters.cc:72
Belle2::UpperBoundedSet
Represents an upper bounded set of arithmetic types.
Definition: UpperBoundedSet.h:39
VXDTFfilterTest::SquaredDistance2Dxy::name
static const std::string name(void)
return name of the class
Definition: filters.cc:59
VXDTFfilterTest::Observer
this observer does simply count the number of times, the attached Filter was used
Definition: filters.cc:126
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::Range
Represents a range of arithmetic types.
Definition: Range.h:39
VXDTFfilterTest::SquaredDistance3D::value
static float value(const spacePoint &p1, const spacePoint &p2)
value function does the actual calculation of this class.
Definition: filters.cc:45
VXDTFfilterTest::BooleanVariable::name
static const std::string name(void)
return name of the class
Definition: filters.cc:90
VXDTFfilterTest::SquaredDistance1Dx::value
static float value(const spacePoint &p1, const spacePoint &p2)
value function does the actual calculation of this class.
Definition: filters.cc:78
VXDTFfilterTest::Observer::notify
static void notify(const Var &, const otherTypes &...)
notify function is called by the filter, this one increases the counter.
Definition: filters.cc:130
Belle2::TEST_F
TEST_F(GlobalLabelTest, LargeNumberOfTimeDependentParameters)
Test large number of time-dep params for registration and retrieval.
Definition: globalLabel.cc:65
Belle2::SelectionVariable
Base class of the selection variable objects used for pair filtering.
Definition: SelectionVariable.h:54
VXDTFfilterTest::SquaredDistance2Dxy::value
static float value(const spacePoint &p1, const spacePoint &p2)
value function does the actual calculation of this class.
Definition: filters.cc:62
VXDTFfilterTest::counter
small class counting usage.
Definition: filters.cc:102
Belle2::ClosedLowerBoundedSet
Represents a closed lower bounded set of arithmetic types.
Definition: ClosedLowerBoundedSet.h:40
VXDTFfilterTest::SquaredDistance2Dxy
a small filter illustrating the behavior of a distance2D-filter in XY
Definition: filters.cc:56
VXDTFfilterTest::BooleanVariable
a small filter illustrating the behavior of a filter which is compatible with boolean comparisons
Definition: filters.cc:87
Belle2::LowerBoundedSet
Represents a lower bounded set of arithmetic types.
Definition: LowerBoundedSet.h:40
VXDTFfilterTest::SquaredDistance3D
a small filter illustrating the behavior of a distance3D-filter
Definition: filters.cc:39
VXDTFfilterTest::SquaredDistance3D::name
static const std::string name(void)
return name of the class
Definition: filters.cc:42
Belle2::Filter
This class is used to select pairs, triplets...
Definition: Filter.h:44
VXDTFfilterTest::BooleanVariable::value
static float value(const spacePoint &p1, const spacePoint &p2)
value function does the actual calculation of this class.
Definition: filters.cc:93
VXDTFfilterTest::spacePoint
just a small proto-container storing coordinates
Definition: filters.cc:26
Belle2::VoidObserver
The most CPU efficient Observer for the VXDTF filter tools (even if useless).
Definition: VoidObserver.h:40
VXDTFfilterTest::spacePoint::spacePoint
spacePoint(float x, float y, float z)
Constructor accepting coordinates.
Definition: filters.cc:29