122 void BPeak::Set(TH1 *hist,
GamR::TK::Gate peak, std::vector<GamR::TK::Gate > background, Option_t *foption, Option_t *option) {
124 if (background.size()==0) { std::cout <<
"Must have at least one background region" << std::endl;
return; }
126 TString opts(option);
129 if (opts.Contains(
"s")) {
130 SetCont(hist, peak, background, foption, option);
133 if (opts.Contains(
"p")) {
134 SetPoint(hist, peak, background, foption, option);
137 if (opts.Contains(
"x")) {
138 SetStep(hist, peak, background, foption, option);
142 SetCont(hist, peak, background, foption, option);
148 TString opts(option);
150 if (opts.Contains(
"v")) {
151 std::cout <<
"Setting peak with regions:" << std::endl;
153 for (
auto &b : background) {
154 std::cout << b.GetLow() <<
" " << b.GetHigh() <<
" ";
156 std::cout << std::endl;
160 mBackgrounds = background;
162 TGraphErrors *back =
new TGraphErrors();
165 for (
int b=0; b<background.size(); ++b) {
166 int iStart = background[b].GetBinLow(hist);
167 int iStop = background[b].GetBinHigh(hist);
168 double last_err = -1.0;
169 for (
int i=iStart; i<=iStop; ++i) {
170 int nPoint = back->GetN();
171 double x = hist->GetBinCenter(i);
172 double y = hist->GetBinContent(i);
173 if (hist->GetBinError(i) > 0.0) {
174 back->SetPoint(nPoint, x, y);
175 back->SetPointError(nPoint, 0, hist->GetBinError(i));
176 last_err = hist->GetBinError(i);
182 if (last_err > 0.0) {
183 back->SetPoint(nPoint, x, y);
184 back->SetPointError(nPoint, 0, last_err);
187 if (x < lowest) { lowest = x; }
188 if (x > highest) {highest = x; }
194 if (opts.Contains(
"c")) {
195 linear =
new TF1(
"linear",
"[0]", lowest, highest);
196 double y1 = hist->GetBinContent(hist->FindBin(lowest));
197 double y2 = hist->GetBinContent(hist->FindBin(highest));
198 linear->SetParameter(0, (y1+y2)/2.0);
201 linear =
new TF1(
"linear",
"[0]+[1]*x", lowest, highest);
202 double x1 = hist->GetBinCenter(hist->FindBin(lowest));
203 double x2 = hist->GetBinCenter(hist->FindBin(highest));
204 double y1 = hist->GetBinContent(hist->FindBin(lowest));
205 double y2 = hist->GetBinContent(hist->FindBin(highest));
209 double m = (y2-y1)/(x2-x1);
210 double c = y2 - m*x2;
212 linear->SetParameter(0, c);
213 linear->SetParameter(1, m);
216 back->Fit(linear, (
"RQN"+(std::string)foption).c_str());
218 Set(hist, peak, linear);
220 linear->Draw(
"same");
229 TString opts(option);
231 if (opts.Contains(
"v")) {
232 std::cout <<
"Setting peak with regions:" << std::endl;
234 for (
auto &b : background) {
235 std::cout << b.GetLow() <<
" " << b.GetHigh() <<
" ";
237 std::cout << std::endl;
241 mBackgrounds = background;
243 TGraphErrors *back_low =
new TGraphErrors();
244 TGraphErrors *back_high =
new TGraphErrors();
245 TGraphErrors *backs[2] = {back_low, back_high};
247 double lowest[2]={1e9, 1e9};
248 double highest[2]={-1e9, -1e9};
249 for (
int b=0; b<background.size(); ++b) {
250 float av_pos = (background[b].GetLow() + background[b].GetHigh())/2.0;
251 if (av_pos < peak.
GetLow()) { back_ind = 0;}
252 else if (av_pos > peak.
GetHigh()) { back_ind = 1; }
253 else { std::cout <<
"Invalid background region, ignoring" << std::endl;
continue; }
255 int iStart = background[b].GetBinLow(hist);
256 int iStop = background[b].GetBinHigh(hist);
257 double last_err = -1.0;
258 for (
int i=iStart; i<=iStop; ++i) {
259 int nPoint = backs[back_ind]->GetN();
260 double x = hist->GetBinCenter(i);
261 double y = hist->GetBinContent(i);
262 if (hist->GetBinError(i) > 0.0) {
263 backs[back_ind]->SetPoint(nPoint, x, y);
264 backs[back_ind]->SetPointError(nPoint, 0, hist->GetBinError(i));
265 last_err = hist->GetBinError(i);
271 if (last_err > 0.0) {
272 backs[back_ind]->SetPoint(nPoint, x, y);
273 backs[back_ind]->SetPointError(nPoint, 0, last_err);
276 if (x < lowest[back_ind]) { lowest[back_ind] = x; }
277 if (x > highest[back_ind]) {highest[back_ind] = x; }
282 Highest = highest[1];
288 if (opts.Contains(
"c")) {
289 backlow =
new TF1(
"backlow",
"[0]", lowest[0], highest[0]);
290 double y1 = hist->GetBinContent(hist->FindBin(lowest[0]));
291 double y2 = hist->GetBinContent(hist->FindBin(highest[0]));
292 backlow->SetParameter(0, (y1+y2)/2.0);
293 backs[0]->Fit(backlow, (
"RQN"+(std::string)foption).c_str());
295 backhigh =
new TF1(
"backlow",
"[0]", lowest[1], highest[1]);
296 y1 = hist->GetBinContent(hist->FindBin(lowest[1]));
297 y2 = hist->GetBinContent(hist->FindBin(highest[1]));
298 backhigh->SetParameter(0, (y1+y2)/2.0);
299 backs[1]->Fit(backhigh, (
"RQN"+(std::string)foption).c_str());
303 backlow =
new TF1(
"backlow",
"[0]+[1]*x", lowest[0], highest[0]);
304 double x1 = hist->GetBinCenter(hist->FindBin(lowest[0]));
305 double x2 = hist->GetBinCenter(hist->FindBin(highest[0]));
306 double y1 = hist->GetBinContent(hist->FindBin(lowest[0]));
307 double y2 = hist->GetBinContent(hist->FindBin(highest[0]));
311 double m = (y2-y1)/(x2-x1);
312 double c = y2 - m*x2;
314 backlow->SetParameter(0, c);
315 backlow->SetParameter(1, m);
316 backs[0]->Fit(backlow, (
"RQN"+(std::string)foption).c_str());
318 backhigh =
new TF1(
"backhigh",
"[0]+[1]*x", lowest[1], highest[1]);
319 x1 = hist->GetBinCenter(hist->FindBin(lowest[1]));
320 x2 = hist->GetBinCenter(hist->FindBin(highest[1]));
321 y1 = hist->GetBinContent(hist->FindBin(lowest[1]));
322 y2 = hist->GetBinContent(hist->FindBin(highest[1]));
329 backhigh->SetParameter(0, c);
330 backhigh->SetParameter(1, m);
331 backs[1]->Fit(backhigh, (
"RQN"+(std::string)foption).c_str());
335 linear =
new TF1(
"linear",
"[0] + [1]*x", peak.
GetLow(), peak.
GetHigh());
336 double y1 = backlow->Eval(hist->GetBinCenter(hist->FindBin(peak.
GetLow())));
337 double y2 = backhigh->Eval(hist->GetBinCenter(hist->FindBin(peak.
GetHigh())));
338 double x1 = peak.
GetLow();
341 double m = (y2-y1)/(x2-x1);
342 double c = y2 - m*x2;
344 linear->SetParameter(0, c);
345 linear->SetParameter(1, m);
347 TGraphErrors *back_graph =
new TGraphErrors();
348 int i = hist->FindBin(x1);
349 double binWidth = hist->GetBinWidth(i);
350 double backErr = backlow->IntegralError(hist->GetBinLowEdge(i), hist->GetBinLowEdge(i)+binWidth)/binWidth;
351 back_graph->SetPoint(0, x1, y1);
352 back_graph->SetPointError(0, 0, backErr);
354 i = hist->FindBin(x2);
355 binWidth = hist->GetBinWidth(i);
356 backErr = backhigh->IntegralError(hist->GetBinLowEdge(i), hist->GetBinLowEdge(i)+binWidth)/binWidth;
357 back_graph->SetPoint(0, x2, y2);
358 back_graph->SetPointError(0, 0, backErr);
360 back_graph->Fit(linear, (
"RQN"+(std::string)foption).c_str());
362 Set(hist, peak, linear);
364 linear->Draw(
"same");
373 SetPoint(hist, peak, background, foption, option);
375 TString opts(option);
386 if (opts.Contains(
"c")) {
387 linear =
new TF1(
"linear",
"0.5 * ROOT::Math::erfc((x-[0])/(sqrt(2)*[1])) * ([2]) + (1.0 - 0.5 * ROOT::Math::erfc((x-[0])/(sqrt(2)*[1]))) * ([3])", Lowest, Highest);
389 linear->SetParameter(0, cent);
390 linear->SetParameter(1, sigma);
391 linear->SetParameter(2, backlow->GetParameter(0));
392 linear->SetParameter(3, backhigh->GetParameter(0));
394 linear->SetParError(0, cent_err);
395 linear->SetParError(1, sigma_err);
396 linear->SetParError(2, backlow->GetParError(0));
397 linear->SetParError(3, backhigh->GetParError(0));
400 linear =
new TF1(
"linear",
"0.5 * ROOT::Math::erfc((x-[0])/(sqrt(2)*[1])) * ([2] + [3]*x) + (1.0 - 0.5 * ROOT::Math::erfc((x-[0])/(sqrt(2)*[1]))) * ([4] + [5]*x)", Lowest, Highest);
402 linear->SetParameter(0, cent);
403 linear->SetParameter(1, sigma);
404 linear->SetParameter(2, backlow->GetParameter(0));
405 linear->SetParameter(3, backlow->GetParameter(1));
406 linear->SetParameter(4, backhigh->GetParameter(0));
407 linear->SetParameter(5, backhigh->GetParameter(1));
409 linear->SetParError(0, cent_err);
410 linear->SetParError(1, sigma_err);
411 linear->SetParError(2, backlow->GetParError(0));
412 linear->SetParError(3, backlow->GetParError(1));
413 linear->SetParError(4, backhigh->GetParError(0));
414 linear->SetParError(5, backhigh->GetParError(1));
416 Set(hist, peak, linear);
419 for (
int i=0; i<3; ++i) {
425 if (opts.Contains(
"c")) {
426 linear->SetParameter(0, cent);
427 linear->SetParameter(1, sigma);
428 linear->SetParameter(2, backlow->GetParameter(0));
429 linear->SetParameter(3, backhigh->GetParameter(0));
431 linear->SetParError(0, cent_err);
432 linear->SetParError(1, sigma_err);
433 linear->SetParError(2, backlow->GetParError(0));
434 linear->SetParError(3, backhigh->GetParError(0));
437 linear->SetParameter(0, cent);
438 linear->SetParameter(1, sigma);
439 linear->SetParameter(2, backlow->GetParameter(0));
440 linear->SetParameter(3, backlow->GetParameter(1));
441 linear->SetParameter(4, backhigh->GetParameter(0));
442 linear->SetParameter(5, backhigh->GetParameter(1));
444 linear->SetParError(0, cent_err);
445 linear->SetParError(1, sigma_err);
446 linear->SetParError(2, backlow->GetParError(0));
447 linear->SetParError(3, backlow->GetParError(1));
448 linear->SetParError(4, backhigh->GetParError(0));
449 linear->SetParError(5, backhigh->GetParError(1));
452 Set(hist, peak, linear);
455 linear->Draw(
"same");
569 void BPeak::Set(TH2 *hist, Option_t *foption, Option_t *option) {
571 std::cout <<
"Select prompt region: " << std::endl;
572 TH1D *projx = hist->ProjectionX();
574 prompt.
SetGate(gPad->GetCanvas(),
"x");
575 std::cout <<
"Select non-prompt regions: " << std::endl;
576 std::vector<GamR::TK::Gate > nonprompt;
579 int retval = np.
SetGate(gPad->GetCanvas(),
"x");
580 if (retval>0) {
break; }
581 nonprompt.push_back(np);
583 if (nonprompt.size() == 0) { std::cout <<
"Must have at least one non-prompt region!" << std::endl;
return; }
585 TH1D *hPrompt = prompt.
ApplyX(hist,
"hPrompt");
587 TH1D *hNonPrompt = nonprompt[0].ApplyX(hist,
"hNonPrompt0");
588 double np_width = nonprompt[0].GetBinWidth(projx);
589 for (
int i=1; i<nonprompt.size(); ++i) {
590 hNonPrompt->Add(nonprompt[i].ApplyX(hist, (
"hNonPrompt"+std::to_string(i)).c_str()));
591 np_width += nonprompt[i].GetBinWidth(projx);
593 hNonPrompt->Scale(p_width/np_width);
596 hPrompt->Draw(
"hist");
597 hNonPrompt->SetLineColor(kRed);
598 hNonPrompt->Draw(
"hist same");
600 BPeak::LFit lfit(hNonPrompt);
604 std::cout <<
"Select peak region: " << std::endl;
605 peak.
SetGate(gPad->GetCanvas(),
"x");
606 std::cout <<
"Select background regions:" << std::endl;
607 std::vector<GamR::TK::Gate > background;
609 double highest = -1e9;
612 int retval = bg.
SetGate(gPad->GetCanvas(),
"x");
613 if (retval>0) {
break; }
614 background.push_back(bg);
615 if (bg.
GetLow() < lowest) {
622 if (background.size() == 0) { std::cout <<
"Must have at least one background region!" << std::endl;
return; }
624 ROOT::Fit::DataRange range(background[0].GetLow(), background[0].GetHigh());
625 for (
int i=1; i<background.size(); ++i) {
626 range.AddRange(0, background[i].GetLow(), background[i].GetHigh());
630 linear =
new TF1(
"linear", lfit, lowest, highest, 2);
633 double x1 = hPrompt->GetBinCenter(hPrompt->FindBin(lowest));
634 double x2 = hPrompt->GetBinCenter(hPrompt->FindBin(highest));
635 double y1 = hPrompt->GetBinContent(hPrompt->FindBin(lowest)) - hNonPrompt->GetBinContent(hNonPrompt->FindBin(lowest));
636 double y2 = hPrompt->GetBinContent(hPrompt->FindBin(highest)) - hNonPrompt->GetBinContent(hNonPrompt->FindBin(highest));
640 double m = (y2-y1)/(x2-x1);
641 double c = y2 - m*x2;
643 linear->SetParameter(0, c);
644 linear->SetParameter(1, m);
646 ROOT::Fit::DataOptions opt;
647 opt.fUseEmpty =
true;
648 ROOT::Fit::BinData data(opt, range);
649 ROOT::Fit::FillData(data, hPrompt);
650 ROOT::Math::WrappedMultiTF1 fitFunc(*linear, linear->GetNdim());
651 ROOT::Fit::Fitter fitter;
652 fitter.SetFunction(fitFunc,
false);
653 fitter.LikelihoodFit(data);
655 fitter.Result().Print(std::cout);
658 Set(hPrompt, peak, linear);
660 TH2D* hBackSub = (TH2D*)hPrompt->Clone(
"hBackSub");
661 hBackSub->Add(hNonPrompt, -1.0);
662 hBackSub->Draw(
"hist");
664 TF1 *lindraw =
new TF1(
"lindraw",
"[0] + [1]*x", lowest, highest);
665 lindraw->SetParameter(0, linear->GetParameter(0));
666 lindraw->SetParameter(1, linear->GetParameter(1));
667 lindraw->Draw(
"same");