Hello, I have this code that performs a double gaussian fit on two close photopeaks in a gamma spectrum. It works fine but whenever I try to set different parameters names for the Gauss function, this messes up the fit and I can’t figure out why. Any explanation or suggestion? Thanks!
#include "TCanvas.h"
#include "TFile.h"
#include "TH1.h"
#include "TF1.h"
#include "TAxis.h"
#include "TStyle.h"
#include <string>
#include <iostream>
void doublefit(
std::string filename="Co60hq0.root"
,std::string histoname="hq0"
,std::string bkgfunc="expo"
,int xminGFit1=1100
,int xmaxGFit1=1240
,int xminGFit2=1250
,int xmaxGFit2=1400
){
gStyle->SetOptFit(1111);
TCanvas* can = new TCanvas("can","my canvas",800,800);
// retrieve the histogram from the input file
TFile* file = TFile::Open(filename.c_str());
if(!file){
std::cout << "File " << filename << " not found... check the input name!" << std::endl;
return;
}
TH1D* histo = (TH1D*) file->Get(histoname.c_str());
if(!histo){
std::cout << "histo " << histoname << " not found... check the input name!" << std::endl;
return;
}
// Get the TTree from the file
TTree *nt = (TTree*) file->Get("nt");
// Create a new histogram with 500 bins in the range [0, 10000]
TH1D *histonew = new TH1D("histonew", "hq0new", 500, 0, 10000);
// Draw the variable "qlong" from the TTree "nt" into the histogram "histonew"
nt->Draw("qlong>>histonew", "ch==0");
TH1D* histo_copy = (TH1D*)histonew->Clone();
histo_copy->SetName("histo_copy");
// Calibration
TH1D *histo_cal = new TH1D("histo_cal", "Calibrated Histogram", 500, 0, 3000);
nt->Draw("-32.4+qlong*0.1942>>histo_cal", "ch==0");
// Gaussian fit
const double normGaus=730; // Gauss normalization
const double meanGaus=1173.2; // Gauss mean
const double sigmaGaus=50;
std::string fitfunc = "gaus(0)";
TF1* gaussFunc = new TF1("gaussFunc",fitfunc.c_str(),xminGFit1,xmaxGFit1);
gaussFunc->SetParName(0,"Gauss Norm.");
gaussFunc->SetParName(1,"Gauss mean");
gaussFunc->SetParName(2,"Gauss sigma");
gaussFunc->SetParameters(normGaus, meanGaus, sigmaGaus);
//TFile* file_output = new TFile("file_output.root","recreate");
//can->Write();
//histo_cal->Write();
TF1 *bkgFunc = new TF1("bkgFunc", "expo", 0, 3000);
bkgFunc->SetParName(0,"Exp amp");
bkgFunc->SetParName(1,"Exp decay");
bkgFunc->SetParameters(0, 1.0); // Exponential amplitude
bkgFunc->SetParameters(1, -0.001); // Exponential decay constant
//TF1 *fitFunc = new TF1("fitFunc", "gaussFunc + bkgFunc(3)", xminGFit1, xmaxGFit1);
//histo_cal->Fit(fitFunc, "R");
// Second gaussian fit
const double normGaus2=620;
const double meanGaus2=1333;
const double sigmaGaus2=50;
TF1* gaussFunc2 = new TF1("gaussFunc2",fitfunc.c_str(),xminGFit2,xmaxGFit2);
gaussFunc2->SetParName(0,"Gauss Norm.2");
gaussFunc2->SetParName(1,"Gauss mean2");
gaussFunc2->SetParName(2,"Gauss sigma2");
gaussFunc2->SetParameters(normGaus2, meanGaus2, sigmaGaus2);
//TF1 *fitFunc2 = new TF1("fitFunc2", "gaussFunc2 + bkgFunc(3)", xminGFit2, xmaxGFit2);
//histo_cal->Fit(fitFunc2, "R+");
TF1 *fitFuncDouble = new TF1("fitFuncDouble", "gaussFunc + gaussFunc2 + bkgFunc(3)", xminGFit1, xmaxGFit2);
histo_cal->Fit(fitFuncDouble, "IR0");
histo_cal->SetTitle("Co60");
histo_cal->GetXaxis()->SetTitle("Energy [keV]");
histo_cal->GetYaxis()->SetTitle("Counts");
fitFuncDouble->Draw("same");
// Legend
TLegend *legend = new TLegend(0.1,0.75,0.3,0.9);
legend->SetHeader("Legend","C");
legend->AddEntry(histo_cal,"Calibrated data","f");
legend->AddEntry("fitFuncDouble","Double Gaussian fit","l");
legend->SetTextSize(0.02);
legend->Draw("same");
// Energy vs Pos graph
TCanvas *canvas3=new TCanvas("canvas3", "canvas", 800, 800);
TGraph *graph = new TGraph();
for (int i = 0; i < histo->GetNbinsX(); ++i) {
double original_value = histo->GetBinCenter(i+1);
double calibrated_value = -32.4 + original_value * 0.1942;
graph->SetPoint(i, original_value, calibrated_value);
}
graph->SetTitle("Calibration Linearity;Original Data;Calibrated Data");
graph->SetMarkerStyle(20);
graph->SetMarkerSize(0.1);
graph->SetMarkerColor(kBlue);
graph->Draw("AP");
return;
}