97 std::cout <<
"No peaks provided!" << std::endl;
101 TString options = opt;
103 bool quiet = options.Contains(
"q");
104 bool verbose = options.Contains(
"v");
105 if(quiet && verbose) {
106 std::cout <<
"Don't know how to be quiet and verbose at once (" << opt <<
"), going to be verbose!" << std::endl;
109 bool retryFit = options.Contains(
"retryfit");
110 options.ReplaceAll(
"retryfit",
"");
113 int firstBin = fit_hist->GetXaxis()->GetFirst();
114 int lastBin = fit_hist->GetXaxis()->GetLast();
118 ROOT::Math::MinimizerOptions::SetDefaultMinimizer(
"Minuit2",
"Combination");
119 TVirtualFitter::SetMaxIterations(100000);
120 TVirtualFitter::SetPrecision(1e-4);
129 if(verbose) { std::cout <<
"Initializing Fit...." << std::endl; }
142 TFitResultPtr fit_res = fit_hist->Fit(
fTotalFitFunction, Form(
"SRI%s", options.Data()));
148 if(!quiet) { std::cout <<
GREEN <<
"Re-fitting with released parameters (without any limits)" <<
RESET_COLOR << std::endl; }
152 if(i == 1) {
continue; }
163 if(!quiet) { std::cout <<
YELLOW <<
"Re-fitting with \"E\" option to get better error estimation using Minos technique." <<
RESET_COLOR << std::endl; }
168 if(fit_res.Get() !=
nullptr) {
174 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;
176 fit_hist->Draw(
"hist");
182 std::cout <<
"****************" << std::endl
183 <<
"Summary of Fit: " << std::endl;
187 fit_hist->GetXaxis()->SetRange(firstBin, lastBin);
189 ROOT::Math::MinimizerOptions::SetDefaultMinimizer(
"Minuit2",
"Combination");
198 Int_t param_counter = 0;
205 TF1* peak_func = p_it->GetFitFunction();
210 TMatrixDSym covariance_matrix = fit_res->GetCovarianceMatrix();
211 bool goodCovarianceMatrix =
true;
212 if(covariance_matrix.GetNrows() < peak_func->GetNpar() || covariance_matrix.GetNcols() < peak_func->GetNpar()) {
213 goodCovarianceMatrix =
false;
217 Int_t param_to_zero_counter = 0;
218 std::vector<Int_t> param_to_zero_list;
220 if(other_p_it != p_it) {
221 for(
int i = 0; i < other_p_it->GetFitFunction()->GetNpar(); ++i) {
222 param_to_zero_list.push_back(param_to_zero_counter);
223 ++param_to_zero_counter;
226 for(
int i = 0; i < peak_func->GetNpar(); ++i) {
227 if(p_it->IsBackgroundParameter(i)) {
228 param_to_zero_list.push_back(param_to_zero_counter);
230 ++param_to_zero_counter;
232 Double_t low_range = 0.;
233 Double_t high_range = 0.;
235 peak_func->SetRange(low_range, high_range);
240 param_to_zero_list.push_back(i);
244 for(
auto i : param_to_zero_list) {
245 for(
auto j : param_to_zero_list) {
246 if(goodCovarianceMatrix) { covariance_matrix(i, j) = 0.0; }
248 total_function_copy->SetParameter(i, 0.0);
250 if(peak_func !=
nullptr) {
251 for(
int i = 0; i < peak_func->GetNpar(); ++i) {
257 peak_func->SetParLimits(i, low, high);
261 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));
262 if(goodCovarianceMatrix) {
263 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));
265 std::cout <<
"Not setting area error because we don't have a good covariance matrix!" << std::endl;
268 total_function_copy->Delete();
271 for(
int i = 0; i <
fBGToFit->GetNpar(); ++i) {
279 p_it->SetGlobalBackground(
new TF1(*global_bg));
297 Int_t param_counter = 0;
298 Int_t peak_counter = 0;
300 TF1* peak_func = p_it->GetFitFunction();
301 if(peak_func !=
nullptr) {
302 for(
int i = 0; i < peak_func->GetNpar(); ++i) {
303 fTotalFitFunction->SetParName(param_counter, Form(
"%s_%i", peak_func->GetParName(i), peak_counter));
306 Double_t limit_low = 0.;
307 Double_t limit_high = 0.;
308 peak_func->GetParLimits(i, limit_low, limit_high);
316 for(
int i = 0; i <
fBGToFit->GetNpar(); ++i) {
320 Double_t limit_low = 0.;
321 Double_t limit_high = 0.;
322 fBGToFit->GetParLimits(i, limit_low, limit_high);
333 Int_t params_so_far = 0;
336 TF1* peakFunction = p_it->GetFitFunction();
337 if(peakFunction ==
nullptr) {
338 std::cerr <<
"Failed to get fit function for peak from " << p_it <<
" at " << p_it->Centroid() << std::endl;
341 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; }
342 sum += peakFunction->EvalPar(dim, &par[params_so_far]);
343 params_so_far += peakFunction->GetNpar();
345 sum +=
fBGToFit->EvalPar(dim, &par[params_so_far]);
353 Int_t params_so_far = 0;
355 TF1* peak_func = p_it->GetBackgroundFunction();
356 sum += peak_func->EvalPar(dim, &par[params_so_far]);
357 params_so_far += peak_func->GetNpar();
359 sum +=
fBGToFit->EvalPar(dim, &par[params_so_far]);
369 fBGToFit->SetParName(3,
"bg_offset");
371 Double_t lowLimit = 0.;
372 Double_t highLimit = 0.;
373 fBGToFit->GetParLimits(0, lowLimit, highLimit);
375 double value =
fBGToFit->GetParameter(0);
376 if(value == 0 && lowLimit == 0 && highLimit == 0) {
377 fBGToFit->SetParLimits(0, 0.0, fit_hist->GetBinContent(fit_hist->FindBin(
fRangeHigh)) * 100.);
380 fBGToFit->GetParLimits(1, lowLimit, highLimit);
383 if(value == 0 && lowLimit == 0 && highLimit == 0) {
386 fBGToFit->GetParLimits(2, lowLimit, highLimit);
389 if(value == 0 && lowLimit == 0 && highLimit == 0) {
390 fBGToFit->SetParameter(
"C", 0.0000);
393 fBGToFit->GetParLimits(3, lowLimit, highLimit);
396 if(value == 0 && lowLimit == 0 && highLimit == 0) {
420 TF1* bg_to_draw =
new TF1;
422 bg_to_draw->SetLineColor(
static_cast<Color_t
>(kRed +
fColorIndex));
425 TF1* peak_func = p_it->GetFitFunction();
426 TF1* total_function_copy =
new TF1;
430 Int_t param_to_zero_counter = 0;
432 if(other_p_it != p_it) {
433 for(
int i = 0; i < other_p_it->GetFitFunction()->GetNpar(); ++i) {
434 total_function_copy->SetParameter(param_to_zero_counter, 0.0);
435 ++param_to_zero_counter;
438 for(
int i = 0; i < peak_func->GetNpar(); ++i) {
439 if(!p_it->IsBackgroundParameter(i)) {
440 bg_to_draw->SetParameter(param_to_zero_counter, 0.0);
442 ++param_to_zero_counter;
446 total_function_copy->Draw(
"same");
448 bg_to_draw->Draw(
"same");