Typedefs | Functions
test_Roi2dToAdc.cxx File Reference
#include <string>
#include <iostream>
#include <fstream>
#include <iomanip>
#include "dunecore/DuneInterface/Tool/TpcDataTool.h"
#include "dunecore/ArtSupport/DuneToolManager.h"
#include <cassert>

Go to the source code of this file.

Typedefs

using Index = unsigned int
 

Functions

int showAdcMap (const AdcChannelDataMap &acm, string myname, const AdcChannelDataMap *pacmchk=0)
 
int showRoi (const Tpc2dRoi &roi, string myname)
 
int test_Roi2dToAdc (bool useExistingFcl=false)
 
int main (int argc, char *argv[])
 

Typedef Documentation

using Index = unsigned int

Definition at line 26 of file test_Roi2dToAdc.cxx.

Function Documentation

int main ( int  argc,
char *  argv[] 
)

Definition at line 240 of file test_Roi2dToAdc.cxx.

240  {
241  bool useExistingFcl = false;
242  if ( argc > 1 ) {
243  string sarg(argv[1]);
244  if ( sarg == "-h" ) {
245  cout << "Usage: " << argv[0] << " [ARG]" << endl;
246  cout << " If ARG = true, existing FCL file is used." << endl;
247  return 0;
248  }
249  useExistingFcl = sarg == "true" || sarg == "1";
250  }
251  return test_Roi2dToAdc(useExistingFcl);
252 }
int test_Roi2dToAdc(bool useExistingFcl=false)
QTextStream & endl(QTextStream &s)
int showAdcMap ( const AdcChannelDataMap acm,
string  myname,
const AdcChannelDataMap pacmchk = 0 
)

Definition at line 28 of file test_Roi2dToAdc.cxx.

28  {
29  Index nerr = 0;
30  // Display samples.
31  cout << myname << " chan toff samples" << endl;
32  for ( auto& iacd : acm ) {
33  const AdcChannelData& acd = iacd.second;
34  Index icha = acd.channel();
35  bool dochk = pacmchk;
36  if ( dochk && pacmchk->count(icha) == 0 ) {
37  ++nerr;
38  dochk = false;
39  }
40  const AdcChannelData* pacdchk = dochk ? &pacmchk->find(icha)->second : nullptr;
41  if ( dochk ) {
42  if ( pacdchk->samples.size() != acd.samples.size() ) ++nerr;
43  }
44  cout << myname << setw(6) << acd.channel() << setw(6) << acd.tickOffset() << ":";
45  bool first = true;
46  Index isam = 0;
47  for ( float sam : acd.samples ) {
48  if ( first ) first = false;
49  else cout << ",";
50  if ( dochk ) {
51  if ( pacdchk->samples.size() <= isam ||
52  fabs(pacdchk->samples[isam] - sam ) > 1.e-5 ) ++nerr;
53  }
54  cout << setw(4) << sam;
55  ++isam;
56  }
57  cout << " (";
58  for ( bool sig : acd.signal ) cout << (sig ? "X" : ".");
59  cout << ")" << endl;
60  }
61  // Display ROIs.
62  cout << myname << " chan nROI ROIs" << endl;
63  for ( auto& iacd : acm ) {
64  const AdcChannelData& acd = iacd.second;
65  Index icha = acd.channel();
66  cout << myname << setw(6) << icha << setw(6) << acd.rois.size() << ":";
67  bool first = true;
68  for ( const AdcRoi& roi : acd.rois ) {
69  if ( first ) first = false;
70  else cout << ",";
71  cout << " " << roi.first << "-" << roi.second;
72  }
73  cout << endl;
74  }
75  return nerr;
76 }
std::pair< AdcIndex, AdcIndex > AdcRoi
Definition: AdcTypes.h:54
unsigned int Index
AdcRoiVector rois
AdcLongIndex tickOffset() const
Q_EXPORT QTSManip setw(int w)
Definition: qtextstream.h:331
Channel channel() const
AdcFilterVector signal
AdcSignalVector samples
QTextStream & endl(QTextStream &s)
int showRoi ( const Tpc2dRoi roi,
string  myname 
)

Definition at line 78 of file test_Roi2dToAdc.cxx.

78  {
79  cout << myname << "ROI channel offset: " << roi.channelOffset() << endl;
80  cout << myname << "ROI sample offset: " << roi.sampleOffset() << endl;
81  for ( Index kcha=0; kcha<roi.channelSize(); ++kcha ) {
82  Index icha = roi.channelOffset() + kcha;
83  cout << myname << setw(6) << icha << ":";
84  bool first = true;
85  for ( Index ksam=0; ksam<roi.sampleSize(); ++ksam ) {
86  if ( first ) first = false;
87  else cout << ",";
88  Index isam = roi.sampleOffset() + ksam;
89  cout << setw(6) << roi.value(icha, isam, 8888);
90  }
91  cout << endl;
92  }
93  return 0;
94 }
Index sampleSize() const
Definition: Tpc2dRoi.h:41
unsigned int Index
float value(Index icha, LongIndex itck, float valdef=0.0) const
Definition: Tpc2dRoi.h:55
Index channelOffset() const
Definition: Tpc2dRoi.h:44
Q_EXPORT QTSManip setw(int w)
Definition: qtextstream.h:331
Index channelSize() const
Definition: Tpc2dRoi.h:47
LongIndex sampleOffset() const
Definition: Tpc2dRoi.h:38
QTextStream & endl(QTextStream &s)
int test_Roi2dToAdc ( bool  useExistingFcl = false)

Definition at line 98 of file test_Roi2dToAdc.cxx.

98  {
99  const string myname = "test_Roi2dToAdc: ";
100 #ifdef NDEBUG
101  cout << myname << "NDEBUG must be off." << endl;
102  abort();
103 #endif
104  string line = "-----------------------------";
105 
106  cout << myname << line << endl;
107  string fclfile = "test_Roi2dToAdc.fcl";
108  if ( ! useExistingFcl ) {
109  cout << myname << "Creating top-level FCL." << endl;
110  ofstream fout(fclfile.c_str());
111  fout << "#include \"dataprep_tools.fcl\"" << endl;
112  fout << "tools.mytool1: {" << endl;
113  fout << " tool_type: Roi2dToAdc" << endl;
114  fout << " LogLevel: 1" << endl;
115  fout << "}" << endl;
116  fout.close();
117  } else {
118  cout << myname << "Using existing top-level FCL." << endl;
119  }
120 
121  cout << myname << line << endl;
122  cout << myname << "Fetching tool manager." << endl;
124  assert ( ptm != nullptr );
125  DuneToolManager& tm = *ptm;
126  tm.print();
127  assert( tm.toolNames().size() >= 2 );
128 
129  cout << myname << line << endl;
130  cout << myname << "Create test data." << endl;
131  // The data for each channel is offset by one tick using acd.tick0.
132  TpcData tpd;
133  TpcData::AdcDataPtr pacm = tpd.createAdcData();
134  AdcChannelDataMap acmchk;
135  Index ncha = 4;
136  Index ntck = 10;
137  Index ntckroi = 6;
138  Index icha0 = 100;
139  Index itck0 = 1000;
140  Index tckoff = 2; // Offset from tick0 to signal
142  assert( tpd.get2dRois().size() == 0 );
143  tpd.get2dRois().emplace_back(ncha, ntckroi, icha0, itck0+tckoff);
144  assert( tpd.get2dRois().size() == 1 );
145  Tpc2dRoi& roi = tpd.get2dRois().back();
146  Index dtck = 0;
147  DuneEventInfo* peviMutable = new DuneEventInfo(123, 1);
148  peviMutable->triggerTick0 = itck0;
149  AdcChannelData::EventInfoPtr pevi(peviMutable);
150  Index nsig = 0;
151  for ( Index kcha=0; kcha<ncha; ++kcha, ++dtck ) {
152  Index icha = icha0 + kcha;
153  AdcChannelData& acd = (*pacm)[icha];
154  acd.setEventInfo(pevi);
155  acd.setChannelInfo(icha);
156  acd.tick0 = dtck;
157  acd.samples.resize(ntck, 999);
158  float val = 10*(kcha + 1);
159  idxs[0] = kcha;
160  idxs[1] = dtck;
161  AdcChannelData& acdchk = acmchk[icha];
162  acdchk.setEventInfo(pevi);
163  acdchk.setChannelInfo(icha);
164  acdchk.tick0 = dtck;
165  acdchk.samples.resize(ntck, 0.0);
166  for ( Index ktck=tckoff; ktck<tckoff+3; ++ktck, ++idxs[1] ) {
167  roi.data().setValue(idxs, val);
168  acdchk.samples[ktck] = val;
169  if ( acdchk.rois.size() == 0 ) {
170  acdchk.rois.push_back({ktck, ktck});
171  } else {
172  acdchk.rois.back().second = ktck;
173  }
174  }
175  acdchk.signal.resize(ntck, false);
176  Index isig1 = tckoff > kcha ? tckoff - kcha : 0;
177  Index isig2 = std::min(8 - kcha, ntck);
178  for ( Index ktck=isig1; ktck<isig2; ++ktck ) {
179  acdchk.signal[ktck] = true;
180  ++nsig;
181  }
182  }
183  assert( tpd.getAdcData().size() == 1 );
184  assert( tpd.getAdcData()[0]->size() == ncha );
185  for ( auto& iacd : *(tpd.getAdcData()[0]) ) {
186  const AdcChannelData& acd = iacd.second;
187  assert( acd.samples.size() == ntck );
188  }
189  assert( roi.channelSize() == ncha );
190  assert( roi.channelOffset() == icha0 );
191  assert( roi.sampleSize() == ntckroi );
192  assert( roi.sampleOffset() == itck0 + tckoff );
193 
194  cout << myname << line << endl;
195  cout << myname << "Check ROI data." << endl;
196  assert( tpd.get2dRois().size() == 1 );
197  assert( &tpd.get2dRois().front() == &roi );
198  showRoi(roi, myname);
199  cout << myname << line << endl;
200 
201  cout << myname << line << endl;
202  cout << myname << "Check ADC data before tool:" << endl;
203  assert( tpd.getAdcData().size() == 1 );
204  assert( tpd.getAdcData().front() == pacm );
205  AdcChannelDataMap& acm = *pacm;
206  showAdcMap(acm, myname);
207 
208  cout << myname << line << endl;
209  cout << myname << "Expected data after tool:" << endl;
210  showAdcMap(acmchk, myname);
211 
212  cout << myname << line << endl;
213  cout << myname << "Fetching tool." << endl;
214  auto ptoo = tm.getPrivate<TpcDataTool>("mytool1");
215  assert( ptoo != nullptr );
216 
217  cout << myname << line << endl;
218  cout << myname << "Call tool." << endl;
219  DataMap res = ptoo->updateTpcData(tpd);
220  res.print();
221  assert( res.status() == 0 );
222 
223  cout << myname << line << endl;
224  cout << myname << "Check ADC data after tool." << endl;
225  assert( tpd.getAdcData().size() == 1 );
226  assert( tpd.getAdcData().front() == pacm );
227  assert( showAdcMap(acm, myname, &acmchk) == 0 );
228  assert( res.getInt("r2a_nchaZeroed") == int(ncha) );
229  assert( res.getInt("r2a_nchaFilled") == int(ncha) );
230  assert( res.getInt("r2a_nsamZeroed") == int(ncha*ntck) );
231  assert( res.getInt("r2a_nsamFilled") == int(nsig) );
232 
233  cout << myname << line << endl;
234  cout << myname << "Done." << endl;
235  return 0;
236 }
LongIndex triggerTick0
Definition: DuneEventInfo.h:28
const std::vector< std::string > & toolNames() const
std::shared_ptr< AdcChannelDataMap > AdcDataPtr
Definition: TpcData.h:35
void print() const
unsigned int Index
void print(std::ostream *pout) const
Definition: DataMap.h:245
std::shared_ptr< const EventInfo > EventInfoPtr
tm
Definition: demo.py:21
int showRoi(const Tpc2dRoi &roi, string myname)
int status() const
Definition: DataMap.h:202
AdcDataVector & getAdcData()
Definition: TpcData.h:55
void setChannelInfo(ChannelInfoPtr pchi)
std::array< Index, 2 > IndexArray
Definition: Real2dData.h:34
AdcRoiVector rois
void setEventInfo(EventInfoPtr pevi)
std::unique_ptr< T > getPrivate(std::string name)
int showAdcMap(const AdcChannelDataMap &acm, string myname, const AdcChannelDataMap *pacmchk=0)
AdcFilterVector signal
T min(sqlite3 *const db, std::string const &table_name, std::string const &column_name)
Definition: statistics.h:55
int getInt(Name name, int def=0) const
Definition: DataMap.h:218
void line(double t, double *p, double &x, double &y, double &z)
AdcDataPtr createAdcData(bool updateParent=true)
Definition: TpcData.cxx:99
std::map< AdcChannel, AdcChannelData > AdcChannelDataMap
Tpc2dRoiVector & get2dRois()
Definition: TpcData.h:57
static DuneToolManager * instance(std::string fclname="", int dbg=1)
AdcSignalVector samples
QTextStream & endl(QTextStream &s)