RawFrameSource.cxx
Go to the documentation of this file.
1 #include "RawFrameSource.h"
3 
4 // for tick
5 //#include "lardata/DetectorInfoServices/DetectorPropertiesService.h"
9 
10 #include "TTimeStamp.h"
11 
12 
13 #include "WireCellIface/SimpleFrame.h"
14 #include "WireCellIface/SimpleTrace.h"
15 #include "WireCellUtil/NamedFactory.h"
16 
17 WIRECELL_FACTORY(wclsRawFrameSource, wcls::RawFrameSource,
18  wcls::IArtEventVisitor, WireCell::IFrameSource)
19 
20 
21 using namespace wcls;
22 using namespace WireCell;
23 
25  : m_nticks(0)
26 {
27 }
28 
29 RawFrameSource::~RawFrameSource()
30 {
31 }
32 
33 
34 WireCell::Configuration RawFrameSource::default_configuration() const
35 {
36  Configuration cfg;
37  cfg["art_tag"] = ""; // how to look up the raw digits
38  cfg["tick"] = 0.5*WireCell::units::us;
39  cfg["frame_tags"][0] = "orig"; // the tags to apply to this frame
40  cfg["nticks"] = m_nticks; // if nonzero, truncate or baseline-pad frame to this number of ticks.
41  return cfg;
42 }
43 
44 void RawFrameSource::configure(const WireCell::Configuration& cfg)
45 {
46  const std::string art_tag = cfg["art_tag"].asString();
47  if (art_tag.empty()) {
48  THROW(ValueError() << errmsg{"WireCell::RawFrameSource requires a source_label"});
49  }
50  m_inputTag = cfg["art_tag"].asString();
51 
52  m_tick = cfg["tick"].asDouble();
53  for (auto jtag : cfg["frame_tags"]) {
54  m_frame_tags.push_back(jtag.asString());
55  }
56  m_nticks = get(cfg, "nticks", m_nticks);
57 }
58 
59 
60 
61 // is this the right way to diff an art::Timestamp?
62 static
63 double tdiff(const art::Timestamp& ts1, const art::Timestamp& ts2)
64 {
65  TTimeStamp tts1(ts1.timeHigh(), ts1.timeLow());
66  TTimeStamp tts2(ts2.timeHigh(), ts2.timeLow());
67  return tts2.AsDouble() - tts1.AsDouble();
68 }
69 
70 
71 static
72 SimpleTrace* make_trace(const raw::RawDigit& rd, unsigned int nticks_want)
73 {
74  const int chid = rd.Channel();
75  const int tbin = 0;
76  const raw::RawDigit::ADCvector_t& adcv = rd.ADCs();
77 
78  short baseline = 0;
79  unsigned int nadcs = adcv.size();
80  if (nticks_want > 0) { // don't want natural input size
81  if (nticks_want > nadcs) {
82  baseline = Waveform::most_frequent(adcv);
83  }
84  nadcs = std::min(nadcs, nticks_want);
85  }
86  else {
87  nticks_want = nadcs;
88  }
89 
90  auto strace = new SimpleTrace(chid, tbin, nticks_want);
91  for (unsigned int itick=0; itick < nadcs; ++ itick) {
92  strace->charge()[itick] = adcv[itick];
93  }
94  for (unsigned int itick = nadcs; itick < nticks_want; ++itick) {
95  strace->charge()[itick] = baseline;
96  }
97  return strace;
98 }
99 
100 void RawFrameSource::visit(art::Event & event)
101 {
102  // fixme: want to avoid depending on DetectorPropertiesService for now.
103  const double tick = m_tick;
105  bool okay = event.getByLabel(m_inputTag, rdvh);
106  if (!okay) {
107  std::string msg = "WireCell::RawFrameSource failed to get vector<raw::RawDigit>: " + m_inputTag.encode();
108  std::cerr << msg << std::endl;
109  THROW(RuntimeError() << errmsg{msg});
110  }
111  else if (rdvh->size() == 0) return;
112 
113  const std::vector<raw::RawDigit>& rdv(*rdvh);
114  const size_t nchannels = rdv.size();
115  std::cerr << "RawFrameSource: got " << nchannels << " raw::RawDigit objects\n";
116 
117  WireCell::ITrace::vector traces(nchannels);
118  for (size_t ind=0; ind<nchannels; ++ind) {
119  auto const& rd = rdv.at(ind);
120  traces[ind] = ITrace::pointer(make_trace(rd, m_nticks));
121  if (!ind) {
122  if (m_nticks) {
123  std::cerr
124  << "\tinput nticks=" << rd.ADCs().size() << " setting to " << m_nticks
125  << std::endl;
126  }
127  else {
128  std::cerr
129  << "\tinput nticks=" << rd.ADCs().size() << " keeping as is"
130  << std::endl;
131  }
132  }
133  }
134 
135  const double time = tdiff(event.getRun().beginTime(), event.time());
136  auto sframe = new WireCell::SimpleFrame(event.event(), time, traces, tick);
137  for (auto tag : m_frame_tags) {
138  //std::cerr << "\ttagged: " << tag << std::endl;
139  sframe->tag_frame(tag);
140  }
141  m_frames.push_back(WireCell::IFrame::pointer(sframe));
142  m_frames.push_back(nullptr);
143 }
144 
145 bool RawFrameSource::operator()(WireCell::IFrame::pointer& frame)
146 {
147  frame = nullptr;
148  if (m_frames.empty()) {
149  return false;
150  }
151  frame = m_frames.front();
152  m_frames.pop_front();
153  return true;
154 }
155 // Local Variables:
156 // mode: c++
157 // c-basic-offset: 4
158 // End:
const ADCvector_t & ADCs() const
Reference to the compressed ADC count vector.
Definition: RawDigit.h:210
constexpr std::uint32_t timeLow() const
Definition: Timestamp.h:29
EventNumber_t event() const
Definition: DataViewImpl.cc:85
static constexpr double us
Definition: Units.h:97
static SimpleTrace * make_trace(const raw::RawDigit &rd, unsigned int nticks_want)
Collection of charge vs time digitized from a single readout channel.
Definition: RawDigit.h:69
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
ChannelID_t Channel() const
DAQ channel this raw data was read from.
Definition: RawDigit.h:212
struct vector vector
std::vector< short > ADCvector_t
Type representing a (compressed) vector of ADC counts.
Definition: RawDigit.h:73
tick_as<> tick
Tick number, represented by std::ptrdiff_t.
Definition: electronics.h:75
WIRECELL_FACTORY(wclsRawFrameSource, wcls::RawFrameSource, wcls::IArtEventVisitor, WireCell::IFrameSource) using namespace wcls
Run const & getRun() const
Definition: Event.cc:50
T min(sqlite3 *const db, std::string const &table_name, std::string const &column_name)
Definition: statistics.h:55
static double tdiff(const art::Timestamp &ts1, const art::Timestamp &ts2)
Timestamp const & beginTime() const
Definition: DataViewImpl.cc:92
QTextStream & endl(QTextStream &s)
Event finding and building.