Skip to content

Commit

Permalink
draft
Browse files Browse the repository at this point in the history
  • Loading branch information
andiwand committed May 18, 2022
1 parent 667179e commit 1915fad
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Examples/Io/EDM4hep/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ target_include_directories(
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>)
target_link_libraries(
ActsExamplesIoEDM4hep
PUBLIC ActsCore ActsExamplesFramework ActsPluginEDM4hep)
PUBLIC ActsCore ActsExamplesFramework ActsExamplesDetectorDD4hep ActsPluginEDM4hep)

install(
TARGETS ActsExamplesIoEDM4hep
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

#include "Acts/Geometry/GeometryIdentifier.hpp"
#include "Acts/Utilities/Logger.hpp"

#include "ActsExamples/Framework/IReader.hpp"
#include "ActsExamples/DD4hepDetector/DD4hepGeometryService.hpp"

#include <memory>
#include <string>
Expand All @@ -27,6 +29,8 @@ class EDM4hepSimHitReader final : public IReader {
std::string inputPath;
/// Output simulated (truth) hits collection.
std::string outputSimHits;

std::shared_ptr<DD4hep::DD4hepGeometryService> dd4hepGeometryService;
};

/// Construct the simhit reader.
Expand Down
63 changes: 61 additions & 2 deletions Examples/Io/EDM4hep/src/EDM4hepSimHitReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@

#include "ActsExamples/Io/EDM4hep/EDM4hepSimHitReader.hpp"

#include "Acts/Plugins/EDM4hep/EDM4hepConverter.hpp"
#include "ActsExamples/EventData/SimHit.hpp"
#include "ActsExamples/Framework/WhiteBoard.hpp"

#include "ActsFatras/EventData/Hit.hpp"

#include "Acts/Definitions/Units.hpp"

#include "edm4hep/SimTrackerHit.h"
#include "edm4hep/SimTrackerHitCollection.h"

ActsExamples::EDM4hepSimHitReader::EDM4hepSimHitReader(
Expand Down Expand Up @@ -41,6 +44,62 @@ std::pair<size_t, size_t> ActsExamples::EDM4hepSimHitReader::availableEvents()
return m_eventsRange;
}

ActsFatras::Hit convertEDM4hepSimHit(
const edm4hep::SimTrackerHit& sth,
ActsExamples::DD4hep::DD4hepGeometryService &geometryService) {
std::ofstream debug("/home/andreas/debug.txt", std::ios::app);

try {
auto test = geometryService.lcdd()->volumeManager().lookupDetElement(1250572);
debug << "EDM4hep found test " << test.volumeID() << std::endl;
} catch (...) {
debug << "EDM4hep test failed" << std::endl;
}

debug << "EDM4hep convert cell id " << sth.getCellID() << std::endl;

try {
auto detElement = geometryService.lcdd()->volumeManager().lookupDetElement(sth.getCellID());
debug << "EDM4hep found detElement " << detElement.volumeID() << std::endl;
} catch (...) {
debug << "EDM4hep detElement not found" << std::endl;
}

const auto geometryId = sth.getCellID(); // TODO
ActsFatras::Barcode particleId;
particleId.setParticle(sth.getMCParticle().id()); // TODO

const auto mass = sth.getMCParticle().getMass();
const Acts::ActsVector<3> momentum{
sth.getMomentum().x * Acts::UnitConstants::GeV,
sth.getMomentum().y * Acts::UnitConstants::GeV,
sth.getMomentum().z * Acts::UnitConstants::GeV,
};
const auto energy = std::sqrt(momentum.squaredNorm() + mass * mass);

ActsFatras::Hit::Vector4 pos4{
sth.getPosition().x * Acts::UnitConstants::mm,
sth.getPosition().y * Acts::UnitConstants::mm,
sth.getPosition().z * Acts::UnitConstants::mm,
sth.getTime() * Acts::UnitConstants::ns,
};
ActsFatras::Hit::Vector4 mom4{
momentum.x(),
momentum.y(),
momentum.z(),
energy,
};
ActsFatras::Hit::Vector4 delta4{
0 * Acts::UnitConstants::GeV, 0 * Acts::UnitConstants::GeV,
0 * Acts::UnitConstants::GeV,
0 * Acts::UnitConstants::GeV, // sth.getEDep()
};
int32_t index = -1;

return ActsFatras::Hit(geometryId, particleId, pos4, mom4, mom4 + delta4,
index);
}

ActsExamples::ProcessCode ActsExamples::EDM4hepSimHitReader::read(
const ActsExamples::AlgorithmContext& ctx) {
SimHitContainer::sequence_type unordered;
Expand All @@ -58,7 +117,7 @@ ActsExamples::ProcessCode ActsExamples::EDM4hepSimHitReader::read(
if (collection.getTypeName() == "edm4hep::SimTrackerHitCollection") {
for (const auto& sth :
(const edm4hep::SimTrackerHitCollection&)collection) {
auto hit = Acts::convertEDM4hepSimHit(sth);
auto hit = convertEDM4hepSimHit(sth, *m_cfg.dd4hepGeometryService);
unordered.push_back(std::move(hit));
}
}
Expand Down
1 change: 1 addition & 0 deletions Examples/Python/src/Input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ void addInput(Context& ctx) {
ACTS_PYTHON_STRUCT_BEGIN(c, Config);
ACTS_PYTHON_MEMBER(inputPath);
ACTS_PYTHON_MEMBER(outputSimHits);
ACTS_PYTHON_MEMBER(dd4hepGeometryService);
ACTS_PYTHON_STRUCT_END();
}

Expand Down
14 changes: 14 additions & 0 deletions Examples/Python/tests/test_reader.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from typing import Type
import inspect

from helpers import dd4hepEnabled
from common import getOpenDataDetectorDirectory

import pytest

from helpers import geant4Enabled, AssertCollectionExistsAlg
Expand Down Expand Up @@ -306,7 +309,17 @@ def test_csv_clusters_reader(tmp_path, fatras, conf_const, trk_geo, rng):
assert alg.events_seen == 10


@pytest.mark.skipif(not dd4hepEnabled, reason="DD4hep is not set up")
def test_edm4hep_simhits_reader(conf_const):
import acts.examples.dd4hep

odd_dir = getOpenDataDetectorDirectory()

dd4hepConfig = acts.examples.dd4hep.DD4hepGeometryService.Config(
xmlFileNames=[str(odd_dir / "xml/OpenDataDetector.xml")]
)
geometryService = acts.examples.dd4hep.DD4hepGeometryService(dd4hepConfig)

s = Sequencer(numThreads=1)

s.addReader(
Expand All @@ -317,6 +330,7 @@ def test_edm4hep_simhits_reader(conf_const):
"/home/andreas/cern/source/OpenDataDetector/output_edm4hep.root"
),
outputSimHits="simhits",
dd4hepGeometryService=geometryService,
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "ActsFatras/EventData/Hit.hpp"

#include "edm4hep/SimTrackerHit.h"

namespace Acts {

ActsFatras::Hit convertEDM4hepSimHit(const edm4hep::SimTrackerHit& sth);
Expand Down
2 changes: 1 addition & 1 deletion Plugins/EDM4hep/src/EDM4hepConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ ActsFatras::Hit convertEDM4hepSimHit(const edm4hep::SimTrackerHit& sth) {

const auto geometryId = sth.getCellID();
ActsFatras::Barcode particleId;
particleId.setParticle(sth.getMCParticle().getPDG()); // TODO or id()?
particleId.setParticle(sth.getMCParticle().id());

const auto mass = sth.getMCParticle().getMass();
const ActsVector<3> momentum{
Expand Down

0 comments on commit 1915fad

Please sign in to comment.