9from ipython_tools
import handler
13from subprocess
import check_output, CalledProcessError, STDOUT
15from shutil
import copy
21from ROOT
import Belle2
26 Helper class to show a
PDF file in a jupyter notebook.
32 :param pdf: The filename of the PDF file.
33 :param size: The size to use.
41 """HTML representation"""
42 return f
'<iframe src={self.pdf} width={self.size[0]} height={self.size[1]}></iframe>'
45 """LaTeX representation"""
46 return r'\includegraphics[width=1.0\textwidth]{{{0}}}'.format(self.
pdf)
51 Class for training
and analysing a tracking module, which has a MVA filter
in it.
53 Works best,
if you are on a jupyter ntoebook.
55 You need to supply a run_class, which includes all needed settings, on how to
56 train
and execute the module. This
class will be mixed in with the normal
trackfindingcdc
57 run classes, so you can add the setting (e.g. tracking_coverage etc.)
as normal.
63 recording_module =
"FilterBasedVXDCDCTrackMerger"
65 recording_parameter =
"filter"
68 evaluation_cuts = [0.1, 0.2, ...]
78 generator_module =
"EvtGenInput"
82 def add_recording_modules(self, path):
83 mctrackfinder = path.add_module(
'TrackFinderMCTruthRecoTracks',
84 RecoTracksStoreArrayName=
'MCRecoTracks',
87 path.add_module(
'MCRecoTracksMatcher', mcRecoTracksStoreArrayName=
"MCRecoTracks",
88 prRecoTracksStoreArrayName=
"CDCRecoTracks", UseCDCHits=
True, UsePXDHits=
False, UseSVDHits=
False)
89 path.add_module(
'MCRecoTracksMatcher', mcRecoTracksStoreArrayName=
"MCRecoTracks",
90 prRecoTracksStoreArrayName=
"VXDRecoTracks", UseCDCHits=
False, UsePXDHits=
True, UseSVDHits=
True)
93 path.add_module(
'FilterBasedVXDCDCTrackMerger',
95 CDCRecoTrackStoreArrayName=
"CDCRecoTracks",
96 VXDRecoTrackStoreArrayName=
"VXDRecoTracks",
97 MergedRecoTrackStoreArrayName=
"RecoTracks")
104 def add_validation_modules(self, path):
105 mctrackfinder = path.add_module(
'TrackFinderMCTruthRecoTracks',
106 RecoTracksStoreArrayName=
'MCRecoTracks',
110 path.add_module(
'FilterBasedVXDCDCTrackMerger',
112 CDCRecoTrackStoreArrayName=
"CDCRecoTracks",
113 VXDRecoTrackStoreArrayName=
"VXDRecoTracks",
114 MergedRecoTrackStoreArrayName=
"PrefitRecoTracks")
116 path.add_module(
"SetupGenfitExtrapolation")
118 path.add_module(
"DAFRecoFitter", recoTracksStoreArrayName=
"PrefitRecoTracks")
120 path.add_module(
"TrackCreator", recoTrackColName=
"PrefitRecoTracks")
122 path.add_module(
"FittedTracksStorer", inputRecoTracksStoreArrayName=
"PrefitRecoTracks",
123 outputRecoTracksStoreArrayName=
"RecoTracks")
126 path.add_module(
'MCRecoTracksMatcher', mcRecoTracksStoreArrayName=
"MCRecoTracks",
127 prRecoTracksStoreArrayName=
"RecoTracks", UseCDCHits=
True, UsePXDHits=
True, UseSVDHits=
True)
164 """Record a training file, split it in two parts and call the training method of the mva package"""
175 Use the trained weight file and call the path again using different mva cuts. Validation using the
176 normal tracking validation modules.
182 except FileExistsError:
185 def create_path(mva_cut):
188 def finder_module(self, path):
189 self.add_validation_modules(path)
192 adjust_module(path, self.recording_module,
193 **{self.recording_parameter +
"Parameters": {
"cut": mva_cut},
194 self.recording_parameter:
"mva"})
196 adjust_module(path, self.recording_module, **{self.recording_parameter:
"truth"})
198 output_file_name = f
"results/validation_{mva_cut}.root"
200 run = ValidationRun()
202 if not os.path.exists(run.output_file_name):
203 return {
"path": run.create_path()}
205 return {
"path":
None}
209 calculations = handler.process_parameter_space(create_path, mva_cut=self.
run_class.evaluation_cuts + [999])
211 calculations.wait_for_end()
217 Evaluate the classification power on the test data set and produce a PDF.
223 df = uproot.concatenate(
233 from IPython.display
import display
239 """Call the mva training routine in the train file"""
242 except CalledProcessError
as e:
243 raise RuntimeError(e.output)
246 """Split the recorded file into two halves: training and test file and write it back"""
249 mask = np.random.rand(len(df)) < 0.5
250 training_sample = df[mask]
251 test_sample = df[~mask]
254 outfile[
"records"] = training_sample
256 outfile[
"records"] = test_sample
260 Create a path using the settings of the run_class and process it.
261 This will create a ROOT file
with the recorded data.
267 def create_path(self):
268 path = ReadOrGenerateEventsRun.create_path(self)
270 self.add_recording_modules(path)
272 adjust_module(path, self.recording_module,
273 **{self.recording_parameter +
"Parameters": {
"rootFileName": recording_file_name},
274 self.recording_parameter:
"recording"})
279 path = run.create_path()
282 calculation = handler.process(path)
284 calculation.wait_for_end()
291 """Call the mva expert"""
293 check_output([
"basf2_mva_expert",
297 "--treename",
"records"])
298 except CalledProcessError
as e:
299 raise RuntimeError(e.output)
302 """Call the mva evaluation routine"""
304 check_output([
"basf2_mva_evaluate.py",
308 "--treename",
"records",
311 except CalledProcessError
as e:
312 raise RuntimeError(e.output)
static std::string findFile(const std::string &path, bool silent=false)
Search for given file or directory in local or central release directory, and return absolute path if...
run_class
cached copy of the run class
evaluation_file_name
cached name of the output PDF file
def _call_training_routine(self)
def evaluate_classification(self)
test_file_name
cached path with extension of the testing-output file
use_jupyter
cached flag to use jupyter notebook
def _call_expert_routine(self)
recording_file_name
cached name of the output file
weight_data_location
cached path of the weight input data
expert_file_name
cached path with extension of the testing-export file
training_file_name
cached path without extension of the output file
def __init__(self, run_class, use_jupyter=True)
def _create_records_file(self)
def evaluate_tracking(self)
identifier_name
cached identifier
def _write_train_and_test_files(self)
def _call_evaluation_routine(self)
pdf
cached copy of the pdf filename
size
cached copy of the size
def __init__(self, pdf, size=(600, 700))