From e255171bbefac26cfa2311011c050325522ff12c Mon Sep 17 00:00:00 2001
From: Andreas Adelmann <andreas,adelmann@psi.ch>
Date: Sat, 22 Apr 2017 11:34:00 +0200
Subject: [PATCH] closing #74

---
 src/Classic/AbsBeamline/Ring.cpp     |  5 --
 src/Classic/Algorithms/PartBunch.cpp | 68 ++++++++++++++--------------
 2 files changed, 34 insertions(+), 39 deletions(-)

diff --git a/src/Classic/AbsBeamline/Ring.cpp b/src/Classic/AbsBeamline/Ring.cpp
index 81f2eb1f4..228565eab 100644
--- a/src/Classic/AbsBeamline/Ring.cpp
+++ b/src/Classic/AbsBeamline/Ring.cpp
@@ -182,11 +182,6 @@ void Ring::setRefPartBunch(PartBunch* bunch) {
 
 std::vector<RingSection*> Ring::getSectionsAt(const Vector_t& r) {
     return section_list_m;
-    double phi = atan2(r(1), -r(0))+Physics::pi;
-    // std::cerr << "GetSectionsAt " << phi << " " << phiStep_m << " " << int((phi)/phiStep_m) << " " << ringSections_m.size() << std::endl;
-    if (phi >= 2.*Physics::pi)
-        phi -= 2.*Physics::pi;
-    return ringSections_m[(phi)/phiStep_m];
 }
 
 Rotation3D Ring::getRotationStartToEnd(Euclid3D delta) const {
diff --git a/src/Classic/Algorithms/PartBunch.cpp b/src/Classic/Algorithms/PartBunch.cpp
index afb607326..43a320820 100644
--- a/src/Classic/Algorithms/PartBunch.cpp
+++ b/src/Classic/Algorithms/PartBunch.cpp
@@ -1921,25 +1921,42 @@ void PartBunch::calcMoments() {
     }
 
     /*
-      find particle with ID==0
-      and save index in zID
-     */
+      In OPAL Cycl the particle with ID=0
+      is a special particle, a kind of design particle. 
+
+      Later on we will maintain a seperate structure like in OPAL-t
+      for now we will exclude the particle with ID==0.
+
+      - find particle with ID==0
 
-    unsigned long zID = 0;
-    bool found = false;
-    // TODO: this removes a particle from the data but the
-    //       number of particles remains the same. This will
-    //       inevitably lead to wrong results.
+      - substract the R and P of particle with ID==0 from the 
+        moment calculation. 
+     */
+    
     if (OpalData::getInstance()->isInOPALCyclMode()) {
-        for(unsigned long k = 0; k < this->getLocalNum(); ++k) {
-            if (this->ID[k] == 0) {
-                found  = true;
-                zID = k;
-                break;
-            }
-        }
+      for(unsigned long k = 0; k < this->getLocalNum(); ++k) {
+	if (this->ID[k] == 0) {
+	  part[1] = this->P[k](0);
+	  part[3] = this->P[k](1);
+	  part[5] = this->P[k](2);
+	  part[0] = this->R[k](0);
+	  part[2] = this->R[k](1);
+	  part[4] = this->R[k](2);	
+	  for(int i = 0; i < 2 * Dim; i++) {
+	    loc_centroid[i]   -= part[i];
+	    for(int j = 0; j <= i; j++) {
+	      loc_moment[i][j]   -= part[i] * part[j];
+	    }
+	  }
+	  break;
+	}
+      }
     }
-
+    
+    /*
+      This is now the regular loop
+    */
+    
     for(unsigned long k = 0; k < this->getLocalNum(); ++k) {
         part[1] = this->P[k](0);
         part[3] = this->P[k](1);
@@ -1956,23 +1973,6 @@ void PartBunch::calcMoments() {
         }
     }
 
-    if (found) {
-        part[1] = this->P[zID](0);
-        part[3] = this->P[zID](1);
-        part[5] = this->P[zID](2);
-        part[0] = this->R[zID](0);
-        part[2] = this->R[zID](1);
-        part[4] = this->R[zID](2);
-
-        for(int i = 0; i < 2 * Dim; i++) {
-            loc_centroid[i]   -= part[i];
-            for(int j = 0; j <= i; j++) {
-                loc_moment[i][j]   -= part[i] * part[j];
-            }
-        }
-    }
-
-
     for(int i = 0; i < 2 * Dim; i++) {
         for(int j = 0; j < i; j++) {
             loc_moment[j][i] = loc_moment[i][j];
@@ -2716,4 +2716,4 @@ bool PartBunch::WeHaveEnergyBins() {
 
 Vector_t PartBunch::get_pmean_Distribution() const {
     return dist_m->get_pmean();
-}
\ No newline at end of file
+}
-- 
GitLab