File indexing completed on 2023-03-17 11:00:29
0001
0002
0003 import struct
0004 import os,sys
0005 import json
0006 import shutil
0007
0008 os.umask(0)
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061 class header_shifts:
0062 bxmatch = 32;
0063 mAcount = 16;
0064 orbitmatch = 8;
0065 mBcount = 0;
0066
0067 class header_masks:
0068 bxmatch = 0xff << header_shifts.bxmatch;
0069 mAcount = 0xf << header_shifts.mAcount;
0070 orbitmatch = 0xff << header_shifts.orbitmatch;
0071 mBcount = 0xf
0072
0073
0074
0075 class frd_file_header_v2:
0076 ver_id = "RAW_0002".encode()
0077 header_size = 32
0078 data_type = 20
0079 event_count = 0
0080 run_number = 0
0081 lumisection = 0
0082 file_size = 0
0083
0084
0085 def parseMuonScoutingRawFile(infilepath, outdir, rn_override, maxorbits):
0086
0087 if infilepath != 'stdin':
0088 fin = open(infilepath,'rb')
0089 else:
0090 fin = sys.stdin.buffer
0091
0092
0093
0094
0095 orbitcount=0
0096
0097 orbitcount_total=0
0098
0099 last_ls = 0
0100
0101 orbit_data = bytes()
0102 orbit_nr = 0
0103 orbit_size = 0
0104 flags = 0
0105 c_crc32c = 0
0106
0107
0108
0109 version = 6
0110
0111
0112 fout = None
0113 if infilepath != 'stdin':
0114 fin = open(infilepath,'rb')
0115 else:
0116 fin = sys.stdin.buffer
0117
0118
0119
0120 def update_header():
0121 nonlocal orbitcount
0122 nonlocal last_ls
0123 h = frd_file_header_v2()
0124 h.event_count = orbitcount
0125 h.run_number = rn_override
0126 h.lumisection = last_ls
0127 h.file_size = fout.tell()
0128 fout.seek(0, 0)
0129 fout.write(frd_file_header_v2.ver_id)
0130 fout.write(struct.pack('H',h.header_size))
0131 fout.write(struct.pack('H',h.data_type))
0132 fout.write(struct.pack('I',h.event_count))
0133 fout.write(struct.pack('I',h.run_number))
0134 fout.write(struct.pack('I',h.lumisection))
0135 fout.write(struct.pack('Q',h.file_size))
0136
0137 orbitcount = 0
0138 print(h.ver_id, h.header_size, h.data_type, h.event_count, h.lumisection, h.file_size)
0139
0140
0141
0142 def write_orbit():
0143 nonlocal orbit_size
0144 nonlocal orbit_data
0145 if not orbit_size:
0146 return
0147
0148
0149 fout.write(struct.pack('H',version))
0150 fout.write(struct.pack('H',flags))
0151 fout.write(struct.pack('I',rn_override))
0152
0153 fout.write(struct.pack('I',last_ls))
0154 fout.write(struct.pack('I',orbit_nr))
0155 fout.write(struct.pack('I',orbit_size))
0156 fout.write(struct.pack('I',c_crc32c))
0157 fout.write(orbit_data)
0158
0159 orbit_data = bytes()
0160 orbit_size = 0
0161
0162 def writeout_close():
0163 write_orbit()
0164 update_header()
0165 fout.close()
0166 orbit_nr = 0
0167
0168
0169 while True:
0170
0171
0172 if orbitcount_total > maxorbits:
0173 print(f"finish: {orbitcount_total-1}/{maxorbits} orbits")
0174 writeout_close()
0175
0176 if infilepath != 'stdin':
0177 fin.close()
0178 sys.exit(0)
0179
0180 try:
0181 h_raw = fin.read(4)
0182 bxmatch = struct.unpack('B', h_raw[3:4])[0]
0183 mAcount = struct.unpack('B', h_raw[2:3])[0]
0184 orbitmatch = struct.unpack('B', h_raw[1:2])[0]
0185 mBcount = struct.unpack('B', h_raw[0:1])[0]
0186
0187
0188
0189 bx_raw = fin.read(4)
0190 bx = struct.unpack('i', bx_raw)[0]
0191
0192 orbit_raw = fin.read(4)
0193 orbit = struct.unpack('i', orbit_raw)[0]
0194
0195 new_ls = orbit >> 18
0196
0197 if new_ls > last_ls:
0198
0199 if last_ls:
0200 write_orbit()
0201 update_header()
0202 fout.close()
0203 orbitcount = 0
0204
0205 last_ls = new_ls
0206 fout = open(os.path.join(outdir, f'run{rn_override}_ls{str(new_ls).zfill(4)}_index000000.raw') ,'wb')
0207
0208 fout.write(frd_file_header_v2.ver_id)
0209
0210 fout.write(bytes(24))
0211
0212 read_len = 8*(mAcount+mBcount)
0213 mu_blk = fin.read(8*(mAcount+mBcount))
0214 if len(mu_blk) != read_len:
0215 print('incomplete read')
0216 sys.exit(1)
0217
0218 if not orbit_nr or orbit != orbit_nr:
0219
0220 if orbit_nr:
0221 write_orbit()
0222
0223
0224 if orbit < orbit_nr:
0225 orbit_count = -1
0226 print("got smaller orbit than earlier!")
0227 sys.exit(1)
0228
0229 print("new orbit", orbit)
0230 orbit_nr = orbit
0231
0232
0233 orbitcount += 1
0234
0235 orbitcount_total += 1
0236
0237
0238 orbit_size += 12 + read_len
0239 orbit_data += (h_raw + bx_raw + orbit_raw) + mu_blk
0240
0241 except Exception as ex:
0242
0243 print(f"exception: {ex}")
0244
0245
0246
0247 sys.exit(1)
0248
0249
0250
0251
0252 if len(sys.argv) < 5:
0253 print("parameters: input file (or stdin), output directory, run number (use same as input file), orbits to write")
0254 else:
0255 parseMuonScoutingRawFile(sys.argv[1], sys.argv[2], int(sys.argv[3]), int(sys.argv[4]))
0256
0257
0258
0259