# Problem w/ vector\<vector\> after opening file w/ that type

**URL:** <https://root-forum.cern.ch/t/problem-w-vector-vector-after-opening-file-w-that-type/14772>\
**Category:** ROOT\
**Created:** [August 15, 2012, 8:09pm UTC](https://root-forum.cern.ch/t/problem-w-vector-vector-after-opening-file-w-that-type/14772 "2012-08-15T20:09:49Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![yesw2000](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/3d9bf3/32.png) [@yesw2000](https://root-forum.cern.ch/u/yesw2000)\
**Post date:** [August 15, 2012, 8:09pm UTC](https://root-forum.cern.ch/t/problem-w-vector-vector-after-opening-file-w-that-type/14772/1 "2012-08-15T20:09:49Z")

</div>

Hi,

I am meeting a weird problem with loading vector of vector after opening a root file with that type variable in TTree object. In PyROOT I loaded the class generation and struct definition for vector of vector through a C++ macro “vecs.C”:

```auto
#include <vector>

#ifdef __CINT__
#pragma link C++ class vector<vector<float> >+;
#pragma link C++ class vector<vector<vector<float> > >+;
#endif

struct myStruct
{
  vector<float> *vF;
  vector<vector<float> > *vvF;
  vector<vector<vector<float> > > *vvvF;

  vector<float> lvF;
  vector<vector<float> > lvvF;
  vector<vector<vector<float> > > lvvvF;
};
```

In pyROOT I did the following script “test-vecs.py”

```auto
from ROOT import (TFile,gROOT)

file = TFile("myVV_VVV.root")
tree = file.Get("qcd")

gROOT.ProcessLine(".L vecs.C+")
from ROOT import myStruct
m=myStruct()

# open root file after importing myStruct
# file = TFile("myVV_VVV.root")
# tree = file.Get("qcd")

fV =m.vF
fVV =m.vvF
fVVV=m.vvvF

flV =m.lvF
flVV =m.lvvF
flVVV=m.lvvvF
```

The above script crashed at “fVV =m.vvF”. If I moved root file opening after “m=myStruct()”, everything would work fine. Or fVV and lfVV (related to vector) would work fine if the root file does not have any TTree object containing with vector. Same for vector\<vector\>. The ROOT version with this crash is 5.28.00g. I also tried switch to ROOT later versions, says 5.34.00-rc1, which does not crash but leads to incomplete fVV, fVVV, lacking functions such as size and at, that is:

> [@](#):
>
> > > > print dir(fV)  
> > > > [‘ **add** ’, ‘ **assign** ’, ‘ **bool** ’, ‘ **class** ’, ‘ **cpp\_eq** ’, ‘ **cpp\_ne** ’, ‘ **delattr** ’, ‘ **dict** ’, ‘ **div** ’, ‘ **doc** ’, ‘ **eq** ’, ‘ **format** ’, ‘ **ge** ’, ‘ **getattribute** ’, ‘ **getitem** ’, ‘ **gt** ’, ‘ **hash** ’, ‘ **init** ’, ‘ **le** ’, ‘ **len** ’, ‘\_\_lifeline’, ‘ **lt** ’, ‘ **module** ’, ‘ **mul** ’, ‘ **ne** ’, ‘ **new** ’, ‘ **nonzero** ’, ‘ **radd** ’, ‘ **rdiv** ’, ‘ **reduce** ’, ‘ **reduce\_ex** ’, ‘ **repr** ’, ‘ **rmul** ’, ‘ **rsub** ’, ‘ **setattr** ’, ‘ **setitem** ’, ‘ **sizeof** ’, ‘ **str** ’, ‘ **sub** ’, ‘ **subclasshook** ’, ‘ **weakref** ’, ‘\_vector\_\_at’, ‘at’, ‘back’, ‘begin’, ‘capacity’, ‘clear’, ‘empty’, ‘end’, ‘erase’, ‘front’, ‘insert’, ‘max\_size’, ‘pop\_back’, ‘push\_back’, ‘rbegin’, ‘rend’, ‘reserve’, ‘resize’, ‘size’, ‘swap’, ‘vector’]  
> > > > print dir(fVVV)  
> > > > [‘ **add** ’, ‘ **bool** ’, ‘ **class** ’, ‘ **delattr** ’, ‘ **dict** ’, ‘ **div** ’, ‘ **doc** ’, ‘ **eq** ’, ‘ **format** ’, ‘ **ge** ’, ‘ **getattribute** ’, ‘ **gt** ’, ‘ **hash** ’, ‘ **init** ’, ‘ **le** ’, ‘\_\_lifeline’, ‘ **lt** ’, ‘ **module** ’, ‘ **mul** ’, ‘ **ne** ’, ‘ **new** ’, ‘ **nonzero** ’, ‘ **radd** ’, ‘ **rdiv** ’, ‘ **reduce** ’, ‘ **reduce\_ex** ’, ‘ **repr** ’, ‘ **rmul** ’, ‘ **rsub** ’, ‘ **setattr** ’, ‘ **sizeof** ’, ‘ **str** ’, ‘ **sub** ’, ‘ **subclasshook** ’, ‘ **weakref** ’]

Any idea and solution?  
Thanks.

–Shuwei

---

<div class="post-metadata">

**Author:** ![wlav](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/w/f07891/32.png) [@wlav](https://root-forum.cern.ch/u/wlav)\
**Post date:** [August 16, 2012, 12:58am UTC](https://root-forum.cern.ch/t/problem-w-vector-vector-after-opening-file-w-that-type/14772/2 "2012-08-16T00:58:51Z")

</div>

Shuwei,

w/o checking in detail (I don’t have your data file), but I’d think that the only thing that needs moving would be this line:`gROOT.ProcessLine(".L vecs.C+")`  
It should be at the beginning, i.e. before opening the file. I think this is also true for C++. What would be even better, is if you generate a dictionary with a .rootmap file on the side. Then the auto-loading on TFile opening should work as expected (but again, haven’t tried).

Cheers,  
Wim

---

<div class="post-metadata">

**Author:** ![yesw2000](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/3d9bf3/32.png) [@yesw2000](https://root-forum.cern.ch/u/yesw2000)\
**Post date:** [August 17, 2012, 2:44am UTC](https://root-forum.cern.ch/t/problem-w-vector-vector-after-opening-file-w-that-type/14772/3 "2012-08-17T02:44:36Z")

</div>

Hi Wim,

I tried in C++. It worked fine whether a root file is opened prior to “.L vecs.C+” or after.

```auto
{
TFile *file = TFile::Open("myVV_VVV.root");
TTree* tree = file.Get("qcd");

gROOT->ProcessLine(".L vecs.C+");
myStruct m;

fV =m.vF;
fVV =m.vvF;
fVVV=m.vvvF;

vector<float> flV =m.lvF;
vector<vector<float> > flVV =m.lvvF;
vector<vector<vector<float> > > flVVV=m.lvvvF;
}
```

The test root file can be generated in the following way:

```auto
{
gROOT->ProcessLine(".L vecs2.C+");
myStruct m;

fV =m.vF = new vector<float>;
fVV =m.vvF = new vector<vector<float> >;
fVVV=m.vvvF = new vector<vector<vector<float> > >;
vector<float> flV =m.lvF;
vector<vector<float> > flVV =m.lvvF;
vector<vector<vector<float> > > flVVV=m.lvvvF;

TFile *newFile = TFile::Open("myVV_VVV.root","RECREATE");
TTree *newTree = new TTree("qcd","A TTree with VecOfVec VecOfVecOfVec variables");

newTree->Branch("p_vFloat",&fV);
newTree->Branch("p_vvFloat",&fVV);
newTree->Branch("p_vvvFloat",&fVVV);
newTree->Branch("vFloat",&flV);
newTree->Branch("vvFloat",&flVV);
newTree->Branch("vvvFloat",&flVVV);

flV.push_back(1);
flV.push_back(2);
flV.push_back(11);
flV.push_back(22);

flVV.push_back(flV);
fVV->push_back(flV);
flVVV.push_back(flVV);
flVVV.push_back(*fVV);

newTree->Fill();
newFile->Write();
newFile->Close();
}
```

The content of the ROOT macro “vecs.C+” (it would be more complicated in reality) and its struct name depend on the input root file, that is why I have to open the root file first. Any good solution for my case?

–Shuwei

---

<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:** [August 20, 2012, 9:45pm UTC](https://root-forum.cern.ch/t/problem-w-vector-vector-after-opening-file-w-that-type/14772/4 "2012-08-20T21:45:40Z")

</div>

Hi,

Try`file->MakeProject("libforfile","*","RECREATE++");`in the case you are reading a file.

Cheers,  
Philippe.

---

<div class="post-metadata">

**Author:** ![wlav](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/w/f07891/32.png) [@wlav](https://root-forum.cern.ch/u/wlav)\
**Post date:** [August 21, 2012, 12:59am UTC](https://root-forum.cern.ch/t/problem-w-vector-vector-after-opening-file-w-that-type/14772/5 "2012-08-21T00:59:05Z")

</div>

Hi,

and if that doesn’t work, can you please provide a (sample) data file? I’ve tried various variations, but am unable to reproduce the problem. Thanks.

Cheers,  
Wim

---

<div class="post-metadata">

**Author:** ![yesw2000](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/3d9bf3/32.png) [@yesw2000](https://root-forum.cern.ch/u/yesw2000)\
**Post date:** [August 21, 2012, 3:02pm UTC](https://root-forum.cern.ch/t/problem-w-vector-vector-after-opening-file-w-that-type/14772/6 "2012-08-21T15:02:41Z")

</div>

Hi Wim,

Attached please find a simple sample ROOT file with a TTree “qcd” containing vector and vector\<vector \>.

–Shuwei  
[myVV\_VVV.root](https://root-forum.cern.ch/uploads/default/original/2X/b/bf39c6b325138e1b8d2267f798a62937018d408d.root) (6.61 KB)

---

<div class="post-metadata">

**Author:** ![wlav](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/w/f07891/32.png) [@wlav](https://root-forum.cern.ch/u/wlav)\
**Post date:** [August 21, 2012, 6:12pm UTC](https://root-forum.cern.ch/t/problem-w-vector-vector-after-opening-file-w-that-type/14772/7 "2012-08-21T18:12:44Z")

</div>

Shuwei,

thanks; looks like a bug in TClass.

Even with the .C version and although the vectors work (if used after loading myStruct), asking the TClass for all public methods returns only zeroes:`std::cout << TClass::GetClass("vector<vector<float> >")->GetListOfAllPublicMethods()->GetSize() << std::endl;
std::cout << TClass::GetClass("vector<vector<float,allocator<float> >,allocator<vector<float,allocator<float> > > >")->GetListOfAllPublicMethods()->GetSize() << std::endl;`if the file has been loaded before myStruct.

In the past, PyROOT used to delete STL classes with no methods, but Philippe told me not to. 🙂

So Philippe, any ideas why there is this discrepancy?

The fix is very simple: if any empty STL classes are seen, then in PyROOT, all that is needed is this:`ClassInfo_t* cl = gInterpreter->ClassInfo_Factory(name.c_str());
if (cl) gInterpreter->SetClassInfo(klass, kTRUE);`and so I’ll check that in if there’s no better solution.

Thanks,  
Wim

---

<div class="post-metadata">

**Author:** ![Pepe\_Le\_Pew](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pepe_le_pew/32/12365_2.png) [@Pepe\_Le\_Pew](https://root-forum.cern.ch/u/Pepe_Le_Pew)\
**Post date:** [August 21, 2012, 6:28pm UTC](https://root-forum.cern.ch/t/problem-w-vector-vector-after-opening-file-w-that-type/14772/8 "2012-08-21T18:28:36Z")

</div>

First try to:

`gSystem->Exec("rm -f AutoDict*vector*vector*float*"); // just a precaution
gInterpreter->GenerateDictionary("std::vector<std::vector<float> >", "vector");
gInterpreter->GenerateDictionary("std::vector<std::vector<std::vector<float> > >", "vector");`  
For “std::vector\<std::vector \>” see [url][Storing 2D vector [--\> vector\<vector\<...\> \>] into a TTree](https://root-forum.cern.ch/t/storing-2d-vector-vector-vector-into-a-ttree/14331/1)  
For “std::vector\<std::vectorstd::string \>” see [url][Problem in getting the vector \< vector \<string\> \> branch](https://root-forum.cern.ch/t/problem-in-getting-the-vector-vector-string-branch/14608/1)

---

<div class="post-metadata">

**Author:** ![wlav](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/w/f07891/32.png) [@wlav](https://root-forum.cern.ch/u/wlav)\
**Post date:** [August 21, 2012, 6:35pm UTC](https://root-forum.cern.ch/t/problem-w-vector-vector-after-opening-file-w-that-type/14772/9 "2012-08-21T18:35:49Z")

</div>

Pepe,

at issue is that Shuwei does not know for certain that the vectors are needed: that’s only known after opening the file. Hence, the goal is to make both cases work: opening the file first, then loading the dict and v.v.

Cheers,  
Wim

---

<div class="post-metadata">

**Author:** ![Pepe\_Le\_Pew](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pepe_le_pew/32/12365_2.png) [@Pepe\_Le\_Pew](https://root-forum.cern.ch/u/Pepe_Le_Pew)\
**Post date:** [August 21, 2012, 6:58pm UTC](https://root-forum.cern.ch/t/problem-w-vector-vector-after-opening-file-w-that-type/14772/10 "2012-08-21T18:58:25Z")

</div>

Well, Philippe proposed to use “MakeProject” but, in case of this file, it doesn’t seem to be working at all.  
It seems to me that for both classes (v.v.f and v.v.v.f) one needs to “GenerateDictionary” manually.

---

<div class="post-metadata">

**Author:** ![wlav](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/w/f07891/32.png) [@wlav](https://root-forum.cern.ch/u/wlav)\
**Post date:** [August 21, 2012, 7:03pm UTC](https://root-forum.cern.ch/t/problem-w-vector-vector-after-opening-file-w-that-type/14772/11 "2012-08-21T19:03:33Z")

</div>

Pepe,

Right, something funny is going on with TClass.

For now, I’ve checked the above snippet of code into v5-34-00-patches, as it appears to work in all cases. I’ll await a better solution. 🙂

Cheers,  
Wim

---

<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:** [August 24, 2012, 2:38pm UTC](https://root-forum.cern.ch/t/problem-w-vector-vector-after-opening-file-w-that-type/14772/12 "2012-08-24T14:38:16Z")

</div>

Hi,

Wim is correct. The problem was that in the case where the TClass for a STL collection (like vector\<vector \>) was accessed before the loading of the dictionary (i.e. opening the TFile before loading/creating the dictionary for vector\<vector \>).

This problem is solved by revision 45694 of the trunk and revision 45695 of the v5.34 patch branch.

Cheers,  
Philippe.

---

<div class="post-metadata">

**Author:** ![Pepe\_Le\_Pew](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pepe_le_pew/32/12365_2.png) [@Pepe\_Le\_Pew](https://root-forum.cern.ch/u/Pepe_Le_Pew)\
**Post date:** [September 12, 2012, 6:48pm UTC](https://root-forum.cern.ch/t/problem-w-vector-vector-after-opening-file-w-that-type/14772/13 "2012-09-12T18:48:05Z")

</div>

Just to make it clear.  
The fix, mentioned in the previous post, makes it possible to load dictionaries either before opening the file (the only option working before) or after opening the file (what was fixed).  
However, one still needs to create and load appropriate dictionaries fully manually (and the “MakeProject” still doesn’t work in this case either).
