# Get the number of "/run/beamOn" in source code

**URL:** <https://geant4-forum.web.cern.ch/t/get-the-number-of-run-beamon-in-source-code/4613>\
**Category:** Getting Started\
**Created:** [March 3, 2021, 12:37pm UTC](https://geant4-forum.web.cern.ch/t/get-the-number-of-run-beamon-in-source-code/4613 "2021-03-03T12:37:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![tim](https://geant4-forum.web.cern.ch/letter_avatar_proxy/v4/letter/t/ce73a5/32.png) [@tim](https://geant4-forum.web.cern.ch/u/tim)\
**Post date:** [March 3, 2021, 12:37pm UTC](https://geant4-forum.web.cern.ch/t/get-the-number-of-run-beamon-in-source-code/4613/1 "2021-03-03T12:37:54Z")

</div>

We can set #of events we want to generate with /run/beamOn.  
Is it possible to get this number in source code of ActionInitialization, RunAction or EventAction etc…??

for instance  
int events = xxxx - \> GetNumberOfEventsToBeProcessed()  
or  
int events = xxx - \> GetNumberOfEventsToBeStored()  
something like above.

---

<div class="post-metadata">

**Author:** ![thehubbard](https://geant4-forum.web.cern.ch/user_avatar/geant4-forum.web.cern.ch/thehubbard/32/26_2.png) [@thehubbard](https://geant4-forum.web.cern.ch/u/thehubbard)\
**Post date:** [March 3, 2021, 2:34pm UTC](https://geant4-forum.web.cern.ch/t/get-the-number-of-run-beamon-in-source-code/4613/2 "2021-03-03T14:34:17Z")

</div>

Yes, you can call the following at the BeginOfRunAction to get the total number of events to be run.

```
void RunAction::BeginOfRunAction(const G4Run* aRun) { 
G4int eventsInRun = aRun->GetNumberOfEventToBeProcessed();
}

```

Hope that helps

---

<div class="post-metadata">

**Author:** ![tim](https://geant4-forum.web.cern.ch/letter_avatar_proxy/v4/letter/t/ce73a5/32.png) [@tim](https://geant4-forum.web.cern.ch/u/tim)\
**Post date:** [March 3, 2021, 5:40pm UTC](https://geant4-forum.web.cern.ch/t/get-the-number-of-run-beamon-in-source-code/4613/3 "2021-03-03T17:40:42Z")

</div>

I tested it, It works !  
Thanks !

---

<div class="post-metadata">

**Author:** ![tim](https://geant4-forum.web.cern.ch/letter_avatar_proxy/v4/letter/t/ce73a5/32.png) [@tim](https://geant4-forum.web.cern.ch/u/tim)\
**Post date:** [March 4, 2021, 3:55am UTC](https://geant4-forum.web.cern.ch/t/get-the-number-of-run-beamon-in-source-code/4613/4 "2021-03-04T03:55:09Z")

</div>

Is it possible to update this number in RunAction?

below does not work…

```
void RunAction::BeginOfRunAction(const G4Run* aRun) 
{ 
  // originally, it's 10, for instance.
  G4int eventsInRun = aRun->GetNumberOfEventToBeProcessed(); 
  // update it to 20
  G4RunManager::GetRunManager() ->SetNumberOfEventsToBeProcessed( 20 )
  // this can not update #of events to be processed. its still 10 in the follwowing.
  G4cout << "update #of Evts : " << aRun->GetNumberOfEventToBeProcessed() << G4endl; 
}

```

If I use G4RunManager::GetRunManager()-\> Initialize()  
it seems everything is initialized?

In fact, any place is ok. In other words, is it possible to give #of simulation events in source code without using /run/beamOn ?

---

<div class="post-metadata">

**Author:** ![tim](https://geant4-forum.web.cern.ch/letter_avatar_proxy/v4/letter/t/ce73a5/32.png) [@tim](https://geant4-forum.web.cern.ch/u/tim)\
**Post date:** [March 5, 2021, 8:07am UTC](https://geant4-forum.web.cern.ch/t/get-the-number-of-run-beamon-in-source-code/4613/5 "2021-03-05T08:07:12Z")

</div>

Always we have to decide #of events in .mac using “/run/beamOn” ??  
or, is it possible to decide it in “void main()” ??

---

<div class="post-metadata">

**Author:** ![thehubbard](https://geant4-forum.web.cern.ch/user_avatar/geant4-forum.web.cern.ch/thehubbard/32/26_2.png) [@thehubbard](https://geant4-forum.web.cern.ch/u/thehubbard)\
**Post date:** [March 5, 2021, 2:50pm UTC](https://geant4-forum.web.cern.ch/t/get-the-number-of-run-beamon-in-source-code/4613/6 "2021-03-05T14:50:59Z")

</div>

As per line 170 in [G4RunManager.hh](https://gitlab.cern.ch/geant4/geant4/-/blob/master/source/run/include/G4RunManager.hh) /run/beamOn is analysed before the event loop. The BeginOfRunAction method is executed at the start of the event loop I believe, therefore, this is the reason why you cannot change the value here.

Yes, you can set the beam on and the number of events in the main program. An easy way is to call the beamOn method from the run manager. This can be done using the following:

```auto
RunManager* runMan= new RunManager();
runMan->BeamOn(20);

```

Alternatively you can run UI commands in the main using:

```auto
G4UImanager *uiMan = G4UImanager::GetUIpointer();
uiMan ->ApplyCommand("/run/beamOn 20");

```

---

<div class="post-metadata">

**Author:** ![tim](https://geant4-forum.web.cern.ch/letter_avatar_proxy/v4/letter/t/ce73a5/32.png) [@tim](https://geant4-forum.web.cern.ch/u/tim)\
**Post date:** [March 15, 2021, 6:35pm UTC](https://geant4-forum.web.cern.ch/t/get-the-number-of-run-beamon-in-source-code/4613/7 "2021-03-15T18:35:07Z")

</div>

Yes, I put a few code in the upper main() to decide how many events to be generate.

Then, I’m using beamOn function to tell the number of times to be generated

Thanks !

---

<div class="post-metadata">

**Author:** ![bmorgan](https://geant4-forum.web.cern.ch/user_avatar/geant4-forum.web.cern.ch/bmorgan/32/7_2.png) [@bmorgan](https://geant4-forum.web.cern.ch/u/bmorgan)\
**Post date:** [July 2, 2022, 4:55pm UTC](https://geant4-forum.web.cern.ch/t/get-the-number-of-run-beamon-in-source-code/4613/8 "2022-07-02T16:55:16Z")

</div>


