Double Maxwellian Fit


Please read tips for efficient and successful posting and posting code

_ROOT Version:6.18
_Platform: Ubuntu
_Compiler:


Hi everyone, I have some trouble with the double maxwellian fit. I have some histogram in root file and I wanted to fit them with a Double Maxwellian Fit. The code I’m using is the follow:

Double_t Maxwell(Double_t *x, Double_t *par)
{

if(x[0]>par[1]&&par[2]>0&&par[0]>0) 
 {

 Double_t max = par[0]*(x[0]-par[1])/par[2]*TMath::Exp(-(x[0]-par[1])/par[2]); 
return max;
 }
else {
return 0.;
}
}

//
// fit function with  2 Maxwell functions
//
Double_t DeuxMaxwell(Double_t *x, Double_t *par)
{
return Maxwell(x,par)+Maxwell(x,&par[3]);
}


void Max(){

Int_t n=10; 
double para[6];

TH1F *hspettro[n];
for (Int_t i=0; i<n; i++){
string nome = "Spettro"+ to_string(i+1) + ".root";
TFile *hfile_1 = TFile::Open(&nome[0]);
hspettro[i] = (TH1F*) hfile_1 -> Get ("h3");

}
TF1 *dmw = new TF1("DeuxMaxwell",DeuxMaxwell,20,70,6);
dmw->SetParNames("C1","B1","T1","C2","B2","T2");
dmw->SetParameters(1,1,1,2,2,2);
dmw->SetLineColor(kBlue);
TF1 *mw = new TF1("Maxwell",Maxwell,20,150,3);
mw->SetParameters(1,1,1);
mw->SetLineColor(kRed);

hspettro[0]->Draw();
hspettro[0]->Fit("DeuxMaxwell");

TCanvas *c[n];
for(Int_t i=0; i<n; i++){
		c[i] = new TCanvas (Form("c%d",i),"c",900,700);
    		c[i] -> cd() -> SetLogy();
    		c[i] -> SetTickx(1);
    		c[i] -> SetTicky(1);
    		c[i] -> SetLeftMargin(0.12);
    		c[i] -> SetRightMargin(0.03);
    		c[i] -> SetTopMargin(0.1);
    		c[i] -> SetBottomMargin(0.16);
		hspettro[i]-> SetTitle("Total Energy Ring 9 Ext Poor");
		hspettro[i]-> GetYaxis() -> SetTitle("Counts");
		hspettro[i]-> GetXaxis() -> SetTitle("Energy [MeV]");
		hspettro[i] -> SetLineStyle(1);
    		hspettro[i] -> SetLineColor(i+3);//2
    		hspettro[i] -> SetLineWidth(2);
		hspettro[i]->Draw();
		hspettro[i]->Fit("DeuxMaxwell");
		
		mw->SetParameters(para);		
		mw->Draw("same");	
		dmw->SetParameters(&para[3]);
		dmw->Draw("same");
		dmw->GetParameters(para);
		string nome = "Spettro_" + to_string(i+1) + ".png";
		c[i] -> SaveAs(&nome[0]);   

}
}

I expect 2 line over the histogram, but there is only one. I read a guide on this kind of fit, but I don’t understand why it doesn’t work as I want. Can anyone help me?

Hi,

I am not sure what is the problem. If you want to make two fits , you need to use the fit option + to see both functions.
Otherwise please post the full running script

Thank you

Lorenzo

1 Like

Thanks, It worked.