KaliVeda
Toolkit for HIC analysis
Development with KaliVeda

Writing code with KaliVeda

Automatic class source code generator

KaliVeda provides a class which can write source code for other classes: KVClassFactory. At its simplest, only a class name and short description are required in order to generate a fully ROOT-compatible class from the KaliVeda command line. Giving a base class to derive your new class from will copy and implement all constructors from the base class:

kaliveda [0] KVClassFactory::MakeClass("MyClass", "A new class", "TNamed")
<KVClassFactory::WriteClassHeader> : File MyClass.h generated.
<KVClassFactory::WriteClassImp> : File MyClass.cpp generated.
void WriteClassHeader()
Write the class header file.
static void MakeClass(const Char_t *classname, const Char_t *classdesc, const Char_t *base_class="", Bool_t withTemplate=kFALSE, const Char_t *templateFile="")

will generate the following MyClass.h and MyClass.cpp files which includes a template doxygen comment block for documenting your class (not shown):

#ifndef __MYCLASS_H
#define __MYCLASS_H
#include "TNamed.h"
class MyClass : public TNamed
{
public:
MyClass()
: TNamed()
{
}
MyClass(const char* name, const char* title)
: TNamed(name, title)
{
}
MyClass(const TString& name, const TString& title)
: TNamed(name, title)
{
}
virtual ~MyClass()
{
}
ClassDef(MyClass,1)//A new class
};
#endif
#define ClassDef(name, id)
char name[80]
#include "MyClass.h"
ClassImp(MyClass)
ClassImp(TPyArg)

You can also add methods to your class before generating the code, or use template files to define complicated methods which can be reused in many classes. See the KVClassFactory documentation and associated examples.

Useful C++ Pre-processor Symbols

The header file KVConfig.h which is configured and generated at build time contains several symbols which may be of help when writing your own code using KaliVeda. KVConfig.h is #included by most class headers in the toolkit; in case of doubt, just add #include "KVConfig.h" in your code.

The following symbols are mainly to ensure portability of code:

Symbol Meaning/Function
WITH_MFM can read GANIL MFM format data (KVMFMDataFileReader)
WITH_MESYTEC can read Mesytec acquisition data
WITH_BUILTIN_GRU can read legacy GANIL EBYEDAT format data (KVGANILDataReader)
External software
WITH_GEMINI GEMINI++ interface KVGemini exists (see Optional software)
WITH_ZMQ compiled with ZeroMQ support (KVZMQMessage)
WITH_BOOST compiled with boost library support
WITH_PROTOBUF can read Google Proto Buffer data (KVProtobufDataReader)
WITH_RSQLITE has SQLite interfaces (KVSQLite::database, KVSQLROOTFile)
Hardware
WITH_MULTICORE_CPU integer value equal to number of processors/cores
C++ standard
WITH_CPP11 C++11 language can be used
WITH_CPP14 C++14 & C++11 language can be used
WITH_CPP17 C++17,C++14 & C++11 can be used
WITH_CPP20 C++20, C++17,C++14 & C++11 can be used

Compiling code using KaliVeda and CMake

Beginning with v1.15, KaliVeda adheres to modern CMake usage and provides exported CMake targets with transitive dependencies for other projects to use.

Simple executable example

Compiling the following simple executable,

// file: MyCode.cpp
#include "KVNucleus.h"
int main()
{
KVNucleus xe("129Xe",49.9);
KVNucleus sn("119Sn");
KVNucleus CN = xe + sn;
CN.Print();
}
Description of properties and kinematics of atomic nuclei.
Definition: KVNucleus.h:123
void Print(Option_t *t="") const override
Display nucleus parameters.
Definition: KVNucleus.cpp:750
int main(int argc, char **argv)

requires the following CMakeLists.txt:

cmake_minimum_required(VERSION 3.13)
project(MyProject)
#------- locate KaliVeda installation
find_package(KaliVeda REQUIRED)
add_executable(XeSn MyCode.cpp)
target_link_libraries(XeSn kaliveda::kaliveda)

Configuration of the build then proceeds like this:

$ cmake -B build
-- The C compiler identification is GNU 13.2.0
-- The CXX compiler identification is GNU 13.2.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found Boost: /usr/lib/x86_64-linux-gnu/cmake/Boost-1.74.0/BoostConfig.cmake (found suitable version "1.74.0", minimum required is "1.9")
-- Found ZeroMQ: /usr/lib/x86_64-linux-gnu/libzmq.so (found version "4.3.4")
-- Found KaliVeda: [/home/...] (found version "1.15.0")
-- Configuring done (0.6s)
-- Generating done (0.0s)
-- Build files have been written to: [/home/.../.../build]

Note that the dependencies of the KaliVeda targets on ROOT and any other packages are taken care of by the transitive properties of the exported targets.

After configuring, your code can be compiled like so:

$ cmake --build build
Scanning dependencies of target XeSn
[ 50%] Building CXX object CMakeFiles/XeSn.dir/MyCode.cpp.o
[100%] Linking CXX executable XeSn
[100%] Built target XeSn

Exported targets

Here are the main CMake targets exported by KaliVeda. They are available after a call to find_package(KaliVeda) if variable KaliVeda_FOUND is TRUE:

Target library
kaliveda::kaliveda libkaliveda.so
kaliveda::kaliveda-indra libkaliveda-indra.so
kaliveda::kaliveda-fazia libkaliveda-fazia.so
kaliveda::kaliveda-indrafazia libkaliveda-indrafazia.so

Building a shared library with a ROOT dictionary

In order to be able to use your own classes on the kaliveda (or root) command line, or write objects of your classes in ROOT files, you need to compile them into a shared library containing the necessary ROOT dictionary entries (see I/O of custom classes in the ROOT manual for more details).

This can be done using the root_generate_dictionary CMake macro provided by ROOT (see Using CMake in the ROOT manual). Assuming you have a directory with class source files and a LinkDef.h file like this:

myProj/
    class1.h
    class1.cpp
    class2.h
    class2.cpp
    LinkDef.h

the minimum cmake-based project to build the shared library and other necessary files will look like this:

[myProj/CMakeLists.txt:]
cmake_minimum_required (VERSION 3.13)
project(myProj)
#------- locate KaliVeda installation
find_package(KaliVeda REQUIRED)
#------- create library target
add_library(MyProject SHARED class1.cpp class2.cpp)
#---link with whatever targets are necessary
target_link_libraries(MyProject PUBLIC kaliveda::kaliveda)
#---the following is required by root_generate_dictionary():
#---without, it cannot find the '.h' files for the library
target_include_directories(MyProject PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
#---generate ROOT dictionary, rootmap & pcm file
root_generate_dictionary(
MyProjectDict # name for dictionary target
class1.h class2.h # list of all class headers
MODULE MyProject # name of library target
LINKDEF LinkDef.h # name of LinkDef file
)