ROOT Version: 6.18 for centos7
Platform: Not Provided
Compiler: g++
Hi there, ROOT community.
I have tried to do some training with ROOT fit functionality, described in chapter 5 of Users Guide ROOTUsersGuide . And I got stuck on 5.4 example.
I have written all stuff in .cpp file, it compiled fine, and produced output Canvas with points scattered and with fitting function == 0.
My code is as follows:
Blockquote
#include<TF1.h>
#include<TCanvas.h>
#include<TMath.h>
#include<TH1D.h>
Double_t Background(Double_t* x, Double_t* par){ //fit requires x to be pointer for some reason
return par[0]+par[1]*(x)+par[2](x)(*x);
}
Double_t LorentzianPeak(Double_t* x, Double_t* par){
return ((par[0]par[1])/(2TMath::Pi()))/((par[3]-(x))(par[3]-(*x))+ par[1]*par[1]/4.0);
}
Double_t FittingCurve(Double_t* x, Double_t* par){
return Background(x,par)+LorentzianPeak(x,par+3);
}
int main(){
TCanvas* C = new TCanvas(“Canvas”, “Canvas”,1);
//our task - Fit the LorentzianPeak ~1/(x^2+a^2) with quadratic background a+bx+cx^2 - six parameters altogether
/auto Background = [](Double_t x, Double_t* par){return par[0]+par[1]x[0]+par[2]x[0]x[0];};//background fitting curve
auto LorentzianPeak = [](Double_t x, Double_t par){
return ((par[0]par[1])/(2TMath::Pi()))/((par[3]-x[0])(par[3]-x[0])+ par[1]par[1]/4.0);//Lorentzian Peak’s one
};
auto FittingCurve = [&Background, &LorentzianPeak](Double_t x, Double_t* par){
return Background(x, par) + LorentzianPeak(x,par+3); //first three parameters refers to Background, three others refers to LoretzianPeak, pointer algebra helps to workaround it
};/
//Let’s make this tash with use of histogram. Firstly, let’s fill this with some data, which correspond to such peak
const Int_t Nbins = 60;
Double_t data[Nbins] =
{6, 1,10,12, 6,13,23,22,15,21,
23,26,36,25,27,35,40,44,66,81,
75,57,48,45,46,41,35,36,53,32,
40,37,38,31,36,44,42,37,32,32,
43,44,35,33,33,39,29,41,32,44,
26,39,29,35,32,21,21,15,25,15};
TH1D hist = new TH1D(“hist”,“Lorentzian Peak Fit on quadratic background”,60,0,3);//range [0,3]
for(Int_t i=0;i<Nbins; ++i){
hist->SetBinContent(i+1,data[i]);
hist->SetBinError(i+1,TMath::Sqrt(data[i]));
}//Fills histogram with points
TF1* FittingCurve_tf = new TF1(“fit”,FittingCurve,0,3,6); //6 denotes number of parametets
//FittingCurve_tf->SetParameters(1,1,-1,20,1,1);
hist->Fit(“fit”);
C->SaveAs(“temp.pdf”);
delete C, hist, FittingCurve_tf;
}
Blockquote
You may compare it to those one suggested in the user guide. And here is the result:
- Even if I change initial parameters set, they seemingly do not change under Fit function. Why is that?
- I firstly tried to make all stuff using lambdas, bud did not succeeded (got many non-easy errors). Maybe there is working example you could provide?
I would be very grateful for any suggests.