Re: [ROOT] Write/Read Trees with objects as variables

From: Rene Brun (Rene.Brun@cern.ch)
Date: Mon Sep 29 2003 - 18:48:27 MEST


Hi Pascal,

What is the problem with your example?
For me it works without any changes, although I suggest to modify your code
as shown in the attachements.
In particular, it does not make sense to use the split mode for an histogram.

Rene Brun

Pascal PERRODO wrote:
> 
> Hi rooters,
> 
> I try to create and read Trees containing not only Int_t and Float_t,
> but also objects (TH1F).
> I have tried to follow the recipies in the documentation (p202, Adding a
> TBranch to Hold an Object).
> 
> I have written a simple example in the directory
> /afs/cern.ch/user/p/pperrodo/public/tree/
> tree-read.C : creates the tree tree.root and seems to work
> tree-write.C : reads the tree and failed. Could you help me to read such
> a tree.
> 
> I use the Version   3.05/05        8 May 2003 on redhat.
> 
> Many thanks in advance.
> Regards.
> Pascal Perrodo.
> 
>

// create a tree with TH1F as variable
// P. Perrodo
// 28/09/03
{

TH1F h0("h0","h0",100,0,100);

TFile fout("tree.root","RECREATE");

TTree tr1("tr1","tr1");
 
Int_t   i1;
Float_t x1;
TH1F *h=0; //<========

tr1.Branch("i1",&i1,"i1/I");
tr1.Branch("x1",&x1,"x1/F");
tr1.Branch("hist", "TH1F", &h, 32000, 0); //<========

for ( Int_t n=0 ; n<100 ; n++ ){

  i1 = n;
  x1 = n*n;

  h0.Reset();
  h0.Fill(n);
  h = (TH1F*) h0.Clone(); //<========

  tr1.Fill();
  delete h; //<========
}

tr1.Print();
fout.Write();

}


{

TFile f("tree.root");

TTree *tr = (TTree*) f.Get("tr1");

Int_t   i1;
Float_t   x1;
TH1F *h=0; //<========

//h = new TH1F(); //<========

tr->SetBranchAddress("i1",&i1);
tr->SetBranchAddress("x1",&x1);
TBranch *bh0 = tr->GetBranch("hist");
bh0->SetAddress(&h);

TProfile hd1("hd1","hd1",100,0,100);

Int_t nentries = tr->GetEntries();
Int_t nbytes = 0;
for (Int_t i=0; i<nentries;i++) {
  nbytes += tr->GetEntry(i);
  
  hd1.Fill(i1,x1);

}

hd1.Draw();

//getchar();

}



This archive was generated by hypermail 2b29 : Thu Jan 01 2004 - 17:50:15 MET