tcl.C

Go to the documentation of this file.
00001 // Example of macro illustrating how to write a TClonesArray to a TTree
00002 // the following tests can be run
00003 //    Interactive tests
00004 // Root > .x tcl.C        //no-split interpreted
00005 // Root > .x tcl.C(1)     //split    interpreted
00006 // Root > .x tcl.C++      //no-split compiled
00007 // Root > .x tcl.C++(1)   //split    compiled
00008 //    batch tests: same as above but with no graphics
00009 // root -b -q tcl.C
00010 // root -b -q tcl.C++
00011 // root -b -q "tcl.C(1)"
00012 // root -b -q "tcl.C++(1)"
00013 //
00014 //Author: Rene Brun
00015 
00016 #include "TFile.h"
00017 #include "TClonesArray.h"
00018 #include "TH2.h"
00019 #include "TLine.h"
00020 #include "TTree.h"
00021 #include "TBenchmark.h"
00022 #include "TRandom.h"
00023 
00024 void tclwrite(Int_t split)
00025 {
00026 // Generate a Tree with a TClonesArray
00027 // The array can be split or not
00028    TFile f("tcl.root","recreate");
00029    f.SetCompressionLevel(1); //try level 2 also
00030    TTree T("T","test tcl");
00031    TClonesArray *arr = new TClonesArray("TLine");
00032    TClonesArray &ar = *arr;
00033    T.Branch("tcl",&arr,256000,split);
00034    //By default a TClonesArray is created with its BypassStreamer bit set.
00035    //However, because TLine has a custom Streamer, this bit was reset 
00036    //by TTree::Branch above. We set again this bit because the current 
00037    //version of TLine uses the automatic Streamer.
00038    //BypassingStreamer saves space and time.
00039    arr->BypassStreamer();
00040    for (Int_t ev=0;ev<10000;ev++) {
00041       ar.Clear();
00042       Int_t nlines = Int_t(gRandom->Gaus(50,10));
00043       if(nlines < 0) nlines = 1;
00044       for (Int_t i=0;i<nlines;i++) {
00045          Float_t x1 = gRandom->Rndm();
00046          Float_t y1 = gRandom->Rndm();
00047          Float_t x2 = gRandom->Rndm();
00048          Float_t y2 = gRandom->Rndm();
00049          new(ar[i]) TLine(x1,y1,x2,y2);
00050       }
00051       T.Fill();
00052    }
00053    T.Print();
00054    T.Write();
00055 }
00056 
00057 void tclread()
00058 {
00059 // read file generated by tclwrite
00060 // loop on all entries.
00061 // histogram center of lines
00062    TFile *f = new TFile("tcl.root");
00063    TTree *T = (TTree*)f->Get("T");
00064    TH2F *h2 = new TH2F("h2","center of lines",40,0,1,40,0,1);
00065 
00066    TClonesArray *arr = new TClonesArray("TLine");
00067    T->GetBranch("tcl")->SetAutoDelete(kFALSE);
00068    T->SetBranchAddress("tcl",&arr);
00069    Int_t nentries = (Int_t)(T->GetEntries());
00070    for (Int_t ev=0;ev<nentries;ev++) {
00071       arr->Clear();
00072       T->GetEntry(ev);
00073       Int_t nlines = arr->GetEntriesFast();
00074       for (Int_t i=0;i<nlines;i++) {
00075          TLine *line = (TLine*)arr->At(i);
00076          h2->Fill(0.5*(line->GetX1()+line->GetX2()), 0.5*(line->GetY1()+line->GetY2()));
00077       }
00078    }
00079    h2->Draw("lego");
00080 }
00081 
00082 void tcl(Int_t split=0)
00083 {
00084    gBenchmark->Start("tcl");
00085    tclwrite(split);
00086    tclread();
00087    gBenchmark->Show("tcl");
00088 }

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