Spline.h
Go to the documentation of this file.
1 //____________________________________________________________________________
2 /*!
3 
4 \class genie::Spline
5 
6 \brief A numeric analysis tool class for interpolating 1-D functions.
7 
8  Uses ROOT's TSpline3 for the actual interpolation and can retrieve
9  function (x,y(x)) pairs from an XML file, a flat ascii file, a
10  TNtuple, a TTree or an SQL database.
11 
12 \author Costas Andreopoulos <costas.andreopoulos \at stfc.ac.uk>
13  University of Liverpool & STFC Rutherford Appleton Lab
14 
15 \created May 04, 2004
16 
17 \cpright Copyright (c) 2003-2019, The GENIE Collaboration
18  For the full text of the license visit http://copyright.genie-mc.org
19  or see $GENIE/LICENSE
20 */
21 //____________________________________________________________________________
22 
23 #ifndef _SPLINE_H_
24 #define _SPLINE_H_
25 
26 #include <string>
27 #include <fstream>
28 #include <ostream>
29 
30 #include <TObject.h>
31 #include <TSpline.h>
32 
33 class TNtupleD;
34 class TTree;
35 class TSQLServer;
36 class TGraph;
37 
38 using std::string;
39 using std::ostream;
40 using std::ofstream;
41 
42 namespace genie {
43 
44 class Spline;
45 ostream & operator << (ostream & stream, const Spline & spl);
46 
47 class Spline : public TObject {
48 
49 public:
50  using TObject::Print; // suppress clang 'hides overloaded virtual function [-Woverloaded-virtual]' warnings
51 
52  // ctors & dtor
53  Spline();
54  Spline(string filename, string xtag="", string ytag="", bool is_xml = false);
55  Spline(TNtupleD * ntuple, string xy, string cut="");
56  Spline(TTree * tree, string xy, string cut="");
57  Spline(TSQLServer * db, string query);
58  Spline(int nentries, double x[], double y[]);
59  Spline(int nentries, float x[], float y[]);
60  Spline(const Spline & spline);
61  Spline(const TSpline3 & spline, int nknots);
62  virtual ~Spline();
63 
64  // Load the Spline from XML, flat ASCII, ROOT ntuple/tree/tspline3, or SQL DB
65  bool LoadFromXmlFile (string filename, string xtag, string ytag);
66  bool LoadFromAsciiFile (string filename);
67  bool LoadFromNtuple (TNtupleD * nt, string xy, string cut = "");
68  bool LoadFromTree (TTree * tr, string xy, string cut = "");
69  bool LoadFromDBase (TSQLServer * db, string query);
70  bool LoadFromTSpline3 (const TSpline3 & spline, int nknots);
71 
72  // Get xmin,xmax,nknots, check x variable against valid range and evaluate spline
73  int NKnots (void) const {return fNKnots;}
74  void GetKnot (int iknot, double & x, double & y) const;
75  double GetKnotX (int iknot) const;
76  double GetKnotY (int iknot) const;
77  double XMin (void) const {return fXMin; }
78  double XMax (void) const {return fXMax; }
79  double YMax (void) const {return fYMax; }
80  double Evaluate (double x) const;
81  bool IsWithinValidRange (double x) const;
82 
83  void SetName (string name) { fName = name; }
84  string Name (void) const { return fName; }
85 
86  void YCanBeNegative(bool tf) { fYCanBeNegative = tf; }
87 
88  // Save the Spline in XML, flat ASCII or ROOT format
89  void SaveAsXml (string filename, string xtag, string ytag, string name="") const;
90  void SaveAsXml (ofstream & str, string xtag, string ytag, string name="") const;
91  void SaveAsText(string filename, string format="%10.6f\t%10.6f") const;
92  void SaveAsROOT(string filename, string name="", bool recreate=false) const;
93 
94  // Export Spline as TGraph or TSpline3
95  TGraph * GetAsTGraph (int np = 500, bool xscaling = false,
96  bool inlog=false, double fx=1., double fy=1.) const;
97  TSpline3 * GetAsTSpline (void) const { return fInterpolator; }
98 
99  // Knot manipulation methods in additions to the TSpline3 ones
100  void FindClosestKnot(double x, double & xknot, double & yknot, Option_t * opt="-+") const;
101  bool ClosestKnotValueIsZero(double x, Option_t * opt="-+") const;
102 
103  // Common mathematical operations applied simultaneously on all spline knots
104  void Add (const Spline & spl, double c=1);
105  void Multiply (const Spline & spl, double c=1);
106  void Divide (const Spline & spl, double c=1);
107  void Add (double a);
108  void Multiply (double a);
109  void Divide (double a);
110 
111  // Print knots
112  void Print(ostream & stream) const;
113 
114  // Overloaded operators
115  friend ostream & operator << (ostream & stream, const Spline & spl);
116 
117 private:
118 
119  // Initialize and build spline
120  void InitSpline (void);
121  void ResetSpline (void);
122  void BuildSpline (int nentries, double x[], double y[]);
123 
124  // Private data members
125  string fName;
126  int fNKnots;
127  double fXMin;
128  double fXMax;
129  double fYMax;
130  TSpline3 * fInterpolator;
132 
133 ClassDef(Spline,1)
134 };
135 
136 }
137 
138 #endif
const XML_Char * name
Definition: expat.h:151
TGraph * GetAsTGraph(int np=500, bool xscaling=false, bool inlog=false, double fx=1., double fy=1.) const
Definition: Spline.cxx:490
THE MAIN GENIE PROJECT NAMESPACE
Definition: GeneratorBase.h:8
TTree * ntuple
Definition: macro.C:6
A numeric analysis tool class for interpolating 1-D functions.
Definition: Spline.h:47
void Print(ostream &stream) const
Definition: Spline.cxx:563
bool LoadFromAsciiFile(string filename)
Definition: Spline.cxx:241
void BuildSpline(int nentries, double x[], double y[])
Definition: Spline.cxx:739
string filename
Definition: shutoffs.py:106
bool fYCanBeNegative
Definition: Spline.h:131
Timing fit.
double Evaluate(double x) const
Definition: Spline.cxx:362
bool ClosestKnotValueIsZero(double x, Option_t *opt="-+") const
Definition: Spline.cxx:555
bool IsWithinValidRange(double x) const
Definition: Spline.cxx:356
double YMax(void) const
Definition: Spline.h:79
TSpline3 * GetAsTSpline(void) const
Definition: Spline.h:97
bool LoadFromXmlFile(string filename, string xtag, string ytag)
Definition: Spline.cxx:154
double GetKnotX(int iknot) const
Definition: Spline.cxx:334
void ResetSpline(void)
Definition: Spline.cxx:733
const Int_t nknots
Definition: testXsec.C:24
Long64_t nentries
const double a
double fXMax
Definition: Spline.h:128
void SaveAsROOT(string filename, string name="", bool recreate=false) const
Definition: Spline.cxx:479
std::string format(const int32_t &value, const int &ndigits=8)
Definition: HexUtils.cpp:14
void Add(const Spline &spl, double c=1)
Definition: Spline.cxx:581
void Multiply(const Spline &spl, double c=1)
Definition: Spline.cxx:606
void GetKnot(int iknot, double &x, double &y) const
Definition: Spline.cxx:325
double fYMax
Definition: Spline.h:129
double GetKnotY(int iknot) const
Definition: Spline.cxx:345
void Print(std::string prefix, std::string name, std::string suffix="")
Definition: nue_pid_effs.C:68
double XMax(void) const
Definition: Spline.h:78
string fName
Definition: Spline.h:125
bool LoadFromTSpline3(const TSpline3 &spline, int nknots)
Definition: Spline.cxx:304
void SaveAsText(string filename, string format="%10.6f\t%10.6f") const
Definition: Spline.cxx:458
void SaveAsXml(string filename, string xtag, string ytag, string name="") const
Definition: Spline.cxx:413
const Cut cut
Definition: exporter_fd.C:30
int fNKnots
Definition: Spline.h:126
TSpline3 * fInterpolator
Definition: Spline.h:130
void SetName(string name)
Definition: Spline.h:83
bool LoadFromNtuple(TNtupleD *nt, string xy, string cut="")
Definition: Spline.cxx:252
bool LoadFromDBase(TSQLServer *db, string query)
Definition: Spline.cxx:298
ostream & operator<<(ostream &stream, const AlgConfigPool &config_pool)
string Name(void) const
Definition: Spline.h:84
void YCanBeNegative(bool tf)
Definition: Spline.h:86
int NKnots(void) const
Definition: Spline.h:73
void InitSpline(void)
Definition: Spline.cxx:717
bool LoadFromTree(TTree *tr, string xy, string cut="")
Definition: Spline.cxx:260
virtual ~Spline()
Definition: Spline.cxx:149
double fXMin
Definition: Spline.h:127
void FindClosestKnot(double x, double &xknot, double &yknot, Option_t *opt="-+") const
Definition: Spline.cxx:528
double XMin(void) const
Definition: Spline.h:77
friend ostream & operator<<(ostream &stream, const Spline &spl)
TNtuple * nt
Definition: drawXsec.C:2
void Divide(const Spline &spl, double c=1)
Definition: Spline.cxx:631
enum BeamMode string