CXFit.cpp 17.6 KB
Newer Older
Jérémie Dudouet's avatar
Jérémie Dudouet committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include "CXFit.h"

#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>

#include "TMath.h"
#include "TF1.h"
#include "TH1.h"
#include "TFitResultPtr.h"
#include "TFitResult.h"
#include "TPad.h"
#include "TGNumberEntry.h"
#include "TGButton.h"
16
#include "Fit/Fitter.h"
Jérémie Dudouet's avatar
Jérémie Dudouet committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

#include "CXHist1DPlayer.h"
#include "CXArrow.h"
#include "CXMainWindow.h"

using namespace std;

CXFit::CXFit(TH1 *hist, TVirtualPad *pad, CXHist1DPlayer *player) : TObject()
{
    fHistogram = hist;
    fPad = pad;
    fPlayer = player;

    fListOfArrows = new TList;
    fListOfArrows->SetOwner();

    fListOfPeaks = new TList;
    fListOfPeaks->SetOwner();
}

CXFit::~CXFit()
{
    Clear(fPad);

Jérémie Dudouet's avatar
Jérémie Dudouet committed
41
    delete fListOfArrows;
Jérémie Dudouet's avatar
Jérémie Dudouet committed
42

Jérémie Dudouet's avatar
Jérémie Dudouet committed
43 44 45 46
    delete fFitFunction;
    delete fBackFunction;
    delete fResidue;
    delete fListOfPeaks;
Jérémie Dudouet's avatar
Jérémie Dudouet committed
47 48 49 50 51 52 53

    fPlayer->GetMainWindow()->RefreshPads();
}

void CXFit::AddArrow(Double_t Energy)
{
    Int_t Bin = fHistogram->FindBin(Energy);
54
    Double_t Value = fHistogram->GetBinContent(Bin);
Jérémie Dudouet's avatar
Jérémie Dudouet committed
55 56 57 58 59 60 61 62 63

    for(int i=fHistogram->FindBin(Energy)-2 ; i<=fHistogram->FindBin(Energy)+2 ; i++){
        if(i>0 && fHistogram->GetBinContent(i)>Value){
            Value = fHistogram->GetBinContent(i);
            Bin = i;
        }
    }

    Energy = fHistogram->GetBinCenter(Bin);
64
    Double_t MaxGlob = fHistogram->GetMaximum();
Jérémie Dudouet's avatar
Jérémie Dudouet committed
65
    auto *arrow = new CXArrow(this,Energy,(Value + MaxGlob/100.) ,(Value +MaxGlob/15.),0.01,"<|");
Jérémie Dudouet's avatar
Jérémie Dudouet committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    arrow->SetAngle(30);
    arrow->SetLineWidth(2);
    arrow->Draw();

    fListOfArrows->Add(arrow);
    Update();
}

void CXFit::RemoveArrow(CXArrow *arrow)
{
    if(arrow == nullptr) {
        fListOfArrows->RemoveLast();
        fPad->GetListOfPrimitives()->RemoveLast();
    }
    else {
        fListOfArrows->Remove(arrow);
        fPad->GetListOfPrimitives()->Remove(arrow);
    }

    Update();
}

void CXFit::Update()
{
    fEnergies.clear();
    fBackgd.clear();

    fListOfArrows->Sort();
    TList back,Ener;

Jérémie Dudouet's avatar
Jérémie Dudouet committed
96
    if(!fPlayer->DoNewFit && fListOfArrows->GetEntries()<3) {
Jérémie Dudouet's avatar
Jérémie Dudouet committed
97 98 99 100 101 102
        Clear(fPad);
        WARN_MESS<<"At least one peak and two background are needed for a fit --> command ignored"<<ENDL;
        return;
    }

    for(int i=0 ; i<fListOfArrows->GetEntries() ; i++) {
Jérémie Dudouet's avatar
Jérémie Dudouet committed
103
        auto *arr = dynamic_cast<CXArrow*>(fListOfArrows->At(i));
Jérémie Dudouet's avatar
Jérémie Dudouet committed
104 105 106 107 108
        if(i==0) back.Add(arr);
        else if(i==fListOfArrows->GetEntries()-1) back.Add(arr);
        else Ener.Add(arr);
    }

J. Dudouet's avatar
J. Dudouet committed
109
    for(int i=0 ; i<back.GetEntries() ; i++) {
Jérémie Dudouet's avatar
Jérémie Dudouet committed
110
        auto *arr = dynamic_cast<CXArrow*>(back.At(i));
Jérémie Dudouet's avatar
Jérémie Dudouet committed
111
        Double_t E = arr->GetEnergy();
112 113
        Double_t MaxGlob = fHistogram->GetMaximum();
        Double_t Value = fHistogram->GetBinContent(fHistogram->FindBin(E));
Jérémie Dudouet's avatar
Jérémie Dudouet committed
114 115 116 117 118
        arr->Set(E,(Value + MaxGlob/100.), (Value+MaxGlob/15.));
        arr->SetLineColor(kBlue);
        arr->SetFillColor(kBlue);
        fBackgd.push_back(E);
    }
J. Dudouet's avatar
J. Dudouet committed
119
    for(int i=0 ; i<Ener.GetEntries() ; i++) {
Jérémie Dudouet's avatar
Jérémie Dudouet committed
120
        auto *arr = dynamic_cast<CXArrow*>(Ener.At(i));
Jérémie Dudouet's avatar
Jérémie Dudouet committed
121
        Double_t E = arr->GetEnergy();
122 123
        Double_t MaxGlob = fHistogram->GetMaximum();
        Double_t Value = fHistogram->GetBinContent(fHistogram->FindBin(E));
Jérémie Dudouet's avatar
Jérémie Dudouet committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
        arr->Set(E,(Value + MaxGlob/100.), (Value+MaxGlob/15.));
        arr->SetLineColor(kRed);
        arr->SetFillColor(kRed);
        fEnergies.push_back(E);
    }

    fPlayer->GetMainWindow()->RefreshPads();
}

void CXFit::Clear(TVirtualPad *pad)
{
    fPlayer->EndFit();

    if(pad == nullptr) pad = fPad;

    TList *list = pad->GetListOfPrimitives();

    list->Remove(fFitFunction);
    list->Remove(fBackFunction);
    list->Remove(fResidue);

Jérémie Dudouet's avatar
Jérémie Dudouet committed
145

Jérémie Dudouet's avatar
Jérémie Dudouet committed
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
    for(int i=0 ; i<fListOfPeaks->GetEntries() ; i++)
        list->Remove(fListOfPeaks->At(i));

    for(int i=0 ; i<fListOfArrows->GetEntries() ; i++)
        list->Remove(fListOfArrows->At(i));

    fPlayer->RemoveFit(this);

    fPlayer->GetMainWindow()->RefreshPads();
}

void CXFit::Fit()
{
    if(fListOfArrows->GetEntries()<3) {
        return;
    }

    if(fPad==nullptr) {
        WARN_MESS<<"No selected pad, ignored"<<ENDL;
        return;
    }
    fPad->cd();

    if(fPlayer == nullptr) {
        WARN_MESS<<"1DPlayer not defined, ignored"<<ENDL;
        return;
    }

    if(fHistogram==nullptr || fHistogram->InheritsFrom("TH2")) {
        WARN_MESS<<"No 1D histogram found, ignored"<<ENDL;
        return;
    }

179 180 181 182
    ROOT::Math::MinimizerOptions::SetDefaultMinimizer(fPlayer->GetMinimizer(),fPlayer->GetAlgorithm());
    ROOT::Math::MinimizerOptions::SetDefaultTolerance(fPlayer->GetTolerance());
    ROOT::Math::MinimizerOptions::SetDefaultPrintLevel(fPlayer->GetPrintLevel());

Jérémie Dudouet's avatar
Jérémie Dudouet committed
183 184
//    fPlayer->GetFitResultsBox()->RemoveAll();

185 186 187
    Double_t DefFWHM = fPlayer->fNE_FWHM[0]->GetNumber();
    Double_t DefFWHM_min = fPlayer->fNE_FWHM[1]->GetNumber();
    Double_t DefFWHM_max = fPlayer->fNE_FWHM[2]->GetNumber();
Jérémie Dudouet's avatar
Jérémie Dudouet committed
188

189 190 191
    Double_t LeftTailVal = fPlayer->fNE_LT[0]->GetNumber();;
    Double_t LeftTailValMin = fPlayer->fNE_LT[1]->GetNumber();;
    Double_t LeftTailValMax = fPlayer->fNE_LT[2]->GetNumber();;
Jérémie Dudouet's avatar
Jérémie Dudouet committed
192

193 194 195
    Double_t RightTailVal = fPlayer->fNE_RT[0]->GetNumber();;
    Double_t RightTailValMin = fPlayer->fNE_RT[1]->GetNumber();;
    Double_t RightTailValMax = fPlayer->fNE_RT[2]->GetNumber();;
Jérémie Dudouet's avatar
Jérémie Dudouet committed
196

197 198 199
    Double_t StepVal = 0.01;
    Double_t StepValMin = -1.;
    Double_t StepValMax = 1.;
Jérémie Dudouet's avatar
Jérémie Dudouet committed
200 201 202

    Int_t NPars = 3+6*fEnergies.size();

Jérémie Dudouet's avatar
Jérémie Dudouet committed
203
    delete fFitFunction;
Jérémie Dudouet's avatar
Jérémie Dudouet committed
204 205 206 207 208 209
    fFitFunction = new TF1("MyFit", this, &CXFit::DoubleTailedStepedGaussian, fBackgd[0], fBackgd[1], NPars, "CXFit", "DoubleTailedStepedGaussian");

    fFitFunction->SetParName(0, "NumberOfPeaks");
    fFitFunction->SetParName(1, "BkgConst");
    fFitFunction->SetParName(2, "BkgSlope");

Jérémie Dudouet's avatar
Jérémie Dudouet committed
210
    for(auto i=0U ; i<fEnergies.size() ; i++) {
Jérémie Dudouet's avatar
Jérémie Dudouet committed
211 212 213 214 215 216 217 218 219 220 221 222 223
        fFitFunction->SetParName(3+i*6+0, Form("Height_%d",i));
        fFitFunction->SetParName(3+i*6+1, Form("Position_%d",i));
        fFitFunction->SetParName(3+i*6+2, Form("FWHM_%d",i));
        fFitFunction->SetParName(3+i*6+3, Form("LeftTail_%d",i));
        fFitFunction->SetParName(3+i*6+4, Form("RightTail_%d",i));
        fFitFunction->SetParName(3+i*6+5, Form("AmplitudeStep_%d",i));
    }

    fFitFunction->SetNpx(1000);
    fFitFunction->SetLineColor(kRed);

    fFitFunction->FixParameter(0, fEnergies.size()); // 1 peak

224
    Double_t x,y;
Jérémie Dudouet's avatar
Jérémie Dudouet committed
225 226 227 228 229 230
    x = fPad->GetUxmin();
    y = fPad->GetUxmax();
    fHistogram->GetXaxis()->SetRangeUser(fBackgd[0],fBackgd[1]);

    //Calc Bckd
    fFitFunction->SetParameter(1, fHistogram->GetBinContent(fHistogram->FindBin(fBackgd[0])));
231
    fFitFunction->SetParLimits(1, 0.,fHistogram->GetMaximum());
Jérémie Dudouet's avatar
Jérémie Dudouet committed
232 233

    fFitFunction->SetParameter(2, 0);
234
    fFitFunction->SetParLimits(2, -50., 0.);
Jérémie Dudouet's avatar
Jérémie Dudouet committed
235

Jérémie Dudouet's avatar
Jérémie Dudouet committed
236
    if(!fPlayer->fUsePol1)
Jérémie Dudouet's avatar
Jérémie Dudouet committed
237 238
        fFitFunction->FixParameter(2,0);

Jérémie Dudouet's avatar
Jérémie Dudouet committed
239
    for(auto i=0U ; i<fEnergies.size() ; i++) {
Jérémie Dudouet's avatar
Jérémie Dudouet committed
240 241
        //Height
        fFitFunction->SetParameter(3+i*6+0, fHistogram->GetBinContent(fHistogram->FindBin(fEnergies[i])) - (fHistogram->GetBinContent(fHistogram->FindBin(fBackgd[0]))+fHistogram->GetBinContent(fHistogram->FindBin(fBackgd[1])))*0.5 );
242
        fFitFunction->SetParLimits(3+i*6+0, fHistogram->GetBinContent((fHistogram->FindBin(fEnergies[i]))-(fHistogram->GetBinContent(fHistogram->FindBin(fBackgd[0]))+fHistogram->GetBinContent(fHistogram->FindBin(fBackgd[1])))*0.5)*0.5, fHistogram->GetBinContent(fHistogram->GetMaximumBin())*1.5);
Jérémie Dudouet's avatar
Jérémie Dudouet committed
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
        //Position
        fFitFunction->SetParameter(3+i*6+1, fEnergies[i]);
        fFitFunction->SetParLimits(3+i*6+1, fEnergies[i]-DefFWHM, fEnergies[i]+DefFWHM);
        if(fPlayer->fFixMean->GetState() == kButtonDown)
            fFitFunction->FixParameter(3+i*6+1,fEnergies[i]);
        //FWHM
        fFitFunction->SetParameter(3+i*6+2, DefFWHM);
        fFitFunction->SetParLimits(3+i*6+2, DefFWHM_min, DefFWHM_max);
        if(fPlayer->fFixFWHM->GetState() == kButtonDown)
            fFitFunction->FixParameter(3+i*6+2,DefFWHM);
        //LeftTail
        fFitFunction->SetParameter(3+i*6+3, LeftTailVal);
        fFitFunction->SetParLimits(3+i*6+3, LeftTailValMin, LeftTailValMax);
        if(fPlayer->fUseLT->GetState() == kButtonUp)
            fFitFunction->FixParameter(3+i*6+3,-5);
        else if(fPlayer->fFixLT->GetState() == kButtonDown)
            fFitFunction->FixParameter(3+i*6+3,LeftTailVal);
        //RightTail
        fFitFunction->SetParameter(3+i*6+4, RightTailVal);
        fFitFunction->SetParLimits(3+i*6+4, RightTailValMin, RightTailValMax);
        if(fPlayer->fUseRT->GetState() == kButtonUp)
            fFitFunction->FixParameter(3+i*6+4,5);
        else if(fPlayer->fFixRT->GetState() == kButtonDown)
            fFitFunction->FixParameter(3+i*6+4,RightTailVal);
        //AmplitudeStep
        fFitFunction->SetParameter(3+i*6+5, StepVal);
        fFitFunction->SetParLimits(3+i*6+5, StepValMin, StepValMax);
Jérémie Dudouet's avatar
Jérémie Dudouet committed
270
        if(!fPlayer->fUseStep)
Jérémie Dudouet's avatar
Jérémie Dudouet committed
271 272 273 274 275
            fFitFunction->FixParameter(3+i*6+5,0);
    }

    fHistogram->GetXaxis()->SetRangeUser(x,y);

276 277 278
    TString FitOpt = "R0S";
    if(fPlayer->GetPrintLevel()>0) FitOpt +="V";
    TFitResultPtr r = fHistogram->Fit(fFitFunction,FitOpt.Data());
Jérémie Dudouet's avatar
Jérémie Dudouet committed
279 280 281 282

    if(fPlayer->fFixAmpli->GetState() == kButtonDown) {

        //Extract Background
Jérémie Dudouet's avatar
Jérémie Dudouet committed
283
        delete fBackFunction;
Jérémie Dudouet's avatar
Jérémie Dudouet committed
284 285 286
        fBackFunction = new TF1("Background", this, &CXFit::StepedBackground, fBackgd[0], fBackgd[1], NPars, "CXFit", "StepedBackground");
        fBackFunction->SetParameters(fFitFunction->GetParameters());

Jérémie Dudouet's avatar
Jérémie Dudouet committed
287
        for(auto i=0U ; i<fEnergies.size() ; i++) {
288
            Double_t Ampli = fHistogram->GetBinContent(fHistogram->FindBin(fEnergies[i]))- fBackFunction->Eval(fEnergies[i]);
Jérémie Dudouet's avatar
Jérémie Dudouet committed
289 290 291
            fFitFunction->FixParameter(3+i*6+0,Ampli);
        }

292
        r = fHistogram->Fit(fFitFunction,FitOpt.Data());
Jérémie Dudouet's avatar
Jérémie Dudouet committed
293 294 295
    }

    //Extract Background
Jérémie Dudouet's avatar
Jérémie Dudouet committed
296
    delete fBackFunction;
Jérémie Dudouet's avatar
Jérémie Dudouet committed
297 298 299 300 301 302 303 304
    fBackFunction = new TF1("Background", this, &CXFit::StepedBackground, fBackgd[0], fBackgd[1], NPars, "CXFit", "StepedBackground");
    fBackFunction->SetParameters(fFitFunction->GetParameters());
    fBackFunction->SetNpx(1000);

    fBackFunction->SetLineColor(kBlue);
    fBackFunction->Draw("same");

    //Extract Residue
Jérémie Dudouet's avatar
Jérémie Dudouet committed
305
    delete fResidue;
Jérémie Dudouet's avatar
Jérémie Dudouet committed
306 307 308 309 310 311 312 313 314 315 316 317
    fResidue = new TF1("Residue", this, &CXFit::Residue, fBackgd[0], fBackgd[1], NPars, "CXFit", "Residue");
    fResidue->SetParameters(fFitFunction->GetParameters());
    fResidue->SetNpx(1000);

    fResidue->SetLineWidth(1);
    fResidue->SetLineColor(kBlack);
    fResidue->Draw("same");

    ostringstream text;

    text << "Fit results :";
    cout<<text.str()<<endl;fPlayer->PrintInListBox(text.str(),kPrint);text.str("");
318 319 320 321 322 323 324 325 326 327 328 329
    text << "Status: ";
    if(r->Status()==0)
        text << " Successeful" << endl;
    else
        text << " Failed" << endl;
    cout<<text.str();
    if(r->Status()==0)
        fPlayer->PrintInListBox(text.str(),kPrint);
    else
        fPlayer->PrintInListBox(text.str(),kError);
    text.str("");

Jérémie Dudouet's avatar
Jérémie Dudouet committed
330 331 332 333 334 335 336 337 338
    text << "Chi2  = "<< r->Chi2();
    cout<<text.str()<<endl;fPlayer->PrintInListBox(text.str(),kInfo);text.str("");
    text << "Ndf   = "<< r->Ndf();
    cout<<text.str()<<endl;fPlayer->PrintInListBox(text.str(),kInfo);text.str("");
    text << "P val = "<< r->Prob();
    cout<<text.str()<<endl;fPlayer->PrintInListBox(text.str(),kInfo);text.str("");

    fListOfPeaks->Clear();

Jérémie Dudouet's avatar
Jérémie Dudouet committed
339
    for(auto i=0U ; i< fEnergies.size() ; i++) {
Jérémie Dudouet's avatar
Jérémie Dudouet committed
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
        text<<"Peak "<<i<<":";
        cout<<text.str()<<endl;fPlayer->PrintInListBox(text.str(),kPrint);text.str("");

        TF1 *peak = new TF1(Form("Peak%d",i), this, &CXFit::PeakFunction, fBackgd[0], fBackgd[1], NPars, "CXFit", "PeakFunction");
        peak->SetParameters(fFitFunction->GetParameters());
        peak->SetParErrors(fFitFunction->GetParErrors());
        peak->SetParameter(1,1);//with backgroud
        peak->SetParameter(0,i);
        peak->SetNpx(1000);

        peak->SetLineColor(kGreen);
        peak->SetLineStyle(kDashed);
        peak->Draw("same");

        fListOfPeaks->Add(peak);

356 357 358 359 360 361 362 363 364 365
        Double_t Area     = (peak->Integral(fBackgd[0],fBackgd[1],1e-6)-fBackFunction->Integral(fBackgd[0],fBackgd[1],1e-6))/fHistogram->GetBinWidth(1);
        Double_t AreaErr  = 2*sqrt(Area);
        Double_t Mean     = peak->GetParameter(3+i*6+1);
        Double_t MeanErr  = peak->GetParError(3+i*6+1);
        Double_t FWHM     = peak->GetParameter(3+i*6+2);
        Double_t FWHMErr  = peak->GetParError(3+i*6+2);
        Double_t LeftT    = TMath::Abs(peak->GetParameter(3+i*6+3));
        Double_t LeftTErr = peak->GetParError(3+i*6+3);
        Double_t Right    = peak->GetParameter(3+i*6+4);
        Double_t RightErr = peak->GetParError(3+i*6+4);
Jérémie Dudouet's avatar
Jérémie Dudouet committed
366 367

        peak->SetParameter(1,0);//without backgroud
368 369
        Double_t Max      = peak->GetParameter(3+i*6+0);
        Double_t MaxErr   = peak->GetParError(3+i*6+0);
Jérémie Dudouet's avatar
Jérémie Dudouet committed
370

371 372
        Double_t FWHM_L     = peak->GetX(Max/2,fBackgd[0],Mean,1e-6);
        Double_t FWHM_L_err = peak->GetX((Max-MaxErr)/2,fBackgd[0],Mean,1e-6);
Jérémie Dudouet's avatar
Jérémie Dudouet committed
373

374 375
        Double_t FWHM_R     = peak->GetX(Max/2,Mean,fBackgd[1],1e-6);
        Double_t FWHM_R_err = peak->GetX((Max-MaxErr)/2,Mean, fBackgd[1],1e-6);
Jérémie Dudouet's avatar
Jérémie Dudouet committed
376

377 378
        Double_t FWHM_Real     = FWHM_R-FWHM_L;
        Double_t FWHM_Real_err = (FWHM_R_err-FWHM_L_err)-FWHM_Real;
Jérémie Dudouet's avatar
Jérémie Dudouet committed
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
        peak->SetParameter(1,1);//with backgroud

        text<<left<<setw(11)<<"Mean"<<": "<<setprecision(7)<<setw(10)<<Mean<<" ("<<setprecision(7)<<setw(10)<<MeanErr<<")";
        cout<<text.str()<<endl;
        fPlayer->PrintInListBox(text.str(),kInfo);
        text.str("");

        text<<left<<setw(11)<<"Amplitude"<<": "<<setprecision(7)<<setw(10)<<Max<<" ("<<setprecision(7)<<setw(10)<<MaxErr<<")";
        cout<<text.str()<<endl;
        fPlayer->PrintInListBox(text.str(),kInfo);
        text.str("");

        text<<left<<setw(11)<<"FWHM (gaus)"<<": "<<setprecision(7)<<setw(10)<<FWHM<<" ("<<setprecision(7)<<setw(10)<<FWHMErr<<")";
        cout<<text.str()<<endl;
        fPlayer->PrintInListBox(text.str(),kInfo);
        text.str("");

        text<<left<<setw(11)<<"FWHM (real)"<<": "<<setprecision(7)<<setw(10)<<FWHM_Real<<" ("<<setprecision(7)<<setw(10)<<FWHM_Real_err<<")";
        cout<<text.str()<<endl;
        fPlayer->PrintInListBox(text.str(),kInfo);
        text.str("");

        text<<left<<setw(11)<<"L Tail"<<": "<<setprecision(7)<<setw(10)<<LeftT<<" ("<<setprecision(7)<<setw(10)<<LeftTErr<<")";
        cout<<text.str()<<endl;
        fPlayer->PrintInListBox(text.str(),kInfo);
        text.str("");

        text<<left<<setw(11)<<"R Tail"<<": "<<setprecision(7)<<setw(10)<<Right<<" ("<<setprecision(7)<<setw(10)<<RightErr<<")";
        cout<<text.str()<<endl;
        fPlayer->PrintInListBox(text.str(),kInfo);
        text.str("");

        text<<left<<setw(11)<<"Area"<<": "<<setprecision(7)<<setw(10)<<Area<<" ("<<setprecision(7)<<setw(10)<<AreaErr<<")";
        cout<<text.str()<<endl;
        fPlayer->PrintInListBox(text.str(),kInfo);
        text.str("");
    }

    fFitFunction->Draw("same");

    fPlayer->GetMainWindow()->RefreshPads();
}

422
Double_t CXFit::DoubleTailedStepedGaussian(Double_t*xx,Double_t*pp)
Jérémie Dudouet's avatar
Jérémie Dudouet committed
423
{
424
    Double_t x   = xx[0];
Jérémie Dudouet's avatar
Jérémie Dudouet committed
425 426 427

    int    NSubPeaks = (int)pp[0]; //Number of subpeaks in the peak range

428
    Double_t f_tot = 0.;
Jérémie Dudouet's avatar
Jérémie Dudouet committed
429 430 431

    Int_t Npar = 6;

432 433
    Double_t Back_const = pp[1];
    Double_t Back_slope = pp[2];
Jérémie Dudouet's avatar
Jérémie Dudouet committed
434 435 436 437

    f_tot += Back_const + x*Back_slope;

    for(int i=0 ; i<NSubPeaks ; i++) {
438 439 440 441 442 443 444 445 446 447 448 449
        Double_t Ampli     = pp[3+i*Npar+0];
        Double_t Mean      = pp[3+i*Npar+1];
        Double_t Sigma     = pp[3+i*Npar+2]*1./sqrt(8.*log(2.));
        Double_t Lambda    = pp[3+i*Npar+3];
        Double_t Rho       = pp[3+i*Npar+4];
        Double_t S         = pp[3+i*Npar+5];

        Double_t U         = (x-Mean)/Sigma;
        Double_t f_g       = Ampli*TMath::Exp(-U*U*0.5);
        Double_t f_lambda  = Ampli*TMath::Exp(-0.5*Lambda*(2.*U-Lambda));
        Double_t f_rho     = Ampli*TMath::Exp(-0.5*Rho*(2.*U-Rho));
        Double_t f_S       = Ampli*S*1./((1+TMath::Exp(U))*(1+TMath::Exp(U)));
Jérémie Dudouet's avatar
Jérémie Dudouet committed
450 451 452 453 454 455 456 457 458 459 460

        if(U<Lambda) f_tot += f_lambda;
        else if(U>Rho) f_tot += f_rho;
        else f_tot += f_g;

        f_tot += f_S;
    }

    return f_tot;
}

461
Double_t CXFit::StepedBackground(Double_t*xx,Double_t*pp)
Jérémie Dudouet's avatar
Jérémie Dudouet committed
462
{
463
    Double_t x   = xx[0];
Jérémie Dudouet's avatar
Jérémie Dudouet committed
464

465
    Double_t f_tot = 0.;
Jérémie Dudouet's avatar
Jérémie Dudouet committed
466 467 468

    int    NSubPeaks = (int)pp[0]; //Number of subpeaks in the peak range

469 470
    Double_t Back_const = pp[1];
    Double_t Back_slope = pp[2];
Jérémie Dudouet's avatar
Jérémie Dudouet committed
471 472 473 474 475 476

    Int_t Npar = 6;

    f_tot += Back_const + x*Back_slope;

    for(int i=0 ; i<NSubPeaks ; i++) {
477 478 479 480
        Double_t Ampli = pp[3+i*Npar+0];
        Double_t Mean  = pp[3+i*Npar+1];
        Double_t Sigma = pp[3+i*Npar+2]*1./sqrt(8.*log(2.));
        Double_t S     = pp[3+i*Npar+5];
Jérémie Dudouet's avatar
Jérémie Dudouet committed
481 482 483 484 485 486 487 488

        f_tot += Ampli*S*1./((1+TMath::Exp((x-Mean)/Sigma))*(1+TMath::Exp((x-Mean)/Sigma)));
    }

    return f_tot;
}


489
double CXFit::PeakFunction(Double_t*xx,Double_t*pp)
Jérémie Dudouet's avatar
Jérémie Dudouet committed
490
{
491
    Double_t x   = xx[0];
Jérémie Dudouet's avatar
Jérémie Dudouet committed
492 493 494

    int    NSubPeaks = (int)pp[0]; //Number of subpeaks in the peak range

495
    Double_t f_tot = 0.;
Jérémie Dudouet's avatar
Jérémie Dudouet committed
496 497 498 499 500 501 502

    Int_t Npar = 6;

    Bool_t WithBackground = true;
    if(pp[1]==0)
        WithBackground = false;

503
    Double_t BackGround = fBackFunction->Eval(x);
Jérémie Dudouet's avatar
Jérémie Dudouet committed
504 505 506 507

    if(WithBackground)
        f_tot += BackGround;

508 509 510 511 512 513
    Double_t Ampli     = pp[3+NSubPeaks*Npar+0];
    Double_t Mean      = pp[3+NSubPeaks*Npar+1];
    Double_t Sigma     = pp[3+NSubPeaks*Npar+2]*1./sqrt(8.*log(2.));
    Double_t Lambda    = pp[3+NSubPeaks*Npar+3];
    Double_t Rho       = pp[3+NSubPeaks*Npar+4];
    Double_t S         = 0;
Jérémie Dudouet's avatar
Jérémie Dudouet committed
514

515 516 517 518 519
    Double_t U         = (x-Mean)/Sigma;
    Double_t f_g       = Ampli*TMath::Exp(-U*U*0.5);
    Double_t f_lambda  = Ampli*TMath::Exp(-0.5*Lambda*(2.*U-Lambda));
    Double_t f_rho     = Ampli*TMath::Exp(-0.5*Rho*(2.*U-Rho));
    Double_t f_S       = Ampli*S*1./((1+TMath::Exp(U))*(1+TMath::Exp(U)));
Jérémie Dudouet's avatar
Jérémie Dudouet committed
520 521 522 523 524 525 526 527 528 529

    if(U<Lambda) f_tot += f_lambda;
    else if(U>Rho) f_tot += f_rho;
    else f_tot += f_g;

    f_tot += f_S;

    return f_tot;
}

530
Double_t CXFit::Residue(Double_t*xx,Double_t*pp)
Jérémie Dudouet's avatar
Jérémie Dudouet committed
531
{
532
    Double_t x   = xx[0];
Jérémie Dudouet's avatar
Jérémie Dudouet committed
533 534 535

    return fFitFunction->Eval(fHistogram->GetBinCenter(fHistogram->FindBin(x))) - fHistogram->GetBinContent(fHistogram->FindBin(x));
}