Heavy ion Analysis Libriares
Loading...
Searching...
No Matches
ErrorCalc.cxx
1/*
2 * StdError.cxx
3 *
4 * Created on: 21 paź 2023
5 * Author: Daniel Wielanek
6 * E-mail: daniel.wielanek@gmail.com
7 * Warsaw University of Technology, Faculty of Physics
8 */
9#include "ErrorCalc.h"
10
11#include "Cout.h"
12
13#include <iostream>
14namespace Hal {
15
16 Double_t ErrorCalc::GetStatError() const { return fStatisticalUncert; }
17
18 Double_t ErrorCalc::BarlowTest(Int_t prec) const {
19 Double_t totalErr = 0;
20 Hal::Cout::Database({"Name", "Value"});
21 TString flag = Form("%%4.%if", prec);
22 auto quadDif = [&](Double_t b) { return TMath::Abs(fStatisticalUncert * fStatisticalUncert - b * b); };
23 auto measDif = [&](Double_t b) { return TMath::Abs(fMeasurement - b); };
24 auto getForm = [&](Double_t val) { return TString(Form(flag, val)); };
25 for (auto i : fValues) {
26 auto uncerts = i.second; // array of all uncerts
27 Double_t uncertSum = 0;
28 Double_t m = 0;
29 for (auto j : uncerts) { // all measurements
30 Double_t xn = j.first;
31 Double_t en = j.second;
32 Double_t deltaSysj = TMath::Power(measDif(xn), 2) - quadDif(en);
33 if (deltaSysj < 0) {
34 deltaSysj = 0; // does not contribute to uncert
35 } else {
36 uncertSum += deltaSysj * deltaSysj;
37 m++;
38 }
39 }
40 if (m == 0) m = 1;
41 Double_t sigmaSysi = TMath::Sqrt(uncertSum / m);
42 if (sigmaSysi > 0) {
43 std::cout << Hal::Cout::GetColor(kGreen);
44 } else {
45 std::cout << Hal::Cout::GetColor(kRed);
46 }
47
48 totalErr += sigmaSysi * sigmaSysi;
49 Hal::Cout::Database({i.first, getForm(sigmaSysi)});
50 }
51
52 totalErr = TMath::Sqrt(totalErr);
53 std::cout << Hal::Cout::GetColor(kBlue);
54 Hal::Cout::Database({"Total", getForm(totalErr)});
55 std::cout << Hal::Cout::GetDisableColor();
56 return totalErr;
57 }
58
59 ErrorCalc::ErrorCalc(TString name, Double_t val, Double_t err) {
60 SetName(name);
61 fMeasurement = val;
62 fStatisticalUncert = err;
63 }
64
65 std::vector<std::pair<Double_t, Double_t>> ErrorCalc::GetAllUncerts(TString name) const {
66 std::vector<std::pair<Double_t, Double_t>> vals;
67 for (auto i : fValues) {
68 if (i.first == name) { return i.second; }
69 }
70 return vals;
71 }
72
73 void ErrorCalc::AddSysError(TString name, Double_t value, Double_t statUncert) {
74 if (statUncert == -1) {
75 statUncert = TMath::Abs(fMeasurement - value);
76 } else if (statUncert < 0) {
77 statUncert = fStatisticalUncert;
78 }
79 std::pair<Double_t, Double_t> p(value, statUncert);
80 for (auto& i : fValues) {
81 if (i.first == name) {
82 i.second.push_back(p);
83 return;
84 }
85 }
86 std::vector<std::pair<Double_t, Double_t>> vec;
87 vec.push_back(p);
88 std::pair<TString, std::vector<std::pair<Double_t, Double_t>>> pair(name, vec);
89 fValues.push_back(pair);
90 }
91
92 Double_t ErrorCalc::TotalSys(Int_t prec) const {
93 Double_t total = 0;
94 TString flag = Form("%%4.%if", prec);
95 auto getForm = [&](Double_t val) { return TString(Form(flag, val)); };
96 Hal::Cout::Database({"Name", "Value"});
97 for (auto i : fValues) {
98 auto errors = GetAllUncerts(i.first);
99 Double_t maxErr = 0;
100 for (auto j : errors) {
101 maxErr = TMath::Max(maxErr, TMath::Abs(fMeasurement - j.first));
102 }
103 total += maxErr * maxErr;
104 Hal::Cout::Database({i.first, getForm(maxErr)});
105 }
106 std::cout << Hal::Cout::GetColor(kBlue);
107 Hal::Cout::Database({"Total", getForm(TMath::Sqrt(total))});
108 std::cout << Hal::Cout::GetDisableColor();
109 return TMath::Sqrt(total);
110 }
111
112 Double_t ErrorCalc::SumError(std::initializer_list<Double_t> errs) {
113 auto vec = Hal::Std::GetVector(errs);
114 double sq = 0;
115 for (auto el : vec) {
116 sq += el * el;
117 }
118 return TMath::Sqrt(sq);
119 }
120
121} /* namespace Hal */
static TString GetColor(Color_t Color)
Definition Cout.cxx:268
static TString GetDisableColor()
Definition Cout.cxx:261
static void Database(Int_t no...)
void AddSysError(TString name, Double_t value, Double_t uncert=-1)
Definition ErrorCalc.cxx:73
Double_t GetStatError() const
Definition ErrorCalc.cxx:16
Double_t BarlowTest(Int_t prec=4) const
Definition ErrorCalc.cxx:18
ErrorCalc(TString name="", Double_t val=0, Double_t err=0)
Definition ErrorCalc.cxx:59
Double_t TotalSys(Int_t prec=4) const
Definition ErrorCalc.cxx:92
static Double_t SumError(std::initializer_list< Double_t > errs)