AdcChannelDumper_tool.cc
Go to the documentation of this file.
1 // AdcChannelDumper_tool.cc
2 
3 #include "AdcChannelDumper.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 using std::fixed;
17 using std::setprecision;
18 
19 using Index = unsigned int;
20 
21 //**********************************************************************
22 // Class methods.
23 //**********************************************************************
24 
26 : m_FileName(ps.get<string>("FileName")),
27  m_Prefix(ps.get<string>("Prefix")),
28  m_NewFile(ps.get<bool>("NewFile")),
29  m_MaxSample(ps.get<int>("MaxSample")),
30  m_pout(nullptr) {
31  if ( m_FileName.size() == 0 ) m_pout = &cout;
32  else if ( ! m_NewFile ) m_pout = new ofstream(m_FileName.c_str());
33 }
34 
35 //**********************************************************************
36 
38  if ( m_pout != nullptr && m_pout != &cout ) {
39  delete m_pout;
40  m_pout = nullptr;
41  }
42 }
43 
44 //**********************************************************************
45 
47  DataMap res;
48  ostream* pout = m_pout;
49  bool newfile = pout == nullptr;
50  // If file is not already set, build the file name and open it.
51  if ( newfile ) {
52  if ( ! m_NewFile ) return res.setStatus(1);
53  string fname = m_FileName;
54  pout = new ofstream(fname.c_str());
55  }
56  if ( pout == nullptr ) return res.setStatus(2);
57  ostream& out = *pout;
58  string pre = m_Prefix;
59  string sbad = "<Unknown>";
60  out << pre << " Run: ";
61  if ( acd.run() == AdcChannelData::badIndex() ) out << sbad;
62  else out << acd.run();
63  out << "-";
64  if ( acd.subRun() == AdcChannelData::badIndex() ) out << sbad;
65  else out << acd.subRun();
66  out << endl;
67  out << pre << " Event: ";
68  if ( acd.event() == AdcChannelData::badIndex() ) out << sbad;
69  else out << acd.event();
70  out << endl;
71  out << pre << " Channel: ";
72  if ( acd.channel() == AdcChannelData::badChannel() ) out << sbad;
73  else out << acd.channel();
74  out << endl;
75  out << pre << " Pedestal: ";
76  if ( acd.pedestal == AdcChannelData::badSignal() ) out << sbad;
77  else out << acd.pedestal;
78  out << endl;
79  Index nraw = acd.raw.size();
80  Index nflg = acd.flags.size();
81  Index nprp = acd.samples.size();
82  Index nsig = acd.signal.size();
83  Index nkeep = 0;
84  for ( bool keep : acd.signal ) if ( keep ) ++nkeep;
85  Index nsam = nraw;
86  if ( nprp > nsam ) nsam = nprp;
87  if ( m_MaxSample >= 0 ) {
88  Index maxsam = m_MaxSample;
89  if ( maxsam < nsam ) nsam = maxsam;
90  }
91  Index widx = 2 + log10(nsam);
92  if ( widx < 6 ) widx = 6;
93  Index wraw = 6;
94  Index wflg = 4;
95  Index wprp = 9;
96  Index wsig = 2;
97  Index wcnt = wprp;
98  out << pre << " Nraw: " << setw(wcnt) << nraw << endl;
99  out << pre << " Nprep: " << setw(wcnt) << nprp << endl;
100  out << pre << " Nsig: " << setw(wcnt) << acd.signal.size() << endl;
101  out << pre << " Nkeep: " << setw(wcnt) << nkeep << endl;
102  out << pre << " Nroi: " << setw(wcnt) << acd.rois.size() << endl;
103  out << pre << " Ndftm: " << setw(wcnt) << acd.dftmags.size() << endl;
104  out << pre << " Ndftp: " << setw(wcnt) << acd.dftphases.size() << endl;
105  if ( nsam > 0 ) {
106  out << pre
107  << setw(widx) << "Data:"
108  << setw(wraw) << "Raw"
109  << setw(wflg) << "Flg"
110  << setw(wprp) << "Prepared"
111  << setw(wsig) << "S" << endl;
112  }
113  for ( Index isam=0; isam<nsam; ++isam ) {
114  ostringstream ssline;
115  ssline << setw(widx) << isam;
116  if ( isam < nraw ) ssline << setw(wraw) << acd.raw[isam];
117  else ssline << setw(wraw) << "";
118  if ( isam < nflg ) ssline << setw(wflg) << acd.flags[isam];
119  else ssline << setw(wflg) << "";
120  if ( isam < nprp ) ssline << fixed << setprecision(2) << setw(wprp) << acd.samples[isam];
121  else ssline << setw(wprp) << "";
122  if ( isam < nsig ) ssline << setw(wsig) << acd.signal[isam];
123  else ssline << setw(wsig) << "";
124  out << pre << ssline.str() << endl;
125  }
126  if ( newfile ) delete pout;
127  return res;
128 }
129 
130 //**********************************************************************
131 
DataMap view(const AdcChannelData &acd) const override
#define DEFINE_ART_CLASS_TOOL(tool)
Definition: ToolMacros.h:42
std::string m_Prefix
AdcIndex subRun() const
unsigned int Index
DataMap & setStatus(int stat)
Definition: DataMap.h:130
std::string string
Definition: nybbler.cc:12
unsigned int Index
Q_EXPORT QTSManip setprecision(int p)
Definition: qtextstream.h:343
std::string m_FileName
static Index badSignal()
AdcRoiVector rois
AdcIndex run() const
AdcChannelDumper(fhicl::ParameterSet const &ps)
AdcIndex event() const
AdcSignalVector dftphases
static constexpr double ps
Definition: Units.h:99
AdcCountVector raw
Q_EXPORT QTSManip setw(int w)
Definition: qtextstream.h:331
Channel channel() const
AdcSignal pedestal
AdcFilterVector signal
static Index badChannel()
auto const & get(AssnsNode< L, R, D > const &r)
Definition: AssnsNode.h:115
static Index badIndex()
std::ostream * m_pout
int bool
Definition: qglobal.h:345
AdcSignalVector samples
AdcSignalVector dftmags
QTextStream & endl(QTextStream &s)
AdcFlagVector flags