TObject::Read, Write() and automatic schema evolution

I would like to ask how can I use automatic schema evolution when I read and write a class by using TObject::Write() and TObject::Read().

For a test I tried followings.

  1. Create a file with a class.

// myclass.h #include "TNamed.h" class myclass: public TNamed { public: int a; myclass(); myclass(const char* name); ClassDef(myclass,1); };

[code]// myclass.cc

#include "myclass.h"
ClassImp(myclass)
myclass::myclass() {}
myclass::myclass(const char* name){SetName(name);}[/code]

// testclass.cc #include "myclass.cc" #include "TFile.h" int main() { static myclass* cuts=new myclass("somename"); cuts->a=4; TFile f("test.root","recreate"); cuts->Write(); }

$ rootcint -f dict.cc -c myclass.h
$ g++ -Wall root-config --libs --cflags -o testclass testclass.cc dict.cc
$ ./testclass

  1. Change the class structure

// new myclass.h #include "TNamed.h" class myclass: public TNamed { public: int a; int b; // <== [color=blue]added[/color] myclass(); myclass(const char* name); ClassDef(myclass,2); // <== [color=blue]increamented[/color] };

  1. Read the file

$ root root [0] .L myclass.cc+ root [1] TFile f("test.root") root [2] somename->Dump()

Then it shows an error
Error in TBuffer::CheckByteCount: object of class myclass read too many bytes: 36 instead of 32
Warning in TBuffer::CheckByteCount: myclass::Streamer() not in sync with data on file test.root, fix Streamer()

I guessed one problem is that the file does not have the streamer info of myclass. So I tried adding streamer info of myclass by similar function with TTree::BuildStreamerInfo() and succeeded adding it in the file. I checked with TFile::ShowStreamerInfo().
But it still complains about mismatch of the byte count.

Do I need to write myclass into a TTree to use automatic schema revolution ?

To benefit from the automatic schema evolution, you must specify the option “+” in the “#pragma” statement of your LinkDef.h file or alternatively specify

rootcint -f dict.cc -c myclass.h+ when running rootcint

Rene

1 Like

It works. Thank you very much.