Two macros here. First make_tree.c which creates a dummy tree with x and y variables. Then add_to_tree.cc which takes the root file created by the first macro and adds an r branch for radial position.

make_tree.cc

// make_tree.cc
//
// Make a very simple tree in a file "a_tree.root".
{

TFile a_tree("a_tree.root", "RECREATE");
TTree*t = new TTree("t", "Tree!");

Double_t x,y;
t->Branch("x", &x);
t->Branch("y", &y);

TRandom3 r3;
for(int i=0; i<10; i++) {
  x = r3.Rndm();
  y = r3.Rndm();
  t->Fill();
}

t->Write();
a_tree.Close();
}

add_to_tree.cc

// add_to_tree.cc
//
// Add a branch to a tree named "t" in a_tree.root.
{
TFile * the_tree_file = new TFile("a_tree.root", "UPDATE");
TTree*t = (TTree*) the_tree_file->Get("t");


Double_t x,y,z;
t->SetBranchAddress("x", &x);
t->SetBranchAddress("y", &y);
t->SetBranchAddress("z", &z);


for(int i=0; i<t->GetEntries(); i++) {
  t->GetEntry(i);
  cout << "x: " << x << "\ty: " << y << endl;
}

Double_t r;
TBranch* b = t->Branch("r", &r);

cout << "About to add to tree." << endl;

for(int i=0; i<t->GetEntries(); i++) {
  t->GetEntry(i);
  r = sqrt(x*x + y*y);
  b->Fill();
}
cout << "Done adding to tree." << endl;

for(int i=0; i<t->GetEntries(); i++) {
  t->GetEntry(i);
  cout << "x: " << x << "\ty: " << y << "\tr: " << r << endl;
}

t->Write("", t->kOverwrite);
the_tree_file->Close();

}
add_branch.txt · Last modified: 2013/07/02 14:51 by mwoods
Recent changes RSS feed CC Attribution-Share Alike 3.0 Unported Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki |