Hey,
Does anyone know if there is any way of removing a specific entry from a tree or perhaps just editing/overwriting over it. I am trying to log information about glue mixes however since the procedure gets broken up into two sessions which are days to weeks apart I need to be able to reload the information and add to it. I was hoping to use a tree for easy comparisons between batches; however, I can’t find a way to edit an entry once it has been written to a tree. Has anyone tried something similar and have a solution?
You can not edit nor remove (directly) entries from a TTree which are designed to be write once, read many times.
However you have 3 options. You can copy the existing TTree to a new file modifying the value that needs updating while doing so (see tutorials/tree/copytree* for some example). You can add a new branch with the additional information. Or the simplest solution is to create a new TTree with the new information and to add this TTree as a ‘Friend’ (See TTree::AddFriend) of the existing TTree.
TFile *file = TFile::Open("input.root",'UPDATE");
TTree *input = 0; file->GetObject("input",input);
TBranch *branch = mytree->Branch("newbranch",&data);
for(Long64_t entry = 0; entry < input->GetEntries(); ++entry) {
input->GetEntry(entry); // This can changed as needed to only read the needed subset.
data = ....;
branch->Fill();
}
mytree->GetCurrentFile()->Write();
However, adding a new branch is not recommended as it decreases the efficiency of reading the TTree.
So to use TTree friendships:
TFile *file = TFile::Open("input.root",'UPDATE");
TTree *input = 0; file->GetObject("input",input);
TTree *output = new TTree("input_addition","addition to the input");
output->Branch(.....);
for(Long64_t entry = 0; entry < input->GetEntries(); ++entry) {
input->GetEntry(entry); // This can changed as needed to only read the needed subset.
.. set the new values ..
output->Fill();
}
input->AddFriend(output);
file->Write();