GRSISort "v4.0.0.5"
An extension of the ROOT analysis Framework
Loading...
Searching...
No Matches
GH2Base.cxx
Go to the documentation of this file.
1#include "GH2Base.h"
2
3#include <iostream>
4
5#include "TDirectory.h"
6
7#include "GH1D.h"
9
11{
12 fProjections->Delete();
13 fSummaryProjections->Delete();
14}
15
17{
18 fProjections = new TList();
19 fSummaryProjections = new TList();
20 fIsSummary = false;
22}
23
24void GH2Base::GH2Clear(Option_t* opt)
25{
26 TString sopt(opt);
27 fProjections->Clear();
28 fSummaryProjections->Clear();
29}
30
31GH1D* GH2Base::Projection_Background(int axis, int firstbin, int lastbin, int first_bg_bin, int last_bg_bin,
33{
34 std::string title;
35 std::string name;
36 std::string sproj;
37 TH1D* proj = nullptr;
38 TH1D* bg_proj = nullptr;
39
40 double xlow = 0.;
41 double xhigh = 0.;
42 double bg_xlow = 0.;
43 double bg_xhigh = 0.;
44
46
47 if(axis == 0) {
48 xlow = GetTH2()->GetXaxis()->GetBinLowEdge(firstbin);
49 xhigh = GetTH2()->GetXaxis()->GetBinUpEdge(lastbin);
50 bg_xlow = GetTH2()->GetXaxis()->GetBinLowEdge(first_bg_bin);
51 bg_xhigh = GetTH2()->GetXaxis()->GetBinUpEdge(last_bg_bin);
52 sproj = "projx";
53 proj = GetTH2()->ProjectionX("temp1", firstbin, lastbin);
54 bg_proj = GetTH2()->ProjectionX("temp2", first_bg_bin, last_bg_bin);
55 } else if(axis == 1) {
56 xlow = GetTH2()->GetYaxis()->GetBinLowEdge(firstbin);
57 xhigh = GetTH2()->GetYaxis()->GetBinUpEdge(lastbin);
58 bg_xlow = GetTH2()->GetYaxis()->GetBinLowEdge(first_bg_bin);
59 bg_xhigh = GetTH2()->GetYaxis()->GetBinUpEdge(last_bg_bin);
60 sproj = "projy";
61 proj = GetTH2()->ProjectionY("temp1", firstbin, lastbin);
62 bg_proj = GetTH2()->ProjectionY("temp2", first_bg_bin, last_bg_bin);
63 } else {
64 return nullptr;
65 }
66 name =
67 Form("%s_%s_%d_%d_bg_%d_%d", GetTH2()->GetName(), sproj.c_str(), firstbin, lastbin, first_bg_bin, last_bg_bin);
68 title = Form("%s_%s_%d[%.02f]_%d[%.02f]_bg_%d[%.02f]_%d[%.02f]", GetTH2()->GetName(), sproj.c_str(), firstbin, xlow,
69 lastbin, xhigh, first_bg_bin, bg_xlow, last_bg_bin, bg_xhigh);
70
71 double bg_scaling = static_cast<double>(lastbin - firstbin) / static_cast<double>(last_bg_bin - first_bg_bin);
73 bg_scaling = 0;
74 }
75
76 proj->Add(bg_proj, -bg_scaling);
77 auto* output = new GH1D(*proj);
78 proj->Delete();
79 bg_proj->Delete();
80
81 output->SetName(name.c_str());
82 output->SetTitle(title.c_str());
83 output->SetParent(GetTH2());
84 output->SetProjectionAxis(axis);
85 output->SetDirectory(nullptr);
86 fProjections->Add(output);
87 return output;
88}
89
90GH1D* GH2Base::GH2ProjectionX(const char* name, int firstbin, int lastbin, Option_t* option, bool KeepEmpty)
91{
92 std::string title;
93 double xlow = GetTH2()->GetYaxis()->GetBinLowEdge(firstbin);
94 double xhigh = GetTH2()->GetYaxis()->GetBinUpEdge(lastbin);
95 bool total = false;
96 if(firstbin == 0 && lastbin == -1) {
97 total = true;
98 title = Form("%s_totalx", GetTH2()->GetName());
99 } else {
100 title = Form("%s_projx_%d[%.02f]_%d[%.02f]", GetTH2()->GetName(), firstbin, xlow, lastbin, xhigh);
101 }
102
103 std::string actual_name = name;
104 if(actual_name == "_px") {
105 if(total) {
106 actual_name = title;
107 } else {
108 actual_name = Form("%s_projx_%d_%d", GetTH2()->GetName(), firstbin, lastbin);
109 }
110 }
111
112 GH1D* output = nullptr;
113 {
115 TH1D* proj = GetTH2()->ProjectionX("temp", firstbin, lastbin, option);
116 output = new GH1D(*proj);
117 proj->Delete();
118 }
119
120 output->SetName(actual_name.c_str());
121 output->SetTitle(title.c_str());
122 output->SetParent(GetTH2());
123 output->SetProjectionAxis(0);
124 output->SetDirectory(nullptr);
125
126 if(fIsSummary) {
127 if(KeepEmpty || output->Integral() > 0) {
128 fSummaryProjections->Add(output);
129 }
130 } else {
131 if(KeepEmpty || output->Integral() > 0) {
132 fProjections->Add(output);
133 }
134 }
135 return output;
136}
137
138GH1D* GH2Base::ProjectionX_Background(int firstbin, int lastbin, int first_bg_bin, int last_bg_bin,
140{
141 return Projection_Background(0, firstbin, lastbin, first_bg_bin, last_bg_bin, mode);
142}
143
144GH1D* GH2Base::GH2ProjectionY(const char* name, int firstbin, int lastbin, Option_t* option, bool KeepEmpty)
145{
146 std::string title;
147 double ylow = GetTH2()->GetXaxis()->GetBinLowEdge(firstbin);
148 double yhigh = GetTH2()->GetXaxis()->GetBinUpEdge(lastbin);
149 bool total = false;
150 if(firstbin == 0 && lastbin == -1) {
151 total = true;
152 title = Form("%s_totaly", GetTH2()->GetName());
153 } else {
154 title = Form("%s_projy_%d[%.02f]_%d[%.02f]", GetTH2()->GetName(), firstbin, ylow, lastbin, yhigh);
155 }
156
157 std::string actual_name = name;
158 if(actual_name == "_py") {
159 if(total) {
160 actual_name = title;
161 } else {
162 actual_name = Form("%s_projy_%d_%d", GetTH2()->GetName(), firstbin, lastbin);
163 }
164 }
165
166 GH1D* output = nullptr;
167 {
169 TH1D* proj = GetTH2()->ProjectionY("temp", firstbin, lastbin, option);
170 output = new GH1D(*proj);
171 proj->Delete();
172 }
173
174 output->SetName(actual_name.c_str());
175 output->SetTitle(title.c_str());
176 output->SetParent(GetTH2());
177 output->SetProjectionAxis(1);
178 output->SetDirectory(nullptr);
179
180 if(fIsSummary) {
181 if(KeepEmpty || output->Integral() > 0) {
182 fSummaryProjections->Add(output);
183 }
184 } else {
185 if(KeepEmpty || output->Integral() > 0) {
186 fProjections->Add(output);
187 }
188 }
189 return output;
190}
191
192GH1D* GH2Base::ProjectionY_Background(int firstbin, int lastbin, int first_bg_bin, int last_bg_bin,
194{
195 return Projection_Background(1, firstbin, lastbin, first_bg_bin, last_bg_bin, mode);
196}
197
198GH1D* GH2Base::GetPrevious(const GH1D* curr, bool)
199{
200 if(fIsSummary) {
201 return GetPrevSummary(curr, false);
202 }
203
204 TObjLink* link = fProjections->FirstLink();
205 while(link != nullptr) {
206 if(link->GetObject() == curr) {
207 break;
208 }
209 link = link->Next();
210 }
211 if(link == nullptr) {
212 return nullptr;
213 }
214
215 if(link->Prev() != nullptr) {
216 return static_cast<GH1D*>(link->Prev()->GetObject());
217 }
218 return static_cast<GH1D*>(fProjections->Last());
219}
220
221GH1D* GH2Base::GetNext(const GH1D* curr, bool)
222{
223 if(fIsSummary) {
224 return GetNextSummary(curr, false);
225 }
226
227 TObjLink* link = fProjections->FirstLink();
228 while(link != nullptr) {
229 if(link->GetObject() == curr) {
230 break;
231 }
232 link = link->Next();
233 }
234 if(link == nullptr) {
235 return nullptr;
236 }
237
238 if(link->Next() != nullptr) {
239 return static_cast<GH1D*>(link->Next()->GetObject());
240 }
241 return static_cast<GH1D*>(fProjections->First());
242}
243
244GH1D* GH2Base::GetNextSummary(const GH1D* curr, bool DrawEmpty)
245{
246 int binnum = 1;
247 std::string name;
248 if(curr != nullptr) {
249 name = curr->GetName();
250 size_t underscore_pos = name.rfind('_');
251 binnum = std::atoi(name.c_str() + underscore_pos + 1);
252 binnum++;
253 }
254
255 int max_binnum = 0;
257 max_binnum = GetTH2()->GetXaxis()->GetNbins();
258 } else {
259 max_binnum = GetTH2()->GetYaxis()->GetNbins();
260 }
261
262 if(binnum > max_binnum) {
263 binnum = 1;
264 }
265 GH1D* hist = nullptr;
266 int start_bin = binnum;
267 while(true) {
268 std::string hist_name = Form("%s_%d", GetTH2()->GetName(), binnum);
269 hist = static_cast<GH1D*>(fSummaryProjections->FindObject(hist_name.c_str()));
270 if((hist != nullptr) && hist->Integral() > 0) {
271 return hist;
272 }
273
274 switch(fSummaryDirection) {
276 hist = GH2ProjectionY(hist_name.c_str(), binnum, binnum, "", DrawEmpty);
277 break;
279 hist = GH2ProjectionX(hist_name.c_str(), binnum, binnum, "", DrawEmpty);
280 break;
281 }
282 if((hist != nullptr) && hist->Integral() > 0) {
283 return hist;
284 }
285 binnum++;
286 if(binnum == start_bin) {
287 break;
288 }
289 binnum = 1;
290 }
291 return hist;
292}
293
294GH1D* GH2Base::GetPrevSummary(const GH1D* curr, bool DrawEmpty)
295{
296 int binnum = 1;
297 std::string name;
298 if(curr != nullptr) {
299 name = curr->GetName();
300 size_t underscore_pos = name.rfind('_');
301 binnum = std::atoi(name.c_str() + underscore_pos + 1);
302 binnum--;
303 }
304
305 int max_binnum = 0;
307 max_binnum = GetTH2()->GetXaxis()->GetNbins();
308 } else {
309 max_binnum = GetTH2()->GetYaxis()->GetNbins();
310 }
311
312 if(binnum <= 0) {
313 binnum = max_binnum;
314 }
315
316 std::string hist_name = Form("%s_%d", GetTH2()->GetName(), binnum);
317 TObject* obj = fSummaryProjections->FindObject(hist_name.c_str());
318 if(obj != nullptr) {
319 return static_cast<GH1D*>(obj);
320 }
321
322 GH1D* hist = nullptr;
323 int start_bin = binnum;
324 while(true) {
325 std::string hist_name2 = Form("%s_%d", GetTH2()->GetName(), binnum);
326 switch(fSummaryDirection) {
328 hist = GH2ProjectionY(hist_name2.c_str(), binnum, binnum, "", DrawEmpty);
329 break;
331 hist = GH2ProjectionX(hist_name2.c_str(), binnum, binnum, "", DrawEmpty);
332 break;
333 }
334 if((hist != nullptr) && hist->Integral() > 0) {
335 return hist;
336 }
337 binnum--;
338 if(binnum == start_bin) {
339 break;
340 }
341 binnum = max_binnum;
342 }
343 return hist;
344}
EBackgroundSubtraction
Definition GH2Base.h:14
TH1D * hist
Definition UserFillObj.h:3
Definition GH1D.h:17
void SetProjectionAxis(int axis)
Definition GH1D.h:47
void SetParent(TObject *obj)
Definition GH1D.h:41
virtual ~GH2Base()
Definition GH2Base.cxx:10
bool fIsSummary
Definition GH2Base.h:119
GH1D * GetPrevious(const GH1D *curr, bool DrawEmpty=true)
Definition GH2Base.cxx:198
EDirection fSummaryDirection
Definition GH2Base.h:120
virtual void GH2Clear(Option_t *opt="")
Definition GH2Base.cxx:24
void Init()
Definition GH2Base.cxx:16
TList * fSummaryProjections
Definition GH2Base.h:118
GH1D * GH2ProjectionY(const char *name="_py", int firstbin=0, int lastbin=-1, Option_t *option="", bool KeepEmpty=false)
Definition GH2Base.cxx:144
GH1D * ProjectionY_Background(int firstbin=0, int lastbin=-1, int first_bg_bin=0, int last_bg_bin=-1, EBackgroundSubtraction mode=EBackgroundSubtraction::kRegionBackground)
Definition GH2Base.cxx:192
GH1D * GetNextSummary(const GH1D *curr, bool DrawEmpty=false)
Definition GH2Base.cxx:244
GH1D * GH2ProjectionX(const char *name="_px", int firstbin=0, int lastbin=-1, Option_t *option="", bool KeepEmpty=false)
Definition GH2Base.cxx:90
virtual TH2 * GetTH2()=0
GH1D * GetNext(const GH1D *curr, bool DrawEmpty=true)
Definition GH2Base.cxx:221
GH1D * ProjectionX_Background(int firstbin=0, int lastbin=-1, int first_bg_bin=0, int last_bg_bin=-1, EBackgroundSubtraction mode=EBackgroundSubtraction::kRegionBackground)
Definition GH2Base.cxx:138
GH1D * Projection_Background(int axis=0, int firstbin=0, int lastbin=-1, int first_bg_bin=0, int last_bg_bin=-1, EBackgroundSubtraction mode=EBackgroundSubtraction::kRegionBackground)
Definition GH2Base.cxx:31
GH1D * GetPrevSummary(const GH1D *curr, bool DrawEmpty=false)
Definition GH2Base.cxx:294
TList * fProjections
Definition GH2Base.h:116