Skip to content

Instantly share code, notes, and snippets.

@jiafulow
Last active April 7, 2016 03:36
Show Gist options
  • Save jiafulow/ae6cac1e32983f429170 to your computer and use it in GitHub Desktop.
Save jiafulow/ae6cac1e32983f429170 to your computer and use it in GitHub Desktop.
Create a TFile with TTrees, then update the TFile with more TTrees #askROOT
Download the C codes and run the following commands:
$ alias rot='root -l -b -q'
$ rot create_trees.C+
$ rot update_trees.C+
Tested in ROOT 6.06/00
#include "TFile.h"
#include "TTree.h"
#include "TRandom.h"
void create_trees() {
TFile * f1 = TFile::Open("giallo.root", "RECREATE");
gRandom->SetSeed(1);
TTree * t1 = new TTree("tree1", "tree1");
double var1;
t1->Branch("var1", &var1);
for (int i=0; i<100; i++) {
var1 = gRandom->Rndm();
t1->Fill();
}
TTree * t2 = new TTree("tree2", "tree2");
double var2;
t2->Branch("var2", &var2);
for (int i=0; i<100; i++) {
var2 = gRandom->Rndm();
t2->Fill();
}
t1->Write();
t2->Write();
f1->Close();
}
#include "TFile.h"
#include "TTree.h"
#include "TRandom.h"
void update_trees() {
TFile * f1 = TFile::Open("giallo.root", "UPDATE");
gRandom->SetSeed(2);
TTree * t3 = new TTree("tree3", "tree3");
double var3;
t3->Branch("var3", &var3);
for (int i=0; i<100; i++) {
var3 = gRandom->Rndm();
t3->Fill();
}
TTree * t4 = new TTree("tree4", "tree4");
double var4;
t4->Branch("var4", &var4);
for (int i=0; i<100; i++) {
var4 = gRandom->Rndm();
t4->Fill();
}
t3->Write();
t4->Write();
f1->Close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment