29 gPARMS->SetDefaultParameter(
"EMULATION250:FORCE_DEFAULT",
FORCE_DEFAULT,
"Set to >0 to force use of default values");
30 gPARMS->SetDefaultParameter(
"EMULATION250:NSA",
NSA_DEF,
"Set NSA for firmware emulation, will be overwritten by BORConfig if present");
31 gPARMS->SetDefaultParameter(
"EMULATION250:NSB",
NSB_DEF,
"Set NSB for firmware emulation, will be overwritten by BORConfig if present");
32 gPARMS->SetDefaultParameter(
"EMULATION250:THR",
THR_DEF,
"Set threshold for firmware emulation, will be overwritten by BORConfig if present");
33 gPARMS->SetDefaultParameter(
"EMULATION250:NPED",
NPED_DEF,
"Set NPED for firmware emulation, will be overwritten by BORConfig if present");
34 gPARMS->SetDefaultParameter(
"EMULATION250:MAXPED",
MAXPED_DEF,
"Set MAXPED for firmware emulation, will be overwritten by BORConfig if present");
35 gPARMS->SetDefaultParameter(
"EMULATION250:NSAT",
NSAT_DEF,
"Set NSAT for firmware emulation, will be overwritten by BORConfig if present");
36 gPARMS->SetDefaultParameter(
"EMULATION250:VERBOSE",
VERBOSE,
"Set verbosity for f250 emulation");
41 std::vector<Df250PulseData*> &pdat_objs)
45 jout <<
" Df250EmulatorAlgorithm_v3::EmulateFirmware ==> Starting emulation <==" << endl;
46 jout <<
"rocid : " << rawData->
rocid <<
" slot: " << rawData->
slot <<
" channel: " << rawData->
channel << endl;
50 if (rawData == NULL) {
51 jerr <<
" ERROR: Df250EmulatorAlgorithm_v3::EmulateFirmware - raw sample data is missing" << endl;
52 jerr <<
" Contact mstaib@jlab.org" << endl;
57 uint32_t channel = rawData->
channel;
63 rawData->GetSingle(f250BORConfig);
67 uint32_t NPED, MAXPED;
81 if (counter == 10) jout <<
" WARNING Df250EmulatorAlgorithm_v3::EmulateFirmware No Df250BORConfig == Using default values == LAST WARNING" << endl;
82 else jout <<
" WARNING Df250EmulatorAlgorithm_v3::EmulateFirmware No Df250BORConfig == Using default values " << endl;
87 NSA = f250BORConfig->
NSA;
88 NSB = f250BORConfig->
NSB;
90 NPED = f250BORConfig->
NPED;
91 MAXPED = f250BORConfig->
MaxPed;
92 NSAT = f250BORConfig->
NSAT;
96 if (
VERBOSE > 0) jout <<
"Df250EmulatorAlgorithm_v3::EmulateFirmware NSA: " << NSA <<
" NSB: " << NSB <<
" THR: " << THR << endl;
110 bool bad_pedestal =
false;
111 bool bad_timing_pedestal =
false;
112 bool no_timing_calculation =
false;
119 vector<uint16_t> samples = rawData->
samples;
120 uint16_t NW = samples.size();
121 uint32_t npulses = 0;
122 const int max_pulses = 3;
123 uint32_t TC[max_pulses] = {};
124 uint32_t
TMIN[max_pulses] = {3};
126 uint32_t pulse_integral[max_pulses] = {};
127 bool has_overflow_samples[max_pulses] = {
false};
128 bool has_underflow_samples[max_pulses] = {
false};
129 uint32_t number_samples_above_threshold[max_pulses] = {0};
130 bool NSA_beyond_PTW[max_pulses] = {
false};
131 bool vpeak_beyond_NSA[max_pulses] = {
false};
132 bool vpeak_not_found[max_pulses] = {
false};
136 for (
unsigned int i=0; i < NW; i++) {
138 if(samples[i] == 0x1fff)
139 jout <<
"Overflow at sample " << i << endl;
140 if(samples[i] == 0x1000)
141 jout <<
"Underflow at sample " << i << endl;
143 if (
VERBOSE > 5) jout <<
"Df250EmulatorAlgorithm_v3::EmulateFirmware samples[" << i <<
"]: " << samples[i] << endl;
150 unsigned int MAX_SAMPLE = NW-NSAT;
152 for (
unsigned int i=0; i < MAX_SAMPLE; i++) {
153 if ((samples[i] & 0xfff) > THR) {
155 jout <<
"threshold crossing at " << i << endl;
163 int samples_over_threshold = 1;
168 for(
unsigned int j=i+1; ((samples[j]&0xfff)>THR) && (j<MAX_SAMPLE+1); j++) {
170 if ((samples[j] & 0xfff) > THR)
171 samples_over_threshold++;
173 if( samples_over_threshold == NSAT ) {
181 for(
unsigned int j=i+1; ((samples[j]&0xfff)>THR) && (j<MAX_SAMPLE+1); j++) {
182 samples_over_threshold++;
184 if( samples_over_threshold == NSAT )
190 if( samples_over_threshold != NSAT )
201 ibegin = i > uint32_t(NSB) ? (i - NSB) : 0;
204 if(ibegin > uint32_t(NW))
207 unsigned int iend = (i + NSA) < uint32_t(NW) ? (i + NSA) : NW;
209 NSA_beyond_PTW[npulses] = (i + NSA - 1) >= uint32_t(NW);
210 for (i = ibegin; i < iend; ++i) {
211 pulse_integral[npulses] += (samples[i] & 0xfff);
213 if(samples[i] == 0x1fff) {
214 has_overflow_samples[npulses] =
true;
216 if(samples[i] == 0x1000) {
217 has_underflow_samples[npulses] =
true;
220 if( (i+1>=TC[npulses]) && ((samples[i] & 0xfff) > THR) )
221 number_samples_above_threshold[npulses]++;
223 for (; i < NW && (samples[i] & 0xfff) >= THR; ++i) {}
224 if (++npulses == max_pulses)
233 uint32_t VPEAK[max_pulses] = {};
234 uint32_t TPEAK[max_pulses] = {};
235 uint16_t TMID[max_pulses] = {};
236 uint16_t VMID[max_pulses] = {};
237 uint16_t TFINE[max_pulses] = {};
238 uint32_t pulse_time[max_pulses] = {};
243 for (
unsigned int i=0; i < NPED; i++) {
244 pedestal += (samples[i] & 0xfff);
246 VMIN += (samples[i] & 0xfff);
249 if ((samples[i] & 0xfff) > MAXPED) {
253 if( (samples[i] == 0x1fff) || (samples[i] == 0x1000) ) {
261 for (
unsigned int i=0; i < 4; i++) {
263 if ( ((samples[i] & 0xfff) > MAXPED) || ((samples[i] & 0xfff) > THR) ) {
264 bad_timing_pedestal =
true;
267 if ( (samples[i] == 0x1000) || (samples[i] == 0x1fff) ) {
268 bad_timing_pedestal =
true;
274 if( (samples[i] & 0xfff) > THR ) {
275 no_timing_calculation =
true;
280 for (
unsigned int p=0; p < npulses; ++p) {
286 if(no_timing_calculation) {
313 for (ipeak = TC[p]; (int)ipeak < NW-1; ++ipeak) {
315 if ((samples[ipeak] & 0xfff) < (samples[ipeak-1] & 0xfff)) {
316 VPEAK[p] = (samples[ipeak-1] & 0xfff);
323 if(ipeak > TC[p]+NSA)
324 vpeak_beyond_NSA[p] =
true;
327 jout <<
" pulse " << p <<
": VMIN: " << VMIN
328 <<
" TC: " << TC[p] <<
" VPEAK: " << VPEAK[p] << endl;
336 vpeak_beyond_NSA[p] =
true;
337 vpeak_not_found[p] =
true;
342 if(no_timing_calculation)
346 VMID[p] = (VMIN + VPEAK[p]) >> 1;
354 for (
unsigned int i = TPEAK[p]; i >= 1; --i) {
355 if ( ((samples[i-1] & 0xfff) <= VMID[p]) && ((samples[i] & 0xfff) > VMID[p]) ) {
368 int Vnext = (samples[TMID[p]] & 0xfff);
369 int Vlast = (samples[TMID[p]-1] & 0xfff);
371 jout <<
" TMIN = " << TMIN[p] <<
" TMID = " << TMID[p] <<
" TPEAK = " << TPEAK[p] << endl
372 <<
" VMID = " << VMID[p] <<
" Vnext = " << Vnext <<
" Vlast = " << Vlast << endl;
374 if (Vnext > Vlast && VMID[p] >= Vlast)
375 TFINE[p] = 64 * (VMID[p] - Vlast) / (Vnext - Vlast);
381 pulse_time[p] = ((TMID[p]-1) << 6) + TFINE[p];
384 VMIN = (VMIN < 99999)? VMIN : 0;
387 jout <<
" pulse " << p <<
": VMID: " << VMID[p] <<
" TMID: " << TMID[p]
388 <<
" TFINE: " << TFINE[p] <<
" time: " << pulse_time[p]
389 <<
" integral: " << pulse_integral[p] << endl;
391 jout <<
" TMIN = " << TMIN[p] <<
" TMID = " << TMID[p] <<
" TPEAK = " << TPEAK[p] << endl;
398 if( p < pdat_objs.size() ) {
399 f250PulseData = pdat_objs[p];
401 if(f250PulseData == NULL) {
402 jerr <<
" NULL f250PulseData object!" << endl;
410 f250PulseData->
slot = rawData->
slot;
418 f250PulseData->
integral = pulse_integral[p];
420 f250PulseData->
QF_overflow = has_overflow_samples[p];
435 f250PulseData->AddAssociatedObject(rawData);
437 pdat_objs.push_back(f250PulseData);
449 if( bad_pedestal ) QF |= (1<<0);
450 if( NSA_beyond_PTW[p] ) QF |= (1<<1);
451 if( has_overflow_samples[p] ) QF |= (1<<2);
452 if( has_underflow_samples[p] ) QF |= (1<<3);
453 if( vpeak_beyond_NSA[p] ) QF |= (1<<4);
454 if( vpeak_not_found[p] ) QF |= (1<<5);
455 if( bad_timing_pedestal ) QF |= (1<<6);
460 cout <<
"bad_pedestal = " << bad_pedestal << endl;
461 cout <<
"NSA_beyond_PTW = " << NSA_beyond_PTW[p] << endl;
462 cout <<
"has_overflow_samples = " << has_overflow_samples[p] << endl;
463 cout <<
"has_underflow_samples = " << has_underflow_samples[p] << endl;
464 cout <<
"vpeak_beyond_NSA = " << vpeak_beyond_NSA[p] << endl;
465 cout <<
"vpeak_not_found = " << vpeak_not_found[p] << endl;
466 cout <<
"bad_timing_pedestal = " << bad_timing_pedestal << endl;
467 cout <<
"total QF = " << QF << endl;
488 if (
VERBOSE > 0) jout <<
" Df250EmulatorAlgorithm_v3::EmulateFirmware ==> Emulation complete <==" << endl;
Df250EmulatorAlgorithm_v3()
uint32_t pedestal_emulated
Value calculated from raw data (if available)
vector< uint16_t > samples
uint32_t course_time_emulated
Value calculated from raw data (if available) - debug.
bool emulated
true if made from Window Raw Data
uint32_t nsamples_integral
number of samples used in integral
uint32_t fine_time_emulated
Value calculated from raw data (if available) - debug.
uint32_t pulse_peak_emulated
Value calculated from raw data (if available)
uint32_t integral_emulated
Value calculated from raw data (if available)
uint32_t nsamples_over_threshold
void EmulateFirmware(const Df250WindowRawData *rawData, std::vector< Df250PulseData * > &pdatt_objs)
uint32_t nsamples_pedestal
number of samples used in pedestal
static TH1I * pedestal[nChan]
uint32_t event_within_block
uint32_t pulse_number
pulse number for this channel, this event starting from 0