Belle II Software  release-08-01-10
training.py
1 
8 
9 import os
10 import os.path
11 import shutil
12 import subprocess
13 
14 from tracking.run.mixins import BrowseTFileOnTerminateRunMixin
15 from tracking.run.mixins import PostProcessingRunMixin
16 from tracking.run.utilities import NonstrictChoices
17 
18 
20  """Prepare and execute a basf2 job to train neural network, postprocess, and inspect"""
21 
22 
23  task = "train"
24 
25 
26  variables = None
27 
28 
29  groupby = None
30 
31 
32  auxiliaries = None
33 
34 
35  truth = "truth"
36 
37  @property
38  def identifier(self):
39  """Database identifier of the filte being trained"""
40  return "trackfindingcdc_" + self.__class__.__name__[:-len("TrainingRun")]
41 
42  @property
43  def sample_file_name(self):
44  """File name of the recorded sample to be trained on
45 
46  Defaults to the class name minus the mandatory TrainingRun postfix
47  """
48  return self.__class__.__name__[:-len("TrainingRun")] + '_' + self.tasktasktask + '.root'
49 
50  def create_argument_parser(self, **kwds):
51  """Create argument parser"""
52  argument_parser = super().create_argument_parser(**kwds)
53 
54  argument_parser.add_argument(
55  "--task",
56  choices=NonstrictChoices(["train", "eval", "explore", ]),
57  default=self.tasktasktask,
58  dest="task",
59  help=("Select a prepared recording task")
60  )
61 
62  return argument_parser
63 
64  def postprocess(self):
65  """Run the training as post-processing job
66 
67  To run only the training run with --postprocess-only
68  """
69 
70  if self.tasktasktask == "train":
71  cmd = [
72  "trackfindingcdc_teacher",
73  ]
74 
75  if self.variablesvariables:
76  cmd += ["--variables"]
77  cmd += self.variablesvariables
78 
79  cmd += [
80  "--identifier=" + self.identifieridentifier,
81  "--truth=" + self.truthtruth,
82  self.sample_file_namesample_file_name,
83  ]
84  print("Running", cmd)
85  subprocess.call(cmd)
86 
87  # Move training file to the right location
88  if self.identifieridentifier.endswith(".xml"):
89  tracking_data_dir_path = os.path.join(os.environ["BELLE2_LOCAL_DIR"], "tracking", "data")
90  shutil.copy(self.identifieridentifier, tracking_data_dir_path)
91 
92  else:
93  cmd = [
94  "trackfindingcdc-classification-overview",
95  ]
96 
97  if self.variablesvariables:
98  cmd += ["-v"]
99  cmd += self.variablesvariables
100 
101  if self.groupbygroupby is not None:
102  cmd += ["-g"]
103  if isinstance(self.groupbygroupby, str):
104  cmd += [self.groupbygroupby]
105  else:
106  cmd += self.groupbygroupby
107 
108  if self.auxiliariesauxiliaries is not None:
109  cmd += ["-a"]
110  if isinstance(self.auxiliariesauxiliaries, str):
111  cmd += [self.auxiliariesauxiliaries]
112  else:
113  cmd += self.auxiliariesauxiliaries
114 
115  cmd += [
116  "--truth=" + self.truthtruth,
117  self.sample_file_namesample_file_name,
118  ]
119  print("Running", cmd)
120  subprocess.call(cmd)
121 
122  self.output_file_nameoutput_file_nameoutput_file_name = self.sample_file_namesample_file_name[:-len(".root")] + ".overview.root"
123 
124  super().postprocess()
output_file_name
There is no default for the name of the output TFile.
Definition: mixins.py:61
string truth
Truth variable name.
Definition: training.py:35
def create_argument_parser(self, **kwds)
Definition: training.py:50
def sample_file_name(self)
Definition: training.py:43
variables
Input variable for the training or the classification analysis.
Definition: training.py:26
output_file_name
Set file name for the TBrowser to show if demanded.
Definition: training.py:122
groupby
Input groupby the classification analysis.
Definition: training.py:29
string task
Recording / training task selected.
Definition: training.py:23
task
Process each event according to the user's desired task (train, eval, explore)
Definition: training.py:70
auxiliaries
Input auxiliaries the classification analysis.
Definition: training.py:32