Belle II Software  release-08-01-10
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 
72  modularAnalysis.fillParticleListFromMC("Upsilon(4S)", "", path=path)
73 
74  out_file_path = tmp_dir_path / "out.root"
75  create_validation_histograms(
76  path,
77  out_file_path,
78  "Upsilon(4S)",
79  variables_1d=[
80  (
81  "M",
82  100,
83  5,
84  15,
85  "mass",
86  "me <wontreply@dont.try>",
87  "description of M",
88  "nothing to check",
89  "x label",
90  )
91  ],
92  variables_2d=[
93  (
94  "M",
95  100,
96  5,
97  15,
98  "M",
99  100,
100  5,
101  15,
102  "mass vs mass",
103  "me <wontreply@dont.try>",
104  "some description nobody reads",
105  "nothing to check",
106  "x label",
107  "why label?",
108  "mop1, mop2",
109  )
110  ],
111  description="Overall description of plots in this package.",
112  )
113 
114  basf2.process(path=path)
115 
116  # Test outcome
117  # ----------------------------------------------------------------------
118 
119  tf = ROOT.TFile(str(out_file_path))
120 
121  # Overall
122  # *******
123 
124  d = tf.Get("Description")
125  self.assertEqual(
126  d.GetTitle(), "Overall description of plots in this package."
127  )
128 
129  # 1D Histogram
130  # ************
131 
132  md = get_metadata(tf.Get("M"))
133  self.assertEqual(md["description"], "description of M")
134  self.assertEqual(md["check"], "nothing to check")
135  self.assertEqual(md["metaoptions"], [])
136  self.assertEqual(md["contact"], "me <wontreply@dont.try>")
137 
138  # 2D Histogram
139  # ************
140 
141  md = get_metadata(tf.Get("MM"))
142  self.assertEqual(md["description"], "some description nobody reads")
143  self.assertEqual(md["check"], "nothing to check")
144  self.assertEqual(md["metaoptions"], ["mop1", "mop2"])
145  self.assertEqual(md["contact"], "me <wontreply@dont.try>")
146 
147 
148 if __name__ == "__main__":
149  unittest.main()
def setupEventInfo(noEvents, path)
def fillParticleListFromMC(decayString, cut, addDaughters=False, skipNonPrimaryDaughters=False, writeOut=False, path=None, skipNonPrimary=False, skipInitial=True)