Skip to content

Commit da3f690

Browse files
committed
Make it possible to retrieve collection names via new svc
1 parent 93b054a commit da3f690

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

k4FWCore/components/CollectionFromObjectSvc.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ StatusCode CollectionFromObjectSvc::initialize() {
4444
return StatusCode::SUCCESS;
4545
}
4646

47+
const std::optional<std::string> CollectionFromObjectSvc::getCollectionNameFor(const podio::ObjectID id) const {
48+
debug() << "Trying to retrieve collection name for object " << id << endmsg;
49+
const auto& idTable = k4FWCore::details::getTESCollectionIDTable(m_dataSvc, this);
50+
auto name = idTable.name(id.collectionID);
51+
if (!name.has_value()) {
52+
error() << "Could not get a collection name for object " << id << endmsg;
53+
return std::nullopt;
54+
}
55+
return name.value();
56+
}
57+
4758
const podio::CollectionBase* CollectionFromObjectSvc::getCollectionFor(const podio::ObjectID id) const {
4859
debug() << "Trying to retrieve collection for object " << id << endmsg;
4960
const auto& idTable = k4FWCore::details::getTESCollectionIDTable(m_dataSvc, this);

k4FWCore/components/CollectionFromObjectSvc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class CollectionFromObjectSvc : public extends<Service, ICollectionFromObjectSvc
3535

3636
protected:
3737
const podio::CollectionBase* getCollectionFor(const podio::ObjectID id) const override;
38+
const std::optional<std::string> getCollectionNameFor(const podio::ObjectID id) const override;
3839

3940
private:
4041
SmartIF<IDataProviderSvc> m_dataSvc;

k4FWCore/include/k4FWCore/ICollectionFromObjectSvc.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
#include <podio/ObjectID.h>
2626
#include <podio/utilities/TypeHelpers.h>
2727

28-
#include <fmt/format.h>
28+
#include <optional>
29+
#include <string>
2930

3031
class ICollectionFromObjectSvc : virtual public IInterface {
3132
public:
@@ -36,8 +37,15 @@ class ICollectionFromObjectSvc : virtual public IInterface {
3637
return dynamic_cast<const typename O::collection_type*>(getCollectionFor(object.id()));
3738
}
3839

40+
// TODO: return string_view? Some form of DataHandle?
41+
template <podio::ObjectType O>
42+
const std::optional<std::string> getCollectionNameFor(const O& object) const {
43+
return getCollectionNameFor(object.id());
44+
}
45+
3946
protected:
4047
virtual const podio::CollectionBase* getCollectionFor(const podio::ObjectID) const = 0;
48+
virtual const std::optional<std::string> getCollectionNameFor(const podio::ObjectID) const = 0;
4149
};
4250

4351
#endif

test/k4FWCoreTest/src/components/TestCollectionFromObjectRetrieval.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,17 @@ struct TestCollectionFromObjectRetrieval final : k4FWCore::Consumer<void(const e
4343
throw std::runtime_error("Could not get the expected collection from the object");
4444
}
4545

46+
const auto name = m_collFromObjSvc->getCollectionNameFor(mc);
47+
if (name.value() != inputLocations(0)[0]) {
48+
throw std::runtime_error("Collection name retrieved via object of collection is not as expected");
49+
}
50+
4651
auto newMCParticle = edm4hep::MutableMCParticle{};
52+
const auto invalidName = m_collFromObjSvc->getCollectionNameFor(newMCParticle);
53+
if (invalidName.has_value()) {
54+
throw std::runtime_error("Retrieved a name for an object that is not known to the TES");
55+
}
56+
4757
const auto* invalidColl = m_collFromObjSvc->getCollectionFor(newMCParticle);
4858
if (invalidColl) {
4959
throw std::runtime_error("Could get a collection for an object that is not in a collection");

0 commit comments

Comments
 (0)