#include <NuTH2Interpolator.h>
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. | |
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
Definition at line 34 of file NuTH2Interpolator.h.
|
|
Definition at line 37 of file NuTH2Interpolator.h. 00037 {
00038 simple,
00039 linear
00040 };
|
|
||||||||||||
|
Constructor.
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 }
|
|
|
Definition at line 26 of file NuTH2Interpolator.cxx. References fBaseHistogram. 00027 {
00028 if (fBaseHistogram) delete fBaseHistogram;
00029 fBaseHistogram = 0;
00030 }
|
|
|
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 }
|
|
||||||||||||
|
Handy function to fill a histogram with the interpolation
Definition at line 202 of file NuTH2Interpolator.cxx. 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 }
|
|
|
Retrieves a pointer to the stored histogram.
Definition at line 59 of file NuTH2Interpolator.h. 00059 { return fBaseHistogram; }
|
|
||||||||||||
|
Function to do the interpolation and return the interpolated value.
Definition at line 83 of file NuTH2Interpolator.cxx. References fBaseHistogram, fInterpolation, HandleExtrapolation(), and InterpolateY(). 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 }
|
|
||||||||||||||||||||
|
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(), and InterpolateY(). 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 }
|
|
||||||||||||||||
|
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 }
|
|
||||||||||||||||
|
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 }
|
|
|
Function to set the interpolation type.
Definition at line 54 of file NuTH2Interpolator.h. 00054 {
00055 fInterpolation = interpolation;
00056 };
|
|
|
Stores a copy of the low resolution histogram.
Definition at line 79 of file NuTH2Interpolator.h. Referenced by GetValue(), HandleExtrapolation(), InterpolateX(), InterpolateY(), NuTH2Interpolator(), and ~NuTH2Interpolator(). |
|
|
Stores the mode of interpolation.
Definition at line 81 of file NuTH2Interpolator.h. Referenced by GetValue(), and HandleExtrapolation(). |
1.3.9.1