Where is the function 'B1ActionInitialization::Build()' called?

Could anyone please tell when & where the function ‘B1ActionInitialization::Build()’ is called?

The action classes (e.g., G4VUserPrimaryGeneratorAction) are instantiated within the function, but it doesn’t look to be called by any other codes in the example. It’s not a constructor, so I believe it’s not invoked by ‘runManager->SetUserInitialization(new B1ActionInitialization())’…

Maybe it’s called by some master process that is triggered by running ’ /run/beamOn’?

It depends if your Geant4 installation is built with support for multithreading or not.

If you run in a non-multithreaded (sequential) mode, it’s invoked exactly by G4RunManager::SetUserInitialization(...).

In the MT mode, it’s B1ActionInitialization::BuildForMaster() that will be called by G4MTRunManager::SetUserInitialization(...). And then, for each created thread B1ActionInitialization::Build() will be called from G4MTRunManagerKernel::StartThread(...).

Thanks anna for the explanation. Just to make sure… did you mean, in a non-multithreaded mode, B1ActionInitialization::Build() is invoked by ‘runManager->SetUserInitialization(new B1ActionInitialization())’ in exampleB1.cc? B1ActionInitialization::Build() is not a constructor, so I thought it should be called explicitly…

Examples are complete, so there is no need to modify them to make them work.

You create B1ActionInitialization (call a constructor) and pass it to runManager (via ::SetUserInitialization(...)) who does all the necessary steps, including calling ::Build(). You can check the source code for G4RunManager::SetUserInitialization(G4VUserActionInitialization*), or check it with gdb (the source code is in the few last lines):

(gdb) b B1ActionInitialization::Build() const 
Breakpoint 1 at 0xa9e7: file <G4path>/examples/basic/B1/src/B1ActionInitialization.cc, line 59.
(gdb) r
Starting program: <G4path>/examples/basic/B1/build/exampleB1 
[...]
Thread 1 "exampleB1" hit Breakpoint 1, B1ActionInitialization::Build (this=0x555555d7ced0) at <G4path>/examples/basic/B1/src/B1ActionInitialization.cc:59
59	  SetUserAction(new B1PrimaryGeneratorAction);
(gdb) bt 3
#0  B1ActionInitialization::Build() const (this=0x555555d7ced0) at <G4path>/examples/basic/B1/src/B1ActionInitialization.cc:59
#1  0x00007ffff6fc5dcf in G4RunManager::SetUserInitialization(G4VUserActionInitialization*) (this=0x55555577fe60, userInit=0x555555d7ced0)
    at <G4path>/source/run/src/G4RunManager.cc:911
#2  0x000055555555cb24 in main(int, char**) (argc=1, argv=0x7fffffffdc88) at <G4path>/examples/basic/B1/exampleB1.cc:77
(gdb) fr 1
#1  0x00007ffff6fc5dcf in G4RunManager::SetUserInitialization (this=0x55555577fe60, userInit=0x555555d7ced0) at <G4path>/source/run/src/G4RunManager.cc:911
(gdb) list 909,913
909	void G4RunManager::SetUserInitialization(G4VUserActionInitialization* userInit)
910	{
911	  userActionInitialization = userInit; 
912	  userActionInitialization->Build();
913	}

Thank you so much! It perfectly makes sense.