Belle II Software development
timeline.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/GlobalTimeLine.h>
11#include <alignment/Manager.h>
12#include <cdc/dbobjects/CDCAlignment.h>
13#include <framework/database/Configuration.h>
14#include <framework/database/Database.h>
15#include <framework/database/EventDependency.h>
16#include <framework/database/TestingPayloadStorage.h>
17#include <framework/dataobjects/EventMetaData.h>
18#include <vxd/dataobjects/VxdID.h>
19
20#include <gtest/gtest.h>
21
22#include <TFile.h>
23
24#include <filesystem>
25#include <iostream>
26#include <string>
27
28using namespace std;
29using namespace Belle2;
30using namespace alignment;
31using namespace timeline;
32
33namespace {
34 template<int UID>
35 class MockDBObj {
36 public:
37 MockDBObj(double value = 0.) : m_param(value) {}
38 // ------------- Interface to global Millepede calibration ----------------
40 static unsigned short getGlobalUniqueID()
41 {
42 static_assert(UID != 0, "Non-zero UID required");
43 return UID;
44 }
46 double getGlobalParam(unsigned short element, unsigned short param) {return m_param;}
48 void setGlobalParam(double value, unsigned short element, unsigned short param) {m_param = value;}
50 std::vector<std::pair<unsigned short, unsigned short>> listGlobalParams() {return {{0, 0}};}
51 private:
52 double m_param{0.};
53 };
54
55
56 class TimeLineTest : public ::testing::Test {
57 protected:
58
59 IntraIoVPayloads beamspotRun0 = {
60 {EventMetaData(0, 0, 0), std::make_shared<GlobalParamSet<BeamSpot>>()}
61 };
62
63 IntraIoVPayloads beamspotRun1 = {
64 {EventMetaData(0, 1, 0), std::make_shared<GlobalParamSet<BeamSpot>>()}
65 };
66
67 IntraIoVPayloads beamspotRun2 = {
68 {EventMetaData(0, 2, 0), std::make_shared<GlobalParamSet<BeamSpot>>()},
69 {EventMetaData(530532, 2, 0), std::make_shared<GlobalParamSet<BeamSpot>>()}
70 };
71
72 IntraIoVPayloads beamspotRun3 = {
73 {EventMetaData(0, 3, 0), std::make_shared<GlobalParamSet<BeamSpot>>()}
74 };
75
76 IntraIoVPayloads beamspotRun4 = {
77 {EventMetaData(0, 4, 0), std::make_shared<GlobalParamSet<BeamSpot>>()}
78 };
79
80 PayloadIovBlockRow beamspotRow = {
81 {IntervalOfValidity(0, 0, 0, 0), beamspotRun0},
82 {IntervalOfValidity(0, 1, 0, 1), beamspotRun1},
83 {IntervalOfValidity(0, 2, 0, 2), beamspotRun2},
84 {IntervalOfValidity(0, 3, 0, 3), beamspotRun3},
85 {IntervalOfValidity(0, 4, 0, 4), beamspotRun4}
86 };
87
88 IntraIoVPayloads vxdRun0to2 = {
89 {EventMetaData(0, 0, 0), std::make_shared<GlobalParamSet<VXDAlignment>>()},
90 };
91
92 IntraIoVPayloads vxdRun3toInf = {
93 {EventMetaData(0, 3, 0), std::make_shared<GlobalParamSet<VXDAlignment>>()},
94 };
95
96 PayloadIovBlockRow vxdRow = {
97 {IntervalOfValidity(0, 0, 0, 2), vxdRun0to2},
98 {IntervalOfValidity(0, 3, 0, 4), vxdRun3toInf}
99
100 };
101
102 IntraIoVPayloads cdcBlock = {
103 {EventMetaData(0, 0, 0), std::make_shared<GlobalParamSet<CDCAlignment>>()}
104
105 };
106 PayloadIovBlockRow cdcRow = {
107 {IntervalOfValidity(0, 0, 0, 4), cdcBlock}
108 };
109
110 PayloadsTable payloadsTable = {
111 {BeamSpot::getGlobalUniqueID(), beamspotRow},
114 };
115
116 std::vector<int> head0 = {0, 1, 2, 3, 4, 5};
117 std::vector<int> head1 = {0, 1, 2, 2, 3, 4};
118 std::vector<int> row0 = {0, 1, 2, 3, 4, 5};
119 std::vector<int> row1 = {0, 0, 0, 0, 1, 1};
120 std::vector<int> row2 = {0, 0, 0, 0, 0, 0};
121
122 EventHeader eventHeader = {
123 EventMetaData(0, 0, 0),
124 EventMetaData(0, 1, 0),
125 EventMetaData(0, 2, 0),
126 EventMetaData(530532, 2, 0),
127 EventMetaData(0, 3, 0),
128 EventMetaData(0, 4, 0)
129 };
130
131 RunHeader runHeader = {
132 {0, 0},
133 {0, 1},
134 {0, 2},
135 {0, 2},
136 {0, 3},
137 {0, 4}
138 };
139
140 TableData tableData = {
141 { 1, row0},
142 {10, row1},
143 {27, row2}
144 };
145
146 TimeTable timeTable = std::make_tuple(eventHeader, runHeader, tableData);
147
148 std::vector<int> irow0 = {0, 1, 1, 1, 1, 1};
149 std::vector<int> irow1 = {0, 0, 0, 0, 1, 0};
150 std::vector<int> irow2 = {0, 0, 0, 0, 0, 0};
151
152 TableData initTableData = {
153 { 1, irow0},
154 {10, irow1},
155 {27, irow2}
156 };
157
158 TimeTable initTimeTable = std::make_tuple(eventHeader, runHeader, initTableData);
159
161 virtual void SetUp()
162 {
165 evtPtr.registerInDataStore();
167 // Event 0 run 0 exp 0
168 evtPtr.construct(0, 0, 0);
169
171 c.setNewPayloadLocation("testPayloads/TestDatabase.txt");
172
175
176 auto beamspotX = GlobalLabel::construct<BeamSpot>(0, 1);
177 auto beamspotZ = GlobalLabel::construct<BeamSpot>(0, 3);
178 auto yingZ = GlobalLabel::construct<VXDAlignment>(VxdID(1, 0, 0, 1).getID(), 3);
179
180 yingZ.registerTimeDependent(4);
181
182 // should be banned:
183 //beamspotX.registerTimeDependent(3);
184 // -> instead:
185 beamspotX.registerTimeDependent(2);
186 beamspotX.registerTimeDependent(3);
187 beamspotX.registerTimeDependent(4);
188
189 beamspotZ.registerTimeDependent(1);
190 beamspotZ.registerTimeDependent(2);
191 beamspotZ.registerTimeDependent(3);
192 beamspotZ.registerTimeDependent(4);
193 beamspotZ.registerTimeDependent(5);
194
195 }
196
198 virtual void TearDown()
199 {
202
203 std::filesystem::remove_all("testPayloads");
206 }
207
208 };
209
211 TEST_F(TimeLineTest, InitialTimeTable)
212 {
213 GlobalLabel label;
214 auto table = makeInitialTimeTable(eventHeader, label);
215
216 std::vector<int> myrow0 = {0, 1, 1, 1, 1, 1};
217 std::vector<int> myrow1 = {0, 0, 0, 0, 1, 0};
218
219 TableData myInitTableData = {
220 { 1, myrow0},
221 {10, myrow1}
222 };
223
224 TimeTable myInitTimeTable = std::make_tuple(eventHeader, runHeader, myInitTableData);
225
226
227 EXPECT_EQ(table, myInitTimeTable);
228
229 }
230
232 TEST_F(TimeLineTest, TimeTable)
233 {
234 finalizeTimeTable(initTimeTable);
235 EXPECT_EQ(initTimeTable, timeTable);
236
237 EXPECT_EQ(getContinuousIndexByTimeID(timeTable, 1, 3), 3);
238 EXPECT_EQ(getContinuousIndexByTimeID(timeTable, 10, 3), 0);
239 EXPECT_EQ(getContinuousIndexByTimeID(timeTable, 27, 3), 0);
240
241 }
242
244 TEST_F(TimeLineTest, PayloadTable)
245 {
246
247 getPayloadByContinuousIndex(payloadsTable, 1, 3).second->setGlobalParam(42., 0, 1);
248 EXPECT_EQ(getPayloadByContinuousIndex(payloadsTable, 1, 3).second->getGlobalParam(0, 1), 42.);
249
250
251 EXPECT_EQ(getPayloadByContinuousIndex(payloadsTable, 1, 0).first, EventMetaData(0, 0, 0));
252 EXPECT_EQ(getPayloadByContinuousIndex(payloadsTable, 1, 1).first, EventMetaData(0, 1, 0));
253 EXPECT_EQ(getPayloadByContinuousIndex(payloadsTable, 1, 2).first, EventMetaData(0, 2, 0));
254 EXPECT_EQ(getPayloadByContinuousIndex(payloadsTable, 1, 3).first, EventMetaData(530532, 2, 0));
255 EXPECT_EQ(getPayloadByContinuousIndex(payloadsTable, 1, 4).first, EventMetaData(0, 3, 0));
256 EXPECT_EQ(getPayloadByContinuousIndex(payloadsTable, 1, 5).first, EventMetaData(0, 4, 0));
257
258 EXPECT_EQ(getPayloadByContinuousIndex(payloadsTable, 10, 0).first, EventMetaData(0, 0, 0));
259 EXPECT_EQ(getPayloadByContinuousIndex(payloadsTable, 10, 1).first, EventMetaData(0, 3, 0));
260
261 EXPECT_EQ(getPayloadByContinuousIndex(payloadsTable, 27, 0).first, EventMetaData(0, 0, 0));
262
263
264 }
265
267 TEST_F(TimeLineTest, TimeTable2PayloadsTable)
268 {
269 GlobalParamVector vector({"BeamSpot", "VXDAlignment", "CDCAlignment"});
270 GlobalCalibrationManager::initGlobalVector(vector);
271
272 int timeid = 0;
273 EventMetaData ev;
274
275 // vxd
276 timeid = 0;
277 ev = gotoNextChangeInRun(timeTable, 10, timeid);
278 EXPECT_EQ(timeid, 0);
279 EXPECT_EQ(ev, EventMetaData(0, 0, 0));
280
281 timeid = 1;
282 ev = gotoNextChangeInRun(timeTable, 10, timeid);
283 EXPECT_EQ(timeid, 1);
284 EXPECT_EQ(ev, EventMetaData(0, 1, 0));
285
286 timeid = 4;
287 ev = gotoNextChangeInRun(timeTable, 10, timeid);
288 EXPECT_EQ(timeid, 4);
289 EXPECT_EQ(ev, EventMetaData(0, 3, 0));
290
291 timeid = 5;
292 ev = gotoNextChangeInRun(timeTable, 10, timeid);
293 EXPECT_EQ(timeid, 5);
294 EXPECT_EQ(ev, EventMetaData(0, 4, 0));
295
296 // beam
297 timeid = 0;
298 ev = gotoNextChangeInRun(timeTable, 1, timeid);
299 EXPECT_EQ(timeid, 0);
300 EXPECT_EQ(ev, EventMetaData(0, 0, 0));
301
302 timeid = 1;
303 ev = gotoNextChangeInRun(timeTable, 1, timeid);
304 EXPECT_EQ(timeid, 1);
305 EXPECT_EQ(ev, EventMetaData(0, 1, 0));
306
307 timeid = 2;
308 ev = gotoNextChangeInRun(timeTable, 1, timeid);
309 EXPECT_EQ(timeid, 3);
310 EXPECT_EQ(ev, EventMetaData(530532, 2, 0));
311
312 timeid = 3;
313 ev = gotoNextChangeInRun(timeTable, 1, timeid);
314 EXPECT_EQ(timeid, 3);
315 EXPECT_EQ(ev, EventMetaData(530532, 2, 0));
316
317 timeid = 4;
318 ev = gotoNextChangeInRun(timeTable, 1, timeid);
319 EXPECT_EQ(timeid, 4);
320 EXPECT_EQ(ev, EventMetaData(0, 3, 0));
321
322 timeid = 5;
323 ev = gotoNextChangeInRun(timeTable, 1, timeid);
324 EXPECT_EQ(timeid, 5);
325 EXPECT_EQ(ev, EventMetaData(0, 4, 0));
326
327
328
329 timeid = 0;
330 ev = gotoNextChangeRunWise(timeTable, 1, timeid);
331 EXPECT_EQ(timeid, 0);
332 EXPECT_EQ(ev, EventMetaData(0, 1, 0));
333
334 timeid = 1;
335 ev = gotoNextChangeRunWise(timeTable, 1, timeid);
336 EXPECT_EQ(timeid, 1);
337 EXPECT_EQ(ev, EventMetaData(0, 2, 0));
338
339 timeid = 2;
340 ev = gotoNextChangeRunWise(timeTable, 1, timeid);
341 EXPECT_EQ(timeid, 3);
342 EXPECT_EQ(ev, EventMetaData(0, 3, 0));
343
344 timeid = 3;
345 ev = gotoNextChangeRunWise(timeTable, 1, timeid);
346 EXPECT_EQ(timeid, 3);
347 EXPECT_EQ(ev, EventMetaData(0, 3, 0));
348
349 timeid = 4;
350 ev = gotoNextChangeRunWise(timeTable, 1, timeid);
351 EXPECT_EQ(timeid, 4);
352 EXPECT_EQ(ev, EventMetaData(0, 4, 0));
353
354 timeid = 5;
355 ev = gotoNextChangeRunWise(timeTable, 1, timeid);
356 EXPECT_EQ(timeid, 5);
357 EXPECT_EQ(ev, EventMetaData(0, 4, 0));
358
359
360
361 timeid = 0;
362 ev = gotoNextChangeRunWise(timeTable, 10, timeid);
363 EXPECT_EQ(timeid, 3);
364 EXPECT_EQ(ev, EventMetaData(0, 3, 0));
365 ev = gotoNextChangeRunWise(timeTable, 10, timeid);
366 EXPECT_EQ(timeid, 3);
367 EXPECT_EQ(ev, EventMetaData(0, 3, 0));
368
369 timeid = 4;
370 ev = gotoNextChangeRunWise(timeTable, 10, timeid);
371 EXPECT_EQ(timeid, 5);
372 EXPECT_EQ(ev, EventMetaData(0, 4, 0));
373
374
375
376
377 auto table = TimeIdsTable2PayloadsTable(timeTable, vector);
378
379
380 EXPECT_EQ(getPayloadByContinuousIndex(table, 27, 0).first.getExperiment(), getPayloadByContinuousIndex(payloadsTable, 27,
381 0).first.getExperiment());
382
383
384 B2INFO(table.at(27).at(0).second.at(0).first.getEvent() << " " << table.at(27).at(0).second.at(0).first.getRun() << " " << table.at(
385 27).at(0).second.at(0).first.getExperiment());
386 EXPECT_EQ(table.at(27).at(0).second.at(0).first, payloadsTable.at(27).at(0).second.at(0).first);
387 B2INFO(table.at(27).at(0).second.at(0).second->getGlobalUniqueID());
388 EXPECT_EQ(table.at(27).at(0).second.at(0).second->getGlobalUniqueID(),
389 payloadsTable.at(27).at(0).second.at(0).second->getGlobalUniqueID());
390
391 B2INFO(table.at(10).at(0).second.at(0).first.getEvent() << " " << table.at(10).at(0).second.at(0).first.getRun() << " " << table.at(
392 10).at(0).second.at(0).first.getExperiment());
393 EXPECT_EQ(table.at(10).at(0).second.at(0).first, payloadsTable.at(10).at(0).second.at(0).first);
394 B2INFO(table.at(10).at(0).second.at(0).second->getGlobalUniqueID());
395 EXPECT_EQ(table.at(10).at(0).second.at(0).second->getGlobalUniqueID(),
396 payloadsTable.at(10).at(0).second.at(0).second->getGlobalUniqueID());
397
398 B2INFO(table.at(1).at(0).second.at(0).first.getEvent() << " " << table.at(1).at(0).second.at(0).first.getRun() << " " << table.at(
399 1).at(0).second.at(0).first.getExperiment());
400 EXPECT_EQ(table.at(1).at(0).second.at(0).first, payloadsTable.at(1).at(0).second.at(0).first);
401 B2INFO(table.at(1).at(0).second.at(0).second->getGlobalUniqueID());
402 EXPECT_EQ(table.at(1).at(0).second.at(0).second->getGlobalUniqueID(),
403 payloadsTable.at(1).at(0).second.at(0).second->getGlobalUniqueID());
404
405 B2INFO(table.at(1).at(2).second.at(1).first.getEvent() << " " << table.at(1).at(2).second.at(1).first.getRun() << " " << table.at(
406 1).at(2).second.at(1).first.getExperiment());
407 EXPECT_EQ(table.at(1).at(2).second.at(1).first, payloadsTable.at(1).at(2).second.at(1).first);
408 B2INFO(table.at(1).at(2).second.at(1).second->getGlobalUniqueID());
409 EXPECT_EQ(table.at(1).at(2).second.at(1).second->getGlobalUniqueID(),
410 payloadsTable.at(1).at(2).second.at(1).second->getGlobalUniqueID());
411
412
413 }
414
416 TEST_F(TimeLineTest, GlobalParamTimeLine)
417 {
418
419 GlobalParamVector vector({"BeamSpot", "VXDAlignment", "CDCAlignment"});
420 GlobalCalibrationManager::initGlobalVector(vector);
421 GlobalLabel label;
422 GlobalParamTimeLine timeline(eventHeader, label, vector);
423
424 timeline.loadFromDB();
425
427 auto beamX0 = GlobalLabel::construct<BeamSpot>(0, 1).label();
428 timeline.updateGlobalParam(GlobalLabel(beamX0), 3.);
429
431 auto beamZ = GlobalLabel::construct<BeamSpot>(0, 3).label();
432 auto beamX = GlobalLabel::construct<BeamSpot>(0, 1).label();
433
435 auto yingZ = GlobalLabel::construct<VXDAlignment>(VxdID(1, 0, 0, 1).getID(), 3).label();
436
437
438 timeline.updateGlobalParam(GlobalLabel(beamZ), 42.);
439 timeline.updateGlobalParam(GlobalLabel(beamX), 43.);
440 timeline.updateGlobalParam(GlobalLabel(yingZ), 44.);
441
442 auto objects = timeline.releaseObjects();
443 EXPECT_EQ(objects.size(), 8);
444
445 for (auto iov_obj : objects)
446 Database::Instance().storeData(DataStore::objectName(iov_obj.second->IsA(), ""), iov_obj.second, iov_obj.first);
447
448 // Ok let's open these payload files manually to see what's in them but at least we use the framework functions to
449 // find the correct payload for any given iov
450 Conditions::TestingPayloadStorage payloads("testPayloads/TestDatabase.txt");
451 {
452 EventMetaData eventMetaData(0, 2);
453 Conditions::PayloadMetadata payloadInfo{"EventDependency"};
454 ASSERT_TRUE(payloads.get(eventMetaData, payloadInfo));
455 ASSERT_FALSE(payloadInfo.filename.empty());
456 TFile file(payloadInfo.filename.c_str());
457 ASSERT_TRUE(file.IsOpen());
458 auto evdep = (EventDependency*) file.Get("EventDependency");
459 ASSERT_TRUE(evdep);
460
461 auto beam = dynamic_cast<BeamSpot*>(evdep->getObject(EventMetaData(530532, 2, 0)));
462 EXPECT_EQ(beam->getIPPosition()[2], 42.);
463
464 beam = dynamic_cast<BeamSpot*>(evdep->getObject(EventMetaData(530532, 2, 0)));
465 EXPECT_EQ(beam->getIPPosition()[0], 43.);
466
467 beam = dynamic_cast<BeamSpot*>(evdep->getObject(EventMetaData(530532 - 1, 2, 0)));
468 EXPECT_EQ(beam->getIPPosition()[2], 0.);
469
470 beam = dynamic_cast<BeamSpot*>(evdep->getObject(EventMetaData(530532 - 1, 2, 0)));
471 EXPECT_EQ(beam->getIPPosition()[0], 0.);
472 }
473 {
474 EventMetaData eventMetaData(0, 3);
475 Conditions::PayloadMetadata payloadInfo{"BeamSpot"};
476 ASSERT_TRUE(payloads.get(eventMetaData, payloadInfo));
477 ASSERT_FALSE(payloadInfo.filename.empty());
478 TFile file(payloadInfo.filename.c_str());
479 ASSERT_TRUE(file.IsOpen());
480
481 auto beam2 = (BeamSpot*) file.Get("BeamSpot");
482 ASSERT_TRUE(beam2);
483 beam2->getIPPosition().Print();
484 EXPECT_EQ(beam2->getIPPosition()[0], 3.);
485 }
486 {
487 EventMetaData eventMetaData(0, 4);
488 Conditions::PayloadMetadata payloadInfo{"VXDAlignment"};
489 ASSERT_TRUE(payloads.get(eventMetaData, payloadInfo));
490 ASSERT_FALSE(payloadInfo.filename.empty());
491 TFile file(payloadInfo.filename.c_str());
492 ASSERT_TRUE(file.IsOpen());
493
494 auto vxd = (VXDAlignment*) file.Get("VXDAlignment");
495 ASSERT_TRUE(vxd);
496 EXPECT_EQ(vxd->getGlobalParam(VxdID(1, 0, 0, 1).getID(), 3), 44.);
497 }
498 // --------------------------------------------------------------------
499 }
500} // namespace
This class contains the beam spot position and size modeled as a gaussian distribution in space.
Definition: BeamSpot.h:22
static unsigned short getGlobalUniqueID()
Return unique ID of BeamSpot in global Millepede calibration (1)
Definition: BeamSpot.h:91
static unsigned short getGlobalUniqueID()
Get global unique id.
Definition: CDCAlignment.h:109
static Configuration & getInstance()
Get a reference to the instance which will be used when the Database is initialized.
Class to store and retrieve temporary payloads.
static std::string objectName(const TClass *t, const std::string &name)
Return the storage name for an object of the given TClass and name.
Definition: DataStore.cc:151
static DataStore & Instance()
Instance of singleton Store.
Definition: DataStore.cc:54
void setInitializeActive(bool active)
Setter for m_initializeActive.
Definition: DataStore.cc:94
void reset(EDurability durability)
Frees memory occupied by data store items and removes all objects from the map.
Definition: DataStore.cc:86
Class for handling changing conditions as a function of event number.
Store event, run, and experiment numbers.
Definition: EventMetaData.h:33
Class to convert to/from global labels for Millepede II to/from detector & parameter identificators.
Definition: GlobalLabel.h:41
static void clearTimeDependentParamaters()
Forget all previously registered time dependent parameters.
Definition: GlobalLabel.cc:66
static void setCurrentTimeInterval(gidTYPE time)
Sets current time id.
Definition: GlobalLabel.h:207
A class that describes the interval of experiments/runs for which an object in the database is valid.
bool registerInDataStore(DataStore::EStoreFlags storeFlags=DataStore::c_WriteOut)
Register the object/array in the DataStore.
Type-safe access to single objects in the data store.
Definition: StoreObjPtr.h:96
bool construct(Args &&... params)
Construct an object of type T in this StoreObjPtr, using the provided constructor arguments.
Definition: StoreObjPtr.h:119
VXD alignment (and maybe some calibration) parameters.
Definition: VXDAlignment.h:19
static unsigned short getGlobalUniqueID()
Get global unique id.
Definition: VXDAlignment.h:47
Class to uniquely identify a any structure of the PXD and SVD.
Definition: VxdID.h:33
static Database & Instance()
Instance of a singleton Database.
Definition: Database.cc:42
bool storeData(const std::string &name, TObject *object, const IntervalOfValidity &iov)
Store an object in the database.
Definition: Database.cc:141
static void reset(bool keepConfig=false)
Reset the database instance.
Definition: Database.cc:50
int getID(const std::vector< double > &breaks, double t)
get id of the time point t
Definition: calibTools.h:60
Abstract base class for different kinds of events.
STL namespace.
Simple struct to group all information necessary for a single payload.