Table of Contents

Adding A Class to ROOT

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 write a (simple) class with a method that creates a ROOT file. You need the following three files: ana.cc, ana.hh and a Makefile.

.cc and .hh File

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  

Some Notes

Makefile

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 
# =============================== 

Some Notes

Loading the Class in ROOT

Troubleshooting

The original articles about the addition of a class to ROOT are provided here.