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_v2::EmulateFirmware ==> Starting emulation <==" << endl;
46 jout <<
"rocid : " << rawData->
rocid <<
" slot: " << rawData->
slot <<
" channel: " << rawData->
channel << endl;
50 if (rawData == NULL) {
51 jerr <<
" ERROR: Df250EmulatorAlgorithm_v2::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_v2::EmulateFirmware No Df250BORConfig == Using default values == LAST WARNING" << endl;
82 else jout <<
" WARNING Df250EmulatorAlgorithm_v2::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_v2::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_v2::EmulateFirmware samples[" << i <<
"]: " << samples[i] << endl;
150 unsigned int MAX_SAMPLE = NW-NSAT;
151 for (
unsigned int i=0; i < MAX_SAMPLE; i++) {
152 if ((samples[i] & 0xfff) > THR) {
154 jout <<
"threshold crossing at " << i << endl;
162 int samples_over_threshold = 1;
166 for(
unsigned int j=i+1; ((samples[j]&0xfff)>=THR) && (j<MAX_SAMPLE+1); j++) {
168 if ((samples[j] & 0xfff) > THR)
169 samples_over_threshold++;
171 if( samples_over_threshold == NSAT ) {
179 for(
unsigned int j=i+1; ((samples[j]&0xfff)>THR) && (j<MAX_SAMPLE+1); j++) {
180 samples_over_threshold++;
182 if( samples_over_threshold == NSAT )
188 if( samples_over_threshold != NSAT )
199 ibegin = i > uint32_t(NSB) ? (i - NSB) : 0;
202 if(ibegin > uint32_t(NW))
205 unsigned int iend = (i + NSA) < uint32_t(NW) ? (i + NSA) : NW;
207 NSA_beyond_PTW[npulses] = (i + NSA - 1) >= uint32_t(NW);
208 for (i = ibegin; i < iend; ++i) {
209 pulse_integral[npulses] += (samples[i] & 0xfff);
211 if(samples[i] == 0x1fff) {
212 has_overflow_samples[npulses] =
true;
214 if(samples[i] == 0x1000) {
215 has_underflow_samples[npulses] =
true;
218 if( (i+1>=TC[npulses]) && ((samples[i] & 0xfff) > THR) )
219 number_samples_above_threshold[npulses]++;
221 for (; i < NW && (samples[i] & 0xfff) >= THR; ++i) {}
222 if (++npulses == max_pulses)
231 uint32_t VPEAK[max_pulses] = {};
232 uint32_t TPEAK[max_pulses] = {};
233 uint16_t TMID[max_pulses] = {};
234 uint16_t VMID[max_pulses] = {};
235 uint16_t TFINE[max_pulses] = {};
236 uint32_t pulse_time[max_pulses] = {};
241 for (
unsigned int i=0; i < NPED; i++) {
242 pedestal += (samples[i] & 0xfff);
244 VMIN += (samples[i] & 0xfff);
247 if ((samples[i] & 0xfff) > MAXPED) {
251 if( (samples[i] == 0x1fff) || (samples[i] == 0x1000) ) {
260 for (
unsigned int i=0; i < 4; i++) {
262 if ( ((samples[i] & 0xfff) > MAXPED) || ((samples[i] & 0xfff) > THR) ) {
263 bad_timing_pedestal =
true;
266 if ( (samples[i] == 0x1000) || (samples[i] == 0x1fff) ) {
267 bad_timing_pedestal =
true;
276 if( (samples[i] & 0xfff) > THR ) {
277 no_timing_calculation =
true;
282 for (
unsigned int p=0; p < npulses; ++p) {
290 if(no_timing_calculation) {
294 vpeak_not_found[p] =
true;
302 while ( (!no_timing_calculation) &&
true) {
314 for (ipeak = TC[p]; (int)ipeak < NW-1; ++ipeak) {
316 if ((samples[ipeak] & 0xfff) < (samples[ipeak-1] & 0xfff)) {
317 VPEAK[p] = (samples[ipeak-1] & 0xfff);
324 if(ipeak > TC[p]+NSA)
325 vpeak_beyond_NSA[p] =
true;
328 jout <<
" pulse " << p <<
": VMIN: " << VMIN
329 <<
" TC: " << TC[p] <<
" VPEAK: " << VPEAK[p] << endl;
337 vpeak_beyond_NSA[p] =
true;
338 vpeak_not_found[p] =
true;
343 VMID[p] = (VMIN + VPEAK[p]) >> 1;
359 for (
unsigned int i = TPEAK[p]; i >= 1; --i) {
360 if ( ((samples[i-1] & 0xfff) <= VMID[p]) && ((samples[i] & 0xfff) > VMID[p]) ) {
373 int Vnext = (samples[TMID[p]] & 0xfff);
374 int Vlast = (samples[TMID[p]-1] & 0xfff);
376 jout <<
" TMIN = " << TMIN[p] <<
" TMID = " << TMID[p] <<
" TPEAK = " << TPEAK[p] << endl
377 <<
" VMID = " << VMID[p] <<
" Vnext = " << Vnext <<
" Vlast = " << Vlast << endl;
379 if (Vnext > Vlast && VMID[p] >= Vlast)
380 TFINE[p] = 64 * (VMID[p] - Vlast) / (Vnext - Vlast);
386 pulse_time[p] = ((TMID[p]-1) << 6) + TFINE[p];
389 VMIN = (VMIN < 99999)? VMIN : 0;
392 jout <<
" pulse " << p <<
": VMID: " << VMID[p] <<
" TMID: " << TMID[p]
393 <<
" TFINE: " << TFINE[p] <<
" time: " << pulse_time[p]
394 <<
" integral: " << pulse_integral[p] << endl;
396 jout <<
" TMIN = " << TMIN[p] <<
" TMID = " << TMID[p] <<
" TPEAK = " << TPEAK[p] << endl;
403 if( p < pdat_objs.size() ) {
404 f250PulseData = pdat_objs[p];
406 if(f250PulseData == NULL) {
407 jerr <<
" NULL f250PulseData object!" << endl;
415 f250PulseData->
slot = rawData->
slot;
423 f250PulseData->
integral = pulse_integral[p];
425 f250PulseData->
QF_overflow = has_overflow_samples[p];
440 f250PulseData->AddAssociatedObject(rawData);
442 pdat_objs.push_back(f250PulseData);
454 if( bad_pedestal ) QF |= (1<<0);
455 if( NSA_beyond_PTW[p] ) QF |= (1<<1);
456 if( has_overflow_samples[p] ) QF |= (1<<2);
457 if( has_underflow_samples[p] ) QF |= (1<<3);
458 if( vpeak_beyond_NSA[p] ) QF |= (1<<4);
459 if( vpeak_not_found[p] ) QF |= (1<<5);
460 if( bad_timing_pedestal ) QF |= (1<<6);
465 cout <<
"bad_pedestal = " << bad_pedestal << endl;
466 cout <<
"NSA_beyond_PTW = " << NSA_beyond_PTW[p] << endl;
467 cout <<
"has_overflow_samples = " << has_overflow_samples[p] << endl;
468 cout <<
"has_underflow_samples = " << has_underflow_samples[p] << endl;
469 cout <<
"vpeak_beyond_NSA = " << vpeak_beyond_NSA[p] << endl;
470 cout <<
"vpeak_not_found = " << vpeak_not_found[p] << endl;
471 cout <<
"bad_timing_pedestal = " << bad_timing_pedestal << endl;
472 cout <<
"total QF = " << QF << endl;
493 if (
VERBOSE > 0) jout <<
" Df250EmulatorAlgorithm_v2::EmulateFirmware ==> Emulation complete <==" << endl;
uint32_t pedestal_emulated
Value calculated from raw data (if available)
vector< uint16_t > samples
void EmulateFirmware(const Df250WindowRawData *rawData, std::vector< Df250PulseData * > &pdatt_objs)
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
uint32_t nsamples_pedestal
number of samples used in pedestal
static TH1I * pedestal[nChan]
Df250EmulatorAlgorithm_v2()
uint32_t event_within_block
uint32_t pulse_number
pulse number for this channel, this event starting from 0