Belle II Software  release-08-01-10
beamBkgMixer.py
1 #!/usr/bin/env python3
2 
3 
10 
11 import basf2 as b2
12 import os
13 import sys
14 import glob
15 
16 # ----------------------------------------------------------------------------------
17 # This example shows some of the possibilities to steer BG mixing with BeamBkgMixer.
18 #
19 # For simplicity no particle generator and/or geant simulation is included,
20 # digitization and event reconstruction is also not done. The output root file
21 # will contain SimHits from BG.
22 #
23 # Note: in multiprocessing mode put BG mixer in the path first then FullSim
24 # ----------------------------------------------------------------------------------
25 
26 b2.set_log_level(b2.LogLevel.INFO)
27 
28 
29 if 'BELLE2_BACKGROUND_MIXING_DIR' not in os.environ:
30  b2.B2ERROR('BELLE2_BACKGROUND_MIXING_DIR variable is not set - it must contain the path to BG mixing samples')
31  sys.exit()
32 
33 # define background (collision) files
34 # glob.glob is the prefered way to get the list of files:
35 # (the directory must include only BG files!)
36 
37 bg = glob.glob(os.environ['BELLE2_BACKGROUND_MIXING_DIR'] + '/*.root')
38 if len(bg) == 0:
39  b2.B2ERROR('No files found in ', os.environ['BELLE2_BACKGROUND_MIXING_DIR'])
40  sys.exit()
41 
42 # alternative: you can specify files explicitely
43 #
44 # dir = '/sw/belle2/bkg/' # change the directory name if you don't run on KEKCC
45 # bg = [
46 # dir + 'Coulomb_HER_100us.root',
47 # dir + 'Coulomb_LER_100us.root',
48 # dir + 'RBB_HER_100us.root',
49 # dir + 'RBB_LER_100us.root',
50 # dir + 'Touschek_HER_100us.root',
51 # dir + 'Touschek_LER_100us.root',
52 # ]
53 # change the file names if differ, remove some or add some more files
54 
55 # Create path
56 main = b2.create_path()
57 
58 # Set number of events to generate
59 eventinfosetter = b2.register_module('EventInfoSetter')
60 eventinfosetter.param({'evtNumList': [10], 'runList': [1]})
61 main.add_module(eventinfosetter)
62 
63 # Gearbox: access to database (xml files)
64 gearbox = b2.register_module('Gearbox')
65 main.add_module(gearbox)
66 
67 # Geometry
68 geometry = b2.register_module('Geometry')
69 main.add_module(geometry)
70 
71 # Simulate the EventLevelTriggerTimeInfo (empty object for now)
72 simulateEventLevelTriggerTimeInfo = b2.register_module('SimulateEventLevelTriggerTimeInfo')
73 main.add_module(simulateEventLevelTriggerTimeInfo)
74 
75 # particle generator can be put here
76 
77 # Mix beam background
78 bkgmixer = b2.register_module('BeamBkgMixer')
79 bkgmixer.param('backgroundFiles', bg) # specify BG files
80 bkgmixer.param('components', ['CDC', 'TOP', 'ECL']) # mix BG only for those components
81 bkgmixer.param('minTime', -5000) # set time window start time [ns]
82 bkgmixer.param('maxTime', 10000) # set time window stop time [ns]
83 bkgmixer.param('scaleFactors', [('Coulomb_LER', 1.05), ('Coulomb_HER', 1.08), ('RBB_LER', 0.8)]) # scale rates of some backgrounds
84 main.add_module(bkgmixer)
85 
86 # FullSim, digitizers, clusterizers and reconstruction modules can be put here
87 
88 # Output
89 output = b2.register_module('RootOutput')
90 output.param('outputFileName', 'testMixer.root')
91 main.add_module(output)
92 
93 # Show progress of processing
94 progress = b2.register_module('Progress')
95 main.add_module(progress)
96 
97 # Process events
98 b2.process(main)
99 
100 # Print call statistics
101 print(b2.statistics)