Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
tree112_copy.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_tree
3/// \notebook -nodraw
4/// Example of Root macro to copy a subset of a Tree to a new Tree, selecting entries.
5///
6/// Only selected entries are copied to the new Tree.
7/// The input file has been generated by the program in `$ROOTSYS/test/Event`
8/// with `Event 1000 1 99 1`
9///
10/// \macro_code
11///
12/// \author Rene Brun
13
14#ifdef ACTUAL_RUN // -------- Second pass: dictionary already built --------
15
16#include "./Event.h" // now safe to include Event, its dictionary is loaded
17
18void run()
19{
20 TFile oldfile("tree108.root");
22 oldfile.GetObject("t4", oldtree);
23
24 const auto nentries = oldtree->GetEntries();
25
26 Event *event = nullptr;
27 oldtree->SetBranchAddress("event_split", &event);
28
29 // Create a new file + a clone of old tree in new file
30 TFile newfile("tree112.root", "recreate");
31 auto newtree = oldtree->CloneTree(0);
32
33 for (auto i : ROOT::TSeqI(nentries)) {
34 oldtree->GetEntry(i);
35 if (event->GetNtrack() > 605)
36 newtree->Fill();
37 event->Clear();
38 }
39
40 newtree->Print();
41 newfile.Write();
42}
43
44#else // -------- First pass: build dictionary + rerun macro --------
45
46void tree112_copy()
47{
48 TString tutdir = gROOT->GetTutorialDir();
49 gROOT->ProcessLine(".L " + tutdir + "/io/tree/Event.cxx+");
50 gROOT->ProcessLine("#define ACTUAL_RUN yes");
51 gROOT->ProcessLine("#include \"" __FILE__ "\"");
52 gROOT->ProcessLine("run()");
53}
54
55#endif
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
int nentries
#define gROOT
Definition TROOT.h:411
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition TFile.h:131
Basic string class.
Definition TString.h:138
A TTree represents a columnar dataset.
Definition TTree.h:89
Namespace for new ROOT classes and functions.
TSeq< int > TSeqI
Definition TSeq.hxx:203