How to compile root Macros
Why compile with g++ and not CINT?
CINT, the root interpreter, is dead slow and I have seen a very noticeable decrease in the execution time of my analysis code when compiled with g++ instead of root. Also it tells you the correct line number where errors and warnings in your code are.
Creating a script to do the compile the macro
below is a script needed to compile the root macro, the first commands prints the subsequent commands, the second compiles your code and the third links the object file with the libraries needed to run root.
#!/bin/bash
set -x
g++ -O2 -pipe -Wall -W -Woverloaded-virtual -fPIC -Iinclude `root-config --cflags` -o $1.o -c $1
g++ -O2 $1.o `root-config --glibs` -lRooFit -o $1.exe
You can also run root-config with --libs instead of --glibs if you want to disable the graphics, so disable displaying the graphs (you can still create them). This may possibly be useful if you will be running on the batch system or just don't want a gui.
Editing your macro to make it compile
Adding missing headers
First off to make it run you will have to make sure all the correct headers are included, this will be macro dependant. The ones I have included are listed below.
#include <iostream>
#include <sstream>
#include <string>
#include <stdlib.h>
#include <cmath>
#include <TROOT.h>
#include <TChain.h>
#include <TCanvas.h>
#include <TH1D.h>
#include <TH2D.h>
#include <TString.h>
#include <RooRealVar.h>
#include <RooPolynomial.h>
#include <RooGaussian.h>
#include <RooDataHist.h>
#include <RooAddPdf.h>
#include <RooPlot.h>
If you get error messages about a class being undefined just add a header for that class like #include
Make the code standards compliant
You might need to explicitly cast variables to the correct type or make sure you are accessing pointers with -> and objects with a dot. Again you will have to work out the bugs in tyourr own code.
Compile it
I called the script above rootg++ so to compile Macro.C I run the following
SetupProject Ganga
./rootg++ Macro.C
This produces an executable (assuming it compiled on the first time which it won't) called Macro.C.exe which can be directly run.
./Macro.C.exe
Runtime Error
Once I came up with this runtime error:
Error: 'pool::DbHandleBase<pool::DbSessionObj>' Incomplete template resolution in shared library /afs/rl.ac.uk/lhcb/lcg/external/ROOT/5.26.00b/slc4_ia32_gcc34/root/cint/cint/stl/vector.dll:1:
Add following line in header for making dictionary
#pragma link C++ class pool::DbHandleBase<pool::DbSessionObj>;
*** Interpreter error recovered ***
Error: 'pool::DbHandleBase<pool::DbDomainObj>' Incomplete template resolution in shared library /afs/rl.ac.uk/lhcb/lcg/external/ROOT/5.26.00b/slc4_ia32_gcc34/root/cint/cint/stl/vector.dll:1:
Add following line in header for making dictionary
#pragma link C++ class pool::DbHandleBase<pool::DbDomainObj>;
*** Interpreter error recovered ***
Error: 'pool::DbHandleBase<pool::DbDatabaseObj>' Incomplete template resolution in shared library /afs/rl.ac.uk/lhcb/lcg/external/ROOT/5.26.00b/slc4_ia32_gcc34/root/cint/cint/stl/vector.dll:1:
Add following line in header for making dictionary
#pragma link C++ class pool::DbHandleBase<pool::DbDatabaseObj>;
*** Interpreter error recovered ***
Error: 'pool::DbHandleBase<pool::DbContainerObj>' Incomplete template resolution in shared library /afs/rl.ac.uk/lhcb/lcg/external/ROOT/5.26.00b/slc4_ia32_gcc34/root/cint/cint/stl/vector.dll:1:
Add following line in header for making dictionary
#pragma link C++ class pool::DbHandleBase<pool::DbContainerObj>;
*** Interpreter error recovered ***
To fix this simply close your shell, re-setup your environment with
SetupProject DaVinci and try to compile and run again.
--
ThomasBird - 2010-07-30