Python for Geant4 Data Analysis
The Python package uproot can be used to read Geant4 output file saved in ROOT format. It can be easily installed using python -m pip install uproot.
If you are familiar with ROOT and would like to migrate to Python for analyzing Geant4 output in ROOT format (for example, an output file from GEARS), you can find here a brief list of Python equivalence of ROOT commands:
- Open file:
- Check file contents:
- List variables in TTree ntuples:
- Draw the distribution of a variable as a histogram:
- Draw the distribution of a selected subset of the variable as a histogram:
- ROOT:
root[] t->Draw("x", "vlm==1") // draw x coordinate of step points in volume 1 - Python:
>>> import awkward as ak >>> import matplotlib.pyplot as plot >>> x = t.arrays(['x'], "vlm==1") # get all x in vlm 1 >>> plot.hist(ak.flatten(x, axis=None)), bins=100) >>> plot.show()Ref. https://awkward-array.org/doc/main/user-guide/how-to-restructure-flatten.html
- ROOT:
- Draw a 2D histogram: