test_SampleTailer.cxx
Go to the documentation of this file.
1 // test_SampleTailer.cxx
2 
3 // David Adams
4 // April 2019
5 //
6 // This is a test and demonstration for SampleTailer.
7 
8 #undef NDEBUG
9 
10 #include "../SampleTailer.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 #include "TLine.h"
19 
20 using std::string;
21 using std::cout;
22 using std::endl;
23 using std::setw;
24 using std::vector;
25 
27 using Index = unsigned int;
28 using IndexVector = std::vector<Index>;
29 
30 //**********************************************************************
31 
32 namespace {
33 
34 string line() {
35  return "-----------------------------";
36 }
37 
38 void drawResults(const SampleTailer& sta, string httl, string fnam) {
39  const string myname = "drawResults: ";
40  cout << myname << line() << endl;
41  cout << myname << "Draw data." << endl;
42  TH1* phd = sta.dataHist();
43  TH1* phs = sta.signalHist();
44  TH1* pht = sta.tailHist();
45  assert( phd != nullptr );
46  assert( phs != nullptr );
47  assert( pht != nullptr );
48  TCanvas* pcan = new TCanvas("mycan", "mycan", 700, 500);
49  pcan->SetGrid();
50  phd->SetLineColor(1);
51  phs->SetLineColor(2);
52  pht->SetLineColor(9);
53  TH1* pha = dynamic_cast<TH1*>(phd->Clone("hax"));
54  pha->SetDirectory(nullptr);
55  pha->GetYaxis()->SetRangeUser(-50, 220);
56  pha->Clear();
57  pha->SetTitle(httl.c_str());
58  pha->Draw("");
59  TLine* plin = new TLine(0, 0, sta.size(), 0);
60  plin->Draw();
61  phd->Draw("SAME");
62  pht->Draw("SAME");
63  phs->Draw("SAME");
64  pcan->Print(fnam.c_str());
65 }
66 
67 void printResults(const SampleTailer& sta) {
68  const string myname = "printResults: ";
69  cout << myname << line() << endl;
70  cout << myname << "Display results." << endl;
71  cout << myname << " i data signal tail" << endl;
72  for ( Index isam=0; isam<sta.size(); ++isam ) {
73  cout << myname << setw(5) << isam << ":";
74  cout << setw(10) << std::fixed << std::setprecision(2) << sta.data(isam);
75  cout << setw(10) << std::fixed << std::setprecision(2) << sta.signal(isam);
76  cout << setw(10) << std::fixed << std::setprecision(2) << sta.tail(isam);
77  cout << endl;
78  }
79 }
80 
81 } // end unnamed namespace
82 
83 //**********************************************************************
84 
85 int test_SampleTailer(bool useVector) {
86  const string myname = "test_SampleTailer: ";
87  cout << myname << "Starting test" << endl;
88 #ifdef NDEBUG
89  cout << myname << "NDEBUG must be off." << endl;
90  abort();
91 #endif
92  string scfg;
93 
94  cout << myname << "Create signals." << endl;
95  Index nsam = 300;
96  FloatVector pulse = { 0.1, 4.5, 15.2, 66.4, 94.3, 100.0, 96.5, 88.4, 72.6, 58.4,
97  42.3, 35.1, 26.0, 18.6, 12.6, 8.8, 6.9, 4.4, 2.0, 0.3 };
98  Index npul = pulse.size();
99  FloatVector sigs1(nsam, 0.0);
100  IndexVector peakPoss = {10, 100, 115, 230};
101  FloatVector peakAmps = {0.5, 2.0, 0.7, 1.0};
102  Index npea = peakPoss.size();
103  for ( Index ipea=0; ipea<npea; ++ipea ) {
104  Index iposPeak = peakPoss[ipea];
105  float norm = peakAmps[ipea];
106  for ( Index ipul=0; ipul<npul; ++ipul ) {
107  Index isam = iposPeak + ipul;
108  if ( isam >= nsam ) break;
109  sigs1[isam] += norm*pulse[ipul];
110  }
111  }
112 
113  cout << myname << "Create utility." << endl;
114  SampleTailer sta(100.0);
115  FloatVector peds(nsam);
116  for ( unsigned int isam=0; isam<peds.size(); ++isam ) {
117  peds[isam] = 5.0 + 0.1*isam;
118  }
119  if ( useVector ) {
120  sta.setPedestalVector(&peds);
121  } else {
122  sta.setPedestal(5.0);
123  }
124  sta.setTail0(-15.0);
125  sta.setUnit("ADC count");
126  cout << myname << " decayTime: " << sta.decayTime() << endl;
127  cout << myname << " beta: " << sta.beta() << endl;
128  cout << myname << " alpha: " << sta.alpha() << endl;
129  cout << myname << " pedestal: " << sta.pedestal() << endl;
130  cout << myname << " tail0: " << sta.tail0() << endl;
131 
132  cout << myname << line() << endl;
133  cout << myname << "Create data from signal." << endl;
134  assert( sta.setSignal(sigs1) == 0 );
135  assert( sta.data().size() == nsam );
136  assert( sta.signal().size() == nsam );
137  assert( sta.tail().size() == nsam );
138  FloatVector dats1 = sta.data();
139  sta.makeHists();
140  printResults(sta);
141  drawResults(sta, "Data, signal and tail from signal", "SampleTrailerFromSignal.png");
142 
143  cout << myname << line() << endl;
144  cout << myname << "Create signal from data." << endl;
145  sta.clear();
146  assert( sta.data().size() == 0 );
147  assert( sta.signal().size() == 0 );
148  assert( sta.tail().size() == 0 );
149  assert( sta.setData(dats1) == 0 );
150  assert( sta.data().size() == nsam );
151  assert( sta.signal().size() == nsam );
152  assert( sta.tail().size() == nsam );
153  FloatVector sigs2 = sta.signal();
154  sta.makeHists();
155  printResults(sta);
156  drawResults(sta, "Data, signal and tail from data", "SampleTrailerFromData.png");
157 
158  cout << myname << line() << endl;
159  cout << myname << "Compare new signal with original.";
160  cout << myname << " i sig1 sig2 diff" << endl;
161  assert( sigs1.size() == sigs2.size() );
162  Index nbad = 0;
163  for ( Index isam=0; isam<nsam; ++isam ) {
164  float dif = sigs2[isam] - sigs1[isam];
165  float adif = fabs(dif);
166  cout << myname << setw(5) << isam << ":";
167  cout << setw(10) << std::fixed << std::setprecision(2) << sigs1[isam];
168  cout << setw(10) << std::fixed << std::setprecision(2) << sigs2[isam];
169  cout << setw(10) << std::fixed << std::setprecision(2) << dif;
170  if ( adif > 1.e-3 ) {
171  ++nbad;
172  cout << " Bad";
173  }
174  cout << endl;
175  }
176  assert( nbad == 0 );
177 
178  return 0;
179 }
180 
181 //**********************************************************************
182 
183 int main(int argc, char**) {
184  bool useVector = argc > 1;
185  return test_SampleTailer(useVector);
186 }
187 
188 //**********************************************************************
std::vector< float > FloatVector
Definition: SampleTailer.h:42
std::vector< Index > IndexVector
int setData(const FloatVector &inData)
TH1 * signalHist() const
Definition: SampleTailer.h:97
std::string string
Definition: nybbler.cc:12
int setSignal(const FloatVector &inSignal)
struct vector vector
TH1 * dataHist() const
Definition: SampleTailer.h:96
unsigned int Index
Q_EXPORT QTSManip setprecision(int p)
Definition: qtextstream.h:343
int setPedestal(float val)
const FloatVector & data() const
Definition: SampleTailer.h:88
const double e
float decayTime() const
Definition: SampleTailer.h:83
const FloatVector & signal() const
Definition: SampleTailer.h:90
float beta() const
Definition: SampleTailer.h:84
int setTail0(float val)
auto norm(Vector const &v)
Return norm of the specified vector.
Q_EXPORT QTSManip setw(int w)
Definition: qtextstream.h:331
int setPedestalVector(const FloatVector *pval)
void line(double t, double *p, double &x, double &y, double &z)
float pedestal() const
Definition: SampleTailer.h:86
int main(int argc, char **)
float alpha() const
Definition: SampleTailer.h:85
Dft::FloatVector FloatVector
int test_SampleTailer(bool useVector)
const FloatVector & tail() const
Definition: SampleTailer.h:92
float tail0() const
Definition: SampleTailer.h:87
QTextStream & endl(QTextStream &s)
int setUnit(Name val)
Definition: SampleTailer.h:68
Index size() const
Definition: SampleTailer.h:94
TH1 * tailHist() const
Definition: SampleTailer.h:98