Skip to content

Commit

Permalink
add simple example code
Browse files Browse the repository at this point in the history
This builds an external application against Kassiopeia libraries.
  • Loading branch information
zykure committed Jul 22, 2020
1 parent 982418d commit 9c2ecdd
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Examples/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Makefile
build.ninja
/ks-particles-example
59 changes: 59 additions & 0 deletions Examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
cmake_minimum_required(VERSION 3.0)

# Name your project
project(Kassiopeia-Examples)

message("CMake prefix path: ${CMAKE_PREFIX_PATH}")

find_package(Boost REQUIRED COMPONENTS filesystem)
find_package(VTK)
if(VTK_FOUND)
include(${VTK_USE_FILE})
endif(VTK_FOUND)

# Don't forget to set CMAKE_PREFIX_PATH to your Kasper installation folder
# before executing cmake. That way, CMake will be able to find the KaLi installation.
find_package(Kassiopeia REQUIRED)
find_package(KEMField REQUIRED)
find_package(KGeoBag REQUIRED)

# Set the default install prefix (you don't need to remember this)
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
get_filename_component(BUILD_PARENT_DIR ${CMAKE_BINARY_DIR} PATH)
set(CMAKE_INSTALL_PREFIX "${BUILD_PARENT_DIR}/install" CACHE PATH "Install path prefix, prepended onto install directories." FORCE)
endif()

set( CMAKE_CXX_STANDARD 14 )
set( CMAKE_CXX_STANDARD_REQUIRED ON )
set( CMAKE_CXX_EXTENSIONS OFF )

# Define your include directories
include_directories (
${Kassiopeia_INCLUDE_DIRS}
${KEMField_INCLUDE_DIRS}
${KGeoBag_INCLUDE_DIRS}
)

SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)

#----------------------------------------
# Build libraries and ROOT dictionaries
#----------------------------------------

# not needed for this example

#----------------------------------------
# Build the executable
#----------------------------------------

set( EXECUTABLES
ks-particles-example
)

foreach(EXECUTABLE ${EXECUTABLES})
add_executable(${EXECUTABLE} ${EXECUTABLE}.cxx)
target_link_libraries (${EXECUTABLE} ${Kassiopeia_LIBRARIES})
endforeach()

# Define a cmake install target
install(TARGETS ${EXECUTABLES} DESTINATION ${CMAKE_INSTALL_PREFIX})
60 changes: 60 additions & 0 deletions Examples/ks-particles-example.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* @file ks-particles.cxx
*
* @date 22.07.2020
* @author Jan Behrens <[email protected]>
*/

// Kasper Common includes
#include <KException.h>
#include <KLogger.h>
#include <KMessage.h>

// Kassiopeia includes
#include <KSMainMessage.h>
#include <KSParticle.h>
#include <KSParticleFactory.h>

KLOGGER("kassiopeia.examples");

using namespace katrin;
using namespace Kassiopeia;

int main(int argc, char** argv)
{
// set message verbosity
KMessageTable::GetInstance().SetTerminalVerbosity(eDebug);
KMessageTable::GetInstance().SetLogVerbosity(eDebug);

// create an instance of the particle factory
KSParticleFactory& tFactory = KSParticleFactory::GetInstance();

// generate particles and print information
for (int i = 1; i < argc; i++) {
KSParticle *tParticle;

mainmsg(eNormal) << "Creating a particle with id: " << argv[i] << eom;

try {
try {
// try using a numeric particle id
long tPID = std::stol(argv[i]);
tParticle = tFactory.Create(tPID);
}
catch (std::invalid_argument) {
// if it fails, try to use a string id
std::string tStringID(argv[i]);
tParticle = tFactory.StringCreate(tStringID);
}
}
catch (KException) {
continue;
}

tParticle->Print();

delete tParticle;
}

return 0;
}

0 comments on commit 9c2ecdd

Please sign in to comment.