From fbb5b348f89152237973551029f7576a20bb5e27 Mon Sep 17 00:00:00 2001
From: Vickie Lynch <lynchve@ornl.gov>
Date: Fri, 28 Aug 2015 08:44:48 -0400
Subject: [PATCH] Refs #13494 shift TOF from value in TOPAZ Param file

---
 .../EventWorkspaceCollection.h                |  1 +
 .../src/EventWorkspaceCollection.cpp          |  9 +++++++
 .../DataHandling/src/LoadEventNexus.cpp       | 25 ++++++++++++++++++-
 .../DataHandling/src/LoadIsawDetCal.cpp       | 19 ++++++++------
 4 files changed, 45 insertions(+), 9 deletions(-)

diff --git a/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/EventWorkspaceCollection.h b/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/EventWorkspaceCollection.h
index 6f5df735250..3bd43957f84 100644
--- a/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/EventWorkspaceCollection.h
+++ b/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/EventWorkspaceCollection.h
@@ -102,6 +102,7 @@ public:
   void populateInstrumentParameters();
   void setTitle(std::string title);
   void applyFilter(boost::function<void (API::MatrixWorkspace_sptr)> filter);
+  virtual bool threadSafe() const;
 };
 
 typedef boost::shared_ptr<EventWorkspaceCollection> EventWorkspaceCollection_sptr;
diff --git a/Code/Mantid/Framework/DataHandling/src/EventWorkspaceCollection.cpp b/Code/Mantid/Framework/DataHandling/src/EventWorkspaceCollection.cpp
index baa1655e5c5..4ea230c4f31 100644
--- a/Code/Mantid/Framework/DataHandling/src/EventWorkspaceCollection.cpp
+++ b/Code/Mantid/Framework/DataHandling/src/EventWorkspaceCollection.cpp
@@ -290,5 +290,14 @@ void EventWorkspaceCollection::applyFilter(boost::function<void (MatrixWorkspace
   }
 }
 
+//-----------------------------------------------------------------------------
+/** Returns true if the EventWorkspace is safe for multithreaded operations.
+ */
+bool EventWorkspaceCollection::threadSafe() const {
+  // Since there is a mutex lock around sorting, EventWorkspaces are always
+  // safe.
+  return true;
+}
+
 } // namespace DataHandling
 } // namespace Mantid
diff --git a/Code/Mantid/Framework/DataHandling/src/LoadEventNexus.cpp b/Code/Mantid/Framework/DataHandling/src/LoadEventNexus.cpp
index f9e7f4bef2a..1f136ae8a92 100644
--- a/Code/Mantid/Framework/DataHandling/src/LoadEventNexus.cpp
+++ b/Code/Mantid/Framework/DataHandling/src/LoadEventNexus.cpp
@@ -1800,7 +1800,8 @@ void LoadEventNexus::loadEvents(API::Progress *const prog,
   }
 
   prog->report("Initializing all pixels");
-  // Remove used banks if parameter is set
+
+  // Remove unused banks if parameter is set
   if (m_ws->getInstrument()->hasParameter("remove-unused-banks")) {
     std::vector<double> instrumentUnused =
         m_ws->getInstrument()->getNumberParameter("remove-unused-banks", true);
@@ -1941,6 +1942,28 @@ void LoadEventNexus::loadEvents(API::Progress *const prog,
                                                "may indicate errors in the raw "
                                                "TOF data." << std::endl;
 
+  // Use T0 offset from TOPAZ Parameter file if it exists
+  if (m_ws->getInstrument()->hasParameter("T0")) {
+    std::vector<double> instrumentT0 =
+        m_ws->getInstrument()->getNumberParameter("T0", true);
+    if (!instrumentT0.empty()) {
+      const double mT0 = instrumentT0.front();
+      if (mT0 != 0.0) {
+        int64_t numHistograms = static_cast<int64_t>(m_ws->getNumberHistograms());
+        PARALLEL_FOR1(m_ws)
+        for (int64_t i = 0; i < numHistograms; ++i) {
+          PARALLEL_START_INTERUPT_REGION
+          // Do the offsetting
+          m_ws->getEventList(i).addTof(mT0);
+          PARALLEL_END_INTERUPT_REGION
+        }
+        PARALLEL_CHECK_INTERUPT_REGION
+        // set T0 in the run parameters
+        API::Run &run = m_ws->mutableRun();
+        run.addProperty<double>("T0", mT0, true);
+      }
+    }
+  }
   // Now, create a default X-vector for histogramming, with just 2 bins.
   Kernel::cow_ptr<MantidVec> axis;
   MantidVec &xRef = axis.access();
diff --git a/Code/Mantid/Framework/DataHandling/src/LoadIsawDetCal.cpp b/Code/Mantid/Framework/DataHandling/src/LoadIsawDetCal.cpp
index e191cd6821e..181b639e924 100644
--- a/Code/Mantid/Framework/DataHandling/src/LoadIsawDetCal.cpp
+++ b/Code/Mantid/Framework/DataHandling/src/LoadIsawDetCal.cpp
@@ -180,15 +180,18 @@ void LoadIsawDetCal::exec() {
       if (instname .compare("WISH") == 0) center(0.0, 0.0, -0.01 * mL1, "undulator", inname);
       else center(0.0, 0.0, -0.01 * mL1, "moderator", inname);
       // mT0 and time of flight are both in microsec
-      IAlgorithm_sptr alg1 = createChildAlgorithm("ChangeBinOffset");
-      alg1->setProperty<MatrixWorkspace_sptr>("InputWorkspace", inputW);
-      alg1->setProperty<MatrixWorkspace_sptr>("OutputWorkspace", inputW);
-      alg1->setProperty("Offset", mT0);
-      alg1->executeAsChildAlg();
-      inputW = alg1->getProperty("OutputWorkspace");
-      // set T0 in the run parameters
       API::Run &run = inputW->mutableRun();
-      run.addProperty<double>("T0", mT0, true);
+      // Check to see if LoadEventNexus had T0 from TOPAZ Parameter file
+      if (!run.hasProperty("T0")) {
+        IAlgorithm_sptr alg1 = createChildAlgorithm("ChangeBinOffset");
+        alg1->setProperty<MatrixWorkspace_sptr>("InputWorkspace", inputW);
+        alg1->setProperty<MatrixWorkspace_sptr>("OutputWorkspace", inputW);
+        alg1->setProperty("Offset", mT0);
+        alg1->executeAsChildAlg();
+        inputW = alg1->getProperty("OutputWorkspace");
+        // set T0 in the run parameters
+        run.addProperty<double>("T0", mT0, true);
+      }
     }
 
     if (line[0] != '5')
-- 
GitLab