For a test I tried followings.
1. Create a file with a class.
- Code: Select all
// myclass.h
#include "TNamed.h"
class myclass: public TNamed
{
public:
Double32_t a;
myclass();
myclass(const char* name);
ClassDef(myclass,1);
};
#ifdef __MAKECINT__
#pragma link C++ class myclass+;
#endif
- Code: Select all
// myclass.cc
#include "myclass.h"
ClassImp(myclass)
myclass::myclass() {}
myclass::myclass(const char* name){SetName(name);}
- Code: Select all
// testclass.cc
#include "myclass.cc"
#include "TFile.h"
#include "TTree.h"
#include "TClonesArray.h"
int main()
{
TFile f("test.root","recreate");
TTree *tree = new TTree("tree", "tree");
TClonesArray *array = new TClonesArray("myclass");
tree->Branch("a", "TClonesArray", &array, 32000, 99);
array->ExpandCreate(1);
((myclass*) (array->At(0)))->a = 4;
tree->Fill();
f.Write();
}
$ rootcint -f dict.cc -c myclass.h+
$ g++ -Wall `root-config --libs --cflags` -o testclass testclass.cc dict.cc
$ ./testclass
2. Change the class structure
- Code: Select all
// new myclass.h
#include "TNamed.h"
class myclass: public TNamed
{
public:
Double_t a; // <== changed type
myclass();
myclass(const char* name);
ClassDef(myclass,2); // <== increamented
};
#ifdef __MAKECINT__
#pragma link C++ class myclass+;
#endif
3. Read the file
- Code: Select all
$ root
root [0] .L myclass.cc+
root [1] TFile f("test.root")
root [2] tree->Draw("a")
Then what I see in TCanvas is a histgram with an entry at 0 instead of 4.
When I enable debugging messages, it says as following. So ROOT notices the structure has been changed.
- Code: Select all
====>Rebuilding TStreamerInfo for class: myclass, version: 1
Creating StreamerInfo for class: myclass, version: 2
StreamerInfo for class: myclass, version=2, checksum=0xd3a58282
TNamed BASE offset= 0 type=67 The basis for a named object (name, title)
Double_t a offset= 28 type= 8
i= 0, TNamed type= 67, offset= 0, len=1, method=0
i= 1, a type= 8, offset= 28, len=1, method=0
Warning in <TStreamerInfo::BuildOld>: element: myclass::Double32_t a has new type: Double_t/8
StreamerInfo for class: myclass, version=1, checksum=0x70d1912d
TNamed BASE offset= 0 type=67 The basis for a named object (name, title)
Double32_t a offset= 28 type= 9
i= 0, TNamed type= 67, offset= 0, len=1, method=0
i= 1, a type=209, offset= 28, len=1, method=0
I am aware that it works when I don't call ".L myclass.cc+", but this is just a sample code to reproduce my problem. What I really do is using the same binary to write and read trees which is linked with my own classes.
with respect