Writing an object to tree

I write some object (of class TTree) to tree with this routine:

const unsigned char size = 5;
  TString strings[size] = {"AA", "B2", "wwer", "yrty", "y6"};
  TFile * treefile = TFile::Open("treefile.root", "RECREATE");
  TTree * tree = new TTree("test", "test");
  TString string;
  tree -> Branch("string", &string);
  for (unsigned char i = 0; i < size; i ++)
    {
      string = strings[i];
      tree -> Fill();
    }
  tree -> Write();
  treefile -> Close()

Then I reead the tree with this routine:

TFile * f = TFile::Open("treefile.root");
  TTree * tree = (TTree *) f -> Get("test");
  TString string;
  tree -> SetBranchAddress("string", &string);
  for (unsigned long i = 0; i < tree -> GetEntries(); i ++)
    {
      tree -> GetEntry(i);
      printf("%lu %s\n", i, string.Data());
    }
  f -> Close();

However, I get an error:

Error in <TTree::SetBranchAddress>: The address for "string" should be the address of a pointer!

What is wrong?


Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


I figured out myself. In the reading routine I need to change to:

  TString * string; ## pointer
  tree -> SetBranchAddress("string", &string);

But it is not clearly prescribed in TTree::SetBranchAddress

Note, it must be initialized, for example with

  TString * string = nullptr; ## pointer
1 Like

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