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