test_fft_speed.cxx
Go to the documentation of this file.
2 #include "WireCellUtil/Units.h"
4 #include "WireCellUtil/Array.h"
5 
6 #include "TCanvas.h"
7 #include "TH1F.h"
8 #include "TLine.h"
9 #include "TText.h"
10 #include "TGraph.h"
11 
12 #include <iostream>
13 #include <algorithm>
14 #include <chrono>
15 #include <complex>
16 
17 #include<fstream>
18 #include<sstream>
19 
20 using namespace std;
21 using namespace WireCell;
22 
23 // The preferred display units for gain.
24 const double GUnit = units::mV/units::fC;
25 
26 int main(int argc, char** argv)
27 {
28  int nInputs = 0;
29  int nBegin = 0;
30  int nEnd = 0;
31  int id = 0;
32 
33  for(int i=1; i<argc; i++) {
34  if( strcmp(argv[i],"-n")==0 ) {
35  stringstream convert( argv[i+1] );
36  if( !( convert>>nInputs ) ) {
37  cerr<<" ---> Error Scan !"<<endl;
38  exit(0);
39  }
40  }
41 
42  if( strcmp(argv[i],"-b")==0 ) {
43  stringstream convert( argv[i+1] );
44  if( !( convert>>nBegin ) ) {
45  cerr<<" ---> Error Scan !"<<endl;
46  exit(0);
47  }
48  }
49 
50  if( strcmp(argv[i],"-e")==0 ) {
51  stringstream convert( argv[i+1] );
52  if( !( convert>>nEnd ) ) {
53  cerr<<" ---> Error Scan !"<<endl;
54  exit(0);
55  }
56  }
57 
58  if( strcmp(argv[i],"-id")==0 ) {
59  stringstream convert( argv[i+1] );
60  if( !( convert>>id ) ) {
61  cerr<<" ---> Error Scan !"<<endl;
62  exit(0);
63  }
64  }
65 
66  }
67 
68  if( nInputs==0 || nBegin==0 || nEnd==0 || id==0) {
69  cerr<<endl<<" Usage: "<<endl
70  <<"\t"<<"./test_scan_fft -id ID_Num -n NumA -b NumB -e NumC"<<endl<<endl;
71  }
72 
73  cout<<TString::Format(" ---> nInputs %d, %d, %d", nInputs, nBegin, nEnd)<<endl;
74 
75 
76  const std::vector<double> gains = {7.8*GUnit, 14.0*GUnit};
77  const std::vector<double> shapings = {1.0*units::us, 2.0*units::us};
78 
79  const double maxtime = 100.0*units::us;
80  const double tick = 0.5*units::us;
81  const Binning tbins(maxtime/tick, 0, maxtime);
82  const int nticks = tbins.nbins();
83  cerr << "Using nticks=" << nticks << endl;
84 
85  // do timing tests
86  {
87  cout<<endl<<" nSamples, fwd_time, fwd_time per sample, rev_time, rev_time per sample, average time of (fwd and rev)"<<endl<<endl;
88 
89  ofstream output(TString::Format("%s_%02d.txt", argv[0], id), ios::out|ios::trunc);
90 
91  std::vector<int> nsampleslist;
92 
93  for(int i=0; i<nInputs; i++) {
94  int nsamples = nBegin + i;
95  if( nsamples>nEnd ) break;
96  nsampleslist.push_back( nsamples );
97  }
98 
99  const int ntries = 1000;
100  for (auto nsamps : nsampleslist) {
101  // Response::ColdElec ce(gains[1], shapings[1]);
102  // const Binning bins(nsamps, 0, maxtime);
103  // Waveform::realseq_t res = ce.generate(bins);
104  // Waveform::compseq_t spec;
105  Array::array_xxc test_array = Array::array_xxc::Zero(ntries,nsamps);
106  for (int i=0;i!=ntries;i++){
107  for (int j=0;j!=nsamps;j++){
108  test_array(i,j) = std::complex<float> (1.1,1.3);
109  }
110  }
111 
112  double fwd_time = 0.0;
113  // for (int itry=0; itry<ntries; ++itry) {
114  // auto t1 = std::chrono::high_resolution_clock::now();
115  // spec = Waveform::dft(res);
116  // auto t2 = std::chrono::high_resolution_clock::now();
117  // fwd_time += std::chrono::duration_cast<std::chrono::nanoseconds>(t2-t1).count();
118  // }
119  // fwd_time /= ntries;
120 
122  Array::dft_cc(test_array,0);
124  fwd_time = std::chrono::duration_cast<std::chrono::nanoseconds>(t2-t1).count()/ntries;
125 
126  double rev_time = 0.0;
127  // for (int itry=0; itry<ntries; ++itry) {
129  // res = Waveform::idft(spec);
130  Array::idft_cc(test_array,0);
132  rev_time = std::chrono::duration_cast<std::chrono::nanoseconds>(t4-t3).count()/ntries;
133  // }
134  // rev_time /= ntries;
135 
136  // cout<<TString::Format(" DFT nsamples= %5d, tforward: %7.2f us, %9.6f us/sample, treverse: %7.2f us, %9.6f us/sample, taverage: %7.2f us",
137  // nsamps,
138  // fwd_time/1000., fwd_time/nsamps/1000.,
139  // rev_time/1000., rev_time/nsamps/1000.,
140  // 0.5*(fwd_time+rev_time)/1000.
141  // )<<endl;
142 
143  output<<TString::Format("%5d %7.2f %9.6f %7.2f %9.6f %7.2f",
144  nsamps,
145  fwd_time/1000., fwd_time/nsamps/1000.,
146  rev_time/1000., rev_time/nsamps/1000.,
147  0.5*(fwd_time+rev_time)/1000.
148  )<<endl;
149 
150  }// for (auto nsamps : nsampleslist) {
151 
152  output.close();
153  }
154 
155  cout<<endl<<" Complete "<<endl<<endl;
156 
157 }// main
array_xxc dft_cc(const array_xxc &arr, int dim=1)
Definition: Array.cxx:67
Eigen::ArrayXXcf array_xxc
A complex, 2D array.
Definition: Array.h:57
STL namespace.
const double tick
const int nticks
Binning tbins(nticks, t0, t0+readout_time)
array_xxc idft_cc(const array_xxc &arr, int dim=1)
Definition: Array.cxx:125
float fC
Definition: units.py:113
def convert(inputfile, outputfile="wire-cell-garfield-fine-response.json.bz2", average=False, shaped=False)
Definition: garfield.py:262
int main(int argc, char **argv)
const double GUnit
Definition: Main.h:22
static const double us
Definition: Units.h:101
int strcmp(const String &s1, const String &s2)
Definition: relates.cpp:14
void Format(TGraph *gr, int lcol, int lsty, int lwid, int mcol, int msty, double msiz)
Definition: Style.cxx:154
int nbins() const
Definition: Binning.h:42
QTextStream & endl(QTextStream &s)
nanosecond nanoseconds
Alias for common language habits.
Definition: spacetime.h:134