Re: Reading a tree

Rene Brun (Rene.Brun@cern.ch)
Mon, 31 May 1999 08:47:53 +0000


Hi Rudy,
I cannot reproduce your problem.
Could you send me your file giving problems with TTree::Draw?
Coukd you add a short macro with the exact sequence of selections?
You will find below a small macro writing (tour example)
and the equivalent to read it.
You shoudl use TTree::GetEvent
There is also an automatic code generator for an analysis function.
You can try TTree::MakeClass.
Please read URL:
http://root.cern.ch/root/HowtoReadTree.html

Rene Brun

void rudyWrite ()
{
TFile *hfile =new TFile ("rudy.root","RECREATE","tree");

TTree *Tr;
Tr = new TTree("T","ROOT tree ");
struct test_struc {Float_t x,y,z;} obj;
float var;
Tr->Branch("vectors",&obj,"x:y:z");

Tr->Branch("variable",&var,"var");
for (Int_t i=0;i<10000;i++){
obj.x=i+1.5;
obj.y=i+1.5;
obj.z=i*i;
var=i;
Tr->Fill();
}
Tr->Print();
hfile->Write();

cout <<"wrote file "<<obj.x<<" "<<obj.y<<" "<<obj.z<<endl;
hfile->Close();

return 0;
}
void rudyRead ()
{
TFile *hfile =new TFile ("rudy.root");

TTree *Tr;
Tr = (TTree*)hfile->Get("T");
struct test_struc {Float_t x,y,z;} obj;
float var;
Tr->SetBranchAddress("vectors",&obj);

Tr->SetBranchAddress("variable",&var);
TH2F *h2 = new TH2F("h2","y vs x",50,0,10000,50,0,10000);
for (Int_t i=0;i<10000;i++){
Tr->GetEvent(i);
h2->Fill(obj.y, obj.x,1);
}
h2->Draw();
}

Rudolf Wedenig wrote:
>
> Hello,
>
> I want to read the content of a variable from a tree without using the
> T->Draw("var");
> option since for complex selections I get a strange behavior (i.e. the
> result of
> t->Draw("x:y","a>2 && b>3 && etc. ")
> depends on the order of the selection criteria.
> t->Draw("x:y","b>3 && a>2 && etc. ")
> leads to a different result).
>
> So I created a small test tree
>
> ************************************************************************
> #include <iostream.h>
> #include "TROOT.h"
> #include "TFile.h"
> #include "TTree.h"
>
> TROOT simple("simple","tree");
> int i;
> //______________________________________________________________________________
> int main ()
> {
> TFile *hfile =new TFile ("htree.root","RECREATE","tree");
>
> TTree *Tr;
> Tr = new TTree("T","ROOT tree ");
> struct test_struc {Float_t x,y,z;} obj;
> float var;
> Tr->Branch("vectors",&obj,"x:y:z");
>
> Tr->Branch("variable",&var,"var");
> for (i=0;i<10000;i++){
> obj.x=i+1.5;
> obj.y=i+1.5;
> obj.z=i*i;
> var=i;
> Tr->Fill();
> }
> Tr->Print();
> hfile->Write();
>
> cout <<"wrote file "<<obj.x<<" "<<obj.y<<" "<<obj.z<<endl;
> hfile->Close();
>
> return 0;
> }
> ********************************************************************
> and try to read i.e. for i= 9000 the value of "var".
> The only way I found is:
>
> TFile *mf = new TFile("htree.root");
> TTree *T=(TTree*) mf-> Get("T");
> T->Scan("var","",,1,9000);
>
> but this not want I really want, I would rather prefer a statement like
>
> float test ;
> test= T->Scan("var","",,1,9000);
> cout<<test<<endl;
>
> but this returns only zero (not surprising, but I am looking for
> something similar).
>
> Does anybody know how to do this?
>
> Rudi