Reading a single branch created by TClonesArray* member of Class from Tree

Hi…
I have a TTree, that has following structure

root [3] ImageTree->Print()
******************************************************************************
*Tree    :ImageTree : Focal Images                                           *
*Entries :     1000 : Total =       776003727 bytes  File  Size =   42963802 *
*        :          : Tree compression factor =  18.08                       *
******************************************************************************
*Branch  :FocalImage01                                                       *
*Entries :     1000 : BranchElement (see below)                              *
*............................................................................*
*Br    0 :fUniqueID : UInt_t                                                 *
*Entries :     1000 : Total  Size=       4656 bytes  File Size  =        234 *
*Baskets :        2 : Basket Size=      51200 bytes  Compression=  17.79     *
*............................................................................*
*Br    1 :fBits     : UInt_t                                                 *
*Entries :     1000 : Total  Size=       8648 bytes  File Size  =       1566 *
*Baskets :        2 : Basket Size=      51200 bytes  Compression=   5.22     *
*............................................................................*
*Br    2 :bunches   : Int_t                                                  *
*Entries :     1000 : Total  Size=       4644 bytes  File Size  =       2263 *
*Baskets :        2 : Basket Size=      51200 bytes  Compression=   1.84     *
*............................................................................*
*Br    3 :photons   : Double_t                                               *
*Entries :     1000 : Total  Size=       8644 bytes  File Size  =       5753 *
*Baskets :        2 : Basket Size=      51200 bytes  Compression=   1.42     *
*............................................................................*
*Br    4 :lasttoa   : Float_t                                                *
*Entries :     1000 : Total  Size=       4644 bytes  File Size  =       3805 *
*Baskets :        2 : Basket Size=      51200 bytes  Compression=   1.09     *
*............................................................................*
*Br    5 :firsttoa  : Float_t                                                *
*Entries :     1000 : Total  Size=       4650 bytes  File Size  =       3664 *
*Baskets :        2 : Basket Size=      51200 bytes  Compression=   1.14     *
*............................................................................*
*Br    6 :max_csk_bunches : UInt_t                                           *
*Entries :     1000 : Total  Size=       4692 bytes  File Size  =        256 *
*Baskets :        2 : Basket Size=      51200 bytes  Compression=  16.30     *
*............................................................................*
*Br    7 :ptrFocalBunch : Int_t ptrFocalBunch_                               *
*Entries :     1000 : Total  Size=     157826 bytes  File Size  =        252 *
*Baskets :        2 : Basket Size=      32000 bytes  Compression=  16.55     *
*............................................................................*
*Br    8 :ptrFocalBunch.fUniqueID : UInt_t fUniqueID[ptrFocalBunch_]         *
*Entries :     1000 : Total  Size=  100106183 bytes  File Size  =     549252 *
*Baskets :      827 : Basket Size=    8256000 bytes  Compression= 182.23     *
*............................................................................*
*Br    9 :ptrFocalBunch.fBits : UInt_t fBits[ptrFocalBunch_]                 *
*Entries :     1000 : Total  Size=  100102859 bytes  File Size  =     597336 *
*Baskets :      827 : Basket Size=    8256000 bytes  Compression= 167.55     *
*............................................................................*
*Br   10 :ptrFocalBunch.x : Float_t x[ptrFocalBunch_]                        *
*Entries :     1000 : Total  Size=  100099535 bytes  File Size  =   10497276 *
*Baskets :      827 : Basket Size=    8255488 bytes  Compression=   9.53     *
*............................................................................*
*Br   11 :ptrFocalBunch.y : Float_t y[ptrFocalBunch_]                        *
*Entries :     1000 : Total  Size=  100099535 bytes  File Size  =   10476949 *
*Baskets :      827 : Basket Size=    8255488 bytes  Compression=   9.55     *
*............................................................................*
*Br   12 :ptrFocalBunch.ctime : Float_t ctime[ptrFocalBunch_]                *
*Entries :     1000 : Total  Size=  100102859 bytes  File Size  =    9633472 *
*Baskets :      827 : Basket Size=    8256000 bytes  Compression=  10.39     *
*............................................................................*
*Br   13 :ptrFocalBunch.photons : Float_t photons[ptrFocalBunch_]            *
*Entries :     1000 : Total  Size=  100104521 bytes  File Size  =    4630539 *
*Baskets :      827 : Basket Size=    8256000 bytes  Compression=  21.61     *
*............................................................................*
*Br   14 :ptrFocalBunch.wvlength : Float_t wvlength[ptrFocalBunch_]          *
*Entries :     1000 : Total  Size=  100105352 bytes  File Size  =    5935059 *
*Baskets :      827 : Basket Size=    8256000 bytes  Compression=  16.86     *
*............................................................................*
*Br   15 :ptrFocalBunch.ipix : Short_t ipix[ptrFocalBunch_]                  *
*Entries :     1000 : Total  Size=   50102028 bytes  File Size  =     335719 *
*Baskets :      827 : Basket Size=    4131840 bytes  Compression= 149.19     *
*............................................................................*
*Br   16 :ptrFocalBunch.isnoise : Bool_t isnoise[ptrFocalBunch_]             *
*Entries :     1000 : Total  Size=   25104521 bytes  File Size  =     225879 *
*Baskets :      827 : Basket Size=    2070528 bytes  Compression= 111.07     *
*............................................................................*
*Br   17 :CORSIKA_BUNCHES : nbunches/I                                       *
*Entries :     1000 : Total  Size=       4667 bytes  File Size  =       2332 *
*Baskets :        2 : Basket Size=      51200 bytes  Compression=   1.79     *
*............................................................................*
*Br   18 :Event     : uevent/i                                               *
*Entries :     1000 : Total  Size=       4631 bytes  File Size  =       1661 *
*Baskets :        2 : Basket Size=      51200 bytes  Compression=   2.50     *
*............................................................................*

I want to read only 2 branches in one of analysis “ptrFocalBunch.ctime” and “ptrFocalBunch.ipix”. How to do that ?
Currently I am reading whole “FocalImage01” branch. How to avoid it ?

Hi,

You could try something like this:

   TTreeReader myReader("ImageTree", f);
   TTreeReaderArray<Float_t> focal_bunch_ctime(myReader, "ptrFocalBunch.ctime");
   TTreeReaderArray<Short_t> focal_bunch_ipix(myReader, "ptrFocalBunch.ipix");

   while (myReader.Next()) {
      for (int i = 0; i < focal_bunch_ctime.GetSize(); ++i) {
            cout << focal_bunch_ctime[i] << endl;
      }
      for (int j = 0; j < focal_bunch_ipix.GetSize(); ++j) {
            cout << focal_bunch_ipix[j] << endl;
      }
   }

Cheers, Bertrand.

Hi,
Thanks for quick reply.
TTreeReader seems to be part of ROOT v >= 6.00. I am using v 5.34. Is there some other way to do it in v5.34.
I just noticed not only TClonesArray* but I am not able to access any other branch individually in the tree.
For e.g.

root [0] .L ../lib/libMSimIO.so 
root [1] TFile f("NO_CEFFIC.root")
root [2] TTree *tr = (TTree*)f.Get("ImageTree;2")
root [4] TBranch *nb_br = tr->GetBranch("bunches")
root [5] Int_t nb ;
root [6] nb_br->SetAddress(&nb)
root [7] nb_br->GetEntry(3)
(Int_t)4
root [8] nb
(Int_t)0

The value of nb is not right one.

Hi,

To use SetBranchAddress() with simple types (e.g. double, int) instead of objects (e.g. std::vector), you should call TTree::SetMakeClass(). Something like:

root [2] TTree *tr = (TTree*)f.Get("ImageTree;2")
root [3] tr->SetMakeClass(1);
root [4] TBranch *nb_br = tr->GetBranch("bunches")

Cheers, Bertrand.

Hi,
Thanks.
That solves the problem for basic data members of class MReflector.
How to solve TClonesArray* part like e.g. accessing branch ptrFocalBunch.ctime" . ?
I have created a class structure similar to Event class in example “$ROOTSYS/root/test/Event.cxx”. I have class MFocalBunch that have members like x,y, ctime, ipix etc. And a class MReflector contains TClonesArray* that holds TClonesArray(“MFocalBunch”, MAX_BUNCHES). While writting .root file I am creating a branch for MReflector with splitting, to TTree “ImageTree” and writting it to ImageTree.
Also does above solution work when writting a standalone code ?

Hi,

Could you provide a simple tree showing the structure of your data? It would be much faster…

Cheers, Bertrand.