Copytree

Copy a subset of a Tree to a new Tree

The input file has been generated by the program in $ROOTSYS/test/Event with Event 1000 1 1 1.

Author: Rene Brun
This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Monday, February 17, 2020 at 04:04 AM.

In [1]:
.! $ROOTSYS/test/eventexe 1000 1 1 1
event:0, rtime=0.626848 s
event:100, rtime=0.254542 s
event:200, rtime=0.234184 s
event:300, rtime=0.231146 s
event:400, rtime=0.247768 s
event:500, rtime=0.238938 s
event:600, rtime=0.246675 s
event:700, rtime=0.263096 s
event:800, rtime=0.298271 s
event:900, rtime=0.084191 s
******************************************************************************
*Tree    :T         : An example of a ROOT tree                              *
*Entries :     1000 : Total =        81215880 bytes  File  Size =   37645431 *
*        :          : Tree compression factor =   2.16                       *
******************************************************************************
*Branch  :event                                                              *
*Entries :     1000 : BranchElement (see below)                              *
*............................................................................*
*Br    0 :TObject   : BASE                                                   *
*Entries :     1000 : Total  Size=      14667 bytes  File Size  =       1807 *
*Baskets :        2 : Basket Size=      51200 bytes  Compression=   7.84     *
*............................................................................*
*Br    1 :fType[20] : Char_t                                                 *
*Entries :     1000 : Total  Size=      24760 bytes  File Size  =       2239 *
*Baskets :        3 : Basket Size=      51200 bytes  Compression=  10.83     *
*............................................................................*
*Br    2 :fEventName : char*                                                 *
*Entries :     1000 : Total  Size=      23661 bytes  File Size  =       4837 *
*Baskets :        3 : Basket Size=      51200 bytes  Compression=   4.78     *
*............................................................................*
*Br    3 :fNtrack   : Int_t                                                  *
*Entries :     1000 : Total  Size=       4651 bytes  File Size  =       1303 *
*Baskets :        2 : Basket Size=      51200 bytes  Compression=   3.18     *
*............................................................................*
*Br    4 :fNseg     : Int_t                                                  *
*Entries :     1000 : Total  Size=       4639 bytes  File Size  =       1897 *
*Baskets :        2 : Basket Size=      51200 bytes  Compression=   2.18     *
*............................................................................*
*Br    5 :fNvertex  : Int_t                                                  *
*Entries :     1000 : Total  Size=       4657 bytes  File Size  =       1264 *
*Baskets :        2 : Basket Size=      51200 bytes  Compression=   3.28     *
*............................................................................*
*Br    6 :fFlag     : UInt_t                                                 *
*Entries :     1000 : Total  Size=       4639 bytes  File Size  =        638 *
*Baskets :        2 : Basket Size=      51200 bytes  Compression=   6.49     *
*............................................................................*
*Br    7 :fTemperature : Double32_t                                          *
*Entries :     1000 : Total  Size=       4681 bytes  File Size  =       3409 *
*Baskets :        2 : Basket Size=      51200 bytes  Compression=   1.22     *
*............................................................................*
*Br    8 :fMeasures[10] : Int_t                                              *
*Entries :     1000 : Total  Size=      44877 bytes  File Size  =      14728 *
*Baskets :        4 : Basket Size=      51200 bytes  Compression=   3.01     *
*............................................................................*
*Br    9 :fMatrix[4][4] : Double32_t                                         *
*Entries :     1000 : Total  Size=      68964 bytes  File Size  =      42323 *
*Baskets :        5 : Basket Size=      51200 bytes  Compression=   1.62     *
*............................................................................*
*Br   10 :fClosestDistance : Double32_t fClosestDistance[fNvertex]           *
*Entries :     1000 : Total  Size=      37612 bytes  File Size  =      21554 *
*Baskets :        4 : Basket Size=      51200 bytes  Compression=   1.71     *
*............................................................................*
*Br   11 :fEvtHdr   : EventHeader                                            *
*Entries :     1000 : Total  Size=      22750 bytes  File Size  =       4439 *
*Baskets :        3 : Basket Size=      51200 bytes  Compression=   5.01     *
*............................................................................*
*Br   12 :fTracks   : TClonesArray*                                          *
*Entries :     1000 : Total  Size=   75907240 bytes  File Size  =   36784634 *
*Baskets :      801 : Basket Size=   25600000 bytes  Compression=   2.06     *
*............................................................................*
*Br   13 :fHighPt   : TRefArray*                                             *
*Entries :     1000 : Total  Size=    1514544 bytes  File Size  =     589634 *
*Baskets :       81 : Basket Size=     632832 bytes  Compression=   2.56     *
*............................................................................*
*Br   14 :fMuons    : TRefArray*                                             *
*Entries :     1000 : Total  Size=      71535 bytes  File Size  =       9289 *
*Baskets :        5 : Basket Size=      51200 bytes  Compression=   7.65     *
*............................................................................*
*Br   15 :fLastTrack : TRef                                                  *
*Entries :     1000 : Total  Size=      16771 bytes  File Size  =       3588 *
*Baskets :        3 : Basket Size=      51200 bytes  Compression=   4.53     *
*............................................................................*
*Br   16 :fWebHistogram : TRef                                               *
*Entries :     1000 : Total  Size=      16792 bytes  File Size  =       1847 *
*Baskets :        3 : Basket Size=      51200 bytes  Compression=   8.80     *
*............................................................................*
*Br   17 :fH        : TH1F*                                                  *
*Entries :     1000 : Total  Size=     961299 bytes  File Size  =      96581 *
*Baskets :       51 : Basket Size=     404480 bytes  Compression=   9.94     *
*............................................................................*
*Br   18 :fTriggerBits : TBits                                               *
*Entries :     1000 : Total  Size=      41317 bytes  File Size  =      13039 *
*Baskets :        4 : Basket Size=      51200 bytes  Compression=   3.13     *
*............................................................................*
*Br   19 :fIsValid  : Bool_t                                                 *
*Entries :     1000 : Total  Size=       1657 bytes  File Size  =        193 *
*Baskets :        2 : Basket Size=      51200 bytes  Compression=   5.93     *
*............................................................................*
*Br   20 :TRefTable : List of branch numbers with referenced objects         *
*Entries :     1000 : Total  Size=    2422133 bytes  File Size  =      33640 *
*Baskets :       76 : Basket Size=      32000 bytes  Compression=  71.07     *
*............................................................................*

1000 events and 78625869 bytes processed.
RealTime=3.494228 seconds, CpuTime=3.080000 seconds
compression level=1, split=1, arg4=1, IMT=0, compression algorithm=1
You write 22.501642 Mbytes/Realtime seconds
You write 25.527880 Mbytes/Cputime seconds

Load the library at macro parsing time: we need this to use its content in the code

In [2]:
R__LOAD_LIBRARY($ROOTSYS/test/libEvent.so)
In [3]:
TString dir = "$ROOTSYS/test/Event.root";
gSystem->ExpandPathName(dir);
const auto filename = gSystem->AccessPathName(dir) ? "./Event.root" : "$ROOTSYS/test/Event.root";

TFile oldfile(filename);
TTree *oldtree;
oldfile.GetObject("T", oldtree);

Deactivate all branches

In [4]:
oldtree->SetBranchStatus("*", 0);

Activate only four of them

In [5]:
for (auto activeBranchName : {"event", "fNtrack", "fNseg", "fH"})
   oldtree->SetBranchStatus(activeBranchName, 1);

Create a new file + a clone of old tree in new file

In [6]:
TFile newfile("small.root", "recreate");
auto newtree = oldtree->CloneTree();

newtree->Print();
newfile.Write();
******************************************************************************
*Tree    :T         : An example of a ROOT tree                              *
*Entries :     1000 : Total =          975986 bytes  File  Size =      66888 *
*        :          : Tree compression factor =  11.53                       *
******************************************************************************
*Branch  :event                                                              *
*Entries :     1000 : BranchElement (see below)                              *
*............................................................................*
*Br    0 :fNtrack   : Int_t                                                  *
*Entries :     1000 : Total  Size=       4730 bytes  File Size  =        972 *
*Baskets :        1 : Basket Size=       3072 bytes  Compression=   3.37     *
*............................................................................*
*Br    1 :fNseg     : Int_t                                                  *
*Entries :     1000 : Total  Size=       4716 bytes  File Size  =       1435 *
*Baskets :        1 : Basket Size=       3072 bytes  Compression=   2.28     *
*............................................................................*
*Br    2 :fH        : TH1F*                                                  *
*Entries :     1000 : Total  Size=     956785 bytes  File Size  =      64481 *
*Baskets :        2 : Basket Size=     773120 bytes  Compression=  11.86     *
*............................................................................*
*Br    3 :TRefTable : List of branch numbers with referenced objects         *
*Entries :     1000 : Total  Size=       8613 bytes  One basket in memory    *
*Baskets :        0 : Basket Size=      32000 bytes  Compression=   1.00     *
*............................................................................*