How Do I Add Data Output and Root Histograms to Basic Example B2a

Hi everyone,

it seems that the problem (at least the original one) is to adapt my suggestion to Geant4.11.0 or Geant4.11.0.p01. Here is how to generate a root file and save the energy stored in the first hit (or 0 if no hits are there).

Modify the RunAction.cc file as follows:

#include "RunAction.hh"

#include "G4Run.hh"
#include "G4RunManager.hh"
#include "G4AnalysisManager.hh"
namespace B2
{

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

RunAction::RunAction()
{
  // set printing event number per each 100 events
  G4RunManager::GetRunManager()->SetPrintProgress(1000);
  //Instantiate analysis manager
  //
  auto analysisManager = G4AnalysisManager::Instance(); //using ROOT
  analysisManager->SetVerboseLevel( 1 );
  analysisManager->SetNtupleMerging( 1 );    
  analysisManager->CreateNtuple("tree", "tree");
  analysisManager->CreateNtupleDColumn("EdepHit0");
  analysisManager->FinishNtuple();  
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

RunAction::~RunAction(){
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

void RunAction::BeginOfRunAction(const G4Run*)
{
  //inform the runManager to save random number seed
  G4RunManager::GetRunManager()->SetRandomNumberStore(false);
  //Inform G4AnalysisManager of output file
  //
  auto analysisManager = G4AnalysisManager::Instance();
  analysisManager->OpenFile( "B2out.root" );
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

void RunAction::EndOfRunAction(const G4Run* )
{
  auto analysisManager = G4AnalysisManager::Instance();
  analysisManager->Write();
  analysisManager->CloseFile();
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

}

and the EventAction.cc as

#include "EventAction.hh"
#include "TrackerHit.hh"
#include "G4Event.hh"
#include "G4EventManager.hh"
#include "G4TrajectoryContainer.hh"
#include "G4Trajectory.hh"
#include "G4ios.hh"
#include "G4AnalysisManager.hh"

namespace B2
{

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

EventAction::EventAction()
{}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

EventAction::~EventAction()
{}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

void EventAction::BeginOfEventAction(const G4Event*)
{}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

void EventAction::EndOfEventAction(const G4Event* event)
{
  // get number of stored trajectories

  G4TrajectoryContainer* trajectoryContainer = event->GetTrajectoryContainer();
  G4int n_trajectories = 0;
  if (trajectoryContainer) n_trajectories = trajectoryContainer->entries();

  // periodic printing

  G4int eventID = event->GetEventID();
  if ( eventID < 100 || eventID % 100 == 0) {
    G4cout << ">>> Event: " << eventID  << G4endl;
    if ( trajectoryContainer ) {
      G4cout << "    " << n_trajectories
             << " trajectories stored in this event." << G4endl;
    }
    G4VHitsCollection* hc = event->GetHCofThisEvent()->GetHC(0);
    G4cout << "    "
           << hc->GetSize() << " hits stored in this event" << G4endl;
  }
 auto evthc = static_cast<TrackerHitsCollection*>(event->GetHCofThisEvent()->GetHC(0));
 auto analysisManager = G4AnalysisManager::Instance();
 G4double EdepHit0 = 0.;
 if (evthc->GetSize() != 0){
    EdepHit0 = (*evthc)[0]->GetEdep();
    G4cout<< EdepHit0 <<G4endl;
 }
 analysisManager->FillNtupleDColumn(0, EdepHit0); 
 analysisManager->AddNtupleRow();
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

}

Example:

./exampleB2a run2.mac
root -l B2out.root
root [0] Attaching file B2out.root as _file0...
(TFile *) 0x12e314140
root [1] tree->GetEntries()
(long long) 500

Note that if the root file if empty it will be deleted automatically by Geant4 and you will see

... delete empty file : B2out.root - done

This might explain the question by @Halait-Ge.
Also, have a look at this answer and please consider taking a Geant4 introductory course.

Cheers, Lorenzo

1 Like