11 TH1D *
ProjX(TH2 *hist,
const char *name) {
12 return hist->ProjectionX(name);
16 return hist->ProjectionX(((std::string)hist->GetName()+
"_px").c_str());
19 TH1D *
ProjX(TVirtualPad *canvas,
const char *name) {
21 if (!hist) {
return NULL; }
22 TH1D *ret =
ProjX(hist, name);
27 TH1D *
ProjX(TVirtualPad *canvas) {
29 if (!hist) {
return NULL; }
30 TH1D *ret =
ProjX(hist);
35 TH1D *
ProjY(TH2 *hist,
const char *name) {
36 return hist->ProjectionY(name);
40 return hist->ProjectionY(((std::string)hist->GetName()+
"_py").c_str());
43 TH1D *
ProjY(TVirtualPad *canvas,
const char *name) {
45 if (!hist) {
return NULL; }
46 TH1D *ret =
ProjY(hist, name);
51 TH1D *
ProjY(TVirtualPad *canvas) {
53 if (!hist) {
return NULL; }
54 TH1D *ret =
ProjY(hist);
73 TH1D *hOut = peak.
ApplyX(hist, name);
80 TH1D *hOut = peak.
ApplyX(hist, ((std::string)hist->GetName()+
"_gx").c_str());
84 TH1D *
GateX(TCanvas *canvas,
const char *name)
88 if (!hist) {
return NULL; }
89 hist->ProjectionX()->Draw(
"hist");
90 std::cout <<
"Select peak region:" << std::endl;
94 TH1D *hOut =
GateX(hist, peak, name);
103 if (!hist) {
return NULL; }
104 hist->ProjectionX()->Draw(
"hist");
105 std::cout <<
"Select peak region:" << std::endl;
109 TH1D *hOut =
GateX(hist, peak);
128 TH1D *hOut = peak.
ApplyY(hist, name);
135 TH1D *hOut = peak.
ApplyY(hist, ((std::string)hist->GetName()+
"_gy").c_str());
139 TH1D *
GateY(TCanvas *canvas,
const char *name)
143 if (!hist) {
return NULL; }
144 hist->ProjectionY()->Draw(
"hist");
145 std::cout <<
"Select peak region:" << std::endl;
149 TH1D *hOut =
GateY(hist, peak, name);
158 if (!hist) {
return NULL; }
159 hist->ProjectionY()->Draw(
"hist");
160 std::cout <<
"Select peak region:" << std::endl;
164 TH1D *hOut =
GateY(hist, peak);
187 if (background.size()==0) { std::cout <<
"Must select at least one background region" << std::endl;
return NULL; }
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);
199 TH1D *hOut = (TH1D*)hTotal->Clone(name);
200 hOut->SetTitle(name);
205 static_cast<double>(backgroundWidth));
207 hOut->Add(hBackground, -scale);
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));
224 return BackgroundSubtractX(hist, peak, background, ((std::string)hist->GetName()+
"_bsx").c_str());
228 std::vector<GamR::TK::Gate > bg = {background};
233 std::vector<GamR::TK::Gate > bg = {background};
251 if (!hist) {
return NULL; }
252 hist->ProjectionX()->Draw(
"hist");
253 std::cout <<
"Select peak region:" << std::endl;
256 std::cout <<
"Select background regions, key press to stop:" << std::endl;
257 std::vector<GamR::TK::Gate > background;
260 int retval = bg.
SetGate(canvas,
"x");
261 if (retval>0) {
break; }
262 background.push_back(bg);
274 if (!hist) {
return NULL; }
275 hist->ProjectionX()->Draw(
"hist");
276 std::cout <<
"Select peak region:" << std::endl;
278 std::cout <<
"Select background regions, key press to stop:" << std::endl;
279 std::vector<GamR::TK::Gate > background;
282 int retval = bg.
SetGate(canvas,
"x");
283 if (retval>0) {
break; }
284 background.push_back(bg);
309 if (background.size()==0) { std::cout <<
"Must select at least one background region" << std::endl;
return NULL; }
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);
321 TH1D *hOut = (TH1D*)hTotal->Clone(name);
322 hOut->SetTitle(name);
327 static_cast<double>(backgroundWidth));
329 hOut->Add(hBackground, -scale);
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));
346 return BackgroundSubtractY(hist, peak, background, ((std::string)hist->GetName()+
"_bsy").c_str());
351 std::vector<GamR::TK::Gate > bg = {background};
357 std::vector<GamR::TK::Gate > bg = {background};
375 if (!hist) {
return NULL; }
376 hist->ProjectionY()->Draw(
"hist");
377 std::cout <<
"Select peak region:" << std::endl;
380 std::cout <<
"Select background regions:" << std::endl;
381 std::vector<GamR::TK::Gate > background;
384 int retval = bg.
SetGate(canvas,
"x");
385 if (retval>0) {
break; }
386 background.push_back(bg);
398 if (!hist) {
return NULL; }
399 hist->ProjectionY()->Draw(
"hist");
400 std::cout <<
"Select peak region:" << std::endl;
403 std::cout <<
"Select background regions:" << std::endl;
404 std::vector<GamR::TK::Gate > background;
407 int retval = bg.
SetGate(canvas,
"x");
408 if (retval>0) {
break; }
409 background.push_back(bg);
430 std::shared_ptr<TH2D>
BackgroundEstimate(
const TH2 *hist, Int_t NiterX, Int_t NiterY, Int_t direction, Int_t filtertype)
432 Int_t nbinx = hist->GetNbinsX();
433 Int_t nbiny = hist->GetNbinsY();
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);
443 auto analyser = std::make_unique<TSpectrum2>();
444 analyser->Background(spectrum, nbinx, nbiny, NiterX, NiterY, direction, filtertype);
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]);
459 TH2D *backsub = (TH2D*)peak->Clone(((std::string)peak->GetName()+
"_bs").c_str());
460 backsub->Add(background, -scale);
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);
470 double sigsquared = pow(sigT, 2) + pow(scale*sigB, 2);
471 backsub->SetBinError(i, j, sqrt(sigsquared));
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);
486 std::vector<std::string> messages = {
"Click for next point, press any key to exit..."};
488 int retval = click.
GetClicks(canvas, -1, messages, 1);
490 for (
int i=0; i<click.
xs.size(); ++i) {
491 gr->AddPoint(click.
xs[i], click.
ys[i]);
493 gr->AddPoint(click.
xs[0], click.
ys[0]);
495 click.
line->Delete();
507 if (filename.size()>0) {
508 std::ofstream ofile(filename.c_str(), std::ios_base::app);
510 std::cout <<
"Enter ID: ";
514 ID = std::atoi(sID.c_str());
517 std::cout <<
"Invalid input!" << std::endl;
520 std::cout <<
"Invalid input!" << std::endl;
523 std::cout <<
"Enter name: ";
527 gr->SetName(name.c_str());
528 ofile << ID <<
" " << gr->GetN() <<
" " << name << std::endl;
529 for (
int i=0; i<gr->GetN(); ++i) {
531 ofile << x <<
" " << y << std::endl;
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;
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; }
560 TCutG *cut =
new TCutG();
561 cut->SetName(name.c_str());
562 cut->SetLineColor(kRed);
563 cut->SetMarkerColor(kRed);
566 while (
ct < npoints) {
567 if (std::fgets(cline,
sizeof cline, file) == NULL ) {
return 0 ; }
569 std::string line2(cline);
570 if (line2.size() <= 1) {
continue; }
571 if (line2[0] ==
'#') {
continue; }
572 if (line2[0] ==
';') {
continue; }
584 if (
id == ID) { fclose(file); canvas->cd(); cut->Draw(
"same LP");
return cut; }
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;
void ss(std::vector< int > indexes, TCanvas *canvas, Option_t *option)
std::pair< double, double > ct(TCanvas *canvas)
const GamR::TK::Gate * GetGate() const
const GamR::TK::Gate * GetGateBG() const
int GetClicks(TVirtualPad *canvas, int n, std::vector< std::string > &messages, int draw=0, int print=0)
int GetBinWidth(TH1 *hist) const
TH1D * ApplyY(TH2 *hist) const
TH1D * ApplyX(TH2 *hist) const
TH1D * ProjY(TH2 *hist, const char *name)
TCutG * DrawCut(TVirtualPad *canvas, bool verbose, std::string filename, int ID)
TH1D * BackgroundSubtractY(TH2 *hist, GamR::TK::Gate peak, std::vector< GamR::TK::Gate > background, const char *name)
TH1D * BackgroundSubtractX(TH2 *hist, GamR::TK::Gate peak, std::vector< GamR::TK::Gate > background, const char *name)
std::shared_ptr< TH2D > BackgroundEstimate(const TH2 *hist, Int_t NiterX, Int_t NiterY, Int_t direction, Int_t filtertype)
TH1D * GateY(TH2 *hist, GamR::TK::Gate peak, const char *name)
TH1D * ProjX(TH2 *hist, const char *name)
TH2D * BackgroundSubtract2D(TH2 *peak, TH2 *background, double scale)
void PrintCut(TCutG *cut)
TH1D * GateX(TH2 *hist, GamR::TK::Gate peak, const char *name)
TH2D * GetHist2D(TVirtualPad *canvas)