Adding a class to ROOT

You can extend ROOT with your own classes. When defining a class, it inherits from TObject .

Defining a class

The definition of a class requires the following steps:

  • Inheriting from TObject .

  • Integrating the class to ROOT.

  • Calling the ClassImp macro.

  • Providing a constructor.

Inheriting from TObject

The TObject class provides the default behaviour and protocols for the objects in the ROOT system. The TObject class is the primary interface to classes providing object I/O (writing the class into a ROOT file), error handling, inspection, introspection, and drawing.

Therefore, your class should inherit from TObject .

Integrating the class to ROOT

Add the following line to your class header file, to integrate your class to ROOT:

   ClassDef(ClassName,ClassVersionID)

The ClassVersionID is used by the ROOT I/O system. It is written on the output stream and during reading. You can check this ClassVersionID during reading and take appropriate action depending on the value of ClassVersionID. Every time you change the data members of a class, increase its ClassVersionID by 1.
Set ClassVersionID >= 1.
Set ClassVersionID = 0 in case you do not need object I/O.

Example

In the TLine.h file:

   ClassDef(TLine,1);

Calling the ClassImp macro

Call the ClassImp macro to give your class run time type identification (RTTI) and full I/O capabilities. In addition, you can generate properly documentation for your class using THtml .

Add the following line to your class header file:

   ClassImp(ClassName);

Example

In the Tline.cxx file:

   ClassImp(TLine)

Constructors

ROOT requires for every class to have one of the following constructors:

  • Default constructor
    A constructor with zero parameters or with one or more parameters all with default values.

  • I/O constructor
    A constructor with exactly one parameter which type is a pointer to one of the types marked as an ioctortype.

The default constructor or I/O constructor is called whenever an object is being read from a ROOT database.

Example for a class

#include "TObject.h"

// Define the ABC class and make it inherit from TObject. Then the ABC class can also be written to a ROOT file.
   class ABC: public TObject {
   public:
   Float_t a, b, c, p;
   ABC() : a(0), b(0), c(0), p(0){};

//Integrating the ABC class to ROOT.
   ClassDef (ABC,1)
};

// Call the ClassImp() macro to give the ABC class RTTI and full I/O capabilities.

#if !defined(__CLING__)
    ClassImp(ABC);
#endif