NuTH2Interpolator Class Reference

Interpolates a histogram. More...

#include <NuTH2Interpolator.h>

List of all members.

Public Types

enum  mode { simple, linear }

Public Member Functions

 NuTH2Interpolator (const TH2D &usehist, Bool_t central_representation=false)
 Constructor.
virtual ~NuTH2Interpolator ()
Double_t GetValue (Double_t x, Double_t y)
 Function to do the interpolation and return the interpolated value.
void SetInterpolationMode (mode interpolation=linear)
 Function to set the interpolation type.
TH2D * GetHist ()
 Retrieves a pointer to the stored histogram.
void FillHistogram (TH2D *hist, Bool_t central=false)

Private Member Functions

TH2D * ConvertCentraltoEdge (const TH2D &chist)
Double_t HandleExtrapolation (Int_t xBin, Double_t x, Int_t yBin, Double_t y)
Double_t InterpolateX (Int_t xBin, Int_t yBin, Double_t x)
 Interpolates along the bottom edge of two x bins.
Double_t InterpolateY (Int_t xBin, Int_t yBin, Double_t y)
 Interpolates along the left edge of two y bins.

Private Attributes

TH2D * fBaseHistogram
 Stores a copy of the low resolution histogram.
mode fInterpolation
 Stores the mode of interpolation.

Detailed Description

Interpolates a histogram.

This class takes a 2D histogram (currently a TH2D), and can interpolate it's value so that a histogram can be converted to higher resolution. This obviously needs to be thought about carefully before applying.

I haven't tested this or altered it to work with 1D histograms

Author:
Nick Devenish, Last checkin:
Author
bckhouse
Version:
Revision
1.6
Date:
Date
2010/09/17 18:01:51

Created on: Sun Aug 10, 2008

Definition at line 34 of file NuTH2Interpolator.h.


Member Enumeration Documentation

Enumerator:
simple 
linear 

Definition at line 37 of file NuTH2Interpolator.h.

00037               {
00038         simple,
00039         linear
00040     };


Constructor & Destructor Documentation

NuTH2Interpolator::NuTH2Interpolator ( const TH2D &  usehist,
Bool_t  central_representation = false 
)

Constructor.

Parameters:
usehist The histogram to use to interpolate. A copy is made.
central_representation True if the bin center of usehist represents it's value, rather than the lower edge.

Definition at line 12 of file NuTH2Interpolator.cxx.

References ConvertCentraltoEdge(), and fBaseHistogram.

00012                                                                         :
00013         fBaseHistogram(0), fInterpolation(linear)
00014 {
00015     // If we have been passed a central representation histogram,
00016     // it will be easier if we just change it to edge representation
00017     if (central) {
00018         fBaseHistogram = ConvertCentraltoEdge(usehist);
00019     } else {
00020         // Make a copy of the histogram
00021         fBaseHistogram = new TH2D(usehist);
00022         fBaseHistogram->SetDirectory(0); 
00023     }
00024 }

NuTH2Interpolator::~NuTH2Interpolator (  )  [virtual]

Definition at line 26 of file NuTH2Interpolator.cxx.

References fBaseHistogram.

00027 {
00028     if (fBaseHistogram) delete fBaseHistogram;
00029     fBaseHistogram = 0;
00030 }


Member Function Documentation

TH2D * NuTH2Interpolator::ConvertCentraltoEdge ( const TH2D &  chist  )  [private]

Converts a histogram with central value representation to lower edge representation

Definition at line 34 of file NuTH2Interpolator.cxx.

Referenced by NuTH2Interpolator().

00035 {
00036     // We have a histogram where the center of each bin represents
00037     // the value of that bin. Change this so that the lower edge of
00038     // the bin represents the value.
00039     
00040     // Get the count of bins
00041     Int_t bincountX = chist.GetNbinsX();
00042     Int_t bincountY = chist.GetNbinsY();
00043     
00044     // Make arrays for X and Y with the bin positions
00045     // Double_t *Xbins = new Double_t[binsX];
00046     // Double_t *Ybins = new Double_t[binsY];
00047     
00048     vector <Double_t> xBins;
00049     vector <Double_t> yBins;
00050     
00051     // Firstly, process the X axis
00052     for (Int_t i = 1; i <= bincountX; i++)
00053         xBins.push_back(chist.GetXaxis()->GetBinCenter(i));
00054     // We need to have a width for the last bin. Use the same as the first
00055     xBins.push_back(xBins.back()+chist.GetXaxis()->GetBinWidth(1));
00056 
00057     // Now do the Y axis
00058     for (Int_t i = 1; i <= bincountY; i++)
00059         yBins.push_back(chist.GetYaxis()->GetBinCenter(i));
00060     // We need to have a width for the last bin. Use the same as the first
00061     yBins.push_back(yBins.back()+chist.GetYaxis()->GetBinWidth(1));
00062     
00063     // Ensure that the count matches up 
00064     if ((Int_t)yBins.size() != bincountY + 1)
00065         cout << "Error: X Vector size " << yBins.size() << " mismatch with bin count " << bincountY << endl;
00066     if ((Int_t)xBins.size() != bincountX + 1)
00067         cout << "Error: Y Vector size " << xBins.size() << " mismatch with bin count " << bincountX << endl;
00068 
00069     // Now, create a histogram with these bins
00070     TH2D *lhist = new TH2D("lrephist", "LRepHist", bincountX, &(xBins.front()), bincountY, &(yBins.front()));
00071     
00072     // Now we should simply be able to copy it over, bin by bin - the numbers should be identical
00073     for (Int_t x = 1; x <= bincountX; x++)
00074         for (Int_t y = 1; y <= bincountY; y++)
00075             lhist->SetBinContent(x, y, chist.GetBinContent(x, y));
00076             
00077     // This should all be complete!
00078     return lhist;
00079 }

void NuTH2Interpolator::FillHistogram ( TH2D *  hist,
Bool_t  central = false 
)

Handy function to fill a histogram with the interpolation

Parameters:
hist Pointer to the histogram to use
central Boolean specifying if the histogram's center should represent the value

Definition at line 202 of file NuTH2Interpolator.cxx.

References GetValue().

00203 {
00204     // Loop over the histogram, filling each bin!
00205     for (Int_t i = 1; i <= high->GetNbinsX(); i++)
00206     {
00207         for (Int_t j = 1; j <= high->GetNbinsY(); j++)
00208         {
00209             Double_t x = 0, y = 0;
00210             // Get the low edge or the center, depending on our parameter
00211             if (!central) {
00212                 x = high->GetXaxis()->GetBinLowEdge(i);
00213                 y = high->GetYaxis()->GetBinLowEdge(j);
00214             } else {
00215                 x = high->GetXaxis()->GetBinCenter(i);
00216                 y = high->GetYaxis()->GetBinCenter(j);
00217             }
00218             high->SetBinContent(i, j, this->GetValue(x, y));
00219         }
00220     }
00221 }

TH2D* NuTH2Interpolator::GetHist (  )  [inline]

Retrieves a pointer to the stored histogram.

Definition at line 59 of file NuTH2Interpolator.h.

References fBaseHistogram.

00059 { return fBaseHistogram; }

Double_t NuTH2Interpolator::GetValue ( Double_t  x,
Double_t  y 
)

Function to do the interpolation and return the interpolated value.

Definition at line 83 of file NuTH2Interpolator.cxx.

References fBaseHistogram, fInterpolation, HandleExtrapolation(), InterpolateY(), and simple.

Referenced by FillHistogram().

00084 {
00085     // Get the count of bins
00086     Int_t bincountX = fBaseHistogram->GetNbinsX();
00087     Int_t bincountY = fBaseHistogram->GetNbinsY();
00088     
00089     // Work out which bin this coordinate is sitting in
00090     Int_t globalBin, xBin, yBin, zBin;
00091     globalBin = fBaseHistogram->FindBin(x, y);
00092     fBaseHistogram->GetBinXYZ(globalBin, xBin, yBin, zBin);
00093 
00094     // If we are outside the bounds, handle this elsewhere
00095     if (xBin >= bincountX || yBin >= bincountY || xBin < 1 || yBin < 1) {
00096         return HandleExtrapolation(xBin, x, yBin, y);
00097     }
00098 
00099     // Has the user just asked for simple?
00100     if (fInterpolation == simple) return fBaseHistogram->GetBinContent(xBin, yBin);
00101     
00102     // To interpolate, we need to interpolate the left and right edges
00103     // to our current y position, and then interpolate those values to
00104     // our current x position
00105     Double_t lInt = InterpolateY(xBin, yBin, y);
00106     Double_t rInt = InterpolateY(xBin+1, yBin, y);
00107     
00108     // Now perform an x interpolation between these two points
00109     Double_t left = fBaseHistogram->GetXaxis()->GetBinLowEdge(xBin);
00110     Double_t right = fBaseHistogram->GetXaxis()->GetBinLowEdge(xBin+1);
00111     
00112     Double_t value = lInt + (x-left) * (rInt-lInt)/(right-left);
00113 
00114     // This should now be the interpolated value!
00115     return value;
00116 }

Double_t NuTH2Interpolator::HandleExtrapolation ( Int_t  xBin,
Double_t  x,
Int_t  yBin,
Double_t  y 
) [private]

Handles the interpolation if the user has asked for a point outside of the histograms range

Definition at line 155 of file NuTH2Interpolator.cxx.

References fBaseHistogram, fInterpolation, InterpolateX(), InterpolateY(), and linear.

Referenced by GetValue().

00156 {
00157     // Get the count of bins
00158     Int_t bincountX = fBaseHistogram->GetNbinsX();
00159     Int_t bincountY = fBaseHistogram->GetNbinsY();
00160     
00161     // Handle the interpolation if we are left or right of the histogram
00162     if (xBin >= bincountX || xBin <= 0)
00163     {
00164         // We are to the side of the histogram. Reset the bin
00165         if (xBin < 1)           xBin = 1;
00166         if (xBin >= bincountX)  xBin = bincountX;
00167         
00168         // Check to see if we are in either of the corners
00169         if (yBin >= bincountY)  return fBaseHistogram->GetBinContent(xBin, bincountY);
00170         if (yBin <= 0)          return fBaseHistogram->GetBinContent(xBin, 1);
00171 
00172         // We aren't in a corner, so can interpolate over the y axis to get the value
00173         if (fInterpolation == linear)
00174             return InterpolateY(xBin, yBin, y);
00175         else
00176             return fBaseHistogram->GetBinContent(xBin, yBin);
00177     }
00178     
00179     // Now, since we have done the corners already, we only need to
00180     // worry about interpolating the top and bottom. Double check our
00181     // bounds anyway, just to be sure
00182     if (yBin >= bincountY || yBin <= 0)
00183     {
00184         // Which is it? this changes where we should reset to...
00185         if (yBin < 1)           yBin = 1;
00186         if (yBin >= bincountY)  yBin = bincountY;
00187         
00188         // Now interpolate between xBin, yBin and xBin+1, yBin
00189         if (fInterpolation == linear)
00190             return InterpolateX(xBin, yBin, x);
00191         else
00192             return fBaseHistogram->GetBinContent(xBin, yBin);
00193     }
00194     
00195     // We shouldn't get here, but is we do, set the value to zero
00196     cout << "Error: Appear to be handling extrapolation whilst inside bounds." << endl;
00197     return 0;
00198 }

Double_t NuTH2Interpolator::InterpolateX ( Int_t  xBin,
Int_t  yBin,
Double_t  x 
) [private]

Interpolates along the bottom edge of two x bins.

Definition at line 138 of file NuTH2Interpolator.cxx.

References fBaseHistogram.

Referenced by HandleExtrapolation().

00139 {
00140     // Grab the values we need
00141     Double_t left = fBaseHistogram->GetXaxis()->GetBinLowEdge(xBin);
00142     Double_t right = fBaseHistogram->GetXaxis()->GetBinLowEdge(xBin+1);
00143     
00144     Double_t leftval = fBaseHistogram->GetBinContent(xBin, yBin);
00145     Double_t rightval = fBaseHistogram->GetBinContent(xBin+1, yBin);
00146     
00147     // Now do the interpolation!
00148     Double_t val = leftval + (x-left)*(rightval-leftval)/(right-left);
00149 
00150     return val;
00151 }

Double_t NuTH2Interpolator::InterpolateY ( Int_t  xBin,
Int_t  yBin,
Double_t  y 
) [private]

Interpolates along the left edge of two y bins.

Definition at line 120 of file NuTH2Interpolator.cxx.

References fBaseHistogram.

Referenced by GetValue(), and HandleExtrapolation().

00121 {
00122     // First interpolate the left edge to this y value
00123     Double_t bottom = fBaseHistogram->GetYaxis()->GetBinLowEdge(yBin);
00124     Double_t top = fBaseHistogram->GetYaxis()->GetBinLowEdge(yBin+1);
00125     
00126     Double_t upperval = fBaseHistogram->GetBinContent(xBin, yBin+1);
00127     Double_t lowerval = fBaseHistogram->GetBinContent(xBin, yBin);
00128     
00129     // Do the interpolation! This means we have the 
00130     Double_t val = lowerval + (y - bottom) * (upperval-lowerval)/(top-bottom);
00131     
00132     return val;
00133 
00134 }

void NuTH2Interpolator::SetInterpolationMode ( mode  interpolation = linear  )  [inline]

Function to set the interpolation type.

Definition at line 54 of file NuTH2Interpolator.h.

References fInterpolation.

00054                                                            {
00055         fInterpolation = interpolation;
00056     };


Member Data Documentation

Stores a copy of the low resolution histogram.

Definition at line 79 of file NuTH2Interpolator.h.

Referenced by GetHist(), GetValue(), HandleExtrapolation(), InterpolateX(), InterpolateY(), NuTH2Interpolator(), and ~NuTH2Interpolator().

Stores the mode of interpolation.

Definition at line 81 of file NuTH2Interpolator.h.

Referenced by GetValue(), HandleExtrapolation(), and SetInterpolationMode().


The documentation for this class was generated from the following files:

Generated on 22 Nov 2017 for loon by  doxygen 1.6.1