-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalibrateCandidate.m
162 lines (150 loc) · 6.04 KB
/
CalibrateCandidate.m
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
function [ calibSignals signals] = CalibrateCandidate( Struct )
% Perform calibration for shower candidates signals
% OMH 09/06/2011
RunSetup = Struct.Setup;
nrun = RunSetup.Run;
SharedGlobals;
Detectors=[RunSetup.Det.Name];
DetectorType=[RunSetup.Det.isScint];
CoincStruct = Struct.Coinc;
tag = CoincStruct.Det.Tag;
% Recons paras
IsShower = CoincStruct.IsShower;
CoincId = CoincStruct.IdCoinc;
ShowerId = CoincId(find(IsShower==1));
Lant = CoincStruct.MultAnt(find(IsShower==1));
L = CoincStruct.Mult(find(IsShower==1));
t = [ 1:ibuff ]/FSAMPLING; % seconds
tmu = t*1e6; % microsecs
fover = 10;
tover = [1 :1/fover: ibuff]/FSAMPLING;
tmuover = tover*1e6;
Calib = RunSetup.InfosRun.Calib;
freqPSDAll = Calib.F;
calibSignals = zeros(sum(L),3);
signals = zeros(sum(L),ibuff);
l=0;
for i=1:length(ShowerId)
%disp(sprintf('Performing calibration for coinc %d',ShowerId(i)))
ind_coinc = find(CoincId==ShowerId(i));
ind_det = find(tag(ind_coinc,:)==1);
ind_ant = find(tag(ind_coinc,:)==1 & DetectorType==0);
Dets = CoincStruct.Det.Id(ind_coinc,ind_det);
%Antennas = CoincStruct.Det.Id(ind_coinc,ind_ant);
Events = CoincStruct.Det.Evt(ind_coinc,ind_det);
GPSD = CoincStruct.Det.GainPSD{ind_coinc};
a = 0;
for j=1:length(Dets)
l = l+1;
%disp(sprintf('Event %d Detector %d',Events(j),Dets(j)))
% Get data
fd = OpenFileData( nrun, Dets( j ) );
if fd<0
disp(sprintf('Could not find data for detector %d.',Dets(j)))
fclose all
continue
end
fseek( fd, ibuff*(Events(j)-1),'bof');
DataEvt = double( fread( fd, ibuff, 'uint8' ) );
DataEvt = DataEvt*SCALE; % Now in Volts
%% Correct signal form (method 2 using system response)
if size(GPSD,1)==0
calibSignals(l,1) = ShowerId(i);
calibSignals(l,2) = Dets(j);
signals(l,:) = DataEvt;
continue % Skip calibration because no PSD is available
end
[freq,DataFFT,m] = FourierTrans(DataEvt,t,0);
%rangeFFT = find(freq>=FREQMIN-5 & freq<=FREQMAX+5);
%freq = freq(rangeFFT);
%DataFFT = DataFFT(rangeFFT);
a = a+1;
GainP = GPSD(a,:); % TO BE FIXED
%rangePSD = find(freqPSD>=FREQMIN-5 & freqPSD<=FREQMAX+5);
%freqPSD = freqPSD(rangePSD);
%Gain = Gain(rangePSD);
GainN = fliplr(GainP); % Symetrize
Gain = [GainN GainP];
GainLin = [10.^(Gain/20)]';
if size(DataFFT,1)~= ibuff
disp(sprintf('Error! Data size for event %d on detector %d is %d samples (should be %d)... skipping it.',Events(j),Dets(j),size(DataFFT,1),ibuff));
fclose all
continue
end
k1 = floor(ibuff/size(GainLin,1));
GainLinInterp = interp(GainLin,k1); % Interpolate
out = find(GainLinInterp<100);
DataFFTin = DataFFT./GainLinInterp; % complex FFT at input
DataFFTin(out) = 1e-1000;
modDataFFTin = abs(DataFFTin);
[aa,DataEvtin,m] = FourierTrans(DataFFTin,freq,1); % Re-invert axis. see FourierTrans
%DataEvtin = ifft(fftshift(DataFFTin)); % Re-invert axis. see FourierTrans
DataEvtin=PassBand(DataEvtin,t,FREQMIN,FREQMAX);
DataEvtSpline = spline(t,DataEvtin,tover); % Oversampling fo better amplitde determination
% Compute max
deltat = 10;
deltac= deltat*fover;
trig = floor(ibuff/2);
trigc = trig*fover;
DataZoom = double( DataEvtSpline(trigc-deltac:trigc+deltac) )';
[ vpeakpeak, tmoy ] = FindVt( DataZoom, trigc, deltac ); % calcul de vpeak et tmoy
calibSignals(l,1) = ShowerId(i);
calibSignals(l,2) = Dets(j);
calibSignals(l,3) = vpeakpeak; % peak-peak amplitude in deltat window around trigger time
signals(l,:) = DataEvtin;
rawAmp = max(DataEvt)-min(DataEvt);
%disp(sprintf('Output amplitude = %3.1f V, Antenna amplitude = %3.1f muV, Gain = %3.1f dB',rawAmp,vpeakpeak*1e6,20*log10(rawAmp/vpeakpeak)))
%k2 = size(DataEvt,1)/size(DataEvtin,1);
%DataEvtinInterp = interp(DataEvtin,k2);
%DataEvtinInterp=passband(DataEvtinInterp,t,50e6,100e6);
% Display
if DISPLAY
freqP = freqPSDAll(j,:);
freqN = -fliplr(freqP);
freqPSD=[freqN freqP];
figure(Dets(j))
set(Dets(j),'Name',sprintf('Coinc %d - Event %d Antenna %d',ShowerId(i),Events(j),Dets(j)),'NumberTitle','off')
subplot(3,2,1)
plot(tmu,DataEvt)
axis([0, max(tmu), 0, 2^NBITS*SCALE])
grid on
xlabel('Time [mus]')
ylabel('Output signal [V]')
subplot(3,2,2)
semilogy(freq,abs(DataFFT))
xlim([-FREQMAX FREQMAX])
%xlim([0, FREQMAX])
xlabel('Frequency [Hz]')
ylabel('Output FFT [LSB]')
subplot(3,2,3)
plot(freqPSD,Gain)
xlabel('Frequency [Hz]')
ylabel('Gain [LSB/V]dB')
xlim([-FREQMAX FREQMAX])
%axis([0,FREQMAX, 0, 100])
subplot(3,2,4)
semilogy(freqPSD,GainLin)
xlabel('Frequency [Hz]')
ylabel('Gain [LSB/V]lin')
%xlim([-FREQMAX FREQMAX])
axis([0, FREQMAX, 100, 1e5])
grid on
subplot(3,2,5)
semilogy(freq,modDataFFTin);
xlabel('Frequency [Hz]')
ylabel('Input FFT [V]')
xlim([-FREQMAX FREQMAX])
%xlim([0, FREQMAX])
subplot(3,2,6)
plot(tmu,DataEvtin,'LineWidth',1)
hold on
plot(tmuover,DataEvtSpline*1e6,'-m','LineWidth',1)
grid on
xlim([0, max(tmu)])
xlabel('Time [mus]')
ylabel('Input signal [muV]')
pause
end
fclose(fd);
end
end