Belle II Software  release-08-01-10
makeBGOverlayMCFile.py
1 #!/usr/bin/env python3
2 
3 
10 
11 from basf2 import create_path, set_log_level, B2ERROR, B2INFO, LogLevel, process, statistics
12 import os
13 from svd import add_svd_simulation
14 import glob
15 import 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
30 argvs = sys.argv
31 if 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()
44 else:
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 
51 scaleFactor = 1.0
52 if len(argvs) > 2:
53  scaleFactor = float(argvs[2])
54 
55 gatedMode = False
56 mode = 'ungated'
57 if len(argvs) > 3 and argvs[3] == 'gated':
58  gatedMode = True
59  mode = 'gated'
60 
61 # background files
62 if '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 
67 bg = glob.glob(os.environ['BELLE2_BACKGROUND_MIXING_DIR'] + '/*.root')
68 
69 if 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
74 if '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 
79 SVDOverlayDir = os.environ['BELLE2_BACKGROUND_MIXING_EXTRA_DIRS'] + '/SVD'
80 SVDOverlayFiles = glob.glob(SVDOverlayDir + '/*_overlay.root')
81 
82 if len(SVDOverlayFiles) == 0:
83  B2ERROR('No root files found in folder ' + SVDOverlayDir)
84  sys.exit()
85 
86 B2INFO('Making BG overlay sample for ' + argvs[1] + ' with ECL compression = ' +
87  str(compression) + ' and PXD in ' + mode + ' mode')
88 B2INFO('Using background samples from folder ' + os.environ['BELLE2_BACKGROUND_MIXING_DIR'])
89 B2INFO('With scaling factor: ' + str(scaleFactor))
90 B2INFO('With SVD xTalk&Noise files from folder ' + str(SVDOverlayDir))
91 
92 set_log_level(LogLevel.WARNING)
93 
94 # Create path
95 main = create_path()
96 
97 # Set number of events to generate
98 main.add_module('EventInfoSetter', evtNumList=[100], expList=[expNo])
99 
100 # Gearbox: access to xml files
101 main.add_module('Gearbox')
102 
103 # Geometry
104 main.add_module('Geometry')
105 
106 # Simulate the EventLevelTriggerTimeInfo (empty object for now)
107 main.add_module('SimulateEventLevelTriggerTimeInfo')
108 
109 if 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')
119 else:
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
127 add_svd_simulation(main)
128 # SVD overlay of random trigger data to add SVD Noise & xTalk
129 main.add_module('BGOverlayInput', skipExperimentCheck=True, bkgInfoName='BackgroundInfoSVDOverlay', inputFileNames=SVDOverlayFiles)
130 main.add_module('BGOverlayExecutor', bkgInfoName='BackgroundInfoSVDOverlay')
131 main.add_module('SVDShaperDigitSorter')
132 
133 # CDC digitization
134 main.add_module('CDCDigitizer')
135 
136 # TOP digitization
137 main.add_module('TOPDigitizer')
138 
139 # ARICH digitization
140 main.add_module('ARICHDigitizer')
141 
142 # ECL digitization
143 main.add_module('ECLDigitizer', WaveformMaker=True, CompressionAlgorithm=compression)
144 
145 # KLM digitization
146 main.add_module('KLMDigitizer')
147 
148 # ECL trigger (generate BGOverlay dataobject for ecl trigger)
149 main.add_module('TRGECLBGTCHit')
150 
151 # Output: digitized hits only
152 branches = ['EventLevelTriggerTimeInfo', 'PXDDigits', 'SVDShaperDigits', 'CDCHits', 'TOPDigits',
153  'ARICHDigits', 'ECLWaveforms', 'KLMDigits', 'TRGECLBGTCHits']
154 if gatedMode:
155  branches += ['PXDInjectionBGTiming']
156 main.add_module('RootOutput', outputFileName='BGforOverlay.root', branchNames=branches)
157 
158 # Show progress of processing
159 main.add_module('Progress')
160 
161 # Process events
162 process(main)
163 
164 # Print call statistics
165 print(statistics)