Problem using trees

Hello,

When I run the following code:

	TChain mc_c("KPiPiPiMassMeasTuple/DecayTree;1");
	mc_c.AddFile("../../MC_data/MC2012_KPiPiPi_Pythia8_Filtered_Down.root"); 
	mc_c.AddFile("../../MC_data/MC2012_KPiPiPi_Pythia8_Filtered_Up.root");

	TH1F * h_mc = new TH1F("h_mc", "MC Data Mass Distribution", 100, 480, 505);

	Int_t nentries;
	Double_t K_M;
	TBranch * b_K_M;

	nentries = mc_c.GetEntries();
	mc_c.SetBranchAddress("K_M", &K_M, &b_K_M);
	for (int j=0;j<nentries;j++){
		b_K_M->GetEntry(j);
		h_mc->Fill(K_M);
	}

	TCanvas * c = new TCanvas("c","c");
	h_mc->Draw();

h_mc will be filled correctly with the entries of the first root file. However, when looping through the second tree K_M is not updated, so the last value from tree #1 is just filled repeatedly.

Is this a bug, or am I mis-using the chain?

I can circumvent the bug by using

mc_c.setBranchStatus("*",0);
mc_c.setBranchStatus("K_M",1);

and in the for loop

mc_c.getEntry(i);

instead of only loading the specific branch. But on large trees it seems to me that this works slower?

Try to generate a simple “analysis skeleton” using:
mc_c.MakeClass(“Trial”);
and then have a look into the generated “Trial.C” file, into the “Trial::Loop” skeleton.

See also: How are multiple TTree->Draw()s done?

BTW. You should NOT have “;1” in the name of the chain, it should simply be:
TChain mc_c(“KPiPiPiMassMeasTuple/DecayTree”);

Hi,
the way I loop over a chain is slightly different. Try replacing those lines:

TChain mc_c(“KPiPiPiMassMeasTuple/DecayTree”); //I’ve removed the ;1, strange name for a tree, btw
mc_c.Add("…/…/MC_data/MC2012_KPiPiPi_Pythia8_Filtered_Down.root");
mc_c.Add("…/…/MC_data/MC2012_KPiPiPi_Pythia8_Filtered_Up.root");

  mc_c->GetEntry(j);
  h_mc->Fill(K_M);

Thanks for your answers!

I got it working with:

	mc_c.SetBranchAddress("K_M", &K_M, &b_K_M);
	for (int j=0;j<nentries;j++){
		Int_t entry = mc_c.LoadTree(j);
		b_K_M->GetEntry(entry);
		h_mc->Fill(K_M);
	}

I guess branch->GetEntry(j) was just called directly on the TTree and obviously the index was too large, so it just returned last entry?
Now I understand the documentation of TChain::LoadTree - it translates from global index to local index.