AdcChannelFFT_tool.cc
Go to the documentation of this file.
1 // AdcChannelFFT_tool.cc
2 
3 #include "AdcChannelFFT.h"
5 #include <iostream>
6 #include <sstream>
7 #include <vector>
8 #include <iomanip>
9 #include "TComplex.h"
10 
11 using std::string;
12 using std::cout;
13 using std::cin;
14 using std::endl;
15 using std::vector;
16 using std::setw;
17 using std::fixed;
18 using DFT = DuneFFT::DFT;
19 
20 //**********************************************************************
21 // Class methods.
22 //**********************************************************************
23 
25 : m_LogLevel(ps.get<int>("LogLevel")),
26  m_FirstTick(ps.get<Index>("FirstTick")),
27  m_NTick(ps.get<Index>("NTick")),
28  m_Action(ps.get<Index>("Action")),
29  m_ReturnOpt(ps.get<Index>("ReturnOpt")),
30  m_DataView(ps.get<Name>("DataView")) {
31  const string myname = "AdcChannelFFT::ctor: ";
32  if ( m_LogLevel ) {
33  cout << myname << "Configuration: " << endl;
34  cout << myname << " LogLevel: " << m_LogLevel << endl;
35  cout << myname << " FirstTick: " << m_FirstTick << endl;
36  cout << myname << " NTick: " << m_NTick << endl;
37  cout << myname << " Action: " << m_Action << endl;
38  cout << myname << " ReturnOpt: " << m_ReturnOpt << endl;
39  cout << myname << " DataView: " << m_DataView << endl;
40  }
41 }
42 
43 //**********************************************************************
44 
46  const string myname = "AdcChannelFFT::view: ";
47  DataMap retTop;
48  if ( m_DataView.size() == 0 ) return viewTop(acd);
49  if ( ! acd.hasView(m_DataView) ) {
50  if ( m_LogLevel >= 2 ) {
51  cout << myname << "View " << m_DataView << " not found for event " << acd.event()
52  << " channel " << acd.channel() << endl;
53  }
54  return retTop.setStatus(1);
55  }
56  Index nproc = 0;
57  Index nfail = 0;
58  AdcIndex nvie = acd.viewSize(m_DataView);
59  for ( AdcIndex ivie=0; ivie<nvie; ++ivie ) {
60  const AdcChannelData* pacd = acd.viewEntry(m_DataView, ivie);
61  DataMap ret = viewTop(*pacd);
62  ++nproc;
63  if ( ret ) ++nfail;
64  }
65  retTop.setInt("fftNproc", nproc);
66  retTop.setInt("fftNfail", nproc);
67  if ( nfail ) retTop.setStatus(2);
68  return retTop;
69 }
70 
71 //**********************************************************************
72 
74  const string myname = "AdcChannelFFT::update: ";
75  if ( m_DataView.size() == 0 ) return updateTop(acd);
76  DataMap retTop;
77  if ( ! acd.hasView(m_DataView) ) {
78  if ( m_LogLevel >= 2 ) {
79  cout << myname << "View " << m_DataView << " not found for event " << acd.event()
80  << " channel " << acd.channel() << endl;
81  }
82  return retTop.setStatus(1);
83  }
84  Index nproc = 0;
85  Index nfail = 0;
86  AdcIndex nent = acd.viewSize(m_DataView);
87  for ( AdcIndex ient=0; ient<nent; ++ient ) {
88  AdcChannelData* pacd = acd.mutableViewEntry(m_DataView, ient);
89  DataMap ret;
90  if ( pacd == nullptr ) {
91  cout << myname << "Channel " << acd.channel() << " view entry "
92  << m_DataView << "[" << ient << "] is null." << endl;
93  ret.setStatus(99);
94  } else {
95  ret = updateTop(*pacd);
96  }
97  ++nproc;
98  if ( ret ) ++nfail;
99  }
100  retTop.setInt("fftNproc", nproc);
101  retTop.setInt("fftNfail", nproc);
102  if ( nfail ) retTop.setStatus(2);
103  if ( m_LogLevel >= 3 ) {
104  cout << myname << "Channel " << acd.channel() << " entry counts: "
105  << nproc << " processed, " << nfail << " failed." << endl;
106  }
107  return retTop;
108 }
109 
110 //**********************************************************************
111 
113  const string myname = "AdcChannelFFT::view: ";
114  DataMap ret;
118  internalView(acd, sams, mags, phas, ret);
119  return ret;
120 }
121 
122 //**********************************************************************
123 
125  const string myname = "AdcChannelFFT::update: ";
126  DataMap ret;
130  internalView(acd, sams, mags, phas, ret);
131  if ( ret ) return ret;
132  if ( m_Action == 3 || m_Action == 4 ) {
133  if ( mags.size() ) {
134  if ( m_LogLevel >= 2 ) cout << myname << "Saving DFT." << endl;
135  acd.dftmags = mags;
136  acd.dftphases = phas;
137  }
138  }
139  if ( m_Action == 13 || m_Action == 14 ) {
140  if ( sams.size() ) {
141  if ( m_LogLevel >= 2 ) cout << myname << "Saving samples." << endl;
142  acd.samples = sams;
143  }
144  }
145  return ret;
146 }
147 
148 //**********************************************************************
149 
150 void AdcChannelFFT::
151 internalView(const AdcChannelData& acd, FloatVector& sams, FloatVector& xams, FloatVector& xphs, DataMap& ret) const {
152  const string myname = "AdcChannelFFT::internalView: ";
153  bool doForward = false;
154  bool doInverse = false;
155  if ( m_Action <= 4 ) {
156  if ( m_Action > 0 ) {
157  if ( m_Action == 2 || m_Action == 4 ) {
158  doForward = acd.dftmags.size() == 0;
159  } else {
160  doForward = true;
161  }
162  }
163  } else if ( m_Action >= 10 && m_Action <= 14 ) {
164  if ( m_Action != 10 ) {
165  if ( m_Action == 12 || m_Action == 14 ) {
166  doInverse = acd.samples.size() == 0;
167  } else {
168  doInverse = true;
169  }
170  }
171  } else {
172  cout << myname << "ERROR: Invalid action: " << m_Action << endl;
173  ret.setStatus(1);
174  return;
175  }
176  Index isam0 = 0;
177  Index nsam = 0;
179  DFT dft(dftNorm);
180  int passLog = m_LogLevel < 3 ? 0 : m_LogLevel - 3;
181  if ( doForward ) {
182  isam0 = m_FirstTick;
183  if ( isam0 >= acd.samples.size() ) {
184  cout << myname << "WARNING: No data in range." << endl;
185  ret.setStatus(11);
186  return;
187  }
188  nsam = acd.samples.size() - isam0;
189  if ( m_LogLevel >= 3 ) cout << myname << "Forward FFT with " << nsam << " samples." << endl;
190  if ( m_NTick > 0 && m_NTick < nsam ) nsam = m_NTick;
191  int rstat = DuneFFT::fftForward(nsam, &acd.samples[isam0], dft, passLog);
192  if ( rstat ) {
193  ret.setStatus(10+rstat);
194  cout << myname << "WARNING: Forward FFT failed." << endl;
195  return;
196  }
197  } else if ( doInverse ) {
198  dft.copyIn(acd.dftmags, acd.dftphases);
199  if ( ! dft.isValid() ) {
200  ret.setStatus(20);
201  cout << "ERROR: Unable to find DFT in AdcChannelData." << endl;
202  return;
203  }
204  int rstat = DuneFFT::fftInverse(dft, sams, passLog);
205  xams = acd.dftmags;
206  xphs = acd.dftphases;
207  if ( m_LogLevel >= 3 ) cout << myname << "Inverse FFT for " << dft.size() << " samples." << endl;
208  if ( rstat ) {
209  ret.setStatus(20+rstat);
210  cout << myname << "WARNING: Inverse FFT failed." << endl;
211  return;
212  }
213  }
214  // Fetch return data stored in the DFT.
215  Index dftRet = m_ReturnOpt % 10;
216  if ( dftRet >= 3 ) {
217  FloatVector fftres(nsam);
218  FloatVector fftims(nsam);
219  for ( Index ifrq=0; ifrq<nsam; ++ifrq ) {
220  fftres[ifrq] = dft.real(ifrq);
221  fftims[ifrq] = dft.imag(ifrq);
222  }
223  ret.setFloatVector("fftReals", fftres);
224  ret.setFloatVector("fftImags", fftims);
225  }
226  // Move data out of the DFT (leaving it invalid).
227  if ( doForward ) dft.moveOut(xams, xphs);
228  // Fetch return data not stored in the DFT.
229  if ( m_ReturnOpt >= 1 ) {
230  ret.setInt("fftTick0", isam0);
231  ret.setInt("fftNTick", nsam);
232  }
233  if ( dftRet >= 1 ) {
234  ret.setInt("fftNMag", xams.size());
235  ret.setInt("fftNPhase", xphs.size());
236  }
237  if ( dftRet >= 2 ) {
238  ret.setFloatVector("fftMags", xams);
239  ret.setFloatVector("fftPhases", xphs);
240  }
241  if ( m_ReturnOpt >= 10 ) {
242  if ( sams.size() ) ret.setFloatVector("fftSamples", sams);
243  else ret.setFloatVector("fftSamples", acd.samples);
244  }
245  return;
246 }
247 
248 //**********************************************************************
249 
std::vector< float > FloatVector
Definition: DataMap.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
struct vector vector
static AdcIndex dftNormalization()
CompactRealDftData< float > DFT
Definition: DuneFFT.h:28
virtual bool isValid() const
Definition: RealDftData.h:42
F imag(Index ifrq) const override
AdcChannelFFT(fhicl::ParameterSet const &ps)
Index size() const
Definition: RealDftData.h:35
std::string Name
Definition: AdcChannelFFT.h:81
static int fftForward(Index ntick, const float *psam, DFT &dft, Index logLevel=0)
Definition: DuneFFT.cxx:23
void setInt(Name name, int val)
Definition: DataMap.h:131
AdcIndex event() const
AdcSignalVector dftphases
static constexpr double ps
Definition: Units.h:99
unsigned int AdcIndex
Definition: AdcTypes.h:15
DataMap updateTop(AdcChannelData &acd) const
Q_EXPORT QTSManip setw(int w)
Definition: qtextstream.h:331
Channel channel() const
AdcChannelData * mutableViewEntry(Name vpnam, AdcIndex ient)
static int fftInverse(const DFT &dft, FloatVector &sams, Index logLevel=0)
Definition: DuneFFT.cxx:79
F real(Index ifrq) const override
DataMap viewTop(const AdcChannelData &acd) const
const AdcChannelData * viewEntry(Name vpnam, AdcIndex ient) const
AdcSignalVector FloatVector
Definition: AdcChannelFFT.h:79
size_t viewSize() const
auto const & get(AssnsNode< L, R, D > const &r)
Definition: AssnsNode.h:115
int moveOut(FloatVector &amps, FloatVector &phas)
int copyIn(const FloatVector &amps, const FloatVector &phas)
bool hasView(Name vnam) const
void setFloatVector(Name name, const FloatVector &val)
Definition: DataMap.h:134
DataMap view(const AdcChannelData &acd) const override
DataMap update(AdcChannelData &acd) const override
AdcSignalVector samples
AdcSignalVector dftmags
QTextStream & endl(QTextStream &s)
void internalView(const AdcChannelData &acd, FloatVector &sams, FloatVector &amps, FloatVector &phas, DataMap &ret) const