Belle II Software development
python_dbinterface.py
1
8from b2test_utils import configure_logging_for_tests
9import ROOT
10from ROOT import Belle2
11import multiprocessing
12import unittest
13
14# @cond internal_test
15
16
17class DBInterface(unittest.TestCase):
18 def assertDeath(self, function, *args, **kwargs):
19 """Run function in child process and check if it died. Only way to check for B2FATAL"""
20 ctx = multiprocessing.get_context("fork")
21 p = ctx.Process(target=function, args=args, kwargs=kwargs)
22 p.start()
23 p.join()
24 self.assertNotEqual(p.exitcode, 0)
25
26 def setUp(self):
27 configure_logging_for_tests()
28
29 def tearDown(self):
31
32 def test_obj(self):
33 """Test object interface"""
34 bp = Belle2.PyDBObj("BeamParameters")
35 # no such thing, should be invalid
36 self.assertFalse(bp.isValid())
37
38 # create a mock dbstore entry
39 override_object = Belle2.BeamParameters()
40 vertex = ROOT.Math.XYZVector(1, 2, 3)
41 # sadly root does not handle the overloads correctly so we don't have an
42 # overload without the covariance matrix
43 override_object.setVertex(vertex, ROOT.std.vector("double")())
44 Belle2.DBStore.Instance().addConstantOverride("BeamParameters", override_object, False)
45
46 # must be valid now
47 self.assertTrue(bp.isValid())
48
49 # make sure type checking works: no access array<->obj or with wrong
50 # type or both
51 self.assertDeath(lambda: Belle2.PyDBObj("BeamParameters", Belle2.PXDSimHit.Class()))
52 self.assertDeath(lambda: Belle2.PyDBArray("BeamParameters"))
53 self.assertDeath(lambda: Belle2.PyDBArray("BeamParameters", Belle2.PXDSimHit.Class()))
54
55 # check for correct vertex
56 self.assertEqual(bp.obj().getVertex(), vertex)
57 # and also "indirection" should work
58 self.assertEqual(bp.getVertex(), vertex)
59 # and it should be read only
60 with self.assertRaises(AttributeError):
61 # aka no setters
62 bp.obj().setVertex()
63
64 # but static members like Class should work
65 bp.obj().Class()
66
67 # make a copy
68 copy = Belle2.BeamParameters(bp.obj())
69 # and compare
70 self.assertEqual(copy, bp.obj())
71
72 def test_array(self):
73 bplist = Belle2.PyDBArray("BeamParameterList")
74 # no such thing, should be invalid
75 self.assertFalse(bplist.isValid())
76 # create a mock dbstore entry
77 override_array = ROOT.TClonesArray("Belle2::BeamParameters")
78 override_array.ExpandCreate(3)
79 for i in range(override_array.GetEntriesFast()):
80 override_array[i].setVertex(ROOT.Math.XYZVector(i, i, i), ROOT.std.vector("double")())
81
82 # must be valid now
83 Belle2.DBStore.Instance().addConstantOverride("BeamParameterList", override_array, False)
84
85 self.assertTrue(bplist.isValid())
86 # make sure type checking works: no access array<->obj or with wrong
87 # type or both
88 self.assertDeath(lambda: Belle2.PyDBArray("BeamParameterList", Belle2.PXDSimHit.Class()))
89 self.assertDeath(lambda: Belle2.PyDBObj("BeamParameterList"))
90 self.assertDeath(lambda: Belle2.PyDBObj("BeamParameterList", Belle2.PXDSimHit.Class()))
91 # make sure the number of entries is consistent
92 self.assertEqual(len(bplist), override_array.GetEntriesFast())
93
94 # try looping over this and keep track about the number of elements
95 for i, e in enumerate(bplist):
96 # make sure the vertex is what we put in
97 self.assertEqual(int(e.getVertex().X()), i)
98 # and that it's in general equal to the original one
99 self.assertEqual(e, override_array[i])
100 # and itself :D
101 self.assertEqual(e, bplist[i])
102 # and to a copy
103 copy = Belle2.BeamParameters(e)
104 self.assertEqual(e, copy)
105 # unless we change the copy
106 copy.setVertex(ROOT.Math.XYZVector(1, 2, 3), ROOT.std.vector("double")())
107 self.assertNotEqual(e, copy)
108 # and check that modification is blocked
109 with self.assertRaises(AttributeError):
110 e.setVertex(ROOT.Math.XYZVector(1, 2, 3), ROOT.std.vector("double")())
111
112 self.assertEqual(i + 1, len(bplist))
113
114 # make sure item assignment is off
115 with self.assertRaises(TypeError):
116 bplist[0] = Belle2.BeamParameters()
117
118
119if __name__ == "__main__":
120 unittest.main()
121
122# @endcond
This class contains the nominal beam parameters and the parameters used for smearing of the primary v...
Class to access a DB Array from Python.
Definition: PyDBArray.h:43
Class to access a DBObjPtr from Python.
Definition: PyDBObj.h:50
static DBStore & Instance()
Instance of a singleton DBStore.
Definition: DBStore.cc:28