test_shiftHistFit.cxx
Go to the documentation of this file.
1 // test_shiftHistFit.cxx
2 
3 // David Adams
4 // May 2021
5 //
6 // This is a test and demonstration for shiftHistFit.
7 
8 #undef NDEBUG
9 
13 #include <string>
14 #include <iostream>
15 #include <sstream>
16 #include <iomanip>
17 #include <vector>
18 #include <cassert>
19 #include "TH1F.h"
20 #include "TF1.h"
21 #include "TRandom.h"
22 
23 using std::string;
24 using std::cout;
25 using std::endl;
26 using std::setw;
27 using std::vector;
28 using std::istringstream;
29 
30 using Index = unsigned int;
31 
32 //**********************************************************************
33 
34 int test_shiftHistFit(double xshift) {
35  const string myname = "test_shiftHistFit: ";
36  cout << myname << "Starting test" << endl;
37  cout << myname << "Shift is " << xshift << endl;
38 #ifdef NDEBUG
39  cout << myname << "NDEBUG must be off." << endl;
40  abort();
41 #endif
42  string line = "-----------------------------";
43  string scfg;
44 
45  cout << myname << line << endl;
46  cout << "Testing shifHistFit." << endl;
47 
48  cout << myname << line << endl;
49  cout << "Build histogram" << endl;
50  // Truth parameters before shift.
51  double x0 = 50.0;
52  double sig0 = 10.0;
53  // Histogram range.
54  double hx1 = x0 + xshift - 50.0;
55  double hx2 = x0 + xshift + 50.0;
56  // Fit range.
57  double fx1 = x0 + xshift - 25.0;
58  double fx2 = x0 + xshift + 25.0;
59  // Fill histgram with data distributed around shifted truth.
60  TH1* ph = new TH1F("hgau", "My gaus", 100, hx1, hx2);
61  for ( int ient=0; ient<1000; ++ient ) {
62  ph->Fill(gRandom->Gaus(x0 + xshift, sig0));
63  }
64 
65  cout << myname << line << endl;
66  cout << "Build function." << endl;
67  TF1* pf = gausTF1(10.0, x0 + xshift + 0.3*sig0, 0.8*sig0, "mygaus");
68  pf->SetRange(fx1, fx2);
69  pf->Print();
70 
71  //ph->Fit(pf);
72  shiftHistFit(ph, pf, "R", "Mean", xshift);
73  pf->Print();
74 
75  TPadManipulator man;
76  man.add(ph, "hist");
77  man.add(pf, "same");
78  man.addAxis();
79  string fnout = "test_shiftHistFit.png";
80  cout << myname << "Printing " << fnout << endl;
81  man.print(fnout);
82 
83  return 0;
84 }
85 
86 //**********************************************************************
87 
88 int main(int narg, const char* argc[]) {
89  double xshift = 1000.0;
90  if ( narg > 1 ) {
91  istringstream ssin(argc[1]);
92  ssin >> xshift;
93  }
94  return test_shiftHistFit(xshift);
95 }
96 
97 //**********************************************************************
int test_shiftHistFit(double xshift)
int add(unsigned int ipad, TObject *pobj, std::string sopt="", bool replace=false)
std::string string
Definition: nybbler.cc:12
struct vector vector
int shiftHistFit(TH1 *ph, TF1 *pf, std::string fopt, int ipar, double xshift)
unsigned int Index
TF1 * gausTF1(double heightIn, double meanIn, double sigmaIn, std::string fname)
Definition: gausTF1.cxx:28
int addAxis(bool flag=true)
Q_EXPORT QTSManip setw(int w)
Definition: qtextstream.h:331
void line(double t, double *p, double &x, double &y, double &z)
int main(int narg, const char *argc[])
int print(std::string fname, std::string spat="{,}")
QTextStream & endl(QTextStream &s)