test_StickyCodeMetrics.cxx
Go to the documentation of this file.
1 // test_StickyCodeMetrics.cxx
2 //
3 // David Adams
4 // April 2017
5 //
6 // Test StickyCodeMetrics.
7 
8 #include <string>
9 #include <iostream>
10 #include <fstream>
11 #include <vector>
13 #include "TH1F.h"
14 #include "TCanvas.h"
15 
16 #undef NDEBUG
17 #include <cassert>
18 
19 using std::string;
20 using std::cout;
21 using std::endl;
22 using std::ofstream;
23 using std::vector;
24 
25 using Index = unsigned int;
27 
28 //**********************************************************************
29 
31  const string myname = "test_StickyCodeMetrics: ";
32 #ifdef NDEBUG
33  cout << myname << "NDEBUG must be off." << endl;
34  abort();
35 #endif
36  string line = "-----------------------------";
37 
38  cout << myname << line << endl;
39 
40  cout << myname << line << endl;
41  cout << myname << "Create data." << endl;
43  counts[111] = 3;
44  counts[112] = 6;
45  counts[113] = 20;
46  counts[114] = 72;
47  counts[115] = 85;
48  counts[116] = 90;
49  counts[117] = 86;
50  counts[118] = 77;
51  counts[119] = 35;
52  counts[120] = 18;
53  counts[121] = 7;
54  counts[122] = 4;
55  counts[123] = 1;
56  counts[127] = 20;
57  counts[128] = 40;
58  counts[129] = 4;
59  AdcCountVector vals;
60  TH1F* ph = new TH1F("hraw", "Input ADC distribution; ADC count; # entries", 4096, 0, 4096);
61  TH1F* ph2 = new TH1F("hraw2", "Input ADC distribution; ADC count; # entries", 30, 100, 130);
62  Index nadc = 0;
63  for ( BinCounter::value_type icnt : counts ) {
64  AdcCount iadc = icnt.first;
65  Index nval = icnt.second;
66  nadc += nval;
67  for ( Index ival=0; ival<nval; ++ival ) {
68  vals.push_back(iadc);
69  ph->Fill(iadc);
70  ph2->Fill(iadc);
71  }
72  }
73  cout << myname << " Bin counter count: " << nadc << endl;
74  cout << myname << " Vector count: " << vals.size() << endl;
75  cout << myname << " Histogram count: " << ph->GetEntries() << endl;
76  assert( vals.size() == nadc );
77  assert( ph->GetEntries() == nadc );
78 
80 
81  cout << myname << line << endl;
82  cout << myname << "Bin Counter evaluation." << endl;
83  scm.evaluate(counts);
84  scm.print();
85  assert( scm.getHist() == nullptr );
86  assert( ! scm.getSharedHist() );
87 
88  cout << myname << line << endl;
89  cout << myname << "ADC samples evaluation." << endl;
90  scm.evaluate(vals);
91  scm.print();
92 
93  cout << myname << line << endl;
94  cout << myname << "Histogram evaluation." << endl;
95  scm.evaluate(ph);
96  scm.print();
97 
98  cout << myname << line << endl;
99  cout << myname << "Limited-range histogram evaluation." << endl;
100  scm.evaluate(ph2);
101  scm.print();
102 
103  cout << myname << line << endl;
104  cout << myname << "Bin Counter data map." << endl;
105  scm.getMetrics().print();
106 
107  TCanvas* pcan = new TCanvas;
108  pcan->SetGridx();
109 
110  cout << myname << line << endl;
111  cout << myname << "Evaluation creating histogram." << endl;
112  StickyCodeMetrics scmh("hadctest", "ADC spectrum for test", 50, 10, 1, 20);
113  assert( scmh.evaluate(counts) == 0 );
114  scmh.print();
115  scmh.getMetrics().print();
116  assert( scmh.getHist() != nullptr );
117  scmh.getHist()->Print();
118  cout << myname << "Histogram integral: " << scmh.getHist()->Integral() << endl;
119  scmh.getHist()->Draw();
120  pcan->Print("test.png");
121  assert( int(scmh.getHist()->Integral()+0.1) == int(nadc) );
122 
123  cout << myname << line << endl;
124  cout << myname << "Evaluation creating wide histogram." << endl;
125  StickyCodeMetrics scmhw("hadctest", "ADC spectrum for test", 100, 10, 1, 20);
126  assert( scmhw.evaluate(counts) == 0 );
127  scmhw.print();
128  scmhw.getMetrics().print();
129  assert( scmhw.getHist() != nullptr );
130  scmhw.getHist()->Print();
131  cout << myname << "Histogram integral: " << scmhw.getHist()->Integral() << endl;
132  scmhw.getHist()->Draw();
133  pcan->Print("testw.png");
134  assert( int(scmhw.getHist()->Integral()+0.1) == int(nadc) );
135 
136  cout << myname << line << endl;
137  cout << myname << "Evaluation creating narrow histogram." << endl;
138  StickyCodeMetrics scmh2("hadctest", "ADC spectrum for test", 15, 5, 1, 20);
139  assert( scmh2.evaluate(counts) == 0 );
140  scmh2.print();
141  scmh2.getMetrics().print();
142  assert( scmh2.getHist() != nullptr );
143  scmh2.getHist()->Print();
144  cout << myname << "Histogram integral: " << scmh2.getHist()->Integral() << endl;
145  cout << myname << "Histogram undrflow: " << scmh2.getHist()->GetBinContent(16) << endl;
146  cout << myname << "Histogram overflow: " << scmh2.getHist()->GetBinContent(0) << endl;
147  scmh2.getHist()->Draw();
148  pcan->Print("test2.png");
149  Index nadc2 = 0;
150  Index nadc2u = 0;
151  Index nadc2o = 0;
152  for ( int iadc=110; iadc<125; ++iadc ) {
153  if ( counts.find(iadc) != counts.end() ) nadc2 += counts[iadc];
154  }
155  for ( int iadc=100; iadc<110; ++iadc ) {
156  if ( counts.find(iadc) != counts.end() ) nadc2u += counts[iadc];
157  }
158  for ( int iadc=125; iadc<135; ++iadc ) {
159  if ( counts.find(iadc) != counts.end() ) nadc2o += counts[iadc];
160  }
161  cout << myname << "Expect integral: " << nadc2 << endl;
162  cout << myname << "Expect undrflow: " << nadc2u << endl;
163  cout << myname << "Expect overflow: " << nadc2o << endl;
164  assert( int(scmh2.getHist()->Integral()+0.1) == int(nadc2) );
165 
166  delete pcan;
167 
168  cout << myname << line << endl;
169  cout << myname << "Done." << endl;
170  return 0;
171 }
172 
173 //**********************************************************************
174 
175 int main(int argc, char* argv[]) {
176  if ( argc > 1 ) {
177  string sarg(argv[1]);
178  if ( sarg == "-h" ) {
179  cout << "Usage: " << argv[0] << endl;
180  return 0;
181  }
182  }
183  return test_StickyCodeMetrics();
184 }
185 
186 //**********************************************************************
std::vector< AdcCount > AdcCountVector
Definition: AdcTypes.h:19
DataMap getMetrics(std::string prefix="scm") const
std::string string
Definition: nybbler.cc:12
struct vector vector
unsigned int Index
void print(std::ostream *pout) const
Definition: DataMap.h:245
counts_as<> counts
Number of ADC counts, represented by signed short int.
Definition: electronics.h:116
int main(int argc, char *argv[])
int test_StickyCodeMetrics()
void line(double t, double *p, double &x, double &y, double &z)
std::map< Index, double > BinCounter
short AdcCount
Definition: AdcTypes.h:18
void print(std::string prefix="") const
int evaluate(const BinCounter &counts)
StickyCodeMetrics::BinCounter BinCounter
QTextStream & endl(QTextStream &s)