ROOT logo
// @(#)root/tmva $Id: TSpline2.cxx 29122 2009-06-22 06:51:30Z brun $   
// Author: Andreas Hoecker, Joerg Stelzer, Helge Voss

/**********************************************************************************
 * Project: TMVA - a Root-integrated toolkit for multivariate data analysis       *
 * Package: TMVA                                                                  *
 * Class  : TSpline2                                                              *
 * Web    : http://tmva.sourceforge.net                                           *
 *                                                                                *
 * Description:                                                                   *
 *      Implementation (see header for description)                               *
 *                                                                                *
 * Authors (alphabetical):                                                        *
 *      Andreas Hoecker <Andreas.Hocker@cern.ch> - CERN, Switzerland              *
 *      Helge Voss      <Helge.Voss@cern.ch>     - MPI-K Heidelberg, Germany      *
 *      Kai Voss        <Kai.Voss@cern.ch>       - U. of Victoria, Canada         *
 *                                                                                *
 * Copyright (c) 2005:                                                            *
 *      CERN, Switzerland                                                         * 
 *      U. of Victoria, Canada                                                    * 
 *      MPI-K Heidelberg, Germany                                                 * 
 *                                                                                *
 * Redistribution and use in source and binary forms, with or without             *
 * modification, are permitted according to the terms listed in LICENSE           *
 * (http://tmva.sourceforge.net/LICENSE)                                          *
 **********************************************************************************/

//_______________________________________________________________________
//                                                                      
// Quadratic interpolation of TGraph
//_______________________________________________________________________

#include "TMath.h"

#include "TMVA/TSpline2.h"

ClassImp(TMVA::TSpline2)

//_______________________________________________________________________
TMVA::TSpline2::TSpline2( const TString& title, TGraph* theGraph )
   : fGraph( theGraph ) // not owned by TSpline2
{
   // constructor from TGraph
   // TSpline is a TNamed object
   SetNameTitle( title, title );
}

//_______________________________________________________________________
TMVA::TSpline2::~TSpline2( void )
{
   // destructor
   if (fGraph) delete fGraph; // ROOT's spline classes also own the TGraph
}

//_______________________________________________________________________
Double_t TMVA::TSpline2::Eval( const Double_t x ) const
{  
   // returns quadratically interpolated TGraph entry around x
   Double_t retval=0;
  
   Int_t ibin = TMath::BinarySearch( fGraph->GetN(),
                                     fGraph->GetX(),
                                     x );

   // sanity checks
   if (ibin < 0               ) ibin = 0;
   if (ibin >= fGraph->GetN()) ibin =  fGraph->GetN() - 1;
  
   Float_t dx = 0; // should be zero
  
   if (ibin == 0 ) {
    
      retval = Quadrax(  x,
                         fGraph->GetX()[ibin]   + dx,
                         fGraph->GetX()[ibin+1] + dx,
                         fGraph->GetX()[ibin+2] + dx,
                         fGraph->GetY()[ibin],
                         fGraph->GetY()[ibin+1],
                         fGraph->GetY()[ibin+2]);
    
   }
   else if (ibin >= (fGraph->GetN()-2)) {
      ibin = fGraph->GetN() - 1; // always fixed to last bin

      retval = Quadrax( x,
                        fGraph->GetX()[ibin-2] + dx,
                        fGraph->GetX()[ibin-1] + dx,
                        fGraph->GetX()[ibin]   + dx,
                        fGraph->GetY()[ibin-2],
                        fGraph->GetY()[ibin-1],
                        fGraph->GetY()[ibin]);
   } 
   else {  
    
      retval = ( Quadrax( x, 
                          fGraph->GetX()[ibin-1] + dx,
                          fGraph->GetX()[ibin]   + dx,
                          fGraph->GetX()[ibin+1] + dx,
                          fGraph->GetY()[ibin-1],
                          fGraph->GetY()[ibin],
                          fGraph->GetY()[ibin+1])
                 + 
                 Quadrax( x, fGraph->GetX()[ibin] + dx,
                          fGraph->GetX()[ibin+1]  + dx,
                          fGraph->GetX()[ibin+2]  + dx,
                          fGraph->GetY()[ibin],
                          fGraph->GetY()[ibin+1],
                          fGraph->GetY()[ibin+2]) )*0.5;
   }

   return retval;
}

//_______________________________________________________________________
void TMVA::TSpline2::BuildCoeff( void )
{
   // no coefficients to precompute
}

//_______________________________________________________________________
void TMVA::TSpline2::GetKnot( Int_t  /*i*/, Double_t& /*x*/, Double_t& /*y*/ ) const
{
   // no knots
}

//_______________________________________________________________________
Double_t TMVA::TSpline2::Quadrax( const Float_t dm,const Float_t dm1,const Float_t dm2,const Float_t dm3,
                                  const Float_t cos1, const Float_t cos2, const Float_t cos3 ) const
{  
   // quadratic interpolation
   // Revised and checked by Francois Nov, 16th, 2000
   // Note the beautiful non-spontaneous symmetry breaking ...
   // It was checked that the old routine gave exactly the same answers.
   //   
   Float_t a = cos1*(dm2-dm3) + cos2*(dm3-dm1) + cos3*(dm1-dm2);
   Float_t b = cos1*(dm2*dm2-dm3*dm3) + cos2*(dm3*dm3-dm1*dm1) + cos3*(dm1*dm1-dm2*dm2);
   Float_t c = cos1*(dm2-dm3)*dm2*dm3 + cos2*(dm3-dm1)*dm3*dm1 + cos3*(dm1-dm2)*dm1*dm2;

   Float_t denom = (dm2-dm3)*(dm3-dm1)*(dm1-dm2);
  
   return (denom != 0.0) ? (-a*dm*dm+b*dm-c)/denom : 0.0;
}


 TSpline2.cxx:1
 TSpline2.cxx:2
 TSpline2.cxx:3
 TSpline2.cxx:4
 TSpline2.cxx:5
 TSpline2.cxx:6
 TSpline2.cxx:7
 TSpline2.cxx:8
 TSpline2.cxx:9
 TSpline2.cxx:10
 TSpline2.cxx:11
 TSpline2.cxx:12
 TSpline2.cxx:13
 TSpline2.cxx:14
 TSpline2.cxx:15
 TSpline2.cxx:16
 TSpline2.cxx:17
 TSpline2.cxx:18
 TSpline2.cxx:19
 TSpline2.cxx:20
 TSpline2.cxx:21
 TSpline2.cxx:22
 TSpline2.cxx:23
 TSpline2.cxx:24
 TSpline2.cxx:25
 TSpline2.cxx:26
 TSpline2.cxx:27
 TSpline2.cxx:28
 TSpline2.cxx:29
 TSpline2.cxx:30
 TSpline2.cxx:31
 TSpline2.cxx:32
 TSpline2.cxx:33
 TSpline2.cxx:34
 TSpline2.cxx:35
 TSpline2.cxx:36
 TSpline2.cxx:37
 TSpline2.cxx:38
 TSpline2.cxx:39
 TSpline2.cxx:40
 TSpline2.cxx:41
 TSpline2.cxx:42
 TSpline2.cxx:43
 TSpline2.cxx:44
 TSpline2.cxx:45
 TSpline2.cxx:46
 TSpline2.cxx:47
 TSpline2.cxx:48
 TSpline2.cxx:49
 TSpline2.cxx:50
 TSpline2.cxx:51
 TSpline2.cxx:52
 TSpline2.cxx:53
 TSpline2.cxx:54
 TSpline2.cxx:55
 TSpline2.cxx:56
 TSpline2.cxx:57
 TSpline2.cxx:58
 TSpline2.cxx:59
 TSpline2.cxx:60
 TSpline2.cxx:61
 TSpline2.cxx:62
 TSpline2.cxx:63
 TSpline2.cxx:64
 TSpline2.cxx:65
 TSpline2.cxx:66
 TSpline2.cxx:67
 TSpline2.cxx:68
 TSpline2.cxx:69
 TSpline2.cxx:70
 TSpline2.cxx:71
 TSpline2.cxx:72
 TSpline2.cxx:73
 TSpline2.cxx:74
 TSpline2.cxx:75
 TSpline2.cxx:76
 TSpline2.cxx:77
 TSpline2.cxx:78
 TSpline2.cxx:79
 TSpline2.cxx:80
 TSpline2.cxx:81
 TSpline2.cxx:82
 TSpline2.cxx:83
 TSpline2.cxx:84
 TSpline2.cxx:85
 TSpline2.cxx:86
 TSpline2.cxx:87
 TSpline2.cxx:88
 TSpline2.cxx:89
 TSpline2.cxx:90
 TSpline2.cxx:91
 TSpline2.cxx:92
 TSpline2.cxx:93
 TSpline2.cxx:94
 TSpline2.cxx:95
 TSpline2.cxx:96
 TSpline2.cxx:97
 TSpline2.cxx:98
 TSpline2.cxx:99
 TSpline2.cxx:100
 TSpline2.cxx:101
 TSpline2.cxx:102
 TSpline2.cxx:103
 TSpline2.cxx:104
 TSpline2.cxx:105
 TSpline2.cxx:106
 TSpline2.cxx:107
 TSpline2.cxx:108
 TSpline2.cxx:109
 TSpline2.cxx:110
 TSpline2.cxx:111
 TSpline2.cxx:112
 TSpline2.cxx:113
 TSpline2.cxx:114
 TSpline2.cxx:115
 TSpline2.cxx:116
 TSpline2.cxx:117
 TSpline2.cxx:118
 TSpline2.cxx:119
 TSpline2.cxx:120
 TSpline2.cxx:121
 TSpline2.cxx:122
 TSpline2.cxx:123
 TSpline2.cxx:124
 TSpline2.cxx:125
 TSpline2.cxx:126
 TSpline2.cxx:127
 TSpline2.cxx:128
 TSpline2.cxx:129
 TSpline2.cxx:130
 TSpline2.cxx:131
 TSpline2.cxx:132
 TSpline2.cxx:133
 TSpline2.cxx:134
 TSpline2.cxx:135
 TSpline2.cxx:136
 TSpline2.cxx:137
 TSpline2.cxx:138
 TSpline2.cxx:139
 TSpline2.cxx:140
 TSpline2.cxx:141
 TSpline2.cxx:142
 TSpline2.cxx:143
 TSpline2.cxx:144