From e250b4ec6fb85ab5ed220bcd7b7417a1967a18cb Mon Sep 17 00:00:00 2001
From: Mikhail Svechnikov <m.svechnikov@fz-juelich.de>
Date: Mon, 16 Dec 2024 16:12:31 +0100
Subject: [PATCH] check data alignment

---
 GUI/Model/Job/JobItem.cpp         |  4 ++--
 GUI/Model/Job/JobItem.h           |  2 +-
 GUI/Model/Sim/InstrumentItems.cpp |  6 ++++--
 GUI/View/View/SimulationView.cpp  | 16 ++++++++++++----
 4 files changed, 19 insertions(+), 9 deletions(-)

diff --git a/GUI/Model/Job/JobItem.cpp b/GUI/Model/Job/JobItem.cpp
index 4fce669fbea..40029de52b8 100644
--- a/GUI/Model/Job/JobItem.cpp
+++ b/GUI/Model/Job/JobItem.cpp
@@ -94,7 +94,7 @@ JobItem::JobItem(const SampleItem* sampleItem, const InstrumentItem* instrumentI
 
     if (dfile_item) {
         copyDatafileItemIntoJob(dfile_item);
-        adjustReaDataToJobInstrument();
+        adjustRealDataToJobInstrument();
 
         createDiffDataItem();
     }
@@ -166,7 +166,7 @@ void JobItem::copyDatafileItemIntoJob(const DatafileItem* source)
         m_dfile_item->data1DItem()->setRealPlotStyle();
 }
 
-void JobItem::adjustReaDataToJobInstrument()
+void JobItem::adjustRealDataToJobInstrument()
 {
     if (instrumentItem()->is<SpecularInstrumentItem>()) {
         // coords of loaded data --> instrumental --> representation coords
diff --git a/GUI/Model/Job/JobItem.h b/GUI/Model/Job/JobItem.h
index e5870fab81d..7f9fd8e7aa0 100644
--- a/GUI/Model/Job/JobItem.h
+++ b/GUI/Model/Job/JobItem.h
@@ -101,7 +101,7 @@ public:
 
     void copyDatafileItemIntoJob(const DatafileItem* source);
     const DatafileItem* dfileItem() const { return m_dfile_item.get(); }
-    void adjustReaDataToJobInstrument();
+    void adjustRealDataToJobInstrument();
 
     void updateFileName();
 
diff --git a/GUI/Model/Sim/InstrumentItems.cpp b/GUI/Model/Sim/InstrumentItems.cpp
index 54a6b7326bd..02410c4b1e6 100644
--- a/GUI/Model/Sim/InstrumentItems.cpp
+++ b/GUI/Model/Sim/InstrumentItems.cpp
@@ -113,6 +113,7 @@ InstrumentItem* InstrumentItem::clone() const
 
 bool InstrumentItem::alignedWith(const DatafileItem* dfi) const
 {
+    ASSERT(dfi);
     for (size_t i = 0; i < detectorRank(); ++i)
         if (axdim(i) != dfi->axdim(i))
             return false;
@@ -425,6 +426,7 @@ void SpecularInstrumentItem::updateToRealData(const DatafileItem* dfi)
 
 bool SpecularInstrumentItem::alignedWith(const DatafileItem* dfi) const
 {
+    ASSERT(dfi);
     if (!dfi->holdsDimensionalData())
         return scanItem()->uniformAlphaAxisSelected() && axdim(0) == dfi->axdim(0);
 
@@ -438,8 +440,8 @@ bool SpecularInstrumentItem::alignedWith(const DatafileItem* dfi) const
     if (!instrumentAxis)
         return false;
 
-    const Scale& native_axis = dfi->dataItem()->c_field()->axis(0);
-    return *instrumentAxis == native_axis;
+    const Scale& real_data_axis = dfi->dataItem()->c_field()->axis(0);
+    return *instrumentAxis == real_data_axis;
 }
 
 std::unique_ptr<Frame> SpecularInstrumentItem::createFrame() const
diff --git a/GUI/View/View/SimulationView.cpp b/GUI/View/View/SimulationView.cpp
index 627dd527211..d37645d15d8 100644
--- a/GUI/View/View/SimulationView.cpp
+++ b/GUI/View/View/SimulationView.cpp
@@ -324,16 +324,24 @@ void SimulationView::updateStateFromUI()
 void SimulationView::simulate()
 {
     readOptionsFromUI();
-    if (!gDoc->instruments()->currentItem()) {
+
+    auto* instrument = gDoc->instruments()->currentItem();
+    auto* sample = gDoc->samples()->currentItem();
+    auto* dataset = optionsItem()->useDataset() ? gDoc->datafiles()->currentItem() : nullptr;
+
+    if (!instrument) {
         GUI::Message::warning("Simulate", "Cannot launch simulation as no instrument is selected");
         return;
     }
-    if (!gDoc->samples()->currentItem()) {
+    if (!sample) {
         GUI::Message::warning("Simulate", "Cannot launch simulation as no sample is selected");
         return;
     }
-    auto* job_item = new JobItem(gDoc->samples()->currentItem(), gDoc->instruments()->currentItem(),
-                                 gDoc->datafiles()->currentItem(), optionsItem());
+    if (dataset && !instrument->alignedWith(dataset)) {
+        GUI::Message::warning("Simulate", "The dataset does not fit the instrument");
+        return;
+    }
+    auto* job_item = new JobItem(sample, instrument, dataset, optionsItem());
     gDoc->jobsRW()->addJobItem(job_item);
     GUI::Sim::simulate(job_item, gDoc->jobsRW());
     gDoc->setModified();
-- 
GitLab