-
Notifications
You must be signed in to change notification settings - Fork 29
[WIP] Add a service to allow retrieving collections to which an object belongs #312
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tmadlener
wants to merge
8
commits into
key4hep:main
Choose a base branch
from
tmadlener:collid-assignment
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4c9f0ed
Add tests for checking the collection ID
tmadlener dfaf86f
Assign a collection id when putting collections to the TES
tmadlener 3867e8c
Add the collection id table to the TES
tmadlener f0d83bf
Add service to retrieve collections from objects
tmadlener 5d18c98
Add tests for collection retrieval from objects
tmadlener 93b054a
Remove no longer necessary test algorithm
tmadlener da3f690
Make it possible to retrieve collection names via new svc
tmadlener ea2146f
Always add the new service to the ApplicationMgr
tmadlener File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Copyright (c) 2014-2024 Key4hep-Project. | ||
* | ||
* This file is part of Key4hep. | ||
* See https://key4hep.github.io/key4hep-doc/ for further info. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
#include "CollectionFromObjectSvc.h" | ||
|
||
#include "k4FWCore/FunctionalUtils.h" | ||
|
||
#include <podio/CollectionBase.h> | ||
#include <podio/ObjectID.h> | ||
|
||
#include <fmt/format.h> | ||
#include <fmt/ostream.h> | ||
|
||
template <> | ||
struct fmt::formatter<podio::ObjectID> : ostream_formatter {}; | ||
|
||
StatusCode CollectionFromObjectSvc::initialize() { | ||
auto sc = Service::initialize(); | ||
if (sc.isFailure()) { | ||
error() << "Unable to initialize base class Serivce." << endmsg; | ||
return sc; | ||
} | ||
m_dataSvc = service("EventDataSvc"); | ||
if (!m_dataSvc) { | ||
error() << "Unable to locate the EventDataSvc" << endmsg; | ||
return StatusCode::FAILURE; | ||
} | ||
|
||
return StatusCode::SUCCESS; | ||
} | ||
|
||
const std::optional<std::string> CollectionFromObjectSvc::getCollectionNameFor(const podio::ObjectID id) const { | ||
debug() << "Trying to retrieve collection name for object " << id << endmsg; | ||
const auto& idTable = k4FWCore::details::getTESCollectionIDTable(m_dataSvc, this); | ||
auto name = idTable.name(id.collectionID); | ||
if (!name.has_value()) { | ||
error() << "Could not get a collection name for object " << id << endmsg; | ||
return std::nullopt; | ||
} | ||
return name.value(); | ||
} | ||
|
||
const podio::CollectionBase* CollectionFromObjectSvc::getCollectionFor(const podio::ObjectID id) const { | ||
debug() << "Trying to retrieve collection for object " << id << endmsg; | ||
const auto& idTable = k4FWCore::details::getTESCollectionIDTable(m_dataSvc, this); | ||
auto name = idTable.name(id.collectionID); | ||
if (!name.has_value()) { | ||
error() << "Could not get a collection name for object " << id << endmsg; | ||
return nullptr; | ||
} | ||
|
||
DataObject* p{nullptr}; | ||
if (m_dataSvc->retrieveObject("/Event/" + name.value(), p).isFailure()) { | ||
error() << "Could not get the collection '" << name.value() << "' from the Event store" << endmsg; | ||
return nullptr; | ||
} | ||
const auto collWrapper = dynamic_cast<AnyDataWrapper<std::unique_ptr<podio::CollectionBase>>*>(p); | ||
if (!collWrapper) { | ||
error() << "Could not cast data object to the necessary type for collection " << name.value() << endmsg; | ||
return nullptr; | ||
} | ||
|
||
return collWrapper->getData().get(); | ||
} | ||
|
||
DECLARE_COMPONENT(CollectionFromObjectSvc) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright (c) 2014-2024 Key4hep-Project. | ||
* | ||
* This file is part of Key4hep. | ||
* See https://key4hep.github.io/key4hep-doc/ for further info. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
#ifndef FWCORE_COLLECTIONFROMOBJECTSVC_H | ||
#define FWCORE_COLLECTIONFROMOBJECTSVC_H | ||
|
||
#include <GaudiKernel/IDataProviderSvc.h> | ||
#include <GaudiKernel/Service.h> | ||
|
||
#include <podio/CollectionBase.h> | ||
#include <podio/ObjectID.h> | ||
|
||
#include "k4FWCore/ICollectionFromObjectSvc.h" | ||
|
||
class CollectionFromObjectSvc : public extends<Service, ICollectionFromObjectSvc> { | ||
using extends::extends; | ||
|
||
public: | ||
StatusCode initialize() override; | ||
|
||
protected: | ||
const podio::CollectionBase* getCollectionFor(const podio::ObjectID id) const override; | ||
const std::optional<std::string> getCollectionNameFor(const podio::ObjectID id) const override; | ||
|
||
private: | ||
SmartIF<IDataProviderSvc> m_dataSvc; | ||
}; | ||
|
||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright (c) 2014-2024 Key4hep-Project. | ||
* | ||
* This file is part of Key4hep. | ||
* See https://key4hep.github.io/key4hep-doc/ for further info. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
#ifndef FWCORE_ICOLLECTIONFROMOBJECTSVC_H | ||
#define FWCORE_ICOLLECTIONFROMOBJECTSVC_H | ||
|
||
#include <GaudiKernel/IInterface.h> | ||
|
||
#include <podio/CollectionBase.h> | ||
#include <podio/ObjectID.h> | ||
#include <podio/utilities/TypeHelpers.h> | ||
|
||
#include <optional> | ||
#include <string> | ||
|
||
class ICollectionFromObjectSvc : virtual public IInterface { | ||
public: | ||
DeclareInterfaceID(ICollectionFromObjectSvc, 1, 0); | ||
|
||
template <podio::ObjectType O> | ||
const typename O::collection_type* getCollectionFor(const O& object) const { | ||
return dynamic_cast<const typename O::collection_type*>(getCollectionFor(object.id())); | ||
} | ||
|
||
// TODO: return string_view? Some form of DataHandle? | ||
template <podio::ObjectType O> | ||
const std::optional<std::string> getCollectionNameFor(const O& object) const { | ||
return getCollectionNameFor(object.id()); | ||
} | ||
|
||
protected: | ||
virtual const podio::CollectionBase* getCollectionFor(const podio::ObjectID) const = 0; | ||
virtual const std::optional<std::string> getCollectionNameFor(const podio::ObjectID) const = 0; | ||
}; | ||
|
||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
test/k4FWCoreTest/options/TestCollectionFromObjectRetrieval.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#!/usr/bin/env python3 | ||
# | ||
# Copyright (c) 2014-2024 Key4hep-Project. | ||
# | ||
# This file is part of Key4hep. | ||
# See https://key4hep.github.io/key4hep-doc/ for further info. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
# This is a simple test checking that the collection IDs that are assigned for | ||
# the Functional algorithms are the same as we get when adding to a frame. | ||
# Effectively, it checks whether we hash the right thing when we assign a | ||
# collection ID. We need this check because for Functional algorithms the | ||
# insertion into a Frame (where the usual assignment happens) happens just | ||
# before writing. | ||
|
||
from Gaudi.Configuration import VERBOSE | ||
from Configurables import ( | ||
TestCollectionFromObjectRetrieval, | ||
ExampleFunctionalProducerMultiple, | ||
CollectionFromObjectSvc, | ||
) | ||
|
||
from k4FWCore import ApplicationMgr | ||
from Configurables import EventDataSvc | ||
|
||
producer = ExampleFunctionalProducerMultiple( | ||
"Producer", OutputCollectionParticles1=["UnconventionalCollName"], OutputLevel=VERBOSE | ||
) | ||
|
||
|
||
collid_checker = TestCollectionFromObjectRetrieval( | ||
"CollectionIDChecker", InputCollection=["UnconventionalCollName"], OutputLevel=VERBOSE | ||
) | ||
|
||
ApplicationMgr( | ||
TopAlg=[producer, collid_checker], | ||
EvtSel="NONE", | ||
EvtMax=1, | ||
ExtSvc=[EventDataSvc(), CollectionFromObjectSvc("CollectionFromObjSvc")], | ||
) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could also be changed to something that works with podio 1.3, as this is not part of that tag.