11 #include <b2bii/modules/B2BIIMdstInput/B2BIIMdstInputModule.h>
13 #include <framework/core/Environment.h>
14 #include <framework/utilities/FileSystem.h>
15 #include <framework/database/Configuration.h>
18 #include "belle_legacy/tables/belletdf.h"
21 #include <framework/utilities/NumberSequence.h>
38 vector<string> globbing(
const vector<string>& patterns)
41 for (
const auto& pat : patterns) {
43 glob(pat.c_str(), GLOB_TILDE, NULL, &glob_result);
44 for (
unsigned int i = 0; i < glob_result.gl_pathc; ++i) {
45 ret.push_back(
string(glob_result.gl_pathv[i]));
47 globfree(&glob_result);
64 setDescription(
"Module to read Belle MDST files.");
68 m_current_file_position = -1;
69 m_current_file_entry = -1;
72 addParam(
"inputFileName", m_inputFileName,
"Belle MDST input file name. "
73 "For more than one file use inputFileNames", std::string(
""));
74 addParam(
"inputFileNames" , m_inputFileNames,
"Belle MDST input file names.",
77 std::vector<std::string> emptyvector;
78 addParam(
"entrySequences", m_entrySequences,
79 "The number sequences (e.g. 23:42,101) defining the entries which are processed for each inputFileName."
80 "Must be specified exactly once for each file to be opened."
81 "The first event has the number 0.", emptyvector);
85 B2BIIMdstInputModule::~B2BIIMdstInputModule()
89 std::vector<std::string> B2BIIMdstInputModule::getInputFiles()
const
91 std::vector<std::string> inputFiles = Environment::Instance().getInputFilesOverride();
92 if (!inputFiles.empty()) {
95 inputFiles = m_inputFileNames;
96 if (!m_inputFileName.empty())
97 inputFiles.push_back(m_inputFileName);
101 void B2BIIMdstInputModule::initialize()
103 m_inputFileNames = globbing(getInputFiles());
105 auto entrySequencesOverride = Environment::Instance().getEntrySequencesOverride();
106 if (entrySequencesOverride.size() > 0)
107 m_entrySequences = entrySequencesOverride;
110 if (m_inputFileNames.empty()) {
111 B2FATAL(
"Empty list of files supplied, cannot continue");
114 if (m_entrySequences.size() > 0 and m_inputFileNames.size() != m_entrySequences.size()) {
115 B2FATAL(
"Number of provided filenames does not match the number of given entrySequences parameters: len(inputFileNames) = "
116 << m_inputFileNames.size() <<
" len(entrySequences) = " << m_entrySequences.size());
121 std::reverse(m_inputFileNames.begin(), m_inputFileNames.end());
124 const char* table_dir = getenv(
"PANTHER_TABLE_DIR");
125 if (!table_dir or !FileSystem::isDir(table_dir)) {
126 string fixed_table_dir = Environment::Instance().getExternalsPath() +
"/share/belle_legacy/panther";
127 B2WARNING(
"PANTHER_TABLE_DIR environment variable not set correctly. This is a known problem with externals v00-05-09, using " <<
128 fixed_table_dir <<
" instead.");
129 if (!FileSystem::isDir(fixed_table_dir))
130 B2FATAL(
"Path " << fixed_table_dir <<
" does not exist, your externals setup seems broken.");
131 setenv(
"PANTHER_TABLE_DIR", fixed_table_dir.c_str(), 1);
139 initializeDataStore();
146 m_fileMetadata.registerInDataStore();
149 void B2BIIMdstInputModule::initializeDataStore()
151 B2DEBUG(99,
"[B2BIIMdstInputModule::initializeDataStore] initialization of DataStore started");
153 m_evtMetaData.registerInDataStore();
155 B2DEBUG(99,
"[B2BIIMdstInputModule::initializeDataStore] initialization of DataStore ended");
159 void B2BIIMdstInputModule::beginRun()
161 B2DEBUG(99,
"B2BIIMdstInput: beginRun called.");
164 bool B2BIIMdstInputModule::openNextFile()
167 BsClrTab(BBS_CLEAR_ALL);
172 if (m_inputFileNames.empty())
return false;
174 const std::string name = m_inputFileNames.back();
175 m_inputFileNames.pop_back();
178 m_fd =
new Belle::Panther_FileIO(name.c_str(), BBS_READ);
181 if (m_fd->read() == -1) {
182 B2FATAL(
"Couldn't read file '" << name <<
"'!");
185 m_current_file_position++;
186 m_current_file_entry = -1;
188 if (m_entrySequences.size() > 0)
190 if (m_entrySequences[m_current_file_position] !=
":") {
196 bool B2BIIMdstInputModule::readNextEvent()
200 m_current_file_entry++;
203 while (rectype < 0 && rectype != -2) {
206 rectype = m_fd->read();
208 B2ERROR(
"Error while reading panther tables! Record skipped.");
212 B2DEBUG(99,
"[B2BIIMdstInputModule::Conversion] Conversion stopped at event #" << m_nevt <<
". EOF detected!");
216 }
while (m_entrySequences.size() > 0
217 and (m_entrySequences[m_current_file_position] !=
":"
218 and m_valid_entries_in_current_file.find(m_current_file_entry) == m_valid_entries_in_current_file.end()));
223 void B2BIIMdstInputModule::event()
233 while (!readNextEvent()) {
234 if (!openNextFile()) {
235 B2DEBUG(99,
"[B2BIIMdstInputModule::Conversion] Conversion stopped at event #" << m_nevt <<
". No more files");
239 evtmetadata.create();
243 Belle::Belle_event_Manager& evman = Belle::Belle_event_Manager::get_manager();
244 Belle::Belle_event& evt = evman[0];
249 Belle::Belle_runhead_Manager& rhdmgr = Belle::Belle_runhead_Manager::get_manager();
250 Belle::Belle_runhead_Manager::const_iterator belleevt = rhdmgr.begin();
252 B2DEBUG(90,
"Event number " << m_nevt);
253 if (belleevt == rhdmgr.end() || not(*belleevt)) {
254 B2WARNING(
"Missing RUNHEAD: Creating RUNHEAD from Event. This is as far as we know fine and handled correctly.");
256 Belle::Belle_runhead& bgr = rhdmgr.add();
257 bgr.ExpMC(evt.ExpMC());
258 bgr.ExpNo(evt.ExpNo());
259 bgr.RunNo(evt.RunNo());
260 bgr.Time(evt.Time());
261 bgr.Date(evt.Date());
262 bgr.Field(evt.MagFieldID());
263 bgr.MaxField(evt.BField());
265 bgr.ELER(evt.ELER());
266 bgr.EHER(evt.EHER());
276 Belle::Belle_runhead& bgr = rhdmgr[0];
277 B2DEBUG(90,
"ExpMC " << bgr.ExpMC() <<
" " << evt.ExpMC());
278 B2DEBUG(90,
"ExpNo " << bgr.ExpNo() <<
" " << evt.ExpNo());
279 B2DEBUG(90,
"RunNo " << bgr.RunNo() <<
" " << evt.RunNo());
280 B2DEBUG(90,
"Time " << bgr.Time() <<
" " << evt.Time());
281 B2DEBUG(90,
"Date " << bgr.Date() <<
" " << evt.Date());
282 B2DEBUG(90,
"Field " << bgr.Field() <<
" " << evt.MagFieldID());
283 B2DEBUG(90,
"MaxField " << bgr.MaxField() <<
" " << evt.BField());
284 B2DEBUG(90,
"Type " << bgr.Type());
285 B2DEBUG(90,
"ELER " << bgr.ELER() <<
" " << evt.ELER());
286 B2DEBUG(90,
"EHER " << bgr.EHER() <<
" " << evt.EHER());
290 evtmetadata->setExperiment(evt.ExpNo());
291 evtmetadata->setRun(evt.RunNo());
292 evtmetadata->setEvent(evt.EvtNo() & 0x0fffffff);
295 bool realData = evt.ExpMC() != 2;
296 evtmetadata->setGeneratedWeight(realData ? -1.0 : 1.0);
299 if (!m_fileMetadata) {
300 m_fileMetadata.create();
301 if (realData) m_fileMetadata->declareRealData();
304 std::string tag = realData ?
"B2BII" :
"B2BII_MC";
305 Conditions::Configuration::getInstance().setInputGlobaltags({tag});
308 if (realData == m_fileMetadata->isMC()) {
309 B2FATAL(
"Information whether we process real or simulated data has changed. Refusing to continue");
313 B2DEBUG(90,
"[B2BIIMdstInputModule] Convert exp/run/evt: " << evt.ExpNo() <<
"/" << evt.RunNo() <<
"/" <<
int(
314 evt.EvtNo() & 0x0fffffff));
317 void B2BIIMdstInputModule::endRun()
319 B2INFO(
"B2BIIMdstInput: endRun done.");
323 void B2BIIMdstInputModule::terminate()
326 B2INFO(
"B2BIIMdstInput: terminate called");