# Filling TTree with strings insert only first TString

**URL:** <https://root-forum.cern.ch/t/filling-ttree-with-strings-insert-only-first-tstring/48656>\
**Category:** Newbie\
**Tags:** tree, root\
**Created:** [February 4, 2022, 10:41am UTC](https://root-forum.cern.ch/t/filling-ttree-with-strings-insert-only-first-tstring/48656 "2022-02-04T10:41:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheEnigmist](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/74df32/32.png) [@TheEnigmist](https://root-forum.cern.ch/u/TheEnigmist)\
**Post date:** [February 4, 2022, 10:41am UTC](https://root-forum.cern.ch/t/filling-ttree-with-strings-insert-only-first-tstring/48656/1 "2022-02-04T10:41:49Z")

</div>

ROOT Version:6.24/06  
Platform: linux/docker

Hello,  
I’m trying to create a TTree with a std::vector wich has inside a TString.  
The problem is that only first string is stored and all other are ignored.

I will provide a test code to reprocude this.

Test.h

```auto
#pragma once
#include <TString.h>
#include <vector>

namespace TS {

	class TestClass {
	public:
		TestClass() {
			aString = "0";
			id = 0;
		}
		TString aString;
		int id;
	};

	class TestContainer {
	public:
		std::vector<TestClass> classes;
	};
}

```

Test.cpp

```auto
#include "Test.h"
void Test(){

  cout << "hello world" << endl;

  TS::TestContainer container;
  for (int i = 1; i <= 10; ++i) {
      TS::TestClass t;
      t.aString.Form("A new string %d", i);
      t.id = i;
      container.classes.push_back(t);
  }
   
  TFile *f = new TFile("file.root","RECREATE");
  TTree test("TreeTest", "");

  test.Branch("TClasses", &container.classes);
  test.Fill();
  test.Write();
  f->Close();
}

```

This is what I get in ROOT:

 ![consoleprint](https://root-forum.cern.ch/uploads/default/original/3X/9/d/9d23aaf650f13a14132b26496f804ea79acd9195.png)

As you can see, TClasses.id is well populated, but the TString is stored only once.  
What I’m wrong?

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [February 4, 2022, 2:24pm UTC](https://root-forum.cern.ch/t/filling-ttree-with-strings-insert-only-first-tstring/48656/2 "2022-02-04T14:24:53Z")

</div>

`TTree::Scan` does not handle collection of strings well. To actually test whether this works or not you would need to read the data explicit (`SetBranchAddress`, `GetEntry`, etc.)

---

<div class="post-metadata">

**Author:** ![TheEnigmist](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/74df32/32.png) [@TheEnigmist](https://root-forum.cern.ch/u/TheEnigmist)\
**Post date:** [February 7, 2022, 5:59pm UTC](https://root-forum.cern.ch/t/filling-ttree-with-strings-insert-only-first-tstring/48656/3 "2022-02-07T17:59:58Z")

</div>

I found a way to print with Scan. I need to use TString.Data() method:  
 ![fix](https://root-forum.cern.ch/uploads/default/original/3X/c/7/c7f6fe8f497920778d76f4ff34bf96a6973334b9.png)

Now I’ve another problem. I cannot access single string value from script. I used `TreeTest->MakeClass("treetest")` to generate .C and .h to easly read data from script.  
If I add this code in Loop() function:

```auto
...
for (int i = 0; i < kMaxTClasses; ++i) {
      std::cout << i << " | id: " << TClasses_id[i] << " | aString: " << TClasses_aString[i] << std::endl;
}
...

```

I get this output:  
 ![image](https://root-forum.cern.ch/uploads/default/original/3X/a/0/a030f34b3d1bb9fce35e93425e6e9e03688ecfde.png)

Seems like it can’t read TString properly from that tree. I used std::string as well, but the ouput is the same.

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [February 7, 2022, 6:32pm UTC](https://root-forum.cern.ch/t/filling-ttree-with-strings-insert-only-first-tstring/48656/4 "2022-02-07T18:32:24Z")

</div>

MakeClass is explicitly not allowing to access the objects but instead there decomposition and the generator may or may not be wrong.

For your use case, I strongly recommend that you investigate using RDataFrame instead of MakeClass (MakeClass has many limitation that are solved by RDataFrame).

---

<div class="post-metadata">

**Author:** ![TheEnigmist](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/74df32/32.png) [@TheEnigmist](https://root-forum.cern.ch/u/TheEnigmist)\
**Post date:** [February 7, 2022, 7:08pm UTC](https://root-forum.cern.ch/t/filling-ttree-with-strings-insert-only-first-tstring/48656/5 "2022-02-07T19:08:57Z")

</div>

I see. But I really don’t know how access data from an RDataFrame. Can you help me accessing that strings from it?  
What I’ve done is loading RDataFrame with this code:

```auto
ROOT::RDataFrame d("TreeTest", "file.root");

```

I see the column names if use `.GetColumnNames` but if I use `.GetColumnType("TClasses.aString")` I get `"ROOT::VecOps::RVec<TString>"`.  
Now how can access a selected range of that strings?

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [February 9, 2022, 8:46am UTC](https://root-forum.cern.ch/t/filling-ttree-with-strings-insert-only-first-tstring/48656/6 "2022-02-09T08:46:35Z")

</div>

Hi @TheEnigmist ,  
RDF is all about accessing data. You have different ways to do so depending on what you want to do with it. If you want to extract the vector of strings from each event, you can use `auto strings = d.Take<ROOT::RVec<TString>>("TClasses.aString");`. If you want to perform some operation on each vector of strings, you want `Foreach`:

```cpp
df.Foreach([] (const ROOT::RVec<TString> &strings_per_event) { ... },
           {"TClasses.aString"});

```

etcetera. See the user guide [here](https://root.cern/doc/master/classROOT_1_1RDataFrame.html) and the tutorials [here](https://root.cern.ch/doc/master/group __tutorial__ dataframe.html).

Cheers,  
Enrico
