30 ClassImp(TSpectrumFit);
 
   35 TSpectrumFit::TSpectrumFit() :TNamed(
"SpectrumFit", 
"Miroslav Morhac peak fitter")
 
   38    fNumberIterations = 1;
 
   41    fStatisticType = kFitOptimChiCounts;
 
   42    fAlphaOptim = kFitAlphaHalving;
 
   44    fFitTaylor = kFitTaylorOrderFirst;
 
  101 TSpectrumFit::TSpectrumFit(Int_t numberPeaks) :TNamed(
"SpectrumFit", 
"Miroslav Morhac peak fitter")
 
  103    if (numberPeaks <= 0){
 
  104       Error (
"TSpectrumFit",
"Invalid number of peaks, must be > than 0");
 
  107    fNPeaks = numberPeaks;
 
  108    fNumberIterations = 1;
 
  111    fStatisticType = kFitOptimChiCounts;
 
  112    fAlphaOptim = kFitAlphaHalving;
 
  114    fFitTaylor = kFitTaylorOrderFirst;
 
  117    fPositionInit   = 
new Double_t[numberPeaks];
 
  118    fPositionCalc   = 
new Double_t[numberPeaks];
 
  119    fPositionErr   = 
new Double_t[numberPeaks];
 
  120    fFixPosition   = 
new Bool_t[numberPeaks];
 
  121    fAmpInit   = 
new Double_t[numberPeaks];
 
  122    fAmpCalc   = 
new Double_t[numberPeaks];
 
  123    fAmpErr    = 
new Double_t[numberPeaks];
 
  124    fFixAmp    = 
new Bool_t[numberPeaks];
 
  125    fArea      = 
new Double_t[numberPeaks];
 
  126    fAreaErr   = 
new Double_t[numberPeaks];
 
  160 TSpectrumFit::~TSpectrumFit()
 
  162    delete [] fPositionInit;
 
  163    delete [] fPositionCalc;
 
  164    delete [] fPositionErr;
 
  165    delete [] fFixPosition;
 
  177 Double_t TSpectrumFit::Erfc(Double_t x)
 
  179    Double_t da1 = 0.1740121, da2 = -0.0479399, da3 = 0.3739278, dap = 0.47047;
 
  191    c = c * t * (da1 + t * (da2 + t * da3));
 
  200 Double_t TSpectrumFit::Derfc(Double_t x)
 
  203    Double_t da1 = 0.1740121, da2 = -0.0479399, da3 = 0.3739278, dap = 0.47047;
 
  214    c = (-1.) * dap * c * t * t * (da1 + t * (2. * da2 + t * 3. * da3)) -
 
  229 Double_t TSpectrumFit::Deramp(Double_t i, Double_t i0, Double_t sigma, Double_t t,
 
  230                            Double_t s, Double_t b)
 
  233    p = (i - i0) / sigma;
 
  248       r = r * Erfc(p + 1. / (2. * b));
 
  251       q = q + s * Erfc(p) / 2.;
 
  266 Double_t TSpectrumFit::Deri0(Double_t i, Double_t amp, Double_t i0, Double_t sigma,
 
  267                           Double_t t, Double_t s, Double_t b)
 
  269    Double_t p, r1, r2, r3, r4, c, d, e;
 
  270    p = (i - i0) / sigma;
 
  273       r1 = 2. * p * exp(-p * p) / sigma;
 
  280       c = p + 1. / (2. * b);
 
  284       r2 = -t * exp(e) * Erfc(c) / (d * b);
 
  285       r3 = -t * exp(e) * Derfc(c) / d;
 
  289       r4 = -s * Derfc(p) / d;
 
  290    r1 = amp * (r1 + r2 + r3 + r4);
 
  303 Double_t TSpectrumFit::Derderi0(Double_t i, Double_t amp, Double_t i0,
 
  306    Double_t p, r1, r2, r3, r4;
 
  307    p = (i - i0) / sigma;
 
  314    r1 = r1 * (4 * p * p - 2) / (sigma * sigma);
 
  315    r2 = 0, r3 = 0, r4 = 0;
 
  316    r1 = amp * (r1 + r2 + r3 + r4);
 
  331 Double_t TSpectrumFit::Dersigma(Int_t num_of_fitted_peaks, Double_t i,
 
  332                              const Double_t *parameter, Double_t sigma,
 
  333                              Double_t t, Double_t s, Double_t b)
 
  336    Double_t r, p, r1, r2, r3, r4, c, d, e;
 
  339    for (j = 0; j < num_of_fitted_peaks; j++) {
 
  340       p = (i - parameter[2 * j + 1]) / sigma;
 
  342       if (TMath::Abs(p) < 3) {
 
  344             r1 = 2. * p * p * exp(-p * p) / sigma;
 
  352          c = p + 1. / (2. * b);
 
  356          r2 = -t * p * exp(e) * Erfc(c) / (d * b);
 
  357          r3 = -t * p * exp(e) * Derfc(c) / d;
 
  361          r4 = -s * p * Derfc(p) / d;
 
  362       r = r + parameter[2 * j] * (r1 + r2 + r3 + r4);
 
  376 Double_t TSpectrumFit::Derdersigma(Int_t num_of_fitted_peaks, Double_t i,
 
  377                                const Double_t *parameter, Double_t sigma)
 
  380    Double_t r, p, r1, r2, r3, r4;
 
  382    for (j = 0; j < num_of_fitted_peaks; j++) {
 
  383       p = (i - parameter[2 * j + 1]) / sigma;
 
  385       if (TMath::Abs(p) < 3) {
 
  387             r1 = exp(-p * p) * p * p * (4. * p * p - 6) / (sigma * sigma);
 
  393       r2 = 0, r3 = 0, r4 = 0;
 
  394       r = r + parameter[2 * j] * (r1 + r2 + r3 + r4);
 
  409 Double_t TSpectrumFit::Dert(Int_t num_of_fitted_peaks, Double_t i,
 
  410                         const Double_t *parameter, Double_t sigma, Double_t b)
 
  413    Double_t r, p, r1, c, e;
 
  415    for (j = 0; j < num_of_fitted_peaks; j++) {
 
  416       p = (i - parameter[2 * j + 1]) / sigma;
 
  417       c = p + 1. / (2. * b);
 
  421       r1 = exp(e) * Erfc(c);
 
  422       r = r + parameter[2 * j] * r1;
 
  437 Double_t TSpectrumFit::Ders(Int_t num_of_fitted_peaks, Double_t i,
 
  438                         const Double_t *parameter, Double_t sigma)
 
  443    for (j = 0; j < num_of_fitted_peaks; j++) {
 
  444       p = (i - parameter[2 * j + 1]) / sigma;
 
  446       r = r + parameter[2 * j] * r1;
 
  463 Double_t TSpectrumFit::Derb(Int_t num_of_fitted_peaks, Double_t i,
 
  464                         const Double_t *parameter, Double_t sigma, Double_t t,
 
  468    Double_t r, p, r1, c, e;
 
  470    for (j = 0; j < num_of_fitted_peaks && t != 0; j++) {
 
  471       p = (i - parameter[2 * j + 1]) / sigma;
 
  472       c = p + 1. / (2. * b);
 
  475       r1 = r1 + Derfc(c) / 2.;
 
  483       r = r + parameter[2 * j] * r1;
 
  485    r = -r * t / (2. * b * b);
 
  492 Double_t TSpectrumFit::Dera1(Double_t i)
 
  500 Double_t TSpectrumFit::Dera2(Double_t i)
 
  516 Double_t TSpectrumFit::Shape(Int_t num_of_fitted_peaks, Double_t i,
 
  517                          const Double_t *parameter, Double_t sigma, Double_t t,
 
  518                          Double_t s, Double_t b, Double_t a0, Double_t a1,
 
  522    Double_t r, p, r1, r2, r3, c, e;
 
  524    for (j = 0; j < num_of_fitted_peaks; j++) {
 
  526          p = (i - parameter[2 * j + 1]) / sigma;
 
  529          if (i == parameter[2 * j + 1])
 
  536       if (TMath::Abs(p) < 3) {
 
  546          c = p + 1. / (2. * b);
 
  550          r2 = t * exp(e) * Erfc(c) / 2.;
 
  554          r3 = s * Erfc(p) / 2.;
 
  555       r = r + parameter[2 * j] * (r1 + r2 + r3);
 
  557    r = r + a0 + a1 * i + a2 * i * i;
 
  569 Double_t TSpectrumFit::Area(Double_t a, Double_t sigma, Double_t t, Double_t b)
 
  571    Double_t odm_pi = 1.7724538, r = 0;
 
  575    if (TMath::Abs(r) < 700)
 
  576       r = a * sigma * (odm_pi + t * b * exp(r));
 
  579       r = a * sigma * odm_pi;
 
  592 Double_t TSpectrumFit::Derpa(Double_t sigma, Double_t t, Double_t b)
 
  594    Double_t odm_pi = 1.7724538, r;
 
  597    if (TMath::Abs(r) < 700)
 
  598       r = sigma * (odm_pi + t * b * exp(r));
 
  614 Double_t TSpectrumFit::Derpsigma(Double_t a, Double_t t, Double_t b)
 
  616    Double_t odm_pi = 1.7724538, r;
 
  619    if (TMath::Abs(r) < 700)
 
  620       r = a * (odm_pi + t * b * exp(r));
 
  636 Double_t TSpectrumFit::Derpt(Double_t a, Double_t sigma, Double_t b)
 
  641    if (TMath::Abs(r) < 700)
 
  642       r = a * sigma * b * exp(r);
 
  658 Double_t TSpectrumFit::Derpb(Double_t a, Double_t sigma, Double_t t, Double_t b)
 
  661    r = (-1) * 0.25 / (b * b);
 
  662    if (TMath::Abs(r) < 700)
 
  663       r = a * sigma * t * exp(r) * (1 - 2 * r);
 
  674 Double_t TSpectrumFit::Ourpowl(Double_t a, Int_t pw)
 
  684    if (pw > 10) c *= a2;
 
  685    if (pw > 12) c *= a2;
 
  820 void TSpectrumFit::FitAwmi(Double_t *source)
 
  822    Int_t i, j, k, shift =
 
  823        2 * fNPeaks + 7, peak_vel, rozmer, iter, pw, regul_cycle,
 
  825    Double_t a, b, c, d = 0, alpha, chi_opt, yw, ywm, f, chi2, chi_min, chi =
 
  826        0, pi, pmin = 0, chi_cel = 0, chi_er;
 
  827    Double_t *working_space = 
new Double_t[5 * (2 * fNPeaks + 7)];
 
  828    for (i = 0, j = 0; i < fNPeaks; i++) {
 
  829       working_space[2 * i] = fAmpInit[i];        
 
  830       if (fFixAmp[i] == 
false) {
 
  831          working_space[shift + j] = fAmpInit[i];        
 
  834       working_space[2 * i + 1] = fPositionInit[i];        
 
  835       if (fFixPosition[i] == 
false) {
 
  836          working_space[shift + j] = fPositionInit[i];        
 
  841    working_space[2 * i] = fSigmaInit;        
 
  842    if (fFixSigma == 
false) {
 
  843       working_space[shift + j] = fSigmaInit;        
 
  846    working_space[2 * i + 1] = fTInit;        
 
  847    if (fFixT == 
false) {
 
  848       working_space[shift + j] = fTInit;        
 
  851    working_space[2 * i + 2] = fBInit;        
 
  852    if (fFixB == 
false) {
 
  853       working_space[shift + j] = fBInit;        
 
  856    working_space[2 * i + 3] = fSInit;        
 
  857    if (fFixS == 
false) {
 
  858       working_space[shift + j] = fSInit;        
 
  861    working_space[2 * i + 4] = fA0Init;        
 
  862    if (fFixA0 == 
false) {
 
  863       working_space[shift + j] = fA0Init;        
 
  866    working_space[2 * i + 5] = fA1Init;        
 
  867    if (fFixA1 == 
false) {
 
  868       working_space[shift + j] = fA1Init;        
 
  871    working_space[2 * i + 6] = fA2Init;        
 
  872    if (fFixA2 == 
false) {
 
  873       working_space[shift + j] = fA2Init;        
 
  878       delete [] working_space;
 
  879       Error (
"FitAwmi",
"All parameters are fixed");
 
  882    if (rozmer >= fXmax - fXmin + 1){
 
  883       delete [] working_space;
 
  884       Error (
"FitAwmi",
"Number of fitted parameters is larger than # of fitted points");
 
  887    for (iter = 0; iter < fNumberIterations; iter++) {
 
  888       for (j = 0; j < rozmer; j++) {
 
  889          working_space[2 * shift + j] = 0, working_space[3 * shift + j] = 0;        
 
  894       chi_opt = 0, pw = fPower - 2;
 
  895       for (i = fXmin; i <= fXmax; i++) {
 
  898          f = Shape(fNPeaks, (Double_t) i, working_space,
 
  899                     working_space[peak_vel], working_space[peak_vel + 1],
 
  900                     working_space[peak_vel + 3],
 
  901                     working_space[peak_vel + 2],
 
  902                     working_space[peak_vel + 4],
 
  903                     working_space[peak_vel + 5],
 
  904                     working_space[peak_vel + 6]);
 
  905          if (fStatisticType == kFitOptimMaxLikelihood) {
 
  907                chi_opt += yw * TMath::Log(f) - f;
 
  912                chi_opt += (yw - f) * (yw - f) / ywm;
 
  914          if (fStatisticType == kFitOptimChiFuncValues) {
 
  920          else if (fStatisticType == kFitOptimMaxLikelihood) {
 
  932              for (j = 0, k = 0; j < fNPeaks; j++) {
 
  933             if (fFixAmp[j] == 
false) {
 
  934                a = Deramp((Double_t) i, working_space[2 * j + 1],
 
  935                            working_space[peak_vel],
 
  936                            working_space[peak_vel + 1],
 
  937                            working_space[peak_vel + 3],
 
  938                            working_space[peak_vel + 2]);
 
  941                   if (fStatisticType == kFitOptimChiFuncValues) {
 
  942                      b = a * (yw * yw - f * f) / (ywm * ywm);
 
  943                      working_space[2 * shift + k] += b * c;        
 
  944                      b = a * a * (4 * yw - 2 * f) / (ywm * ywm);
 
  945                      working_space[3 * shift + k] += b * c;        
 
  949                      b = a * (yw - f) / ywm;
 
  950                      working_space[2 * shift + k] += b * c;        
 
  952                      working_space[3 * shift + k] += b * c;        
 
  957             if (fFixPosition[j] == 
false) {
 
  958                a = Deri0((Double_t) i, working_space[2 * j],
 
  959                           working_space[2 * j + 1],
 
  960                           working_space[peak_vel],
 
  961                           working_space[peak_vel + 1],
 
  962                           working_space[peak_vel + 3],
 
  963                           working_space[peak_vel + 2]);
 
  964                if (fFitTaylor == kFitTaylorOrderSecond)
 
  965                   d = Derderi0((Double_t) i, working_space[2 * j],
 
  966                                 working_space[2 * j + 1],
 
  967                                 working_space[peak_vel]);
 
  970                   if (TMath::Abs(a) > 0.00000001
 
  971                        && fFitTaylor == kFitTaylorOrderSecond) {
 
  972                      d = d * TMath::Abs(yw - f) / (2 * a * ywm);
 
  973                      if (((a + d) <= 0 && a >= 0) || ((a + d) >= 0 && a <= 0))
 
  980                   if (fStatisticType == kFitOptimChiFuncValues) {
 
  981                      b = a * (yw * yw - f * f) / (ywm * ywm);
 
  982                      working_space[2 * shift + k] += b * c;        
 
  983                      b = a * a * (4 * yw - 2 * f) / (ywm * ywm);
 
  984                      working_space[3 * shift + k] += b * c;        
 
  988                      b = a * (yw - f) / ywm;
 
  989                      working_space[2 * shift + k] += b * c;        
 
  991                      working_space[3 * shift + k] += b * c;        
 
  997          if (fFixSigma == 
false) {
 
  998             a = Dersigma(fNPeaks, (Double_t) i, working_space,
 
  999                           working_space[peak_vel],
 
 1000                           working_space[peak_vel + 1],
 
 1001                           working_space[peak_vel + 3],
 
 1002                           working_space[peak_vel + 2]);
 
 1003             if (fFitTaylor == kFitTaylorOrderSecond)
 
 1004                d = Derdersigma(fNPeaks, (Double_t) i,
 
 1005                                 working_space, working_space[peak_vel]);
 
 1008                if (TMath::Abs(a) > 0.00000001
 
 1009                     && fFitTaylor == kFitTaylorOrderSecond) {
 
 1010                   d = d * TMath::Abs(yw - f) / (2 * a * ywm);
 
 1011                   if (((a + d) <= 0 && a >= 0) || ((a + d) >= 0 && a <= 0))
 
 1018                if (fStatisticType == kFitOptimChiFuncValues) {
 
 1019                   b = a * (yw * yw - f * f) / (ywm * ywm);
 
 1020                   working_space[2 * shift + k] += b * c;        
 
 1021                   b = a * a * (4 * yw - 2 * f) / (ywm * ywm);
 
 1022                   working_space[3 * shift + k] += b * c;        
 
 1026                   b = a * (yw - f) / ywm;
 
 1027                   working_space[2 * shift + k] += b * c;        
 
 1029                   working_space[3 * shift + k] += b * c;        
 
 1034          if (fFixT == 
false) {
 
 1035             a = Dert(fNPeaks, (Double_t) i, working_space,
 
 1036                       working_space[peak_vel],
 
 1037                       working_space[peak_vel + 2]);
 
 1040                if (fStatisticType == kFitOptimChiFuncValues) {
 
 1041                   b = a * (yw * yw - f * f) / (ywm * ywm);
 
 1042                   working_space[2 * shift + k] += b * c;        
 
 1043                   b = a * a * (4 * yw - 2 * f) / (ywm * ywm);
 
 1044                   working_space[3 * shift + k] += b * c;        
 
 1048                   b = a * (yw - f) / ywm;
 
 1049                   working_space[2 * shift + k] += b * c;        
 
 1051                   working_space[3 * shift + k] += b * c;        
 
 1056          if (fFixB == 
false) {
 
 1057             a = Derb(fNPeaks, (Double_t) i, working_space,
 
 1058                       working_space[peak_vel], working_space[peak_vel + 1],
 
 1059                       working_space[peak_vel + 2]);
 
 1062                if (fStatisticType == kFitOptimChiFuncValues) {
 
 1063                   b = a * (yw * yw - f * f) / (ywm * ywm);
 
 1064                   working_space[2 * shift + k] += b * c;        
 
 1065                   b = a * a * (4 * yw - 2 * f) / (ywm * ywm);
 
 1066                   working_space[3 * shift + k] += b * c;        
 
 1070                   b = a * (yw - f) / ywm;
 
 1071                   working_space[2 * shift + k] += b * c;        
 
 1073                   working_space[3 * shift + k] += b * c;        
 
 1078          if (fFixS == 
false) {
 
 1079             a = Ders(fNPeaks, (Double_t) i, working_space,
 
 1080                       working_space[peak_vel]);
 
 1083                if (fStatisticType == kFitOptimChiFuncValues) {
 
 1084                   b = a * (yw * yw - f * f) / (ywm * ywm);
 
 1085                   working_space[2 * shift + k] += b * c;        
 
 1086                   b = a * a * (4 * yw - 2 * f) / (ywm * ywm);
 
 1087                   working_space[3 * shift + k] += b * c;        
 
 1091                   b = a * (yw - f) / ywm;
 
 1092                   working_space[2 * shift + k] += b * c;        
 
 1094                   working_space[3 * shift + k] += b * c;        
 
 1099          if (fFixA0 == 
false) {
 
 1103                if (fStatisticType == kFitOptimChiFuncValues) {
 
 1104                   b = a * (yw * yw - f * f) / (ywm * ywm);
 
 1105                   working_space[2 * shift + k] += b * c;        
 
 1106                   b = a * a * (4 * yw - 2 * f) / (ywm * ywm);
 
 1107                   working_space[3 * shift + k] += b * c;        
 
 1111                   b = a * (yw - f) / ywm;
 
 1112                   working_space[2 * shift + k] += b * c;        
 
 1114                   working_space[3 * shift + k] += b * c;        
 
 1119          if (fFixA1 == 
false) {
 
 1120             a = Dera1((Double_t) i);
 
 1123                if (fStatisticType == kFitOptimChiFuncValues) {
 
 1124                   b = a * (yw * yw - f * f) / (ywm * ywm);
 
 1125                   working_space[2 * shift + k] += b * c;        
 
 1126                   b = a * a * (4 * yw - 2 * f) / (ywm * ywm);
 
 1127                   working_space[3 * shift + k] += b * c;        
 
 1131                   b = a * (yw - f) / ywm;
 
 1132                   working_space[2 * shift + k] += b * c;        
 
 1134                   working_space[3 * shift + k] += b * c;        
 
 1139          if (fFixA2 == 
false) {
 
 1140             a = Dera2((Double_t) i);
 
 1143                if (fStatisticType == kFitOptimChiFuncValues) {
 
 1144                   b = a * (yw * yw - f * f) / (ywm * ywm);
 
 1145                   working_space[2 * shift + k] += b * c;        
 
 1146                   b = a * a * (4 * yw - 2 * f) / (ywm * ywm);
 
 1147                   working_space[3 * shift + k] += b * c;        
 
 1151                   b = a * (yw - f) / ywm;
 
 1152                   working_space[2 * shift + k] += b * c;        
 
 1154                   working_space[3 * shift + k] += b * c;        
 
 1160       for (j = 0; j < rozmer; j++) {
 
 1161          if (TMath::Abs(working_space[3 * shift + j]) > 0.000001)
 
 1162             working_space[2 * shift + j] = working_space[2 * shift + j] / TMath::Abs(working_space[3 * shift + j]);        
 
 1164             working_space[2 * shift + j] = 0;        
 
 1169       chi_opt = TMath::Sqrt(TMath::Abs(chi_opt));
 
 1173       for (j = 0; j < rozmer; j++) {
 
 1174          working_space[4 * shift + j] = working_space[shift + j];        
 
 1178          if (fAlphaOptim == kFitAlphaOptimal) {
 
 1179             if (fStatisticType != kFitOptimMaxLikelihood)
 
 1180                chi_min = 10000 * chi2;
 
 1183                chi_min = 0.1 * chi2;
 
 1185             for (pi = 0.1; flag == 0 && pi <= 100; pi += 0.1) {
 
 1186                for (j = 0; j < rozmer; j++) {
 
 1187                   working_space[shift + j] = working_space[4 * shift + j] + pi * alpha * working_space[2 * shift + j];        
 
 1189                for (i = 0, j = 0; i < fNPeaks; i++) {
 
 1190                   if (fFixAmp[i] == 
false) {
 
 1191                      if (working_space[shift + j] < 0)        
 
 1192                         working_space[shift + j] = 0;        
 
 1193                      working_space[2 * i] = working_space[shift + j];        
 
 1196                   if (fFixPosition[i] == 
false) {
 
 1197                      if (working_space[shift + j] < fXmin)        
 
 1198                         working_space[shift + j] = fXmin;        
 
 1199                      if (working_space[shift + j] > fXmax)        
 
 1200                         working_space[shift + j] = fXmax;        
 
 1201                      working_space[2 * i + 1] = working_space[shift + j];        
 
 1205                if (fFixSigma == 
false) {
 
 1206                   if (working_space[shift + j] < 0.001) {        
 
 1207                      working_space[shift + j] = 0.001;        
 
 1209                   working_space[peak_vel] = working_space[shift + j];        
 
 1212                if (fFixT == 
false) {
 
 1213                   working_space[peak_vel + 1] = working_space[shift + j];        
 
 1216                if (fFixB == 
false) {
 
 1217                   if (TMath::Abs(working_space[shift + j]) < 0.001) {        
 
 1218                      if (working_space[shift + j] < 0)        
 
 1219                         working_space[shift + j] = -0.001;        
 
 1221                         working_space[shift + j] = 0.001;        
 
 1223                   working_space[peak_vel + 2] = working_space[shift + j];        
 
 1226                if (fFixS == 
false) {
 
 1227                   working_space[peak_vel + 3] = working_space[shift + j];        
 
 1230                if (fFixA0 == 
false) {
 
 1231                   working_space[peak_vel + 4] = working_space[shift + j];        
 
 1234                if (fFixA1 == 
false) {
 
 1235                   working_space[peak_vel + 5] = working_space[shift + j];        
 
 1238                if (fFixA2 == 
false) {
 
 1239                   working_space[peak_vel + 6] = working_space[shift + j];        
 
 1243                for (i = fXmin; i <= fXmax; i++) {
 
 1246                   f = Shape(fNPeaks, (Double_t) i, working_space,
 
 1247                   working_space[peak_vel],
 
 1248                   working_space[peak_vel + 1],
 
 1249                   working_space[peak_vel + 3],
 
 1250                   working_space[peak_vel + 2],
 
 1251                   working_space[peak_vel + 4],
 
 1252                   working_space[peak_vel + 5],
 
 1253                   working_space[peak_vel + 6]);
 
 1254                   if (fStatisticType == kFitOptimChiFuncValues) {
 
 1259                   if (fStatisticType == kFitOptimMaxLikelihood) {
 
 1261                         chi2 += yw * TMath::Log(f) - f;
 
 1266                         chi2 += (yw - f) * (yw - f) / ywm;
 
 1270                     && fStatisticType != kFitOptimMaxLikelihood)
 
 1272                     && fStatisticType == kFitOptimMaxLikelihood)) {
 
 1273                   pmin = pi, chi_min = chi2;
 
 1283                for (j = 0; j < rozmer; j++) {
 
 1284                   working_space[shift + j] = working_space[4 * shift + j] + pmin * alpha * working_space[2 * shift + j];        
 
 1286                for (i = 0, j = 0; i < fNPeaks; i++) {
 
 1287                   if (fFixAmp[i] == 
false) {
 
 1288                      if (working_space[shift + j] < 0)        
 
 1289                         working_space[shift + j] = 0;        
 
 1290                      working_space[2 * i] = working_space[shift + j];        
 
 1293                   if (fFixPosition[i] == 
false) {
 
 1294                      if (working_space[shift + j] < fXmin)        
 
 1295                         working_space[shift + j] = fXmin;        
 
 1296                      if (working_space[shift + j] > fXmax)        
 
 1297                         working_space[shift + j] = fXmax;        
 
 1298                      working_space[2 * i + 1] = working_space[shift + j];        
 
 1302                if (fFixSigma == 
false) {
 
 1303                   if (working_space[shift + j] < 0.001) {        
 
 1304                      working_space[shift + j] = 0.001;        
 
 1306                   working_space[peak_vel] = working_space[shift + j];        
 
 1309                if (fFixT == 
false) {
 
 1310                   working_space[peak_vel + 1] = working_space[shift + j];        
 
 1313                if (fFixB == 
false) {
 
 1314                   if (TMath::Abs(working_space[shift + j]) < 0.001) {        
 
 1315                      if (working_space[shift + j] < 0)        
 
 1316                         working_space[shift + j] = -0.001;        
 
 1318                         working_space[shift + j] = 0.001;        
 
 1320                   working_space[peak_vel + 2] = working_space[shift + j];        
 
 1323                if (fFixS == 
false) {
 
 1324                   working_space[peak_vel + 3] = working_space[shift + j];        
 
 1327                if (fFixA0 == 
false) {
 
 1328                   working_space[peak_vel + 4] = working_space[shift + j];        
 
 1331                if (fFixA1 == 
false) {
 
 1332                   working_space[peak_vel + 5] = working_space[shift + j];        
 
 1335                if (fFixA2 == 
false) {
 
 1336                   working_space[peak_vel + 6] = working_space[shift + j];        
 
 1344             for (j = 0; j < rozmer; j++) {
 
 1345                working_space[shift + j] = working_space[4 * shift + j] + alpha * working_space[2 * shift + j];        
 
 1347             for (i = 0, j = 0; i < fNPeaks; i++) {
 
 1348                if (fFixAmp[i] == 
false) {
 
 1349                   if (working_space[shift + j] < 0)        
 
 1350                      working_space[shift + j] = 0;        
 
 1351                   working_space[2 * i] = working_space[shift + j];        
 
 1354                if (fFixPosition[i] == 
false) {
 
 1355                   if (working_space[shift + j] < fXmin)        
 
 1356                      working_space[shift + j] = fXmin;        
 
 1357                   if (working_space[shift + j] > fXmax)        
 
 1358                      working_space[shift + j] = fXmax;        
 
 1359                   working_space[2 * i + 1] = working_space[shift + j];        
 
 1363             if (fFixSigma == 
false) {
 
 1364                if (working_space[shift + j] < 0.001) {        
 
 1365                   working_space[shift + j] = 0.001;        
 
 1367                working_space[peak_vel] = working_space[shift + j];        
 
 1370             if (fFixT == 
false) {
 
 1371                working_space[peak_vel + 1] = working_space[shift + j];        
 
 1374             if (fFixB == 
false) {
 
 1375                if (TMath::Abs(working_space[shift + j]) < 0.001) {        
 
 1376                   if (working_space[shift + j] < 0)        
 
 1377                      working_space[shift + j] = -0.001;        
 
 1379                      working_space[shift + j] = 0.001;        
 
 1381                working_space[peak_vel + 2] = working_space[shift + j];        
 
 1384             if (fFixS == 
false) {
 
 1385                working_space[peak_vel + 3] = working_space[shift + j];        
 
 1388             if (fFixA0 == 
false) {
 
 1389                working_space[peak_vel + 4] = working_space[shift + j];        
 
 1392             if (fFixA1 == 
false) {
 
 1393                working_space[peak_vel + 5] = working_space[shift + j];        
 
 1396             if (fFixA2 == 
false) {
 
 1397                working_space[peak_vel + 6] = working_space[shift + j];        
 
 1401             for (i = fXmin; i <= fXmax; i++) {
 
 1404                f = Shape(fNPeaks, (Double_t) i, working_space,
 
 1405                working_space[peak_vel],
 
 1406                working_space[peak_vel + 1],
 
 1407                working_space[peak_vel + 3],
 
 1408                working_space[peak_vel + 2],
 
 1409                working_space[peak_vel + 4],
 
 1410                working_space[peak_vel + 5],
 
 1411                working_space[peak_vel + 6]);
 
 1412                if (fStatisticType == kFitOptimChiFuncValues) {
 
 1417                if (fStatisticType == kFitOptimMaxLikelihood) {
 
 1419                      chi += yw * TMath::Log(f) - f;
 
 1424                      chi += (yw - f) * (yw - f) / ywm;
 
 1429          chi = TMath::Sqrt(TMath::Abs(chi));
 
 1430          if (fAlphaOptim == kFitAlphaHalving && chi > 1E-6)
 
 1431             alpha = alpha * chi_opt / (2 * chi);
 
 1433          else if (fAlphaOptim == kFitAlphaOptimal)
 
 1434             alpha = alpha / 10.0;
 
 1437       } 
while (((chi > chi_opt
 
 1438                  && fStatisticType != kFitOptimMaxLikelihood)
 
 1440                  && fStatisticType == kFitOptimMaxLikelihood))
 
 1441                 && regul_cycle < kFitNumRegulCycles);
 
 1442       for (j = 0; j < rozmer; j++) {
 
 1443          working_space[4 * shift + j] = 0;        
 
 1444          working_space[2 * shift + j] = 0;        
 
 1446       for (i = fXmin, chi_cel = 0; i <= fXmax; i++) {
 
 1450          f = Shape(fNPeaks, (Double_t) i, working_space,
 
 1451          working_space[peak_vel], working_space[peak_vel + 1],
 
 1452          working_space[peak_vel + 3],
 
 1453          working_space[peak_vel + 2],
 
 1454          working_space[peak_vel + 4],
 
 1455          working_space[peak_vel + 5],
 
 1456          working_space[peak_vel + 6]);
 
 1457          chi_opt = (yw - f) * (yw - f) / yw;
 
 1458          chi_cel += (yw - f) * (yw - f) / yw;
 
 1461          for (j = 0, k = 0; j < fNPeaks; j++) {
 
 1462             if (fFixAmp[j] == 
false) {
 
 1463                a = Deramp((Double_t) i, working_space[2 * j + 1],
 
 1464                working_space[peak_vel],
 
 1465                working_space[peak_vel + 1],
 
 1466                working_space[peak_vel + 3],
 
 1467                working_space[peak_vel + 2]);
 
 1470                   working_space[2 * shift + k] += chi_opt * c;        
 
 1472                   working_space[4 * shift + k] += b * c;        
 
 1476             if (fFixPosition[j] == 
false) {
 
 1477                a = Deri0((Double_t) i, working_space[2 * j],
 
 1478                working_space[2 * j + 1],
 
 1479                working_space[peak_vel],
 
 1480                working_space[peak_vel + 1],
 
 1481                working_space[peak_vel + 3],
 
 1482                working_space[peak_vel + 2]);
 
 1485                   working_space[2 * shift + k] += chi_opt * c;        
 
 1487                   working_space[4 * shift + k] += b * c;        
 
 1492          if (fFixSigma == 
false) {
 
 1493             a = Dersigma(fNPeaks, (Double_t) i, working_space,
 
 1494             working_space[peak_vel],
 
 1495             working_space[peak_vel + 1],
 
 1496             working_space[peak_vel + 3],
 
 1497             working_space[peak_vel + 2]);
 
 1500                working_space[2 * shift + k] += chi_opt * c;        
 
 1502                working_space[4 * shift + k] += b * c;        
 
 1506          if (fFixT == 
false) {
 
 1507             a = Dert(fNPeaks, (Double_t) i, working_space,
 
 1508             working_space[peak_vel],
 
 1509             working_space[peak_vel + 2]);
 
 1512                working_space[2 * shift + k] += chi_opt * c;        
 
 1514                working_space[4 * shift + k] += b * c;        
 
 1518          if (fFixB == 
false) {
 
 1519             a = Derb(fNPeaks, (Double_t) i, working_space,
 
 1520             working_space[peak_vel], working_space[peak_vel + 1],
 
 1521             working_space[peak_vel + 2]);
 
 1524                working_space[2 * shift + k] += chi_opt * c;        
 
 1526                working_space[4 * shift + k] += b * c;        
 
 1530          if (fFixS == 
false) {
 
 1531             a = Ders(fNPeaks, (Double_t) i, working_space,
 
 1532             working_space[peak_vel]);
 
 1535                working_space[2 * shift + k] += chi_opt * c;        
 
 1537                working_space[4 * shift + k] += b * c;        
 
 1541          if (fFixA0 == 
false) {
 
 1545                working_space[2 * shift + k] += chi_opt * c;        
 
 1547                working_space[4 * shift + k] += b * c;        
 
 1551          if (fFixA1 == 
false) {
 
 1552             a = Dera1((Double_t) i);
 
 1555                working_space[2 * shift + k] += chi_opt * c;        
 
 1557                working_space[4 * shift + k] += b * c;        
 
 1561          if (fFixA2 == 
false) {
 
 1562             a = Dera2((Double_t) i);
 
 1565                working_space[2 * shift + k] += chi_opt * c;        
 
 1567                working_space[4 * shift + k] += b * c;        
 
 1573    b = fXmax - fXmin + 1 - rozmer;
 
 1574    chi_er = chi_cel / b;
 
 1575    for (i = 0, j = 0; i < fNPeaks; i++) {
 
 1577           Area(working_space[2 * i], working_space[peak_vel],
 
 1578                working_space[peak_vel + 1], working_space[peak_vel + 2]);
 
 1579       if (fFixAmp[i] == 
false) {
 
 1580          fAmpCalc[i] = working_space[shift + j];        
 
 1581          if (working_space[3 * shift + j] != 0)
 
 1582             fAmpErr[i] = TMath::Sqrt(TMath::Abs(working_space[2 * shift + j])) / TMath::Sqrt(TMath::Abs(working_space[3 * shift + j]));        
 
 1584             a = Derpa(working_space[peak_vel],
 
 1585                        working_space[peak_vel + 1],
 
 1586                        working_space[peak_vel + 2]);
 
 1587             b = working_space[4 * shift + j];        
 
 1593             fAreaErr[i] = TMath::Sqrt(TMath::Abs(a * a * b * chi_er));
 
 1602          fAmpCalc[i] = fAmpInit[i];
 
 1606       if (fFixPosition[i] == 
false) {
 
 1607          fPositionCalc[i] = working_space[shift + j];        
 
 1608          if (working_space[3 * shift + j] != 0)        
 
 1609             fPositionErr[i] = TMath::Sqrt(TMath::Abs(working_space[2 * shift + j])) / TMath::Sqrt(TMath::Abs(working_space[3 * shift + j]));        
 
 1614          fPositionCalc[i] = fPositionInit[i];
 
 1615          fPositionErr[i] = 0;
 
 1618    if (fFixSigma == 
false) {
 
 1619       fSigmaCalc = working_space[shift + j];        
 
 1620       if (working_space[3 * shift + j] != 0)        
 
 1621          fSigmaErr = TMath::Sqrt(TMath::Abs(working_space[2 * shift + j])) / TMath::Sqrt(TMath::Abs(working_space[3 * shift + j]));        
 
 1626       fSigmaCalc = fSigmaInit;
 
 1629    if (fFixT == 
false) {
 
 1630       fTCalc = working_space[shift + j];        
 
 1631       if (working_space[3 * shift + j] != 0)        
 
 1632          fTErr = TMath::Sqrt(TMath::Abs(working_space[2 * shift + j])) / TMath::Sqrt(TMath::Abs(working_space[3 * shift + j]));        
 
 1640    if (fFixB == 
false) {
 
 1641       fBCalc = working_space[shift + j];        
 
 1642       if (working_space[3 * shift + j] != 0)        
 
 1643          fBErr = TMath::Sqrt(TMath::Abs(working_space[2 * shift + j])) / TMath::Sqrt(TMath::Abs(working_space[3 * shift + j]));        
 
 1651    if (fFixS == 
false) {
 
 1652       fSCalc = working_space[shift + j];        
 
 1653       if (working_space[3 * shift + j] != 0)        
 
 1654          fSErr = TMath::Sqrt(TMath::Abs(working_space[2 * shift + j])) / TMath::Sqrt(TMath::Abs(working_space[3 * shift + j]));        
 
 1662    if (fFixA0 == 
false) {
 
 1663       fA0Calc = working_space[shift + j];        
 
 1664       if (working_space[3 * shift + j] != 0)        
 
 1665          fA0Err = TMath::Sqrt(TMath::Abs(working_space[2 * shift + j])) / TMath::Sqrt(TMath::Abs(working_space[3 * shift + j]));        
 
 1673    if (fFixA1 == 
false) {
 
 1674       fA1Calc = working_space[shift + j];        
 
 1675       if (working_space[3 * shift + j] != 0)        
 
 1676          fA1Err = TMath::Sqrt(TMath::Abs(working_space[2 * shift + j])) / TMath::Sqrt(TMath::Abs(working_space[3 * shift + j]));        
 
 1684    if (fFixA2 == 
false) {
 
 1685       fA2Calc = working_space[shift + j];        
 
 1686       if (working_space[3 * shift + j] != 0)        
 
 1687          fA2Err = TMath::Sqrt(TMath::Abs(working_space[2 * shift + j])) / TMath::Sqrt(TMath::Abs(working_space[3 * shift + j]));        
 
 1695    b = fXmax - fXmin + 1 - rozmer;
 
 1697    for (i = fXmin; i <= fXmax; i++) {
 
 1698       f = Shape(fNPeaks, (Double_t) i, working_space,
 
 1699                  working_space[peak_vel], working_space[peak_vel + 1],
 
 1700                  working_space[peak_vel + 3], working_space[peak_vel + 2],
 
 1701                  working_space[peak_vel + 4], working_space[peak_vel + 5],
 
 1702                  working_space[peak_vel + 6]);
 
 1705    delete[]working_space;
 
 1721 void TSpectrumFit::StiefelInversion(Double_t **a, Int_t size)
 
 1724    Double_t sk = 0, b, lambdak, normk, normk_old = 0;
 
 1730           for (i = 0; i < size; i++) {
 
 1731          a[i][size + 2] = -a[i][size];        
 
 1732          for (j = 0; j < size; j++) {
 
 1733             a[i][size + 2] += a[i][j] * a[j][size + 1];        
 
 1735          normk += a[i][size + 2] * a[i][size + 2];        
 
 1740          sk = normk / normk_old;
 
 1744       for (i = 0; i < size; i++) {
 
 1745          a[i][size + 3] = -a[i][size + 2] + sk * a[i][size + 3];        
 
 1750       for (i = 0; i < size; i++) {
 
 1751          for (j = 0, b = 0; j < size; j++) {
 
 1752             b += a[i][j] * a[j][size + 3];        
 
 1754          lambdak += b * a[i][size + 3];
 
 1756       if (TMath::Abs(lambdak) > 1e-50)        
 
 1757          lambdak = normk / lambdak;
 
 1761       for (i = 0; i < size; i++)
 
 1762          a[i][size + 1] += lambdak * a[i][size + 3];        
 
 1765    } 
while (k < size && TMath::Abs(normk) > 1e-50);        
 
 1857 void TSpectrumFit::FitStiefel(Double_t *source)
 
 1859    Int_t i, j, k, shift =
 
 1860        2 * fNPeaks + 7, peak_vel, rozmer, iter, regul_cycle,
 
 1862    Double_t a, b, alpha, chi_opt, yw, ywm, f, chi2, chi_min, chi =
 
 1863        0, pi, pmin = 0, chi_cel = 0, chi_er;
 
 1864    Double_t *working_space = 
new Double_t[5 * (2 * fNPeaks + 7)];
 
 1865    for (i = 0, j = 0; i < fNPeaks; i++) {
 
 1866       working_space[2 * i] = fAmpInit[i];        
 
 1867       if (fFixAmp[i] == 
false) {
 
 1868          working_space[shift + j] = fAmpInit[i];        
 
 1871       working_space[2 * i + 1] = fPositionInit[i];        
 
 1872       if (fFixPosition[i] == 
false) {
 
 1873          working_space[shift + j] = fPositionInit[i];        
 
 1878    working_space[2 * i] = fSigmaInit;        
 
 1879    if (fFixSigma == 
false) {
 
 1880       working_space[shift + j] = fSigmaInit;        
 
 1883    working_space[2 * i + 1] = fTInit;        
 
 1884    if (fFixT == 
false) {
 
 1885       working_space[shift + j] = fTInit;        
 
 1888    working_space[2 * i + 2] = fBInit;        
 
 1889    if (fFixB == 
false) {
 
 1890       working_space[shift + j] = fBInit;        
 
 1893    working_space[2 * i + 3] = fSInit;        
 
 1894    if (fFixS == 
false) {
 
 1895       working_space[shift + j] = fSInit;        
 
 1898    working_space[2 * i + 4] = fA0Init;        
 
 1899    if (fFixA0 == 
false) {
 
 1900       working_space[shift + j] = fA0Init;        
 
 1903    working_space[2 * i + 5] = fA1Init;        
 
 1904    if (fFixA1 == 
false) {
 
 1905       working_space[shift + j] = fA1Init;        
 
 1908    working_space[2 * i + 6] = fA2Init;        
 
 1909    if (fFixA2 == 
false) {
 
 1910       working_space[shift + j] = fA2Init;        
 
 1915       Error (
"FitAwmi",
"All parameters are fixed");
 
 1916       delete [] working_space;
 
 1919    if (rozmer >= fXmax - fXmin + 1){
 
 1920       Error (
"FitAwmi",
"Number of fitted parameters is larger than # of fitted points");
 
 1921       delete [] working_space;
 
 1924    Double_t **working_matrix = 
new Double_t *[rozmer];
 
 1925    for (i = 0; i < rozmer; i++)
 
 1926       working_matrix[i] = 
new Double_t[rozmer + 4];
 
 1927    for (iter = 0; iter < fNumberIterations; iter++) {
 
 1928       for (j = 0; j < rozmer; j++) {
 
 1929          working_space[3 * shift + j] = 0;        
 
 1930          for (k = 0; k < (rozmer + 4); k++) {
 
 1931             working_matrix[j][k] = 0;
 
 1938       for (i = fXmin; i <= fXmax; i++) {
 
 1941              for (j = 0, k = 0; j < fNPeaks; j++) {
 
 1942             if (fFixAmp[j] == 
false) {
 
 1943                working_space[2 * shift + k] =
 
 1944                    Deramp((Double_t) i, working_space[2 * j + 1],
 
 1945                working_space[peak_vel],
 
 1946                working_space[peak_vel + 1],
 
 1947                working_space[peak_vel + 3],
 
 1948                working_space[peak_vel + 2]);
 
 1951             if (fFixPosition[j] == 
false) {
 
 1952                working_space[2 * shift + k] =
 
 1953                    Deri0((Double_t) i, working_space[2 * j],
 
 1954                working_space[2 * j + 1], working_space[peak_vel],
 
 1955                working_space[peak_vel + 1],
 
 1956                working_space[peak_vel + 3],
 
 1957                working_space[peak_vel + 2]);
 
 1960          } 
if (fFixSigma == 
false) {
 
 1961             working_space[2 * shift + k] =
 
 1962                 Dersigma(fNPeaks, (Double_t) i, working_space,
 
 1963             working_space[peak_vel],
 
 1964             working_space[peak_vel + 1],
 
 1965             working_space[peak_vel + 3],
 
 1966             working_space[peak_vel + 2]);
 
 1969          if (fFixT == 
false) {
 
 1970             working_space[2 * shift + k] =
 
 1971                 Dert(fNPeaks, (Double_t) i, working_space,
 
 1972             working_space[peak_vel], working_space[peak_vel + 2]);
 
 1975          if (fFixB == 
false) {
 
 1976             working_space[2 * shift + k] =
 
 1977                 Derb(fNPeaks, (Double_t) i, working_space,
 
 1978             working_space[peak_vel], working_space[peak_vel + 1],
 
 1979             working_space[peak_vel + 2]);
 
 1982          if (fFixS == 
false) {
 
 1983             working_space[2 * shift + k] =
 
 1984                 Ders(fNPeaks, (Double_t) i, working_space,
 
 1985             working_space[peak_vel]);
 
 1988          if (fFixA0 == 
false) {
 
 1989             working_space[2 * shift + k] = 1.;
 
 1992          if (fFixA1 == 
false) {
 
 1993             working_space[2 * shift + k] = Dera1((Double_t) i);
 
 1996          if (fFixA2 == 
false) {
 
 1997             working_space[2 * shift + k] = Dera2((Double_t) i);
 
 2002          f = Shape(fNPeaks, (Double_t) i, working_space,
 
 2003          working_space[peak_vel], working_space[peak_vel + 1],
 
 2004          working_space[peak_vel + 3],
 
 2005          working_space[peak_vel + 2],
 
 2006          working_space[peak_vel + 4],
 
 2007          working_space[peak_vel + 5],
 
 2008          working_space[peak_vel + 6]);
 
 2009          if (fStatisticType == kFitOptimMaxLikelihood) {
 
 2011                chi_opt += yw * TMath::Log(f) - f;
 
 2016                chi_opt += (yw - f) * (yw - f) / ywm;
 
 2018          if (fStatisticType == kFitOptimChiFuncValues) {
 
 2024          else if (fStatisticType == kFitOptimMaxLikelihood) {
 
 2034          for (j = 0; j < rozmer; j++) {
 
 2035             for (k = 0; k < rozmer; k++) {
 
 2036                b = working_space[2 * shift +
 
 2037                                   j] * working_space[2 * shift + k] / ywm;
 
 2038                if (fStatisticType == kFitOptimChiFuncValues)
 
 2039                   b = b * (4 * yw - 2 * f) / ywm;
 
 2040                working_matrix[j][k] += b;
 
 2042                   working_space[3 * shift + j] += b;
 
 2045          if (fStatisticType == kFitOptimChiFuncValues)
 
 2046             b = (f * f - yw * yw) / (ywm * ywm);
 
 2050          for (j = 0; j < rozmer; j++) {
 
 2051             working_matrix[j][rozmer] -= b * working_space[2 * shift + j];
 
 2054       for (i = 0; i < rozmer; i++) {
 
 2055          working_matrix[i][rozmer + 1] = 0;        
 
 2057       StiefelInversion(working_matrix, rozmer);
 
 2058       for (i = 0; i < rozmer; i++) {
 
 2059          working_space[2 * shift + i] = working_matrix[i][rozmer + 1];        
 
 2064       chi_opt = TMath::Sqrt(TMath::Abs(chi_opt));
 
 2068       for (j = 0; j < rozmer; j++) {
 
 2069          working_space[4 * shift + j] = working_space[shift + j];        
 
 2073          if (fAlphaOptim == kFitAlphaOptimal) {
 
 2074             if (fStatisticType != kFitOptimMaxLikelihood)
 
 2075                chi_min = 10000 * chi2;
 
 2078                chi_min = 0.1 * chi2;
 
 2080             for (pi = 0.1; flag == 0 && pi <= 100; pi += 0.1) {
 
 2081                for (j = 0; j < rozmer; j++) {
 
 2082                   working_space[shift + j] = working_space[4 * shift + j] + pi * alpha * working_space[2 * shift + j];        
 
 2084                for (i = 0, j = 0; i < fNPeaks; i++) {
 
 2085                   if (fFixAmp[i] == 
false) {
 
 2086                      if (working_space[shift + j] < 0)        
 
 2087                         working_space[shift + j] = 0;        
 
 2088                      working_space[2 * i] = working_space[shift + j];        
 
 2091                   if (fFixPosition[i] == 
false) {
 
 2092                      if (working_space[shift + j] < fXmin)        
 
 2093                         working_space[shift + j] = fXmin;        
 
 2094                      if (working_space[shift + j] > fXmax)        
 
 2095                         working_space[shift + j] = fXmax;        
 
 2096                      working_space[2 * i + 1] = working_space[shift + j];        
 
 2100                if (fFixSigma == 
false) {
 
 2101                   if (working_space[shift + j] < 0.001) {        
 
 2102                      working_space[shift + j] = 0.001;        
 
 2104                   working_space[peak_vel] = working_space[shift + j];        
 
 2107                if (fFixT == 
false) {
 
 2108                   working_space[peak_vel + 1] = working_space[shift + j];        
 
 2111                if (fFixB == 
false) {
 
 2112                   if (TMath::Abs(working_space[shift + j]) < 0.001) {        
 
 2113                      if (working_space[shift + j] < 0)        
 
 2114                         working_space[shift + j] = -0.001;        
 
 2116                         working_space[shift + j] = 0.001;        
 
 2118                   working_space[peak_vel + 2] = working_space[shift + j];        
 
 2121                if (fFixS == 
false) {
 
 2122                   working_space[peak_vel + 3] = working_space[shift + j];        
 
 2125                if (fFixA0 == 
false) {
 
 2126                   working_space[peak_vel + 4] = working_space[shift + j];        
 
 2129                if (fFixA1 == 
false) {
 
 2130                   working_space[peak_vel + 5] = working_space[shift + j];        
 
 2133                if (fFixA2 == 
false) {
 
 2134                   working_space[peak_vel + 6] = working_space[shift + j];        
 
 2138                for (i = fXmin; i <= fXmax; i++) {
 
 2141                   f = Shape(fNPeaks, (Double_t) i, working_space,
 
 2142                   working_space[peak_vel],
 
 2143                   working_space[peak_vel + 1],
 
 2144                   working_space[peak_vel + 3],
 
 2145                   working_space[peak_vel + 2],
 
 2146                   working_space[peak_vel + 4],
 
 2147                   working_space[peak_vel + 5],
 
 2148                   working_space[peak_vel + 6]);
 
 2149                   if (fStatisticType == kFitOptimChiFuncValues) {
 
 2154                   if (fStatisticType == kFitOptimMaxLikelihood) {
 
 2156                         chi2 += yw * TMath::Log(f) - f;
 
 2161                         chi2 += (yw - f) * (yw - f) / ywm;
 
 2165                     && fStatisticType != kFitOptimMaxLikelihood)
 
 2167                     && fStatisticType == kFitOptimMaxLikelihood)) {
 
 2168                   pmin = pi, chi_min = chi2;
 
 2178                for (j = 0; j < rozmer; j++) {
 
 2179                   working_space[shift + j] = working_space[4 * shift + j] + pmin * alpha * working_space[2 * shift + j];        
 
 2181                for (i = 0, j = 0; i < fNPeaks; i++) {
 
 2182                   if (fFixAmp[i] == 
false) {
 
 2183                      if (working_space[shift + j] < 0)        
 
 2184                         working_space[shift + j] = 0;        
 
 2185                      working_space[2 * i] = working_space[shift + j];        
 
 2188                   if (fFixPosition[i] == 
false) {
 
 2189                      if (working_space[shift + j] < fXmin)        
 
 2190                         working_space[shift + j] = fXmin;        
 
 2191                      if (working_space[shift + j] > fXmax)        
 
 2192                         working_space[shift + j] = fXmax;        
 
 2193                      working_space[2 * i + 1] = working_space[shift + j];        
 
 2197                if (fFixSigma == 
false) {
 
 2198                   if (working_space[shift + j] < 0.001) {        
 
 2199                      working_space[shift + j] = 0.001;        
 
 2201                   working_space[peak_vel] = working_space[shift + j];        
 
 2204                if (fFixT == 
false) {
 
 2205                   working_space[peak_vel + 1] = working_space[shift + j];        
 
 2208                if (fFixB == 
false) {
 
 2209                   if (TMath::Abs(working_space[shift + j]) < 0.001) {        
 
 2210                      if (working_space[shift + j] < 0)        
 
 2211                         working_space[shift + j] = -0.001;        
 
 2213                         working_space[shift + j] = 0.001;        
 
 2215                   working_space[peak_vel + 2] = working_space[shift + j];        
 
 2218                if (fFixS == 
false) {
 
 2219                   working_space[peak_vel + 3] = working_space[shift + j];        
 
 2222                if (fFixA0 == 
false) {
 
 2223                   working_space[peak_vel + 4] = working_space[shift + j];        
 
 2226                if (fFixA1 == 
false) {
 
 2227                   working_space[peak_vel + 5] = working_space[shift + j];        
 
 2230                if (fFixA2 == 
false) {
 
 2231                   working_space[peak_vel + 6] = working_space[shift + j];        
 
 2239             for (j = 0; j < rozmer; j++) {
 
 2240                working_space[shift + j] = working_space[4 * shift + j] + alpha * working_space[2 * shift + j];        
 
 2242             for (i = 0, j = 0; i < fNPeaks; i++) {
 
 2243                if (fFixAmp[i] == 
false) {
 
 2244                   if (working_space[shift + j] < 0)        
 
 2245                      working_space[shift + j] = 0;        
 
 2246                   working_space[2 * i] = working_space[shift + j];        
 
 2249                if (fFixPosition[i] == 
false) {
 
 2250                   if (working_space[shift + j] < fXmin)        
 
 2251                      working_space[shift + j] = fXmin;        
 
 2252                   if (working_space[shift + j] > fXmax)        
 
 2253                      working_space[shift + j] = fXmax;        
 
 2254                   working_space[2 * i + 1] = working_space[shift + j];        
 
 2258             if (fFixSigma == 
false) {
 
 2259                if (working_space[shift + j] < 0.001) {        
 
 2260                   working_space[shift + j] = 0.001;        
 
 2262                working_space[peak_vel] = working_space[shift + j];        
 
 2265             if (fFixT == 
false) {
 
 2266                working_space[peak_vel + 1] = working_space[shift + j];        
 
 2269             if (fFixB == 
false) {
 
 2270                if (TMath::Abs(working_space[shift + j]) < 0.001) {        
 
 2271                   if (working_space[shift + j] < 0)        
 
 2272                      working_space[shift + j] = -0.001;        
 
 2274                      working_space[shift + j] = 0.001;        
 
 2276                working_space[peak_vel + 2] = working_space[shift + j];        
 
 2279             if (fFixS == 
false) {
 
 2280                working_space[peak_vel + 3] = working_space[shift + j];        
 
 2283             if (fFixA0 == 
false) {
 
 2284                working_space[peak_vel + 4] = working_space[shift + j];        
 
 2287             if (fFixA1 == 
false) {
 
 2288                working_space[peak_vel + 5] = working_space[shift + j];        
 
 2291             if (fFixA2 == 
false) {
 
 2292                working_space[peak_vel + 6] = working_space[shift + j];        
 
 2296             for (i = fXmin; i <= fXmax; i++) {
 
 2299                f = Shape(fNPeaks, (Double_t) i, working_space,
 
 2300                working_space[peak_vel],
 
 2301                working_space[peak_vel + 1],
 
 2302                working_space[peak_vel + 3],
 
 2303                working_space[peak_vel + 2],
 
 2304                working_space[peak_vel + 4],
 
 2305                working_space[peak_vel + 5],
 
 2306                working_space[peak_vel + 6]);
 
 2307                if (fStatisticType == kFitOptimChiFuncValues) {
 
 2312                if (fStatisticType == kFitOptimMaxLikelihood) {
 
 2314                      chi += yw * TMath::Log(f) - f;
 
 2319                      chi += (yw - f) * (yw - f) / ywm;
 
 2324          chi = TMath::Sqrt(TMath::Abs(chi));
 
 2325          if (fAlphaOptim == kFitAlphaHalving && chi > 1E-6)
 
 2326             alpha = alpha * chi_opt / (2 * chi);
 
 2328          else if (fAlphaOptim == kFitAlphaOptimal)
 
 2329             alpha = alpha / 10.0;
 
 2332       } 
while (((chi > chi_opt
 
 2333                  && fStatisticType != kFitOptimMaxLikelihood)
 
 2335                  && fStatisticType == kFitOptimMaxLikelihood))
 
 2336                 && regul_cycle < kFitNumRegulCycles);
 
 2337       for (j = 0; j < rozmer; j++) {
 
 2338          working_space[4 * shift + j] = 0;        
 
 2339          working_space[2 * shift + j] = 0;        
 
 2341       for (i = fXmin, chi_cel = 0; i <= fXmax; i++) {
 
 2345          f = Shape(fNPeaks, (Double_t) i, working_space,
 
 2346          working_space[peak_vel], working_space[peak_vel + 1],
 
 2347          working_space[peak_vel + 3],
 
 2348          working_space[peak_vel + 2],
 
 2349          working_space[peak_vel + 4],
 
 2350          working_space[peak_vel + 5],
 
 2351          working_space[peak_vel + 6]);
 
 2352          chi_opt = (yw - f) * (yw - f) / yw;
 
 2353          chi_cel += (yw - f) * (yw - f) / yw;
 
 2356              for (j = 0, k = 0; j < fNPeaks; j++) {
 
 2357             if (fFixAmp[j] == 
false) {
 
 2358                a = Deramp((Double_t) i, working_space[2 * j + 1],
 
 2359                working_space[peak_vel],
 
 2360                working_space[peak_vel + 1],
 
 2361                working_space[peak_vel + 3],
 
 2362                working_space[peak_vel + 2]);
 
 2364                   working_space[2 * shift + k] += chi_opt;        
 
 2366                   working_space[4 * shift + k] += b;        
 
 2370             if (fFixPosition[j] == 
false) {
 
 2371                a = Deri0((Double_t) i, working_space[2 * j],
 
 2372                working_space[2 * j + 1],
 
 2373                working_space[peak_vel],
 
 2374                working_space[peak_vel + 1],
 
 2375                working_space[peak_vel + 3],
 
 2376                working_space[peak_vel + 2]);
 
 2378                   working_space[2 * shift + k] += chi_opt;        
 
 2380                   working_space[4 * shift + k] += b;        
 
 2385          if (fFixSigma == 
false) {
 
 2386             a = Dersigma(fNPeaks, (Double_t) i, working_space,
 
 2387             working_space[peak_vel],
 
 2388             working_space[peak_vel + 1],
 
 2389             working_space[peak_vel + 3],
 
 2390            working_space[peak_vel + 2]);
 
 2392                working_space[2 * shift + k] += chi_opt;        
 
 2394                working_space[4 * shift + k] += b;        
 
 2398          if (fFixT == 
false) {
 
 2399             a = Dert(fNPeaks, (Double_t) i, working_space,
 
 2400             working_space[peak_vel],
 
 2401             working_space[peak_vel + 2]);
 
 2403                working_space[2 * shift + k] += chi_opt;        
 
 2405                working_space[4 * shift + k] += b;        
 
 2409          if (fFixB == 
false) {
 
 2410             a = Derb(fNPeaks, (Double_t) i, working_space,
 
 2411             working_space[peak_vel], working_space[peak_vel + 1],
 
 2412             working_space[peak_vel + 2]);
 
 2414                working_space[2 * shift + k] += chi_opt;        
 
 2416                working_space[4 * shift + k] += b;        
 
 2420          if (fFixS == 
false) {
 
 2421             a = Ders(fNPeaks, (Double_t) i, working_space,
 
 2422             working_space[peak_vel]);
 
 2424                working_space[2 * shift + k] += chi_opt;        
 
 2426                working_space[4 * shift + k] += b;        
 
 2430          if (fFixA0 == 
false) {
 
 2433                working_space[2 * shift + k] += chi_opt;        
 
 2435                working_space[4 * shift + k] += b;        
 
 2439          if (fFixA1 == 
false) {
 
 2440             a = Dera1((Double_t) i);
 
 2442                working_space[2 * shift + k] += chi_opt;        
 
 2444                working_space[4 * shift + k] += b;        
 
 2448          if (fFixA2 == 
false) {
 
 2449             a = Dera2((Double_t) i);
 
 2451                working_space[2 * shift + k] += chi_opt;        
 
 2453                working_space[4 * shift + k] += b;        
 
 2459    b = fXmax - fXmin + 1 - rozmer;
 
 2460    chi_er = chi_cel / b;
 
 2461    for (i = 0, j = 0; i < fNPeaks; i++) {
 
 2463           Area(working_space[2 * i], working_space[peak_vel],
 
 2464                working_space[peak_vel + 1], working_space[peak_vel + 2]);
 
 2465       if (fFixAmp[i] == 
false) {
 
 2466          fAmpCalc[i] = working_space[shift + j];        
 
 2467          if (working_space[3 * shift + j] != 0)
 
 2468             fAmpErr[i] = TMath::Sqrt(TMath::Abs(working_space[2 * shift + j])) / TMath::Sqrt(TMath::Abs(working_space[3 * shift + j]));        
 
 2470             a = Derpa(working_space[peak_vel],
 
 2471             working_space[peak_vel + 1],
 
 2472             working_space[peak_vel + 2]);
 
 2473             b = working_space[4 * shift + j];        
 
 2479             fAreaErr[i] = TMath::Sqrt(TMath::Abs(a * a * b * chi_er));
 
 2488          fAmpCalc[i] = fAmpInit[i];
 
 2492       if (fFixPosition[i] == 
false) {
 
 2493          fPositionCalc[i] = working_space[shift + j];        
 
 2494          if (working_space[3 * shift + j] != 0)        
 
 2495             fPositionErr[i] = TMath::Sqrt(TMath::Abs(working_space[2 * shift + j])) / TMath::Sqrt(TMath::Abs(working_space[3 * shift + j]));        
 
 2500          fPositionCalc[i] = fPositionInit[i];
 
 2501          fPositionErr[i] = 0;
 
 2504    if (fFixSigma == 
false) {
 
 2505       fSigmaCalc = working_space[shift + j];        
 
 2506       if (working_space[3 * shift + j] != 0)        
 
 2507          fSigmaErr = TMath::Sqrt(TMath::Abs(working_space[2 * shift + j])) / TMath::Sqrt(TMath::Abs(working_space[3 * shift + j]));        
 
 2512       fSigmaCalc = fSigmaInit;
 
 2515    if (fFixT == 
false) {
 
 2516       fTCalc = working_space[shift + j];        
 
 2517       if (working_space[3 * shift + j] != 0)        
 
 2518          fTErr = TMath::Sqrt(TMath::Abs(working_space[2 * shift + j])) / TMath::Sqrt(TMath::Abs(working_space[3 * shift + j]));        
 
 2526    if (fFixB == 
false) {
 
 2527       fBCalc = working_space[shift + j];        
 
 2528       if (working_space[3 * shift + j] != 0)        
 
 2529          fBErr = TMath::Sqrt(TMath::Abs(working_space[2 * shift + j])) / TMath::Sqrt(TMath::Abs(working_space[3 * shift + j]));        
 
 2537    if (fFixS == 
false) {
 
 2538       fSCalc = working_space[shift + j];        
 
 2539       if (working_space[3 * shift + j] != 0)        
 
 2540          fSErr = TMath::Sqrt(TMath::Abs(working_space[2 * shift + j])) / TMath::Sqrt(TMath::Abs(working_space[3 * shift + j]));        
 
 2548    if (fFixA0 == 
false) {
 
 2549       fA0Calc = working_space[shift + j];        
 
 2550       if (working_space[3 * shift + j] != 0)        
 
 2551          fA0Err = TMath::Sqrt(TMath::Abs(working_space[2 * shift + j])) / TMath::Sqrt(TMath::Abs(working_space[3 * shift + j]));        
 
 2559    if (fFixA1 == 
false) {
 
 2560       fA1Calc = working_space[shift + j];        
 
 2561       if (working_space[3 * shift + j] != 0)        
 
 2562          fA1Err = TMath::Sqrt(TMath::Abs(working_space[2 * shift + j])) / TMath::Sqrt(TMath::Abs(working_space[3 * shift + j]));        
 
 2570    if (fFixA2 == 
false) {
 
 2571       fA2Calc = working_space[shift + j];        
 
 2572       if (working_space[3 * shift + j] != 0)        
 
 2573          fA2Err = TMath::Sqrt(TMath::Abs(working_space[2 * shift + j])) / TMath::Sqrt(TMath::Abs(working_space[3 * shift + j]));        
 
 2581    b = fXmax - fXmin + 1 - rozmer;
 
 2583    for (i = fXmin; i <= fXmax; i++) {
 
 2584       f = Shape(fNPeaks, (Double_t) i, working_space,
 
 2585       working_space[peak_vel], working_space[peak_vel + 1],
 
 2586       working_space[peak_vel + 3], working_space[peak_vel + 2],
 
 2587       working_space[peak_vel + 4], working_space[peak_vel + 5],
 
 2588       working_space[peak_vel + 6]);
 
 2591    for (i = 0; i < rozmer; i++)
 
 2592       delete [] working_matrix[i];
 
 2593    delete [] working_matrix;
 
 2594    delete [] working_space;
 
 2608 void TSpectrumFit::SetFitParameters(Int_t xmin,Int_t xmax, Int_t numberIterations, Double_t alpha, Int_t statisticType, Int_t alphaOptim, Int_t power, Int_t fitTaylor)
 
 2610    if(xmin<0 || xmax <= xmin){
 
 2611       Error(
"SetFitParameters", 
"Wrong range");
 
 2614    if (numberIterations <= 0){
 
 2615       Error(
"SetFitParameters",
"Invalid number of iterations, must be positive");
 
 2618    if (alpha <= 0 || alpha > 1){
 
 2619       Error (
"SetFitParameters",
"Invalid step coefficient alpha, must be > than 0 and <=1");
 
 2622    if (statisticType != kFitOptimChiCounts
 
 2623         && statisticType != kFitOptimChiFuncValues
 
 2624         && statisticType != kFitOptimMaxLikelihood){
 
 2625       Error(
"SetFitParameters",
"Wrong type of statistic");
 
 2628    if (alphaOptim != kFitAlphaHalving
 
 2629         && alphaOptim != kFitAlphaOptimal){
 
 2630       Error(
"SetFitParameters",
"Wrong optimization algorithm");
 
 2633    if (power != kFitPower2 && power != kFitPower4
 
 2634         && power != kFitPower6 && power != kFitPower8
 
 2635         && power != kFitPower10 && power != kFitPower12){
 
 2636       Error(
"SetFitParameters",
"Wrong power");
 
 2639    if (fitTaylor != kFitTaylorOrderFirst
 
 2640         && fitTaylor != kFitTaylorOrderSecond){
 
 2641       Error(
"SetFitParameters",
"Wrong order of Taylor development");
 
 2644    fXmin=xmin,fXmax=xmax,fNumberIterations=numberIterations,fAlpha=alpha,fStatisticType=statisticType,fAlphaOptim=alphaOptim,fPower=power,fFitTaylor=fitTaylor;
 
 2656 void TSpectrumFit::SetPeakParameters(Double_t sigma, Bool_t fixSigma, 
const Double_t *positionInit, 
const Bool_t *fixPosition, 
const Double_t *ampInit, 
const Bool_t *fixAmp)
 
 2660       Error (
"SetPeakParameters",
"Invalid sigma, must be > than 0");
 
 2663    for(i=0; i < fNPeaks; i++){
 
 2664       if((Int_t) positionInit[i] < fXmin || (Int_t) positionInit[i] > fXmax){
 
 2665          Error (
"SetPeakParameters",
"Invalid peak position, must be in the range fXmin, fXmax");
 
 2669          Error (
"SetPeakParameters",
"Invalid peak amplitude, must be > than 0");
 
 2673    fSigmaInit = sigma,fFixSigma = fixSigma;
 
 2674    for(i=0; i < fNPeaks; i++){
 
 2675       fPositionInit[i] = (Double_t) positionInit[i];
 
 2676       fFixPosition[i] = fixPosition[i];
 
 2677       fAmpInit[i] = (Double_t) ampInit[i];
 
 2678       fFixAmp[i] = fixAmp[i];
 
 2691 void TSpectrumFit::SetBackgroundParameters(Double_t a0Init, Bool_t fixA0, Double_t a1Init, Bool_t fixA1, Double_t a2Init, Bool_t fixA2)
 
 2710 void TSpectrumFit::SetTailParameters(Double_t tInit, Bool_t fixT, Double_t bInit, Bool_t fixB, Double_t sInit, Bool_t fixS)
 
 2725 void TSpectrumFit::GetSigma(Double_t &sigma, Double_t &sigmaErr)
 
 2740 void TSpectrumFit::GetBackgroundParameters(Double_t &a0, Double_t &a0Err, Double_t &a1, Double_t &a1Err, Double_t &a2, Double_t &a2Err)
 
 2760 void TSpectrumFit::GetTailParameters(Double_t &t, Double_t &tErr, Double_t &b, Double_t &bErr, Double_t &s, Double_t &sErr)