Adding Your Class to ROOT: ClassDef
ClassDef is a C preprocessor macro that must be used if your class derives from TObject. It can be used if you simply want to enhance your class, see this paragraph. ClassDef contains member declarations, i.e. it inserts a few new members into your class. No per-object data member is added.
The ClassDef macro family (read on and you will see that there are more) is defined in the file Rtypes.h. This file is referenced by all ROOT include files, so you will automatically get them if you use a ROOT include file.
To apply ClassDef() to your class, insert it somewhere in your class's definition. It contains its own private and public tags so it can be added to either a private or public part of a class definition.
class MyClass: public TObject {
ClassDef(MyClass, 1); //The class title
};
The ClassDef macro takes two arguments: the class name and the class version. There can be a comment after the ClassDef macro which is used e.g. for documenting the class.
ClassDef and the Class Name
The class name must be stated without the enclosing namespace, i.e. forclass A::B you will write
namespace A {
class B {
ClassDef(B, 1); // An example
};
}
For templates you can use ClassDef too, without template arguments; see e.g. the header file for TParameter<int>.
ClassDef and the Class Version
You will need to increase the version number whenever you change the definition of your class, or ROOT will not be able to handle objects written before and after this change in one process. Start counting at 1. The version number 0 (zero) disables I/O for the class - which makes sense for classes that are purely transient (e.g. a factory).See e.g. TLine.h for a real life application of ClassDef.
ROOT I/O Requirements for Classes
To be able to store objects with ROOT they must have a default constructor, i.e. one that can be called without arguments:
class MyClass: public TObject {
public:
MyClass(int i = 0); // default constructor
ClassDef(MyClass, 1); //The class title
};
ClassDef and Virtual Functions
ClassDef assumes that your class has a virtual function table, and so it adds a few virtual functions to it. This in turn means that if you should declare your destructor as virtual:
...
virtual ~MyClass();
If you don't have virtual functions, and if you do not plan to derive from the class, then you can use the ClassDefNV version of the macro. It's like ClassDef, only it assumes that there is no inheritance involved.
ClassDef and Documentation
The comment after ClassDef is used as the title of the class's TClass object. It is also displayed in the documentation's class index.
In the old days, ClassImp was needed to tell THtml which source file belongs to which class. Currently, THtml usually does not need it anymore; the file name gets derived from the class name or the header file name and searched recursively using the input path. If THtml cannot determine the class's source file you can put ClassImp(MyClass) into the source file to help it. See for example TLine.cxx.
Dictionary: ClassDef Member Definitions
ClassDef declares a few members to your class. Some of them are inline, but some need to have an implementation. ROOT generates it automatically as part of the dictionary. We have documentation on how to generate a dictionary with rootcint. Since recently, genreflex can also deal with ClassDef classes and TObject inheritance.
If you do not create the dictionary for your class you will have missing symbols at link time (e.g. ShowMembers()), because ClassDef declares class members but they are not implemented anywhere!