Copytree 3

Example of Root macro to copy a subset of a Tree to a new Tree, selecting entries.

Only selected entries are copied to the new Tree. The input file has been generated by the program in $ROOTSYS/test/Event with Event 1000 1 99 1

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

In [1]:
.! $ROOTSYS/test/eventexe 1000 1 1 1
event:0, rtime=0.579129 s
event:100, rtime=0.267457 s
event:200, rtime=0.266200 s
event:300, rtime=0.264432 s
event:400, rtime=0.259910 s
event:500, rtime=0.260565 s
event:600, rtime=0.261987 s
event:700, rtime=0.269674 s
event:800, rtime=0.291752 s
event:900, rtime=0.086294 s
******************************************************************************
*Tree    :T         : An example of a ROOT tree                              *
*Entries :     1000 : Total =        81215880 bytes  File  Size =   37645439 *
*        :          : 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=4.064812 seconds, CpuTime=3.230000 seconds
compression level=1, split=1, arg4=1, IMT=0, compression algorithm=1
You write 19.343052 Mbytes/Realtime seconds
You write 24.342375 Mbytes/Cputime seconds
In [2]:
R__LOAD_LIBRARY($ROOTSYS/test/libEvent.so)

Get old file, old tree and set top branch address

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);

const auto nentries = oldtree->GetEntries();

Event *event = nullptr;
oldtree->SetBranchAddress("event", &event);

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

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

for (auto i : ROOT::TSeqI(nentries)) {
   oldtree->GetEntry(i);
   if (event->GetNtrack() > 605)
      newtree->Fill();
   event->Clear();
}

newtree->Print();
newfile.Write();
******************************************************************************
*Tree    :T         : An example of a ROOT tree                              *
*Entries :      123 : Total =        10127094 bytes  File  Size =       3837 *
*        :          : Tree compression factor =  74.81                       *
******************************************************************************
*Branch  :event                                                              *
*Entries :      123 : BranchElement (see below)                              *
*............................................................................*
*Br    0 :TObject   : BASE                                                   *
*Entries :      123 : Total  Size=       2381 bytes  One basket in memory    *
*Baskets :        0 : Basket Size=      51200 bytes  Compression=   1.00     *
*............................................................................*
*Br    1 :fType[20] : Char_t                                                 *
*Entries :      123 : Total  Size=       3619 bytes  One basket in memory    *
*Baskets :        0 : Basket Size=      51200 bytes  Compression=   1.00     *
*............................................................................*
*Br    2 :fEventName : char*                                                 *
*Entries :      123 : Total  Size=       3487 bytes  One basket in memory    *
*Baskets :        0 : Basket Size=      51200 bytes  Compression=   1.00     *
*............................................................................*
*Br    3 :fNtrack   : Int_t                                                  *
*Entries :      123 : Total  Size=       1147 bytes  One basket in memory    *
*Baskets :        0 : Basket Size=      51200 bytes  Compression=   1.00     *
*............................................................................*
*Br    4 :fNseg     : Int_t                                                  *
*Entries :      123 : Total  Size=       1135 bytes  One basket in memory    *
*Baskets :        0 : Basket Size=      51200 bytes  Compression=   1.00     *
*............................................................................*
*Br    5 :fNvertex  : Int_t                                                  *
*Entries :      123 : Total  Size=       1153 bytes  One basket in memory    *
*Baskets :        0 : Basket Size=      51200 bytes  Compression=   1.00     *
*............................................................................*
*Br    6 :fFlag     : UInt_t                                                 *
*Entries :      123 : Total  Size=       1135 bytes  One basket in memory    *
*Baskets :        0 : Basket Size=      51200 bytes  Compression=   1.00     *
*............................................................................*
*Br    7 :fTemperature : Double32_t                                          *
*Entries :      123 : Total  Size=       1177 bytes  One basket in memory    *
*Baskets :        0 : Basket Size=      51200 bytes  Compression=   1.00     *
*............................................................................*
*Br    8 :fMeasures[10] : Int_t                                              *
*Entries :      123 : Total  Size=       6103 bytes  One basket in memory    *
*Baskets :        0 : Basket Size=      51200 bytes  Compression=   1.00     *
*............................................................................*
*Br    9 :fMatrix[4][4] : Double32_t                                         *
*Entries :      123 : Total  Size=       9053 bytes  One basket in memory    *
*Baskets :        0 : Basket Size=      51200 bytes  Compression=   1.00     *
*............................................................................*
*Br   10 :fClosestDistance : Double32_t fClosestDistance[fNvertex]           *
*Entries :      123 : Total  Size=       5615 bytes  One basket in memory    *
*Baskets :        0 : Basket Size=      51200 bytes  Compression=   1.00     *
*............................................................................*
*Br   11 :fEvtHdr   : EventHeader                                            *
*Entries :      123 : Total  Size=       3365 bytes  One basket in memory    *
*Baskets :        0 : Basket Size=      51200 bytes  Compression=   1.00     *
*............................................................................*
*Br   12 :fTracks   : TClonesArray*                                          *
*Entries :      123 : Total  Size=    9455395 bytes  One basket in memory    *
*Baskets :        0 : Basket Size=   25600000 bytes  Compression=   1.00     *
*............................................................................*
*Br   13 :fHighPt   : TRefArray*                                             *
*Entries :      123 : Total  Size=     189014 bytes  One basket in memory    *
*Baskets :        0 : Basket Size=     632832 bytes  Compression=   1.00     *
*............................................................................*
*Br   14 :fMuons    : TRefArray*                                             *
*Entries :      123 : Total  Size=       9128 bytes  One basket in memory    *
*Baskets :        0 : Basket Size=      51200 bytes  Compression=   1.00     *
*............................................................................*
*Br   15 :fLastTrack : TRef                                                  *
*Entries :      123 : Total  Size=       2645 bytes  One basket in memory    *
*Baskets :        0 : Basket Size=      51200 bytes  Compression=   1.00     *
*............................................................................*
*Br   16 :fWebHistogram : TRef                                               *
*Entries :      123 : Total  Size=       2663 bytes  One basket in memory    *
*Baskets :        0 : Basket Size=      51200 bytes  Compression=   1.00     *
*............................................................................*
*Br   17 :fH        : TH1F*                                                  *
*Entries :      123 : Total  Size=     118217 bytes  One basket in memory    *
*Baskets :        0 : Basket Size=     404480 bytes  Compression=   1.00     *
*............................................................................*
*Br   18 :fTriggerBits : TBits                                               *
*Entries :      123 : Total  Size=       5674 bytes  One basket in memory    *
*Baskets :        0 : Basket Size=      51200 bytes  Compression=   1.00     *
*............................................................................*
*Br   19 :fIsValid  : Bool_t                                                 *
*Entries :      123 : Total  Size=        784 bytes  One basket in memory    *
*Baskets :        0 : Basket Size=      51200 bytes  Compression=   1.00     *
*............................................................................*
*Br   20 :TRefTable : List of branch numbers with referenced objects         *
*Entries :      123 : Total  Size=     302390 bytes  File Size  =       3837 *
*Baskets :        9 : Basket Size=      32000 bytes  Compression=  74.81     *
*............................................................................*