Resetting TRef after filling TTree

Hi all-

I am currently using two trees, each in their own file, saving a different object in each file. Each of these objects owns a TRef to the other object. A basic outline of the code is:

[code]class ClassA: public TObject
{
public:
TRef fReferenceToB;


};

class ClassB: public TObject
{
public:
TRef fReferenceToA;


};

TFile* ClassAFile = TFile::Open(“ClassAFile.root”, “recreate”);
TTree classATree(“classATree”, “classATree”);
TFile* ClassBFile = TFile::Open(“ClassBFile.root”, “recreate”);
TTree classBTree(“classBTree”, “classBTree”);

ClassA* classA = new ClassA();
ClassB* classB = new ClassB();
classATree.Branch(“ClassABranch”, &classA);
classBTree.Branch(“ClassBBranch”, &classB);

for (Int_t i=0; i<EventsInFile; i++ ){
classA->fReferenceToB = classB;
classB->fReferenceToA = classA;

classATree.Fill();
classBTree.Fill();

classA->Clear();
classB->Clear();
}
[/code]

The problem I am having is that the UUIDs of the TRefs are not getting cleared so that each “Event” or entry in the tree has the same Unique ID. This has the consequence that the references are not properly set to NULL as one cycles through the events. For example:

[code]// Assuming we read in the files created above
ClassA* classA = new ClassA();
ClassB* classB = new ClassB();
classATree->SetBranchAddress(“ClassABranch”, &classA);
classBTree->SetBranchAddress(“ClassBBranch”, &classB);

classATree->GetEntry(0);
cout << classA->fReferenceToB.GetObject() << endl;
// Will output NULL as expected

classBTree->GetEntry(0);
cout << classA->fReferenceToB.GetObject() << endl;
// Now it will properly resolve

classATree->GetEntry(1);
cout << classA->fReferenceToB.GetObject() << endl;
// Expect it to be NULL since I didn’t read the classBTree, but it is not
[/code]

The reason this is, is that the UUIDs of the objects don’t change and so the TRef variables always point to the same unique IDs. I was able to get these to have the expected behavior ( to return NULL in the last line ) by calling:

ResetBit( kIsReferenced );
ResetBit( kHasUUID );

in the classes’ Clear() functions, but I think this might not be appropriate. Does anyone have suggestions here or is there a more proper way to ensure that the TRefs refer to the object in a particular TTree Entry?

Thanks,
Mike

Hi,

This is indeed the correct way of reset the referenced object.

Cheers,
Philippe.