Skip to content

Commit

Permalink
Improve the interface for producers
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcarcell committed Aug 8, 2023
1 parent 7487241 commit 012e9f8
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 41 deletions.
19 changes: 19 additions & 0 deletions k4FWCore/include/k4FWCore/FunctionalUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef K4FWCORE_FUNCTIONALUTILS_H
#define K4FWCORE_FUNCTIONALUTILS_H

#include "GaudiAlg/GaudiAlgorithm.h"
#include "GaudiKernel/DataObjectHandle.h"
#include "k4FWCore/DataWrapper.h"

// Base class used for the Traits template argument of the
// Gaudi::Functional algorithms
struct BaseClass_t {
template<typename T>
using OutputHandle = DataObjectWriteHandle<DataWrapper<T>>;
// template<typename T>
// using InputHandle = DataObjectReadHandle<DataWrapper<T>>;

using BaseClass = Gaudi::Algorithm;
};

#endif
23 changes: 11 additions & 12 deletions test/k4FWCoreTest/src/components/ExampleFunctionalProducer.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
#include <GaudiKernel/DataObjectHandle.h>
#include "Gaudi/Algorithm.h"
#include "Gaudi/Property.h"
#include "GaudiAlg/Producer.h"
#include "k4FWCore/DataWrapper.h"
#include "GaudiKernel/DataObjectHandle.h"
#include "k4FWCore/FunctionalUtils.h"

#include "edm4hep/MCParticleCollection.h"

#include <string>

// This will always be Gaudi::Algorithm
using BaseClass_t = Gaudi::Functional::Traits::BaseClass_t<Gaudi::Algorithm>;

// Which type of collection we are producing
// Has to be wrapped in DataWrapper
using colltype = DataWrapper<edm4hep::MCParticleCollection>;
// Which collection we are producing
using colltype = edm4hep::MCParticleCollection;

struct ExampleFunctionalProducer final : Gaudi::Functional::Producer<colltype(), BaseClass_t> {
// The pair in KeyValue can be changed from python and it corresponds
Expand All @@ -22,11 +20,12 @@ struct ExampleFunctionalProducer final : Gaudi::Functional::Producer<colltype(),

// This is the function that will be called to produce the data
colltype operator()() const override {
auto coll = std::make_unique<edm4hep::MCParticleCollection>();
coll->push_back({1, 2, 3, 4, 5, 6, {}, {}, {}, {}, {}, {}});
coll->push_back({2, 3, 4, 5, 6, 7, {}, {}, {}, {}, {}, {}});
colltype dw = DataWrapper<edm4hep::MCParticleCollection>(std::move(coll));
return dw;
auto coll = edm4hep::MCParticleCollection();
coll.push_back({1, 2, 3, 4, 5, 6, {}, {}, {}, {}, {}, {}});
coll.push_back({2, 3, 4, 5, 6, 7, {}, {}, {}, {}, {}, {}});
// We have to return whatever collection type we specified in the
// template argument
return coll;
}

private:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "Gaudi/Algorithm.h"
#include "Gaudi/Property.h"
#include "GaudiAlg/Producer.h"
#include "k4FWCore/DataWrapper.h"
#include "k4FWCore/FunctionalUtils.h"

#include "edm4hep/MCParticleCollection.h"
#include "edm4hep/SimTrackerHitCollection.h"
Expand All @@ -11,16 +11,12 @@

#include <string>

// This will always be Gaudi::Algorithm
using BaseClass_t = Gaudi::Functional::Traits::BaseClass_t<Gaudi::Algorithm>;

// Which type of collections we are producing
// They have to be wrapped in DataWrapper
using Float_t = DataWrapper<podio::UserDataCollection<float>>;
using Particle_t = DataWrapper<edm4hep::MCParticleCollection>;
using SimTrackerHit_t = DataWrapper<edm4hep::SimTrackerHitCollection>;
using TrackerHit_t = DataWrapper<edm4hep::TrackerHitCollection>;
using Track_t = DataWrapper<edm4hep::TrackCollection>;
using Float_t = podio::UserDataCollection<float>;
using Particle_t = edm4hep::MCParticleCollection;
using SimTrackerHit_t = edm4hep::SimTrackerHitCollection;
using TrackerHit_t = edm4hep::TrackerHitCollection;
using Track_t = edm4hep::TrackCollection;

struct ExampleFunctionalProducerMultiple final
: Gaudi::Functional::Producer<std::tuple<Float_t, Particle_t, SimTrackerHit_t, TrackerHit_t, Track_t>(),
Expand All @@ -39,34 +35,30 @@ struct ExampleFunctionalProducerMultiple final
// The following was copied and adapted from the
// k4FWCoreTest_CreateExampleEventData test

auto floatVector = std::make_unique<podio::UserDataCollection<float>>();
floatVector->push_back(125.);
floatVector->push_back(25.);
floatVector->push_back(m_event);
Float_t floatVectorDW = DataWrapper<podio::UserDataCollection<float>>(std::move(floatVector));
auto floatVector = podio::UserDataCollection<float>();
floatVector.push_back(125.);
floatVector.push_back(25.);
floatVector.push_back(m_event);

auto particles = std::make_unique<edm4hep::MCParticleCollection>();
auto particle = particles->create();
auto particles = edm4hep::MCParticleCollection();
auto particle = particles.create();
auto& p4 = particle.momentum();
p4.x = m_magicNumberOffset + m_event + 5;
p4.y = m_magicNumberOffset + 6;
p4.z = m_magicNumberOffset + 7;
particle.setMass(m_magicNumberOffset + m_event + 8);
Particle_t particleDW = DataWrapper<edm4hep::MCParticleCollection>(std::move(particles));

auto simTrackerHits = std::make_unique<edm4hep::SimTrackerHitCollection>();
auto hit = simTrackerHits->create();
auto simTrackerHits = edm4hep::SimTrackerHitCollection();
auto hit = simTrackerHits.create();
hit.setPosition({3, 4, 5});
SimTrackerHit_t simTrackerHitDW = DataWrapper<edm4hep::SimTrackerHitCollection>(std::move(simTrackerHits));

auto trackerHits = std::make_unique<edm4hep::TrackerHitCollection>();
auto trackerHit = trackerHits->create();
auto trackerHits = edm4hep::TrackerHitCollection();
auto trackerHit = trackerHits.create();
trackerHit.setPosition({3, 4, 5});
TrackerHit_t trackerHitDW = DataWrapper<edm4hep::TrackerHitCollection>(std::move(trackerHits));

auto tracks = std::make_unique<edm4hep::TrackCollection>();
auto track = tracks->create();
auto track2 = tracks->create();
auto tracks = edm4hep::TrackCollection();
auto track = tracks.create();
auto track2 = tracks.create();
// set members
track.setType(1);
track.setChi2(2.1);
Expand All @@ -80,9 +72,9 @@ struct ExampleFunctionalProducerMultiple final
// set associatons
track.addToTrackerHits(trackerHit);
track.addToTracks(track2);
Track_t trackDW = DataWrapper<edm4hep::TrackCollection>(std::move(tracks));

return std::make_tuple(floatVectorDW, particleDW, simTrackerHitDW, trackerHitDW, trackDW);
return std::make_tuple(std::move(floatVector), std::move(particles), std::move(simTrackerHits),
std::move(trackerHits), std::move(tracks));
}

private:
Expand Down

0 comments on commit 012e9f8

Please sign in to comment.