Belle II Software  release-05-01-25
neurotrainer.py
1 #!/user/bin/env python
2 
3 import basf2
4 from ROOT import Belle2
5 import os
6 import glob
7 
8 """
9 Example script showing how to train neural networks
10 to be used with the CDCTriggerNeuroModule.
11 
12 This script uses realistic values for the amount of training data
13 and the number of runs, so it will run a long time.
14 """
15 
16 # ------------ #
17 # user options #
18 # ------------ #
19 
20 # set random seed
21 basf2.set_random_seed(1)
22 
23 # paths for the trained networks, the training data and the log files
24 mlpdir = Belle2.FileSystem.findFile('trg/cdc/data')
25 traindir = Belle2.FileSystem.findFile('trg/cdc/data')
26 logdir = Belle2.FileSystem.findFile('trg/cdc/data')
27 # filenames for the trained networks, the training data and the log files
28 mlpname = 'NeuroTrigger.root'
29 trainname = 'NeuroTriggerTraindata.root'
30 logname = 'NeuroTriggerLog' # file extensions are appended automatically
31 
32 # number of threads to be used for parallel training
33 nthreads = 1
34 
35 # We want to train on single tracks within the acceptance of the track finder.
36 particlegun_params = {
37  'pdgCodes': [-13, 13], # muons
38  'nTracks': 1, # single tracks
39  'momentumGeneration': 'inversePt', # uniform in the track curvature
40  'momentumParams': [0.3, 10.], # 0.3: minimum pt for standard track finder
41  'thetaGeneration': 'uniformCos', # uniform in solid angle
42  'thetaParams': [23, 144], # hit SL 6 from z in [-50, 50]
43  'phiGeneration': 'uniform', # uniform in solid angle
44  'phiParams': [0, 360], # full phi
45  'vertexGeneration': 'uniform', # uniform vertex distribution
46  'xVertexParams': [0, 0.0], # vertex on z-axis
47  'yVertexParams': [0, 0.0], # vertex on z-axis
48  'zVertexParams': [-50.0, 50.0]} # target range for training
49 
50 background = True
51 bkgdir = '/sw/belle2/bkg.mixing/'
52 
53 
54 # ------------------------- #
55 # create path up to trigger #
56 # ------------------------- #
57 
58 main = basf2.create_path()
59 
60 # The CDCTriggerNeuroTrainer module stops the event loop when there is enough data,
61 # so just put a very high number of events here.
62 main.add_module('EventInfoSetter', evtNumList=1000000000)
63 main.add_module('Progress')
64 main.add_module('Gearbox')
65 main.add_module('Geometry', components=['BeamPipe', 'Cryostat',
66  'PXD', 'SVD', 'CDC',
67  'MagneticFieldConstant4LimitedRCDC'])
68 particlegun = basf2.register_module('ParticleGun')
69 particlegun.param(particlegun_params)
70 main.add_module(particlegun)
71 main.add_module('FullSim')
72 if background:
73  main.add_module('BeamBkgMixer',
74  backgroundFiles=glob.glob(os.path.join(bkgdir, '*usual*.root')),
75  components=['CDC'])
76 main.add_module('CDCDigitizer')
77 
78 
79 # -------------------------------------- #
80 # add trigger modules up to neurotrigger #
81 # -------------------------------------- #
82 
83 main.add_module('CDCTriggerTSF',
84  InnerTSLUTFile=Belle2.FileSystem.findFile("data/trg/cdc/innerLUT_Bkg_p0.70_b0.80.coe"),
85  OuterTSLUTFile=Belle2.FileSystem.findFile("data/trg/cdc/outerLUT_Bkg_p0.70_b0.80.coe"))
86 main.add_module('CDCTrigger2DFinder')
87 # For single tracks the event time estimate is not very reliable,
88 # so use the true event time here and hope for the best...
89 main.add_module('CDCTriggerETF', trueEventTime=True)
90 
91 
92 # ---------------- #
93 # add the training #
94 # ---------------- #
95 
96 # To get target values for the training, we need relations between 2D track and MCParticles.
97 # We only want matched tracks (no clones) with many true hits.
98 main.add_module('CDCTriggerMCMatcher', minAxial=4, axialOnly=True,
99  relateClonesAndMerged=False,
100  TrgTrackCollectionName='TRGCDC2DFinderTracks')
101 
102 main.add_module('CDCTriggerNeuroTrainer',
103  # input and target arrays
104  inputCollectionName='TRGCDC2DFinderTracks',
105  targetCollectionName='MCParticles',
106  trainOnRecoTracks=False, # train with MCParticles as targets
107  # output files
108  filename=os.path.join(mlpdir, mlpname),
109  trainFilename=os.path.join(traindir, trainname),
110  logFilename=os.path.join(logdir, logname),
111  # sector definition
112  nMLP=5, # total number of sectors
113  # hit pattern sectorization (5 sectors for different missing stereo hits)
114  SLpatternMask=[int('010101010', 2)], # ignore axial hits in sector selection
115  SLpattern=[int('111111111', 2), # full hits
116  int('101111111', 2), # SL 7 missing
117  int('111011111', 2), # SL 5 missing
118  int('111110111', 2), # SL 3 missing
119  int('111111101', 2)], # SL 1 missing
120  # phase space sectorization (trivial here, only 1 sector)
121  invptRange=[[-5., 5.]], # sectorization in charge/pt
122  phiRange=[[0., 360.]], # sectorization in phi
123  thetaRange=[[0., 180.]], # sectorization in theta (requires 3D input tracks)
124  selectSectorByMC=False, # use 2D track parameters to select sector
125  invptRangeTrain=[[-5., 5.]], # sector ranges during train may overlap
126  phiRangeTrain=[[0., 360.]], # i.e. they can be larger than the final sectors
127  thetaRangeTrain=[[0., 180.]], # (will be shrunk after training)
128  # network structure
129  multiplyHidden=False, # set the number of hidden nodes directly
130  nHidden=[[81]], # 1 hidden layer with 81 nodes for all sectors
131  wMax=63., # limit weights to [-63, 63]
132  # target definition
133  targetZ=True, # output z-vertex
134  targetTheta=True, # output also polar angle
135  outputScale=[[-50., 50., 0., 180.]], # output 1 (z) scaled to [-50, 50]cm,
136  # output 2 (theta) scaled to [0, 180]deg
137  rescaleTarget=False, # targets outside of output range are skipped
138  # relevant ID ranges: region around 2D track from which hits are taken
139  # (determined from a histogram that is generated from hits related to MCParticles)
140  nTrainPrepare=1000, # number of tracks used to prepare the ID histogram
141  relevantCut=0.02, # cut on the ID histogram
142  cutSum=False, # cut directly on the ID histogram bins
143  # training parameters
144  multiplyNTrain=True, # set training data relative to degrees of freedom
145  nTrainMax=10., # training data (10x degrees of freedom)
146  nTrainMin=10., # don't train if there is less than 10x DoF training data
147  nValid=1000, # number of validation samples (to avoid overtraining)
148  nTest=5000, # number of test samples (to select best of several runs)
149  repeatTrain=10, # train each sector 10x with different initial weights
150  checkInterval=500, # stop training if validation error does not improve for 500 epochs
151  maxEpochs=10000, # stop training after 10000 epochs
152  nThreads=nthreads, # number of parallel threads
153  stopLoop=True, # stop event loop when there is enough training data
154  # log level
155  logLevel=basf2.LogLevel.DEBUG, # show some debug output
156  debugLevel=50)
157 # show only the message of the debug output
158 basf2.logging.set_info(basf2.LogLevel.DEBUG, basf2.LogInfo.LEVEL | basf2.LogInfo.MESSAGE)
159 
160 
161 # Process events
162 basf2.process(main)
163 
164 # Print call statistics
165 print(basf2.statistics)
basf2.process
def process(path, max_event=0)
Definition: __init__.py:25
Belle2::FileSystem::findFile
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...
Definition: FileSystem.cc:147