Exclude points from fit (not range!)

Hi

I’m taking the Gaussian sigmas of the same energy peaks under different conditions and fitting these to a pol2.

Sometimes low statistics cause the Gaussian to fit poorly, giving a sigma which is unrealistic (I will loop over different bin offsets and use the one closest to the median perhaps).

I looked, but could not find a way to exclude points from the TF1 based on the y-values (I saw fitExclude.C , however I need a single fit to the data, not two fits to different sections, as if the bad point did not exist).

I include a working example, to run just

g++ -o ded ded.cpp root-config --cflags --glibs && ./ded

Cheers!
example.tar.bz2 (1.07 MB)

  1. fix … TF1 *gas[2];

  2. you use the built-in “gaus” and “pol2” functions so, I don’t think you can use the TF1::RejectPoint mechanism -> you would need to implement your own versions of these functions and inside of them add the possibility to “reject points”

  3. if you want to reject points according to the fitted histogram’s bin content, here’s a “trial”: [code]#include “TH1.h”
    #include “TF1.h”
    #include “TAxis.h”
    #include “TLine.h”
    #include “TPad.h”

Bool_t reject_flag = kFALSE;
TH1F *reject_h = 0;
Double_t reject_below = 0;
Double_t reject_above = 0;

Double_t MyPol1(Double_t *x, Double_t *par)
{
Double_t v = 0;

if ( reject_flag &&
reject_h &&
(reject_below < reject_above) &&
( (reject_h->GetBinContent(reject_h->FindBin(x[0])) < reject_below) ||
(reject_h->GetBinContent(reject_h->FindBin(x[0])) > reject_above) ) ) {
TF1::RejectPoint();
// v = 0;
} else {
v = par[0] + par[1]*x[0];
}

return v;
}

void trial(void) {
// create the source function and histogram
TF1 *f1 = new TF1(“f1”, “[0] + [1] * x”, 0, 5);
f1->SetParameters(6, -1, 5, 3, 0.2);
TH1F *h = new TH1F(“h”,“reject trial”, 100, 0, 5);
h->FillRandom(“f1”, 2000);

// create the fitting function
TF1 *f = new TF1(“f”, MyPol1, 0, 5, 2);
f->SetParameters(2, -1);

// fit the histogram (everything will automatically be drawn, too)
reject_flag = kTRUE;
reject_h = h; // we will “reject” points from the “reject_h” histogram
reject_below = 10; // we will “reject” points which are below "reject_below"
reject_above = 25; // we will “reject” points which are above "reject_above"
h->Fit(f, “”);
reject_flag = kFALSE;

// draw the “reject_below” and “reject_above” lines
if ( reject_h && (reject_below < reject_above) ) {
Double_t Xmin = reject_h->GetXaxis()->GetXmin();
Double_t Xmax = reject_h->GetXaxis()->GetXmax();
(new TLine(Xmin, reject_below, Xmax, reject_below))->Draw();
(new TLine(Xmin, reject_above, Xmax, reject_above))->Draw();
}

// make sure we see everything
gPad->Modified(); gPad->Update();
}[/code]