test_AdcDataPlotter.cxx
Go to the documentation of this file.
1 // test_AdcDataPlotter.cxx
2 //
3 // David Adams
4 // April 2017
5 //
6 // Test AdcDataPlotter.
7 
8 #include <string>
9 #include <iostream>
10 #include <fstream>
11 #include <sstream>
12 #include <vector>
15 #include <TRandom.h>
16 
17 #undef NDEBUG
18 #include <cassert>
19 
20 using std::string;
21 using std::cout;
22 using std::endl;
23 using std::ostringstream;
24 using std::ofstream;
26 using std::vector;
27 
28 //**********************************************************************
29 
30 int test_AdcDataPlotter(bool useExistingFcl =false) {
31  const string myname = "test_AdcDataPlotter: ";
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  string fclfile = "test_AdcDataPlotter.fcl";
40  if ( ! useExistingFcl ) {
41  cout << myname << "Creating top-level FCL." << endl;
42  ofstream fout(fclfile.c_str());
43  fout << "#include \"dataprep_tools.fcl\"" << endl; // Need adcStringBuilder
44  fout << "#include \"dunecommon_tools.fcl\"" << endl; // Need tickRanges
45  fout << "tools.tickRanges.myTicks: { begin:10 end:90 labels:[\"My ticks\"] }" << endl;
46  fout << "tools.mytool: {" << endl;
47  fout << " tool_type: AdcDataPlotter" << endl;
48  fout << " DataType: 0" << endl;
49  fout << " DataView: \"\"" << endl;
50  fout << " LogLevel: 2" << endl;
51  fout << " TickRange: \"myTicks\"" << endl;
52  fout << " TickRebin: 1" << endl;
53  fout << " ChannelRanges: []" << endl;
54  fout << " ClockFactor: 0.0" << endl;
55  fout << " ClockOffset: 0.0" << endl;
56  fout << " FembTickOffsets: []" << endl;
57  fout << " MaxSignal: \"10*[gain]/14.0\"" << endl;
58  fout << " SkipChannelStatus: []" << endl;
59  fout << " EmptyColor: 18" << endl;
60  fout << " ChannelLineModulus: 4" << endl;
61  fout << " ChannelLinePattern: [1]" << endl;
62  fout << " Palette: 1026" << endl;
63  fout << " HistName: \"hadc\"" << endl;
64  fout << " HistTitle: \"Prepared ADC run %RUN% event %EVENT%\"" << endl;
65  fout << " PlotTitle: \"Run %RUN% event %EVENT% xyx UTC\"" << endl;
66  fout << " PlotFileName: \"myplotall-run%0RUN%-evt%0EVENT%.png\"" << endl;
67  fout << " PlotSizeX: 0" << endl;
68  fout << " PlotSizeY: 0" << endl;
69  fout << " RootFileName: \"adc.root\"" << endl;
70  fout << "}" << endl;
71  fout << "tools.mytool2: @local::tools.mytool" << endl;
72  fout << "tools.mytool2.DataView: rois" << endl;
73  fout << "tools.mytool2.HistTitle: \"Prepared ROI ADC run %RUN% event %EVENT%\"" << endl;
74  fout << "tools.mytool2.PlotFileName: \"myplotroi-run%0RUN%-evt%0EVENT%.png\"" << endl;
75  fout << "tools.mytool3: @local::tools.mytool" << endl;
76  fout << "tools.mytool3.DataView: rnis" << endl;
77  fout << "tools.mytool3.HistTitle: \"Prepared not ROI ADC run %RUN% event %EVENT%\"" << endl;
78  fout << "tools.mytool3.PlotFileName: \"myplotrni-run%0RUN%-evt%0EVENT%.png\"" << endl;
79  fout << "tools.runDataTool: {" << endl;
80  fout << " tool_type: FclRunDataTool" << endl;
81  fout << " LogLevel: 1" << endl;
82  fout << " FileNames: [\"rundata.fcl\"]" << endl;
83  fout << "}" << endl;
84  fout.close();
85  ofstream fout2("rundata.fcl");
86  fout2 << "run: 123" << endl;
87  fout2 << "gain: 14.0" << endl;
88  fout2 << "shaping: 2.0" << endl;
89  fout2.close();
90  } else {
91  cout << myname << "Using existing top-level FCL." << endl;
92  }
93 
94  cout << myname << line << endl;
95  cout << myname << "Fetching tool manager." << endl;
97  assert ( ptm != nullptr );
98  DuneToolManager& tm = *ptm;
99  tm.print();
100  assert( tm.toolNames().size() >= 4 );
101 
102  cout << myname << line << endl;
103  cout << myname << "Fetching tool." << endl;
104  auto padv = tm.getPrivate<TpcDataTool>("mytool");
105  assert( padv != nullptr );
106  auto padv2 = tm.getPrivate<TpcDataTool>("mytool2");
107  assert( padv2 != nullptr );
108  auto padv3 = tm.getPrivate<TpcDataTool>("mytool3");
109  assert( padv3 != nullptr );
110 
111  cout << myname << line << endl;
112  cout << myname << "Create data and call tool." << endl;
113  AdcIndex nevt = 2;
114  float peds[20] = {701, 711, 733, 690, 688, 703, 720, 720, 695, 702,
115  410, 404, 388, 389, 400, 401, 410, 404, 395, 396};
116  vector<double> wf = {5.0, 20.1, 53.2, 80.6, 130.2, 160.1, 150.4, 125.7, 72.5, 41.3, 18.4,
117  -6.5, -34.9, -56.6, -88.9, -132.6, -170.8, -172.9, -144.6, -112.6,
118  -79.4, -44.9, -22.1, -12.6, -4.7};
119  for ( AdcIndex ievt=0; ievt<nevt; ++ievt ) {
120  cout << myname << "Event " << ievt << endl;
121  AdcChannelDataMap datamap;
122  AdcIndex ncha = 20;
123  AdcIndex icha1 = 10000;
124  AdcIndex icha2 = icha1 + ncha;
125  for ( AdcIndex icha=icha1; icha<icha2; ++icha ) {
126  std::pair<AdcChannelDataMap::iterator, bool> kdat = datamap.emplace(icha, AdcChannelData());
127  assert(kdat.second);
128  AdcChannelDataMap::iterator idat = kdat.first;
129  AdcChannelData& data = idat->second;
130  data.setEventInfo(123, ievt);
131  float ped = peds[icha-icha1];
132  data.setChannelInfo(icha);
133  data.pedestal = ped;
134  for ( AdcIndex itic=0; itic<100; ++itic ) {
135  float xadc = ped + gRandom->Gaus(0.0, 10.0);
136  AdcCount iadc = xadc;
137  data.raw.push_back(iadc);
138  data.samples.push_back(iadc - ped);
139  }
140  AdcIndex tp = 10*ievt + 60 - 2.3*(icha-icha1);
141  for ( unsigned int iwf=0; iwf<wf.size(); ++iwf ) {
142  unsigned int isam = tp+iwf;
143  if ( isam < data.samples.size() ) {
144  data.raw[isam] += wf[iwf];
145  data.samples[isam] += wf[iwf];
146  }
147  }
148  for ( unsigned int isam=0; isam<data.samples.size(); ++isam ) {
149  data.samples[isam] *= 0.04;
150  }
151  data.sampleUnit = "ke";
152  }
153  assert( padv->viewMap(datamap) == 0 );
154  // Add and plot view rois.
155  for ( AdcIndex icha=icha1; icha<icha2; ++icha ) {
156  AdcChannelData& dain = datamap[icha];
157  assert( dain.channel() == icha );
158  AdcIndex nsam = dain.samples.size();
159  AdcIndex tp = 10*ievt + 60 - 2.3*(icha-icha1);
160  AdcChannelData::View& vroi = dain.updateView("rois");
161  vroi.push_back(dain);
162  AdcChannelData& dout = vroi.back();
163  dout.tick0 = dain.tick0 + tp;
164  for ( unsigned int iwf=0; iwf<wf.size(); ++iwf ) {
165  AdcIndex isam = tp + iwf;
166  if ( isam >= nsam ) break;
167  dout.samples.push_back(dain.samples[isam]);
168  }
169  }
170  assert( padv2->viewMap(datamap) == 0 );
171  // Add and plot view rnis.
172  for ( AdcIndex icha=icha1; icha<icha2; ++icha ) {
173  AdcChannelData& dain = datamap[icha];
174  assert( dain.channel() == icha );
175  AdcIndex nsam = dain.samples.size();
176  AdcIndex tp = 10*ievt + 60 - 2.3*(icha-icha1);
177  AdcChannelData::View& vrni = dain.updateView("rnis");
178  vrni.push_back(dain);
179  AdcChannelData& dout = vrni.back();
180  for ( unsigned int isam=0; isam<tp; ++isam) {
181  dout.samples.push_back(dain.samples[isam]);
182  }
183  vrni.push_back(dain);
184  AdcChannelData& dout2 = vrni.back();
185  unsigned int isam0 = tp + wf.size();
186  dout2.tick0 = isam0;
187  for ( unsigned int isam=isam0; isam<nsam; ++isam) {
188  dout2.samples.push_back(dain.samples[isam]);
189  }
190  }
191  assert( padv3->viewMap(datamap) == 0 );
192  }
193 
194  cout << myname << line << endl;
195  cout << myname << "Done." << endl;
196  return 0;
197 }
198 
199 //**********************************************************************
200 
201 int main(int argc, char* argv[]) {
202  bool useExistingFcl = false;
203  if ( argc > 1 ) {
204  string sarg(argv[1]);
205  if ( sarg == "-h" ) {
206  cout << "Usage: " << argv[0] << " [ARG]" << endl;
207  cout << " If ARG = true, existing FCL file is used." << endl;
208  return 0;
209  }
210  useExistingFcl = sarg == "true" || sarg == "1";
211  }
212  return test_AdcDataPlotter(useExistingFcl);
213 }
214 
215 //**********************************************************************
intermediate_table::iterator iterator
std::vector< AdcChannelData > View
const std::vector< std::string > & toolNames() const
std::string string
Definition: nybbler.cc:12
struct vector vector
void print() const
int main(int argc, char *argv[])
tm
Definition: demo.py:21
void setChannelInfo(ChannelInfoPtr pchi)
size_t size
Definition: lodepng.cpp:55
void setEventInfo(EventInfoPtr pevi)
AdcCountVector raw
unsigned int AdcIndex
Definition: AdcTypes.h:15
std::unique_ptr< T > getPrivate(std::string name)
Channel channel() const
AdcSignal pedestal
void line(double t, double *p, double &x, double &y, double &z)
View & updateView(Name vnam)
std::map< AdcChannel, AdcChannelData > AdcChannelDataMap
int test_AdcDataPlotter(bool useExistingFcl=false)
short AdcCount
Definition: AdcTypes.h:18
static DuneToolManager * instance(std::string fclname="", int dbg=1)
AdcSignalVector samples
QTextStream & endl(QTextStream &s)