Re: Writing ROOT files within another program

From: Rene Brun (Rene.Brun@cern.ch)
Date: Wed Nov 24 1999 - 09:59:07 MET


Hi Anthony,
What you want to do is possible if you just want to do a simple write of
an object in your G4 session. Below, you will find a simple example with
a class A (modified from my previous posting of class A).
 the following files:
   -a one line compile/link script goA
   -A.h
   -A.cxx
   -Amain.cxx a simple main program writing 10 instances of A on a file

Rene Brun

//-------------file goA
g++ -o A -g -fPIC -I$ROOTSYS/include  A.cxx Amain.cxx \
 -L$ROOTSYS/lib -lCore -lCint  -lm -ldl -rdynamic

//-------------file A.h
#ifndef __A_HH__
#define __A_HH__
#include "TObject.h"

class A : public TObject {
public:

  A();
  virtual ~A();
  
  Float_t fA;
  Float_t fB;
  
  ClassDef(A,1)  //My small class example
};

#endif

//-------------file A.cxx
#include "A.h"

#include "TBuffer.h"
#include "TMemberInspector.h"

 ClassImp(A)

A::A() {
  fA = 1;
  fB = 2;
}

A::~A() {
}
   
void A::Streamer(TBuffer &R__b)
{
   // Stream an object of class A.

   if (R__b.IsReading()) {
      Version_t R__v = R__b.ReadVersion(); if (R__v) { }
      TObject::Streamer(R__b);
      R__b >> fA;
      R__b >> fB;
   } else {
      R__b.WriteVersion(A::IsA());
      TObject::Streamer(R__b);
      R__b << fA;
      R__b << fB;
   }
}

void A::ShowMembers(TMemberInspector &, char *) {}

//-----------------------file Amain.cxx
#include "TROOT.h"
#include "TFile.h"
#include "TRandom.h"
#include "A.h"
      
TROOT simple("simple","trees");
 
int main()
 {


   A *a = new A; 
   char name[10];
   TFile f("A.root","recreate");
   for (Int_t i=0;i<10;i++) {
      a->fA = gRandom->Rndm();
      a->fB = gRandom->Gaus(0,1);
      sprintf(name,"A%d",i);
      a->Write(name);
   }
   f.ls();
   return 0;
 }
 

Anthony Faust wrote:
> 
> 
>         I am writing a class that is intended to be used both with a Geant4
> program, where the class will only be filled an written to a ROOT file,
> and as a stand alone analysis class to be loaded in a ROOT session and will then
> read back the generated G4 ROOT files.
> 
>         To do this, I created a class which inclued methods to fill my class
> object in the G4 environment all hidden behind preprocessor diectives so they
> would not be seem when working in ROOT. Similarly, I would like to hide as much
> of the ROOT information from G4 as I can so as to minimize the code size.
> However, I can no seem to do this without the linker complaining about a number
> of missing definitions.
> 
>         What then would be the minimal definition of a class so that its members
> could be written to a ROOT file? Can I simply include a Class::Streamer function
> or must I link in the full ClassDict.XX files (These are the ones I would like
> to avoid) ?
> 
>         Thanks.
> 
> _____________
> Dr. Anthony A. Faust                            | Anthony.Faust@dres.dnd.ca
> Defence Research Establishment Suffield         | PH:  (403) 544-5362
> Box 4000, Medicine Hat, Alberta, CANADA T1A 8K6 | FAX: (403) 544-4704



This archive was generated by hypermail 2b29 : Tue Jan 04 2000 - 00:43:43 MET