8 TF1Fitter::TF1Fitter(
const char *name,
int nPars):TF1(name, static_cast<DummyFuncPtr_t>(nullptr), 0,0,nPars)
10 fMean=0; fSigm=0;fTH1 = 0;
13 void TF1Fitter::Copy(TObject& f1)
const
20 double TF1Fitter::operator()(
const double* x,
const double*
params)
22 if (!params) params = GetParameters();
23 return EvalPar(x,params);
26 double TF1Fitter::EvalPar(
const double *x,
const double *par)
32 void TF1Fitter::SetHist(TH1 *hist)
37 if (*GetName()==0) SetName(fTH1->GetName());
38 fMean = fTH1->GetMean();
39 fSigm = fTH1->GetRMS();
41 int nBins = fTH1->GetNbinsX();
42 double Xmin = fTH1->GetBinLowEdge(1);
43 double Xmax = fTH1->GetBinLowEdge(nBins)+fTH1->GetBinWidth(nBins);
49 void TF1Fitter::Draw(
const char *opt)
57 double TF1GausFitter::EvalPar(
const double *x,
const double *par)
59 double p1 = 1./(par[1]*par[1]);
60 return par[2]*exp(-0.5*p1*(x[0]-par[0])*(x[0]-par[0]));
63 void TF1GausFitter::Init()
65 static const double SQR2 = sqrt(2.);
71 double gral = fTH1->Integral();
72 double erf = 0.5*TMath::Erf((fXmax-fMean)/(SQR2*fSigm));
73 SetParameter(0,fMean);
74 SetParameter(1,fSigm);
75 SetParameter(2,gral/erf);
76 double xLow = GetParameters()[0]-9*GetParameters()[1];
77 double xUpp = GetParameters()[0]+9*GetParameters()[1];
78 SetParLimits(0,xLow,xUpp);
79 SetParLimits(1,GetParameters()[1]*0.1,GetParameters()[1]*10);
80 SetParLimits(2, 0,GetParameters()[2]*10);
85 double TF1TwoGausFitter::EvalPar(
const double *x,
const double *par)
88 double sig2 = par[1]+par[4];
89 double w1 = 1./(sig1*sig1);
90 double w2 = 1./(sig2*sig2);
91 double c1 = par[2]*(1-par[5]);
92 double c2 = par[2]*( par[5]);
94 return c1*exp(-0.5*w1*(x[0]-par[0])*(x[0]-par[0]))
95 +c2*exp(-0.5*w2*(x[0]-par[3])*(x[0]-par[3]));
99 void TF1TwoGausFitter::Init()
101 static const double SQR2 = sqrt(2.);
102 SetParName(0,
"Mean");
103 SetParName(1,
"Sigm");
104 SetParName(2,
"Norm");
105 SetParName(3,
"MeanBak");
106 SetParName(4,
"AddSigmBak");
107 SetParName(5,
"ContriBak");
110 double gral = fTH1->Integral();
111 double erf = 0.5*TMath::Erf((fXmax-fMean)/(SQR2*fSigm));
112 SetParameter(0,fMean);
113 SetParameter(1,fSigm);
114 SetParameter(2,gral/erf);
115 double xLow = GetParameters()[0]-9*GetParameters()[1];
116 double xUpp = GetParameters()[0]+9*GetParameters()[1];
117 SetParLimits(0,xLow,xUpp);
118 SetParLimits(1,GetParameters()[1]*0.1,GetParameters()[1]*10);
119 SetParLimits(2, 0,GetParameters()[2]*10);
121 SetParameter(3,fMean);
122 SetParameter(4,fSigm);
125 SetParLimits(3,xLow,xUpp);
126 SetParLimits(4,0,GetParameters()[1]*10);
131 double TF1GausPol2Fitter::EvalPar(
const double *x,
const double *par)
133 double sig1 = par[1];
134 double w1 = 1./(sig1*sig1);
140 double bak = p0+x[0]*(p1 + x[0]*p2);
142 return c1*exp(-0.5*w1*(x[0]-par[0])*(x[0]-par[0])) + bak;
145 void TF1GausPol2Fitter::Init()
147 static const double SQR2 = sqrt(2.);
148 SetParName(0,
"Mean");
149 SetParName(1,
"Sigm");
150 SetParName(2,
"Norm");
156 double gral = fTH1->Integral();
157 double erf = 0.5*TMath::Erf((fXmax-fMean)/(SQR2*fSigm));
158 SetParameter(0,fMean);
159 SetParameter(1,fSigm);
160 SetParameter(2,gral/erf);
161 double xLow = GetParameters()[0]-9*GetParameters()[1];
162 double xUpp = GetParameters()[0]+9*GetParameters()[1];
163 SetParLimits(0,xLow,xUpp);
164 SetParLimits(1,GetParameters()[1]*0.1,GetParameters()[1]*10);
192 TwoGausFitter::TwoGausFitter(TH1 *th,
const char *name):TH1Fitter(th,name)
196 void TwoGausFitter::Prep()
198 static const double SQR2 = sqrt(2.);
200 TF1 *gaus1 =
new TF1Gaus(
"Signal"); Add(gaus1);
201 TF1 *gaus2 =
new TF1Gaus(
"BackGround"); Add(gaus2);
203 double mean = mTH1->GetMean();
204 printf(
"mean=%g\n",mean);
205 double sigm = mTH1->GetRMS();
206 printf(
"sigm=%g\n",sigm);
207 double gral = mTH1->Integral();
208 printf(
"gral=%g\n",gral);
209 double erf = 0.5*TMath::Erf((mXmax-mean)/(SQR2*sigm));
210 printf(
"erf=%g\n",erf);
211 erf+= 0.5*TMath::Erf((mean-mXmin)/(SQR2*sigm));
212 printf(
"erf=%g\n",erf);
217 mTF1->SetParLimits(0,mPars[0]-3*sigm,mPars[1]+10*sigm);
218 mTF1->SetParLimits(1,mPars[1]*0.1,mPars[1]*10);
219 mTF1->SetParLimits(2, 0,mPars[2]*1000);
223 mPars[4] = mPars[1]*0.1;
224 mPars[5] = mPars[2]*0.1;
226 mTF1->SetParLimits(3,mPars[0]-10*sigm,mPars[1]+10*sigm);
227 mTF1->SetParLimits(4,mPars[4]*0.1,mPars[4]*10);
228 mTF1->SetParLimits(5,0. ,mPars[5]*1000);
232 double TwoGausFitter::Fit(
const char *opt)
235 return TH1Fitter::Fit(opt);
238 TH1Fitter::TH1Fitter(TH1 *th,
const char *name):TNamed(name,
"")
240 memset(mBeg,0,mEnd-mBeg);
245 void TH1Fitter::Set(TH1 *th)
249 if (*GetName()==0) SetName(mTH1->GetName());
250 if (mXmin < mXmax)
return;
251 int nBins = mTH1->GetNbinsX();
252 mXmin = mTH1->GetBinLowEdge(1);
253 mXmax = mTH1->GetBinLowEdge(nBins)+mTH1->GetBinWidth(nBins);
256 void TH1Fitter::Add(TF1 *fcn)
259 mTF1s[mNTF1s++] = fcn;
260 mNPars += fcn->GetNpar();
264 TF1 *TH1Fitter::GetTF1()
268 mTF1 =
new TF1AuxFitter(mNPars, mNTF1s, mTF1s);
269 mTF1->SetName(GetName());
271 mTF1->SetRange(mXmin,mXmax);
275 double TH1Fitter::Fit(
const char *opt)
278 mTF1->SetParameters(mPars);
280 mTF1->GetParameters(mPars);
281 return mTF1->GetChisquare();
284 void TH1Fitter::Draw(
const char *)
286 double par[100],*ipar=par;
287 mTF1->GetParameters(par);
289 for (
int i = -1;i<mNTF1s;i++) {
291 tf->SetLineColor(gMyColors[(i+1)%5]);
292 tf->SetParameters(ipar);
294 if (i>-1) ipar += tf->GetNpar();