Skip to content
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

Add an option to copy CellID metadata when overlaying #12

Merged
merged 5 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions k4Reco/Overlay/components/OverlayTiming.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,23 @@
#include "OverlayTiming.h"
#include <GaudiKernel/MsgStream.h>

#include "podio/Frame.h"
#include "podio/FrameCategories.h"

#include "edm4hep/MutableCaloHitContribution.h"
#include "edm4hep/CaloHitContributionCollection.h"
#include "edm4hep/Constants.h"
#include "edm4hep/EventHeaderCollection.h"
#include "edm4hep/MCParticleCollection.h"
#include "edm4hep/SimCalorimeterHitCollection.h"
#include "edm4hep/SimTrackerHitCollection.h"

#include "k4FWCore/MetadataUtils.h"

#include <TMath.h>

#include <limits>
#include <random>
#include <utility>
#include <vector>

template <typename T> inline float time_of_flight(const T& pos) {
// Returns the time of flight to the radius in ns
Expand Down Expand Up @@ -378,4 +387,25 @@ retType OverlayTiming::operator()(const edm4hep::EventHeaderCollection&
std::move(ocaloHitContribs));
}

StatusCode OverlayTiming::finalize() {
if (m_copyCellIDMetadata) {
for (auto& [input, output] :
{std::make_pair(inputLocations("SimTrackerHits"), outputLocations("OutputSimTrackerHits")),
std::make_pair(inputLocations("SimCalorimeterHits"), outputLocations("OutputSimCalorimeterHits"))}) {
for (size_t i = 0; i < input.size(); ++i) {
auto value = k4FWCore::getParameter<std::string>(
podio::collMetadataParamName(input[i], edm4hep::labels::CellIDEncoding), this);
if (value.has_value()) {
k4FWCore::putParameter(podio::collMetadataParamName(output[i], edm4hep::labels::CellIDEncoding),
value.value(), this);
} else {
warning() << "No metadata found for " << input[i] << " when copying CellID metadata was requested" << endmsg;
}
}
}
}

return Gaudi::Algorithm::finalize();
}

DECLARE_COMPONENT(OverlayTiming)
7 changes: 7 additions & 0 deletions k4Reco/Overlay/components/OverlayTiming.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@
#include "Gaudi/Parsers/Factory.h"
#include "Gaudi/Property.h"

#include <map>
#include <random>
#include <string>
#include <vector>

struct EventHolder {
std::vector<std::vector<std::string>> m_fileNames;
Expand Down Expand Up @@ -94,6 +97,7 @@ struct OverlayTiming : public k4FWCore::MultiTransformer<retType(
template <typename T> void overlayCollection(std::string collName, const podio::CollectionBase& inColl);

virtual StatusCode initialize() final;
virtual StatusCode finalize() final;

retType virtual operator()(
const edm4hep::EventHeaderCollection& headers, const edm4hep::MCParticleCollection& mcParticles,
Expand Down Expand Up @@ -140,6 +144,9 @@ struct OverlayTiming : public k4FWCore::MultiTransformer<retType(
this, "TimeWindows", std::map<std::string, std::vector<float>>(), "Time windows for the different collections"};
Gaudi::Property<bool> m_allowReusingBackgroundFiles{
this, "AllowReusingBackgroundFiles", false, "If true the same background file can be used for the same event"};
Gaudi::Property<bool> m_copyCellIDMetadata{this, "CopyCellIDMetadata", false,
"If metadata is found in the signal file, copy it to the output file, "
Copy link
Contributor

@andresailer andresailer Oct 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which names are replaced?
And is there ever a case where we wouldn't want this to be done?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the ones that are InputCollection__CellIDEncoding (the convention in DD4hep) are changed to OutputCollection__CellIDEncoding. If for some reason the convention is different then I guess we wouldn't want this but there is no general way of finding the metadata associated to an input collection since from the algorithm there isn't a way of knowing all the names of the metadata parameters (which would allow for looking for a name that contains the current input collection). If the option is removed then there would be some warnings if the metadata paremeters are not found.

"replacing the old names with the new names"};

// Gaudi::Property<int> m_maxCachedFrames{
// this, "MaxCachedFrames", 0, "Maximum number of frames cached from background files"};
Expand Down
4 changes: 4 additions & 0 deletions k4Reco/Overlay/options/runOverlayTiming.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@
["background_group2_1.root"],
]
overlay.TimeWindows = {"MCParticle": [0, 23.5], "VertexBarrelCollection": [0, 23.5], "VertexEndcapCollection": [0, 23.5], "HCalRingCollection": [0, 23.5]}
overlay.CopyCellIDMetadata = True

# In case the original collections should be dropped
# iosvc.outputCommands = ["drop MCParticles", "drop VertexBarrelCollection"]

ApplicationMgr(TopAlg=[overlay],
EvtSel="NONE",
Expand Down
Loading