Belle II Software  release-08-01-10
trainingPreparation.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 
11 
12 
23 
24 
25 import basf2 as b2
26 from setup_modules import setup_RTCtoSPTCConverters
27 import argparse
28 import os
29 
30 # ---------------------------------------------------------------------------------------
31 # Argument parser to enable training sample selection via comandline option.
32 parser = argparse.ArgumentParser(description='Training sample preparation:\
33  Prepare a data sample to train the sector map.\n\
34  Usage: basf2 traininPreparation.py -i dataSample.root -- --enable_selection boolean')
35 parser.add_argument(
36  '--enable_selection',
37  dest='use_NoKick',
38  action='store_const',
39  const=True,
40  default=False,
41  help='enable the selection of training sample based on track parameters')
42 
43 parser.add_argument(
44  '--disable_checkFit',
45  dest='checkFit',
46  action='store_const',
47  const=False,
48  default=True,
49  help="By default only RecoTracks with valid fit are taken for training. Using this option will disable that. ")
50 
51 arguments = parser.parse_args()
52 use_noKick = arguments.use_NoKick
53 
54 
55 # ---------------------------------------------------------------------------------------
56 # Settings
57 
58 # Logging and Debug Level
59 # TODO: Remove logLevel, as it can be set via basf2 option -l
60 b2.set_log_level(b2.LogLevel.ERROR)
61 b2.log_to_file('logVXDTF2Preparation.log', append=False)
62 # if false PXD hits will be ignored in the trainings data collection
63 # Currently we dont do PXD tracking with vxdtf2 (as of 15.02.2018)
64 usePXD = False
65 
66 # ---------------------------------------------------------------------------------------
67 # Create paths
68 path = b2.create_path()
69 
70 
71 # Input Module
72 rootInputM = b2.register_module('RootInput')
73 path.add_module(rootInputM)
74 
75 # Event Info Module
76 eventinfoprinter = b2.register_module('EventInfoPrinter')
77 path.add_module(eventinfoprinter)
78 
79 path.add_module("PrintCollections", printForEvent=1)
80 
81 
82 # puts the geometry and gearbox in the path
83 gearbox = b2.register_module('Gearbox')
84 path.add_module(gearbox)
85 # the geometry is loaded from the DB by default now! The correct geometry
86 # should be pickked according to exp number for the generated events.
87 geometry = b2.register_module('Geometry')
88 path.add_module(geometry)
89 
90 # Event counter
91 # eventCounter = register_module('EventCounter')
92 # path.add_module(eventCounter)
93 
94 
95 # put PXD and SVD SpacePoints into the same StoreArray
96 if usePXD:
97  spCreatorPXD = b2.register_module('PXDSpacePointCreator')
98  spCreatorPXD.param('NameOfInstance', 'PXDSpacePointCreator')
99  spCreatorPXD.param('SpacePoints', 'PXDSpacePoints')
100  path.add_module(spCreatorPXD)
101 
102 spCreatorSVD = b2.register_module('SVDSpacePointCreator')
103 spCreatorSVD.param('OnlySingleClusterSpacePoints', False)
104 spCreatorSVD.param('NameOfInstance', 'SVDSpacePointCreator')
105 spCreatorSVD.param('SpacePoints', 'SVDSpacePoints')
106 path.add_module(spCreatorSVD)
107 
108 
109 # Converts GenFit track candidates and checks them, with respect to the SecMap settings
110 # Produces SpacePoint TrackCand which is used in VXDTFTrainingDataCollector.
111 setup_RTCtoSPTCConverters(path=path,
112  SVDSPscollection='SVDSpacePoints',
113  PXDSPscollection='PXDSpacePoints',
114  RTCinput='MCRecoTracks',
115  sptcOutput='checkedSPTCs',
116  usePXD=usePXD,
117  logLevel=b2.LogLevel.ERROR,
118  useNoKick=use_noKick,
119  useOnlyFittedTracks=True) # train on fitted tracks only
120 
121 
122 # SecMap BootStrap
123 # Module to fetch SecMap Config and store or load SecMap Training.
124 # Config is defined in /tracking/modules/vxdtfRedesing/src/SectorMapBootstrapModule.cc
125 # and must be available for the training of the SecMap
126 # Double False only fetches config.
127 secMapBootStrap = b2.register_module('SectorMapBootstrap')
128 secMapBootStrap.param('ReadSectorMap', False)
129 secMapBootStrap.param('WriteSectorMap', False)
130 path.add_module(secMapBootStrap)
131 
132 
133 # Module for generation of train sample for SecMap Training
134 nameTag = 'Belle2'
135 if os.environ.get('USE_BEAST2_GEOMETRY'):
136  nameTag = 'Beast2'
137 
138 if usePXD:
139  nameTag += '_VXD'
140 else:
141  nameTag += '_SVDOnly'
142 
143 #
144 SecMapTrainerBase = b2.register_module('VXDTFTrainingDataCollector')
145 SecMapTrainerBase.param('NameTag', nameTag)
146 SecMapTrainerBase.param('SpacePointTrackCandsName', 'checkedSPTCs')
147 # SecMapTrainerBase.logging.log_level = LogLevel.DEBUG
148 path.add_module(SecMapTrainerBase)
149 
150 # this can take quite long so it is good to know if it is still running
151 path.add_module('Progress')
152 
153 path.add_module("PrintCollections", printForEvent=1)
154 
155 b2.process(path)
156 
157 # to show the settings of all modules (only those differing from default)
158 b2.print_path(path)
159 
160 print(b2.statistics)