16from basf2
import find_file
17from b2test_utils
import configure_logging_for_tests
21 """Test to run all example scripts."""
23 @unittest.skipIf(not os.getenv('BELLE2_EXAMPLES_DATA_DIR'),
24 "$BELLE2_EXAMPLES_DATA_DIR not found.")
25 @unittest.skipIf(not os.getenv('BELLE2_VALIDATION_DATA_DIR'),
26 "$BELLE2_VALIDATION_DATA_DIR not found.")
29 Internal function to test a directory full of example scripts with an optional list of broken scripts to be skipped.
32 path_to_glob (str): the path to search
for scripts
33 broken (list(str)): (optional) scripts that are known to be broken
and can be skipped
37 configure_logging_for_tests()
38 all_egs = sorted(glob.glob(find_file(path_to_glob) + f
"/{filepattern}*.py"))
40 filename = os.path.basename(eg)
41 if filename
not in broken:
42 with self.subTest(msg=filename):
43 outputfilename = filename.replace(
'.py',
'.root')
44 result = subprocess.run([
'basf2',
'-n', f
'{nevents}', eg,
'-o',
45 outputfilename], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
46 if result.returncode != 0:
50 sys.stdout.buffer.write(result.stdout)
51 self.assertEqual(result.returncode, 0)
53 if os.path.exists(outputfilename)
and isNtuple(outputfilename):
54 scanTTree(outputfilename)
57def scanTTree(filename):
58 from ROOT
import TFile
60 tfile = TFile(filename,
"READ")
61 print(f
"TFile: {filename}")
64 ttrees = [key.GetName()
for key
in tfile.GetListOfKeys()
if key.GetClassName() ==
"TTree"]
66 for ttree_name
in ttrees:
67 if ttree_name ==
"persistent":
69 print(f
"TTree: {ttree_name}")
72 ttree = tfile.Get(ttree_name)
73 num_entries = ttree.GetEntries()
76 print(
"No entry found")
78 for branch
in ttree.GetListOfBranches():
79 branch_name = branch.GetName()
80 print(f
"TBranch: {branch_name}")
85 for branch
in ttree.GetListOfBranches():
86 branch_name = branch.GetName()
87 branch_value = getattr(ttree, branch_name)
89 if isinstance(branch_value, float):
90 print(f
"TBranch: {branch_name}, {branch_value:.4g}")
92 print(f
"TBranch: {branch_name}, {branch_value}")
97def isNtuple(filename):
98 from ROOT
import Belle2, TFile
100 tfile = TFile(filename,
"READ")
101 ttree = tfile.Get(
"persistent")
108 return metadata.getDataDescription()[
"isNtupleMetaData"]
def _test_examples_dir(self, path_to_glob, broken=None, filepattern="", nevents=10)