test_AdcTickModViewer.cxx
Go to the documentation of this file.
1 // test_AdcTickModViewer.cxx
2 //
3 // David Adams
4 // April 2017
5 //
6 // Test AdcTickModViewer.
7 
8 #include <string>
9 #include <iostream>
10 #include <fstream>
11 #include <vector>
14 #include "TError.h"
15 
16 #undef NDEBUG
17 #include <cassert>
18 
19 using std::string;
20 using std::cout;
21 using std::endl;
22 using std::ofstream;
23 using std::vector;
25 
26 //**********************************************************************
27 
28 void TestErrorHandler(Int_t, Bool_t, const char*, const char* msg) {
29  const string myname = "TestErrorHandler: ";
30  cout << myname << msg << endl;
31  abort();
32 }
33 
34 //**********************************************************************
35 
36 int test_AdcTickModViewer(bool useExistingFcl, bool doUpdate, bool doUpdateMap) {
37  const string myname = "test_AdcTickModViewer: ";
38 #ifdef NDEBUG
39  cout << myname << "NDEBUG must be off." << endl;
40  abort();
41 #endif
42  string line = "-----------------------------";
43 
44  // Set a Root error handler and make sure it is not called when
45  // the tool is used.
46  SetErrorHandler(TestErrorHandler);
47 
48  cout << myname << line << endl;
49  string fclfile = "test_AdcTickModViewer.fcl";
50  if ( ! useExistingFcl ) {
51  cout << myname << "Creating top-level FCL." << endl;
52  ofstream fout(fclfile.c_str());
53  fout << "#include \"dataprep_tools.fcl\"" << endl; // Need adcNameManipulator
54  fout << "tools.mytool: {" << endl;
55  fout << " tool_type: AdcTickModViewer" << endl;
56  fout << " LogLevel: 1" << endl;
57  fout << " TickModPeriod: 4" << endl;
58  fout << " TimeOffsetTool: myTimeOffsetTool" << endl;
59  fout << " FitSigmaMin: 1.0" << endl;
60  fout << " FitSigmaMax: 20.0" << endl;
61  fout << " HistName: \"adctm_ch%0CHAN%_tm%0TICKMOD%\"" << endl;
62  fout << " HistTitle: \"ADC spectrum for channel %CHAN% tickmod %TICKMOD%\"" << endl;
63  fout << " HistChannelCount: 100" << endl;
64  fout << " PlotChannels: []" << endl;
65  fout << " AllPlotFileName: \"adctm%TICKMOD%_ch%0CHAN%.png\"" << endl;
66  fout << " MinPlotFileName: \"adctmMin_ch%0CHAN%.png\"" << endl;
67  fout << " MaxPlotFileName: \"adctmMax_ch%0CHAN%.png\"" << endl;
68  fout << " PhaseGrouping: channel" << endl;
69  fout << " PhasePlotFileName: \"adcPhase_ch%0CHAN%.png\"" << endl;
70  fout << " PhaseVariable: phase" << endl;
71  fout << " RootFileName: \"adctm.root\"" << endl;
72  fout << " TreeFileName: \"tickmod.root\"" << endl;
73  fout << " TreeFileName: \"\"" << endl;
74  fout << " PlotSizeX: 1400" << endl;
75  fout << " PlotSizeY: 1000" << endl;
76  fout << " PlotShowFit: 0" << endl;
77  fout << " PlotSplitX: 2" << endl;
78  fout << " PlotSplitY: 2" << endl;
79  fout << " PlotFrequency: 0" << endl;
80  fout << " PhasePlotSizeX: 1400" << endl;
81  fout << " PhasePlotSizeY: 1000" << endl;
82  fout << " PhasePlotSplitX: 2" << endl;
83  fout << " PhasePlotSplitY: 2" << endl;
84  fout << "}" << endl;
85  fout << "tools.myTimeOffsetTool: {" << endl;
86  fout << " tool_type: FixedTimeOffsetTool" << endl;
87  fout << " LogLevel: 2" << endl;
88  fout << " Value: 1000" << endl;
89  fout << " Rem: 0.0" << endl;
90  fout << " Unit: tick" << endl;
91  fout << "}" << endl;
92  fout.close();
93  } else {
94  cout << myname << "Using existing top-level FCL." << endl;
95  }
96 
97  cout << myname << line << endl;
98  cout << myname << "Fetching tool manager." << endl;
100  assert ( ptm != nullptr );
101  DuneToolManager& tm = *ptm;
102  tm.print();
103  assert( tm.toolNames().size() >= 1 );
104 
105  cout << myname << line << endl;
106  auto pvtm = tm.getPrivate<TpcDataTool>("mytool");
107  assert( pvtm != nullptr );
108  if ( ! doUpdate ) pvtm = nullptr;
109 
110  cout << myname << line << endl;
111  cout << myname << "Create data." << endl;
112  AdcIndex nevt = 2;
113  string lab = "plane 3u";
114  float peds[10] = {701.1, 711.2, 733.3, 690.4, 688.5, 703.6, 720.7, 720.8, 695.9, 702.0};
115  vector<AdcChannelDataMap> acms(nevt);
116  AdcIndex ncha = 10;
117  for ( AdcIndex ievt=0; ievt<nevt; ++ievt ) {
118  cout << myname << "Event " << ievt << endl;
119  AdcChannelDataMap& datamap = acms[ievt];
120  for ( AdcIndex icha=0; icha<ncha; ++icha ) {
121  std::pair<AdcChannelDataMap::iterator, bool> kdat = datamap.emplace(icha, AdcChannelData());
122  assert(kdat.second);
123  AdcChannelDataMap::iterator idat = kdat.first;
124  AdcChannelData& data = idat->second;
125  float ped = peds[icha];
126  data.setEventInfo(101, ievt, 23);
127  data.setChannelInfo(icha);
128  data.pedestal = ped;
129  for ( AdcIndex itic=0; itic<100; ++itic ) {
130  float xadc = ped + rand()%20 - 10.0;
131  AdcIndex iticeff = itic - 3*icha;
132  if ( iticeff > 20 && iticeff < 45 ) xadc +=600;
133  AdcCount iadc = xadc;
134  data.raw.push_back(iadc);
135  data.flags.push_back(0);
136  data.samples.push_back(iadc - ped);
137  data.signal.push_back(xadc - ped > 300.0 );
138  }
139  data.pedestal += 10.0;
140  AdcIndex tp = 10*ievt + 60 - 2.3*icha;
141  AdcIndex tm = tp - 8;
142  data.raw[tp] += 100;
143  data.samples[tp] += 100;
144  data.raw[tm] -= 100;
145  data.samples[tm] -= 100;
146  data.flags[tm] = 4;
147  data.roisFromSignal();
148  }
149  }
150 
151  cout << myname << line << endl;
152  cout << myname << "Call tool." << endl;
153  unsigned int expCount = 0;
154  for ( const AdcChannelDataMap& acm : acms ) {
155  cout << myname << "Event " << acm.begin()->second.event() << endl;
156  expCount += 25;
157  for ( const AdcChannelDataMap::value_type& iacd : acm ) {
158  const AdcChannelData& acd = iacd.second;
159  cout << myname << "Event " << acd.event() << ", channel " << acd.channel() << endl;
160  cout << myname << "ADC channel data size: " << acd.raw.size() << endl;
161  DataMap dm = pvtm->view(acd);
162  dm.print(myname);
163  assert( dm.status() == 0 );
164  assert( dm.haveHistVector("tmHists") );
165  const DataMap::HistVector& phs = dm.getHistVector("tmHists");
166  assert( phs.size() == 4 );
167  for ( const TH1* ph : phs ) {
168  cout << myname << " " << ph->GetName() << ": " << ph->GetTitle() << endl;
169  double countSum = ph->Integral();
170  double countUnder = ph->GetBinContent(0);
171  double countOver = ph->GetBinContent(ph->GetNbinsX()+1);
172  cout << myname << " Hist integral:" << countSum << endl;
173  cout << myname << " Hist undrflow:" << countUnder << endl;
174  cout << myname << " Hist overflow:" << countOver << endl;
175  double countTot = countSum + countUnder + countOver;
176  assert( countTot == expCount );
177  }
178  }
179  }
180 
181  cout << myname << line << endl;
182  cout << myname << "Done." << endl;
183  return 0;
184 }
185 
186 //**********************************************************************
187 
188 int main(int argc, char* argv[]) {
189  bool useExistingFcl = false;
190  bool doUpdate = true;
191  bool doUpdateMap = true;
192  if ( argc > 1 ) {
193  string sarg(argv[1]);
194  if ( sarg == "-h" ) {
195  cout << "Usage: " << argv[0] << " [ARG]" << endl;
196  cout << " If ARG = true, existing FCL file is used." << endl;
197  return 0;
198  }
199  useExistingFcl = sarg == "true" || sarg == "1";
200  }
201  return test_AdcTickModViewer(useExistingFcl, doUpdate, doUpdateMap);
202 }
203 
204 //**********************************************************************
void TestErrorHandler(Int_t, Bool_t, const char *, const char *msg)
intermediate_table::iterator iterator
void msg(const char *fmt,...)
Definition: message.cpp:107
const std::vector< std::string > & toolNames() const
std::string string
Definition: nybbler.cc:12
struct vector vector
void print() const
int test_AdcTickModViewer(bool useExistingFcl, bool doUpdate, bool doUpdateMap)
void print(std::ostream *pout) const
Definition: DataMap.h:245
int main(int argc, char *argv[])
tm
Definition: demo.py:21
const HistVector & getHistVector(Name name) const
Definition: DataMap.h:224
int status() const
Definition: DataMap.h:202
void setChannelInfo(ChannelInfoPtr pchi)
void setEventInfo(EventInfoPtr pevi)
AdcIndex event() const
AdcCountVector raw
unsigned int AdcIndex
Definition: AdcTypes.h:15
std::unique_ptr< T > getPrivate(std::string name)
Channel channel() const
AdcSignal pedestal
AdcFilterVector signal
void line(double t, double *p, double &x, double &y, double &z)
std::map< AdcChannel, AdcChannelData > AdcChannelDataMap
bool haveHistVector(Name name) const
Definition: DataMap.h:213
short AdcCount
Definition: AdcTypes.h:18
std::vector< TH1 * > HistVector
Definition: DataMap.h:57
static DuneToolManager * instance(std::string fclname="", int dbg=1)
AdcSignalVector samples
QTextStream & endl(QTextStream &s)
AdcFlagVector flags