Belle II Software development
makeBGOverlayMCFile.py
1#!/usr/bin/env python3
2
3
10
11from basf2 import create_path, set_log_level, B2ERROR, B2INFO, LogLevel, process, statistics
12import os
13from svd import add_svd_simulation
14import glob
15import sys
16
17# -----------------------------------------------------------------------------------
18# Prepare a root file for BG overlay using simulated BG samples
19#
20# Usage:
21# basf2 makeBGOverlayMCFile.py phase [scaleFactor PXDmode]
22# Arguments:
23# phase one of: phase2, phase3, phase31
24# scaleFactor overall scaling factor (default=1)
25# PXDmode one of: ungated, gated (default=ungated)
26# Default output file: BGforOverlay.root
27# -----------------------------------------------------------------------------------
28
29# argument parsing
30argvs = sys.argv
31if len(argvs) > 1:
32 if argvs[1] == 'phase2':
33 compression = 3
34 expNo = 1002
35 elif argvs[1] == 'phase3':
36 compression = 4
37 expNo = 0
38 elif argvs[1] == 'phase31':
39 compression = 4
40 expNo = 1003
41 else:
42 B2ERROR('The argument can be either phase2, phase3 or phase31')
43 sys.exit()
44else:
45 B2ERROR('No argument given specifying the running phase')
46 print('Usage: basf2 ' + argvs[0] + ' phase2/phase3/phase31 [scaleFactor=1 ' +
47 'PXDmode=ungated/gated]')
48 print()
49 sys.exit()
50
51scaleFactor = 1.0
52if len(argvs) > 2:
53 scaleFactor = float(argvs[2])
54
55gatedMode = False
56mode = 'ungated'
57if len(argvs) > 3 and argvs[3] == 'gated':
58 gatedMode = True
59 mode = 'gated'
60
61# background files
62if 'BELLE2_BACKGROUND_MIXING_DIR' not in os.environ:
63 B2ERROR('BELLE2_BACKGROUND_MIXING_DIR variable is not set - it must contain the path to BG samples')
64 B2INFO('on KEKCC: /sw/belle2/bkg.mixing')
65 sys.exit()
66
67bg = glob.glob(os.environ['BELLE2_BACKGROUND_MIXING_DIR'] + '/*.root')
68
69if len(bg) == 0:
70 B2ERROR('No root files found in folder ' + os.environ['BELLE2_BACKGROUND_MIXING_DIR'])
71 sys.exit()
72
73# extra files for adding SVD pick-up noise
74if 'BELLE2_BACKGROUND_MIXING_EXTRA_DIRS' not in os.environ:
75 B2ERROR('BELLE2_BACKGROUND_MIXING_EXTRA_DIRS variable is not set - it must contain the path to extra samples')
76 B2INFO('on KEKCC: /sw/belle2/bkg.mixing_extra')
77 sys.exit()
78
79SVDOverlayDir = os.environ['BELLE2_BACKGROUND_MIXING_EXTRA_DIRS'] + '/SVD'
80SVDOverlayFiles = glob.glob(SVDOverlayDir + '/*_overlay.root')
81
82if len(SVDOverlayFiles) == 0:
83 B2ERROR('No root files found in folder ' + SVDOverlayDir)
84 sys.exit()
85
86B2INFO('Making BG overlay sample for ' + argvs[1] + ' with ECL compression = ' +
87 str(compression) + ' and PXD in ' + mode + ' mode')
88B2INFO('Using background samples from folder ' + os.environ['BELLE2_BACKGROUND_MIXING_DIR'])
89B2INFO('With scaling factor: ' + str(scaleFactor))
90B2INFO('With SVD xTalk&Noise files from folder ' + str(SVDOverlayDir))
91
92set_log_level(LogLevel.WARNING)
93
94# Create path
95main = create_path()
96
97# Set number of events to generate
98main.add_module('EventInfoSetter', evtNumList=[100], expList=[expNo])
99
100# Gearbox: access to xml files
101main.add_module('Gearbox')
102
103# Geometry
104main.add_module('Geometry')
105
106# Simulate the EventLevelTriggerTimeInfo (empty object for now)
107main.add_module('SimulateEventLevelTriggerTimeInfo')
108
109if gatedMode:
110 # Beam background mixer
111 main.add_module('BeamBkgMixer', backgroundFiles=bg, overallScaleFactor=scaleFactor,
112 minTimePXD=-20000.0, maxTimePXD=20000.0)
113
114 # Emulate injection vetos for PXD
115 main.add_module('PXDInjectionVetoEmulator')
116
117 # PXD digitizer (no data reduction!)
118 main.add_module('PXDDigitizer')
119else:
120 # Beam background mixer
121 main.add_module('BeamBkgMixer', backgroundFiles=bg, overallScaleFactor=scaleFactor)
122
123 # PXD digitizer (no data reduction!)
124 main.add_module('PXDDigitizer', IntegrationWindow=False)
125
126# SVD digitization
127add_svd_simulation(main)
128# SVD overlay of random trigger data to add SVD Noise & xTalk
129main.add_module('BGOverlayInput', skipExperimentCheck=True, bkgInfoName='BackgroundInfoSVDOverlay', inputFileNames=SVDOverlayFiles)
130main.add_module('BGOverlayExecutor', bkgInfoName='BackgroundInfoSVDOverlay')
131main.add_module('SVDShaperDigitSorter')
132
133# CDC digitization
134main.add_module('CDCDigitizer')
135
136# TOP digitization
137main.add_module('TOPDigitizer')
138
139# ARICH digitization
140main.add_module('ARICHDigitizer')
141
142# ECL digitization
143main.add_module('ECLDigitizer', WaveformMaker=True, CompressionAlgorithm=compression)
144
145# KLM digitization
146main.add_module('KLMDigitizer')
147
148# ECL trigger (generate BGOverlay dataobject for ecl trigger)
149main.add_module('TRGECLBGTCHit')
150
151# Output: digitized hits only
152branches = ['EventLevelTriggerTimeInfo', 'PXDDigits', 'SVDShaperDigits', 'CDCHits', 'TOPDigits',
153 'ARICHDigits', 'ECLWaveforms', 'KLMDigits', 'TRGECLBGTCHits']
154if gatedMode:
155 branches += ['PXDInjectionBGTiming']
156main.add_module('RootOutput', outputFileName='BGforOverlay.root', branchNames=branches)
157
158# Show progress of processing
159main.add_module('Progress')
160
161# Process events
162process(main)
163
164# Print call statistics
165print(statistics)