Hi there,
Just for information (and to end the post root.cern.ch/phpBB2/viewtopic.php?t=3995),
here’s a way to couple Geant4 with a regular root class (containing TString or not) such as:
#ifndef TMyRootEvent_hpp
#define TMyRootEvent_hpp
#include "TObject.h"
#include "TString.h"
class TMyRootEvent : public TObject
{
protected :
Int_t fNCells ;
TString fParticleName ;
Double_t fEkin_0 ; // initial kinetic energy [MeV]
TString *fSensitiveCellName ; //[fNCells] sensitive cell name
Double_t *fEkin ; //[fNCells] kinetic energy at the entrance of each sensitive cell [MeV]
public :
TMyRootEvent ()
{
fNCells = -1 ;
fSensitiveCellName = NULL ;
fEkin = NULL ;
}
~TMyRootEvent ()
{
delete fSensitiveCellName ; fSensitiveCellName = NULL ;
delete fEkin ; fEkin = NULL ;
}
void InitEvent ( Int_t aNCells )
{
fNCells = aNCells ;
fSensitiveCellName = new TString [fNCells] ;
fEkin = new Double_t [fNCells] ;
return ;
}
// Getters...
// Setters...
ClassDef (TMyRootEvent,1) ;
} ;
#endif
The implementation has to be inside the class ! (before the end of the class “};”)
(Otherwise some “Multiple Definition” error messages appear…)
The GNUmakefile in that case looks like this:
name := exampleN01
G4TARGET := $(name)
G4EXLIB := true
.PHONY: all
all: dict.cxx lib bin
dict.cxx :
rootcint -f dict.cxx -c include/TMyRootEvent.hpp include/linkdef.h
myclean :
rm -rf dict.* $(G4TMP)/$(G4SYSTEM)/exampleN01
include $(G4INSTALL)/config/architecture.gmk
G4NOHIST := true
CPPFLAGS += $(shell root-config --cflags)
# Here comes dict.cxx
LDFLAGS += $(shell root-config --libs) dict.cxx
include $(G4INSTALL)/config/binmake.gmk
The include/linkdef.h file just contains:
#ifdef __CINT__
#pragma link C++ class TMyRootEvent ;
#endif
The root file & the tree are created in RunAction:
RunAction()
fTMyRootEvent = new TMyRootEvent () ;
fTMyRootEvent->InitEvent(fNCells) ;
BeginOfRunAction()
fRootFout = new TFile ("test.root","RECREATE","My GEANT4 simulation") ;
fRootTree = new TTree ("tree","My GEANT4 simulation") ;
fRootTree->Branch("evt","TMyRootEvent",&fTMyRootEvent) ;
EndOfRunAction()
fRootTree->Write() ;
fRootFout->Close() ;
The tree can be filled (fRootTree->Fill()) wherever (EndOfEventAction, SteppingAction, …).
To read the root file generated, one can use:
{
gROOT->ProcessLine(".L TMyRootEvent.hpp+") ;
TFile *fin = new TFile ("test.root") ;
TTree *tree = (TTree*)fin->Get("tree") ;
TMyRootEvent* evt = new TMyRootEvent() ;
tree->SetBranchAddress("evt",&evt) ;
tree->Show(0) ;
tree->Draw("evt->GetEkin_0()") ;
tree->Draw("evt->GetEkin(1)","evt->GetParticleName()==\"alpha\"") ;
}
Thanks to Philippe Canal for his precious help.
Z