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 example for creating DD4hepPlugin #3

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
5 changes: 1 addition & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ jobs:
- uses: cvmfs-contrib/github-action-cvmfs@v2
- name: Start container
run: |
docker run -it --name CI_container -v ${GITHUB_WORKSPACE}:/Package -v /cvmfs:/cvmfs:shared -d clicdp/cc7-lcg /bin/bash
- name: Setup container
run: |
docker exec CI_container /bin/bash -c " ln -s /usr/lib64/liblzma.so.5.2.2 /usr/lib64/liblzma.so;"
docker run -it --name CI_container -v ${GITHUB_WORKSPACE}:/Package -v /cvmfs:/cvmfs:shared -d ghcr.io/aidasoft/centos7:latest /bin/bash
- name: CMake Configure
run: |
docker exec CI_container /bin/bash -c 'cd Package;\
Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ find_package(ROOT COMPONENTS RIO Tree)
find_package(EDM4HEP)
find_package(k4FWCore)
find_package(Acts)
find_package(DD4hep)

#---------------------------------------------------------------
# Load macros and functions for Gaudi-based projects
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ This repository contains the necessary tools to use ACTS functionality in Key4he

* k4FWCore

## Installation
## Compilation and testing


```
source /cvmfs/sw-nightlies.hsf.org/key4hep/setup.sh
mkdir build install
cd build;
cmake .. -DCMAKE_INSTALL_PREFIX=../install
make install


cmake .. -DCMAKE_INSTALL_PREFIX=../install -G Ninja
ninja
ctest
```

23 changes: 19 additions & 4 deletions k4ActsTracking/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
file(GLOB _plugin_sources src/components/*.cpp)
gaudi_add_module(k4ActsTrackingPlugins
SOURCES ${_plugin_sources}
LINK Gaudi::GaudiKernel Gaudi::GaudiAlgLib k4FWCore::k4FWCore EDM4HEP::edm4hep)
LINK Gaudi::GaudiKernel Gaudi::GaudiAlgLib DD4hep::DDCore)



file(GLOB _dd4hep_plugin_sources src/dd4hepplugin/*.cpp)
add_dd4hep_plugin(k4ActsDD4hepPlugin SHARED ${_dd4hep_plugin_sources})
target_link_libraries(k4ActsDD4hepPlugin DD4hep::DDCore DD4hep::DDRec DD4hep::DDG4 DD4hep::DDParsers)


SET(compile_flags -Wformat-security -Wno-long-long -Wdeprecated -fdiagnostics-color=auto -Wall -Wextra -pedantic)

target_compile_options(k4ActsTrackingPlugins PUBLIC ${compile_flags})
target_compile_options(k4ActsDD4hepPlugin PUBLIC ${compile_flags})

set(GAUDI_GENCONF_DIR "genConfDir")

function(set_test_env _testname)
set_property(TEST ${_testname} APPEND PROPERTY ENVIRONMENT
LD_LIBRARY_PATH=${CMAKE_BINARY_DIR}:$<TARGET_FILE_DIR:k4ActsTrackingPlugins>:$<TARGET_FILE_DIR:ROOT::Core>:$<TARGET_FILE_DIR:k4FWCore::k4FWCore>:$<TARGET_FILE_DIR:EDM4HEP::edm4hep>:$<TARGET_FILE_DIR:podio::podio>:$ENV{LD_LIBRARY_PATH}
PYTHONPATH=${CMAKE_BINARY_DIR}/${CMAKE_PROJECT_NAME}/${GAUDI_GENCONF_DIR}:$<TARGET_FILE_DIR:k4FWCore::k4FWCore>/../python:$ENV{PYTHONPATH}
LD_LIBRARY_PATH=${CMAKE_BINARY_DIR}:$<TARGET_FILE_DIR:k4ActsTrackingPlugins>:$<TARGET_FILE_DIR:k4ActsDD4hepPlugin>:$<TARGET_FILE_DIR:ROOT::Core>:$<TARGET_FILE_DIR:k4FWCore::k4FWCore>:$<TARGET_FILE_DIR:EDM4HEP::edm4hep>:$<TARGET_FILE_DIR:podio::podio>:$ENV{LD_LIBRARY_PATH}
PYTHONPATH=${CMAKE_BINARY_DIR}/${CMAKE_PROJECT_NAME}/genConfDir:$<TARGET_FILE_DIR:k4FWCore::k4FWCore>/../python:$ENV{PYTHONPATH}
PATH=$<TARGET_FILE_DIR:k4FWCore::k4FWCore>/../bin:$ENV{PATH}
K4ACTSTRACKING=${CMAKE_CURRENT_LIST_DIR}/
)
endfunction()

add_test(NAME ActsAlgTest
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
COMMAND k4run k4ActsTracking/options/run_acts_plugin.py)
set_test_env(ActsAlgTest)
21 changes: 21 additions & 0 deletions k4ActsTracking/options/run_acts_plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import os
from pprint import pprint
from Gaudi.Configuration import *

from Configurables import ActsAlg, GeoSvc
algList = []

geosvc = GeoSvc("GeoSvc")
# using this instead of CLIC_o3_v14 because it is much faster to instantiate
geosvc.detectors = [os.environ["LCGEO"]+"/CLIC/compact/CLIC_o2_v04/CLIC_o2_v04.xml"]

a = ActsAlg("MyActsAlg")
algList.append(a)

from Configurables import ApplicationMgr
ApplicationMgr( TopAlg = algList,
EvtSel = 'NONE',
EvtMax = 2,
ExtSvc = [geosvc],
OutputLevel=INFO
)
30 changes: 30 additions & 0 deletions k4ActsTracking/src/components/ActsAlg.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "ActsAlg.h"

#include <DD4hep/DetFactoryHelper.h>
#include <DD4hep/Factories.h>

DECLARE_COMPONENT(ActsAlg)

ActsAlg::ActsAlg(const std::string& aName, ISvcLocator* aSvcLoc) : GaudiAlgorithm(aName, aSvcLoc) {}

ActsAlg::~ActsAlg() {}

StatusCode ActsAlg::initialize() {
info() << "Initializing the ACTS algorithm" << endmsg;
int argc = 0;
char* argv = {};
dd4hep::PluginDebug dbg;
std::string name = "k4acts_addActsExtensions";
long result = dd4hep::PluginService::Create<long>(name, &dd4hep::Detector::getInstance(), argc, &argv);
if (0 == result) {
error() << "Failed to locate plugin " << name << "\n" << dbg.missingFactory(name) << endmsg;
return StatusCode::FAILURE;
}

info() << "Successfully loaded the plugin!" << endmsg;
return StatusCode::SUCCESS;
}

StatusCode ActsAlg::execute() { return StatusCode::SUCCESS; }

StatusCode ActsAlg::finalize() { return StatusCode::SUCCESS; }
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
#include "Gaudi/Property.h"
#include "GaudiAlg/GaudiAlgorithm.h"

class EmptyAlg : public GaudiAlgorithm {
class ActsAlg : public GaudiAlgorithm {
public:
explicit EmptyAlg(const std::string&, ISvcLocator*);
virtual ~EmptyAlg();
explicit ActsAlg(const std::string&, ISvcLocator*);
virtual ~ActsAlg();
/** Initialize.
* @return status code
*/
Expand Down
13 changes: 0 additions & 13 deletions k4ActsTracking/src/components/EmptyAlg.cpp

This file was deleted.

83 changes: 83 additions & 0 deletions k4ActsTracking/src/dd4hepplugin/actsdd4hepplugin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include <DD4hep/DetElement.h>
#include <DD4hep/Detector.h>
#include <DD4hep/Factories.h>
#include <DD4hep/Printout.h>

#include <DD4hep/IDDescriptor.h>

#include <DDRec/DetectorData.h>
#include <DDRec/SurfaceHelper.h>
#include <DDRec/SurfaceManager.h>

#include <string>

using dd4hep::DetElement;
using dd4hep::PrintLevel;

namespace {

/** Plugin for adding the ACTS Extensions to surface, or DetElements, or something
*
*/

static long addActsExtensions(dd4hep::Detector& description, int, char**) {
const std::string LOG_SOURCE("AddActsExtensions");
printout(PrintLevel::INFO, LOG_SOURCE, "Running plugin");

//Getting the surface manager
dd4hep::rec::SurfaceManager& surfMan = *description.extension<dd4hep::rec::SurfaceManager>();

dd4hep::rec::SurfaceHelper ds(description.world());
dd4hep::rec::SurfaceList const& detSL = ds.surfaceList();
int counter = 0;
for (dd4hep::rec::ISurface* surf : detSL) {
dd4hep::Volume volume = ((dd4hep::rec::Surface*)surf)->volume();
auto* ddsurf = ((dd4hep::rec::Surface*)surf);
auto const volumeName = std::string(volume->GetName());
if (not ddsurf->detElement().isValid())
continue;

dd4hep::BitFieldCoder* cellIDcoder = nullptr;
if (ddsurf->type().isSensitive() and not ddsurf->type().isHelper()) {
// surface is sensitive, assuming it has a readout
auto detectorName = ddsurf->detElement().type();
try {
auto theDetector = dd4hep::SensitiveDetector(description.sensitiveDetector(detectorName));
cellIDcoder = theDetector.readout().idSpec().decoder();
printout(PrintLevel::DEBUG, LOG_SOURCE, "The encoding string for detector %s is %s", detectorName.c_str(),
cellIDcoder->fieldDescription().c_str());
} catch (...) {
printout(PrintLevel::INFO, LOG_SOURCE, "Not readout for detector %s", detectorName.c_str());
continue;
}
} else {
continue;
}
counter++;

std::string path = ddsurf->detElement().path();
auto layerNumber = cellIDcoder->get(ddsurf->id(), "layer");
printout(PrintLevel::INFO, LOG_SOURCE, "Found Surface at %s in layer %d at radius %3.2f mm", path.c_str(),
layerNumber, ddsurf->origin().rho());

//fixme: replace this with the ACTS type to be used as an extension
dd4hep::rec::DoubleParameters* para = nullptr;
try { // use existing map, or create a new one
para = ddsurf->detElement().extension<dd4hep::rec::DoubleParameters>();
andresailer marked this conversation as resolved.
Show resolved Hide resolved
para->doubleParameters["SortingPolicy"] = 123;
} catch (...) {
para = new dd4hep::rec::DoubleParameters;
para->doubleParameters["SortingPolicy"] = 321;
ddsurf->detElement().addExtension<dd4hep::rec::DoubleParameters>(para);
}
if (counter > 1000) {
break;
}
} // for all surfaces

return 42;
}

} // namespace

DECLARE_APPLY(k4acts_addActsExtensions, ::addActsExtensions)