Skip to content

Commit

Permalink
[wip] Add necessary functionality for SIO backend
Browse files Browse the repository at this point in the history
  • Loading branch information
tmadlener committed Feb 11, 2022
1 parent 919598b commit b1d08a0
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 5 deletions.
2 changes: 1 addition & 1 deletion include/podio/AssociationCollection.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class AssociationCollection : public podio::CollectionBase {
}

std::string getValueTypeName() const override {
return std::string("podio::Association<") + FromT::TypeName + "," + ToT::TypeName + ">";
return podio::detail::associationSIOName<FromT, ToT>();
}

std::string getDataTypeName() const override {
Expand Down
12 changes: 12 additions & 0 deletions include/podio/AssociationFwd.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#ifndef PODIO_ASSOCIATIONFWD_H
#define PODIO_ASSOCIATIONFWD_H

#include <algorithm>
#include <deque>
#include <string>
#include <vector>

namespace podio {
Expand All @@ -22,6 +24,10 @@ namespace detail {
template <typename T>
using GetDefT = typename GetDefType<T>::type;

/**
* Helper template struct and type-alias to retrieve the collection type from
* a given data type
*/
template <typename T>
struct GetCollType {
using type = typename T::CollT;
Expand All @@ -30,6 +36,12 @@ namespace detail {
template <typename T>
using GetCollT = typename GetCollType<T>::type;

template <typename FromT, typename ToT>
inline std::string associationSIOName() {
auto n = std::string("Association_FROM_") + FromT::TypeName + "_TO_" + FromT::TypeName;
std::replace(n.begin(), n.end(), ':', '_');
return n;
}
} // namespace detail

// Forward declarations and typedefs used throughout the whole Association
Expand Down
78 changes: 78 additions & 0 deletions include/podio/AssociationSIOBlock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#ifndef PODIO_ASSOCIATIONSIOBLOCK_H
#define PODIO_ASSOCIATIONSIOBLOCK_H

#include "podio/AssociationFwd.h"
#include "podio/SIOBlock.h"

#include <sio/api.h>
#include <sio/io_device.h>
#include <sio/version.h>

#include <algorithm>
#include <string>

namespace podio {
template <typename FromT, typename ToT>
class AssociationSIOBlock : public podio::SIOBlock {
public:
AssociationSIOBlock() :
SIOBlock(podio::detail::associationSIOName<FromT, ToT>(), sio::version::encode_version(0, 1)) {
podio::SIOBlockFactory::instance().registerBlockForCollection(podio::detail::associationSIOName<FromT, ToT>(),
this);
}

AssociationSIOBlock(const std::string& name) : SIOBlock(name, sio::version::encode_version(0, 1)) {
}

void read(sio::read_device& device, sio::version_type) override {
auto buffers = _col->getBuffers();
if (!_col->isSubsetCollection()) {
auto* dataVec = buffers.dataAsVector<float>();
unsigned size{0};
device.data(size);
dataVec->resize(size);
podio::handlePODDataSIO(device, dataVec->data(), size);
}

// ---- references
auto* refColls = buffers.references;
for (auto& refC : *refColls) {
unsigned size{0};
device.data(size);
refC->resize(size);
podio::handlePODDataSIO(device, refC->data(), size);
}
}

void write(sio::write_device& device) override {
_col->prepareForWrite();
auto buffers = _col->getBuffers();
if (!_col->isSubsetCollection()) {
auto* dataVec = buffers.dataAsVector<float>();
unsigned size = dataVec->size();
device.data(size);
podio::handlePODDataSIO(device, dataVec->data(), size);
}

// ---- references
auto* refColls = buffers.references;
for (auto& refC : *refColls) {
unsigned size = refC->size();
device.data(size);
podio::handlePODDataSIO(device, refC->data(), size);
}
}

void createCollection(const bool subsetCollection = false) override {
setCollection(new AssociationCollection<FromT, ToT>());
_col->setSubsetCollection(subsetCollection);
}

SIOBlock* create(const std::string& name) const override {
return new AssociationSIOBlock(name);
}
};

} // namespace podio

#endif // PODIO_ASSOCIATIONBLOCK_H
4 changes: 1 addition & 3 deletions tests/associations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,7 @@ TEST_CASE("Association basics", "[associations]") {
TEST_CASE("AssociationCollection basics", "[associations]") {
auto coll = TestAColl();

REQUIRE(coll.getValueTypeName() == "podio::Association<ExampleHit,ExampleCluster>");

REQUIRE(true);
// TODO: basics without I/O
}

TEST_CASE("AssociationCollection constness", "[associations][static-checks][const-correctness]") {
Expand Down
4 changes: 4 additions & 0 deletions tests/read_and_write_sio.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
#include "read_test.h"

#include "podio/AssociationSIOBlock.h"
#include "podio/EventStore.h"
#include "podio/SIOReader.h"
#include "podio/SIOWriter.h"

// User needs to declare this for generation and plugin loading for SIO!
static podio::AssociationSIOBlock<ExampleMC, ex42::ExampleWithARelation> anAssoc;

int main() {
podio::SIOReader reader;
reader.openFile("example.sio");
Expand Down
7 changes: 7 additions & 0 deletions tests/read_sio.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
#include "podio/AssociationSIOBlock.h"
#include "podio/SIOReader.h"
#include "read_test.h"

#include "datamodel/ExampleMC.h"
#include "datamodel/ExampleWithARelation.h"

// User needs to declare this for generation and plugin loading for SIO!
static podio::AssociationSIOBlock<ExampleMC, ex42::ExampleWithARelation> anAssoc;

int main() {
// auto reader = podio::SIOReader();
podio::SIOReader reader; // SIOReader has no copy c'tor ...
Expand Down
3 changes: 2 additions & 1 deletion tests/read_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -395,10 +395,11 @@ void processEvent(podio::EventStore& store, int eventNum, podio::version::Versio
if (associations.size() != nmspaces.size()) {
throw std::runtime_error("AssociationsCollection does not have the expected size");
}
const auto nNameSpc = nmspaces.size();
int assocIndex = 0;
for (auto assoc : associations) {
if (!((assoc.getWeight() == 0.5 * assocIndex) && (assoc.getFrom() == mcps[assocIndex]) &&
(assoc.getTo() == nmspaces[assocIndex]))) {
(assoc.getTo() == nmspaces[nNameSpc - 1 - assocIndex]))) {
throw std::runtime_error("Association does not have expected content");
}
assocIndex++;
Expand Down
8 changes: 8 additions & 0 deletions tests/read_timed_sio.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
#include "podio/AssociationSIOBlock.h"
#include "podio/BenchmarkRecorder.h"
#include "podio/SIOReader.h"
#include "podio/TimedReader.h"

#include "datamodel/ExampleMC.h"
#include "datamodel/ExampleWithARelation.h"

#include "read_test.h"

// User needs to declare this for generation and plugin loading for SIO!
static podio::AssociationSIOBlock<ExampleMC, ex42::ExampleWithARelation> anAssoc;

int main() {
podio::benchmark::BenchmarkRecorder recorder("read_benchmark_sio.root");

Expand Down
8 changes: 8 additions & 0 deletions tests/write_sio.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
#include "podio/AssociationSIOBlock.h"
#include "podio/EventStore.h"
#include "podio/SIOWriter.h"

#include "datamodel/ExampleMC.h"
#include "datamodel/ExampleWithARelation.h"

#include "write_test.h"

// User needs to declare this for generation and plugin loading for SIO!
static podio::AssociationSIOBlock<ExampleMC, ex42::ExampleWithARelation> anAssoc;

int main(int, char**) {
podio::EventStore store;
podio::SIOWriter writer("example.sio", &store);
Expand Down
7 changes: 7 additions & 0 deletions tests/write_timed_sio.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
#include "podio/AssociationSIOBlock.h"
#include "podio/BenchmarkRecorder.h"
#include "podio/EventStore.h"
#include "podio/SIOWriter.h"
#include "podio/TimedWriter.h"

#include "datamodel/ExampleMC.h"
#include "datamodel/ExampleWithARelation.h"

#include "write_test.h"
// User needs to declare this for generation and plugin loading for SIO!
static podio::AssociationSIOBlock<ExampleMC, ex42::ExampleWithARelation> anAssoc;

int main() {
podio::benchmark::BenchmarkRecorder recorder("write_benchmark_sio.root");
Expand Down

0 comments on commit b1d08a0

Please sign in to comment.