# Find address of variable using name

**URL:** <https://root-forum.cern.ch/t/find-address-of-variable-using-name/50631>\
**Category:** Newbie\
**Created:** [July 2, 2022, 7:05am UTC](https://root-forum.cern.ch/t/find-address-of-variable-using-name/50631 "2022-07-02T07:05:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ACalcaterra](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/7ea924/32.png) [@ACalcaterra](https://root-forum.cern.ch/u/ACalcaterra)\
**Post date:** [July 2, 2022, 7:05am UTC](https://root-forum.cern.ch/t/find-address-of-variable-using-name/50631/1 "2022-07-02T07:05:17Z")

</div>

Hi, and thank you in advance for reading this. Double thanks for actual helping! 😁

I have an Rtuple where variables are stored with names such as “PionMomentum[100]”, “KaonMomentum[100]” and analogously for many other variables.

Now say that I want to access momentum for a particle _whose type has been  
decided in my code_.  
In other terms, my code can compute easily a variable name as a string,  
but this of course is not the same as the variable name itself. How can I do that?  
Is there a way in ROOT to do this:

```auto
std::string wantThisParticle = std::string("KaonMomentum"); // or a TString, or whatever)
double* rootVarAddress = (double*)someRootGetAddress(wantThisParticle);
double kaonP = rootVarAddress[5]; // get momentum of Kaon 5

```

For now what I do is establish myself a std::map\<string, pointer\> when I do all my SetBranchAddress calls, but doing this and consulting it is heavy, and smells badly as duplicating something that must be in ROOT already.

I have been reading lots of ROOT manuals, finding a histogram in a directory by name seems easy, but finding a TLeaf or TBranch is another pair of (T)leaves.  
Thank you again!

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [July 2, 2022, 9:02am UTC](https://root-forum.cern.ch/t/find-address-of-variable-using-name/50631/2 "2022-07-02T09:02:56Z")

</div>

Not sure what you mean, but to read from a tree you do

```auto
  tree->SetBranchAddress("varNameInTree",&varNameInMyCode);

```

i.e., the variable name in your code does not have to be the same as the name that was used in the tree --but of course, the variable _types_ (int, float,…) have to be the same. See a complete example on how to read all (or any) entries in the function `void tree1r()` in this example:  
[https://root.cern/doc/master/tree1\_8C\_source.html](https://root.cern/doc/master/tree1_8C_source.html)  
If you want, say, entry 5, just do `t1->GetEntry(4);` (entry numbers start from zero) instead of a loop over all entries, and so on.

---

<div class="post-metadata">

**Author:** ![ACalcaterra](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/7ea924/32.png) [@ACalcaterra](https://root-forum.cern.ch/u/ACalcaterra)\
**Post date:** [July 2, 2022, 10:08am UTC](https://root-forum.cern.ch/t/find-address-of-variable-using-name/50631/3 "2022-07-02T10:08:46Z")

</div>

Thank you for your answer, but I think I have not been clear enough.

I already know how to read from disk my data, and the variable where ROOT puts my data may have of course any name I like.

My point was:  
I have several blocks of data (vectors, if you want) in ROOT memory (they have already been read from disk) and they have names, similar or not.  
What I need to do, is to decide _programmatically_ from which one I have to access my data.  
I am able to code  
double pMom = PionMomentum[5] ;  
or  
double pMom = KaonMomentum[5];

but I need to decide which one while the code runs. Not beforehand.

In other words, I want to ask ROOT the _pointer_ where some variable named “ANYTHING”, that has _already_ been read from disk, is stored.

Since these are MY variables, I used them when I made my SetBranchAddress calls,  
of course I know where they are. Only, I would prefer to ask ROOT, that must know it because ROOT uses those addresses at any other event it reads. It has cached them  
somewhere at SetBranchAddress times. Where?

I hope this is clearer. \>-)

---

<div class="post-metadata">

**Author:** ![ACalcaterra](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/7ea924/32.png) [@ACalcaterra](https://root-forum.cern.ch/u/ACalcaterra)\
**Post date:** [July 2, 2022, 10:43am UTC](https://root-forum.cern.ch/t/find-address-of-variable-using-name/50631/4 "2022-07-02T10:43:36Z")

</div>

Let me try to be maximally clear, referring to your example in the tutorial.  
At line 95 I see:

```auto
 hpx->Fill(px);

```

This works, but it is not what I want. What I want is:

```auto
double* oDear = (double*)pleaseTellMe("px");
hpx->Fill(*oDear);

```

I would like to know the real name of ROOT function pleaseTellMe, assuming it exists.  
And, no, I prefer not to do:

```auto
double* oDear = &px;

```

This is, in some way, what I am doing now. Only, I do not like it, it’s tiring, and I would bet that the above unknown ROOT function exists.  
Thanks again everybody!

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [July 2, 2022, 11:02am UTC](https://root-forum.cern.ch/t/find-address-of-variable-using-name/50631/5 "2022-07-02T11:02:35Z")

</div>

So you mean that in

```auto
hpx->Fill(px);

```

you don’t know in advance whether you need to fill px, or py, or some other variable? you want to somehow determine, or ask the ‘user’, what variable to use?  
For that, in my example

```auto
tree->SetBranchAddress("varNameInTree",&varNameInMyCode);

```

the actual name “varNameInTree” (e.g. “px”) will be known only during runtime (e.g. it is read or somehow stored in a string or a char\* called “whatIwant”); in this case you could do

```auto
  double var; // generic name, of the type of any acceptable leaf name in the tree
  string whatIwant;
  cout << "enter the variable name ";
  cin >> whatIwant;
  tree->SetBranchAddress(whatIwant.c_str(),&var);

```

but you have to make sure that the string contents (e.g. “px”) actually match the name of a branch/leaf in the tree (and var should be the correct type, double in this example).

By the way, to fill a TTree you do `tree->Fill();` without arguments, which will fill all active branches. `hpx->Fill("px");` is for a histogram. But since you said you have an Rtuple I assumed we are talking about a tree.

---

<div class="post-metadata">

**Author:** ![ACalcaterra](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/7ea924/32.png) [@ACalcaterra](https://root-forum.cern.ch/u/ACalcaterra)\
**Post date:** [July 2, 2022, 4:37pm UTC](https://root-forum.cern.ch/t/find-address-of-variable-using-name/50631/6 "2022-07-02T16:37:41Z")

</div>

Spending some time hacking TTree::Show gave me the answer, that I write here, somewhat edited to remove my fumbling around.  
Just in case somebody else may be interested:

```auto
 oot -l /tmp/test.root
root [0] 
Attaching file /tmp/test.root as _file0...
root [1] TTree* tree = (TTree*)_file0->Get("ntp1");
root [6] tree->GetEntry(1)
(Int_t)1902
root [20] TLeaf* zMc = (TLeaf*)tree.FindLeaf("zMc")
root [21] Float_t* whereIs = (Float_t*)zMc->GetValuePointer()
root [22] .p *whereIs // don't know what this is
(Float_t)(-1.00000000000000000e+02)
root [23] whereIs++
(Float_t*)0x2086f6e8
root [24] .p *whereIs
(Float_t)(-6.36714696884155273e-01)
root [25] tree->GetEntry(11)
(Int_t)1502
root [26] .p *whereIs
(Float_t)(-1.04294419288635254e+00)
root [27] tree->GetEntry(1)
(Int_t)1902
root [28] .p *whereIs
(Float_t)(-6.36714696884155273e-01)
root [29] tree->GetEntry(57)
(Int_t)2022
root [30] .p *whereIs
(Float_t)(-2.05433964729309082e+00)
...and so on...

```

This method seems less efficient than my caching-by-hand the addresses of vectors, because I do it only once before my event loop.  
Mmmh…also here I can do that of course…but I am exposed to changes in ROOT innards. Somebody might remove the “-100” in the future.  
After all, since my method is already coded and debugged, I will stay with that for now.  
Cheers!
