# Reading history inside a TTree

**URL:** <https://root-forum.cern.ch/t/reading-history-inside-a-ttree/44754>\
**Category:** ROOT\
**Tags:** hist, root\
**Created:** [May 7, 2021, 9:18am UTC](https://root-forum.cern.ch/t/reading-history-inside-a-ttree/44754 "2021-05-07T09:18:05Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![PPierre](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/ebca7d/32.png) [@PPierre](https://root-forum.cern.ch/u/PPierre)\
**Post date:** [May 7, 2021, 9:18am UTC](https://root-forum.cern.ch/t/reading-history-inside-a-ttree/44754/1 "2021-05-07T09:18:05Z")

</div>

Holà,

I would need some help about reading an histogram which is a leaf of a TBranch (‘depositedEnergy’) on a TTree.  
I would like to get the standard deviation. Here is a snippet of my C++ code inside a macro :

```auto
TFile f("myROOTfile"); //open the root file
    Double_t temp; // define the variables to hold the read values
    TTree *T = (TTree*)f.Get("Detector_PV_Detector_PV_depositedEnergy"); //open my tree
    T->SetBranchAddress("depositedEnergy",&depositedEnergy); //branch name + address of the variable
    cout << T->GetStdDev() << " " << T->GetEntries() << endl;

```

Without any surprise I have an error message saying that “T” is a TTree and the method GetStdDev() doesn’t belong to that class (you can only apply it to a TH1F).

How should one use the Tree-\>GetHistogram() method please ?

(Would have you proceed another way ?)

Thanks !

\_ROOT Version: 6.10  
\_Platform: macOS

* * *

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [May 7, 2021, 9:30am UTC](https://root-forum.cern.ch/t/reading-history-inside-a-ttree/44754/2 "2021-05-07T09:30:34Z")

</div>

Try something like this:

```auto
TFile *f = TFile::Open("myROOTfile");
TTree *t = (TTree *)f->Get("Detector_PV_Detector_PV_depositedEnergy");
t->Draw("depositedEnergy>>h");
h->GetStdDev();

```

---

<div class="post-metadata">

**Author:** ![PPierre](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/ebca7d/32.png) [@PPierre](https://root-forum.cern.ch/u/PPierre)\
**Post date:** [May 7, 2021, 9:34am UTC](https://root-forum.cern.ch/t/reading-history-inside-a-ttree/44754/3 "2021-05-07T09:34:39Z")

</div>

Thanks ! How h should be declare though ?

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [May 7, 2021, 9:40am UTC](https://root-forum.cern.ch/t/reading-history-inside-a-ttree/44754/4 "2021-05-07T09:40:33Z")

</div>

`TH1F *h = (TH1F *)gDirectory->Get("h");`  
For example:

```auto
root [0] auto f = TFile::Open("data.root");
root [1] TTree *t = (TTree *)f->Get("TreeS");
root [2] t->Draw("var0>>h");
Info in <TCanvas::MakeDefCanvas>: created default TCanvas with name c1
root [3] TH1F *h = (TH1F *)gDirectory->Get("h");
root [4] h->GetStdDev()
(double) 0.54692870
root [6]

```

---

<div class="post-metadata">

**Author:** ![PPierre](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/ebca7d/32.png) [@PPierre](https://root-forum.cern.ch/u/PPierre)\
**Post date:** [May 7, 2021, 9:42am UTC](https://root-forum.cern.ch/t/reading-history-inside-a-ttree/44754/5 "2021-05-07T09:42:26Z")

</div>

Ok I get it, works for me. Thanks !

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [May 7, 2021, 9:56am UTC](https://root-forum.cern.ch/t/reading-history-inside-a-ttree/44754/6 "2021-05-07T09:56:11Z")

</div>

Or even simpler:

```auto
ROOT::RDataFrame df("Detector_PV_Detector_PV_depositedEnergy", "myROOTfile");
auto h = df.Histo1D("depositedEnergy");
cout << h->GetStdDev() << endl;

```

---

<div class="post-metadata">

**Author:** ![PPierre](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/ebca7d/32.png) [@PPierre](https://root-forum.cern.ch/u/PPierre)\
**Post date:** [May 7, 2021, 10:00am UTC](https://root-forum.cern.ch/t/reading-history-inside-a-ttree/44754/7 "2021-05-07T10:00:36Z")

</div>

Ok I will try you last answer, knowing that I will write that in a loop, I would like to avoid drawing anything.

PS :  
Also similar to your first proposal :

```auto
T->Draw("depositedEnergy"); // stored in the current directory
TH1 *h = (TH1*)gPad->GetPrimitive("htemp");

```

---

<div class="post-metadata">

**Author:** ![PPierre](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/ebca7d/32.png) [@PPierre](https://root-forum.cern.ch/u/PPierre)\
**Post date:** [May 7, 2021, 10:05am UTC](https://root-forum.cern.ch/t/reading-history-inside-a-ttree/44754/8 "2021-05-07T10:05:23Z")

</div>

Should one use :

## EnableImplicitMT()

in order to declare RDataFrame ?

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [May 7, 2021, 10:05am UTC](https://root-forum.cern.ch/t/reading-history-inside-a-ttree/44754/9 "2021-05-07T10:05:57Z")

</div>

> [@PPierre](#):
>
> Also similar to your first proposal :
> 
> ```auto
> T->Draw("depositedEnergy"); // stored in the current directory
> TH1 *h = (TH1*)gPad->GetPrimitive("htemp");
> 
> ```

You’re right, but in my example you have control on the histogram variable name (e.g. if you have several ones) instead of `htemp`

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [May 7, 2021, 10:06am UTC](https://root-forum.cern.ch/t/reading-history-inside-a-ttree/44754/10 "2021-05-07T10:06:47Z")

</div>

> [@PPierre](#):
>
> Should one use :
> 
> ## EnableImplicitMT()
> 
> in order to declare RDataFrame ?

Yes, if you want to use multiple threads

---

<div class="post-metadata">

**Author:** ![PPierre](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/ebca7d/32.png) [@PPierre](https://root-forum.cern.ch/u/PPierre)\
**Post date:** [May 7, 2021, 10:18am UTC](https://root-forum.cern.ch/t/reading-history-inside-a-ttree/44754/11 "2021-05-07T10:18:56Z")

</div>

AH ok sorry it’s not what I wanted to ask.  
It 's just that I have that error message : **error: no member named ‘RDataFrame’ in namespace ‘ROOT’** and I don’t know how to proceed ? I cannot find any header with that name…

#include"ROOT/RDataFrame.h"

fatal error: ‘ROOT/RDataFrame.h’ file not found

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [May 7, 2021, 10:27am UTC](https://root-forum.cern.ch/t/reading-history-inside-a-ttree/44754/12 "2021-05-07T10:27:46Z")

</div>

Oh, OK, sorry, you’re using 6.10 and I think it’s too old. It has been introduced in 6.14, according to this post: [RDataFrame, a modern tool to manipulate and analyze ROOT datasets](https://root-forum.cern.ch/t/rdataframe-a-modern-tool-to-manipulate-and-analyze-root-datasets/29384)

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [May 21, 2021, 10:28am UTC](https://root-forum.cern.ch/t/reading-history-inside-a-ttree/44754/13 "2021-05-21T10:28:51Z")

</div>

Closed on May 21, 2021, 10:28am UTC

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