From $ROOTSYS/tutorials/tree/tree3.C

#include "TFile.h"
#include "TTree.h"
#include "TRandom.h"
#include "TCanvas.h"

void tree3w() {
// Example of a Tree where branches are variable length arrays
// A second Tree is created and filled in parallel.
// Run this script with
//   .x tree3.C
// In the function treer, the first Tree is open.
// The second Tree is declared friend of the first tree.
// TTree::Draw is called with variables from both Trees.
//
//  Author: Rene Brun

   const Int_t kMaxTrack = 500;
   Int_t ntrack;
   Int_t stat[kMaxTrack];
   Int_t sign[kMaxTrack];
   Float_t px[kMaxTrack];
   Float_t py[kMaxTrack];
   Float_t pz[kMaxTrack];
   Float_t pt[kMaxTrack];
   Float_t zv[kMaxTrack];
   Float_t chi2[kMaxTrack];
   Double_t sumstat;

   TFile f("tree3.root","recreate");
   TTree *t3 = new TTree("t3","Reconst ntuple");
   t3->Branch("ntrack",&ntrack,"ntrack/I");
   t3->Branch("stat",stat,"stat[ntrack]/I");
   t3->Branch("sign",sign,"sign[ntrack]/I");
   t3->Branch("px",px,"px[ntrack]/F");
   t3->Branch("py",py,"py[ntrack]/F");
   t3->Branch("pz",pz,"pz[ntrack]/F");
   t3->Branch("zv",zv,"zv[ntrack]/F");
   t3->Branch("chi2",chi2,"chi2[ntrack]/F");

   TFile fr("tree3f.root","recreate");
   TTree *t3f = new TTree("t3f","a friend Tree");
   t3f->Branch("ntrack",&ntrack,"ntrack/I");
   t3f->Branch("sumstat",&sumstat,"sumstat/D");
   t3f->Branch("pt",pt,"pt[ntrack]/F");

   for (Int_t i=0;i<1000;i++) {
      Int_t nt = gRandom->Rndm()*(kMaxTrack-1);
      ntrack = nt;
      sumstat = 0;
      for (Int_t n=0;n<nt;n++) {
         stat[n] = n%3;
         sign[n] = i%2;
         px[n]   = gRandom->Gaus(0,1);
         py[n]   = gRandom->Gaus(0,2);
         pz[n]   = gRandom->Gaus(10,5);
         zv[n]   = gRandom->Gaus(100,2);
         chi2[n] = gRandom->Gaus(0,.01);
         sumstat += chi2[n];
         pt[n]   = TMath::Sqrt(px[n]*px[n] + py[n]*py[n]);
      }
      t3->Fill();
      t3f->Fill();
   }
   t3->Print();
   f.cd();
   t3->Write();
   fr.cd();
   t3f->Write();
}

void tree3r()
{
   TFile *f = new TFile("tree3.root");
   TTree *t3 = (TTree*)f->Get("t3");
   t3->AddFriend("t3f","tree3f.root");
   t3->Draw("pz","pt>3");
}

void tree3r2()
{
   TPad *p = new TPad("p","p",0.6, 0.4, 0.98, 0.8);
   p->Draw(); p->cd();
   TFile *f1 = new TFile("tree3.root");
   TFile *f2 = new TFile("tree3f.root");
   TTree *t3 = (TTree*)f1->Get("t3");
   t3->AddFriend("t3f",f2);
   t3->Draw("pz","pt>3");
}

void tree3()
{
   tree3w();
   tree3r();
   tree3r2();
}
 tree3.C:1
 tree3.C:2
 tree3.C:3
 tree3.C:4
 tree3.C:5
 tree3.C:6
 tree3.C:7
 tree3.C:8
 tree3.C:9
 tree3.C:10
 tree3.C:11
 tree3.C:12
 tree3.C:13
 tree3.C:14
 tree3.C:15
 tree3.C:16
 tree3.C:17
 tree3.C:18
 tree3.C:19
 tree3.C:20
 tree3.C:21
 tree3.C:22
 tree3.C:23
 tree3.C:24
 tree3.C:25
 tree3.C:26
 tree3.C:27
 tree3.C:28
 tree3.C:29
 tree3.C:30
 tree3.C:31
 tree3.C:32
 tree3.C:33
 tree3.C:34
 tree3.C:35
 tree3.C:36
 tree3.C:37
 tree3.C:38
 tree3.C:39
 tree3.C:40
 tree3.C:41
 tree3.C:42
 tree3.C:43
 tree3.C:44
 tree3.C:45
 tree3.C:46
 tree3.C:47
 tree3.C:48
 tree3.C:49
 tree3.C:50
 tree3.C:51
 tree3.C:52
 tree3.C:53
 tree3.C:54
 tree3.C:55
 tree3.C:56
 tree3.C:57
 tree3.C:58
 tree3.C:59
 tree3.C:60
 tree3.C:61
 tree3.C:62
 tree3.C:63
 tree3.C:64
 tree3.C:65
 tree3.C:66
 tree3.C:67
 tree3.C:68
 tree3.C:69
 tree3.C:70
 tree3.C:71
 tree3.C:72
 tree3.C:73
 tree3.C:74
 tree3.C:75
 tree3.C:76
 tree3.C:77
 tree3.C:78
 tree3.C:79
 tree3.C:80
 tree3.C:81
 tree3.C:82
 tree3.C:83
 tree3.C:84
 tree3.C:85
 tree3.C:86
 tree3.C:87
 tree3.C:88
 tree3.C:89
 tree3.C:90
 tree3.C:91
 tree3.C:92
 tree3.C:93
 tree3.C:94
 tree3.C:95
 tree3.C:96