I have a file containing a TTree with several branches. I would like to copy it to a new file and modify the content of one of the branches (a double) to a value I set. I’ve tried searching for this, but couldn’t find a way to do this. Could you let me know this this can be done?
//Get old file, old tree and set top branch address
TFile *oldfile = new TFile("$ROOTSYS/test/Event.root");
TTree *oldtree = (TTree*)oldfile->Get("T");
Long64_t nentries = oldtree->GetEntries();
Event *event = 0;
oldtree->SetBranchAddress("event",&event);
//Create a new file + a clone of old tree in new file
TFile *newfile = new TFile("small.root","recreate");
TTree *newtree = oldtree->CloneTree(0);
for (Long64_t i=0;i<nentries; i++) {
oldtree->GetEntry(i);
event->SetTemperature( (event->GetTemperatue() - 32) * 5/9 ); // Change from Fahrenheit to Celsius:
if (event->GetNtrack() > 605) newtree->Fill(); // filtering unwanted entries.
event->Clear();
}
newtree->AutoSave();
delete oldfile;
delete newfile;
In the example you provided, does this code change the branch event in the tree T? I cannot go through the code. What is SetTemprature()? Is it a member function in ROOT?
Actually no, due to a bug in my code snippet. Fill must (of course) be called after the data object has been modified. Corrected version:
//Get old file, old tree and set top branch address
TFile *oldfile = new TFile("$ROOTSYS/test/Event.root");
TTree *oldtree = (TTree*)oldfile->Get("T");
Long64_t nentries = oldtree->GetEntries();
Event *event = 0;
oldtree->SetBranchAddress("event",&event);
//Create a new file + a clone of old tree in new file
TFile *newfile = new TFile("small.root","recreate");
TTree *newtree = oldtree->CloneTree(0);
for (Long64_t i=0;i<nentries; i++) {
oldtree->GetEntry(i);
// Do some data modifications.
event->SetTemperature( (event->GetTemperatue() - 32) * 5/9 ); // Change from Fahrenheit to Celsius:
if (event->GetNtrack() > 605) // filtering unwanted entries.
{
newtree->Fill(); // For those entry passing the filter, store the modified version of the data (and the untouched part too).
}
event->Clear();
}
newtree->AutoSave();
delete oldfile;
delete newfile;
Thank you. I checked the file including SetTemperature but still don’t understand what are:
SetTemperature
GetTemperatue()
GetNtrack()
I am sorry but I am very new to ROOT and do need to modify the content of some branches in some root files. I just need a simple modification, like adding a constant to all entries of a branch.
Thank you all for your help. I am almost there however still there’s one more problem. I use the following code to modify the branch “event_ID” in the tree “Singles” and what I get in the cloned root file is that the new evend_IDs includes both old and modified values! Please note that I am talking about the branch in the new root file which I wish to include only the modified numbers. Could you spot where the problem is?