How to access specific values in TTree and manipulate them?


I am trying to access the values in the tree I created and just be able to view them for later.

The following code works fine:

#include <iostream>
#include <fstream>
#include "TTree.h"
#include "TH1F.h"

void pngtotree(){
    TTree *image = new TTree("Milky Way", "Milky Way");
    image -> ReadFile("milkywaytree.txt", "Red/I:Green:Blue:Y:X");
    image -> Show(0);
   TH1F *h1 = new TH1F("h1","Color Histogram", 256., 0.,256.);
    image -> Draw("Red");
    image -> Print();
    for (int i = 0; i < 10; i++){
        image -> Show(i);

and when I run it, I get the event 0 to show perfectly as I had in the file.

But, I would like to say, get the value in Red[0] or Y[100](if they were arrays instead of branches), and store them somewhere or change them, but I don’t know how to do that exactly.

Also, I would like to know if there are any functions in TTree or similar that I could use to quickly get the mean values of the branch.

I am fairly new to Root so any explanations would be greatly appreciated.


it is not possible to change the values stored in a tree “on the fly”. The solution here would be to read the tree and write a new one containing the altered values.
For what concerns the mean, the quickest solution could be to use the TTree::Draw method and get the momenta of the distribution via the obtained histogram (see the documentation here: … c468523e45).