CookedFrameSource.cxx
Go to the documentation of this file.
1 #include "CookedFrameSource.h"
3 
7 
8 #include "TTimeStamp.h"
9 
10 #include "WireCellIface/SimpleFrame.h"
11 #include "WireCellIface/SimpleTrace.h"
12 #include "WireCellUtil/NamedFactory.h"
13 
14 WIRECELL_FACTORY(wclsCookedFrameSource,
17  WireCell::IFrameSource)
18 
19 using namespace wcls;
20 using namespace WireCell;
21 
23 
24 CookedFrameSource::~CookedFrameSource() {}
25 
27 CookedFrameSource::default_configuration() const
28 {
29  Configuration cfg;
30  cfg["art_tag"] = ""; // how to look up the cooked digits
31  cfg["tick"] = 0.5 * WireCell::units::us;
32  cfg["frame_tags"][0] = "orig"; // the tags to apply to this frame
33  cfg["nticks"] = m_nticks; // if nonzero, truncate or zero-pad frame to this number of ticks.
34  return cfg;
35 }
36 
37 void
38 CookedFrameSource::configure(const WireCell::Configuration& cfg)
39 {
40  const std::string art_tag = cfg["art_tag"].asString();
41  if (art_tag.empty()) {
42  THROW(ValueError() << errmsg{"WireCell::CookedFrameSource requires a source_label"});
43  }
44  m_inputTag = cfg["art_tag"].asString();
45 
46  m_tick = cfg["tick"].asDouble();
47  for (auto jtag : cfg["frame_tags"]) {
48  m_frame_tags.push_back(jtag.asString());
49  }
50  m_nticks = get(cfg, "nticks", m_nticks);
51 }
52 
53 // this code assumes that the high part of timestamp represents number of seconds from Jan 1st, 1970 and the low part
54 // represents the number of nanoseconds.
55 static double
56 tdiff(const art::Timestamp& ts1, const art::Timestamp& ts2)
57 {
58  TTimeStamp tts1(ts1.timeHigh(), ts1.timeLow());
59  TTimeStamp tts2(ts2.timeHigh(), ts2.timeLow());
60  return tts2.AsDouble() - tts1.AsDouble();
61 }
62 
63 static SimpleTrace*
64 make_trace(const recob::Wire& rw, unsigned int nticks_want)
65 {
66  // uint
67  const raw::ChannelID_t chid = rw.Channel();
68  const int tbin = 0;
69  const std::vector<float> sig = rw.Signal();
70 
71  const float baseline = 0.0;
72  unsigned int nsamp = sig.size();
73  if (nticks_want > 0) { nsamp = std::min(nsamp, nticks_want); }
74  else {
75  nticks_want = nsamp;
76  }
77 
78  auto strace = new SimpleTrace(chid, tbin, nticks_want);
79  auto& q = strace->charge();
80  for (unsigned int itick = 0; itick < nsamp; ++itick) {
81  q[itick] = sig[itick];
82  }
83  for (unsigned int itick = nsamp; itick < nticks_want; ++itick) {
84  q[itick] = baseline;
85  }
86  return strace;
87 }
88 
89 void
90 CookedFrameSource::visit(art::Event& e)
91 {
92  auto const& event = e;
93  // fixme: want to avoid depending on DetectorPropertiesService for now.
94  const double tick = m_tick;
96  bool okay = event.getByLabel(m_inputTag, rwvh);
97  if (!okay) {
99  "WireCell::CookedFrameSource failed to get vector<recob::Wire>: " + m_inputTag.encode();
100  std::cerr << msg << std::endl;
101  THROW(RuntimeError() << errmsg{msg});
102  }
103  else if (rwvh->size() == 0)
104  return;
105 
106  const std::vector<recob::Wire>& rwv(*rwvh);
107  const size_t nchannels = rwv.size();
108  std::cerr << "CookedFrameSource: got " << nchannels << " recob::Wire objects\n";
109 
110  WireCell::ITrace::vector traces(nchannels);
111  for (size_t ind = 0; ind < nchannels; ++ind) {
112  auto const& rw = rwv.at(ind);
113  traces[ind] = ITrace::pointer(make_trace(rw, m_nticks));
114  if (!ind) { // first time through
115  if (m_nticks) {
116  std::cerr << "\tinput nticks=" << rw.NSignal() << " setting to " << m_nticks << std::endl;
117  }
118  else {
119  std::cerr << "\tinput nticks=" << rw.NSignal() << " keeping as is" << std::endl;
120  }
121  }
122  }
123 
124  const double time = tdiff(event.getRun().beginTime(), event.time());
125  auto sframe = new WireCell::SimpleFrame(event.event(), time, traces, tick);
126  for (auto tag : m_frame_tags) {
127  //std::cerr << "\ttagged: " << tag << std::endl;
128  sframe->tag_frame(tag);
129  }
130  m_frames.push_back(WireCell::IFrame::pointer(sframe));
131  m_frames.push_back(nullptr);
132 }
133 
134 bool
135 CookedFrameSource::operator()(WireCell::IFrame::pointer& frame)
136 {
137  frame = nullptr;
138  if (m_frames.empty()) { return false; }
139  frame = m_frames.front();
140  m_frames.pop_front();
141  return true;
142 }
143 // Local Variables:
144 // mode: c++
145 // c-basic-offset: 4
146 // End:
constexpr std::uint32_t timeLow() const
Definition: Timestamp.h:29
static constexpr double us
Definition: Units.h:97
void msg(const char *fmt,...)
Definition: message.cpp:107
std::string string
Definition: nybbler.cc:12
constexpr std::uint32_t timeHigh() const
Definition: Timestamp.h:34
struct vector vector
WIRECELL_FACTORY(wclsCookedFrameSource, wcls::CookedFrameSource, wcls::IArtEventVisitor, WireCell::IFrameSource) using namespace wcls
const double e
raw::ChannelID_t Channel() const
Returns the ID of the channel (or InvalidChannelID)
Definition: Wire.h:231
tick_as<> tick
Tick number, represented by std::ptrdiff_t.
Definition: electronics.h:75
std::vector< float > Signal() const
Return a zero-padded full length vector filled with RoI signal.
Definition: Wire.cxx:47
T min(sqlite3 *const db, std::string const &table_name, std::string const &column_name)
Definition: statistics.h:55
static SimpleTrace * make_trace(const recob::Wire &rw, unsigned int nticks_want)
static double tdiff(const art::Timestamp &ts1, const art::Timestamp &ts2)
Class holding the regions of interest of signal from a channel.
Definition: Wire.h:118
Declaration of basic channel signal object.
unsigned int ChannelID_t
Type representing the ID of a readout channel.
Definition: RawTypes.h:28
QTextStream & endl(QTextStream &s)
Event finding and building.