Reading history inside a TTree


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 :

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

Try something like this:

TFile *f = TFile::Open("myROOTfile");
TTree *t = (TTree *)f->Get("Detector_PV_Detector_PV_depositedEnergy");
1 Like

Thanks ! How h should be declare though ?

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

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]
1 Like

Ok I get it, works for me. Thanks !

1 Like

Or even simpler:

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

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 :

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

Should one use :


in order to declare RDataFrame ?

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

Yes, if you want to use multiple threads

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…


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

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

1 Like

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