Belle II Software  release-05-01-25
softwareTriggerCut.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2015 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Nils Braun *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #include <hlt/softwaretrigger/core/SoftwareTriggerCut.h>
12 #include <gtest/gtest.h>
13 #include <stdint.h>
14 
15 using namespace std;
16 
17 namespace Belle2 {
22  namespace SoftwareTrigger {
23 
25  class SoftwareTriggerCutTest : public ::testing::Test {};
26 
29  {
30  SoftwareTriggerObject object;
31 
32  auto cut = SoftwareTriggerCut::compile("1 == 1", 1, false);
33  EXPECT_EQ(SoftwareTriggerCutResult::c_accept, cut->checkPreScaled(object));
34 
35  cut = SoftwareTriggerCut::compile("1 == 1", 1, true);
36  EXPECT_EQ(SoftwareTriggerCutResult::c_reject, cut->checkPreScaled(object));
37 
38  cut = SoftwareTriggerCut::compile("-1 == 1", 1, false);
39  EXPECT_EQ(SoftwareTriggerCutResult::c_noResult, cut->checkPreScaled(object));
40 
41  cut = SoftwareTriggerCut::compile("-1 == 1", 1, true);
42  EXPECT_EQ(SoftwareTriggerCutResult::c_noResult, cut->checkPreScaled(object));
43  }
44 
46  TEST_F(SoftwareTriggerCutTest, prescalingRandom)
47  {
48 
49  SoftwareTriggerObject object;
50 
51  // As a random seed is implemented in the software trigger cut, we test it multiple times.
52  auto cut = SoftwareTriggerCut::compile("1 == 1", 0, false);
53  for (unsigned int i = 0; i < 1e3; i++) {
54  EXPECT_EQ(SoftwareTriggerCutResult::c_noResult, cut->checkPreScaled(object));
55  }
56 
57  // For reject cuts, the prescale is not allowed to have any influence
58  cut = SoftwareTriggerCut::compile("1 == 1", 0, true);
59  for (unsigned int i = 0; i < 1e3; i++) {
60  EXPECT_EQ(SoftwareTriggerCutResult::c_reject, cut->checkPreScaled(object));
61  }
62 
63  cut = SoftwareTriggerCut::compile("1 == 1", 1, true);
64  for (unsigned int i = 0; i < 1e3; i++) {
65  EXPECT_EQ(SoftwareTriggerCutResult::c_reject, cut->checkPreScaled(object));
66  }
67 
68  cut = SoftwareTriggerCut::compile("1 == 1", 1);
69  for (unsigned int i = 0; i < 1e3; i++) {
70  EXPECT_EQ(SoftwareTriggerCutResult::c_accept, cut->checkPreScaled(object));
71  }
72  // As we set a stable seed, we know already the result quite well
73  cut = SoftwareTriggerCut::compile("1 == 1", 10);
74 
75  unsigned int numberOfYes = 0;
76  unsigned int numberOfNo = 0;
77  for (unsigned int i = 0; i < 1e4; i++) {
78  const auto cutResult = cut->checkPreScaled(object);
79  EXPECT_NE(SoftwareTriggerCutResult::c_reject, cutResult);
80 
81  if (cutResult == SoftwareTriggerCutResult::c_accept) {
82  numberOfYes++;
83  } else if (cutResult == SoftwareTriggerCutResult::c_noResult) {
84  numberOfNo++;
85  }
86  }
87 
88  // More or less 1 out of 10...
89  EXPECT_EQ(numberOfYes, 1022);
90  EXPECT_EQ(numberOfNo, 1e4 - 1022);
91  }
92 
94  TEST_F(SoftwareTriggerCutTest, prescalingCounter)
95  {
96 
97  SoftwareTriggerObject object;
98 
99  // We test it multiple times, just to be sure.
100  auto cut = SoftwareTriggerCut::compile("1 == 1", 0, false);
101  for (uint32_t i = 0; i < 1e3; i++) {
102  EXPECT_EQ(SoftwareTriggerCutResult::c_noResult, cut->check(object).first);
103  }
104 
105  // For reject cuts, the prescale is not allowed to have any influence
106  cut = SoftwareTriggerCut::compile("1 == 1", 0, true);
107  for (uint32_t i = 0; i < 1e3; i++) {
108  EXPECT_EQ(SoftwareTriggerCutResult::c_reject, cut->check(object).first);
109  }
110 
111  cut = SoftwareTriggerCut::compile("1 == 1", 1, true);
112  for (uint32_t i = 0; i < 1e3; i++) {
113  EXPECT_EQ(SoftwareTriggerCutResult::c_reject, cut->check(object).first);
114  }
115 
116  cut = SoftwareTriggerCut::compile("1 == 1", 1);
117  for (uint32_t i = 0; i < 1e3; i++) {
118  EXPECT_EQ(SoftwareTriggerCutResult::c_accept, cut->check(object).first);
119  }
120 
121  // Now let's test the counters.
122  cut = SoftwareTriggerCut::compile("1 == 1", 10);
123  uint32_t counter = 0;
124  uint32_t numberOfYes = 0;
125  uint32_t numberOfNo = 0;
126  // Since the counter starts with 0, we expect (729/10)+1 yes.
127  for (uint32_t i = 0; i < 729; i++) {
128  const auto cutResult = cut->check(object, &counter).first;
129  EXPECT_NE(SoftwareTriggerCutResult::c_reject, cutResult);
130  if (cutResult == SoftwareTriggerCutResult::c_accept) {
131  numberOfYes++;
132  } else if (cutResult == SoftwareTriggerCutResult::c_noResult) {
133  numberOfNo++;
134  }
135  }
136  uint32_t expectedYes = (729 / 10) + 1;
137  EXPECT_EQ(numberOfYes, expectedYes);
138  EXPECT_EQ(numberOfNo, 729 - expectedYes);
139 
140  cut = SoftwareTriggerCut::compile("1 == 1", 7);
141  counter = 1;
142  numberOfYes = 0;
143  numberOfNo = 0;
144  // Now the counter starts with 1, so we expect (544/7) yes.
145  for (uint32_t i = 1; i <= 544; i++) {
146  const auto cutResult = cut->check(object, &counter).first;
147  EXPECT_NE(SoftwareTriggerCutResult::c_reject, cutResult);
148  if (cutResult == SoftwareTriggerCutResult::c_accept) {
149  numberOfYes++;
150  } else if (cutResult == SoftwareTriggerCutResult::c_noResult) {
151  numberOfNo++;
152  }
153  }
154  expectedYes = (544 / 7);
155  EXPECT_EQ(numberOfYes, expectedYes);
156  EXPECT_EQ(numberOfNo, 544 - expectedYes);
157  }
158  }
160 }
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::TEST_F
TEST_F(GlobalLabelTest, LargeNumberOfTimeDependentParameters)
Test large number of time-dep params for registration and retrieval.
Definition: globalLabel.cc:65
Belle2::SoftwareTrigger::SoftwareTriggerCutTest
Base class for the cut tests.
Definition: softwareTriggerCut.cc:25