1 | #include <DAQ/Df250EmulatorAlgorithm_v3.h> |
2 | |
3 | |
4 | |
5 | Df250EmulatorAlgorithm_v3::Df250EmulatorAlgorithm_v3(JEventLoop *loop){ |
6 | |
7 | FORCE_DEFAULT = 0; |
8 | |
9 | USE_CRATE_DEFAULTS = 1; |
10 | |
11 | |
12 | NSA_DEF = 20; |
13 | NSB_DEF = 5; |
14 | THR_DEF = 120; |
15 | NPED_DEF = 4; |
16 | MAXPED_DEF = 512; |
17 | NSAT_DEF = 2; |
18 | |
19 | |
20 | NSA_DEF = 15; |
21 | NSB_DEF = 1; |
22 | THR_DEF = 108; |
23 | NPED_DEF = 4; |
24 | MAXPED_DEF = 512; |
25 | NSAT_DEF = 2; |
26 | |
27 | |
28 | |
29 | VERBOSE = 0; |
30 | |
31 | if(gPARMS){ |
32 | gPARMS->SetDefaultParameter("EMULATION250:USE_CRATE_DEFAULTS", USE_CRATE_DEFAULTS,"Set to >0 to force use of crate-dependent default values"); |
33 | gPARMS->SetDefaultParameter("EMULATION250:FORCE_DEFAULT", FORCE_DEFAULT,"Set to >0 to force use of default values"); |
34 | gPARMS->SetDefaultParameter("EMULATION250:NSA", NSA_DEF,"Set NSA for firmware emulation, will be overwritten by BORConfig if present"); |
35 | gPARMS->SetDefaultParameter("EMULATION250:NSB", NSB_DEF,"Set NSB for firmware emulation, will be overwritten by BORConfig if present"); |
36 | gPARMS->SetDefaultParameter("EMULATION250:THR", THR_DEF,"Set threshold for firmware emulation, will be overwritten by BORConfig if present"); |
37 | gPARMS->SetDefaultParameter("EMULATION250:NPED", NPED_DEF,"Set NPED for firmware emulation, will be overwritten by BORConfig if present"); |
38 | gPARMS->SetDefaultParameter("EMULATION250:MAXPED", MAXPED_DEF,"Set MAXPED for firmware emulation, will be overwritten by BORConfig if present"); |
39 | gPARMS->SetDefaultParameter("EMULATION250:NSAT", NSAT_DEF,"Set NSAT for firmware emulation, will be overwritten by BORConfig if present"); |
40 | gPARMS->SetDefaultParameter("EMULATION250:VERBOSE", VERBOSE,"Set verbosity for f250 emulation"); |
41 | } |
42 | } |
43 | |
44 | void Df250EmulatorAlgorithm_v3::EmulateFirmware(const Df250WindowRawData* rawData, |
45 | std::vector<Df250PulseData*> &pdat_objs) |
46 | { |
47 | |
48 | if (VERBOSE > 0) { |
| |
49 | jout << " Df250EmulatorAlgorithm_v3::EmulateFirmware ==> Starting emulation <==" << endl; |
50 | jout << "rocid : " << rawData->rocid << " slot: " << rawData->slot << " channel: " << rawData->channel << endl; |
51 | } |
52 | |
53 | |
54 | if (rawData == NULL__null) { |
| |
55 | jerr << " ERROR: Df250EmulatorAlgorithm_v3::EmulateFirmware - raw sample data is missing" << endl; |
56 | jerr << " Contact mstaib@jlab.org" << endl; |
57 | return; |
58 | } |
59 | if (rawData->samples.size() == 0) { |
| |
60 | jerr << " ERROR: Df250EmulatorAlgorithm_v3::EmulateFirmware - raw sample data has zero size" << endl; |
61 | jerr << "rocid : " << rawData->rocid << " slot: " << rawData->slot << " channel: " << rawData->channel << endl; |
62 | |
63 | return; |
64 | } |
65 | |
66 | |
67 | uint32_t channel = rawData->channel; |
68 | |
69 | |
70 | |
71 | |
72 | const Df250BORConfig *f250BORConfig = NULL__null; |
73 | rawData->GetSingle(f250BORConfig); |
74 | |
75 | uint32_t NSA; |
76 | int32_t NSB; |
77 | uint32_t NPED, MAXPED; |
78 | uint16_t THR; |
79 | uint16_t NSAT; |
80 | |
81 | if (f250BORConfig == NULL__null || FORCE_DEFAULT){ |
82 | static int counter = 0; |
83 | NSA = NSA_DEF; |
84 | NSB = NSB_DEF; |
85 | THR = THR_DEF; |
86 | NPED = NPED_DEF; |
| 4 | | Value assigned to 'NPED' | |
|
87 | MAXPED = MAXPED_DEF; |
88 | NSAT = NSAT_DEF; |
89 | if (counter < 10){ |
| |
90 | counter++; |
91 | if (counter == 10) jout << " WARNING Df250EmulatorAlgorithm_v3::EmulateFirmware No Df250BORConfig == Using default values == LAST WARNING" << endl; |
| |
92 | else jout << " WARNING Df250EmulatorAlgorithm_v3::EmulateFirmware No Df250BORConfig == Using default values " << endl; |
93 | |
94 | } |
95 | |
96 | |
97 | if(USE_CRATE_DEFAULTS) { |
| |
98 | |
99 | |
100 | if( (rawData->rocid >= 11) && (rawData->rocid <= 22) ) { |
101 | NSA = 15; |
102 | NSB = 1; |
103 | THR = 108; |
104 | |
105 | |
106 | NSAT = 2; |
107 | } |
108 | |
109 | else if( (rawData->rocid >= 31) && (rawData->rocid <= 46) ) { |
110 | NSA = 26; |
111 | NSB = 1; |
112 | THR = 105; |
113 | |
114 | |
115 | NSAT = 2; |
116 | } |
117 | |
118 | else if( (rawData->rocid == 73) || (rawData->rocid == 74) || ( (rawData->rocid == 75) && (rawData->slot >= 7) && (rawData->slot <= 16)) ) { |
119 | NSA = 6; |
120 | NSB = 3; |
121 | THR = 300; |
122 | |
123 | |
124 | NSAT = 2; |
125 | } |
126 | |
127 | else if( (rawData->rocid == 71) || (rawData->rocid == 72) || ( (rawData->rocid == 75) && (rawData->slot >= 3) && (rawData->slot <= 6)) ) { |
128 | NSA = 6; |
129 | NSB = 3; |
130 | THR = 150; |
131 | |
132 | |
133 | NSAT = 2; |
134 | } |
135 | |
136 | else if( rawData->rocid == 77 ) { |
137 | NSA = 10; |
138 | NSB = 1; |
139 | THR = 160; |
140 | |
141 | |
142 | NSAT = 2; |
143 | } |
144 | |
145 | else if( (rawData->rocid >= 83) && (rawData->rocid <= 84) ) { |
146 | NSA = 10; |
147 | NSB = 3; |
148 | THR = 130; |
149 | |
150 | |
151 | NSAT = 2; |
152 | } |
153 | |
154 | else if( rawData->rocid == 94 ) { |
155 | NSA = 20; |
156 | NSB = 5; |
157 | THR = 120; |
158 | |
159 | |
160 | NSAT = 2; |
161 | } |
162 | } |
163 | } |
164 | else{ |
165 | NSA = f250BORConfig->NSA; |
166 | NSB = f250BORConfig->NSB; |
167 | THR = f250BORConfig->adc_thres[channel]; |
168 | NPED = f250BORConfig->NPED; |
169 | MAXPED = f250BORConfig->MaxPed; |
170 | NSAT = f250BORConfig->NSAT; |
171 | |
172 | } |
173 | |
174 | if (VERBOSE > 0) jout << "Df250EmulatorAlgorithm_v3::EmulateFirmware NSA: " << NSA << " NSB: " << NSB << " THR: " << THR << endl; |
| |
175 | |
176 | |
177 | |
178 | |
179 | |
180 | |
181 | |
182 | |
183 | |
184 | |
185 | |
186 | |
187 | |
188 | bool bad_pedestal = false; |
189 | bool bad_timing_pedestal = false; |
190 | bool no_timing_calculation = false; |
191 | |
192 | |
193 | |
194 | |
195 | |
196 | |
197 | vector<uint16_t> samples = rawData->samples; |
198 | uint16_t NW = samples.size(); |
199 | uint32_t npulses = 0; |
200 | const int max_pulses = 3; |
201 | uint32_t TC[max_pulses] = {}; |
202 | uint32_t TMIN[max_pulses] = {3}; |
203 | |
204 | uint32_t pulse_integral[max_pulses] = {}; |
205 | bool has_overflow_samples[max_pulses] = {false}; |
206 | bool has_underflow_samples[max_pulses] = {false}; |
207 | uint32_t number_samples_above_threshold[max_pulses] = {0}; |
208 | bool NSA_beyond_PTW[max_pulses] = {false}; |
209 | bool vpeak_beyond_NSA[max_pulses] = {false}; |
210 | bool vpeak_not_found[max_pulses] = {false}; |
211 | |
212 | |
213 | if(VERBOSE > 0) { |
| |
214 | for (unsigned int i=0; i < NW; i++) { |
215 | if(VERBOSE > 2) { |
216 | if(samples[i] == 0x1fff) |
217 | jout << "Overflow at sample " << i << endl; |
218 | if(samples[i] == 0x1000) |
219 | jout << "Underflow at sample " << i << endl; |
220 | } |
221 | if (VERBOSE > 5) jout << "Df250EmulatorAlgorithm_v3::EmulateFirmware samples[" << i << "]: " << samples[i] << endl; |
222 | } |
223 | } |
224 | |
225 | |
226 | |
227 | |
228 | unsigned int MAX_SAMPLE = NW-NSAT; |
229 | |
230 | |
231 | for (unsigned int i=0; i < MAX_SAMPLE; i++) { |
| 10 | | Assuming 'i' is >= 'MAX_SAMPLE' | |
|
| 11 | | Loop condition is false. Execution continues on line 312 | |
|
232 | if ((samples[i] & 0xfff) > THR) { |
233 | if (VERBOSE > 1) { |
234 | jout << "threshold crossing at " << i << endl; |
235 | } |
236 | |
237 | |
238 | TC[npulses] = i+1; |
239 | |
240 | |
241 | if( NSAT>1 ){ |
242 | int samples_over_threshold = 1; |
243 | |
244 | if(i==0) { |
245 | |
246 | |
247 | for(unsigned int j=i+1; ((samples[j]&0xfff)>THR) && (j<MAX_SAMPLE+1); j++) { |
248 | |
249 | if ((samples[j] & 0xfff) > THR) |
250 | samples_over_threshold++; |
251 | |
252 | if( samples_over_threshold == NSAT ) { |
253 | |
254 | |
255 | break; |
256 | } |
257 | |
258 | } |
259 | } else { |
260 | for(unsigned int j=i+1; ((samples[j]&0xfff)>THR) && (j<MAX_SAMPLE+1); j++) { |
261 | samples_over_threshold++; |
262 | |
263 | if( samples_over_threshold == NSAT ) |
264 | break; |
265 | } |
266 | } |
267 | |
268 | |
269 | if( samples_over_threshold != NSAT ) |
270 | continue; |
271 | } |
272 | |
273 | |
274 | |
275 | |
276 | |
277 | |
278 | unsigned int ibegin; |
279 | if(NSB > 0) |
280 | ibegin = i > uint32_t(NSB) ? (i - NSB) : 0; |
281 | else { |
282 | ibegin = i - NSB; |
283 | if(ibegin > uint32_t(NW)) |
284 | break; |
285 | } |
286 | unsigned int iend = (i + NSA) < uint32_t(NW) ? (i + NSA) : NW; |
287 | |
288 | NSA_beyond_PTW[npulses] = (i + NSA - 1) >= uint32_t(NW); |
289 | for (i = ibegin; i < iend; ++i) { |
290 | pulse_integral[npulses] += (samples[i] & 0xfff); |
291 | |
292 | if(samples[i] == 0x1fff) { |
293 | has_overflow_samples[npulses] = true; |
294 | } |
295 | if(samples[i] == 0x1000) { |
296 | has_underflow_samples[npulses] = true; |
297 | } |
298 | |
299 | if( (i+1>=TC[npulses]) && ((samples[i] & 0xfff) > THR) ) |
300 | number_samples_above_threshold[npulses]++; |
301 | } |
302 | for (; i < NW && (samples[i] & 0xfff) >= THR; ++i) {} |
303 | if (++npulses == max_pulses) |
304 | break; |
305 | TMIN[npulses] = i; |
306 | } |
307 | } |
308 | |
309 | |
310 | |
311 | |
312 | uint32_t VPEAK[max_pulses] = {}; |
313 | uint32_t TPEAK[max_pulses] = {}; |
314 | uint16_t TMID[max_pulses] = {}; |
315 | uint16_t VMID[max_pulses] = {}; |
316 | uint16_t TFINE[max_pulses] = {}; |
317 | uint32_t pulse_time[max_pulses] = {}; |
318 | |
319 | |
320 | uint32_t pedestal = 0; |
321 | uint32_t VMIN = 0; |
322 | for (unsigned int i=0; i < NPED; i++) { |
| 12 | | Assuming 'i' is >= 'NPED' | |
|
| 13 | | Loop condition is false. Execution continues on line 336 | |
|
323 | pedestal += (samples[i] & 0xfff); |
324 | if(i<4) |
325 | VMIN += (samples[i] & 0xfff); |
326 | |
327 | |
328 | if ((samples[i] & 0xfff) > MAXPED) { |
329 | bad_pedestal = true; |
330 | } |
331 | |
332 | if( (samples[i] == 0x1fff) || (samples[i] == 0x1000) ) { |
333 | bad_pedestal = true; |
334 | } |
335 | } |
336 | VMIN /= NPED; |
| |
337 | |
338 | |
339 | |
340 | for (unsigned int i=0; i < 4; i++) { |
341 | |
342 | if ( ((samples[i] & 0xfff) > MAXPED) || ((samples[i] & 0xfff) > THR) ) { |
343 | bad_timing_pedestal = true; |
344 | } |
345 | |
346 | if ( (samples[i] == 0x1000) || (samples[i] == 0x1fff) ) { |
347 | bad_timing_pedestal = true; |
348 | } |
349 | |
350 | |
351 | |
352 | |
353 | if( (samples[i] & 0xfff) > THR ) { |
354 | no_timing_calculation = true; |
355 | } |
356 | } |
357 | |
358 | |
359 | for (unsigned int p=0; p < npulses; ++p) { |
360 | |
361 | |
362 | |
363 | |
364 | |
365 | if(no_timing_calculation) { |
366 | TMID[p] = TC[p]; |
367 | TFINE[p] = 0; |
368 | VPEAK[p] = 0; |
369 | |
370 | |
371 | } |
372 | |
373 | |
374 | |
375 | |
376 | |
377 | |
378 | |
379 | |
380 | while (true) { |
381 | |
382 | |
383 | |
384 | |
385 | |
386 | |
387 | |
388 | |
389 | |
390 | |
391 | unsigned int ipeak; |
392 | for (ipeak = TC[p]; (int)ipeak < NW-1; ++ipeak) { |
393 | |
394 | if ((samples[ipeak] & 0xfff) < (samples[ipeak-1] & 0xfff)) { |
395 | VPEAK[p] = (samples[ipeak-1] & 0xfff); |
396 | TPEAK[p] = ipeak-1; |
397 | break; |
398 | } |
399 | } |
400 | |
401 | |
402 | if(ipeak > TC[p]+NSA) |
403 | vpeak_beyond_NSA[p] = true; |
404 | |
405 | if (VERBOSE > 1) { |
406 | jout << " pulse " << p << ": VMIN: " << VMIN |
407 | << " TC: " << TC[p] << " VPEAK: " << VPEAK[p] << endl; |
408 | } |
409 | |
410 | |
411 | if (VPEAK[p] == 0) { |
412 | TMID[p] = TC[p]; |
413 | TFINE[p] = 0; |
414 | VPEAK[p] = 0; |
415 | vpeak_beyond_NSA[p] = true; |
416 | vpeak_not_found[p] = true; |
417 | break; |
418 | } |
419 | |
420 | |
421 | if(no_timing_calculation) |
422 | break; |
423 | |
424 | |
425 | VMID[p] = (VMIN + VPEAK[p]) >> 1; |
426 | |
427 | |
428 | |
429 | |
430 | |
431 | |
432 | |
433 | for (unsigned int i = TPEAK[p]; i >= 1; --i) { |
434 | if ( ((samples[i-1] & 0xfff) <= VMID[p]) && ((samples[i] & 0xfff) > VMID[p]) ) { |
435 | |
436 | |
437 | TMID[p] = i; |
438 | break; |
439 | } |
440 | } |
441 | |
442 | if (TMID[p] == 0) { |
443 | TFINE[p] = 0; |
444 | } |
445 | else { |
446 | |
447 | int Vnext = (samples[TMID[p]] & 0xfff); |
448 | int Vlast = (samples[TMID[p]-1] & 0xfff); |
449 | if (VERBOSE > 2) { |
450 | jout << " TMIN = " << TMIN[p] << " TMID = " << TMID[p] << " TPEAK = " << TPEAK[p] << endl |
451 | << " VMID = " << VMID[p] << " Vnext = " << Vnext << " Vlast = " << Vlast << endl; |
452 | } |
453 | if (Vnext > Vlast && VMID[p] >= Vlast) |
454 | TFINE[p] = 64 * (VMID[p] - Vlast) / (Vnext - Vlast); |
455 | else |
456 | TFINE[p] = 62; |
457 | if(TFINE[p] == 64) |
458 | TFINE[p] = 0; |
459 | } |
460 | pulse_time[p] = ((TMID[p]-1) << 6) + TFINE[p]; |
461 | break; |
462 | } |
463 | VMIN = (VMIN < 99999)? VMIN : 0; |
464 | |
465 | if (VERBOSE > 1) { |
466 | jout << " pulse " << p << ": VMID: " << VMID[p] << " TMID: " << TMID[p] |
467 | << " TFINE: " << TFINE[p] << " time: " << pulse_time[p] |
468 | << " integral: " << pulse_integral[p] << endl; |
469 | if (VERBOSE > 2) { |
470 | jout << " TMIN = " << TMIN[p] << " TMID = " << TMID[p] << " TPEAK = " << TPEAK[p] << endl; |
471 | |
472 | } |
473 | } |
474 | |
475 | |
476 | Df250PulseData* f250PulseData; |
477 | if( p < pdat_objs.size() ) { |
478 | f250PulseData = pdat_objs[p]; |
479 | |
480 | if(f250PulseData == NULL__null) { |
481 | jerr << " NULL f250PulseData object!" << endl; |
482 | continue; |
483 | } |
484 | } else { |
485 | |
486 | f250PulseData = new Df250PulseData; |
487 | |
488 | f250PulseData->rocid = rawData->rocid; |
489 | f250PulseData->slot = rawData->slot; |
490 | f250PulseData->channel = rawData->channel; |
491 | f250PulseData->itrigger = rawData->itrigger; |
492 | |
493 | f250PulseData->event_within_block = 1; |
494 | f250PulseData->QF_pedestal = bad_pedestal; |
495 | f250PulseData->pedestal = pedestal; |
496 | |
497 | f250PulseData->integral = pulse_integral[p]; |
498 | f250PulseData->QF_NSA_beyond_PTW = NSA_beyond_PTW[p]; |
499 | f250PulseData->QF_overflow = has_overflow_samples[p]; |
500 | f250PulseData->QF_underflow = has_underflow_samples[p]; |
501 | f250PulseData->nsamples_over_threshold = number_samples_above_threshold[p]; |
502 | |
503 | f250PulseData->course_time = TMID[p]; |
504 | f250PulseData->fine_time = TFINE[p]; |
505 | f250PulseData->QF_vpeak_beyond_NSA = vpeak_beyond_NSA[p]; |
506 | f250PulseData->QF_vpeak_not_found = vpeak_not_found[p]; |
507 | f250PulseData->QF_bad_pedestal = bad_timing_pedestal; |
508 | |
509 | f250PulseData->pulse_number = p; |
510 | f250PulseData->nsamples_integral = NSA + NSB; |
511 | f250PulseData->nsamples_pedestal = NPED; |
512 | f250PulseData->emulated = true; |
513 | |
514 | f250PulseData->AddAssociatedObject(rawData); |
515 | const_cast<Df250WindowRawData*>(rawData)->AddAssociatedObject(f250PulseData); |
516 | pdat_objs.push_back(f250PulseData); |
517 | } |
518 | |
519 | |
520 | f250PulseData->integral_emulated = pulse_integral[p]; |
521 | f250PulseData->pedestal_emulated = pedestal; |
522 | f250PulseData->pulse_peak_emulated = VPEAK[p]; |
523 | f250PulseData->course_time_emulated = TMID[p]; |
524 | f250PulseData->fine_time_emulated = TFINE[p]; |
525 | |
526 | |
527 | uint32_t QF = 0; |
528 | if( bad_pedestal ) QF |= (1<<0); |
529 | if( NSA_beyond_PTW[p] ) QF |= (1<<1); |
530 | if( has_overflow_samples[p] ) QF |= (1<<2); |
531 | if( has_underflow_samples[p] ) QF |= (1<<3); |
532 | if( vpeak_beyond_NSA[p] ) QF |= (1<<4); |
533 | if( vpeak_not_found[p] ) QF |= (1<<5); |
534 | if( bad_timing_pedestal ) QF |= (1<<6); |
535 | f250PulseData->QF_emulated = QF; |
536 | |
537 | if(VERBOSE > 3) { |
538 | cout << boolalpha; |
539 | cout << "bad_pedestal = " << bad_pedestal << endl; |
540 | cout << "NSA_beyond_PTW = " << NSA_beyond_PTW[p] << endl; |
541 | cout << "has_overflow_samples = " << has_overflow_samples[p] << endl; |
542 | cout << "has_underflow_samples = " << has_underflow_samples[p] << endl; |
543 | cout << "vpeak_beyond_NSA = " << vpeak_beyond_NSA[p] << endl; |
544 | cout << "vpeak_not_found = " << vpeak_not_found[p] << endl; |
545 | cout << "bad_timing_pedestal = " << bad_timing_pedestal << endl; |
546 | cout << "total QF = " << QF << endl; |
547 | } |
548 | |
549 | |
550 | if( f250PulseData->emulated ) { |
551 | f250PulseData->integral = f250PulseData->integral_emulated; |
552 | f250PulseData->pedestal = f250PulseData->pedestal_emulated; |
553 | f250PulseData->pulse_peak = f250PulseData->pulse_peak_emulated; |
554 | f250PulseData->course_time = f250PulseData->course_time_emulated; |
555 | f250PulseData->fine_time = f250PulseData->fine_time_emulated; |
556 | |
557 | |
558 | |
559 | |
560 | |
561 | |
562 | |
563 | } |
564 | |
565 | } |
566 | |
567 | if (VERBOSE > 0) jout << " Df250EmulatorAlgorithm_v3::EmulateFirmware ==> Emulation complete <==" << endl; |
568 | return; |
569 | } |