KAHIBARO
Discord Login Register

Variables and Data Types

Numbers

In GATE simulations you use Python numbers to describe almost every physical quantity, such as energy, position coordinates, time, and activity. Python has two main numeric types that are important for GATE beginners: integers and floating-point numbers.

An integer is a whole number without a decimal point, for example 0, 10, or -3. You often use integers to count events, specify the number of threads, or index crystals and detector IDs.

A floating-point number, or float, is a number with a decimal point, for example 0.0, 3.14, or 1e6. You use floats for physical values that can vary continuously, such as distances in millimeters, energies in MeV, or activity in Bq. Scientific notation like 1e6 means $1 \times 10^6$.

In Python, basic arithmetic uses the usual symbols: + for addition, - for subtraction, * for multiplication, and / for division. The operator raises a number to a power, such as 23 for $2^3$. When you divide two integers with /, Python returns a float, which is convenient for physics calculations.

In GATE code you typically combine numbers with units, for example 10 * gate.g4_units.mm. The number itself is just a Python integer or float. The unit object converts that number into a quantity that Geant4 understands.

Always use floats for physical quantities in simulations. Use integers for counters, IDs, and indices.

Strings

Strings represent text. In GATE related Python code you use them for names, labels, file paths, and options. You create a string by putting characters between quotes, for example "world", 'detector1', or "output/sim_results.root".

Strings can be written with single quotes or double quotes. Python treats "world" and 'world' as the same. You can join, or concatenate, strings with the + operator, for example "detector_" + str(i) to create names like "detector_0" and "detector_1" in a loop. The function str(...) converts numbers and other objects to a string.

String content is case sensitive. "World" and "world" are different. This matters when you set configuration keys or attach actors to volumes by name, because GATE will not find a volume if the name does not match exactly.

You also often use strings as keys in dictionaries and to specify options, for example "gamma", "proton", or "SimulationStatsActor". Treat these option strings as exact codes that GATE expects.

Boolean values

Boolean values represent truth values, which control decisions in your script. Python has two Boolean constants: True and False. The capital letters are important; true and false do not exist in Python.

You usually obtain Boolean values from comparisons such as x > 0 or energy == 511. These expressions return True or False and are used in conditional statements that appear in another chapter.

Booleans are essential when you want to enable or disable features in a GATE simulation. For example, you may have a variable use_visualization = True that controls whether the geometry visualization is configured, or save_hits = False that disables some output to speed up early tests.

You combine Booleans with logical operators and, or, and not. For instance, you might check that both the energy is within a range and the particle is a gamma before processing it further. This type of logic is common in analysis code and filters that select particular events.

Boolean values in Python are exactly True and False. They are case sensitive and must not be written in lowercase.

Lists

Lists store ordered collections of items. In GATE related scripts you often use lists for coordinates, sets of materials, multiple sources, or repeated parameter values. You create a list with square brackets, for example [1, 2, 3] or ["water", "bone", "lung"].

Lists can contain different types of elements in the same list. For example, you can store a mixture of numbers and strings, although for clarity in simulation scripts it is usually better to keep each list homogeneous. You access elements by index, starting at 0. The first element is my_list[0], the second is my_list[1], and so on. Negative indices count from the end, with my_list[-1] as the last element.

In geometry definitions you will frequently use lists of three numbers to represent positions and sizes, such as [x, y, z]. You might also use lists to store multiple detector names, source IDs, or several energy values that you later loop over.

Lists are mutable, which means you can change them after creation. You can append elements with my_list.append(value) or modify an item with my_list[index] = new_value. This is useful when building a configuration step by step, for example when you dynamically add actors or volumes depending on user choices.

Indexing starts at 0. The last element is at index len(my_list) - 1, not at len(my_list).

Dictionaries

Dictionaries store key value pairs and are one of the most useful structures for organizing GATE configuration in Python. A dictionary associates each key with a value. You create a dictionary with curly braces, for example {"material": "Water", "size": [10, 10, 10]}.

Keys are usually strings, such as "name", "material", "energy", or "position", and the values can be any Python object, including numbers, strings, lists, or other dictionaries. You access a value by its key, for example volume["material"]. If you assign volume["material"] = "Lung", you update the stored value.

Dictionaries are ideal for grouping parameters that belong together, such as all the properties of a source or all the settings of an actor. They make the code clearer, because the meaning of each value is given explicitly by its key instead of just by its position in a list.

You can also use dictionaries to create simple configuration objects. For example, you might define a dictionary that holds all global simulation settings, or one dictionary per detector, then pass them to helper functions that build the geometry or sources.

Dictionaries map keys to values. Each key must be unique. Access values by key, not by position.

Views: 14

Comments

Please login to add a comment.

Don't have an account? Register now!