CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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  double sumpkval = 1.;
166  pkval = 0.;
167  sigpkval = 0.5;
168  if (nbsup == nbinf) {
169  return 301;
170  } else {
171  if (nbinf > 4)
172  nbinf -= 3;
173  else
174  nbinf = 1;
175  if (nbsup < NMAXSAMP - 4)
176  nbsup += 3;
177  else
178  nbsup = NMAXSAMP;
179 
180  for (k = 0; k < nbinf; k++)
181  bong[k] = 0.;
182  for (k = nbsup; k < NMAXSAMP; k++)
183  bong[k] = 0.;
184 
185  for (k = 0, jfind = 0; k < NMAXSAMP; k++) {
186  if (bong[k] > 0.)
187  jfind++;
188  }
189  if (jfind == 0) {
190  return 302;
191  } else if (jfind == 1) {
192  return 303;
193  } else {
194  for (k = 0; k < NMAXSAMP; k++) {
195  if (k < 100)
196  bing[k + 1] = (int)bong[k];
197  }
198 
199  TMarkov *peak = new TMarkov();
200 
201  peak->peakFinder(&bing[0]);
202  pkval = peak->getPeakValue(0);
203  sigpkval = peak->getPeakValue(1);
204 
205  delete peak;
206 
207  sumpkval = 0.0;
208 
209  if (sumpkval > 1000.)
210  sumpkval = 10.;
211 
212  pkval += (firstsample - 1);
213  }
214  }
215 
216  return 0;
217 }
218 
220  int testneg = 0;
221  ampl = 0.;
222  timeatmax = 0.;
223  int heresamplemax = samplemax - firstsample;
224 
225  int beginfit = heresamplemax - fNum_samp_bef_max;
226  int endfit = heresamplemax + fNum_samp_aft_max;
227 
228  int nval = endfit - beginfit + 1;
229  if (nval != NSPARAB)
230  return 201;
231  for (int kn = beginfit; kn <= endfit; kn++) {
232  if (bong[kn] <= 0)
233  testneg = 1;
234  }
235  if (testneg == 1)
236  return 202;
237 
238  for (int i = 0; i < nval; i++) {
239  val[i] = bong[beginfit + i];
240  fv1[i] = 1.;
241  fv2[i] = (double)(i);
242  fv3[i] = ((double)(i)) * ((double)(i));
243  }
244 
245  TVectorD y(nval, val);
246  TVectorD f1(nval, fv1);
247  TVectorD f2(nval, fv2);
248  TVectorD f3(nval, fv3);
249 
250  double bj[3];
251  bj[0] = f1 * y;
252  bj[1] = f2 * y;
253  bj[2] = f3 * y;
254  TVectorD b(3, bj);
255 
256  double aij[9];
257  aij[0] = f1 * f1;
258  aij[1] = f1 * f2;
259  aij[2] = f1 * f3;
260  aij[3] = f2 * f1;
261  aij[4] = f2 * f2;
262  aij[5] = f2 * f3;
263  aij[6] = f3 * f1;
264  aij[7] = f3 * f2;
265  aij[8] = f3 * f3;
266  TMatrixD a(3, 3, aij);
267 
268  double det1;
269  a.InvertFast(&det1);
270 
271  TVectorD c = a * b;
272 
273  double *par = c.GetMatrixArray();
274  if (par[2] != 0.) {
275  timeatmax = -par[1] / (2. * par[2]);
276  ampl = par[0] - par[2] * (timeatmax * timeatmax);
277  }
278 
279  if (ampl <= 0.) {
280  ampl = bong[heresamplemax];
281  return 203;
282  }
283 
284  if ((int)timeatmax > NSPARAB)
285  return 204;
286  if ((int)timeatmax < 0)
287  return 205;
288 
289  timeatmax += (beginfit + firstsample);
290 
291  int tplus[3], tminus[3];
292  double xampl[3];
293  xampl[0] = 0.2 * ampl;
294  xampl[1] = 0.5 * ampl;
295  xampl[2] = 0.8 * ampl;
296 
297  int hitplus[3];
298  int hitminus[3];
299  double width[3];
300 
301  for (int i = 0; i < 3; i++) {
302  hitplus[i] = 0;
303  hitminus[i] = 0;
304  width[i] = 0.0;
305  tplus[i] = firstsample + lastsample;
306  tminus[i] = 0;
307  }
308 
309  // calculate first estimate of half width
310  int im = heresamplemax;
311  int iplusbound = firstsample + lastsample - im;
312 
313  for (int j = 0; j < 3; j++) {
314  for (int i = 1; i < im; i++) {
315  if (bong[im - i] < xampl[j] && bong[im - i + 1] > xampl[j]) {
316  tminus[j] = im - i;
317  hitminus[j]++;
318  i = im;
319  }
320  }
321 
322  for (int i = 0; i < iplusbound; i++) {
323  if (bong[im + i] > xampl[j] && bong[im + i + 1] < xampl[j]) {
324  tplus[j] = im + i;
325  hitplus[j]++;
326  i = iplusbound;
327  }
328  }
329 
330  // interpolate to get a better estimate
331 
332  double slopeplus = double(bong[tplus[j] + 1] - bong[tplus[j]]);
333  double slopeminus = double(bong[tminus[j] + 1] - bong[tminus[j]]);
334 
335  double timeminus = double(tminus[j]) + 0.5;
336  if (slopeminus != 0)
337  timeminus = tminus[j] + (xampl[j] - double(bong[tminus[j]])) / slopeminus;
338 
339  double timeplus = double(tplus[j]) + 0.5;
340  if (slopeplus != 0)
341  timeplus = tplus[j] + (xampl[j] - double(bong[tplus[j]])) / slopeplus;
342 
343  width[j] = timeplus - timeminus;
344  }
345 
346  width20 = width[0];
347  width50 = width[1];
348  width80 = width[2];
349 
350  return 0;
351 }
352 
354  int error;
355  trise = 0.;
356 
357  double t20 = interpolate(ampl * level2);
358  if (t20 < 0.) {
359  error = (int)-t20;
360  return error;
361  }
362  double t80 = interpolate(ampl * level3);
363  if (t80 < 0.) {
364  error = (int)-t80;
365  return error;
366  }
367  trise = t80 - t20;
368 
369  return 0;
370 }
371 
372 double TMatacq::interpolate(double amplx) {
373  double T;
374  int kmax = (int)pkval - firstsample;
375 
376  int bin_low = 0;
377  for (Int_t k = 0; k < kmax; k++)
378  if (0. < bong[k] && bong[k] < amplx) {
379  bin_low = k;
380  }
381  if (bin_low == 0)
382  return -301.;
383  int bin_high = 0;
384  for (Int_t k = kmax; k >= 0; k--)
385  if (bong[k] > amplx) {
386  bin_high = k;
387  }
388  if (bin_high == 0)
389  return -302.;
390  if (bin_high < bin_low)
391  return -303.;
392 
393  if (bin_low == bin_high) {
394  T = (double)bin_high;
395  } else {
396  double slope = (bong[bin_high] - bong[bin_low]) / ((double)(bin_high - bin_low));
397  T = (double)bin_high - (bong[bin_high] - amplx) / slope;
398  }
399 
400  return T;
401 }
402 
403 void TMatacq::enterdata(Int_t anevt) {
404  if (anevt < 2 * nevlasers) {
405  if (anevt < nevlasers) {
406  nevmtq0++;
407  status[nevmtq0 - 1] = anevt;
408  comp_trise[nevmtq0 - 1] = trise;
409  comp_peak[nevmtq0 - 1] = pkval;
410  } else {
411  nevmtq1++;
412  status[nevmtq0 + nevmtq1 - 1] = anevt;
413  comp_trise[nevmtq0 + nevmtq1 - 1] = trise;
414  comp_peak[nevmtq0 + nevmtq1 - 1] = pkval;
415  }
416  } else {
417  if (anevt < 3 * nevlasers) {
418  nevmtq0++;
419  status[nevmtq0 - 1] = anevt;
420  comp_trise[nevmtq0 - 1] = trise;
421  comp_peak[nevmtq0 - 1] = pkval;
422  } else {
423  nevmtq1++;
424  status[nevmtq0 + nevmtq1 - 1] = anevt;
425  comp_trise[nevmtq0 + nevmtq1 - 1] = trise;
426  comp_peak[nevmtq0 + nevmtq1 - 1] = pkval;
427  }
428  }
429 }
430 
431 void TMatacq::printmatacqData(int gRunNumber, int color, int timestart) {
432  FILE *fmatacq;
433  char filename[80];
434  int i;
435  double ss;
436  sprintf(filename, "runMatacq%d.pedestal", gRunNumber);
437  fmatacq = fopen(filename, "w");
438  if (fmatacq == nullptr)
439  printf("Error while opening file : %s\n", filename);
440 
441  double sumtrise = 0.;
442  double sumtrise2 = 0.;
443  int timestop = timestart + 3;
444  double mintrise = 10000.;
445  double maxtrise = 0.;
446  for (i = 0; i < nevmtq0; i++) {
447  if (comp_trise[i] > maxtrise) {
448  maxtrise = comp_trise[i];
449  }
450  if (comp_trise[i] < mintrise) {
451  mintrise = comp_trise[i];
452  }
453  sumtrise += comp_trise[i];
454  sumtrise2 += comp_trise[i] * comp_trise[i];
455  }
456  meantrise = sumtrise / ((double)nevmtq0);
457  ss = (sumtrise2 / ((double)nevmtq0) - meantrise * meantrise);
458  if (ss < 0.)
459  ss = 0.;
460  sigtrise = sqrt(ss);
461  fprintf(
462  fmatacq, "%d %d %d %d %f %f %f %f\n", nevmtq0, color, timestart, timestop, meantrise, sigtrise, mintrise, maxtrise);
463 
464  sumtrise = 0.;
465  sumtrise2 = 0.;
466  mintrise = 10000.;
467  maxtrise = 0.;
468  for (i = nevmtq0; i < nevmtq0 + nevmtq1; i++) {
469  if (comp_trise[i] > maxtrise) {
470  maxtrise = comp_trise[i];
471  }
472  if (comp_trise[i] < mintrise) {
473  mintrise = comp_trise[i];
474  }
475  sumtrise += comp_trise[i];
476  sumtrise2 += comp_trise[i] * comp_trise[i];
477  }
478  meantrise = sumtrise / ((double)nevmtq1);
479  ss = (sumtrise2 / ((double)nevmtq1) - meantrise * meantrise);
480  if (ss < 0.)
481  ss = 0.;
482  sigtrise = sqrt(ss);
483  fprintf(
484  fmatacq, "%d %d %d %d %f %f %f %f\n", nevmtq1, color, timestart, timestop, meantrise, sigtrise, mintrise, maxtrise);
485 
486  int iret = fclose(fmatacq);
487  printf(" Closing file : %d\n", iret);
488 }
489 
490 int TMatacq::countBadPulses(int gRunNumber) {
491  FILE *fmatacq;
492  char filename[80];
493  sprintf(filename, "badevtsMatacq%d.dat", gRunNumber);
494  fmatacq = fopen(filename, "w");
495  if (fmatacq == nullptr)
496  printf("Error while opening file : %s\n", filename);
497 
498  int nevbad = 0;
499  for (Int_t i = 0; i < nevmtq0 + nevmtq1; i++) {
500  if (comp_trise[i] < meantrise - 3. * sigtrise) {
501  nevbad++;
502  fprintf(fmatacq, "%d \n", status[i]);
503  status[i] = -1;
504  }
505  if (comp_trise[i] > meantrise + 3. * sigtrise) {
506  nevbad++;
507  fprintf(fmatacq, "%d \n", status[i]);
508  status[i] = -1;
509  }
510  }
511 
512  int iret = fclose(fmatacq);
513  printf(" Closing file : %d\n", iret);
514  return nevbad;
515 }
516 
517 void TMatacq::printitermatacqData(int gRunNumber, int color, int timestart) {
518  FILE *fmatacq;
519  char filename[80];
520  int i;
521  double ss;
522  sprintf(filename, "runiterMatacq%d.pedestal", gRunNumber);
523  fmatacq = fopen(filename, "w");
524  if (fmatacq == nullptr)
525  printf("Error while opening file : %s\n", filename);
526 
527  int nevmtqgood = 0;
528  double sumtrise = 0.;
529  double sumtrise2 = 0.;
530  int timestop = timestart + 3;
531  double mintrise = 10000.;
532  double maxtrise = 0.;
533  for (i = 0; i < nevmtq0; i++) {
534  if (status[i] >= 0) {
535  nevmtqgood++;
536  if (comp_trise[i] > maxtrise) {
537  maxtrise = comp_trise[i];
538  }
539  if (comp_trise[i] < mintrise) {
540  mintrise = comp_trise[i];
541  }
542  sumtrise += comp_trise[i];
543  sumtrise2 += comp_trise[i] * comp_trise[i];
544  }
545  }
546  meantrise = sumtrise / ((double)nevmtqgood);
547  ss = (sumtrise2 / ((double)nevmtqgood) - meantrise * meantrise);
548  if (ss < 0.)
549  ss = 0.;
550  sigtrise = sqrt(ss);
551  fprintf(fmatacq,
552  "%d %d %d %d %f %f %f %f\n",
553  nevmtqgood,
554  color,
555  timestart,
556  timestop,
557  meantrise,
558  sigtrise,
559  mintrise,
560  maxtrise);
561 
562  nevmtqgood = 0;
563  sumtrise = 0.;
564  sumtrise2 = 0.;
565  mintrise = 10000.;
566  maxtrise = 0.;
567  for (i = nevmtq0; i < nevmtq0 + nevmtq1; i++) {
568  if (status[i] >= 0) {
569  nevmtqgood++;
570  if (comp_trise[i] > maxtrise) {
571  maxtrise = comp_trise[i];
572  }
573  if (comp_trise[i] < mintrise) {
574  mintrise = comp_trise[i];
575  }
576  sumtrise += comp_trise[i];
577  sumtrise2 += comp_trise[i] * comp_trise[i];
578  }
579  }
580  meantrise = sumtrise / ((double)nevmtqgood);
581  ss = (sumtrise2 / ((double)nevmtqgood) - meantrise * meantrise);
582  if (ss < 0.)
583  ss = 0.;
584  sigtrise = sqrt(ss);
585  fprintf(fmatacq,
586  "%d %d %d %d %f %f %f %f\n",
587  nevmtqgood,
588  color,
589  timestart,
590  timestop,
591  meantrise,
592  sigtrise,
593  mintrise,
594  maxtrise);
595 
596  int iret = fclose(fmatacq);
597  printf(" Closing file : %d\n", iret);
598 }
void printitermatacqData(Int_t, Int_t, Int_t)
Definition: TMatacq.cc:517
double getPeakValue(int i) const
Definition: TMarkov.h:24
const edm::EventSetup & c
static const double slope[3]
int init
Definition: HydjetWrapper.h:64
double interpolate(double)
Definition: TMatacq.cc:372
list status
Definition: mps_update.py:107
int compute_trise()
Definition: TMatacq.cc:353
void printmatacqData(Int_t, Int_t, Int_t)
Definition: TMatacq.cc:431
void init()
Definition: TMatacq.cc:17
void enterdata(Int_t)
Definition: TMatacq.cc:403
T sqrt(T t)
Definition: SSEVec.h:19
int doFit()
Definition: TMatacq.cc:219
printf("params %d %f %f %f\n", minT, eps, errmax, chi2max)
int rawPulseAnalysis(Int_t, Double_t *)
Definition: TMatacq.cc:57
Definition: TMarkov.h:6
void peakFinder(int *)
Definition: TMarkov.cc:105
list level1
Definition: getInfo.py:10
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
tuple filename
Definition: lut2db_cfg.py:20
long double T
int countBadPulses(Int_t)
Definition: TMatacq.cc:490
uint16_t *__restrict__ uint16_t const *__restrict__ adc