test_ExpTailRemover.cxx
Go to the documentation of this file.
1 // test_ExpTailRemover.cxx
2 //
3 // David Adams
4 // April 2019
5 //
6 // Test ExpTailRemover.
7 
8 #include <string>
9 #include <iostream>
10 #include <fstream>
11 #include <sstream>
12 #include <iomanip>
16 #include "TRandom.h"
17 
18 #undef NDEBUG
19 #include <cassert>
20 
21 using std::string;
22 using std::cout;
23 using std::endl;
24 using std::ofstream;
25 using std::istringstream;
26 using std::setw;
28 
29 using Index = unsigned int;
30 using IndexVector = std::vector<Index>;
32 
33 //**********************************************************************
34 
35 namespace {
36 
37 struct SigStats {
38  Index count = 0;
39  float mean = 0.0;
40  float rms = 0.0;
41  SigStats(const AdcChannelData& acd, string lab);
42 };
43 
44 SigStats::SigStats(const AdcChannelData& acd, string lab) {
45  Index nSig = 0;
46  double sumSig = 0.0;
47  double sumSig2 = 0.0;
48  Index nsam = acd.samples.size();
49  if ( acd.signal.size() < nsam ) return;
50  bool dbg = false;
51  for ( Index isam=0; isam<nsam; ++isam ) {
52  if ( acd.signal[isam] ) continue;
53  ++nSig;
54  double sig = acd.samples[isam];
55  sumSig += sig;
56  sumSig2 += sig*sig;
57  if ( dbg ) cout << lab << setw(5) << isam << ":" << setw(10) << sig << endl;
58  }
59  if ( nSig == 0 ) return;
60  count = nSig;
61  mean = sumSig/nSig;
62  rms = sqrt(sumSig2/nSig - mean*mean);
63  if ( lab.size() ) {
64  cout << lab << " # signal: " << count << endl;
65  cout << lab << " mean: " << mean << endl;
66  cout << lab << " RMS: " << rms << endl;
67  }
68 }
69 
70 } // end unnamed namespace
71 
72 //**********************************************************************
73 
74 int test_ExpTailRemover(bool useExistingFcl, Index flag, float noiseSigma, bool setSeed) {
75  const string myname = "test_ExpTailRemover: ";
76 #ifdef NDEBUG
77  cout << myname << "NDEBUG must be off." << endl;
78  abort();
79 #endif
80  string line = "-----------------------------";
81 
82  cout << myname << line << endl;
83  string fclfile = "test_ExpTailRemover.fcl";
84  float decayTime = 100.0;
85  float ped = 5.0;
86  float tail0 = -15.0;
87  if ( ! useExistingFcl ) {
88  cout << myname << "Creating top-level FCL." << endl;
89  ofstream fout(fclfile.c_str());
90  fout << "tools: {" << endl;
91  fout << " sigfind: {" << endl;
92  fout << " tool_type: AdcThresholdSignalFinder" << endl;
93  fout << " LogLevel: 1" << endl;
94  fout << " Threshold: 10" << endl;
95  fout << " BinsAfter: 10" << endl;
96  fout << " BinsBefore: 5" << endl;
97  fout << " FlagPositive: true" << endl;
98  fout << " FlagNegative: true" << endl;
99  fout << " }" << endl;
100  fout << " mytool: {" << endl;
101  fout << " tool_type: ExpTailRemover" << endl;
102  fout << " LogLevel: 3" << endl;
103  fout << " SignalFlag: " << flag << endl;
104  fout << " SignalIterationLimit: 10" << endl;
105  fout << " SignalTool: \"sigfind\"" << endl;
106  fout << " DecayTime: " << decayTime << endl;
107  fout << " IncludeChannelRanges: [\"all\"]" << endl;
108  fout << " ExcludeChannelRanges: []" << endl;
109  fout << " }" << endl;
110  fout << "}" << endl;
111  fout.close();
112  } else {
113  cout << myname << "Using existing top-level FCL." << endl;
114  }
115 
116  cout << myname << line << endl;
117  cout << myname << "Fetching tool manager." << endl;
119  assert ( ptm != nullptr );
120  DuneToolManager& tm = *ptm;
121  tm.print();
122  assert( tm.toolNames().size() == 2 );
123 
124  cout << myname << line << endl;
125  cout << myname << "Fetching tool." << endl;
126  auto ptoo = tm.getPrivate<TpcDataTool>("mytool");
127  assert( ptoo != nullptr );
128 
129  cout << myname << "Create signals." << endl;
130  Index nsam = 300;
131  FloatVector pulse = { 0.1, 4.5, 15.2, 66.4, 94.3, 100.0, 96.5, 88.4, 72.6, 58.4,
132  42.3, 35.1, 26.0, 18.6, 12.6, 8.8, 6.9, 4.4, 2.0, 0.3 };
133  Index npul = pulse.size();
134  FloatVector sigs1(nsam, 0.0);
135  AdcFilterVector isSignal(nsam, false);
136  IndexVector peakPoss = {10, 100, 115, 230};
137  FloatVector peakAmps = {0.5, 2.0, 0.7, 1.0};
138  Index npea = peakPoss.size();
139  for ( Index ipea=0; ipea<npea; ++ipea ) {
140  Index iposPeak = peakPoss[ipea];
141  float norm = peakAmps[ipea];
142  for ( Index ipul=0; ipul<npul; ++ipul ) {
143  Index isam = iposPeak + ipul;
144  if ( isam >= nsam ) break;
145  sigs1[isam] += norm*pulse[ipul];
146  isSignal[isam] = true;
147  }
148  }
149 
150  cout << myname << line << endl;
151  cout << myname << "Add noise to the signal." << endl;
152  if ( setSeed ) gRandom->SetSeed();
153  for ( float& sig : sigs1 ) sig += gRandom->Gaus(0.0, noiseSigma);
154 
155  cout << myname << "Create sample tailer." << endl;
156  SampleTailer sta(decayTime);
157  sta.setPedestal(ped);
158  sta.setTail0(tail0);
159  sta.setUnit("ADC count");
160  cout << myname << " decayTime: " << sta.decayTime() << endl;
161  cout << myname << " beta: " << sta.beta() << endl;
162  cout << myname << " alpha: " << sta.alpha() << endl;
163  cout << myname << " pedestal: " << sta.pedestal() << endl;
164  cout << myname << " tail0: " << sta.tail0() << endl;
165 
166  cout << myname << line << endl;
167  cout << myname << "Add pedestal and tail to the signal to create data." << endl;
168  assert( sta.setSignal(sigs1) == 0 );
169 
170  cout << myname << line << endl;
171  cout << myname << "Create channel data." << endl;
172  AdcChannelData acd;
173  acd.setEventInfo(123, 456);
174  acd.setChannelInfo(789);
175  acd.pedestal = 1000.0;
176  acd.samples = sta.data();
177  acd.signal = isSignal;
178 
179  cout << myname << line << endl;
180  cout << myname << "Check input noise." << endl;
181  SigStats inStats(acd, myname);
182  assert( inStats.count > 0 );
183 
184  cout << myname << line << endl;
185  cout << myname << "Use tool to remove tail from data." << endl;
186  DataMap res = ptoo->update(acd);
187  res.print();
188  assert ( res == 0 );
189 
190  cout << myname << line << endl;
191  cout << myname << "Check output noise." << endl;
192  SigStats ouStats(acd, myname);
193  assert( ouStats.count > 0 );
194 
195  cout << myname << "Done." << endl;
196  return 0;
197 }
198 
199 //**********************************************************************
200 
201 int main(int argc, char* argv[]) {
202  bool useExistingFcl = false;
203  Index flag = 2;
204  float noiseSigma = 2.0;
205  bool setSeed = false;
206  if ( argc > 1 ) {
207  string sarg(argv[1]);
208  if ( sarg == "-h" ) {
209  cout << "Usage: " << argv[0] << " [ARG [OPT [noise [setSeed]]]]" << endl;
210  cout << " If ARG = true, existing FCL file is used." << endl;
211  cout << " OPT [2] is SignalOption = 0, 1, 2, or 3" << endl;
212  cout << " noise [2.0] is the sigma of the noise added to the data" << endl;
213  cout << " setSeed nonzero means a random random seed for the noise" << endl;
214  return 0;
215  }
216  useExistingFcl = sarg == "true" || sarg == "1";
217  }
218  if ( argc > 2 ) {
219  string sarg(argv[2]);
220  flag = std::stoi(sarg);
221  }
222  if ( argc > 3 ) {
223  string sarg(argv[3]);
224  istringstream ssarg(sarg);
225  ssarg >> noiseSigma;
226  }
227  if ( argc > 4 ) {
228  string sarg(argv[3]);
229  setSeed = sarg != "0";
230  }
231  return test_ExpTailRemover(useExistingFcl, flag, noiseSigma, setSeed);
232 }
233 
234 //**********************************************************************
std::vector< Index > IndexVector
int main(int argc, char *argv[])
double rms(sqlite3 *db, std::string const &table_name, std::string const &column_name)
Definition: statistics.cc:40
bool dbg
const std::vector< std::string > & toolNames() const
std::string string
Definition: nybbler.cc:12
int setSignal(const FloatVector &inSignal)
void print() const
unsigned int Index
void print(std::ostream *pout) const
Definition: DataMap.h:245
tm
Definition: demo.py:21
int setPedestal(float val)
void setChannelInfo(ChannelInfoPtr pchi)
const FloatVector & data() const
Definition: SampleTailer.h:88
float decayTime() const
Definition: SampleTailer.h:83
float beta() const
Definition: SampleTailer.h:84
void setEventInfo(EventInfoPtr pevi)
int test_ExpTailRemover(bool useExistingFcl, Index flag, float noiseSigma, bool setSeed)
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
std::unique_ptr< T > getPrivate(std::string name)
AdcSignal pedestal
AdcFilterVector signal
std::vector< bool > AdcFilterVector
Definition: AdcTypes.h:27
void line(double t, double *p, double &x, double &y, double &z)
std::vector< AdcSignal > AdcSignalVector
Definition: AdcTypes.h:22
float pedestal() const
Definition: SampleTailer.h:86
float alpha() const
Definition: SampleTailer.h:85
Dft::FloatVector FloatVector
double mean(sqlite3 *db, std::string const &table_name, std::string const &column_name)
Definition: statistics.cc:16
float tail0() const
Definition: SampleTailer.h:87
static DuneToolManager * instance(std::string fclname="", int dbg=1)
AdcSignalVector samples
QTextStream & endl(QTextStream &s)
int setUnit(Name val)
Definition: SampleTailer.h:68