139 if(fitHist ==
nullptr) {
144 GetRange(xlow, xhigh);
145 Int_t bin = fitHist->FindBin(GetParameter(
"centroid"));
146 Int_t binlow = fitHist->GetXaxis()->FindBin(xlow);
147 Int_t binhigh = fitHist->GetXaxis()->FindBin(xhigh);
148 SetParLimits(0, 0, fitHist->GetMaximum());
151 GetParLimits(1, low, high);
152 if(low == high && low == 0.) {
153 SetParLimits(1, xlow, xhigh);
155 GetParLimits(2, low, high);
156 if(low == high && low == 0.) {
157 SetParLimits(2, 0.5, (xhigh - xlow));
159 SetParLimits(3, 0.000001, 10);
160 SetParLimits(4, 0.000001, 100);
162 SetParLimits(5, 0.0, 1.0E2);
163 SetParLimits(6, 0.0, fitHist->GetBinContent(bin) * 100.);
164 SetParLimits(9, xlow, xhigh);
166 if((fitHist ==
nullptr) && (
GetHist() !=
nullptr)) {
170 if(fitHist ==
nullptr) {
171 std::cout <<
"No histogram is associated yet, no initial guesses made" << std::endl;
179 SetParameter(
"Height", fitHist->GetBinContent(bin));
180 SetParameter(
"centroid", GetParameter(
"centroid"));
181 SetParameter(
"sigma", TMath::Sqrt(9.0 + 4. * GetParameter(
"centroid") / 1000.) / 2.35);
182 SetParameter(
"beta", GetParameter(
"sigma") / 2.0);
183 SetParameter(
"R", 0.001);
184 SetParameter(
"step", 1.0);
185 SetParameter(
"A", fitHist->GetBinContent(binhigh));
186 SetParameter(
"B", (fitHist->GetBinContent(binlow) - fitHist->GetBinContent(binhigh)) / (xlow - xhigh));
187 SetParameter(
"C", 0.0000);
188 SetParameter(
"bg_offset", GetParameter(
"centroid"));
189 FixParameter(8, 0.00);
190 FixParameter(3, GetParameter(
"beta"));
191 FixParameter(4, 0.00);
198 TString options = opt;
200 bool quiet = options.Contains(
"q");
201 bool verbose = options.Contains(
"v");
202 if(quiet && verbose) {
203 std::cout <<
"Don't know how to be quiet and verbose at once (" << opt <<
"), going to be verbose!" << std::endl;
206 bool retryFit = options.Contains(
"retryfit");
207 options.ReplaceAll(
"retryfit",
"");
208 if(!verbose && !quiet) { options.Append(
"q"); }
210 if(fitHist ==
nullptr &&
GetHist() ==
nullptr) {
211 std::cout <<
"No hist passed, trying something... ";
212 fitHist = fHistogram;
214 if(fitHist ==
nullptr) {
215 std::cout <<
"No histogram associated with Peak" << std::endl;
221 ROOT::Math::MinimizerOptions::SetDefaultMinimizer(
"Minuit2",
"Combination");
222 TVirtualFitter::SetMaxIterations(100000);
223 TVirtualFitter::SetPrecision(1e-3);
230 std::vector<double> lowerLimit(GetNpar());
231 std::vector<double> upperLimit(GetNpar());
232 for(
int i = 0; i < GetNpar(); ++i) {
233 GetParLimits(i, lowerLimit[i], upperLimit[i]);
235 FixParameter(i, GetParameter(i));
239 TFitResultPtr fitres;
242 fitres = fitHist->Fit(
this, Form(
"%sRLSN", options.Data()));
244 fitres = fitHist->Fit(
this, Form(
"%sRSN", options.Data()));
248 if(
static_cast<int>(fitres) == -1) {
return false; }
250 for(
int i = 0; i < GetNpar(); ++i) {
251 SetParLimits(i, lowerLimit[i], upperLimit[i]);
256 fitres = fitHist->Fit(
this, Form(
"%sRLS", options.Data()));
258 fitres = fitHist->Fit(
this, Form(
"%sRS", options.Data()));
262 if(
static_cast<int>(fitres) == -1) {
return false; }
267 if(fitres->ParError(2) != fitres->ParError(2)) {
268 if(fitres->Parameter(3) < 1) {
272 if(verbose) { std::cout <<
"Beta may have broken the fit, retrying with R=0" << std::endl; }
274 fitHist->GetListOfFunctions()->Last()->Delete();
276 fitres = fitHist->Fit(
this, Form(
"%sRLS", options.Data()));
278 fitres = fitHist->Fit(
this, Form(
"%sRS", options.Data()));
287 if(!quiet) { std::cout <<
GREEN <<
"Re-fitting with released parameters (without any limits)" <<
RESET_COLOR << std::endl; }
288 for(
int i = 0; i < GetNpar(); ++i) {
292 fitres = fitHist->Fit(
this, Form(
"%sRLS", options.Data()));
294 fitres = fitHist->Fit(
this, Form(
"%sRS", options.Data()));
298 if(!quiet) { std::cout <<
YELLOW <<
"Re-fitting with \"E\" option to get better error estimation using Minos technique." <<
RESET_COLOR << std::endl; }
300 fitres = fitHist->Fit(
this, Form(
"%sERLS", options.Data()));
302 fitres = fitHist->Fit(
this, Form(
"%sERS", options.Data()));
308 Double_t binWidth = fitHist->GetBinWidth(1);
309 Double_t width = GetParameter(
"sigma");
311 std::cout <<
"Chi^2/NDF = " << fitres->Chi2() / fitres->Ndf() << std::endl;
313 fChi2 = fitres->Chi2();
314 fNdf = fitres->Ndf();
317 GetRange(xlow, xhigh);
318 Double_t int_low = xlow - 10. * width;
319 Double_t int_high = xhigh + 10. * width;
324 auto* tmppeak =
new TPeak;
326 tmppeak->SetParameter(
"step", 0.0);
327 tmppeak->SetParameter(
"A", 0.0);
328 tmppeak->SetParameter(
"B", 0.0);
329 tmppeak->SetParameter(
"C", 0.0);
330 tmppeak->SetParameter(
"bg_offset", 0.0);
331 tmppeak->SetRange(int_low, int_high);
332 tmppeak->SetName(
"tmppeak");
336 fArea = (tmppeak->Integral(int_low, int_high)) / binWidth;
338 TMatrixDSym CovMat = fitres->GetCovarianceMatrix();
344 fDArea = (tmppeak->IntegralError(int_low, int_high, tmppeak->GetParameters(), CovMat.GetMatrixArray())) / binWidth;
347 std::cout <<
"Integral: " <<
fArea <<
" +/- " <<
fDArea << std::endl;
352 Copy(*fitHist->GetListOfFunctions()->Last());
355 if(!quiet) {
Print(
"+"); }
396 std::cout <<
"No hist set" << std::endl;
399 if(
fChi2 < 0.000000001) {
400 std::cout <<
"No fit performed" << std::endl;
406 GetRange(xlow, xhigh);
407 Int_t nbins =
GetHist()->GetXaxis()->GetNbins();
408 auto* res =
new Double_t[nbins];
409 auto* bin =
new Double_t[nbins];
413 for(
int i = 1; i <= nbins; i++) {
414 if(
GetHist()->GetBinCenter(i) <= xlow ||
GetHist()->GetBinCenter(i) >= xhigh) {
417 res[points] = (
GetHist()->GetBinContent(i) - Eval(
GetHist()->GetBinCenter(i))) +
418 GetParameter(
"Height") / 2;
419 bin[points] =
GetHist()->GetBinCenter(i);
465 Int_t binlow =
hist->FindBin(int_low);
466 Int_t binhigh =
hist->FindBin(int_high);
467 Double_t binWidth =
hist->GetBinWidth(binlow);
468 Double_t hist_integral =
hist->Integral(binlow, binhigh);
469 Double_t xlow =
hist->GetXaxis()->GetBinLowEdge(binlow);
470 Double_t xhigh =
hist->GetXaxis()->GetBinUpEdge(binhigh);
473 Double_t bg_area = (
Background()->Integral(xlow, xhigh)) / binWidth;
476 Double_t peakarea = hist_integral - bg_area;
489 Int_t binlow =
hist->FindBin(int_low);
490 Int_t binhigh =
hist->FindBin(int_high);
491 Double_t binWidth =
hist->GetBinWidth(binlow);
492 Double_t hist_integral =
hist->Integral(binlow, binhigh);
493 Double_t xlow =
hist->GetXaxis()->GetBinLowEdge(binlow);
494 Double_t xhigh =
hist->GetXaxis()->GetBinUpEdge(binhigh);
497 Double_t bg_area = (
Background()->Integral(xlow, xhigh)) / binWidth;
500 Double_t peakerr = sqrt(hist_integral + bg_area);