source: fact/tools/pyscripts/sandbox/dneise/rdietlic/ftcal.py@ 13385

Last change on this file since 13385 was 13368, checked in by neise, 13 years ago
initial commit of my sandbox
File size: 6.6 KB
Line 
1#!/usr/bin/python
2#
3# Werner Lustermann
4# ETH Zurich
5#
6import pyfact
7from myhisto import *
8from hist import *
9import numpy as np
10import numpy.random as rnd
11from scipy import interpolate as ip
12from ROOT import *
13from time import time
14from optparse import OptionParser
15
16t_s = time()
17
18#dfname = '/data03/fact-construction/raw/2011/11/24/20111124_121.fits'
19#calfname = '/data03/fact-construction/raw/2011/11/24/20111124_111.drs.fits'
20
21dfname = '/data00/fact-construction/raw/2011/11/24/20111124_113.fits.gz'
22calfname = '/data00/fact-construction/raw/2011/11/24/20111124_111.drs.fits.gz'
23
24rd = pyfact.rawdata( dfname, calfname )
25
26fsampling = 2. # sampling frequency
27freq = 250. # testfrequency
28P_nom = 1000./freq # nominal Period due to testfrequency
29DAMPING = 0.02 # Damping applied to correction
30NChip = 1 # Number of Chips
31NEvents = 300
32
33
34parser = OptionParser()
35parser.add_option('-e', '--extended', action = 'store_true', dest='extend', default = False )
36(options, args) = parser.parse_args()
37
38save = "yes"
39
40if(options.extend):
41
42 NChip = int(raw_input("Wieviele Chips? ([1, ... , "+str(rd.NPIX/9)+"]): "))
43 NEventsp = int(raw_input("Wieviele Events? ([1, ... , "+str(rd.NEvents)+"]) "))
44 save = raw_input("Soll CellTime gespeichert werden? (yes/no) ")
45
46
47h = hist_list(tcalHistograms, NChip, "Chip")
48
49
50# Functions needed for calibration
51
52# find negative-going crossings of mean value
53def Crossing(Data, Mean, t_nom, Start, Chip):
54 TimeXing = np.zeros(rd.NROI)
55 MeanXing = np.zeros(rd.NROI)
56 NumXing = 0
57 CellTime = CellTimef(np.roll(t_nom, -Start))
58
59 for i in range(rd.NROI-1):
60 if ((Data[i] > Mean) & (Data[i+1] < Mean)):
61 MeanXing[NumXing] = i
62
63 FirstCell = CellTime[i]
64 SecondCell = CellTime[i+1]
65
66 TimeXing[NumXing] = FirstCell+(SecondCell-FirstCell)/(1.-Data[i+1]/(Data[i]))*(1.-Mean/(Data[i]))
67 NumXing += 1
68
69 # calculate the frequency of the testwave
70 freqp = fsampling*NumXing*1000./1024
71 if(np.abs(freqp-freq) > 20):
72 print "This is not a timecalibration run!!"
73 raise SystemExit
74
75 return MeanXing, TimeXing, NumXing
76
77def CrossingOliver(Data, Mean, t_nom, Start, Chip):
78 TimeXing = np.zeros(lpipe)
79 MeanXing = np.zeros(lpipe)
80 NumXing = 0
81 CellTime = CellTimef(t_nom)
82
83
84 for i in range(lpipe-1):
85 if ((Data[i] > Mean) & (Data[i+1] < Mean)):
86 MeanXing[NumXing] = np.mod(i+Start, lpipe)
87
88 FirstCell = CellTime[np.mod(i+Start,lpipe)]
89 SecondCell = CellTime[np.mod(i+1+Start, lpipe)]
90
91 if(SecondCell < FirstCell): SecondCell =+ lpipe/fsampl
92
93 TimeXing[NumXing] = FirstCell+(SecondCell-FirstCell)/(1.-Data[i+1]/(Data[i]))*(1.-Mean/(Data[i]))
94 NumXing += 1
95
96
97 return MeanXing, TimeXing, NumXing
98
99# Determine time between crossings and apply correction
100def Corr(MeanXing, NumXing, TimeXing, t_nom, Start, Chip):
101
102 # loop over mean crossings
103 for i in range(int(NumXing-1)):
104 I1 = MeanXing[i]
105 I2 = MeanXing[i+1]
106 Period = TimeXing[i+1] - TimeXing[i]
107
108
109 if(np.abs(1-Period/P_nom) > 0.2):
110 #print "Skipping zero crossing due to too large deviation of period."
111 h.list[Chip].dict["loleftout"].Fill(MeanXing[i])
112 #h.list[Chip].dict["leftout"].Fill(np.mod((MeanXing[i]+Start), rd.NROI))
113 continue
114
115 #h.list[Chip].dict["lomeanxing"].Fill(MeanXing[i])
116 h.list[Chip].dict["meanxing"].Fill(np.mod((MeanXing[i]+Start), rd.NROI))
117
118 # calculate the frequency of the testwave
119 freqp = 1000./Period
120 h.list[Chip].dict["freq"].Fill(freqp)
121
122 C = (P_nom-Period)*DAMPING
123 numnom = Period*fsampling
124 Correction = np.zeros(rd.NROI)
125 Correction[I1+1:I2+1] += C/numnom
126 Correction[:I1+1] -= (I2-I1)*C/(numnom*(rd.NROI-(I2-I1)))
127 Correction[I2+1:] -= (I2-I1)*C/(numnom*(rd.NROI-(I2-I1)))
128
129 Correction = np.roll(Correction, Start)
130 t_nom += Correction
131 #h.list[Chip].dict["lomeanxing"].Fill(MeanXing[-1])
132 h.list[Chip].dict["meanxing"].Fill(np.mod((MeanXing[-1]+Start), rd.NROI))
133
134 return t_nom
135
136def CorrOliver(MeanXing, NumXing, TimeXing, t_nom, Start, Chip):
137
138 CellTime = CellTimef(t_nom)
139 for i in range(int(NumXing-1)):
140 I1 = MeanXing[i]
141 I2 = MeanXing[i+1]
142 Period = TimeXing[i+1] - TimeXing[i]
143 if(Period<0): Period += rd.NROI/fsampl
144
145 if(np.abs(1-Period/P_nom) > 0.2):
146 #print "Skipping zero crossing due to too large deviation of period."
147 continue
148
149 Correction = (P_nom-Period)*DAMPING
150
151 # Positive correction from MeanXing[i] to MeanXing[i+1]
152 # and negative to all others
153 Time = 0
154 for j in range(rd.NROI):
155 diff = CellTime[j+1] - CellTime[j]
156 if ((I2>I1 and j>I1 and j<=I2) or (I2<I1 and (j<=I2 or j>I1))):
157 diff += Correction/np.mod(I2-I1+rd.NROI, rd.NROI)
158 else:
159 diff -= Correction/(rd.NROI-np.mod(I2-I1+rd.NROI, rd.NROI))
160 CellTime[j] = Time
161 Time += diff
162 CellTime[rd.NROI] = Time
163
164 print CellTime[-1]
165 t_nom = t_nomf(CellTime)
166
167 return t_nom
168
169
170
171# Summing up the nominal times for each cell to the total cell time.
172def CellTimef(t_nom):
173 CellTime = np.zeros(rd.NROI+1)
174 for i in range(rd.NROI):
175 CellTime[i+1] = CellTime[i] + t_nom[i]
176
177 return CellTime
178
179# getting t_nom from CellTime
180def t_nomf(CellTime):
181 t_nom = np.zeros(rd.NROI)
182 for i in range(rd.NROI):
183 t_nom[i] = CellTime[i+1]-CellTime[i]
184
185 return t_nom
186
187
188rd.next()
189NPIX = NChip*9
190NEvents = NEventsp
191
192# create array to put the calibrated times for each cell in
193t_nom = np.ones([NPIX, rd.NROI])/fsampling
194# loop over events
195for Event in range( NEvents ):
196 print "Event ", Event
197 Chip = 0
198
199 # loop over every 9th channel in Chip
200 for Pix in range(NPIX)[8::9]:
201 print "Pix ", Pix
202
203 # calculate t_nom for each pixel
204 MeanXing, TimeXing, NumXing = Crossing(rd.acalData[Pix], np.average(rd.acalData[Pix]), t_nom[Pix], rd.startCells[Pix], Chip)
205 h.list[Chip].dict["numxing"].Fill(NumXing)
206 #h.list[Chip].dict["start"].Fill(rd.startCells[Pix])
207 t_nom[Pix] = Corr(MeanXing, NumXing, TimeXing, t_nom[Pix], rd.startCells[Pix], Chip)
208
209 Chip += 1
210
211 rd.next()
212
213t_nom = t_nom[8::9]
214
215if(save == "yes"):
216 np.save(str(dfname[-20:-8])+"_"+str(NEvents)+"x"+str(NChip), t_nom)
217 np.savetxt(str(dfname[-20:-8])+"_"+str(NEvents)+"x"+str(NChip), t_nom)
218
219for Chip in range(NChip):
220 for i in range(rd.NROI):
221 h.list[Chip].dict["int_dev"].SetBinContent(i+1, np.sum(t_nom[Chip][:i])-i*1/fsampling)
222 h.list[Chip].dict["diff_dev"].SetBinContent(i+1, np.sum(t_nom[Chip][i] - 1/fsampling))
223
224pyfact.SaveHistograms(h.list, str(dfname[-20:-8])+"_"+str(NEvents)+"x"+str(NChip)+".root", "RECREATE")
225
226
227t_f = time()
228
229
230
231print "time ellapsed = ", (t_f - t_s)/60., " min."
232print "Speichern = ", save
233print "NChip = ", NChip
234print "NEvents = ", NEventsp
235print "Histo saved as = ", str(dfname[-20:-8])+str(NEvents)+"x"+str(NChip)+".root"
236if(save == "yes"):
237 print "CellTime saved as = ", str(dfname[-20:-8])+str(NEvents)+"x"+str(NChip)+".npy"
238
Note: See TracBrowser for help on using the repository browser.