| 1 | /* ======================================================================== *\ | 
|---|
| 2 | ! | 
|---|
| 3 | ! * | 
|---|
| 4 | ! * This file is part of MARS, the MAGIC Analysis and Reconstruction | 
|---|
| 5 | ! * Software. It is distributed to you in the hope that it can be a useful | 
|---|
| 6 | ! * and timesaving tool in analysing Data of imaging Cerenkov telescopes. | 
|---|
| 7 | ! * It is distributed WITHOUT ANY WARRANTY. | 
|---|
| 8 | ! * | 
|---|
| 9 | ! * Permission to use, copy, modify and distribute this software and its | 
|---|
| 10 | ! * documentation for any purpose is hereby granted without fee, | 
|---|
| 11 | ! * provided that the above copyright notice appear in all copies and | 
|---|
| 12 | ! * that both that copyright notice and this permission notice appear | 
|---|
| 13 | ! * in supporting documentation. It is provided "as is" without express | 
|---|
| 14 | ! * or implied warranty. | 
|---|
| 15 | ! * | 
|---|
| 16 | ! | 
|---|
| 17 | ! | 
|---|
| 18 | !   Author(s): Thomas Bretz  1/2009 <mailto:tbretz@astro.uni-wuerzburg.de> | 
|---|
| 19 | ! | 
|---|
| 20 | !   Copyright: Software Development, 2000-2009 | 
|---|
| 21 | ! | 
|---|
| 22 | ! | 
|---|
| 23 | \* ======================================================================== */ | 
|---|
| 24 |  | 
|---|
| 25 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
| 26 | // | 
|---|
| 27 | //  MSpline3 | 
|---|
| 28 | // | 
|---|
| 29 | // This is a extension of TSpline3. In addition to TSpline3 it allows access | 
|---|
| 30 | // to Xmin, Xman and Np. The construction is a bit simplified because no | 
|---|
| 31 | // title hase to be given (it can be given later by SetTitle anyway) | 
|---|
| 32 | // and is provides constructors which allow to scale the x-values by | 
|---|
| 33 | // pre-defined multiplier (e.g. frequency) to create the spline. | 
|---|
| 34 | // | 
|---|
| 35 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
| 36 | #include "MSpline3.h" | 
|---|
| 37 |  | 
|---|
| 38 | #include <TF1.h> | 
|---|
| 39 |  | 
|---|
| 40 | #include "MArrayD.h" | 
|---|
| 41 |  | 
|---|
| 42 | ClassImp(MSpline3); | 
|---|
| 43 |  | 
|---|
| 44 | using namespace std; | 
|---|
| 45 |  | 
|---|
| 46 | // -------------------------------------------------------------------------- | 
|---|
| 47 | // | 
|---|
| 48 | //  Constructor. | 
|---|
| 49 | // | 
|---|
| 50 | MSpline3::MSpline3(const TF1 &f, const char *opt, Double_t valbeg, Double_t valend) | 
|---|
| 51 | : TSpline3("MSpline3", f.GetXmin(), f.GetXmax(), &f, f.GetNpx(), opt, valbeg, valend) | 
|---|
| 52 | { | 
|---|
| 53 | } | 
|---|
| 54 |  | 
|---|
| 55 | MSpline3::MSpline3(const TF1 &f, Double_t freq, const char *opt,Double_t valbeg, Double_t valend) | 
|---|
| 56 | : TSpline3("MSpline3", f.GetXmin()*freq, f.GetXmax()*freq, ConvertFunc(f, freq).GetArray(), f.GetNpx(), opt, valbeg, valend) | 
|---|
| 57 | { | 
|---|
| 58 | } | 
|---|
| 59 |  | 
|---|
| 60 | // -------------------------------------------------------------------------- | 
|---|
| 61 | // | 
|---|
| 62 | //  This is a helper to convert the x-values by multiplying with freq | 
|---|
| 63 | // before initializing the spline | 
|---|
| 64 | // | 
|---|
| 65 | TGraph *MSpline3::ConvertSpline(const TSpline &s, Float_t freq) const | 
|---|
| 66 | { | 
|---|
| 67 | const UInt_t npx = s.GetNpx(); | 
|---|
| 68 |  | 
|---|
| 69 | // WARNING: This is a stupid workaround because the TSpline3- | 
|---|
| 70 | // constructor takes a pointer as input! It is not thread-safe! | 
|---|
| 71 | static TGraph g; | 
|---|
| 72 | g.Set(npx); | 
|---|
| 73 |  | 
|---|
| 74 | for (UInt_t i=0; i<npx; i++) | 
|---|
| 75 | { | 
|---|
| 76 | Double_t x, y; | 
|---|
| 77 | s.GetKnot(i, x, y); | 
|---|
| 78 | g.SetPoint(i, x*freq, y); | 
|---|
| 79 | } | 
|---|
| 80 |  | 
|---|
| 81 | return &g; | 
|---|
| 82 | } | 
|---|
| 83 |  | 
|---|
| 84 | // -------------------------------------------------------------------------- | 
|---|
| 85 | // | 
|---|
| 86 | //  This is a helper to convert the x-values by multiplying with freq | 
|---|
| 87 | // before initializing the spline | 
|---|
| 88 | // | 
|---|
| 89 | TGraph *MSpline3::ConvertGraph(const TGraph &s, Float_t freq) const | 
|---|
| 90 | { | 
|---|
| 91 | const UInt_t npx = s.GetN(); | 
|---|
| 92 |  | 
|---|
| 93 | // WARNING: This is a stupid workaround because the TSpline3- | 
|---|
| 94 | // constructor takes a pointer as input! It is not thread-safe! | 
|---|
| 95 | static TGraph g; | 
|---|
| 96 | g.Set(npx); | 
|---|
| 97 |  | 
|---|
| 98 | for (UInt_t i=0; i<npx; i++) | 
|---|
| 99 | { | 
|---|
| 100 | Double_t x, y; | 
|---|
| 101 | s.GetPoint(i, x, y); | 
|---|
| 102 | g.SetPoint(i, x*freq, y); | 
|---|
| 103 | } | 
|---|
| 104 |  | 
|---|
| 105 | return &g; | 
|---|
| 106 | } | 
|---|
| 107 |  | 
|---|
| 108 | // -------------------------------------------------------------------------- | 
|---|
| 109 | // | 
|---|
| 110 | //  This is a helper to convert the x-values by multiplying with freq | 
|---|
| 111 | // before initializing the spline. The conversion from the function to | 
|---|
| 112 | // a discrete binning is done similar to the constructor of TSpline | 
|---|
| 113 | // | 
|---|
| 114 | MArrayD &MSpline3::ConvertFunc(const TF1 &f, Float_t freq) const | 
|---|
| 115 | { | 
|---|
| 116 | const UInt_t npx = f.GetNpx(); | 
|---|
| 117 |  | 
|---|
| 118 | // WARNING: This is a stupid workaround because the TSpline3- | 
|---|
| 119 | // constructor takes a pointer as input! It is not thread-safe! | 
|---|
| 120 | static MArrayD g; | 
|---|
| 121 | g.Set(npx); | 
|---|
| 122 |  | 
|---|
| 123 | const Double_t step = (f.GetXmax()-f.GetXmin())/(npx-1); | 
|---|
| 124 |  | 
|---|
| 125 | for (UInt_t i=0; i<npx; ++i) | 
|---|
| 126 | { | 
|---|
| 127 | const Double_t x = f.GetXmin() + i*step; | 
|---|
| 128 | g[i] = f.Eval(x); | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 | return g; | 
|---|
| 132 | } | 
|---|
| 133 |  | 
|---|
| 134 | // FIXME: As soon as TSpline3 allows access to fPoly we can implement | 
|---|
| 135 | //        a much faster evaluation of the spline, especially in | 
|---|
| 136 | //        special conditions like in MAnalogSignal::AddPulse | 
|---|
| 137 | //        This will be the case for root > 5.22/00 | 
|---|
| 138 |  | 
|---|
| 139 | /* | 
|---|
| 140 | Double_t MSpline3::EvalFast(Double_t x) const | 
|---|
| 141 | { | 
|---|
| 142 | // Eval this spline at x | 
|---|
| 143 | const Int_t klow=FindFast(x); | 
|---|
| 144 | return fPoly[klow].Eval(x); | 
|---|
| 145 | } | 
|---|
| 146 |  | 
|---|
| 147 | Int_t MSpline3::FindFast(Double_t x) const | 
|---|
| 148 | { | 
|---|
| 149 | // | 
|---|
| 150 | // If out of boundaries, extrapolate | 
|---|
| 151 | // It may be badly wrong | 
|---|
| 152 |  | 
|---|
| 153 | // if (x<=fXmin) | 
|---|
| 154 | //     return 0; | 
|---|
| 155 | // | 
|---|
| 156 | // if (x>=fXmax) | 
|---|
| 157 | //     return fNp-1; | 
|---|
| 158 |  | 
|---|
| 159 | // | 
|---|
| 160 | // Equidistant knots, use histogramming | 
|---|
| 161 | if (fKstep) | 
|---|
| 162 | return TMath::Min(Int_t((x-fXmin)/fDelta),fNp-1); | 
|---|
| 163 |  | 
|---|
| 164 | // | 
|---|
| 165 | // Non equidistant knots, binary search | 
|---|
| 166 | Int_t klow = 0; | 
|---|
| 167 | Int_t khig = fNp-1; | 
|---|
| 168 |  | 
|---|
| 169 | Int_t khalf; | 
|---|
| 170 | while (khig-klow>1) | 
|---|
| 171 | if(x>fPoly[khalf=(klow+khig)/2].X()) | 
|---|
| 172 | klow=khalf; | 
|---|
| 173 | else | 
|---|
| 174 | khig=khalf; | 
|---|
| 175 |  | 
|---|
| 176 | // This could be removed, sanity check | 
|---|
| 177 | //if(!(fPoly[klow].X()<=x && x<=fPoly[klow+1].X())) | 
|---|
| 178 | //    Error("Eval", | 
|---|
| 179 | //          "Binary search failed x(%d) = %f < %f < x(%d) = %f\n", | 
|---|
| 180 | //          klow,fPoly[klow].X(),x,fPoly[klow+1].X()); | 
|---|
| 181 |  | 
|---|
| 182 | return klow; | 
|---|
| 183 | } | 
|---|
| 184 | */ | 
|---|