# How to use TTreeReaderValue?

**URL:** <https://root-forum.cern.ch/t/how-to-use-ttreereadervalue/37773>\
**Category:** Newbie\
**Created:** [January 29, 2020, 8:22pm UTC](https://root-forum.cern.ch/t/how-to-use-ttreereadervalue/37773 "2020-01-29T20:22:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mario\_Sessini](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mario_sessini/32/20771_2.png) [@Mario\_Sessini](https://root-forum.cern.ch/u/Mario_Sessini)\
**Post date:** [January 29, 2020, 8:22pm UTC](https://root-forum.cern.ch/t/how-to-use-ttreereadervalue/37773/1 "2020-01-29T20:22:57Z")

</div>

Hello,

I am trying to compile a very simple code just to understand how to use TTreeReader and TTreeReaderValue.

`#include “TFile.h”  
#include “TH1F.h”  
#include “TCanvas.h”  
#include “TMath.h”  
#include “TTreeReader.h”  
#include “TTreeReaderValue.h”

void Tuto(){

TFile \*f = TFile::Open(“[http://root.cern/files/introtutorials/eventdata.root](http://root.cern/files/introtutorials/eventdata.root)”);  
TTreeReader myReader(“EventTree”,f);  
TTreeReaderValue\<Float\_t\> posY(myReader, “fPosY”);

TH1F \*posY\_p = new TH1F(“posY\_p”,“PosY vs Momentum”,100,0,100);

while(myReader.Next()){  
posY\_p-\>Fill(posY);  
}

posY\_p-\>Draw();  
}`

I have no problem with TTreeReader alone, but TTreeReaderValue gives me the following error message :

“error: no matching constructor for initialization of ‘TTreeReaderValue\<Float\_t\>’ (aka  
‘TTreeReaderValue’)  
TTreeReaderValue\<Float\_t\> posY(myReader, “fPosY”);”

Do you have any idea ?

_ROOT Version:_ 6.18/0.4  
_Platform:_ Ubuntu 16.04  
_Compiler:_ g++

Thank you very much.

---

<div class="post-metadata">

**Author:** ![jblomer](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jblomer](https://root-forum.cern.ch/u/jblomer)\
**Post date:** [January 29, 2020, 10:48pm UTC](https://root-forum.cern.ch/t/how-to-use-ttreereadervalue/37773/2 "2020-01-29T22:48:09Z")

</div>

Hi Mario,

I could not reproduce the exact error message, but I see the following issues. The `fPosY` branch is in fact not a simple value but a member an `fParticle` object, where every given event can consist of multiple particles. You can check the schema of the tree with `EventTree->Print()`. The brackets in the line

```
*Br 2 :fParticles.fPosY : Double_t fPosY[fParticles_] *

```

indicate that `fPosY` is an array of `Double_t` values. So instead of using a `TTreeReaderValue` you’d need to `#include <TTreeReaderArray.h>` and use

```
TTreeReaderArray<Double_t> posY(myReader, "fParticles.fPosY");

```

In the loop, you can then fill the histogram with an inner loop over all the y values like

```
for (auto v : posY)
   posY_p->Fill(v);

```

Alternatively, you can use an [RDataFrame](https://root.cern.ch/doc/master/classROOT_1_1RDataFrame.html) (`#include <ROOT/RDataFrame.hxx>`) and write

```
ROOT::RDataFrame df("EventTree", "http://root.cern/files/introtutorials/eventdata.root");
auto h = df.Histo1D({"posY_p", "PosY vs Momentum", 100, 0, 100}, "fParticles");
h->DrawCopy();

```

Cheers,  
Jakob

---

<div class="post-metadata">

**Author:** ![Mario\_Sessini](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mario_sessini/32/20771_2.png) [@Mario\_Sessini](https://root-forum.cern.ch/u/Mario_Sessini)\
**Post date:** [January 30, 2020, 9:28am UTC](https://root-forum.cern.ch/t/how-to-use-ttreereadervalue/37773/3 "2020-01-30T09:28:37Z")

</div>

Hello Jakob,

It works perfectly now thank you.

Cheers,  
Mario.
