9 from ipython_tools
import handler
10 from root_pandas
import read_root, to_root
13 from subprocess
import check_output, CalledProcessError, STDOUT
15 from shutil
import copy
21 from 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 '<iframe src={0} width={1[0]} height={1[1]}></iframe>'.format(self.
pdfpdf, self.
sizesize)
45 """LaTeX representation"""
46 return r'\includegraphics[width=1.0\textwidth]{{{0}}}'.format(self.
pdfpdf)
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.
62 # This module will be trained
63 recording_module = "FilterBasedVXDCDCTrackMerger"
64 # This is the name of the parameter of this module, which will be set to "mva" etc.
65 recording_parameter = "filter"
67 # These mva cuts will be tested during evaluation.
68 evaluation_cuts = [0.1, 0.2, ...]
76 # Some options, which will control the run classes
78 generator_module = "EvtGenInput"
80 # This will be added to the "normal" path, to record the training data (you do not have to set the module to
81 # recording, as this is done automatically).
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)
92 # Merge CDC and CXD tracks
93 path.add_module('FilterBasedVXDCDCTrackMerger',
95 CDCRecoTrackStoreArrayName="CDCRecoTracks",
96 VXDRecoTrackStoreArrayName="VXDRecoTracks",
97 MergedRecoTrackStoreArrayName="RecoTracks")
101 # This will be added to the "normal" path, to evaluate the mva cuts. In most cases, this is the same as the
102 # add_recording_modules (as the module parameters will be set automatically), but maybe you need
104 def add_validation_modules(self, path):
105 mctrackfinder = path.add_module('TrackFinderMCTruthRecoTracks',
106 RecoTracksStoreArrayName='MCRecoTracks',
109 # Merge CDC and CXD tracks
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")
125 # We need to include the matching ourselves, as we have already a matching algorithm in place
126 path.add_module('MCRecoTracksMatcher', mcRecoTracksStoreArrayName="MCRecoTracks",
127 prRecoTracksStoreArrayName="RecoTracks", UseCDCHits=True, UsePXDHits=True, UseSVDHits=True)
161 self.
run_classrun_class.weight_data_location))
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 =
"results/validation_{mva_cut}.root".format(mva_cut=mva_cut)
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_classrun_class.evaluation_cuts + [999])
211 calculations.wait_for_end()
217 Evaluate the classification power on the test data set and produce a PDF.
226 from IPython.display
import display
232 """Call the mva training routine in the train file"""
235 except CalledProcessError
as e:
236 raise RuntimeError(e.output)
239 """Split the recorded file into two halves: training and test file and write it back"""
242 mask = np.random.rand(len(df)) < 0.5
243 training_sample = df[mask]
244 test_sample = df[~mask]
246 to_root(training_sample, self.
training_file_nametraining_file_name, tree_key=
"records")
247 to_root(test_sample, self.
test_file_nametest_file_name, tree_key=
"records")
251 Create a path using the settings of the run_class and process it.
252 This will create a ROOT file with the recorded data.
258 def create_path(self):
259 path = ReadOrGenerateEventsRun.create_path(self)
261 self.add_recording_modules(path)
263 adjust_module(path, self.recording_module,
264 **{self.recording_parameter +
"Parameters": {
"rootFileName": recording_file_name},
265 self.recording_parameter:
"recording"})
270 path = run.create_path()
273 calculation = handler.process(path)
275 calculation.wait_for_end()
282 """Call the mva expert"""
284 check_output([
"basf2_mva_expert",
288 "--treename",
"records"])
289 except CalledProcessError
as e:
290 raise RuntimeError(e.output)
293 """Call the mva evaluation routine"""
295 check_output([
"basf2_mva_evaluate.py",
299 "--treename",
"records",
302 except CalledProcessError
as e:
303 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))
std::vector< std::vector< double > > merge(std::vector< std::vector< std::vector< double >>> toMerge)
merge { vector<double> a, vector<double> b} into {a, b}