Belle II Software development
trainSecMap.py
1#!/usr/bin/env python3
2
3
10
11
30
31import basf2 as b2
32import sys
33import argparse
34import os
35
36
37# ---------------------------------------------------------------------------------------
38# Argument parser for input of training sample file via comandline option.
39arg_parser = argparse.ArgumentParser(description='Sector Map Training:\
40 Trains and stores SecMap from provided data sample.\n\
41 Usage: basf2 trainSecMap.py -- --train_sample traindata.root --secmap trainedSecMap.root')
42
43arg_parser.add_argument('--train_sample', '-i', type=str, nargs='*',
44 help='List of prepared training data file names which will be used for the training of the SecMap')
45arg_parser.add_argument('--secmap', '-s', type=str,
46 help='Inclusion of the root file containing the trained SecMap for the application of the VXDTF2.')
47arg_parser.add_argument(
48 '--threshold',
49 '-t',
50 type=int,
51 default=70,
52 help='Relative threshold (in %) used to prune the sector maps. Will remove X % of the least used subgraphs.')
53
54arguments = arg_parser.parse_args(sys.argv[1:])
55train_data = arguments.train_sample
56secmap_name = arguments.secmap
57relThreshold = arguments.threshold
58assert len(train_data) > 0, 'No data sample for training provided!'
59
60# ---------------------------------------------------------------------------------------
61# Logging and Debug Level
62b2.set_log_level(b2.LogLevel.ERROR)
63b2.log_to_file('logVXDTF2Training.log', append=False)
64
65
66# ---------------------------------------------------------------------------------------
67path = b2.create_path()
68
69# Event Info Setter which is requiered as the SecMap Training is performed in the Initilize
70# Phase. Thus, this script does not run over any events at all, but over the data available
71# in the prepared training sample root file.
72eventinfosetter = b2.register_module('EventInfoSetter')
73# default phase3 geometry:
74exp_number = 0
75# if environment variable is set then phase2 (aka Beast2) geometry will be taken
76if os.environ.get('USE_BEAST2_GEOMETRY'):
77 exp_number = 1002
78eventinfosetter.param("expList", [exp_number])
79path.add_module(eventinfosetter)
80
81# puts the geometry and gearbox in the path
82gearbox = b2.register_module('Gearbox')
83path.add_module(gearbox)
84# the geometry is loaded from the DB by default now! The correct geometry
85# should be pickked according to exp number
86geometry = b2.register_module('Geometry')
87path.add_module(geometry)
88
89
90# SecMapBootStrap Module is requiered, as it holds all the sector maps and
91# for storing the trained SecMap.
92secMapBootStrap = b2.register_module('SectorMapBootstrap')
93secMapBootStrap.param('ReadSectorMap', False)
94secMapBootStrap.param('WriteSectorMap', True)
95if secmap_name:
96 secMapBootStrap.param('SectorMapsOutputFile', secmap_name)
97elif os.environ.get('USE_BEAST2_GEOMETRY'):
98 secMapBootStrap.param('SectorMapsOutputFile', 'SectorMaps_Beast2.root')
99path.add_module(secMapBootStrap)
100
101# Perform SecMap Training on provided data sample
102merger = b2.register_module('RawSecMapMerger')
103merger.param('rootFileNames', train_data)
104merger.param('threshold', relThreshold)
105path.add_module(merger)
106
107b2.print_path(path)
108b2.process(path)
109print(b2.statistics)