FrameUtil.cxx
Go to the documentation of this file.
4 
5 #include <algorithm>
6 #include <iostream>
7 
8 using namespace std;
9 using namespace WireCell;
10 
11 
12 typedef std::pair<int, const std::vector<float>* > tbin_charge_t;
13 typedef std::map<int, std::vector<tbin_charge_t> > channel_index_t;
14 
15 IFrame::pointer Gen::sum(std::vector<IFrame::pointer> frames, int ident)
16 {
17  // Extract starting times and ticks of all frames
18  const int nframes = frames.size();
19  std::vector<double> times(nframes), ticks(nframes);
20  for (int ind=0; ind<nframes; ++ind) {
21  times[ind] = frames[ind]->time();
22  ticks[ind] = frames[ind]->tick();
23  }
24  auto tick_mm = std::minmax_element(ticks.begin(), ticks.end());
25  if (*tick_mm.first != *tick_mm.second) {
26  return nullptr;
27  }
28  const double tick = ticks[0];
29  auto times_mm = std::minmax_element(times.begin(), times.end());
30  const double start_time = *times_mm.first;
31 
32  // Make an temporary index mapping channel to all the trace data,
33  // applying time offsets as we fill.
35  for (int ind=0; ind<nframes; ++ind) {
36  IFrame::pointer frame = frames[ind];
37  const double dt = frame->time() - start_time;
38  const int tbinoff = dt/tick;
39  for (auto trace : *frame->traces()) {
40  const int ch = trace->channel();
41  const int new_tbin = tbinoff + trace->tbin();
42  index[ch].push_back(make_pair(new_tbin, &(trace->charge())));
43  }
44  }
45 
46  // Process each channel to make flattened trace.
47  ITrace::vector out_traces;
48  for (auto ctc : index) {
49  auto ch = ctc.first;
50  auto tcv = ctc.second; // vector of tbin_charge_t
51  vector<int> tbins;
52  for (auto tc : tcv) { // go through once to find tbin bounds
53  const int tbin = tc.first;
54  tbins.push_back(tbin);
55  tbins.push_back(tbin+tc.second->size());
56  }
57  auto tbin_mm = std::minmax_element(tbins.begin(), tbins.end());
58  const int trace_tbin = *tbin_mm.first;
59  const int trace_nbins = *tbin_mm.second-trace_tbin;
60  vector<float> charge(trace_nbins, 0.0);
61  for (auto tc : tcv) {
62  int ind = tc.first - trace_tbin;
63  for (auto q : *tc.second) {
64  charge[ind] += q;
65  ++ind;
66  }
67  }
68  ITrace::pointer trace = make_shared<SimpleTrace>(ch, trace_tbin, charge);
69  out_traces.push_back(trace);
70  }
71 
72  return make_shared<SimpleFrame>(ident, start_time, out_traces, tick);
73 }
std::shared_ptr< const IFrame > pointer
Definition: IData.h:19
std::map< int, std::vector< tbin_charge_t > > channel_index_t
Definition: FrameUtil.cxx:13
STL namespace.
std::vector< pointer > vector
Definition: IData.h:21
tick ticks
Alias for common language habits.
Definition: electronics.h:77
const double tick
Binning tbins(nticks, t0, t0+readout_time)
IFrame::pointer sum(std::vector< IFrame::pointer > frames, int ident)
Definition: FrameUtil.cxx:15
Definition: Main.h:22
std::pair< int, const std::vector< float > * > tbin_charge_t
Definition: FrameUtil.cxx:12