Modifying TTree

Hi,
yes I read your question, but dumping a tree to a text file, modify entries and then make a new tree from the text file is inefficient and prone to errors so I thought I’d suggest an alternative approach :slight_smile:

Anyway: just read the entries to have them available in your program, here is a list of the different methods to read values from a TTree with links to tutorials.

For example, with TTreeReader:

void read() {
  TFile f("file.root"); // open file
  TTreeReader r("tree", &f); // create reader for tree "tree" in file f
  TTreeReaderValue<double> x(r, "x"); // attach object `x` to branch "x"
  while(r.Next())
    std::cout << *x << std::endl;
}

and with TDataFrame:

void PrintValue(double x) { std::cout << x << std::endl; }

void read() {
  ROOT::Experimental::TDataFrame d("tree", "file.root");
  // for each entry, call `printValue` on branch "x"
  d.Foreach(PrintValue, {"x"}); 
}
1 Like