Belle II Software development
particleWeighting.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#include <analysis/dbobjects/ParticleWeightingLookUpTable.h>
10#include <analysis/dbobjects/ParticleWeightingBinLimits.h>
11#include <analysis/dbobjects/ParticleWeightingAxis.h>
12
13#include <framework/utilities/TestHelpers.h>
14
15#include <gtest/gtest.h>
16
17using namespace std;
18using namespace Belle2;
19
20namespace {
21
22 TEST(ParticleWeighting, BinLimits)
23 {
24 EXPECT_B2FATAL(ParticleWeightingBinLimits(42, 42));
25#ifndef __clang_analyzer__
26 auto* a = new ParticleWeightingBinLimits(0, 1);
27 EXPECT_TRUE(a->first() == 0);
28 EXPECT_TRUE(a->second() == 1);
29 auto* b = new ParticleWeightingBinLimits(1, 0);
30 EXPECT_TRUE(b->first() == 0);
31 EXPECT_TRUE(b->second() == 1);
32#endif
33 }
34
35 TEST(ParticleWeighting, Axis)
36 {
37 auto* a = new ParticleWeightingAxis();
38 EXPECT_TRUE(a->getName() == "");
39 a->setName("Test axis name");
40 EXPECT_TRUE(a->getName() == "Test axis name");
41
42 auto* bin1 = new ParticleWeightingBinLimits(0, 1);
43 auto* bin1b = new ParticleWeightingBinLimits(1, 2);
44 auto* bin2 = new ParticleWeightingBinLimits(2, 3);
45
46 int added_bin_id_1 = a->addBin(bin1);
47 EXPECT_EQ(added_bin_id_1, 1);
48 int added_bin_id_2 = a->addBin(bin2);
49 EXPECT_EQ(added_bin_id_2, 2);
50#ifndef __clang_analyzer__
51 auto* bin3 = new ParticleWeightingBinLimits(0, 5);
52 auto* bin4 = new ParticleWeightingBinLimits(0.5, 1.5);
53 auto* bin5 = new ParticleWeightingBinLimits(2.5, 3);
54 EXPECT_B2FATAL(a->addBin(bin3));
55 EXPECT_B2FATAL(a->addBin(bin4));
56 EXPECT_B2FATAL(a->addBin(bin5));
57#endif
58 EXPECT_TRUE(a->findBin(0.5) == 1);
59 EXPECT_TRUE(a->findBin(1.5) == -1);
60 EXPECT_TRUE(a->findBin(bin1) == 1);
61 EXPECT_TRUE(a->addBin(bin1b) == 3);
62 }
63
64 TEST(ParticleWeighting, KeyMap)
65 {
66 auto* bl1 = new ParticleWeightingBinLimits(0, 1);
67 auto* bl2 = new ParticleWeightingBinLimits(1, 2);
68 auto* bl3 = new ParticleWeightingBinLimits(1, 3);
69 string a1 = "Axis1";
70 string a2 = "Axis2";
71 string a3 = "Axis3";
72 NDBin bin1;
73 bin1.insert(make_pair(a1, bl1));
74 bin1.insert(make_pair(a2, bl1));
75 NDBin bin2;
76 bin2.insert(make_pair(a1, bl2));
77 bin2.insert(make_pair(a2, bl2));
78 NDBin bin3;
79 bin3.insert(make_pair(a1, bl3));
80 bin3.insert(make_pair(a2, bl3));
81 NDBin bin1a;
82 bin1a.insert(make_pair(a1, bl2));
83 bin1a.insert(make_pair(a2, bl2));
84 bin1a.insert(make_pair(a3, bl3));
85 NDBin bin1b;
86 bin1b.insert(make_pair(a1, bl1));
87 bin1b.insert(make_pair(a1, bl2));
88 NDBin bin1c;
89 bin1c.insert(make_pair(a3, bl3));
90 bin1c.insert(make_pair(a2, bl2));
91 map<string, double> entry1;
92 entry1.insert(make_pair(a1, 0.1));
93 entry1.insert(make_pair(a2, 0.2));
94 map<string, double> entry2;
95 entry2.insert(make_pair(a1, 1.1));
96 entry2.insert(make_pair(a2, 1.2));
97 map<string, double> entry3;
98 entry3.insert(make_pair(a1, 10));
99 entry3.insert(make_pair(a2, 10));
100
101 auto* kmp = new ParticleWeightingKeyMap();
102 int added_bin_id_1 = kmp->addKey(bin1);
103 EXPECT_EQ(added_bin_id_1, 0);
104 int added_bin_id_2 = kmp->addKey(bin2);
105 EXPECT_EQ(added_bin_id_2, 1);
106 EXPECT_B2FATAL(kmp->addKey(bin1, 42));
107 EXPECT_B2FATAL(kmp->addKey(bin1a));
108 EXPECT_B2FATAL(kmp->addKey(bin1b));
109 EXPECT_B2FATAL(kmp->addKey(bin1c));
110 EXPECT_EQ(kmp->getKey(entry1), 0);
111 EXPECT_EQ(kmp->getKey(entry2), 1);
112 EXPECT_EQ(kmp->getKey(entry3), -1);
113 }
114
115 TEST(ParticleWeighting, LookUpTable)
116 {
117 auto* bl1 = new ParticleWeightingBinLimits(0, 1);
118 auto* bl2 = new ParticleWeightingBinLimits(1, 2);
119 auto* bl3 = new ParticleWeightingBinLimits(1, 3);
120 string a1 = "Axis1";
121 string a2 = "Axis2";
122 string v1 = "Var1";
123 string v2 = "Var2";
124 NDBin bin1;
125 bin1.insert(make_pair(a1, bl1));
126 bin1.insert(make_pair(a2, bl1));
127 NDBin bin2;
128 bin2.insert(make_pair(a1, bl2));
129 bin2.insert(make_pair(a2, bl2));
130 NDBin bin3;
131 bin3.insert(make_pair(a1, bl3));
132 bin3.insert(make_pair(a2, bl3));
133 WeightInfo info1;
134 info1.insert(make_pair(v1, 41));
135 info1.insert(make_pair(v2, 42));
136 WeightInfo info2;
137 info2.insert(make_pair(v1, 31));
138 info2.insert(make_pair(v2, 32));
139 WeightInfo info3;
140 info3.insert(make_pair(v1, 21));
141 info3.insert(make_pair(v2, 22));
142 map<string, double> entry1;
143 entry1.insert(make_pair(a1, 0.1));
144 entry1.insert(make_pair(a2, 0.2));
145 map<string, double> entry1a;
146 entry1a.insert(make_pair(a1, 100));
147 entry1a.insert(make_pair(a2, 200));
148
149 auto* ltb = new ParticleWeightingLookUpTable();
150 ltb->addEntry(info1, bin1);
151 ltb->addEntry(info2, bin2);
152 vector<string> axes = ltb->getAxesNames();
153 vector<string> known_axes;
154 known_axes.push_back(a1);
155 known_axes.push_back(a2);
156 EXPECT_TRUE(equal(axes.begin(), axes.end(), known_axes.begin()));
157 WeightInfo obtained_info = ltb->getInfo(entry1);
158 double info_at_1 = obtained_info.at(v1);
159 EXPECT_EQ(info_at_1, 41);
160 double info_at_2 = obtained_info.at(v2);
161 EXPECT_EQ(info_at_2, 42);
162
163 EXPECT_B2FATAL(ltb->getInfo(entry1a));
164
165 ltb->defineOutOfRangeWeight(info3);
166 obtained_info = ltb->getInfo(entry1a);
167 info_at_2 = obtained_info.at(v2);
168 EXPECT_EQ(info_at_2, 22);
169
170 }
171
172} // namespace
Class for handling LookUp tables.
Just pair of numbers - min and max values of bin border.
std::map< std::string, ParticleWeightingBinLimits * > NDBin
N-dim bin: pairs of bin limits with name of the axis variable.
std::map< std::string, double > WeightInfo
Weight information: a line from the weight lookup table.
Abstract base class for different kinds of events.
const std::vector< double > v2
MATLAB generated random vector.
const std::vector< double > v1
MATLAB generated random vector.
STL namespace.