forked from ANNIEsoft/ToolAnalysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
offsetFit_MultipleLAPPD.cpp
1272 lines (1182 loc) · 63.5 KB
/
offsetFit_MultipleLAPPD.cpp
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
// This is a script for offset fitting, use it on the output LAPPDTree.root
// This script require all LAPPD events appear in sequence, and all CTC events appear in sequence
/* procedure:
1. load LAPPDTree.root (TODO: or use LAPPDTree_runNumber.root)
2. load TimeStamp tree, get run number and part file number
3. for each unique run number and part file number, do fit:
4. in this file, for each LAPPD ID:
5. loop TimeStamp tree
load data timestamp
load data beamgate
load PPS for board 0 and board 1
6. loop Trig (or GTrig) tree
load CTC PPS (word = 32)
load target trigger word
7. Find the number of resets in LAPPD PPS:
There must be at least one event in each reset.
Based on the event index order, only fit before the reset which doesn't have data event.
Save the data event and PPS index order.
8. Use the target trigger word, fit the offset
9. save the offset for this LAPPD ID, run number, part file number, index, reset number.
10. Print info to txt file
Laser trigger word: 47
Undelayed beam trigger word: 14
root -l -q 'offsetFit_MultipleLAPPD.cpp("LAPPDTree.root", 14, 1, 10, 0)'
root -l -q 'offsetFit_MultipleLAPPD.cpp("LAPPDTree.root", 47, 0, 10, 0)'
*/
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <cmath>
#include <algorithm>
#include "TFile.h"
#include "TTree.h"
#include "TH2D.h"
#include "TH1D.h"
#include "TString.h"
vector<vector<ULong64_t>> fitInThisReset(
const std::vector<ULong64_t> &LAPPDDataTimeStampUL,
const std::vector<ULong64_t> &LAPPDDataBeamgateUL,
const std::vector<ULong64_t> &LAPPD_PPS,
const int fitTargetTriggerWord,
const std::vector<ULong64_t> &CTCTrigger,
const std::vector<ULong64_t> &CTCPPS,
const ULong64_t PPSDeltaT)
{
cout << "***************************************" << endl;
cout << "Fitting in this reset with:" << endl;
cout << "LAPPDDataTimeStampUL size: " << LAPPDDataTimeStampUL.size() << endl;
cout << "LAPPDDataBeamgateUL size: " << LAPPDDataBeamgateUL.size() << endl;
cout << "LAPPD_PPS size: " << LAPPD_PPS.size() << endl;
cout << "CTCTrigger size: " << CTCTrigger.size() << endl;
cout << "CTCPPS size: " << CTCPPS.size() << endl;
cout << "PPSDeltaT: " << PPSDeltaT << endl; // in ps
// for this input PPS, fit an offset
// return the offset and other information in order
// precedure:
// 1. check drift
// 2. shift the timestmap and beamgate based on drift
// 3. fit the offset
std::vector<double> PPSInterval_ACDC;
for (int i = 1; i < LAPPD_PPS.size(); i++)
{
double diff = static_cast<double>(LAPPD_PPS[i] - LAPPD_PPS[i - 1]);
cout<<fixed<<"PPSDiff = "<<diff<<endl;
// cout << "LAPPD_PPS[" << i << "]: " << LAPPD_PPS[i] / 1E9 / 1000 << ", diff is " << diff / 1E9 / 1000 << endl;
// cout << "diff: " << diff/1E9 << ", 0.9*deltaT = " << 0.9 * PPSDeltaT/1E9 << ", 1.1*deltaT = " << 1.1 * PPSDeltaT/1E9 << endl;
// only save the time interval that is close to the deltaT, to avoid the clock tick level miss and the whole PPS pulse was missed.
if (diff > 0.9 * PPSDeltaT && diff < 1.1 * PPSDeltaT)
{
PPSInterval_ACDC.push_back((PPSDeltaT - diff) / 1000 / 1000);
cout << "push back interval in us = " << (PPSDeltaT - diff) / 1000 / 1000 << ", 0.9*PPSdeltaT = " << 0.9 * PPSDeltaT / 1E9 << ", 1.1*PPSdeltaT = " << 1.1 * PPSDeltaT / 1E9 << endl;
}
else
{
cout << "0.9*PPSdeltaT = " << 0.9 * PPSDeltaT / 1E9 << ", 1.1*PPSdeltaT = " << 1.1 * PPSDeltaT / 1E9 << ", diff = " << (PPSDeltaT - diff) / 1000 / 1000 << endl;
}
//cout << "Diff of Interval to deltaT in second is " << (PPSDeltaT - diff) / 1000 / 1E9 << endl;
}
int totalPPS = LAPPD_PPS.size();
int driftedPPS = 0;
TH1D *h = new TH1D("h", "h", 1000, 0, 1E3);
for (int i = 0; i < PPSInterval_ACDC.size(); i++)
{
// only fill the drift histogram if there is a drift > 2 microseconds
cout<<"PPSInterval_ACDC["<<i<<"] = "<<PPSInterval_ACDC[i]<<endl;
if (PPSInterval_ACDC[i] > 2)
{
h->Fill(PPSInterval_ACDC[i]); // fill in microseconds
cout << "Fill histogram: PPSInterval_ACDC[" << i << "]: " << PPSInterval_ACDC[i] << endl;
}
}
TF1 *gausf = new TF1("gausf", "gaus", 0, 1E3);
h->Fit(gausf, "Q"); // Q for quiet mode
ULong64_t drift = static_cast<ULong64_t>(gausf->GetParameter(1) * 1E3 * 1E3); // drift in ps
ULong64_t trueInterval = PPSDeltaT - drift;
std::cout << "Gaussian Drift in ps is " << drift << std::endl;
std::cout << "True PPS interval in ps is " << trueInterval << std::endl;
delete gausf;
delete h;
// initialize variables need for fitting
int orphanCount = 0; // count the number that how many data event timestamp doesn't matched to a target trigger within an interval.
std::map<double, std::vector<vector<int>>> DerivationMap;
cout << "LAPPD_PPS.size() " << LAPPD_PPS.size() << " CTCPPS.size() " << CTCPPS.size() << endl;
for (int i = 0; i < LAPPD_PPS.size(); i++)
{
if (i > 5)
{
if (i % static_cast<int>(LAPPD_PPS.size() / 5) == 0)
cout << "Fitting PPS " << i << " of " << LAPPD_PPS.size() << endl;
}
ULong64_t LAPPD_PPS_ns = LAPPD_PPS.at(i) / 1000;
ULong64_t LAPPD_PPS_truncated_ps = LAPPD_PPS.at(i) % 1000;
for (int j = 0; j < CTCPPS.size(); j++)
{
vector<double> diffSum;
ULong64_t offsetNow_ns = 0;
if (drift == 0)
{
offsetNow_ns = CTCPPS.at(j) - LAPPD_PPS_ns;
}
else
{
double LAPPD_PPS_ns_dd = static_cast<double>(LAPPD_PPS_ns);
double driftScaling = LAPPD_PPS_ns_dd / (trueInterval / 1000);
ULong64_t totalDriftedClock = static_cast<ULong64_t>(drift * driftScaling / 1000);
offsetNow_ns = CTCPPS.at(j) - (LAPPD_PPS_ns + totalDriftedClock);
if (i < 3 && j < 3)
cout<<"totalDriftedClock in ns = "<<driftScaling <<" number of interval * "<<drift/1000 <<" ns drift per interval = "<<totalDriftedClock<<" ns."<<endl;
}
if (i < 3 && j < 3)
cout << "In ns: CTCPPS.at(" << j << "): " << CTCPPS.at(j) << ", LAPPD_PPS.at(" << i << "): " << LAPPD_PPS.at(i) << ", use LAPPD_PPS_ns: " << LAPPD_PPS_ns << ", offsetNow_ns: " << offsetNow_ns << endl;
// now, we have offset in ns.
// We do fit in ns level, then save the previse information in ps level.
orphanCount = 0;
vector<int> notOrphanIndex;
vector<int> ctcPairedIndex;
vector<int> ctcOrphanPairedIndex;
for (int lappdb = 0; lappdb < LAPPDDataBeamgateUL.size(); lappdb++)
{
ULong64_t TS_ns = LAPPDDataTimeStampUL.at(lappdb) / 1000;
ULong64_t TS_truncated_ps = LAPPDDataTimeStampUL.at(lappdb) % 1000;
// if fit for the undelayed beam trigger, use BG
if (fitTargetTriggerWord == 14)
{
TS_ns = LAPPDDataBeamgateUL.at(lappdb) / 1000;
TS_truncated_ps = LAPPDDataBeamgateUL.at(lappdb) % 1000;
}
ULong64_t driftCorrectionForTS = 0;
if (drift != 0)
{
double TS_ns_dd = static_cast<double>(TS_ns);
double driftScaling = TS_ns_dd / (trueInterval / 1000);
driftCorrectionForTS = static_cast<ULong64_t>(drift * driftScaling / 1000);
if(lappdb<3 && i<3 && j<3)
cout<<i<<j<<lappdb<<": driftCorrectionForTS = "<<driftScaling <<" number of interval * "<<drift/1000 <<" ns drift per interval = "<<driftCorrectionForTS<<" ns."<<endl;
}
// this is the TS we use for matching
ULong64_t DriftCorrectedTS_ns = TS_ns + offsetNow_ns + driftCorrectionForTS;
// initialise other matching variables
ULong64_t minMatchDiff = std::numeric_limits<ULong64_t>::max();
bool useThisValue = true;
int minPairIndex = 0;
string reason = "none";
Long64_t first_derivation = 0;
int minIndex = 0;
double useValue = true;
// find the best match of target trigger to this TS, in ns level.
for (int ctcb = 0; ctcb < CTCTrigger.size(); ctcb++)
{
ULong64_t CTCTrigger_ns = CTCTrigger.at(ctcb);
Long64_t diff = CTCTrigger_ns - DriftCorrectedTS_ns;
if (diff < 0)
diff = -diff;
if (diff < minMatchDiff)
{
minMatchDiff = diff;
minPairIndex = ctcb;
}
Long64_t LastBound = diff - minMatchDiff;
if (LastBound < 0)
LastBound = -LastBound;
Long64_t FirstBound = diff - first_derivation;
if (FirstBound < 0)
FirstBound = -FirstBound;
// save the dt for the first matching to determin the matching position in range.
if (ctcb == 0)
first_derivation = diff;
if (ctcb == CTCTrigger.size() - 1 && LastBound / 1E9 < 0.01)
{
useValue = false;
reason = "When matching the last TS, this diff is too close to (or even is) the minMatchDiff at index " + std::to_string(minPairIndex) + " in " + std::to_string(CTCTrigger.size()) + ". All LAPPD TS is out of the end of CTC trigger range";
}
if (ctcb == CTCTrigger.size() - 1 && FirstBound / 1E9 < 0.01)
{
useValue = false;
reason = "When matching the last TS, this diff is too close to (or even is) the first_derivation at index " + std::to_string(minPairIndex) + " in " + std::to_string(CTCTrigger.size()) + ". All LAPPD TS is out of the beginning of CTC trigger range";
}
}
if (useValue)
{
diffSum.push_back(minMatchDiff);
double minAllowedDiff = 0;
double maxAllowedDiff = 100E3;
if (fitTargetTriggerWord == 14)
{
minAllowedDiff = 322E3;
maxAllowedDiff = 326E3;
}
if (minMatchDiff > maxAllowedDiff || minMatchDiff < minAllowedDiff)
{
// TODO: adjust the limit for laser.
orphanCount += 1;
ctcOrphanPairedIndex.push_back(minPairIndex);
}
else
{
notOrphanIndex.push_back(lappdb);
ctcPairedIndex.push_back(minPairIndex);
}
}
else
{
orphanCount += 1;
}
}
double mean_dev = 0;
for (int k = 0; k < diffSum.size(); k++)
{
mean_dev += diffSum[k];
}
if (diffSum.size() > 0)
mean_dev = mean_dev / diffSum.size();
double mean_dev_noOrphan = 0;
for (int k = 0; k < notOrphanIndex.size(); k++)
{
if (fitTargetTriggerWord == 14)
{
if (diffSum.at(k) > 322E3 && diffSum.at(k) < 326E3)
mean_dev_noOrphan += diffSum[notOrphanIndex[k]];
}
else
{
mean_dev_noOrphan += diffSum[notOrphanIndex[k]];
}
}
if ((diffSum.size() - orphanCount) != 0)
{
mean_dev_noOrphan = mean_dev_noOrphan / (diffSum.size() - orphanCount);
}
else
{
mean_dev_noOrphan = -1; // if all timestamps are out of the range, set it to -1
}
bool increMean_dev = false;
int maxAttempts = 1000;
int attemptCount = 0;
if (mean_dev > 0)
{
int increament_dev = 0;
while (true)
{
// TODO
auto iter = DerivationMap.find(mean_dev);
if (iter == DerivationMap.end() || iter->second.empty())
{
vector<int> Info = {i, j, orphanCount, static_cast<int>(mean_dev_noOrphan * 1000), increament_dev};
DerivationMap[mean_dev].push_back(Info);
DerivationMap[mean_dev].push_back(notOrphanIndex);
DerivationMap[mean_dev].push_back(ctcPairedIndex);
DerivationMap[mean_dev].push_back(ctcOrphanPairedIndex);
break;
}
else
{
increament_dev += 1;
mean_dev += 0.001; // if the mean_dev is already in the map, increase it by 1ps
attemptCount += 1;
if (attemptCount > maxAttempts)
break;
}
}
}
}
}
// finish matching, found the minimum mean_dev in the map, extract the matching information
double min_mean_dev = std::numeric_limits<double>::max();
int final_i = 0;
int final_j = 0;
int gotOrphanCount = 0;
double gotMin_mean_dev_noOrphan = 0;
double increament_times = 0;
vector<int> final_notOrphanIndex;
vector<int> final_ctcPairedIndex;
vector<int> final_ctcOrphanIndex;
for (const auto &minIter : DerivationMap)
{
if (minIter.first > 10 && minIter.first < min_mean_dev)
{
min_mean_dev = minIter.first;
final_i = minIter.second[0][0];
final_j = minIter.second[0][1];
gotOrphanCount = minIter.second[0][2];
gotMin_mean_dev_noOrphan = static_cast<int>(minIter.second[0][3] / 1000);
increament_times = minIter.second[0][4];
final_notOrphanIndex = minIter.second[1];
final_ctcPairedIndex = minIter.second[2];
final_ctcOrphanIndex = minIter.second[3];
}
}
ULong64_t final_offset_ns = 0;
ULong64_t final_offset_ps_negative = 0;
if (drift == 0)
{
final_offset_ns = CTCPPS.at(final_j) - (LAPPD_PPS.at(final_i) / 1000);
final_offset_ps_negative = LAPPD_PPS.at(final_i) % 1000;
}
else
{
ULong64_t LAPPD_PPS_ns = LAPPD_PPS.at(final_i) / 1000;
ULong64_t LAPPD_PPS_truncated_ps = LAPPD_PPS.at(final_i) % 1000;
double driftScaling = static_cast<double>(LAPPD_PPS_ns) / (trueInterval / 1000); // this is the same drift scaling as in the matching loop
ULong64_t totalDriftedClock = static_cast<ULong64_t>(drift * driftScaling / 1000);
cout<<fixed<<"Found CTCPPS as "<<CTCPPS.at(final_j)<<", LAPPD_PPS_ns = "<<LAPPD_PPS_ns;
final_offset_ns = CTCPPS.at(final_j) - (LAPPD_PPS_ns + totalDriftedClock);
final_offset_ps_negative = LAPPD_PPS_truncated_ps; // this is useless if there is a drift though.
cout<<" final_offset_ns = "<<final_offset_ns<<", driftScaling = "<<driftScaling<<", totalDriftedClock_ns = "<<totalDriftedClock<<endl;
}
/*
cout << "******* Fit Finished *******" << endl;
cout << "*** Final offset in is " << final_offset_ns << " ns minus " << final_offset_ps_negative << " ps" << endl;
cout << "*** Final orphan count is " << gotOrphanCount << endl;
cout << "*** Final mean_dev_noOrphan is " << gotMin_mean_dev_noOrphan << " ps" << endl;
cout << "*** Final increament times in this result is " << increament_times << endl;
cout << "*** Final mean deviation is " << min_mean_dev << " ps" << endl;
cout << "*** Final PPS index is " << final_i << ", in total of " << LAPPD_PPS.size() << endl;
cout << "*** Final CTC PPS index is " << final_j << ", in total of " << CTCPPS.size() << endl;
cout << "***************************" << endl;
cout << "******* Saving *************" << endl;
*/
cout << "\033[1;34m******* Fit Finished *******\033[0m" << endl;
cout << "\033[1;34m*** Final offset in is \033[1;31m" << final_offset_ns << "\033[1;34m ns minus \033[1;31m" << final_offset_ps_negative << "\033[1;34m ps\033[0m" << endl;
cout << "\033[1;34m*** Final orphan count is \033[1;31m" << gotOrphanCount << "\033[0m" << endl;
cout << "\033[1;34m*** Final mean_dev_noOrphan is \033[1;31m" << gotMin_mean_dev_noOrphan << "\033[1;34m ns\033[0m" << endl;
cout << "\033[1;34m*** Final increament times in this result is \033[1;31m" << increament_times << "\033[0m" << endl;
cout << "\033[1;34m*** Final mean deviation is \033[1;31m" << min_mean_dev << "\033[1;34m ns\033[0m" << endl;
cout << "\033[1;34m*** Final PPS index is \033[1;31m" << final_i << "\033[1;34m, in total of \033[1;31m" << LAPPD_PPS.size() << "\033[0m" << endl;
cout << "\033[1;34m*** Final CTC PPS index is \033[1;31m" << final_j << "\033[1;34m, in total of \033[1;31m" << CTCPPS.size() << "\033[0m" << endl;
cout << "\033[1;34m***************************\033[0m" << endl;
cout << "\033[1;34m******* Saving *************\033[0m" << endl;
// now, based on this offset, calculate the event time for each event, and save to result.
vector<ULong64_t> TimeStampRaw;
vector<ULong64_t> BeamGateRaw;
vector<ULong64_t> TimeStamp_ns;
vector<ULong64_t> BeamGate_ns;
vector<ULong64_t> TimeStamp_ps;
vector<ULong64_t> BeamGate_ps;
vector<ULong64_t> EventIndex;
vector<ULong64_t> EventDeviation_ns;
vector<ULong64_t> CTCTriggerIndex;
vector<ULong64_t> CTCTriggerTimeStamp_ns;
vector<ULong64_t> BeamGate_correction_tick;
vector<ULong64_t> TimeStamp_correction_tick;
vector<long long> PPS_tick_correction; // if timestamp falls in between of PPS i and i+1, corrected timestamp = timestamp + PPS_tick_correction[i]
vector<long long> LAPPD_PPS_missing_ticks;
vector<ULong64_t> LAPPD_PPS_interval_ticks;
vector<ULong64_t> BG_PPSBefore;
vector<ULong64_t> BG_PPSAfter;
vector<ULong64_t> BG_PPSDiff;
vector<ULong64_t> BG_PPSMiss;
vector<ULong64_t> TS_PPSBefore;
vector<ULong64_t> TS_PPSAfter;
vector<ULong64_t> TS_PPSDiff;
vector<ULong64_t> TS_PPSMiss;
vector<ULong64_t> TS_driftCorrection_ns;
vector<ULong64_t> BG_driftCorrection_ns;
// calculate the missing ticks for each LAPPD PPS
PPS_tick_correction.push_back(0);
LAPPD_PPS_missing_ticks.push_back(0);
LAPPD_PPS_interval_ticks.push_back(0);
ULong64_t intervalTicks = 320000000 * (PPSDeltaT / 1000 / 1E9);
for (int i = 1; i < LAPPD_PPS.size(); i++)
{
ULong64_t thisPPS = LAPPD_PPS.at(i) / 3125;
ULong64_t prevPPS = LAPPD_PPS.at(i - 1) / 3125;
long long thisInterval = thisPPS - prevPPS;
long long thisMissingTicks = intervalTicks - thisInterval;
LAPPD_PPS_interval_ticks.push_back(thisInterval);
// if the difference between this PPS and previous PPS is > intervalTicks-20 and < intervalTicks+5,
// the missed value is thisInterval - intervalTicks
// push this value plus the sum of previous missing ticks to the vector
// else just push the sum of previous missing ticks
long long sumOfPreviousMissingTicks = 0;
for (int j = 0; j < LAPPD_PPS_missing_ticks.size(); j++)
{
sumOfPreviousMissingTicks += LAPPD_PPS_missing_ticks.at(j);
}
if (thisMissingTicks > -20 && thisMissingTicks < 20)
{
LAPPD_PPS_missing_ticks.push_back(thisMissingTicks);
PPS_tick_correction.push_back(thisMissingTicks + sumOfPreviousMissingTicks);
}
else
{
// some time one pps might be wired, but the combination of two is ok.
bool combined = false;
// if there are missing PPS, interval is like 22399999990 % 3200000000 = 3199999990
long long pInterval = thisInterval % intervalTicks;
long long missingPTicks = 0;
if (intervalTicks - pInterval < 30)
missingPTicks = intervalTicks - pInterval;
else if (pInterval < 30)
missingPTicks = -pInterval;
//////
if (missingPTicks == 1 || missingPTicks == -1)
{
cout << "Found missing tick is " << missingPTicks << ",continue." << endl;
LAPPD_PPS_missing_ticks.push_back(0);
PPS_tick_correction.push_back(sumOfPreviousMissingTicks);
continue;
}
if (missingPTicks != 0)
{
LAPPD_PPS_missing_ticks.push_back(missingPTicks);
PPS_tick_correction.push_back(missingPTicks + sumOfPreviousMissingTicks);
combined = true;
cout << "Pushing PPS correction " << i << ", this PPS interval tick is " << thisInterval << ", missing ticks: " << thisMissingTicks << ", push missing " << LAPPD_PPS_missing_ticks.at(i) << ", push correction " << PPS_tick_correction.at(i) << endl;
continue;
}
// if one PPS is not recoreded correctly, like one interval is 1574262436, followed by a 4825737559
// then 1574262436 + 4825737559 = 6399999995 = 3200000000 * 2 - 5
long long nextInterval = 0;
if (i < LAPPD_PPS.size() - 1)
{
nextInterval = LAPPD_PPS.at(i + 1) / 3125 - thisPPS;
long long combinedMissingTicks = intervalTicks - (nextInterval + thisInterval) % intervalTicks;
if (combinedMissingTicks > -30 && combinedMissingTicks < 30)
{
LAPPD_PPS_missing_ticks.push_back(combinedMissingTicks);
PPS_tick_correction.push_back(combinedMissingTicks + sumOfPreviousMissingTicks);
combined = true;
cout << "Pushing PPS correction " << i << ", this PPS interval tick is " << thisInterval << ", missing ticks: " << thisMissingTicks << ", push missing " << LAPPD_PPS_missing_ticks.at(i) << ", push correction " << PPS_tick_correction.at(i) << endl;
continue;
}
}
if (!combined)
{
LAPPD_PPS_missing_ticks.push_back(0);
PPS_tick_correction.push_back(sumOfPreviousMissingTicks);
}
}
cout << "Pushing PPS correction " << i << ", this PPS interval tick is " << thisInterval << ", missing ticks: " << thisMissingTicks << ", push missing " << LAPPD_PPS_missing_ticks.at(i) << ", push correction " << PPS_tick_correction.at(i) << endl;
}
// loop all data events, plus the offset, save the event time and beamgate time
// fing the closest CTC trigger, also save all information
cout << "Start saving results. PPS size: " << LAPPD_PPS.size() << ", beamgate size: " << LAPPDDataBeamgateUL.size() << endl;
cout << "First PPS: " << LAPPD_PPS.at(0) / 3125 << ", Last PPS: " << LAPPD_PPS.at(LAPPD_PPS.size() - 1) / 3125 << endl;
cout << "First BG: " << LAPPDDataBeamgateUL.at(0) / 3125 << ", Last BG: " << LAPPDDataBeamgateUL.at(LAPPDDataBeamgateUL.size() - 1) / 3125 << endl;
cout << "First TS: " << LAPPDDataTimeStampUL.at(0) / 3125 << ", Last TS: " << LAPPDDataTimeStampUL.at(LAPPDDataTimeStampUL.size() - 1) / 3125 << endl;
for (int l = 0; l < LAPPDDataTimeStampUL.size(); l++)
{
ULong64_t TS_ns = LAPPDDataTimeStampUL.at(l) / 1000;
ULong64_t TS_truncated_ps = LAPPDDataTimeStampUL.at(l) % 1000;
ULong64_t BG_ns = LAPPDDataBeamgateUL.at(l) / 1000;
ULong64_t BG_truncated_ps = LAPPDDataBeamgateUL.at(l) % 1000;
ULong64_t driftCorrectionForTS = 0;
ULong64_t driftCorrectionForBG = 0;
if (drift != 0)
{
double driftScaling = static_cast<double>(TS_ns) / (trueInterval / 1000);
driftCorrectionForTS = static_cast<ULong64_t>(drift/ 1000 * driftScaling );
double driftScalingBG = static_cast<double>(BG_ns) / (trueInterval / 1000);
driftCorrectionForBG = static_cast<ULong64_t>(drift/ 1000 * driftScalingBG );
cout<<"drift = "<<drift<<", driftScaling = "<<driftScaling<<", driftCorrectionForTS = "<<driftCorrectionForTS<<", driftCorrectionForBG = "<<driftCorrectionForBG<<endl;
cout<<"driftCorrectionForTS = "<<driftCorrectionForTS<<", driftCorrectionForBG = "<<driftCorrectionForBG<<endl;
}
ULong64_t DriftCorrectedTS_ns = TS_ns + final_offset_ns + driftCorrectionForTS;
ULong64_t DriftCorrectedBG_ns = BG_ns + final_offset_ns + driftCorrectionForBG;
cout<<"DriftCorrectedTS_ns = "<<DriftCorrectedTS_ns<<" =: final_offset_ns = "<<final_offset_ns<<" + driftCorrectionForTS = "<<driftCorrectionForTS<<" + TS_ns = "<<TS_ns<<endl;
ULong64_t min_mean_dev_match = std::numeric_limits<ULong64_t>::max();
int matchedIndex = 0;
for (int c = 0; c < CTCTrigger.size(); c++)
{
ULong64_t CTCTrigger_ns = CTCTrigger.at(c);
Long64_t diff = CTCTrigger_ns - DriftCorrectedTS_ns;
if (diff < 0)
diff = -diff;
if (diff < min_mean_dev_match)
{
matchedIndex = c;
min_mean_dev_match = diff;
}
}
// check the position of beamgate and timestamp raw fall into which pps interval;
bool TSFound = false;
bool BGFound = false;
for (int i = 0; i < LAPPD_PPS.size() - 1; i++)
{
if (LAPPD_PPS.at(i) / 3125 < LAPPDDataTimeStampUL.at(l) / 3125 && LAPPD_PPS.at(i + 1) / 3125 > LAPPDDataTimeStampUL.at(l) / 3125)
{
TimeStamp_correction_tick.push_back(PPS_tick_correction.at(i) + 1000);
TSFound = true;
break;
}
}
if (!TSFound && LAPPDDataTimeStampUL.at(l) / 3125 > LAPPD_PPS.at(LAPPD_PPS.size() - 1) / 3125)
{
TimeStamp_correction_tick.push_back(PPS_tick_correction.at(LAPPD_PPS.size() - 1) + 1000);
TSFound = true;
}
if (!TSFound && LAPPDDataTimeStampUL.at(l) / 3125 < LAPPD_PPS.at(0) / 3125)
{
TimeStamp_correction_tick.push_back(0 + 1000);
TSFound = true;
}
for (int i = 0; i < LAPPD_PPS.size() - 1; i++)
{
if (LAPPD_PPS.at(i) / 3125 < LAPPDDataBeamgateUL.at(l) / 3125 && LAPPD_PPS.at(i + 1) / 3125 > LAPPDDataBeamgateUL.at(l) / 3125)
{
BeamGate_correction_tick.push_back(PPS_tick_correction.at(i) + 1000);
BGFound = true;
cout << "Normal push: BGraw = " << LAPPDDataBeamgateUL.at(l) / 3125 << ", pps = " << LAPPD_PPS.at(i) << ", pps/3125 = " << LAPPD_PPS.at(i) / 3125 << endl;
if (LAPPD_PPS_interval_ticks.at(i) != intervalTicks)
{
cout << "Warning: PPS interval is not " << intervalTicks << " at index " << i << ", it is " << LAPPD_PPS_interval_ticks.at(i) << endl;
}
break;
}
}
if (!BGFound && LAPPDDataBeamgateUL.at(l) / 3125 > LAPPD_PPS.at(LAPPD_PPS.size() - 1) / 3125)
{
BeamGate_correction_tick.push_back(PPS_tick_correction.at(LAPPD_PPS.size() - 1) + 1000);
cout << "BGraw = " << LAPPDDataBeamgateUL.at(l) / 3125 << ", pps = " << LAPPD_PPS.at(LAPPD_PPS.size() - 1) << ", pps/3125 = " << LAPPD_PPS.at(LAPPD_PPS.size() - 1) / 3125 << endl;
BGFound = true;
}
if (!BGFound && LAPPDDataBeamgateUL.at(l) / 3125 < LAPPD_PPS.at(0) / 3125)
{
BeamGate_correction_tick.push_back(0 + 1000);
cout << "BGraw less than pps0" << endl;
BGFound = true;
}
if (!TSFound || !BGFound)
{
cout << "Error: PPS not found for event " << l << ", TSFound: " << TSFound << ", BGFound: " << BGFound << endl;
}
/*
cout << "******Found result:" << endl;
cout << "LAPPDDataTimeStampUL.at(" << l << "): " << LAPPDDataTimeStampUL.at(l)/3125 << endl;
cout << "LAPPDDataBeamgateUL.at(" << l << "): " << LAPPDDataBeamgateUL.at(l)/3125 << endl;
cout << "TS_ns: " << TS_ns << endl;
cout << "BG_ns: " << BG_ns << endl;
cout << "final_offset_ns: " << final_offset_ns << endl;
cout << "drift: " << drift << endl;
cout << "TS driftscaling: " << TS_ns / trueInterval / 1000 << endl;
cout << "DriftCorrectedTS_ns: " << DriftCorrectedTS_ns << endl;
cout << "TS_truncated_ps: " << TS_truncated_ps << endl;
cout << "DriftCorrectedBG_ns: " << DriftCorrectedBG_ns << endl;
cout << "BG_truncated_ps: " << BG_truncated_ps << endl;
cout << "Found min_mean_dev_match: " << min_mean_dev_match << endl;
cout << "MatchedIndex: " << matchedIndex << endl;
cout << "CTCTrigger.at(" << matchedIndex << "): " << CTCTrigger.at(matchedIndex) << endl;
cout << "BeamGate_correction_tick at " << l << ": " << BeamGate_correction_tick.at(l) << endl;
cout << "TimeStamp_correction_tick at " << l << ": " << TimeStamp_correction_tick.at(l) << endl;
*/
// for this LAPPDDataBeamgateUL.at(l), in the LAPPD_PPS vector, find it's closest PPS before and after, and also calculate the time difference between them, and the missing tick between them.
// save as BG_PPSBefore_tick, BG_PPSAfter_tick, BG_PPSDiff_tick, BG_PPSMissing_tick
// Do the same thing for LAPPDDataTimeStampUL.at(l), save as TS_PPSBefore_tick, TS_PPSAfter_tick, TS_PPSDiff_tick, TS_PPSMissing_tick
ULong64_t BG_PPSBefore_tick = 0;
ULong64_t BG_PPSAfter_tick = 0;
ULong64_t BG_PPSDiff_tick = 0;
ULong64_t BG_PPSMissing_tick = 0;
ULong64_t TS_PPSBefore_tick = 0;
ULong64_t TS_PPSAfter_tick = 0;
ULong64_t TS_PPSDiff_tick = 0;
ULong64_t TS_PPSMissing_tick = 0;
// if the first PPS is later than beamgate, then set before = 0, after is the first, diff is the first, missing is the first
cout << "Start finding PPS before and after the beamgate" << endl;
if ((LAPPD_PPS.at(0) / 3125 > LAPPDDataBeamgateUL.at(l) / 3125) || (LAPPD_PPS.at(LAPPD_PPS.size() - 1) / 3125 < LAPPDDataBeamgateUL.at(l) / 3125))
{
if (LAPPD_PPS.at(0) / 3125 > LAPPDDataBeamgateUL.at(l) / 3125)
{
BG_PPSBefore_tick = 0;
BG_PPSAfter_tick = LAPPD_PPS.at(0) / 3125;
BG_PPSDiff_tick = LAPPD_PPS.at(0) / 3125;
BG_PPSMissing_tick = LAPPD_PPS.at(0) / 3125;
cout << "First PPS is later than beamgate, before is 0, after is the first, diff is the first, missing is the first" << endl;
}
// if the last PPS is earlier than beamgate, then set before is the last, after = 0, diff is the last, missing is the last
if (LAPPD_PPS.at(LAPPD_PPS.size() - 1) / 3125 < LAPPDDataBeamgateUL.at(l) / 3125)
{
BG_PPSBefore_tick = LAPPD_PPS.at(LAPPD_PPS.size() - 1) / 3125;
BG_PPSAfter_tick = 0;
BG_PPSDiff_tick = LAPPD_PPS.at(LAPPD_PPS.size() - 1) / 3125;
BG_PPSMissing_tick = LAPPD_PPS.at(LAPPD_PPS.size() - 1) / 3125;
cout << "Last PPS is earlier than beamgate, before is the last, after is 0, diff is the last, missing is the last" << endl;
}
}
else
{
// if the first PPS is earlier than beamgate, and the last PPS is later than beamgate, then find the closest PPS before and after
for (int i = 0; i < LAPPD_PPS.size() - 1; i++)
{
if (LAPPD_PPS.at(i) / 3125 < LAPPDDataBeamgateUL.at(l) / 3125 && LAPPD_PPS.at(i + 1) / 3125 > LAPPDDataBeamgateUL.at(l) / 3125)
{
BG_PPSBefore_tick = LAPPD_PPS.at(i) / 3125;
BG_PPSAfter_tick = LAPPD_PPS.at(i + 1) / 3125;
BG_PPSDiff_tick = LAPPD_PPS.at(i + 1) / 3125 - LAPPD_PPS.at(i) / 3125;
ULong64_t DiffTick = (LAPPD_PPS.at(i + 1) - LAPPD_PPS.at(i)) / 3125 - 1000;
BG_PPSMissing_tick = 3200000000 - DiffTick;
cout << "Found PPS before and after the beamgate, before: " << BG_PPSBefore_tick << ", after: " << BG_PPSAfter_tick << ", diff: " << BG_PPSDiff_tick << ", missing: " << BG_PPSMissing_tick << endl;
break;
}
}
}
// do the samething for timestamp
cout << "Start finding PPS before and after the timestamp" << endl;
if ((LAPPD_PPS.at(0) / 3125 > LAPPDDataTimeStampUL.at(l) / 3125) || (LAPPD_PPS.at(LAPPD_PPS.size() - 1) / 3125 < LAPPDDataTimeStampUL.at(l) / 3125))
{
if (LAPPD_PPS.at(0) / 3125 > LAPPDDataTimeStampUL.at(l) / 3125)
{
TS_PPSBefore_tick = 0;
TS_PPSAfter_tick = LAPPD_PPS.at(0) / 3125;
TS_PPSDiff_tick = LAPPD_PPS.at(0) / 3125;
TS_PPSMissing_tick = LAPPD_PPS.at(0) / 3125;
cout << "First PPS is later than timestamp, before is 0, after is the first, diff is the first, missing is the first" << endl;
}
if (LAPPD_PPS.at(LAPPD_PPS.size() - 1) / 3125 < LAPPDDataTimeStampUL.at(l) / 3125)
{
TS_PPSBefore_tick = LAPPD_PPS.at(LAPPD_PPS.size() - 1) / 3125;
TS_PPSAfter_tick = 0;
TS_PPSDiff_tick = LAPPD_PPS.at(LAPPD_PPS.size() - 1) / 3125;
TS_PPSMissing_tick = LAPPD_PPS.at(LAPPD_PPS.size() - 1) / 3125;
cout << "Last PPS is earlier than timestamp, before is the last, after is 0, diff is the last, missing is the last" << endl;
}
}
else
{
for (int i = 0; i < LAPPD_PPS.size() - 1; i++)
{
if (LAPPD_PPS.at(i) / 3125 < LAPPDDataTimeStampUL.at(l) / 3125 && LAPPD_PPS.at(i + 1) / 3125 > LAPPDDataTimeStampUL.at(l) / 3125)
{
TS_PPSBefore_tick = LAPPD_PPS.at(i) / 3125;
TS_PPSAfter_tick = LAPPD_PPS.at(i + 1) / 3125;
TS_PPSDiff_tick = LAPPD_PPS.at(i + 1) / 3125 - LAPPD_PPS.at(i) / 3125;
ULong64_t DiffTick = (LAPPD_PPS.at(i + 1) - LAPPD_PPS.at(i)) / 3125 - 1000;
TS_PPSMissing_tick = 3200000000 - DiffTick;
cout << "Found PPS before and after the timestamp, before: " << TS_PPSBefore_tick << ", after: " << TS_PPSAfter_tick << ", diff: " << TS_PPSDiff_tick << ", missing: " << TS_PPSMissing_tick << endl;
break;
}
}
}
TimeStampRaw.push_back(LAPPDDataTimeStampUL.at(l) / 3125);
BeamGateRaw.push_back(LAPPDDataBeamgateUL.at(l) / 3125);
TimeStamp_ns.push_back(DriftCorrectedTS_ns);
BeamGate_ns.push_back(DriftCorrectedBG_ns);
TimeStamp_ps.push_back(TS_truncated_ps);
BeamGate_ps.push_back(BG_truncated_ps);
EventIndex.push_back(l);
EventDeviation_ns.push_back(min_mean_dev_match);
// to get ps, should minus the TS_truncated_ps
CTCTriggerIndex.push_back(matchedIndex);
CTCTriggerTimeStamp_ns.push_back(CTCTrigger.at(matchedIndex));
BG_PPSBefore.push_back(BG_PPSBefore_tick);
BG_PPSAfter.push_back(BG_PPSAfter_tick);
BG_PPSDiff.push_back(BG_PPSDiff_tick);
BG_PPSMiss.push_back(BG_PPSMissing_tick);
TS_PPSBefore.push_back(TS_PPSBefore_tick);
TS_PPSAfter.push_back(TS_PPSAfter_tick);
TS_PPSDiff.push_back(TS_PPSDiff_tick);
TS_PPSMiss.push_back(TS_PPSMissing_tick);
TS_driftCorrection_ns.push_back(driftCorrectionForTS);
BG_driftCorrection_ns.push_back(driftCorrectionForBG);
}
ULong64_t totalEventNumber = LAPPDDataTimeStampUL.size();
ULong64_t gotOrphanCount_out = gotOrphanCount;
ULong64_t gotMin_mean_dev_noOrphan_out = gotMin_mean_dev_noOrphan;
ULong64_t increament_times_out = increament_times;
ULong64_t min_mean_dev_out = min_mean_dev;
ULong64_t final_i_out = final_i;
ULong64_t final_j_out = final_j;
ULong64_t drift_out = drift;
vector<ULong64_t> FitInfo = {final_offset_ns, final_offset_ps_negative, gotOrphanCount_out, gotMin_mean_dev_noOrphan_out, increament_times_out, min_mean_dev_out, final_i_out, final_j_out, totalEventNumber, drift_out};
vector<vector<ULong64_t>> Result = {FitInfo, TimeStampRaw, BeamGateRaw, TimeStamp_ns, BeamGate_ns, TimeStamp_ps, BeamGate_ps, EventIndex, EventDeviation_ns, CTCTriggerIndex, CTCTriggerTimeStamp_ns, BeamGate_correction_tick, TimeStamp_correction_tick, LAPPD_PPS_interval_ticks, BG_PPSBefore, BG_PPSAfter, BG_PPSDiff, BG_PPSMiss, TS_PPSBefore, TS_PPSAfter, TS_PPSDiff, TS_PPSMiss, TS_driftCorrection_ns, BG_driftCorrection_ns};
return Result;
}
vector<vector<ULong64_t>> fitInPartFile(TTree *lappdTree, TTree *triggerTree, int runNumber, int subRunNumber, int partFileNumber, int LAPPD_ID, int fitTargetTriggerWord, bool triggerGrouped, int intervalInSecond)
{
cout << "***************************************" << endl;
cout << "Fitting in run " << runNumber << ", sub run " << subRunNumber << ", part file " << partFileNumber << " for LAPPD ID " << LAPPD_ID << endl;
vector<ULong64_t> LAPPD_PPS0;
vector<ULong64_t> LAPPD_PPS1;
vector<ULong64_t> LAPPDDataTimeStampUL;
vector<ULong64_t> LAPPDDataBeamgateUL;
vector<ULong64_t> CTCTargetTimeStamp;
vector<ULong64_t> CTCPPSTimeStamp;
int LAPPD_ID_inTree;
int runNumber_inTree;
int subRunNumber_inTree;
int partFileNumber_inTree;
ULong64_t ppsTime0;
ULong64_t ppsTime1;
ULong64_t LAPPDTimeStampUL;
ULong64_t LAPPDBeamgateUL;
ULong64_t LAPPDDataTimestamp;
ULong64_t LAPPDDataBeamgate;
double LAPPDDataTimestampFloat;
double LAPPDDataBeamgateFloat;
vector<uint32_t> *CTCTriggerWord = nullptr;
ULong64_t CTCTimeStamp;
vector<uint32_t> *groupedTriggerWords = nullptr;
vector<ULong64_t> *groupedTriggerTimestamps = nullptr;
lappdTree->SetBranchAddress("RunNumber", &runNumber_inTree);
lappdTree->SetBranchAddress("SubRunNumber", &subRunNumber_inTree);
lappdTree->SetBranchAddress("PartFileNumber", &partFileNumber_inTree);
lappdTree->SetBranchAddress("LAPPD_ID", &LAPPD_ID_inTree);
lappdTree->SetBranchAddress("LAPPDDataTimeStampUL", &LAPPDTimeStampUL);
lappdTree->SetBranchAddress("LAPPDDataBeamgateUL", &LAPPDBeamgateUL);
lappdTree->SetBranchAddress("LAPPDDataTimestamp", &LAPPDDataTimestamp);
lappdTree->SetBranchAddress("LAPPDDataBeamgate", &LAPPDDataBeamgate);
lappdTree->SetBranchAddress("LAPPDDataTimestampFloat", &LAPPDDataTimestampFloat);
lappdTree->SetBranchAddress("LAPPDDataBeamgateFloat", &LAPPDDataBeamgateFloat);
lappdTree->SetBranchAddress("ppsTime0", &ppsTime0);
lappdTree->SetBranchAddress("ppsTime1", &ppsTime1);
// triggerTree->Print();
triggerTree->SetBranchAddress("RunNumber", &runNumber_inTree);
triggerTree->SetBranchAddress("SubRunNumber", &subRunNumber_inTree);
triggerTree->SetBranchAddress("PartFileNumber", &partFileNumber_inTree);
if (triggerGrouped)
{
triggerTree->SetBranchAddress("gTrigWord", &groupedTriggerWords);
triggerTree->SetBranchAddress("gTrigTime", &groupedTriggerTimestamps);
}
else
{
triggerTree->SetBranchAddress("CTCTriggerWord", &CTCTriggerWord);
triggerTree->SetBranchAddress("CTCTimeStamp", &CTCTimeStamp);
}
int l_nEntries = lappdTree->GetEntries();
int t_nEntries = triggerTree->GetEntries();
int repeatedPPSNumber0 = -1;
int repeatedPPSNumber1 = -1;
// 5. loop TimeStamp tree
for (int i = 0; i < l_nEntries; i++)
{
lappdTree->GetEntry(i);
// if(i<100)
// cout<<"LAPPD_ID_inTree: "<<LAPPD_ID_inTree<<", runNumber_inTree: "<<runNumber_inTree<<", subRunNumber_inTree: "<<subRunNumber_inTree<<", partFileNumber_inTree: "<<partFileNumber_inTree<<endl;
// cout<<"LAPPD_ID: "<<LAPPD_ID<<", runNumber: "<<runNumber<<", subRunNumber: "<<subRunNumber<<", partFileNumber: "<<partFileNumber<<endl;
if (LAPPD_ID_inTree == LAPPD_ID && runNumber_inTree == runNumber && subRunNumber_inTree == subRunNumber && partFileNumber_inTree == partFileNumber)
{
if (LAPPDTimeStampUL != 0)
{
cout << "In unit of 1ps, after conversion and saving, LAPPDTimeStampUL: " << LAPPDTimeStampUL * 1000 / 8 * 25 << ", LAPPDBeamgateUL: " << LAPPDBeamgateUL * 1000 / 8 * 25 << endl;
cout << "In second, use double, Timestamp: " << static_cast<double>(LAPPDTimeStampUL * 1000 / 8 * 25) / 1E9 / 1000 << ", Beamgate: " << static_cast<double>(LAPPDBeamgateUL * 1000 / 8 * 25) / 1E9 / 1000 << endl;
LAPPDDataTimeStampUL.push_back(LAPPDTimeStampUL * 1000 / 8 * 25);
LAPPDDataBeamgateUL.push_back(LAPPDBeamgateUL * 1000 / 8 * 25);
/*
cout << "LAPPDDataTimestamp: " << LAPPDDataTimestamp << ", LAPPDDataBeamgate: " << LAPPDDataBeamgate << endl;
cout << "LAPPDDataTimestampFloat: " << LAPPDDataTimestampFloat << ", LAPPDDataBeamgateFloat: " << LAPPDDataBeamgateFloat << endl;
ULong64_t ULTS = LAPPDTimeStampUL*1000/8*25;
double DTS = static_cast<double>(LAPPDDataTimestamp);
double plusDTS = DTS + LAPPDDataTimestampFloat;
ULong64_t ULBG = LAPPDBeamgateUL*1000/8*25;
double DBG = static_cast<double>(LAPPDDataBeamgate);
double plusDBG = DBG + LAPPDDataBeamgateFloat;
cout << std::fixed << " ULTS: " << ULTS << ", DTS: " << DTS << ", plusDTS: " << plusDTS << endl;
cout << std::fixed << " ULBG: " << ULBG << ", DBG: " << DBG << ", plusDBG: " << plusDBG << endl;
cout << endl;
*/
}
else if (LAPPDTimeStampUL == 0)
{
// cout << "ppsTime0: " << ppsTime0 << ", ppsTime1: " << ppsTime1 << endl;
if (LAPPD_PPS0.size() == 0)
LAPPD_PPS0.push_back(ppsTime0 * 1000 / 8 * 25);
if (LAPPD_PPS1.size() == 0)
LAPPD_PPS1.push_back(ppsTime1 * 1000 / 8 * 25);
if (LAPPD_PPS0.size() > 0 && ppsTime0 * 1000 / 8 * 25 != LAPPD_PPS0.at(LAPPD_PPS0.size() - 1))
LAPPD_PPS0.push_back(ppsTime0 * 1000 / 8 * 25);
else
repeatedPPSNumber0 += 1;
if (LAPPD_PPS1.size() > 0 && ppsTime1 * 1000 / 8 * 25 != LAPPD_PPS1.at(LAPPD_PPS1.size() - 1))
LAPPD_PPS1.push_back(ppsTime1 * 1000 / 8 * 25);
else
repeatedPPSNumber1 += 1;
}
}
}
cout << "repeatedPPSNumber0: " << repeatedPPSNumber0 << ", loaded PPS0 size: " << LAPPD_PPS0.size() << endl;
cout << "repeatedPPSNumber1: " << repeatedPPSNumber1 << ", loaded PPS1 size: " << LAPPD_PPS1.size() << endl;
// 6. loop Trig (or GTrig) tree
for (int i = 0; i < t_nEntries; i++)
{
triggerTree->GetEntry(i);
if (runNumber_inTree == runNumber && subRunNumber_inTree == subRunNumber && partFileNumber_inTree == partFileNumber)
{
// cout<<"triggerGrouped: "<<triggerGrouped<<", fitTargetTriggerWord: "<<fitTargetTriggerWord<<endl;
if (triggerGrouped)
{
for (int j = 0; j < groupedTriggerWords->size(); j++)
{
// cout<<"At j = "<<j<<",finding groupedTriggerWords: "<<groupedTriggerWords->at(j)<<", fitTargetTriggerWord: "<<fitTargetTriggerWord<<endl;
if (groupedTriggerWords->at(j) == fitTargetTriggerWord)
CTCTargetTimeStamp.push_back(groupedTriggerTimestamps->at(j));
if (groupedTriggerWords->at(j) == 32)
CTCPPSTimeStamp.push_back(groupedTriggerTimestamps->at(j));
}
}
else
{
for (int j = 0; j < CTCTriggerWord->size(); j++)
{
if (CTCTriggerWord->at(j) == fitTargetTriggerWord)
CTCTargetTimeStamp.push_back(CTCTimeStamp);
if (CTCTriggerWord->at(j) == 32)
CTCPPSTimeStamp.push_back(CTCTimeStamp);
}
}
}
}
cout << "Vector for partfile " << partFileNumber << " for LAPPD ID " << LAPPD_ID << " loaded." << endl;
cout << "LAPPDDataTimeStampUL in ps size: " << LAPPDDataTimeStampUL.size() << endl;
cout << "LAPPDDataBeamgateUL in ps size: " << LAPPDDataBeamgateUL.size() << endl;
cout << "LAPPD_PPS0 size: " << LAPPD_PPS0.size() << endl;
cout << "LAPPD_PPS1 size: " << LAPPD_PPS1.size() << endl;
cout << "CTCTargetTimeStamp size: " << CTCTargetTimeStamp.size() << endl;
cout << "CTCPPSTimeStamp size: " << CTCPPSTimeStamp.size() << endl;
// 7. Find the number of resets in LAPPD PPS:
int LAPPDDataFitStopIndex = 0;
int resetNumber = 0;
// first check is there a reset, if no, set the LAPPDDataFitStopIndex to the size of LAPPDDataTimeStampUL
// if all PPS in this part file was increamented, then there is no reset
for (int i = 1; i < LAPPD_PPS0.size(); i++)
{
if (LAPPD_PPS0[i] < LAPPD_PPS0[i - 1])
{
resetNumber += 1;
cout << "For LAPPD ID " << LAPPD_ID << ", run number " << runNumber << ", sub run number " << subRunNumber << ", part file number " << partFileNumber << ", reset "
<< " found at PPS_ACDC0 index " << i << endl;
break;
}
}
for (int i = 1; i < LAPPD_PPS1.size(); i++)
{
if (LAPPD_PPS1[i] < LAPPD_PPS1[i - 1])
{
resetNumber += 1;
cout << "For LAPPD ID " << LAPPD_ID << ", run number " << runNumber << ", sub run number " << subRunNumber << ", part file number " << partFileNumber << ", reset "
<< " found at PPS_ACDC1 index " << i << endl;
break;
}
}
if (resetNumber == 0)
cout << "For LAPPD ID " << LAPPD_ID << ", run number " << runNumber << ", sub run number " << subRunNumber << ", part file number " << partFileNumber << ", no reset found." << endl;
// if reset found, loop the timestamp in order to find the LAPPDDataFitStopIndex
//
if (resetNumber != 0)
{
for (int i = 1; i < LAPPDDataTimeStampUL.size(); i++)
{
if (LAPPDDataTimeStampUL[i] < LAPPDDataTimeStampUL[i - 1])
{
LAPPDDataFitStopIndex = i - 1;
break;
}
}
// TODO: extend this to later offsets
}
// 8. Use the target trigger word, fit the offset
// TODO: fit for each reset
vector<vector<ULong64_t>> ResultTotal;
if (LAPPDDataTimeStampUL.size() == LAPPDDataBeamgateUL.size())
{
if (LAPPD_PPS0.size() == 0 || LAPPD_PPS1.size() == 0)
{
cout << "Error: PPS0 or PPS1 is empty, return empty result." << endl;
return ResultTotal;
}
vector<vector<ULong64_t>> ResultACDC0 = fitInThisReset(LAPPDDataTimeStampUL, LAPPDDataBeamgateUL, LAPPD_PPS0, fitTargetTriggerWord, CTCTargetTimeStamp, CTCPPSTimeStamp, intervalInSecond * 1E9 * 1000);
vector<vector<ULong64_t>> ResultACDC1 = fitInThisReset(LAPPDDataTimeStampUL, LAPPDDataBeamgateUL, LAPPD_PPS1, fitTargetTriggerWord, CTCTargetTimeStamp, CTCPPSTimeStamp, intervalInSecond * 1E9 * 1000);
// 9. save the offset for this LAPPD ID, run number, part file number, index, reset number.
cout << "Fitting in part file " << partFileNumber << " for LAPPD ID " << LAPPD_ID << " done." << endl;
// Combine ResultACDC0 and ResultACDC1 to ResultTotal
for (int i = 0; i < ResultACDC0.size(); i++)
{
ResultTotal.push_back(ResultACDC0[i]);
}
for (int i = 0; i < ResultACDC1.size(); i++)
{
ResultTotal.push_back(ResultACDC1[i]);
}
}
return ResultTotal;
}
void offsetFit_MultipleLAPPD(string fileName, int fitTargetTriggerWord, bool triggerGrouped, int intervalInSecond, int processPFNumber)
{
// 1. load LAPPDTree.root
const string file = fileName;
TFile *f = new TFile(file.c_str(), "READ");
if (!f->IsOpen())
{
std::cerr << "Error: cannot open file " << file << std::endl;
return;
}
std::cout << "Opened file " << file << std::endl;
std::ofstream outputOffset("offset.txt");
outputOffset << "runNumber"
<< "\t"
<< "subRunNumber"
<< "\t"
<< "partFileNumber"
<< "\t"
<< "resetNumber"
<< "\t"
<< "LAPPD_ID"
<< "\t"
<< "offset_ACDC0_ns"
<< "\t"
<< "offset_ACDC1_ns"
<< "\t"
<< "offset_ACDC0_ps_negative"
<< "\t"