GamR  0.0.0
GammaROOT
Loading...
Searching...
No Matches
Cut.cc
Go to the documentation of this file.
1#include <chrono>
2
3#include <TMarker.h>
4
5#include "Cut.hh"
6
7
8
9namespace GamR {
10 namespace Spect {
11 TH1D *ProjX(TH2 *hist, const char *name) {
12 return hist->ProjectionX(name);
13 }
14
15 TH1D *ProjX(TH2 *hist) {
16 return hist->ProjectionX(((std::string)hist->GetName()+"_px").c_str());
17 }
18
19 TH1D *ProjX(TVirtualPad *canvas, const char *name) {
20 TH2D *hist = GamR::Utils::GetHist2D(canvas);
21 if (!hist) { return NULL; }
22 TH1D *ret = ProjX(hist, name);
23 ret->Draw("hist");
24 return ret;
25 }
26
27 TH1D *ProjX(TVirtualPad *canvas) {
28 TH2D *hist = GamR::Utils::GetHist2D(canvas);
29 if (!hist) { return NULL; }
30 TH1D *ret = ProjX(hist);
31 ret->Draw("hist");
32 return ret;
33 }
34
35 TH1D *ProjY(TH2 *hist, const char *name) {
36 return hist->ProjectionY(name);
37 }
38
39 TH1D *ProjY(TH2 *hist) {
40 return hist->ProjectionY(((std::string)hist->GetName()+"_py").c_str());
41 }
42
43 TH1D *ProjY(TVirtualPad *canvas, const char *name) {
44 TH2D *hist = GamR::Utils::GetHist2D(canvas);
45 if (!hist) { return NULL; }
46 TH1D *ret = ProjY(hist, name);
47 ret->Draw("hist");
48 return ret;
49 }
50
51 TH1D *ProjY(TVirtualPad *canvas) {
52 TH2D *hist = GamR::Utils::GetHist2D(canvas);
53 if (!hist) { return NULL; }
54 TH1D *ret = ProjY(hist);
55 ret->Draw("hist");
56 return ret;
57 }
58
59
70 TH1D *GateX(TH2 *hist, GamR::TK::Gate peak, const char *name)
71 {
72 /* applying gates in X direction */
73 TH1D *hOut = peak.ApplyX(hist, name);
74 return hOut;
75 }
76
77 TH1D *GateX(TH2 *hist, GamR::TK::Gate peak)
78 {
79 /* applying gates in X direction */
80 TH1D *hOut = peak.ApplyX(hist, ((std::string)hist->GetName()+"_gx").c_str());
81 return hOut;
82 }
83
84 TH1D *GateX(TCanvas *canvas, const char *name)
85 {
86 /* get histogram */
87 TH2D *hist = GamR::Utils::GetHist2D(canvas);
88 if (!hist) { return NULL; }
89 hist->ProjectionX()->Draw("hist");
90 std::cout << "Select peak region:" << std::endl;
91 GamR::TK::Gate peak(canvas, "x");
92 //std::cout << peak << std::endl;
93
94 TH1D *hOut = GateX(hist, peak, name);
95 hOut->Draw("hist");
96 return hOut;
97 }
98
99 TH1D *GateX(TCanvas *canvas)
100 {
101 /* get histogram */
102 TH2D *hist = GamR::Utils::GetHist2D(canvas);
103 if (!hist) { return NULL; }
104 hist->ProjectionX()->Draw("hist");
105 std::cout << "Select peak region:" << std::endl;
106 GamR::TK::Gate peak(canvas, "x");
107 //std::cout << peak << std::endl;
108
109 TH1D *hOut = GateX(hist, peak);
110 hOut->Draw("hist");
111 return hOut;
112 }
113
114
125 TH1D *GateY(TH2 *hist, GamR::TK::Gate peak, const char *name)
126 {
127 /* applying gates in Y direction */
128 TH1D *hOut = peak.ApplyY(hist, name);
129 return hOut;
130 }
131
132 TH1D *GateY(TH2 *hist, GamR::TK::Gate peak)
133 {
134 /* applying gates in Y direction */
135 TH1D *hOut = peak.ApplyY(hist, ((std::string)hist->GetName()+"_gy").c_str());
136 return hOut;
137 }
138
139 TH1D *GateY(TCanvas *canvas, const char *name)
140 {
141 /* get histogram */
142 TH2D *hist = GamR::Utils::GetHist2D(canvas);
143 if (!hist) { return NULL; }
144 hist->ProjectionY()->Draw("hist");
145 std::cout << "Select peak region:" << std::endl;
146 GamR::TK::Gate peak(canvas, "x");
147 //std::cout << peak << std::endl;
148
149 TH1D *hOut = GateY(hist, peak, name);
150 hOut->Draw("hist");
151 return hOut;
152 }
153
154 TH1D *GateY(TCanvas *canvas)
155 {
156 /* get histogram */
157 TH2D *hist = GamR::Utils::GetHist2D(canvas);
158 if (!hist) { return NULL; }
159 hist->ProjectionY()->Draw("hist");
160 std::cout << "Select peak region:" << std::endl;
161 GamR::TK::Gate peak(canvas, "x");
162 //std::cout << peak << std::endl;
163
164 TH1D *hOut = GateY(hist, peak);
165 hOut->Draw("hist");
166 return hOut;
167 }
168
169
185 TH1D *BackgroundSubtractX(TH2 *hist, GamR::TK::Gate peak, std::vector<GamR::TK::Gate > background, const char *name)
186 {
187 if (background.size()==0) { std::cout << "Must select at least one background region" << std::endl; return NULL; }
188 /* applying gates in X direction */
189 TH1D *hTotal = peak.ApplyX(hist, "hTotal");
190 TH1D *hProjX = (TH1D*)hist->ProjectionX();
191 TH1D *hBackground = background[0].ApplyX(hist, "hBackground");
192 int backgroundWidth = background[0].GetBinWidth(hProjX);
193 for (int i=1; i<background.size(); ++i) {
194 TH1D *hBack = background[i].ApplyX(hist, "hBack");
195 hBackground->Add(hBack);
196 backgroundWidth += background[i].GetBinWidth(hProjX);
197 }
198
199 TH1D *hOut = (TH1D*)hTotal->Clone(name);
200 hOut->SetTitle(name);
201
202 /* get bin widths of gates */
203 double scale =
204 (static_cast<double>(peak.GetBinWidth(hProjX)) /
205 static_cast<double>(backgroundWidth));
206
207 hOut->Add(hBackground, -scale);
208
209 //do correct error propagation
210 for (int i=1; i<=hOut->GetNbinsX(); ++i) {
211 double sigT = hTotal->GetBinError(i);
212 double T = hTotal->GetBinContent(i);
213 double sigB = hBackground->GetBinError(i);
214 double B = hBackground->GetBinContent(i);
215 double sigsquared = pow(sigT, 2) + pow(scale*sigB, 2);
216 hOut->SetBinError(i, sqrt(sigsquared));
217 }
218
219 return hOut;
220 }
221
222 TH1D *BackgroundSubtractX(TH2 *hist, GamR::TK::Gate peak, std::vector<GamR::TK::Gate > background)
223 {
224 return BackgroundSubtractX(hist, peak, background, ((std::string)hist->GetName()+"_bsx").c_str());
225 }
226
227 TH1D *BackgroundSubtractX(TH2 *hist, GamR::TK::Gate peak, GamR::TK::Gate background, const char *name) {
228 std::vector<GamR::TK::Gate > bg = {background};
229 return BackgroundSubtractX(hist, peak, bg, name);
230 }
231
232 TH1D *BackgroundSubtractX(TH2 *hist, GamR::TK::Gate peak, GamR::TK::Gate background) {
233 std::vector<GamR::TK::Gate > bg = {background};
234 return BackgroundSubtractX(hist, peak, bg);
235 }
236
237 TH1D *BackgroundSubtractX(TH2 *hist, GamR::Nucleus::Transition peak, const char *name)
238 {
239 return BackgroundSubtractX(hist, *peak.GetGate(), *peak.GetGateBG(), name);
240 }
241
243 {
244 return BackgroundSubtractX(hist, *peak.GetGate(), *peak.GetGateBG());
245 }
246
247 TH1D *BackgroundSubtractX(TCanvas *canvas, const char *name)
248 {
249 /* get histogram */
250 TH2D *hist = GamR::Utils::GetHist2D(canvas);
251 if (!hist) { return NULL; }
252 hist->ProjectionX()->Draw("hist");
253 std::cout << "Select peak region:" << std::endl;
254 GamR::TK::Gate peak(canvas, "x");
255 //std::cout << peak << std::endl;
256 std::cout << "Select background regions, key press to stop:" << std::endl;
257 std::vector<GamR::TK::Gate > background;
258 while(true) {
260 int retval = bg.SetGate(canvas, "x");
261 if (retval>0) { break; }
262 background.push_back(bg);
263 }
264
265 TH1D *hOut = BackgroundSubtractX(hist, peak, background, name);
266 hOut->Draw("hist");
267 return hOut;
268 }
269
270 TH1D *BackgroundSubtractX(TCanvas *canvas)
271 {
272 /* get histogram */
273 TH2D *hist = GamR::Utils::GetHist2D(canvas);
274 if (!hist) { return NULL; }
275 hist->ProjectionX()->Draw("hist");
276 std::cout << "Select peak region:" << std::endl;
277 GamR::TK::Gate peak(canvas, "x");
278 std::cout << "Select background regions, key press to stop:" << std::endl;
279 std::vector<GamR::TK::Gate > background;
280 while(true) {
282 int retval = bg.SetGate(canvas, "x");
283 if (retval>0) { break; }
284 background.push_back(bg);
285 }
286
287 TH1D *hOut = BackgroundSubtractX(hist, peak, background);
288 hOut->Draw("hist");
289 return hOut;
290 }
291
307 TH1D *BackgroundSubtractY(TH2 *hist, GamR::TK::Gate peak, std::vector<GamR::TK::Gate > background, const char *name)
308 {
309 if (background.size()==0) { std::cout << "Must select at least one background region" << std::endl; return NULL; }
310 /* applying gates in Y direction */
311 TH1D *hTotal = peak.ApplyY(hist, "hTotal");
312 TH1D *hProjY = (TH1D*)hist->ProjectionY();
313 TH1D *hBackground = background[0].ApplyY(hist, "hBackground");
314 int backgroundWidth = background[0].GetBinWidth(hProjY);
315 for (int i=1; i<background.size(); ++i) {
316 TH1D *hBack = background[i].ApplyY(hist, "hBack");
317 hBackground->Add(hBack);
318 backgroundWidth += background[i].GetBinWidth(hProjY);
319 }
320
321 TH1D *hOut = (TH1D*)hTotal->Clone(name);
322 hOut->SetTitle(name);
323
324 /* get bin widths of gates */
325 double scale =
326 (static_cast<double>(peak.GetBinWidth(hProjY)) /
327 static_cast<double>(backgroundWidth));
328
329 hOut->Add(hBackground, -scale);
330
331 //do correct error propagation
332 for (int i=1; i<=hOut->GetNbinsX(); ++i) {
333 double sigT = hTotal->GetBinError(i);
334 double T = hTotal->GetBinContent(i);
335 double sigB = hBackground->GetBinError(i);
336 double B = hBackground->GetBinContent(i);
337 double sigsquared = pow(sigT, 2) + pow(scale*sigB, 2);
338 hOut->SetBinError(i, sqrt(sigsquared));
339 }
340
341 return hOut;
342 }
343
344 TH1D *BackgroundSubtractY(TH2 *hist, GamR::TK::Gate peak, std::vector<GamR::TK::Gate > background)
345 {
346 return BackgroundSubtractY(hist, peak, background, ((std::string)hist->GetName()+"_bsy").c_str());
347 }
348
349 TH1D *BackgroundSubtractY(TH2 *hist, GamR::TK::Gate peak, GamR::TK::Gate background, const char *name)
350 {
351 std::vector<GamR::TK::Gate > bg = {background};
352 return BackgroundSubtractY(hist, peak, bg, name);
353 }
354
355 TH1D *BackgroundSubtractY(TH2 *hist, GamR::TK::Gate peak, GamR::TK::Gate background)
356 {
357 std::vector<GamR::TK::Gate > bg = {background};
358 return BackgroundSubtractY(hist, peak, bg);
359 }
360
361 TH1D *BackgroundSubtractY(TH2 *hist, GamR::Nucleus::Transition peak, const char *name)
362 {
363 return BackgroundSubtractY(hist, *peak.GetGate(), *peak.GetGateBG(), name);
364 }
365
367 {
368 return BackgroundSubtractY(hist, *peak.GetGate(), *peak.GetGateBG());
369 }
370
371 TH1D *BackgroundSubtractY(TCanvas *canvas, const char *name)
372 {
373 /* get histogram */
374 TH2D *hist = GamR::Utils::GetHist2D(canvas);
375 if (!hist) { return NULL; }
376 hist->ProjectionY()->Draw("hist");
377 std::cout << "Select peak region:" << std::endl;
378 GamR::TK::Gate peak(canvas, "x");
379 //std::cout << peak << std::endl;
380 std::cout << "Select background regions:" << std::endl;
381 std::vector<GamR::TK::Gate > background;
382 while(true) {
384 int retval = bg.SetGate(canvas, "x");
385 if (retval>0) { break; }
386 background.push_back(bg);
387 }
388
389 TH1D *hOut = BackgroundSubtractY(hist, peak, background, name);
390 hOut->Draw("hist");
391 return hOut;
392 }
393
394 TH1D *BackgroundSubtractY(TCanvas *canvas)
395 {
396 /* get histogram */
397 TH2D *hist = GamR::Utils::GetHist2D(canvas);
398 if (!hist) { return NULL; }
399 hist->ProjectionY()->Draw("hist");
400 std::cout << "Select peak region:" << std::endl;
401 GamR::TK::Gate peak(canvas, "x");
402 //std::cout << peak << std::endl;
403 std::cout << "Select background regions:" << std::endl;
404 std::vector<GamR::TK::Gate > background;
405 while(true) {
407 int retval = bg.SetGate(canvas, "x");
408 if (retval>0) { break; }
409 background.push_back(bg);
410 }
411
412 TH1D *hOut = BackgroundSubtractY(hist, peak, background);
413 hOut->Draw("hist");
414 return hOut;
415 }
416
430 std::shared_ptr<TH2D> BackgroundEstimate(const TH2 *hist, Int_t NiterX, Int_t NiterY, Int_t direction, Int_t filtertype)
431 {
432 Int_t nbinx = hist->GetNbinsX();
433 Int_t nbiny = hist->GetNbinsY();
434
435 Double_t **spectrum = new Double_t *[nbinx];
436 for (int x = 0; x < nbinx; ++x) {
437 spectrum[x] = new Double_t[nbiny];
438 for (int y = 0; y < nbiny; ++y) {
439 spectrum[x][y] = hist->GetBinContent(x + 1, y + 1);
440 }
441 }
442
443 auto analyser = std::make_unique<TSpectrum2>();
444 analyser->Background(spectrum, nbinx, nbiny, NiterX, NiterY, direction, filtertype);
445
446 auto name = std::string(hist->GetName()) + "_bg";
447 auto output = std::shared_ptr<TH2D>(static_cast<TH2D *>(hist->Clone(name.c_str())));
448 output->SetTitle("Background subtracted");
449 for (int x = 0; x < nbinx; ++x) {
450 for (int y = 0; y < nbiny; ++y) {
451 output->SetBinContent(x + 1, y + 1, spectrum[x][y]);
452 }
453 }
454 delete[] spectrum;
455 return output;
456 }
457
458 TH2D *BackgroundSubtract2D(TH2 *peak, TH2 *background, double scale) {
459 TH2D *backsub = (TH2D*)peak->Clone(((std::string)peak->GetName()+"_bs").c_str());
460 backsub->Add(background, -scale);
461 //correct error propagation
462 double lastSigT = 1.0;
463 for (int i=0; i<=backsub->GetNbinsX(); ++i) {
464 for (int j=0; j<=backsub->GetNbinsY(); ++j) {
465 double sigT = peak->GetBinError(i,j);
466 double T = peak->GetBinContent(i,j);
467 double sigB = background->GetBinError(i,j);
468 double B = background->GetBinContent(i,j);
469
470 double sigsquared = pow(sigT, 2) + pow(scale*sigB, 2);
471 backsub->SetBinError(i, j, sqrt(sigsquared));
472 lastSigT = sigT;
473 }
474 }
475
476 return backsub;
477 }
478
479 TCutG *DrawCut(TVirtualPad *canvas, bool verbose, std::string filename, int ID) {
480 TGraph *gr = new TGraph();
481 gr->SetLineColor(kRed);
482 gr->SetMarkerColor(kRed);
483 gr->SetMarkerStyle(8);
484
486 std::vector<std::string> messages = {"Click for next point, press any key to exit..."};
487
488 int retval = click.GetClicks(canvas, -1, messages, 1);
489
490 for (int i=0; i<click.xs.size(); ++i) {
491 gr->AddPoint(click.xs[i], click.ys[i]);
492 }
493 gr->AddPoint(click.xs[0], click.ys[0]);
494
495 click.line->Delete();
496 click.line=NULL;
497
498 gr->Draw("same LP");
499
500 canvas->Update();
501
502 if (verbose) {
503 PrintCut((TCutG*)gr);
504 }
505
506
507 if (filename.size()>0) {
508 std::ofstream ofile(filename.c_str(), std::ios_base::app);
509 while (ID<=-1) {
510 std::cout << "Enter ID: ";
511 std::string sID;
512 std::cin >> sID;
513 try {
514 ID = std::atoi(sID.c_str());
515 }
516 catch (...) {
517 std::cout << "Invalid input!" << std::endl;
518 }
519 if (ID == 0) {
520 std::cout << "Invalid input!" << std::endl;
521 }
522 }
523 std::cout << "Enter name: ";
524 std::string name;
525 std::cin >> name;
526 double x,y;
527 gr->SetName(name.c_str());
528 ofile << ID << " " << gr->GetN() << " " << name << std::endl;
529 for (int i=0; i<gr->GetN(); ++i) {
530 gr->GetPoint(i,x,y);
531 ofile << x << " " << y << std::endl;
532 }
533 }
534
535 return (TCutG*)gr;
536 }
537
538 TCutG *DrawCut(std::string cutfile, int ID, TVirtualPad *canvas) {
539 FILE *file = fopen(cutfile.c_str(), "ra");
540 if (file == NULL) { std::cout << "Cuts file " << cutfile << " does not exist" << std::endl; return 0; }
541 std::stringstream ss;
542 char cline[2048];
543
544 int ind = 0;
545 while(std::fgets(cline, sizeof cline, file)!=NULL) {
546 std::string line(cline);
547 if (line.size() <= 1) { continue; }
548 if (line[0] == '#') { continue; }
549 if (line[0] == ';') { continue; }
550
551 ss.clear();
552 ss.str(line);
553
554 std::string name;
555 int id, npoints;
556 ss >> id;
557 ss >> npoints;
558 ss >> name;
559
560 TCutG *cut = new TCutG();
561 cut->SetName(name.c_str());
562 cut->SetLineColor(kRed);
563 cut->SetMarkerColor(kRed);
564
565 int ct = 0;
566 while (ct < npoints) {
567 if (std::fgets(cline, sizeof cline, file) == NULL ) { return 0 ; }
568
569 std::string line2(cline);
570 if (line2.size() <= 1) { continue; }
571 if (line2[0] == '#') { continue; }
572 if (line2[0] == ';') { continue; }
573
574 ss.clear();
575 ss.str(line2);
576
577 double x, y;
578 ss >> x;
579 ss >> y;
580
581 cut->AddPoint(x,y);
582 ++ct;
583 }
584 if (id == ID) { fclose(file); canvas->cd(); cut->Draw("same LP"); return cut; }
585 else { delete cut; }
586 }
587
588 return 0;
589 }
590
591 void PrintCut(TCutG *cut) {
592 double x,y;
593 std::cout << "ID " << cut->GetN() << std::endl;
594 for (int i=0; i<cut->GetN(); ++i) {
595 cut->GetPoint(i,x,y);
596 std::cout << x << " " << y << std::endl;
597 }
598 }
599
600
601 } // namespace Spect
602} // namespace GamR
void ss(std::vector< int > indexes, TCanvas *canvas, Option_t *option)
std::pair< double, double > ct(TCanvas *canvas)
const GamR::TK::Gate * GetGate() const
Definition Transition.hh:39
const GamR::TK::Gate * GetGateBG() const
Definition Transition.hh:40
std::vector< double > xs
Definition Utilities.hh:38
std::vector< double > ys
Definition Utilities.hh:39
int GetClicks(TVirtualPad *canvas, int n, std::vector< std::string > &messages, int draw=0, int print=0)
Definition Utilities.cc:127
int GetBinWidth(TH1 *hist) const
Definition Gate.hh:75
TH1D * ApplyY(TH2 *hist) const
Definition Gate.cc:168
TH1D * ApplyX(TH2 *hist) const
Definition Gate.cc:218
int SetGate()
Definition Gate.cc:76
TH1D * ProjY(TH2 *hist, const char *name)
Definition Cut.cc:35
TCutG * DrawCut(TVirtualPad *canvas, bool verbose, std::string filename, int ID)
Definition Cut.cc:479
TH1D * BackgroundSubtractY(TH2 *hist, GamR::TK::Gate peak, std::vector< GamR::TK::Gate > background, const char *name)
Definition Cut.cc:307
TH1D * BackgroundSubtractX(TH2 *hist, GamR::TK::Gate peak, std::vector< GamR::TK::Gate > background, const char *name)
Definition Cut.cc:185
std::shared_ptr< TH2D > BackgroundEstimate(const TH2 *hist, Int_t NiterX, Int_t NiterY, Int_t direction, Int_t filtertype)
Definition Cut.cc:430
TH1D * GateY(TH2 *hist, GamR::TK::Gate peak, const char *name)
Definition Cut.cc:125
TH1D * ProjX(TH2 *hist, const char *name)
Definition Cut.cc:11
TH2D * BackgroundSubtract2D(TH2 *peak, TH2 *background, double scale)
Definition Cut.cc:458
void PrintCut(TCutG *cut)
Definition Cut.cc:591
TH1D * GateX(TH2 *hist, GamR::TK::Gate peak, const char *name)
Definition Cut.cc:70
TH2D * GetHist2D(TVirtualPad *canvas)
Definition Utilities.cc:221
Definition Gain.cc:19