94 std::cout <<
"No peaks provided!" << std::endl;
98 TString options = opt;
100 bool quiet = options.Contains(
"q");
101 bool verbose = options.Contains(
"v");
102 if(quiet && verbose) {
103 std::cout <<
"Don't know how to be quiet and verbose at once (" << opt <<
"), going to be verbose!" << std::endl;
106 bool retryFit = options.Contains(
"retryfit");
107 options.ReplaceAll(
"retryfit",
"");
110 int firstBin = fit_hist->GetXaxis()->GetFirst();
111 int lastBin = fit_hist->GetXaxis()->GetLast();
115 ROOT::Math::MinimizerOptions::SetDefaultMinimizer(
"Minuit2",
"Combination");
116 TVirtualFitter::SetMaxIterations(100000);
117 TVirtualFitter::SetPrecision(1e-4);
126 if(verbose) { std::cout <<
"Initializing Fit...." << std::endl; }
139 TFitResultPtr fit_res = fit_hist->Fit(
fTotalFitFunction, Form(
"SRI%s", options.Data()));
145 if(!quiet) { std::cout <<
GREEN <<
"Re-fitting with released parameters (without any limits)" <<
RESET_COLOR << std::endl; }
149 if(i == 1) {
continue; }
160 if(!quiet) { std::cout <<
YELLOW <<
"Re-fitting with \"E\" option to get better error estimation using Minos technique." <<
RESET_COLOR << std::endl; }
165 if(fit_res.Get() !=
nullptr) {
171 std::cout <<
RED <<
"Failed to get fit result, be aware that the results and especially the error estimates might be wrong!" <<
RESET_COLOR << std::endl;
173 fit_hist->Draw(
"hist");
179 std::cout <<
"****************" << std::endl
180 <<
"Summary of Fit: " << std::endl;
184 fit_hist->GetXaxis()->SetRange(firstBin, lastBin);
186 ROOT::Math::MinimizerOptions::SetDefaultMinimizer(
"Minuit2",
"Combination");
195 Int_t param_counter = 0;
202 TF1* peak_func = p_it->GetFitFunction();
207 TMatrixDSym covariance_matrix = fit_res->GetCovarianceMatrix();
208 bool goodCovarianceMatrix =
true;
209 if(covariance_matrix.GetNrows() < peak_func->GetNpar() || covariance_matrix.GetNcols() < peak_func->GetNpar()) {
210 goodCovarianceMatrix =
false;
214 Int_t param_to_zero_counter = 0;
215 std::vector<Int_t> param_to_zero_list;
217 if(other_p_it != p_it) {
218 for(
int i = 0; i < other_p_it->GetFitFunction()->GetNpar(); ++i) {
219 param_to_zero_list.push_back(param_to_zero_counter);
220 ++param_to_zero_counter;
223 for(
int i = 0; i < peak_func->GetNpar(); ++i) {
224 if(p_it->IsBackgroundParameter(i)) {
225 param_to_zero_list.push_back(param_to_zero_counter);
227 ++param_to_zero_counter;
229 Double_t low_range = 0.;
230 Double_t high_range = 0.;
232 peak_func->SetRange(low_range, high_range);
237 param_to_zero_list.push_back(i);
241 for(
auto i : param_to_zero_list) {
242 for(
auto j : param_to_zero_list) {
243 if(goodCovarianceMatrix) { covariance_matrix(i, j) = 0.0; }
245 total_function_copy->SetParameter(i, 0.0);
247 if(peak_func !=
nullptr) {
248 for(
int i = 0; i < peak_func->GetNpar(); ++i) {
254 peak_func->SetParLimits(i, low, high);
258 p_it->SetArea(total_function_copy->Integral(p_it->Centroid() - p_it->Width() * 5., p_it->Centroid() + p_it->Width() * 5., 1e-8) / fit_hist->GetBinWidth(1));
259 if(goodCovarianceMatrix) {
260 p_it->SetAreaErr(total_function_copy->IntegralError(p_it->Centroid() - p_it->Width() * 5., p_it->Centroid() + p_it->Width() * 5., total_function_copy->GetParameters(), covariance_matrix.GetMatrixArray(), 1E-5) / fit_hist->GetBinWidth(1));
262 std::cout <<
"Not setting area error because we don't have a good covariance matrix!" << std::endl;
265 total_function_copy->Delete();
268 for(
int i = 0; i <
fBGToFit->GetNpar(); ++i) {
276 p_it->SetGlobalBackground(
new TF1(*global_bg));
294 Int_t param_counter = 0;
295 Int_t peak_counter = 0;
297 TF1* peak_func = p_it->GetFitFunction();
298 if(peak_func !=
nullptr) {
299 for(
int i = 0; i < peak_func->GetNpar(); ++i) {
300 fTotalFitFunction->SetParName(param_counter, Form(
"%s_%i", peak_func->GetParName(i), peak_counter));
303 Double_t limit_low = 0.;
304 Double_t limit_high = 0.;
305 peak_func->GetParLimits(i, limit_low, limit_high);
313 for(
int i = 0; i <
fBGToFit->GetNpar(); ++i) {
317 Double_t limit_low = 0.;
318 Double_t limit_high = 0.;
319 fBGToFit->GetParLimits(i, limit_low, limit_high);
330 Int_t params_so_far = 0;
333 TF1* peakFunction = p_it->GetFitFunction();
334 if(peakFunction ==
nullptr) {
335 std::cerr <<
"Failed to get fit function for peak from " << p_it <<
" at " << p_it->Centroid() << std::endl;
338 if(
fVerboseLevel >=
EVerbosity::kLoops) { std::cout <<
"Evaluating fit function " << peakFunction <<
" using " << peakFunction->GetNpar() <<
" parameters starting at " << params_so_far <<
" (" << &par[params_so_far] <<
")" << std::endl; }
339 sum += peakFunction->EvalPar(dim, &par[params_so_far]);
340 params_so_far += peakFunction->GetNpar();
342 sum +=
fBGToFit->EvalPar(dim, &par[params_so_far]);
350 Int_t params_so_far = 0;
352 TF1* peak_func = p_it->GetBackgroundFunction();
353 sum += peak_func->EvalPar(dim, &par[params_so_far]);
354 params_so_far += peak_func->GetNpar();
356 sum +=
fBGToFit->EvalPar(dim, &par[params_so_far]);
366 fBGToFit->SetParName(3,
"bg_offset");
368 Double_t lowLimit = 0.;
369 Double_t highLimit = 0.;
370 fBGToFit->GetParLimits(0, lowLimit, highLimit);
372 double value =
fBGToFit->GetParameter(0);
373 if(value == 0 && lowLimit == 0 && highLimit == 0) {
374 fBGToFit->SetParLimits(0, 0.0, fit_hist->GetBinContent(fit_hist->FindBin(
fRangeHigh)) * 100.);
377 fBGToFit->GetParLimits(1, lowLimit, highLimit);
380 if(value == 0 && lowLimit == 0 && highLimit == 0) {
383 fBGToFit->GetParLimits(2, lowLimit, highLimit);
386 if(value == 0 && lowLimit == 0 && highLimit == 0) {
387 fBGToFit->SetParameter(
"C", 0.0000);
390 fBGToFit->GetParLimits(3, lowLimit, highLimit);
393 if(value == 0 && lowLimit == 0 && highLimit == 0) {
417 TF1* bg_to_draw =
new TF1;
419 bg_to_draw->SetLineColor(
static_cast<Color_t
>(kRed +
fColorIndex));
422 TF1* peak_func = p_it->GetFitFunction();
423 TF1* total_function_copy =
new TF1;
427 Int_t param_to_zero_counter = 0;
429 if(other_p_it != p_it) {
430 for(
int i = 0; i < other_p_it->GetFitFunction()->GetNpar(); ++i) {
431 total_function_copy->SetParameter(param_to_zero_counter, 0.0);
432 ++param_to_zero_counter;
435 for(
int i = 0; i < peak_func->GetNpar(); ++i) {
436 if(!p_it->IsBackgroundParameter(i)) {
437 bg_to_draw->SetParameter(param_to_zero_counter, 0.0);
439 ++param_to_zero_counter;
443 total_function_copy->Draw(
"same");
445 bg_to_draw->Draw(
"same");