This is an old revision of the document!
Instead of writing C++ macros which are intepreted by CINT, one can think of adding a class to ROOT itself. This may be particularly useful for methods that draw and save histograms, apply cuts, etc. The advantage over macros is the stability, as the code is compiled and then executed, and not interpreted by CINT.
Suppose you want to have a class with a method, that creates a root-file. You need the following files: ana.cc, ana.hh and a Makefile.
Your .cc file should look somehow like this:
#include "ana.hh" using namespace std; ClassImp(ana) // ----------------------------------------------------- // Constructor // ----------------------------------------------------- ana::ana() { cout << "This is the AnalysisClass, v 1.0" << endl; } // ------------------------------------------------------ // Destructor // ------------------------------------------------------ ana::~ana(){ } // ---------------------------------------- // test // ---------------------------------------- void ana::test(){ TFile *bla = new TFile("bla.root","CREATE"); }
Your .hh file should look like:
#ifndef ANA #define ANA #include <TObject.h> #include <TFile.h> #include <TF1.h> #include <TROOT.h> class ana: public TObject { public: ana(); ~ana(); void test(); ClassDef(ana,1) }; #endif
The Makefile should look like this:
# =============================== # Makefile for ana # =============================== ROOTGLIBS = $(shell $(ROOTSYS)/bin/root-config --glibs) ROOTCFLAGS = $(shell $(ROOTSYS)/bin/root-config --cflags) CXX = g++ CXXFLAGS = -g -Wall -fPIC SOFLAGS = -shared CXXFLAGS += $(ROOTCFLAGS) ANACLASSES = ana.o anaDict.o # =============================== ana: ana.cc # ------------------------------- $(CXX) $(CXXFLAGS) -c ana.cc -o ana.o $(ROOTSYS)/bin/rootcint -f anaDict.cc -c ana.hh $(CXX) $(CXXFLAGS) -c anaDict.cc -o anaDict.o $(CXX) $(SOFLAGS) $(ANACLASSES) -o libAnaClasses.so $(ROOTGLIBS) # =============================== clean: rm -f ana.o anaDict.o anaDict.cc anaDict.h libAnaClasses.so # ===============================
make
in your directory, you should have five new files: ana.o, anaDict.cc, anaDict.h, anaDict.o and libAnaClasses.so. .L libAnaClasses.so
.ana myanaclass
.myanaclass.test()
.symbol lookup error … undefined symbol: _ZN5TFile4OpenEPKcS1_S1_ii
, there may be several reasons:$ROOTSYS
is set (echo $ROOTSYS
) and if yes, check if the path of ROOT is the same as in $LD_LIBRARY_PATH
and $PATH
. If you have setup Gaudi and DaVinci, these things should be correct.The original articles about the addition of a class to ROOT is provided here.