ROOT
master
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
18
void
run()
19
{
20
TFile
oldfile
(
"tree108.root"
);
21
TTree
*
oldtree
;
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
46
void
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
Event.h
TRangeDynCast
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Definition
TCollection.h:358
nentries
int nentries
Definition
THbookFile.cxx:91
gROOT
#define gROOT
Definition
TROOT.h:411
Event
Definition
collection_proxies.C:163
ROOT::Detail::TRangeCast
Definition
TCollection.h:311
TFile
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition
TFile.h:131
TString
Basic string class.
Definition
TString.h:138
TTree
A TTree represents a columnar dataset.
Definition
TTree.h:89
ROOT
Definition
EExecutionPolicy.hxx:4
ROOT::TSeqI
TSeq< int > TSeqI
Definition
TSeq.hxx:203
tutorials
io
tree
tree112_copy.C
ROOT master - Reference Guide Generated on Sun Sep 21 2025 04:30:18 (GVA Time) using Doxygen 1.10.0