Skip to content
Snippets Groups Projects
Commit 4fa4721b authored by Danilo Piparo's avatar Danilo Piparo
Browse files

[Tree] Modernise and format copy tree tutorials

parent 936867ff
No related branches found
No related tags found
No related merge requests found
......@@ -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();
}
......@@ -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();
}
......@@ -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();
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment