tree3.C

Go to the documentation of this file.
00001 #include "TFile.h"
00002 #include "TTree.h"
00003 #include "TRandom.h"
00004 #include "TCanvas.h"
00005 
00006 void tree3w() {
00007 // Example of a Tree where branches are variable length arrays
00008 // A second Tree is created and filled in parallel.
00009 // Run this script with
00010 //   .x tree3.C
00011 // In the function treer, the first Tree is open.
00012 // The second Tree is declared friend of the first tree.
00013 // TTree::Draw is called with variables from both Trees.
00014 //
00015 //  Author: Rene Brun
00016 
00017    const Int_t kMaxTrack = 500;
00018    Int_t ntrack;
00019    Int_t stat[kMaxTrack];
00020    Int_t sign[kMaxTrack];
00021    Float_t px[kMaxTrack];
00022    Float_t py[kMaxTrack];
00023    Float_t pz[kMaxTrack];
00024    Float_t pt[kMaxTrack];
00025    Float_t zv[kMaxTrack];
00026    Float_t chi2[kMaxTrack];
00027    Double_t sumstat;
00028 
00029    TFile f("tree3.root","recreate");
00030    TTree *t3 = new TTree("t3","Reconst ntuple");
00031    t3->Branch("ntrack",&ntrack,"ntrack/I");
00032    t3->Branch("stat",stat,"stat[ntrack]/I");
00033    t3->Branch("sign",sign,"sign[ntrack]/I");
00034    t3->Branch("px",px,"px[ntrack]/F");
00035    t3->Branch("py",py,"py[ntrack]/F");
00036    t3->Branch("pz",pz,"pz[ntrack]/F");
00037    t3->Branch("zv",zv,"zv[ntrack]/F");
00038    t3->Branch("chi2",chi2,"chi2[ntrack]/F");
00039 
00040    TFile fr("tree3f.root","recreate");
00041    TTree *t3f = new TTree("t3f","a friend Tree");
00042    t3f->Branch("ntrack",&ntrack,"ntrack/I");
00043    t3f->Branch("sumstat",&sumstat,"sumstat/D");
00044    t3f->Branch("pt",pt,"pt[ntrack]/F");
00045 
00046    for (Int_t i=0;i<1000;i++) {
00047       Int_t nt = gRandom->Rndm()*(kMaxTrack-1);
00048       ntrack = nt;
00049       sumstat = 0;
00050       for (Int_t n=0;n<nt;n++) {
00051          stat[n] = n%3;
00052          sign[n] = i%2;
00053          px[n]   = gRandom->Gaus(0,1);
00054          py[n]   = gRandom->Gaus(0,2);
00055          pz[n]   = gRandom->Gaus(10,5);
00056          zv[n]   = gRandom->Gaus(100,2);
00057          chi2[n] = gRandom->Gaus(0,.01);
00058          sumstat += chi2[n];
00059          pt[n]   = TMath::Sqrt(px[n]*px[n] + py[n]*py[n]);
00060       }
00061       t3->Fill();
00062       t3f->Fill();
00063    }
00064    t3->Print();
00065    f.cd();
00066    t3->Write();
00067    fr.cd();
00068    t3f->Write();
00069 }
00070 
00071 void tree3r()
00072 {
00073    TFile *f = new TFile("tree3.root");
00074    TTree *t3 = (TTree*)f->Get("t3");
00075    t3->AddFriend("t3f","tree3f.root");
00076    t3->Draw("pz","pt>3");
00077 }
00078 
00079 void tree3r2()
00080 {
00081    TPad *p = new TPad("p","p",0.6, 0.4, 0.98, 0.8);
00082    p->Draw(); p->cd();
00083    TFile *f1 = new TFile("tree3.root");
00084    TFile *f2 = new TFile("tree3f.root");
00085    TTree *t3 = (TTree*)f1->Get("t3");
00086    t3->AddFriend("t3f",f2);
00087    t3->Draw("pz","pt>3");
00088 }
00089 
00090 void tree3()
00091 {
00092    tree3w();
00093    tree3r();
00094    tree3r2();
00095 }

Generated on Tue Jul 5 15:45:11 2011 for ROOT_528-00b_version by  doxygen 1.5.1