# Retrieving variables from Tree

**URL:** <https://root-forum.cern.ch/t/retrieving-variables-from-tree/41825>\
**Category:** Newbie\
**Created:** [October 12, 2020, 2:56pm UTC](https://root-forum.cern.ch/t/retrieving-variables-from-tree/41825 "2020-10-12T14:56:49Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sala](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/sala/32/23903_2.png) [@sala](https://root-forum.cern.ch/u/sala)\
**Post date:** [October 12, 2020, 2:56pm UTC](https://root-forum.cern.ch/t/retrieving-variables-from-tree/41825/1 "2020-10-12T14:56:50Z")

</div>

Hi everyone,  
I have a beginner’s question.

I am trying too manipulate a RooRealVar, and I read somewhere in the forum that the only way to do that is to manipulate variables directly from the tree. So I’m trying to add a new branch with the log of a variable, but I’m struggling to do that.  
I know in python I can just do (assuming tree has a branch named x)

```auto
for event in tree:
    logx = log10(event.x)

```

What is the equivalent in C++?

Thanks,  
Andrea

---

<div class="post-metadata">

**Author:** ![etejedor](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@etejedor](https://root-forum.cern.ch/u/etejedor)\
**Post date:** [October 13, 2020, 7:17am UTC](https://root-forum.cern.ch/t/retrieving-variables-from-tree/41825/2 "2020-10-13T07:17:06Z")

</div>

Hello,

Here’s an example of how to read data from a tree in C++:

> **[ROOT: tutorials/tree/hsimpleReader.C File Reference](https://root.cern.ch/doc/v610/hsimpleReader_8C.html)**

As for adding new branches to a TTree, you can find documentation in the TTree class:

> **[ROOT: TTree Class Reference](https://root.cern.ch/doc/master/classTTree.html)**

Hope that helps!

Enric

---

<div class="post-metadata">

**Author:** ![sala](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/sala/32/23903_2.png) [@sala](https://root-forum.cern.ch/u/sala)\
**Post date:** [October 13, 2020, 8:54am UTC](https://root-forum.cern.ch/t/retrieving-variables-from-tree/41825/3 "2020-10-13T08:54:53Z")

</div>

Hi @etejedor, thanks for your answer.  
I can’t make it work though. My code is

```auto
TFile *file = new TFile("../All_tmp.root");
TTree *tree = (TTree*)file->Get("Xib0/mytree");
        
TTreeReader reader ("Xib0/mytree",file);
TTreeReaderValue<Float_t> ipchi2 (reader,"lab0_IPCHI2_OWNPV");
Float_t loglab0;
TBranch* newbranch = tree->Branch("loglab0IPCHI2",&loglab0,"loglab0IPCHI2/F");
while (reader.Next()){
    loglab0 = log10(*ipchi2);
    newbranch->Fill();
 }
RooRealVar y("loglab0IPCHI2","loglab0IPCHI2",-2.5,6.);

```

and it doesn’t correctly load variable y when I try to sPlot it (see images 2 and 3 below).  
Maybe you see what’s wrong?[mysPlot.pdf](https://root-forum.cern.ch/uploads/short-url/f50AlvqhQWnsrnsjOIIOmwl6GB5.pdf) (34.5 KB)

---

<div class="post-metadata">

**Author:** ![etejedor](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@etejedor](https://root-forum.cern.ch/u/etejedor)\
**Post date:** [October 13, 2020, 9:57am UTC](https://root-forum.cern.ch/t/retrieving-variables-from-tree/41825/4 "2020-10-13T09:57:20Z")

</div>

@StephanH can a branch that has been just written be used by `RooRealVar` here?

Also, I have doubts if it is possible to both read and write the same TTree simultaneously, or if you need to read one tree and write in another, then close the file of the latter. @pcanal can perhaps comment.

---

<div class="post-metadata">

**Author:** ![sala](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/sala/32/23903_2.png) [@sala](https://root-forum.cern.ch/u/sala)\
**Post date:** [October 13, 2020, 10:13am UTC](https://root-forum.cern.ch/t/retrieving-variables-from-tree/41825/5 "2020-10-13T10:13:35Z")

</div>

Thanks @etejedor. Let’s wait for the experts response!

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [October 13, 2020, 10:19pm UTC](https://root-forum.cern.ch/t/retrieving-variables-from-tree/41825/6 "2020-10-13T22:19:19Z")

</div>

When done carefully and in different branches, you should be able to read and write to the same TTree. The code snippet does not indicate how the RooRealVar is attached to the existing TTree. For example it might be opening a 2nd copy of the file in which case ordering of things might matter a lot; In this regard, in the above snippet the file should probably be opened in Update mode and the TFile and/or TTree written.

---

<div class="post-metadata">

**Author:** ![sala](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/sala/32/23903_2.png) [@sala](https://root-forum.cern.ch/u/sala)\
**Post date:** [October 14, 2020, 8:01am UTC](https://root-forum.cern.ch/t/retrieving-variables-from-tree/41825/7 "2020-10-14T08:01:01Z")

</div>

Thanks for the suggestion @pcanal. I’ll try to get things working.  
Andrea
