BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
JobFunctions.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/Model/Model/JobFunctions.cpp
6 //! @brief Implements auxiliary functions in JobModelFunctions namespace.
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 "Base/Util/Assert.h"
17 #include "Device/Detector/IDetector.h"
28 #include "GUI/Model/Job/JobItem.h"
33 #include "GUI/Util/Error.h"
34 #include "GUI/Util/Path.h"
35 
36 namespace {
37 
38 //! Links RealDataItem to the JobItem's instrument.
39 // (re-)Linking is necessary because of following reason
40 // 1) Copying of RealDataItem from RealDataModel on board of JobItem requires relink to the copied
41 // insturment
42 // 2) During relink all masks (if exists) will be converted to the default units of current detector
43 void processInstrumentLink(JobItem* jobItem)
44 {
45  RealDataItem* realData = jobItem->realDataItem();
46  if (!realData)
47  throw Error("GUI::Model::JobFunctions::processInstrumentLink() -> Error. No data.");
48 
49  realData->linkToInstrument(jobItem->instrumentItem());
50 }
51 
52 //! Copies masks and ROI from RealDataItem on board of instrument.
53 void copyMasksToInstrument(JobItem* jobItem)
54 {
55  auto* mask_container = jobItem->realDataItem()->maskContainerItem();
56  jobItem->instrumentItem()->importMasks(mask_container);
57 }
58 
59 //! Crops RealDataItem to the region of interest.
60 void cropRealData(JobItem* jobItem)
61 {
62  ASSERT(jobItem->instrumentItem()->is<Instrument2DItem>());
63 
64  RealDataItem* realData = jobItem->realDataItem();
65 
66  // adjusting real data to the size of region of interest
67  IntensityDataItem* intensityItem = realData->intensityDataItem();
68 
69  std::unique_ptr<Datafield> origData(intensityItem->getDatafield()->clone());
70 
71  auto* instrument_item = jobItem->instrument2DItem();
72  GUI::Model::JobItemUtils::createDefaultDetectorMap(intensityItem, instrument_item);
73 
74  instrument_item->createInstrument()->detector().iterateOverNonMaskedPoints(
75  [&](IDetector::const_iterator it) {
76  auto* cropped_data = intensityItem->getDatafield();
77  (*cropped_data)[it.roiIndex()] = (*origData)[it.detectorIndex()];
78  });
79 
80  intensityItem->updateDataRange();
81 }
82 
83 //! Creates necessary fit containers for jobItem intended for fitting.
84 void createFitContainers(JobItem* jobItem)
85 {
86  FitSuiteItem* fitSuiteItem = jobItem->createFitSuiteItem();
87 
88  fitSuiteItem->createFitParametersContainer();
89  fitSuiteItem->createMinimizerContainer();
90 }
91 
92 } // namespace
93 
94 // ************************************************************************************************
95 // class implementation
96 // ************************************************************************************************
97 
99 {
100  ASSERT(job_item && job_item->isValidForFitting());
101  ASSERT(job_item->instrumentItem() && job_item->instrumentItem()->is<SpecularInstrumentItem>());
102  ASSERT(!job_item->dataItemView());
103 
104  auto* view_item = job_item->createDataViewItem();
105 
106  auto* property_container = view_item->createPropertyContainerItem();
107 
108  property_container->addItem(job_item->realDataItem()->dataItem());
109  property_container->addItem(job_item->dataItem());
110 
111  // also triggers Data1DViewItem::setAxesRangeToData and
112  // GUI::Model::DataViewUtils::updateAxesTitle by setting new value of P_AXES_UNITS.
113  auto* converter = job_item->instrumentItem()->createCoordSystem();
114  view_item->setAxesUnits(GUI::Model::JobItemUtils::availableUnits(*converter));
115 }
116 
118 {
119  auto* copiedInstrument = jobItem->copyInstrumentIntoJob(from);
120  copiedInstrument->setInstrumentName(copiedInstrument->instrumentType() + " instrument");
121  copiedInstrument->setId(QUuid::createUuid().toString());
122  jobItem->setInstrumentName(from->instrumentName());
123 }
124 
125 //! Setup items intended for storing results of the job.
126 
128 {
129  const bool isSpecularInstrument = jobItem->instrumentItem()->is<SpecularInstrumentItem>();
130  const bool isIntensityInstrument = jobItem->instrumentItem()->is<GISASInstrumentItem>()
131  || jobItem->instrumentItem()->is<OffspecInstrumentItem>()
132  || jobItem->instrumentItem()->is<DepthProbeInstrumentItem>();
133 
134  ASSERT(isSpecularInstrument || isIntensityInstrument);
135 
136  if (isSpecularInstrument)
137  jobItem->setDataType<SpecularDataItem>();
138  else if (isIntensityInstrument)
139  jobItem->setDataType<IntensityDataItem>();
140  else
141  ASSERT(0);
142 }
143 
144 //! Setups JobItem for fit.
145 
147  const RealDataItem* realDataItem)
148 {
149  ASSERT(jobItem->instrumentItem());
150 
151  copyRealDataItem(jobItem, realDataItem);
152  processInstrumentLink(jobItem);
153  copyMasksToInstrument(jobItem);
154 
155  if (jobItem->instrumentItem()->is<GISASInstrumentItem>())
156  cropRealData(jobItem);
157  if (jobItem->instrumentItem()->is<SpecularInstrumentItem>())
158  initDataView(jobItem);
159 
160  createFitContainers(jobItem);
161 }
162 
164 {
165  if (!realDataItem)
166  return;
167 
168  RealDataItem* realDataItemCopy = jobItem->copyRealDataIntoJob(realDataItem);
169  ASSERT(realDataItemCopy);
170 
171  realDataItemCopy->dataItem()->setDatafield(realDataItem->dataItem()->getDatafield()->clone());
172 
173  // adapting the name to job name
174  realDataItemCopy->dataItem()->setFileName(
176 
177  // #baimport ++ copy members of realDataItem?
178 
179  if (!realDataItem->hasNativeData())
180  return;
181 
182  realDataItemCopy->setNativeDatafield(realDataItem->nativeDatafield()->clone());
183  realDataItemCopy->nativeData()->setFileName(
185 }
Defines class Data1DViewItem.
Defines class DataPropertyContainer.
Defines error class.
Defines class FitSuiteItem.
Defines class InstrumentItem and all its children.
Defines class Instrument.
Defines class IntensityDataItem.
Defines namespace GUI::Model::FilenameUtils.
Defines abstract item with a material property.
Defines auxiliary functions in namespace GUI::Model::JobModelFunctions.
Defines namespace GUI::Model::JobItemUtils.
Defines class JobItem.
Defines class MaterialItem.
Defines MinimizerItem class.
Defines class Helpers functions.
Defines pointwise axis item.
Defines class RealDataItem.
Defines class SpecularDataItem.
DataPropertyContainer * createPropertyContainerItem()
virtual void setDatafield(Datafield *data)=0
The given pointer becomes owned by this class!!
Definition: DataItem.cpp:20
Datafield * getDatafield()
Definition: DataItem.h:41
void setFileName(const QString &filename)
Definition: DataItem.cpp:46
void addItem(DataItem *data_item)
Definition: Error.h:21
FitParameterContainerItem * createFitParametersContainer()
MinimizerContainerItem * createMinimizerContainer()
Abstract base class for instrument-specific item classes.
bool is() const
QString instrumentName() const
virtual ICoordSystem * createCoordSystem() const =0
void setInstrumentName(const QString &instrumentName)
virtual void importMasks(const MaskContainerItem *)
void updateDataRange()
Sets min,max values for z-axis, if axes is not locked, and ranges are not yet set.
Instrument2DItem * instrument2DItem()
Definition: JobItem.cpp:238
Data1DViewItem * dataItemView()
Definition: JobItem.cpp:305
QString jobName() const
Definition: JobItem.cpp:84
void setInstrumentName(const QString &name)
Definition: JobItem.cpp:330
InstrumentItem * instrumentItem() const
Definition: JobItem.cpp:233
T * setDataType()
Definition: JobItem.h:171
DataItem * dataItem()
Definition: JobItem.cpp:101
RealDataItem * realDataItem()
Definition: JobItem.cpp:290
bool isValidForFitting()
Definition: JobItem.cpp:154
RealDataItem * copyRealDataIntoJob(const RealDataItem *real_data)
Definition: JobItem.cpp:295
QString getIdentifier() const
Definition: JobItem.cpp:73
FitSuiteItem * createFitSuiteItem()
Definition: JobItem.cpp:262
InstrumentItem * copyInstrumentIntoJob(const InstrumentItem *instrument)
Definition: JobItem.cpp:245
Data1DViewItem * createDataViewItem()
Definition: JobItem.cpp:315
Provides access to experimental data, for display and fitting. Owns an AbstractDataLoader.
Definition: RealDataItem.h:33
MaskContainerItem * maskContainerItem()
Returns mask container item.
void linkToInstrument(const InstrumentItem *instrument)
void setNativeDatafield(Datafield *data)
takes ownership of data
bool hasNativeData() const
IntensityDataItem * intensityDataItem()
DataItem * nativeData()
const Datafield * nativeDatafield() const
DataItem * dataItem()
QString jobReferenceFileName(const QString &itemName)
Constructs the name of the file with reference data.
QString jobNativeDataFileName(const QString &id)
void setupJobItemOutput(JobItem *jobItem)
Setup items intended for storing results of the job.
void copyRealDataItem(JobItem *jobItem, const RealDataItem *realDataItem)
Copy RealDataItem to jobItem intended for fitting.
void setupJobItemForFit(JobItem *jobItem, const RealDataItem *realDataItem)
Setups JobItem for fit.
void setupJobItemInstrument(JobItem *jobItem, const InstrumentItem *from)
Properly copies instrument into job item.
void initDataView(JobItem *job_item)
Initializes Data1DViewItem and assigns it to the passed JobItem.
void createDefaultDetectorMap(DataItem *intensityItem, const Instrument2DItem *instrumentItem)
ComboProperty availableUnits(const ICoordSystem &converter)
QString toString(const QModelIndex &index)
Provides string representation of index data.