Belle II Software development
EclCNNPID.py
1#!/usr/bin/env python3
2
3
10
11""" This script creates 10 events with 12 charged stable particles
12 per event, then extracts CNN value for each particle.
13 The CNN values represent probabilities for a track being
14 muon or pion like.
15
16INPUT:
17 No input
18
19USAGE:
20 basf2 EclCNNPID.py -- [--beam-bkg-dir <path_to_beam_bkg_files>]
21
22EXAMPLE:
23 basf2 EclCNNPID.py
24
25IMPORTANT NOTE:
26 In this example script Gearbox and Geometry modules
27 are automatically registered in add_simulation().
28
29 In order to use CNN_PID_ECL module in your script,
30 it is essential to add the following lines before
31 fillParticleList() function:
32 mainPath.add_module('Gearbox')
33 mainPath.add_module('Geometry')
34"""
35
36
37import argparse
38
39
40def argparser():
41 parser = argparse.ArgumentParser()
42
43 parser.add_argument(
44 '--beam-bkg-dir',
45 type=str,
46 default=None,
47 help='Directory that contains beam background files')
48
49 return parser
50
51
52if __name__ == '__main__':
53
54 args = argparser().parse_args()
55
56 import basf2 as b2 # noqa
57 from ROOT import Belle2 # noqa
58 import modularAnalysis as ma # noqa
59 from simulation import add_simulation # noqa
60 from reconstruction import add_reconstruction # noqa
61 from background import get_background_files # noqa
62 from eclCNNPID import CNN_PID_ECL # noqa
63
64 chargedStable_pdg_list = []
65 for idx in range(len(Belle2.Const.chargedStableSet)):
66 particle = Belle2.Const.chargedStableSet.at(idx)
67 pdgId = particle.getPDGCode()
68 chargedStable_pdg_list.extend([pdgId, -pdgId])
69
70 # Necessary global tag for CNN_PID_ECL module
71 b2.conditions.prepend_globaltag('cnn_pid_ecl')
72
73 # Path
74 mainPath = b2.create_path()
75
76 b2.set_log_level(b2.LogLevel.WARNING)
77
78 # Register ParticleGun module
79 particleGun = b2.register_module('ParticleGun')
80 b2.set_random_seed(123)
81 particleGun_param = {
82 'pdgCodes': chargedStable_pdg_list,
83 'nTracks': 0, # 0 means it generates 1 track per pdgId per event.
84 'momentumGeneration': 'uniformPt',
85 'momentumParams': [0.3, 0.9],
86 'thetaGeneration': 'uniform',
87 'thetaParams': [70, 90], # In the ECL barrel
88 'phiGeneration': 'uniform',
89 'phiParams': [0, 360],
90 'xVertexParams': [0.0, 0.0],
91 'yVertexParams': [0.0, 0.0],
92 'zVertexParams': [0.0, 0.0],
93 }
94 particleGun.param(particleGun_param)
95 mainPath.add_module(particleGun)
96
97 mainPath.add_module(
98 'EventInfoSetter',
99 expList=1003, # Exp 1003 is early phase 3
100 runList=0,
101 evtNumList=10)
102
103 if args.beam_bkg_dir:
104 # Simulation with beam background
105 add_simulation(
106 mainPath,
107 bkgfiles=get_background_files(
108 folder=args.beam_bkg_dir
109 )
110 )
111 else:
112 # Simulation
113 add_simulation(mainPath)
114
115 # Reconstruction
116 add_reconstruction(mainPath)
117
118 # Add ECLFillCellIdMapping module
119 mainPath.add_module('ECLFillCellIdMapping')
120
121 # CNN_PID_ECL module
122 mainPath.add_module(CNN_PID_ECL(path=mainPath))
123
124 name = 'pi+:particles'
125 ma.fillParticleList(name, '', path=mainPath)
126
127 ma.variablesToNtuple(
128 decayString=name,
129 variables=['cnn_pid_ecl_muon'],
130 treename='particles',
131 filename='test_chargedStable_particles_cnn_output.root',
132 path=mainPath
133 )
134
135 mainPath.add_module('Progress')
136 b2.process(mainPath)
137 print(b2.statistics)