Belle II Software development
globalParam.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 <alignment/dbobjects/VXDAlignment.h>
10#include <alignment/GlobalParam.h>
11#include <alignment/Manager.h>
12#include <cdc/dbobjects/CDCTimeZeros.h>
13#include <cdc/dbobjects/CDCTimeWalks.h>
14#include <cdc/dbobjects/CDCAlignment.h>
15#include <cdc/dbobjects/CDCXtRelations.h>
16#include <framework/dataobjects/EventMetaData.h>
17#include <klm/dbobjects/bklm/BKLMAlignment.h>
18#include <klm/dbobjects/eklm/EKLMAlignment.h>
19
20#include <gtest/gtest.h>
21
22#include <iostream>
23#include <string>
24
25using namespace std;
26using namespace Belle2;
27using namespace alignment;
28
29namespace {
31// class MockDBObj : public TObject {
32// public:
33// static unsigned short getGlobalUniqueID() { return 1; }
34// double getGlobalParam(unsigned short element, unsigned short param)
35// {
36// if (element != 1)
37// return 0.;
38// if (param != 1)
39// return 0.;
40//
41// return m_value;
42// }
43// void setGlobalParam(unsigned short element, unsigned short param, double val)
44// {
45// if (element != 1)
46// return;
47// if (param != 1)
48// return;
49// m_value = val;
50// }
51// std::vector<std::pair<unsigned short, unsigned short>> listGlobalParams() {return {{1, 1}};}
52// private:
53// double m_value {0.};
54// };
55
56 class MockDetectorInterface : public IGlobalParamInterface {
57 public:
58 explicit MockDetectorInterface(int id) : m_id(id) {}
59 int hasBeenCalled() { return m_called; }
60 virtual void readFromResult(std::vector<std::tuple<unsigned short, unsigned short, unsigned short, double>>&,
61 GlobalParamVector&) override final
62 {
63 std::cout << " Interface " << m_id << ": called readFromResult()" << std::endl;
64 ++m_called;
65 }
66 private:
67 int m_id { -1};
68 int m_called {0};
69 };
70
72 class GlobalParamTest : public ::testing::Test {
73 protected:
74
76 virtual void SetUp()
77 {
78
79 }
80
82 virtual void TearDown()
83 {
84
85 }
86
87 };
88
92 TEST_F(GlobalParamTest, GlobalParamComponentsInterfaces)
93 {
94 // Full vector
95 GlobalParamVector gpv;
96 // Vector restricted to BeamSpot
97 GlobalParamVector gpvComp({"BeamSpot"});
98
99 // Interfaces for testing
100 auto interface1 = new MockDetectorInterface(1);
101 auto interface2 = new MockDetectorInterface(2);
102 auto interface3 = new MockDetectorInterface(3);
103
104 auto iptr1 = std::shared_ptr<IGlobalParamInterface>(interface1);
105 auto iptr2 = std::shared_ptr<IGlobalParamInterface>(interface2);
106 auto iptr3 = std::shared_ptr<IGlobalParamInterface>(interface3);
107
108 // Init the vectors with db objects and interfaces (see GlobalCalibrationManager::initGlobalVector(...))
109 for (auto vect : std::vector<GlobalParamVector*>({&gpv, &gpvComp})) {
110 vect->addDBObj<BeamSpot>(iptr1);
111 vect->addDBObj<VXDAlignment>(iptr2);
112 vect->addDBObj<CDCAlignment>(iptr2);
113 vect->addDBObj<CDCTimeZeros>();
114 vect->addDBObj<CDCTimeWalks>();
115 vect->addDBObj<CDCXtRelations>();
116 vect->addDBObj<BKLMAlignment>();
117 vect->addDBObj<EKLMAlignment>();
118
119 vect->addSubDetectorInterface(iptr3);
120 vect->addSubDetectorInterface(iptr3);
121 vect->addSubDetectorInterface(iptr3);
122 }
123
124 // First a check, no interface called yet
125 EXPECT_EQ(interface1->hasBeenCalled(), 0);
126 EXPECT_EQ(interface2->hasBeenCalled(), 0);
127 EXPECT_EQ(interface3->hasBeenCalled(), 0);
128
129 std::vector<std::tuple<unsigned short, unsigned short, unsigned short, double>> emptyResult;
130
131 // Full vector accepted everything and calls all interfaces
132 gpv.postReadFromResult(emptyResult);
133 EXPECT_EQ(interface1->hasBeenCalled(), 1);
134 EXPECT_EQ(interface2->hasBeenCalled(), 1); // only one more read due to uniqueness
135 EXPECT_EQ(interface3->hasBeenCalled(), 1);
136
137 // BeamSpot have interface 1 and interface 3 (added for any components configuration)
138 // Interface 2 should not be stored/called in 'gpvComp'
139 gpvComp.postReadFromResult(emptyResult);
140 EXPECT_EQ(interface1->hasBeenCalled(), 2);
141 EXPECT_EQ(interface2->hasBeenCalled(), 1);
142 EXPECT_EQ(interface3->hasBeenCalled(), 2);
143
144 EXPECT_EQ(gpv.getGlobalParamSet<VXDAlignment>().isConstructed(), false);
145 EXPECT_EQ(gpvComp.getGlobalParamSet<VXDAlignment>().isConstructed(), false);
146 // Construct the DB objects to be able to check pointers to them
147 gpv.construct();
148 gpvComp.construct();
149
150 // Full vector contains all objects
151 EXPECT_EQ(!!gpv.getDBObj<VXDAlignment>(), true);
152 EXPECT_EQ(!!gpv.getDBObj<BeamSpot>(), true);
153 // BeamSpot global vector should only containt BeamSpot
154 EXPECT_EQ(!!gpvComp.getDBObj<VXDAlignment>(), false);
155 EXPECT_EQ(!!gpvComp.getDBObj<BeamSpot>(), true);
156
157 // Full vector contains all objects
158 EXPECT_EQ(gpv.getGlobalParamSet<VXDAlignment>().isConstructed(), true);
159 EXPECT_EQ(gpv.getGlobalParamSet<BeamSpot>().isConstructed(), true);
160 // BeamSpot global vector should only containt BeamSpot
161 EXPECT_EQ(gpvComp.getGlobalParamSet<VXDAlignment>().isConstructed(), false);
162 EXPECT_EQ(gpvComp.getGlobalParamSet<BeamSpot>().isConstructed(), true);
163
164 // Accessing non-existing object will return an empty set
165 EXPECT_EQ(gpvComp.getGlobalParamSet<VXDAlignment>().is<VXDAlignment>(), false);
166 EXPECT_EQ(gpvComp.getGlobalParamSet<VXDAlignment>().is<EmptyGlobalParamSet>(), true);
167 EXPECT_EQ(gpvComp.getGlobalParamSet<VXDAlignment>().empty(), true);
168 EXPECT_EQ(gpvComp.getGlobalParamSet<VXDAlignment>().isConstructed(), false);
169 EXPECT_EQ((bool) gpvComp.getGlobalParamSet<VXDAlignment>(), false);
170
171 EXPECT_EQ(gpv.getGlobalParamSet<BeamSpot>().is<BeamSpot>(), true);
172 EXPECT_EQ(gpv.getGlobalParamSet<BeamSpot>().is<EmptyGlobalParamSet>(), false);
173 EXPECT_EQ(gpv.getGlobalParamSet<BeamSpot>().empty(), false);
174 EXPECT_EQ(gpv.getGlobalParamSet<BeamSpot>().isConstructed(), true);
175 EXPECT_EQ((bool) gpv.getGlobalParamSet<BeamSpot>(), true);
176
177
178 EXPECT_EQ(gpvComp.getGlobalParamSet<EmptyGlobalParamSet>().is<EmptyGlobalParamSet>(), true);
179
180 // This commented because BeamSpot is not yet in the DB. Instead, for now
181 // test VXDAlignment and once I get to this back, I want to have test for
182 // all supported db objects.
183 /*
184 GlobalParamVector newgpv({"BeamSpot", "CDCAlignment"});
185 GlobalCalibrationManager::initGlobalVector(newgpv);
186
187 EXPECT_EQ(newgpv.getGlobalParamSet<BeamSpot>().isConstructed(), false);
188 newgpv.construct();
189 EXPECT_EQ(newgpv.getGlobalParamSet<BeamSpot>().isConstructed(), true);
190
191 newgpv.setGlobalParam(42., BeamSpot::getGlobalUniqueID(), 0, 1);
192 EXPECT_EQ(newgpv.getGlobalParam(BeamSpot::getGlobalUniqueID(), 0, 1), 42.);
193 newgpv.loadFromDB(EventMetaData(1, 0, 0));
194 EXPECT_EQ(newgpv.getGlobalParam(BeamSpot::getGlobalUniqueID(), 0, 1), 0.);
195
196 newgpv.updateGlobalParam(42., BeamSpot::getGlobalUniqueID(), 0, 1);
197 EXPECT_EQ(newgpv.getGlobalParam(BeamSpot::getGlobalUniqueID(), 0, 1), 42.);
198
199 newgpv.setGlobalParam(42., CDCAlignment::getGlobalUniqueID(), 0, 1);
200 EXPECT_EQ(newgpv.getGlobalParam(CDCAlignment::getGlobalUniqueID(), 0, 1), 42.);
201 */
202
203 GlobalParamVector newgpv({"VXDAlignment", "CDCAlignment"});
204 GlobalCalibrationManager::initGlobalVector(newgpv);
205
206 EXPECT_EQ(newgpv.getGlobalParamSet<VXDAlignment>().isConstructed(), false);
207 newgpv.construct();
208 EXPECT_EQ(newgpv.getGlobalParamSet<VXDAlignment>().isConstructed(), true);
209
210 newgpv.setGlobalParam(42., VXDAlignment::getGlobalUniqueID(), 0, 1);
211 EXPECT_EQ(newgpv.getGlobalParam(VXDAlignment::getGlobalUniqueID(), 0, 1), 42.);
212 newgpv.loadFromDB(EventMetaData(1, 0, 0));
213 EXPECT_EQ(newgpv.getGlobalParam(VXDAlignment::getGlobalUniqueID(), 0, 1), 0.);
214
215 newgpv.updateGlobalParam(42., VXDAlignment::getGlobalUniqueID(), 0, 1);
216 EXPECT_EQ(newgpv.getGlobalParam(VXDAlignment::getGlobalUniqueID(), 0, 1), 42.);
217
218 newgpv.setGlobalParam(42., CDCAlignment::getGlobalUniqueID(), 0, 1);
219 EXPECT_EQ(newgpv.getGlobalParam(CDCAlignment::getGlobalUniqueID(), 0, 1), 42.);
220
221 }
222
223} // namespace
Class to store BKLM alignment data in the database.
Definition: BKLMAlignment.h:30
This class contains the beam spot position and size modeled as a gaussian distribution in space.
Definition: BeamSpot.h:22
CDC alignment constants.
Definition: CDCAlignment.h:24
static unsigned short getGlobalUniqueID()
Get global unique id.
Definition: CDCAlignment.h:109
Database object for time-walk.
Definition: CDCTimeWalks.h:25
Database object for timing offset (t0).
Definition: CDCTimeZeros.h:26
Database object for xt-relations.
Class to store EKLM alignment data in the database.
Definition: EKLMAlignment.h:30
Store event, run, and experiment numbers.
Definition: EventMetaData.h:33
VXD alignment (and maybe some calibration) parameters.
Definition: VXDAlignment.h:19
static unsigned short getGlobalUniqueID()
Get global unique id.
Definition: VXDAlignment.h:47
Abstract base class for different kinds of events.
STL namespace.