Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
fa125fns.h
Go to the documentation of this file.
1 
2 #include <stdint.h>
3 #include <vector>
4 #include <iostream>
5 using namespace std;
6 
7 #include <Rtypes.h> // ROOT type definitions (e.g. Int_t)
8 
9 
10 // FA125 emulation functions cdc_hit, cdc_time etc for NPK=1 (only the first peak is identified)
11 //
12 // Peak amplitude and integral are returned without pedestal subtraction
13 //
14 // Returned values are unscaled. (ie IBIT, ABIT, PBIT are not used here)
15 //
16 // The pedestal returned is the mean of the NPED2 samples ending PED samples before the sample containing the hit threshold crossing.
17 //
18 //
19 // The ADC data buffer has NW samples in total, comprising
20 // [unused samples][NPED samples for pedestal][samples WINDOW_START to WINDOW_END][20 or more extra samples]
21 //
22 // WINDOW_START is the sample immediately after the NPED samples in the pedestal window.
23 // The hit search starts with sample WINDOW_START+PG and ends including sample WINDOW_END. This is for ease of use with older data.
24 // In the new firmware, there are no unused samples at the start, and 20 samples at the end.
25 // The first sample is numbered sample 0.
26 //
27 //
28 // This requires that:
29 // WINDOW_START >= NPED
30 // WINDOW_END+20 < NW (number of samples supplied)
31 // NPED2 >= NPED
32 //
33 
34 
35  // cdc_time q_code values:
36 
37  // q_code Time returned Condition
38  // 0 Leading edge time Good
39  // 1 X*10 - 29 Sample value of 0 found
40  // 1 X*10 - 28 Sample value greater than PED_MAX found in adc[0 to PED]
41  // 1 X*10 - 27 Sample values lie below the high timing threshold
42  // 1 TCL*10 + 4 Low timing threshold crossing sample TCL occurs too late to upsample
43  // 1 TCL*10 + 5 One or more upsampled values are negative
44  // 1 TCL*10 + 9 The upsampled values are too low
45 
46 
47 void cdc_hit(Int_t&, Int_t&, Int_t&, Int_t[], Int_t, Int_t, Int_t, Int_t, Int_t, Int_t); // look for a hit
48 void cdc_time(Int_t&, Int_t&, Int_t[], Int_t, Int_t, Int_t, Int_t); // find hit time
49 void cdc_integral(Long_t&, Int_t&, Int_t, Int_t[], Int_t, Int_t); // find integral
50 void cdc_max(Int_t&, Int_t&, Int_t, Int_t[], Int_t); // find first max amplitude after hit
51 void upsamplei(Int_t[], Int_t, Int_t[], Int_t); // upsample
52 
53 
54 
55 void cdc_hit(Int_t &hitfound, Int_t &hitsample, Int_t &pedestal, Int_t adc[], Int_t WINDOW_START, Int_t WINDOW_END, Int_t HIT_THRES, Int_t NPED, Int_t NPED2, Int_t PG) {
56 
57 
58  pedestal=0; //pedestal
59  Int_t threshold=0;
60 
61  Int_t i=0;
62 
63  // calc pedestal as mean of NPED samples before trigger
64  for (i=0; i<NPED; i++) {
65  pedestal += adc[WINDOW_START-NPED+i];
66  }
67 
68  pedestal = ( NPED==0 ? 0:(pedestal/NPED) ); // Integer div is ok as fpga will do 2 rightshifts
69 
70  threshold = pedestal + HIT_THRES;
71 
72  // look for threshold crossing
73  i = WINDOW_START - 1 + PG;
74  hitfound = 0;
75 
76  while ((hitfound==0) && (i<WINDOW_END-1)) {
77 
78  i++;
79 
80  if (adc[i] >= threshold) {
81  if (adc[i+1] >= threshold) {
82  hitfound = 1;
83  hitsample = i;
84  }
85  }
86  }
87 
88  if (hitfound == 1) {
89 
90  //calculate new pedestal ending just before the hit
91 
92  pedestal = 0;
93 
94  for (i=0; i<NPED2; i++) {
95  pedestal += adc[hitsample-PG-i];
96  }
97 
98  pedestal = ( NPED2==0 ? 0:(pedestal/NPED2) );
99  }
100 
101 
102 }
103 
104 
105 
106 
107 void cdc_integral(Long_t& integral, Int_t& overflows, Int_t timesample, Int_t adc[], Int_t WINDOW_END, Int_t INT_END) {
108 
109  Int_t i=0;
110 
111  integral = 0;
112  overflows = 0;
113 
114  if (timesample <= WINDOW_END) {
115 
116  Int_t lastsample = timesample + INT_END - 1;
117 
118  if (lastsample > WINDOW_END) lastsample = WINDOW_END;
119 
120  for (i = timesample; i <= lastsample; i++ ) {
121 
122  integral += (Long_t)adc[i];
123  if (adc[i]==(Long_t)4095) overflows++;
124 
125  }
126 
127  }
128 
129 
130 }
131 
132 
133 
134 
135 void cdc_max(Int_t& maxamp, Int_t &maxsample, Int_t hitsample, Int_t adc[], Int_t WINDOW_END) {
136 
137  int i;
138  int ndec = 0; //number of decreasing samples
139 
140  //make sure we are on an up-slope
141 
142  while ((adc[hitsample] <= adc[hitsample-1]) && (hitsample <= WINDOW_END)) hitsample++;
143 
144 
145  maxamp = adc[hitsample];
146  maxsample = hitsample;
147 
148 
149  for (i=hitsample; i<=WINDOW_END; i++) {
150 
151  if (adc[i] > adc[i-1]) {
152  maxamp = adc[i];
153  maxsample = i;
154  ndec = 0;
155  }
156 
157  if (adc[i] <= adc[i-1]) ndec++;
158  if (ndec==2) break;
159  }
160 
161 
162  if (hitsample >= WINDOW_END) {
163  maxamp = adc[WINDOW_END];
164  maxsample = WINDOW_END;
165  }
166 
167 }
168 
169 
170 
171 
172 void cdc_time(Int_t &le_time, Int_t &q_code, Int_t adc[], Int_t NU, Int_t PG, Int_t THRES_HIGH, Int_t THRES_LOW) {
173 
174 
175  // adc[NU] array of samples
176  // NU=20 size of array
177  // PG pedestal gap - hit threshold crossing is PG samples after adc[PED] - the single sample to be used as pedestal here
178  // THRES_HIGH high timing threshold (eg 4 x pedestal-width )
179  // THRES_LOW high timing threshold (eg 1 x pedestal-width )
180  //
181  // le_time leading edge time as 0.1x number of samples since first sample supplied
182  // q_code quality code, 0=good, >0=rough estimate (firmware returns 0=good, 1=not so good)
183  //
184  // q_code Time returned Condition
185  // 0 Leading edge time Good
186  // 1 X*10 - 29 Sample value of 0 found
187  // 1 X*10 - 28 Sample value greater than PED_MAX found in adc[0 to PED]
188  // 1 X*10 - 27 Sample values lie below the high timing threshold
189  // 1 TCL*10 + 4 Low timing threshold crossing sample TCL occurs too late to upsample
190  // 1 TCL*10 + 5 One or more upsampled values are negative
191  // 1 TCL*10 + 9 The upsampled values are too low
192  //
193 
194 
195 
196  const Int_t NUPSAMPLED = 6; // number of upsampled values to calculate, minimum is 6
197  const Int_t SET_ADC_MIN = 20; // adjust adc values so that the min is at 20
198  const Int_t LIMIT_PED_MAX = 511; // max acceptable value in adc[0 to PED]
199  const Int_t PED = 5; // take local pedestal to be adc[PED]
200 
201  const Int_t START_SEARCH = PED+1; // -- start looking for hi threshold xing with this sample
202 
203  const Int_t X = PED + PG; // hit threshold crossing sample is adc[X]
204  const Int_t ROUGH_TIME = (X*10)-30; // -- add onto this to return rough time estimates
205 
206  Int_t iubuf[NUPSAMPLED] = {0}; // array of upsampled values; iubuf[0] maps to low thres xing sample
207 
208  Int_t adc_thres_hi = 0; // high threshold
209  Int_t adc_thres_lo = 0; // low threshold
210 
211  // -- contributions to hit time, these are summed together eventually, units of sample/10
212  Int_t itime1 = 0; // which sample
213  Int_t itime2 = 0; // which minisample
214  Int_t itime3 = 0; // correction from interpolation
215 
216  // -- search vars
217  Int_t adc_sample_hi = 0; // integer range 0 to NU := 0; --sample number for adc val at or above hi thres
218  Int_t adc_sample_lo = 0; // integer range 0 to NU := 0; -- sample num for adc val at or below lo thres
219  Int_t adc_sample_lo2 = 0; // integer range 0 to 12:= 0; -- minisample num for adc val at or below lo thres
220 
221  Bool_t over_threshold = kFALSE;
222  Bool_t below_threshold = kFALSE;
223 
224 
225  // upsampling checks
226  Int_t ups_adjust = 0;
227 
228  Int_t i = 0;
229 
230 
231 
232  //check all samples are >0
233  //check all samples from 0 to pedestal are <= LIMIT_PED_MAX
234  //adc=zero and pedestal limit checks are in same fadc clock cycle
235 
236 
237  i = 0;
238  while (i<NU) {
239 
240  if (adc[i] == 0) {
241  le_time = ROUGH_TIME + 1;
242  q_code = 1;
243  }
244 
245  if ((i < PED+1) && (adc[i] > LIMIT_PED_MAX)) {
246  le_time = ROUGH_TIME + 2;
247  q_code = 2;
248  }
249  i++;
250  }
251 
252  if (q_code>0) return;
253 
254 
255  // add offset to move min val in subset equal to SET_ADC_MIN
256  // this is to move samples away from 0 to avoid upsampled pts going -ve (on a curve betw 2 samples)
257 
258  Int_t adcmin = 4095;
259 
260  i=0;
261 
262  while (i<NU) {
263 
264  if (adc[i] < adcmin) {
265  adcmin = adc[i];
266  }
267 
268  i++;
269  }
270 
271  Int_t adcoffset = SET_ADC_MIN - adcmin;
272 
273  i=0;
274 
275  while (i<NU) {
276  adc[i] = adc[i] + adcoffset;
277  // if (adc[i] > 4095) adc[i] = 4095; //DO NOT saturate
278  i++;
279  }
280 
281  // eg if adcmin is 100, setmin is 30, adcoffset = 30 - 100 = -70, move adc down by 70
282 
283 
284  //////////////////////////////
285 
286  // calc thresholds
287 
288  adc_thres_hi = adc[PED] + THRES_HIGH;
289  adc_thres_lo = adc[PED] + THRES_LOW;
290 
291  // search for high threshold crossing
292 
293  over_threshold = kFALSE;
294  i = START_SEARCH;
295 
296  while ((!over_threshold)&&(i<NU)) {
297 
298  if (adc[i] >= adc_thres_hi) {
299  adc_sample_hi = i;
300  over_threshold = kTRUE;
301  }
302 
303  i++;
304  }
305 
306 
307  if (!over_threshold) {
308 
309  le_time = ROUGH_TIME + 3;
310  q_code = 3;
311  return;
312 
313  }
314 
315 
316  // search for low threshold crossing
317 
318  below_threshold = kFALSE;
319  i = adc_sample_hi-1;
320 
321  while ((!below_threshold) && (i>=PED)) {
322 
323  if (adc[i] <= adc_thres_lo) {
324  adc_sample_lo = i;
325  itime1 = i*10;
326  below_threshold = kTRUE;
327  }
328 
329  i--;
330  }
331 
332 
333 
334  if (adc[adc_sample_lo] == adc_thres_lo) { // no need to upsample
335 
336  le_time = itime1;
337  q_code = 0;
338  return;
339 
340  }
341 
342 
343  if (adc_sample_lo > NU-7) { // too late to upsample
344 
345  le_time = itime1 + 4;
346  q_code = 4;
347  return;
348 
349  }
350 
351 
352 
353  //upsample values from adc_sample_lo to adc_sample_lo + 1
354 
355  upsamplei(adc, adc_sample_lo, iubuf, NUPSAMPLED);
356 
357 
358 
359  //check upsampled values are >0
360 
361  Bool_t negups = kFALSE;
362 
363  i=0;
364  while ((!negups)&&(i<NUPSAMPLED)) {
365 
366  if (iubuf[i] < 0 ) {
367  negups = kTRUE;
368  }
369 
370  i++;
371  }
372 
373 
374  if (negups) {
375 
376  le_time = itime1 + 5;
377  q_code = 5;
378  return;
379 
380  }
381 
382 
383  // correct errors
384  // iubuf[0] should be equal to adc[adc_sample_lo] and iubuf[5] should equal adc[adc_sample_lo+1]
385  // very steep pulse gradients cause errors in upsampling with larger errors in the later values
386  // match iubuf[0] to adc[adc_sample_lo] so that the threshold crossing must be at or after iubuf[0]
387 
388  ups_adjust = iubuf[0] - adc[adc_sample_lo];
389 
390  // move threshold correspondingly instead of correcting upsampled values
391 
392  adc_thres_lo = adc_thres_lo + ups_adjust;
393 
394  // check that threshold crossing lies within the range of iubuf[0 to 5]
395 
396  if (iubuf[NUPSAMPLED-1]<= adc_thres_lo) { //bad upsampling
397 
398  le_time = itime1 + 9; //midway
399  q_code = 6;
400  return;
401 
402  }
403 
404 
405 
406  // search through upsampled array
407 
408  below_threshold = kFALSE;
409  i = NUPSAMPLED-2;
410 
411  while ((!below_threshold) && (i>=0)) {
412 
413  if (iubuf[i] <= adc_thres_lo) {
414  adc_sample_lo2 = i;
415  below_threshold = kTRUE;
416  }
417 
418  i--;
419  }
420 
421 
422 
423  if (!below_threshold) { //upsampled points did not go below thres
424 
425  printf("upsampled points did not go below threshold - should be impossible\n");
426  le_time = 0;
427  q_code = 9;
428  return;
429  }
430 
431 
432 
433 
434  itime2 = adc_sample_lo2*2; // convert from sample/5 to sample/10
435 
436 
437 
438  //interpolate
439 
440  itime3 = 0;
441 
442 
443  if (iubuf[adc_sample_lo2] != adc_thres_lo) {
444 
445  if (2*adc_thres_lo >= iubuf[adc_sample_lo2] + iubuf[adc_sample_lo2+1]) itime3 = 1;
446 
447  }
448 
449 
450  le_time = itime1 + itime2 + itime3; // -- this is time from first sample point, in 1/10ths of samples
451  q_code = 0;
452 
453 
454 }
455 
456 
457 void cdc_time2(Double_t &le_time, Int_t &q_code, Int_t pedestal, Int_t adc[], Int_t NU, Int_t PG, Int_t THRES_HIGH, Int_t THRES_LOW) {
458 
459  // DIFFERENT TIME CALC!!
460 
461  // adc[NU] array of samples
462  // NU=20 size of array
463  // PG pedestal gap - hit threshold crossing is PG samples after adc[PED] - the single sample to be used as pedestal here
464  // THRES_HIGH high timing threshold (eg 4 x pedestal-width )
465  // THRES_LOW high timing threshold (eg 1 x pedestal-width )
466  //
467  // le_time leading edge time as 0.1x number of samples since first sample supplied
468  // q_code quality code, 0=good, >0=rough estimate (firmware returns 0=good, 1=not so good)
469  //
470  // q_code Time returned Condition
471  // 0 Leading edge time Good
472  // 1 X*10 - 29 Sample value of 0 found
473  // 1 X*10 - 28 Sample value greater than PED_MAX found in adc[0 to PED]
474  // 1 X*10 - 27 Sample values lie below the high timing threshold
475  // 1 TCL*10 + 4 Low timing threshold crossing sample TCL occurs too late to upsample
476  // 1 TCL*10 + 5 One or more upsampled values are negative
477  // 1 TCL*10 + 9 The upsampled values are too low
478  //
479 
480 
481 
482  const Int_t NUPSAMPLED = 6; // number of upsampled values to calculate, minimum is 6
483  const Int_t SET_ADC_MIN = 20; // adjust adc values so that the min is at 20
484  const Int_t LIMIT_PED_MAX = 511; // max acceptable value in adc[0 to PED]
485  Int_t PED = 5; // take local pedestal to be adc[PED]
486 
487  const Int_t START_SEARCH = PED+1; // -- start looking for hi threshold xing with this sample
488 
489  const Int_t X = PED + PG; // hit threshold crossing sample is adc[X]
490  const Int_t ROUGH_TIME = (X*10)-30; // -- add onto this to return rough time estimates
491 
492  Int_t iubuf[NUPSAMPLED] = {0}; // array of upsampled values; iubuf[0] maps to low thres xing sample
493 
494  Int_t adc_thres_hi = 0; // high threshold
495  Int_t adc_thres_lo = 0; // low threshold
496 
497  // -- contributions to hit time, these are summed together eventually, units of sample/10
498  Int_t itime1 = 0; // which sample
499  Int_t itime2 = 0; // which minisample
500  Int_t itime3 = 0; // correction from interpolation
501 
502  // -- search vars
503  Int_t adc_sample_hi = 0; // integer range 0 to NU := 0; --sample number for adc val at or above hi thres
504  Int_t adc_sample_hi2 = 0; // integer range 0 to NU := 0; --sample number for adc val at or above hi thres
505  Int_t adc_sample_lo = 0; // integer range 0 to NU := 0; -- sample num for adc val at or below lo thres
506  Int_t adc_sample_lo2 = 0; // integer range 0 to 12:= 0; -- minisample num for adc val at or below lo thres
507 
508  Bool_t over_threshold = kFALSE;
509  Bool_t below_threshold = kFALSE;
510 
511 
512  // upsampling checks
513  Int_t ups_adjust = 0;
514 
515  Int_t i = 0;
516 
517  int VERBOSE = 0;
518 
519 
520 
521  le_time = 0;
522 
523  //check all samples are >0
524  //check all samples from 0 to pedestal are <= LIMIT_PED_MAX
525  //adc=zero and pedestal limit checks are in same fadc clock cycle
526 
527 
528  i = 0;
529  while (i<NU) {
530 
531  if (adc[i] == 0) {
532  le_time = ROUGH_TIME + 1;
533  q_code = 1;
534  }
535 
536  if ((i < PED+1) && (adc[i] > LIMIT_PED_MAX)) {
537  le_time = ROUGH_TIME + 2;
538  q_code = 2;
539  }
540  i++;
541  }
542 
543  if (q_code>0) return;
544 
545 
546  // add offset to move min val in subset equal to SET_ADC_MIN
547  // this is to move samples away from 0 to avoid upsampled pts going -ve (on a curve betw 2 samples)
548 
549  Int_t adcmin = 4095;
550 
551  i=0;
552 
553  while (i<NU) {
554 
555  if (adc[i] < adcmin) {
556  adcmin = adc[i];
557  }
558 
559  i++;
560  }
561 
562  Int_t adcoffset = SET_ADC_MIN - adcmin;
563 
564  i=0;
565 
566  while (i<NU) {
567  adc[i] = adc[i] + adcoffset;
568  if (adc[i] > 4095) adc[i] = 4095; //saturate
569  i++;
570  }
571 
572  // eg if adcmin is 100, setmin is 30, adcoffset = 30 - 100 = -70, move adc down by 70
573 
574 
575  // find max adc value
576 
577  int maxamp = 0;
578  i = 0;
579 
580  while (i<NU) {
581  if (adc[i]>maxamp) maxamp = adc[i];
582  i++;
583  }
584 
585  //*** set pedestal according to maxamp.....
586 
587  if (VERBOSE) printf("\nfound maxamp %i\n",maxamp);
588 
589  /* PED = 4; */
590  /* if (maxamp>200) PED = 5; */
591  /* if (maxamp>500) PED = 6; */
592  /* if (maxamp>3000) PED = 7; */
593 
594  // pedestal = adc[PED];
595 
596 
597  adc_thres_hi = pedestal + THRES_HIGH;
598  adc_thres_lo = pedestal + THRES_LOW;
599 
600 
601  //////////////////////////////
602 
603  // calc thresholds
604 
605  //adc_thres_hi = adc[PED] + THRES_HIGH;
606  //adc_thres_lo = adc[PED] + THRES_LOW;
607 
608  if (VERBOSE) printf("thresholds %i %i\n",adc_thres_hi,adc_thres_lo);
609 
610  // search for high threshold crossing
611 
612  over_threshold = kFALSE;
613  i = START_SEARCH;
614 
615  while ((!over_threshold)&&(i<NU)) {
616 
617  if (adc[i] >= adc_thres_hi) {
618  adc_sample_hi = i;
619  over_threshold = kTRUE;
620  }
621 
622  i++;
623  }
624 
625 
626  if (!over_threshold) {
627 
628  le_time = ROUGH_TIME + 3;
629  q_code = 3;
630  return;
631 
632  }
633 
634  if (VERBOSE) printf("found high threshold upward xing in sample %i val %i\n",adc_sample_hi,adc[adc_sample_hi]);
635 
636  // search for low threshold crossing
637 
638  below_threshold = kFALSE;
639  i = adc_sample_hi-1;
640 
641  while ((!below_threshold) && (i>=PED)) {
642 
643  if (adc[i] <= adc_thres_lo) {
644  adc_sample_lo = i;
645  itime1 = i*10;
646  below_threshold = kTRUE;
647  }
648 
649  i--;
650  }
651 
652  if (VERBOSE) printf("found low threshold downward xing in sample %i val %i\n",adc_sample_lo,adc[adc_sample_lo]);
653 
654 
655  if (adc[adc_sample_lo] == adc_thres_lo) { // no need to upsample */
656 
657  le_time = itime1;
658  q_code = 0;
659  // return;
660 
661  }
662 
663 
664  if (adc_sample_lo > NU-7) { // too late to upsample
665 
666  le_time = itime1 + 4;
667  q_code = 4;
668  return;
669 
670  }
671 
672 
673 
674  if (le_time == 0) {
675 
676  //upsample values from adc_sample_lo to adc_sample_lo + 1
677 
678  if (VERBOSE) printf("upsampling sample %i val %i to next val %i \n",adc_sample_lo,adc[adc_sample_lo],adc[adc_sample_lo+1]);
679 
680  upsamplei(adc, adc_sample_lo, iubuf, NUPSAMPLED);
681 
682  for (i=0;i<NUPSAMPLED; i++) if (VERBOSE) printf("iubuf %i\n",iubuf[i]);
683 
684 
685  //check upsampled values are >0
686 
687  Bool_t negups = kFALSE;
688 
689  i=0;
690  while ((!negups)&&(i<NUPSAMPLED)) {
691 
692  if (iubuf[i] < 0 ) {
693  negups = kTRUE;
694  }
695 
696  i++;
697  }
698 
699 
700  if (negups) {
701 
702  le_time = itime1 + 5;
703  q_code = 5;
704  return;
705 
706  }
707 
708 
709  // correct errors
710  // iubuf[0] should be equal to adc[adc_sample_lo] and iubuf[5] should equal adc[adc_sample_lo+1]
711  // very steep pulse gradients cause errors in upsampling with larger errors in the later values
712  // match iubuf[0] to adc[adc_sample_lo] so that the threshold crossing must be at or after iubuf[0]
713 
714  ups_adjust = iubuf[0] - adc[adc_sample_lo];
715 
716  // move threshold correspondingly instead of correcting upsampled values
717 
718  adc_thres_lo = adc_thres_lo + ups_adjust;
719 
720  // check that threshold crossing lies within the range of iubuf[0 to 5]
721 
722  if (iubuf[NUPSAMPLED-1]<= adc_thres_lo) { //bad upsampling
723  le_time = itime1 + 9; //midway
724  q_code = 6;
725  return;
726  }
727 
728  // search through upsampled array
729 
730  below_threshold = kFALSE;
731  i = NUPSAMPLED-2;
732 
733  while ((!below_threshold) && (i>=0)) {
734 
735  if (iubuf[i] <= adc_thres_lo) {
736  adc_sample_lo2 = i;
737  below_threshold = kTRUE;
738  }
739 
740  i--;
741  }
742 
743  if (!below_threshold) { //upsampled points did not go below thres
744  if (VERBOSE) printf("upsampled points did not go below threshold - should be impossible\n");
745  le_time = 0;
746  q_code = 9;
747  return;
748  }
749 
750  itime2 = adc_sample_lo2*2; // convert from sample/5 to sample/10
751 
752 
753 
754  //interpolate
755 
756  itime3 = 0;
757 
758  if (iubuf[adc_sample_lo2] != adc_thres_lo) {
759  if (2*adc_thres_lo >= iubuf[adc_sample_lo2] + iubuf[adc_sample_lo2+1]) itime3 = 1;
760  }
761 
762 
763  le_time = itime1 + itime2 + itime3; // -- this is time from first sample point, in 1/10ths of samples
764  q_code = 0;
765 
766  }
767 
768 
769  // extra part below here.
770 
771 
772  double tl_time = adc_sample_lo*10 + adc_sample_lo2*2;
773 
774  if ( adc_thres_lo > iubuf[adc_sample_lo2] ) {
775 
776  if ( iubuf[adc_sample_lo2] == iubuf[adc_sample_lo2 + 1] ) {
777  if (VERBOSE) printf("iubuf plateau \n");
778  } else {
779  tl_time += 2*(adc_thres_lo - iubuf[adc_sample_lo2])/double(iubuf[adc_sample_lo2 + 1] - iubuf[adc_sample_lo2]);
780  }
781  } else {
782  if (VERBOSE) printf("adc_thres_lo %i = iubuf[adc_sample_lo2]\n",adc_thres_lo);
783  }
784 
785  if (VERBOSE) printf("le_time %.0f tl_time %.1f \n",le_time,tl_time); //should be similar
786 
787 
788 
789  //******************** upsample high time threshold sample
790 
791  double th_time = 0;
792 
793  adc_sample_hi--; //move it back one so that thres is between this and the next
794 
795  if (VERBOSE) printf("upsampling sample %i val %i to next val %i \n",adc_sample_hi,adc[adc_sample_hi],adc[adc_sample_hi+1]);
796 
797  upsamplei(adc, adc_sample_hi, iubuf, NUPSAMPLED);
798 
799  for (i=0;i<NUPSAMPLED; i++) if (VERBOSE) printf("iubuf %i\n",iubuf[i]);
800 
801  ups_adjust = iubuf[0] - adc[adc_sample_hi];
802 
803  // move threshold correspondingly instead of correcting upsampled values
804 
805  adc_thres_hi = adc_thres_hi + ups_adjust;
806 
807  // check that threshold crossing lies within the range of iubuf[0 to 5]
808 
809  if (iubuf[NUPSAMPLED-1]<= adc_thres_hi) { //bad upsampling
810  if (VERBOSE) printf("bad upsampling\n");
811  q_code = 16;
812  th_time = 0;
813  return;
814  }
815 
816 
817  // search through upsampled array
818 
819  below_threshold = kFALSE;
820  i = NUPSAMPLED-2;
821 
822  while ((!below_threshold) && (i>=0)) {
823 
824  if (iubuf[i] <= adc_thres_hi) {
825  adc_sample_hi2 = i;
826  below_threshold = kTRUE;
827  }
828 
829  i--;
830  }
831 
832 
833 
834  if (!below_threshold) { //upsampled points did not go below thres
835  if (VERBOSE) printf("upsampled points did not go below threshold - should be impossible\n");
836  th_time = 0;
837  q_code = 19;
838  return;
839  }
840 
841 
842 
843 
844  itime2 = adc_sample_hi2*2; // convert from sample/5 to sample/10
845 
846 
847  th_time = adc_sample_hi*10 + adc_sample_hi2*2;
848 
849  th_time += 2*(adc_thres_hi - iubuf[adc_sample_hi2])/double(iubuf[adc_sample_hi2 + 1] - iubuf[adc_sample_hi2]);
850 
851  if (VERBOSE) printf("th_time %.1f \n",th_time);
852 
853 
854 
855 
856 
857  // calc thresholds again - upsadjust moved them.
858 
859  adc_thres_hi = pedestal + THRES_HIGH;
860  adc_thres_lo = pedestal + THRES_LOW;
861 
862 
863 
864  // if (VERBOSE) printf("TH sample %i value %i LE sample %i value %i pedestal %i\n",adc_sample_hi,adc[adc_sample_hi],adc_sample_lo,le_time,adc[PED]);
865 
866 
867  //interpolate through th_time and le_time to reach pedestal.
868 
869  le_time = th_time - (th_time - tl_time)*THRES_HIGH/(THRES_HIGH-THRES_LOW);
870 
871  if (VERBOSE) printf("new le_time %.1f\n",le_time);
872 
873  q_code = 0;
874  return;
875 
876 
877 
878 }
879 
880 
881 
882 
883 
884 void upsamplei(Int_t x[], Int_t startpos, Int_t z[], const Int_t NUPSAMPLED) {
885 
886  // x is array of samples
887  // z is array of upsampled data
888  // startpos is where to start upsampling in array x, only need to upsample a small region
889 
890 
891  const Int_t nz = NUPSAMPLED;
892 
893  // const Int_t Kscale = 32768;
894  // const Int_t K[43]={-8,-18,-27,-21,10,75,165,249,279,205,-2,-323,-673,-911,-873,-425,482,1773,3247,4618,5591,5943,5591,4618,3247,1773,482,-425,-873,-911,-673,-323,-2,205,279,249,165,75,10,-21,-27,-18,-8}; //32768
895 
896  Int_t k,j,dk;
897 
898 
899  const Int_t Kscale = 16384;
900  const Int_t K[43] = {-4, -9, -13, -10, 5, 37, 82, 124, 139, 102, -1, -161, -336, -455, -436, -212, 241, 886, 1623, 2309, 2795, 2971, 2795, 2309, 1623, 886, 241, -212, -436, -455, -336, -161, -1, 102, 139, 124, 82, 37, 5, -10, -13, -9, -4};
901 
902 
903  //don't need to calculate whole range of k possible
904  //earliest value k=42 corresponds to sample 4.2
905  // k=43 sample 4.4
906  // k=46 sample 5.0
907 
908 
909  // sample 4 (if possible) would be at k=41
910  // sample 4.2 k=42
911  // sample 5 k=46
912 
913  // sample x k=41 + (x-4)*5
914  // sample x-0.2 k=40 + (x-4)*5
915 
916 
917  Int_t firstk = 41 + (startpos-4)*5;
918 
919 
920  for (k=firstk; k<firstk+nz; k++) {
921 
922  dk = k - firstk;
923 
924  z[dk]=0.0;
925 
926  for (j=k%5;j<43;j+=5) {
927 
928  z[dk] += x[(k-j)/5]*K[j];
929 
930  }
931 
932  // printf("dk %i z %i 5z %i 5z/scale %i\n",dk,z[dk],5.0*z[dk],5.0*z[dk]/Kscale);
933 
934  z[dk] = (Int_t)(5*z[dk])/Kscale;
935 
936  }
937 
938 }
939 
void cdc_max(Int_t &, Int_t &, Int_t, Int_t[], Int_t)
Definition: fa125fns.h:135
void cdc_time(Int_t &, Int_t &, Int_t[], Int_t, Int_t, Int_t, Int_t)
Definition: fa125fns.h:172
Double_t x[NCHANNELS]
Definition: st_tw_resols.C:39
void upsamplei(Int_t[], Int_t, Int_t[], Int_t)
Definition: fa125fns.h:884
#define X(str)
Definition: hddm-c.cpp:83
void cdc_time2(Double_t &le_time, Int_t &q_code, Int_t pedestal, Int_t adc[], Int_t NU, Int_t PG, Int_t THRES_HIGH, Int_t THRES_LOW)
Definition: fa125fns.h:457
const bool VERBOSE
void cdc_hit(Int_t &, Int_t &, Int_t &, Int_t[], Int_t, Int_t, Int_t, Int_t, Int_t, Int_t)
Definition: fa125fns.h:55
static TH1I * pedestal[nChan]
printf("string=%s", string)
void cdc_integral(Long_t &, Int_t &, Int_t, Int_t[], Int_t, Int_t)
Definition: fa125fns.h:107