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