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