Copytree 2

Copy a subset of a Tree to a new Tree, one branch in a separate file.

One branch of the new Tree is written to a separate file The input file has been generated by the program in $ROOTSYS/test/Event with the command 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:05 AM.

In [1]:
.! $ROOTSYS/test/eventexe 1000 1 1 1
event:0, rtime=0.557297 s
event:100, rtime=0.253062 s
event:200, rtime=0.241891 s
event:300, rtime=0.243029 s
event:400, rtime=0.224583 s
event:500, rtime=0.244231 s
event:600, rtime=0.246809 s
event:700, rtime=0.235484 s
event:800, rtime=0.264974 s
event:900, rtime=0.083917 s
******************************************************************************
*Tree    :T         : An example of a ROOT tree                              *
*Entries :     1000 : Total =        81215880 bytes  File  Size =   37645446 *
*        :          : 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.626455 seconds, CpuTime=3.010000 seconds
compression level=1, split=1, arg4=1, IMT=0, compression algorithm=1
You write 21.681192 Mbytes/Realtime seconds
You write 26.121551 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);

Activate only four of them

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

Create a new file + a clone of old tree header. do not copy events

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

Divert branch fh to a separate file and copy all events

In [6]:
newtree->GetBranch("fH")->SetFile("small_fH.root");
newtree->CopyEntries(oldtree);

newtree->Print();
newfile.Write();
******************************************************************************
*Tree    :T         : An example of a ROOT tree                              *
*Entries :     1000 : Total =        81121436 bytes  File  Size =   30265959 *
*        :          : Tree compression factor =   2.16                       *
******************************************************************************
*Branch  :event                                                              *
*Entries :     1000 : BranchElement (see below)                              *
*............................................................................*
*Br    0 :TObject   : BASE                                                   *
*Entries :     1000 : Total  Size=      14742 bytes  File Size  =       1340 *
*Baskets :        1 : Basket Size=      51200 bytes  Compression=   8.42     *
*............................................................................*
*Br    1 :fType[20] : Char_t                                                 *
*Entries :     1000 : Total  Size=      24752 bytes  File Size  =       1506 *
*Baskets :        1 : Basket Size=      51200 bytes  Compression=  12.80     *
*............................................................................*
*Br    2 :fEventName : char*                                                 *
*Entries :     1000 : Total  Size=      23653 bytes  File Size  =       3735 *
*Baskets :        1 : Basket Size=      51200 bytes  Compression=   4.92     *
*............................................................................*
*Br    3 :fNtrack   : Int_t                                                  *
*Entries :     1000 : Total  Size=       4730 bytes  File Size  =        972 *
*Baskets :        1 : Basket Size=      51200 bytes  Compression=   3.37     *
*............................................................................*
*Br    4 :fNseg     : Int_t                                                  *
*Entries :     1000 : Total  Size=       4716 bytes  File Size  =       1435 *
*Baskets :        1 : Basket Size=      51200 bytes  Compression=   2.28     *
*............................................................................*
*Br    5 :fNvertex  : Int_t                                                  *
*Entries :     1000 : Total  Size=       4737 bytes  File Size  =        932 *
*Baskets :        1 : Basket Size=      51200 bytes  Compression=   3.51     *
*............................................................................*
*Br    6 :fFlag     : UInt_t                                                 *
*Entries :     1000 : Total  Size=       4716 bytes  File Size  =        444 *
*Baskets :        1 : Basket Size=      51200 bytes  Compression=   7.36     *
*............................................................................*
*Br    7 :fTemperature : Double32_t                                          *
*Entries :     1000 : Total  Size=       4765 bytes  File Size  =       2630 *
*Baskets :        1 : Basket Size=      51200 bytes  Compression=   1.25     *
*............................................................................*
*Br    8 :fMeasures[10] : Int_t                                              *
*Entries :     1000 : Total  Size=      44780 bytes  File Size  =      11442 *
*Baskets :        1 : Basket Size=      65024 bytes  Compression=   3.08     *
*............................................................................*
*Br    9 :fMatrix[4][4] : Double32_t                                         *
*Entries :     1000 : Total  Size=      68867 bytes  File Size  =      33549 *
*Baskets :        2 : Basket Size=      97280 bytes  Compression=   1.63     *
*............................................................................*
*Br   10 :fClosestDistance : Double32_t fClosestDistance[fNvertex]           *
*Entries :     1000 : Total  Size=      37512 bytes  File Size  =      16798 *
*Baskets :        1 : Basket Size=      55296 bytes  Compression=   1.74     *
*............................................................................*
*Br   11 :fEvtHdr   : EventHeader                                            *
*Entries :     1000 : Total  Size=      22742 bytes  File Size  =       3247 *
*Baskets :        1 : Basket Size=      51200 bytes  Compression=   5.44     *
*............................................................................*
*Br   12 :fTracks   : TClonesArray*                                          *
*Entries :     1000 : Total  Size=   75825324 bytes  File Size  =   29611332 *
*Baskets :        3 : Basket Size=   25600000 bytes  Compression=   2.05     *
*............................................................................*
*Br   13 :fHighPt   : TRefArray*                                             *
*Entries :     1000 : Total  Size=    1506705 bytes  File Size  =     458972 *
*Baskets :        2 : Basket Size=   25600000 bytes  Compression=   2.62     *
*............................................................................*
*Br   14 :fMuons    : TRefArray*                                             *
*Entries :     1000 : Total  Size=      71445 bytes  File Size  =       5438 *
*Baskets :        2 : Basket Size=      99840 bytes  Compression=  10.33     *
*............................................................................*
*Br   15 :fLastTrack : TRef                                                  *
*Entries :     1000 : Total  Size=      16763 bytes  File Size  =       2662 *
*Baskets :        1 : Basket Size=      51200 bytes  Compression=   4.84     *
*............................................................................*
*Br   16 :fWebHistogram : TRef                                               *
*Entries :     1000 : Total  Size=      16784 bytes  File Size  =       1281 *
*Baskets :        1 : Basket Size=      51200 bytes  Compression=  10.06     *
*............................................................................*
*Br   17 :fH        : TH1F*                                                  *
*Entries :     1000 : Total  Size=     956798 bytes  File Size  =      64481 *
*Baskets :        2 : Basket Size=   25600000 bytes  Compression=  11.86     *
*............................................................................*
*Br   18 :fTriggerBits : TBits                                               *
*Entries :     1000 : Total  Size=      41221 bytes  File Size  =      10023 *
*Baskets :        1 : Basket Size=      60416 bytes  Compression=   3.25     *
*............................................................................*
*Br   19 :fIsValid  : Bool_t                                                 *
*Entries :     1000 : Total  Size=       1737 bytes  File Size  =        100 *
*Baskets :        1 : Basket Size=      51200 bytes  Compression=   8.72     *
*............................................................................*
*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     *
*............................................................................*