Heavy ion Analysis Libriares
Loading...
Searching...
No Matches
CorrFitMapRebin.cxx
1/*
2 * CorrFitMapRebin.cxx
3 *
4 * Created on: 8 gru 2020
5 * Author: Daniel Wielanek
6 * E-mail: daniel.wielanek@gmail.com
7 * Warsaw University of Technology, Faculty of Physics
8 */
9#include "CorrFitMapRebin.h"
10
11#include "CorrFitInfo.h"
12#include "Cout.h"
13#include "Femto1DCF.h"
14#include "Femto3DCF.h"
15#include "FemtoSHCF.h"
16#include "StdMath.h"
17
18#include <TFile.h>
19#include <TTree.h>
20
21#include <iostream>
22
23namespace Hal {
24 CorrFitMapRebin::CorrFitMapRebin() :
25 fInFile(nullptr),
26 fOutFile(nullptr),
27 fInTree(nullptr),
28 fOutTree(nullptr),
29 fInfo(nullptr),
30 fRebinX(0),
31 fRebinY(0),
32 fRebinZ(0) {}
33
34 Bool_t CorrFitMapRebin::Compress(TString outFile) {
35 fInFile = new TFile(fInFileName, "read");
36 fInfo = (CorrFitInfo*) fInFile->Get("Info");
37 fInTree = (TTree*) fInFile->Get("map");
38 fOutFileName = outFile;
39 TObject* obj = fInfo->GetCf();
40 Femto3DCF* cf3d = dynamic_cast<Femto3DCF*>(obj);
41 Femto1DCF* cf1d = dynamic_cast<Femto1DCF*>(obj);
42 FemtoSHCF* cfsh = dynamic_cast<FemtoSHCF*>(obj);
43 if (cf3d != nullptr) {
44 return Rebin3D();
45 } else if (cfsh != nullptr) {
46 return Rebin1D();
47 } else if (cf1d != nullptr) {
48 return Rebin1D();
49 } else {
50 std::cout << "Unknown CF" << std::endl;
51 }
52
53 return kFALSE;
54 }
55
56 CorrFitMapRebin::~CorrFitMapRebin() {
57 if (fInFile) {
58 fInFile->Close();
59 delete fInFile;
60 }
61 if (fOutFile) {
62 fOutFile->Close();
63 delete fOutFile;
64 }
65 }
66
67 Bool_t CorrFitMapRebin::Rebin1D() {
68 DividedHisto1D* obj = (DividedHisto1D*) fInfo->GetCf();
69 Int_t sizeX = obj->GetNum()->GetNbinsX();
70 if (sizeX % fRebinX != 0) {
71 std::cout << Form("Cannot rebin X-axis in %s", obj->ClassName()) << std::endl;
72 return kFALSE;
73 }
74
75 TH1D* den = (TH1D*) obj->GetDen();
76
77 Array_1<Float_t>* data = nullptr;
78 fInTree->SetBranchAddress("data", &data);
79 fOutFile = new TFile(fOutFileName, "recreate");
80 fOutTree = new TTree("map", "");
81 Array_1<Float_t>* newData = new Array_1<Float_t>();
82 fOutTree->Branch("data", &newData);
83 fInTree->GetEntry(0);
84 newData->MakeBigger(data->GetSize() / fRebinX);
85
86 Array_1<Float_t> weight(sizeX);
87
88 Float_t* temp = new Float_t[fRebinX];
89 // Int_t newBins = sizeX / fRebinX;
90 for (int i = 0; i < sizeX / fRebinX; i++) {
91 Float_t sum = 0;
92 for (int j = 0; j < fRebinX; j++) {
93 temp[j] = den->GetBinContent(i * fRebinX + j);
94 sum += temp[j];
95 }
96 for (int j = 0; j < fRebinX; j++) {
97 weight[i * fRebinX + j] = temp[j] / sum;
98 if (sum == 0) weight[i * fRebinX + j] = 1.0 / ((Float_t) fRebinX);
99 }
100 }
101 delete[] temp;
102
103
104 for (int i = 0; i < fInTree->GetEntries(); i++) {
105 fInTree->GetEntry(i);
106 for (int j = 0; j < newData->GetSize(); j++) {
107 Float_t sum = 0;
108 for (int k = 0; k < fRebinX; k++) {
109 sum += (*data)[j * fRebinX + k] * weight[j * fRebinX + k];
110 }
111 (*newData)[j] = sum;
112 }
113 fOutTree->Fill();
114 }
115
116 fOutTree->Write();
117 CorrFitInfo* newInfo = new CorrFitInfo(*fInfo);
118 ((DividedHisto1D*) newInfo->GetCf())->Rebin(fRebinX, "x");
119 newInfo->Write();
120 fOutFile->Close();
121 delete fOutFile;
122 fOutFile = nullptr;
123 return kTRUE;
124 }
125
126 Bool_t CorrFitMapRebin::Rebin3D() {
127 Femto3DCF* obj = (Femto3DCF*) fInfo->GetCf();
128 Int_t sizeX = obj->GetNum()->GetNbinsX();
129 Int_t sizeY = obj->GetNum()->GetNbinsY();
130 Int_t sizeZ = obj->GetNum()->GetNbinsZ();
131 TH3* den = (TH3*) obj->GetDen();
132 if (sizeX % fRebinX != 0) {
133 std::cout << "Cannot rebin X-axis in 3DCF" << std::endl;
134 return kFALSE;
135 }
136 if (sizeY % fRebinY != 0) {
137 std::cout << "Cannot rebin Y-axis in 3DCF" << std::endl;
138 return kFALSE;
139 }
140 if (sizeZ % fRebinZ != 0) {
141 std::cout << "Cannot rebin Z-axis in 3DCF" << std::endl;
142 return kFALSE;
143 }
144 Array_1<Float_t>* data = nullptr;
145 fInTree->SetBranchAddress("data", &data);
146 fOutFile = new TFile(fOutFileName, "recreate");
147 fOutTree = new TTree("map", "");
148 Array_1<Float_t>* newData = new Array_1<Float_t>();
149 fOutTree->Branch("data", &newData);
150 fInTree->GetEntry(0);
151 newData->MakeBigger(data->GetSize() / fRebinX / fRebinY / fRebinZ);
152
153 Array_3<Int_t> mapL(sizeX, sizeY, sizeZ);
154 Array_3<Float_t> mapW(sizeX, sizeY, sizeZ);
155 Array_3<Int_t> mapS(sizeX / fRebinX, sizeY / fRebinY, sizeZ / fRebinZ);
156 for (int i = 0; i < sizeX; i++) {
157 for (int j = 0; j < sizeY; j++) {
158 for (int k = 0; k < sizeZ; k++) {
159 mapL[i][j][k] = Hal::Std::Bin3dToBin1d(sizeX, sizeY, i, j, k, kFALSE);
160 }
161 }
162 }
163 Int_t sizeXs = sizeX / fRebinX;
164 Int_t sizeYs = sizeY / fRebinY;
165 Int_t sizeZs = sizeZ / fRebinZ;
166 Double_t dummyScale = 1.0 / (Float_t)(fRebinX * fRebinY * fRebinZ);
167 Array_3<Float_t> temp(fRebinX, fRebinY, fRebinZ);
168 for (int i = 0; i < sizeXs; i++) {
169 for (int j = 0; j < sizeYs; j++) {
170 for (int k = 0; k < sizeZs; k++) {
171 mapS[i][j][k] = Hal::Std::Bin3dToBin1d(sizeXs, sizeYs, i, j, k, kFALSE);
172 Float_t sum = 0;
173 for (int a = 0; a < fRebinX; a++) {
174 const Int_t tX = i * fRebinX + a;
175 for (int b = 0; b < fRebinY; b++) {
176 const Int_t tY = j * fRebinY + b;
177 for (int c = 0; c < fRebinZ; c++) {
178 const Int_t tZ = k * fRebinZ + c;
179 // Int_t bin = mapL[tX][tY][tZ];
180 sum += den->GetBinContent(tX, tY, tZ);
181 }
182 }
183 }
184 for (int a = 0; a < fRebinX; a++) {
185 const Int_t tX = i * fRebinX + a;
186 for (int b = 0; b < fRebinY; b++) {
187 const Int_t tY = j * fRebinY + b;
188 for (int c = 0; c < fRebinZ; c++) {
189 const Int_t tZ = k * fRebinZ + c;
190 mapW[tX][tY][tZ] = den->GetBinContent(tX, tY, tZ) / sum;
191 if (sum == 0) { mapW[tX][tY][tZ] = dummyScale; }
192 }
193 }
194 }
195 }
196 }
197 }
198 Int_t step = fInTree->GetEntries() / 100;
199 for (int iEv = 0; iEv < fInTree->GetEntries(); iEv++) {
200 fInTree->GetEntry(iEv);
201 if (iEv % step) { Cout::ProgressBar(iEv, fInTree->GetEntries()); }
202 for (int i = 0; i < sizeXs; i++) {
203 for (int j = 0; j < sizeYs; j++) {
204 for (int k = 0; k < sizeZs; k++) {
205 Float_t sum = 0;
206 for (int a = 0; a < fRebinX; a++) {
207 const Int_t tX = i * fRebinX + a;
208 for (int b = 0; b < fRebinY; b++) {
209 const Int_t tY = j * fRebinY + b;
210 for (int c = 0; c < fRebinZ; c++) {
211 const Int_t tZ = k * fRebinZ + c;
212 Int_t bin = mapL[tX][tY][tZ];
213 sum += (*data)[bin] * mapW[tX][tY][tZ];
214 }
215 }
216 }
217 (*newData)[mapS[i][j][k]] = sum;
218 }
219 }
220 }
221 fOutTree->Fill();
222 }
223 fOutTree->Write();
224 CorrFitInfo* newInfo = new CorrFitInfo(*fInfo);
225 ((DividedHisto3D*) newInfo->GetCf())->Rebin(fRebinX, "x");
226 ((DividedHisto3D*) newInfo->GetCf())->Rebin(fRebinY, "y");
227 ((DividedHisto3D*) newInfo->GetCf())->Rebin(fRebinZ, "z");
228 newInfo->Write();
229 fOutFile->Close();
230 delete fOutFile;
231 fOutFile = nullptr;
232 return kTRUE;
233 }
234
235 CorrFitMapRebin::CorrFitMapRebin(TString inFile, Int_t nbins) : CorrFitMapRebin() {
236 fInFileName = inFile;
237 fRebinX = fRebinY = fRebinZ = nbins;
238 }
239} // namespace Hal