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