-
Notifications
You must be signed in to change notification settings - Fork 1
/
_DREvent.py
124 lines (100 loc) · 3.62 KB
/
_DREvent.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# Conversion fron ascii data format to DREvent class
'''
New DREvent class for 2023 test-beam. The header of the event contains four more words than it used to in 2021. For the moment this version of the DREvent class simply shifts the reading of the header to read off the same contents than the 2021 data
TO BE DONE: implement teh readout of teh four new words
'''
class DREvent:
''' Class that represent a Dual Readout event at TB 2021 @H8 '''
def __init__(self):
''' Constructor '''
self.EventTime = ""
self.EventNumber = 0
self.SpillNumber = 0
self.NumOfPhysEv = 0
self.NumOfPedeEv = 0
self.NumOfSpilEv = 0
self.TriggerMask = 0
self.ADCs = {} # Simple dict key(channel) : value
self.TDCs = {} # Dict key:tuple key(channel) : ( value, check )
def headLine(self):
"""Write header in ascii data dump"""
return "%8s, %8s, %8s, %8s, %10s, %6s, %6s" % (
"evNum", "#phEv", "#peEv", "#spEv", "trigM",
"#ADCs", "#TDCs", )
def __str__(self):
"""Overload of str operator (for data dump)"""
return "%8d, %8d, %8d, %8d, 0x%08x, %6d, %6d" % (
self.EventNumber, self.NumOfPhysEv, self.NumOfPedeEv, self.NumOfSpilEv, self.TriggerMask,
len(self.ADCs), len(self.TDCs) )
def getAdcChannel(self, ch):
"""get the data value for ADC channel ch"""
return self.ADCs[ch]
def getTdcChannel(self, ch):
"""get the data value for TDC channel ch"""
return self.TDCs[ch] # Tuple: (value, check)
# Parse the evLine and return a DREvent object
def DRdecode(evLine):
"""Function that converts a raw data record (event) from
ascii to object oriented representation: DREvent class"""
# Create new DREvent
e = DREvent()
# Split sections
strHeader = evLine.split(":")[0]
strPayload = evLine.split(":")[1]
strPayload = strPayload.replace("TDC", ":TDC")
strADCs = strPayload.split(":")[0]
strTDCs = strPayload.split(":")[1]
# Parse strHeader
hList = strHeader.split()
e.EventNumber = int( hList[2] )
e.EventTime = hList[4]
e.SpillNumber = int( hList[6] )
e.NumOfPhysEv = int( hList[9] )
e.NumOfPedeEv = int( hList[10] )
e.NumOfSpilEv = int( hList[11] )
try:
e.TriggerMask = int( hList[14], 16 )
except ValueError:
print 'ERROR: INVALID TRIGGER MASK:', hList[14]
e.TriggerMask = 0xFFFFFFFF
# Parse ADC
listADCs = strADCs.split()
for i in range(len(listADCs)):
if i%2 ==0:
ch = int(listADCs[i] , 10)
val= int(listADCs[i+1], 16)
e.ADCs[ch]=val
# Parse TDC
entries = -1
try: entries = int(strTDCs.split()[2], 10)
except ValueError:
# In the 1st runs the TCD size was exadecimal, then it was changed in decimal
print "WARNING: TCD size not in decimal format, trying exadecimal"
try: entries = int(strTDCs.split()[2], 16)
except ValueError:
print "WARNING: TCD size with unknown format."
pass
strTDCs = strTDCs[ strTDCs.find("val.s") + 6 : ]
listTDCs = strTDCs.split()
if entries > 0:
for i in range(len(listTDCs)):
if i%3 ==0:
ch = int( listTDCs[i+0], 10 ) # Channel
ver = int( listTDCs[i+1], 10 ) # Varification number
val = int( listTDCs[i+2], 10 ) # Value
e.TDCs[ch] = ( val, ver)
return e
# Main for testing purpose
if __name__ == "__main__":
import sys
if len(sys.argv) < 2:
print "Usage:", sys.argv[0], "filename [v=verbose]"
verbose = False
if len(sys.argv) == 3:
verbose = True
for i, line in enumerate( open( sys.argv[1] ) ):
ev = DRdecode(line)
if verbose:
if i%30 == 0:
print ev.headLine()
print ev