-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostRawFilter.m
287 lines (251 loc) · 9.31 KB
/
PostRawFilter.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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
function [CaracEvt] = PostRawFilter( Struct, id )
% RAWFILTER Raw signal filter for pulses from Candidates
% Taken from RawFilter.m
% Treatment for
% OMH 04/12/2012
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% 1-a) Unpack settings
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
RunSetup = Struct.Setup;
nrun = RunSetup.Run;
SharedGlobals
if exist( 'id' )
DISPLAY = 0;
end
NbCoinc = RunSetup.TotalCoinc;
Detectors = [ RunSetup.Det.Name ];
DetectorType = [ RunSetup.Det.isScint ];
CoincStruct = Struct.Coinc;
if ~isfield( Struct, 'rawfilter' )
Struct.rawfilter.settings.autocreate = 1;
end
settings = Struct.rawfilter.settings;
MultRef=[Struct.Coinc.Mult];
CaracEvt=cell(length(Detectors),8);
SigmaSave=zeros(1,length(Detectors));
MuSave=zeros(1,length(Detectors));
Sat=zeros(1,length(Detectors));
minBrut = -ones(1,length(Detectors));
maxBrut = -ones(1,length(Detectors));
NoBox=zeros(1,length(Detectors));
settings.threshold = threshold;
settings.granularity_box = granularity_box; % s
settings.granularity_sampling = granularity_sampling; % s
settings.max_out = max_out;
settings.max_total_ToT = max_out_ToT; % s
settings.max_block_ToT = max_block_ToT; % s
%settings.bounce_ratio = bounce_ratio;
%settings.inhibit_window = inhibit_window; % s
settings.noise_mode = noise_mode;
Struct.rawfilter.settings = settings; % update structure
granularity_box = round( settings.granularity_box*FSAMPLING );
granularity_sampling = round( settings.granularity_sampling*FSAMPLING );
Evt = CoincStruct.Det.Evt;
signals = CoincStruct.ShowerRecons.ShowerSignals;
CoincId = CoincStruct.IdCoinc;
ind = find(CoincId==id);
trig = CoincStruct.Det.Tag(ind,:);
nTrig = sum(trig);
indant = find(trig==1);
ib = ibuff;
tmu = [ 1:ib ]/FSAMPLING*1e6; % micro-seconds
ngood = 0;
%% Loop on detectors
for i = 1:nTrig
DataEvt = signals{ind,indant(i)}/SCALE; % Amplitude back in LSB
%figure,plot(DataEvt)
if length( DataEvt ) ~= ib
disp( sprintf( [ ...
'Error!\n' ...
'\tData size for event %d on detector %d is %d samples\n' ...
'\tShould have been %d\n' ...
'\tSkipping it.' ], ...
Evt(ind,indant(i)), Detectors( indant(i) ), length( DataEvt ), ib ) );
continue
end
maxBrut(indant(i)) = max(DataEvt);
minBrut(indant(i)) = min(DataEvt);
if maxBrut(indant(i))==255 | minBrut(indant(i))==0
Sat(indant(i))=1;
end;
% Extract the stationary noise parameters
%===
KLOW = [ 1:(round( length( DataEvt )/2 )-100) ]; % 1st half subset of data. Before trigger
indmax=find(max(DataEvt(KLOW))==DataEvt(KLOW));
if DataEvt(KLOW(indmax))>155 & indmax>150
KLOW=[1:indmax-10];
end;
if ( settings.noise_mode == 1 ) | ( DISPLAY == 1 )
[ y, x ] = hist( DataEvt( KLOW ), [ 0:255 ] );
[ ym, km ] = max( y );
xm = x( km );
Dx = unique( abs( x( y > 0.5*ym ) - xm ) ); % Select samples within Width at Half Full Height (WHFH).
n = round( Dx(end)*2.5577 ); % 3 sigmas width, corrected from WHFH and rounded.
K = km + [ -n:1:n ];
K = K( K <= 256 & K >= 1 );
xk = x( K );
pk = y( K )/sum( y( K ) );
mu = sum( xk.*pk ) ; % Noise mean value
if size(Dx,2)<4 % Amplitude histogram too sharp
sig=std(DataEvt(KLOW));
else
sig = sqrt( sum( ( xk - mu ).^2.*pk ) ); % Noise sigma
end
end
if ( settings.noise_mode ~= 1 )
mu = mean( DataEvt( KLOW ) );
sig = std( DataEvt( KLOW ) );
end
MuSave(indant(i))=mu;
SigmaSave(indant(i))=sig;
% Window the signal block(s)
%===
Win = ( abs( DataEvt - mu ) >= settings.threshold*sig )';
% Get start Win = ( abs( DataEvt - mu ) >= settings.threshold*sig )';
if sum(Win)>1 % More than 1 point above threshold
Win = Agglomerate( Win, granularity_sampling); % join isolated point
end
%===
S = find( Win );
if isempty( S )
n_blocks = 0;
block_start = [];
block_end = [];
else
dS = [ 2, diff( S ) ];
bg = find( dS > 1 );
ed = [ bg(2:end)-1, length( S ) ];
n_blocks = length( bg );
block_start = S(bg);
block_end = S(ed);
for k=1:n_blocks
%k
ibef = block_start(k)-1;
%DataEvt(ibef-1*granularity_box:ibef)-mu
%DataEvt(sel+block_start(k)-granularity_sampling)-mu
%(settings.threshold-2)*sig
sel = find(abs(DataEvt(max(1,ibef-granularity_box):ibef)-mu) >= (settings.threshold-2)*sig);
selind = ibef-1*granularity_box+sel-1;
selind = selind(selind>0);
Win(selind) = 1;
%pause
iaft = block_end(k)+1;
sel = find(abs(DataEvt(iaft:min(length(DataEvt),iaft+1*granularity_box))-mu) >= (settings.threshold-2)*sig);
selind = iaft+sel-1;
selind = selind(selind<=ib);
Win(selind) = 1;
%iaft+sel-1
%pause
end
end
%Win = Agglomerate( Win, granularity_sampling); % join isolated point
Win = Agglomerate( Win, granularity_box); % join box
S = find( Win );
if isempty( S )
n_blocks = 0;
block_start = [];
block_end = [];
else
dS = [ 2, diff( S ) ];
bg = find( dS > 1 );
ed = [ bg(2:end)-1, length( S ) ];
n_blocks = length( bg );
block_start = S(bg);
block_end = S(ed);
end
% Clean blocks shorter than granularity
%===
block_len = block_end - block_start + 1;
% Global cuts
%===
time_over_threshold = sum( block_len )/FSAMPLING;
%%%%%%% On enleve les cuts pour une analyse a posteriori
if n_blocks == 0
n_blocks = 0;
block_start = [];
block_end = [];
block_len = [];
block_amp = [];
block_dt = [];
elseif ( n_blocks > 0 )
% Clean reflections and blocks obviously too long
%===
block_len = block_len/FSAMPLING;
block_amp = zeros( size( block_len ) );
for k = 1:n_blocks
K = [ block_start( k ):1:block_end( k ) ];
block_amp( k ) = max( abs( DataEvt( K ) - mu ) );
end
if n_blocks >= 2
block_dt = [ 1.0, ( block_start( 2:end ) - block_end( 1:end-1 ) )/FSAMPLING ];
else
block_dt = [ 1.0 ];
end
end
% Update statistics
%===
if n_blocks >= 1
ngood = ngood + 1;
is_selected = 1;
else
is_selected = 0;
MultRef(indant(i))=MultRef(indant(i))-1;
NoBox(indant(i))=1;
end
CellEvt={is_selected n_blocks time_over_threshold block_start block_end block_len block_amp block_dt};
CaracEvt(indant(i),:)=CellEvt;
clear CellEvt
%% DISPLAY
if DISPLAY
disp 'DISPLAY'
figure( 12 );
set( 12,'Name', sprintf( 'Event %d - Antenna %d ', Evt(ind,indant(i)), Detectors(indant(i))),'NumberTitle', 'off' );
% ADCs distribution
%===
subplot( 2, 1, 1 );
h = bar( x, y, 1.0 ); hold on;
set( h, 'FaceColor', 'none', 'EdgeColor', 'k' );
plot( x, ym*exp( -0.5*( x - mu ).^2/sig^2 ), 'r', 'LineWidth', 2 );
hold off; grid on;
axis( [ 0, 255, 0, 10*ceil( max( y/10 ) ) ] );
xlabel( 'ADCs [ LSB ]', labelOpts{:} );
ylabel( '# entries', labelOpts{:} );
set( gca, 'FontSize', 14 );
strtitle = { 'REJECT', 'ACCEPT' };
title( strtitle{ is_selected+1 }, 'FontSize', 20 );
% Signal as time
%===
subplot( 2, 1, 2 );
tmu = 1:length(DataEvt);
plot( tmu, DataEvt, 'k-' ); hold on;
for k = 1:n_blocks
t1 = tmu( block_start( k ) );
t2 = tmu( block_end( k ) );
%t1 = block_start( k );
%t2 = block_end( k );
lineOpts = { 'Color', 'r', 'LineWidth', 1 };
line( t1*[ 1, 1 ], [ 0, 255 ], lineOpts{:} );
line( t2*[ 1, 1 ], [ 0, 255 ], lineOpts{:} );
line( [ t1, t2 ], [ 0, 0 ], lineOpts{:} );
line( [ t1, t2 ], [ 255, 255 ], lineOpts{:} );
end
hold off; grid on;
axis( [ min( tmu ), max( tmu ), 0, 255 ] );
line( [ min(tmu),max(tmu) ], [ mu + settings.threshold*sig mu + settings.threshold*sig ], 'Color','b' );
line( [ min(tmu),max(tmu) ], [ mu - settings.threshold*sig mu - settings.threshold*sig ], 'Color','b' );
line( [ min(tmu),max(tmu) ], [ mu + (settings.threshold-2)*sig mu + (settings.threshold-2)*sig ], 'Color','g' );
line( [ min(tmu),max(tmu) ], [ mu - (settings.threshold-2)*sig mu - (settings.threshold-2)*sig ], 'Color','g' );
line( [ min(tmu),max(tmu) ], [ mu + (settings.threshold+1)*sig mu + (settings.threshold+1)*sig ], 'Color','y' );
line( [ min(tmu),max(tmu) ], [ mu - (settings.threshold+1)*sig mu - (settings.threshold+1)*sig ], 'Color','y' );
xlabel( 'Time [ \mus ]', labelOpts{:} );
ylabel( 'ADCs [ LSB ]', labelOpts{:} );
set( gca, 'FontSize', 14 );
sig
n_blocks
ToT = time_over_threshold*1e9
Dur = block_len*1e9
block_amp
CaracEvt
pause
end
end