Skip to content

Instantly share code, notes, and snippets.

@jiafulow
Last active April 7, 2016 03:41
Show Gist options
  • Save jiafulow/01247b4565fc249d5d79fe747cab596b to your computer and use it in GitHub Desktop.
Save jiafulow/01247b4565fc249d5d79fe747cab596b to your computer and use it in GitHub Desktop.
Clone a TTree and modify the values stored in selected branches #askROOT
Download the C codes and run the following commands:
$ alias rot='root -l -b -q'
$ rot create_tree.C+
$ rot clone_tree.C+
Tested in ROOT 6.06/00
#include "TFile.h"
#include "TTree.h"
#include "TRandom.h"
void clone_tree() {
TFile * f1 = TFile::Open("giallo.root", "READ");
TTree * t1 = (TTree*) f1->Get("tree1");
gRandom->SetSeed(2);
TFile * f2 = TFile::Open("giallo_clone.root", "RECREATE");
TTree * t2 = t1->CloneTree(0);
double var1;
double var2;
t1->SetBranchAddress("var1", &var1);
t1->SetBranchAddress("var2", &var2);
for (int i=0; i<t1->GetEntries(); i++) {
t1->GetEntry(i);
var2 /= 2.0; // modify this branch
t2->Fill();
}
t2->Write();
f2->Close();
f1->Close();
}
#include "TFile.h"
#include "TTree.h"
#include "TRandom.h"
void create_tree() {
TFile * f1 = TFile::Open("giallo.root", "RECREATE");
gRandom->SetSeed(1);
TTree * t1 = new TTree("tree1", "tree1");
double var1;
double var2;
t1->Branch("var1", &var1);
t1->Branch("var2", &var2);
for (int i=0; i<100; i++) {
var1 = gRandom->Rndm();
var2 = var1 * 2.0;
t1->Fill();
}
t1->Write();
f1->Close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment