From 4fa4721bfa04bfe3fc0927c12a930847b8a53dfe Mon Sep 17 00:00:00 2001 From: Danilo Piparo <danilo.piparo@cern.ch> Date: Mon, 24 Sep 2018 15:35:37 +0200 Subject: [PATCH] [Tree] Modernise and format copy tree tutorials --- tutorials/tree/copytree.C | 42 ++++++++++++++++++------------------ tutorials/tree/copytree2.C | 43 +++++++++++++++++-------------------- tutorials/tree/copytree3.C | 44 ++++++++++++++++++++------------------ 3 files changed, 64 insertions(+), 65 deletions(-) diff --git a/tutorials/tree/copytree.C b/tutorials/tree/copytree.C index 00fff7e8560..8606fdfe35c 100644 --- a/tutorials/tree/copytree.C +++ b/tutorials/tree/copytree.C @@ -10,31 +10,31 @@ /// /// \author Rene Brun +// Load the library at macro parsing time: we need this to use its content in the code R__LOAD_LIBRARY($ROOTSYS/test/libEvent.so) -void copytree() { - //Get old file, old tree and set top branch address - TFile *oldfile; +void copytree() +{ + TString dir = "$ROOTSYS/test/Event.root"; gSystem->ExpandPathName(dir); - if (!gSystem->AccessPathName(dir)) - {oldfile = new TFile("$ROOTSYS/test/Event.root");} - else {oldfile = new TFile("./Event.root");} - TTree *oldtree = (TTree*)oldfile->Get("T"); - Event *event = new Event(); - oldtree->SetBranchAddress("event",&event); - oldtree->SetBranchStatus("*",0); - oldtree->SetBranchStatus("event",1); - oldtree->SetBranchStatus("fNtrack",1); - oldtree->SetBranchStatus("fNseg",1); - oldtree->SetBranchStatus("fH",1); - - //Create a new file + a clone of old tree in new file - TFile *newfile = new TFile("small.root","recreate"); - TTree *newtree = oldtree->CloneTree(); + const auto filename = gSystem->AccessPathName(dir) ? "./Event.root" : "$ROOTSYS/test/Event.root"; + + TFile oldfile(filename); + TTree *oldtree; + oldfile.GetObject("T", oldtree); + + // Deactivate all branches + oldtree->SetBranchStatus("*", 0); + + // Activate only four of them + for (auto activeBranchName : {"event", "fNtrack", "fNseg", "fH"}) + oldtree->SetBranchStatus(activeBranchName, 1); + + // Create a new file + a clone of old tree in new file + TFile newfile("small.root", "recreate"); + auto newtree = oldtree->CloneTree(); newtree->Print(); - newfile->Write(); - delete oldfile; - delete newfile; + newtree->Write(); } diff --git a/tutorials/tree/copytree2.C b/tutorials/tree/copytree2.C index 427efec5d46..fd3d0ac6adb 100644 --- a/tutorials/tree/copytree2.C +++ b/tutorials/tree/copytree2.C @@ -11,36 +11,33 @@ /// /// \author Rene Brun +// Load the library at macro parsing time: we need this to use its content in the code R__LOAD_LIBRARY($ROOTSYS/test/libEvent.so) -void copytree2() { +void copytree2() +{ - //Get old file, old tree and set top branch address - TFile *oldfile; TString dir = "$ROOTSYS/test/Event.root"; gSystem->ExpandPathName(dir); - if (!gSystem->AccessPathName(dir)) - {oldfile = new TFile("$ROOTSYS/test/Event.root");} - else {oldfile = new TFile("./Event.root");} TTree *oldtree = (TTree*)oldfile->Get("T"); - Event *event = new Event(); - oldtree->SetBranchAddress("event",&event); - oldtree->SetBranchStatus("*",0); - oldtree->SetBranchStatus("event",1); - oldtree->SetBranchStatus("fNtrack",1); - oldtree->SetBranchStatus("fNseg",1); - oldtree->SetBranchStatus("fH",1); - - - //Create a new file + a clone of old tree header. Do not copy events - TFile *newfile = new TFile("small.root","recreate"); - TTree *newtree = oldtree->CloneTree(0); - - //Divert branch fH to a separate file and copy all events + 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 + 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 + TFile newfile("small.root", "recreate"); + auto newtree = oldtree->CloneTree(0); + + // Divert branch fH to a separate file and copy all events newtree->GetBranch("fH")->SetFile("small_fH.root"); newtree->CopyEntries(oldtree); newtree->Print(); - newfile->Write(); - delete oldfile; - delete newfile; + newtree->Write(); } diff --git a/tutorials/tree/copytree3.C b/tutorials/tree/copytree3.C index 1845af297b1..32c3b733252 100644 --- a/tutorials/tree/copytree3.C +++ b/tutorials/tree/copytree3.C @@ -13,31 +13,33 @@ R__LOAD_LIBRARY($ROOTSYS/test/libEvent.so) -void copytree3() { - - //Get old file, old tree and set top branch address - TFile *oldfile; +void copytree3() +{ + // Get old file, old tree and set top branch address TString dir = "$ROOTSYS/test/Event.root"; gSystem->ExpandPathName(dir); - if (!gSystem->AccessPathName(dir)) - {oldfile = new TFile("$ROOTSYS/test/Event.root");} - else {oldfile = new TFile("./Event.root");} - TTree *oldtree = (TTree*)oldfile->Get("T"); - Long64_t nentries = oldtree->GetEntries(); - Event *event = 0; - oldtree->SetBranchAddress("event",&event); - - //Create a new file + a clone of old tree in new file - TFile *newfile = new TFile("small.root","recreate"); - TTree *newtree = oldtree->CloneTree(0); - - for (Long64_t i=0;i<nentries; i++) { + 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 + 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(); + if (event->GetNtrack() > 605) + newtree->Fill(); event->Clear(); } + newtree->Print(); - newtree->AutoSave(); - delete oldfile; - delete newfile; + newtree->Write(); } -- GitLab