# Can not write std::vector\<TVector3\> to a TTree

**URL:** <https://root-forum.cern.ch/t/can-not-write-std-vector-tvector3-to-a-ttree/59703>\
**Category:** Newbie\
**Tags:** root\
**Created:** [June 20, 2024, 12:08pm UTC](https://root-forum.cern.ch/t/can-not-write-std-vector-tvector3-to-a-ttree/59703 "2024-06-20T12:08:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Crisps](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/crisps/32/39101_2.png) [@Crisps](https://root-forum.cern.ch/u/Crisps)\
**Post date:** [June 20, 2024, 12:08pm UTC](https://root-forum.cern.ch/t/can-not-write-std-vector-tvector3-to-a-ttree/59703/1 "2024-06-20T12:08:54Z")

</div>

Dear experts

I want to write `std::vector<TVector3>` to a TTree, but I can not find it in the output ROOT file.

1. I have already generated the dictionary and linked it in CMakeLists.txt
2. The code is like:

```auto
std::vector<TVector3> fv;
std::vector<double> fd;
TFile* f_out = new TFile("some.root", "RECREATE");
TTree* t_out = new TTree("t", "t");
t_out->Branch("v", &fv);
t_out->Branch("d", &fd);
for(some_loop){
    fv = v;
    fd = d;
    t_out->Fill();
}
t_out->Write();
f_out->Close();

```

But in the output file I only get the value d, why?

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [June 20, 2024, 12:47pm UTC](https://root-forum.cern.ch/t/can-not-write-std-vector-tvector3-to-a-ttree/59703/2 "2024-06-20T12:47:54Z")

</div>

Hi,

thanks for the interesting post.  
I tried a slightly modified version of your code with 6.32.02, fresh from last Tuesday:

```auto
// Quick check: generate dictionary at runtime
gInterpreter->GenerateDictionary("vector<TVector3>", "vector;TVector3.h");

std::vector<TVector3> fv;
std::vector<double> fd;
TFile* f_out = new TFile("some.root", "RECREATE");
TTree* t_out = new TTree("t", "t");

t_out->Branch("v", &fv);
t_out->Branch("d", &fd);

for(auto i : {1.,2.,3.}){
    fd.emplace_back(i);
    fv.emplace_back(i,i*2,i*3);
    t_out->Fill();
}

t_out->Write();
f_out->Close();

```

It seems to work (pardon my quick workaround for making explicit the creation of the dictionary). This is what I have in the file:

```plaintext
~> rootls -lt some.root:t
TTree Jun 20 14:39 2024 t;1 "t" 
  v "v" 355
  d "d" 163
  Cluster INCLUSIVE ranges:
   - # 0: [0, 2]
  The total number of clusters is 1

```

Are you sure the library that contains the dictionary is loaded when you write out your data?  
One way to check this is to ask ROOT if the vector class has a dictionary:

```auto
TClass::GetClass("vector<TVector3>")->HasDictionary()

```

and to see the list of loaded libraries:

```auto
gSystem->GetLibraries()

```

For example, try to compare at the prompt the output of the 2 commands before and after running

```auto
gInterpreter->GenerateDictionary("vector<TVector3>", "vector;TVector3.h");

```

I hope this helps starting your debugging session.

Cheers,  
D

---

<div class="post-metadata">

**Author:** ![Crisps](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/crisps/32/39101_2.png) [@Crisps](https://root-forum.cern.ch/u/Crisps)\
**Post date:** [June 20, 2024, 1:20pm UTC](https://root-forum.cern.ch/t/can-not-write-std-vector-tvector3-to-a-ttree/59703/3 "2024-06-20T13:20:03Z")

</div>

I just Tried your method and it works fine!  
Now I’m wondering where I did wrong in my dictionary gen process…here is what I did:  
1.Make a vector\_of\_TVector3\_LinkDef.h and write:

```auto
#ifdef __CINT__
#pragma link C++ class std::vector<TVector3>+;
#endif

```

1. use `rootcling -f vector_of_TVector3_Dict.cxx path_to_root_include/TVector3.h vector_of_TVector3_LinkDef.h`
2. Add Dict source file in CMakeLists.txt:  
`add_library(lib other_source_files Dict.cxx)`
3. Compile and run it  
Any suggestion?

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [June 20, 2024, 1:21pm UTC](https://root-forum.cern.ch/t/can-not-write-std-vector-tvector3-to-a-ttree/59703/4 "2024-06-20T13:21:24Z")

</div>

Hi,

Could you check whether the library is loaded at runtime as suggested above?

Cheers,  
Danilo

---

<div class="post-metadata">

**Author:** ![Crisps](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/crisps/32/39101_2.png) [@Crisps](https://root-forum.cern.ch/u/Crisps)\
**Post date:** [June 20, 2024, 2:34pm UTC](https://root-forum.cern.ch/t/can-not-write-std-vector-tvector3-to-a-ttree/59703/5 "2024-06-20T14:34:17Z")

</div>

It gives this:

```auto
Error in <TTree::Branch>: The class requested (vector<TVector3>) for the branch "trkPos" is an instance of an stl collection and does not have a compiled CollectionProxy. Please generate the dictionary for this collection (vector<TVector3>) to avoid to write corrupted data.

```

seems dictionary is not loaded…why? I have already add the dictionary file in CMakeList.txt `add_library(lib other_source_files Dict.cxx)` and `target_link_libraries(main PUBLIC lib)`

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [June 21, 2024, 4:43am UTC](https://root-forum.cern.ch/t/can-not-write-std-vector-tvector3-to-a-ttree/59703/6 "2024-06-21T04:43:20Z")

</div>

Hi,

Could you perhaps find out if the dictionary is actually compiled (and the pcm created), if yes, in which library the dictionary is compiled and whether the library is loaded at runtime?

Best,  
D
