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