CelltreeSource.cxx
Go to the documentation of this file.
4 
6 
7 #include "TFile.h"
8 #include "TTree.h"
9 #include "TClonesArray.h"
10 #include "TH1F.h"
11 
12 
15 
16 using namespace WireCell;
17 
19  : m_calls(0)
20 {
21 }
22 
23 Root::CelltreeSource::~CelltreeSource()
24 {
25 }
26 
28 {
29  if (cfg["filename"].empty()) {
30  THROW(ValueError() << errmsg{"CelltreeSource: must supply input \"filename\" configuration item."});
31  }
32  m_cfg = cfg;
33 }
34 
35 WireCell::Configuration Root::CelltreeSource::default_configuration() const
36 {
38  // Give a URL for the input file.
39  cfg["filename"] = "";
40 
41  // which event in the celltree file to be processed
42  cfg["EventNo"] = "0";
43 
44  // Give a list of frame/tree tags.
45 
46  // just raw waveform and no other choice at present
47  // Tree: Sim, Wf: raw_wf
48  cfg["frames"][0] = "orig";
49 
50 
51  return cfg;
52 }
53 
54 bool Root::CelltreeSource::operator()(IFrame::pointer& out)
55 {
56  out = nullptr;
57  ++m_calls;
58  std::cerr << "CelltreeSource: called " << m_calls << " times\n";
59  if (m_calls > 2) {
60  std::cerr << "CelltreeSource: past EOS\n";
61  return false;
62  }
63  if (m_calls > 1) {
64  std::cerr << "CelltreeSource: EOS\n";
65  return true; // this is to send out EOS
66  }
67 
68 
69  std::string url = m_cfg["filename"].asString();
70 
71  TFile* tfile = TFile::Open(url.c_str());
72 
73 
74 
75  TTree *tree = (TTree*)tfile->Get("/Event/Sim");
76  if (!tree) {
77  THROW(IOError() << errmsg{"No tree: /Event/Sim in input file"});
78  }
79 
80  tree->SetBranchStatus("*",0);
81 
82  int run_no, subrun_no, event_no;
83  tree->SetBranchStatus("eventNo",1);
84  tree->SetBranchAddress("eventNo" , &event_no);
85  tree->SetBranchStatus("runNo",1);
86  tree->SetBranchAddress("runNo" , &run_no);
87  tree->SetBranchStatus("subRunNo",1);
88  tree->SetBranchAddress("subRunNo", &subrun_no);
89 
90 
91  std::vector<int> *channelid = new std::vector<int>;
92  TClonesArray* esignal = new TClonesArray;
93 
94  tree->SetBranchStatus("raw_channelId",1);
95  tree->SetBranchAddress("raw_channelId", &channelid);
96  tree->SetBranchStatus("raw_wf",1);
97  tree->SetBranchAddress("raw_wf", &esignal);
98 
99  int frame_number = std::stoi(m_cfg["EventNo"].asString());
100 
101  // loop entry
102  int siz = 0;
103  unsigned int entries = tree->GetEntries();
104  for(unsigned int ent = 0; ent<entries; ent++)
105  {
106  siz = tree->GetEntry(ent);
107  if(event_no == frame_number)
108  {
109  break;
110  }
111  }
112 
113 
114  // need run number and subrunnumber
115  int frame_ident = event_no;
116  double frame_time=0;
117 
118  // some output using eventNo, runNo, subRunNO, ...
119  std::cerr<<"CelltreeSource: frame "<<frame_number<<"\n";
120  std::cerr << "CelltreeSource: runNo "<<run_no<<", subrunNo "<<subrun_no<<", eventNo "<<event_no<<"\n";
121 
122  ITrace::vector all_traces;
123  std::unordered_map<IFrame::tag_t, IFrame::trace_list_t> tagged_traces;
124  // celltree input now is raw data, no information about any noisy or bad channels
125  // leave cmm empty.
127 
128 
129  if (siz>0 && frame_number == frame_ident){
130  int nchannels = channelid->size();
131 
132  auto frametag = m_cfg["frames"][0].asString();
133  int channel_number = 0;
134 
135  std::cerr<<"CelltreeSource: loading "<<frametag<<" "<<nchannels<<" channels \n";
136 
137  // fill waveform ...
138  TH1S *signal_s;
139  TH1F *signal_f;
140 
141  for (int ind=0; ind < nchannels; ++ind) {
142  signal_s = dynamic_cast<TH1S*>(esignal->At(ind));
143  signal_f = dynamic_cast<TH1F*>(esignal->At(ind));
144 
145  bool flag_float = 1;
146  if (signal_f==0 && signal_s!=0)
147  flag_float = 0;
148 
149  if (!signal_f && !signal_s) continue;
150  channel_number = channelid->at(ind);
151 
152  ITrace::ChargeSequence charges;
153  int nticks;
154  if (flag_float==1)
155  nticks = signal_f->GetNbinsX();
156  else
157  nticks = signal_s->GetNbinsX();
158  //std::cerr<<"CelltreeSource: tick "<<nticks<<"\n";
159  //nticks = 9600, this could be an issue cause just 9594 have non-ZERO value around baseline
160  for (int itickbin = 0; itickbin != nticks; itickbin++){
161  if (flag_float==1){
162  if(signal_f->GetBinContent(itickbin+1)!=0) {
163  charges.push_back(signal_f->GetBinContent(itickbin+1));
164  }
165  }else{
166  if(signal_s->GetBinContent(itickbin+1)!=0) {
167  charges.push_back(signal_s->GetBinContent(itickbin+1));
168  }
169  }
170  }
171 
172  const size_t index = all_traces.size();
173  tagged_traces[frametag].push_back(index);
174 
175  all_traces.push_back(std::make_shared<SimpleTrace>(channel_number, 0, charges));
176  }
177  auto sframe = new SimpleFrame(frame_ident, frame_time,
178  all_traces, 0.5*units::microsecond, cmm);
179  for (auto const& it : tagged_traces) {
180  sframe->tag_traces(it.first, it.second);
181  std::cerr << "CelltreeSource: tag " << it.second.size() << " traces as: \"" << it.first << "\"\n";
182  }
183 
184  out = IFrame::pointer(sframe);
185 
186  return true;
187  }else{
188  std::cerr << "CelltreeSource: Event Number is out of boundary! " << std::endl;
189  return false;
190  }
191 
192 
193 
194 
195 
196 
197  // // runNo, subRunNo??
198  // trun->SetBranchAddress("eventNo", &frame_ident);
199  // trun->SetBranchAddress("total_time_bin", &nticks);
200  // //trun->SetBranchAddress("time_offset", &frame_time); use this??
201  // trun->GetEntry(0);
202 
203  // std::cerr << "CelltreeSource: frame ident="<<frame_ident<<", time=0, nticks="<<nticks<<"\n";
204 
205 
206 }
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
WIRECELL_FACTORY(CelltreeSource, WireCell::Root::CelltreeSource, WireCell::IFrameSource, WireCell::IConfigurable) using namespace WireCell
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
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)