9 #include <b2bii/modules/B2BIIMdstInput/B2BIIMdstInputModule.h>
11 #include <framework/core/Environment.h>
12 #include <framework/utilities/FileSystem.h>
13 #include <framework/database/Configuration.h>
16 #include "belle_legacy/tables/belletdf.h"
19 #include <framework/utilities/NumberSequence.h>
36 vector<string> globbing(
const vector<string>& patterns)
39 for (
const auto& pat : patterns) {
41 glob(pat.c_str(), GLOB_TILDE, NULL, &glob_result);
42 for (
unsigned int i = 0; i < glob_result.gl_pathc; ++i) {
43 ret.push_back(
string(glob_result.gl_pathv[i]));
45 globfree(&glob_result);
62 setDescription(
"Module to read Belle MDST files.");
66 m_current_file_position = -1;
67 m_current_file_entry = -1;
70 addParam(
"inputFileName", m_inputFileName,
"Belle MDST input file name. "
71 "For more than one file use inputFileNames", std::string(
""));
72 addParam(
"inputFileNames" , m_inputFileNames,
"Belle MDST input file names.",
75 std::vector<std::string> emptyvector;
76 addParam(
"entrySequences", m_entrySequences,
77 "The number sequences (e.g. 23:42,101) defining the entries which are processed for each inputFileName."
78 "Must be specified exactly once for each file to be opened."
79 "The first event has the number 0.", emptyvector);
83 B2BIIMdstInputModule::~B2BIIMdstInputModule()
87 std::vector<std::string> B2BIIMdstInputModule::getInputFiles()
const
89 std::vector<std::string> inputFiles = Environment::Instance().getInputFilesOverride();
90 if (!inputFiles.empty()) {
93 inputFiles = m_inputFileNames;
94 if (!m_inputFileName.empty())
95 inputFiles.push_back(m_inputFileName);
99 void B2BIIMdstInputModule::initialize()
101 m_inputFileNames = globbing(getInputFiles());
103 auto entrySequencesOverride = Environment::Instance().getEntrySequencesOverride();
104 if (entrySequencesOverride.size() > 0)
105 m_entrySequences = entrySequencesOverride;
108 if (m_inputFileNames.empty()) {
109 B2FATAL(
"Empty list of files supplied, cannot continue");
112 if (m_entrySequences.size() > 0 and m_inputFileNames.size() != m_entrySequences.size()) {
113 B2FATAL(
"Number of provided filenames does not match the number of given entrySequences parameters: len(inputFileNames) = "
114 << m_inputFileNames.size() <<
" len(entrySequences) = " << m_entrySequences.size());
119 std::reverse(m_inputFileNames.begin(), m_inputFileNames.end());
122 const char* table_dir = getenv(
"PANTHER_TABLE_DIR");
123 if (!table_dir or !FileSystem::isDir(table_dir)) {
124 string fixed_table_dir = Environment::Instance().getExternalsPath() +
"/share/belle_legacy/panther";
125 B2WARNING(
"PANTHER_TABLE_DIR environment variable not set correctly. This is a known problem with externals v00-05-09, using " <<
126 fixed_table_dir <<
" instead.");
127 if (!FileSystem::isDir(fixed_table_dir))
128 B2FATAL(
"Path " << fixed_table_dir <<
" does not exist, your externals setup seems broken.");
129 setenv(
"PANTHER_TABLE_DIR", fixed_table_dir.c_str(), 1);
137 initializeDataStore();
144 m_fileMetadata.registerInDataStore();
147 void B2BIIMdstInputModule::initializeDataStore()
149 B2DEBUG(99,
"[B2BIIMdstInputModule::initializeDataStore] initialization of DataStore started");
151 m_evtMetaData.registerInDataStore();
153 B2DEBUG(99,
"[B2BIIMdstInputModule::initializeDataStore] initialization of DataStore ended");
157 void B2BIIMdstInputModule::beginRun()
159 B2DEBUG(99,
"B2BIIMdstInput: beginRun called.");
162 bool B2BIIMdstInputModule::openNextFile()
165 BsClrTab(BBS_CLEAR_ALL);
170 if (m_inputFileNames.empty())
return false;
172 const std::string name = m_inputFileNames.back();
173 m_inputFileNames.pop_back();
176 m_fd =
new Belle::Panther_FileIO(name.c_str(), BBS_READ);
179 if (m_fd->read() == -1) {
180 B2FATAL(
"Couldn't read file '" << name <<
"'!");
183 m_current_file_position++;
184 m_current_file_entry = -1;
186 if (m_entrySequences.size() > 0)
188 if (m_entrySequences[m_current_file_position] !=
":") {
194 bool B2BIIMdstInputModule::readNextEvent()
198 m_current_file_entry++;
201 while (rectype < 0 && rectype != -2) {
204 rectype = m_fd->read();
206 B2ERROR(
"Error while reading panther tables! Record skipped.");
210 B2DEBUG(99,
"[B2BIIMdstInputModule::Conversion] Conversion stopped at event #" << m_nevt <<
". EOF detected!");
214 }
while (m_entrySequences.size() > 0
215 and (m_entrySequences[m_current_file_position] !=
":"
216 and m_valid_entries_in_current_file.find(m_current_file_entry) == m_valid_entries_in_current_file.end()));
221 void B2BIIMdstInputModule::event()
231 while (!readNextEvent()) {
232 if (!openNextFile()) {
233 B2DEBUG(99,
"[B2BIIMdstInputModule::Conversion] Conversion stopped at event #" << m_nevt <<
". No more files");
241 Belle::Belle_event_Manager& evman = Belle::Belle_event_Manager::get_manager();
242 Belle::Belle_event& evt = evman[0];
247 Belle::Belle_runhead_Manager& rhdmgr = Belle::Belle_runhead_Manager::get_manager();
248 Belle::Belle_runhead_Manager::const_iterator belleevt = rhdmgr.begin();
250 B2DEBUG(90,
"Event number " << m_nevt);
251 if (belleevt == rhdmgr.end() || not(*belleevt)) {
252 B2WARNING(
"Missing RUNHEAD: Creating RUNHEAD from Event. This is as far as we know fine and handled correctly.");
254 Belle::Belle_runhead& bgr = rhdmgr.add();
255 bgr.ExpMC(evt.ExpMC());
256 bgr.ExpNo(evt.ExpNo());
257 bgr.RunNo(evt.RunNo());
258 bgr.Time(evt.Time());
259 bgr.Date(evt.Date());
260 bgr.Field(evt.MagFieldID());
261 bgr.MaxField(evt.BField());
263 bgr.ELER(evt.ELER());
264 bgr.EHER(evt.EHER());
274 Belle::Belle_runhead& bgr = rhdmgr[0];
275 B2DEBUG(90,
"ExpMC " << bgr.ExpMC() <<
" " << evt.ExpMC());
276 B2DEBUG(90,
"ExpNo " << bgr.ExpNo() <<
" " << evt.ExpNo());
277 B2DEBUG(90,
"RunNo " << bgr.RunNo() <<
" " << evt.RunNo());
278 B2DEBUG(90,
"Time " << bgr.Time() <<
" " << evt.Time());
279 B2DEBUG(90,
"Date " << bgr.Date() <<
" " << evt.Date());
280 B2DEBUG(90,
"Field " << bgr.Field() <<
" " << evt.MagFieldID());
281 B2DEBUG(90,
"MaxField " << bgr.MaxField() <<
" " << evt.BField());
282 B2DEBUG(90,
"Type " << bgr.Type());
283 B2DEBUG(90,
"ELER " << bgr.ELER() <<
" " << evt.ELER());
284 B2DEBUG(90,
"EHER " << bgr.EHER() <<
" " << evt.EHER());
288 evtmetadata->setExperiment(evt.ExpNo());
289 evtmetadata->setRun(evt.RunNo());
290 evtmetadata->setEvent(evt.EvtNo() & 0x0fffffff);
293 bool realData = evt.ExpMC() != 2;
294 evtmetadata->setGeneratedWeight(realData ? -1.0 : 1.0);
297 if (!m_fileMetadata) {
299 if (realData) m_fileMetadata->declareRealData();
302 std::string tag = realData ?
"B2BII" :
"B2BII_MC";
303 Conditions::Configuration::getInstance().setInputGlobaltags({tag});
306 if (realData == m_fileMetadata->isMC()) {
307 B2FATAL(
"Information whether we process real or simulated data has changed. Refusing to continue");
311 B2DEBUG(90,
"[B2BIIMdstInputModule] Convert exp/run/evt: " << evt.ExpNo() <<
"/" << evt.RunNo() <<
"/" <<
int(
312 evt.EvtNo() & 0x0fffffff));
315 void B2BIIMdstInputModule::endRun()
317 B2INFO(
"B2BIIMdstInput: endRun done.");
321 void B2BIIMdstInputModule::terminate()
324 B2INFO(
"B2BIIMdstInput: terminate called");
bool create(bool replace=false)
Create a default object in the data store.
Type-safe access to single objects in the data store.
std::set< int64_t > generate_number_sequence(const std::string &str)
Generate a sequence of numbers defined by a string.
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Abstract base class for different kinds of events.