Is there a way to force an alias to an integer value?

[ I put this into “Getting Started” because there isn’t a category for UI commands. ]

I may be missing something not so obvious in the documentation. Is there a UI command (or mix of UI and shell, perhaps) that I can use to force a computed alias quantity into an integer?

I’m computing a cut-off parameter from a quartic “fit” as follows:

# Use quartic fit to max-steps to set upper limit (Horner's rule)
/control/alias C0 1000
/control/alias C1 6
/control/alias C2 0.025
/control/alias C3 -5e-5
/control/alias C4 4e-7
/control/multiply KSTEP {C4} {VOLT}     # V*c4
/control/add KSTEP {C3} {KSTEP}         # c3 + V*c4
/control/multiply KSTEP {KSTEP} {VOLT}  # V*(c3 + V*c4)
/control/add KSTEP {C2} {KSTEP}         # c2 + V*(c3 + V*c4)
/control/multiply KSTEP {KSTEP} {VOLT}  # V*(c2 + V*(c3 + V*c4))
/control/add KSTEP {C1} {KSTEP}         # c1 + V*(c2 + V*(c3 +V*c4))
/control/multiply KSTEP {KSTEP} {VOLT}  # V*(c1 + V*(c2 + V*(c3 +V*c4)))
/control/add KSTEP {C0} {KSTEP}         # c0 + V*(c1 + V*(c2 + V*(c3 +V*c4)))

where {VOLT} is a set argument from elsewhere. The resulting {KSTEP} is passed to a macro command which expects an integer value. This causes some of my jobs to fail with very sensible errors:

1062.45000: integer expected.
***** Illegal parameter (0) </g4cmp/maximumSteps {KSTEP}000> *****

***** Batch is interrupted!! *****

Multiplying by 1000 (instead of suffixing zeroes) won’t work in general, because for some other voltages, or different coefficients, I may end up not having just two or three decimal places.

So I’m looking for something along the lines of a /control/round or /control/integercommand that would do the job. Any suggestions?

I will mention that if I were doing a simple division, I could do this with a sufficiently baroque use of /control/remainder

/control/divide RATIO 7 3         # returns 2.33333
/control/remainer REM 7 3         # returns 1
/control/divide FRAC {REM} 3      # returns 0.33333
/control/subtract INTEGER {RATIO} {FRAC}   # returns 2

Unfortunately, this method can’t be adapted to my case: /control/remainder itself only takes integer values (I was hoping to play a game with divide by 1 to get the fractional part). So my original question stands.

Hi @mkelsey, unfortunately I don’t think there’s a way to do this. The code that defines the add and multiply commands:

uses floating point G4UIparameters, and the result of the addition is stored as floating point:

We could look at adding something like a round or cast command to address this use case.

Yep, that’s what I found, Ben. What I ended up doing was doing the arithmetic in my Slurm submission script, and passing the result in to be used directly. Bash does strictly integer math, but with unlimited precision, so I could compute component values way above 2^31.

I don’t think just adding something like round or cast is entirely worth it. If I were going to make a proposal, it would be to borrow CLHEP’s expression parser and add a full /control/expr type of command (I did something like this in our simulation framework to support arbitrary SI unit expressions).

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.