CMS 3D CMS Logo

TMatacq.cc
Go to the documentation of this file.
1 /*
2  * \class TMatacq
3  *
4  * \author: Patrice Verrecchia - CEA/Saclay
5  */
7 
8 #include <iostream>
9 #include <cmath>
10 #include "TVectorD.h"
11 
13 
14 using namespace std;
15 //ClassImp(TMatacq)
16 
17 void TMatacq::init() {
18  for (int k = 0; k < NMAXSAMP; k++)
19  bong[k] = 0.;
20 
21  for (int k = 0; k <= 100; k++)
22  bing[k] = 0;
23 
24  for (int k = 0; k < NSPARAB; k++)
25  t[k] = (double)k;
26 
27  return;
28 }
29 
30 // Constructor...
32  int Ntot, int Nsamp1, int Nsamp2, int cut, int Nbef, int Naft, int niv1, int niv2, int niv3, int nevl, int NSlide) {
33  fNsamples = Ntot;
34  presample = Nsamp1;
35  endsample = Nsamp2;
36  nsigcut = (double)cut;
37  fNum_samp_bef_max = Nbef;
38  fNum_samp_aft_max = Naft;
39  level1 = ((double)niv1) / 100.;
40  level2 = ((double)niv2) / 100.;
41  level3 = ((double)niv3) / 100.;
42  nevlasers = nevl;
43  slidingmean = 0.0;
44  nslide = NSlide;
45  for (int k = 0; k < nevlasers; k++)
46  status[k] = 0;
47  for (int k = 0; k < nevlasers; k++)
48  status[k + nevlasers] = 0;
49 
50  nevmtq0 = 0;
51  nevmtq1 = 0;
52 }
53 
54 // Destructor
56 
57 int TMatacq::rawPulseAnalysis(Int_t Nsamp, Double_t *adc) // GHM
58 {
59  using namespace std;
60 
61  // std::cout << "Entering rawPulseAnalysis" << std::endl;
62 
63  int k, ithr;
64  double dsum = 0., dsum2 = 0.;
65 
66  // std::cout << "calling init" << std::endl;
67  init();
68  // std::cout << ".......done" << std::endl;
69 
70  if (Nsamp != fNsamples) {
71  printf("found different number of samples fNsamples=%d Nsamp=%d\n", fNsamples, Nsamp);
72  return 100;
73  }
74 
75  for (k = 0; k < presample; k++) {
76  dsum += adc[k];
77  dsum2 += adc[k] * adc[k];
78  }
79  bl = dsum / ((double)presample);
80  double ss = (dsum2 / ((double)presample) - bl * bl);
81  if (ss < 0.)
82  ss = 0.;
83  sigbl = sqrt(ss);
84  for (ithr = 0, k = presample; k < endsample; k++) {
85  if (adc[k] > (bl + nsigcut * sigbl) && ithr == 0) {
86  ithr = 1;
87  firstsample = k;
88  }
89  }
90 
91  if (ithr == 0)
92  return 101;
93 
94  for (ithr = 0, k = firstsample; k < Nsamp; k++) {
95  if (adc[k] < (bl + nsigcut * sigbl) && ithr == 0) {
96  ithr = 1;
97  lastsample = k;
98  }
99  }
100  if (ithr == 0)
101  lastsample = Nsamp;
102 
103  if (lastsample > firstsample + NMAXSAMP)
104  lastsample = firstsample + NMAXSAMP;
105 
106  val_max = 0.;
107  samplemax = 0;
108  for (Int_t is = firstsample; is < lastsample; is++) {
109  bong[is - firstsample] = adc[is] - bl;
110  if (bong[is - firstsample] > val_max) {
111  val_max = bong[is - firstsample];
112  samplemax = is;
113  }
114  }
115  if (samplemax == 0)
116  return 103;
117  if (samplemax > lastsample)
118  return 104;
119  if (samplemax < firstsample)
120  return 105;
121 
122  int endslide = samplemax - nslide;
123  int beginslide = nslide;
124  int islidingmean = 0;
125  slidingmean = 0.0;
126 
127  for (int i = beginslide; i < endslide; i++) {
128  slidingmean += adc[i];
129  islidingmean += 1;
130  }
131  if (islidingmean != 0)
132  slidingmean /= double(islidingmean);
133 
134  return 0;
135 }
137  int k;
138  int nbinf = 0;
139  int jfind = 0;
140  int nbsup = 0;
141  double thres = val_max * level1;
142 
143  for (k = 0, jfind = 0; k < NMAXSAMP; k++) {
144  if (jfind == 0) {
145  if (bong[k] > thres) {
146  nbinf = k;
147  jfind = 1;
148  }
149  }
150  }
151  if (jfind == 0)
152  nbinf = 0;
153 
154  for (k = NMAXSAMP, jfind = 0; k > nbinf; k--) {
155  if (jfind == 0) {
156  if (bong[k] > thres) {
157  nbsup = k;
158  jfind = 1;
159  }
160  }
161  }
162  if (nbsup == 0)
163  nbsup = nbinf;
164 
165  pkval = 0.;
166  sigpkval = 0.5;
167  if (nbsup == nbinf) {
168  return 301;
169  } else {
170  if (nbinf > 4)
171  nbinf -= 3;
172  else
173  nbinf = 1;
174  if (nbsup < NMAXSAMP - 4)
175  nbsup += 3;
176  else
177  nbsup = NMAXSAMP;
178 
179  for (k = 0; k < nbinf; k++)
180  bong[k] = 0.;
181  for (k = nbsup; k < NMAXSAMP; k++)
182  bong[k] = 0.;
183 
184  for (k = 0, jfind = 0; k < NMAXSAMP; k++) {
185  if (bong[k] > 0.)
186  jfind++;
187  }
188  if (jfind == 0) {
189  return 302;
190  } else if (jfind == 1) {
191  return 303;
192  } else {
193  for (k = 0; k < NMAXSAMP; k++) {
194  if (k < 100)
195  bing[k + 1] = (int)bong[k];
196  }
197 
198  TMarkov *peak = new TMarkov();
199 
200  peak->peakFinder(&bing[0]);
201  pkval = peak->getPeakValue(0);
202  sigpkval = peak->getPeakValue(1);
203 
204  delete peak;
205 
206  pkval += (firstsample - 1);
207  }
208  }
209 
210  return 0;
211 }
212 
214  int testneg = 0;
215  ampl = 0.;
216  timeatmax = 0.;
217  int heresamplemax = samplemax - firstsample;
218 
219  int beginfit = heresamplemax - fNum_samp_bef_max;
220  int endfit = heresamplemax + fNum_samp_aft_max;
221 
222  int nval = endfit - beginfit + 1;
223  if (nval != NSPARAB)
224  return 201;
225  for (int kn = beginfit; kn <= endfit; kn++) {
226  if (bong[kn] <= 0)
227  testneg = 1;
228  }
229  if (testneg == 1)
230  return 202;
231 
232  for (int i = 0; i < nval; i++) {
233  val[i] = bong[beginfit + i];
234  fv1[i] = 1.;
235  fv2[i] = (double)(i);
236  fv3[i] = ((double)(i)) * ((double)(i));
237  }
238 
239  TVectorD y(nval, val);
240  TVectorD f1(nval, fv1);
241  TVectorD f2(nval, fv2);
242  TVectorD f3(nval, fv3);
243 
244  double bj[3];
245  bj[0] = f1 * y;
246  bj[1] = f2 * y;
247  bj[2] = f3 * y;
248  TVectorD b(3, bj);
249 
250  double aij[9];
251  aij[0] = f1 * f1;
252  aij[1] = f1 * f2;
253  aij[2] = f1 * f3;
254  aij[3] = f2 * f1;
255  aij[4] = f2 * f2;
256  aij[5] = f2 * f3;
257  aij[6] = f3 * f1;
258  aij[7] = f3 * f2;
259  aij[8] = f3 * f3;
260  TMatrixD a(3, 3, aij);
261 
262  double det1;
263  a.InvertFast(&det1);
264 
265  TVectorD c = a * b;
266 
267  double *par = c.GetMatrixArray();
268  if (par[2] != 0.) {
269  timeatmax = -par[1] / (2. * par[2]);
270  ampl = par[0] - par[2] * (timeatmax * timeatmax);
271  }
272 
273  if (ampl <= 0.) {
274  ampl = bong[heresamplemax];
275  return 203;
276  }
277 
278  if ((int)timeatmax > NSPARAB)
279  return 204;
280  if ((int)timeatmax < 0)
281  return 205;
282 
283  timeatmax += (beginfit + firstsample);
284 
285  int tplus[3], tminus[3];
286  double xampl[3];
287  xampl[0] = 0.2 * ampl;
288  xampl[1] = 0.5 * ampl;
289  xampl[2] = 0.8 * ampl;
290 
291  int hitplus[3];
292  int hitminus[3];
293  double width[3];
294 
295  for (int i = 0; i < 3; i++) {
296  hitplus[i] = 0;
297  hitminus[i] = 0;
298  width[i] = 0.0;
299  tplus[i] = firstsample + lastsample;
300  tminus[i] = 0;
301  }
302 
303  // calculate first estimate of half width
304  int im = heresamplemax;
305  int iplusbound = firstsample + lastsample - im;
306 
307  for (int j = 0; j < 3; j++) {
308  for (int i = 1; i < im; i++) {
309  if (bong[im - i] < xampl[j] && bong[im - i + 1] > xampl[j]) {
310  tminus[j] = im - i;
311  hitminus[j]++;
312  i = im;
313  }
314  }
315 
316  for (int i = 0; i < iplusbound; i++) {
317  if (bong[im + i] > xampl[j] && bong[im + i + 1] < xampl[j]) {
318  tplus[j] = im + i;
319  hitplus[j]++;
320  i = iplusbound;
321  }
322  }
323 
324  // interpolate to get a better estimate
325 
326  double slopeplus = double(bong[tplus[j] + 1] - bong[tplus[j]]);
327  double slopeminus = double(bong[tminus[j] + 1] - bong[tminus[j]]);
328 
329  double timeminus = double(tminus[j]) + 0.5;
330  if (slopeminus != 0)
331  timeminus = tminus[j] + (xampl[j] - double(bong[tminus[j]])) / slopeminus;
332 
333  double timeplus = double(tplus[j]) + 0.5;
334  if (slopeplus != 0)
335  timeplus = tplus[j] + (xampl[j] - double(bong[tplus[j]])) / slopeplus;
336 
337  width[j] = timeplus - timeminus;
338  }
339 
340  width20 = width[0];
341  width50 = width[1];
342  width80 = width[2];
343 
344  return 0;
345 }
346 
348  int error;
349  trise = 0.;
350 
351  double t20 = interpolate(ampl * level2);
352  if (t20 < 0.) {
353  error = (int)-t20;
354  return error;
355  }
356  double t80 = interpolate(ampl * level3);
357  if (t80 < 0.) {
358  error = (int)-t80;
359  return error;
360  }
361  trise = t80 - t20;
362 
363  return 0;
364 }
365 
366 double TMatacq::interpolate(double amplx) {
367  double T;
368  int kmax = (int)pkval - firstsample;
369 
370  int bin_low = 0;
371  for (Int_t k = 0; k < kmax; k++)
372  if (0. < bong[k] && bong[k] < amplx) {
373  bin_low = k;
374  }
375  if (bin_low == 0)
376  return -301.;
377  int bin_high = 0;
378  for (Int_t k = kmax; k >= 0; k--)
379  if (bong[k] > amplx) {
380  bin_high = k;
381  }
382  if (bin_high == 0)
383  return -302.;
384  if (bin_high < bin_low)
385  return -303.;
386 
387  if (bin_low == bin_high) {
388  T = (double)bin_high;
389  } else {
390  double slope = (bong[bin_high] - bong[bin_low]) / ((double)(bin_high - bin_low));
391  T = (double)bin_high - (bong[bin_high] - amplx) / slope;
392  }
393 
394  return T;
395 }
396 
397 void TMatacq::enterdata(Int_t anevt) {
398  if (anevt < 2 * nevlasers) {
399  if (anevt < nevlasers) {
400  nevmtq0++;
401  status[nevmtq0 - 1] = anevt;
402  comp_trise[nevmtq0 - 1] = trise;
403  comp_peak[nevmtq0 - 1] = pkval;
404  } else {
405  nevmtq1++;
406  status[nevmtq0 + nevmtq1 - 1] = anevt;
407  comp_trise[nevmtq0 + nevmtq1 - 1] = trise;
408  comp_peak[nevmtq0 + nevmtq1 - 1] = pkval;
409  }
410  } else {
411  if (anevt < 3 * nevlasers) {
412  nevmtq0++;
413  status[nevmtq0 - 1] = anevt;
414  comp_trise[nevmtq0 - 1] = trise;
415  comp_peak[nevmtq0 - 1] = pkval;
416  } else {
417  nevmtq1++;
418  status[nevmtq0 + nevmtq1 - 1] = anevt;
419  comp_trise[nevmtq0 + nevmtq1 - 1] = trise;
420  comp_peak[nevmtq0 + nevmtq1 - 1] = pkval;
421  }
422  }
423 }
424 
425 void TMatacq::printmatacqData(int gRunNumber, int color, int timestart) {
426  FILE *fmatacq;
427  char filename[80];
428  int i;
429  double ss;
430  sprintf(filename, "runMatacq%d.pedestal", gRunNumber);
431  fmatacq = fopen(filename, "w");
432  if (fmatacq == nullptr)
433  printf("Error while opening file : %s\n", filename);
434 
435  double sumtrise = 0.;
436  double sumtrise2 = 0.;
437  int timestop = timestart + 3;
438  double mintrise = 10000.;
439  double maxtrise = 0.;
440  for (i = 0; i < nevmtq0; i++) {
441  if (comp_trise[i] > maxtrise) {
442  maxtrise = comp_trise[i];
443  }
444  if (comp_trise[i] < mintrise) {
445  mintrise = comp_trise[i];
446  }
447  sumtrise += comp_trise[i];
448  sumtrise2 += comp_trise[i] * comp_trise[i];
449  }
450  meantrise = sumtrise / ((double)nevmtq0);
451  ss = (sumtrise2 / ((double)nevmtq0) - meantrise * meantrise);
452  if (ss < 0.)
453  ss = 0.;
454  sigtrise = sqrt(ss);
455  fprintf(
456  fmatacq, "%d %d %d %d %f %f %f %f\n", nevmtq0, color, timestart, timestop, meantrise, sigtrise, mintrise, maxtrise);
457 
458  sumtrise = 0.;
459  sumtrise2 = 0.;
460  mintrise = 10000.;
461  maxtrise = 0.;
462  for (i = nevmtq0; i < nevmtq0 + nevmtq1; i++) {
463  if (comp_trise[i] > maxtrise) {
464  maxtrise = comp_trise[i];
465  }
466  if (comp_trise[i] < mintrise) {
467  mintrise = comp_trise[i];
468  }
469  sumtrise += comp_trise[i];
470  sumtrise2 += comp_trise[i] * comp_trise[i];
471  }
472  meantrise = sumtrise / ((double)nevmtq1);
473  ss = (sumtrise2 / ((double)nevmtq1) - meantrise * meantrise);
474  if (ss < 0.)
475  ss = 0.;
476  sigtrise = sqrt(ss);
477  fprintf(
478  fmatacq, "%d %d %d %d %f %f %f %f\n", nevmtq1, color, timestart, timestop, meantrise, sigtrise, mintrise, maxtrise);
479 
480  int iret = fclose(fmatacq);
481  printf(" Closing file : %d\n", iret);
482 }
483 
484 int TMatacq::countBadPulses(int gRunNumber) {
485  FILE *fmatacq;
486  char filename[80];
487  sprintf(filename, "badevtsMatacq%d.dat", gRunNumber);
488  fmatacq = fopen(filename, "w");
489  if (fmatacq == nullptr)
490  printf("Error while opening file : %s\n", filename);
491 
492  int nevbad = 0;
493  for (Int_t i = 0; i < nevmtq0 + nevmtq1; i++) {
494  if (comp_trise[i] < meantrise - 3. * sigtrise) {
495  nevbad++;
496  fprintf(fmatacq, "%d \n", status[i]);
497  status[i] = -1;
498  }
499  if (comp_trise[i] > meantrise + 3. * sigtrise) {
500  nevbad++;
501  fprintf(fmatacq, "%d \n", status[i]);
502  status[i] = -1;
503  }
504  }
505 
506  int iret = fclose(fmatacq);
507  printf(" Closing file : %d\n", iret);
508  return nevbad;
509 }
510 
511 void TMatacq::printitermatacqData(int gRunNumber, int color, int timestart) {
512  FILE *fmatacq;
513  char filename[80];
514  int i;
515  double ss;
516  sprintf(filename, "runiterMatacq%d.pedestal", gRunNumber);
517  fmatacq = fopen(filename, "w");
518  if (fmatacq == nullptr)
519  printf("Error while opening file : %s\n", filename);
520 
521  int nevmtqgood = 0;
522  double sumtrise = 0.;
523  double sumtrise2 = 0.;
524  int timestop = timestart + 3;
525  double mintrise = 10000.;
526  double maxtrise = 0.;
527  for (i = 0; i < nevmtq0; i++) {
528  if (status[i] >= 0) {
529  nevmtqgood++;
530  if (comp_trise[i] > maxtrise) {
531  maxtrise = comp_trise[i];
532  }
533  if (comp_trise[i] < mintrise) {
534  mintrise = comp_trise[i];
535  }
536  sumtrise += comp_trise[i];
537  sumtrise2 += comp_trise[i] * comp_trise[i];
538  }
539  }
540  meantrise = sumtrise / ((double)nevmtqgood);
541  ss = (sumtrise2 / ((double)nevmtqgood) - meantrise * meantrise);
542  if (ss < 0.)
543  ss = 0.;
544  sigtrise = sqrt(ss);
545  fprintf(fmatacq,
546  "%d %d %d %d %f %f %f %f\n",
547  nevmtqgood,
548  color,
549  timestart,
550  timestop,
551  meantrise,
552  sigtrise,
553  mintrise,
554  maxtrise);
555 
556  nevmtqgood = 0;
557  sumtrise = 0.;
558  sumtrise2 = 0.;
559  mintrise = 10000.;
560  maxtrise = 0.;
561  for (i = nevmtq0; i < nevmtq0 + nevmtq1; i++) {
562  if (status[i] >= 0) {
563  nevmtqgood++;
564  if (comp_trise[i] > maxtrise) {
565  maxtrise = comp_trise[i];
566  }
567  if (comp_trise[i] < mintrise) {
568  mintrise = comp_trise[i];
569  }
570  sumtrise += comp_trise[i];
571  sumtrise2 += comp_trise[i] * comp_trise[i];
572  }
573  }
574  meantrise = sumtrise / ((double)nevmtqgood);
575  ss = (sumtrise2 / ((double)nevmtqgood) - meantrise * meantrise);
576  if (ss < 0.)
577  ss = 0.;
578  sigtrise = sqrt(ss);
579  fprintf(fmatacq,
580  "%d %d %d %d %f %f %f %f\n",
581  nevmtqgood,
582  color,
583  timestart,
584  timestop,
585  meantrise,
586  sigtrise,
587  mintrise,
588  maxtrise);
589 
590  int iret = fclose(fmatacq);
591  printf(" Closing file : %d\n", iret);
592 }
void printitermatacqData(Int_t, Int_t, Int_t)
Definition: TMatacq.cc:511
static const double slope[3]
int init
Definition: HydjetWrapper.h:64
double interpolate(double)
Definition: TMatacq.cc:366
level1
Definition: getInfo.py:10
int compute_trise()
Definition: TMatacq.cc:347
double getPeakValue(int i) const
Definition: TMarkov.h:24
void printmatacqData(Int_t, Int_t, Int_t)
Definition: TMatacq.cc:425
void init()
Definition: TMatacq.cc:17
void enterdata(Int_t)
Definition: TMatacq.cc:397
T sqrt(T t)
Definition: SSEVec.h:19
int doFit()
Definition: TMatacq.cc:213
int rawPulseAnalysis(Int_t, Double_t *)
Definition: TMatacq.cc:57
Definition: TMarkov.h:6
void peakFinder(int *)
Definition: TMarkov.cc:105
int findPeak()
Definition: TMatacq.cc:136
double b
Definition: hdecay.h:118
TMatacq(int, int, int, int, int, int, int, int, int, int, int)
Definition: TMatacq.cc:31
~TMatacq() override
Definition: TMatacq.cc:55
double a
Definition: hdecay.h:119
long double T
int countBadPulses(Int_t)
Definition: TMatacq.cc:484
uint16_t *__restrict__ uint16_t const *__restrict__ adc