9 #include <framework/modules/rootio/SeqRootInputModule.h>
11 #include <framework/core/Environment.h>
12 #include <framework/datastore/DataStore.h>
13 #include <framework/datastore/StoreObjPtr.h>
14 #include <framework/dataobjects/FileMetaData.h>
15 #include <framework/io/RootIOUtilities.h>
16 #include <framework/database/Configuration.h>
36 setDescription(
"Read .sroot files produced by SeqRootOutput.");
40 addParam(
"inputFileName" , m_inputFileName,
41 "Input file name. Can also be a gzip-compressed file (with suffix .gz). "
42 "Parameter can be overridden using the -i argument to basf2.",
45 addParam(
"inputFileNames", m_filelist,
"List of input files", empty);
46 addParam(
"fileNameIsPattern", m_fileNameIsPattern,
"If true interpret the output "
47 "filename as a boost::format pattern instead of the standard where "
48 "subsequent files are named .sroot-N. For example 'myfile-f%08d.sroot'",
50 addParam(
"declareRealData", m_realData,
"Declare the input to be real, not generated data",
false);
53 SeqRootInputModule::~SeqRootInputModule() =
default;
55 void SeqRootInputModule::initialize()
58 if (!m_inputFileName.empty() && !m_filelist.empty()) {
59 B2FATAL(
"Cannot specify both 'inputFileName' and 'inputFileNames'");
61 const std::vector<std::string>& inputFiles = Environment::Instance().getInputFilesOverride();
62 if (!inputFiles.empty()) {
63 if (inputFiles.size() > 1) {
64 m_filelist = inputFiles;
66 m_inputFileName = inputFiles[0];
67 m_nfile = m_filelist.size();
68 }
else if (m_filelist.size() > 0) {
69 m_nfile = m_filelist.size();
70 m_inputFileName = m_filelist[0];
84 m_file =
new SeqFile(m_inputFileName.c_str(),
"r",
nullptr, 0, m_fileNameIsPattern);
85 if (m_file->status() <= 0)
86 B2FATAL(
"SeqRootInput : Error in opening input file : " << m_inputFileName);
88 B2INFO(
"SeqRootInput : Open " << m_inputFileName);
93 auto* evtbuf =
new char[EvtMessage::c_MaxEventSize];
94 int size = m_file->read(evtbuf, EvtMessage::c_MaxEventSize);
97 m_streamer->restoreDataStore(evtmsg);
98 if (evtmsg->
type() == MSG_STREAMERINFO) {
100 B2INFO(
"Reading StreamerInfo");
101 if (info_cnt != 0) B2FATAL(
"SeqRootInput : Reading StreamerInfos twice");
113 B2FATAL(
"SeqRootInput : Error in reading first event");
122 fileMetaData->declareRealData();
126 Conditions::Configuration::getInstance().setInputGlobaltags({});
130 void SeqRootInputModule::beginRun()
132 gettimeofday(&m_t0,
nullptr);
136 B2INFO(
"SeqRootInput: beginRun called.");
140 void SeqRootInputModule::event()
145 if (++m_nevt == 0)
return;
148 auto* evtbuf =
new char[EvtMessage::c_MaxEventSize];
150 int size = m_file->read(evtbuf, EvtMessage::c_MaxEventSize);
152 B2ERROR(
"SeqRootInput : file read error");
158 }
else if (size == 0) {
159 B2INFO(
"SeqRootInput : EOF detected");
163 if (m_fileptr >= m_nfile) {
168 printf(
"fileptr = %d ( of %d )\n", m_fileptr, m_nfile);
170 m_inputFileName = m_filelist[m_fileptr];
171 m_file =
new SeqFile(m_inputFileName,
"r");
172 if (m_file->status() <= 0)
173 B2FATAL(
"SeqRootInput : Error in opening input file : " << m_inputFileName);
174 B2INFO(
"SeqRootInput : Open " << m_inputFileName);
177 int is = m_file->read(evtbuf, EvtMessage::c_MaxEventSize);
179 B2FATAL(
"SeqRootInput : Error in reading file. error code = " << is);
182 is = m_file->read(evtbuf, EvtMessage::c_MaxEventSize);
184 B2FATAL(
"SeqRootInput : Error in reading file. error code = " << is);
192 double dsize = (double)size / 1000.0;
194 m_size2 += dsize * dsize;
196 if (evtmsg->
type() == MSG_STREAMERINFO) {
197 B2WARNING(
"SeqRootInput : StreamerInfo is found in the middle of *.sroot-* files. Skip record");
198 int is = m_file->read(evtbuf, EvtMessage::c_MaxEventSize);
200 B2FATAL(
"SeqRootInput : Error in reading file. error code = " << is);
206 m_streamer->restoreDataStore(evtmsg);
215 void SeqRootInputModule::endRun()
218 gettimeofday(&m_tend,
nullptr);
219 auto etime = (double)((m_tend.tv_sec - m_t0.tv_sec) * 1000000 +
220 (m_tend.tv_usec - m_t0.tv_usec));
225 double flowmb = m_size / etime * 1000.0;
226 double evrate = (double)m_nevt / (etime / 1000.0);
227 double avesize = m_size / (double)m_nevt;
228 double avesize2 = m_size2 / (double)m_nevt;
229 double sigma2 = avesize2 - avesize * avesize;
230 double sigma = sqrt(sigma2);
234 B2INFO(
"SeqRootInput : " << m_nevt <<
" events read with total bytes of " << m_size <<
" kB");
235 B2INFO(
"SeqRootInput : event rate = " << evrate <<
" (KHz)");
236 B2INFO(
"SeqRootInput : flow rate = " << flowmb <<
" (MB/s)");
237 B2INFO(
"SeqRootInput : event size = " << avesize <<
" +- " << sigma <<
" (kB)");
239 B2INFO(
"SeqRootInput: endRun done.");
243 void SeqRootInputModule::terminate()
247 B2INFO(
"SeqRootInput: terminate called");
Stream/restore DataStore objects to/from EvtMessage.
Class to manage streamed object.
ERecordType type() const
Get record type.
A class to manage I/O for a chain of blocked files.
bool registerInDataStore(DataStore::EStoreFlags storeFlags=DataStore::c_WriteOut)
Register the object/array in the DataStore.
bool create(bool replace=false)
Create a default object in the data store.
Type-safe access to single objects in the data store.
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Abstract base class for different kinds of events.