Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/HepMC3DataSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<size_t> m_file_entries; // Number of entries in the input files

// Private helper methods
void openNextFile();
Expand Down
2 changes: 1 addition & 1 deletion src/CommandLineParser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ bool CommandLineParser::handleSourceOption(std::vector<SourceConfig>& sources, c
std::vector<std::string> 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") {
Expand Down
36 changes: 30 additions & 6 deletions src/HepMC3DataSource.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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() {
Expand Down
Loading