11 #include "TClonesArray.h" 16 #include <unordered_map> 30 Root::CelltreeFrameSink::~CelltreeFrameSink()
39 cfg[
"anode"] =
"AnodePlane";
40 cfg[
"output_filename"] =
"";
41 cfg[
"frames"] = Json::arrayValue;
42 cfg[
"cmmtree"] = Json::arrayValue;
43 cfg[
"root_file_mode"] =
"RECREATE";
50 cfg[
"summaries"] = Json::arrayValue;
57 cfg[
"summary_operator"] = Json::objectValue;
67 fn = cfg[
"output_filename"].asString();
72 auto anode_tn = get<std::string>(
cfg,
"anode",
"AnodePlane");
73 m_anode = Factory::lookup_tn<IAnodePlane>(anode_tn);
75 cerr <<
"Root::CelltreeFrameSink: failed to get anode: \"" << anode_tn <<
"\"\n";
94 for (
auto jone : cfg) {
95 ret.insert(jone.asString());
103 auto const& all_traces = frame->traces();
104 for (
size_t index : frame->tagged_traces(tag)) {
105 ret.push_back(all_traces->at(
index));
110 auto ftags = frame->frame_tags();
111 if (
std::find(ftags.begin(), ftags.end(), tag) == ftags.end()) {
122 std::cerr <<
"CelltreeFrameSink: EOS\n";
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");
136 Sim = (TTree*)output_tf->Get(
"Event/Sim");
138 Sim =
new TTree(
"Sim",
"Wire-cell toolkit simulation output");
142 Sim->Branch(
"runNo", &runNo,
"runNo/I");
143 Sim->Branch(
"subRunNo", &subRunNo,
"subRunNo/I");
144 Sim->Branch(
"eventNo", &eventNo,
"eventNo/I");
153 if (traces.empty()) {
154 std::cerr <<
"CelltreeFrameSink: no tagged traces for\"" <<
tag <<
"\"\n";
158 std::cerr <<
"CelltreeFrameSink: tag: \"" <<
tag <<
"\" with " << traces.size() <<
" traces\n";
160 std::vector<int> *raw_channelId =
new std::vector<int>;
162 if ( !
tag.compare(
"gauss")) channelIdname =
"calibGaussian_channelId";
163 if ( !
tag.compare(
"wiener")) channelIdname =
"calibWiener_channelId";
164 if ( !
tag.compare(
"orig")) channelIdname =
"raw_channelId";
166 TBranch *bchannelId = Sim->Branch(channelIdname.c_str(), &raw_channelId);
168 TClonesArray *sim_wf =
new TClonesArray(
"TH1F");
169 TH1::AddDirectory(kFALSE);
171 if ( !
tag.compare(
"gauss")) wfname =
"calibGaussian_wf";
172 if ( !
tag.compare(
"wiener")) wfname =
"calibWiener_wf";
173 if ( !
tag.compare(
"orig")) wfname =
"raw_wf";
175 TBranch *bwf = Sim->Branch(wfname.c_str(), &sim_wf, 256000, 0);
182 for (
auto trace : traces) {
183 int ch =
trace->channel();
185 raw_channelId->push_back(ch);
190 TH1F *
htemp =
new ( (*sim_wf)[sim_wf_ind] ) TH1F(
"",
"", temp_nbins, 0, nsamples);
192 auto const& wave =
trace->charge();
193 const int nbins = wave.size();
195 const int tmin =
trace->tbin();
197 for(Int_t i=0; i<
nbins; i++){
198 if(tmin+i+1<=nsamples){
200 htemp->SetBinContent(ibin+1, wave[i]+htemp->GetBinContent(ibin+1));
206 cout<<
"channelId size: " << raw_channelId->size() <<
"\n";
216 if (traces.empty()) {
217 std::cerr <<
"CelltreeFrameSink: warning: no traces tagged with \"" <<
tag <<
"\", skipping summary\n";
220 auto const&
summary = frame->trace_summary(
tag);
222 std::cerr <<
"CelltreeFrameSink: warning: empty summary tagged with \"" <<
tag <<
"\", skipping summary\n";
230 std::cerr <<
"CelltreeFrameSink: summary tag: \"" << tag <<
"\" with " << traces.size() <<
" traces\n";
233 std::vector<double> *channelThreshold =
new std::vector<double>;
234 TBranch *bthreshold = Sim->Branch(
"channelThreshold", &channelThreshold);
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();
242 channelThreshold->at(chid) =
val;
245 channelThreshold->at(chid) +=
val;
266 for (
auto const& it: input_cmm) {
267 auto cmmkey = it.first;
269 if (
tag.compare(cmmkey) != 0 )
continue;
271 std::cerr <<
"CelltreeFrameSink: saving channel mask \"" << cmmkey <<
"\"\n";
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>;
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);
283 for (
auto const &chmask : it.second){
284 Channel->push_back(chmask.first);
285 auto mask = chmask.second;
287 std::cerr <<
"CelltreeFrameSink: Warning: channel mask: " 288 << chmask.first <<
" has >1 dead period [begin, end] \n";
291 for (
size_t ind = 0; ind <
mask.size(); ++ind){
292 Begin->push_back(
mask[ind].first);
304 std::cerr <<
"CelltreeFrameSink: closing output file " << ofname <<
std::endl;
305 auto count = output_tf->Write();
306 std::cerr <<
"\twrote " << count <<
" bytes." <<
std::endl;
ITrace::vector cget_tagged_traces(IFrame::pointer frame, IFrame::tag_t tag)
std::shared_ptr< const IFrame > pointer
string_set_tc cgetset(const WireCell::Configuration &cfg)
boost::error_info< struct tag_errmsg, std::string > errmsg
virtual bool operator()(const IFrame::pointer &frame, IFrame::pointer &out_frame)
Frame sink interface.
std::vector< pointer > vector
virtual WireCell::Configuration default_configuration() const
Optional, override to return a hard-coded default configuration.
WIRECELL_FACTORY(CelltreeFrameSink, WireCell::Root::CelltreeFrameSink, WireCell::IFrameFilter, WireCell::IConfigurable) using namespace std
IAnodePlane::pointer m_anode
Thrown when a wrong value has been encountered.
virtual void configure(const WireCell::Configuration &config)
Configurable interface.
FMT_CONSTEXPR bool find(Ptr first, Ptr last, T value, Ptr &out)
ChannelMappingService::Channel Channel
Json::Value Configuration
second_as<> second
Type of time stored in seconds, in double precision.
std::unordered_set< std::string > string_set_tc
QTextStream & endl(QTextStream &s)