-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwaveform.cpp
More file actions
2162 lines (1874 loc) · 75.6 KB
/
waveform.cpp
File metadata and controls
2162 lines (1874 loc) · 75.6 KB
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
// To compile in SHELL:
// "g++ waveformAnalysisPos.cpp waveform.cpp `root-config --cflags --libs`"
// Best data to show fit is DataF_CH0@DT5730S_59483_run_new_1300_2-3.5 with Refl
// PMT conversion factor inside data/miscellaneous
#include <time.h>
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <map>
#include <numeric>
#include <sstream>
#include <vector>
#include "TBox.h"
#include "TCanvas.h"
#include "TF1.h"
#include "TFile.h"
#include "TFitResult.h"
#include "TFitResultPtr.h"
#include "TGraph.h"
#include "TH1.h"
#include "TH2.h"
#include "TLatex.h"
#include "TLegend.h"
#include "TLine.h"
#include "TMath.h"
#include "TMatrixD.h"
#include "TMultiGraph.h"
#include "TPad.h"
#include "TROOT.h"
#include "TStyle.h"
#include "TSystem.h"
#include "TTree.h"
#include "waveformAnalysisPos.hpp"
// Define global constants
constexpr int nMinAnalysedRows{1}; // Minimum rows EXCLUDED
constexpr int nMaxAnalysedRows{60000}; // Maximum rows INCLUDED (60k to use)
// Asymmetric gaussian functions
Double_t asymGaussians(Double_t *x, Double_t *par) {
// par[0] = N
// par[1] = #mu
// par[2] = #sigma_{1}
// par[3] = #sigma_{2}
Double_t xVal = x[0];
Double_t fitVal =
par[0] *
(((xVal < par[1]) * (TMath::Exp(-0.5 * ((xVal - par[1]) / par[2]) *
((xVal - par[1]) / par[2])))) +
((xVal >= par[1]) * (TMath::Exp(-0.5 * ((xVal - par[1]) / par[3]) *
((xVal - par[1]) / par[3])))));
return fitVal;
}
Double_t asym2Gaussians(Double_t *x, Double_t *par) {
// par[0] = N^{1}
// par[1] = #mu^{1}
// par[2] = #sigma^{1}_{1}
// par[3] = #sigma^{1}_{2}
// par[4] = N^{2}
// par[5] = #mu^{2}
// par[6] = #sigma^{2}_{1}
// par[7] = #sigma^{2}_{2}
// par[8] = Background
return asymGaussians(x, &par[0]) + asymGaussians(x, &par[4]) + par[8];
}
Double_t asym2GaussiansConstrained(Double_t *x, Double_t *par) {
// par[0] = N^{1}
// par[1] = #mu^{1}
// par[2] = #sigma^{1}_{1}
// par[3] = #sigma^{1}_{2}
// par[4] = N^{2}
// par[5] = #sigma^{2}_{1}
// par[6] = #sigma^{2}_{2}
// par[7] = Background
return asymGaussians(x, new Double_t[4]{par[0], par[1], par[2], par[3]}) +
asymGaussians(x, new Double_t[4]{par[4], 2 * par[1], par[5], par[6]}) +
par[7];
}
Double_t asym2GaussiansConstrainedSameSigma(Double_t *x, Double_t *par) {
// par[0] = N^{1}
// par[1] = #mu^{1}
// par[2] = #sigma^{1}_{1}
// par[3] = #sigma^{1}_{2}
// par[4] = N^{2}
// par[5] = Background
return asymGaussians(x, new Double_t[4]{par[0], par[1], par[2], par[3]}) +
asymGaussians(x, new Double_t[4]{par[4], 2 * par[1], par[2], par[3]}) +
par[5];
}
// Group functions for fitting PE histo
void fitPEHistoNoExp(TH1F *hPhotoElectrons) {
// Import user defined function asymmetric gaussians for 1 PE
TF1 *fAsymmetric1PE = new TF1("fAsymmetric1PE", asymGaussians, 0.6, 1.9, 4);
fAsymmetric1PE->SetLineColor(kRed);
fAsymmetric1PE->SetLineWidth(4);
fAsymmetric1PE->SetLineStyle(2);
fAsymmetric1PE->SetParNames("N^{1}", "#mu^{1}", "#sigma^{1}_{1}",
"#sigma^{1}_{2}");
fAsymmetric1PE->SetParameter(0, 0.3); // N^{1}
fAsymmetric1PE->SetParameter(1, 1.); // #mu^{1}
fAsymmetric1PE->SetParameter(2, 0.5); // #sigma^{1}_{1}
fAsymmetric1PE->SetParameter(3, 0.5); // #sigma^{1}_{2}
// Import user defined function asymmetric gaussians for 2 PE
TF1 *fAsymmetric2PE = new TF1("fAsymmetric2PE", asymGaussians, 1.9, 2.5, 4);
fAsymmetric2PE->SetLineColor(kOrange + 2);
fAsymmetric2PE->SetLineWidth(4);
fAsymmetric2PE->SetLineStyle(2);
fAsymmetric2PE->SetParNames("N^{2}", "#mu^{2}", "#sigma^{2}_{1}",
"#sigma^{2}_{2}");
fAsymmetric2PE->SetParameter(0, 0.1); // N^{2}
fAsymmetric2PE->SetParameter(1, 2.); // #mu^{2}
fAsymmetric2PE->SetParameter(2, 1); // #sigma^{2}_{1}
fAsymmetric2PE->SetParameter(3, 0.5); // #sigma^{2}_{2}
// Define total function as sum of 1 PE + 2 PE
TF1 *fTotal = new TF1("fTotal", asym2Gaussians, 0.5, 3., 9);
fTotal->SetLineColor(kGreen + 2);
fTotal->SetLineWidth(4);
fTotal->SetLineStyle(2);
// Define parameter array for total functions
Double_t par1[9];
// Normalise hPhotoElectrons->Scale(1.0 / hPhotoElectrons->GetMaximum());
hPhotoElectrons->Scale(1.0 / hPhotoElectrons->GetMaximum());
// Fit PE graphs
// 1PE
hPhotoElectrons->Fit(fAsymmetric1PE, "RN");
fAsymmetric1PE->GetParameters(&par1[0]);
// Print pvalue and reduced chi squared
std::cout << "\n\n**** FIT RESULT 1 PE peak ****\n\nP value "
" = "
<< fAsymmetric1PE->GetProb() << '\n';
std::cout << "Reduced chi squared = "
<< fAsymmetric1PE->GetChisquare() / fAsymmetric1PE->GetNDF()
<< "\n\n";
// 2PE
hPhotoElectrons->Fit(fAsymmetric2PE, "RN");
fAsymmetric2PE->GetParameters(&par1[4]);
// Print pvalue and reduced chi squared
std::cout << "\n\n**** FIT RESULT 2 PE peak ****\n\nP value "
" = "
<< fAsymmetric2PE->GetProb() << '\n';
std::cout << "Reduced chi squared = "
<< fAsymmetric2PE->GetChisquare() / fAsymmetric2PE->GetNDF()
<< "\n\n";
// Total function fit NOT CONSTRAINED
par1[8] = 0.005;
fTotal->SetParameters(par1);
fTotal->SetParNames("N^{1}", "#mu^{1}", "#sigma^{1}_{1}", "#sigma^{1}_{2}",
"N^{2}", "#mu^{2}", "#sigma^{2}_{1}", "#sigma^{2}_{2}",
"Background");
TFitResultPtr fitResult = hPhotoElectrons->Fit(fTotal, "S R+ N");
// Get results
std::cout
<< "\n\n**** FIT RESULT TOTAL NOT CONSTRAINED #mu ****\n\nP value "
" = "
<< fTotal->GetProb() << '\n';
std::cout << "Reduced chi squared = "
<< fTotal->GetChisquare() / fTotal->GetNDF() << "\n\n";
TMatrixD covMatrix = fitResult->GetCorrelationMatrix();
TMatrixD corMatrix = fitResult->GetCovarianceMatrix();
std::cout << "\n*** Print covariance matrix ***\n" << std::endl;
covMatrix.Print();
std::cout << "\n*** Print correlation matrix ***\n" << std::endl;
corMatrix.Print();
// Total function fit CONSTRAINED
// Define total function as sum of 1 PE + 2 PE
TF1 *fTotalConst =
new TF1("fTotalConst", asym2GaussiansConstrained, 0.5, 3., 8);
fTotalConst->SetLineColor(kRed);
fTotalConst->SetLineWidth(4);
fTotalConst->SetLineStyle(2);
fTotalConst->SetParameters(par1[0], par1[1], par1[2], par1[3], par1[4],
par1[6], par1[7], par1[8]);
fTotalConst->SetParNames("N^{1}", "#mu^{1}", "#sigma^{1}_{1}",
"#sigma^{1}_{2}", "N^{2}", "#sigma^{2}_{1}",
"#sigma^{2}_{2}", "Background");
TFitResultPtr fitResultConst = hPhotoElectrons->Fit(fTotalConst, "S R+ ");
// Get results
std::cout
<< "\n\n**** FIT RESULT TOTAL CONSTRAINED #mu ****\n\nP value "
" = "
<< fTotalConst->GetProb() << '\n';
std::cout << "Reduced chi squared = "
<< fTotalConst->GetChisquare() / fTotalConst->GetNDF() << "\n\n";
TMatrixD covMatrixConst = fitResultConst->GetCorrelationMatrix();
TMatrixD corMatrixConst = fitResultConst->GetCovarianceMatrix();
std::cout << "\n*** Print covariance matrix ***\n" << std::endl;
covMatrixConst.Print();
std::cout << "\n*** Print correlation matrix ***\n" << std::endl;
corMatrixConst.Print();
// Total function fit CONSTRAINED SAME SIGMAS
// Define total function as sum of 1 PE + 2 PE
TF1 *fTotalConstSameSigmas = new TF1(
"fTotalConstSameSigmas", asym2GaussiansConstrainedSameSigma, 0.5, 3., 6);
fTotalConstSameSigmas->SetLineColor(kRed);
fTotalConstSameSigmas->SetLineWidth(4);
fTotalConstSameSigmas->SetLineStyle(2);
fTotalConstSameSigmas->SetParameters(par1[0], par1[1], par1[2], par1[3],
par1[4], par1[8]);
fTotalConstSameSigmas->SetParNames("N^{1}", "#mu^{1}", "#sigma^{1}_{1}",
"#sigma^{1}_{2}", "N^{2}", "Background");
TFitResultPtr fitResultConstSameSigma =
hPhotoElectrons->Fit(fTotalConstSameSigmas, "S R+ N");
// Get results
std::cout << "\n\n**** FIT RESULT TOTAL CONSTRAINED SAME SIGMA #mu ****\n\nP "
"value "
" = "
<< fTotalConstSameSigmas->GetProb() << '\n';
std::cout << "Reduced chi squared = "
<< fTotalConstSameSigmas->GetChisquare() /
fTotalConstSameSigmas->GetNDF()
<< "\n\n";
TMatrixD covMatrixConstSameSigma =
fitResultConstSameSigma->GetCorrelationMatrix();
TMatrixD corMatrixConstSameSigma =
fitResultConstSameSigma->GetCovarianceMatrix();
std::cout << "\n*** Print covariance matrix ***\n" << std::endl;
covMatrixConstSameSigma.Print();
std::cout << "\n*** Print correlation matrix ***\n" << std::endl;
corMatrixConstSameSigma.Print();
}
// Asymmetric gaussian functions
Double_t asym2GaussiansExpo(Double_t *x, Double_t *par) {
// par[0] = N^{1}
// par[1] = #mu^{1}
// par[2] = #sigma^{1}_{1}
// par[3] = #sigma^{1}_{2}
// par[4] = N^{2}
// par[5] = #mu^{2}
// par[6] = #sigma^{2}_{1}
// par[7] = #sigma^{2}_{2}
// par[8] = Constant
// par[9] = Slope
// par[10] = Background
return asymGaussians(x, &par[0]) + asymGaussians(x, &par[4]) +
TMath::Exp(par[8] + par[9] * x[0]) + par[10];
}
Double_t asym2GaussiansExpoConstrained(Double_t *x, Double_t *par) {
// par[0] = N^{1}
// par[1] = #mu^{1}
// par[2] = #sigma^{1}_{1}
// par[3] = #sigma^{1}_{2}
// par[4] = N^{2}
// par[5] = #sigma^{2}_{1}
// par[6] = #sigma^{2}_{2}
// par[7] = Constant
// par[8] = Slope
// par[9] = Background
return asymGaussians(x, new Double_t[4]{par[0], par[1], par[2], par[3]}) +
asymGaussians(x, new Double_t[4]{par[4], 2 * par[1], par[5], par[6]}) +
TMath::Exp(par[7] + par[8] * x[0]) + par[9];
}
Double_t asym2GaussiansExpoConstrainedSameSigma(Double_t *x, Double_t *par) {
// par[0] = N^{1}
// par[1] = #mu^{1}
// par[2] = #sigma^{1}_{1}
// par[3] = #sigma^{1}_{2}
// par[4] = N^{2}
// par[5] = Constant
// par[6] = Slope
// par[7] = Background
return asymGaussians(x, new Double_t[4]{par[0], par[1], par[2], par[3]}) +
asymGaussians(x, new Double_t[4]{par[4], 2 * par[1], par[2], par[3]}) +
TMath::Exp(par[5] + par[6] * x[0]) + par[7];
}
// Group functions for fitting PE histo
void fitPEHistoExp(TH1F *hPhotoElectrons) {
// Import user defined function asymmetric gaussians for 1 PE
TF1 *fAsymmetric1PE = new TF1("fAsymmetric1PE", asymGaussians, 0.5, 1.9, 4);
fAsymmetric1PE->SetLineColor(kRed);
fAsymmetric1PE->SetLineWidth(4);
fAsymmetric1PE->SetLineStyle(2);
fAsymmetric1PE->SetParNames("N^{1}", "#mu^{1}", "#sigma^{1}_{1}",
"#sigma^{1}_{2}");
fAsymmetric1PE->SetParameter(0, 0.3); // N^{1}
fAsymmetric1PE->SetParameter(1, 1.); // #mu^{1}
fAsymmetric1PE->SetParameter(2, 0.5); // #sigma^{1}_{1}
fAsymmetric1PE->SetParameter(3, 0.5); // #sigma^{1}_{2}
// Import user defined function asymmetric gaussians for 2 PE
TF1 *fAsymmetric2PE = new TF1("fAsymmetric2PE", asymGaussians, 1.9, 2.6, 4);
fAsymmetric2PE->SetLineColor(kOrange + 2);
fAsymmetric2PE->SetLineWidth(4);
fAsymmetric2PE->SetLineStyle(2);
fAsymmetric2PE->SetParNames("N^{2}", "#mu^{2}", "#sigma^{2}_{1}",
"#sigma^{2}_{2}");
fAsymmetric2PE->SetParameter(0, 0.1); // N^{2}
fAsymmetric2PE->SetParameter(1, 2.2); // #mu^{2}
fAsymmetric2PE->SetParameter(2, 1); // #sigma^{2}_{1}
fAsymmetric2PE->SetParameter(3, 0.5); // #sigma^{2}_{2}
// Define expo function for noise fit
TF1 *fExpo = new TF1("fExpo", "expo", 0.1, 3.);
fExpo->SetLineColor(kGreen + 2);
fExpo->SetLineWidth(4);
fExpo->SetLineStyle(2);
fExpo->SetParameter(0, -0.4); // Constant
fExpo->SetParameter(1, -1.2); // Slope
// Define total function as sum of 1 PE + 2 PE
TF1 *fTotal = new TF1("fTotal", asym2GaussiansExpo, 0.1, 3., 11);
fTotal->SetLineColor(kGreen + 2);
fTotal->SetLineWidth(4);
fTotal->SetLineStyle(2);
// Define parameter array for total functions
Double_t par1[11];
// Normalise hPhotoElectrons->Scale(1.0 / hPhotoElectrons->GetMaximum());
hPhotoElectrons->Scale(1.0 / hPhotoElectrons->GetMaximum());
// Fit PE graphs
// 1PE
hPhotoElectrons->Fit(fAsymmetric1PE, "RN");
fAsymmetric1PE->GetParameters(&par1[0]);
// Print pvalue and reduced chi squared
std::cout << "\n\n**** FIT RESULT 1 PE peak ****\n\nP value "
" = "
<< fAsymmetric1PE->GetProb() << '\n';
std::cout << "Reduced chi squared = "
<< fAsymmetric1PE->GetChisquare() / fAsymmetric1PE->GetNDF()
<< "\n\n";
// 2PE
hPhotoElectrons->Fit(fAsymmetric2PE, "RN");
fAsymmetric2PE->GetParameters(&par1[4]);
// Print pvalue and reduced chi squared
std::cout << "\n\n**** FIT RESULT 2 PE peak ****\n\nP value "
" = "
<< fAsymmetric2PE->GetProb() << '\n';
std::cout << "Reduced chi squared = "
<< fAsymmetric2PE->GetChisquare() / fAsymmetric2PE->GetNDF()
<< "\n\n";
// Exponential noise
hPhotoElectrons->Fit(fExpo, "N", "", 0., 0.2);
fExpo->GetParameters(&par1[8]);
// Print pvalue and reduced chi squared
std::cout << "\n\n**** FIT RESULT EXPO NOISE ****\n\nP value "
" = "
<< fExpo->GetProb() << '\n';
std::cout << "Reduced chi squared = "
<< fExpo->GetChisquare() / fExpo->GetNDF() << "\n\n";
// Total function fit NOT CONSTRAINED
par1[10] = 0.001;
fTotal->SetParameters(par1);
fTotal->SetParNames("N^{1}", "#mu^{1}", "#sigma^{1}_{1}", "#sigma^{1}_{2}",
"N^{2}", "#mu^{2}", "#sigma^{2}_{1}", "#sigma^{2}_{2}",
"Constant", "Slope");
fTotal->SetParName(10, "Background");
TFitResultPtr fitResult = hPhotoElectrons->Fit(fTotal, "S R+");
// Get results
std::cout
<< "\n\n**** FIT RESULT TOTAL NOT CONSTRAINED #mu ****\n\nP value "
" = "
<< fTotal->GetProb() << '\n';
std::cout << "Reduced chi squared = "
<< fTotal->GetChisquare() / fTotal->GetNDF() << "\n\n";
TMatrixD covMatrix = fitResult->GetCorrelationMatrix();
TMatrixD corMatrix = fitResult->GetCovarianceMatrix();
std::cout << "\n*** Print covariance matrix ***\n" << std::endl;
covMatrix.Print();
std::cout << "\n*** Print correlation matrix ***\n" << std::endl;
corMatrix.Print();
// Total function fit CONSTRAINED
// Define total function as sum of 1 PE + 2 PE
TF1 *fTotalConst =
new TF1("fTotalConst", asym2GaussiansExpoConstrained, 0.1, 3., 10);
fTotalConst->SetLineColor(kOrange + 2);
fTotalConst->SetLineWidth(4);
fTotalConst->SetLineStyle(2);
fTotalConst->SetParameters(par1[0], par1[1], par1[2], par1[3], par1[4],
par1[6], par1[7], par1[8], par1[9], par1[10]);
fTotalConst->SetParNames("N^{1}", "#mu^{1}", "#sigma^{1}_{1}",
"#sigma^{1}_{2}", "N^{2}", "#sigma^{2}_{1}",
"#sigma^{2}_{2}", "Constant", "Slope", "Background");
TFitResultPtr fitResultConst = hPhotoElectrons->Fit(fTotalConst, "S R+");
// Get results
std::cout
<< "\n\n**** FIT RESULT TOTAL CONSTRAINED #mu ****\n\nP value "
" = "
<< fTotalConst->GetProb() << '\n';
std::cout << "Reduced chi squared = "
<< fTotalConst->GetChisquare() / fTotalConst->GetNDF() << "\n\n";
TMatrixD covMatrixConst = fitResultConst->GetCorrelationMatrix();
TMatrixD corMatrixConst = fitResultConst->GetCovarianceMatrix();
std::cout << "\n*** Print covariance matrix ***\n" << std::endl;
covMatrixConst.Print();
std::cout << "\n*** Print correlation matrix ***\n" << std::endl;
corMatrixConst.Print();
// Total function fit CONSTRAINED SAME SIGMAS
// Define total function as sum of 1 PE + 2 PE
TF1 *fTotalConstSameSigmas =
new TF1("fTotalConstSameSigmas", asym2GaussiansExpoConstrainedSameSigma,
0.1, 3., 8);
fTotalConstSameSigmas->SetLineColor(kRed);
fTotalConstSameSigmas->SetLineWidth(4);
fTotalConstSameSigmas->SetLineStyle(2);
fTotalConstSameSigmas->SetParameters(par1[0], par1[1], par1[2], par1[3],
par1[4], par1[8], par1[9], par1[10]);
fTotalConstSameSigmas->SetParNames("N^{1}", "#mu^{1}", "#sigma^{1}_{1}",
"#sigma^{1}_{2}", "N^{2}", "Constant",
"Slope", "Background");
TFitResultPtr fitResultConstSameSigma =
hPhotoElectrons->Fit(fTotalConstSameSigmas, "S R+");
// Get results
std::cout << "\n\n**** FIT RESULT TOTAL CONSTRAINED SAME SIGMA #mu ****\n\nP "
"value "
" = "
<< fTotalConstSameSigmas->GetProb() << '\n';
std::cout << "Reduced chi squared = "
<< fTotalConstSameSigmas->GetChisquare() /
fTotalConstSameSigmas->GetNDF()
<< "\n\n";
TMatrixD covMatrixConstSameSigma =
fitResultConstSameSigma->GetCorrelationMatrix();
TMatrixD corMatrixConstSameSigma =
fitResultConstSameSigma->GetCovarianceMatrix();
std::cout << "\n*** Print covariance matrix ***\n" << std::endl;
covMatrixConstSameSigma.Print();
std::cout << "\n*** Print correlation matrix ***\n" << std::endl;
corMatrixConstSameSigma.Print();
}
void setFitStyle() {
gROOT->SetStyle("Plain");
gStyle->SetOptStat(0); // It was 10
gStyle->SetOptFit(0); // It was 1111
gStyle->SetPalette(57);
gStyle->SetOptTitle(1);
gStyle->SetStatY(0.9);
gStyle->SetStatX(0.9);
gStyle->SetStatW(0.2);
gStyle->SetStatH(0.2);
gStyle->SetTitleX(0.5);
gStyle->SetTitleY(0.98);
gStyle->SetTitleAlign(23);
gStyle->SetTitleBorderSize(0);
gStyle->SetTitleXOffset(0.8f);
gStyle->SetTitleYOffset(.7f);
// gStyle->SetLineScalePS(1);
gStyle->SetTitleXSize(0.05);
gStyle->SetTitleYSize(0.05);
// gStyle->SetPadTopMargin(0.02);
// gStyle->SetPadBottomMargin(0.6); // More room for x-axis labels
// gStyle->SetPadTopMargin(-9.);
// gStyle->SetPadRightMargin(-9.);
// gStyle->SetPadBottomMargin(-9.);
// gStyle->SetPadLeftMargin(-9.);
// gStyle->SetTitleW(0.5f);
}
/// Create a binned histogram from the pulse sum graph within a fixed window
TH1F *BinPulseSum(TGraph *gPulseSum, double globalStart, double globalEnd,
int N, const char *name) {
TH1F *h = new TH1F(name, Form("%s; Time [ns]; Counts", name), N, globalStart,
globalEnd);
int nPoints = gPulseSum->GetN();
double *x = gPulseSum->GetX();
double *y = gPulseSum->GetY();
for (int i = 0; i < nPoints; ++i) {
if (x[i] >= globalStart && x[i] <= globalEnd) {
h->Fill(x[i], y[i]); // Fill using binning of h
// optionally: h->AddBinContent(h->FindBin(x[i]), y[i]);
}
}
return h;
}
/// Extract pulse sum graph from file
TGraph *sumPulseAnalysis(const std::string &infileName) {
double const samplePeriod = 2.0; // ns
std::ifstream infile(infileName.c_str());
std::string line;
std::map<int, double> map{};
int row{0}, rowSum{0};
while (std::getline(infile, line)) {
if (rowSum < nMinAnalysedRows) {
++rowSum;
continue;
}
if (rowSum >= nMaxAnalysedRows) break;
std::stringstream ss(line);
std::string item;
std::vector<double> samples;
double timestamp = 0.;
int column = 1;
while (std::getline(ss, item, '\t')) {
if (item.empty()) continue;
if (column == 3)
timestamp = std::stod(item);
else if (column >= 7)
samples.push_back(std::stod(item));
++column;
}
WaveformAnalysisPos wf(samples, timestamp, samplePeriod);
const auto &pulses = wf.getPulses();
for (const auto &p : pulses) {
if (p.peakValue > 13000.) continue;
for (size_t j = 0; j < p.times.size(); ++j) {
map[p.times[j]] += p.values[j];
}
}
++rowSum;
}
// Convert map -> TGraph
std::vector<double> times, values;
for (auto &kv : map) {
times.push_back(kv.first);
values.push_back(kv.second);
}
return new TGraph(times.size(), times.data(), values.data());
}
/// Fit Gaussian to get mean & sigma for a graph
std::pair<double, double> FitTriggerRegion(TGraph *gPulseSum) {
// Find max bin
int maxId = TMath::LocMax(gPulseSum->GetN(), gPulseSum->GetY());
double sigmaId = gPulseSum->GetRMS();
double startPoint = gPulseSum->GetX()[maxId] - 0.5 * sigmaId;
double endPoint = gPulseSum->GetX()[maxId] + 0.5 * sigmaId;
// Fit with Gaussian
TF1 *fGaus = new TF1("fGaus", "gaus", startPoint, endPoint);
fGaus->SetLineColor(kRed);
fGaus->SetLineWidth(2);
fGaus->SetLineStyle(2);
fGaus->SetParameter(0, gPulseSum->GetY()[maxId]); // amplitude
fGaus->SetParameter(1, gPulseSum->GetX()[maxId]); // mean
fGaus->SetParameter(2, sigmaId); // sigma
gPulseSum->Fit(fGaus, "RQ0"); // Quiet, no draw
return {fGaus->GetParameter(1), fGaus->GetParameter(2)}; // mean, sigma
}
// Process three files and combine into final histogram
void ProcessFilesAndCombine(const std::string &fileI, const std::string &fileR,
const std::string &fileIR, int Nbins, TH1F *&hI,
TH1F *&hR, TH1F *&hIR, TH1F *&hFinal) {
auto gI = sumPulseAnalysis(fileI);
auto gR = sumPulseAnalysis(fileR);
auto gIR = sumPulseAnalysis(fileIR);
// Gaussian fits for trigger regions
auto [meanI, sigmaI] = FitTriggerRegion(gI);
auto [meanR, sigmaR] = FitTriggerRegion(gR);
auto [meanIR, sigmaIR] = FitTriggerRegion(gIR);
double startI = 0., endI = 450.;
double startR = meanR - 3. * sigmaR, endR = meanR + 3. * sigmaR;
double startIR = meanIR - 3. * sigmaIR, endIR = meanIR + 3. * sigmaIR;
// Global trigger window
double globalStart = std::min({startI, startR, startIR});
double globalEnd = std::max({endI, endR, endIR});
std::cout << ">>> Trigger windows:" << std::endl;
std::cout << " I : [" << startI << ", " << endI << "]" << std::endl;
std::cout << " R : [" << startR << ", " << endR << "]" << std::endl;
std::cout << " IR : [" << startIR << ", " << endIR << "]" << std::endl;
std::cout << ">>> Global: [" << globalStart << ", " << globalEnd << "]"
<< std::endl;
// Bin graphs in same window
hI = BinPulseSum(gI, globalStart, globalEnd, Nbins, "hI");
hR = BinPulseSum(gR, globalStart, globalEnd, Nbins, "hR");
hIR = BinPulseSum(gIR, globalStart, globalEnd, Nbins, "hIR");
// Subtract & normalize
TH1F *hRcorr = (TH1F *)hR->Clone("hRcorr");
hRcorr->Add(hIR, -1.0);
hFinal = (TH1F *)hRcorr->Clone("hFinal");
hFinal->Divide(hI);
}
// Use:
// std::string fileI =
// "./data/61Degrees/CH0_3PTFE-LED_61_1.3_2-3.5_70_INC_TRANSM.txt";
// std::string fileR =
// "./data/61Degrees/4.10mm/"
// "DataF_CH1@DT5730S_59483_run_61_4.10_TRANSM_REFL.txt";
// std::string fileIR =
// "./data/61Degrees/CH1_3PTFE-LED_61_1.3_2-3.5_70_INC_REFL.txt";
void runAnalysis() {
std::string fileI =
"./data/DATA/"
"DataF_CH0@DT5730S_59483_run_3PTFE-LED_45_1.3_2-3.5_70_INC_TRANSM_"
"SCRAPED.txt";
std::string fileR =
"./data/DATA/DataF_CH1@DT5730S_59483_run_45_2.05_TRANSM_REFL_METAL_1.txt";
std::string fileIR =
"./data/DATA/"
"DataF_CH1@DT5730S_59483_run_3PTFE-LED_45_1.3_2-3.5_70_INC_REFL_SCRAPED."
"txt";
TH1F *hI, *hR, *hIR, *hFinal;
setFitStyle();
ProcessFilesAndCombine(fileI, fileR, fileIR, 10., hI, hR, hIR, hFinal);
// Incident
TCanvas *c1 = new TCanvas("c1", "Incident", 800, 600);
hI->SetLineColor(kRed + 1);
hI->SetLineWidth(3);
hI->SetTitle("Incident Signal");
hI->GetXaxis()->SetTitle("Time [ns]");
hI->GetYaxis()->SetTitle("Counts");
hI->Draw("HIST");
TLegend *leg1 = new TLegend(0.7, 0.7, 0.9, 0.9);
leg1->AddEntry(hI, "Incident Signal", "l");
leg1->Draw();
c1->SaveAs("./plots/dig/incident.pdf");
// Reflected raw
TCanvas *c2 = new TCanvas("c2", "Reflected", 800, 600);
hR->SetLineColor(kBlue + 1);
hR->SetLineWidth(3);
hR->SetTitle("Reflected Signal (Raw)");
hR->GetXaxis()->SetTitle("Time [ns]");
hR->GetYaxis()->SetTitle("Counts");
hR->Draw("HIST");
TLegend *leg2 = new TLegend(0.7, 0.7, 0.9, 0.9);
leg2->AddEntry(hR, "Reflected Signal (Raw)", "l");
leg2->Draw();
c2->SaveAs("./plots/dig/reflected_raw.pdf");
// Incident-reflected
TCanvas *c3 = new TCanvas("c3", "Incident Reflected", 800, 600);
hIR->SetLineColor(kGreen + 2);
hIR->SetLineWidth(3);
hIR->SetTitle("Incident-Reflected Overlap");
hIR->GetXaxis()->SetTitle("Time [ns]");
hIR->GetYaxis()->SetTitle("Counts");
hIR->Draw("HIST");
TLegend *leg3 = new TLegend(0.7, 0.7, 0.9, 0.9);
leg3->AddEntry(hIR, "Incident-Reflected Overlap", "l");
leg3->Draw();
c3->SaveAs("./plots/dig/incident_reflected.pdf");
// Final corrected
TCanvas *c4 = new TCanvas("c4", "Final Corrected", 800, 600);
hFinal->SetLineColor(kMagenta + 2);
hFinal->SetLineWidth(1);
hFinal->SetLineWidth(3);
hFinal->SetTitle("Final Result: (R - IR) / I");
hFinal->GetXaxis()->SetTitle("Time after trigger [ns]");
hFinal->GetYaxis()->SetTitle("Arbirtary units");
hFinal->Draw("HIST");
TLegend *leg4 = new TLegend(0.7, 0.7, 0.9, 0.9);
// First box (red)
double yMin = hFinal->GetMinimum();
double yMax = hFinal->GetMaximum() * 0.5; // 50% of max for visibility
TBox *box1 = new TBox(220., yMin, 300., yMax);
box1->SetFillColor(kOrange + 8);
box1->SetFillStyle(3354); // Hatched style
box1->SetLineColor(kOrange + 8);
box1->SetLineWidth(2);
// Second box (dark blue)
TBox *box2 = new TBox(175., yMin, 248., yMax);
box2->SetFillColor(kBlue + 2); // Dark blue fill
box2->SetFillStyle(3345);
box2->SetLineColor(kBlue + 2);
box2->SetLineWidth(2);
box2->Draw("same");
box1->Draw("same");
// Define lines for the endpoints of the boxes
TLine *line1 = new TLine(220., yMin, 220., yMax); // Left edge of box1
TLine *line2 = new TLine(300., yMin, 300., yMax); // Right edge of box1
TLine *line3 = new TLine(175., yMin, 175., yMax); // Left edge of box2
TLine *line4 = new TLine(248., yMin, 248., yMax); // Right edge of box2
// Style for visibility (optional)
line1->SetLineColor(kOrange + 8);
line2->SetLineColor(kOrange + 8);
line3->SetLineColor(kBlue + 2);
line4->SetLineColor(kBlue + 2);
line1->SetLineWidth(4);
line2->SetLineWidth(4);
line3->SetLineWidth(4);
line4->SetLineWidth(4);
// Draw after boxes
line1->Draw("same");
line2->Draw("same");
line3->Draw("same");
line4->Draw("same");
// Legend
leg4->AddEntry(hFinal, "Corrected ratio", "L");
leg4->AddEntry(box1, "Stability region", "F");
leg4->AddEntry(box2, "Incorrect method", "F");
leg4->Draw();
c4->SaveAs("./plots/dig/final_result.pdf");
}
// This function analyses the waveform by building areaVStime, Noise, PE
// counts and pulseWidth histos
// Use below ()
// (
// AreaConvFactor areaConv = Refl,
// std::string infileName =
// "./data/miscellaneous/"
// "DataF_CH0@DT5730S_59483_run_new_1300_2-3.5.txt",
// std::string rootFileName = "./rootFiles/miscellaneous/wfAnalysis.root")
void waveformAnalysis(
AreaConvFactor areaConv = Refl,
std::string infileName =
"./data/miscellaneous/"
"DataF_CH0@DT5730S_59483_run_new_1300_2-3.5.txt",
std::string rootFileName = "./rootFiles/miscellaneous/wfAnalysis.root") {
// To avoid reloading manually if .so is present
R__LOAD_LIBRARY(waveformAnalysisPos_cpp.so);
// Area conversion factor (current assumption is 1 PE = 11000 ADC*ns)
// For Transm PMT = 4000.
// For Refl PMT = 12500.
auto const areaConvFactor = static_cast<double>(areaConv);
// Variables used later
double const samplePeriod = 2.0; // In [ns]
std::ifstream infile(infileName.c_str());
std::string line;
std::vector<double> colours{1, 3, 4, 5, 6, 7, 8, 9}; // Colour vector
TMultiGraph *mg = new TMultiGraph();
TMultiGraph *mgSuperimposed = new TMultiGraph();
std::vector<TGraph *> graphs{};
std::vector<TGraph *> graphsSuperimposed{};
std::map<int, double> map{};
int row{0};
int rowSum{0};
// Select random generator seed for colours based on current time
srand(time(NULL));
// Creating TFile
TFile *file1 = new TFile(rootFileName.c_str(), "RECREATE");
// Define histograms
TH2F *hAreaVsTime = new TH2F("hAreaVsTime",
"; Time after "
"trigger [ns]; Area [ADC#times ns]",
40, 100., 460., 100, 0., 100000.);
TH1F *hNoise = new TH1F("hNoise", "; ADC Counts; Entries", 30, 2755, 2785);
TH1F *hPhotoElectrons =
new TH1F("hPE", "; Area [PE]; Normalized counts", 90, 0, 6);
TH1F *hWidth =
new TH1F("hWidth", "Width distribution; Width [ns]; Counts", 20, 2, 50);
TH1F *hPETrigger = new TH1F(
"hPETrigger", "Trigger region; Area [PE]; Normalized counts", 150, 0, 6);
TH1F *hPETriggerWV = new TH1F(
"hPETriggerWV",
"Trigger region all waveform; Area [PE]; Normalized counts", 150, -5, 20);
TH1F *hPEPreTrigger =
new TH1F("hPEPreTrigger",
"Pre trigger region; Area [PE]; Normalized counts", 15, 0, 2);
TH1F *hPEPostTrigger1 = new TH1F(
"hPEPostTrigger1", "Post trigger region 1; Area [PE]; Normalized counts",
15, 0., 1.6);
TH1F *hPEPostTrigger2 = new TH1F(
"hPEPostTrigger2", "Post trigger region 2; Area [PE]; Normalized counts",
15, 0, 2.5);
// Plotting parameters
int const nPulseParam{14};
Parameter pulsePar[nPulseParam] = {
{"Width [ns]", 2., 50.},
{"Area [ADC #times ns]", 0., 30000.},
{"Area [PE]", 0, 6},
{"Relative peak time [ns]", 100., 300.},
{"Noise RMS [ADC]", 8034, 8040},
{"Peak amplitude [ADC]", 7900, 15500},
{"Height over width [ADC/ns]", 0., 2000},
{"Peak fraction position", 0., 1.},
{"Area over full time width [ADC]", 0., 2000.},
{"Rise time [ns]", 0., 20.},
{"FWHM [ns]", 0., 20.},
{"90% area time [ns]", 0., 35.},
{"Neg area/overall area", 0., 0.05},
{"Neg counts/overall counts", 0., 1.}};
int const nBins{30};
TH1F *hPulsePar[nPulseParam];
TH2F *h2PulsePar[nPulseParam][nPulseParam];
TGraph *gPulsePar[nPulseParam][nPulseParam];
// Initialisation loop for plotting parameters
// par_i labels the rows and par_j labels the columns
for (int par_i = 0; par_i < nPulseParam; ++par_i) {
hPulsePar[par_i] =
new TH1F(Form("h1PulsePar_%d", par_i),
Form("%s; %s; Counts", pulsePar[par_i].label.c_str(),
pulsePar[par_i].label.c_str()),
nBins, pulsePar[par_i].min, pulsePar[par_i].max);
for (int par_j = 0; par_j < nPulseParam; ++par_j) {
if (par_i > par_j) {
gPulsePar[par_i][par_j] = new TGraph();
gPulsePar[par_i][par_j]->SetTitle(
Form("%s vs %s;%s;%s", pulsePar[par_i].label.c_str(),
pulsePar[par_j].label.c_str(), pulsePar[par_j].label.c_str(),
pulsePar[par_i].label.c_str()));
} else if (par_i < par_j) {
h2PulsePar[par_i][par_j] = new TH2F(
Form("h2PulsePar_%d_%d", par_i, par_j),
Form("%s vs %s;%s;%s", pulsePar[par_i].label.c_str(),
pulsePar[par_j].label.c_str(), pulsePar[par_j].label.c_str(),
pulsePar[par_i].label.c_str()),
nBins, pulsePar[par_j].min, pulsePar[par_j].max, nBins,
pulsePar[par_i].min, pulsePar[par_i].max);
}
}
}
// Loop over rows (waveforms) to extract pulse sum graph
while (std::getline(infile, line)) {
// Control over analysed rows
if (rowSum < nMinAnalysedRows) {
++rowSum;
continue;
}
if (rowSum >= nMaxAnalysedRows) {
break;
}
// Defining loop variables
std::stringstream ss(line);
std::string item;
std::vector<double> samples;
double timestamp = 0.;
int column = 1;
// Loop over columns
while (std::getline(ss, item, '\t')) {
if (item.empty()) {
continue;
}
if (column == 3) {
timestamp = std::stod(item);
} else if (column >= 7) {
samples.push_back(std::stod(item));
}
++column;
}
// Creating WaveformAnalysis object
WaveformAnalysisPos wf(samples, timestamp, samplePeriod);
const auto &pulses = wf.getPulses();
for (size_t i = 0; i < pulses.size(); ++i) {
const auto &p = pulses[i];
// Insert selections on pulses
if (p.peakValue > 13000.) {
continue;
}
// Generate sum of pulses using a STL map. Here the keys of the map
// correspond to the time values of pulses, while values correpond to the
// voltage/ADC counts. Contrary to std::vector<T>, std::map<T1,T2>'s
// operator[] is more secure: when you have an empty map, with no
// specified size, and you access map[X], the map safely creates the
// key-value pair (X,0.0) - in this case I put 0.0 because it is the
// default initialiser for double values
for (int j = 0; j < static_cast<int>(p.times.size()); ++j) {
map[p.times[j]] += p.values[j];
}
}
++rowSum;
}
// Create canvas for summing pulses
TCanvas *cPulseSum = new TCanvas("cPulseSum", "Pulse sum", 1500, 700);
std::vector<double> xValues{};
std::vector<double> yValues{};
xValues.reserve(map.size());
yValues.reserve(map.size());
for (auto const &[key, value] : map) {
xValues.push_back(key);
yValues.push_back(value);
}
// Create graph for summing pulses
TGraph *gPulseSum = new TGraph(map.size(), xValues.data(), yValues.data());
gPulseSum->SetTitle("; Time after trigger [ns]; ADC Counts");
gPulseSum->SetLineColor(kBlue);
gPulseSum->SetLineWidth(3);
gPulseSum->SetMarkerColor(kBlack);
gPulseSum->SetMarkerStyle(20);
gPulseSum->SetMarkerSize(1);
// gPulseSum relevant paramters
auto const maxId{TMath::LocMax(gPulseSum->GetN(), gPulseSum->GetY())};
auto const sigmaId{gPulseSum->GetRMS()};
double const startPoint{gPulseSum->GetX()[maxId] - 0.5 * gPulseSum->GetRMS()};
double const endPoint{gPulseSum->GetX()[maxId] + 0.5 * gPulseSum->GetRMS()};
// Create fit function for summed pulses
TF1 *fGaus = new TF1("fGaus", "gaus", startPoint, endPoint);
fGaus->SetLineColor(kRed);
fGaus->SetLineWidth(4);
fGaus->SetLineStyle(2);
fGaus->SetParameter(0, gPulseSum->GetY()[maxId]); // Amplitude
fGaus->SetParameter(1, gPulseSum->GetX()[maxId]); // Mean
fGaus->SetParameter(2, sigmaId); // Sigma
gPulseSum->Fit(fGaus, "R");
// Save gPulseSum fit relevant parameters
double const meanSum{fGaus->GetParameter(1)};
double const sigmaSum{fGaus->GetParameter(2)};
// Variable for analysis in trigger region in 1 single file
int pulseCounter{};
int pulseCounterTriggerRegion{};
double totTrigArea{};
double numTrigPE{};
double totTrigAreaWF{};
// Below gaussian fit on "Pulse sum" graph is used (antisymmetric 2. sigmas)
double const triggerStart{220.}; // 220. to use
double const triggerEnd{300.}; // 300. to use
// Variable for analysis in pre-trigger region in 1 single file
int pulseCounterPreTriggerRegion{};
double totPreTrigArea{};
double numPreTrigPE{};
double const preTriggerStart{1.}; // 80. to use
double const preTriggerEnd{100.}; // 100. to use
// Variable for analysis in post-trigger region 1 in 1 single file
int pulseCounterPostTriggerRegion1{};
double totPostTrigArea1{};
double numPostTrigPE1{};
double const postTriggerStart1{325.};