-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDigSig.cc
1305 lines (1099 loc) · 31.7 KB
/
DigSig.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "DigSig.h"
#include "RunningStats.h"
#include <TFile.h>
#include <TTree.h>
#include <TF1.h>
#include <TH2.h>
//#include <THnSparse.h>
#include <TSpline.h>
#include <TPad.h>
#include <TMath.h>
#include <TGraphErrors.h>
#include <TSpectrum.h>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <limits>
using namespace std;
/*
DigSig::DigSig()
{
}
*/
DigSig::DigSig(const int chnum, const int nsamp) :
ch(chnum),
nsamples(nsamp)
{
//cout << "In DigSig::DigSig(" << ch << "," << nsamples << ")" << endl;
TString name;
// ch = chnum;
// nsamples = nsamp;
_type = (ch/8)%2; // For MBD, 0=time, 1=charge
name = "hrawpulse"; name += ch;
hRawPulse = new TH1F(name,name,nsamples,-0.5,nsamples-0.5);
name = "hsubpulse"; name += ch;
hSubPulse = new TH1F(name,name,nsamples,-0.5,nsamples-0.5);
//gRawPulse = new TGraphErrors(nsamples);
gRawPulse = new TGraphErrors();
name = "grawpulse"; name += ch;
gRawPulse->SetName(name);
//gSubPulse = new TGraphErrors(nsamples);
gSubPulse = new TGraphErrors();
name = "gsubpulse"; name += ch;
gSubPulse->SetName(name);
hpulse = hRawPulse; // hpulse,gpulse point to raw by default
gpulse = gRawPulse; // we switch to sub for default if ped is applied
ped0stats = new RunningStats();
ped0 = 0.; // ped average
use_ped0 = 0;
name = "hPed0_"; name += ch;
hPed0 = new TH1F(name,name,16384,-0.5,16383.5);
//hPed0 = new TH1F(name,name,1000,1,0); // automatically determine the range
minped0samp = -9999;
maxped0samp = -9999;
minped0x = 0.;
maxped0x = 0.;
ped_presamp = 0;
ped_presamp_nsamps = 0;
_gaussian = nullptr;
h2Template = 0;
h2Residuals = 0;
SetTemplateSize(120,2048,-2,9.9);
// range of good amplitudes for templates
// units are usually in ADC counts
template_min_good_amplitude = 20.;
template_max_good_amplitude = 4080.;
// time shift from fit
f_time_offset = 4.0;
name = "hAmpl"; name += ch;
hAmpl = new TH1F(name,name,17100,-100,17000);
name = "hTime"; name += ch;
hTime = new TH1F(name,name,3100,0,31);
f_ampl = 0;
f_time = 0;
template_fcn = nullptr;
}
void DigSig::SetTemplateSize(const Int_t nptsx, const Int_t nptsy, const Double_t begt, const Double_t endt)
{
template_npointsx = nptsx;
template_npointsy = nptsy;
template_begintime = begt;
template_endtime = endt;
template_y.resize(template_npointsx);
template_yrms.resize(template_npointsx);
Double_t xbinwid = (template_endtime - template_begintime)/(template_npointsx-1);
Double_t ybinwid = (1.1+0.1)/template_npointsy; // yscale... should we vary this?
if ( h2Template != 0 ) delete h2Template;
if ( h2Residuals != 0 ) delete h2Residuals;
TString name = "h2Template"; name += ch;
h2Template = new TH2F(name,name,template_npointsx,template_begintime-xbinwid/2.,template_endtime-xbinwid/2,
template_npointsy,-0.1+ybinwid/2.0,1.1+ybinwid/2.0);
name = "h2Residuals"; name += ch;
h2Residuals = new TH2F(name,name,template_npointsx,template_begintime-xbinwid/2.,template_endtime+xbinwid/2,
80,-20,20);
/*
int nbins[] = { template_npointsx, nbinsy };
Double_t lowrange[] = { template_begintime-xbinwid/2.0, -0.1+ybinwid/2.0 };
Double_t highrange[] = { template_endtime+xbinwid/2.0, 1.1+ybinwid/2.0 };
h2Template = new THnSparseF(name,name,2,nbins,lowrange,highrange);
*/
//h2Template->cd( gDirectory );
}
DigSig::~DigSig()
{
/*
delete hRawPulse;
delete hSubPulse;
delete gRawPulse;
delete gSubPulse;
delete ped0stats;
*/
//h2Template->Write();
}
void DigSig::SetTemplateMinMaxGoodADC(const Double_t min, const Double_t max)
{
template_min_good_amplitude = min;
template_max_good_amplitude = max;
}
void DigSig::SetTemplateMinMaxFitRange(const Double_t min, const Double_t max)
{
template_min_xrange = min;
template_max_xrange = max;
}
// This sets y, and x to sample number (starts at 0)
int DigSig::SetY(const Float_t *y, const int invert)
{
hpulse->Reset();
f_ampl = -9999.;
f_time = -9999.;
for (int isamp=0; isamp<nsamples; isamp++)
{
hRawPulse->SetBinContent( isamp+1, y[isamp] );
gRawPulse->SetPoint( isamp, Double_t(isamp), y[isamp] );
}
// Apply pedestal
if ( use_ped0 != 0 || minped0samp >= 0 || minped0x != maxped0x || ped_presamp!=0 )
{
//cout << "sub" << endl;
if ( minped0samp >= 0 )
{
CalcEventPed0(minped0samp,maxped0samp);
}
else if ( minped0x != maxped0x )
{
CalcEventPed0(minped0x,maxped0x);
}
else if ( ped_presamp != 0 )
{
CalcEventPed0_PreSamp(ped_presamp,ped_presamp_nsamps);
}
for (int isamp=0; isamp<nsamples; isamp++)
{
hSubPulse->SetBinContent( isamp+1, invert*(y[isamp]-ped0) );
hSubPulse->SetBinError( isamp+1, ped0rms );
gSubPulse->SetPoint( isamp, (Double_t)isamp, invert*(y[isamp]-ped0) );
gSubPulse->SetPointError( isamp, 0., ped0rms );
}
}
return 1;
}
int DigSig::SetXY(const Float_t *x, const Float_t *y, const int invert)
{
hRawPulse->Reset();
hSubPulse->Reset();
f_ampl = -9999.;
f_time = -9999.;
_status = 0;
//cout << "nsamples " << nsamples << endl;
//cout << "use_ped0 " << use_ped0 << "\t" << ped0 << endl;
for( int isamp=0; isamp<nsamples; isamp++ )
{
//cout << isamp << "\t" << x[isamp] << "\t" << y[isamp] << endl;
hRawPulse->SetBinContent( isamp+1, y[isamp] );
gRawPulse->SetPoint( isamp, x[isamp], y[isamp] );
}
if ( use_ped0 != 0 || minped0samp >= 0 || minped0x != maxped0x || ped_presamp!=0 )
{
if ( minped0samp >= 0 )
{
CalcEventPed0(minped0samp,maxped0samp);
}
else if ( minped0x != maxped0x )
{
CalcEventPed0(minped0x,maxped0x);
}
else if ( ped_presamp != 0 )
{
if ( _type==0 )
{
CalcEventPed0_PreSamp(3,ped_presamp_nsamps);
}
else
{
CalcEventPed0_PreSamp(ped_presamp,ped_presamp_nsamps);
}
}
for (int isamp=0; isamp<nsamples; isamp++)
{
// How do we handle data which is not in samples, but is in time,
// such as DRS4 data
hSubPulse->SetBinContent( isamp+1, invert*(y[isamp]-ped0) );
hSubPulse->SetBinError( isamp+1, ped0rms );
gSubPulse->SetPoint( isamp, x[isamp], invert*(y[isamp]-ped0) );
gSubPulse->SetPointError( isamp, 0., ped0rms );
//cout << "sub " << x[isamp] << "\t" << invert*(y[isamp]-ped0) << endl;
//cout << "sub2 " << invert << "\t" << y[isamp] << "\t" << ped0 << endl;
}
}
return 1;
}
Double_t DigSig::GetSplineAmpl()
{
TSpline3 s3("s3",gSubPulse);
// First find maximum, to rescale
f_ampl = -999999.;
double step_size = 0.01;
//cout << "step size " << step_size << endl;
for (double ix=0; ix<nsamples; ix += step_size)
{
Double_t val = s3.Eval(ix);
if ( val > f_ampl )
{
f_ampl = val;
}
}
return f_ampl;
}
// This does a straight line fit for now...
/*
Double_t DigSig::FitPulse()
{
const Double_t pedcut[] = {1650,1560};
const Double_t maxcut[] = {12000,12600};
TF1 f("f","pol1",0,31);
f.SetParameter(0,0);
f.SetParameter(1,3000.);
Double_t start = 0;
Double_t stop = 31;
Double_t x, y;
for (int isamp=0; isamp<nsamples; isamp++)
{
gpulse->GetPoint(isamp,x,y);
if ( y>pedcut[ch] )
{
start = x;
break;
}
}
for (int isamp=(int)start; isamp<nsamples; isamp++)
{
gpulse->GetPoint(isamp,x,y);
if ( y>maxcut[ch] )
{
stop = x-1;
break;
}
}
f.SetRange(start,stop);
gpulse->Fit(&f,"R");
Double_t slope = f.GetParameter(1);
//cout << "xxx " << slope << endl;
return slope;
}
*/
void DigSig::FillPed0(const Int_t sampmin, const Int_t sampmax)
{
Double_t x, y;
for (int isamp=sampmin; isamp<=sampmax; isamp++)
{
gRawPulse->GetPoint(isamp,x,y);
//gRawPulse->Print("all");
hPed0->Fill( y );
ped0stats->Push( y );
ped0 = ped0stats->Mean();
ped0rms = ped0stats->RMS();
//cout << "ped0 " << ch << " " << n << "\t" << ped0 << endl;
//cout << "ped0 " << ch << "\t" << ped0 << endl;
}
}
void DigSig::FillPed0(const Double_t begin, const Double_t end)
{
Double_t x, y;
Int_t n = gRawPulse->GetN();
for (int isamp=0; isamp<n; isamp++)
{
gRawPulse->GetPoint(isamp,x,y);
if ( x>=begin && x<=end )
{
hPed0->Fill( y );
ped0stats->Push( y );
ped0 = ped0stats->Mean();
ped0rms = ped0stats->RMS();
//cout << "ped0 " << ch << " " << n << "\t" << x << "\t" << y << endl;
}
// quit if we are past the ped region
if ( x>end ) break;
}
}
void DigSig::FillPed0PreSamp(const Int_t presample, const Int_t nsamps)
{
Double_t x, y;
//Int_t n = gRawPulse->GetN();
//Int_t max = gRawPulse->GetHistogram()->GetMaximumBin();
Long64_t max = TMath::LocMax(gRawPulse->GetN(),gRawPulse->GetY());
Int_t minsamp = max - presample - nsamps + 1;
Int_t maxsamp = max - presample;
//cout << "CalcEventPed0_PreSamp: " << max << endl;
if ( minsamp<0 )
{
minsamp = 0;
_status = 1; // bad pedestal
}
if ( maxsamp<0 )
{
maxsamp = 0;
_status = 1;
}
for (int isamp=minsamp; isamp<=maxsamp; isamp++)
{
gRawPulse->GetPoint(isamp,x,y);
hPed0->Fill(y);
ped0stats->Push( y );
}
}
void DigSig::FitPed0()
{
if ( _gaussian==nullptr )
{
double min = hPed0->GetBinLowEdge(1);
int nbins = hPed0->GetNbinsX();
double max = hPed0->GetBinLowEdge(nbins+1);
TString name = "gaussian"; name += ch;
_gaussian = new TF1(name,"gaus",min,max);
}
Double_t mean = hPed0->GetBinCenter( hPed0->GetMaximumBin() );
Double_t ampl = hPed0->GetBinContent( hPed0->GetMaximumBin() );
const double nominal_sigma = 6.0;
_gaussian->SetParameters(ampl,mean,nominal_sigma);
_gaussian->SetRange(mean-4*nominal_sigma,mean+4*nominal_sigma);
cout << _gaussian->GetName() << "\t" << ampl << "\t" << mean << endl;
hPed0->Fit(_gaussian,"R");
ped0 = _gaussian->GetParameter(1);
ped0rms = _gaussian->GetParameter(2);
}
void DigSig::SetPed0(const Double_t mean, const Double_t rms)
{
ped0 = mean;
ped0rms = rms;
use_ped0 = 1;
hpulse = hSubPulse;
gpulse = gSubPulse;
//cout << "ch " << ch << " Ped = " << ped0 << endl;
}
// Get Event by Event Ped0 if requested
void DigSig::CalcEventPed0(const Int_t minpedsamp, const Int_t maxpedsamp)
{
hPed0->Reset();
ped0stats->Clear();
Double_t x, y;
for (int isamp=minpedsamp; isamp<=maxpedsamp; isamp++)
{
gRawPulse->GetPoint(isamp,x,y);
hPed0->Fill(y);
ped0stats->Push( y );
}
// use straight mean for pedestal
// Could consider using fit to hPed0 to remove outliers
SetPed0( ped0stats->Mean(), ped0stats->RMS() );
//cout << "In CalcEventPed0 " << GetPed0() << endl;
}
// Get Event by Event Ped0 if requested
void DigSig::CalcEventPed0(const Double_t minpedx, const Double_t maxpedx)
{
hPed0->Reset();
ped0stats->Clear();
Double_t x, y;
Int_t n = gRawPulse->GetN();
for (int isamp=0; isamp<n; isamp++)
{
gRawPulse->GetPoint(isamp,x,y);
if ( x>= minpedx && x<= maxpedx)
{
hPed0->Fill(y);
ped0stats->Push( y );
}
}
// use straight mean for pedestal
// Could consider using fit to hPed0 to remove outliers
Double_t mean = ped0stats->Mean();
Double_t rms = ped0stats->RMS();
SetPed0( mean, rms );
//cout << "ped0stats " << mean << "\t" << rms << endl;
}
// Get Event by Event Ped0, num samples before peak
// presample is number of samples before peak, nsamps is how many samples
// before that to add
void DigSig::CalcEventPed0_PreSamp(const int presample, const int nsamps)
{
hPed0->Reset();
ped0stats->Clear();
Double_t x, y;
//Int_t n = gRawPulse->GetN();
//Int_t max = gRawPulse->GetHistogram()->GetMaximumBin();
Long64_t max = TMath::LocMax(gRawPulse->GetN(),gRawPulse->GetY());
Int_t minsamp = max - presample - nsamps + 1;
Int_t maxsamp = max - presample;
//cout << "CalcEventPed0_PreSamp: " << max << endl;
if ( minsamp<0 )
{
minsamp = 0;
_status = 1; // bad pedestal
}
if ( maxsamp<0 )
{
maxsamp = 0;
_status = 1;
}
for (int isamp=minsamp; isamp<=maxsamp; isamp++)
{
gRawPulse->GetPoint(isamp,x,y);
hPed0->Fill(y);
ped0stats->Push( y );
}
// use straight mean for pedestal
// Could consider using fit to hPed0 to remove outliers
Double_t mean = ped0stats->Mean();
Double_t rms = ped0stats->RMS();
SetPed0( mean, rms );
static int counter = 0;
if (counter<10)
{
cout << "CalcEventPed0_PreSamp: ped0stats " << mean << "\t" << rms << endl;
counter++;
}
}
Double_t DigSig::LeadingEdge(const Double_t threshold)
{
// Find first point above threshold
// We also make sure the next point is above threshold
// to get rid of a high fluctuation
int n = gSubPulse->GetN();
Double_t *x = gSubPulse->GetX();
Double_t *y = gSubPulse->GetY();
int sample = -1;
for (int isamp=0; isamp<n; isamp++)
{
if ( y[isamp] > threshold )
{
if ( isamp==n || y[isamp+1] > threshold )
{
sample = isamp;
break;
}
}
}
if ( sample < 1 ) return -9999.; // no signal above threshold
// Linear Interpolation of start time
Double_t dx = x[sample] - x[sample-1];
Double_t dy = y[sample] - y[sample-1];
Double_t dt1 = y[sample] - threshold;
Double_t t0 = x[sample] - dt1*(dx/dy);
return t0;
}
Double_t DigSig::dCFD(const Double_t fraction_threshold)
{
// Find first point above threshold
// We also make sure the next point is above threshold
// to get rid of a high fluctuation
int n = gSubPulse->GetN();
Double_t *x = gSubPulse->GetX();
Double_t *y = gSubPulse->GetY();
// Get max amplitude
Double_t ymax = TMath::MaxElement(n,y);
if ( f_ampl == -9999. ) f_ampl = ymax;
Double_t threshold = fraction_threshold * ymax; // get fraction of amplitude
//cout << "threshold = " << threshold << "\tymax = " << ymax <<endl;
int sample = -1;
for (int isamp=0; isamp<n; isamp++)
{
if ( y[isamp] > threshold )
{
if ( isamp==n || y[isamp+1] > threshold )
{
sample = isamp;
break;
}
}
}
if ( sample < 1 ) return -9999.; // no signal above threshold
// Linear Interpolation of start time
Double_t dx = x[sample] - x[sample-1];
Double_t dy = y[sample] - y[sample-1];
Double_t dt1 = y[sample] - threshold;
Double_t t0 = x[sample] - dt1*(dx/dy);
f_time = t0;
return t0;
}
Double_t DigSig::MBD(const Int_t max_samp)
{
// Get the amplitude of the sample number to get time
Double_t *y = gSubPulse->GetY();
// SHOULD INCLUDE TIME CALIBRATION HERE
f_time = y[max_samp];
// Get max amplitude, and set it if it hasn't already been set
int n = gSubPulse->GetN();
Double_t ymax = TMath::MaxElement(n,y);
if ( f_ampl == -9999. ) f_ampl = ymax;
//if ( y[0] > 25 && ch==0 )
/*
if ( ch==0 )
{
cout << ch << f_time << "\t" << f_ampl << endl;
gSubPulse->Draw("ap");
gPad->SetGridy(1);
gPad->SetGridx(1);
PadUpdate(0);
}
*/
return f_time;
}
Double_t DigSig::Integral(const Double_t xmin, const Double_t xmax)
{
Int_t n = gSubPulse->GetN();
Double_t* x = gSubPulse->GetX();
Double_t* y = gSubPulse->GetY();
f_integral = 0.;
for (int ix=0; ix<n; ix++)
{
if (x[ix]>=xmin && x[ix]<=xmax)
{
// Get dx
Double_t dx = (x[ix+1]-x[ix-1])/2.0;
f_integral += (y[ix]*dx);
}
}
return f_integral;
}
void DigSig::LocMax(Double_t& x_at_max, Double_t& ymax, Double_t xminrange, Double_t xmaxrange)
{
// Find index of maximum peak
Int_t n = gSubPulse->GetN();
Double_t* x = gSubPulse->GetX();
Double_t* y = gSubPulse->GetY();
// if flipped or equal, we search the whole range
if ( xmaxrange <= xminrange )
{
xminrange = -DBL_MAX;
xmaxrange = DBL_MAX;
}
ymax = -DBL_MAX;
for (int i=0; i<n; i++)
{
// Skip if out of range
if ( x[i] < xminrange ) continue;
if ( x[i] > xmaxrange ) break;
if ( y[i] > ymax )
{
ymax = y[i];
x_at_max = x[i];
}
}
}
void DigSig::LocMin(Double_t& x_at_max, Double_t& ymin, Double_t xminrange, Double_t xmaxrange)
{
// Find index of maximum peak
Int_t n = gSubPulse->GetN();
Double_t* x = gSubPulse->GetX();
Double_t* y = gSubPulse->GetY();
// if flipped or equal, we search the whole range
if ( xmaxrange <= xminrange )
{
xminrange = -DBL_MAX;
xmaxrange = DBL_MAX;
}
ymin = DBL_MAX;
for (int i=0; i<n; i++)
{
// Skip if out of range
if ( x[i] < xminrange ) continue;
if ( x[i] > xmaxrange ) break;
if ( y[i] < ymin )
{
ymin = y[i];
x_at_max = x[i];
}
}
// old way of getting locmax
//int locmax = TMath::LocMin(n,y);
}
void DigSig::Print()
{
Double_t x, y;
cout << "CH " << ch << endl;
for (int isamp=0; isamp<nsamples; isamp++)
{
gpulse->GetPoint(isamp,x,y);
cout << isamp << "\t" << x << "\t" << y << endl;
}
}
void DigSig::PadUpdate(const int interact)
{
// Make sure TCanvas is created externally!
gPad->Modified();
gPad->Update();
if ( interact )
{
cout << ch << " ? ";
TString junk;
cin >> junk;
if (junk[0] == 'w' || junk[0] == 's')
{
TString name = "ch"; name += ch; name += ".png";
gPad->SaveAs( name );
}
}
}
Double_t DigSig::TemplateFcn(Double_t *x, Double_t *par)
{
// par[0] is the amplitude (relative to the spline amplitude)
// par[1] is the start time (in sample number)
// x[0] units are in sample number
Double_t xx = x[0]-par[1];
Double_t f = 0.;
int verbose = 0;
//verbose = 100;
// When fit is out of limits of good part of spline, ignore fit
if ( xx<template_begintime || xx>template_endtime )
{
TF1::RejectPoint();
if ( xx < template_begintime )
{
//Double_t x0,y0;
Double_t y0 = template_y[0];
return par[0]*y0;
}
else if ( xx > template_endtime )
{
//Double_t x0,y0;
Double_t y0 = template_y[template_npointsx-1];
return par[0]*y0;
}
}
// Linear Interpolation of template
Double_t x0 = 0.;
Double_t y0 = 0.;
Double_t x1 = 0.;
Double_t y1 = 0.;
// find the index in the vector which is closest to xx
Double_t step = (template_endtime - template_begintime) / (template_npointsx-1);
Double_t index = (xx - template_begintime)/step;
int ilow = TMath::FloorNint( index );
int ihigh = TMath::CeilNint( index );
if ( ilow < 0 || ihigh >= template_npointsx )
{
if ( verbose>0 )
{
cout << "ERROR, ilow ihigh " << ilow << "\t" << ihigh << endl;
cout << " " << xx << " " << x[0] << " " << par[1] << endl;
}
if ( ilow<0 )
{
ilow = 0;
}
else if ( ihigh >= template_npointsx )
{
ihigh = template_npointsx - 1;
}
}
if ( ilow==ihigh )
{
f = par[0]*template_y[ilow];
}
else
{
x0 = template_begintime + ilow*step;
y0 = template_y[ilow];
x1 = template_begintime + ihigh*step;
y1 = template_y[ihigh];
f = par[0]*(y0+((y1-y0)/(x1-x0))*(xx-x0)); // linear interpolation
}
// reject points with very bad rms in shape
if ( template_yrms[ilow]>=1.0 || template_yrms[ihigh]>=1.0 )
{
TF1::RejectPoint();
}
return f;
}
int DigSig::FitTemplate()
{
int verbose = 0;
//verbose = 100; // uncomment to see fits
float dcfd_time = -99999.;
//if ( ch==41 ) cout << "ZZZ " << f_ampl << endl;
if ( f_ampl>25 )
{
dcfd_time = f_time;
//cout << "SETTING dcfd_time " << dcfd_time << endl;
}
if ( verbose>0 ) cout << "Fitting ch " << ch << endl;
/*
Int_t maxadc = -1;
Int_t peak_samp = -1;
for (int isamp=0; isamp<NSAMPLES; isamp++)
{
x[isamp] = (Float_t)isamp;
adcsub[isamp] = adc[isamp] - ped;
adcerr[isamp] = pedrms;
if ( adcsub[isamp]>maxadc )
{
maxadc = adcsub[isamp];
peak_samp = isamp;
}
}
TGraphErrors gSubpulse(NSAMPLES,x,adcsub,0,adcerr);
if ( verbose>10 )
{
gSubPulse->SetMarkerStyle(20);
gSubPulse->SetMarkerColor(2);
gSubPulse->SetLineColor(2);
}
fit_shape = fit_pshape[ch];
fit_sherr = fit_psherr[ch];
//if ( verbose>10 ) cout << "CH is " << ch << "\t" << fit_shape << endl;
*/
// Check if channel is empty
if ( gSubPulse->GetN() == 0 )
{
f_ampl = -9999.;
f_time = -9999.;
//cout << "gSubPulse empty" << endl;
return 1;
}
// Get x-position of maximum
Double_t x_at_max, ymax;
LocMax(x_at_max, ymax);
template_fcn->SetParameters(ymax, x_at_max-2);
//template_fcn->SetParLimits(1,-5.,4.);
//template_fcn->SetRange(template_min_xrange,template_max_xrange);
if ( verbose==0 ) gSubPulse->Fit(template_fcn,"RNQ");
else gSubPulse->Fit(template_fcn,"R");
// Get fit parameters
f_ampl = template_fcn->GetParameter(0);
f_time = template_fcn->GetParameter(1);
//if ( ch==41 && fabs(f_ampl) > 0. )
if ( verbose>0 && fabs(f_ampl) > 0. )
{
cout << "FitTemplate " << f_ampl << "\t" << f_time << "\t" << dcfd_time << endl;
gSubPulse->Draw("ap");
template_fcn->SetLineColor(4);
template_fcn->Draw("same");
PadUpdate(0);
if ( dcfd_time>-1000 && fabs(dcfd_time - f_time)>0.1 )
{
std::cout << "dcfd " << dcfd_time << endl;
string junk;
cout << "? ";
cin >> junk;
}
}
/*
Double_t chi2ndf = template_fcn->GetChisquare()/template_fcn->GetNDF();
// Store fit values
amp = static_cast<Float_t>( template_fcn->GetParameter(0) );
fquality = (Short_t)chi2ndf; // Need to define this still
// For the tdc, we choose 360 tdc ticks per sample
Float_t samp_number = 7.0+template_fcn->GetParameter(1);
if ( samp_number<0. )
{
samp_number = 0.;
amp = 0; // for now, if the time is bad, we zero out the channel
}
else if ( samp_number>18.0 )
{
samp_number = 18.0;
amp = 0;
}
tdc = static_cast<Short_t>( samp_number*360. );
*/
return 1;
}
int DigSig::FillSplineTemplate()
{
int verbose = 0;
//verbose = 100;
Double_t max = TMath::MaxElement(gSubPulse->GetN(),gSubPulse->GetY());
// skip if the waveform is marked as bad
if ( _status != 0 ) return 0;
if ( max < template_min_good_amplitude ) return 0;
if ( verbose ) gSubPulse->Draw("ap");
TSpline3 s3("s3",gSubPulse);
// First find maximum, to rescale
f_ampl = -999999.;
double step_size = h2Template->GetXaxis()->GetBinWidth(1);
//cout << "step size " << step_size << endl;
for (double ix=0; ix<nsamples; ix += step_size)
{
Double_t val = s3.Eval(ix);
if ( val > f_ampl )
{
f_ampl = val;
}
}
//cout << f_ampl << endl;
if ( f_ampl<template_min_good_amplitude || f_ampl>template_max_good_amplitude ) return 0;
if ( verbose>0 )
{
//cout << ch << endl;
s3.SetLineColor(2);
s3.Draw("same");
gSubPulse->Draw("p");
PadUpdate();
}
// Now go back to find the time by finding x at midpoint of rise
for (double ix=0; ix<nsamples; ix += step_size)
{
Double_t val = s3.Eval(ix);
if ( val > 0.5*f_ampl )
{
// interpolate midpoint
Double_t dy_mid = val - 0.5*f_ampl;
Double_t dy_prev = val - s3.Eval(ix-step_size);
Double_t dx_prev = step_size;
f_time = ix - (dy_mid/dy_prev)*dx_prev;
break;
}
}
// correct the pulse back
f_time -= f_time_offset;
// Get Time and Max of spline to rescale
//cout << f_ampl << "\t" << time << endl;
hAmpl->Fill( f_ampl );
hTime->Fill( f_time );
//cout << "nsamples " << nsamples << endl;
for (int isamp=0; isamp<nsamples; isamp++)
{
Double_t x, y;
gSubPulse->GetPoint(isamp,x,y);
Double_t fillvalues[2] = {0.};
fillvalues[0] = x - f_time; //corr_time
if ( f_ampl != 0. )
{
fillvalues[1] = y/f_ampl; //scaled_ampl
}