EDM FAQ: The ClassDef and ClassImp macros


The ROOT Object I/O system requires that two macros, ClassDef() and ClassImp(), be installed in a class to automatically declare and implement methods used to perform I/O.

ClassDef() is placed inside the class definition in the header file, in any public, protected, or private section. We have tended to place ClassDef() at the end of a class definition as a minor style preference since it contains public and private declarations. ClassDef() takes two arguments, the class name and class I/O version. The class name is self-explanatory.

The class version identifies the version of the data format on disk for a class. Class versions should not be confused with Offline cdfsoft2 versions or CVS source code versions. Class version is a 16 bit quantity stored in two bytes (ROOT's Version_t data type), with a special value of 0 which should be avoided. ROOT interprets a version of 0 as "no version in use". For instance, if one were to add a new data member to a class which is to be read/written, then one would increment the class I/O version, and treat the new data member in an class version-based if-elseif block inside the Streamer method.

The ClassImp() macro is placed into the compiled source file for a class. It takes one argument, the class name. As a matter of style, I have put ClassImp() and Streamer() in a separate source file from the rest of the class implementation, but there so no requirement to do so.

For the class ToyMuon, ClassDef() is placed in the ToyMuon.hh file, inside and near the end of the class definition of ToyMuon:

class ToyMuon
{
// Definition of class
// It does not matter whether this is public, protected, or private
//=============================================================================
// ROOT I/O Hook
//=============================================================================
    ClassDef(ToyMuon,1)  // ClassName, ClassVersion
}
The ClassImp() macro is placed in a ToyMuon compiled source file, in this case in ToyMuon_streamer.cc though it could also go in ToyMuon.cc:
//=============================================================================
// ROOT I/O Hook
//=============================================================================
  ClassImp(ToyMuon)


Comments on this page may be sent to Rob Kennedy