-
Notifications
You must be signed in to change notification settings - Fork 0
/
TimeCalibration.py
executable file
·195 lines (148 loc) · 5.18 KB
/
TimeCalibration.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import math
import ROOT
import Base
import Configuration
import Globals
from array import array
from datetime import date
def solveLine(slope, offset, y):
return (y - offset)/slope
class ChannelTDC:
def __init__(self):
print "Calib"
self.board = -1
self.bank = -1
self.channel = -1
self.injectTimes = array("d")
self.iBTs = array("d") # Array for the level of current on the TAC - affects TDC slope
self.tdcPeaks = array("d")
self.numPoints = 0
def setLocation(self, board, bank, channel):
self.board = board
self.bank = bank
self.channel = channel
def addChannel(self, channel, time, ibt=0):
fitInfo = self.fitTdcPeak(channel)
self.addTdcPeak(time, fitInfo)
self.iBTs.append(ibt)
def fitTdcPeak(self, channel):
spectrum = ROOT.TSpectrum()
numFoundPeaks = spectrum.Search(channel.tdcHist,2,"", 0.01 )
if (numFoundPeaks == 1):
peaks = spectrum.GetPositionX()
tdcPeak = peaks[0]
return tdcPeak
else:
print "More or less than one peak found - defaulting to TDC mean"
tdcPeak = channel.tdcHist.GetMean()
return tdcPeak
def addTdcPeak(self, injectTDC, peak):
self.numPoints += 1
self.injectTimes.append(injectTDC)
self.tdcPeaks.append(peak)
def makeSaturationGraph(self):
saturated = 0
tdcs = array('f')
ibts = array('f')
for peak in range(len(self.tdcPeaks)):
if (self.tdcPeaks[peak] < 255):
tdcs.append(self.tdcPeaks[peak]
ibts.append(self.iBTs[peak]
else:
saturated += 1
self.saturationGraph = ROOT.TGraph(len(ibts), ibts, tdcs)
def makeGraph(self):
try:
self.graph = ROOT.TGraph(self.numPoints, self.injectTimes, self.tdcPeaks)
name = str(self.board) + "-" + str(self.bank) + "-" + str(self.channel) + "-TDCGraph"
self.graph.SetName(name)
self.graph.Write()
except:
try:
print self.numPoints
except:
print "bad num points"
try:
print len(self.injectTimes)
except:
print "bad inject times"
try:
print len(self.tdcPeaks)
except:
print "bad peaks"
class TimeCalibrator:
def __init__(self):
print "Galifray? Meh."
self.chanTDCs = []
self.ibtCalibration = CalibrationTypes.IBTCalibration()
def setup(self):
for board in range(Globals.BOARDS):
for bank in range(Globals.BANKS):
for channel in range(Globals.CHANNELS):
chanTDC = ChannelTDC()
chanTDC.setLocation(board, bank, channel)
self.chanTDCs.append(chanTDC)
def loadDataCampaign(self, campaign):
self.dataCampaign = campaign
configuration = Configuration.ConfigurationReader(campaign)
i = 0
for run in self.dataCampaign.runs:
print "Looking at run " + str(run.runNumber)
settings = configuration.loadSettings(run.runNumber)
injectTDC = (settings.getIntegrationWindow() - settings.getInjectTDC())
run.setup()
readeri = Base.DATEReader(self.dataCampaign.fileNames[i])
print "Starting to analyse file:"
print self.dataCampaign.fileNames[i]
readeri.readBinary(run)
for channel in run.channelRuns:
uniqueId = channel.uniqueChan
self.chanTDCs[uniqueId].addChannel(channel, injectTDC)
i += 1
def findIBTLimits(self):
for timeCircuit in self.chanTDCs:
timeCircuit.makeSaturationGraph()
timeCircuit.saturationGraph.Fit("pol1")
fit = timeCircuit.saturationGraph.GetFunction("pol1")
slope = fit.GetParameter(1)
offset = fit.GetParameter(0)
saturated = timeCircuit.numSaturatedPoints
limit = solveLine(slope, offset, 255,0)
self.findTriptObject(timeCircuit.board, timeCircuit.bank, timeCircuit.channel).addIBTLimit(limit)
def findIBTCalibration(self):
for tript in self.triptObjects:
ibt = tript.getOptimumIBT()
self.ibtCalibration.addIBTValue(tript.board, tript.module, tript.tript, ibt)
def writeIBTCalibration(self):
dbManager = DataBase.DataBaseManager()
dbManager.writeIBTCalibration(self.ibtCalibration)
def findTDCCalibration(self):
today = date.today()
self.calibFile = open("TDC-Calib.txt","w")
for timeCircuit in self.chanTDCs:
timeCircuit.makeGraph()
timeCircuit.graph.Fit("pol1")
fit = timeCircuit.graph.GetFunction("pol1")
slope = fit.GetParameter(1)
pedestal = fit.GetParameter(0)
self.calibration.addTDCEntry(timeCircuit.board, timeCircuit.bank, timeCircuit.channel, pedestal, slope)
#string = str(timeCircuit.board) + " " + str(timeCircuit.bank) + " " + str(timeCircuit.channel) + " "
#string += str(pedestal) + " " + str(slope) + "\n"
#self.calibFile.write(string)
def findTriptObject(self, board, bank, channel):
for object in self.triptObjects:
module = (bank*2) + (channel/Globals.MODULECHANNELS)
tript = ((channel+1) / Globals.TRIPCHANNELS) % Globals.MODULETRIPS
if (board == object.board and module == object.module and tript == object.tript):
return object
class TriPtObject(object):
def __init__(self, board, bank, channel):
self.limits = []
self.board = board
self.module = (bank*2) + (channel/Globals.MODULECHANNELS)
self.tripId = ((channel+1) / Globals.TRIPCHANNELS) % Globals.MODULETRIPS
def addIBTLimit(self, limit):
self.limits.append(limit)
def findOptimumIBT(self):
self.limits.sort()
return self.limits[0]