Fitting pol2()

Hi folks

Finally I get the solution to my problem. I was trying to fit a pol2 into my data but root was fitting a linear function… I was using:

TF1 *myfit;
.
.
.
c1 = new TCanvas(“c1”,“Transmission”,20,50,1200,800);

c1->SetFillColor(kWhite);
c1->SetGrid();
myfit = new TF1(“myfit”,“pol2(0)”, 3170, 3181);
.
.
.
gr->SetTitle(message);
gr->GetXaxis()->SetTitle(“runs”);
gr->GetYaxis()->SetTitle(“Signal Output”);

gr->Fit(“myfit”,“R”);
gr->Draw(“ACP”);
.
.
.

this did not work, but then I wrote this instead:

.
.
.
gr->Fit(“pol2”);

and it is working now. To tell you the truth, this is the first time using root, and after around 4 hours trying to find the error I hit a post that brought me to this solution. I would like to know what the first code that I wrote did not work. I really appreciate your comments.

Second question: how can I extended mi fitting to a wider range … can I use the option “R” in the line

gr->Fit(“pol2”,“R”)

I did like that but root does not show the fitting.

Third and final question: if this is the way to do a plot, then I do not need the object TF1 *myfit ?

Again, thanks for any comments,

Cheers,
Kris[quote][/quote][quote][/quote]

Could you send the shortest possible running script showing this problem?

Rene

This was my program. Please, check line 26 and 46. It is there where I was wondering if I was doing wrong. The text file that I open in the program consist of 2 columns of numbers.

I really appreciate your comments.

{
//THANK YOU TO @DABAGIAN
//MODIFY JULY 8/2004 BY @KRISFRAJER

#include “iostream.h”

gROOT->Reset();
const Int_t n = 5;

char fname[100], message[50];
ifstream in;
Int_t ring=2;
Int_t run=3173;
Int_t chann=21;
Double_t p, q;
Double_t x[n], y[n];
TF1 *myfit;

sprintf(fname,“plotDt%drn5.txt”,chann);
in.open(fname);

c1 = new TCanvas(“c1”,“Transmission”,20,50,1200,800);

c1->SetFillColor(kWhite);
c1->SetGrid();
//line26
myfit = new TF1(“myfit”,"[0]+[1]*x", 3170, 3181);

//I was trying to get a square fit, so a replace the //formula on myfit object to something like “pol2” //and “[0]+[1]*x+[2]*x^2”. None of this work. What
//was I doing wrong?

for (Int_t i=0;i<n;i++){
in >> p >> q;
x[i] = p;
y[i] = q;
printf(" i %i %f %f \n",i,x[i],y[i]);
}

gr = new TGraph(n,x,y);
gr->SetLineColor(1);
gr->SetLineWidth(1);
gr->SetMarkerColor(4);
gr->SetMarkerStyle(25);
sprintf(message,“detector %d”,chann);
gr->SetTitle(message);
gr->GetXaxis()->SetTitle(“runs”);
gr->GetYaxis()->SetTitle(“Signal Output”);

//line46
gr->Fit(“myfit”,“R”);
//I found out that if I write the last line as
//gr->Fit(“pol2”);
//my program worked!
gr->Draw(“ACP”);

// TCanvas::Update() draws the frame, after
// which one can change it
c1->Update();
c1->GetFrame()->SetFillColor(kWhite);
c1->GetFrame()->SetBorderSize(2);
c1->Modified();
in.close();
}

you forgot the data file

Rene

Sorry, I forgot to include the file…

Kris
plotDt21rn5.txt (65 Bytes)

If you want to draw your picture with the function range, you should draw the function first. See your script slightly modified in the attachement

Rene
fpol2.C (1.51 KB)

Thank you rene,

now it is working as I wanted. Now my next challenge is to plot several graphs in the same pad, so I will use TPad. Wish me luck,

Krisfrajer

I have a quick question… can I write this code that i just showed you in c++? for example

int main(int argc, char *argv[])
{
gROOT->Reset();
onst Int_t n = 5;

char fname[100], message[50];
ifstream in;
.
.
.

}

Thanks,

Krisfrajer

Yes but remove

and add the proper #include (at the start of the file).

Cheers,
Philippe.

#include “TH1.h”
#include “TF1.h”
#include “TProfile.h”
#include “TNtuple.h”
#include “TRandom.h”
#include “TGraph.h”
#include “TCanvas.h”
#include “TFrame.h”

this was what I need to de what I wanted,
Thank you,

Krisfrajer