-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNeutrinoDirectionality.cc
1292 lines (1083 loc) · 51.6 KB
/
NeutrinoDirectionality.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 "NeutrinoDirectionality.h"
#include "DetectorConfig.h"
#include "Formatting.h"
#include "Timer.h"
using std::cout, std::string, std::ifstream, std::array, std::getline;
void FillDetectorConfig()
{
// Filling values based on different periods
int noSegments = 154;
int noPeriods = 6;
for (int i = 0; i < noPeriods; i++)
{
int excludeSize = excludeList[i].size();
int counter = 0, tmp = 0;
vector<int> period;
for (int j = 0; j < noSegments; j++)
{
// Filling live segments by checking exclude list
tmp = excludeList[i][counter];
if (j == tmp)
{
period.push_back(0);
if (counter + 1 < excludeSize)
{
counter += 1;
}
}
else
{
period.push_back(1);
}
}
detectorConfig.push_back(period);
}
if (DETECTOR_VERBOSITY)
{
cout << "--------------------------------------------\n";
cout << "Below is the detector configuration.\n";
cout << "--------------------------------------------\n";
for (int i = 0; i < noPeriods; i++)
{
cout << "Detector configuration for period: " << i + 1 << '\n';
for (int j = 140; j >= 0; j -= 14)
{
for (int k = 0; k < 14; k++)
{
if (detectorConfig[i][j + k])
{
cout << "\u25A0 ";
}
else
{
cout << "\u25A1 ";
}
}
cout << '\n';
}
cout << '\n';
}
}
}
bool CheckNeighbor(int periodNo, int segment, char direction)
{
// Used for dead segment calculations
bool neighbor = false;
periodNo = periodNo - 1;
switch (direction)
{
case 'r':
neighbor = detectorConfig[periodNo][segment + 1];
break;
case 'l':
neighbor = detectorConfig[periodNo][segment - 1];
break;
case 'u':
neighbor = detectorConfig[periodNo][segment + 14];
break;
case 'd':
neighbor = detectorConfig[periodNo][segment - 14];
break;
default:
cout << "That direction doesn't exist!\n";
return false;
}
return neighbor;
}
Directionality::Directionality()
{
for (int dataset = Data; dataset < DatasetSize; dataset++) // Dataset
{
for (int signalSet = CorrelatedReactorOn; signalSet < TotalDifference; signalSet++) // Signal set
{
for (int direction = X; direction < DirectionSize; direction++)
{
// No reactor off for simulations
if ((dataset == Sim || dataset == SimUnbiased)
&& (signalSet == CorrelatedReactorOff || signalSet == AccidentalReactorOff))
continue;
string data = DatasetToString(dataset);
string signal = SignalToString(signalSet);
string axis = AxisToString(direction);
string histogramName = data + " " + signal + " " + axis;
histogram[dataset][signalSet][direction]
= TH1F(histogramName.c_str(), data.c_str(), bins, -histogramMax, histogramMax);
}
}
}
ResetLineNumber();
}
void Directionality::ReadFileList(int dataset, int periodNo)
{
char const* path;
switch (dataset)
{
case Data:
path = dataPath;
break;
case Sim:
path = simPath;
break;
case PerfSim:
path = perfSimPath;
break;
default:
path = dataPath;
break;
}
// Combining names into file list name
string fileList;
if (periodNo != 6)
fileList = Form(path, std::to_string(periodNo).c_str(), std::to_string(periodNo).c_str());
else
fileList = path;
// Opening and checking file list
ifstream file;
file.open(fileList, ifstream::in);
if (!(file.is_open() && file.good()))
{
cout << "File list not found! Exiting.\n";
cout << "Trying to find: " << fileList << '\n';
return;
}
while (file.good() && getline(file, files[lineNumber]))
{
lineNumber++;
}
files[lineNumber] = "Done";
lineNumber++;
}
void Directionality::SetUpHistograms(int dataset, int periodNo)
{
// Declaring some variables for use later
int totalLines = 0;
reactorOn = true;
dataSet = dataset;
period = periodNo;
char const* fileName;
switch (dataset)
{
case Data:
fileName = dataFileName;
break;
case Sim:
fileName = simFileName;
break;
case PerfSim:
fileName = perfSimFileName;
break;
default:
fileName = dataFileName;
break;
}
while (index < files.size())
{
if (files[index] == "Done")
{
index++;
return;
}
if (dataSet == Data || dataSet == DataUnbiased)
{
if (lineCounter % 100 == 0)
{
cout << "Reading file: " << lineCounter << "/" << totalDataLines << '\r';
cout.flush();
}
}
else if (dataSet == Sim || dataSet == SimUnbiased)
{
if (lineCounter % 50 == 0)
{
cout << "Reading file: " << lineCounter << "/" << totalSimLines << '\r';
cout.flush();
}
}
else if (dataSet == PerfSim || dataSet == PerfSimUnbiased)
{
cout << "Reading file: " << lineCounter << "/" << totalPerfSimLines << '\r';
cout.flush();
}
// Combining names into root file name
TString rootFilename;
if (period != 6)
rootFilename = Form(fileName, std::to_string(period).c_str(), files[index].data());
else
rootFilename = Form(fileName, files[index].data());
if (rootFilename.Contains(" 0"))
{
rootFilename.ReplaceAll(" 0", "");
reactorOn = false;
}
else if (rootFilename.Contains(" 1"))
{
rootFilename.ReplaceAll(" 1", "");
reactorOn = true;
}
// Open the root file
auto rootFile = std::make_unique<TFile>(rootFilename);
// Going into empty scope to let the pointers die out for safety
{
TVectorD* runtime = (TVectorD*)rootFile->Get("runtime");
TVectorD* promptVeto = (TVectorD*)rootFile->Get("accumulated/P2kIBDPlugin.tveto_prompt"); // prompt veto deadtime
TVectorD* delayedVeto = (TVectorD*)rootFile->Get("accumulated/P2kIBDPlugin.tveto_delayed"); // delayed veto deadtime
xRx = runtime->Max() / (runtime->Max() - promptVeto->Max()) * runtime->Max() / (runtime->Max() - delayedVeto->Max());
if (reactorOn && dataSet == Data)
{
livetimeOn += runtime->Max() / xRx;
}
else if (dataSet == Data)
{
livetimeOff += runtime->Max() / xRx;
}
}
// Grab rootTree and cast to unique pointer
TTree* rootTree = (TTree*)rootFile->Get("P2kIBDPlugin/Tibd");
long nEntries = rootTree->GetEntries();
for (long i = 0; i < nEntries; i++)
{
rootTree->GetEntry(i);
for (direction = X; direction < DirectionSize; direction++)
{
// Grabbing relevant values from the rootTree entry
promptPosition = rootTree->GetLeaf("xyz")->GetValue(direction);
delayedPosition = rootTree->GetLeaf("n_xyz")->GetValue(direction);
promptSegment = rootTree->GetLeaf("maxseg")->GetValue(0);
delayedSegment = rootTree->GetLeaf("n_seg")->GetValue(0);
// We throw out events where the neutron moves in a direction we're not checking
if (promptSegment != delayedSegment && promptPosition == delayedPosition)
continue;
if (promptSegment == 32 || delayedSegment == 32)
continue;
if (direction != Z) // Cubical distance cuts in Z for X and Y
{
float zPromptPosition = rootTree->GetLeaf("xyz")->GetValue(Z);
float zDelayedPosition = rootTree->GetLeaf("n_xyz")->GetValue(Z);
float displacement = fabs(zPromptPosition - zDelayedPosition);
if (displacement > 60)
continue;
}
// Copying some loop values into current entry
Esmear = rootTree->GetLeaf("Esmear")->GetValue(0);
nCaptTime = rootTree->GetLeaf("ncapt_dt")->GetValue(0);
FillHistogram();
}
}
// Returns the next character in the input sequence, without extracting it: The character is left as the next character
// to be extracted from the stream
rootFile->Close();
lineCounter++;
index++;
}
}
void Directionality::FillHistogram()
{
// Applying energy cut
if (Esmear < 0.8 || Esmear > 7.4)
{
return;
}
int signalSet = 0;
if (nCaptTime > pow(10, 3) && nCaptTime < 120 * pow(10, 3)) // Correlated Dataset
{
// Calculate neutron displacement
double displacement = delayedPosition - promptPosition;
// Figure out whether the reactor is on and assign signal index
signalSet = reactorOn ? CorrelatedReactorOn : CorrelatedReactorOff;
// Fill regular dataset with displacement but Z only takes same segment
if (direction != Z)
{
// Correcting center to center displacment
if (displacement > 0)
displacement = segmentWidth;
else if (displacement < 0)
displacement = -segmentWidth;
histogram[dataSet][signalSet][direction].Fill(displacement);
}
// Fill dead segment correction dataset
FillHistogramUnbiased(signalSet);
}
else if (nCaptTime > pow(10, 6)) // Accidental Dataset
{
// Calculate neutron displacement
double displacement = delayedPosition - promptPosition;
// Figure out whether the reactor is on and assign signal index
signalSet = reactorOn ? AccidentalReactorOn : AccidentalReactorOff;
// Fill regular dataset with displacement but Z only takes same segment
if (direction != Z)
{
// Correcting center to center displacment
if (displacement > 0)
displacement = segmentWidth;
else if (displacement < 0)
displacement = -segmentWidth;
histogram[dataSet][signalSet][direction].Fill(displacement, xRx);
}
// Fill dead segment correction dataset
FillHistogramUnbiased(signalSet);
}
}
void Directionality::FillHistogramUnbiased(int signalSet)
{
bool posDirection = false, negDirection = false;
bool isDisplacement = false;
// Need to weight accidental datasets by deadtime correction factor
double weight = (signalSet == AccidentalReactorOff || signalSet == AccidentalReactorOn) ? xRx : 1;
isDisplacement = promptSegment != delayedSegment ? true : false;
if (isDisplacement && direction != Z)
{
// Calculate neutron displacement
double displacement = delayedPosition - promptPosition;
// Fill neutron displacement but only for x and y
histogram[dataSet + 1][signalSet][direction].Fill(displacement, weight);
}
// Check for live neighbors in different directions based on which axis we're filling
if (direction == X)
{
posDirection = CheckNeighbor(period, promptSegment, 'r');
negDirection = CheckNeighbor(period, promptSegment, 'l');
}
else if (direction == Y)
{
posDirection = CheckNeighbor(period, promptSegment, 'u');
negDirection = CheckNeighbor(period, promptSegment, 'd');
}
else if (direction == Z && !isDisplacement) // Fill Z with same segment events
{
double displacement = delayedPosition - promptPosition;
histogram[dataSet][signalSet][direction].Fill(displacement, weight);
}
// Dataset + 1 returns the unbiased version of that dataset
if (!isDisplacement)
{
if (posDirection && !negDirection)
{
histogram[dataSet + 1][signalSet][direction].Fill(1.0, weight);
if (dataSet == PerfSim && direction == X)
nxPlusPlusCounter[promptSegment]++;
else if (dataSet == PerfSim && direction == Y)
nyPlusPlusCounter[promptSegment]++;
}
else if (!posDirection && negDirection)
{
histogram[dataSet + 1][signalSet][direction].Fill(-1.0, weight);
if (dataSet == PerfSim && direction == X)
nxMinusMinusCounter[promptSegment]++;
else if (dataSet == PerfSim && direction == Y)
nyMinusMinusCounter[promptSegment]++;
}
else if (posDirection && negDirection)
{
histogram[dataSet + 1][signalSet][direction].Fill(0.0, weight);
if (dataSet == PerfSim && direction == X)
nxPlusMinusCounter[promptSegment]++;
else if (dataSet == PerfSim && direction == Y)
nyPlusMinusCounter[promptSegment]++;
}
}
}
void Directionality::SubtractBackgrounds()
{
/* IBD events = (Correlated - Accidental/100)_{reactor on} + (-livetimeOn/livetimeOff*Correlated +
livetimeOn/livetimeOff*Accidental/100)_{reactor off} */
// Defining variables for IBD background subtraction
double totalIBDs = 0, totalIBDErr = 0, effIBDs = 0;
// Doing n counts for comparison with/without background subtraction
double nPlus = 0, nPlusPlus = 0, nMinus = 0, nMinusMinus = 0, nPlusMinus = 0;
double nPlusError = 0, nPlusPlusError = 0, nMinusError = 0, nMinusMinusError = 0, nPlusMinusError = 0;
for (int dataset = DataUnbiased; dataset < DatasetSize; dataset += 2)
{
if (!NCOUNT_VERBOSITY)
continue;
for (int direction = X; direction < Z; direction++)
{
for (int signalSet = CorrelatedReactorOn; signalSet < SignalSize - 1; signalSet++)
{
// Grabbing data from filled bins, rest should be empty
nPlus = histogram[dataset][signalSet][direction].GetBinContent(296);
nPlusPlus = histogram[dataset][signalSet][direction].GetBinContent(152);
nMinus = histogram[dataset][signalSet][direction].GetBinContent(6);
nMinusMinus = histogram[dataset][signalSet][direction].GetBinContent(150);
nPlusMinus = histogram[dataset][signalSet][direction].GetBinContent(151);
nPlusError = histogram[dataset][signalSet][direction].GetBinError(296);
nPlusPlusError = histogram[dataset][signalSet][direction].GetBinError(152);
nMinusError = histogram[dataset][signalSet][direction].GetBinError(6);
nMinusMinusError = histogram[dataset][signalSet][direction].GetBinError(150);
nPlusMinusError = histogram[dataset][signalSet][direction].GetBinError(151);
cout << "N (with background) counts for: " << boldOn << DatasetToString(dataset) << " "
<< AxisToString(direction) << " " << SignalToString(signalSet) << ":\n";
cout << "N+: " << resetFormats << nPlus << '\n';
cout << boldOn << "N-: " << resetFormats << nMinus << '\n';
cout << boldOn << "N++: " << resetFormats << nPlusPlus << '\n';
cout << boldOn << "N--: " << resetFormats << nMinusMinus << '\n';
cout << boldOn << "N+-: " << resetFormats << nPlusMinus << '\n';
cout << "--------------------------------------------\n";
cout << "N (with background) count errors for: " << boldOn << DatasetToString(dataset) << " "
<< AxisToString(direction) << " " << SignalToString(signalSet) << ":\n";
cout << "N+: " << resetFormats << nPlusError << '\n';
cout << boldOn << "N-: " << resetFormats << nMinusError << '\n';
cout << boldOn << "N++: " << resetFormats << nPlusPlusError << '\n';
cout << boldOn << "N--: " << resetFormats << nMinusMinusError << '\n';
cout << boldOn << "N+-: " << resetFormats << nPlusMinusError << '\n';
cout << "--------------------------------------------\n";
}
}
}
for (int dataset = Data; dataset < DatasetSize; dataset++)
{
for (int direction = X; direction < DirectionSize; direction++)
{
string histogramName;
string data = DatasetToString(dataset);
histogramName = data + " Total Difference " + AxisToString(direction);
// Copying Correlated Reactor On to start
histogram[dataset][TotalDifference][direction] = TH1F(histogram[dataset][CorrelatedReactorOn][direction]);
histogram[dataset][TotalDifference][direction].SetNameTitle(histogramName.c_str(), data.c_str());
histogram[dataset][TotalDifference][direction].Add(&histogram[dataset][AccidentalReactorOn][direction], -1. / 100);
if (dataset == Data || dataset == DataUnbiased)
{
histogram[dataset][TotalDifference][direction].Add(&histogram[dataset][CorrelatedReactorOff][direction],
-livetimeOn * atmosphericScaling / livetimeOff);
histogram[dataset][TotalDifference][direction].Add(&histogram[dataset][AccidentalReactorOff][direction],
livetimeOn * atmosphericScaling / (100 * livetimeOff));
}
totalIBDs = histogram[dataset][TotalDifference][direction].IntegralAndError(
0, histogram[dataset][TotalDifference][direction].GetNbinsX() + 1, totalIBDErr);
effIBDs = pow(totalIBDs, 2) / pow(totalIBDErr, 2); // Effective IBD counts. Done by Poisson Distribution
// N^2/(sqrt(N)^2) = N; Eff. counts = counts^2/counts_err^2
totalIBD[dataset][direction] = totalIBDs;
totalIBDError[dataset][direction] = totalIBDErr;
if (direction == Z)
{
effectiveIBD[dataset][direction] = effIBDs;
continue;
}
if (dataset == Data || dataset == Sim || dataset == PerfSim)
{
mean[dataset][direction] = histogram[dataset][TotalDifference][direction].GetMean();
sigma[dataset][direction] = histogram[dataset][TotalDifference][direction].GetStdDev() / sqrt(effIBDs);
effectiveIBD[dataset][direction] = effIBDs;
}
else
{
effectiveIBD[dataset][direction] = effIBDs;
}
}
if (dataset == DataUnbiased || dataset == SimUnbiased)
continue;
// Z is fit to a Guassian and only takes same segment inputs
// Possible thanks to 1mm resolution in Z
TF1 gaussian("Fit", "gaus", -140, 140);
histogram[dataset][TotalDifference][Z].Fit("Fit", "RQ");
float zMean = gaussian.GetParameter(1);
float zError = gaussian.GetParError(1);
mean[dataset][Z] = zMean;
sigma[dataset][Z] = zError;
// Deleting fit because we don't want the plot options stuck here
delete histogram[dataset][TotalDifference][Z].GetListOfFunctions()->FindObject("Fit");
}
// Printing out values
if (IBDCOUNT_VERBOSITY)
{
for (int dataset = Data; dataset < DatasetSize; dataset++)
{
cout << "Total and Effective IBD Events for: " << boldOn << DatasetToString(dataset) << resetFormats << '\n';
for (int direction = X; direction < DirectionSize; direction++)
{
cout << boldOn << AxisToString(direction) << ": " << resetFormats << totalIBD[dataset][direction] << " ± "
<< totalIBDError[dataset][direction] << boldOn << ". Effective IBD counts: " << resetFormats
<< effectiveIBD[dataset][direction] << ".\n";
}
cout << "--------------------------------------------\n";
}
}
cout << boldOn << cyanOn << "Subtracted backgrounds.\n" << resetFormats;
cout << "--------------------------------------------\n";
CalculateUnbiasing();
}
void Directionality::CalculateUnbiasing()
{
// Defining variables used in calculation. Check the error propagation technote for details on the method
double rPlus = 0, rMinus = 0;
double p = 0, pError = 0;
double nPlus = 0, nPlusPlus = 0, nMinus = 0, nMinusMinus = 0, nPlusMinus = 0;
double nPlusError = 0, nPlusPlusError = 0, nMinusError = 0, nMinusMinusError = 0, nPlusMinusError = 0;
for (int dataset = DataUnbiased; dataset < DatasetSize; dataset += 2)
{
for (int direction = X; direction < Z; direction++)
{
// Dataset - 1 returns the biased dataset counts
// Grabbing data from filled bins, rest should be empty
nPlus = histogram[dataset][TotalDifference][direction].GetBinContent(296);
nPlusPlus = histogram[dataset][TotalDifference][direction].GetBinContent(152);
nMinus = histogram[dataset][TotalDifference][direction].GetBinContent(6);
nMinusMinus = histogram[dataset][TotalDifference][direction].GetBinContent(150);
nPlusMinus = histogram[dataset][TotalDifference][direction].GetBinContent(151);
nPlusError = histogram[dataset][TotalDifference][direction].GetBinError(296);
nPlusPlusError = histogram[dataset][TotalDifference][direction].GetBinError(152);
nMinusError = histogram[dataset][TotalDifference][direction].GetBinError(6);
nMinusMinusError = histogram[dataset][TotalDifference][direction].GetBinError(150);
nPlusMinusError = histogram[dataset][TotalDifference][direction].GetBinError(151);
rPlus = nPlus / (nPlusPlus + nPlusMinus);
rMinus = nMinus / (nMinusMinus + nPlusMinus);
p = segmentWidth * (rPlus - rMinus) / (rPlus + rMinus + 1);
pError
= segmentWidth
* pow(1 / ((nMinus * (nPlusMinus + nPlusPlus) + (nMinusMinus + nPlusMinus) * (nPlus + nPlusMinus + nPlusPlus))),
2)
* sqrt(
pow((nMinusMinus + nPlusMinus) * (nPlusMinus + nPlusPlus), 2)
* (pow(nPlusError * (2 * nMinus + nMinusMinus + nPlusMinus), 2)
+ pow(nMinusError * (2 * nPlus + nPlusPlus + nPlusMinus), 2))
+ pow((nPlus * (nPlusMinus + nMinusMinus) * (2 * nMinus + nMinusMinus + nPlusMinus) * nPlusPlusError), 2)
+ pow((nPlusMinusError
* (nPlus * pow((nMinusMinus + nPlusMinus), 2)
+ nMinus * (2 * nMinusMinus * nPlus - 2 * nPlus * nPlusPlus - pow((nPlusMinus + nPlusPlus), 2)))),
2)
+ pow((nMinus * (nPlusMinus + nPlusPlus) * (2 * nPlus + nPlusMinus + nPlusPlus) * nMinusMinusError), 2));
mean[dataset][direction] = p;
sigma[dataset][direction] = pError;
if (NCOUNT_VERBOSITY)
{
cout << "N counts for: " << boldOn << DatasetToString(dataset) << " " << AxisToString(direction) << ":\n";
cout << "N+: " << resetFormats << nPlus << '\n';
cout << boldOn << "N-: " << resetFormats << nMinus << '\n';
cout << boldOn << "N++: " << resetFormats << nPlusPlus << '\n';
cout << boldOn << "N--: " << resetFormats << nMinusMinus << '\n';
cout << boldOn << "N+-: " << resetFormats << nPlusMinus << '\n';
cout << "--------------------------------------------\n";
cout << "N count errors for: " << boldOn << DatasetToString(dataset) << " " << AxisToString(direction) << ":\n";
cout << "N+: " << resetFormats << nPlusError << '\n';
cout << boldOn << "N-: " << resetFormats << nMinusError << '\n';
cout << boldOn << "N++: " << resetFormats << nPlusPlusError << '\n';
cout << boldOn << "N--: " << resetFormats << nMinusMinusError << '\n';
cout << boldOn << "N+-: " << resetFormats << nPlusMinusError << '\n';
cout << "--------------------------------------------\n";
}
double stdDev = (segmentWidth / (rPlus + rMinus + 1)) * sqrt(4 * rPlus * rMinus + rPlus + rMinus);
cout << "Standard Deviation for " << boldOn << DatasetToString(dataset) << " " << AxisToString(direction);
cout << resetFormats << " " << stdDev << "\n";
cout << "--------------------------------------------\n";
}
effectiveIBD[dataset][Z] = effectiveIBD[dataset - 1][Z];
mean[dataset][Z] = mean[dataset - 1][Z];
sigma[dataset][Z] = sigma[dataset - 1][Z];
}
cout << boldOn << cyanOn << "Calculated Means.\n" << resetFormats;
cout << "--------------------------------------------\n";
// Printing out values
if (MEAN_VERBOSITY)
{
for (int dataset = Data; dataset < DatasetSize; dataset++)
{
cout << "Mean and sigma values for: " << boldOn << DatasetToString(dataset) << resetFormats << '\n';
for (int direction = X; direction < DirectionSize; direction++)
{
cout << boldOn << "p" << AxisToString(direction) << ": " << resetFormats << mean[dataset][direction] << " ± "
<< sigma[dataset][direction] << '\n';
}
cout << "--------------------------------------------\n";
}
}
}
void Directionality::AddSystematics()
{
// Systematics extracted from BiPo study
// Defining variables for readability of code
double sigmaX, sigmaY, sigmaZ;
for (int dataset = Data; dataset < DatasetSize; dataset++)
{
sigmaX = sigma[dataset][X];
sigmaY = sigma[dataset][Y];
sigmaZ = sigma[dataset][Z];
sigmaSystematics[dataset][X] = sqrt(pow(sigmaX, 2) + pow(0.25, 2) + pow(0.08, 2));
sigmaSystematics[dataset][Y] = sqrt(pow(sigmaY, 2) + pow(0.39, 2) + pow(0.08, 2));
sigmaSystematics[dataset][Z] = sqrt(pow(sigmaZ, 2) + pow(0.06, 2) + pow(0.06, 2));
}
cout << boldOn << cyanOn << "Added Systematics.\n" << resetFormats;
cout << "--------------------------------------------\n";
// Printing out values
if (SYSTEMATIC_MEAN_VERBOSITY)
{
for (int dataset = Data; dataset < DatasetSize; dataset++)
{
cout << "Mean and sigma values for: " << boldOn << DatasetToString(dataset) << resetFormats << '\n';
for (int direction = X; direction < DirectionSize; direction++)
{
cout << boldOn << "p" << AxisToString(direction) << ": " << resetFormats << mean[dataset][direction] << " ± "
<< sigmaSystematics[dataset][direction] << '\n';
}
cout << "--------------------------------------------\n";
}
}
}
void Directionality::CalculateAngles()
{
// Defining variables for readability of code
double px, py, pz;
double sigmaX, sigmaY, sigmaZ;
double sigmaXSystematics, sigmaYSystematics, sigmaZSystematics;
double effIBDX, effIBDY, effIBDZ;
for (int dataset = Data; dataset < DatasetSize; dataset++)
{
// Grabbing values from current dataset
px = mean[dataset][X];
py = mean[dataset][Y];
pz = mean[dataset][Z];
sigmaX = sigma[dataset][X];
sigmaY = sigma[dataset][Y];
sigmaZ = sigma[dataset][Z];
sigmaXSystematics = sigmaSystematics[dataset][X];
sigmaYSystematics = sigmaSystematics[dataset][Y];
sigmaZSystematics = sigmaSystematics[dataset][Z];
effIBDX = effectiveIBD[dataset][X];
effIBDY = effectiveIBD[dataset][Y];
effIBDZ = effectiveIBD[dataset][Z];
// phi = arctan(y / x)
double tanPhi = py / px;
double phiTemp = atan(tanPhi) * 180.0 / pi;
double tanPhiError = sqrt(pow((sigmaX * py) / pow(px, 2), 2) + pow(sigmaY / px, 2));
double phiErrorTemp = (tanPhiError / (1 + pow(tanPhi, 2))) * 180.0 / pi;
double tanPhiErrorSystematics = sqrt(pow((sigmaXSystematics * py) / pow(px, 2), 2) + pow(sigmaYSystematics / px, 2));
double phiErrorSystematicsTemp = (tanPhiErrorSystematics / (1 + pow(tanPhi, 2))) * 180.0 / pi;
// theta = arctan(z / sqrt(x^2 + y^2))
double tanTheta = pz / sqrt(pow(px, 2) + pow(py, 2));
double thetaTemp = atan(tanTheta) * 180.0 / pi;
double tanThetaError = sqrt((1 / (px * px + py * py))
* (pow((px * pz * sigmaX / (px * px + py * py)), 2)
+ pow((py * pz * sigmaY / (px * px + py * py)), 2) + pow(sigmaZ, 2)));
double thetaErrorTemp = (tanThetaError / (1 + pow(tanTheta, 2))) * 180.0 / pi;
double tanThetaErrorSystematics
= sqrt((1 / (px * px + py * py))
* (pow((px * pz * sigmaXSystematics / (px * px + py * py)), 2)
+ pow((py * pz * sigmaYSystematics / (px * px + py * py)), 2) + pow(sigmaZSystematics, 2)));
double thetaErrorSystematicsTemp = (tanThetaErrorSystematics / (1 + pow(tanTheta, 2))) * 180.0 / pi;
// Storing values
phi[dataset] = phiTemp;
phiError[dataset] = phiErrorTemp;
phiErrorSystematics[dataset] = phiErrorSystematicsTemp;
theta[dataset] = thetaTemp;
thetaError[dataset] = thetaErrorTemp;
thetaErrorSystematics[dataset] = thetaErrorSystematicsTemp;
}
// Calculating "true" neutrino direction
// Based on Figure 1, https://doi.org/10.1103/PhysRevD.103.032001
float xTrue = -5.97, yTrue = -5.09, zTrue = 1.19;
float xTrueError = 0.1, yTrueError = 0.1, zTrueError = 0.1;
// Scaling by average prompt location
xTrue += 50.99 / 1000;
yTrue -= 23.31 / 1000;
zTrue -= 0.33 / 1000;
// Same angle calculation as above
float tanPhiTrue = yTrue / xTrue;
phiTrue = atan(tanPhiTrue) * 180.0 / pi;
float tanPhiTrueError = sqrt(pow((yTrue * xTrueError) / (xTrue * xTrue), 2) + pow(yTrueError / xTrue, 2));
phiTrueError = tanPhiTrueError / (1 + pow(tanPhiTrue, 2)) * 180.0 / pi;
float tanThetaTrue = zTrue / sqrt(pow(xTrue, 2) + pow(yTrue, 2));
thetaTrue = atan(tanThetaTrue) * 180.0 / pi;
float tanThetaTrueError
= sqrt(pow(1 / sqrt(xTrue * xTrue + yTrue * yTrue), 2)
* (pow(xTrue * zTrue / (xTrue * xTrue + yTrue * yTrue) * xTrueError, 2)
+ pow(yTrue * zTrue / (xTrue * xTrue + yTrue * yTrue) * yTrueError, 2) + pow(zTrueError, 2)));
thetaTrueError = tanThetaTrueError / (1 + pow(tanPhiTrue, 2)) * 180.0 / pi;
}
void Directionality::CalculateCovariances()
{
// Calculating covariances
array<array<float, 2>, 2> covarianceMatrix;
array<array<float, 2>, 2> covarianceMatrixSystematics;
// Defining variables for readability
float px, py, pz;
float sigmaX, sigmaY, sigmaZ;
float sigmaXSystematics, sigmaYSystematics, sigmaZSystematics;
float phiTemp, thetaTemp;
// From error propagation document
for (int dataset = Data; dataset < DatasetSize; dataset++)
{
// Grabbing values of current dataset from struct
px = mean[dataset][X];
py = mean[dataset][Y];
pz = mean[dataset][Z];
sigmaX = sigma[dataset][X];
sigmaY = sigma[dataset][Y];
sigmaZ = sigma[dataset][Z];
sigmaXSystematics = sigmaSystematics[dataset][X];
sigmaYSystematics = sigmaSystematics[dataset][Y];
sigmaZSystematics = sigmaSystematics[dataset][Z];
phiTemp = phi[dataset];
thetaTemp = theta[dataset];
// Filling out first matrix
covarianceMatrix[0][0] = (pow(sigmaX, 2) * pow(py, 2) / (pow(px, 4))) + pow(sigmaY, 2) / pow(px, 2);
covarianceMatrix[0][1] = (pow(pz, 2) / (px * pow(pow(px, 2) + pow(py, 2), (3. / 2.))))
* (pow(sigmaY, 2) - pow(sigmaX, 2));
covarianceMatrix[1][0] = ((py * pz) / (px * pow(pow(px, 2) + pow(py, 2), (3. / 2.)))) * (pow(sigmaY, 2) - pow(sigmaX, 2));
covarianceMatrix[1][1] = ((pow(px, 2) * pow(pz, 2) * pow(sigmaX, 2) + pow(py, 2) * pow(pz, 2) * pow(sigmaY, 2))
/ (pow(pow(px, 2) + pow(py, 2), 3)))
+ (pow(sigmaZ, 2)) / (pow(px, 2) + pow(py, 2));
covarianceMatrixSystematics[0][0] = (pow(sigmaXSystematics, 2) * pow(py, 2) / (pow(px, 4)))
+ pow(sigmaYSystematics, 2) / pow(px, 2);
covarianceMatrixSystematics[0][1] = (pow(pz, 2) / (px * pow(pow(px, 2) + pow(py, 2), (3. / 2.))))
* (pow(sigmaYSystematics, 2) - pow(sigmaXSystematics, 2));
covarianceMatrixSystematics[1][0] = ((py * pz) / (px * pow(pow(px, 2) + pow(py, 2), (3. / 2.))))
* (pow(sigmaYSystematics, 2) - pow(sigmaXSystematics, 2));
covarianceMatrixSystematics[1][1]
= ((pow(px, 2) * pow(pz, 2) * pow(sigmaXSystematics, 2) + pow(py, 2) * pow(pz, 2) * pow(sigmaYSystematics, 2))
/ (pow(pow(px, 2) + pow(py, 2), 3)))
+ (pow(sigmaZSystematics, 2)) / (pow(px, 2) + pow(py, 2));
// Including angles
covarianceMatrix[0][0] = covarianceMatrix[0][0] * pow(cos(phiTemp * pi / 180), 4);
covarianceMatrix[0][1] = covarianceMatrix[0][1] * pow(cos(phiTemp * pi / 180), 2) * pow(cos(thetaTemp * pi / 180), 2);
covarianceMatrix[1][0] = covarianceMatrix[1][0] * pow(cos(phiTemp * pi / 180), 2) * pow(cos(thetaTemp * pi / 180), 2);
covarianceMatrix[1][1] = covarianceMatrix[1][1] * pow(cos(thetaTemp * pi / 180), 4);
covarianceMatrixSystematics[0][0] = covarianceMatrixSystematics[0][0] * pow(cos(phiTemp * pi / 180), 4);
covarianceMatrixSystematics[0][1] = covarianceMatrixSystematics[0][1] * pow(cos(phiTemp * pi / 180), 2)
* pow(cos(thetaTemp * pi / 180), 2);
covarianceMatrixSystematics[1][0] = covarianceMatrixSystematics[1][0] * pow(cos(phiTemp * pi / 180), 2)
* pow(cos(thetaTemp * pi / 180), 2);
covarianceMatrixSystematics[1][1] = covarianceMatrixSystematics[1][1] * pow(cos(thetaTemp * pi / 180), 4);
// Eigenvalues
float a = covarianceMatrix[0][0], aSystematics = covarianceMatrixSystematics[0][0];
float b = covarianceMatrix[0][1], bSystematics = covarianceMatrixSystematics[0][1];
float c = covarianceMatrix[1][0], cSystematics = covarianceMatrixSystematics[1][0];
float d = covarianceMatrix[1][1], dSystematics = covarianceMatrixSystematics[1][1];
float lambda1 = ((a + d) + sqrt(pow(a, 2) - 2 * a * d + 4 * b * c + pow(d, 2))) / 2;
float lambda2 = ((a + d) - sqrt(pow(a, 2) - 2 * a * d + 4 * b * c + pow(d, 2))) / 2;
float lambda1Sytematics = ((aSystematics + dSystematics)
+ sqrt(pow(aSystematics, 2) - 2 * aSystematics * dSystematics
+ 4 * bSystematics * cSystematics + pow(dSystematics, 2)))
/ 2;
float lambda2Sytematics = ((aSystematics + dSystematics)
- sqrt(pow(aSystematics, 2) - 2 * aSystematics * dSystematics
+ 4 * bSystematics * cSystematics + pow(dSystematics, 2)))
/ 2;
// Eigenvector to calculate tilt
float vector1_1 = lambda1 - d;
float vector1_2 = c;
float normalizer = 1.0 / c;
vector1_1 *= normalizer;
vector1_2 *= normalizer;
float tiltTemp = atan(vector1_1) * 180.0 / pi;
tiltTemp += 90;
float vector1_1Systematics = lambda1Sytematics - dSystematics;
float vector1_2Systematics = cSystematics;
float normalizerSystematics = 1.0 / cSystematics;
vector1_1Systematics *= normalizerSystematics;
vector1_2Systematics *= normalizerSystematics;
float tiltSystematicsTemp = atan(vector1_1Systematics) * 180.0 / pi;
tiltSystematicsTemp += 90;
// Calculating final angle errors
float phiErrorTemp = sqrt(2.291 * lambda1) * 180.0 / pi;
float thetaErrorTemp = sqrt(2.291 * lambda2) * 180.0 / pi;
float phiErrorSystematicsTemp = sqrt(2.291 * lambda1Sytematics) * 180.0 / pi;
float thetaErrorSystematicsTemp = sqrt(2.291 * lambda2Sytematics) * 180.0 / pi;
// Filling array
phiEllipseError[dataset] = phiErrorTemp;
phiEllipseErrorSystematics[dataset] = phiErrorSystematicsTemp;
thetaEllipseError[dataset] = thetaErrorTemp;
thetaEllipseErrorSystematics[dataset] = thetaErrorSystematicsTemp;
tilt[dataset] = tiltTemp;
tiltSystematics[dataset] = tiltSystematicsTemp;
}
// Prints out the 1 sigma values if COVARIANCE_VERBOSITY is set to 1
// Change at top of file
if (COVARIANCE_VERBOSITY)
{
for (int dataset = Data; dataset < DatasetSize; dataset++)
{
cout << "The 1 sigma ellipse for: " << boldOn << DatasetToString(dataset) << resetFormats << " with systematics.\n";
cout << greenOn;
cout << boldOn << underlineOn << "Phi:" << resetFormats << greenOn << " " << phi[dataset] << "\u00B0 ± "
<< phiEllipseErrorSystematics[dataset] << "\u00B0.\n";
cout << boldOn << underlineOn << "Theta:" << resetFormats << greenOn << " " << theta[dataset] << "\u00B0 ± "
<< thetaEllipseErrorSystematics[dataset] << "\u00B0.\n";
cout << boldOn << underlineOn << "Tilt:" << resetFormats << greenOn << " " << tiltSystematics[dataset] << "\u00B0.\n"
<< resetFormats;
cout << "--------------------------------------------\n";
cout << "The 1 sigma ellipse for: " << boldOn << DatasetToString(dataset) << resetFormats
<< " without systematics.\n";
cout << greenOn;
cout << boldOn << underlineOn << "Phi:" << resetFormats << greenOn << " " << phi[dataset] << "\u00B0 ± "
<< phiEllipseError[dataset] << "\u00B0.\n";
cout << boldOn << underlineOn << "Theta:" << resetFormats << greenOn << " " << theta[dataset] << "\u00B0 ± "
<< thetaEllipseError[dataset] << "\u00B0.\n";
cout << boldOn << underlineOn << "Tilt:" << resetFormats << greenOn << " " << tilt[dataset] << "\u00B0.\n"
<< resetFormats;
cout << "--------------------------------------------\n";
}
}
// Final cone of uncertainty
float a = phiEllipseError[DataUnbiased];
float b = thetaEllipseError[DataUnbiased];
thetaTemp = 90 - theta[DataUnbiased];
float solidAngle = pi * a * b * sin(thetaTemp * pi / 180.0);
float solidAngleRadians = solidAngle * pow((pi / 180.0), 2);
float coneAngle = acos(1 - (solidAngleRadians / (2 * pi))) * 180.0 / pi;
cout << boldOn << greenOn << underlineOn << "Cone of Uncertainty:" << resetFormats << greenOn << " " << coneAngle << "\u00B0"
<< '\n'
<< resetFormats;
cout << "--------------------------------------------\n";
}
void Directionality::OffsetTheta()
{
// We measure theta from the z-axis down to the neutrino vector
for (int dataset = Data; dataset < DatasetSize; dataset++)
{
theta[dataset] = 90 - theta[dataset];
}
thetaTrue = 90 + thetaTrue;
}
void Directionality::PrintAngles()
{
cout << boldOn << cyanOn << "Final Angles!\n" << resetFormats;
cout << "--------------------------------------------\n";
for (int dataset = Data; dataset < DatasetSize; dataset++)
{
float phiErrorTemp, thetaErrorTemp;
if (dataset == Sim || dataset == SimUnbiased || dataset == PerfSim || dataset == PerfSimUnbiased || ANGLES_STATISTICS)
{
phiErrorTemp = phiError[dataset];
thetaErrorTemp = thetaError[dataset];
}
else
{
phiErrorTemp = phiErrorSystematics[dataset];