test_RootParFormula.cxx
Go to the documentation of this file.
1 // test_RootParFormula.cxx
2 
3 // David Adams
4 // July 2021.
5 //
6 // This is a test and demonstration for RootParFormula.
7 
8 #undef NDEBUG
9 
10 #include "../RootParFormula.h"
11 #include <string>
12 #include <iostream>
13 #include <iomanip>
14 #include <cassert>
15 #include <vector>
16 #include "TH1F.h"
17 #include "TCanvas.h"
18 
19 using std::string;
20 using std::cout;
21 using std::endl;
22 using std::setw;
23 using std::vector;
24 
25 using Index = unsigned int;
26 using DVec = std::vector<double>;
27 
28 //**********************************************************************
29 
30 bool floatcheck(double x1, double x2) {
31  return fabs(x1-x2) < 1.e-4;
32 }
33 
34 //**********************************************************************
35 
37  const string myname = "test_RootParFormula: ";
38  cout << myname << "Starting test" << endl;
39 #ifdef NDEBUG
40  cout << myname << "NDEBUG must be off." << endl;
41  abort();
42 #endif
43  string line = "-----------------------------";
44  string scfg;
45 
46  cout << myname << line << endl;
47  cout << myname << "Create formula and check." << endl;
48  string snam = "myform";
49  string sform = "[offset] + [scale]*x";
50  RootParFormula rpf(snam, sform);
51  cout << myname << "Name: " << rpf.name() << endl;
52  assert( rpf.name() == snam );
53  cout << myname << "Formula: " << rpf.formulaString() << endl;
54  assert( rpf.formulaString() == sform );
55  cout << myname << "# params: " << rpf.npar() << endl;
56  assert( rpf.npar() == 2 );
57  cout << myname << "Params: [";
58  bool first = true;
59  for ( string nam : rpf.pars() ) {
60  if ( first ) first = false;
61  else cout << ", ";
62  cout << nam;
63  }
64  cout << "]" << endl;
65  assert( rpf.pars().size() == 2 );
66  assert( rpf.pars()[0] == "offset" );
67  assert( rpf.pars()[1] == "scale" );
68  cout << myname << "# vars: " << rpf.nvar() << endl;
69  assert( rpf.nvar() == 1 );
70  assert( rpf.setPars().size() == 0 );
71  assert( rpf.unsetPars().size() == 2 );
72  assert( rpf.resetPars().size() == 0 );
73  assert( rpf.pars().size() == 2 );
74  assert( ! rpf.ready() );
75 
76  cout << myname << line << endl;
77  cout << myname << "Check par names" << endl;
78  assert( rpf.isPar("offset") );
79  assert( rpf.isPar("scale") );
80  assert( ! rpf.isPar("nosuch") );
81 
82  cout << myname << line << endl;
83  cout << myname << "Set parameters." << endl;
84  assert( rpf.unsetPars().size() == 2 );
85  assert( rpf.resetPars().size() == 0 );
86  assert( ! rpf.ready() );
87  rpf.setParValue("offset", 10.0);
88  assert( rpf.unsetPars().size() == 1 );
89  assert( rpf.resetPars().size() == 0 );
90  assert( ! rpf.ready() );
91  rpf.setParValue("scale", 3.0);
92  assert( rpf.unsetPars().size() == 0 );
93  assert( rpf.resetPars().size() == 0 );
94  assert( rpf.ready() );
95  rpf.setParValue("scale", 2.0);
96  assert( rpf.unsetPars().size() == 0 );
97  assert( rpf.resetPars().size() == 1 );
98  assert( rpf.ready() );
99 
100  cout << myname << line << endl;
101  cout << myname << "Check eval." << endl;
102  DVec vals = { 0.0, 1.0, 2.0 };
103  DVec evcs = { 10.0, 12.0, 14.0 };
104  for ( Index ival=0; ival<vals.size(); ++ival ) {
105  double eval = rpf.eval(vals[ival]);
106  cout << myname << " " << vals[ival] << ": " << eval << endl;
107  assert( floatcheck(eval, evcs[ival]) );
108  DVec vars = { vals[ival] };
109  double eval2 = rpf.eval(vars);
110  assert( floatcheck(eval2, evcs[ival]) );
111  }
112 
113  cout << myname << line << endl;
114  cout << myname << "Set default eval." << endl;
115  double def = rpf.defaultEval();
116  assert( floatcheck(def, 0.0 ) );
117  assert( rpf.setDefaultEval(99.0) == 0 );
118  def = rpf.defaultEval();
119  assert( floatcheck(def, 99.0) );
120 
121  cout << myname << line << endl;
122  cout << myname << "Reset pars." << endl;
123  assert( rpf.ready() );
124  assert( floatcheck(rpf.eval(0.0), 10.0) );
125  assert( rpf.unsetParValues() == 0 );
126  assert( ! rpf.ready() );
127  assert( floatcheck(rpf.eval(0.0), 99.0) );
128  rpf.setParValue("offset", 10.0);
129  assert( ! rpf.ready() );
130  rpf.setParValue("scale", 3.0);
131  assert( rpf.ready() );
132  assert( floatcheck(rpf.eval(0.0), 10.0) );
133 
134  cout << myname << line << endl;
135  cout << myname << "Done." << endl;
136 
137  return 0;
138 }
139 
140 //**********************************************************************
141 
142 int main() {
143  return test_RootParFormula();
144 }
145 
146 //**********************************************************************
int main()
Name name() const override
std::string string
Definition: nybbler.cc:12
struct vector vector
int test_RootParFormula()
int setParValue(Name parnam, Value parval) override
Names setPars() const override
Index nvar() const override
unsigned int Index
Index npar() const override
bool floatcheck(double x1, double x2)
double eval(const Values &vars) const override
Value defaultEval() const override
Name formulaString() const override
std::vector< double > DVec
Names pars() const override
int unsetParValues() override
Names unsetPars() const override
Q_EXPORT QTSManip setw(int w)
Definition: qtextstream.h:331
bool ready() const override
Names resetPars() const override
bool isPar(Name parnam) const override
void line(double t, double *p, double &x, double &y, double &z)
int setDefaultEval(Value val) override
QTextStream & endl(QTextStream &s)