83 std::cout <<
"No peaks provided!" << std::endl;
87 TString options = opt;
89 bool quiet = options.Contains(
"q");
90 bool verbose = options.Contains(
"v");
91 if(quiet && verbose) {
92 std::cout <<
"Don't know how to be quiet and verbose at once (" << opt <<
"), going to be verbose!" << std::endl;
95 bool retryFit = options.Contains(
"retryfit");
96 options.ReplaceAll(
"retryfit",
"");
99 int firstBin = fit_hist->GetXaxis()->GetFirst();
100 int lastBin = fit_hist->GetXaxis()->GetLast();
104 ROOT::Math::MinimizerOptions::SetDefaultMinimizer(
"Minuit2",
"Combination");
105 TVirtualFitter::SetMaxIterations(100000);
106 TVirtualFitter::SetPrecision(1e-4);
115 if(verbose) { std::cout <<
"Initializing Fit...." << std::endl; }
128 TFitResultPtr fit_res = fit_hist->Fit(
fTotalFitFunction, Form(
"SRI%s", options.Data()));
134 if(!quiet) { std::cout <<
GREEN <<
"Re-fitting with released parameters (without any limits)" <<
RESET_COLOR << std::endl; }
136 if(i == 1) {
continue; }
143 if(!quiet) { std::cout <<
YELLOW <<
"Re-fitting with \"E\" option to get better error estimation using Minos technique." <<
RESET_COLOR << std::endl; }
148 if(fit_res.Get() !=
nullptr) {
154 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;
156 fit_hist->Draw(
"hist");
162 std::cout <<
"****************" << std::endl
163 <<
"Summary of Fit: " << std::endl;
167 fit_hist->GetXaxis()->SetRange(firstBin, lastBin);
169 ROOT::Math::MinimizerOptions::SetDefaultMinimizer(
"Minuit2",
"Combination");
178 Int_t param_counter = 0;
185 TF1* peak_func = p_it->GetFitFunction();
190 TMatrixDSym covariance_matrix = fit_res->GetCovarianceMatrix();
191 bool goodCovarianceMatrix =
true;
192 if(covariance_matrix.GetNrows() < peak_func->GetNpar() || covariance_matrix.GetNcols() < peak_func->GetNpar()) {
193 goodCovarianceMatrix =
false;
197 Int_t param_to_zero_counter = 0;
198 std::vector<Int_t> param_to_zero_list;
200 if(other_p_it != p_it) {
201 for(
int i = 0; i < other_p_it->GetFitFunction()->GetNpar(); ++i) {
202 param_to_zero_list.push_back(param_to_zero_counter);
203 ++param_to_zero_counter;
206 for(
int i = 0; i < peak_func->GetNpar(); ++i) {
207 if(p_it->IsBackgroundParameter(i)) {
208 param_to_zero_list.push_back(param_to_zero_counter);
210 ++param_to_zero_counter;
212 Double_t low_range = 0.;
213 Double_t high_range = 0.;
215 peak_func->SetRange(low_range, high_range);
220 param_to_zero_list.push_back(i);
224 for(
auto i : param_to_zero_list) {
225 for(
auto j : param_to_zero_list) {
226 if(goodCovarianceMatrix) { covariance_matrix(i, j) = 0.0; }
228 total_function_copy->SetParameter(i, 0.0);
230 if(peak_func !=
nullptr) {
231 for(
int i = 0; i < peak_func->GetNpar(); ++i) {
237 peak_func->SetParLimits(i, low, high);
241 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));
242 if(goodCovarianceMatrix) {
243 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));
245 std::cout <<
"Not setting area error because we don't have a good covariance matrix!" << std::endl;
248 total_function_copy->Delete();
251 for(
int i = 0; i <
fBGToFit->GetNpar(); ++i) {
259 p_it->SetGlobalBackground(
new TF1(*global_bg));
277 Int_t param_counter = 0;
278 Int_t peak_counter = 0;
280 TF1* peak_func = p_it->GetFitFunction();
281 if(peak_func !=
nullptr) {
282 for(
int i = 0; i < peak_func->GetNpar(); ++i) {
283 fTotalFitFunction->SetParName(param_counter, Form(
"%s_%i", peak_func->GetParName(i), peak_counter));
286 Double_t limit_low = 0.;
287 Double_t limit_high = 0.;
288 peak_func->GetParLimits(i, limit_low, limit_high);
296 for(
int i = 0; i <
fBGToFit->GetNpar(); ++i) {
300 Double_t limit_low = 0.;
301 Double_t limit_high = 0.;
302 fBGToFit->GetParLimits(i, limit_low, limit_high);
313 Int_t params_so_far = 0;
315 TF1* peak_func = p_it->GetFitFunction();
316 sum += peak_func->EvalPar(dim, &par[params_so_far]);
317 params_so_far += peak_func->GetNpar();
319 sum +=
fBGToFit->EvalPar(dim, &par[params_so_far]);
327 Int_t params_so_far = 0;
329 TF1* peak_func = p_it->GetBackgroundFunction();
330 sum += peak_func->EvalPar(dim, &par[params_so_far]);
331 params_so_far += peak_func->GetNpar();
333 sum +=
fBGToFit->EvalPar(dim, &par[params_so_far]);
343 fBGToFit->SetParName(3,
"bg_offset");
345 Double_t lowLimit = 0.;
346 Double_t highLimit = 0.;
347 fBGToFit->GetParLimits(0, lowLimit, highLimit);
349 double value =
fBGToFit->GetParameter(0);
350 if(value == 0 && lowLimit == 0 && highLimit == 0) {
351 fBGToFit->SetParLimits(0, 0.0, fit_hist->GetBinContent(fit_hist->FindBin(
fRangeHigh)) * 100.);
354 fBGToFit->GetParLimits(1, lowLimit, highLimit);
357 if(value == 0 && lowLimit == 0 && highLimit == 0) {
360 fBGToFit->GetParLimits(2, lowLimit, highLimit);
363 if(value == 0 && lowLimit == 0 && highLimit == 0) {
364 fBGToFit->SetParameter(
"C", 0.0000);
367 fBGToFit->GetParLimits(3, lowLimit, highLimit);
370 if(value == 0 && lowLimit == 0 && highLimit == 0) {
394 TF1* bg_to_draw =
new TF1;
396 bg_to_draw->SetLineColor(
static_cast<Color_t
>(kRed +
fColorIndex));
399 TF1* peak_func = p_it->GetFitFunction();
400 TF1* total_function_copy =
new TF1;
404 Int_t param_to_zero_counter = 0;
406 if(other_p_it != p_it) {
407 for(
int i = 0; i < other_p_it->GetFitFunction()->GetNpar(); ++i) {
408 total_function_copy->SetParameter(param_to_zero_counter, 0.0);
409 ++param_to_zero_counter;
412 for(
int i = 0; i < peak_func->GetNpar(); ++i) {
413 if(!p_it->IsBackgroundParameter(i)) {
414 bg_to_draw->SetParameter(param_to_zero_counter, 0.0);
416 ++param_to_zero_counter;
420 total_function_copy->Draw(
"same");
422 bg_to_draw->Draw(
"same");