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