Modifying clone of TTree

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,

    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++)
	cout << time << endl;
	time = 1;  // <- Value is not changing

    cout << endl << endl;

    for (int i=0; i<10; 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;
    return file;
  1. How can I change the value of time in the cloned tree?
  2. Why exactly is this not working?


_ROOT Version: 6.18/04
_Platform: Ubuntu 18.04
_Compiler: gcc 7.5

You would do something similar to:
where instead of filtering entries, you would SetBranchAddress on the data you need to update and modify it between the GetEntry and Fill.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.