8 #include <analysis/VariableManager/Manager.h>
9 #include <analysis/VariableManager/Utility.h>
10 #include <analysis/dataobjects/Particle.h>
11 #include <framework/utilities/TestHelpers.h>
13 #include <gtest/gtest.h>
17 using namespace Belle2::Variable;
21 double dummyVar(
const Particle*) {
return 42.0; }
22 double dummyVarWithParameters(
const Particle*,
const std::vector<double>& parameters)
25 for (
auto& x : parameters)
31 std::string arg = arguments[0];
32 auto func = [arg](
const Particle*) ->
double {
39 TEST(VariableTest, ManagerDeathTest)
42 EXPECT_B2FATAL(Manager::Instance().getVariable(
"THISDOESNTEXIST"));
43 EXPECT_B2FATAL(Manager::Instance().getVariable(
"abs(THISDOESNTEXIST+:THISDOENTEITHER"));
44 EXPECT_B2FATAL(Manager::Instance().getVariable(
"abs(THISDOESNTEXIST':THISDOENTEITHER"));
45 EXPECT_B2FATAL(Manager::Instance().getVariable(
"abs(THISDOESNTEXIST-:THISDOENTEITHER"));
46 EXPECT_B2FATAL(Manager::Instance().getVariable(
"abs(THISDOESNTEXIST&THISDOENTEITHER"));
47 EXPECT_B2FATAL(Manager::Instance().getVariable(
"abs(THISDOESNTEXIST&"));
48 EXPECT_B2FATAL(Manager::Instance().getVariable(
"abs(&THISISILLEGAL+1"));
50 EXPECT_B2FATAL(Manager::Instance().getVariable(
"abs(THISDOESNTEXIST + THISDOENTEITHER"));
51 EXPECT_B2FATAL(Manager::Instance().getVariable(
"abs(THISDOESNTEXIST+abs(THISDOENTEITHER))"));
52 EXPECT_B2FATAL(Manager::Instance().getVariable(
"abs(THISDOESNTEXIST + abs( THISDOENTEITHER))"));
53 EXPECT_B2FATAL(Manager::Instance().getVariable(
"abs(THISDOESNTEXIST * 1)"));
54 EXPECT_B2FATAL(Manager::Instance().getVariable(
"abs(THISDOESNTEXIST - 2)"));
55 EXPECT_B2FATAL(Manager::Instance().getVariable(
"abs(THISDOESNTEXIST**3)"));
56 EXPECT_B2FATAL(Manager::Instance().getVariable(
"abs(THISDOESNTEXIST**abs(p))"));
57 EXPECT_B2FATAL(Manager::Instance().getVariable(
"abs(THISDOESNTEXIST&**abs(p))"));
58 EXPECT_B2FATAL(Manager::Instance().getVariable(
"abs(funcdoesnotexist&(p))"));
61 const Manager::Var* absvar1 = Manager::Instance().getVariable(
"abs(1+1)");
62 EXPECT_TRUE(absvar1 !=
nullptr);
63 EXPECT_EQ(std::get<double>(absvar1->
function(
nullptr)), 2);
65 const Manager::Var* absvar2 = Manager::Instance().getVariable(
"abs(1/2)");
66 EXPECT_TRUE(absvar2 !=
nullptr);
67 EXPECT_EQ(std::get<double>(absvar2->
function(
nullptr)), 0.5);
69 const Manager::Var* absvar3 = Manager::Instance().getVariable(
"abs(2*10)");
70 EXPECT_TRUE(absvar3 !=
nullptr);
71 EXPECT_EQ(std::get<double>(absvar3->
function(
nullptr)), 20);
73 const Manager::Var* absvar4 = Manager::Instance().getVariable(
"abs(2**10)");
74 EXPECT_TRUE(absvar4 !=
nullptr);
75 EXPECT_EQ(std::get<double>(absvar4->
function(
nullptr)), 1024);
77 const Manager::Var* absvar5 = Manager::Instance().getVariable(
"abs(2^10)");
78 EXPECT_TRUE(absvar5 !=
nullptr);
79 EXPECT_EQ(std::get<double>(absvar5->
function(
nullptr)), 1024);
81 const Manager::Var* absvar6 = Manager::Instance().getVariable(
"abs(10*p)");
82 EXPECT_TRUE(absvar6 !=
nullptr);
84 const Manager::Var* absvar7 = Manager::Instance().getVariable(
"abs(10+p)");
85 EXPECT_TRUE(absvar7 !=
nullptr);
87 const Manager::Var* absvar8 = Manager::Instance().getVariable(
"abs(10-p)");
88 EXPECT_TRUE(absvar8 !=
nullptr);
90 const Manager::Var* absvar9 = Manager::Instance().getVariable(
"abs(10**p)");
91 EXPECT_TRUE(absvar9 !=
nullptr);
93 const Manager::Var* absvar10 = Manager::Instance().getVariable(
"abs(10^p)");
94 EXPECT_TRUE(absvar10 !=
nullptr);
96 const Manager::Var* absvar11 = Manager::Instance().getVariable(
"abs(10/p)");
97 EXPECT_TRUE(absvar11 !=
nullptr);
99 const Manager::Var* pvar = Manager::Instance().getVariable(
"p");
100 EXPECT_TRUE(pvar !=
nullptr);
102 const Manager::Var* absvar12 = Manager::Instance().getVariable(
"abs(2+3*2)");
103 EXPECT_TRUE(absvar12 !=
nullptr);
104 EXPECT_EQ(std::get<double>(absvar12->
function(
nullptr)), 8);
106 const Manager::Var* absvar13 = Manager::Instance().getVariable(
"abs(((2+3)*2))");
107 EXPECT_TRUE(absvar13 !=
nullptr);
108 EXPECT_EQ(std::get<double>(absvar13->
function(
nullptr)), 10);
111 const Manager::Var* minformulas = Manager::Instance().getVariable(
"passesCut(min(2+min(1/3, abs(-2/3)), 2+2/3) > 2.3)");
112 EXPECT_TRUE(minformulas !=
nullptr);
113 EXPECT_TRUE(std::get<bool>(minformulas->
function(
nullptr)));
117 const Manager::Var* daughterProductP = Manager::Instance().getVariable(
"daughterProductOf(p)");
118 EXPECT_TRUE(daughterProductP !=
nullptr);
120 const Manager::Var* daughterSumP = Manager::Instance().getVariable(
"daughterSumOf(p)");
121 EXPECT_TRUE(daughterSumP !=
nullptr);
123 const Manager::Var* daughter1P = Manager::Instance().getVariable(
"daughter(1, p)");
124 EXPECT_TRUE(daughter1P !=
nullptr);
126 const Manager::Var* extraInfo = Manager::Instance().getVariable(
"extraInfo(signalProbability)");
127 EXPECT_TRUE(extraInfo !=
nullptr);
129 const Manager::Var* nested = Manager::Instance().getVariable(
"daughterSumOf(daughter(1, extraInfo(signalProbability)))");
130 EXPECT_TRUE(nested !=
nullptr);
132 EXPECT_B2FATAL(Manager::Instance().getVariable(
"funcDoesNotExist(p)"));
134 EXPECT_B2FATAL(Manager::Instance().getVariable(
"daughterSumOf(daughter(1, ExtraInfoWrongName(signalProbability)))"));
137 auto vec = Manager::Instance().resolveCollections({
"myCollection"});
138 EXPECT_EQ(vec.size(), 1);
139 EXPECT_EQ(vec[0],
"myCollection");
140 EXPECT_TRUE(Manager::Instance().addCollection(
"myCollection", {
"p",
"px",
"py",
"pz"}));
142 auto vec2 = Manager::Instance().resolveCollections({
"myCollection"});
143 EXPECT_EQ(vec2.size(), 4);
144 EXPECT_EQ(vec2[0],
"p");
145 EXPECT_EQ(vec2[1],
"px");
146 EXPECT_EQ(vec2[2],
"py");
147 EXPECT_EQ(vec2[3],
"pz");
150 EXPECT_B2FATAL(Manager::Instance().getVariable(
"myAlias"));
151 Manager::Instance().addAlias(
"myAlias",
"daughterSumOf(daughter(1, extraInfo(signalProbability)))");
152 const Manager::Var* aliasDoesExists = Manager::Instance().getVariable(
"myAlias");
153 EXPECT_TRUE(aliasDoesExists !=
nullptr);
156 auto& vm = Manager::Instance();
157 EXPECT_TRUE(vm.addAlias(
"myAliasAlias",
"myAlias"));
159 EXPECT_EQ(aliasDoesExists, vm.getVariable(
"myAliasAlias"));
162 EXPECT_TRUE(vm.addAlias(
"aliasLoop1",
"aliasLoop2"));
163 EXPECT_TRUE(vm.addAlias(
"aliasLoop2",
"aliasLoop3"));
164 EXPECT_TRUE(vm.addAlias(
"aliasLoop3",
"aliasLoop4"));
165 EXPECT_TRUE(vm.addAlias(
"aliasLoop4",
"aliasLoop1"));
166 EXPECT_B2FATAL(vm.getVariable(
"aliasLoop1"));
167 EXPECT_B2FATAL(vm.getVariable(
"aliasLoop3"));
170 EXPECT_NO_B2WARNING(Manager::Instance().addAlias(
"myAlias",
"daughterSumOf(daughter(1, extraInfo(signalProbability)))"));
172 EXPECT_B2WARNING(Manager::Instance().addAlias(
"myAlias",
"daughterSumOf(daughter(0, extraInfo(signalProbability)))"));
174 EXPECT_B2ERROR(Manager::Instance().addAlias(
"M",
"daughterSumOf(daughter(1, extraInfo(signalProbability)))"));
177 EXPECT_B2FATAL(Manager::Instance().registerVariable(
"p", (
Manager::FunctionPtr)&dummyVar,
"description",
178 Manager::VariableDataType::c_double));
180 EXPECT_B2FATAL(Manager::Instance().registerVariable(
"something", (
Manager::FunctionPtr)
nullptr,
"blah",
181 Manager::VariableDataType::c_double));
184 Manager::Instance().registerVariable(
"testingthedummyvar", (
Manager::FunctionPtr)&dummyVar,
"blah",
185 Manager::VariableDataType::c_double);
186 const Manager::Var* dummy = Manager::Instance().getVariable(
"testingthedummyvar");
187 ASSERT_NE(dummy,
nullptr);
189 EXPECT_DOUBLE_EQ(std::get<double>(dummy->
function(
nullptr)), 42.0);
192 REGISTER_VARIABLE(
"testingthedummyvar2", dummyVar,
"something else");
193 dummy = Manager::Instance().getVariable(
"testingthedummyvar2");
194 ASSERT_NE(dummy,
nullptr);
195 EXPECT_TRUE(dummy->
description ==
"something else");
196 EXPECT_DOUBLE_EQ(std::get<double>(dummy->
function(
nullptr)), 42.0);
200 "blah", Manager::VariableDataType::c_double);
201 dummy = Manager::Instance().getVariable(
"testingthedummyvarwithparameters(3)");
202 ASSERT_NE(dummy,
nullptr);
203 EXPECT_DOUBLE_EQ(std::get<double>(dummy->
function(
nullptr)), 3.0);
204 EXPECT_DOUBLE_EQ(std::get<double>(Manager::Instance().getVariable(
"testingthedummyvarwithparameters(3)")->
function(
nullptr)), 3.0);
205 EXPECT_DOUBLE_EQ(std::get<double>(Manager::Instance().getVariable(
"testingthedummyvarwithparameters(3,5)")->
function(
nullptr)),
207 EXPECT_DOUBLE_EQ(std::get<double>(Manager::Instance().getVariable(
"testingthedummyvarwithparameters(3,7,8)")->
function(
nullptr)),
211 "blah", Manager::VariableDataType::c_double);
212 dummy = Manager::Instance().getVariable(
"testingthedummymetavar(1 < 2)");
213 ASSERT_NE(dummy,
nullptr);
214 EXPECT_DOUBLE_EQ(std::get<double>(dummy->
function(
nullptr)), 5.0);
215 EXPECT_DOUBLE_EQ(std::get<double>(Manager::Instance().getVariable(
"testingthedummymetavar(123)")->
function(
nullptr)), 3.0);
219 REGISTER_VARIABLE(
"testingthedummyvarwithparameters2(n,m)", dummyVarWithParameters,
"something else");
220 dummy = Manager::Instance().getVariable(
"testingthedummyvarwithparameters2(4,5)");
221 ASSERT_NE(dummy,
nullptr);
222 EXPECT_DOUBLE_EQ(std::get<double>(dummy->
function(
nullptr)), 9.0);
230 EXPECT_TRUE(Manager::Instance().getVariables().size() > 0);
233 EXPECT_B2FATAL(Manager::Instance().registerVariable(
" space", (
Manager::FunctionPtr)dummyVar,
"blah",
234 Manager::VariableDataType::c_double));
235 EXPECT_B2FATAL(Manager::Instance().registerVariable(
"star*", (
Manager::FunctionPtr)dummyVar,
"blah",
236 Manager::VariableDataType::c_double));
237 EXPECT_B2FATAL(Manager::Instance().registerVariable(
"*", (
Manager::FunctionPtr)dummyVar,
"blah",
238 Manager::VariableDataType::c_double));
241 Manager::Instance().registerVariable(
"abcdef0123945859432689_ZEFUEONHSUTNSXA", (
Manager::FunctionPtr)dummyVar,
"blah",
242 Manager::VariableDataType::c_double);
247 Manager::Instance().registerVariable(
"dummyvar", (
Manager::FunctionPtr)&dummyVar,
"blah", Manager::VariableDataType::c_double);
249 Manager::VariableDataType::c_double);
251 std::unique_ptr<Cut> a = Cut::compile(
"dummyvar > 1.0");
252 EXPECT_TRUE(a->check(
nullptr));
253 a = Cut::compile(
"1.0 < dummyvar <= dummyvar");
254 EXPECT_TRUE(a->check(
nullptr));
256 a = Cut::compile(
"dummyvar < 100.0");
257 EXPECT_TRUE(a->check(
nullptr));
258 a = Cut::compile(
"dummyvar <= dummyvar <= dummyvar");
259 EXPECT_TRUE(a->check(
nullptr));
261 a = Cut::compile(
"dummymetavar(123) < 100.0");
262 EXPECT_TRUE(a->check(
nullptr));
263 a = Cut::compile(
"dummymetavar(1) <= dummymetavar(1<2) <= dummymetavar(1<23)");
264 EXPECT_TRUE(a->check(
nullptr));
This class implements a common way to implement cut/selection functionality for arbitrary objects.
Class to store reconstructed particles.
std::function< VarVariant(const Particle *)> FunctionPtr
functions stored take a const Particle* and return VarVariant.
std::function< FunctionPtr(const std::vector< std::string > &)> MetaFunctionPtr
meta functions stored take a const std::vector<std::string>& and return a FunctionPtr.
std::function< VarVariant(const Particle *, const std::vector< double > &)> ParameterFunctionPtr
parameter functions stored take a const Particle*, const std::vector<double>& and return VarVariant.
TEST(TestgetDetectorRegion, TestgetDetectorRegion)
Test Constructors.
Abstract base class for different kinds of events.
std::string description
Description of what this function does.
A variable returning a floating-point value for a given Particle.
FunctionPtr function
Pointer to function.