User Tools

Site Tools


playground:playground

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
playground:playground [2008/11/29 16:51] decianmplayground:playground [2015/03/04 13:46] (current) – external edit 127.0.0.1
Line 1: Line 1:
-====== Adding A Class to ROOT ====== +====== PlayGround ======
-===== Idea ===== +
-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. +
- +
-===== Example ===== +
-Suppose you want to have an method, that creates a root-file. You need the following files: **ana.cc**, **ana.hh** and a **Makefile**. +
- +
-==== .cc and .hh File ==== +
-Your **.cc** file should look somehow like this: +
- +
-<code> +
-#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");  +
-}  +
-</code> +
- +
-Your **.hh** file should look like: +
-<code> +
-#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   +
-</code> +
- +
-==== Some Notes ==== +
-  * You **must** write a constructor yourself. Normally in C++, if you forget your constructor, the compiler will create a standard-constructor for you. However, if you don't provide a constructor here, you'll run into trouble. +
-  * The **ClassImp(ana)** and the **ClassDef(ana, 1)** are necessary for the full functionality. ana names the class and the number in **ClassDef** is the VersionID; it has to be greater than zero. **ClassDef(...)** always has to be the last statement before the closing bracket. These statements are needed to create the dictionary for ROOT in the compilation process. +
- +
-==== Makefile ==== +
-The Makefile should look like this: +
- +
-<code> +
-# ===============================  +
-# 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  +
-# ===============================  +
-</code> +
- +
-==== Some Notes ==== +
-  * When doing copy & paste: Don't forget to replace the whitespaces with a tab. +
-  * After typing ''make'' in your directory, you should have five new files: **ana.o**, **anaDict.cc**, **anaDict.h**, **anaDict.o** and **libAnaClasses.so**. +
-  * I don't know anything about Makefiles. Any improvement of knowledge about them would therefore be helpful... +
- +
-==== Loading the Class in ROOT ==== +
-  * Start root and type '' .L libAnaClasses.so ''+
-  * Create an instance of your object. Type ''ana myanaclass''+
-  * Execute the method. Type ''myanaclass.test()''+
- +
-===== Troubleshooting ===== +
-  * Always compile and run the class on the same machine. Otherwise you may get problems with different root versions, different architectures (32- vs. 64-bit), etc. +
-  * If your code compiles correctly, but you get errors like ''symbol lookup error ... undefined symbol: _ZN5TFile4OpenEPKcS1_S1_ii'', there may be several reasons: +
-    * Check if ''$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. +
-    * Did you think of the constructor? A missing constructor may give similar error messages. +
-    * There may be also be something missing in the Makefile, meaning that not everything is linked correctly. +
-  * The described procedure works for me on lxplus and locally (leopard.physik.uzh.ch). +
- +
-===== Links ===== +
-The original articles about the addition of a class to ROOT is provided [[http://root.cern.ch/root/Using.html|here]]. +
- +
- +
- +
  
playground/playground.1227973880.txt.gz · Last modified: 2008/11/29 16:51 by decianm