Problem extracting variables from TChain!

Hi every one

I am trying to manipulate variables inside a TChain but something goes wrong.

First of all I creat three Tchain, t , t2 and t3 with the same number of entries in this way:

       TChain t("T");
       TChain t2("som");
       TChain t3("sn");

         t.Add("ntp-480.root");
         t.Add("ntp-481.root");
          t.Add("ntp-482.root");

          t2.Add("som-480.root");
          t2.Add("som-481.root");
          t2.Add("som-482.root");

          t3..Add("som-480-sn.root");
          t3.Add("som-481-sn.root");
          t3.Add("som-482-sn.root");

 then I add t2 and t3 as friends to t. 


       t.AddFriend("som");
       t.AddFriend("sn");

 at this point everything works. If a use the Draw() functions everything is correct.   In order to manipulate variables inside t,  I use TLeafs: 


         TLeaf *leaf1 = t.GetLeaf("var1);
         TLeaf *leaf2 = t.GetLeaf("var2");
         TLeaf *leaf3 = t.GetLeaf("var3");

then with the iteration:

      for(i=0;i<t>GetBranch()->GetEntry(i);
             leaf2->GetBranch()->GetEntry(i);
             leaf3->GetBranch()->GetEntry(i);

             cout<<leaf1>GetValue()<<" "<<leaf2>GetValue()<<" "<<leaf3>GetValue()<<" "<<leaf4>GetValue()<<endl; 

}

    it doesn't work! It seems that it considers just the entries from the last file added and the others are messed up... 

Am I doing something wrong? Any suggestions? 

Thanks,


Marco.

Hi,

The TLeaf (and TBranch object) that are retrieved from a TChain actually belong to the local TTree. One need to retrieve the value each and every time a local TTree is retrieved by TChain (usually this means when the value of TChain::GetTreeNumber changes).

Instead of calling TLeaf::GetValue, you should use SetBranchAddress. For a good example on how to do so, just look at the file produced when you call chain->MakeSelector or chain->MakeClass.

Cheers,
Philippe.