BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
data1ditem.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // qt-mvvm: Model-view-view-model framework for large GUI applications
4 //
5 //! @file mvvm/model/mvvm/standarditems/data1ditem.cpp
6 //! @brief Implements class CLASS?
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2020
11 //! @authors Gennady Pospelov et al, Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
17 #include <stdexcept>
18 
19 using namespace ModelView;
20 
21 namespace {
22 size_t total_bin_count(Data1DItem* item)
23 {
24  auto axis = item->item<BinnedAxisItem>(Data1DItem::T_AXIS);
25  return axis ? static_cast<size_t>(axis->size()) : 0;
26 }
27 } // namespace
28 
30 {
31  // prevent editing in widgets, since there is no corresponding editor
32  addProperty(P_VALUES, std::vector<double>())->setDisplayName("Values")->setEditable(false);
33 
34  addProperty(P_ERRORS, std::vector<double>())->setDisplayName("Errors")->setEditable(false);
35 
38  true);
39  setValues(std::vector<double>());
40 }
41 
42 //! Sets axis. Bin content will be set to zero.
43 
44 // void Data1DItem::setAxis(std::unique_ptr<BinnedAxisItem> axis)
45 //{
46 // // we disable possibility to re-create axis to facilitate undo/redo
47 
48 // if (getItem(T_AXIS, 0))
49 // throw std::runtime_error("Axis was already set. Currently we do not support axis change");
50 
51 // insertItem(axis.release(), {T_AXIS, 0});
52 // setValues(std::vector<double>(total_bin_count(this), 0.0));
53 //}
54 
55 //! Returns coordinates of bin centers.
56 
57 std::vector<double> Data1DItem::binCenters() const
58 {
59  auto axis = item<BinnedAxisItem>(T_AXIS);
60  return axis ? axis->binCenters() : std::vector<double>{};
61 }
62 
63 //! Sets internal data buffer to given data. If size of axis doesn't match the size of the data,
64 //! exception will be thrown.
65 
66 void Data1DItem::setValues(const std::vector<double>& data)
67 {
68  if (total_bin_count(this) != data.size())
69  throw std::runtime_error("Data1DItem::setValues() -> Data doesn't match size of axis");
70 
72 }
73 
74 //! Returns values stored in bins.
75 
76 std::vector<double> Data1DItem::binValues() const
77 {
78  return property<std::vector<double>>(P_VALUES);
79 }
80 
81 //! Sets errors on values in bins.
82 
83 void Data1DItem::setErrors(const std::vector<double>& errors)
84 {
85  if (total_bin_count(this) != errors.size())
86  throw std::runtime_error("Data1DItem::setErrors() -> Data doesn't match size of axis");
87 
88  setProperty(P_ERRORS, errors);
89 }
90 
91 //! Returns value errors stored in bins.
92 
93 std::vector<double> Data1DItem::binErrors() const
94 {
95  return property<std::vector<double>>(P_ERRORS);
96 }
Defines class CLASS?
Item to represent an axis with arbitrary binning.
Definition: axisitems.h:61
Complex item holding mixed SessionItem types (single properties and other CompountItems).
Definition: compounditem.h:28
T * addProperty(const std::string &name)
Adds property item of given type.
Definition: compounditem.h:43
Represents one-dimensional data (axis and values).
Definition: data1ditem.h:30
static const std::string T_AXIS
Definition: data1ditem.h:34
std::vector< double > binValues() const
Returns values stored in bins.
Definition: data1ditem.cpp:76
void setValues(const std::vector< double > &data)
Sets internal data buffer to given data.
Definition: data1ditem.cpp:66
std::vector< double > binCenters() const
Sets axis. Bin content will be set to zero.
Definition: data1ditem.cpp:57
static const std::string P_VALUES
Definition: data1ditem.h:32
void setErrors(const std::vector< double > &errors)
Sets errors on values in bins.
Definition: data1ditem.cpp:83
std::vector< double > binErrors() const
Returns value errors stored in bins.
Definition: data1ditem.cpp:93
static const std::string P_ERRORS
Definition: data1ditem.h:33
void registerTag(const TagInfo &tagInfo, bool set_as_default=false)
Registers tag to hold items under given name.
T data(int role=ItemDataRole::DATA) const
Returns data of given type T for given role.
Definition: sessionitem.h:148
T * item(const std::string &tag) const
Returns first item under given tag casted to a specified type.
Definition: sessionitem.h:156
void setProperty(const std::string &tag, const T &value)
Sets value to property item.
Definition: sessionitem.h:190
Holds info about single tag for SessionItem.
Definition: taginfo.h:28
Defines class CLASS?
const model_type Data1DItemType
Definition: mvvm_types.h:50
const model_type FixedBinAxisItemType
Definition: mvvm_types.h:52
const model_type PointwiseAxisItemType
Definition: mvvm_types.h:58
materialitems.h Collection of materials to populate MaterialModel.