Hi
im sorry for posting unexecutable code… however, the only thing that was missing to make it run was the (empty) Notify() method. Meanwhile I found a way how to read the tree and getting the correct UserInfo, but still i dont really understand the behaviour.
Here is again the content of my MyTree.h (just for the sake of completeness, its the same as above with minor changes)
#ifndef MyTree_h
#define MyTree_h
#include <TROOT.h>
#include <TChain.h>
#include <TFile.h>
class MyTree {
public :
TTree* fChain; //!< pointer to the analyzed TTree or TChain
Int_t fCurrent; //!< current Tree number in a TChain
Int_t N_seg;
Int_t N_bins;
// Declaration of leaf types
Double_t** signal;
// List of branches
TBranch *b_signal; //!
MyTree(Int_t nseg,Int_t nbins);
MyTree(TTree *tree);
virtual ~MyTree();
};
#endif
MyTree::MyTree(Int_t nseg,Int_t nbins){
TTree* tree = new TTree("MyTree","MyTree");
tree->GetUserInfo()->Add(new TParameter<Int_t>("N_seg",nseg));
tree->GetUserInfo()->Add(new TParameter<Int_t>("N_bins",nbins));
this->N_seg = nseg;
this->N_bins = nbins;
signal = new double*[N_seg];
for (int i=0;i<N_seg;i++){signal[i] = new double[N_bins];}
InitNew(tree);
}
MyTree::MyTree(TTree *tree){
if (tree == 0) {return;}
cout << tree->GetUserInfo()->Print() << endl;
//cout << tree->GetUserInfo()->Last()->GetVal() << endl;
Init(tree);
}
void MyTree::InitNew(TTree *tree){
// Set branch addresses and branch pointers
if (!tree) return;
fChain = tree;
fCurrent = -1;
fChain->SetMakeClass(1);
this->b_signal = fChain->Branch("signal" ,signal ,TString::Format("signal[%d][%d]/D",N_seg,N_bins));
Notify();
}
void MyTree::Init(TTree *tree){
if (!tree) return;
fChain = tree;
fCurrent = -1;
fChain->SetMakeClass(1);
cout << tree->GetUserInfo()->Print() << endl;
// TODO : allocate memory
fChain->SetBranchAddress("signal", signal, &b_signal);
Notify();
}
void MyTree::Notify(){}
And here is the code that i use to write the tree (TestTree()) and read it (TestTree2() and TestTree3())
#include "MyTree.h"
void TestTree(){
TFile* res = new TFile("TEST.root","RECREATE");
MyTree* t = new MyTree(3,3);
res->Write();
}
void TestTree2(){
TChain* c = new TChain("MyTree","MyTree");
c->Add("TEST.root");
c->GetUserInfo()->Print();
MyTree* t = new MyTree(c);
}
void TestTree3(){
TFile *f = new TFile("TEST.root");
TTree *t1 = (TTree*)f->Get("MyTree");
t1->GetUserInfo()->Print();
MyTree* t = new MyTree(t1);
t->fChain->GetUserInfo()->Print();
}
And this is what i get as output…
root [0] .L MyTreeRun.c
root [1] TestTree()
root [2] TestTree2()
Collection name='UserInfo', class='TList', size=0
Collection name='UserInfo', class='TList', size=0
0
Collection name='UserInfo', class='TList', size=0
0
root [3] TestTree3()
Collection name='UserInfo', class='TList', size=2
TParameter<int> N_seg = 3
TParameter<int> N_bins = 3
Collection name='UserInfo', class='TList', size=2
TParameter<int> N_seg = 3
TParameter<int> N_bins = 3
0
Collection name='UserInfo', class='TList', size=2
TParameter<int> N_seg = 3
TParameter<int> N_bins = 3
0
Collection name='UserInfo', class='TList', size=2
TParameter<int> N_seg = 3
TParameter<int> N_bins = 3
I guess its my misunderstanding of how to use TChains. It seems as the UserInfo is lost/not read when I read the tree from the file via a TChain. Only when I open the TTree “manually” from the file the UserInfo is correct. However, when I read the tree in this way i cannot (at least i dont know how) read from multiple files.