7 #include <framework/modules/rootio/SeqRootInputModule.h>
9 #include <framework/core/Environment.h>
10 #include <framework/datastore/DataStore.h>
11 #include <framework/datastore/StoreObjPtr.h>
12 #include <framework/dataobjects/FileMetaData.h>
13 #include <framework/io/RootIOUtilities.h>
14 #include <framework/database/Configuration.h>
34 setDescription(
"Read .sroot files produced by SeqRootOutput.");
38 addParam(
"inputFileName" , m_inputFileName,
39 "Input file name. Can also be a gzip-compressed file (with suffix .gz). "
40 "Parameter can be overridden using the -i argument to basf2.",
43 addParam(
"inputFileNames", m_filelist,
"List of input files", empty);
44 addParam(
"fileNameIsPattern", m_fileNameIsPattern,
"If true interpret the output "
45 "filename as a boost::format pattern instead of the standard where "
46 "subsequent files are named .sroot-N. For example 'myfile-f%08d.sroot'",
48 addParam(
"declareRealData", m_realData,
"Declare the input to be real, not generated data",
false);
51 SeqRootInputModule::~SeqRootInputModule() =
default;
53 void SeqRootInputModule::initialize()
56 if (!m_inputFileName.empty() && !m_filelist.empty()) {
57 B2FATAL(
"Cannot specify both 'inputFileName' and 'inputFileNames'");
59 const std::vector<std::string>& inputFiles = Environment::Instance().getInputFilesOverride();
60 if (!inputFiles.empty()) {
61 if (inputFiles.size() > 1) {
62 m_filelist = inputFiles;
64 m_inputFileName = inputFiles[0];
65 m_nfile = m_filelist.size();
66 }
else if (m_filelist.size() > 0) {
67 m_nfile = m_filelist.size();
68 m_inputFileName = m_filelist[0];
82 m_file =
new SeqFile(m_inputFileName.c_str(),
"r",
nullptr, 0, m_fileNameIsPattern);
83 if (m_file->status() <= 0)
84 B2FATAL(
"SeqRootInput : Error in opening input file : " << m_inputFileName);
86 B2INFO(
"SeqRootInput : Open " << m_inputFileName);
91 auto* evtbuf =
new char[EvtMessage::c_MaxEventSize];
92 int size = m_file->read(evtbuf, EvtMessage::c_MaxEventSize);
95 m_streamer->restoreDataStore(evtmsg);
96 if (evtmsg->
type() == MSG_STREAMERINFO) {
98 B2INFO(
"Reading StreamerInfo");
99 if (info_cnt != 0) B2FATAL(
"SeqRootInput : Reading StreamerInfos twice");
111 B2FATAL(
"SeqRootInput : Error in reading first event");
118 fileMetaData.registerInDataStore();
119 fileMetaData.create();
120 fileMetaData->declareRealData();
124 Conditions::Configuration::getInstance().setInputGlobaltags({});
128 void SeqRootInputModule::beginRun()
130 gettimeofday(&m_t0,
nullptr);
134 B2INFO(
"SeqRootInput: beginRun called.");
138 void SeqRootInputModule::event()
143 if (++m_nevt == 0)
return;
146 auto* evtbuf =
new char[EvtMessage::c_MaxEventSize];
148 int size = m_file->read(evtbuf, EvtMessage::c_MaxEventSize);
150 B2ERROR(
"SeqRootInput : file read error");
156 }
else if (size == 0) {
157 B2INFO(
"SeqRootInput : EOF detected");
161 if (m_fileptr >= m_nfile) {
166 printf(
"fileptr = %d ( of %d )\n", m_fileptr, m_nfile);
168 m_inputFileName = m_filelist[m_fileptr];
169 m_file =
new SeqFile(m_inputFileName,
"r");
170 if (m_file->status() <= 0)
171 B2FATAL(
"SeqRootInput : Error in opening input file : " << m_inputFileName);
172 B2INFO(
"SeqRootInput : Open " << m_inputFileName);
175 int is = m_file->read(evtbuf, EvtMessage::c_MaxEventSize);
177 B2FATAL(
"SeqRootInput : Error in reading file. error code = " << is);
180 is = m_file->read(evtbuf, EvtMessage::c_MaxEventSize);
182 B2FATAL(
"SeqRootInput : Error in reading file. error code = " << is);
190 double dsize = (double)size / 1000.0;
192 m_size2 += dsize * dsize;
194 if (evtmsg->
type() == MSG_STREAMERINFO) {
195 B2WARNING(
"SeqRootInput : StreamerInfo is found in the middle of *.sroot-* files. Skip record");
196 int is = m_file->read(evtbuf, EvtMessage::c_MaxEventSize);
198 B2FATAL(
"SeqRootInput : Error in reading file. error code = " << is);
204 m_streamer->restoreDataStore(evtmsg);
213 void SeqRootInputModule::endRun()
216 gettimeofday(&m_tend,
nullptr);
217 auto etime = (double)((m_tend.tv_sec - m_t0.tv_sec) * 1000000 +
218 (m_tend.tv_usec - m_t0.tv_usec));
223 double flowmb = m_size / etime * 1000.0;
224 double evrate = (double)m_nevt / (etime / 1000.0);
225 double avesize = m_size / (double)m_nevt;
226 double avesize2 = m_size2 / (double)m_nevt;
227 double sigma2 = avesize2 - avesize * avesize;
228 double sigma = sqrt(sigma2);
232 B2INFO(
"SeqRootInput : " << m_nevt <<
" events read with total bytes of " << m_size <<
" kB");
233 B2INFO(
"SeqRootInput : event rate = " << evrate <<
" (KHz)");
234 B2INFO(
"SeqRootInput : flow rate = " << flowmb <<
" (MB/s)");
235 B2INFO(
"SeqRootInput : event size = " << avesize <<
" +- " << sigma <<
" (kB)");
237 B2INFO(
"SeqRootInput: endRun done.");
241 void SeqRootInputModule::terminate()
245 B2INFO(
"SeqRootInput: terminate called");