Heavy ion Analysis Libriares
Loading...
Searching...
No Matches
StdMath.cxx
1/*
2 * Hal::StdMath.cxx
3 *
4 * Created on: 16 lip 2020
5 * Author: Daniel Wielanek
6 * E-mail: daniel.wielanek@gmail.com
7 * Warsaw University of Technology, Faculty of Physics
8 */
9#include "StdMath.h"
10#include "Cout.h"
11
12#include <TH1.h>
13#include <TH2.h>
14#include <TMatrixD.h>
15
16NamespaceImp(Hal::Std)
17
18 ;
19namespace Hal {
20 namespace Std {
21
22 void FitParabola(Double_t x1,
23 Double_t x2,
24 Double_t x3,
25 Double_t y1,
26 Double_t y2,
27 Double_t y3,
28 Double_t& a,
29 Double_t& b,
30 Double_t& c) {
31 TMatrixD AM(3, 3);
32 TMatrixD BM(3, 1);
33 AM[0][0] = 1;
34 AM[0][1] = x1;
35 AM[0][2] = x1 * x1;
36 AM[1][0] = 1;
37 AM[1][1] = x2;
38 AM[1][2] = x2 * x2;
39 AM[2][0] = 1;
40 AM[2][1] = x3;
41 AM[2][2] = x3 * x3;
42
43 BM[0][0] = y1;
44 BM[1][0] = y2;
45 BM[2][0] = y3;
46
47 TMatrixD SOL(3, 1);
48 AM = AM.InvertFast();
49
50 SOL = AM * BM;
51
52 c = SOL[0][0];
53 b = SOL[1][0];
54 a = SOL[2][0];
55 }
56
57 Int_t SolveParabola(Double_t a, Double_t b, Double_t c, Double_t& x1, Double_t& x2) {
58 Double_t Delta = b * b - 4.0 * a * c;
59 if (Delta < 0) return 0;
60 if (Delta == 0) {
61 x1 = x2 = -b / (2.0 * a);
62 return 1;
63 }
64 Delta = TMath::Sqrt(Delta);
65 x1 = (-b - Delta) / (2.0 * a);
66 x2 = (-b + Delta) / (2.0 * a);
67 return 2;
68 }
69
70 Int_t Bin3dToBin1d(Int_t nbinsX, Int_t nBinsY, Int_t binX, Int_t binY, Int_t binZ, Bool_t root) {
71 if (root) {
72 return (binX - 1) * nbinsX * nBinsY + (binY - 1) * nBinsY + binZ - 1;
73 } else {
74 return binX * nbinsX * nBinsY + binY * nBinsY + binZ;
75 }
76 }
77
78 TVector3 Bin1dToBin3d(Int_t nbinsX, Int_t nBinsY, Int_t bin, Bool_t root) {
79 Int_t XY = nBinsY * nbinsX;
80 Int_t alpha = TMath::Floor(bin / XY);
81 Int_t beta = TMath::Floor((bin - alpha * XY) / nbinsX);
82 Int_t gamma = bin - alpha * XY - beta * nbinsX;
83 if (root) {
84 return TVector3(alpha + 1, beta + 1, gamma + 1);
85 } else {
86 return TVector3(alpha, beta, gamma);
87 }
88 }
89
90 TMatrixD GetVec(const TH1& h, Bool_t horizontal) {
91 if (horizontal) {
92 TMatrixD vect(1, h.GetNbinsX());
93 for (int i = 1; i <= h.GetNbinsX(); i++) {
94 vect[0][i - 1] = h.GetBinContent(i);
95 }
96 return vect;
97 } else {
98 TMatrixD vect(h.GetNbinsX(), 1);
99 for (int i = 1; i <= h.GetNbinsX(); i++) {
100 vect[i - 1][0] = h.GetBinContent(i);
101 }
102 return vect;
103 }
104 }
105
106 TMatrixD GetMatrix(const TH2& h, Bool_t swap) {
107 int binX, binY;
108 double minX, minY, maxX, maxY;
109 Hal::Std::GetAxisPar(h, binX, minX, maxX, "x");
110 Hal::Std::GetAxisPar(h, binY, minY, maxY, "y");
111 if (binX != binY) {
112 Hal::Cout::PrintInfo("Cannot call Hal::Std::GetMatrix on non-square histogram", EInfo::kError);
113 return TMatrixD(1, 1);
114 }
115 if (minY != minX || maxX != maxY) {
116 Hal::Cout::PrintInfo("Wrong histogram Hal::Std::GetMatrix min/max are not equal", EInfo::kWarning);
117 }
118 TMatrixD vect(binX, binX);
119 for (int i = 1; i <= binX; i++) {
120 for (int j = 1; j <= binX; j++) {
121 if (swap) {
122 vect[binX - j][i - 1] = h.GetBinContent(i, j);
123 } else {
124 vect[j - 1][i - 1] = h.GetBinContent(i, j);
125 }
126 }
127 }
128 return vect;
129 }
130
131 Double_t Discretize(Int_t areas, Double_t min, Double_t max, Double_t val, Char_t type) {
132 Double_t step_size = (max - min) / ((Double_t) areas);
133 Double_t step = (val - min) / step_size;
134 if (areas <= 1) {
135 step = 0;
136 step_size = 0;
137 }
138 switch (type) {
139 case '+': {
140 step = TMath::Ceil(step);
141 } break;
142 case '-': {
143 step = TMath::Floor(step);
144 } break;
145 case '=': {
146 step = std::round(step);
147 } break;
148 default: return 0; break;
149 }
150 Double_t res = min + step * step_size;
151 if (res < min) return min;
152 if (res > max) return max;
153 return res;
154 }
155
156 Double_t StatError2Var(Char_t ope, Double_t x, Double_t y, Double_t dx, Double_t dy) {
157 if (dx < 0) dx = TMath::Sqrt(TMath::Abs(x));
158 if (dy < 0) dy = TMath::Sqrt(TMath::Abs(y));
159 Double_t dfx = 0, dfy = 0;
160 switch (ope) {
161 case '+': {
162 dfx = 1;
163 dfy = 1;
164 } break;
165 case '-': {
166 dfx = 1;
167 dfy = 1;
168 } break;
169 case '/': {
170 dfx = 1.0 / y;
171 dfy = x / y / y;
172 } break;
173 case '*': {
174 dfx = y;
175 dfy = x;
176 } break;
177 default: break;
178 }
179 return TMath::Sqrt(dfx * dfx * dx * dx + dfy * dfy * dy * dy);
180 }
181
182 Int_t MultiToOneDimIndex(const std::vector<int>& size, const std::vector<int>& position) {
183 if (size.size() != position.size()) return -1;
184 Int_t step = 1;
185 Int_t pos = 0;
186 for (int i = size.size() - 1; i >= 0; i--) {
187 pos = pos + position[i] * step;
188 step = step * size[i];
189 }
190 return pos;
191 }
192
193 std::vector<int> OneToMultiDimIndex(const std::vector<int>& size, Int_t n) {
194 std::vector<int> res(size.size());
195 for (int i = size.size() - 1; i >= 0; i--) {
196 Int_t pos = n % size[i];
197 n = (n - pos) / size[i];
198 res[i] = pos;
199 }
200 return res;
201 }
202
203 std::pair<Int_t, Int_t> Division(Int_t num, Int_t div) {
204 std::pair<Int_t, Int_t> res;
205 Int_t reminder = num % div;
206 res.second = reminder;
207 Int_t afdiv = (num - reminder) / div;
208 res.first = afdiv;
209 return res;
210 }
211 } // namespace Std
212} // namespace Hal
static void PrintInfo(TString text, Hal::EInfo status)
Definition Cout.cxx:370