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