test_GausStepFitter.cxx
Go to the documentation of this file.
1 // test_GausStepFitter.cxx
2 
3 // David Adams
4 // December 2018.
5 //
6 // This is a test of GausStepFitter.
7 
8 #undef NDEBUG
9 
10 #include "../GausStepFitter.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_GausStepFitter(double satfrac) {
34  const string myname = "test_GausStepFitter: ";
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  GausStepFitter gsf(50, 3.0, 20, "ff");
64  assert( gsf.fit(ph) == 0 );
65  TF1* pffit = ph->GetFunction("ff");
66  if ( pffit == nullptr ) {
67  cout << myname << " Fit function not found!" << endl;
68  ph->GetListOfFunctions()->Print();
69  } else {
70  cout << myname << " Fit mean: " << pffit->GetParameter("Mean") << endl;
71  cout << myname << " Fit sigma: " << pffit->GetParameter("Sigma") << endl;
72  }
73 
74  cout << myname << line << endl;
75  TCanvas* pcan = new TCanvas;
76  ph->Draw();
77  ostringstream ssfnam;
78  static int ncan = 0;
79  ssfnam << "gausstep" << ncan << ".png";
80  ++ncan;
81  string fnam = ssfnam.str();
82  cout << myname << "Writing " << fnam << endl;
83  pcan->Print(fnam.c_str());
84  delete pcan;
85 
86  return 0;
87 }
88 
89 //**********************************************************************
90 
91 int main(int narg, const char* argc[]) {
92  DoubleVector satfracs = {0.0, 0.1, 0.2, 0.5};
93  if ( narg > 1 ) {
94  string sarg = argc[1];
95  if ( sarg == "-h" ) {
96  cout << "Usage: " << argc[0] << " [F1 F2 ...]" << endl;
97  return 0;
98  }
99  satfracs.clear();
100  for ( int iarg=1; iarg<narg; ++iarg ) {
101  double satfrac = 0.0;
102  istringstream ssin(argc[iarg]);
103  ssin >> satfrac;
104  satfracs.push_back(satfrac);
105  }
106  }
107  int err = 0;
108  for ( double satfrac : satfracs ) err += test_GausStepFitter(satfrac);
109  return err;
110 }
111 
112 //**********************************************************************
int main(int narg, const char *argc[])
std::string string
Definition: nybbler.cc:12
TF1 * gausTF1(double heightIn, double meanIn, double sigmaIn, std::string fname)
Definition: gausTF1.cxx:28
std::vector< double > DoubleVector
Definition: fcldump.cxx:27
int fit(TH1 *ph) const
void err(const char *fmt,...)
Definition: message.cpp:226
Q_EXPORT QTSManip setw(int w)
Definition: qtextstream.h:331
void line(double t, double *p, double &x, double &y, double &z)
int test_GausStepFitter(double satfrac)
QTextStream & endl(QTextStream &s)