Analysis of Geant4 data by ROOT Tree
ROOTの機能を使うのでROOTのライブラリの場所のとかの指定をGNUmakefileに
書いておく。必ずbinmake.gmkをインクルードする前のところに書くこと
#-----------------------------------------------------------
# environment setting for ROOT output
#-----------------------------------------------------------
# enable ROOT output option
CPPFLAGS += -D__OUTPUT_ROOTFILE__
# add compile flags and extra libs for ROOT output
CPPFLAGS += $(shell $(ROOTSYS)/bin/root-config --cflags)
ROOTLIBS = $(shell $(ROOTSYS)/bin/root-config --glibs)
EXTRALIBS += $(ROOTLIBS)
#-----------------------------------------------------------
include $(G4INSTALL)/config/binmake.gmk
これができたらUserRunActionをいじる。
まずUserRunAction.hhにこんなことを書いておく。
private:
  TFile *fOutfile;  // output rootfile pointer
  TTree *t1;
  //variables for branch
  G4double tE1,tE2;
public:
  inline TFile* GetOutFilePtr() const {return fOutfile;}
  inline TTree* GetTTreePtr() const {return t1;}
  void SettE1(G4double *val){tE1 = *val;}
  void SettE2(G4double *val){tE2a = *val;}
 
TFile.hとTTree.hのincludeは忘れずに。
次にBeginOfRunActionでROOTのアウトプットファイルとかTreeとかを定義。
   fOutfile = new TFile("hoge.root","RECREATE"); // output file
   t1 = new TTree("t1","hoge analysis"); // define the Tree
   // define branches
   t1 -> Branch("E1",&tE1,"tE1/D");
   t1 -> Branch("E2",&tE2,"tE2/D");
こんなかんじ。さらにEndOfRunActionで確認のためのPrintと書き込みを行う。
  t1->Print();
  t1->Write();
さらにUserEventActionのEndOfEventActionでTreeへデータをFillしていく
  UserRunAction *runAction = (UserRunAction *) runManager -> GetUserRunAction()  
  TTree *t1 = runAction -> GetTTreePtr();
  if(evtn%2 == 0){
    G4double val;
    //G4double tEProton;
    val = E1;
    runAction -> SettE1(&val);
    val = E2;
    runAction -> SettE2(&val);
    t1 -> Fill();
  }
runActionはUserRunActionのポインタです。runManagerは自分で定義していなくても
G4UserRunAction持っている関数なので大丈夫です。
これでTreeに書き込めます。