Belle II Software  release-06-02-00
test_validation_metadata_tools.py
1 #!/usr/bin/env python3
2 
3 
10 
11 # std
12 import unittest
13 import tempfile
14 import pathlib
15 
16 # 3rd
17 import ROOT
18 import basf2
19 import modularAnalysis
20 import generators as ge
21 
22 # ours
23 from validation_tools.metadata import create_validation_histograms
24 from validationplots import get_metadata
25 
26 
27 TRIVIAL_DECFILE = """
28 Alias MyB+ B+
29 Alias MyB- B-
30 
31 Decay Upsilon(4S)
32 1.0 MyB+ MyB- VSS;
33 Enddecay
34 
35 End
36 """
37 
38 
39 class TestValidationMetadataSetter(unittest.TestCase):
40  """ This test tests the ValidationMetadataSetter module via its interface
41  :func:`validation_tools.metadata.create_validation_histograms`
42  """
43 
44  def setUp(self):
45  """ Open temporary directory to work in. """
46 
47  self.tmp_dirtmp_dir = tempfile.TemporaryDirectory()
48 
49  def tearDown(self):
50  """ Clean up temporary directory """
51  self.tmp_dirtmp_dir.cleanup()
52 
53  def test(self):
54  """ Perform tests """
55  tmp_dir_path = pathlib.Path(self.tmp_dirtmp_dir.name)
56 
57  dec_path = tmp_dir_path / "test_y4s_trivial.dec"
58  with dec_path.open("w") as decfile:
59  decfile.write(TRIVIAL_DECFILE)
60 
61  # Run basf2
62  # ----------------------------------------------------------------------
63 
64  path = basf2.create_path()
65  modularAnalysis.setupEventInfo(noEvents=10, path=path)
66  ge.add_evtgen_generator(
67  path=path,
68  finalstate="signal",
69  signaldecfile=basf2.find_file(str(dec_path)),
70  )
71 
73 
74  modularAnalysis.fillParticleListFromMC("Upsilon(4S)", "", path=path)
75 
76  out_file_path = tmp_dir_path / "out.root"
77  create_validation_histograms(
78  path,
79  out_file_path,
80  "Upsilon(4S)",
81  variables_1d=[
82  (
83  "M",
84  100,
85  5,
86  15,
87  "mass",
88  "me <wontreply@dont.try>",
89  "description of M",
90  "nothing to check",
91  "x label",
92  )
93  ],
94  variables_2d=[
95  (
96  "M",
97  100,
98  5,
99  15,
100  "M",
101  100,
102  5,
103  15,
104  "mass vs mass",
105  "me <wontreply@dont.try>",
106  "some description nobody reads",
107  "nothing to check",
108  "x label",
109  "why label?",
110  "mop1, mop2",
111  )
112  ],
113  description="Overall description of plots in this package.",
114  )
115 
116  basf2.process(path=path)
117 
118  # Test outcome
119  # ----------------------------------------------------------------------
120 
121  tf = ROOT.TFile(str(out_file_path))
122 
123  # Overall
124  # *******
125 
126  d = tf.Get("Description")
127  self.assertEqual(
128  d.GetTitle(), "Overall description of plots in this package."
129  )
130 
131  # 1D Histogram
132  # ************
133 
134  md = get_metadata(tf.Get("M"))
135  self.assertEqual(md["description"], "description of M")
136  self.assertEqual(md["check"], "nothing to check")
137  self.assertEqual(md["metaoptions"], [])
138  self.assertEqual(md["contact"], "me <wontreply@dont.try>")
139 
140  # 2D Histogram
141  # ************
142 
143  md = get_metadata(tf.Get("MM"))
144  self.assertEqual(md["description"], "some description nobody reads")
145  self.assertEqual(md["check"], "nothing to check")
146  self.assertEqual(md["metaoptions"], ["mop1", "mop2"])
147  self.assertEqual(md["contact"], "me <wontreply@dont.try>")
148 
149 
150 if __name__ == "__main__":
151  unittest.main()
def loadGearbox(path, silence_warning=False)
def fillParticleListFromMC(decayString, cut, addDaughters=False, skipNonPrimaryDaughters=False, writeOut=False, path=None)
def setupEventInfo(noEvents, path)