Heavy ion Analysis Libriares
Loading...
Searching...
No Matches
Fluctuation1D.cxx
1/*
2 * FluctuationMath.cxx
3 *
4 * Created on: 31 sie 2017
5 * Author: Daniel Wielanek
6 * E-mail: daniel.wielanek@gmail.com
7 * Warsaw University of Technology, Faculty of Physics
8 */
9#include "Fluctuation1D.h"
10
11#include "Cout.h"
12#include "HtmlFile.h"
13#include "HtmlTable.h"
14
15#include <TCanvas.h>
16#include <TLatex.h>
17#include <TMath.h>
18#include <TSystem.h>
19#include <iostream>
20
21namespace Hal {
23 fN(0),
24 fNError(0),
25 fOmega(0),
26 fSSigma(0),
27 fKappaSigma(0),
28 fOmegaError(0),
29 fSigmaError(0),
30 fKappaError(0),
31 fNSample(10),
32 fHistogram(NULL) {
33 fM[0] = fM[1] = fM[2] = fM[3] = fM[4] = fM[5] = fM[6] = 0;
34 fMErr[0] = fMErr[1] = fMErr[2] = fMErr[3] = fMErr[4] = fMErr[5] = fMErr[6] = 0;
35 }
36
37 void Fluctuation1D::Calc(TH1D* h) {
38 fN = fM[0] = fM[1] = fM[2] = fM[3] = fM[4] = fM[5] = fM[6] = 0.0;
39 Double_t entries = h->GetEntries();
40 // fHisto->Scale(1.0/entries);
41 for (int i = 1; i <= h->GetNbinsX(); i++) {
42 Double_t pN = ((Double_t) h->GetBinContent(i));
43 Double_t N = (Double_t) h->GetBinCenter(i);
44 fN += N * pN;
45 }
46 fN = fN / entries;
47 for (int i = 1; i <= h->GetNbinsX(); i++) {
48 Double_t pN = ((Double_t) h->GetBinContent(i));
49 Double_t N = (Double_t) h->GetBinCenter(i);
50 for (int j = 1; j <= 6; j++) {
51 fM[j] += TMath::Power(N - fN, j) * pN;
52 }
53 }
54 for (int j = 1; j <= 6; j++) {
55 fM[j] = fM[j] / entries;
56 }
57 fOmega = GetCentralMoment(2) / fN;
58 fSSigma = GetCentralMoment(3) / GetCentralMoment(2);
59 fKappaSigma = GetCentralMoment(4) / GetCentralMoment(2) - 3.0 * GetCentralMoment(2);
60 }
61
62 Double_t Fluctuation1D::GetCentralMoment(Int_t i) const {
63 if (i < 1 || i > 6) {
64 Cout::PrintInfo("wrong mu index", EInfo::kLowWarning);
65 return 0;
66 } else {
67 return fM[i];
68 }
69 }
70
71 Double_t Fluctuation1D::GetCentralMomentError(Int_t i) const {
72 if (i < 1 || i > 6) {
73 Cout::PrintInfo("wrong mu index", EInfo::kLowWarning);
74 return 0;
75 } else {
76 return fMErr[i];
77 }
78 }
79
80 void Fluctuation1D::CalcError(TH1D* h) {
81 TH1D* hh = (TH1D*) h->Clone("temp_boostrap");
82 Fluctuation1D* stat = new Fluctuation1D();
83 Double_t* mean = new Double_t[fNSample];
84 Double_t* omega = new Double_t[fNSample];
85 Double_t* sigma = new Double_t[fNSample];
86 Double_t* kappa = new Double_t[fNSample];
87 Double_t** central_moment = new Double_t*[5];
88 for (int i = 0; i < 5; i++) {
89 central_moment[i] = new Double_t[fNSample];
90 }
91 for (int i = 0; i < fNSample; i++) {
92 hh->Reset();
93 for (int j = 0; j < h->GetEntries(); j++) {
94 hh->Fill(h->GetRandom());
95 }
96 stat->Calc(hh);
97 omega[i] = stat->GetOmega();
98 sigma[i] = stat->GetSSigma();
99 kappa[i] = stat->GetKappaSigma();
100 mean[i] = stat->GetMean();
101 for (int j = 2; j < 7; j++)
102 central_moment[j - 2][i] = stat->GetCentralMoment(j);
103 }
104 fOmegaError = GetRMS(omega, fNSample);
105 fSigmaError = GetRMS(sigma, fNSample);
106 fKappaError = GetRMS(kappa, fNSample);
107 fNError = GetRMS(mean, fNSample);
108 for (int i = 2; i < 7; i++) {
109 fMErr[i] = GetRMS(central_moment[i - 2], fNSample);
110 }
111 for (int i = 0; i < 5; i++) {
112 delete[] central_moment[i];
113 }
114 delete[] central_moment;
115 delete[] omega;
116 delete[] kappa;
117 delete[] sigma;
118 delete[] mean;
119 delete stat;
120 delete hh;
121 }
122
124 if (h->InheritsFrom("TH2")) {
125 Cout::PrintInfo("Cannot call Fluctuation1D(TH2D*)", EInfo::kLowWarning);
126 return;
127 }
128 fHistogram = (TH1D*) h->Clone();
129 Recalculate();
130 }
131
132 Fluctuation1D::Fluctuation1D(TH2D* h, TString opt) : Fluctuation1D() {
133 if (opt.EqualTo("x") || opt.EqualTo("pos")) {
134 fHistogram = h->ProjectionX(Form("%s_pos", h->GetName()));
135 } else if (opt.EqualTo("y") || opt.EqualTo("neg")) {
136 fHistogram = h->ProjectionY(Form("%s_neg", h->GetName()));
137 } else if (opt.EqualTo("sum") || opt.EqualTo("+")) {
138 fHistogram = CalcProfile(h, 1);
139 } else {
140 fHistogram = CalcProfile(h, 0);
141 }
142 Recalculate();
143 }
144
145 TString Fluctuation1D::HTMLExtract(Int_t no, TString dir) const {
146 TString path = Form("%s/fluct_%i", dir.Data(), no);
147 gSystem->MakeDirectory(path);
148 TString filename = Form("%s/fluct.html", path.Data());
149 HtmlFile file(filename, kFALSE);
150 HtmlTable table("", "haltable", "");
151 HtmlRow row1("", "dark_blue", "");
152 row1.AddContent(HtmlCell("label"));
153 row1.AddContent(HtmlCellCol("value", 2));
154 table.AddContent(row1);
155 HtmlRow row2("", "dark_blue", "");
156 row2.AddContent(HtmlCell("Name"));
157 row2.AddContent(HtmlCellCol(GetName(), 2));
158 table.AddContent(row2);
159 HtmlRow row3("", "dark_blue", "");
160 row2.AddContent(HtmlCell("NSample"));
161 row2.AddContent(HtmlCellCol(Form("%i", fNSample), 2));
162 table.AddContent(row3);
163 file.AddContent(table);
164 HtmlTable table2("", "haltable", "");
165 HtmlRow row4("", "dark_blue", "");
166 row4.AddContent(HtmlCell("No."));
167 row4.AddContent(HtmlCell("Name/Value"));
168 table2.AddContent(row4);
169 file.AddContent(table2);
170 TCanvas* c1 = new TCanvas();
171 fHistogram->DrawClone();
172 TLatex latex;
173 latex.SetNDC(kTRUE);
174 latex.DrawLatex(0.5, 0.8, Form("#omega %4.3f #pm %4.3f", GetOmega(), GetOmegaError()));
175 latex.DrawLatex(0.5, 0.75, Form("#kappa#sigma^{2} %4.3f #pm %4.3f", GetKappaSigma(), GetKappaSigmaError()));
176 latex.DrawLatex(0.5, 0.7, Form("S#sigma %4.3f #pm %4.3f", GetSSigma(), GetSSigmaError()));
177 c1->SaveAs(Form("%s//fluct.root", path.Data()));
178 delete c1;
179 TString file_name = Form("%s/fluct.root", path.Data());
180 file.AddStringContent(HtmlCore::GetJsDiv("fluct.root", "canvas;1"));
181 file.Save();
182 return HtmlCore::GetUrl(Form("fluct_%i/fluct.html", no), this->ClassName());
183 }
184
185 void Fluctuation1D::Add(const Object* pack) {
186 if (pack->InheritsFrom("Hal::Fluctuation1D")) {
187 Fluctuation1D* other = (Fluctuation1D*) pack;
188 fHistogram->Add(other->fHistogram);
189 Recalculate();
190 } else {
191 Cout::PrintInfo(Form("Cannot ddd %s to %s", pack->ClassName(), this->ClassName()), EInfo::kLowWarning);
192 }
193 }
194
195 Long64_t Fluctuation1D::Merge(TCollection* collection) {
196 if (collection) {
197 Fluctuation1D* pack = NULL;
198 TIter iterator(collection);
199 while ((pack = (Fluctuation1D*) iterator())) {
200 fHistogram->Add(pack->fHistogram);
201 }
202 Recalculate();
203 }
204 return 1;
205 }
206
207 Double_t Fluctuation1D::GetRMS(Double_t* array, Int_t size) const {
208 Double_t average = 0;
209 Double_t n = size;
210 for (int i = 0; i < size; i++) {
211 average += array[i];
212 }
213 average = average / n;
214 Double_t rms = 0;
215 for (int i = 0; i < size; i++) {
216 Double_t dx = array[i] - average;
217 rms += dx * dx;
218 }
219 return TMath::Sqrt(rms / n);
220 }
221
222 TH1D* Fluctuation1D::CalcProfile(TH2D* h, Int_t opt) {
223 Double_t min_pos = h->GetXaxis()->GetBinLowEdge(1);
224 Double_t max_pos = h->GetXaxis()->GetBinUpEdge(h->GetXaxis()->GetNbins());
225 Double_t min_neg = h->GetYaxis()->GetBinLowEdge(1);
226 Double_t max_neg = h->GetYaxis()->GetBinUpEdge(h->GetYaxis()->GetNbins());
227 Double_t min, max;
228 if (opt == 0) { // difference
229 min = min_pos - max_neg;
230 max = max_pos - min_neg;
231 } else { // sum
232 min = min_pos + min_neg;
233 max = max_pos + max_neg;
234 }
235 min = TMath::Floor(min) - 0.5;
236 max = TMath::Ceil(max) + 0.5;
237
238 TH1D* temp = new TH1D("profile", "profile", (max - min), min, max);
239 for (int j = 1; j <= h->GetNbinsX(); j++) {
240 Double_t y = h->GetYaxis()->GetBinCenter(j);
241 if (opt == 0) y = -y;
242 for (int i = 1; i <= h->GetNbinsY(); i++) {
243 Double_t x = h->GetXaxis()->GetBinCenter(i);
244 Double_t n = h->GetBinContent(i, j);
245 temp->Fill(x + y, n);
246 }
247 }
248 return temp;
249 }
250
252 Calc(fHistogram);
253 CalcError(fHistogram);
254 }
255
256 void Fluctuation1D::Browse(TBrowser* b) {
257 gPad->Clear();
258 TVirtualPad* c1 = gPad;
259 Draw("all");
260 gPad = c1;
261 b->Add(fHistogram);
262 }
263
264 void Fluctuation1D::Draw(Option_t* opt) {
265 TString option = opt;
266 if (option == "all") {
267 fHistogram->SetStats(kFALSE);
268 fHistogram->Draw();
269 TLatex latex;
270 latex.SetNDC(kTRUE);
271 latex.DrawLatex(0.5, 0.8, Form("#omega %4.3f #pm %4.3f", GetOmega(), GetOmegaError()));
272 latex.DrawLatex(0.5, 0.75, Form("#kappa#sigma^{2} %4.3f #pm %4.3f", GetKappaSigma(), GetKappaSigmaError()));
273 latex.DrawLatex(0.5, 0.7, Form("S#sigma %4.3f #pm %4.3f", GetSSigma(), GetSSigmaError()));
274 } else {
275 fHistogram->Draw();
276 }
277 }
278
279 void Fluctuation1D::GetFrom2D(TH1D* /*h*/, TString /*opt*/) {}
280
281 void Fluctuation1D::Print(Option_t* /*option*/) const {
282 std::cout << "Fluctuation1D " << std::endl;
283 std::cout << Form("Mean\t\t:%4.3f+/-%4.3f", GetMean(), GetMeanError()) << std::endl;
284 std::cout << Form("KappaSigma\t\t:%4.3f+/-%4.3f", GetKappaSigma(), GetKappaSigmaError()) << std::endl;
285 std::cout << Form("Omega\t\t:%4.3f+/-%4.3f", GetOmega(), GetOmegaError()) << std::endl;
286 std::cout << Form("SSigma\t\t:%4.3f+/-%4.3f", GetSSigma(), GetSSigmaError()) << std::endl;
287 for (int i = 2; i < 7; i++) {
288 std::cout << Form("Central moment[%i]\t:%4.3f+/-%4.3f", i, GetCentralMoment(i), GetCentralMomentError(i)) << std::endl;
289 }
290 std::cout << "**********************" << std::endl;
291 }
292
293 Fluctuation1D::~Fluctuation1D() {
294 if (fHistogram) delete fHistogram;
295 }
296} // namespace Hal
static void PrintInfo(TString text, Hal::EInfo status)
Definition Cout.cxx:370
Double_t GetOmegaError() const
Double_t GetKappaSigma() const
virtual TString HTMLExtract(Int_t no, TString dir="") const
Double_t GetMeanError() const
virtual Long64_t Merge(TCollection *collection)
Double_t GetKappaSigmaError() const
Double_t GetSSigmaError() const
virtual void Add(const Object *pack)
Double_t GetSSigma() const
Double_t GetMean() const
Double_t GetCentralMomentError(Int_t i) const
Double_t GetCentralMoment(Int_t i) const
Double_t GetOmega() const
static TString GetUrl(TString adress, TString text)
Definition HtmlCore.cxx:164
static TString GetJsDiv(TString root_file, TString object_name, TString draw_opt="colz", TString draw_div_name="drawing")
Definition HtmlCore.cxx:226
virtual void AddContent(const HtmlObject &obj)
Definition HtmlTable.cxx:29
virtual void AddContent(const HtmlObject &obj)
Definition HtmlTable.cxx:17