Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModeAHDC.java
More file actions
368 lines (331 loc) · 14.3 KB
/
Copy pathModeAHDC.java
File metadata and controls
368 lines (331 loc) · 14.3 KB
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
//import ftouchte.Pulse;
import java.util.List;
import java.util.ArrayList;
/*public class Test {
public static void main(String[] args) {
short[] samples = {1,2,3,4,5,6,7,6,5,4,3,2,1};
//short[] samples = {1,2,3,4,5,6,4095,4095,4095,4095,4095,4,3,2,1};
System.out.println("=====> Test ModeAHDC");
ModeAHDC H = new ModeAHDC();
//System.out.println("samples : " + samples.toString());
System.out.println("=====> Setting parameters");
short adcOffset = 0;
if (samples.length > 5) {
for (int bin=0; bin<5; ++bin)
adcOffset += samples[bin];
}
adcOffset = Integer.valueOf(adcOffset / 5).shortValue();
adcOffset = 0;
H.adcOffset = adcOffset; // adc
H.samplingTime = 1; // ns // 44 ns
H.sparseSample = 0;
H.timeStamp = 0;
H.fineTimeStampResolution = 0;
H.amplitudeFractionCFA = 0.5f;
H.binDelayCFD = 5;
H.fractionCFD = 0.3f;
System.out.println("adcOffset = " + H.adcOffset + " adc");
System.out.println("samplingTime = " + H.samplingTime + " ns");
System.out.println("sparseSample = " + H.sparseSample);
//System.out.println("binOffset = " + H.binOffset);
System.out.println("timeStample = " + H.timeStamp);
System.out.println("fineTimeStampleResolution = " + H.fineTimeStampResolution);
System.out.println("=====> Application of the ModeAHDC");
List<Pulse> pulses = H.extract(samples);
//System.out.println("binMax = " + pulses.get(0).binMax);
System.out.println("adcMax = " + pulses.get(0).adcMax);
System.out.println("integral = " + pulses.get(0).integral);
System.out.println("timeMax = " + pulses.get(0).time + " ns");
System.out.println("timestamp = " + pulses.get(0).timestamp);
System.out.println("timeRiseCFA = " + pulses.get(0).timeRiseCFA + " ns");
System.out.println("timeFallCFA = " + pulses.get(0).timeFallCFA + " ns");
System.out.println("timeOverThreshold = " + pulses.get(0).timeOverThresholdCFA + " ns");
System.out.println("timeCFD = " + pulses.get(0).timeCFD + " ns");
System.out.println(pulses.get(0));
}
}*/
/**
* A new extraction method dedicated to the AHDC signal waveform
*
* Some blocks of code are inspired by MVTFitter.java
*
* @author ftouchte
*/
public class ModeAHDC {
// Settings parameters (they can be initialised by a CCDB)
public float samplingTime;
public int sparseSample;
public short adcOffset;
public long timeStamp;
public float fineTimeStampResolution;
public static final short ADC_LIMIT = 4095; // 2^12-1
public float amplitudeFractionCFA;
public int binDelayCFD;
public float fractionCFD;
// Calculation intermediaries
private int binMax; //Bin of the max ADC over the pulse
private int binOffset; //Offset due to sparse sample
private float adcMax; //Max value of ADC over the pulse (fitted)
private float timeMax; //Time of the max ADC over the pulse (fitted)
private float integral; //Sum of ADCs over the pulse (not fitted)
private long timestamp;
private short[] samplesCorr; //Waveform after offset (pedestal) correction
private int binNumber; //Number of bins in one waveform
private float timeRiseCFA; // moment when the signal reaches a Constant Fraction of its Amplitude uphill (fitted)
private float timeFallCFA; // moment when the signal reaches a Constant Fraction of its Amplitude downhill (fitted)
private float timeOverThresholdCFA; // is equal to (timeFallCFA - timeRiseCFA)
private float timeCFD; // time extracted using the Constant Fraction Discriminator (CFD) algorithm (fitted)
/**
* This method extracts relevant informations from the digitized signal
* (the samples) and store them in a Pulse
*
* @param pars CCDB row
* @param id link to row in source bank
* @param samples ADC samples
*/
//@Override
public List<Pulse> extract(short... samples){
waveformCorrection(samples,adcOffset,samplingTime,sparseSample);
fitAverage(samplingTime);
computeTimeAtConstantFractionAmplitude(samplingTime,amplitudeFractionCFA);
computeTimeUsingConstantFractionDiscriminator(samplingTime,fractionCFD,binDelayCFD);
fineTimeStampCorrection(timeStamp,fineTimeStampResolution);
// output
Pulse pulse = new Pulse();
pulse.adcMax = adcMax;
pulse.time = timeMax;
pulse.timestamp = timestamp;
pulse.integral = integral;
pulse.timeRiseCFA = timeRiseCFA;
pulse.timeFallCFA = timeFallCFA;
pulse.timeOverThresholdCFA = timeOverThresholdCFA;
pulse.timeCFD = timeCFD;
//pulse.binMax = binMax;
//pulse.binOffset = binOffset;
pulse.pedestal = adcOffset;
List<Pulse> output = new ArrayList<>();
output.add(pulse);
return output;
}
/**
* This method subtracts the pedestal (noise) from samples and stores it in : samplesCorr
* It also computes a first value for : adcMax, binMax, timeMax and integral
* This code is inspired by the one of MVTFitter.java
* @param samples ADC samples
* @param adcOffset pedestal or noise level
* @param samplingTime time between two adc bins
* @param sparseSample used to define binOffset
*/
private void waveformCorrection(short[] samples, short adcOffset, float samplingTime, int sparseSample){
binNumber = samples.length;
binMax = 0;
adcMax = (short) (samples[0] - adcOffset);
integral = 0;
samplesCorr = new short[binNumber];
for (int bin = 0; bin < binNumber; bin++){
samplesCorr[bin] = (short) (samples[bin] - adcOffset);
if (adcMax < samplesCorr[bin]){
adcMax = samplesCorr[bin];
binMax = bin;
}
integral += samplesCorr[bin];
}
/**
* If adcMax == ADC_LIMIT, that means there is saturation
* In that case, binMax is the middle of the first plateau
* This convention can be changed
*/
if ((short) adcMax == ADC_LIMIT) {
int binMax2 = binMax;
for (int bin = binMax; bin < binNumber; bin++){
if (samplesCorr[bin] == ADC_LIMIT) {
binMax2 = bin;
}
else {
break;
}
}
binMax = (binMax + binMax2)/2;
}
binOffset = sparseSample*binMax;
timeMax = (binMax + binOffset)*samplingTime;
}
/**
* This method gives a more precise value of the max of the waveform by computing the average of five points around the binMax
* It is an alternative to fitParabolic()
* The suitability of one of these fits can be the subject of a study
* Remark : This method updates adcMax but doesn't change timeMax
* @param samplingTime time between 2 ADC bins
*/
private void fitAverage(float samplingTime){
if ((binMax - 2 >= 0) && (binMax + 2 <= binNumber - 1)){
adcMax = 0;
for (int bin = binMax - 2; bin <= binMax + 2; bin++){
adcMax += samplesCorr[bin];
}
adcMax = adcMax/5;
}
}
/**
* Fit the max of the pulse using parabolic fit, this method updates the timeMax and adcMax values
* @param samplingTime time between 2 ADC bins
*/
private void fitParabolic(float samplingTime) {
}
/**
* From MVTFitter.java
* Make fine timestamp correction (using dream (=electronic chip) clock)
* @param timeStamp timing informations (used to make fine corrections)
* @param fineTimeStampResolution precision of dream clock (usually 8)
*/
private void fineTimeStampCorrection (long timeStamp, float fineTimeStampResolution) {
this.timestamp = timeStamp;
String binaryTimeStamp = Long.toBinaryString(timeStamp); //get 64 bit timestamp in binary format
if (binaryTimeStamp.length()>=3){
byte fineTimeStamp = Byte.parseByte(binaryTimeStamp.substring(binaryTimeStamp.length()-3,binaryTimeStamp.length()),2); //fineTimeStamp : keep and convert last 3 bits of binary timestamp
timeMax += (float) ((fineTimeStamp+0.5) * fineTimeStampResolution); //fineTimeStampCorrection
// Question : I wonder if I have to do the same thing of all time quantities that the extract() methods compute.
}
}
/**
* This method determines the moment when the signal reaches a Constant Fraction of its Amplitude (i.e fraction*adcMax)
* It fills the attributs : timeRiseCFA, timeFallCFA, timeOverThresholdCFA
* @param samplingTime time between 2 ADC bins
* @param amplitudeFraction amplitude fraction between 0 and 1
*/
private void computeTimeAtConstantFractionAmplitude(float samplingTime, float amplitudeFractionCFA){
float threshold = amplitudeFractionCFA*adcMax;
// timeRiseCFA
int binRise = 0;
for (int bin = 0; bin < binMax; bin++){
if (samplesCorr[bin] < threshold)
binRise = bin; // last pass below threshold and before adcMax
} // at this stage : binRise < timeRiseCFA/samplingTime <= binRise + 1 // timeRiseCFA is determined by assuming a linear fit between binRise and binRise + 1
float slopeRise = 0;
if (binRise + 1 <= binNumber-1)
slopeRise = samplesCorr[binRise+1] - samplesCorr[binRise];
float fittedBinRise = (slopeRise == 0) ? binRise : binRise + (threshold - samplesCorr[binRise])/slopeRise;
timeRiseCFA = (fittedBinRise + binOffset)*samplingTime; // binOffset is determined in wavefromCorrection() // must be the same for all time ? // or must be defined using fittedBinRise*sparseSample
// timeFallCFA
int binFall = binMax;
for (int bin = binMax; bin < binNumber; bin++){
if (samplesCorr[bin] > threshold){
binFall = bin;
}
else {
binFall = bin;
break; // first pass below the threshold
}
} // at this stage : binFall - 1 <= timeRiseCFA/samplingTime < binFall // timeFallCFA is determined by assuming a linear fit between binFall - 1 and binFall
float slopeFall = 0;
if (binFall - 1 >= 0)
slopeFall = samplesCorr[binFall] - samplesCorr[binFall-1];
float fittedBinFall = (slopeFall == 0) ? binFall : binFall + (threshold - samplesCorr[binFall-1])/slopeFall;
timeFallCFA = (fittedBinFall + binOffset)*samplingTime;
// timeOverThreshold
timeOverThresholdCFA = timeFallCFA - timeRiseCFA;
}
/**
* This methods extracts a time using the Constant Fraction Discriminator (CFD) algorithm
* It fills the attribut : timeCFD
* @param samplingTime time between 2 ADC bins
* @param fractionCFD CFD fraction parameter between 0 and 1
* @param binDelayCFD CFD delay parameter
*/
private void computeTimeUsingConstantFractionDiscriminator(float samplingTime, float fractionCFD, int binDelayCFD){
float[] signal = new float[binNumber];
// signal generation
for (int bin = 0; bin < binNumber; bin++){
signal[bin] = (1 - fractionCFD)*samplesCorr[bin]; // we fill it with a fraction of the original signal
if (bin < binNumber - binDelayCFD)
signal[bin] += -1*fractionCFD*samplesCorr[bin + binDelayCFD]; // we advance and invert a complementary fraction of the original signal and superimpose it to the previous signal
}
// determine the two humps
int binHumpSup = 0;
int binHumpInf = 0;
for (int bin = 0; bin < binNumber; bin++){
if (signal[bin] > signal[binHumpSup])
binHumpSup = bin;
}
for (int bin = 0; bin < binHumpSup; bin++){ // this loop has been added to be sure : binHumpInf < binHumpSup
if (signal[bin] < signal[binHumpInf])
binHumpInf = bin;
}
// research for zero
int binZero = 0;
for (int bin = binHumpInf; bin <= binHumpSup; bin++){
if (signal[bin] < 0)
binZero = bin; // last pass below zero
} // at this stage : binZero < timeCFD/samplingTime <= binZero + 1 // timeCFD is determined by assuming a linear fit between binZero and binZero + 1
float slopeCFD = 0;
if (binZero + 1 <= binNumber)
slopeCFD = signal[binZero+1] - signal[binZero];
float fittedBinZero = (slopeCFD == 0) ? binZero : binZero + (0 - signal[binZero])/slopeCFD;
timeCFD = (fittedBinZero + binOffset)*samplingTime;
}
public static void main(String[] args) {
short[] samples = {1,2,3,4,5,6,7,6,5,4,3,2,1};
//short[] samples = {1,2,3,4,5,6,4095,4095,4095,4095,4095,4,3,2,1};
System.out.println("=====> Test ModeAHDC");
ModeAHDC H = new ModeAHDC();
//System.out.println("samples : " + samples.toString());
System.out.println("=====> Setting parameters");
short adcOffset = 0;
if (samples.length > 5) {
for (int bin=0; bin<5; ++bin)
adcOffset += samples[bin];
}
adcOffset = Integer.valueOf(adcOffset / 5).shortValue();
adcOffset = 0;
H.adcOffset = adcOffset; // adc
H.samplingTime = 1; // ns // 44 ns
H.sparseSample = 0;
H.timeStamp = 0;
H.fineTimeStampResolution = 0;
H.amplitudeFractionCFA = 0.5f;
H.binDelayCFD = 5;
H.fractionCFD = 0.3f;
System.out.println("adcOffset = " + H.adcOffset + " adc");
System.out.println("samplingTime = " + H.samplingTime + " ns");
System.out.println("sparseSample = " + H.sparseSample);
//System.out.println("binOffset = " + H.binOffset);
System.out.println("timeStample = " + H.timeStamp);
System.out.println("fineTimeStampleResolution = " + H.fineTimeStampResolution);
System.out.println("=====> Application of the ModeAHDC");
List<Pulse> pulses = H.extract(samples);
//System.out.println("binMax = " + pulses.get(0).binMax);
System.out.println("adcMax = " + pulses.get(0).adcMax);
System.out.println("integral = " + pulses.get(0).integral);
System.out.println("timeMax = " + pulses.get(0).time + " ns");
System.out.println("timestamp = " + pulses.get(0).timestamp);
System.out.println("timeRiseCFA = " + pulses.get(0).timeRiseCFA + " ns");
System.out.println("timeFallCFA = " + pulses.get(0).timeFallCFA + " ns");
System.out.println("timeOverThreshold = " + pulses.get(0).timeOverThresholdCFA + " ns");
System.out.println("timeCFD = " + pulses.get(0).timeCFD + " ns");
System.out.println(pulses.get(0));
}
}
/*public class Pulse {
public long timestamp;
public float integral;
public float time;
public float pedestal;
public float adcMax;
public float timeRiseCFA;
public float timeFallCFA;
public float timeOverThresholdCFA;
public float timeCFD;
//Units are the same as the raw units of the samples.
//@param integral pulse integral, pedestal-subtracted
//@param time pulse time
public Pulse(float integral, float time) {
this.integral = integral;
this.time = time;
}
public Pulse(){}
@Override
public String toString() {
return String.format("pulse: integral=%f time=%f",
integral, time);
}
}*/
You can’t perform that action at this time.
