From 272b5f7fa593b5ee9dd83178a2e78f5377476efc Mon Sep 17 00:00:00 2001
From: Gamelin Alexis <alexis.gamelin@synchrotron-soleil.fr>
Date: Tue, 20 Jun 2023 16:03:14 +0200
Subject: [PATCH] [Fix] Bunch.emit

Calculation was wrong for off center beams.
---
 mbtrack2/tracking/particles.py | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/mbtrack2/tracking/particles.py b/mbtrack2/tracking/particles.py
index 9dfd691..1220a24 100644
--- a/mbtrack2/tracking/particles.py
+++ b/mbtrack2/tracking/particles.py
@@ -235,12 +235,13 @@ class Bunch:
         """
         Return the bunch emittance for each plane.
         """
-        emitX = (np.mean(self['x']**2)*np.mean(self['xp']**2) - 
-                 np.mean(self['x']*self['xp'])**2)**(0.5)
-        emitY = (np.mean(self['y']**2)*np.mean(self['yp']**2) - 
-                 np.mean(self['y']*self['yp'])**2)**(0.5)
-        emitS = (np.mean(self['tau']**2)*np.mean(self['delta']**2) - 
-                 np.mean(self['tau']*self['delta'])**2)**(0.5)
+        cor = np.squeeze([[self[name] - self[name].mean()] for name in self])
+        emitX = np.sqrt(np.mean(cor[0]**2)*np.mean(cor[1]**2) - 
+                        np.mean(cor[0]*cor[1])**2)
+        emitY = np.sqrt(np.mean(cor[2]**2)*np.mean(cor[3]**2) - 
+                        np.mean(cor[2]*cor[3])**2)
+        emitS = np.sqrt(np.mean(cor[4]**2)*np.mean(cor[5]**2) - 
+                        np.mean(cor[4]*cor[5])**2)
         return np.array([emitX, emitY, emitS])
     
     @property
-- 
GitLab