Re: [ROOT] Extraction of data from Tree

From: Rene Brun (Rene.Brun@cern.ch)
Date: Mon Jun 05 2000 - 08:54:09 MEST


Hi Christian,
TTree::MakeCode does not support currently the case of one branch
with more than one leaf. Use TTree::MakeClass instead.
To generate the code via MakeClass, do:
  idTree.MakeClass();
This will generate idTree.h and idTree.C. Modify idTree.C as shown below
and do:
  .L idTree.C
  idTree t
  t.Loop();

void idTree::Loop()
{
//   In a Root session, you can do:
//      Root > .L idTree.C
//      Root > idTree t
//      Root > t.GetEntry(12); // Fill t data members with entry number 12
//      Root > t.Show();       // Show values of entry 12
//      Root > t.Show(16);     // Read and show values of entry 16
//      Root > t.Loop();       // Loop on all entries
//

//     This is the loop skeleton
//       To read only selected branches, Insert statements like:
// METHOD1:
//    fTree->SetBranchStatus("*",0);  // disable all branches
//    fTree->SetBranchStatus("branchname",1);  // activate branchname
// METHOD2: replace line
//    fTree->GetEntry(i);  // read all branches
//by  b_branchname->GetEntry(i); //read only this branch
   if (fTree == 0) return;

   Int_t nentries = Int_t(fTree->GetEntries());

   Int_t nbytes = 0, nb = 0;
   for (Int_t jentry=0; jentry<nentries;jentry++) {
      Int_t ientry = LoadTree(jentry); //in case of a TChain, ientry is the
entry number in the current file
      nb = fTree->GetEntry(jentry);   nbytes += nb;
      if(jentry<100) {
        printf("idx,x,y,z=
%8i,%8i,%8i,%8i\n",idBranch_idx,idBranch_x,idBranch_y,idBranch_z);
     }
   }
   printf("nentries= %8i, nbytes= %8i \n",nentries,nbytes);
}


Concerning your second question. You can have two or more Trees in the same
loop. In the example above, you can add a second tree (say t2) and in the loop,
do:
  t2->GetEntry(jentry); //if the loop index is the same, or
  t2->GetEntry(idx); //to read entry idx where idx is a variable of the 1st tree

You can connect the second Tree t2 in the function Loop, or better modify
the class idTree to add a member pointing to the second tree and connect it
in the class constructor or Init function.

Rene Brun


cstrato@EUnet.at wrote:
> 
> Dear ROOTers:
> 
> Recently, I have downloaded version 2.24/04 of ROOT, this really
> excellent framework,
> to my PowerBook G3/Wallstreet running LinuxPPC. Everything did work
> immediately.
> 
> Since I am new to ROOT I am trying some code and have already some
> questions:
> 
> 1, I have stored an ascii-table (5 columns: num, idx, x, y, z) in one
> TBranch
> of a TTree. Now I would like to extract "x:y:z" for each idx.
> 
> First I tried it interactively:
> root [0] .x TestTree.C
> root [3] idTree->Draw(">>myList","idx==2")
> root [4] idTree->SetEventList(myList)
> root [8] a=idTree->GetV1()
>  (Float_t*)0x30027010
> root [12] for(int i=0;i<40;i++) cout<<a[i]<<","
> root [13] cout<<endl
> 
> In this way I can extract "x,y,z" using GetV1(), GetV2(), GetV3().
> 
> However, when I used TTree->MakeCode("Code4idTree.cc"), and use
> printf() in the
> loop of the automatically generated code, I get strange values instead
> of my table.
> 
> Can someone help me and tell me what I did wrong?
> Below I enclose the files:
> a, TestTree.C: this file stores the data in the tree
> b, Code4idTree.cc: the automatically generated file with modified loop
> only
> c, Code4idTree1.cc: the automatically generated but modified file
> 
> 2, I have a second ascii-table (4 columns: x, y, data1, data2), which I
> have currently
> stored in a Ttuple as: data1[x][y] and data2[x][y]
> I would like to extract data1, data2 from this table for each idx of the
> first table.
> What is the best way to do this?
> Should I store data1, data2 differently?
> 
> Thank you very much in advance for your help
> Christian
> (Christian Stratowa, Vienna, Austria)
> 
> APPENDICES
> a, TestTree.C: this file stores the data in the tree
> {
> //   example of macro to read data from ascii file mytest.txt
> //   and create a root file with a tree.
>    gROOT->Reset();
> #include <iostream.h>
> 
>    ifstream in;
>    in.open("mytest.txt", ios::in);
> 
> Int_t num, idx, x, y, z;
>    typedef struct {
>       Int_t idx, x, y, z;
>    } INDEX;
> //   static INDEX id;
>    INDEX id;
> 
>    TFile *f = new TFile("TestTree.root","RECREATE");
>    TTree *idTree = new TTree("idTree","data from file mytest.txt");
>    TBranch *idBranch = idTree->Branch("idBranch",&id,"idx/I:x:y:z");
> 
> // read header line
>    Int_t nlines = 0;
>    if(nlines == 0) {
>       char header[81];
>       in.getline(header,80);
>       nlines++;
>    }
> 
>    while (1) {
>       in >> num >> idx >> x >> y >> z;
>       if (!in.good()) break;
>       id.idx = idx;
>       id.x = x;
>       id.y = y;
>       id.z = z;
>       if (nlines < 5) printf("idx=%8i,z=%8i,x=%8i,y=%8i\n",idx,z,x,y);
>       idTree->Fill();
>       nlines++;
>    }
>    printf(" found %d lines\n",nlines);
> 
>    in.close();
> 
>    f->Write();
> }
> 
> b, Code4idTree.cc: the automatically generated file with modified loop
> only
> {
> //////////////////////////////////////////////////////////
> //   This file has been automatically generated
> //     (Sun Jun  4 17:23:33 2000 by ROOT version2.24/04)
> //   from TTree idTree/data from file mytest.txt
> //   found on file: TestTree.root
> //////////////////////////////////////////////////////////
> 
> //Reset ROOT and connect tree file
> //   gROOT->Reset();
>    TFile *f =
> (TFile*)gROOT->GetListOfFiles()->FindObject("TestTree.root");
>    if (!f) {
>       f = new TFile("TestTree.root");
>    }
>    TTree *idTree = (TTree*)gDirectory->Get("idTree");
> 
> //Declaration of leaves types
>    Int_t           idBranch_idx;
>    Int_t           idBranch_x;
>    Int_t           idBranch_y;
>    Int_t           idBranch_z;
> 
> //Set branch addresses
>    idTree->SetBranchAddress("idBranch",&idBranch_idx);
>    idTree->SetBranchAddress("idBranch",&idBranch_x);
>    idTree->SetBranchAddress("idBranch",&idBranch_y);
>    idTree->SetBranchAddress("idBranch",&idBranch_z);
> 
> //     This is the loop skeleton
> //       To read only selected branches, Insert statements like:
> // idTree->SetBranchStatus("*",0);  // disable all branches
> // TTreePlayer->SetBranchStatus("branchname",1);  // activate branchname
> 
>    Int_t nentries = idTree->GetEntries();
> 
>    Int_t nbytes = 0;
>    for (Int_t i=0; i<nentries;i++) {
>       nbytes += idTree->GetEntry(i);
>       if(i<100) {
>         printf("idx,x,y,z=
> %8i,%8i,%8i,%8i\n",idBranch_idx,idBranch_x,idBranch_y,idBranch_z);
> //       printf("idx,x,y,z= \n");
>       }
>    }
>    printf("nentries= %8i, nbytes= %8i \n",nentries,nbytes);
> }
> 
> c, Code4idTree1.cc: the automatically generated but modified file
> {
> //////////////////////////////////////////////////////////
> //   This file has been automatically generated
> //     (Sun Jun  4 17:23:33 2000 by ROOT version2.24/04)
> //   from TTree idTree/data from file mytest.txt
> //   found on file: TestTree.root
> //////////////////////////////////////////////////////////
> 
> //Reset ROOT and connect tree file
> //   gROOT->Reset();
>    TFile *f =
> (TFile*)gROOT->GetListOfFiles()->FindObject("TestTree.root");
>    if (!f) {
>       f = new TFile("TestTree.root");
>    }
>    TTree *idTree = (TTree*)gDirectory->Get("idTree");
> //   TBranch *idBranch = idTree->GetBranch("idBranch");
> 
> //Declaration of leaves types
>    typedef struct {
>       Int_t idx, x, y, z;
>    } INDEX;
>    INDEX id;
> 
> //Set branch addresses
>    idTree->SetBranchAddress("idBranch",&id.idx);
>    idTree->SetBranchAddress("idBranch",&id.x);
>    idTree->SetBranchAddress("idBranch",&id.y);
>    idTree->SetBranchAddress("idBranch",&id.z);
> 
> //     This is the loop skeleton
> //       To read only selected branches, Insert statements like:
> // idTree->SetBranchStatus("*",0);  // disable all branches
> // TTreePlayer->SetBranchStatus("branchname",1);  // activate branchname
> 
>    Int_t nentries = idTree->GetEntries();
> 
>    Int_t nbytes = 0;
>    for (Int_t i=0; i<nentries;i++) {
>       nbytes += idTree->GetEntry(i);
> //      nbytes += idBranch->GetEntry(i);
>       if(i<100) {
>         printf("idx,x,y,z= %8i,%8i,%8i,%8i\n",id.idx,id.x,id.y,id.z);
>       }
>    }
>    printf("nentries= %8i, nbytes= %8i \n",nentries,nbytes);
> }



This archive was generated by hypermail 2b29 : Tue Jan 02 2001 - 11:50:27 MET