Belle II Software  release-06-02-00
B2A104-SimulateAndReconstruct-withBeamBkg.py
1 #!/usr/bin/env python3
2 
3 
10 
11 # ########################################################################
12 # #
13 # Stuck? Ask for help at questions.belle2.org #
14 # #
15 # This tutorial demonstrates how to perform detector simulation and #
16 # reconstruction (track finding+track fitting+ecl reconstruction+...) #
17 # on a previously generated events with beam background mixing. #
18 # #
19 # In this example Beam Background is added. See #
20 # B2A103-SimulateAndReconstruct-withoutBeamBkg.py to check #
21 # how to simulate+reconstruct without beam background. #
22 # #
23 # The processed events are saved to the output ROOT file that #
24 # now contain in addition to the generated particles #
25 # (MCParticle objects stored in the StoreArray<MCParticle>) also #
26 # reconstructed MDST objects (Track/ECLCluster/KLMCluster/...). #
27 # #
28 # ########################################################################
29 
30 import basf2 as b2
31 import modularAnalysis as ma
32 import simulation as si
33 import reconstruction as re
34 import glob
35 import os.path
36 
37 
38 # check if the required input file exists (from B2A101 example)
39 if not os.path.isfile('B2A101-Y4SEventGeneration-evtgen.root'):
40  b2.B2FATAL(
41  'Required input file (B2A101-Y4SEventGeneration-evtgen.root) does not exist. \n'
42  'Please run B2A101-Y4SEventGeneration.py tutorial script first.')
43 
44 # create a path
45 my_path = b2.create_path()
46 
47 # load input ROOT file
48 ma.inputMdst('default', 'B2A101-Y4SEventGeneration-evtgen.root', path=my_path)
49 
50 # background files
51 # location of the files is obtained from a shell variable - check first if it is set
52 if 'BELLE2_BACKGROUND_DIR' not in os.environ:
53  b2.B2FATAL(
54  'BELLE2_BACKGROUND_DIR variable is not set. \n'
55  'Please export (setenv) the variable to the location of BG overlay sample. \n'
56  'Check https://confluence.desy.de/display/BI/Beam+background+samples to find them')
57 # get list of files and check the list length
58 bg = glob.glob(os.environ['BELLE2_BACKGROUND_DIR'] + '/*.root')
59 if len(bg) == 0:
60  b2.B2FATAL('No files found in ', os.environ['BELLE2_BACKGROUND_DIR'])
61 
62 # simulation
63 si.add_simulation(path=my_path, bkgfiles=bg)
64 
65 # reconstruction
66 re.add_reconstruction(path=my_path)
67 
68 # dump in MDST format
69 re.add_mdst_output(path=my_path,
70  mc=True,
71  filename='B2A101-Y4SEventGeneration-gsim-BKGx1.root')
72 
73 # Show progress of processing
74 my_path.add_module('ProgressBar')
75 
76 # Process the events
77 b2.process(my_path)
78 
79 # print out the summary
80 print(b2.statistics)