MagnifySource.cxx
Go to the documentation of this file.
4 
6 
7 #include "TFile.h"
8 #include "TTree.h"
9 #include "TH2.h"
10 
11 
14 
15 using namespace WireCell;
16 
18  : m_calls(0)
19 {
20 }
21 
22 Root::MagnifySource::~MagnifySource()
23 {
24 }
25 
27 {
28  if (cfg["filename"].empty()) {
29  THROW(ValueError() << errmsg{"MagnifySource: must supply input \"filename\" configuration item."});
30  }
31  m_cfg = cfg;
32 }
33 
34 WireCell::Configuration Root::MagnifySource::default_configuration() const
35 {
37  // Give a URL for the input file.
38  cfg["filename"] = "";
39 
40  // Give a list of frame tags. These are translated to histogram
41  // names h[uvw]_<tag> to be used for input. Missing histograms
42  // raise an exception. Each frame found will be loaded as tagged
43  // traces into the output frame. The ensemble of traces will be
44  // tagged by both <tag> and the per-plane subset as
45  // <planeletter>plane.
46  cfg["frames"][0] = "raw";
47 
48  // A list of pairs mapping a cmm key name to a ttree name.
49  cfg["cmmtree"] = Json::arrayValue;
50 
51  return cfg;
52 }
53 
54 
55 bool Root::MagnifySource::operator()(IFrame::pointer& out)
56 {
57  out = nullptr;
58  ++m_calls;
59  std::cerr << "MagnifySource: called " << m_calls << " times\n";
60  if (m_calls > 2) {
61  std::cerr << "MagnifySource: past EOS\n";
62  return false;
63  }
64  if (m_calls > 1) {
65  std::cerr << "MagnifySource: EOS\n";
66  return true; // this is to send out EOS
67  }
68 
69 
70  std::string url = m_cfg["filename"].asString();
71 
72  TFile* tfile = TFile::Open(url.c_str());
73 
74  int frame_ident=0;
75  int nticks=0;
76  double frame_time=0;
77  {
78  TTree *trun = (TTree*)tfile->Get("Trun");
79  if (!trun) {
80  std::cerr << "No tree: Trun in input file \n";
81  }
82  else{
83  // runNo, subRunNo??
84  trun->SetBranchAddress("eventNo", &frame_ident);
85  trun->SetBranchAddress("total_time_bin", &nticks);
86  //trun->SetBranchAddress("time_offset", &frame_time); use this??
87  trun->GetEntry(0);
88  }
89  }
90  std::cerr << "MagnifySource: frame ident="<<frame_ident<<", time=0, nticks="<<nticks<<"\n";
91 
92 
94  for (auto jcmmtree : m_cfg["cmmtree"]) {
95  auto cmmkey = jcmmtree[0].asString();
96  auto treename = jcmmtree[1].asString();
97  TTree *tree = dynamic_cast<TTree*>(tfile->Get(treename.c_str()));
98  if (!tree) {
99  std::cerr << "MagnifySource: failed to find tree \"" << treename << "\" in " << tfile->GetName() << std::endl;
100  THROW(IOError() << errmsg{"MagnifySource: failed to find tree."});
101  }
102 
103  std::cerr << "MagnifySource: loading channel mask \"" << cmmkey << "\" from tree \"" << treename << "\"\n";
104 
105  int chid=0, plane=0, start_time=0, end_time=0;
106  tree->SetBranchAddress("chid",&chid);
107  tree->SetBranchAddress("plane",&plane);
108  tree->SetBranchAddress("start_time",&start_time);
109  tree->SetBranchAddress("end_time",&end_time);
110 
111  const int nentries = tree->GetEntries();
112  for (int ientry = 0; ientry < nentries; ++ientry){
113  tree->GetEntry(ientry);
115  br.first = start_time;
116  br.second = end_time;
117  cmm[cmmkey][chid].push_back(br);
118  }
119  }
120 
121 
122  ITrace::vector all_traces;
123  std::unordered_map<IFrame::tag_t, IFrame::trace_list_t> tagged_traces;
124 
125  {
126  for (auto jtag : m_cfg["frames"]) {
127  auto frametag = jtag.asString();
128  int channel_number = 0;
129  for (int iplane=0; iplane<3; ++iplane) {
130  std::string plane_name = Form("%cplane", 'u'+iplane);
131  std::string hist_name = Form("h%c_%s", 'u'+iplane, frametag.c_str());
132  std::cerr << "MagnifySource: loading " << hist_name << std::endl;
133  TH2* hist = (TH2*)tfile->Get(hist_name.c_str());
134 
135  ITrace::vector plane_traces;
136 
137  int nchannels = hist->GetNbinsX();
138  int nticks = hist->GetNbinsY();
139  double qtot = 0;
140 
141  // loop over channels in plane
142  for (int chip = 0; chip<nchannels; ++chip) {
143  const int ichbin = chip+1;
144 
145  ITrace::ChargeSequence charges;
146  for (int itickbin = 1; itickbin <= nticks; ++itickbin) {
147  auto q = hist->GetBinContent(ichbin, itickbin);
148  charges.push_back(q);
149  qtot += q;
150  }
151  const size_t index = all_traces.size();
152  tagged_traces[frametag].push_back(index);
153  all_traces.push_back(std::make_shared<SimpleTrace>(channel_number, 0, charges));
154 
155  ++channel_number;
156  }
157  std::cerr << "MagnifySource: plane " << iplane
158  << ": " << nchannels << " X " << nticks
159  << " qtot=" << qtot
160  << std::endl;
161  } // plane
162 
163  }
164  }
165 
166  auto sframe = new SimpleFrame(frame_ident, frame_time,
167  all_traces, 0.5*units::microsecond, cmm);
168  for (auto const& it : tagged_traces) {
169  sframe->tag_traces(it.first, it.second);
170  std::cerr << "MagnifySource: tag " << it.second.size() << " traces as: \"" << it.first << "\"\n";
171  }
172 
173  out = IFrame::pointer(sframe);
174  return true;
175 }
176 
177 
std::shared_ptr< const IFrame > pointer
Definition: IData.h:19
Thrown when an error involving accessing input or output has occurred.
Definition: Exceptions.h:46
std::pair< int, int > BinRange
A half-open range of bins (from first bin to one past last bin)
Definition: Waveform.h:38
std::string string
Definition: nybbler.cc:12
microsecond_as<> microsecond
Type of time stored in microseconds, in double precision.
Definition: spacetime.h:114
boost::error_info< struct tag_errmsg, std::string > errmsg
Definition: Exceptions.h:54
cfg
Definition: dbjson.py:29
std::vector< pointer > vector
Definition: IData.h:21
const int nticks
WIRECELL_FACTORY(MagnifySource, WireCell::Root::MagnifySource, WireCell::IFrameSource, WireCell::IConfigurable) using namespace WireCell
static ITrace::vector tagged_traces(IFrame::pointer frame, IFrame::tag_t tag)
def configure(cfg)
Definition: cuda.py:34
std::map< std::string, ChannelMasks > ChannelMaskMap
Collect channel masks by some label.
Definition: Waveform.h:59
#define THROW(e)
Definition: Exceptions.h:25
Thrown when a wrong value has been encountered.
Definition: Exceptions.h:37
Definition: Main.h:22
Json::Value Configuration
Definition: Configuration.h:50
const GenericPointer< typename T::ValueType > & pointer
Definition: pointer.h:1124
std::vector< float > ChargeSequence
Sequential collection of charge.
Definition: ITrace.h:21
decltype(auto) constexpr empty(T &&obj)
ADL-aware version of std::empty.
Definition: StdUtils.h:92
QTextStream & endl(QTextStream &s)