From 8c97a04e2484b16798b5158b87fa5e9e480e34f8 Mon Sep 17 00:00:00 2001
From: Jochem Snuverink <jochem.snuverink@psi.ch>
Date: Wed, 11 Jul 2018 16:54:49 +0200
Subject: [PATCH] update reference particle location when trackOrbit file
 saved, fix header, for #231

---
 src/Algorithms/ParallelCyclotronTracker.cpp | 39 +++++++++++----------
 src/Structure/DataSink.cpp                  |  4 +--
 2 files changed, 23 insertions(+), 20 deletions(-)

diff --git a/src/Algorithms/ParallelCyclotronTracker.cpp b/src/Algorithms/ParallelCyclotronTracker.cpp
index c1037cf90..fdcff90b1 100644
--- a/src/Algorithms/ParallelCyclotronTracker.cpp
+++ b/src/Algorithms/ParallelCyclotronTracker.cpp
@@ -2477,13 +2477,13 @@ void ParallelCyclotronTracker::singleParticleDump() {
         if(myNode_m == 0) {
             int notReceived = Ippl::getNodes() - 1;
             int numberOfPart = 0;
-
+            // receive other nodes
             while(notReceived > 0) {
 
                 int node = COMM_ANY_NODE;
                 Message *rmsg =  Ippl::Comm->receive_block(node, tag);
 
-                if(rmsg == 0)
+                if(rmsg == nullptr)
                     ERRORMSG("Could not receive from client nodes in main." << endl);
 
                 --notReceived;
@@ -2491,25 +2491,16 @@ void ParallelCyclotronTracker::singleParticleDump() {
                 rmsg->get(&numberOfPart);
 
                 for(int i = 0; i < numberOfPart; ++i) {
-
                     rmsg->get(&id);
                     tmpi.push_back(id);
-                    rmsg->get(&x);
-                    tmpr.push_back(x);
-                    rmsg->get(&x);
-                    tmpr.push_back(x);
-                    rmsg->get(&x);
-                    tmpr.push_back(x);
-                    rmsg->get(&x);
-                    tmpr.push_back(x);
-                    rmsg->get(&x);
-                    tmpr.push_back(x);
-                    rmsg->get(&x);
-                    tmpr.push_back(x);
+                    for(int ii = 0; ii < 6; ii++) {
+                        rmsg->get(&x);
+                        tmpr.push_back(x);
+                    }
                 }
                 delete rmsg;
             }
-
+            // own node
             for(int i = 0; i < counter; ++i) {
 
                 tmpi.push_back(itsBunch_m->ID[found[i]]);
@@ -2520,13 +2511,21 @@ void ParallelCyclotronTracker::singleParticleDump() {
                     tmpr.push_back(itsBunch_m->P[found[i]](j));
                 }
             }
-
+            // store
             dvector_t::iterator itParameter = tmpr.begin();
 
             for(auto id : tmpi) {
 
                 outfTrackOrbit_m << "ID" << id;
 
+                if (id == 0) { // for stat file
+                    itsBunch_m->RefPartR_m[0] = *itParameter;
+                    itsBunch_m->RefPartR_m[1] = *(itParameter + 2);
+                    itsBunch_m->RefPartR_m[2] = *(itParameter + 4);
+                    itsBunch_m->RefPartP_m[0] = *(itParameter + 1);
+                    itsBunch_m->RefPartP_m[1] = *(itParameter + 3);
+                    itsBunch_m->RefPartP_m[2] = *(itParameter + 5);
+                }
                 for(int ii = 0; ii < 6; ii++) {
 
                     outfTrackOrbit_m << " " << *itParameter;
@@ -2536,7 +2535,6 @@ void ParallelCyclotronTracker::singleParticleDump() {
                 outfTrackOrbit_m << std::endl;
             }
         } else {
-
             // for other nodes
             Message *smsg = new Message();
             smsg->put(counter);
@@ -2568,6 +2566,11 @@ void ParallelCyclotronTracker::singleParticleDump() {
                 outfTrackOrbit_m << itsBunch_m->R[i](0) << " " << itsBunch_m->P[i](0) << " ";
                 outfTrackOrbit_m << itsBunch_m->R[i](1) << " " << itsBunch_m->P[i](1) << " ";
                 outfTrackOrbit_m << itsBunch_m->R[i](2) << " " << itsBunch_m->P[i](2) << std::endl;
+
+                if (itsBunch_m->ID[i] == 0) { // for stat file
+                    itsBunch_m->RefPartR_m = itsBunch_m->R[i];
+                    itsBunch_m->RefPartP_m = itsBunch_m->P[i];
+                }
             }
         }
     }
diff --git a/src/Structure/DataSink.cpp b/src/Structure/DataSink.cpp
index 16e75fcfc..e61161409 100644
--- a/src/Structure/DataSink.cpp
+++ b/src/Structure/DataSink.cpp
@@ -882,7 +882,7 @@ void DataSink::writeSDDSHeader(std::ofstream &outputFile,
                    << indent << "name=R0_s,\n"
                    << indent << "type=double,\n"
                    << indent << "units=m,\n"
-                   << indent << "description=\"45 R0 Particle position in s\"\n"
+                   << indent << "description=\"45 R0 Particle position in z\"\n"
                    << "&end\n";
         outputFile << "&column\n"
                    << indent << "name=P0_x,\n"
@@ -900,7 +900,7 @@ void DataSink::writeSDDSHeader(std::ofstream &outputFile,
                    << indent << "name=P0_s,\n"
                    << indent << "type=double,\n"
                    << indent << "units=1,\n"
-                   << indent << "description=\"48 R0 Particle momentum in s\"\n"
+                   << indent << "description=\"48 R0 Particle momentum in z\"\n"
                    << "&end\n";
         columnStart = 49;
     }
-- 
GitLab