Belle II Software development
training.py
1
8
9import os
10import os.path
11import shutil
12import subprocess
13
14from tracking.run.mixins import BrowseTFileOnTerminateRunMixin
15from tracking.run.mixins import PostProcessingRunMixin
16from 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
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.tasktask + '.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.tasktask,
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.tasktask == "train":
71 cmd = [
72 "trackfindingcdc_teacher",
73 ]
74
75 if self.variables:
76 cmd += ["--variables"]
77 cmd += self.variables
78
79 cmd += [
80 "--identifier=" + self.identifier,
81 "--truth=" + self.truth,
83 ]
84 print("Running", cmd)
85 subprocess.call(cmd)
86
87 # Move training file to the right location
88 if self.identifier.endswith(".xml"):
89 tracking_data_dir_path = os.path.join(os.environ["BELLE2_LOCAL_DIR"], "tracking", "data")
90 shutil.copy(self.identifier, tracking_data_dir_path)
91
92 else:
93 cmd = [
94 "trackfindingcdc-classification-overview",
95 ]
96
97 if self.variables:
98 cmd += ["-v"]
99 cmd += self.variables
100
101 if self.groupby is not None:
102 cmd += ["-g"]
103 if isinstance(self.groupby, str):
104 cmd += [self.groupby]
105 else:
106 cmd += self.groupby
107
108 if self.auxiliaries is not None:
109 cmd += ["-a"]
110 if isinstance(self.auxiliaries, str):
111 cmd += [self.auxiliaries]
112 else:
113 cmd += self.auxiliaries
114
115 cmd += [
116 "--truth=" + self.truth,
117 self.sample_file_name,
118 ]
119 print("Running", cmd)
120 subprocess.call(cmd)
121
122 self.output_file_nameoutput_file_name = self.sample_file_name[:-len(".root")] + ".overview.root"
123
124 super().postprocess()
None output_file_name
There is no default for the name of the output TFile.
Definition: mixins.py:60
def create_argument_parser(self, **kwds)
Definition: training.py:50
def sample_file_name(self)
Definition: training.py:43
None groupby
Input groupby the classification analysis.
Definition: training.py:29
str task
Recording / training task selected.
Definition: training.py:23
output_file_name
Set file name for the TBrowser to show if demanded.
Definition: training.py:122
task
Process each event according to the user's desired task (train, eval, explore)
Definition: training.py:70
None variables
Input variable for the training or the classification analysis.
Definition: training.py:26
str truth
Truth variable name.
Definition: training.py:35
None auxiliaries
Input auxiliaries the classification analysis.
Definition: training.py:32