Define a flag to during the run

Hello, in my stepping action I save some information (kinetic energy, momentum etc) in a Root file

for example

    int pdg = step->GetTrack()->GetParticleDefinition()->GetPDGEncoding();
    			 analysisManager->FillNtupleDColumn(0,0, pdg);

etc.

Now I must add a flag to choose by the macro if saving or not the information in the Root file…i.e.
something like

if (fsavingFlagCmd) {
nt pdg = step->GetTrack()->GetParticleDefinition()->GetPDGEncoding();
        			 analysisManager->FillNtupleDColumn(0,0, pdg);
...

}

So I added

  1. In my PhysicsList messenger the lines

    savingFlagCmd(0)

    savingFlagCmd = new G4UIcmdWithABool("/random/setSavingFlag",this);
    savingFlagCmd->SetGuidance(“Saving Root Flag”);
    savingFlagCmd->SetParameterName(“flag”,true);
    savingFlagCmd->SetDefaultValue(true);

  2. in my PhysicsListMessenger.hh

G4UIcmdWithABool* savingFlagCmd;

  1. In my SteppingAction.cc
    #include "PhysicsListMessenger.hh"

    B1SteppingAction::B1SteppingAction(B1EventAction* eventAction, PhysicsListMessenger* savingFlagCmd)

fsavingFlagCmd(savingFlagCmd),

if(savingFlagCmd){

  1. In my Steppingaction.hh

G4bool fsavingFlagCmd;

  1. In my run1.mac
    /random/setSavingFlag false

but I get errors


here the files

PhysicsListMessenger.hh (2.9 KB) B1SteppingAction.hh (2.7 KB)
PhysicsListMessenger.cc (5.5 KB)
run1.txt (1.9 KB)
B1SteppingAction.hh (2.7 KB)

Thank you

Anyone to fix it please?

Why are you adding a command for your stepping action to the physics list messenger? Does the command in that messenger class actually modify or set a flag data member in your stepping action?

Hi @mkelsey in my program

  1. I save data from stepping action in a Root file
  2. I do the scoring mesh

Given that I simulate many events my supervisor asked to add the possibility to save or not data from the stepping action in the Root files (i.e. when I run to do the scoring mesh I don’t save data in the Root file).
Obviously i can just comment the linea in the stepping action, but my supervisor wants a “nice” program, so che asked me ti add a flag to enable or disabile the data from stepping action storming.
So che asked me to add a flag that I can command by the macro…so if the command in the macro is true I save data in Root file, otherwise if it is false I don’t store them.
To do it, my supervisor suggested me to define in the Physics list Messenger a boolean varieble then I tried to do it in the way that I showed in the first message, but I got errore during tue compiling…

Do you know how to fix it, or a simpler way to do it?

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