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

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

iam using ubuntu 20.04.
Geant4 version 11.0

You’re trying to build code from an older Geant4 version (10.7, I presume). See the release notes for G4 11 Geant4 11.0 Release Notes, and in particular,

  • Removed deprecated g4csv.hh, g4hdf5.hh, g4root.hh, g4xml.hh headers.
1 Like