To read an instance member's value stored in a branch

Hi there,

I’d like to store objects into a branch, and also split them. Let’s create an example:

	TTree tree;
	TObject *in_ptr = 0;

	tree.Branch("TheBranch", "TObject", &in_ptr, 3200, 99);

	new (in_ptr) TObject();
	in_ptr->SetUniqueID(1000);
 
	tree.Fill();
	tree.Print();

So we’ll get the following tree’s description:

******************************************************************************
*Tree    :          :                                                        *
*Entries :        1 : Total =            2263 bytes  File  Size =          0 *
*        :          : Tree compression factor =   1.00                       *
******************************************************************************
*Branch  :TheBranch                                                          *
*Entries :        1 : BranchElement (see below)                              *
*............................................................................*
*Br    0 :fUniqueID : UInt_t                                                 *
*Entries :        1 : Total  Size=        669 bytes  One basket in memory    *
*Baskets :        0 : Basket Size=       3200 bytes  Compression=   1.00     *
*............................................................................*
*Br    1 :fBits     : UInt_t                                                 *
*Entries :        1 : Total  Size=        653 bytes  One basket in memory    *
*Baskets :        0 : Basket Size=       3200 bytes  Compression=   1.00     *
*............................................................................*

So there is ‘fUniqueID’ branch. Now, I’d like to extract a value only from ‘fUniqueID’ branch, without extracting an entire TObject’s instance. Trying to allocate storage for it:

	UInt_t *out_ptr = 0;
	tree.SetBranchAddress("fUniqueID", &out_ptr);
	cout << "out_ptr = " << out_ptr << endl;

Here, out_ptr doesn’t change, i.e preserves a null value. But when I tried the following code instead,

	TObject *out_ptr = 0;
	tree.SetBranchAddress("TheBranch", &out_ptr);
	cout << "out_ptr = " << out_ptr << endl;

– this worked: it did allocate allocate memory for the top ‘TheBranch’.

So where I am wrong? And, how should I get values directly from the ‘fUniqueID’ branch?

Thanks!


ROOT Version: 6.18.04
Platform: Linux, macOS
Compiler: g++ 7.4.0 on Linux, clang-1001.0.46.4 on macOS


Beside the information provided by Wile.
Note that

UInt_t *out_ptr = 0; 
tree.SetBranchAddress("fUniqueID", &out_ptr); 
cout << "out_ptr = " << out_ptr << endl;

should be

UInt_t out; 
tree.SetBranchAddress("fUniqueID", &out); 
cout << "out = " << out << endl; // Note that out will be populated only after calls to GetEntry

That’s I’ve tried first :o) Nope, it seems to leave output untouched. Look, the following code

   UInt_t out = 999999;
   t.SetBranchAddress("fUniqueID", &out);
   t.GetEntry(0);
   cout << "out = " << out << endl;

Prints the initial out’s value, 999999.

try

   t.GetEntry(0); // to avoid some hystersis that may lead to the undoing of some SetBranchAddress.
   UInt_t out = 999999;
   t.SetMakeClass(1);
   t.SetBranchAddress("fUniqueID", &out);
   t.GetEntry(0);
   cout << "out = " << out << endl;

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