134 if(fitHist ==
nullptr) {
139 GetRange(xlow, xhigh);
140 Int_t bin = fitHist->FindBin(GetParameter(
"centroid"));
141 Int_t binlow = fitHist->GetXaxis()->FindBin(xlow);
142 Int_t binhigh = fitHist->GetXaxis()->FindBin(xhigh);
143 SetParLimits(0, 0, fitHist->GetMaximum());
146 GetParLimits(1, low, high);
147 if(low == high && low == 0.) {
148 SetParLimits(1, xlow, xhigh);
150 GetParLimits(2, low, high);
151 if(low == high && low == 0.) {
152 SetParLimits(2, 0.5, (xhigh - xlow));
154 SetParLimits(3, 0.000001, 10);
155 SetParLimits(4, 0.000001, 100);
157 SetParLimits(5, 0.0, 1.0E2);
158 SetParLimits(6, 0.0, fitHist->GetBinContent(bin) * 100.);
159 SetParLimits(9, xlow, xhigh);
161 if((fitHist ==
nullptr) && (
GetHist() !=
nullptr)) {
165 if(fitHist ==
nullptr) {
166 std::cout <<
"No histogram is associated yet, no initial guesses made" << std::endl;
174 SetParameter(
"Height", fitHist->GetBinContent(bin));
175 SetParameter(
"centroid", GetParameter(
"centroid"));
176 SetParameter(
"sigma", TMath::Sqrt(9.0 + 4. * GetParameter(
"centroid") / 1000.) / 2.35);
177 SetParameter(
"beta", GetParameter(
"sigma") / 2.0);
178 SetParameter(
"R", 0.001);
179 SetParameter(
"step", 1.0);
180 SetParameter(
"A", fitHist->GetBinContent(binhigh));
181 SetParameter(
"B", (fitHist->GetBinContent(binlow) - fitHist->GetBinContent(binhigh)) / (xlow - xhigh));
182 SetParameter(
"C", 0.0000);
183 SetParameter(
"bg_offset", GetParameter(
"centroid"));
184 FixParameter(8, 0.00);
185 FixParameter(3, GetParameter(
"beta"));
186 FixParameter(4, 0.00);
193 TString options = opt;
195 bool quiet = options.Contains(
"q");
196 bool verbose = options.Contains(
"v");
197 if(quiet && verbose) {
198 std::cout <<
"Don't know how to be quiet and verbose at once (" << opt <<
"), going to be verbose!" << std::endl;
201 bool retryFit = options.Contains(
"retryfit");
202 options.ReplaceAll(
"retryfit",
"");
203 if(!verbose && !quiet) { options.Append(
"q"); }
205 if(fitHist ==
nullptr &&
GetHist() ==
nullptr) {
206 std::cout <<
"No hist passed, trying something... ";
207 fitHist = fHistogram;
209 if(fitHist ==
nullptr) {
210 std::cout <<
"No histogram associated with Peak" << std::endl;
216 ROOT::Math::MinimizerOptions::SetDefaultMinimizer(
"Minuit2",
"Combination");
217 TVirtualFitter::SetMaxIterations(100000);
218 TVirtualFitter::SetPrecision(1e-3);
225 std::vector<double> lowerLimit(GetNpar());
226 std::vector<double> upperLimit(GetNpar());
227 for(
int i = 0; i < GetNpar(); ++i) {
228 GetParLimits(i, lowerLimit[i], upperLimit[i]);
230 FixParameter(i, GetParameter(i));
234 TFitResultPtr fitres;
237 fitres = fitHist->Fit(
this, Form(
"%sRLSN", options.Data()));
239 fitres = fitHist->Fit(
this, Form(
"%sRSN", options.Data()));
243 if(
static_cast<int>(fitres) == -1) {
return false; }
245 for(
int i = 0; i < GetNpar(); ++i) {
246 SetParLimits(i, lowerLimit[i], upperLimit[i]);
251 fitres = fitHist->Fit(
this, Form(
"%sRLS", options.Data()));
253 fitres = fitHist->Fit(
this, Form(
"%sRS", options.Data()));
257 if(
static_cast<int>(fitres) == -1) {
return false; }
262 if(fitres->ParError(2) != fitres->ParError(2)) {
263 if(fitres->Parameter(3) < 1) {
267 if(verbose) { std::cout <<
"Beta may have broken the fit, retrying with R=0" << std::endl; }
269 fitHist->GetListOfFunctions()->Last()->Delete();
271 fitres = fitHist->Fit(
this, Form(
"%sRLS", options.Data()));
273 fitres = fitHist->Fit(
this, Form(
"%sRS", options.Data()));
282 if(!quiet) { std::cout <<
GREEN <<
"Re-fitting with released parameters (without any limits)" <<
RESET_COLOR << std::endl; }
283 for(
int i = 0; i < GetNpar(); ++i) {
287 fitres = fitHist->Fit(
this, Form(
"%sRLS", options.Data()));
289 fitres = fitHist->Fit(
this, Form(
"%sRS", options.Data()));
293 if(!quiet) { std::cout <<
YELLOW <<
"Re-fitting with \"E\" option to get better error estimation using Minos technique." <<
RESET_COLOR << std::endl; }
295 fitres = fitHist->Fit(
this, Form(
"%sERLS", options.Data()));
297 fitres = fitHist->Fit(
this, Form(
"%sERS", options.Data()));
303 Double_t binWidth = fitHist->GetBinWidth(1);
304 Double_t width = GetParameter(
"sigma");
306 std::cout <<
"Chi^2/NDF = " << fitres->Chi2() / fitres->Ndf() << std::endl;
308 fChi2 = fitres->Chi2();
309 fNdf = fitres->Ndf();
312 GetRange(xlow, xhigh);
313 Double_t int_low = xlow - 10. * width;
314 Double_t int_high = xhigh + 10. * width;
319 auto* tmppeak =
new TPeak;
321 tmppeak->SetParameter(
"step", 0.0);
322 tmppeak->SetParameter(
"A", 0.0);
323 tmppeak->SetParameter(
"B", 0.0);
324 tmppeak->SetParameter(
"C", 0.0);
325 tmppeak->SetParameter(
"bg_offset", 0.0);
326 tmppeak->SetRange(int_low, int_high);
327 tmppeak->SetName(
"tmppeak");
331 fArea = (tmppeak->Integral(int_low, int_high)) / binWidth;
333 TMatrixDSym CovMat = fitres->GetCovarianceMatrix();
339 fDArea = (tmppeak->IntegralError(int_low, int_high, tmppeak->GetParameters(), CovMat.GetMatrixArray())) / binWidth;
342 std::cout <<
"Integral: " <<
fArea <<
" +/- " <<
fDArea << std::endl;
347 Copy(*fitHist->GetListOfFunctions()->Last());
350 if(!quiet) {
Print(
"+"); }
391 std::cout <<
"No hist set" << std::endl;
394 if(
fChi2 < 0.000000001) {
395 std::cout <<
"No fit performed" << std::endl;
401 GetRange(xlow, xhigh);
402 Int_t nbins =
GetHist()->GetXaxis()->GetNbins();
403 auto* res =
new Double_t[nbins];
404 auto* bin =
new Double_t[nbins];
408 for(
int i = 1; i <= nbins; i++) {
409 if(
GetHist()->GetBinCenter(i) <= xlow ||
GetHist()->GetBinCenter(i) >= xhigh) {
412 res[points] = (
GetHist()->GetBinContent(i) - Eval(
GetHist()->GetBinCenter(i))) +
413 GetParameter(
"Height") / 2;
414 bin[points] =
GetHist()->GetBinCenter(i);
460 Int_t binlow =
hist->FindBin(int_low);
461 Int_t binhigh =
hist->FindBin(int_high);
462 Double_t binWidth =
hist->GetBinWidth(binlow);
463 Double_t hist_integral =
hist->Integral(binlow, binhigh);
464 Double_t xlow =
hist->GetXaxis()->GetBinLowEdge(binlow);
465 Double_t xhigh =
hist->GetXaxis()->GetBinUpEdge(binhigh);
468 Double_t bg_area = (
Background()->Integral(xlow, xhigh)) / binWidth;
471 Double_t peakarea = hist_integral - bg_area;
484 Int_t binlow =
hist->FindBin(int_low);
485 Int_t binhigh =
hist->FindBin(int_high);
486 Double_t binWidth =
hist->GetBinWidth(binlow);
487 Double_t hist_integral =
hist->Integral(binlow, binhigh);
488 Double_t xlow =
hist->GetXaxis()->GetBinLowEdge(binlow);
489 Double_t xhigh =
hist->GetXaxis()->GetBinUpEdge(binhigh);
492 Double_t bg_area = (
Background()->Integral(xlow, xhigh)) / binWidth;
495 Double_t peakerr = sqrt(hist_integral + bg_area);