Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
copytree2.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_tree
3 /// \notebook -nodraw
4 /// Copy a subset of a Tree to a new Tree, one branch in a separate file.
5 ///
6 /// One branch of the new Tree is written to a separate file
7 /// The input file has been generated by the program in `$ROOTSYS/test/Event`
8 /// with the command `Event 1000 1 1 1`
9 ///
10 /// \macro_code
11 ///
12 /// \author Rene Brun
13 
14 // Load the library at macro parsing time: we need this to use its content in the code
15 R__LOAD_LIBRARY($ROOTSYS/test/libEvent.so)
16 
17 void copytree2()
18 {
19 
20  TString dir = "$ROOTSYS/test/Event.root";
21  gSystem->ExpandPathName(dir);
22  const auto filename = gSystem->AccessPathName(dir) ? "./Event.root" : "$ROOTSYS/test/Event.root";
23 
24  TFile oldfile(filename);
25  TTree *oldtree;
26  oldfile.GetObject("T", oldtree);
27 
28  // Activate only four of them
29  for (auto activeBranchName : {"event", "fNtrack", "fNseg", "fH"}) {
30  oldtree->SetBranchStatus(activeBranchName, 1);
31  }
32 
33  // Create a new file + a clone of old tree header. Do not copy events
34  TFile newfile("small.root", "recreate");
35  auto newtree = oldtree->CloneTree(0);
36 
37  // Divert branch fH to a separate file and copy all events
38  newtree->GetBranch("fH")->SetFile("small_fH.root");
39  newtree->CopyEntries(oldtree);
40 
41  newtree->Print();
42  newfile.Write();
43 }