# Cout string value from a TTree branch

**URL:** <https://root-forum.cern.ch/t/cout-string-value-from-a-ttree-branch/47011>\
**Category:** Newbie\
**Created:** [September 27, 2021, 9:52am UTC](https://root-forum.cern.ch/t/cout-string-value-from-a-ttree-branch/47011 "2021-09-27T09:52:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![JunKai](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/junkai/32/27450_2.png) [@JunKai](https://root-forum.cern.ch/u/JunKai)\
**Post date:** [September 27, 2021, 9:52am UTC](https://root-forum.cern.ch/t/cout-string-value-from-a-ttree-branch/47011/1 "2021-09-27T09:52:53Z")

</div>

Hi ,

I am looping trough a TTree and trying to cout the values of a particular branch `text`, which contains string values. I am running the code as macro in ROOT. However I get nothing when the code runs, it just output blank lines (Everything worked fine including printing float values).

this is the code that does the thing:

```auto
string text;

Tree->SetBranchAddress("text", &text);

for(int i=0; i <1000; i++){
	InTree->GetEntry(i);
	cout << text << endl;
}

```

Can somebody helps to point out the problem in my code?  
Thanks in advance

\_ROOT Version:6.24.00 (conda-forge channel, conda = 4.8.3)  
\_Platform:\_Linux 3.10.0-1127.el7.x86\_64  
\_Compiler:g++ (crosstool-NG 1.24.0.133\_b0863d8\_dirty) 9.3.0 (comes with conda)

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [September 27, 2021, 9:55am UTC](https://root-forum.cern.ch/t/cout-string-value-from-a-ttree-branch/47011/2 "2021-09-27T09:55:02Z")

</div>

Attach the output from: `Tree->Print();`

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [September 27, 2021, 10:13am UTC](https://root-forum.cern.ch/t/cout-string-value-from-a-ttree-branch/47011/4 "2021-09-27T10:13:39Z")

</div>

Try:

```cpp
std::string *muDecayVolume = 0;
Tree->SetBranchAddress("muDecayVolume", &muDecayVolume);

for(Long_64 i = 0; i < 1000; i++) {
  Tree->GetEntry(i);
  cout << *muDecayVolume << endl;
}

Tree->ResetBranchAddresses(); // disconnect from local variables
delete muDecayVolume; // cleanup

```

---

<div class="post-metadata">

**Author:** ![JunKai](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/junkai/32/27450_2.png) [@JunKai](https://root-forum.cern.ch/u/JunKai)\
**Post date:** [September 27, 2021, 10:19am UTC](https://root-forum.cern.ch/t/cout-string-value-from-a-ttree-branch/47011/5 "2021-09-27T10:19:32Z")

</div>

Thank you it worked !  
Can you explain what is the reson behind this to me?

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [September 27, 2021, 10:35am UTC](https://root-forum.cern.ch/t/cout-string-value-from-a-ttree-branch/47011/6 "2021-09-27T10:35:10Z")

</div>

Probably an “object of some class” (e.g., a “std::string”) versus an “ordinary C++ data type” (e.g., a “double”).

---

<div class="post-metadata">

**Author:** ![JunKai](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/junkai/32/27450_2.png) [@JunKai](https://root-forum.cern.ch/u/JunKai)\
**Post date:** [September 27, 2021, 10:35am UTC](https://root-forum.cern.ch/t/cout-string-value-from-a-ttree-branch/47011/7 "2021-09-27T10:35:55Z")

</div>

thanks !
