diff --git a/include/HepMC3DataSource.h b/include/HepMC3DataSource.h index f075be7..4eb8106 100644 --- a/include/HepMC3DataSource.h +++ b/include/HepMC3DataSource.h @@ -47,6 +47,7 @@ class HepMC3DataSource : public DataSource { size_t m_current_file; // Index of the current input file being processed size_t m_total_files; // Total number of input files long long m_still_to_skip; // Number of events still to skip + std::vector m_file_entries; // Number of entries in the input files // Private helper methods void openNextFile(); diff --git a/src/CommandLineParser.cc b/src/CommandLineParser.cc index 3a43da9..aa9ff21 100644 --- a/src/CommandLineParser.cc +++ b/src/CommandLineParser.cc @@ -153,7 +153,7 @@ bool CommandLineParser::handleSourceOption(std::vector& sources, c std::vector files = splitCommaSeparated(value); source->input_files = expandGlobPatterns(files); } else if (property == "skip") { - source->skip = std::stoi(value); + source->skip = std::stoll(value); } else if (property == "frequency") { source->mean_event_frequency = std::stof(value); } else if (property == "static_events") { diff --git a/src/HepMC3DataSource.cc b/src/HepMC3DataSource.cc index ed952e5..9e31616 100644 --- a/src/HepMC3DataSource.cc +++ b/src/HepMC3DataSource.cc @@ -11,6 +11,7 @@ HepMC3DataSource::HepMC3DataSource(const SourceConfig& config, size_t source_ind m_still_to_skip = config_->skip; m_current_file = 0; m_total_files = config_->input_files.size(); + m_file_entries.resize(m_total_files, 0); openNextFile(); } @@ -23,6 +24,18 @@ void HepMC3DataSource::openNextFile() { if (!config_->repeat_on_eof) { throw std::runtime_error("No more input files to process for source: " + config_->name); } else { + // If end of files before skipped value has been reached find the correct file index to open + if(m_still_to_skip > 0) { + m_still_to_skip = m_still_to_skip % total_entries_; // Wrap around if repeat_on_eof is true + size_t cumulative_entries = 0; + for (size_t i = 0; i < m_total_files; ++i) { + cumulative_entries += m_file_entries[i]; + if (cumulative_entries > m_still_to_skip) { + m_current_file = i; + break; + } + } + } std::cout << "Reached end of input files for source: " << config_->name << ". Repeating from the first file." << std::endl; m_current_file = 0; // Loop back to the first file if repeat_on_eof is true @@ -46,20 +59,28 @@ void HepMC3DataSource::openNextFile() { throw std::runtime_error("Failed to open HepMC3 file: " + input_file); } - // Get entry count directly from TTree - total_entries_ = reader_->m_tree->GetEntries(); + if (m_file_entries[m_current_file] == 0) { + m_file_entries[m_current_file] = reader_->m_tree->GetEntries(); + total_entries_ += m_file_entries[m_current_file]; + } - std::cout << "Found " << total_entries_ << " events in HepMC3 file" << std::endl; + std::cout << "Found " << m_file_entries[m_current_file] << " events in HepMC3 file" << std::endl; current_entry_index_ = 0; // Skip initial events if specified if (m_still_to_skip > 0) { std::cout << "Skipping first " << m_still_to_skip << " events for source " << config_->name << " as per configuration" << std::endl; - if (m_still_to_skip >= total_entries_) { + if (m_still_to_skip >= m_file_entries[m_current_file]) { std::cout << "Warning: Skip value exceeds total entries, moving to next file." << std::endl; - m_still_to_skip = m_still_to_skip - total_entries_; // Wrap around if repeat_on_eof is true + m_still_to_skip = m_still_to_skip - m_file_entries[m_current_file]; // Wrap around if repeat_on_eof is true m_current_file++; + + while (m_current_file < m_total_files && m_still_to_skip >= m_file_entries[m_current_file]) { + m_still_to_skip -= m_file_entries[m_current_file]; + m_current_file++; + } + openNextFile(); // Open next file if available return; } else { @@ -71,7 +92,10 @@ void HepMC3DataSource::openNextFile() { } bool HepMC3DataSource::hasMoreEntries() const { - return current_entry_index_ + entries_needed_ <= total_entries_; + if(config_->repeat_on_eof && total_entries_ > 0) { + return true; + } + return (current_entry_index_ + entries_needed_) <= total_entries_; } bool HepMC3DataSource::loadNextEvent() {