diff --git a/include/podio/ROOTDataSource.h b/include/podio/ROOTDataSource.h index aaace54ca..f336a078b 100644 --- a/include/podio/ROOTDataSource.h +++ b/include/podio/ROOTDataSource.h @@ -2,180 +2,177 @@ #define PODIO_DATASOURCE_H__ // STL -#include #include +#include // ROOT #include #include // Podio +#include #include #include -#include namespace podio { - using Record_t = std::vector; - - class ROOTDataSource : public ROOT::RDF::RDataSource { - public: - /// - /// @brief Construct the podio::ROOTDataSource from the provided file. - /// - explicit ROOTDataSource(const std::string& filePath, int nEvents = -1); - - /// - /// @brief Construct the podio::ROOTDataSource from the provided file - /// list. - /// - explicit ROOTDataSource(const std::vector& filePathList, - int nEvents = -1); - - /// - /// @brief Inform the podio::ROOTDataSource of the desired level of - /// parallelism. - /// - void SetNSlots(unsigned int nSlots) override; - - /// - /// @brief Retrieve from podio::ROOTDataSource per-thread readers for the - /// desired columns. - /// - template - std::vector GetColumnReaders(std::string_view columnName); - - /// - /// @brief Inform podio::ROOTDataSource that an event-loop is about to - /// start. - /// - void Initialize() override; - - /// - /// @brief Retrieve from podio::ROOTDataSource a set of ranges of entries - /// that can be processed concurrently. - /// - std::vector> GetEntryRanges() override; - - /// - /// @brief Inform podio::ROOTDataSource that a certain thread is about to - /// start working on a certain range of entries. - /// - void InitSlot(unsigned int slot, ULong64_t firstEntry) override; - - /// - /// @brief Inform podio::ROOTDataSource that a certain thread is about to - /// start working on a certain entry. - /// - bool SetEntry(unsigned int slot, ULong64_t entry) override; - - /// - /// @brief Inform podio::ROOTDataSource that a certain thread finished - /// working on a certain range of entries. - /// - void FinalizeSlot(unsigned int slot) override; - - /// - /// @brief Inform podio::ROOTDataSource that an event-loop finished. - /// - void Finalize() override; - - /// - /// @brief Returns a reference to the collection of the dataset's column - /// names - /// - const std::vector& GetColumnNames() const override; - - /// - /// @brief Checks if the dataset has a certain column. - /// - bool HasColumn(std::string_view columnName) const override; - - /// - /// @brief Type of a column as a string. Required for JITting. - /// - std::string GetTypeName(std::string_view columnName) const override; - - protected: - /// - /// @brief Type-erased vector of pointers to pointers to column - /// values --- one per slot. - /// - Record_t GetColumnReadersImpl ( - std::string_view name, - const std::type_info& typeInfo) override; - - std::string AsString() override { return "Podio data source"; }; - - private: - /// Number of slots/threads - unsigned int m_nSlots = 1; - - /// Input filename - std::vector m_filePathList = {}; - - /// Total number of events - unsigned int m_nEvents = 0; - - /// Ranges of events available to be processed - std::vector> m_rangesAvailable = {}; - - /// Ranges of events available ever created - std::vector> m_rangesAll = {}; - - /// Column names - std::vector m_columnNames {}; - - /// Column types - std::vector m_columnTypes = {}; - - /// Collections, m_Collections[columnIndex][slotIndex] - std::vector> m_Collections = {}; - - /// Active collections - std::vector m_activeCollections = {}; - - /// Root podio readers - std::vector> m_podioReaders = {}; - - /// Podio frames - std::vector> m_frames = {}; - - /// - /// @brief Setup input for the podio::ROOTDataSource. - /// - /// @param[in] Number of events. - /// @return void. - /// - void SetupInput(int nEvents); - }; +using Record_t = std::vector; - template - std::vector - ROOTDataSource::GetColumnReaders(std::string_view columnName) { - std::cout << "podio::ROOTDataSource: Getting column readers for column: " - << columnName << std::endl; +class ROOTDataSource : public ROOT::RDF::RDataSource { +public: + /// + /// @brief Construct the podio::ROOTDataSource from the provided file. + /// + explicit ROOTDataSource(const std::string& filePath, int nEvents = -1); - std::vector readers; + /// + /// @brief Construct the podio::ROOTDataSource from the provided file + /// list. + /// + explicit ROOTDataSource(const std::vector& filePathList, int nEvents = -1); - return readers; - } + /// + /// @brief Inform the podio::ROOTDataSource of the desired level of + /// parallelism. + /// + void SetNSlots(unsigned int nSlots) override; /// - /// @brief Create RDataFrame from multiple Podio files. + /// @brief Retrieve from podio::ROOTDataSource per-thread readers for the + /// desired columns. /// - /// @param[in] filePathList List of file paths from which the RDataFrame - /// will be created. - /// @return RDataFrame created from input file list. + template + std::vector GetColumnReaders(std::string_view columnName); + /// - ROOT::RDataFrame CreateDataFrame(const std::vector& filePathList); + /// @brief Inform podio::ROOTDataSource that an event-loop is about to + /// start. + /// + void Initialize() override; /// - /// @brief Create RDataFrame from a Podio file. + /// @brief Retrieve from podio::ROOTDataSource a set of ranges of entries + /// that can be processed concurrently. + /// + std::vector> GetEntryRanges() override; + /// - /// @param[in] filePath File path from which the RDataFrame will be created. - /// @return RDataFrame created from input file list. + /// @brief Inform podio::ROOTDataSource that a certain thread is about to + /// start working on a certain range of entries. + /// + void InitSlot(unsigned int slot, ULong64_t firstEntry) override; + + /// + /// @brief Inform podio::ROOTDataSource that a certain thread is about to + /// start working on a certain entry. + /// + bool SetEntry(unsigned int slot, ULong64_t entry) override; + + /// + /// @brief Inform podio::ROOTDataSource that a certain thread finished + /// working on a certain range of entries. + /// + void FinalizeSlot(unsigned int slot) override; + + /// + /// @brief Inform podio::ROOTDataSource that an event-loop finished. + /// + void Finalize() override; + + /// + /// @brief Returns a reference to the collection of the dataset's column + /// names + /// + const std::vector& GetColumnNames() const override; + /// - ROOT::RDataFrame CreateDataFrame(const std::string& filePath); + /// @brief Checks if the dataset has a certain column. + /// + bool HasColumn(std::string_view columnName) const override; + + /// + /// @brief Type of a column as a string. Required for JITting. + /// + std::string GetTypeName(std::string_view columnName) const override; + +protected: + /// + /// @brief Type-erased vector of pointers to pointers to column + /// values --- one per slot. + /// + Record_t GetColumnReadersImpl(std::string_view name, const std::type_info& typeInfo) override; + + std::string AsString() override { + return "Podio data source"; + }; + +private: + /// Number of slots/threads + unsigned int m_nSlots = 1; + + /// Input filename + std::vector m_filePathList = {}; + + /// Total number of events + unsigned int m_nEvents = 0; + + /// Ranges of events available to be processed + std::vector> m_rangesAvailable = {}; + + /// Ranges of events available ever created + std::vector> m_rangesAll = {}; + + /// Column names + std::vector m_columnNames{}; + + /// Column types + std::vector m_columnTypes = {}; + + /// Collections, m_Collections[columnIndex][slotIndex] + std::vector> m_Collections = {}; + + /// Active collections + std::vector m_activeCollections = {}; + + /// Root podio readers + std::vector> m_podioReaders = {}; + + /// Podio frames + std::vector> m_frames = {}; + + /// + /// @brief Setup input for the podio::ROOTDataSource. + /// + /// @param[in] Number of events. + /// @return void. + /// + void SetupInput(int nEvents); +}; + +template +std::vector ROOTDataSource::GetColumnReaders(std::string_view columnName) { + std::cout << "podio::ROOTDataSource: Getting column readers for column: " << columnName << std::endl; + + std::vector readers; + + return readers; } +/// +/// @brief Create RDataFrame from multiple Podio files. +/// +/// @param[in] filePathList List of file paths from which the RDataFrame +/// will be created. +/// @return RDataFrame created from input file list. +/// +ROOT::RDataFrame CreateDataFrame(const std::vector& filePathList); + +/// +/// @brief Create RDataFrame from a Podio file. +/// +/// @param[in] filePath File path from which the RDataFrame will be created. +/// @return RDataFrame created from input file list. +/// +ROOT::RDataFrame CreateDataFrame(const std::string& filePath); +} // namespace podio + #endif /* PODIO_DATASOURCE_H__ */ diff --git a/src/ROOTDataSource.cc b/src/ROOTDataSource.cc index 3bfaf5e3a..9ee09cc53 100644 --- a/src/ROOTDataSource.cc +++ b/src/ROOTDataSource.cc @@ -3,9 +3,9 @@ // STL #include #include -#include -#include #include +#include +#include #include // ROOT @@ -16,343 +16,293 @@ #include namespace podio { - ROOTDataSource::ROOTDataSource(const std::string& filePath, - int nEvents) : m_nSlots{1} { - m_filePathList.emplace_back(filePath); - SetupInput(nEvents); - } - +ROOTDataSource::ROOTDataSource(const std::string& filePath, int nEvents) : m_nSlots{1} { + m_filePathList.emplace_back(filePath); + SetupInput(nEvents); +} - ROOTDataSource::ROOTDataSource( - const std::vector& filePathList, - int nEvents) : m_nSlots{1}, - m_filePathList{filePathList} { - SetupInput(nEvents); - } +ROOTDataSource::ROOTDataSource(const std::vector& filePathList, int nEvents) : + m_nSlots{1}, m_filePathList{filePathList} { + SetupInput(nEvents); +} +/// @TODO Check for the existence of the file, which might be coming from web +/// or EOS. +void ROOTDataSource::SetupInput(int nEvents) { + // std::cout << "podio::ROOTDataSource: Constructing the source ..." << std::endl; - /// @TODO Check for the existence of the file, which might be coming from web - /// or EOS. - void ROOTDataSource::SetupInput(int nEvents) { - // std::cout << "podio::ROOTDataSource: Constructing the source ..." << std::endl; + if (m_filePathList.empty()) { + throw std::runtime_error("podio::ROOTDataSource: No input files provided!"); + } - if (m_filePathList.empty()) { - throw std::runtime_error("podio::ROOTDataSource: No input files provided!"); + for (const auto& filePath : m_filePathList) { + // Check if file exists + // if (!std::filesystem::exists(filePath)) { + // throw std::runtime_error("podio::ROOTDataSource: Provided file \"" + // + filePath + "\" does not exist!"); + // } + + // Check if the provided file contains required metadata + std::unique_ptr inFile(TFile::Open(filePath.data(), "READ")); + auto metadata = inFile->Get("podio_metadata"); + if (!metadata) { + throw std::runtime_error("podio::ROOTDataSource: Provided file is missing podio metadata!"); } + } + // Create probing frame + podio::Frame frame; + unsigned int nEventsInFiles = 0; + podio::ROOTReader podioReader; + podioReader.openFiles(m_filePathList); + nEventsInFiles = podioReader.getEntries("events"); + frame = podio::Frame(podioReader.readEntry("events", 0)); + + // Determine over how many events to run + if (nEventsInFiles > 0) { + /* + std::cout << "podio::ROOTDataSource: Found " << nEventsInFiles + << " events in files: \n"; for (const auto& filePath : m_filePathList) { - // Check if file exists - // if (!std::filesystem::exists(filePath)) { - // throw std::runtime_error("podio::ROOTDataSource: Provided file \"" - // + filePath + "\" does not exist!"); - // } - - // Check if the provided file contains required metadata - std::unique_ptr inFile(TFile::Open(filePath.data(), "READ")); - auto metadata = inFile->Get("podio_metadata"); - if (!metadata) { - throw std::runtime_error( - "podio::ROOTDataSource: Provided file is missing podio metadata!"); - } + std::cout << " - " << filePath << "\n"; } + */ + } else { + throw std::runtime_error("podio::ROOTDataSource: No events found!"); + } - // Create probing frame - podio::Frame frame; - unsigned int nEventsInFiles = 0; - podio::ROOTReader podioReader; - podioReader.openFiles(m_filePathList); - nEventsInFiles = podioReader.getEntries("events"); - frame = podio::Frame(podioReader.readEntry("events", 0)); - - // Determine over how many events to run - if (nEventsInFiles > 0) { - /* - std::cout << "podio::ROOTDataSource: Found " << nEventsInFiles - << " events in files: \n"; - for (const auto& filePath : m_filePathList) { - std::cout << " - " << filePath << "\n"; - } - */ - } else { - throw std::runtime_error("podio::ROOTDataSource: No events found!"); - } + if (nEvents < 0) { + m_nEvents = nEventsInFiles; + } else if (nEvents == 0) { + throw std::runtime_error("podio::ROOTDataSource: Requested to run over zero events!"); + } else { + m_nEvents = nEvents; + } + if (nEventsInFiles < m_nEvents) { + m_nEvents = nEventsInFiles; + } - if (nEvents < 0) { - m_nEvents = nEventsInFiles; - } else if (nEvents == 0) { - throw std::runtime_error( - "podio::ROOTDataSource: Requested to run over zero events!"); - } else { - m_nEvents = nEvents; - } - if (nEventsInFiles < m_nEvents) { - m_nEvents = nEventsInFiles; + // std::cout << "podio::ROOTDataSource: Running over " << m_nEvents << " events." + // << std::endl; + + // Get collections stored in the files + std::vector collNames = frame.getAvailableCollections(); + // std::cout << "podio::ROOTDataSource: Found following collections:\n"; + for (auto& collName : collNames) { + const podio::CollectionBase* coll = frame.get(collName); + if (coll->isValid()) { + m_columnNames.emplace_back(collName); + m_columnTypes.emplace_back(coll->getValueTypeName()); + // std::cout << " - " << collName << "\n"; } + } +} - // std::cout << "podio::ROOTDataSource: Running over " << m_nEvents << " events." - // << std::endl; - - // Get collections stored in the files - std::vector collNames = frame.getAvailableCollections(); - // std::cout << "podio::ROOTDataSource: Found following collections:\n"; - for (auto& collName: collNames) { - const podio::CollectionBase* coll = frame.get(collName); - if (coll->isValid()) { - m_columnNames.emplace_back(collName); - m_columnTypes.emplace_back(coll->getValueTypeName()); - // std::cout << " - " << collName << "\n"; - } - } +void ROOTDataSource::SetNSlots(unsigned int nSlots) { + // std::cout << "podio::ROOTDataSource: Setting num. of slots to: " << nSlots + // << std::endl; + m_nSlots = nSlots; + + if (m_nSlots > m_nEvents) { + throw std::runtime_error("podio::ROOTDataSource: Number of events too small!"); } + int eventsPerSlot = m_nEvents / m_nSlots; + for (size_t i = 0; i < (m_nSlots - 1); ++i) { + m_rangesAll.emplace_back(eventsPerSlot * i, eventsPerSlot * (i + 1)); + } + m_rangesAll.emplace_back(eventsPerSlot * (m_nSlots - 1), m_nEvents); + m_rangesAvailable = m_rangesAll; - void - ROOTDataSource::SetNSlots(unsigned int nSlots) { - // std::cout << "podio::ROOTDataSource: Setting num. of slots to: " << nSlots - // << std::endl; - m_nSlots = nSlots; + // Initialize set of addresses needed + m_Collections.resize(m_columnNames.size(), std::vector(m_nSlots, nullptr)); - if (m_nSlots > m_nEvents) { - throw std::runtime_error("podio::ROOTDataSource: Number of events too small!"); - } + // Initialize podio readers + for (size_t i = 0; i < m_nSlots; ++i) { + m_podioReaders.emplace_back(std::make_unique()); + } - int eventsPerSlot = m_nEvents / m_nSlots; - for (size_t i = 0; i < (m_nSlots - 1); ++i) { - m_rangesAll.emplace_back(eventsPerSlot * i, eventsPerSlot * (i + 1)); - } - m_rangesAll.emplace_back(eventsPerSlot * (m_nSlots - 1), m_nEvents); - m_rangesAvailable = m_rangesAll; + for (size_t i = 0; i < m_nSlots; ++i) { + m_podioReaders[i]->openFiles(m_filePathList); + } - // Initialize set of addresses needed - m_Collections.resize( - m_columnNames.size(), - std::vector(m_nSlots, nullptr)); + for (size_t i = 0; i < m_nSlots; ++i) { + m_frames.emplace_back(std::make_unique(podio::Frame(m_podioReaders[i]->readEntry("events", 0)))); + } +} - // Initialize podio readers - for (size_t i = 0; i < m_nSlots; ++i) { - m_podioReaders.emplace_back(std::make_unique()); - } +void ROOTDataSource::Initialize() { + // std::cout << "podio::ROOTDataSource: Initializing the source ..." << std::endl; +} - for (size_t i = 0; i < m_nSlots; ++i) { - m_podioReaders[i]->openFiles(m_filePathList); - } +std::vector> ROOTDataSource::GetEntryRanges() { + // std::cout << "podio::ROOTDataSource: Getting entry ranges ..." << std::endl; - for (size_t i = 0; i < m_nSlots; ++i) { - m_frames.emplace_back( - std::make_unique( - podio::Frame(m_podioReaders[i]->readEntry("events", 0)))); + std::vector> rangesToBeProcessed; + for (auto& range : m_rangesAvailable) { + rangesToBeProcessed.emplace_back(std::pair{range.first, range.second}); + if (rangesToBeProcessed.size() >= m_nSlots) { + break; } } - - void - ROOTDataSource::Initialize() { - // std::cout << "podio::ROOTDataSource: Initializing the source ..." << std::endl; + if (m_rangesAvailable.size() > m_nSlots) { + m_rangesAvailable.erase(m_rangesAvailable.begin(), m_rangesAvailable.begin() + m_nSlots); + } else { + m_rangesAvailable.erase(m_rangesAvailable.begin(), m_rangesAvailable.end()); } + /* + std::cout << "podio::ROOTDataSource: Ranges to be processed:\n"; + for (auto& range: rangesToBeProcessed) { + std::cout << " {" << range.first << ", " << range.second + << "}\n"; + } - std::vector> - ROOTDataSource::GetEntryRanges() { - // std::cout << "podio::ROOTDataSource: Getting entry ranges ..." << std::endl; + if (m_rangesAvailable.size() > 0) { - std::vector> rangesToBeProcessed; + std::cout << "podio::ROOTDataSource: Ranges remaining:\n"; for (auto& range: m_rangesAvailable) { - rangesToBeProcessed.emplace_back( - std::pair{range.first, range.second} - ); - if (rangesToBeProcessed.size() >= m_nSlots) { - break; - } - } - - if (m_rangesAvailable.size() > m_nSlots) { - m_rangesAvailable.erase(m_rangesAvailable.begin(), - m_rangesAvailable.begin() + m_nSlots); - } else { - m_rangesAvailable.erase(m_rangesAvailable.begin(), - m_rangesAvailable.end()); - } - - - /* - std::cout << "podio::ROOTDataSource: Ranges to be processed:\n"; - for (auto& range: rangesToBeProcessed) { std::cout << " {" << range.first << ", " << range.second << "}\n"; } - - if (m_rangesAvailable.size() > 0) { - - std::cout << "podio::ROOTDataSource: Ranges remaining:\n"; - for (auto& range: m_rangesAvailable) { - std::cout << " {" << range.first << ", " << range.second - << "}\n"; - } - } else { - std::cout << "podio::ROOTDataSource: No more remaining ranges.\n"; - } - */ - - return rangesToBeProcessed; + } else { + std::cout << "podio::ROOTDataSource: No more remaining ranges.\n"; } + */ + return rangesToBeProcessed; +} - void - ROOTDataSource::InitSlot([[maybe_unused]] unsigned int slot, - [[maybe_unused]] ULong64_t firstEntry) { - // std::cout << "podio::ROOTDataSource: Initializing slot: " << slot - // << " with first entry " << firstEntry << std::endl; - } - +void ROOTDataSource::InitSlot([[maybe_unused]] unsigned int slot, [[maybe_unused]] ULong64_t firstEntry) { + // std::cout << "podio::ROOTDataSource: Initializing slot: " << slot + // << " with first entry " << firstEntry << std::endl; +} - bool - ROOTDataSource::SetEntry(unsigned int slot, ULong64_t entry) { - // std::cout << "podio::ROOTDataSource: In slot: " << slot << ", setting entry: " - // << entry << std::endl; - - m_frames[slot] = std::make_unique( - podio::Frame(m_podioReaders[slot]->readEntry("events", entry))); - - for (auto& collectionIndex: m_activeCollections) { - m_Collections[collectionIndex][slot] = - m_frames[slot]->get(m_columnNames.at(collectionIndex)); - /* - std::cout << "CollName: " << m_columnNames.at(collectionIndex) << "\n"; - std::cout << "Address: " << m_Collections[collectionIndex][slot] << "\n"; - std::cout << "Coll size: " << m_Collections[collectionIndex][slot]->size() << "\n"; - if (m_Collections[collectionIndex][slot]->isValid()) { - std::cout << "Collection valid\n"; - } - */ - } +bool ROOTDataSource::SetEntry(unsigned int slot, ULong64_t entry) { + // std::cout << "podio::ROOTDataSource: In slot: " << slot << ", setting entry: " + // << entry << std::endl; - return true; - } + m_frames[slot] = std::make_unique(podio::Frame(m_podioReaders[slot]->readEntry("events", entry))); - - void - ROOTDataSource::FinalizeSlot([[maybe_unused]] unsigned int slot) { + for (auto& collectionIndex : m_activeCollections) { + m_Collections[collectionIndex][slot] = m_frames[slot]->get(m_columnNames.at(collectionIndex)); /* - std::cout << "podio::ROOTDataSource: Finalizing slot: " << slot << std::endl; - std::cout << "Reader: " << &m_podioReaderRefs[slot].get() << std::endl; - - for (auto& collectionIndex: m_activeCollections) { - std::cout << "CollName: " << m_columnNames.at(collectionIndex) << "\n"; - std::cout << "Address: " << m_Collections[collectionIndex][slot] << "\n"; - if (m_Collections[collectionIndex][slot]->isValid()) { - std::cout << "Collection valid\n"; - } - std::cout << "Coll size: " << m_Collections[collectionIndex][slot]->size() << "\n"; + std::cout << "CollName: " << m_columnNames.at(collectionIndex) << "\n"; + std::cout << "Address: " << m_Collections[collectionIndex][slot] << "\n"; + std::cout << "Coll size: " << m_Collections[collectionIndex][slot]->size() << "\n"; + if (m_Collections[collectionIndex][slot]->isValid()) { + std::cout << "Collection valid\n"; } */ } + return true; +} + +void ROOTDataSource::FinalizeSlot([[maybe_unused]] unsigned int slot) { + /* + std::cout << "podio::ROOTDataSource: Finalizing slot: " << slot << std::endl; + std::cout << "Reader: " << &m_podioReaderRefs[slot].get() << std::endl; - void - ROOTDataSource::Finalize() { - // std::cout << "podio::ROOTDataSource: Finalizing ..." << std::endl; + for (auto& collectionIndex: m_activeCollections) { + std::cout << "CollName: " << m_columnNames.at(collectionIndex) << "\n"; + std::cout << "Address: " << m_Collections[collectionIndex][slot] << "\n"; + if (m_Collections[collectionIndex][slot]->isValid()) { + std::cout << "Collection valid\n"; + } + std::cout << "Coll size: " << m_Collections[collectionIndex][slot]->size() << "\n"; } + */ +} +void ROOTDataSource::Finalize() { + // std::cout << "podio::ROOTDataSource: Finalizing ..." << std::endl; +} - Record_t - ROOTDataSource::GetColumnReadersImpl( - std::string_view columnName, - [[maybe_unused]] const std::type_info& typeInfo) { - /* - std::cout << "podio::ROOTDataSource: Getting column reader implementation for column:\n" - << " " << columnName - << "\n with type: " << typeInfo.name() << std::endl; - */ +Record_t ROOTDataSource::GetColumnReadersImpl(std::string_view columnName, + [[maybe_unused]] const std::type_info& typeInfo) { + /* + std::cout << "podio::ROOTDataSource: Getting column reader implementation for column:\n" + << " " << columnName + << "\n with type: " << typeInfo.name() << std::endl; + */ + + auto itr = std::find(m_columnNames.begin(), m_columnNames.end(), columnName); + if (itr == m_columnNames.end()) { + std::string errMsg = "podio::ROOTDataSource: Can't find requested column \""; + errMsg += columnName; + errMsg += "\"!"; + throw std::runtime_error(errMsg); + } + auto columnIndex = std::distance(m_columnNames.begin(), itr); + m_activeCollections.emplace_back(columnIndex); + /* + std::cout << "podio::ROOTDataSource: Active collections so far:\n" + << " "; + for (auto& i: m_activeCollections) { + std::cout << i << ", "; + } + std::cout << std::endl; + */ - auto itr = std::find(m_columnNames.begin(), m_columnNames.end(), - columnName); - if (itr == m_columnNames.end()) { - std::string errMsg = "podio::ROOTDataSource: Can't find requested column \""; - errMsg += columnName; - errMsg += "\"!"; - throw std::runtime_error(errMsg); - } - auto columnIndex = std::distance(m_columnNames.begin(), itr); - m_activeCollections.emplace_back(columnIndex); + Record_t columnReaders(m_nSlots); + for (size_t slotIndex = 0; slotIndex < m_nSlots; ++slotIndex) { /* - std::cout << "podio::ROOTDataSource: Active collections so far:\n" - << " "; - for (auto& i: m_activeCollections) { - std::cout << i << ", "; - } - std::cout << std::endl; + std::cout << " Column index: " << columnIndex << "\n"; + std::cout << " Slot index: " << slotIndex << "\n"; + std::cout << " Address: " + << &m_Collections[columnIndex][slotIndex] + << std::endl; */ - - Record_t columnReaders(m_nSlots); - for (size_t slotIndex = 0; slotIndex < m_nSlots; ++slotIndex) { - /* - std::cout << " Column index: " << columnIndex << "\n"; - std::cout << " Slot index: " << slotIndex << "\n"; - std::cout << " Address: " - << &m_Collections[columnIndex][slotIndex] - << std::endl; - */ - columnReaders[slotIndex] = (void*) &m_Collections[columnIndex][slotIndex]; - } - - return columnReaders; + columnReaders[slotIndex] = (void*)&m_Collections[columnIndex][slotIndex]; } + return columnReaders; +} - const std::vector& - ROOTDataSource::GetColumnNames() const { - // std::cout << "podio::ROOTDataSource: Looking for column names" << std::endl; - - return m_columnNames; - } - +const std::vector& ROOTDataSource::GetColumnNames() const { + // std::cout << "podio::ROOTDataSource: Looking for column names" << std::endl; - bool - ROOTDataSource::HasColumn(std::string_view columnName) const { - // std::cout << "podio::ROOTDataSource: Looking for column: " << columnName - // << std::endl; + return m_columnNames; +} - if (std::find(m_columnNames.begin(), - m_columnNames.end(), - columnName) != m_columnNames.end()) { - return true; - } +bool ROOTDataSource::HasColumn(std::string_view columnName) const { + // std::cout << "podio::ROOTDataSource: Looking for column: " << columnName + // << std::endl; - return false; + if (std::find(m_columnNames.begin(), m_columnNames.end(), columnName) != m_columnNames.end()) { + return true; } + return false; +} - std::string - ROOTDataSource::GetTypeName(std::string_view columnName) const { - // std::cout << "podio::ROOTDataSource: Looking for type name of column: " - // << columnName << std::endl; - - auto itr = std::find(m_columnNames.begin(), m_columnNames.end(), - columnName); - if (itr != m_columnNames.end()) { - auto i = std::distance(m_columnNames.begin(), itr); - // std::cout << "podio::ROOTDataSource: Found type name: " - // << m_columnTypes.at(i) << std::endl; +std::string ROOTDataSource::GetTypeName(std::string_view columnName) const { + // std::cout << "podio::ROOTDataSource: Looking for type name of column: " + // << columnName << std::endl; - return m_columnTypes.at(i) + "Collection"; - } + auto itr = std::find(m_columnNames.begin(), m_columnNames.end(), columnName); + if (itr != m_columnNames.end()) { + auto i = std::distance(m_columnNames.begin(), itr); + // std::cout << "podio::ROOTDataSource: Found type name: " + // << m_columnTypes.at(i) << std::endl; - return "float"; + return m_columnTypes.at(i) + "Collection"; } + return "float"; +} - ROOT::RDataFrame - CreateDataFrame(const std::vector& filePathList) { - ROOT::RDataFrame rdf(std::make_unique(filePathList)); - - return rdf; - } +ROOT::RDataFrame CreateDataFrame(const std::vector& filePathList) { + ROOT::RDataFrame rdf(std::make_unique(filePathList)); + return rdf; +} - ROOT::RDataFrame - CreateDataFrame(const std::string& filePath) { - ROOT::RDataFrame rdf(std::make_unique(filePath)); +ROOT::RDataFrame CreateDataFrame(const std::string& filePath) { + ROOT::RDataFrame rdf(std::make_unique(filePath)); - return rdf; - } + return rdf; } +} // namespace podio diff --git a/tests/root_io/read_with_rdatasource_root.cpp b/tests/root_io/read_with_rdatasource_root.cpp index 1933e6c83..bb7e220d6 100644 --- a/tests/root_io/read_with_rdatasource_root.cpp +++ b/tests/root_io/read_with_rdatasource_root.cpp @@ -1,25 +1,22 @@ #include "read_frame.h" #include "read_frame_auxiliary.h" -#include "podio/ROOTDataSource.h" #include "datamodel/ExampleClusterCollection.h" +#include "podio/ROOTDataSource.h" #include #include - -ROOT::VecOps::RVec -getEnergy(const ExampleClusterCollection& inColl) { +ROOT::VecOps::RVec getEnergy(const ExampleClusterCollection& inColl) { ROOT::VecOps::RVec result; - for (const auto& cluster: inColl) { + for (const auto& cluster : inColl) { result.push_back(cluster.energy()); } return result; } - int main(int argc, const char* argv[]) { std::string inputFile = "example_frame.root"; if (argc == 2) { @@ -34,8 +31,7 @@ int main(int argc, const char* argv[]) { dframe.Describe().Print(); std::cout << std::endl; - auto cluterEnergy = dframe.Define("cluster_energy", getEnergy, {"clusters"}) - .Histo1D("cluster_energy"); + auto cluterEnergy = dframe.Define("cluster_energy", getEnergy, {"clusters"}).Histo1D("cluster_energy"); cluterEnergy->Print(); return EXIT_SUCCESS;