12 :
TGRSIFit(
"multipeakbg", this, &
TMultiPeak::MultiPhotoPeakBG, xlow, xhigh, centroids.size() * 6 + 5,
"TMultiPeak",
"MultiPhotoPeakBG")
14 std::cout <<
"Warning, the class TMultiPeak is deprecated (use TPeakFitter instead)!" << std::endl;
17 fBackground =
new TF1(Form(
"MPbackground_%d_to_%d",
static_cast<Int_t
>(xlow),
static_cast<Int_t
>(xhigh)),
this, &
TMultiPeak::MultiStepBG, xlow, xhigh, centroids.size() * 6 + 5,
"TMuliPeak",
"MultiStepBG");
23 for(
double cent : centroids) {
24 Bool_t out_of_range_flag =
false;
26 std::cout <<
"centroid " << cent <<
" is higher than range" << std::endl;
27 out_of_range_flag =
true;
28 }
else if(cent < xlow) {
29 std::cout <<
"centroid " << cent <<
" is lower than range" << std::endl;
30 out_of_range_flag =
true;
32 if(out_of_range_flag) {
33 std::cout <<
"ignoring peak at " << cent <<
", make a new multi peak with the corrected energy" << std::endl;
36 peak->AddToGlobalList(kFALSE);
40 SetRange(xlow, xhigh);
42 SetName(Form(
"MultiPeak_%d_to_%d",
static_cast<Int_t
>(xlow),
43 static_cast<Int_t
>(xhigh)));
121 if((fithist ==
nullptr) && (
GetHist() !=
nullptr)) {
125 if(fithist ==
nullptr) {
126 std::cout <<
"No histogram is associated yet, no initial guesses made" << std::endl;
130 FixParameter(0,
static_cast<double>(
fPeakVec.size()));
134 GetRange(xlow, xhigh);
135 Int_t binlow = fithist->GetXaxis()->FindBin(xlow);
136 Int_t binhigh = fithist->GetXaxis()->FindBin(xhigh);
139 SetParLimits(1, 0.0, fithist->GetBinContent(binlow) * 100.0);
140 SetParameter(
"A", fithist->GetBinContent(binlow));
141 SetParameter(
"B", (fithist->GetBinContent(binlow) - fithist->GetBinContent(binhigh)) / (xlow - xhigh));
142 SetParameter(
"C", 0.0000);
143 SetParameter(
"bg_offset", (xhigh + xlow) / 2.0);
148 for(
int i = 0; i < static_cast<int>(
fPeakVec.size()); ++i) {
149 Double_t centroid =
fPeakVec.at(i)->GetCentroid();
150 Int_t bin = fithist->GetXaxis()->FindBin(centroid);
151 SetParLimits(6 * i + 5, 0, fithist->GetBinContent(bin) * 5.);
152 SetParLimits(6 * i + 6, centroid - 4, centroid + 4);
154 SetParLimits(6 * i + 7, 0.1, 1.5);
155 SetParLimits(6 * i + 8, 0.000001, 10);
156 SetParLimits(6 * i + 9, 0.000001, 100);
157 SetParLimits(6 * i + 10, 0.0, 1.0E2);
161 SetParameter(Form(
"Height_%i", i), fithist->GetBinContent(bin));
162 SetParameter(Form(
"Centroid_%i", i), centroid);
165 SetParameter(Form(
"Sigma_%i", i), TMath::Sqrt(9.0 + 4. * GetParameter(Form(
"Centroid_%i", i)) / 1000.));
166 SetParameter(Form(
"Beta_%i", i), GetParameter(Form(
"Sigma_%i", i)) / 2.0);
167 SetParameter(Form(
"R_%i", i), 1.0);
168 SetParameter(Form(
"Step_%i", i), 1.0);
171 FixParameter(6 * i + 8, GetParameter(Form(
"Beta_%i", i)));
172 FixParameter(6 * i + 9, 0.00);
181 TString optstr = opt;
182 if((fithist ==
nullptr) && (
GetHist() ==
nullptr)) {
183 std::cout <<
"No hist passed, trying something...";
184 fithist = fHistogram;
186 if(fithist ==
nullptr) {
187 std::cout <<
"No histogram associated with Peak" << std::endl;
194 TVirtualFitter::SetMaxIterations(100000);
195 ROOT::Math::MinimizerOptions::SetDefaultMinimizer(
"Minuit2",
"Combination");
198 TString options(opt);
199 bool print_flag =
true;
200 if(options.Contains(
"Q")) {
206 TFitResultPtr fitres;
209 fitres = fithist->Fit(
this, Form(
"%sLRSQ", opt));
211 fitres = fithist->Fit(
this, Form(
"%sRSQ", opt));
214 std::vector<Double_t> sigma_list(
static_cast<int>(GetParameter(0) + 0.5));
215 for(
int i = 0; i < static_cast<int>(GetParameter(0) + 0.5); ++i) {
217 sigma_list[i] = GetParameter(6 * i + 7);
219 std::sort(sigma_list.begin(), sigma_list.end(), std::greater<>());
220 Double_t median = sigma_list.at(sigma_list.size() / 2);
222 Double_t range_low = 0.;
223 Double_t range_high = 0.;
224 for(
int i = 0; i < static_cast<int>(GetParameter(0) + 0.5); ++i) {
225 GetParLimits(6 * i + 7, range_low, range_high);
226 if(range_low != range_high) {
227 SetParLimits(6 * i + 7, median * .95, median * 1.05);
233 fitres = fithist->Fit(
this, Form(
"%sLRS", opt));
235 fitres = fithist->Fit(
this, Form(
"%sRS", opt));
243 TMatrixDSym CovMat = fitres->GetCovarianceMatrix();
255 TMatrixDSym emptyCovMat = CovMat;
256 for(
size_t i = 0; i <
fPeakVec.size(); ++i) {
257 emptyCovMat(6 * i + 5, 6 * i + 5) = 0.0;
258 emptyCovMat(6 * i + 6, 6 * i + 6) = 0.0;
259 emptyCovMat(6 * i + 7, 6 * i + 7) = 0.0;
260 emptyCovMat(6 * i + 8, 6 * i + 8) = 0.0;
261 emptyCovMat(6 * i + 9, 6 * i + 9) = 0.0;
262 emptyCovMat(6 * i + 10, 6 * i + 10) = 0.0;
266 std::cout <<
"Chi^2/NDF = " << fitres->Chi2() / fitres->Ndf() << std::endl;
269 for(
int i = 0; i < static_cast<int>(
fPeakVec.size()); ++i) {
271 tmpMp->ClearParameters();
272 Double_t binWidth = fithist->GetBinWidth(1);
274 TMatrixDSym tmpCovMat = emptyCovMat;
275 peak->SetParameter(
"Height", GetParameter(Form(
"Height_%i", i)));
276 peak->SetParameter(
"centroid", GetParameter(Form(
"Centroid_%i", i)));
277 peak->SetParameter(
"sigma", GetParameter(Form(
"Sigma_%i", i)));
278 peak->SetParameter(
"beta", GetParameter(Form(
"Beta_%i", i)));
279 peak->SetParameter(
"R", GetParameter(Form(
"R_%i", i)));
280 peak->SetParameter(
"step", GetParameter(Form(
"Step_%i", i)));
281 peak->SetParameter(
"A", 0.0);
282 peak->SetParameter(
"B", 0.0);
283 peak->SetParameter(
"C", 0.0);
284 peak->SetParameter(
"bg_offset", 0.0);
286 peak->
SetNdf(fitres->Ndf());
289 tmpCovMat(6 * i + 5, 6 * i + 5) = CovMat(6 * i + 5, 6 * i + 5);
290 tmpCovMat(6 * i + 6, 6 * i + 6) = CovMat(6 * i + 6, 6 * i + 6);
291 tmpCovMat(6 * i + 7, 6 * i + 7) = CovMat(6 * i + 7, 6 * i + 7);
292 tmpCovMat(6 * i + 8, 6 * i + 8) = CovMat(6 * i + 8, 6 * i + 8);
293 tmpCovMat(6 * i + 9, 6 * i + 9) = CovMat(6 * i + 9, 6 * i + 9);
295 tmpMp->SetParameter(
"N_Peaks", GetParameter(
"N_Peaks"));
296 tmpMp->SetParameter(Form(
"Height_%i", i), GetParameter(Form(
"Height_%i", i)));
297 tmpMp->SetParameter(Form(
"Centroid_%i", i), GetParameter(Form(
"Centroid_%i", i)));
298 tmpMp->SetParameter(Form(
"Sigma_%i", i), GetParameter(Form(
"Sigma_%i", i)));
299 tmpMp->SetParameter(Form(
"Beta_%i", i), GetParameter(Form(
"Beta_%i", i)));
300 tmpMp->SetParameter(Form(
"R_%i", i), GetParameter(Form(
"R_%i", i)));
302 Double_t width = GetParameter(Form(
"Sigma_%i", i));
305 GetRange(xlow, xhigh);
306 Double_t int_low = xlow - 10. * width;
307 Double_t int_high = xhigh + 10. * width;
311 tmpMp->SetRange(int_low, int_high);
315 peak->
SetArea((tmpMp->Integral(int_low, int_high)) / binWidth);
316 peak->
SetAreaErr((tmpMp->IntegralError(int_low, int_high, tmpMp->GetParameters(), tmpCovMat.GetMatrixArray())) /
318 peak->SetParameter(
"centroid", GetParameter(Form(
"Centroid_%i", i)));
319 peak->SetParError(peak->GetParNumber(
"centroid"), GetParError(GetParNumber(Form(
"Centroid_%i", i))));
320 peak->SetParameter(
"sigma", GetParameter(GetParNumber(Form(
"Sigma_%i", i))));
321 peak->SetParError(peak->GetParNumber(
"sigma"), GetParError(GetParNumber(Form(
"Sigma_%i", i))));
323 std::cout <<
"Integral: " << peak->
GetArea() <<
" +- " << peak->
GetAreaErr() << std::endl;
445 GetRange(xlow, xhigh);
449 Double_t centroid = peak->GetCentroid();
450 Double_t range = 2. * peak->GetFWHM();
452 auto* sum =
new TF1(Form(
"tmp%s", peak->GetName()),
this, &
TMultiPeak::SinglePeakBG, centroid - range, centroid + range,
453 fPeakVec.size() * 6 + 11,
"TMultiPeak",
"SinglePeakBG");
455 for(
int j = 0; j < GetNpar(); ++j) {
456 sum->SetParameter(j, GetParameter(j));
458 for(
int j = 0; j < 5; ++j) {
459 sum->SetParameter(npeaks * 6 + 5 + j, peak->GetParameter(j));
463 sum->SetLineStyle(2);
464 sum->SetLineColor(kMagenta);
466 sum->SetRange(centroid - range, centroid + range);
467 sum->DrawCopy(
"SAME");