Can't obtain std::vector from Root file by name

ROOT Version: 6.10.06
Platform, compiler: ArchLinux, gcc 7.3.0

After writing an std::vector in aRoot file I can’t get it back in a Root shell session by simply typing its name. Here’s a little demonstrator:

root [0] std::vector<int> test
(std::vector<int> &) {}
root [1] auto *f = TFile::Open("test.root", "RECREATE")
(TFile *) 0x559e43e48af0
root [2] f->WriteObjectAny(&test, TClass::GetClass("std::vector<int>"), "vectorOfInt")
(int) 67
root [3] f->Close()
root [4] auto *f2 = TFile::Open("test.root")
(TFile *) 0x559e43e58e20
root [5] .ls
TFile**         test.root
 TFile*         test.root
  KEY: vector<int>      vectorOfInt;1   object title
root [6] vectorOfInt
input_line_57:2:3: error: use of undeclared identifier 'vectorOfInt'

Using TFile::Get I can make it work:

root [7] auto *vect = (std::vector<int>*) (f2->Get("vectorOfInt"))
(std::vector<int, allocator<int> > *) 0x561ead369170
root [8] vect->size()
(unsigned long) 0

so it’s not a big issue, but still just typing the name of the object would be more handy. Is this problem related to the fact that the object is not a TObject? Will it be fixed?

std::vector<int> *vect; f2->GetObject("vectorOfInt", vect);

std::vector *vect; f2->GetObject(“vectorOfInt”, vect);

Well, this is more or less the workaround I mention in my original post, but what I would like to be able to do is something like:


in the same way you can do for e.g. a TH1F stored in a Root file. To be more specific, if for a TH1F I do the same operations I described for std::vector then I get no error:

root [0] TH1F h
(TH1F &) Name:  Title:  NbinsX: 1
root [1] auto *f = TFile::Open("test.root", "RECREATE")
(TFile *) 0x5625507a82d0
root [2] f->WriteObjectAny(&h, TClass::GetClass("TH1F"), "histo")
(int) 226
root [3] f->Close()
root [4] auto *f2 = TFile::Open("test.root")
(TFile *) 0x562550b4f580
root [5] .ls
TFile**         test.root
 TFile*         test.root
  KEY: TH1F     histo;1 object title
root [6] histo->GetName()
(const char *) ""

Indeed, that shortcut is only available for types inheriting from TObject. I have created

Thanks Axel, I’ll follow the evolution of this issue on Jira.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.