FrameUtils.cxx
Go to the documentation of this file.
2 #include "FrameUtils.h"
3 
4 #include <algorithm>
5 #include <unordered_map>
6 #include <iostream>
7 
8 using namespace WireCell;
9 
11 {
12  auto traces = frame->traces();
13  const size_t ntraces = traces->size();
14  std::vector<double> means, rmses, lengths, tbins;
15  for (auto trace : *traces) {
16  auto const& charge = trace->charge();
17 
18  auto mr = Waveform::mean_rms(charge);
19  means.push_back(mr.first);
20  rmses.push_back(mr.second*mr.second);
21  const int nsamps = charge.size();
22  lengths.push_back(nsamps);
23  tbins.push_back(trace->tbin());
24 
25  if (std::isnan(mr.second)) {
26  std::cerr << "Frame: channel " << trace->channel() << " rms is NaN\n";
27  }
28 
29  for (int ind=0; ind<nsamps; ++ind) {
30  float val = charge[ind];
31  if (std::isnan(val)) {
32  std::cerr << "Frame: channel " << trace->channel() << " sample " << ind << " is NaN\n";
33  }
34  if (std::isinf(val)) {
35  std::cerr << "Frame: channel " << trace->channel() << " sample " << ind << " is INF\n";
36  }
37  }
38  }
39  double meanmean = Waveform::sum(means)/ntraces;
40  double totrms = sqrt(Waveform::sum(rmses));
41  double meanlen = Waveform::sum(lengths)/ntraces;
42  double meantbin = Waveform::sum(tbins)/ntraces;
43  std::cerr << "Frame: " << ntraces << " traces,"
44  << " <mean>=" << meanmean
45  << " TotRMS=" << totrms
46  << " <tbin>=" << meantbin
47  << " <len>=" << meanlen
48  << std::endl;
49  for (auto it : frame->masks()) {
50  std::cerr << "\t" << it.first << " : " << it.second.size() << std::endl;
51  }
52 }
53 
54 
57  const std::vector<int>& channels)
58 {
59  const size_t nchannels = channels.size();
60  std::unordered_map<int, size_t> ch2col;
61  for (size_t ind=0; ind<nchannels; ++ind) {
62  ch2col[channels[ind]] = ind;
63  }
64 
65  const size_t ncols = block.cols();
66  for (auto trace : traces) {
67  const auto& samples = trace->charge();
68  const size_t nsamples = samples.size();
69  const size_t tbin = trace->tbin();
70 
71  if (tbin >= ncols) { // underflow impossible as they are unsigned.
72  continue; // trace is off screen
73  }
74 
75  // find the row to fill
76  const int ch = trace->channel();
77  const auto chit = ch2col.find(ch);
78  if (chit == ch2col.end()) {
79  continue;
80  }
81  const size_t irow = chit->second;
82  WireCell::Array::array_xf tofill(ncols-tbin);
83  const size_t maxbin = std::min(nsamples, ncols-tbin);
84  for (size_t bin=0; bin<maxbin; ++bin) {
85  tofill[bin] = samples[bin];
86  }
87  block.block(irow, tbin, 1, ncols-tbin).row(0) += tofill;
88  }
89 }
90 
91 int wct::sigproc::maxcount_baseline(const ITrace::vector& traces, const WireCell::Binning& binning)
92 {
93  std::vector<int> hist(binning.nbins(), 0);
94  for (auto trace : traces) {
95  auto const& samples = trace->charge();
96  for (auto sample : samples) {
97  hist[binning.bin(sample)] += 1;
98  }
99  }
100  auto mme = std::minmax_element(hist.begin(), hist.end());
101  return mme.second - hist.begin();
102 }
103 
105 {
106  auto const& all_traces = frame->traces();
107  ITrace::vector ret;
108  for (size_t index : frame->tagged_traces(tag)) {
109  ret.push_back(all_traces->at(index));
110  }
111  if (!ret.empty()) {
112  return ret;
113  }
114  auto ftags = frame->frame_tags();
115  if (std::find(ftags.begin(), ftags.end(), tag) == ftags.end()) {
116  return ret;
117  }
118  return *all_traces; // must make copy
119 }
120 
121 
122 
123 // Local Variables:
124 // mode: c++
125 // c-basic-offset: 4
126 // End:
std::shared_ptr< const IFrame > pointer
Definition: IData.h:19
Eigen::ArrayXf array_xf
Definition: Array.h:41
void dump_frame(WireCell::IFrame::pointer frame)
Definition: FrameUtils.cxx:10
std::vector< pointer > vector
Definition: IData.h:21
void raster(WireCell::Array::array_xxf &block, WireCell::ITrace::vector traces, const std::vector< int > &channels)
Definition: FrameUtils.cxx:55
Binning tbins(nticks, t0, t0+readout_time)
dummy_int isinf(...)
Definition: format.h:276
int bin(double val) const
Definition: Binning.h:80
Definition: Main.h:22
std::string tag_t
Definition: IFrame.h:28
T min(sqlite3 *const db, std::string const &table_name, std::string const &column_name)
Definition: statistics.h:55
Val sum(const Sequence< Val > &seq)
Return sum of all entries in sequence.
Definition: Waveform.h:178
int maxcount_baseline(const WireCell::ITrace::vector &traces, const WireCell::Binning &binning=WireCell::Binning(4096, 0, 4096))
std::pair< double, double > mean_rms(const realseq_t &wave)
Definition: Waveform.cxx:24
FMT_CONSTEXPR bool find(Ptr first, Ptr last, T value, Ptr &out)
Definition: format.h:1970
WireCell::ITrace::vector tagged_traces(WireCell::IFrame::pointer frame, WireCell::IFrame::tag_t tag)
QTextStream & bin(QTextStream &s)
int nbins() const
Definition: Binning.h:42
QTextStream & endl(QTextStream &s)
dummy_int isnan(...)
Definition: format.h:278
Eigen::ArrayXXf array_xxf
A real, 2D array.
Definition: Array.h:54