Skip to content

Commit

Permalink
Adding C++ analysis tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kjvbrt committed Sep 19, 2024
1 parent 93188c1 commit 6a18882
Show file tree
Hide file tree
Showing 11 changed files with 181 additions and 175 deletions.
6 changes: 2 additions & 4 deletions analyzers/dataframe/src/SmearObjects.cc
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,7 @@ ROOT::VecOps::RVec<edm4hep::TrackState> SmearedTracks::operator()(

smeared_track.covMatrix = covMatrix_edm4hep;

// if (m_debug) {
if (false) {
if (m_debug) {
std::cout << std::endl
<< "Original track " << track.D0 << " " << track.phi << " "
<< track.omega << " " << track.Z0 << " " << track.tanLambda
Expand Down Expand Up @@ -293,8 +292,7 @@ TVectorD CovSmear(TVectorD x, TMatrixDSym C, TRandom *ran, bool debug = false) {
TVectorD r(Nvec);
for (Int_t i = 0; i < Nvec; i++)
r(i) = ran->Gaus(0.0, 1.0); // Array of normal random numbers
// if (debug)
if (false)
if (debug)
std::cout << " random nb " << ran->Gaus(0.0, 1.0) << std::endl;
TVectorD xOut = x + DCv * (Ut * r); // Observed parameter vector
//
Expand Down
2 changes: 1 addition & 1 deletion cmake/FCCAnalysesFunctions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ endfunction()

function(add_generic_test _testname _testcmd)
add_test(NAME ${_testname}
COMMAND ${_testcmd}
COMMAND sh -c ${_testcmd}
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
set_property(TEST ${_testname}
APPEND PROPERTY ENVIRONMENT
Expand Down
34 changes: 16 additions & 18 deletions examples/data_source/standalone.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'''
Standalone test of EDM4hep RDataSource in Python.
Example of standalone FCCAnalyses script using podio::DataSource.
'''

import ROOT
Expand All @@ -13,39 +13,37 @@ def main():
input_list = ['https://fccsw.web.cern.ch/fccsw/testsamples/edm4hep1/'
'p8_ee_WW_ecm240_edm4hep.root']

ROOT.gSystem.Load("libFCCAnalyses")
ROOT.gSystem.Load('libFCCAnalyses')
if ROOT.dummyLoader:
print('----> Debug: Found FCCAnalyses library.')
print("----> Info: Loading analyzers from libFCCAnalyses... ",)
print('----> DEBUG: Found FCCAnalyses library.')
print('----> INFO: Loading analyzers from libFCCAnalyses... ',)

if ROOT.podio.DataSource:
print('----> Debug: Found Podio ROOT DataSource.')
print('----> Info: Loading events through podio::DataSource...')
print('----> DEBUG: Found Podio ROOT DataSource.')
print('----> INFO: Loading events through podio::DataSource...')

try:
dframe = ROOT.podio.CreateDataFrame(input_list)
except TypeError as excp:
print('----> Error: Unable to build dataframe!')
print('----> ERROR: Unable to build dataframe!')
print(excp)

dframe2 = dframe.Define(
"electron_truth",
"recoParticle::selPDG(11)(MCRecoAssociations)")
'electron_truth',
'recoParticle::selPDG(11)(MCRecoAssociations)')

dframe3 = dframe2.Define(
"electron_truth_pt",
"recoParticle::getPt(electron_truth)")
'electron_truth_pt',
'recoParticle::getPt(electron_truth)')

dframe4 = dframe3.Filter("electron_truth_pt.size() < 3")
dframe4 = dframe3.Filter('electron_truth_pt.size() < 3')

count = dframe4.Count()
dframe4.Snapshot("events", "output.root", {"electron_truth_pt"})
# dframe4.Snapshot("events", "output.root", {"electron_truth",
# "electron_truth_pt"})
dframe4.Snapshot('events', 'output.root', ['electron_truth_pt'])

print("---------------------")
print("Nuber of events: ", count.GetValue())
print('---------------------')
print('Number of events: ', count.GetValue())


if __name__ == "__main__":
if __name__ == '__main__':
main()
2 changes: 1 addition & 1 deletion man/man1/fccanalysis-run.1
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Specify max number of events to process\&.
Run over the test file\&.
.TP
.B \-\-use-data-source
Load events through EDM4hep RDataSource\&.
Load events through podio::DataSource\&.
.TP
.B \-\-bench
Output benchmark results to a JSON file\&.
Expand Down
43 changes: 5 additions & 38 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,21 @@ add_subdirectory(unittest)

add_subdirectory(benchmark)

add_subdirectory(integration)

add_integration_test("examples/FCCee/higgs/mH-recoil/mumu/analysis_stage1.py")
add_integration_test("examples/FCCee/flavour/Bc2TauNu/analysis_B2TauNu_truth.py")
add_integration_test("examples/FCCee/test/jet_constituents.py")
add_integration_test("examples/FCCee/vertex_lcfiplus/analysis_V0.py")

add_standalone_test("examples/FCCee/fullSim/caloNtupleizer/analysis.py")

if(WITH_SOURCE)
add_standalone_test("examples/data_source/standalone.py")
add_integration_test("examples/data_source/histmaker_source.py")
add_integration_test("examples/data_source/stages_source.py")
add_integration_test("examples/data_source/analysis_stage1.py")
add_standalone_test("examples/data_source/standalone.py")
endif()

add_standalone_test("examples/FCCee/fullSim/caloNtupleizer/analysis.py")

# C++ Analyses

# podio::DataSource
add_executable(test_data_source ${CMAKE_SOURCE_DIR}/tests/data_source/test_source.cxx)
target_include_directories(test_data_source PRIVATE ${CMAKE_SOURCE_DIR}/include)
target_link_libraries(test_data_source podio::podioDataSource
EDM4HEP::edm4hep
)

add_executable(test_source_associations
${CMAKE_SOURCE_DIR}/tests/data_source/test_source_associations.cxx)
target_include_directories(test_source_associations PRIVATE
${CMAKE_SOURCE_DIR}/include)
target_link_libraries(test_source_associations podio::podioDataSource
EDM4HEP::edm4hep
FCCAnalyses
)

# Low level access
add_executable(test_low_level ${CMAKE_SOURCE_DIR}/tests/data_source/test_low_level.cxx)
target_link_libraries(test_low_level ROOT::ROOTDataFrame
podio::podioDataSource
EDM4HEP::edm4hep
EDM4HEP::edm4hepDict
)

add_executable(test_low_level_associations
${CMAKE_SOURCE_DIR}/tests/data_source/test_low_level_associations.cxx)
target_link_libraries(test_low_level_associations ROOT::ROOTDataFrame
podio::podioDataSource
EDM4HEP::edm4hep
EDM4HEP::edm4hepDict
FCCAnalyses
)

# TODO: make this test run in the spack build environment
#add_generic_test(build_new_case_study "tests/build_new_case_study.sh")
59 changes: 59 additions & 0 deletions tests/integration/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Low level EDM4hep access
add_executable(low_level_edm4hep
${CMAKE_SOURCE_DIR}/tests/integration/low_level_edm4hep.cxx
)
target_link_libraries(low_level_edm4hep ROOT::ROOTDataFrame
podio::podioDataSource
EDM4HEP::edm4hep
EDM4HEP::edm4hepDict
)
add_generic_test(
low_level_edm4hep
"${CMAKE_CURRENT_BINARY_DIR}/low_level_edm4hep 1"
"https://fccsw.web.cern.ch/fccsw/testsamples/edm4hep1/p8_ee_WW_ecm240_edm4hep.root"
)

add_executable(
low_level_associations
${CMAKE_SOURCE_DIR}/tests/integration/low_level_associations.cxx)
target_link_libraries(low_level_associations ROOT::ROOTDataFrame
podio::podioDataSource
EDM4HEP::edm4hep
EDM4HEP::edm4hepDict
FCCAnalyses
)
add_generic_test(
low_level_associations
"${CMAKE_CURRENT_BINARY_DIR}/low_level_associations 1"
"https://fccsw.web.cern.ch/fccsw/testsamples/edm4hep1/p8_ee_WW_ecm240_edm4hep.root"
)

if(WITH_SOURCE)
add_executable(data_source
${CMAKE_SOURCE_DIR}/tests/integration/data_source.cxx)
target_include_directories(data_source PRIVATE ${CMAKE_SOURCE_DIR}/include)
target_link_libraries(data_source podio::podioDataSource
EDM4HEP::edm4hep
)
add_generic_test(
data_source
"${CMAKE_CURRENT_BINARY_DIR}/data_source 1"
"https://fccsw.web.cern.ch/fccsw/testsamples/edm4hep1/p8_ee_WW_ecm240_edm4hep.root"
)

add_executable(
data_source_associations
${CMAKE_SOURCE_DIR}/tests/integration/data_source_associations.cxx
)
target_include_directories(data_source_associations PRIVATE
${CMAKE_SOURCE_DIR}/include)
target_link_libraries(data_source_associations podio::podioDataSource
EDM4HEP::edm4hep
FCCAnalyses
)
add_generic_test(
data_source_associations
"${CMAKE_CURRENT_BINARY_DIR}/data_source_associations 1"
"https://fccsw.web.cern.ch/fccsw/testsamples/edm4hep1/p8_ee_WW_ecm240_edm4hep.root"
)
endif()
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@

// EDM4hep
#include <edm4hep/ReconstructedParticle.h>
#include <edm4hep/MCRecoParticleAssociationCollection.h>
#include <edm4hep/RecoMCParticleLinkCollection.h>
#include <edm4hep/MCParticle.h>
#include <edm4hep/MCParticleCollection.h>
#include <edm4hep/SimCalorimeterHitCollection.h>

edm4hep::MCParticleCollection selElectrons(edm4hep::MCParticleCollection& inParticles) {
edm4hep::MCParticleCollection selElectrons(const edm4hep::MCParticleCollection& inParticles) {
edm4hep::MCParticleCollection electrons;
electrons.setSubsetCollection();
for (auto particle: inParticles) {
Expand All @@ -28,17 +28,17 @@ edm4hep::MCParticleCollection selElectrons(edm4hep::MCParticleCollection& inPart
}

struct selPDG {
selPDG(int pdg = 11, bool chargeConjugateAllowed = true);
selPDG(int pdg, bool chargeConjugateAllowed);
const int m_pdg;
const bool m_chargeConjugateAllowed;
edm4hep::MCParticleCollection operator() (edm4hep::MCParticleCollection& inParticles);
edm4hep::MCParticleCollection operator() (const edm4hep::MCParticleCollection& inParticles);
};

selPDG::selPDG(int pdg,
bool chargeConjugateAllowed) : m_pdg(pdg),
m_chargeConjugateAllowed(chargeConjugateAllowed) {};

edm4hep::MCParticleCollection selPDG::operator() (edm4hep::MCParticleCollection& inParticles) {
edm4hep::MCParticleCollection selPDG::operator() (const edm4hep::MCParticleCollection& inParticles) {
edm4hep::MCParticleCollection result;
result.setSubsetCollection();
for (auto particle: inParticles) {
Expand All @@ -57,7 +57,7 @@ edm4hep::MCParticleCollection selPDG::operator() (edm4hep::MCParticleCollection&
return result;
}

ROOT::VecOps::RVec<float> getPx(edm4hep::MCParticleCollection& inParticles) {
ROOT::VecOps::RVec<float> getPx(const edm4hep::MCParticleCollection& inParticles) {
ROOT::VecOps::RVec<float> result;
for (auto particle: inParticles) {
result.push_back(particle.getMomentum().x);
Expand Down Expand Up @@ -87,48 +87,44 @@ edm4hep::MCParticleCollection get_stable_particles_from_decay(edm4hep::MCParticl

edm4hep::MCParticle get_mcParticle(
const edm4hep::ReconstructedParticle& recoParticle,
const edm4hep::MCRecoParticleAssociationCollection& assocColl) {
const edm4hep::RecoMCParticleLinkCollection& assocColl) {
edm4hep::MCParticle no_result;

for (const auto& assoc: assocColl) {
if (assoc.getRec() == recoParticle) {
return assoc.getSim();
if (assoc.getFrom() == recoParticle) {
return assoc.getTo();
}
}

return no_result;
}


int main(int argc, char *argv[]) {
// auto verbosity = ROOT::Experimental::RLogScopedVerbosity(ROOT::Detail::RDF::RDFLogChannel(), ROOT::Experimental::ELogLevel::kInfo);
int main(int argc, const char *argv[]) {
auto verbosity = ROOT::Experimental::RLogScopedVerbosity(
ROOT::Detail::RDF::RDFLogChannel(),
ROOT::Experimental::ELogLevel::kInfo
);

int nCPU = 4;
int nThreads = 1;
if (argc > 1) {
nCPU = atoi(argv[1]);
nThreads = atoi(argv[1]);
}

std::vector<std::string> filePathList;
std::string filePathBase = "/home/jsmiesko/source/FCCAnalyses/inputs/";
filePathList.emplace_back(filePathBase + "p8_ee_WW_ecm240/p8_ee_WW_ecm240_10.edm4hep.root");
filePathList.emplace_back(filePathBase + "p8_ee_WW_ecm240/p8_ee_WW_ecm240_11.edm4hep.root");
filePathList.emplace_back(filePathBase + "p8_ee_WW_ecm240/p8_ee_WW_ecm240_12.edm4hep.root");
filePathList.emplace_back(filePathBase + "p8_ee_WW_ecm240/p8_ee_WW_ecm240_1.edm4hep.root");
filePathList.emplace_back(filePathBase + "p8_ee_WW_ecm240/p8_ee_WW_ecm240_2.edm4hep.root");
filePathList.emplace_back(filePathBase + "p8_ee_WW_ecm240/p8_ee_WW_ecm240_3.edm4hep.root");
filePathList.emplace_back(filePathBase + "p8_ee_WW_ecm240/p8_ee_WW_ecm240_4.edm4hep.root");
filePathList.emplace_back(filePathBase + "p8_ee_WW_ecm240/p8_ee_WW_ecm240_5.edm4hep.root");
filePathList.emplace_back(filePathBase + "p8_ee_WW_ecm240/p8_ee_WW_ecm240_6.edm4hep.root");
filePathList.emplace_back(filePathBase + "p8_ee_WW_ecm240/p8_ee_WW_ecm240_7.edm4hep.root");
filePathList.emplace_back(filePathBase + "p8_ee_WW_ecm240/p8_ee_WW_ecm240_8.edm4hep.root");
filePathList.emplace_back(filePathBase + "p8_ee_WW_ecm240/p8_ee_WW_ecm240_9.edm4hep.root");

ROOT::EnableImplicitMT(nCPU);

ROOT::RDataFrame rdf(std::make_unique<podio::DataSource>(filePathList));
if (nThreads > 1) {
ROOT::EnableImplicitMT(nThreads);
}

std::string filePath = "https://fccsw.web.cern.ch/fccsw/testsamples/"
"edm4hep1/p8_ee_WW_ecm240_edm4hep.root";
if (argc > 2) {
filePath = argv[2];
}

ROOT::RDataFrame rdf(std::make_unique<podio::DataSource>(filePath));

// rdf.Describe().Print();
std::cout << std::endl;
// std::cout << std::endl;

std::cout << "Info: Num. of slots: " << rdf.GetNSlots() << std::endl;

Expand All @@ -144,9 +140,9 @@ int main(int argc, char *argv[]) {

auto canvas = std::make_unique<TCanvas>("canvas", "Canvas", 450, 450);
h_particles_px->Draw();
canvas->Print("source_particles_px.pdf");
canvas->Print("/tmp/source_particles_px.pdf");
h_electrons_px->Draw();
canvas->Print("source_electrons_px.pdf");
canvas->Print("/tmp/source_electrons_px.pdf");

return EXIT_SUCCESS;
}
Loading

0 comments on commit 6a18882

Please sign in to comment.