//---------- Here are two dummy classes--------------- class TarItem : public TObject{ public: TarItem() : TObject(){} TArrayI *tarData; }; class Tar2Item : public TObject{ public: Tar2Item() : TObject(){} TArrayI *tar2Data; //NOT WORKING VERSION //TArrayI tar2Data; //VORKING VERSION Int_t dummy = 0; }; //---------- Here are two dummy classes--------------- //----main routine------------ void TArrayTest() { gROOT->Reset(); // Create the first root file (Tar) TFile *findex; TTree *tindex; TarItem *tar = new TarItem(); findex = new TFile("Tar.root", "CREATE"); tindex = new TTree("Index", "Level0 Index file"); tindex->Branch("Tar", "TarItem", &tar); //Insert the data into Tar Int_t data[] = {0,1,2,3,4,5,6,7,8,9}; for (Int_t i = 0; i < 5; i++){ tar = new TarItem(); tar->tarData = new TArrayI(10, data); tindex->Fill(); } findex->Write(); findex->Close(); // Read the root file just for test cout << "Start Tar scan \n"; TFile *tf = new TFile("Tar.root", "READ"); TTree *tree = (TTree*)tf->Get("Index"); TarItem *ind = new TarItem(); tree->SetBranchAddress("Tar", &ind); for (Int_t i = 0; i < tree->GetEntries(); i++){ tree->GetEntry(i); for (Int_t j = 0; j < 10; j++){ cout << "indice " << j << " : " << (Int_t)ind->tarData->At(j) << "\n"; } } tf->Close(); cout << "End scan Tar close \n"; // create the second root file (Tar2) TFile *f2index; TTree *t2index; Tar2Item *tar2 = new Tar2Item(); f2index = new TFile("Tar2.root", "CREATE"); t2index = new TTree("Index2", "Level0 Index file"); t2index->Branch("Tar2", "Tar2Item", &tar2); //Insert the data into Tar2 Int_t data[] = {10,11,12,13,14,15,16,17,18,19}; for (Int_t i = 0; i < 5; i++){ tar2 = new Tar2Item(); //tar2->tar2Data = TArrayI(10, data); //WORKING VERION tar2->tar2Data = new TArrayI(10, data); //NOT WORKING VERION t2index->Fill(); } f2index->Write(); f2index->Close(); cout << "Start Tar2 scan \n"; TFile *tf2 = new TFile("Tar2.root", "READ"); TTree *tree2 = (TTree*)tf2->Get("Index2"); Tar2Item *ind2 = new Tar2Item(); tree2->SetBranchAddress("Tar2", &ind2); // Read the root file just for test for (Int_t i = 0; i < tree2->GetEntries(); i++){ tree2->GetEntry(i); for (Int_t j = 0; j < 10; j++){ cout << "indice " << j << " : " << (Int_t)ind2->tar2Data->At(j) << "\n"; } } tf2->Close(); cout << "End scan Tar close \n"; //Now I want to read the two files using the FRIEND property cout << "--------------- Friend Start Tar scan \n"; tf = new TFile("Tar.root", "READ"); tf2 = new TFile("Tar2.root", "READ"); tree = (TTree*)tf->Get("Index"); //----------------------------------------- // ***** the troubles begin here ********** //----------------------------------------- //---------NOT WORKING VERSION------------ tree->AddFriend("Index2", tf2); Tar2Item *ind2 = 0; tree->SetBranchAddress("Index2.Tar2", &ind2); for (Int_t i = 0; i < tree->GetEntries(); i++){ tree->GetEntry(i); for (Int_t j = 0; j < 10; j++){ cout << "indice " << j << " : " << (Int_t)ind2->tar2Data->At(j) << "\n"; } } //---------------------------------------- //---------WORKING VERSION------------ /* tree->AddFriend("Index2", tf2); Tar2Item *ind2 = 0; tree->SetBranchAddress("Tar2", &ind2); TArrayI *outTar; for (Int_t i = 0; i < tree->GetEntries(); i++){ tree->GetEntry(i); for (Int_t j = 0; j < 10; j++){ cout << "indice " << j << " : " << (Int_t)ind2->tar2Data.At(j) << "\n"; } } */ //---------WORKING VERSION------------ tf->Close(); tf2->Close(); cout << "----FRIEND End scan close \n"; }