I am trying to fit a histogram in a specific range, while excluding some entries in there. The procedure seems pretty forward and the fitting parameters seem reasonable. What is bizarre however is the drawing of the “exclusion fitting function” : There is a sinking, right at the coordinate where I define the exclusion range, as shown in the image
My code is the following
[code] #include “Riostream.h”
Bool_t reject;
Double_t exclude(Double_t *x, Double_t *par)
{
if (reject && x[0] > 420 && x[0] < 460) {
TF1::RejectPoint();
return 0;
}
//return par[0] + par[1]*x[0];
return par[0] + par[1]*x[0] + par[2]*x[0]*x[0];
}
void testFitExclude(){//(char * file_c) {
gROOT->SetStyle("Plain");
gStyle->SetOptStat(0000);
gStyle->SetOptFit(1111);
gStyle->SetOptTitle(0);
TCanvas *c = new TCanvas("c", "c");
c->SetFillColor(5);
c->SetFrameFillColor(10);
TString dir = gSystem->UnixPathName(gInterpreter->GetCurrentMacroName());
dir.ReplaceAll("testFitExclude.C","");
dir.ReplaceAll("/./","/");
ifstream in;
in.open(Form("%sB_p_1500_120.dat",dir.Data()));//
Float_t x,y;//,z,w,k,l,m;
Int_t nlines = 0;
TH1F *h1 = new TH1F("h1","Experimental",1024,1,1024);
while (1) {
in >> x >> y;
if (!in.good()) break;
if (nlines < 5) printf("x=%8f, y=%8f\n");
h1->Fill(x,y);
nlines++;
}
printf("Found %d points\n",nlines);
in.close();
h1->SetLineColor(kBlack);
h1->SetLineWidth(1);
h1->Draw();
h1->GetXaxis()->SetTitle("Channels");
h1->GetYaxis()->SetTitle("Events");
reject = kTRUE;
TF1 *fit = new TF1("fit",exclude,0,1024,3);//<--------------------------
fit->SetParameters(2,-1);
h1->Fit("fit","W",NULL,400,850);
reject = kFALSE;
//TF1 *fit = new TF1("fit","pol2",0,1024);
// h1->Fit(“fit”,“W”,NULL,465,850);//465 850
h1->GetFunction(“fit”)->SetLineColor(kRed);
TF1 *extraPol = new TF1("extraPol",exclude,0,1024,3);
//TF1 *extraPol = new TF1("extraPol","pol2",0,1024);
extraPol->SetParameters(fit->GetParameters());
extraPol->Draw("same");
TH1F *hnew = (TH1F*)h1->Clone("hnew");
hnew->Add(extraPol, -1);
hnew->Draw("same");
hnew->SetLineColor(kMagenta);
TString histfilename150 = "testHisto.dat";//TString::Format("%s_Projected_150.dat",file.Data());
SingleExportAscii(hnew,histfilename150);
//TLegend *leg = c->BuildLegend();
//print(fline->GetParameters());
}
/**
- \brief Export Single Histogram into ASCII file
/
Bool_t SingleExportAscii(TH1 hist, TString &filename, TString folder="", TString separator="\t")
{
Int_t i,j;
Double_t xcenter, xwidth;
Bool_t success=kFALSE;
//filename = folder + hist->GetName() + “.dat”;
ofstream file_out(filename);
//file_out << "# Output " << hist->ClassName() << “: " << hist->GetName() << " (” << hist->GetTitle() << “)\n”;
if (hist->GetDimension()==1)
{
//file_out << “# BinCenter” << separator << “Content\n”;
for (i=1; i<=hist->GetNbinsX(); i++)
file_out << int(hist->GetBinCenter(i)) << separator << hist->GetBinContent(i) << endl;
if (i>1)
success=kTRUE;
}
else if (hist->GetDimension()==2)
{
file_out << “# xBinCenter” << separator << “yBinCenter” << separator << “Content” << separator << “xBinHalfWidth” << separator << “yBinHalfWidth” << separator << “Error” << endl;
for (i=1; i <= hist->GetNbinsX(); i++)
{
xcenter = hist->GetXaxis()->GetBinCenter(i);
xwidth = hist->GetXaxis()->GetBinWidth(i)/2;
for (j=1; j <= hist->GetNbinsY(); j++)
file_out << xcenter << separator << hist->GetYaxis()->GetBinCenter(j) << separator << hist->GetBinContent(i,j) << separator << xwidth << separator << hist->GetYaxis()->GetBinWidth(j)/2 << separator << hist->GetBinError(i,j) << endl;
if (j>1)
file_out << endl; // produce a blank line after each set of Ybins for a certain Xbin. Gnuplot likes this.
}
if (i>1)
success=kTRUE;
}
file_out.close();
if (success == kTRUE)
cout << "*** TRemiHistExport: Histogram " << hist->GetName() << " written to " << filename << endl;
return success;
}[/code]
How can I get rid of these sinking, right and left of the exclusion range?
Thank you very much in advance!