Belle II Software  release-05-01-25
run.py
1 import argparse
2 import pickle
3 
4 from tracking.run.event_generation import StandardEventGenerationRun
5 from tracking.run.mixins import BrowseTFileOnTerminateRunMixin, PostProcessingRunMixin
6 
7 
9  """Harvester to select crops, postprocess, and inspect"""
10 
11 
12  output_file_name = None
13 
14  def harvesting_module(self, path=None):
15  """This virtual method must be overridden by the inheriting class"""
16  raise RuntimeError("Override the harvesting_module method")
17 
18  def create_argument_parser(self, **kwds):
19  """Parse the arguments and append them to the harvester's list"""
20  argument_parser = super().create_argument_parser(**kwds)
21  harvesting_argument_group = argument_parser.add_argument_group("Harvest arguments")
22 
23  harvesting_argument_group.add_argument(
24  "-o",
25  "--output",
26  dest="output_file_name",
27  default=argparse.SUPPRESS,
28  help="File name for the harvest products"
29  )
30 
31  return argument_parser
32 
33  def pickle_crops(self, harvesting_module, crops, **kwds):
34  """Save the raw crops as a pickle file"""
35  with open(self.output_file_name + ".pickle", "wb") as pickle_file:
36  pickle.dump(crops, pickle_file)
37 
38  def unpickle_crops(self):
39  """Load the raw crops from a pickle file"""
40  with open(self.output_file_name + ".pickle", "rb") as pickle_file:
41  return pickle.load(pickle_file)
42 
43  def postprocess(self):
44  """Post-process the crops"""
45  if self.postprocess_only:
46  harvesting_module = self.harvesting_module()
47  if self.output_file_name:
48  harvesting_module.output_file_name = self.output_file_name
49  try:
50  crops = self.unpickle_crops()
51  except FileNotFoundError:
52  print("Crops pickle file not found. Create it now.")
53  else:
54  harvesting_module.refine(crops)
55 
56  super().postprocess()
57 
58  def adjust_path(self, path):
59  """Add the harvester to the basf2 path"""
60  super().adjust_path(path)
61  harvesting_module = self.harvesting_module()
62  if self.output_file_name:
63  harvesting_module.output_file_name = self.output_file_name
64  harvesting_module.refiners.append(self.pickle_crops)
65  path.add_module(harvesting_module)
66  return path
67 
68 
70  """Harvester to generate MC events followed by crop selection, postprocessing, inspection"""
71  pass
tracking.harvest.run.HarvestingRunMixin.unpickle_crops
def unpickle_crops(self)
Definition: run.py:38
tracking.run.mixins
Definition: mixins.py:1
tracking.run.mixins.PostProcessingRunMixin.postprocess_only
bool postprocess_only
By default, browse the output TFile too.
Definition: mixins.py:22
tracking.run.mixins.BrowseTFileOnTerminateRunMixin
Definition: mixins.py:50
tracking.run.mixins.PostProcessingRunMixin
Definition: mixins.py:18
tracking.harvest.run.HarvestingRunMixin.pickle_crops
def pickle_crops(self, harvesting_module, crops, **kwds)
Definition: run.py:33
tracking.run.event_generation.StandardEventGenerationRun
Definition: event_generation.py:188
tracking.harvest.run.HarvestingRunMixin.output_file_name
output_file_name
Disable the writing of an output ROOT file.
Definition: run.py:12
tracking.harvest.run.HarvestingRunMixin.harvesting_module
def harvesting_module(self, path=None)
Definition: run.py:14
tracking.harvest.run.HarvestingRunMixin.postprocess
def postprocess(self)
Definition: run.py:43
tracking.harvest.run.HarvestingRunMixin
Definition: run.py:8
tracking.harvest.run.HarvestingRunMixin.create_argument_parser
def create_argument_parser(self, **kwds)
Definition: run.py:18
tracking.run.event_generation
Definition: event_generation.py:1
tracking.harvest.run.HarvestingRun
Definition: run.py:69
tracking.harvest.run.HarvestingRunMixin.adjust_path
def adjust_path(self, path)
Definition: run.py:58