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

I wish to set up a Geant4 simulation that does the following. I want to have gamma rays, the number and energy of which I can specify and vary, strike a very thin layer of a high-Z material, such as a lead or a gold foil. For my output, I am interested in the following. First, I would like to have a visual display of the events. I want this visual display to feature a tracker-like detector that shows the tracks of the secondary particles produced by the gamma ray interactions with the thin layer of high-Z material. Second, I would like to have the energy and momentum (magnitude and azimuthal direction) of the secondary particles outputted into histograms that I can see in a .root file.

I first posted a question about this under the title Adding Secondary Particle Data Output and Root Histograms to Basic Example B2a, but have not received an answer yet. Example B2a has the setup I need, but has no data output for secondary particles produced in the simulation in the form of Root histograms. Extended Example->Electromagentic->TestEm1 has this type of output, but not setup I need.

Since then, I have tried the following. I copied the files HistoManager.hh and StackingAction.hh from /electromagnetic/TestEm1/include to B2/B2a/include. I then copied the files HistoManager.cc and StackingAction.cc from /electromagnetic/TestEm1/src to B2/B2a/src. I also copied the file plotHisto.C from electromagnetic/TestEm1 to B2/B2a. I then switched to my B2a-build directory, and executed the following commands that are needed after modifying the GEANT4 simulations:

‘’’
cmake -DCMAKE_PREFIX_PATH=/Users/jamesbowen/geant4-v11.0.0-install /Users/jamesbowen/B2/B2a
‘’’
‘’’
make-j4
‘’’
After this, I ran the simulation, and it worked. However, when I tried to create the histograms, this is what happened:

‘’’
Jamess-MBP:B2a jamesbowen$ root -l plotHisto.C
error: invalid version number in ‘-mmacosx-version-min=11.1’
root [0]
Processing plotHisto.C…
Error in TFile::TFile: file run1.root does not exist
[/usr/local/Cellar/root/6.10.08/lib/root/libCling.so] cling_runtime_internal_throwIfInvalidPointer (no debug info)
[] (no debug info)
[] (no debug info)
[] (no debug info)
[/usr/local/Cellar/root/6.10.08/lib/root/libCling.so] cling::IncrementalExecutor::runStaticInitializersOnce(cling::Transaction const&) (no debug info)
[/usr/local/Cellar/root/6.10.08/lib/root/libCling.so] cling::Interpreter::executeTransaction(cling::Transaction&) (no debug info)
[/usr/local/Cellar/root/6.10.08/lib/root/libCling.so] cling::IncrementalParser::commitTransaction(llvm::PointerIntPair<cling::Transaction*, 2u, cling::IncrementalParser::EParseResult, llvm::PointerLikeTypeTraitscling::Transaction*, llvm::PointerIntPairInfo<cling::Transaction*, 2u, llvm::PointerLikeTypeTraitscling::Transaction* > >&, bool) (no debug info)
[/usr/local/Cellar/root/6.10.08/lib/root/libCling.so] cling::IncrementalParser::Compile(llvm::StringRef, cling::CompilationOptions const&) (no debug info)
[/usr/local/Cellar/root/6.10.08/lib/root/libCling.so] cling::Interpreter::EvaluateInternal(std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator > const&, cling::CompilationOptions, cling::Value*, cling::Transaction**, unsigned long) (no debug info)
[/usr/local/Cellar/root/6.10.08/lib/root/libCling.so] cling::Interpreter::process(std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator > const&, cling::Value*, cling::Transaction**, bool) (no debug info)
[/usr/local/Cellar/root/6.10.08/lib/root/libCling.so] cling::MetaProcessor::readInputFromFile(llvm::StringRef, cling::Value*, unsigned long, bool) (no debug info)
[/usr/local/Cellar/root/6.10.08/lib/root/libCling.so] TCling::ProcessLine(char const*, TInterpreter::EErrorCode*) (no debug info)
[/usr/local/Cellar/root/6.10.08/lib/root/libCling.so] TCling::ProcessLineSynch(char const*, TInterpreter::EErrorCode*) (no debug info)
[/usr/local/Cellar/root/6.10.08/lib/root/libCore.so] TApplication::ExecuteFile(char const*, int*, bool) (no debug info)
[/usr/local/Cellar/root/6.10.08/lib/root/libRint.so] TRint::ProcessLineNr(char const*, char const*, int*) (no debug info)
[/usr/local/Cellar/root/6.10.08/lib/root/libRint.so] TRint::Run(bool) (no debug info)
[/usr/local/Cellar/root/6.10.08/bin/root.exe] main (no debug info)
[/usr/lib/system/libdyld.dylib] start (no debug info)
[] (no debug info)
libc++abi.dylib: terminating with uncaught exception of type cling::InvalidDerefException: Trying to dereference null pointer or trying to call routine taking non-null arguments
Jamess-MBP:B2a jamesbowen$
‘’’

It looks like no run1.root file was produced when I ran the simulation. I used the following command to run the simulation:

‘’’
Jamess-MBP:B2a jamesbowen$ ./exampleB2a run2.mac
‘’’

What have I missed? What else do I need to do to run produce the run1.root file so I can create the histograms?

Hello,

really a lot of questions here… from what I could get it seems that the most urgent thing is to be able to create a ROOT output file. Here I will show you how to do it for exampleB2a (using Geant4.10.7.p03). Please note that this is a quick solution that I put together on a Sunday evening. There are many ways to use ROOT in a Geant4 application, but the list of examples reduces to basically two strategies.
1- Sourcing ROOT in your cmake project and calling ROOT in ad-hoc classes of your project.
2- Creating a ROOT output file using G4AnalysisManager (no other env apart from the Geant4 one are needed). I will show you this solution.

Modify src/B2RunAction as follows:

#include "B2RunAction.hh"
#include "G4root.hh"
#include "G4Run.hh"
#include "G4RunManager.hh"

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

B2RunAction::B2RunAction()
 : G4UserRunAction()
{ 
  // 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......

B2RunAction::~B2RunAction()
{
  delete G4AnalysisManager::Instance();
}

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

void B2RunAction::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" );
}

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

void B2RunAction::EndOfRunAction(const G4Run* )
{
  //Inform G4AnalysisManager its time to end
  //
  auto analysisManager = G4AnalysisManager::Instance();
  analysisManager->Write();
  analysisManager->CloseFile();
}

When we create the B2RunAction object we inform the AnalysisManager to create a ROOT tree with one column to save the Edep value of the first hit. When the B2RunAction object is destroyed we delete as well the analysismanager. At the beginning of each Run we open a (ROOT) file B2out.root, note that the ROOT extension is specified by #include "g4root.hh". At the end of each run we write and close the ROOT file. If you want to have multiple runs I suggest specifying the name of each root file as B2out_runNo so you have a different ROOT file created per each run.

Now, how to retrieve the information you want to be saved in your ROOT file? Modify the src/B2EventAction.cc file as follows:

#include "B2EventAction.hh"
#include "B2TrackerHit.hh"
#include "G4Event.hh"
#include "G4EventManager.hh"
#include "G4TrajectoryContainer.hh"
#include "G4Trajectory.hh"
#include "G4ios.hh"
#include "g4root.hh"

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

B2EventAction::B2EventAction()
: G4UserEventAction()
{}

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

B2EventAction::~B2EventAction()
{}

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

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

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

void B2EventAction::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<B2TrackerHitsCollection*>(event->GetHCofThisEvent()->GetHC(0));
 G4double EdepHit0 = (*evthc)[0]->GetEdep();
 G4cout<< EdepHit0 <<G4endl;
 auto analysisManager = G4AnalysisManager::Instance();
 analysisManager->FillNtupleDColumn(0, EdepHit0); 
 analysisManager->AddNtupleRow();
}  

At the end of each event, we get the event hit collection and the energy deposited of the first hit, then we fill column 0 of our ROOT file with its value and add a row to the ROOT file.
Example of modified B2a (again with Geant4.10.7.p03) using this test.mac macro:

/run/initialize
/tracking/verbose 1
/run/beamOn 5
./exampleB4a test.mac
root -l B2out.root 
Attaching file B2out.root as _file0...
(TFile *) 0x15667bd60
root [1] tree->Scan()
************************
*    Row   * EdepHit0. *
************************
*        0 * 0.0061069 *
*        1 * 0.0073405 *
*        2 * 0.0043915 *
*        3 * 0.0203340 *
*        4 * 0.0072836 *
************************
(long long) 5

Note: from Geant4.11.0.0 on, the analysis manager changed (Geant4 11.0 Release Notes). If you want to adapt the code to Geant4.11.0.0 or higher, you have to remove the #include "g4root.hh" and specify the ROOT extension .root in the output file name. Also, you have to remove delete G4AnalysisManager::Instance(); and add #include "G4AnalysisManager.hh" both in EventAction.cc and RunAction.cc.

Hope you can start from this and modify it according to your needs.
Best regards, Lorenzo

1 Like

lopezzot,

This looks excellent, thank you. I will take a close look at it, and let you know if there are any other issues.

I am sorry about the lengthy question. This was the most complex task I have yet asked about on this forum, and I thought there was a lot of ground to cover. I will try to be more concise next time.

Again, thanks.

Lorenzo,

I inserted the edits you recommended. When I went to process the edits, I got the following errors:

‘’’
Jamess-MacBook-Pro:B2-build jamesbowen$ cmake -DCMAKE_PREFIX_PATH=/Users/jamesbowen/geant4-v11.0.0-install /Users/jamesbowen/B2/B2a
– Configuring done
– Generating done
– Build files have been written to: /Users/jamesbowen/B2-build
Jamess-MacBook-Pro:B2-build jamesbowen$ make -j4
Consolidate compiler generated dependencies of target exampleB2a
[ 8%] Building CXX object CMakeFiles/exampleB2a.dir/src/EventAction.cc.o
[ 16%] Building CXX object CMakeFiles/exampleB2a.dir/src/RunAction.cc.o
/Users/jamesbowen/B2/B2a/src/EventAction.cc:88:3: error: use of undeclared
identifier ‘analysisManager’
analysisManager->FillNtupleDColumn(0, EdepHit0);
^
/Users/jamesbowen/B2/B2a/src/EventAction.cc:89:3: error: use of undeclared
identifier ‘analysisManager’
analysisManager->AddNtupleRow();
^
/Users/jamesbowen/B2/B2a/src/RunAction.cc:47:26: error: use of undeclared
identifier ‘G4AnalysisManager’
auto analysisManager = G4AnalysisManager::Instance(); //using ROOT
^
/Users/jamesbowen/B2/B2a/src/RunAction.cc:69:26: error: use of undeclared
identifier ‘G4AnalysisManager’
auto analysisManager = G4AnalysisManager::Instance();
^
/Users/jamesbowen/B2/B2a/src/RunAction.cc:79:26: error: use of undeclared
identifier ‘G4AnalysisManager’
auto analysisManager = G4AnalysisManager::Instance();
^
2 errors generated.
make[2]: *** [CMakeFiles/exampleB2a.dir/src/EventAction.cc.o] Error 1
make[2]: *** Waiting for unfinished jobs…
3 errors generated.
make[2]: *** [CMakeFiles/exampleB2a.dir/src/RunAction.cc.o] Error 1
make[1]: *** [CMakeFiles/exampleB2a.dir/all] Error 2
make: *** [all] Error 2
Jamess-MacBook-Pro:B2-build jamesbowen$
‘’’
Judging from what this says, it looks like I might need to add something about “AnalysisManager” or “G4AnalysisManager” to the #include list in B2EventAction.cc. Is that correct?

Also, FYI, I do have GEANT4 v11.0, so I had to follow the note you had at the bottom.

James

Hi,
yes please have a look at the bottom of my previous reply:

have you added it?
Also make sure in EventAction.cc you have

Cheers, Lorenzo

1 Like

Thanks Lorenzo. I was earlier looking at the e-mail notification I got, which cut off the last few sentences of your first reply. Therefore, I did not see it. I made those changes, reprocessed it, and it looks like it is error-free now.

I do have another question. Your first reply told me to make changes to a macro called test.mac. I do not see any macro by that name in B2a or in electromagnetic->TestEm1. Do I need to create that macro from scratch?

James

Hi,

that macro card was just a super simple one I created to see if the ROOT file is correctly created. It has nothing to do with the code I suggested. Yes, you can create it from scratch, but do note that the code will work with any macro, so I suggest using the macro card that produces the actual events you need to simulate.

Cheers, Lorenzo

1 Like

Lorenzo,

There is a macro file provided in example B2a called run2.mac. It contains all of the basic commands similar to the ones in your macro file above. I tried running it as follows:

‘’’
Jamess-MacBook-Pro:B2-build jamesbowen$ ./exampleB2a run2.mac
‘’’
It looks like there was a problem though. Here is what happened. I have omitted the extensive text that preceded the lines that signaled the beginning of the run, for brevity’s sake.

‘’’
G4VisManager: Using G4TrajectoryDrawByCharge as fallback trajectory model.
See commands in /vis/modeling/trajectories/ for other options.

Run 0 starts.

-------- EEEE ------- G4Exception-START -------- EEEE -------
*** G4Exception : Analysis_F001
issued by : G4GenericAnalysisManager::OpenFileImpl
Cannot open file “B2out”.
Please, use a file name with an extension or define the default file type
via G4AnalysisManager::SetDefaultFileType()
*** Fatal Exception *** core dump ***
**** Track information is not available at this moment
**** Step information is not available at this moment

-------- EEEE -------- G4Exception-END --------- EEEE -------

*** G4Exception: Aborting execution ***

CAUGHT SIGNAL: 6 ### address: 0x7fff6dc8332a, signal = SIGABRT, value = 6, description = abort program (formerly SIGIOT).

Backtrace:
[PID=19375, TID=-1][ 0/19]> 0 ??? 0x0000000000000000 0x0 + 0
[PID=19375, TID=-1][ 1/19]> 1 libsystem_c.dylib 0x00007fff6dc0a808 abort + 120
[PID=19375, TID=-1][ 2/19]> 2 libG4global.dylib 0x00000001065be50d Z11G4ExceptionPKcS0_19G4ExceptionSeverityS0.cold.1 + 29
[PID=19375, TID=-1][ 3/19]> 3 libG4global.dylib 0x0000000106591724 Z11G4ExceptionPKcS0_19G4ExceptionSeverityS0 + 3444
[PID=19375, TID=-1][ 4/19]> 4 libG4analysis.dylib 0x0000000105afa598 _ZN24G4GenericAnalysisManager12OpenFileImplERK8G4String + 472
[PID=19375, TID=-1][ 5/19]> 5 exampleB2a 0x00000001035b6fc7 _ZN2B29RunAction16BeginOfRunActionEPK5G4Run + 55
[PID=19375, TID=-1][ 6/19]> 6 libG4run.dylib 0x0000000103faeee4 _ZN12G4RunManager17RunInitializationEv + 1044
[PID=19375, TID=-1][ 7/19]> 7 libG4run.dylib 0x0000000103fae720 _ZN12G4RunManager6BeamOnEiPKci + 80
[PID=19375, TID=-1][ 8/19]> 8 libG4run.dylib 0x0000000103fd7c2d _ZN14G4RunMessenger11SetNewValueEP11G4UIcommand8G4String + 1133
[PID=19375, TID=-1][ 9/19]> 9 libG4intercoms.dylib 0x000000010651d04d _ZN11G4UIcommand4DoItE8G4String + 2669
[PID=19375, TID=-1][10/19]> 10 libG4intercoms.dylib 0x0000000106537d89 _ZN11G4UImanager12ApplyCommandEPKc + 2345
[PID=19375, TID=-1][11/19]> 11 libG4intercoms.dylib 0x00000001065151ed _ZN9G4UIbatch11ExecCommandERK8G4String + 29
[PID=19375, TID=-1][12/19]> 12 libG4intercoms.dylib 0x0000000106515548 _ZN9G4UIbatch12SessionStartEv + 264
[PID=19375, TID=-1][13/19]> 13 libG4intercoms.dylib 0x0000000106534a34 _ZN11G4UImanager16ExecuteMacroFileEPKc + 68
[PID=19375, TID=-1][14/19]> 14 libG4intercoms.dylib 0x000000010652f5a3 _ZN20G4UIcontrolMessenger11SetNewValueEP11G4UIcommand8G4String + 195
[PID=19375, TID=-1][15/19]> 15 libG4intercoms.dylib 0x000000010651d04d _ZN11G4UIcommand4DoItE8G4String + 2669
[PID=19375, TID=-1][16/19]> 16 libG4intercoms.dylib 0x0000000106537d89 _ZN11G4UImanager12ApplyCommandEPKc + 2345
[PID=19375, TID=-1][17/19]> 17 exampleB2a 0x00000001035b11d5 main + 741
[PID=19375, TID=-1][18/19]> 18 libdyld.dylib 0x00007fff6db3bcc9 start + 1

Abort trap: 6
Jamess-MacBook-Pro:B2-build jamesbowen$
‘’’

Do you have any idea what might be happening here? It looks like it might have something to do with the macro B2out, but I am not sure.

Hi,

have you added the root extension in the output file name?
As I was suggesting here:

Cheers, Lorenzo

1 Like

Hi Lorenzo,

Yes, you did specify that. Sorry I overlooked it.

I have files names “exampleB2.out” and “exampleB2a.out” in my directory B2a-build (where I run the example from), and “exampleB2a.out” in directory B2/B2a (my source from which I extracted the original contents of B2a-build). Do I need to edit one of these, or all of them? There is also a lot contained in each these macros. Where exactly do my make this change, and what exactly do I put in there?

Dear I see your comment very thoughtful and I get many important idea and at the same time I have a problem of generating the ROOT output files. I am using Geant4 11.0. 0, and as shown in the snapshot below I have putted the “output.root” under the analysis manager open file I couldn’t get the root out put data and again I tried by putting on the file name of the histo Manager as indicated by red mark but yet no root output data. May be to see my problems I have attached My CmakeLists sripts also. Would please give me a hint how I can solve the problem?

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

Lorenzo,

It worked, thanks!

How can I create histograms that show the energy and momentum of the secondary particles created when particles from particle gun strike the target? I tried to do this by bringing the files HistoManager.cc and StackingAction.cc over from extended examples->electromagnetic->TestEm1. I was able to compile the new files in B1a without any problems, but these histograms were not created when I ran the program.

Hi,
I’m glad it worked.
Usually dragging files from one example to another is not enough to get things done properly. Primaries, secondaries, hits, …, these things are explained in every Geant4 introductory course and clearly are well beyond a single forum topic. Plus the title of your topic is “how to add root output to exampleB2a”, now we are going a bit off-topic.
I do suggest taking such a course and then coming back to the forum for more specific questions (if any).

My two cents. Lorenzo

Hi Lorenzo,

My original question above was specifically about how to create these specific histrograms of the secondary particles.

Also, speaking of introductory courses, when and where are these offered? Can they be accessed remotely, or better yet, taken online right now? I have been trying to learn GEANT4 by myself with the help of the Getting Started page, Guide for Application Developers page, and this forum. An introductory course, even if self-paced, would definitely help.

Hi,

have a look at this webpage.

Cheers, Lorenzo

Lorenzo,

Thanks for this web page. However, it looks like the upcoming online courses are already full. Are there any notes from previous GEANT4 introductory courses online?

@lopezzot, how can i access the web page you mention above. it is given me errors when i click.

Hi,

recently Geant4 switched to a new website.
You can now find links to future and past courses here.

@lopezzot thank you so much