How to access to std::vector<TVector3> in a TBranch?

Dear experts

I want to read a std::vector<TVector3> branch and here is what I do:

//include files
#pragma link C++ class std::vector<TVector3>+;
void drawShape(){
    TFile* f = new TFile("file.root", "READ");
    TTree* t = (TTree*)f->Get("t");
    std::vector<TVector3>* pos;
    t->SetBranchAddress("pos", &pos);
    for(int i=0; i<t->GetEntries(); ++t){
        t->GetEntry(i);
        for(auto& p : (*pos)){
            //do something
        }
    }
}

And I do: root -l drawShape.cxx+
But I got seg fault with the message:


Sorry I was not able to copy error message, what did I do wrong to get this error?

_ROOT Version: 6.18.00
_Platform: CentOS 7
Compiler: Not Provided


Hi @Crisps,

Thanks for your question.

Sorry that you’re experiencing issues. Would it be possible for you to provide a minimal reproducer with the root file so that I can try to reproduce the issue locally.

Cheers,
Dev

Most likely replacing

with

    std::vector<TVector3>* pos = nullptr;

should solve the problem (without this the TTree used some arbitrary memory location as the place where to write the vector)

You are right! This solves the problem