AdcDataDumper_tool.cc
Go to the documentation of this file.
1 // AdcDataDumper_tool.cc
2 
3 #include "AdcDataDumper.h"
4 #include <iostream>
5 #include <fstream>
6 #include <sstream>
7 #include <iomanip>
8 
9 using std::string;
10 using std::cout;
11 using std::endl;
12 using std::ofstream;
13 using std::ostream;
14 using std::ostringstream;
15 using std::setw;
16 
17 using Index = unsigned int;
18 
19 //**********************************************************************
20 // Local definitions.
21 //**********************************************************************
22 
23 namespace {
24 
25 template<class T>
26 T divide(double sum, Index nval) {
27  cout << "AdcDataDumper::divide: Invalid type." << endl;
28  return 0.0;
29 }
30 
31 template<>
32 AdcSignal divide<AdcSignal>(double sum, Index nval) {
33  return sum/nval;
34 }
35 
36 template<>
37 AdcCount divide<AdcCount>(double sum, Index nval) {
38  return AdcCount(sum/nval + 0.5);
39 }
40 
41 /* Same type as AdcCount
42 template<>
43 AdcFlag divide<AdcFlag>(double sum, Index nval) {
44  if ( sum == 0.0 ) return 0;
45  return 100;
46 }
47 */
48 
49 //**********************************************************************
50 
51 // Apply limits and rebinning to an input vector.
52 
53 template<class V>
54 class DisplayVector {
55 public:
56  V vout;
57  DisplayVector(Index first, Index a_rebin, Index maxn, const V& vin) {
58  Index rebin = a_rebin == 0 ? 1 : a_rebin;
59  for ( Index isam1=first; isam1<vin.size(); isam1+=rebin ) {
60  typename V::value_type binval = vin[isam1];
61  if ( rebin > 1 ) {
62  double binsum = 0.0;
63  Index isam2 = isam1 + rebin;
64  if ( isam2 > vin.size() ) break;
65  for ( Index isam=isam1; isam<isam2; ++isam ) {
66  binsum += vin[isam];
67  }
68  binval = divide<typename V::value_type>(binsum, rebin);
69  }
70  vout.push_back(binval);
71  if ( vout.size() >= maxn ) break;
72  }
73  }
74 };
75 
76 //**********************************************************************
77 
78 char charThresh(double val, double thresh) {
79  if ( val > thresh ) return '+';
80  if ( val < -thresh ) return '-';
81  return ' ';
82 }
83 
84 } // end unnamed namespace
85 
86 //**********************************************************************
87 // Class methods.
88 //**********************************************************************
89 
91 : m_FileName(ps.get<string>("FileName")),
92  m_Prefix(ps.get<string>("Prefix")),
93  m_NewFile(ps.get<bool>("NewFile")),
94  m_ShowChannelCount(ps.get<bool>("ShowChannelCount")),
95  m_ShowTickCounts(ps.get<bool>("ShowTickCounts")),
96  m_ShowRaw(ps.get<bool>("ShowRaw")),
97  m_ShowPrepared(ps.get<bool>("ShowPrepared")),
98  m_ShowFirst(ps.get<unsigned int>("ShowFirst")),
99  m_ShowRebin(ps.get<unsigned int>("ShowRebin")),
100  m_ShowMax(ps.get<unsigned int>("ShowMax")),
101  m_ShowThreshold(ps.get<float>("ShowThreshold")),
102  m_ShowOpt(ps.get<unsigned int>("ShowOpt")),
103  m_pout(nullptr) {
104  if ( m_FileName.size() == 0 ) m_pout = &cout;
105  else if ( ! m_NewFile ) m_pout = new ofstream(m_FileName.c_str());
106 }
107 
108 //**********************************************************************
109 
111  if ( m_pout != nullptr && m_pout != &cout ) {
112  delete m_pout;
113  m_pout = nullptr;
114  }
115 }
116 
117 //**********************************************************************
118 
120  DataMap ret;
121  ostream* pout = m_pout;
122  bool newfile = pout == nullptr;
123  // If file is not already set, build the file name and open it.
124  if ( newfile ) {
125  if ( ! m_NewFile ) return ret.setStatus(1);
126  string fname = m_FileName;
127  string::size_type npos = string::npos;
128  string::size_type ipos = fname.find("%PAT%");
129  ipos = fname.find("%CHAN1%");
130  if ( ipos != npos ) {
131  string srep = "NOCHAN";
132  if ( acds.size() ) {
133  ostringstream ssrep;
134  ssrep << acds.begin()->first;
135  srep = ssrep.str();
136  }
137  while ( ipos != npos ) {
138  fname.replace(ipos, 7, srep);
139  ipos = fname.find("%CHAN1%", ipos+7);
140  }
141  }
142  ipos = fname.find("%CHAN2%");
143  if ( ipos != npos ) {
144  string srep = "NOCHAN";
145  if ( acds.size() ) {
146  ostringstream ssrep;
147  ssrep << acds.rbegin()->first;
148  string srep = ssrep.str();
149  while ( ipos != npos ) {
150  fname.replace(ipos, 7, srep);
151  ipos = fname.find("%CHAN2%", ipos+7);
152  }
153  }
154  }
155  pout = new ofstream(fname.c_str());
156  }
157  if ( pout == nullptr ) return ret.setStatus(2);
158  ostream& out = *pout;
159  string pre = m_Prefix + ":";
160  if ( m_ShowChannelCount ) out << pre << " Channel count: " << acds.size() << endl;
161  Index wcha = 6;
162  Index wcou = 0;
163  if ( m_ShowRaw || m_ShowPrepared ) {
164  out << pre << " Values are displayed starting at tick " << m_ShowFirst;
165  if ( m_ShowRebin > 1 ) out << " with rebinning of " << m_ShowRebin;
166  else out << " without rebinning";
167  out << endl;
168  }
169  for ( const AdcChannelDataMap::value_type& iacd : acds ) {
170  const AdcChannelData& acd = iacd.second;
171  ostringstream sschanpre;
172  sschanpre << pre << setw(wcha) << acd.channel() << ":";
173  string chanpre = sschanpre.str();
174  sschanpre.str("");
175  sschanpre << pre << setw(wcha+1) << " ";
176  string nochanpre = sschanpre.str();
177  if ( m_ShowTickCounts ) {
178  out << chanpre;
179  out << " nraw=" << setw(wcou) << acd.raw.size();
180  out << " nsam=" << setw(wcou) << acd.samples.size();
181  out << " nflg=" << setw(wcou) << acd.flags.size();
182  out << " nsig=" << setw(wcou) << acd.signal.size();
183  out << " nroi=" << setw(wcou) << acd.rois.size();
184  out << endl;
185  chanpre = nochanpre;
186  }
187  if ( m_ShowRaw ) {
188  DisplayVector<AdcCountVector> dvec(m_ShowFirst, m_ShowRebin, m_ShowMax, acd.raw);
189  out << chanpre << " Raw:";
190  if ( m_ShowOpt == 2 ) {
191  out << " |";
192  for ( AdcCount val : dvec.vout ) out << setw(1) << charThresh(val, m_ShowThreshold);
193  out << "|";
194  } else {
195  for ( AdcCount val : dvec.vout ) out << setw(6) << val;
196  }
197  out << endl;
198  chanpre = nochanpre;
199  }
200  if ( m_ShowPrepared ) {
201  DisplayVector<AdcSignalVector> dvec(m_ShowFirst, m_ShowRebin, m_ShowMax, acd.samples);
202  out << chanpre << " Prp:";
203  if ( m_ShowOpt == 2 ) {
204  out << " |";
205  for ( AdcCount val : dvec.vout ) out << setw(1) << charThresh(val, m_ShowThreshold);
206  out << "|";
207  } else {
208  for ( AdcCount val : dvec.vout ) out << setw(6) << int(round(val));
209  }
210  out << endl;
211  chanpre = nochanpre;
212  }
213  }
214  if ( newfile ) delete pout;
215  return ret;
216 }
217 
218 //**********************************************************************
219 
AdcDataDumper(fhicl::ParameterSet const &ps)
bool m_ShowChannelCount
Definition: AdcDataDumper.h:54
#define DEFINE_ART_CLASS_TOOL(tool)
Definition: ToolMacros.h:42
unsigned int Index
DataMap & setStatus(int stat)
Definition: DataMap.h:130
std::string string
Definition: nybbler.cc:12
float AdcSignal
Definition: AdcTypes.h:21
unsigned int m_ShowRebin
Definition: AdcDataDumper.h:59
unsigned int Index
unsigned int m_ShowFirst
Definition: AdcDataDumper.h:58
AdcRoiVector rois
std::string m_Prefix
Definition: AdcDataDumper.h:52
std::ostream * m_pout
Definition: AdcDataDumper.h:65
static constexpr double ps
Definition: Units.h:99
std::string m_FileName
Definition: AdcDataDumper.h:51
AdcCountVector raw
unsigned int m_ShowMax
Definition: AdcDataDumper.h:60
Q_EXPORT QTSManip setw(int w)
Definition: qtextstream.h:331
Channel channel() const
AdcFilterVector signal
unsigned int m_ShowOpt
Definition: AdcDataDumper.h:62
~AdcDataDumper() override
std::map< AdcChannel, AdcChannelData > AdcChannelDataMap
bool m_ShowTickCounts
Definition: AdcDataDumper.h:55
auto const & get(AssnsNode< L, R, D > const &r)
Definition: AssnsNode.h:115
short AdcCount
Definition: AdcTypes.h:18
def rebin(a, args)
Definition: arrays.py:2
int bool
Definition: qglobal.h:345
float m_ShowThreshold
Definition: AdcDataDumper.h:61
AdcSignalVector samples
QTextStream & endl(QTextStream &s)
AdcFlagVector flags
DataMap viewMap(const AdcChannelDataMap &acds) const override