Friends TChain SetBranchStatus

Dear experts,

I get a weird behaviour with TChain friends when activating only few branches.
I tried to reproduce the problem with the following macro: here I add t2 as friend of t1, switch on only branch x for t1. When reading t1, I am not able to get also the value for variable z from t2.

This is not the case if using TTree instead of TChain.

What am I doing wrong?

Thanks,
Antonio

void create_trees()
{
	TFile* fout = new TFile("test_friend.root","RECREATE");

	TTree* t1 = new TTree("t1","");
	TTree* t2 = new TTree("t2","");

	double x = 0;
	double y = 0;
	double z = 0;

	t1 -> Branch("x", &x, "x/D");
	t1 -> Branch("y", &y, "y/D");
	
	t2 -> Branch("z", &z, "z/D");

	TRandom rnd;
	for(auto e=0; e<10000; e++)
	{
		x = rnd.Gaus();
		y = rnd.Gaus();
		z = rnd.Gaus();

		t1 -> Fill();
		t2 -> Fill();
	}

	t1 -> Write();
	t2 -> Write();

	fout -> Close();
}

void read_trees()
{
	//TFile* fin = TFile::Open("test_friend.root");

	//TTree* t1 = (TTree*) fin -> Get("t1");
	//TTree* t2 = (TTree*) fin -> Get("t2");
	
	TChain* t1 = new TChain("t1");
	t1 -> Add("test_friend.root");
	TChain* t2 = new TChain("t2");
	t2 -> Add("test_friend.root");

	double x = 0;
	double y = 0;
	double z = 0;

	t1 -> SetBranchAddress("x", &x);
	t1 -> SetBranchAddress("y", &y);

	t1 -> SetBranchStatus("*", 0);
	t1 -> SetBranchStatus("x", 1);
	
	t2 -> SetBranchAddress("z", &z);

	t1 -> AddFriend(t2);

	int nentrs = t1 -> GetEntries();

	for(auto e=0; e<nentrs; e++)
	{
		t1 -> GetEntry(e);

		cout << "x = " << x << " / y = " << y << " / z = " << z << endl;
	}
}

_ROOT Version: 6.23/01


SetBranchStatus applies to both the main tree and its friends and the main tree statuses take precedence over the friend status. So adding:

t1 -> SetBranchStatus("z", 1);

should fix the problem.

1 Like

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