Tree friendship in root file and chains

Hello,

I wonder how to save “friendship” of two trees in root files, and then restore this friendship when using a Tchain

Basically, here’s my pseudo-code:

TFile *fout = new TFile(filename_out,"RECREATE");

TTree *tRawData = new TTree("tRawData","Donnees brutes");
TTree *tCorrectedData = new TTree("tCorrectedData","Corrigees");

<...  Filling trees ....>

tRawData->AddFriend("tCorrectedData");
fout->Write();
fout->Close();

TChain cFull("tRawData");
for (i=fileindex_start; i<=fileindex_stop; i++) {
        sprintf(filename_in,"%s_%03d.root",basename, i);
        cFull.Add(filename_in);
}
cFull.Draw("Q2shift >> htmp");

But I get : Bad numerical expression : “Q2shift”

Looks like “Q2shift” branch is not known to cFull…

Any suggestions ?
Thanks in advance.

nico, using 4.04/02 under WinXP

Ok, after all, it seems to me that friendship IS NOT recorded in root file. So, I try something else, but it is so strange

here are my 3 root files (dat_001,dat_002, dat_003), each containing 2 Trees: tRawData and tCorrectedData:

nicolas.charvin.free.fr/bazar/root/

Then, I try to chain them:

TChain ch("tRawData");
ch.Add("dat_001.root");
ch.Add("dat_002.root");
ch.Add("dat_003.root");

ch.Draw("Q2");

it works perfectly

then [code]
TChain ch2(“tCorrectedData”);
ch2.Add(“dat_001.root”);
ch2.Add(“dat_002.root”);
ch2.Add(“dat_003.root”);

ch2.Draw(“Q2shift”);
[/code]

again, it works perfectly

but ch.AddFriend("tCorrectedData"); ch.Draw("Q2shift");

I get the right number of entries, but a lot are equal to Zero. And I don’t know why…

Does anyone have a suggestion ?
Thanks

nico
FriendFull.C (4.85 KB)

[quote]Code:
ch.AddFriend(“tCorrectedData”);
ch.Draw(“Q2shift”);
I get the right number of entries, but a lot are equal to Zero. And I don’t know why…
[/quote]The problem is that the name ‘tCorrectedData’ is ambiguous and could refer to the TTree or the TChain (and it get the wrong). Instead use

ch.AddFriend(&ch2); ch.Draw("Q2shift");

[quote]Ok, after all, it seems to me that friendship IS NOT recorded in root file.[/quote]It is:

root [0] TFile *_file0 = TFile::Open("dat_001.root") root [1] tRawData->GetListOfFriends()->ls() Friend Tree: tCorrectedData in file:However (and it is a bug) it does not seem to be used when you go through the TChain. I will check into that.

For example this works:

root [0] TFile *_file0 = TFile::Open("dat_001.root") root [1] tRawData.Draw("Q2shift");
Cheers,
Philippe.

[quote=“pcanal”]The problem is that the name ‘tCorrectedData’ is ambiguous and could refer to the TTree or the TChain (and it get the wrong). Instead use

ch.AddFriend(&ch2); ch.Draw("Q2shift");
[/quote]

Hey, this syntax works ??!! :open_mouth:
It looks much more familiar to my (little) programming background. And it probably works elsewhere in my root scripts. Thanks a lot, it will probably help me to understand ROOT a bit further.

Great! Now I can automate all my data reduction process.
Thanks!

nicolas

[quote][The friendship] is [recorded in the file]:

root [0] TFile *_file0 = TFile::Open("dat_001.root") root [1] tRawData->GetListOfFriends()->ls() Friend Tree: tCorrectedData in file:
However (and it is a bug) it does not seem to be used when you go through the TChain. I will check into that.
[/quote]This problem is now fixed in the CVS repository. This means that with your file the following now works:[code]TChain ch(“tRawData”);
ch.Add(“dat_001.root”);
ch.Add(“dat_002.root”);
ch.Add(“dat_003.root”);

ch.Draw(“Q2shift”); [/code]Cheers,
Philippe.