Re: [ROOT] TNamed inheritance and Trees

From: Rene Brun (Rene.Brun@cern.ch)
Date: Thu Apr 13 2000 - 15:08:01 MEST


Hi Steve,
Your program is OK, but you cannot use the split mode because your event
object contains (in TNamed) data members with variable length  basic
types.
See http://root.cern.ch/root/HowtoWriteTree.html "Making a Tree with
many branches"

Rene Brun

Steve Lautenschlager wrote:
> 
> Hello,
> 
> I have written a very basic class which inherits from TNamed.
> When creating a TTree, I appear to be setting the name with
> myTest->SetName("JunkName");
> 
> but I can't read it back in from the file with
> myTest->GetName();
> 
> I'm just assuming that the TNamed functions and data members are all
> inherited properly and that the default streamer is sufficient here.
> 
> _Should_ this work as I am expecting?  Code follows.
> 
> Thanks,
> Steve
> 
> #ifndef ROOT_TsTest
> #define ROOT_TsTest
> ////////////////////////////////////////////////////////////////
> //
> // file: TsTest.h
> //
> ////////////////////////////////////////////////////////////////
> 
> #include "TObject.h"
> #include "TNamed.h"
> #include "TClonesArray.h"
> #include "TH1.h"
> #include "TMath.h"
> 
> #include <iostream.h>
> 
> class TsTest : public TNamed {
> 
> private:
> 
> public:
>   TsTest();
>   TsTest(Text_t *name, Text_t *title);
>   virtual ~TsTest();
> 
>   ClassDef(TsTest,1)  //Event structure
> };
> 
> #endif
> 
> ////////////////////////////////////////////////////////////////
> //
> // file: TsTest.cxx
> //
> ////////////////////////////////////////////////////////////////
> #include "TsTest.h"
> ClassImp(TsTest)
> 
> //_____________________________________________________________
> TsTest::TsTest() : TNamed()
> {
>   // Default ctor for TsTest
> }
> 
> //____________________________________________________________
> TsTest::TsTest(Text_t *name, Text_t *title) : TNamed(name, title)
> {
>   // Ctor
> }
> 
> //_____________________________________________________________
> TsTest::~TsTest()
> {
>   // Dtor for TsTest
> }
> 
> ////////////////////////////////////////////////////////////////
> //
> // file: TsTestMain.cxx
> //
> ////////////////////////////////////////////////////////////////
> #include <stdlib.h>
> #include <iostream.h>
> #include <stdio.h>
> 
> #include "TROOT.h"
> #include "TFile.h"
> #include "TRandom.h"
> #include "TTree.h"
> #include "TBranch.h"
> #include "TClonesArray.h"
> #include "TStopwatch.h"
> 
> #include "TsTest.h"
> 
> //_________________________________________________________________
> int main(int argc, char **argv)
> {
>    TROOT simple("simple","Example of creation of a tree");
> 
>    Int_t nevent = 400;     // by default create 400 events
>    Int_t comp   = 1;       // by default file is compressed
>    Int_t split  = 1;       // by default, split Event in sub branches
>    Int_t write  = 1;       // by default the tree is filled
>    Int_t hfill  = 0;       // by default histograms are not filled
>    Int_t read   = 0;
>    Int_t arg4   = 1;
>    Int_t arg5   = 600;     //default number of tracks per event
> 
>    if (argc > 1)  nevent = atoi(argv[1]);
>    if (argc > 2)  comp   = atoi(argv[2]);
>    if (argc > 3)  split  = atoi(argv[3]);
>    if (argc > 4)  arg4   = atoi(argv[4]);
>    if (argc > 5)  arg5   = atoi(argv[5]);
>    if (arg4 ==  0) { write = 0; hfill = 0; read = 1;}
>    if (arg4 ==  1) { write = 1; hfill = 0;}
>    if (arg4 == 10) { write = 0; hfill = 1;}
>    if (arg4 == 11) { write = 1; hfill = 1;}
>    if (arg4 == 20) { write = 0; read  = 1;}  //read sequential
>    if (arg4 == 25) { write = 0; read  = 2;}  //read random
> 
>    TFile *hfile;
>    TTree *tree;
>    TsTest *mytest = new TsTest();
> 
>    //   Create a timer object to benchmark this loop
>    TStopwatch timer;
>    timer.Start();
>    Int_t nb = 0;
>    Int_t ev;
>    Int_t bufsize;
>    Double_t told = 0;
>    Double_t tnew = 0;
>    Int_t printev = 100;
>    if (arg5 < 100) printev = 1000;
>    if (arg5 < 10)  printev = 10000;
> 
> //         Read case
>    if (read) {
>       hfile = new TFile("Test.root");
>       tree = (TTree*)hfile->Get("T");
>       TBranch *branch = tree->GetBranch("test");
>       branch->SetAddress(&mytest);
>       Int_t nentries = (Int_t)tree->GetEntries();
>       nevent = TMath::Max(nevent,nentries);
>       if (read == 1) {  //read sequential
>          for (ev = 0; ev < nevent; ev++) {
>            if (ev%printev == 0) cout<<"event_here1="<<ev<<endl;
>            nb += tree->GetEntry(ev);        //read complete event in memory
>            cout << mytest->GetName() << endl;
>          }
>       } else {    //read random
>          Int_t evrandom;
>          for (ev = 0; ev < nevent; ev++) {
>            if (ev%printev == 0) cout<<"event_here2="<<ev<<endl;
>            evrandom = Int_t(nevent*gRandom->Rndm(1));
>            nb += tree->GetEntry(evrandom);  //read complete event in memory
>            const Text_t *name = mytest->GetName();
>            cout << name << endl;
> 
>          }
>       }
>    } else {
> //         Write case
>       // Create a new ROOT binary machine independent file.
>       // Note that this file may contain any kind of ROOT objects,
> histograms,
>       // pictures, graphics objects, detector geometries, tracks, events,
> etc..
>       // This file is now becoming the current directory.
>       hfile = new TFile("Test.root","RECREATE","Test ROOT file");
>       hfile->SetCompressionLevel(comp);
> 
>      // Create histogram to show write_time in function of time
>      Float_t curtime = -0.5;
>      Int_t ntime = nevent/printev;
>      TH1F *htime = new TH1F("htime","Real-Time to write versus
> time",ntime,0,ntime);
> 
>      // Create a ROOT Tree and one superbranch
>       TTree *tree = new TTree("T","An example of a ROOT tree");
>       tree->SetAutoSave(1000000000);  // autosave when 1 Gbyte written
>       bufsize = 256000;
>       if (split)  bufsize /= 4;
>       tree->Branch("test", "TsTest", &mytest, bufsize,split);
> 
>       for (ev = 0; ev < nevent; ev++) {
>          if (ev%printev == 0) {
>             tnew = timer.RealTime();
>             printf("event_here3:%d, rtime=%f s\n",ev,tnew-told);
>             htime->Fill(curtime,tnew-told);
>             curtime += 1;
>             told=tnew;
>             timer.Continue();
>          }
> 
>          Float_t sigmat, sigmas;
>          gRandom->Rannor(sigmat,sigmas);
>          Int_t ntrack   = Int_t(arg5 +arg5*sigmat/120.);
>          Float_t random = gRandom->Rndm(1);
> 
>          mytest->SetName("JunkName");
>          /*         event->SetHeader(ev, 200, 960312, random);
>          event->SetNseg(Int_t(10*ntrack+20*sigmas));
>          event->SetNvertex(1);
>          event->SetFlag(UInt_t(random+0.5));
>          event->SetTemperature(random+20.);
>          */
> 
>          if (write) nb += tree->Fill();  //fill the tree
> 
>       }
>       if (write) {
>          hfile->Write();
>          tree->Print();
>       }
>    }
> 
>    //  Stop timer and print results
>    timer.Stop();
>    Float_t mbytes = 0.000001*nb;
>    Double_t rtime = timer.RealTime();
>    Double_t ctime = timer.CpuTime();
> 
>    printf("\n%d events and %d bytes processed.\n",nevent,nb);
>    printf("RealTime=%f seconds, CpuTime=%f seconds\n",rtime,ctime);
>    if (read) {
>       printf("You read %f Mbytes/Realtime seconds\n",mbytes/rtime);
>       printf("You read %f Mbytes/Cputime seconds\n",mbytes/ctime);
>    } else {
>       printf("compression level=%d, split=%d, arg4=%d\n",comp,split,arg4);
>       printf("You write %f Mbytes/Realtime seconds\n",mbytes/rtime);
>       printf("You write %f Mbytes/Cputime seconds\n",mbytes/ctime);
>    }
>    hfile->Close();
>    return 0;
> }
> ______________________________________________________
> Get Your Private, Free Email at http://www.hotmail.com



This archive was generated by hypermail 2b29 : Tue Jan 02 2001 - 11:50:23 MET