ROOT Version: 5
Platform: Scientific Linux 6.10
Compiler: g++ 4.4.7
Hi everyone, I am struggling with writing TBranch objects out of a vector into a new tree. More specifically, I am unsure about how one initializes the address of a new TBranch so that when I fill the TTree, a copy of a pre-existing TBranch is written to it. Here is a minimal code to show the problem:
#include <iostream>
#include <TFile.h>
#include <TTree.h>
#include <TBranch.h>
#include <TKey.h>
using namespace std;
int main(){
int i = 0;
vector<TBranch*> Detectors;
TFile* data = TFile::Open("Data.root");
TKey *key = 0;
TTree* event = 0;
TBranch* detector = 0;
TObjArray* branch_list = 0;
if(data->IsOpen()){
TIter next_event(data->GetListOfKeys());
while((key = (TKey*)next_event())){
if(strcmp(key->GetClassName(), "TTree")){
continue;
}
event = (TTree*)data->Get(key->GetName());
branch_list = event->GetListOfBranches();
for(i = 0; i < branch_list->GetEntries(); i++){
detector = (TBranch*)branch_list->At(i);
Detectors.push_back(detector);
}
}
}
else{cout << "ERROR: File failed to open." << endl;}
TFile* rootout = TFile::Open("Test.root", "recreate");
for(i = 0; i < Detectors.size(); i++){
TTree* out_tree;
//Don't know how to initialize the branch to copy the Detectors[i] branch and associate it with the TTree
TBranch* out_branch = out_tree->Branch(Detectors[i]->GetName(), &Detectors[i], "data/I");
out_branch = Detectors[i];
out_tree->Fill();
out_tree->Write();
}
Detectors.clear();
data->Close();
rootout->Close();
return 0;
}