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>
17using 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)
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"));
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))"));
62 EXPECT_TRUE(absvar1 !=
nullptr);
63 EXPECT_EQ(std::get<double>(absvar1->
function(
nullptr)), 2);
66 EXPECT_TRUE(absvar2 !=
nullptr);
67 EXPECT_EQ(std::get<double>(absvar2->
function(
nullptr)), 0.5);
70 EXPECT_TRUE(absvar3 !=
nullptr);
71 EXPECT_EQ(std::get<double>(absvar3->
function(
nullptr)), 20);
74 EXPECT_TRUE(absvar4 !=
nullptr);
75 EXPECT_EQ(std::get<double>(absvar4->
function(
nullptr)), 1024);
78 EXPECT_TRUE(absvar5 !=
nullptr);
79 EXPECT_EQ(std::get<double>(absvar5->
function(
nullptr)), 1024);
82 EXPECT_TRUE(absvar6 !=
nullptr);
85 EXPECT_TRUE(absvar7 !=
nullptr);
88 EXPECT_TRUE(absvar8 !=
nullptr);
91 EXPECT_TRUE(absvar9 !=
nullptr);
94 EXPECT_TRUE(absvar10 !=
nullptr);
97 EXPECT_TRUE(absvar11 !=
nullptr);
100 EXPECT_TRUE(pvar !=
nullptr);
103 EXPECT_TRUE(absvar12 !=
nullptr);
104 EXPECT_EQ(std::get<double>(absvar12->
function(
nullptr)), 8);
107 EXPECT_TRUE(absvar13 !=
nullptr);
108 EXPECT_EQ(std::get<double>(absvar13->
function(
nullptr)), 10);
112 EXPECT_TRUE(minformulas !=
nullptr);
113 EXPECT_TRUE(std::get<bool>(minformulas->
function(
nullptr)));
118 EXPECT_TRUE(daughterProductP !=
nullptr);
121 EXPECT_TRUE(daughterSumP !=
nullptr);
124 EXPECT_TRUE(daughter1P !=
nullptr);
127 EXPECT_TRUE(extraInfo !=
nullptr);
130 EXPECT_TRUE(nested !=
nullptr);
134 EXPECT_B2FATAL(
Manager::Instance().getVariable(
"daughterSumOf(daughter(1, ExtraInfoWrongName(signalProbability)))"));
138 EXPECT_EQ(vec.size(), 1);
139 EXPECT_EQ(vec[0],
"myCollection");
140 EXPECT_TRUE(
Manager::Instance().addCollection(
"myCollection", {
"p",
"px",
"py",
"pz"}));
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");
153 EXPECT_TRUE(aliasDoesExists !=
nullptr);
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)))"));
178 Manager::VariableDataType::c_double));
181 Manager::VariableDataType::c_double));
185 Manager::VariableDataType::c_double);
187 ASSERT_NE(dummy,
nullptr);
189 EXPECT_DOUBLE_EQ(std::get<double>(dummy->
function(
nullptr)), 42.0);
192 REGISTER_VARIABLE(
"testingthedummyvar2", dummyVar,
"something else");
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);
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);
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");
221 ASSERT_NE(dummy,
nullptr);
222 EXPECT_DOUBLE_EQ(std::get<double>(dummy->
function(
nullptr)), 9.0);
234 Manager::VariableDataType::c_double));
236 Manager::VariableDataType::c_double));
238 Manager::VariableDataType::c_double));
242 Manager::VariableDataType::c_double);
245 TEST(VariableTest,
Cut)
249 Manager::VariableDataType::c_double);
251 std::unique_ptr<Cut> a =
Cut::compile(
"dummyvar > 1.0");
252 EXPECT_TRUE(a->check(
nullptr));
254 EXPECT_TRUE(a->check(
nullptr));
257 EXPECT_TRUE(a->check(
nullptr));
259 EXPECT_TRUE(a->check(
nullptr));
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.
static std::unique_ptr< GeneralCut > compile(const std::string &cut)
Creates an instance of a cut and returns a unique_ptr to it, if you need a copy-able object instead y...
Class to store reconstructed particles.
std::function< VarVariant(const Particle *)> FunctionPtr
functions stored take a const Particle* and return VarVariant.
std::vector< std::string > resolveCollections(const std::vector< std::string > &variables)
Resolve Collection Returns variable names corresponding to the given collection or if it is not a col...
const Var * getVariable(std::string name)
Get the variable belonging to the given key.
static Manager & Instance()
get singleton instance.
std::function< FunctionPtr(const std::vector< std::string > &)> MetaFunctionPtr
meta functions stored take a const std::vector<std::string>& and return a FunctionPtr.
bool addAlias(const std::string &alias, const std::string &variable)
Add alias Return true if the alias was successfully added.
std::function< VarVariant(const Particle *, const std::vector< double > &)> ParameterFunctionPtr
parameter functions stored take a const Particle*, const std::vector<double>& and return VarVariant.
void registerVariable(const std::string &name, const Manager::FunctionPtr &f, const std::string &description, const Manager::VariableDataType &v, const std::string &unit="")
Register a variable.
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.