Hi all, hopefully, a simple question for someone who knows it, but something I’ve spent ages banging away at. I want to clone an existing TTree then create a new branch, filling it from a vector.
Below is my attempt so far. The trouble is, this seems to be creating new rows in the final TTree with each fill, instead of filling from row 0 as I was expecting. It also seems to be duplicating the values from the final row of the TTree I was attaching to.
void Chi2_Append() {
TFile *f1 = new TFile("File1", "read");
TTree *t1 = (TTree *) f1->Get("BaseTTree"); // base tree to append to
TFile *f2 = new TFile("File2", "read");
std::vector<double> *best_fit_toy = (std::vector<double> *) f2->Get("chi2_best_fit_toy");
TFile *newFile = new TFile("FinalFile","recreate");
TTree *newTree = (TTree *)t1->CloneTree(); // TODO: Look into this creating TH1 error.
newFile->cd();
std::string vector_name = "chi2_best_fit_toy"; // chi2_best_fit_toy is of type vector<double>
std::string branch_info = vector_name + "/D";
double toy_branch = -999;
newTree->Branch(vector_name.c_str(), &(toy_branch), branch_info.c_str()); //Create Branch for best fit toy
newTree->SetBranchStatus("*",0);
newTree->SetBranchStatus("chi2_best_fit_toy",1); //Only Fill ChiSquared branch
for (std::vector<double>::iterator i = best_fit_toy->begin(); i != best_fit_toy->end(); i++) {
toy_branch = *i;
}
newTree->Fill();
newTree->SetBranchStatus("*",1);
newTree->Write();
}