How to add a branch of user defined class

Hi, Thanks very much for your reply, I finally work it through following the Danilo’s reply in this thread.

I am able to write a vector of my user defined class into the root file, but now I have new issue of how to read them. The following code does not work:

void ReadROOTTree() {
  
  TFile file("tree.root", "READ");

  TTree* tree = dynamic_cast<TTree*>(file.Get("myTree"));


  std::vector<X> xVecPtr ;

  tree->SetBranchAddress("xBranch", &xVecPtr);

  // Read the first entry of the tree
  tree->GetEntry(0);


  // Close the file
  file.Close();
}

int main() {
  ReadROOTTree();
  return 0;
}

It pass the compilation but give out the error: Error in TTree::SetBranchAddress: The address for “xBranch” should be the address of a pointer!.

Further I have no idea how to use a pointer to access each element in the vector in that branch, otherwise copying the whole vector out would definitely be not efficient. Any suggestion would be appreciated.

Thanks,
Haolin