Define a flag to during the run

Write a messenger class for your stepping action. Make that class a data member of your stepping action, and pass a pointer to the stepping action object as an argument of the messenger class’s construction. Then in the messenger code where you trap the UI command, you can call a set function in your stepping action to set the flag.

The specific details of writing the code are left to you.

@mkelsey thank you.
Is there an example to understand how to do it?

In the Geant4 examples/ directory, I find (find examples -name '*Messenger.cc') 294 examples you could choose from, all in the various extended/ and advanced/ examples (the basic/ examples do not include custom UI commands).

I suspect that looking at one of the StackingAction or RunAction Messenger examples might be most appropriate for your purposes. Maybe TestEm2?

thank you @mkelsey

I think that I was able to define the class, but I do errors passing it to the stepping action.

I did in this way:

  1. In my physicslistmessenger I added
    image

    image



image

  1. In my PhysicsListMessenger.hh I added
    image

    image

  2. In my SteppingAction.cc I added
    image



    image

  3. In my B1SteppingAction.hh I added
    image

But compiling I get these errors

Do you know what I did wrong please?
Thank you

B1SteppingAction.cc (11.1 KB) PhysicsListMessenger.cc (5.5 KB)
B1SteppingAction.hh (2.7 KB) PhysicsListMessenger.hh (2.9 KB)

Why are you trying to use the PhysicsListMessenger? That class is for the physics list. You should be writing your own separate Messenger class to use with your SteppingAction. Make that separate Messenger class a data member of your stepping action. Instantiate it in the SteppingAction construction, and pass a pointer to the SteppingAction into the separate Messenger class constructor, and store it in the Messenger as an unowned pointer.

It is up to you to write your own code to do what you want. Are you familiar with C++ in terms of design and philosophy? Not just copying and trying to modify code you don’t understand.

Sorry @mkelsey I didnt’ understand that I had to write a SteppingActionMessenger.

I know just basic commands…I’m not an expert programmer…

I tried to do it…

  1. I wrote my SteppingActionMessenger.hh

  2. I wrote my SteppingActionMessenger.cc

  3. In my SteppingAction.hh I added the lines:
    image

    image

In In my SteppingAction.cc I added the lines:
image

image

When I compile I don’t get errors, but

it looks like that Geant save the steps when I set
savingFlagCmd->SetDefaultValue(false);
instead of when I set savingFlagCmd->SetDefaultValue(true);

  1. If I add in the run1.mac the line

/random/setRootFlag false
to set the flag value I get this error
image

THank you
B1SteppingAction.cc (11.0 KB)
SteppingActionMessenger.cc (466 Bytes)

SteppingActionMessenger.hh (437 Bytes) B1SteppingAction.hh (2.7 KB)

Why do you not instantiate the Messenger object in your SteppingAction constructor?

Sorry @mkelsey how could I do it?
Becausd I added the line

RootFlag(this),

here


but now I get the same Root file both if I set
savingFlagCmd->SetDefaultValue(true);
or
savingFlagCmd->SetDefaultValue(false);

in the SteppingActionMessenger and moreover I still get this error
image
if I add the line
/random/setRootFlag false
in the run1.mac

Thank you

You still haven’t implemented any action to take when the user enters the macro command. Where is SetNewValue() defined and implemented in your SettingActionMessenger class? Please read the documentation.

Sorry @mkelsey but it looks like hard for me to define the flag without a bit example code.

Here my SteppingActionMessenger.hh

Here my SteppingActionMessenger.cc

Here My SteppinAction.hh

Here part of My SteppinAction.cc

But I get this error

Your SetNewValue() function definition, in particular the if block, is wrong. Did you look at any of the examples? Try looking at TestEm1/src/StepMaxMessenger.cc. Think about what the code is doing, don’t just copy and change letters.

Sorry @mkelsey unfortunately I still get error

Read the code to yourself out loud, and describe what it says it’s doing. Not what you want it to do. Then go look again at the example I pointed you toward. Read that code to yourself out loud, and describe what it says it’s doing. You should discover that what you have written is not what you intended, and not what works.

If you don’t know what the pointer operator (->) does in C++, or if you don’t know how to call functions with arguments, you will need to study basic C++ programming on your own.

I modified in this way:

SteppingActionMessenger.hh
image

SteppingActionMessenger.cc

SteppingAction.hh

SteppingAction.cc (just some lines)

but I still get this error

@mkelsey please, can you write the right lines, so looking the right code I could understand my errors…
Thank you

When you call your SetRootFlag() function from the Messenger, you are passing in a string (look at the data type assigned to the newValues variable). When you wrote your SteppingAction class, you defined the SetRootFlag() function to take a bool as an argument. Obviously those are different.

The Messenger class provides functions which can automatically convert the input string to all the data types you might need. In this particular case, you’ll want to use StoB(newValues) (String to Bool).

Sorry @mkelsey I used the StoB(newValues)

but you see…I still get that error

Read the error message out loud to yourself. Which line number of the file is it on? What exactly is it telling you is wrong? Look specifically at the line number in the error message, then look at the same line number in your code.

If you want to be able to write your own software, and understand how it works, then you need to be able to do it yourself, identify problems and fix them yourself. The problem you are having has nothing to do with Geant4. It is basic C++ programming.

@mkelsey Yes I read the wrong line and what it is telling that it’s wrong…but I can’t understand the reason…
I mean
this is the wrong line theSteppingAction->SetRootFlag(StoB(newValues)); and it says that there is a not thefined type “SteppingAction”

but it is defined in my SteppingActionMessenger.hh

No, it is NOT “defined in my SteppingActionMessenger.hh”. It is declared in that .hh file (look up "forward declaration in C++). All you’ve done there is told the compiler that a class with that name exists, but haven’t told it how the class is structured.

In your SteppingActionMessenger.cc file, you appear to have

#include "B1SteppingAction.hh"

Is that the header file where you defined your SteppingAction class? If so, what is the name of the class you assigned in that file? You should be using the same class name for the SteppingAction in your Messenger.

Thank you @mkelsey. In these days I installed Qt as you can red here https://geant4-forum.web.cern.ch/t/use-qt-with-geant-in-windows/ so I didn’t have Geant4. Moreover, now I’ve to do something more important (the flag is just a fuction that my supervisor wants, but it isnt’ important for the analysis, because I can just comment the lines for the moment).
I will try again the flag next days and I will update this topic