// - create a simple TTree // - create a clone of the tree // - add a branch of the tree //mpkansasuni 18.05.2016 #include "TFile.h" #include "TTree.h" #include "TRandom.h" #include "TTree.h" #include "TMath.h" #include Int_t Run, Event; Float_t x,y,z; void CreateParentTree() { // create a simple TTree with 5 branches // Two branches ("Run" and "Event") will be used to index the Tree TFile *f = new TFile("treeparent.root","recreate"); TTree *T = new TTree("T","test friend trees"); T->Branch("Run",&Run,"Run/I"); T->Branch("Event",&Event,"Event/I"); T->Branch("x",&x,"x/F"); T->Branch("y",&y,"y/F"); T->Branch("z",&z,"z/F"); // T->Branch("d", &d, "d/F"); TRandom r; for (Int_t i=0;i<10000;i++) { if (i < 5000) Run = 1; else Run = 2; Event = i; x = r.Gaus(10,1); y = r.Gaus(20,2); z = r.Landau(2,1); T->Fill(); } T->Print(); T->Write(); delete f; } void AddaBranchTree(){ //Get old file TString treefile = "treeparent.root"; TFile *ftree = new TFile(treefile); TTree *vesseltree = (TTree*)ftree->Get("T"); vesseltree->Print(); Float_t x, y, z, rsph; vesseltree->SetBranchAddress("x", &x); vesseltree->SetBranchAddress("y", &y); vesseltree->SetBranchAddress("z", &z); //create the new file TFile *g = new TFile("treeparentNew.root", "RECREATE"); TTree *newtree = vesseltree->CloneTree(0); // newtree->Branch("rsph", &rsph, "rsph/F"); int nentries = vesseltree->GetEntries(); std::cout << nentries << std::endl; for (int i = 0; i < nentries; i++){ vesseltree->GetEntry(i); if ((x + y + z) == 0.0) rsph = 0.0; // just a precaution else rsph = (x + y) / (x + y + z); newtree->Fill(); } newtree->Print(); newtree->Write(); delete g; delete ftree; } void treefriendtest() { CreateParentTree(); AddaBranchTree(); }