# Re: [ROOT] Spline, tgraph and function fit

From: Kevin Reil (reil@slac.stanford.edu)
Date: Thu Mar 18 2004 - 00:40:48 MET

```Hi Rene,

Thanks for the quick response and the addition. However, I was looking for
something more. I guess I want a spline method that is simply a linear
interpolation between points for which I can Eval. The TGraph::Eval method
you created is a TSpline3 which does not simply interpolate between
points.

The problem is for regions in the data (x-axis is time) for which there is
no data and the TSpline3 goes crazy between the two points (and a straight
line between is approximately the correct answer).
The application is tracking the stability of a value versus time where the
value is approximately a constant (but with gaps in time with no reading).

The current TGraph::Draw("L") method creates the lines but no TSpline is
created which stores those lines.

Does this make more sense? Or is there a way to make TSpline simply
interpolate that I missed?

Cheers,
Kevin

|------------------------------------|---------------------------------|
| Kevin Reil                         | 2575 Sand Hill Road, MS 26      |
| X2447, 103D A&E Bldg. 041          | Menlo Park, CA 94025            |
|------------------------------------|---------------------------------|
| http://www.slac.stanford.edu/~reil | Office (650) 926-2447           |
| reil@slac.stanford.edu             | Home   (650) 938-1767           |
| http://reil.no-ip.org              | Fax    (650) 926-5368           |
|----------------------------------------------------------------------|
|                    And my father dwelt in a tent.                    |
|----------------------------------------------------------------------|

On Wed, 17 Mar 2004, Rene Brun wrote:

>Hi Kevin,
>
>I have added a new function in TGraph to make a spline interpolation between
>points
>
>Double_t Eval(Double_t x, TSpline *spline=0) const;
>// Interpolate points in this graph at x using a TSpline
>//  -if spline==0 a TSpline3 object is created using this graph
>//   and the interpolated value from the spline is returned.
>//   the internally created spline is deleted on return.
>//  -if spline is specified, it is used to return the interpolated value.
>
>Now in CVS
>
>Rene Brun
>
>Kevin Reil wrote:
>>
>> Hi all,
>>
>> I've attached below a dialogue I had before about TSpline::Eval. The
>> method described has worked great.
>>
>> However, I want to get a pointer to a TSpline (simple spline not TSpline3
>> or TSpline5).
>>
>> If I have a graph (and assume it has some points filled x=0 to x=100)
>> and if I draw the graph telling it to connect the dots "L" I get a simple
>> spline.
>>
>> TGraph *gr;
>> gr->Draw("L");
>>
>> Is there in fact an object created so I could do something like.
>>
>> TGraph *gr;
>> gr->Draw("L");
>> TSpline *s = (TSpline*)gROOT->FindObject("stemp");
>> // analagous to htemp from nt-Draw();
>> s->Eval(52.3);
>>
>>
>> TGraph *gr;
>> gr->Draw("L");
>> TSpline3 *s = new TSpline3("grs",gr);
>> s->Eval(52.3);
>>
>> Thanks,
>> Kevin
>>
>> -----------------------------------------------------------------------------
>>
>> Hi Kevin,
>>
>> You can use for example the TSpline class. Here is an example:
>> You can use TSpline::Eval to evaluate the spline at a point x.
>>
>> {
>>    gROOT->Reset();
>>    c1 = new TCanvas("c1","gerrors2",200,10,700,500);
>>    c1->SetFillColor(42);
>>    c1->SetGrid();
>>
>>       // draw a frame to define the range
>>    TH1F *hr = c1->DrawFrame(-0.4,0,1.2,12);
>>    hr->SetXTitle("X title");
>>    hr->SetYTitle("Y title");
>>    c1->GetFrame()->SetFillColor(21);
>>    c1->GetFrame()->SetBorderSize(12);
>>
>>       // create first graph
>>    Int_t n1 = 9;
>>    Double_t x1[]  = {-0.22, 0.05, 0.25, 0.35, 0.61,0.7,0.85,0.89,0.95};
>>    Double_t y1[]  = {1,2.9,5.6,7.4,9.6,8.7,6.3,4.5,2};
>>    Double_t ex1[] = {.05,.1,.07,.07,.05,.06,.07,.08,.05};
>>    Double_t ey1[] = {.8,.7,.6,.5,.4,.5,.6,.7,.8};
>>    gr1 = new TGraphErrors(n1,x1,y1,ex1,ey1);
>>    gr1->SetMarkerColor(kBlue);
>>    gr1->SetMarkerStyle(21);
>>    gr1->Draw("LP");
>>
>>      // use a cubic spline to smooth the graph
>>    TSpline3 *s = new TSpline3("grs",gr1);
>>    s->SetLineColor(kRed);
>>    s->Draw("same");
>> }
>>
>>
>> Rene Brun
>>
>> Kevin Reil wrote:
>> >
>> > Hi,
>> >
>> > I was wondering if it is possible to Eval a spline fit.
>> >
>> > TGraph *gr = new TGraph(100,*x, *y);
>> > gr->Draw("apl");
>> >
>> > The l has made a nice spline fit from x=0 to x=100 for example.
>> >
>> > Now, I want to get the value from the spline line at say x=53.2
>> > Something like:
>> > gr->FindObject("spline")->Eval(53.2);
>> >
>> > Thank you,
>> > Kevin
>> >
>> > -------------------------------------------
>> > - Kevin Reil                              -
>> > - #244A INSCC                             -
>> > - http://www.physics.utah.edu/~reil       -
>> > - reil@physics.utah.edu                   -
>> > -------------------------------------------
>> > - And my father dwelt in a tent.          -
>> > -------------------------------------------
>>
>> |------------------------------------|---------------------------------|
>> | Kevin Reil                         | 2575 Sand Hill Road, MS 26      |
>> | X2447, 103D A&E Bldg. 041          | Menlo Park, CA 94025            |
>> |------------------------------------|---------------------------------|
>> | http://www.slac.stanford.edu/~reil | Office (650) 926-2447           |
>> | reil@slac.stanford.edu             | Home   (650) 938-1767           |
>> | http://reil.no-ip.org              | Fax    (650) 926-5368           |
>> |----------------------------------------------------------------------|
>> |                    And my father dwelt in a tent.                    |
>> |----------------------------------------------------------------------|
>>
>> On Tue, 16 Mar 2004, Frank Winklmeier wrote:
>>
>> >Hi,
>> >
>> >I get the following error message on using TChain::GetEntry()
>> >
>> >root [28] chain.GetEntry(1,0)  Error in <TBranch::GetBasket>: File:
>> >BBbar-41.root at byte:0, branch:event, entry:-1
>> >(Int_t)(-1)
>> >
>> >Can somebody explain me what this error means and where it originally
>> >comes from (it's not in TBranch::GetBasket)? How can I find out what's
>> >wrong with this root file?
>> >
>> >Thanks,
>> >Frank
>> >
>> >
>> >
>
>
```

This archive was generated by hypermail 2b29 : Sun Jan 02 2005 - 05:50:06 MET