7/14/2016

Compile my C/C++ program with certain version of Python interpreter

If you have more than two Pythons in your system, such as in the case that Python2 and Python3 are installed in your system, you may have to specify one explicitly to compile that with your application program, which is written not in Python. Here is how to specify PythonX.Y with the python version number X.Y in a Makefile to build your application.

First, you need to find out compiler and linker options specified at the time of installation of PythonX.Y:

% pythonX.Y-config --cflags

will show you the compiler options <CFLAGS> and

% pythonX.Y-config --ldflags

will show you the linker options <LDFLAGS>.

Second, you add lines in the Makefile to include <CFLAGS> in the compilation and load <LDFLAGS> in the linkage.

That's it!

12/11/2014

How to CMake MyProject together with ROOT GUI

I have MyProject composed of source files

myProject/c++/src/myMain.cpp
myProject/c++/include/myMain.h
myProject/c++/include/myMainLinkDef.h

to run on ROOT GUI. In myMain.cpp, a class MyMainGUI is called in a loop of  TApplication. In myMain.h,  this class is declared with the first line

    RQ_OBJECT("myMainGui").

myMainLinkDef.h includes pragma comment lines which look like

#pragma link C++ class MyMainGUI;
#pragma link C++ class mylib::MyFunction;

where mylib::MyFunction is a class used in ROOT GUI and given in

/somewhere/myLibrary/myLib.h
/somewhere/myLibrary/libmyLib.a

Problem: Compose a cmake package to build MyProject in an arbitrary directory myWKDIR.

I describe a solution in the following.

1. Prepare myProject/c++/CMakeLists.txt (presupposition: ROOT version 5.34):
==========================================================
cmake_minimum_required(VERSION 2.8)

set(CMAKE_VERBOSE_MAKEFILE 1)
#is convenient for debugging the make process.

project(MyProject)

set(CMAKE_CXX_COMPILER g++)
# since I used g++ to compile libmyLib.a

set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/..)
#since I'm going to build MyProject in myWKDIR/build/
#so that ${PROJECT_BINARY_DIR}/.. means myWKDIR.

#Prepare for the ROOT GUI
#-------------------------------------
#imitating geant4/examples/analysis/AnaEx02
#/////////////////////////////////////////////////////////////////////
find_package(ROOT REQUIRED)
find_program(ROOT_CINT_EXECUTABLE rootcint PATHS ${ROOT_DIR}/bin NO_DEFAULT_PATH})

#External Libraries to be linked
#---------------------------------------
set(myLibrary_DIR /somewhere/myLibrary)
add_library(MyLib STATIC IMPORTED)
set_property(TARGET MyLib PROPERTY
IMPORTED_LOCATION ${myLibrary_DIR}/libmyLib.a)
#Other ways to include libmyLib.a in the ld process is to specify this library in
#CMAKE_EXE_LINKER_FLAGS as
#-L${myLibrary_DIR} -lmyLib
#or find_library(MyLib myLib PATHS ${myLibrary_DIR})
#and include MyLib in target_link_libraries.

#building a dictionary for ROOT GUI
#------------------------------------------------
set(INCLUDE_DIRECTORIES
${PROJECT_SOURCE_DIR}
${PROJECT_SOURCE_DIR}/include
${myLibrary_DIR}
${ROOT_INCLUDE_DIR}
)
include_directories(${INCLUDE_DIRECTORIES})

#generate the dictionary
#the following part is imported from http://lcio.desy.de/v01-60/cmake/MacroRootDict.cmake
set(MAINPRGM "myMain")
set(headers include/myMain.h)
set( _dict_includes )
FOREACH( _inc ${INCLUDE_DIRECTORIES} )
SET( _dict_includes "${_dict_includes}\t-I${_inc}")
ENDFOREACH()
ADD_CUSTOM_COMMAND(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${MAINPRGM}Dict.cpp ${CMAKE_CURRENT_BINARY_DIR}/${MAINPRGM}Dict.h
COMMAND ${ROOT_CINT_EXECUTABLE} -f ${CMAKE_CURRENT_BINARY_DIR}/${MAINPRGM}Dict.cpp -c ${_dict_includes} ${headers} include/${MAINPRGM}LinkDef.h
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
DEPENDS ${headers} include/${MAINPRGM}LinkDef.h
COMMENT "generating: ${MAINPRGM}Dict.cpp ${MAINPRGM}Dict.h"
)
list(APPEND headers ${CMAKE_CURRENT_BINARY_DIR}/${MAINPRGM}Dict.h)

#build the target
#-------------------------
#prepare for the ld flag
execute_process(COMMAND root-config --glibs OUTPUT_VARIABLE     
               ROOT_LD_FLAGS OUTPUT_STRIP_TRAILING_WHITESPACE)                 
set(CMAKE_EXE_LINKER_FLAGS "${ROOT_LD_FLAGS}")

#destination
set(sources src/myMain.cpp
${CMAKE_CURRENT_BINARY_DIR}/${MAINPRGM}Dict.cpp
)
add_executable(${PROJECT_NAME}.exe ${sources} ${headers})
target_link_libraries(${PROJECT_NAME}.exe MyLib)

2. build MyProject in myWKDIR
============================
> mkdir myWKDIR
> mkdir myWKDIR/build
cd myWKDIR/build
myWKDIR/build> cmake /directory_path_to/myProject/c++
myWKDIR/build> make

Any comments are welcome.

2/15/2014

Choosing a python bundle for PyROOT

After played with python 2.7 for a while, I got a need for ROOT drawings. So, I decided to try PyROOT which was supposed to be installed already in my ROOT 5.34.09 installation:

% port -v installed root
The following ports are currently installed:
  root @5.34.09_1+graphviz+gsl+minuit2+opengl+python27+roofit+soversion+ssl+tmva+xml (active) platform='darwin 12' archs='x86_64'

OK, python27 is there. But my python told

% python -c "import ROOT"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named ROOT

Well, PyROOT needs two files, ROOT.py and libPyROOT.so which must be visible from python. Where are they?
% port contents root | grep ROOT.py
  /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/ROOT.py
% port contents root | grep libPyROOT.so
  /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/libPyROOT.so
  /opt/local/lib/root/libPyROOT.so

OK, you guys are there. But when I make these guys visible (by copying or symbolic links) from python, it told

% python -c "import ROOT"
Fatal Python error: PyThreadState_Get: no current thread
Abort trap: 6

What's wrong? After for a while, I found the bundle of python which I was using did not fit with the guy libPyROOT.so, who came through macport. To use this guy, I need to switch my python to the one came also from macport. This one was installed as /opt/local/bin/python2.7, and

%/opt/local/bin/python2.7 -c "import ROOT"

Passed!  What is the difference between 'em?

%/usr/bin/python
Python 2.7.5 (default, Aug  1 2013, 01:01:17)
[GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwin

%/opt/local/bin/python2.7
Python 2.7.5 (default, Aug 25 2013, 00:04:04) 
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin

Well, well, well. I can set my default python as the later one by, for instance,
sudo ln -s /opt/local/bin/python2.7 /opt/local/bin/python
and exporting PATH with /opt/local/bin placed earlier than /usr/bin so that I can just write, e.g.

%python -c "from ROOT import TCanvas"

10/28/2013

gcc and gfortran on OS X 10.9 Marvericks

A new release of OS X 10.9 Marvericks was installed on my MacBook 15-inch Retina Pro 2012.
I kept all softwares previously installed on OS X 10.8 as they were. All of them are working
well without a change except for the following.

gcc 4.8.1
========
I needed to install the Command Line Tools by issuing 
%xcode-select --install
to make gcc work. This is necessary regardless whether one has
already installed Xquartz or not.

gfortran
=========
I need
-fno-underscoring
option at the load time to use gnu extensions. 
This was not required before in my case.

10/17/2013

How to make PDF v1.6 files comprisable in LaTeX?

 This issue may concern when you are using TeX Live distribution of dvipdfmx.

It is a common problem that PDF v1.6 files are not comprisable in LaTeX sources to produce PDF outputs using dvipdfmx. This problem occurs due to a line at the beginning of a file dvipdfmx.cfg that reads

V 5

which specify the output PDF version to be 1.5. The problem is fixed by just replacing this line by

V 6

The file dvipdfmx.cfg is placed in <distribution>/texlive/<version>/texmf-config/dvipdfmx for typical cases of TeX Live distributions.

I encountered this problem in TeXShop with ptex2pdf for Japanese writing and found the file in the directory with <distribution> = /usr/local and <version> = 2013.

8/29/2013

A primitive way to pass a pointer to CINT macro

I have a TTree pointed to by *tree in my C++ program and want to pass it to MyMacro.C to use it in a CINT session invoked from the C++ program. Here is a primitive-but-it-does-work way as below.

Lines in my C++ program read

TTree *tree = ....
std::stringstream ss;
ss<< tree;
std::string macroName = "MyMacro.C("+'"'+ss.str()+'"'+")";
gROOT->Macro(macroName.c_str());

and in MyMacro.C, I have

void MyMacro(std::string &tp){

TTree *tree;
std::stringstream ss(tp);
ss>> tree;

int nentries = (Int_t)tree->GetEntries();
TObject *tobj;
...
}

8/27/2013

Using MyClass in a CINT macro

When I am evaluating something in my C++ program and calling a CINT macro by

gROOT->Macro(mymacro.C);

to draw figures, I need to pass parameters of the evaluation to this macro.
When the number of parameters increases and their structures get complicated,  passing them by

mymacro.C(hard coded parameter list)

is not useful. In this case, I prepare a class MyClass to define a set of parameters to use
in my evaluation and pass it to the macro. Results of the evaluation kept in MyClass
can certainly be passed to the macro for drawings. The way to do this is well described in
The CINT Dictionary Generator.

Here I note my sample case where MyClass is stand alone and not inheriting from TObject class.
MyClass is defined in MyClass.h and implemented in MyClass.cpp
MyClass has a constructor without arguments.
Parameters are defined as static members of MyClass.
MyClass is called in mymacro.C as

MyClass passParams;

In the Makefile which I showed on Aug. 13, the following lines are added to create
a dictionary MyClassDict.o to compile with the main part:
CINTCLASS = MyClass
UOBJS := $(UOBJS) $(CINTCLASS).o $(CINTCLASS)Dict.o
$(CINTCLASS)Dict.cpp: $(CINTCLASS).h
<<tab>> rootcint -f $(CINTCLASS)Dict.cpp -c $(CINTCLASS).h

Now I can draw figures of results from a C++ computation interactively in CINT.