Get ASCII function data into TF1 object

Hello,

I am a student and work currently at SMI, Vienna, where I should perform some simulations using PANDA ROOT, which is basically just ROOT with some additions. My question is clearly related to the ROOT base classes, so I thought it would be a good idea to post my question here.

My problem is the following: I have an ASCII file with function values, x in the first and y in the second column, with x steps of variable lenght. Now I would like to create a TF1 object which uses that values. Unfortunately, I could not find out how to do that “natively”. So far, I have written a quite bloated bit of code, where I create a function that has to read the ASCII file every time it is called, interpolates between the sampling points, and then create a TF1 object using a pointer to that function. Of course, that works, and for testing it is alright, but I do not like it very much, especially since its performance is miserable. I really need the function in an TF1 object, since I have to take random numbers out of the distribution defined by that function.

Any ideas?

Thanks a lot and sorry for me asking so basic questions
Christian Leitold

see example below

Rene

[code]//example showing how to create a TF1 object using a TGraph.
TGraph *theGraph;
Double_t fgtf1(Double_t *x, Double_t *) {
Double_t y = theGraph->Eval(x[0]);
return y;
}
void gtf1() {
const Int_t n = 10;
Double_t x[] = {-0.22, 0.05, 0.25, 0.35, 0.5, 0.61,0.7,0.85,0.89,0.95};
Double_t y[] = {1,2.9,5.6,7.4,9,9.6,8.7,6.3,4.5,1};
theGraph = new TGraph(n,x,y);
Double_t xmin = -0.24, xmax = 0.96;
TF1 *f1 = new TF1(“f1”,fgtf1,xmin,xmax,0);
//draw the function and get get random numbers into a histogram
TCanvas *c1 = new TCanvas(“c1”,“c1”,800,1000);
c1->Divide(1,2);
c1->cd(1);
f1->Draw();
theGraph->SetLineColor(kBlue);
theGraph->Draw(“l”);
c1->cd(2);
TH1F *h = new TH1F(“h1”,“random numbers from gtf1”,100,xmin,xmax);
for (Int_t i=0;i<100000;i++) {
Double_t r = f1->GetRandom();
h->Fill®;
}
h->Draw();
}

[/code]

Hi,

thanks for your reply, I think that is exactly what I was looking for! With that solution, I have to read the ASCII file only once, which should be a real improvement.