AdcChannelDftPlotter.h
Go to the documentation of this file.
1 // AdcChannelDftPlotter.h
2 
3 // David Adams
4 // April 2018
5 //
6 // Tool to plot DFT info for ADC channel data.
7 //
8 // Four plots are supported: graphs of DFT magnitude and phase and
9 // histograms of power and power/tick. All are shown as a function of frequency.
10 //
11 // The DFT magnitude and phase are drawn with graphs with one point for
12 // each relevant frequency, i.e. (Nsam + 2)/2 values.
13 //
14 // The power and power/tick are drawn as binned histograms with range half the
15 // sampling frequency. That value and the and binning are specified in the
16 // configuration.
17 //
18 // Configuration:
19 // AdcMultiChannelPlotter params with XXX = Plot
20 // Variable - Variable to plot:
21 // magnitude - graph of DFT magnitude
22 // phase - graph of DFT phase
23 // power - histogram of power
24 // power/tick - histogram of power/tick
25 // ChannelStatusFlag - Indicates channels to skip for multi-channel plots.
26 // 0 - keep all
27 // 1 - skip bad channels
28 // 2 - skip noisy channels
29 // 3 - skip bad and noisy channels
30 // ChannelSelection - TFormula string for selecting channels to be retained.
31 // Formula parameters can be any AdcChannelDataAttribute.
32 // Channel is retained if formal returns nonzero.
33 // E.g. "[samRms]>0.5".
34 // SampleFreq - Sampling frequency in kHz used to define the x-axis.
35 // If zero, frequency index is used instead.
36 // XMin - Specifies the min value for X-axis in histogram and plot
37 // XMax - Specifies the max value for X-axis in histogram and plot
38 // YMax - Specifies the maximum value for the Y-axis in the plot:
39 // 0: Use automatic value
40 // > 0: Use YMax
41 // < 0: Use -YMax if there are no higher values, otherwise automatic
42 // YMinLog - If nonzero, log scale is used with this minimum.
43 // NbinX - # bins in the power histogram
44 // HistName - Histogram/graph name
45 // HistTitle - Histogram/graph title
46 // HistSummaryTitles - Histogram/graph titles
47 // Plus the parameters specified in AdcMultiChannelPlotter with XXX = Plot.
48 //
49 // The X-range is calculated automatically if Xmin >= Xmax.
50 
51 #ifndef AdcChannelDftPlotter_H
52 #define AdcChannelDftPlotter_H
53 
55 #include "fhiclcpp/ParameterSet.h"
57 #include "TH1.h"
58 #include "TFormula.h"
59 #include <memory>
60 
62 class TPadManipulator;
63 
65 
66 public:
67 
68  using Index = unsigned int;
69  using Name = std::string;
70 
72 
73  ~AdcChannelDftPlotter() override;
74 
75  // Inherited methods.
76  DataMap view(const AdcChannelData& acd) const override;
77  int viewMapChannels(Name crn, const AcdVector& acds, TPadManipulator& man, Index ncr, Index icr) const override;
78  int viewMapSummary(Index ilev, Name cgn, Name crn, TPadManipulator& man, Index ncr, Index icr) const override;
79  bool updateWithView() const override { return true; }
80  DataMap beginEvent(const DuneEventInfo&) const override;
81  DataMap endEvent(const DuneEventInfo&) const override;
82 
83 private:
84 
85  // Configuration data.
89  float m_SampleFreq;
90  double m_XMin; // Need double so user can shift bins
91  double m_XMax;
92  float m_YMax;
93  float m_YMinLog;
98 
99  // Derived from configuration.
100  bool m_skipBad;
103  std::unique_ptr<TFormula> m_ptfsel;
104 
105  // ADC string tools.
107 
108  // Subclass holding state.
109  // For each channel, keep a count and a histogram with sum over calls
110  // if the variable is power.
111  // counts: # calls for each channel range
112  // nchans: summed # channels for each channel range
113  // nvens: summed # view entries for each channel range
114  // hists: histogram with sum over calls for each channel range
115  //
116  using IndexMap = std::map<Name, Index>;
117  using HistMap = std::map<Name, TH1*>;
118  class SubState {
119  public:
121  for ( HistMap::value_type ihst : hists ) delete ihst.second;;
122  }
123  Index& count(Name crn) {
124  if ( ! counts.count(crn) ) counts[crn] = 0;
125  return counts[crn];
126  }
127  Index& nchan(Name crn) {
128  if ( ! nchans.count(crn) ) nchans[crn] = 0;
129  return nchans[crn];
130  }
132  if ( ! nvens.count(crn) ) nvens[crn] = 0;
133  return nvens[crn];
134  }
135  TH1*& hist(Name crn) {
136  if ( ! hists.count(crn) ) hists[crn] = nullptr;
137  return hists[crn];
138  }
139  // # of CRN's with non-null hists.
140  Index histCount() const {
141  Index cnt = 0;
142  for ( HistMap::value_type ihst : hists ) {
143  if ( ihst.second != nullptr ) ++cnt;
144  }
145  return cnt;
146  }
147  void clear() {
148  counts.clear();
149  nchans.clear();
150  nvens.clear();
151  hists.clear();
152  }
157  };
158  class State {
159  public:
160  SubState& jobState() { return m_ss[0]; };
161  SubState& eventState() { return m_ss[1]; };
162  SubState m_ss[2];
163  // Current event and CRNs for the event.
164  Index event = 0;
166  // Set the event add a channel range.
167  // If the event changes, the vector of CRNs is first cleared.
168  // Returns nonzero if the channel range is already included.
169  int setEventChannelRange(Index a_event, Name crn) {
170  if ( event != a_event ) {
171  event = a_event;
172  eventChannelRanges.clear();
173  }
174  if ( find(eventChannelRanges.begin(), eventChannelRanges.end(), crn)
175  != eventChannelRanges.end() ) return 1;
176  eventChannelRanges.push_back(crn);
177  return 0;
178  }
179  };
180 
181  // State.
182  std::shared_ptr<State> m_pstate; // Shared allows copy/assignment
183  State& getState() const { return *m_pstate; };
184  SubState& getSubState(Index ilev) const {
185  if ( ilev > 1 ) abort();
186  return getState().m_ss[ilev];
187  }
188  SubState& getJobState() const { return m_pstate->jobState(); };
189  SubState& getEventState() const { return m_pstate->eventState(); };
190 
191  // Internal method to view a channel and put hist/graph in result.
192  DataMap viewLocal(Name crn, const AcdVector& acds) const;
193 
194  // Fill the pad for a channel.
195  // Histogram "dftHist" or graph "dftGraph" from dm is drawn.
196  int fillPad(DataMap& dm, TPadManipulator& man) const;
197 
198  // Use selection formla to check if a channel should be skipped, i.e.
199  // if formal returns zero.
200  bool skipChannel(const AdcChannelData& acd) const;
201 
202 };
203 
204 
205 #endif
SubState & getSubState(Index ilev) const
std::vector< Name > NameVector
DataMap beginEvent(const DuneEventInfo &) const override
unsigned int Index
std::string string
Definition: nybbler.cc:12
DataMap endEvent(const DuneEventInfo &) const override
bool skipChannel(const AdcChannelData &acd) const
std::map< Name, TH1 * > HistMap
SubState & getJobState() const
SubState & getEventState() const
DataMap viewLocal(Name crn, const AcdVector &acds) const
bool updateWithView() const override
int setEventChannelRange(Index a_event, Name crn)
std::map< Name, Index > IndexMap
int viewMapSummary(Index ilev, Name cgn, Name crn, TPadManipulator &man, Index ncr, Index icr) const override
int viewMapChannels(Name crn, const AcdVector &acds, TPadManipulator &man, Index ncr, Index icr) const override
std::unique_ptr< TFormula > m_ptfsel
static constexpr double ps
Definition: Units.h:99
AdcChannelDftPlotter(fhicl::ParameterSet const &ps)
std::shared_ptr< State > m_pstate
DataMap view(const AdcChannelData &acd) const override
const AdcChannelStringTool * m_adcStringBuilder
int fillPad(DataMap &dm, TPadManipulator &man) const
std::vector< const AdcChannelData * > AcdVector
Event finding and building.