HistFrameSink.cxx
Go to the documentation of this file.
5 
6 #include "TFile.h"
7 #include "TH2F.h"
8 
9 #include <iostream>
10 #include <algorithm>
11 #include <unordered_map>
12 
15 
16 
17 using namespace std;
18 using namespace WireCell;
19 
21  : m_filepat("histframe-%02d.root")
22  , m_anode_tn("AnodePlane")
23  , m_units(1.0)
24 {
25 }
26 
27 Root::HistFrameSink::~HistFrameSink()
28 {
29 }
30 
31 
33 {
35  cfg["filename"] = m_filepat;
36  cfg["anode"] = m_anode_tn;
37  cfg["units"] = units::mV;
38  return cfg;
39 }
40 
42 {
43  m_filepat = get<std::string>(cfg, "filename", m_filepat);
44  m_units = get<double>(cfg, "units", m_units);
45  m_anode_tn = get<std::string>(cfg, "anode", m_anode_tn);
46  m_anode = Factory::lookup_tn<IAnodePlane>(m_anode_tn);
47  if (!m_anode) {
48  cerr << "Root::HistFrameSink: failed to get anode: \"" << m_anode_tn << "\"\n";
49  return;
50  }
51  cerr << "Root::HistFrameSink: configured with: "
52  << "file:" << m_filepat << ", "
53  << "units:" << m_units << ", "
54  << "anode:" << m_anode_tn << endl;
55 }
56 
57 
58 
60 {
61  if (!frame) {
62  cerr << "Root::HistFrameSink: no frame\n";
63  return true;
64  }
65 
66  std::string fname = Form(m_filepat.c_str(), frame->ident());
67  TFile* file = TFile::Open(fname.c_str(), "recreate");
68 
69 
70  typedef std::tuple< ITrace::vector, std::vector<int>, std::vector<int> > tct_tuple;
71  std::unordered_map<int, tct_tuple> perplane;
72 
73  // collate traces into per plane and also calculate bounds
74  ITrace::shared_vector traces = frame->traces();
75  for (ITrace::pointer trace : *traces) {
76  int ch = trace->channel();
77  auto wpid = m_anode->resolve(ch);
78  int wpident = wpid.ident();
79  double tmin = trace->tbin();
80  double tlen = trace->charge().size();
81 
82  auto& tct = perplane[wpident];
83  get<0>(tct).push_back(trace);
84  get<1>(tct).push_back(ch);
85  get<2>(tct).push_back(tmin);
86  get<2>(tct).push_back(tmin+tlen);
87 
88  if (wpident < 0) {
89  cerr << "Channel "<<ch<<" has illegal wire plane ident: " << wpid << endl;
90  }
91  }
92 
93  const double t0 = frame->time();
94  const double tick = frame->tick();
95 
96  for (auto& thisplane : perplane) {
97  int wpident = thisplane.first;
98  auto& tct = thisplane.second;
99  auto& traces = get<0>(tct);
100  auto& chans = get<1>(tct);
101  auto chmm = std::minmax_element(chans.begin(), chans.end());
102  auto& tbins = get<2>(tct);
103  auto tbmm = std::minmax_element(tbins.begin(), tbins.end());
104 
105 
106  const double tmin = t0 + tick*(*tbmm.first);
107  const double tmax = t0 + tick*(*tbmm.second);
108  const int ntbins = (*tbmm.second)-(*tbmm.first);
109 
110  const int chmin = round(*chmm.first);
111  const int chmax = round(*chmm.second + 1);
112  const int nchbins = chmax - chmin;
113 
114  TH2F* hist = new TH2F(Form("plane%d", wpident),
115  Form("Plane %d", wpident),
116  ntbins, tmin/units::us, tmax/units::us,
117  nchbins, chmin, chmax);
118  hist->SetDirectory(file); // needed?
119  hist->SetXTitle("time [us]");
120  hist->SetYTitle("channel");
121 
122  double qtot = 0;
123  int nbins_tot = 0;
124  for (auto& trace : traces) {
125  double fch = trace->channel() + 0.5; // make sure we land in bin-center.
126  int tbin = trace->tbin();
127  auto& charge = trace->charge();
128  int nbins = charge.size();
129  nbins_tot += nbins;
130  for (int ibin=0; ibin<nbins; ++ibin) {
131  const double t = t0 + (tick)*(tbin+ibin+0.5); // 0.5 to land in bin-center
132  hist->Fill(t/units::us, fch, charge[ibin]/m_units);
133  qtot += charge[ibin];
134  }
135  }
136 
137  cerr << wpident
138  << " ntraces:" << traces.size() << " "
139  << " nsamples:" << nbins_tot << " "
140  << " qtot:" << qtot/m_units << " "
141  << " qunit:" << m_units << " "
142  << " integ:" << hist->Integral()
143  << " min:" << hist->GetMinimum()
144  << " max:" << hist->GetMaximum()
145  << " chan:"<<nchbins<<"[" << chmin << "," << chmax << "] "
146  << " time:"<<ntbins<<"[" << tmin/units::us << "," << tmax/units::us <<"]us\n";
147 
148 
149  hist->Write();
150  }
151  file->Close();
152  delete file;
153  file = nullptr;
154  return true;
155 }
code to link reconstructed objects back to the MC truth information
hfilts push_back(hfilt)
std::shared_ptr< const IFrame > pointer
Definition: IData.h:19
Definition: posix.h:188
std::string string
Definition: nybbler.cc:12
IAnodePlane::pointer m_anode
Definition: HistFrameSink.h:35
WIRECELL_FACTORY(HistFrameSink, WireCell::Root::HistFrameSink, WireCell::IFrameSink, WireCell::IConfigurable) using namespace std
STL namespace.
static const double mV
Definition: Units.h:180
cfg
Definition: dbjson.py:29
const double tick
Binning tbins(nticks, t0, t0+readout_time)
Definition: Main.h:22
virtual bool operator()(const IFrame::pointer &frame)
Frame sink interface.
virtual WireCell::Configuration default_configuration() const
Optional, override to return a hard-coded default configuration.
Json::Value Configuration
Definition: Configuration.h:50
static const double us
Definition: Units.h:105
virtual void configure(const WireCell::Configuration &config)
Configurable interface.
std::shared_ptr< const vector > shared_vector
Definition: IData.h:22
QTextStream & endl(QTextStream &s)