1 from ipython_tools
import handler
2 from root_pandas
import read_root, to_root
5 from subprocess
import check_output, CalledProcessError, STDOUT
7 from shutil
import copy
13 from ROOT
import Belle2
18 Helper class to show a PDF file in a jupyter notebook.
24 :param pdf: The filename of the PDF file.
25 :param size: The size to use.
33 """HTML representation"""
34 return '<iframe src={0} width={1[0]} height={1[1]}></iframe>'.format(self.
pdf, self.
size)
37 """LaTeX representation"""
38 return r'\includegraphics[width=1.0\textwidth]{{{0}}}'.format(self.
pdf)
43 Class for training and analysing a tracking module, which has a MVA filter in it.
45 Works best, if you are on a jupyter ntoebook.
47 You need to supply a run_class, which includes all needed settings, on how to
48 train and execute the module. This class will be mixed in with the normal trackfindingcdc
49 run classes, so you can add the setting (e.g. tracking_coverage etc.) as normal.
54 # This module will be trained
55 recording_module = "FilterBasedVXDCDCTrackMerger"
56 # This is the name of the parameter of this module, which will be set to "mva" etc.
57 recording_parameter = "filter"
59 # These mva cuts will be tested during evaluation.
60 evaluation_cuts = [0.1, 0.2, ...]
68 # Some options, which will control the run classes
70 generator_module = "EvtGenInput"
72 # This will be added to the "normal" path, to record the training data (you do not have to set the module to
73 # recording, as this is done automatically).
74 def add_recording_modules(self, path):
75 mctrackfinder = path.add_module('TrackFinderMCTruthRecoTracks',
76 RecoTracksStoreArrayName='MCRecoTracks',
79 path.add_module('MCRecoTracksMatcher', mcRecoTracksStoreArrayName="MCRecoTracks",
80 prRecoTracksStoreArrayName="CDCRecoTracks", UseCDCHits=True, UsePXDHits=False, UseSVDHits=False)
81 path.add_module('MCRecoTracksMatcher', mcRecoTracksStoreArrayName="MCRecoTracks",
82 prRecoTracksStoreArrayName="VXDRecoTracks", UseCDCHits=False, UsePXDHits=True, UseSVDHits=True)
84 # Merge CDC and CXD tracks
85 path.add_module('FilterBasedVXDCDCTrackMerger',
87 CDCRecoTrackStoreArrayName="CDCRecoTracks",
88 VXDRecoTrackStoreArrayName="VXDRecoTracks",
89 MergedRecoTrackStoreArrayName="RecoTracks")
93 # This will be added to the "normal" path, to evaluate the mva cuts. In most cases, this is the same as the
94 # add_recording_modules (as the module parameters will be set automatically), but maybe you need
96 def add_validation_modules(self, path):
97 mctrackfinder = path.add_module('TrackFinderMCTruthRecoTracks',
98 RecoTracksStoreArrayName='MCRecoTracks',
101 # Merge CDC and CXD tracks
102 path.add_module('FilterBasedVXDCDCTrackMerger',
104 CDCRecoTrackStoreArrayName="CDCRecoTracks",
105 VXDRecoTrackStoreArrayName="VXDRecoTracks",
106 MergedRecoTrackStoreArrayName="PrefitRecoTracks")
108 path.add_module("SetupGenfitExtrapolation")
110 path.add_module("DAFRecoFitter", recoTracksStoreArrayName="PrefitRecoTracks")
112 path.add_module("TrackCreator", recoTrackColName="PrefitRecoTracks")
114 path.add_module("FittedTracksStorer", inputRecoTracksStoreArrayName="PrefitRecoTracks",
115 outputRecoTracksStoreArrayName="RecoTracks")
117 # We need to include the matching ourselves, as we have already a matching algorithm in place
118 path.add_module('MCRecoTracksMatcher', mcRecoTracksStoreArrayName="MCRecoTracks",
119 prRecoTracksStoreArrayName="RecoTracks", UseCDCHits=True, UsePXDHits=True, UseSVDHits=True)
156 """Record a training file, split it in two parts and call the training method of the mva package"""
167 Use the trained weight file and call the path again using different mva cuts. Validation using the
168 normal tracking validation modules.
174 except FileExistsError:
177 def create_path(mva_cut):
180 def finder_module(self, path):
181 self.add_validation_modules(path)
184 adjust_module(path, self.recording_module,
185 **{self.recording_parameter +
"Parameters": {
"cut": mva_cut},
186 self.recording_parameter:
"mva"})
188 adjust_module(path, self.recording_module, **{self.recording_parameter:
"truth"})
190 output_file_name =
"results/validation_{mva_cut}.root".format(mva_cut=mva_cut)
192 run = ValidationRun()
194 if not os.path.exists(run.output_file_name):
195 return {
"path": run.create_path()}
197 return {
"path":
None}
201 calculations = handler.process_parameter_space(create_path, mva_cut=self.
run_class.evaluation_cuts + [999])
203 calculations.wait_for_end()
209 Evaluate the classification power on the test data set and produce a PDF.
218 from IPython.display
import display
224 """Call the mva training routine in the train file"""
227 except CalledProcessError
as e:
228 raise RuntimeError(e.output)
231 """Split the recorded file into two halves: training and test file and write it back"""
234 mask = np.random.rand(len(df)) < 0.5
235 training_sample = df[mask]
236 test_sample = df[~mask]
243 Create a path using the settings of the run_class and process it.
244 This will create a ROOT file with the recorded data.
250 def create_path(self):
251 path = ReadOrGenerateEventsRun.create_path(self)
253 self.add_recording_modules(path)
255 adjust_module(path, self.recording_module,
256 **{self.recording_parameter +
"Parameters": {
"rootFileName": recording_file_name},
257 self.recording_parameter:
"recording"})
262 path = run.create_path()
265 calculation = handler.process(path)
267 calculation.wait_for_end()
274 """Call the mva expert"""
276 check_output([
"basf2_mva_expert",
280 "--treename",
"records"])
281 except CalledProcessError
as e:
282 raise RuntimeError(e.output)
285 """Call the mva evaluation routine"""
287 check_output([
"basf2_mva_evaluate.py",
291 "--treename",
"records",
294 except CalledProcessError
as e:
295 raise RuntimeError(e.output)