I love pointers but sometimes

Hi all,
I’m a new Rooter and I’ve a little problem, if any of you could help me …
Let’s have a look at this short code:

Class.h

[code]#ifndef Classe
#define Classe
#include “TH1F.h”

class Classe
{
public:
Classe();

TH1F *h1;
TH1F *h2;
TH1F *h3;
ClassDef(Classe,1)
};

#endif[/code]

Class.cxx

[code]#include “Class.h”

Classe::Classe()
{
h1 = new TH1F(“h1”,“The first”,100,0,100);
h2 = new TH1F(“h2”,“The second”,100,0,100);
h3 = new TH1F(“h3”,“The third”,100,0,100);
}
[/code]

Script.C

[code]{
gSystem->CompileMacro(“Class.cxx”,“gfk”);
TFile *file = new TFile(“puppa.root”,“RECREATE”);
TTree *tree = new TTree(“tree”,“My tree”);
Classe *pointer = new Classe();
tree->Branch(“ramo”,“Classe”,&pointer,8000,99);

for(Int_t i=0;i<30;i++)
if(i<10)
pointer->h1->Fill(i);
else if(i<20)
pointer->h2->Fill(i);
else if(i<30)
pointer->h3->Fill(i);

tree->Fill();
file->Write();
}[/code]

The problem is that when I execute the script and I look at the Root file, in the branch created, histos have only one entry which is 0!!! The right ones are in the top directory of the rootfile!
I also tried objects instead of pointers and the result was worse: no histos at all in the tree (again the right ones are in the top dir)!!!
I think that the problem might be the adress passed in the TTree::Branch method or the way I fill the tree.
How can I solve my problem???
Thanks a lot!
Bye…

With the code as you send it it is normal
to have only one entry in the Tree. Your statement tree->Fill is outside the loop.
If instead you do:
for(Int_t i=0;i<30;i++) {
if(i<10)
pointer->h1->Fill(i);
else if(i<20)
pointer->h2->Fill(i);
else if(i<30)
pointer->h3->Fill(i);

tree->Fill(); 

}
tree->Print();
file->Write();

you should get the following output showing 30 entries


*Tree :tree : My tree *
*Entries : 30 : Total = 88260 bytes File Size = 4267 *

  •    :          : Tree compression factor =  16.03                       *
    

*Branch :ramo *
*Entries : 30 : BranchElement (see below) *

*Br 0 :h1 : *
*Entries : 30 : Total Size= 29059 bytes File Size = 1459 *
*Baskets : 3 : Basket Size= 8000 bytes Compression= 15.62 *

*Br 1 :h2 : *
*Entries : 30 : Total Size= 29089 bytes File Size = 1453 *
*Baskets : 3 : Basket Size= 8000 bytes Compression= 15.70 *

*Br 2 :h3 : *
*Entries : 30 : Total Size= 29059 bytes File Size = 1355 *
*Baskets : 3 : Basket Size= 8000 bytes Compression= 16.82 *

Rene

Hi,
thanks for replying, but in this way I still have the problem!
What I get with the code is a tree with a branch containing 3 histos filled with 30 0s!!!
What I’d like to find is a tree with one branch containing 3 histos but the 1st should contain one hit for each bin from 0 to 10, …
Thanks again!
Bye…

You are misunderstanding. What you have is a tree with one branch with 3 sub-branches. Each of the sub-branches has one historgram. The tree has 30 entries each with 3 histogram. The very first entry has h1 with a zero, h2 and h3 empty. The 2nd entry has h1 with a zero and a one, h2 and h3 empty. The 30th entry has [quote]"the 1st [histo] should contain one hit for each bin from 0 to 10, … "[/quote]
I would recommend that you re-read the chapter on TTrees, Histograms and TFile in the ROOT User’s Guide.

Cheers,
Philippe.