BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
FitComparisonController.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/View/PlotComparison/FitComparisonController.cpp
6 //! @brief Implements class FitComparisonController
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
16 #include "Device/Histo/DiffUtil.h"
21 #include "GUI/Model/Job/JobItem.h"
23 
24 namespace {
25 
26 // different limits on relative difference plot are required
27 // to provide the best appearance
28 const double relative_diff_min_2d = 1e-05;
29 const double relative_diff_max_2d = 1.0;
30 
31 } // namespace
32 
34 public:
35  DiffItemController(const QString& data_type, QObject* parent);
36  ~DiffItemController() override;
37  void setItem(JobItem* job_item);
40  void subscribe();
41  void unsubscribe();
42 
43 private:
47 };
48 
50 
52  : QObject(parent)
53  , m_diff_item_controller(new DiffItemController(IntensityDataItem::M_TYPE, this))
54  , m_appearanceRepeater(new PropertyRepeater(this))
55  , m_xAxisRepeater(new PropertyRepeater(this))
56  , m_yAxisRepeater(new PropertyRepeater(this))
57  , m_zAxisRepeater(new PropertyRepeater(this))
58 {
59 }
60 
62 {
63  ASSERT(dynamic_cast<IntensityDataItem*>(m_diff_item_controller->diffItem()));
64  return dynamic_cast<IntensityDataItem*>(m_diff_item_controller->diffItem());
65 }
66 
68 {
69  ASSERT(job_item);
70 
71  clear();
73 
74  auto* sim_data_item = dynamic_cast<IntensityDataItem*>(job_item->dataItem());
75  auto* real_data_item = dynamic_cast<IntensityDataItem*>(job_item->realDataItem()->dataItem());
76 
77  double zmin = real_data_item->getLowerZ();
78  double zmax = real_data_item->getUpperZ();
79  sim_data_item->setLowerAndUpperZ(zmin, zmax);
80 
81  diffItem()->xAxisItem()->setTitle(sim_data_item->getXaxisTitle());
82  diffItem()->yAxisItem()->setTitle(sim_data_item->getYaxisTitle());
83  diffItem()->setLowerAndUpperZ(relative_diff_min_2d, relative_diff_max_2d);
85 
86  m_appearanceRepeater->addItem(real_data_item);
87  m_appearanceRepeater->addItem(sim_data_item);
89 
90  m_xAxisRepeater->addItem(real_data_item->xAxisItem());
91  m_xAxisRepeater->addItem(sim_data_item->xAxisItem());
92  m_xAxisRepeater->addItem(diffItem()->xAxisItem());
93 
94  m_yAxisRepeater->addItem(real_data_item->yAxisItem());
95  m_yAxisRepeater->addItem(sim_data_item->yAxisItem());
96  m_yAxisRepeater->addItem(diffItem()->yAxisItem());
97 
98  m_zAxisRepeater->addItem(real_data_item->zAxisItem());
99  m_zAxisRepeater->addItem(sim_data_item->zAxisItem());
100 }
101 
103 {
105 }
106 
108 {
109  diffItem()->resetView();
110  diffItem()->setLowerAndUpperZ(relative_diff_min_2d, relative_diff_max_2d);
111 }
112 
114 {
116 
121 }
122 
123 DiffItemController::DiffItemController(const QString& data_type, QObject* parent)
124  : QObject(parent)
125  , m_current_item(nullptr)
126  , m_tempIntensityDataModel(new SessionModel("TempIntensityDataModel", this))
127  , m_diff_item(dynamic_cast<DataItem*>(m_tempIntensityDataModel->insertNewItem(data_type)))
128 {
129  ASSERT(m_diff_item);
130 }
131 
133 {
134  unsubscribe();
135 }
136 
137 void DiffItemController::setItem(JobItem* job_item)
138 {
139  ASSERT(job_item);
140  if (m_current_item)
141  unsubscribe();
142  m_current_item = job_item;
143  subscribe();
144  updateDiffData();
145 }
146 
148 {
149  ASSERT(m_current_item);
150 
151  auto* sim_data = m_current_item->dataItem();
152  auto* real_data = m_current_item->realDataItem()->dataItem();
153  ASSERT(sim_data && real_data);
154 
155  if (!sim_data->getDatafield()) // job failed
156  return;
157 
159  DiffUtil::relativeDifferenceField(*sim_data->getDatafield(), *real_data->getDatafield()));
160 }
161 
163 {
164  return m_diff_item;
165 }
166 
168 {
169  if (!m_current_item) {
170  ASSERT(false);
171  return;
172  }
173 
174  // on simulation data change
176  [=](Datafield*) { updateDiffData(); });
177 
178  // on diff item units change
179  connect(m_diff_item, &DataItem::axesUnitsChanged, this, [=](const ComboProperty&) {
181  });
182 }
183 
185 {
186  if (!m_current_item)
187  return;
188 
189  disconnect(m_current_item->dataItem(), nullptr, this, nullptr);
190  disconnect(m_diff_item, nullptr, this, nullptr);
191  m_current_item = nullptr;
192 }
Defines various axis items.
FitComparisonController2D::DiffItemController DiffItemController
Defines class FitComparisonController.
Defines class IntensityDataItem.
Defines class JobItem.
Defines class PropertyRepeater.
Defines class RealDataItem.
Defines class SpecularDataItem.
void setTitle(const QString &title)
Definition: AxesItems.cpp:85
Custom property to define list of string values with multiple selections. Intended for QVariant.
Definition: ComboProperty.h:25
Abstract base class for IntensityDataItem and SpecularDataItem. Owns one simulated data set of type D...
Definition: DataItem.h:34
virtual void setDatafield(Datafield *data)=0
The given pointer becomes owned by this class!!
Definition: DataItem.cpp:20
void datafieldChanged(Datafield *data)
virtual void updateCoords(const InstrumentItem *instrument)=0
void axesUnitsChanged(const ComboProperty &units)
DiffItemController(const QString &data_type, QObject *parent)
DiffItemController(const QString &data_type, QObject *parent)
void setItem(JobItem *job_item)
DiffItemController * m_diff_item_controller
PropertyRepeater * m_appearanceRepeater
FitComparisonController2D(QObject *parent=nullptr)
double getLowerZ() const
Returns lower and upper zoom ranges of z-axis.
void setLowerAndUpperZ(double zmin, double zmax)
void setAxesRangeToData() override
set zoom range of x,y axes to axes of input data
void resetView()
Set axes viewport to original data.
const BasicAxisItem * yAxisItem() const
const BasicAxisItem * xAxisItem() const
InstrumentItem * instrumentItem() const
Definition: JobItem.cpp:233
DataItem * dataItem()
Definition: JobItem.cpp:101
RealDataItem * realDataItem()
Definition: JobItem.cpp:290
Tracks property change (axes range, units etc) for the collection of IntensityDataItems and sets same...
void addItem(SessionItem *sessionItem)
DataItem * dataItem()
Base class for a GUI data collection. A collection is e.g. all real data (RealDataModel)....
Definition: SessionModel.h:42