File indexing completed on 2024-04-06 12:09:42
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 #include "TH1.h"
0018 #include "TF1.h"
0019 #include "TROOT.h"
0020 #include "TStyle.h"
0021 #include "TMath.h"
0022
0023 Double_t langaufun(Double_t *x, Double_t *par) {
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036 Double_t invsq2pi = 0.3989422804014;
0037 Double_t mpshift = -0.22278298;
0038
0039
0040 Double_t np = 100.0;
0041 Double_t sc = 5.0;
0042
0043
0044 Double_t xx;
0045 Double_t mpc;
0046 Double_t fland;
0047 Double_t sum = 0.0;
0048 Double_t xlow, xupp;
0049 Double_t step;
0050 Double_t i;
0051
0052
0053 mpc = par[1] - mpshift * par[0];
0054
0055
0056 xlow = x[0] - sc * par[3];
0057 xupp = x[0] + sc * par[3];
0058
0059 step = (xupp - xlow) / np;
0060
0061
0062 for (i = 1.0; i <= np / 2; i++) {
0063 xx = xlow + (i - .5) * step;
0064 fland = TMath::Landau(xx, mpc, par[0]) / par[0];
0065 sum += fland * TMath::Gaus(x[0], xx, par[3]);
0066
0067 xx = xupp - (i - .5) * step;
0068 fland = TMath::Landau(xx, mpc, par[0]) / par[0];
0069 sum += fland * TMath::Gaus(x[0], xx, par[3]);
0070 }
0071
0072 return (par[2] * step * sum * invsq2pi / par[3]);
0073 }
0074
0075 TF1 *langaufit(TH1F *his,
0076 Double_t *fitrange,
0077 Double_t *startvalues,
0078 Double_t *parlimitslo,
0079 Double_t *parlimitshi,
0080 Double_t *fitparams,
0081 Double_t *fiterrors,
0082 Double_t *ChiSqr,
0083 Int_t *NDF) {
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101 Int_t i;
0102 Char_t FunName[100];
0103
0104 sprintf(FunName, "Fitfcn_%s", his->GetName());
0105
0106 TF1 *ffitold = (TF1 *)gROOT->GetListOfFunctions()->FindObject(FunName);
0107 if (ffitold)
0108 delete ffitold;
0109
0110 TF1 *ffit = new TF1(FunName, langaufun, fitrange[0], fitrange[1], 4);
0111 ffit->SetParameters(startvalues);
0112 ffit->SetParNames("Width", "MP", "Area", "GSigma");
0113
0114 for (i = 0; i < 4; i++) {
0115 ffit->SetParLimits(i, parlimitslo[i], parlimitshi[i]);
0116 }
0117
0118 try {
0119 his->Fit(FunName, "RB0Q SERIAL");
0120 } catch (...) {
0121 }
0122
0123 if (ffit) {
0124 ffit->GetParameters(fitparams);
0125 for (i = 0; i < 4; i++) {
0126 fiterrors[i] = ffit->GetParError(i);
0127 }
0128 ChiSqr[0] = ffit->GetChisquare();
0129 NDF[0] = ffit->GetNDF();
0130 }
0131
0132 return (ffit);
0133 }
0134
0135 Int_t langaupro(Double_t *params, Double_t &maxx, Double_t &FWHM) {
0136
0137
0138
0139
0140
0141 Double_t p, x, fy, fxr, fxl;
0142 Double_t step;
0143 Double_t l, lold;
0144 Int_t i = 0;
0145 Int_t MAXCALLS = 10000;
0146
0147
0148
0149 p = params[1] - 0.1 * params[0];
0150 step = 0.05 * params[0];
0151 lold = -2.0;
0152 l = -1.0;
0153
0154 while ((l != lold) && (i < MAXCALLS)) {
0155 i++;
0156
0157 lold = l;
0158 x = p + step;
0159 l = langaufun(&x, params);
0160
0161 if (l < lold)
0162 step = -step / 10;
0163
0164 p += step;
0165 }
0166
0167 if (i == MAXCALLS)
0168 return (-1);
0169
0170 maxx = x;
0171
0172 fy = l / 2;
0173
0174
0175
0176 p = maxx + params[0];
0177 step = params[0];
0178 lold = -2.0;
0179 l = -1e300;
0180 i = 0;
0181
0182 while ((l != lold) && (i < MAXCALLS)) {
0183 i++;
0184
0185 lold = l;
0186 x = p + step;
0187 l = TMath::Abs(langaufun(&x, params) - fy);
0188
0189 if (l > lold)
0190 step = -step / 10;
0191
0192 p += step;
0193 }
0194
0195 if (i == MAXCALLS)
0196 return (-2);
0197
0198 fxr = x;
0199
0200
0201
0202 p = maxx - 0.5 * params[0];
0203 step = -params[0];
0204 lold = -2.0;
0205 l = -1e300;
0206 i = 0;
0207
0208 while ((l != lold) && (i < MAXCALLS)) {
0209 i++;
0210
0211 lold = l;
0212 x = p + step;
0213 l = TMath::Abs(langaufun(&x, params) - fy);
0214
0215 if (l > lold)
0216 step = -step / 10;
0217
0218 p += step;
0219 }
0220
0221 if (i == MAXCALLS)
0222 return (-3);
0223
0224 fxl = x;
0225
0226 FWHM = fxr - fxl;
0227 return (0);
0228 }