10cdc_and_svd_ckf_merger_mva_training
11-----------------------------------------
16This python script is used for the training and validation of the classifier of
17the MVA-based result filter of the CDCToSVDSeedCKF, which combines tracks that
18were found by the CDC and SVD standalone tracking algorithms.
20To avoid mistakes, b2luigi is used to create a task chain for a combined training and
21validation of all classifiers.
23The order of the b2luigi tasks in this script is as follows (top to bottom):
24* Two tasks to create input samples for training and testing (``GenerateSimTask`` and
25``SplitNMergeSimTask``). The ``SplitNMergeSimTask`` takes a number of events to be
26generated and a number of events per task to reduce runtime. It then divides the total
27number of events by the number of events per task and creates as ``GenerateSimTask`` as
28needed, each with a specific random seed, so that in the end the total number of
29training and testing events are simulated. The individual files are then combined
30by the SplitNMergeSimTask into one file each for training and testing.
31* The ``ResultRecordingTask`` writes out the data used for training of the MVA.
32* The ``CKFResultFilterTeacherTask`` trains the MVA, FastBDT per default, with a
33given set of FastBDT options.
34* The ``ValidationAndOptimisationTask`` uses the trained weight files and cut values
35provided to run the tracking chain with the weight file under test, and also
36runs the tracking validation.
37* Finally, the ``MainTask`` is the "brain" of the script. It invokes the
38``ValidationAndOptimisationTask`` with the different combinations of FastBDT options
39and cut values on the MVA classifier output.
41Due to the dependencies, the calls of the task are reversed. The MainTask
42calls the ``ValidationAndOptimisationTask`` with different FastBDT options and cut
43values, and the ``ValidationAndOptimisationTask`` itself calls the required teacher,
44training, and simulation tasks.
46b2luigi: Understanding the steering file
47~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
49All trainings and validations are done in the correct order in this steering
50file. For the purpose of creating a dependency graph, the `b2luigi
51<https://b2luigi.readthedocs.io>`_ python package is used, which extends the
52`luigi <https://luigi.readthedocs.io>`_ package developed by spotify.
54Each task that has to be done is represented by a special class, which defines
55which defines parameters, output files and which other tasks with which
56parameters it depends on. For example a teacher task, which runs
57``basf2_mva_teacher.py`` to train the classifier, depends on a data collection
58task which runs a reconstruction and writes out track-wise variables into a root
59file
for training. An evaluation/validation task
for testing the classifier
60requires both the teacher task,
as it needs the weightfile to be present,
and
61also a data collection task, because it needs a dataset
for testing classifier.
63The final task that defines which tasks need to be done
for the steering file to
64finish
is the ``MainTask``. When you only want to run parts of the
65training/validation pipeline, you can comment out requirements
in the Master
66task
or replace them by lower-level tasks during debugging.
71This steering file relies on b2luigi_
for task scheduling. It can be installed
74 python3 -m pip install [--user] b2luigi
76Use the ``--user`` option
if you have
not rights to install python packages into
77your externals (e.g. because you are using cvmfs)
and install them
in
78``$HOME/.local`` instead.
83Instead of command line arguments, the b2luigi script
is configured via a
84``settings.json`` file. Open it
in your favorite text editor
and modify it to
85fit to your requirements.
90You can test the b2luigi without running it via::
92 python3 cdc_and_svd_ckf_merger_mva_training.py --dry-run
93 python3 cdc_and_svd_ckf_merger_mva_training.py --show-output
95This will show the outputs
and show potential errors
in the definitions of the
96luigi task dependencies. To run the the steering file
in normal (local) mode,
99 python3 cdc_and_svd_ckf_merger_mva_training.py
101One can use the interactive luigi web interface via the central scheduler
102which visualizes the task graph
while it
is running. Therefore, the scheduler
103daemon ``luigid`` has to run
in the background, which
is located
in
104``~/.local/bin/luigid``
in case b2luigi had been installed
with ``--user``. For
109Then, execute your steering (e.g.
in another terminal)
with::
111 python3 cdc_and_svd_ckf_merger_mva_training.py --scheduler-port 8886
113To view the web interface, open your webbrowser enter into the url bar::
117If you don
't run the steering file on the same machine on which you run your webbrowser, you have two options:
119 1. Run both the steering file and ``luigid`` remotely
and use
120 ssh-port-forwarding to your local host. Therefore, run on your local
123 ssh -N -f -L 8886:localhost:8886 <remote_user>@<remote_host>
125 2. Run the ``luigid`` scheduler locally
and use the ``--scheduler-host <your
126 local host>`` argument when calling the steering file
128Accessing the results / output files
129~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
131All output files are stored
in a directory structure
in the ``result_path``. The
132directory tree encodes the used b2luigi parameters. This ensures reproducibility
133and makes parameter searches easy. Sometimes, it
is hard to find the relevant
134output files. You can view the whole directory structure by running ``tree
135<result_path>``. Ise the unix ``find`` command to find the files that interest
138 find <result_path> -name
"*.root"
145# from tracking import add_track_finding
146from tracking.path_utils import add_hit_preparation_modules, add_cdc_track_finding, add_svd_standalone_tracking
147from tracking.harvesting_validation.combined_module import CombinedTrackingValidationModule
151from ckf_training import my_basf2_mva_teacher, create_fbdt_option_string
152from tracking_mva_filter_payloads.write_tracking_mva_filter_payloads_to_db import write_tracking_mva_filter_payloads_to_db
154# wrap python modules that are used here but not in the externals into a try except block
155install_helpstring_formatter = ("\nCould not find {module} python module.Try installing it via\n"
156 " python3 -m pip install [--user] {module}\n")
159 from b2luigi.core.utils
import create_output_dirs
160 from b2luigi.basf2_helper
import Basf2PathTask, Basf2Task
161except ModuleNotFoundError:
162 print(install_helpstring_formatter.format(module=
"b2luigi"))
166class GenerateSimTask(Basf2PathTask):
168 Generate simulated Monte Carlo with background overlay.
170 Make sure to use different ``random_seed`` parameters
for the training data
171 format the classifier trainings
and for the test data
for the respective
172 evaluation/validation tasks.
176 experiment_number = b2luigi.IntParameter()
178 n_events = b2luigi.IntParameter()
181 random_seed = b2luigi.Parameter()
183 bkgfiles_dir = b2luigi.Parameter(
194 Create output file name depending on number of events and production
195 mode that
is specified
in the random_seed string.
197 :param n_events: Number of events to simulate.
198 :param random_seed: Random seed to use
for the simulation to create independent samples.
202 if random_seed
is None:
204 return "generated_mc_N" + str(n_events) +
"_" + random_seed +
".root"
208 Generate list of output files that the task should produce.
209 The task is considered finished
if and only
if the outputs all exist.
213 def create_path(self):
215 Create basf2 path to process with event generation
and simulation.
218 path = basf2.create_path()
222 path.add_module(
"EvtGenInput")
244 Generate simulated Monte Carlo with background overlay.
246 Make sure to use different ``random_seed`` parameters
for the training data
247 format the classifier trainings
and for the test data
for the respective
248 evaluation/validation tasks.
252 experiment_number = b2luigi.IntParameter()
254 n_events = b2luigi.IntParameter()
257 random_seed = b2luigi.Parameter()
259 bkgfiles_dir = b2luigi.Parameter(
270 Create output file name depending on number of events and production
271 mode that
is specified
in the random_seed string.
273 :param n_events: Number of events to simulate.
274 :param random_seed: Random seed to use
for the simulation to create independent samples.
278 if random_seed
is None:
280 return "generated_mc_N" + str(n_events) +
"_" + random_seed +
".root"
284 Generate list of output files that the task should produce.
285 The task is considered finished
if and only
if the outputs all exist.
291 This task requires several GenerateSimTask to be finished so that he required number of events is created.
293 n_events_per_task = MainTask.n_events_per_task
294 quotient, remainder = divmod(self.n_events, n_events_per_task)
295 for i
in range(quotient):
298 num_processes=MainTask.num_processes,
299 random_seed=self.
random_seed +
'_' + str(i).zfill(3),
300 n_events=n_events_per_task,
306 num_processes=MainTask.num_processes,
307 random_seed=self.
random_seed +
'_' + str(quotient).zfill(3),
312 @b2luigi.on_temporary_files
315 When all GenerateSimTasks finished, merge the output.
317 create_output_dirs(self)
319 file_list = [item for sublist
in self.get_input_file_names().values()
for item
in sublist]
320 print(
"Merge the following files:")
322 cmd = [
"b2file-merge",
"-f"]
323 args = cmd + [self.get_output_file_name(self.
output_file_name())] + file_list
324 subprocess.check_call(args)
325 print(
"Finished merging. Now remove the input files to save space.")
327 for tempfile
in file_list:
328 args = cmd2 + [tempfile]
329 subprocess.check_call(args)
334 Task to record data for the final result filter. This only requires found
and MC-matched SVD
and CDC tracks that need to be
335 merged, all state filters are set to
"all"
339 experiment_number = b2luigi.IntParameter()
341 n_events_training = b2luigi.IntParameter()
344 random_seed = b2luigi.Parameter()
347 result_filter_records_name = b2luigi.Parameter()
351 Generate list of output files that the task should produce.
352 The task is considered finished
if and only
if the outputs all exist.
358 This task requires that the training SplitMergeSimTask is finished.
369 Create a path for the recording of the result filter. This file
is then used to train the result filter.
371 :param result_filter_records_name: Name of the recording file.
374 path = basf2.create_path()
377 file_list = [fname
for sublist
in self.get_input_file_names().values()
378 for fname
in sublist
if "generated_mc_N" in fname
and "training" in fname
and fname.endswith(
".root")]
379 path.add_module(
"RootInput", inputFileNames=file_list)
381 path.add_module(
"Gearbox")
382 path.add_module(
"Geometry")
383 path.add_module(
"SetupGenfitExtrapolation")
385 add_hit_preparation_modules(path, components=[
"SVD"])
388 mc_reco_tracks =
"MCRecoTracks"
389 path.add_module(
'TrackFinderMCTruthRecoTracks',
390 RecoTracksStoreArrayName=mc_reco_tracks)
393 cdc_reco_tracks =
"CDCRecoTracks"
394 add_cdc_track_finding(path, output_reco_tracks=cdc_reco_tracks)
395 path.add_module(
"MCRecoTracksMatcher", UsePXDHits=
False, UseSVDHits=
False, UseCDCHits=
True,
396 mcRecoTracksStoreArrayName=mc_reco_tracks,
397 prRecoTracksStoreArrayName=cdc_reco_tracks)
399 path.add_module(
"DAFRecoFitter", recoTracksStoreArrayName=cdc_reco_tracks)
402 svd_reco_tracks =
"SVDRecoTracks"
403 add_svd_standalone_tracking(path, reco_tracks=svd_reco_tracks)
404 path.add_module(
"MCRecoTracksMatcher", UsePXDHits=
False, UseSVDHits=
True, UseCDCHits=
False,
405 mcRecoTracksStoreArrayName=mc_reco_tracks,
406 prRecoTracksStoreArrayName=svd_reco_tracks)
408 direction =
"backward"
409 path.add_module(
"CDCToSVDSeedCKF",
410 inputRecoTrackStoreArrayName=cdc_reco_tracks,
412 fromRelationStoreArrayName=cdc_reco_tracks,
413 toRelationStoreArrayName=svd_reco_tracks,
415 relatedRecoTrackStoreArrayName=svd_reco_tracks,
416 cdcTracksStoreArrayName=cdc_reco_tracks,
417 vxdTracksStoreArrayName=svd_reco_tracks,
419 relationCheckForDirection=direction,
421 firstHighFilterParameters={
"direction": direction},
422 advanceHighFilterParameters={
"direction": direction},
424 writeOutDirection=direction,
427 filter=
"recording_with_relations",
428 filterParameters={
"rootFileName": result_filter_records_name})
432 def create_path(self):
434 Create basf2 path to process with event generation
and simulation.
443 A teacher task runs the basf2 mva teacher on the training data provided by a
444 data collection task.
446 Since teacher tasks are needed for all quality estimators covered by this
447 steering file
and the only thing that changes
is the required data
448 collection task
and some training parameters, I decided to use inheritance
449 and have the basic functionality
in this base
class/interface
and have the
450 specific teacher tasks inherit
from it.
453 experiment_number = b2luigi.IntParameter()
455 n_events_training = b2luigi.IntParameter()
458 random_seed = b2luigi.Parameter()
460 result_filter_records_name = b2luigi.Parameter()
462 training_target = b2luigi.Parameter(
469 exclude_variables = b2luigi.ListParameter(
471 hashed=
True, default=[]
475 fast_bdt_option = b2luigi.ListParameter(
477 hashed=
True, default=[200, 8, 3, 0.1]
483 Name of weightfile that is created by the teacher task.
485 :param fast_bdt_option: FastBDT option that
is used to train this MVA
487 if fast_bdt_option
is None:
489 fast_bdt_string = create_fbdt_option_string(fast_bdt_option)
490 weightfile_name =
"trk_CDCToSVDSeedResultFilter" + fast_bdt_string
491 return weightfile_name
495 Generate list of luigi Tasks that this Task depends on.
506 Generate list of output files that the task should produce.
507 The task is considered finished
if and only
if the outputs all exist.
513 Use basf2_mva teacher to create MVA weightfile from collected training
516 This
is the main process that
is dispatched by the ``run`` method that
517 is inherited
from ``Basf2Task``.
521 my_basf2_mva_teacher(
522 records_files=records_files,
524 weightfile_identifier=weightfile_identifier,
529 basf2_mva.download(weightfile_identifier, self.get_output_file_name(weightfile_identifier +
".root"))
535 Validate the performance of the trained filters by trying various combinations of FastBDT options, as well
as cut values
for
536 the states, the number of best candidates kept after each filter,
and similar
for the result filter.
539 experiment_number = b2luigi.IntParameter()
541 n_events_training = b2luigi.IntParameter()
543 fast_bdt_option = b2luigi.ListParameter(
545 hashed=
True, default=[200, 8, 3, 0.1]
549 n_events_testing = b2luigi.IntParameter()
551 result_filter_cut = b2luigi.FloatParameter()
554 basf2.conditions.prepend_testing_payloads(
"localdb/database.txt")
558 Generate list of output files that the task should produce.
559 The task is considered finished
if and only
if the outputs all exist.
562 yield self.add_to_output(
563 f
"cdc_svd_merger_ckf_validation{fbdt_string}_{self.result_filter_cut}.root")
567 This task requires trained result filters, and that an independent data set
for validation was created using the
568 ``SplitMergeSimTask``
with the random seed optimisation.
571 result_filter_records_name=
"filter_records.root",
575 random_seed=
'training'
581 random_seed=
"optimisation",
586 Create a path to validate the trained filters.
588 path = basf2.create_path()
591 file_list = [fname
for sublist
in self.get_input_file_names().values()
592 for fname
in sublist
if "generated_mc_N" in fname
and "optimisation" in fname
and fname.endswith(
".root")]
593 path.add_module(
"RootInput", inputFileNames=file_list)
595 path.add_module(
"Gearbox")
596 path.add_module(
"Geometry")
597 path.add_module(
"SetupGenfitExtrapolation")
599 add_hit_preparation_modules(path, components=[
"SVD"])
601 cdc_reco_tracks =
"CDCRecoTracks"
602 svd_reco_tracks =
"SVDRecoTracks"
603 reco_tracks =
"RecoTracks"
604 mc_reco_tracks =
"MCRecoTracks"
607 add_cdc_track_finding(path, output_reco_tracks=cdc_reco_tracks)
609 path.add_module(
"DAFRecoFitter", recoTracksStoreArrayName=cdc_reco_tracks)
612 add_svd_standalone_tracking(path, reco_tracks=svd_reco_tracks)
614 direction =
"backward"
620 f
"trk_CDCToSVDSeedResultFilterParameter{fbdt_string}",
622 f
"trk_CDCToSVDSeedResultFilter{fbdt_string}",
625 basf2.conditions.prepend_testing_payloads(
"localdb/database.txt")
626 result_filter_parameters = {
"DBPayloadName": f
"trk_CDCToSVDSeedResultFilterParameter{fbdt_string}"}
630 inputRecoTrackStoreArrayName=cdc_reco_tracks,
631 fromRelationStoreArrayName=cdc_reco_tracks,
632 toRelationStoreArrayName=svd_reco_tracks,
633 relatedRecoTrackStoreArrayName=svd_reco_tracks,
634 cdcTracksStoreArrayName=cdc_reco_tracks,
635 vxdTracksStoreArrayName=svd_reco_tracks,
636 relationCheckForDirection=direction,
638 firstHighFilterParameters={
639 "direction": direction},
640 advanceHighFilterParameters={
641 "direction": direction},
642 writeOutDirection=direction,
644 filter=
'mva_with_relations',
645 filterParameters=result_filter_parameters
648 path.add_module(
'RelatedTracksCombiner',
649 VXDRecoTracksStoreArrayName=svd_reco_tracks,
650 CDCRecoTracksStoreArrayName=cdc_reco_tracks,
651 recoTracksStoreArrayName=reco_tracks)
653 path.add_module(
'TrackFinderMCTruthRecoTracks',
654 RecoTracksStoreArrayName=mc_reco_tracks,
660 path.add_module(
"MCRecoTracksMatcher", UsePXDHits=
False, UseSVDHits=
True, UseCDCHits=
True,
661 mcRecoTracksStoreArrayName=mc_reco_tracks,
662 prRecoTracksStoreArrayName=reco_tracks)
666 output_file_name=self.get_output_file_name(
667 f
"cdc_svd_merger_ckf_validation{fbdt_string}_{self.result_filter_cut}.root"),
668 reco_tracks_name=reco_tracks,
669 mc_reco_tracks_name=mc_reco_tracks,
676 def create_path(self):
678 Create basf2 path to process with event generation
and simulation.
685 Wrapper task that needs to finish for b2luigi to finish running this steering file.
687 It
is done
if the outputs of all required subtasks exist. It
is thus at the
688 top of the luigi task graph. Edit the ``requires`` method to steer which
689 tasks
and with which parameters you want to run.
692 n_events_training = b2luigi.get_setting(
694 "n_events_training", default=1000
698 n_events_testing = b2luigi.get_setting(
700 "n_events_testing", default=500
704 n_events_per_task = b2luigi.get_setting(
706 "n_events_per_task", default=100
710 num_processes = b2luigi.get_setting(
712 "basf2_processes_per_worker", default=0
717 bkgfiles_by_exp = b2luigi.get_setting(
"bkgfiles_by_exp")
719 bkgfiles_by_exp = {int(key): val
for (key, val)
in bkgfiles_by_exp.items()}
723 Generate list of tasks that needs to be done for luigi to finish running
734 cut_values.append((i+1) * 0.2)
736 experiment_numbers = b2luigi.get_setting(
"experiment_numbers")
739 for experiment_number, fast_bdt_option, cut_value
in itertools.product(
740 experiment_numbers, fast_bdt_options, cut_values
743 experiment_number=experiment_number,
745 fast_bdt_option=fast_bdt_option,
747 result_filter_cut=cut_value,
751if __name__ ==
"__main__":
753 b2luigi.set_setting(
"env_script",
"./setup_basf2.sh")
754 b2luigi.get_setting(
"batch_system", default=
"lsf")
755 workers = b2luigi.get_setting(
"workers", default=1)
756 b2luigi.process(
MainTask(), workers=workers, batch=
True)
def get_background_files(folder=None, output_file_info=True)
b2luigi random_seed
Random basf2 seed.
b2luigi fast_bdt_option
Hyperparameter option of the FastBDT algorithm.
b2luigi n_events_training
Number of events to generate for the training data set.
b2luigi training_target
Feature/variable to use as truth label in the quality estimator MVA classifier.
b2luigi experiment_number
Experiment number of the conditions database, e.g.
def get_weightfile_identifier(self, fast_bdt_option=None)
b2luigi exclude_variables
List of collected variables to not use in the training of the QE MVA classifier.
b2luigi result_filter_records_name
Name of the input file name.
b2luigi random_seed
Random basf2 seed.
def output_file_name(self, n_events=None, random_seed=None)
Name of the ROOT output file with generated and simulated events.
b2luigi bkgfiles_dir
Directory with overlay background root files.
b2luigi n_events
Number of events to generate.
b2luigi experiment_number
Experiment number of the conditions database, e.g.
b2luigi n_events_training
Number of events to generate for the training data set.
b2luigi n_events_testing
Number of events to generate for the test data set.
def create_result_recording_path(self, result_filter_records_name)
b2luigi random_seed
Random basf2 seed.
b2luigi n_events_training
Number of events to generate.
b2luigi experiment_number
Experiment number of the conditions database, e.g.
b2luigi result_filter_records_name
Name of the records file for training the final result filter.
b2luigi random_seed
Random basf2 seed.
def output_file_name(self, n_events=None, random_seed=None)
Name of the ROOT output file with generated and simulated events.
b2luigi bkgfiles_dir
Directory with overlay background root files.
b2luigi n_events
Number of events to generate.
b2luigi experiment_number
Experiment number of the conditions database, e.g.
b2luigi fast_bdt_option
FastBDT option to use to train the StateFilters.
b2luigi n_events_training
Number of events to generate for the training data set.
b2luigi n_events_testing
Number of events to generate for the testing, validation, and optimisation data set.
def create_optimisation_and_validation_path(self)
b2luigi result_filter_cut
Value of the cut on the MVA classifier output for a result candidate.
b2luigi experiment_number
Experiment number of the conditions database, e.g.
def add_simulation(path, components=None, bkgfiles=None, bkgOverlay=True, forceSetPXDDataReduction=False, usePXDDataReduction=True, cleanupPXDDataReduction=True, generate_2nd_cdc_hits=False, simulateT0jitter=True, isCosmics=False, FilterEvents=False, usePXDGatedMode=False, skipExperimentCheckForBG=False, save_slow_pions_in_mc=False)