How to extract data from a Tree into an array?

I’m a beginner.
I want to analyse data in a tree.But I don’t know how to extract data from a tree.
For instance,
suppose t1(TTree)->b1(TBranch)->d1(TLeaf)
I want to select data from d1 with some conditions and put it in a array
How to accomplish the process?
Thank you very much!

Checkout the documentation for TTree::Draw under ‘How to obtain more info’.

How to obtain more info from TTree::Draw

Once TTree::Draw has been called, it is possible to access useful information still stored in the TTree object via the following functions:

    GetSelectedRows() // return the number of values accepted by the selection expression. In case where no selection was specified, returns the number of values processed.
    GetV1() // returns a pointer to the double array of V1
    GetV2() // returns a pointer to the double array of V2
    GetV3() // returns a pointer to the double array of V3
    GetV4() // returns a pointer to the double array of V4
    GetW() // returns a pointer to the double array of Weights where weight equal the result of the selection expression.

where V1,V2,V3 correspond to the expressions in

TTree::Draw("V1:V2:V3:V4",selection);

If the expression has more than 4 component use GetVal(index)

Example:

Root > ntuple->Draw("py:px","pz>4");
Root > TGraph *gr = new TGraph(ntuple->GetSelectedRows(),
                              ntuple->GetV2(), ntuple->GetV1());
Root > gr->Draw("ap"); //draw graph in current pad

A more complete complete tutorial (treegetval.C) shows how to use the GetVal() method.

creates a TGraph object with a number of points corresponding to the number of entries selected by the expression “pz>4”, the x points of the graph being the px values of the Tree and the y points the py values.

Important note: By default TTree::Draw creates the arrays obtained with GetW, GetV1, GetV2, GetV3, GetV4, GetVal with a length corresponding to the parameter fEstimate. The content will be the last GetSelectedRows() % GetEstimate() values calculated. By default fEstimate=1000000 and can be modified via TTree::SetEstimate. To keep in memory all the results (in case where there is only one result per entry), use

tree->SetEstimate(tree->GetEntries()+1); // same as tree->SetEstimate(-1);

You must call SetEstimate if the expected number of selected rows you need to look at is greater than 1000000.

You can use the option “goff” to turn off the graphics output of TTree::Draw in the above example.

Thank you for your help.
I imitated the example:


I get a pointer to “EuclidesEvents.fE”. Although I can visit the address with the pointer,the values can’t match up to the data in “EuclidesEvents.fE” . Could you figure out where is wrong?
I’m very grateful to your help!

This is another screenshot.

How many entries total? Did you call SetEstimate?

1 Like

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