# Accessing and adding variables of trees

**URL:** <https://root-forum.cern.ch/t/accessing-and-adding-variables-of-trees/36122>\
**Category:** TMVA\
**Created:** [September 23, 2019, 7:57am UTC](https://root-forum.cern.ch/t/accessing-and-adding-variables-of-trees/36122 "2019-09-23T07:57:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![swanski82](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@swanski82](https://root-forum.cern.ch/u/swanski82)\
**Post date:** [September 23, 2019, 7:57am UTC](https://root-forum.cern.ch/t/accessing-and-adding-variables-of-trees/36122/1 "2019-09-23T07:57:16Z")

</div>

Hi,

I have input root file as its tree and branches look like when I type the commands as:

```auto
TFile *_file0 = TFile::Open("./Bs2KK_MC2017_36.root")
TTree *T = (TTree*)_file0->Get("T1")
T->Print()

******************************************************************************
*Tree :T1 : CMSSW HF tree *
*Entries : 12799 : Total = 4604483253 bytes File Size = 1151609353 *
* : : Tree compression factor = 4.00 *
******************************************************************************
*Branch :TAna01Event *
*Entries : 12799 : BranchElement (see below) *
*............................................................................*
*Br 169 :fMuons.fChi2LocalMomentum : Float_t fChi2LocalMomentum[fMuons_] *
*Entries : 12799 : Total Size= 300421 bytes File Size = 81759 *
*Baskets : 37 : Basket Size= 25088 bytes Compression= 3.66 *
*............................................................................*
*Br 170 :fMuons.fNumberOfLostTrkHits : Int_t fNumberOfLostTrkHits[fMuons_] *
*Entries : 12799 : Total Size= 300503 bytes File Size = 56581 *
*Baskets : 37 : Basket Size= 25088 bytes Compression= 5.29 *
*............................................................................*
*Br 171 :fMuons.fNumberOfValidTrkHits : Int_t fNumberOfValidTrkHits[fMuons_]*
*Entries : 12799 : Total Size= 300544 bytes File Size = 59670 *
*Baskets : 37 : Basket Size= 25088 bytes Compression= 5.02 *
*............................................................................*
*Br 172 :fMuons.fSegmentComp : Float_t fSegmentComp[fMuons_] *
*Entries : 12799 : Total Size= 300175 bytes File Size = 76596 *
*Baskets : 37 : Basket Size= 25088 bytes Compression= 3.90 *
*............................................................................*

```

I’d like to get the variables, e.g. fMuons.fSegmentComp and add them to the data loader container.

How can I access and add the variables to be used for the MVA training?

Thanks a lot.

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [September 23, 2019, 8:47am UTC](https://root-forum.cern.ch/t/accessing-and-adding-variables-of-trees/36122/2 "2019-09-23T08:47:09Z")

</div>

Hi,

With “data loader container” and “MVA training” you’re referring to TMVA I suppose? You can use `dataloader->AddVariable()`, see e.g. [tutorials/tmva/TMVAClassification.C](https://root.cern/doc/master/TMVAClassification_8C.html)

Axel.

---

<div class="post-metadata">

**Author:** ![swanski82](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@swanski82](https://root-forum.cern.ch/u/swanski82)\
**Post date:** [September 23, 2019, 8:53am UTC](https://root-forum.cern.ch/t/accessing-and-adding-variables-of-trees/36122/3 "2019-09-23T08:53:59Z")

</div>

Thanks. Yes, I know dataloader-\>AddVariable(). My question is more like accessing the variables I have printed out from the tree. It is NOT straightforward as just calling dataloader-\>AddVariable( “var”, ‘F’ );

So in my case I cannot just call e.g. dataloader-\>AddVariable( “fSegmentComp”, ‘F’ );

from the print out you can also follow; I have a tree name T1, then its branch element TAna01Event and then the variables in branches that I want to access :  
\*Br 172 :fMuons.fSegmentComp : Float\_t fSegmentComp[fMuons\_]

which I could not achieve to access so far and need help…

---

<div class="post-metadata">

**Author:** ![kialbert](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/kialbert/32/16537_2.png) [@kialbert](https://root-forum.cern.ch/u/kialbert)\
**Post date:** [September 24, 2019, 5:05pm UTC](https://root-forum.cern.ch/t/accessing-and-adding-variables-of-trees/36122/4 "2019-09-24T17:05:24Z")

</div>

Hi,

Have you tried using `dataloader->AddVariable("fMuons.fSegmentComp")`?

If this branch is an array you can add the individual elements like so:

```auto
dataloader->AddVariable("fMuons.fSegmentComp[0]")
dataloader->AddVariable("fMuons.fSegmentComp[1]")
// ...

```

Cheers,  
Kim
