test_AdcPedestalFitter.cxx
Go to the documentation of this file.
1 // test_AdcPedestalFitter.cxx
2 //
3 // David Adams
4 // April 2017
5 //
6 // Test AdcPedestalFitter.
7 
8 #include <string>
9 #include <iostream>
10 #include <fstream>
13 #include "TError.h"
14 
15 #undef NDEBUG
16 #include <cassert>
17 
18 using std::string;
19 using std::cout;
20 using std::endl;
21 using std::ofstream;
23 
24 //**********************************************************************
25 
26 void TestErrorHandler(Int_t, Bool_t, const char*, const char* msg) {
27  const string myname = "TestErrorHandler: ";
28  cout << myname << msg << endl;
29  abort();
30 }
31 
32 //**********************************************************************
33 
34 int test_AdcPedestalFitter(bool useExistingFcl, bool doUpdate, bool doUpdateMap) {
35  const string myname = "test_AdcPedestalFitter: ";
36 #ifdef NDEBUG
37  cout << myname << "NDEBUG must be off." << endl;
38  abort();
39 #endif
40  string line = "-----------------------------";
41 
42  // Set a Root error handler and make sure it is not called when
43  // the tool is used.
44  SetErrorHandler(TestErrorHandler);
45 
46  cout << myname << line << endl;
47  string fclfile = "test_AdcPedestalFitter.fcl";
48  if ( ! useExistingFcl ) {
49  cout << myname << "Creating top-level FCL." << endl;
50  ofstream fout(fclfile.c_str());
51  fout << "#include \"test_dataprep.fcl\"" << endl; // For adcNameManipulator
52  fout << "tools.mytool: {" << endl;
53  fout << " tool_type: AdcPedestalFitter" << endl;
54  fout << " LogLevel: 1" << endl;
55  fout << " AdcRange: \"pow(2,12)\"" << endl;
56  fout << " FitOpt: 3" << endl;
57  fout << " FitPrecision: 1.0" << endl;
58  fout << " SkipFlags: []" << endl;
59  fout << " AdcFitRange: \"10*int(0.7*[gain]+1)\"" << endl;
60  fout << " FitRmsMin: \"1.0\"" << endl;
61  fout << " FitRmsMax: \"20.0\"" << endl;
62  fout << " RemoveStickyCode: false" << endl;
63  fout << " HistName: \"adcped_%EVENT%_%CHAN%\"" << endl;
64  fout << " HistTitle: \"ADC pedestal for event %EVENT% channel %CHAN%\"" << endl;
65  fout << " PlotFileName: \"adcped_ev%EVENT%_chan%CHAN%.png\"" << endl;
66  fout << " RootFileName: \"adcped.root\"" << endl;
67  fout << " HistManager: \"\"" << endl;
68  fout << " PlotSizeX: 700" << endl;
69  fout << " PlotSizeY: 500" << endl;
70  fout << " PlotShowFit: 2" << endl;
71  fout << " PlotSplitX: 0" << endl;
72  fout << " PlotSplitY: 0" << endl;
73  fout << "}" << endl;
74  fout << "tools.mymaptool: @local::tools.mytool" << endl;
75  fout << "tools.mymaptool.LogLevel: 2" << endl;
76  fout << "tools.mymaptool.PlotFileName: \"adcpedmap_ev%EVENT%_chan%CHAN%.png\"" << endl;
77  fout << "tools.mymaptool.PlotSplitX: 2" << endl;
78  fout << "tools.mymaptool.RootFileName: \"\"" << endl;
79  fout << "tools.mymaptool.PlotSizeX: 1400" << endl;
80  fout << "tools.mymaptool.PlotSizeY: 1000" << endl;
81  fout << "tools.runDataTool: {" << endl;
82  fout << " tool_type: FclRunDataTool" << endl;
83  fout << " LogLevel: 1" << endl;
84  fout << " FileNames: [\"rundata.fcl\"]" << endl;
85  fout << "}" << endl;
86  fout.close();
87  ofstream fout2("rundata.fcl");
88  fout2 << "run: 123" << endl;
89  fout2 << "gain: 14.0" << endl;
90  fout2 << "shaping: 2.0" << endl;
91  fout2.close();
92  } else {
93  cout << myname << "Using existing top-level FCL." << endl;
94  }
95 
96  cout << myname << line << endl;
97  cout << myname << "Fetching tool manager." << endl;
99  assert ( ptm != nullptr );
100  DuneToolManager& tm = *ptm;
101  tm.print();
102  assert( tm.toolNames().size() > 1 );
103 
104  cout << myname << line << endl;
105  cout << myname << "Fetching histogram manaager." << endl;
106  auto phm = tm.getShared<TpcDataTool>("mytool");
107  assert( phm != nullptr );
108 
109  cout << myname << line << endl;
110  cout << myname << "Fetching tool." << endl;
111  auto padvNotUsed = tm.getPrivate<TpcDataTool>("mytool");
112  assert( padvNotUsed != nullptr );
113  auto padvsin = tm.getPrivate<TpcDataTool>("mytool");
114  assert( padvsin != nullptr );
115  auto padvmap = tm.getPrivate<TpcDataTool>("mymaptool");
116  assert( padvmap != nullptr );
117  if ( ! doUpdate ) padvsin = nullptr;
118  if ( ! doUpdateMap ) padvmap = nullptr;
119 
120  cout << myname << line << endl;
121  cout << myname << "Create data and call tool." << endl;
122  AdcIndex nevt = 2;
123  string lab = "plane 3u";
124  float peds[10] = {701.1, 711.2, 733.3, 690.4, 688.5, 703.6, 720.7, 720.8, 695.9, 702.0};
125 
126  for ( AdcIndex ievt=0; ievt<nevt; ++ievt ) {
127  cout << myname << "Event " << ievt << endl;
128  AdcChannelDataMap datamap;
129  AdcIndex ncha = 10;
130  for ( AdcIndex icha=0; icha<ncha; ++icha ) {
131  std::pair<AdcChannelDataMap::iterator, bool> kdat = datamap.emplace(icha, AdcChannelData());
132  assert(kdat.second);
133  AdcChannelDataMap::iterator idat = kdat.first;
134  AdcChannelData& data = idat->second;
135  float ped = peds[icha];
136  data.setEventInfo(101, 23);
137  data.setChannelInfo(icha);
138  data.pedestal = ped;
139  for ( AdcIndex itic=0; itic<100; ++itic ) {
140  float xadc = ped + rand()%20 - 10.0;
141  AdcIndex iticeff = itic - 3*icha;
142  if ( iticeff > 20 && iticeff < 40 ) xadc +=600;
143  AdcCount iadc = xadc;
144  data.raw.push_back(iadc);
145  data.flags.push_back(0);
146  data.samples.push_back(iadc - ped);
147  data.signal.push_back(xadc - ped > 300.0 );
148  }
149  data.pedestal += 10.0;
150  AdcIndex tp = 10*ievt + 60 - 2.3*icha;
151  AdcIndex tm = tp - 8;
152  data.raw[tp] += 100;
153  data.samples[tp] += 100;
154  data.raw[tm] -= 100;
155  data.samples[tm] -= 100;
156  data.flags[tm] = 4;
157  data.roisFromSignal();
158  if ( padvsin != nullptr ) {
159  double ped0 = datamap[icha].pedestal;
160  //assert( padv->view(datamap[icha]) == 0 );
161  double ped1 = datamap[icha].pedestal;
162  assert( ! datamap[icha].hasMetadata("fitPedPeakBinFraction") );
163  assert( padvsin->update(datamap[icha]) == 0 );
164  double ped2 = datamap[icha].pedestal;
165  cout << myname << "Old pedestal: " << ped0 << endl;
166  cout << myname << "New pedestal: " << ped2 << endl;
167  assert( ped1 == ped0 );
168  assert( ped2 != ped0 );
169  assert( ped2 != 0.0 );
170  assert( datamap[icha].hasMetadata("fitPedPeakBinFraction") );
171  assert( datamap[icha].hasMetadata("fitPedFractionLow") );
172  assert( datamap[icha].hasMetadata("fitPedFractionHigh") );
173  //assert( fabs(ped2-ped) < 0.01 );
174  }
175  }
176  if ( padvmap != nullptr ) {
177  for ( AdcIndex icha=0; icha<ncha; ++icha ) datamap[icha].metadata.clear();
178  assert( ! datamap[0].hasMetadata("fitPedPeakBinFraction") );
179  assert( padvmap->updateMap(datamap) == 0 );
180  string mname = "fitPedPeakBinFraction";
181  for ( AdcIndex icha=0; icha<ncha; ++icha ) {
182  cout << myname << "Checking channel " << icha << endl;
183  cout << myname << " Metadata size: " << datamap[icha].metadata.size() << endl;
184  cout << myname << " " << mname << " = " << datamap[icha].metadata[mname] << endl;
185  assert( datamap[icha].hasMetadata(mname) );
186  }
187  }
188  }
189 
190  cout << myname << line << endl;
191  cout << myname << "Done." << endl;
192  return 0;
193 }
194 
195 //**********************************************************************
196 
197 int main(int argc, char* argv[]) {
198  bool useExistingFcl = false;
199  bool doUpdate = true;
200  bool doUpdateMap = true;
201  if ( argc > 1 ) {
202  string sarg(argv[1]);
203  if ( sarg == "-h" ) {
204  cout << "Usage: " << argv[0] << " [ARG]" << endl;
205  cout << " If ARG = true, existing FCL file is used." << endl;
206  return 0;
207  }
208  useExistingFcl = sarg == "true" || sarg == "1";
209  }
210  return test_AdcPedestalFitter(useExistingFcl, doUpdate, doUpdateMap);
211 }
212 
213 //**********************************************************************
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
int test_AdcPedestalFitter(bool useExistingFcl, bool doUpdate, bool doUpdateMap)
void print() const
tm
Definition: demo.py:21
void setChannelInfo(ChannelInfoPtr pchi)
void setEventInfo(EventInfoPtr pevi)
AdcCountVector raw
int main(int argc, char *argv[])
unsigned int AdcIndex
Definition: AdcTypes.h:15
std::unique_ptr< T > getPrivate(std::string name)
AdcSignal pedestal
AdcFilterVector signal
void line(double t, double *p, double &x, double &y, double &z)
void TestErrorHandler(Int_t, Bool_t, const char *, const char *msg)
std::map< AdcChannel, AdcChannelData > AdcChannelDataMap
short AdcCount
Definition: AdcTypes.h:18
static DuneToolManager * instance(std::string fclname="", int dbg=1)
AdcSignalVector samples
T * getShared(std::string name)
QTextStream & endl(QTextStream &s)
AdcFlagVector flags