Belle II Software  release-08-01-10
minimal.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 
11 
12 
13 import basf2
14 
15 from tracking.run import utilities
16 
17 import argparse
18 import logging
19 from ROOT import Belle2
20 
21 
22 def get_logger():
23  return logging.getLogger(__name__)
24 
25 # Provides a commandline interface to specify some parameters to an associated basf2 path execution
26 
27 
28 class EmptyRun(object):
29  """Configure for a bare-bones basf2 job"""
30 
31  # Declarative section which can be redefined in a subclass
32 
33 
34  description = "Empty execution of basf2"
35 
36  def __init__(self, **kwds):
37  """Constructor"""
38  if kwds:
39  raise ValueError("Received_unknown unknown argument")
40 
41  @property
42  def name(self):
43  """provide name of this object"""
44  return self.__class__.__name__
45 
47  """Configure basf2 job script from command-line arguments then run it"""
48  self.configure_from_commandlineconfigure_from_commandline()
49  self.executeexecute()
50 
51  def execute(self):
52  """Create the basf2 path then run the job"""
53  # Create path and run #
54 
55  path = self.create_pathcreate_path()
56  self.adjust_pathadjust_path(path)
57  self.runrun(path)
58 
59  def run(self, path):
60  """Process the basf2 path"""
61  # Run basf2 module path #
62 
63  get_logger().info('Start processing')
64  basf2.print_path(path)
65  basf2.process(path)
66  get_logger().info("\n%s", str(basf2.statistics))
67 
69  """Convert the command-line arguments to a basf2 job script"""
70  argument_parser = self.create_argument_parsercreate_argument_parser()
71  arguments = argument_parser.parse_args()
72  self.configureconfigure(arguments)
73 
74  def configure(self, arguments):
75  """Save the command-line arguments as key-value pairs"""
76  # Simply translate the arguments that have
77  # the same name as valid instance arguments
78  for (key, value) in list(vars(arguments).items()):
79  if value is None:
80  continue
81  if hasattr(self, key):
82  get_logger().info("Setting %s to %s", key, value)
83  setattr(self, key, value)
84 
85  def create_argument_parser(self, **kwds):
86  """Parse the command-line arguments to a basf2 job script"""
87  argument_parser = utilities.ArgumentParser(description=self.descriptiondescription, **kwds)
88  return argument_parser
89 
90  def create_path(self):
91  """Create a new basf2 path"""
92  path = basf2.create_path()
93  return path
94 
95  def adjust_path(self, path):
96  """Hook that gives the opportunity to check the path for consistency before processing it"""
97 
98 # Minimal run stub defining some general parameters
99 
100 
101 class MinimalRun(EmptyRun):
102  """Configure for a minimal basf2 job"""
103 
104 
105  description = "Minimally populated execution of basf2"
106 
107  # Declarative section which can be redefined in a subclass
108 
109 
110  allow_input = True
111 
112  n_events = 10000
113 
114  root_input_file = None
115 
116  random_seed = None
117 
118  n_processes = 0
119 
120  n_events_to_skip = 0
121 
122  def create_argument_parser(self, **kwds):
123  """Convert command-line arguments to basf2 argument list"""
124  argument_parser = super().create_argument_parser(**kwds)
125  master_argument_group = argument_parser.add_argument_group("Master arguments")
126 
127  if self.allow_inputallow_input:
128  master_argument_group.add_argument(
129  '-i',
130  '--input',
131  default=argparse.SUPPRESS,
132  dest='root_input_file',
133  help='File path to the ROOT file from which the simulated events shall be loaded.'
134  )
135 
136  master_argument_group.add_argument(
137  '-n',
138  '--events',
139  dest='n_events',
140  default=self.n_eventsn_events,
141  type=int,
142  help='Number of events to be generated',
143  )
144 
145  master_argument_group.add_argument(
146  '-r',
147  '--random-seed',
148  dest='random_seed',
149  default=argparse.SUPPRESS,
150  type=str,
151  help='The random number generator seed to be set before the processing starts.',
152  )
153 
154  master_argument_group.add_argument(
155  '-p',
156  '--processes',
157  dest='n_processes',
158  default=argparse.SUPPRESS,
159  type=int,
160  help='The number of parallel processes to be used for processing.',
161  )
162 
163  master_argument_group.add_argument(
164  '--n-events-to-skip',
165  dest='n_events_to_skip',
166  default=argparse.SUPPRESS,
167  type=int,
168  help='The number of events to skip',
169  )
170 
171  return argument_parser
172 
173  def create_path(self):
174  """Create and configure the basf2 path"""
175  path = super().create_path()
176 
177  if self.random_seedrandom_seed is not None:
178  basf2.set_random_seed(self.random_seedrandom_seed)
179 
180  environment = Belle2.Environment.Instance()
181  environment.setNumberEventsOverride(self.n_eventsn_events)
182 
183  # If there is no input file is the EventInfoSetter as master module
184  if not self.root_input_fileroot_input_file:
185  # Master module: EventInfoSetter
186  path.add_module('EventInfoSetter',
187  evtNumList=[self.n_eventsn_events],
188  runList=[1],
189  expList=[0],
190  skipNEvents=self.n_events_to_skipn_events_to_skip
191  )
192 
193  else:
194  # Master module: RootInput
195  path.add_module('RootInput',
196  inputFileName=self.root_input_fileroot_input_file,
197  skipNEvents=self.n_events_to_skipn_events_to_skip)
198 
199  # Progress module
200  path.add_module('Progress')
201 
202  if self.n_processesn_processes:
203  environment = Belle2.Environment.Instance()
204  environment.setNumberProcessesOverride(self.n_processesn_processes)
205 
206  return path
static Environment & Instance()
Static method to get a reference to the Environment instance.
Definition: Environment.cc:28
def create_argument_parser(self, **kwds)
Definition: minimal.py:85
def run(self, path)
Definition: minimal.py:59
string description
Description of the run setup to be displayed on command line.
Definition: minimal.py:34
def configure(self, arguments)
Definition: minimal.py:74
def configure_from_commandline(self)
Definition: minimal.py:68
def __init__(self, **kwds)
Definition: minimal.py:36
def configure_and_execute_from_commandline(self)
Definition: minimal.py:46
def adjust_path(self, path)
Definition: minimal.py:95
def create_argument_parser(self, **kwds)
Definition: minimal.py:122
int n_events
By default, process 10000 events.
Definition: minimal.py:112
int n_events_to_skip
By default, do not skip any events at the start of the input ROOT TFile.
Definition: minimal.py:120
root_input_file
By default, there is no input ROOT TFile.
Definition: minimal.py:114
random_seed
By default, the random-number seed is unassigned.
Definition: minimal.py:116
int n_processes
By default, no parallel processing.
Definition: minimal.py:118
bool allow_input
By default, this basf2 job can read events from an input ROOT TFile.
Definition: minimal.py:110