Public Member Functions | Private Attributes | List of all members
WireCell::Root::CelltreeFrameSink Class Reference

#include <CelltreeFrameSink.h>

Inheritance diagram for WireCell::Root::CelltreeFrameSink:
WireCell::IFrameFilter WireCell::IConfigurable WireCell::IFunctionNode< IFrame, IFrame > WireCell::IComponent< IConfigurable > WireCell::IFunctionNodeBase WireCell::Interface WireCell::INode WireCell::IComponent< INode > WireCell::Interface

Public Member Functions

 CelltreeFrameSink ()
 
virtual ~CelltreeFrameSink ()
 
virtual bool operator() (const IFrame::pointer &frame, IFrame::pointer &out_frame)
 Frame sink interface. More...
 
virtual void configure (const WireCell::Configuration &config)
 Configurable interface. More...
 
virtual WireCell::Configuration default_configuration () const
 Optional, override to return a hard-coded default configuration. More...
 
- Public Member Functions inherited from WireCell::IFrameFilter
virtual ~IFrameFilter ()
 
virtual std::string signature ()
 Set the signature for all subclasses. More...
 
- Public Member Functions inherited from WireCell::IFunctionNode< IFrame, IFrame >
virtual ~IFunctionNode ()
 
virtual bool operator() (const boost::any &anyin, boost::any &anyout)
 The calling signature: More...
 
virtual std::vector< std::stringinput_types ()
 
virtual std::vector< std::stringoutput_types ()
 
- Public Member Functions inherited from WireCell::IFunctionNodeBase
virtual ~IFunctionNodeBase ()
 
virtual NodeCategory category ()
 Return the behavior category type. More...
 
virtual int concurrency ()
 By default assume all subclasses are stateless. More...
 
- Public Member Functions inherited from WireCell::INode
virtual ~INode ()
 
virtual void reset ()
 
- Public Member Functions inherited from WireCell::IComponent< INode >
virtual ~IComponent ()
 
- Public Member Functions inherited from WireCell::Interface
virtual ~Interface ()
 
- Public Member Functions inherited from WireCell::IConfigurable
virtual ~IConfigurable ()
 
- Public Member Functions inherited from WireCell::IComponent< IConfigurable >
virtual ~IComponent ()
 

Private Attributes

Configuration m_cfg
 
IAnodePlane::pointer m_anode
 
int m_nsamples
 
int m_nrebin
 

Additional Inherited Members

- Public Types inherited from WireCell::IFrameFilter
typedef std::shared_ptr< IFrameFilterpointer
 
- Public Types inherited from WireCell::IFunctionNode< IFrame, IFrame >
typedef IFrame input_type
 
typedef IFrame output_type
 
typedef std::shared_ptr< const IFrameinput_pointer
 
typedef std::shared_ptr< const IFrameoutput_pointer
 
typedef IFunctionNode< IFrame, IFramesignature_type
 
- Public Types inherited from WireCell::IFunctionNodeBase
typedef std::shared_ptr< IFunctionNodeBasepointer
 
- Public Types inherited from WireCell::INode
enum  NodeCategory {
  unknown, sourceNode, sinkNode, functionNode,
  queuedoutNode, joinNode, splitNode, faninNode,
  fanoutNode, multioutNode, hydraNode
}
 
- Public Types inherited from WireCell::IComponent< INode >
typedef std::shared_ptr< INodepointer
 Access subclass facet by pointer. More...
 
typedef std::vector< pointervector
 Vector of shared pointers. More...
 
- Public Types inherited from WireCell::Interface
typedef std::shared_ptr< Interfacepointer
 
- Public Types inherited from WireCell::IComponent< IConfigurable >
typedef std::shared_ptr< IConfigurablepointer
 Access subclass facet by pointer. More...
 
typedef std::vector< pointervector
 Vector of shared pointers. More...
 

Detailed Description

Definition at line 19 of file CelltreeFrameSink.h.

Constructor & Destructor Documentation

Root::CelltreeFrameSink::CelltreeFrameSink ( )

Definition at line 25 of file CelltreeFrameSink.cxx.

Root::CelltreeFrameSink::~CelltreeFrameSink ( )
virtual

Definition at line 30 of file CelltreeFrameSink.cxx.

31 {
32 }

Member Function Documentation

void Root::CelltreeFrameSink::configure ( const WireCell::Configuration config)
virtual

Configurable interface.

Implements WireCell::IConfigurable.

Definition at line 63 of file CelltreeFrameSink.cxx.

64 {
66 
67  fn = cfg["output_filename"].asString();
68  if (fn.empty()) {
69  THROW(ValueError() << errmsg{"Must provide output filename to CelltreeFrameSink"});
70  }
71 
72  auto anode_tn = get<std::string>(cfg, "anode", "AnodePlane");
73  m_anode = Factory::lookup_tn<IAnodePlane>(anode_tn);
74  if (!m_anode) {
75  cerr << "Root::CelltreeFrameSink: failed to get anode: \"" << anode_tn << "\"\n";
76  return;
77  }
78 
79  m_nsamples = get<int>(cfg, "nsamples", 0);
80  if(m_nsamples == 0) {
81  THROW(ValueError() << errmsg{"nsamples has to be configured"});
82  }
83 
84  m_cfg = cfg;
85 
86  m_nrebin = get<int>(cfg, "nrebin", m_nrebin);
87 }
std::string string
Definition: nybbler.cc:12
boost::error_info< struct tag_errmsg, std::string > errmsg
Definition: Exceptions.h:54
cfg
Definition: dbjson.py:29
QAsciiDict< Entry > fn
#define THROW(e)
Definition: Exceptions.h:25
Thrown when a wrong value has been encountered.
Definition: Exceptions.h:37
WireCell::Configuration Root::CelltreeFrameSink::default_configuration ( ) const
virtual

Optional, override to return a hard-coded default configuration.

Reimplemented from WireCell::IConfigurable.

Definition at line 35 of file CelltreeFrameSink.cxx.

36 {
38 
39  cfg["anode"] = "AnodePlane";
40  cfg["output_filename"] = "";
41  cfg["frames"] = Json::arrayValue;
42  cfg["cmmtree"] = Json::arrayValue;
43  cfg["root_file_mode"] = "RECREATE";
44  cfg["nsamples"] = 0;
45  cfg["nrebin"] = 1;
46 
47  // List tagged traces from which to save the "trace summary"
48  // vector into a 1D vector which will be named after the tag.
49  // See "summary_operator".
50  cfg["summaries"] = Json::arrayValue;
51 
52  // An object mapping tags to operators for aggregating trace
53  // summary values on the same channel. Operator may be "sum" to
54  // add up all values on the same channel or "set" to assign values
55  // to the channel bin (last one wins). If a tag is not found, the
56  // default operator is "sum".
57  cfg["summary_operator"] = Json::objectValue;
58 
59 
60  return cfg;
61 }
cfg
Definition: dbjson.py:29
Json::Value Configuration
Definition: Configuration.h:50
bool Root::CelltreeFrameSink::operator() ( const IFrame::pointer frame,
IFrame::pointer out_frame 
)
virtual

Frame sink interface.

Implements WireCell::IFunctionNode< IFrame, IFrame >.

Definition at line 118 of file CelltreeFrameSink.cxx.

119 {
120  out_frame = frame;
121  if(!frame){
122  std::cerr << "CelltreeFrameSink: EOS\n";
123  return true;
124  }
125 
126  const std::string ofname = m_cfg["output_filename"].asString();
127  const std::string mode = m_cfg["root_file_mode"].asString();
128  std::cerr << "CelltreeFrameSink: opening for output: " << ofname << " with \"" << mode << "\"\n";
129  TFile* output_tf = TFile::Open(ofname.c_str(), mode.c_str());
130  if(!output_tf->GetDirectory("Event")) output_tf->mkdir("Event");
131  output_tf->cd("Event");
132 
133  // [HY] Simulation output celltree
134  // Sim Tree
135  TTree *Sim;
136  Sim = (TTree*)output_tf->Get("Event/Sim");
137  if(!Sim) {
138  Sim = new TTree("Sim","Wire-cell toolkit simulation output");
139  Int_t runNo = 0;
140  Int_t subRunNo = 0;
141  Int_t eventNo = 0;
142  Sim->Branch("runNo", &runNo, "runNo/I");
143  Sim->Branch("subRunNo", &subRunNo, "subRunNo/I");
144  Sim->Branch("eventNo", &eventNo, "eventNo/I");
145  Sim->Fill();
146  }
147 
148  // trace frames
149  for (auto tag : cgetset(m_cfg["frames"])){
150 
151  //string tag = "gauss";
152  ITrace::vector traces = cget_tagged_traces(frame, tag);
153  if (traces.empty()) {
154  std::cerr << "CelltreeFrameSink: no tagged traces for\"" << tag << "\"\n";
155  continue;
156  }
157 
158  std::cerr << "CelltreeFrameSink: tag: \"" << tag << "\" with " << traces.size() << " traces\n";
159 
160  std::vector<int> *raw_channelId = new std::vector<int>;
161  std::string channelIdname;
162  if ( !tag.compare("gauss")) channelIdname = "calibGaussian_channelId";
163  if ( !tag.compare("wiener")) channelIdname = "calibWiener_channelId";
164  if ( !tag.compare("orig")) channelIdname = "raw_channelId";
165  //const std::string channelIdname = Form("%s_channelId", tag.c_str());
166  TBranch *bchannelId = Sim->Branch(channelIdname.c_str(), &raw_channelId);
167 
168  TClonesArray *sim_wf = new TClonesArray("TH1F");
169  TH1::AddDirectory(kFALSE);
170  std::string wfname;
171  if ( !tag.compare("gauss")) wfname = "calibGaussian_wf";
172  if ( !tag.compare("wiener")) wfname = "calibWiener_wf";
173  if ( !tag.compare("orig")) wfname = "raw_wf";
174  //const std::string wfname = Form("%s_wf", tag.c_str());
175  TBranch *bwf = Sim->Branch(wfname.c_str(), &sim_wf, 256000, 0);
176 
177  int nsamples = m_nsamples;
178  //std::cout<<"nsamples: "<<m_nsamples<<std::endl;
179 
180  int sim_wf_ind = 0;
181 
182  for (auto trace : traces) {
183  int ch = trace->channel();
184  //std::cout<<"channel number: "<<ch<<std::endl;
185  raw_channelId->push_back(ch);
186  // fill raw_wf
187 
188  int temp_nbins = nsamples/m_nrebin;
189 
190  TH1F *htemp = new ( (*sim_wf)[sim_wf_ind] ) TH1F("", "", temp_nbins, 0, nsamples);
191  //TH1I *htemp = new ( (*sim_wf)[sim_wf_ind] ) TH1I("", "", nsamples, 0, nsamples);
192  auto const& wave = trace->charge();
193  const int nbins = wave.size();
194  //std::cout<<"waveform size: "<<nbins<<std::endl;
195  const int tmin = trace->tbin();
196  //std::cout<<"tmin: "<<tmin<<std::endl;
197  for(Int_t i=0; i<nbins; i++){
198  if(tmin+i+1<=nsamples){
199  int ibin = (tmin+i)/m_nrebin;
200  htemp->SetBinContent(ibin+1, wave[i]+htemp->GetBinContent(ibin+1));
201  }
202  }
203  sim_wf_ind ++;
204  } // traces
205 
206  cout<<"channelId size: " << raw_channelId->size() << "\n";
207  bchannelId->Fill();
208  bwf->Fill();
209  } // frames
210 
211 
212  // trace summaries
213  // currently only one option "threshold"
214  for (auto tag : cgetset(m_cfg["summaries"])) {
215  auto traces = cget_tagged_traces(frame, tag);
216  if (traces.empty()) {
217  std::cerr << "CelltreeFrameSink: warning: no traces tagged with \"" << tag << "\", skipping summary\n";
218  continue;
219  }
220  auto const& summary = frame->trace_summary(tag);
221  if (summary.empty()) {
222  std::cerr << "CelltreeFrameSink: warning: empty summary tagged with \"" << tag << "\", skipping summary\n";
223  continue;
224  }
225 
226  // default: "sum"
227  // to save thresholds: "set"
228  std::string oper = get<std::string>(m_cfg["summary_operator"], tag, "sum");
229 
230  std::cerr << "CelltreeFrameSink: summary tag: \"" << tag << "\" with " << traces.size() << " traces\n";
231 
232  // vector<double> channelThreshold index <--> channelId
233  std::vector<double> *channelThreshold = new std::vector<double>;
234  TBranch *bthreshold = Sim->Branch("channelThreshold", &channelThreshold);
235 
236  const int ntot = traces.size();
237  channelThreshold->resize(ntot, 0);
238  for (int ind=0; ind<ntot; ++ind) {
239  const int chid = traces[ind]->channel();
240  const double val = summary[ind];
241  if (oper == "set") {
242  channelThreshold->at(chid) = val;
243  }
244  else {
245  channelThreshold->at(chid) += val;
246  }
247  }
248 
249  // debug
250  //for (int i=0; i<channelThreshold->size(); i++)
251  //{
252  // cout<<" chid: "<< i << " threshold: "<< channelThreshold->at(i) << "\n";
253  //}
254 
255  bthreshold->Fill();
256  }
257 
258 
259  // bad channels
260  // vector<int> badChannel
261  // vector<int> badBegin
262  // vector<int> badEnd
263  for (auto tag : cgetset(m_cfg["cmmtree"])) {
264 
265  Waveform::ChannelMaskMap input_cmm = frame->masks();
266  for (auto const& it: input_cmm) {
267  auto cmmkey = it.first;
268 
269  if ( tag.compare(cmmkey) != 0 ) continue;
270 
271  std::cerr << "CelltreeFrameSink: saving channel mask \"" << cmmkey << "\"\n";
272 
273  std::vector<int> *Channel = new std::vector<int>;
274  std::vector<int> *Begin = new std::vector<int>;
275  std::vector<int> *End = new std::vector<int>;
276  const std::string Channelname = Form("%sChannel", tag.c_str());
277  const std::string Beginname = Form("%sBegin", tag.c_str());
278  const std::string Endname = Form("%sEnd", tag.c_str());
279  TBranch *bch = Sim->Branch(Channelname.c_str(), &Channel);
280  TBranch *bb = Sim->Branch(Beginname.c_str(), &Begin);
281  TBranch *be = Sim->Branch(Endname.c_str(), &End);
282 
283  for (auto const &chmask : it.second){
284  Channel->push_back(chmask.first);
285  auto mask = chmask.second;
286  if(mask.size()!=1) {
287  std::cerr << "CelltreeFrameSink: Warning: channel mask: "
288  << chmask.first << " has >1 dead period [begin, end] \n";
289  continue;
290  }
291  for (size_t ind = 0; ind < mask.size(); ++ind){
292  Begin->push_back(mask[ind].first);
293  End->push_back(mask[ind].second);
294  }
295  }
296 
297  bch->Fill();
298  bb->Fill();
299  be->Fill();
300  }
301  }
302 
303 
304  std::cerr << "CelltreeFrameSink: closing output file " << ofname << std::endl;
305  auto count = output_tf->Write();
306  std::cerr << "\twrote " << count << " bytes." << std::endl;
307  output_tf->Close();
308  delete output_tf;
309  output_tf = nullptr;
310  return true;
311 }
ITrace::vector cget_tagged_traces(IFrame::pointer frame, IFrame::tag_t tag)
string_set_tc cgetset(const WireCell::Configuration &cfg)
std::string string
Definition: nybbler.cc:12
std::vector< pointer > vector
Definition: IData.h:21
std::map< std::string, ChannelMasks > ChannelMaskMap
Collect channel masks by some label.
Definition: Waveform.h:59
void End(void)
Definition: gXSecComp.cxx:210
ChannelMappingService::Channel Channel
def summary(store)
Definition: info.py:119
second_as<> second
Type of time stored in seconds, in double precision.
Definition: spacetime.h:80
int ntot
Definition: train_cnn.py:133
QTextStream & endl(QTextStream &s)

Member Data Documentation

IAnodePlane::pointer WireCell::Root::CelltreeFrameSink::m_anode
private

Definition at line 34 of file CelltreeFrameSink.h.

Configuration WireCell::Root::CelltreeFrameSink::m_cfg
private

Definition at line 33 of file CelltreeFrameSink.h.

int WireCell::Root::CelltreeFrameSink::m_nrebin
private

Definition at line 37 of file CelltreeFrameSink.h.

int WireCell::Root::CelltreeFrameSink::m_nsamples
private

Definition at line 35 of file CelltreeFrameSink.h.


The documentation for this class was generated from the following files: