Hello there,
There is something that has been bothering me for some time. I want to clone a TTree and change some values. I work with GATE and I want to apply some post-processing, I know a workaround, that is creating a new tree and creating new branches manualy but how would one do this properly? Here is a minimal example:
#include <iostream>
#include "TFile.h"
#include "TTree.h"
using namespace std;
TFile * getTFile(const std::string filename, const string option);
void Set_Branch_Adresses(TTree *const current_tree, double& time);
int main()
{
string treename("PhaseSpace"), filename_infile,
filename_outfile("testfile.root");
TFile *readfile_phase_out;
TTree *readtree_phase_out;
filename_infile = (string) "path/to/root/file";
readfile_phase_out = getTFile(filename_infile, "READ");
readtree_phase_out = (TTree*)readfile_phase_out->Get(treename.c_str());
TFile* write_file = getTFile(filename_outfile, "Recreate");
TTree* write_tree = readtree_phase_out->CloneTree();
double time;
Set_Branch_Adresses(write_tree, time);
for (int i=0; i<10; i++)
{
write_tree->GetEntry(i);
cout << time << endl;
time = 1; // <- Value is not changing
}
cout << endl << endl;
for (int i=0; i<10; i++)
{
write_tree->GetEntry(i);
cout << time << endl; // nothing changed
}
}
void Set_Branch_Adresses(TTree *const current_tree, double& time)
{
current_tree->SetBranchAddress("Time", &time);
}
TFile * getTFile(const string filename, const string option)
{
TFile *file = new TFile(filename.c_str(), option.c_str());
if (!file->IsOpen())
{
cerr << "Could Not open Inputfile: " << filename << endl;
exit(1);
}
return file;
}
- How can I change the value of time in the cloned tree?
- Why exactly is this not working?
Cheers,
Andi
_ROOT Version: 6.18/04
_Platform: Ubuntu 18.04
_Compiler: gcc 7.5