Belle II Software development
mixins.py
1#!/usr/bin/env python3
2
3
10
11from tracking.run.minimal import EmptyRun
12import tracking.root_utils as root_utils
13
14import logging
15
16
17def get_logger():
18 return logging.getLogger(__name__)
19
20
22 pass
23
24
25class PostProcessingRunMixin(EmptyRun):
26 """Post-process the basf2 job output"""
27
28
29 postprocess_only = False
30
31 def create_argument_parser(self, **kwds):
32 """Parse the command-line post-processing arguments"""
33 argument_parser = super().create_argument_parser(**kwds)
34 postprocess_argument_group = argument_parser.add_argument_group("Postprocessing arguments")
35 postprocess_argument_group.add_argument(
36 '-po',
37 '--postprocess-only',
38 action='store_true',
39 default=self.postprocess_only,
40 dest='postprocess_only',
41 help='Only run the post processing of this run.',)
42
43 return argument_parser
44
45 def run(self, path):
46 """Post-process the basf2 job output"""
47 if not self.postprocess_only:
48 super().run(path)
49
50 self.postprocess()
51
52 def postprocess(self):
53 """By default, do nothing. (may be overridden)"""
54
55
56class BrowseTFileOnTerminateRunMixin(PostProcessingRunMixin):
57 """Browse interactively the basf2 job output"""
58
59
60 output_file_name = None
61
62 show_results = False
63
64 def create_argument_parser(self, **kwds):
65 """Parse the command-line TFile-browsing arguments"""
66 argument_parser = super().create_argument_parser(**kwds)
67
68 postprocess_argument_group = argument_parser
69 for group in argument_parser._action_groups:
70 if group.title == "Postprocessing arguments":
71 postprocess_argument_group = group
72 break
73
74 postprocess_argument_group.add_argument(
75 '-s',
76 '--show',
77 action='store_true',
78 default=self.show_results,
79 dest='show_results',
80 help='Show generated plots in a TBrowser immediatly.',)
81
82 return argument_parser
83
84 def postprocess(self):
85 """Browse the TFile interactively"""
86 if self.show_results and self.output_file_name:
87 with root_utils.root_open(self.output_file_name) as tfile:
88 root_utils.root_browse(tfile)
89 input("Close with return key.")
90
91 super().postprocess()
92
93
95 """Configure for basf2 job output ROOT TFile"""
96
97
98 root_output_file = None
99
100 def create_argument_parser(self, **kwds):
101 """Parse the command-line output-file-specification argument"""
102 argument_parser = super().create_argument_parser(**kwds)
103 argument_parser.add_argument(
104 'root_output_file',
105 help='Output file to which the simulated events shall be written.'
106 )
107
108 return argument_parser
109
110 def create_path(self):
111 """Create a new basf2 path and add the RootOutput module to it"""
112 path = super().create_path()
113
114 path.add_module(
115 'RootOutput',
116 outputFileName=self.root_output_file
117 )
118
119 return path
None output_file_name
There is no default for the name of the output TFile.
Definition: mixins.py:60
bool show_results
By default, do not show the browsing results.
Definition: mixins.py:62
def create_argument_parser(self, **kwds)
Definition: mixins.py:31
bool postprocess_only
By default, browse the output TFile too.
Definition: mixins.py:29
def create_argument_parser(self, **kwds)
Definition: mixins.py:100
None root_output_file
There is no default for the name of the output TFile.
Definition: mixins.py:98