test_AdcChannelMetric.cxx
Go to the documentation of this file.
1 // test_AdcChannelMetric.cxx
2 //
3 // David Adams
4 // April 2017
5 //
6 // Test AdcChannelMetric.
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_AdcChannelMetric(bool useExistingFcl =false) {
31  const string myname = "test_AdcChannelMetric: ";
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_AdcChannelMetric.fcl";
40  string hname = "hchped_tpp3c";
41  if ( ! useExistingFcl ) {
42  cout << myname << "Creating top-level FCL." << endl;
43  ofstream fout(fclfile.c_str());
44  fout << "#include \"protodune_dataprep_tools.fcl\"" << endl; // Need adcNameManipulator
45  fout << "tools.mytool: {" << endl;
46  fout << " tool_type: AdcChannelMetric" << endl;
47  fout << " LogLevel: 3" << endl;
48  fout << " Metric: \"pedestal\"" << endl;
49  fout << " DataView: \"\"" << endl;
50  fout << " PedestalReference: \"\"" << endl;
51  fout << " MetricSummaryView: \"\"" << endl;
52  fout << " ChannelRanges: [tpp3c, tpp3z]" << endl;
53  fout << " MetricBins: 0" << endl;
54  fout << " MetricMin: \"0.0\"" << endl;
55  fout << " MetricMax: \"2000.0*[gain]/14.0\"" << endl;
56  fout << " ChannelLineModulus: 200" << endl;
57  fout << " ChannelLinePattern: [20, 40, 60, 80, 100, 120, 140, 160, 180]" << endl;
58  fout << " ChannelLinePatternSolid: [0]" << endl;
59  fout << " HistName: \"hchped_%CRNAME%\"" << endl;
60  fout << " HistTitle: \"ADC pedestals for run %RUN% event %EVENT% %CRLABEL%\"" << endl;
61  fout << " MetricLabel: \"Pedestal [ADC counts]\"" << endl;
62  fout << " PlotSizeX: 0" << endl;
63  fout << " PlotSizeY: 0" << endl;
64  fout << " PlotUsesStatus: 0" << endl;
65  fout << " PlotFileName: \"mypeds-run%RUN%-evt%EVENT%_%CRNAME%.png\"" << endl;
66  fout << " RootFileName: \"\"" << endl;
67  fout << " MetadataFlags: [\"write\"]" << endl;
68  fout << "}" << endl;
69  fout << "tools.runDataTool: {" << endl;
70  fout << " tool_type: FclRunDataTool" << endl;
71  fout << " LogLevel: 1" << endl;
72  fout << " FileNames: [\"rundata.fcl\"]" << endl;
73  fout << "}" << endl;
74  fout.close();
75  ofstream fout2("rundata.fcl");
76  fout2 << "run: 123" << endl;
77  fout2 << "gain: 14.0" << endl;
78  fout2 << "shaping: 2.0" << endl;
79  fout2.close();
80  } else {
81  cout << myname << "Using existing top-level FCL." << endl;
82  }
83 
84  cout << myname << line << endl;
85  cout << myname << "Fetching tool manager." << endl;
87  assert ( ptm != nullptr );
88  DuneToolManager& tm = *ptm;
89  tm.print();
90  assert( tm.toolNames().size() >= 1 );
91 
92  cout << myname << line << endl;
93  cout << myname << "Fetching tool." << endl;
94  auto padv = tm.getPrivate<TpcDataTool>("mytool");
95  assert( padv != nullptr );
96 
97  cout << myname << line << endl;
98  cout << myname << "Create data and call tool." << endl;
99  AdcIndex nevt = 2;
100  const int nped = 20;
101  float peds[nped] = {701, 711, 733, 690, 688, 703, 720, 720, 695, 702,
102  410, 404, 388, 389, 400, 401, 410, 404, 395, 396};
103  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,
104  -6.5, -34.9, -56.6, -88.9, -132.6, -170.8, -172.9, -144.6, -112.6,
105  -79.4, -44.9, -22.1, -12.6, -4.7};
106  for ( AdcIndex ievt=0; ievt<nevt; ++ievt ) {
107  cout << myname << "Event " << ievt << endl;
108  AdcChannelDataMap datamap;
109  AdcIndex ncha = 200;
110  AdcIndex icha1 = 10000;
111  AdcIndex icha2 = icha1 + ncha;
112  for ( AdcIndex icha=icha1; icha<icha2; ++icha ) {
113  std::pair<AdcChannelDataMap::iterator, bool> kdat = datamap.emplace(icha, AdcChannelData());
114  assert(kdat.second);
115  AdcChannelDataMap::iterator idat = kdat.first;
116  AdcChannelData& data = idat->second;
117  data.setEventInfo(123, ievt);
118  float ped = peds[(icha-icha1)%nped];
119  data.setChannelInfo(icha);
120  data.pedestal = ped;
121  for ( AdcIndex itic=0; itic<100; ++itic ) {
122  float xadc = ped + gRandom->Gaus(0.0, 10.0);
123  AdcCount iadc = xadc;
124  data.raw.push_back(iadc);
125  data.samples.push_back(iadc - ped);
126  }
127  AdcIndex tp = 10*ievt + 60 - 2.3*(icha-icha1);
128  for ( unsigned int iwf=0; iwf<wf.size(); ++iwf ) {
129  unsigned int isam = tp+iwf;
130  if ( isam < data.samples.size() ) {
131  data.raw[isam] += wf[iwf];
132  data.samples[isam] += wf[iwf];
133  }
134  }
135  for ( unsigned int isam=0; isam<data.samples.size(); ++isam ) {
136  data.samples[isam] *= 0.04;
137  }
138  data.sampleUnit = "ke";
139  assert( ! data.hasMetadata("pedestal") );
140  }
141  DataMap ret = padv->updateMap(datamap);
142  ret.print();
143  assert( ret == 0 );
144  cout << myname << "Checking histogram " << hname << endl;
145  TH1* phout = ret.getHist(hname);
146  assert( phout != nullptr );
147  assert( phout->GetName() == hname );
148  assert( phout->GetEntries() == ncha );
149  for ( const auto& idata : datamap ) {
150  assert( idata.second.hasMetadata("pedestal") );
151  }
152  }
153 
154  cout << myname << line << endl;
155  cout << myname << "Done." << endl;
156  return 0;
157 }
158 
159 //**********************************************************************
160 
161 int main(int argc, char* argv[]) {
162  bool useExistingFcl = false;
163  if ( argc > 1 ) {
164  string sarg(argv[1]);
165  if ( sarg == "-h" ) {
166  cout << "Usage: " << argv[0] << " [ARG]" << endl;
167  cout << " If ARG = true, existing FCL file is used." << endl;
168  return 0;
169  }
170  useExistingFcl = sarg == "true" || sarg == "1";
171  }
172  return test_AdcChannelMetric(useExistingFcl);
173 }
174 
175 //**********************************************************************
intermediate_table::iterator iterator
int test_AdcChannelMetric(bool useExistingFcl=false)
int main(int argc, char *argv[])
const std::vector< std::string > & toolNames() const
std::string string
Definition: nybbler.cc:12
struct vector vector
void print() const
void print(std::ostream *pout) const
Definition: DataMap.h:245
tm
Definition: demo.py:21
void setChannelInfo(ChannelInfoPtr pchi)
bool hasMetadata(Name mname) const
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)
AdcSignal pedestal
void line(double t, double *p, double &x, double &y, double &z)
std::map< AdcChannel, AdcChannelData > AdcChannelDataMap
short AdcCount
Definition: AdcTypes.h:18
static DuneToolManager * instance(std::string fclname="", int dbg=1)
AdcSignalVector samples
QTextStream & endl(QTextStream &s)