test_GausRmsFitter.cxx
Go to the documentation of this file.
1 // test_GausRmsFitter.cxx
2 
3 // David Adams
4 // December 2018.
5 //
6 // This is a test of GausRmsFitter.
7 
8 #undef NDEBUG
9 
10 #include "../GausRmsFitter.h"
11 #include "../gausTF1.h"
12 #include "TH1F.h"
13 #include "TF1.h"
14 #include "TCanvas.h"
15 
16 #include <string>
17 #include <iostream>
18 #include <sstream>
19 #include <iomanip>
20 #include <cassert>
21 #include <vector>
22 
23 using std::string;
24 using std::cout;
25 using std::endl;
26 using std::setw;
27 using std::ostringstream;
28 using std::istringstream;
29 using DoubleVector = std::vector<double>;
30 
31 //**********************************************************************
32 
33 int test_GausRmsFitter(int dbg, double satfrac) {
34  const string myname = "test_GausRmsFitter: ";
35  cout << myname << "Starting test" << endl;
36 #ifdef NDEBUG
37  cout << myname << "NDEBUG must be off." << endl;
38  abort();
39 #endif
40  string line = "-----------------------------";
41  string scfg;
42 
43  cout << myname << line << endl;
44  cout << myname << "Test data" << endl;
45  ostringstream ssttl;
46  ssttl << "Test data with frac " << satfrac << endl;
47  TH1* ph = new TH1F("hdat", ssttl.str().c_str(), 100, 0, 100);
48  ph->SetDirectory(nullptr);
49  TF1* pf1 = gausTF1(1.0, 50.0, 4.0, "f1");
50  cout << myname << "Created function " << pf1->GetName() << endl;
51  TF1* pf2 = gausTF1(1.0, 30.0, 4.0, "f2");
52  cout << myname << "Created function " << pf2->GetName() << endl;
53  int npt = 1000;
54  ph->FillRandom("f1", npt);
55  ph->FillRandom("f2", satfrac*npt);
56  ph->GetListOfFunctions()->Clear();
57  cout << myname << " Hist mean: " << ph->GetMean() << endl;
58  cout << myname << " Hist RMS: " << ph->GetRMS() << endl;
59  delete pf1;
60  delete pf2;
61 
62  cout << myname << line << endl;
63  GausRmsFitter gsf(4.0, 4.0, "ff");
64  if ( dbg > 0 ) gsf.setLogLevel(dbg);
65  assert( gsf.fit(ph, 50) == 0 );
66  TF1* pffit = ph->GetFunction("ff");
67  if ( pffit == nullptr ) {
68  cout << myname << " Fit function not found!" << endl;
69  ph->GetListOfFunctions()->Print();
70  } else {
71  double x1, x2;
72  pffit->GetRange(x1, x2);
73  double sum = pffit->Integral(x1, x2);
74  cout << myname << " Fit name: " << pffit->GetName() << endl;
75  cout << myname << " Fit mean: " << pffit->GetParameter("Mean") << endl;
76  cout << myname << " Fit sigma: " << pffit->GetParameter("Sigma") << endl;
77  cout << myname << " Fit range: (" << x1 << ", " << x2 << ")" << endl;
78  cout << myname << " Fit integral: " << sum << endl;
79  }
80 
81  cout << myname << line << endl;
82  TCanvas* pcan = new TCanvas;
83  ph->Draw();
84  ostringstream ssfnam;
85  static int ncan = 0;
86  ssfnam << "gausstep" << ncan << ".png";
87  ++ncan;
88  string fnam = ssfnam.str();
89  cout << myname << "Writing " << fnam << endl;
90  pcan->Print(fnam.c_str());
91  delete pcan;
92 
93  return 0;
94 }
95 
96 //**********************************************************************
97 
98 int main(int narg, const char* argc[]) {
99  int dbg = 0;
100  DoubleVector satfracs = {0.0, 0.1, 0.2, 0.5};
101  if ( narg > 1 ) {
102  string sarg = argc[1];
103  if ( sarg == "-h" ) {
104  cout << "Usage: " << argc[0] << " [DBG] [F1 F2 ...]" << endl;
105  return 0;
106  }
107  istringstream ssin(sarg);
108  ssin >> dbg;
109  }
110  if ( narg > 2 ) {
111  string sarg = argc[2];
112  satfracs.clear();
113  for ( int iarg=2; iarg<narg; ++iarg ) {
114  double satfrac = 0.0;
115  istringstream ssin(argc[iarg]);
116  ssin >> satfrac;
117  satfracs.push_back(satfrac);
118  }
119  }
120  int err = 0;
121  for ( double satfrac : satfracs ) err += test_GausRmsFitter(dbg, satfrac);
122  return err;
123 }
124 
125 //**********************************************************************
int main(int narg, const char *argc[])
bool dbg
std::string string
Definition: nybbler.cc:12
int test_GausRmsFitter(int dbg, double satfrac)
TF1 * gausTF1(double heightIn, double meanIn, double sigmaIn, std::string fname)
Definition: gausTF1.cxx:28
std::vector< double > DoubleVector
Definition: fcldump.cxx:27
void err(const char *fmt,...)
Definition: message.cpp:226
Q_EXPORT QTSManip setw(int w)
Definition: qtextstream.h:331
int fit(TH1 *ph, double mean0) const
void line(double t, double *p, double &x, double &y, double &z)
QTextStream & endl(QTextStream &s)
void setLogLevel(Index lev)
Definition: GausRmsFitter.h:55