Python in a high level (in terms of abstraction) versatile programming language well adapted for scientific use
Both Imperative and Oriented Object approach (at the same time)
Interpeted and dynamic typing (implicite type) and easy object manipulation and indexing
At the price of speed and memory managment (compared to C)
Various scientific libaries for data analysis and ploting (numpy, scipy, matplotlib)
Can read many file format like text and root (with dedicated library)
Free and open source
Written in C and able to read any C Class
3 Approaches :
The Root approach PyROOT: directly using Root in Python
The inbetween approach : rootpy : Redefine some ROOT classes in a more Pythonic way. Seems not be compatible with Python 3. Abbandoned ?
The Python approach root_numpy: Transfoming Root tree to numpy structured array
ROOT comes with its own Python implementation : PyROOT
Pro :
Cons :
import ROOT as rt
tfil = rt.TFile('Simu_TReCam_contact_puit1.root')
tree = tfil.Get('Singles')
h1 = rt.TH1D('energy', 'energy', 200, 0, 0.15)
for i in range(tree.GetEntries()):
tree.GetEntry(i)
h1.Fill(tree.energy)
c1 = rt.TCanvas( 'c1', '', 200, 10, 700, 500 )
h1.Draw()
c1.Draw()
tfil.Close()
%matplotlib inline
import numpy as np # Data manipulaion
import matplotlib.pyplot as plt # Graphic library
import ROOT as rt
tfil = rt.TFile('Simu_TReCam_contact_puit1.root')
tree = tfil.Get('Singles')
ene = np.zeros(tree.GetEntries()) # Initialize a 1 dim array with 0
for i in range(tree.GetEntries()):
tree.GetEntry(i)
ene[i] = tree.energy
plt.hist(ene, bins=200, range=(0, 0.15), histtype='step') # Both fill and plot the histogram
plt.show()
import numpy as np
import matplotlib.pyplot as plt
import root_numpy as rnp
singles = rnp.root2array('Simu_TReCam_contact_puit1.root', 'Singles')
# One function to load a whole ROOT Tree in an array
print(singles)
print(singles.dtype)
ene1 = singles['energy']
posx = singles['globalPosX']
print(ene)
print(posx)
plt.hist(ene1, bins=100, range=(0, 0.15), histtype='step')
plt.show()
noise = np.random.normal(0, 0.003, len(ene1))
ene2 = ene1 + noise # Add Gaussian random fluctuation
ene3 = ene2[(ene2>0.11) & (ene2<0.13)] # Energy Cut
plt.hist(ene2, bins=100, range=(0, 0.15), histtype='step')
plt.hist(ene3, bins=100, range=(0, 0.15), histtype='step')
plt.show()
Numpy also possess its own format to save arrays either in text or binary format
Adding direct save of Gate simulation in numpy format would solve the issue of root_numpy compatibility with ROOT and get completely rid of ROOT ;)
I'm going to have a look at it to add it to Gate and also add a couple of exemple for data anlaysis