Skip to content

Commit

Permalink
feat: setup testing framework
Browse files Browse the repository at this point in the history
  • Loading branch information
wdconinc authored Aug 14, 2023
1 parent bf945e2 commit 63bb29c
Show file tree
Hide file tree
Showing 8 changed files with 271 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ add_library(edm4eic::edm4eicDict ALIAS edm4eicDict )
list(APPEND EDM4EIC_INSTALL_LIBS edm4eic edm4eicDict)

add_subdirectory(utils)
add_subdirectory(test)

install(TARGETS ${EDM4EIC_INSTALL_LIBS}
EXPORT ${PROJECT_NAME}Targets
Expand Down
73 changes: 73 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# SPDX-License-Identifier: Apache-2.0

IF(NOT BUILD_TESTING)
RETURN()
ENDIF()

function(set_test_env _testname)
set_property(TEST ${_testname} APPEND PROPERTY ENVIRONMENT
LD_LIBRARY_PATH=$<TARGET_FILE_DIR:edm4hep>:$<TARGET_FILE_DIR:podio::podio>:$<$<TARGET_EXISTS:edm4hepRDF>:$<TARGET_FILE_DIR:edm4hepRDF>:>$<TARGET_FILE_DIR:ROOT::Core>:$ENV{LD_LIBRARY_PATH}
ROOT_INCLUDE_PATH=${PROJECT_SOURCE_DIR}/edm4hep:${PROJECT_SOURCE_DIR}/utils/include:$ENV{ROOT_INCLUDE_PATH}
)
set_tests_properties(${_testname} PROPERTIES
FAIL_REGULAR_EXPRESSION "[^a-z]Error;ERROR;error;Failed"
)
endfunction()

add_executable(write_events write_events.cc)
target_include_directories(write_events PUBLIC ${PROJECT_SOURCE_DIR}/edm4hep )
target_link_libraries(write_events edm4hep podio::podioRootIO)
add_test(NAME write_events COMMAND write_events)
set_test_env(write_events)

add_executable(read_events read_events.cc)
target_include_directories(read_events PUBLIC ${PROJECT_SOURCE_DIR}/edm4hep )
target_link_libraries(read_events edm4hep podio::podioRootIO)
add_test(NAME read_events COMMAND read_events)
set_property(TEST read_events PROPERTY
DEPENDS write_events
)
set_test_env(read_events)

IF(TARGET ROOT::ROOTDataFrame)
add_executable(test_rdf test_rdf.cc)
target_include_directories(test_rdf PUBLIC ${PROJECT_SOURCE_DIR}/edm4hep ${PROJECT_SOURCE_DIR}/dataframe )
target_link_libraries(test_rdf edm4hepRDF ROOT::ROOTDataFrame podio::podioRootIO)
add_test(NAME test_rdf COMMAND test_rdf)
set_test_env(test_rdf)
set_property(TEST test_rdf PROPERTY
DEPENDS write_events
)

add_test(NAME py_test_rdf COMMAND python ${CMAKE_CURRENT_LIST_DIR}/test_rdf.py)
set_test_env(py_test_rdf)
get_property(ENVIRONMENT TEST py_test_rdf PROPERTY ENVIRONMENT)
set_property(TEST py_test_rdf PROPERTY ENVIRONMENT
${ENVIRONMENT}
PYTHONPATH=${PROJECT_SOURCE_DIR}/python:$ENV{PYTHONPATH}
)
set_tests_properties(py_test_rdf PROPERTIES DEPENDS write_events)
endif()


find_package(HepMC3)
find_package(HepPDT)

if(HepMC3_FOUND AND HepPDT_FOUND )
add_executable(edm4hep_testhepmc hepmc/edm4hep_testhepmc.cc)
target_include_directories(edm4hep_testhepmc PUBLIC ${HEPMC3_INCLUDE_DIR} ${HEPPDT_INCLUDE_DIR} )
target_link_libraries(edm4hep_testhepmc edm4hep podio::podioRootIO ${HEPPDT_LIBRARIES} ${HEPMC3_LIBRARIES})
add_test(NAME edm4hep_testhepmc COMMAND edm4hep_testhepmc)
set_test_env(edm4hep_testhepmc)
set_property(TEST edm4hep_testhepmc APPEND PROPERTY ENVIRONMENT
ROOT_INCLUDE_PATH=${HEPMC3_INCLUDE_DIR}:$ENV{ROOT_INCLUDE_PATH}
)
endif()

if (nlohmann_json_FOUND)
add_test(NAME convert_events COMMAND edm4hep2json edm4hep_events.root)
set_property(TEST convert_events PROPERTY DEPENDS write_events)
set_test_env(convert_events)
endif()

add_subdirectory(utils)
10 changes: 10 additions & 0 deletions test/read_events.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// SPDX-License-Identifier: Apache-2.0

#include "read_events.h"
#include "podio/ROOTFrameReader.h"

int main() {
read_events<podio::ROOTFrameReader>("edm4eic_events.root");

return 0;
}
59 changes: 59 additions & 0 deletions test/read_events.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// SPDX-License-Identifier: Apache-2.0

#ifndef EDM4EIC_TEST_READ_EVENTS_H__
#define EDM4EIC_TEST_READ_EVENTS_H__

// test data model
#include "edm4eic/RawTrackerHitCollection.h"

// podio specific includes
#include "podio/Frame.h"

// STL
#include <cassert>
#include <exception>
#include <iostream>
#include <vector>

void processEvent(const podio::Frame& event, bool verboser, unsigned eventNum) {
auto& raw_hits = event.get<edm4eic::RawTrackerHitCollection>("RawTrackerHits");

if (raw_hits.isValid()) {

//-------- print particles for debugging:

std::cout << "\n collection: "
<< "RawTrackerHits"
<< " of type " << raw_hits.getValueTypeName() << "\n\n"
<< raw_hits << std::endl;
//-------------------------------

// check a few things (to be completed ...)
auto raw_hit = raw_hits[0];
if (raw_hit.getCellID() != 0x0123456789abcdefLL)
throw std::runtime_error("wrong CellID for first hit - should be 0x0123456789abcdefLL ");

} else {
throw std::runtime_error("Collection 'RawTrackerHits' should be present");
}

//===============================================================================

const auto& evtType = event.getParameter<std::string>("EventType");
std::cout << "Event Type: " << evtType << std::endl;
}

template <typename ReaderT>
void read_events(const std::string& filename) {
ReaderT reader;
reader.openFile(filename);

unsigned nEvents = reader.getEntries("events");
for (unsigned i = 0; i < nEvents; ++i) {
std::cout << "reading event " << i << std::endl;
const auto event = podio::Frame(reader.readNextEntry("events"));
processEvent(event, true, i);
}
}

#endif
35 changes: 35 additions & 0 deletions test/test_rdf.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// SPDX-License-Identifier: Apache-2.0

#include "edm4eic/utils/dataframe.h"

#include "edm4eic/RawTrackerHitData.h"

#include <ROOT/RDataFrame.hxx>

#include <iostream>

int main(int argc, char* argv[]) {

ROOT::EnableImplicitMT();

std::cout << "Create RDataFrame ..." << std::endl;

ROOT::RDataFrame df("events", "edm4eic_events.root");

std::cout << "Apply selectors and define new branches ..." << std::endl;
auto df2 = df.Define("MCParticles_pt", edm4eic::utils::pt<edm4eic::MCParticleData>, {"MCParticles"})
.Define("MCParticles_eta", edm4eic::utils::eta<edm4eic::MCParticleData>, {"MCParticles"})
.Define("MCParticles_cosTheta", edm4eic::utils::cos_theta<edm4eic::MCParticleData>, {"MCParticles"})
.Define("SimTrackerHits_r", edm4eic::utils::r<edm4eic::SimTrackerHitData>, {"SimTrackerHits"})
.Define("SimTrackerHit_pt", edm4eic::utils::pt<edm4eic::SimTrackerHitData>, {"SimTrackerHits"})
.Define("TrackerHits_r", edm4eic::utils::r<edm4eic::TrackerHitPlaneData>, {"TrackerHitPlanes"});

std::string outfilename = "edm4eic_events_rdf.root";
std::cout << "Writing snapshot to disk ... \t" << outfilename << std::endl;

df2.Snapshot("events", outfilename,
{"MCParticles_pt", "MCParticles_eta", "MCParticles_cosTheta", "SimTrackerHits_r", "SimTrackerHit_pt",
"TrackerHits_r"});

return 0;
}
31 changes: 31 additions & 0 deletions test/test_rdf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env python3

# SPDX-License-Identifier: Apache-2.0

import ROOT
import edm4eic

ROOT.EnableImplicitMT()

print('Create RDataFrame ...')
df = ROOT.RDataFrame('events', 'edm4eic_events.root')

print('Apply selectors and define new branches ...')
df2 = (df.Define('MCParticles_pt', 'edm4eic::utils::pt(MCParticles)')
.Define('MCParticles_eta', 'edm4eic::utils::eta(MCParticles)')
.Define('MCParticles_cosTheta', 'edm4eic::utils::cos_theta(MCParticles)')
.Define('SimTrackerHits_r', 'edm4eic::utils::r(SimTrackerHits)')
.Define('SimTrackerHits_pt', 'edm4eic::utils::pt(SimTrackerHits)')
.Define('TrackerHits_r', 'edm4eic::utils::r(TrackerHitPlanes)')
)

filename = 'edm4eic_events_py_rdf.root'
print(f'Writing snapshot to disk ... \t{filename}')

df2.Snapshot('events', filename,
['MCParticles_pt',
'MCParticles_eta',
'MCParticles_cosTheta',
'SimTrackerHits_r',
'SimTrackerHits_pt',
'TrackerHits_r'])
9 changes: 9 additions & 0 deletions test/write_events.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// SPDX-License-Identifier: Apache-2.0

#include "write_events.h"
#include "podio/ROOTFrameWriter.h"

int main(int argc, char* argv[]) {

write<podio::ROOTFrameWriter>("edm4eic_events.root");
}
53 changes: 53 additions & 0 deletions test/write_events.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// SPDX-License-Identifier: Apache-2.0

#ifndef EDM4EIC_TEST_WRITE_EVENTS_H
#define EDM4EIC_TEST_WRITE_EVENTS_H

// Data model
#include "edm4eic/RawTrackerHitCollection.h"

// STL
#include <iostream>
#include <vector>

// podio specific includes
#include "podio/Frame.h"

template <class WriterT>
void write(std::string outfilename) {
std::cout << "start processing" << std::endl;

WriterT writer(outfilename);

unsigned nevents = 10;

// =============== event loop ================================
for (unsigned i = 0; i < nevents; ++i) {
std::cout << " --- processing event " << i << std::endl;
auto event = podio::Frame();

auto raw_hits = edm4eic::RawTrackerHitCollection();
auto raw_hit = raw_hits.create();
raw_hit.setCellID(0x0123456789abcdefLL);
raw_hit.setCharge(0x01234567);
raw_hit.setTimeStamp(0x89abcdef);

//-------- print hits for debugging:
std::cout << "\n collection: "
<< "RawTrackerHits"
<< " of type " << raw_hits.getValueTypeName() << "\n\n"
<< raw_hits << std::endl;

event.put(std::move(raw_hits), "RawTrackerHits");

//===============================================================================

event.putParameter("EventType", "test");

writer.writeFrame(event, "events");
}

writer.finish();
}

#endif

0 comments on commit 63bb29c

Please sign in to comment.