Re: [ROOT] reading NTuples

From: Suzanne Panacek (spanacek@fnal.gov)
Date: Wed Apr 25 2001 - 21:11:31 MEST


Hi,
I am just working on documenting this in the Users Guide, so I'll take a
shot at explaining this.

To read a tree you need to:
1) get the tree from the file
2) create variables to hold the read value
3) use TTree::SetBranchAddress to associate an adress with the branch
4) call Ttree::GetEntry(n) to get the nth entry.

In your case, since you have several trees with different data that you
would like to analyze together you can use the Friends feature (new in ROOT
3.0) . See http://root.cern.ch/root/htmldoc/TTree.html#TTree:AddFriend

Here is an extract from $ROOTSYS/tutorials/tree1.C (in the latest CVS) which
reads 5 branches of one tree and fills two histograms with the results:
...
   TFile *f = new TFile("tree1.root");
   TTree *t1 = (TTree*)f->Get("t1");
   Float_t px, py, pz;
   Double_t random;
   Int_t ev;
   t1->SetBranchAddress("px",&px);
   t1->SetBranchAddress("py",&py);
   t1->SetBranchAddress("pz",&pz);
   t1->SetBranchAddress("random",&random);
   t1->SetBranchAddress("ev",&ev);

   // two histograms
   TH1F *hpx   = new TH1F("hpx","px distribution",100,-3,3);
   TH2F *hpxpy = new TH2F("hpxpy","py vs px",30,-3,3,30,-3,3);

   // all entries and fill the histograms
   Int_t nentries = (Int_t)t1->GetEntries();
   for (Int_t i=0;i<nentries;i++) {
     t1->GetEntry(i);
     hpx->Fill(px);
     hpxpy->Fill(px,py);
  }

--------------

Suzanne Panacek

----- Original Message -----
From: "Disco Jason" <jrieger@indiana.edu>
To: "root talk" <roottalk@pcroot.cern.ch>
Sent: Wednesday, April 25, 2001 1:34 PM
Subject: [ROOT] reading NTuples


> hey all,
> I have a simple enough question.  I have a root file that contains
> 4 NTuples, one for events, one for tracks, one for showers, and one for
> events info. After I read in the file and ls, I get the following list
> of ntuples:
>
> File** /home/jrieger/eventsr.root
>  TFile* /home/jrieger/eventsr.root
>   KEY: TNtuple evt;1 Event Ntuple
>   KEY: TNtuple trk;1 Track Ntuple
>   KEY: TNtuple shw;1 Shower Ntuple
>   KEY: TNtuple evtinfo;1 Event Info Ntuple
>
> What I am wondering is, if I want to loop through the events and get the
> tracks for each event, how to I extract the information from the ntuple,
> i.e. read the ntuple.  Any help would be appreciated.  Thanks.  -Jason
>
>



This archive was generated by hypermail 2b29 : Tue Jan 01 2002 - 17:50:43 MET