BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
DetectorItems.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/coregui/Models/DetectorItems.cpp
6 //! @brief Implements classes DetectorItems
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 
24 
25 namespace {
26 const QString res_func_group_label = "Type";
27 const QString analyzer_direction_tooltip = "Direction of the polarization analysis";
28 const QString analyzer_efficiency_tooltip = "Efficiency of the polarization analysis";
29 const QString analyzer_transmission_tooltip = "Total transmission of the polarization analysis";
30 } // namespace
31 
32 const QString DetectorItem::T_MASKS = "Mask tag";
33 const QString DetectorItem::P_RESOLUTION_FUNCTION = "Resolution function";
34 const QString DetectorItem::P_ANALYZER_DIRECTION = "Analyzer direction";
35 const QString DetectorItem::P_ANALYZER_EFFICIENCY = QString::fromStdString("Efficiency");
36 const QString DetectorItem::P_ANALYZER_TOTAL_TRANSMISSION = QString::fromStdString("Transmission");
37 
38 DetectorItem::DetectorItem(const QString& modelType) : SessionItem(modelType)
39 {
40  registerTag(T_MASKS, 0, -1, QStringList() << "MaskContainer");
42 
43  addProperty<VectorItem>(P_ANALYZER_DIRECTION)->setToolTip(analyzer_direction_tooltip);
46  .setToolTip(analyzer_efficiency_tooltip);
47  addProperty(P_ANALYZER_TOTAL_TRANSMISSION, 1.0)->setToolTip(analyzer_transmission_tooltip);
48 
49  QString additional_name = QString::fromStdString("Analyzer");
50  addTranslator(VectorParameterTranslator(P_ANALYZER_DIRECTION, "Direction", {additional_name}));
53 
54  mapper()->setOnPropertyChange([this](const QString& name) {
57  });
58 }
59 
60 std::unique_ptr<IDetector2D> DetectorItem::createDetector() const
61 {
62  auto result = createDomainDetector();
63  addMasksToDomain(result.get());
64 
65  if (auto resFunc = createResolutionFunction())
66  result->setResolutionFunction(*resFunc);
67 
68  kvector_t analyzer_dir = item<VectorItem>(P_ANALYZER_DIRECTION)->getVector();
69  double analyzer_eff = getItemValue(P_ANALYZER_EFFICIENCY).toDouble();
70  double analyzer_total_trans = getItemValue(P_ANALYZER_TOTAL_TRANSMISSION).toDouble();
71  if (analyzer_dir.mag() > 0.0)
72  result->setAnalyzerProperties(analyzer_dir, analyzer_eff, analyzer_total_trans);
73 
74  return result;
75 }
76 
78 {
79  if (auto maskContainer = maskContainerItem())
80  delete takeRow(rowOfChild(maskContainer));
81 }
82 
84 {
85  return dynamic_cast<MaskContainerItem*>(getItem(T_MASKS));
86 }
87 
89 {
90  if (!maskContainerItem())
92 }
93 
94 void DetectorItem::importMasks(const MaskContainerItem* maskContainer)
95 {
96  clearMasks();
97 
98  if (maskContainer)
99  model()->copyItem(maskContainer, this, T_MASKS);
100 }
101 
103 {
104  auto item = addGroupProperty(P_RESOLUTION_FUNCTION, "Resolution function group");
105  item->setDisplayName(res_func_group_label);
106  item->setToolTip("Detector resolution function");
107 }
108 
110 {
111  auto& resfuncItem = groupItem<ResolutionFunctionItem>(DetectorItem::P_RESOLUTION_FUNCTION);
112 
113  if (resfuncItem.modelType() == "ResolutionFunction2DGaussian") {
114  QString units = modelType() == "SphericalDetector" ? "deg" : "mm";
115 
117  ->setToolTip("Resolution along horizontal axis (in " + units + ")");
119  ->setToolTip("Resolution along vertical axis (in " + units + ")");
120  }
121 }
122 
123 std::unique_ptr<IResolutionFunction2D> DetectorItem::createResolutionFunction() const
124 {
125  auto& resfuncItem = groupItem<ResolutionFunctionItem>(DetectorItem::P_RESOLUTION_FUNCTION);
126  return resfuncItem.createResolutionFunction(axesToDomainUnitsFactor());
127 }
128 
130 {
131  auto maskContainer = maskContainerItem();
132 
133  if (!maskContainer)
134  return;
135 
136  const double scale = axesToDomainUnitsFactor();
137 
138  for (int i_row = maskContainer->children().size(); i_row > 0; --i_row) {
139  if (auto maskItem = dynamic_cast<MaskItem*>(maskContainer->children().at(i_row - 1))) {
140 
141  if (maskItem->modelType() == "RegionOfInterest") {
142  double xlow = scale * maskItem->getItemValue(RectangleItem::P_XLOW).toDouble();
143  double ylow = scale * maskItem->getItemValue(RectangleItem::P_YLOW).toDouble();
144  double xup = scale * maskItem->getItemValue(RectangleItem::P_XUP).toDouble();
145  double yup = scale * maskItem->getItemValue(RectangleItem::P_YUP).toDouble();
146  detector->setRegionOfInterest(xlow, ylow, xup, yup);
147 
148  } else {
149  std::unique_ptr<IShape2D> shape(maskItem->createShape(scale));
150  bool mask_value = maskItem->getItemValue(MaskItem::P_MASK_VALUE).toBool();
151  detector->addMask(*shape, mask_value);
152  }
153  }
154  }
155 }
Defines classes DetectorItems.
Defines interface IDetector2D.
Defines MaskItems classes.
Defines interface IParameterTranslator and subclasses.
Defines class ResolutionFunction2DGaussian.
Defines family of ResolutionFunctionItem.
Defines namespace SessionItemUtils.
Defines class SessionModel.
Defines class VectorItem.
double mag() const
Returns magnitude of the vector.
MaskContainerItem * maskContainerItem() const
DetectorItem(const QString &modelType)
void update_resolution_function_tooltips()
void register_resolution_function()
void importMasks(const MaskContainerItem *maskContainer)
static const QString P_ANALYZER_EFFICIENCY
Definition: DetectorItems.h:30
virtual std::unique_ptr< IDetector2D > createDomainDetector() const =0
std::unique_ptr< IResolutionFunction2D > createResolutionFunction() const
static const QString P_ANALYZER_DIRECTION
Definition: DetectorItems.h:29
virtual double axesToDomainUnitsFactor() const
Scales the values provided by axes (to perform deg->rad conversion on the way to domain).
Definition: DetectorItems.h:66
void addMasksToDomain(IDetector2D *detector) const
static const QString P_RESOLUTION_FUNCTION
Definition: DetectorItems.h:28
static const QString P_ANALYZER_TOTAL_TRANSMISSION
Definition: DetectorItems.h:31
static const QString T_MASKS
Definition: DetectorItems.h:27
std::unique_ptr< IDetector2D > createDetector() const
void createMaskContainer()
Abstract 2D detector interface.
Definition: IDetector2D.h:31
void setRegionOfInterest(double xlow, double ylow, double xup, double yup)
Sets rectangular region of interest with lower left and upper right corners defined.
Definition: IDetector2D.cpp:48
void addMask(const IShape2D &shape, bool mask_value=true)
Adds mask of given shape to the stack of detector masks.
Definition: IDetector2D.cpp:74
Container holding various masks as children.
Definition: MaskItems.h:24
A base class for all mask items.
Definition: MaskItems.h:31
static const QString P_MASK_VALUE
Definition: MaskItems.h:33
void setOnPropertyChange(std::function< void(QString)> f, const void *caller=0)
Definition: ModelMapper.cpp:35
static RealLimits limitless()
Creates an object withoud bounds (default)
Definition: RealLimits.cpp:130
static const QString P_XUP
Definition: MaskItems.h:43
static const QString P_YLOW
Definition: MaskItems.h:42
static const QString P_YUP
Definition: MaskItems.h:44
static const QString P_XLOW
Definition: MaskItems.h:41
SessionItem * addProperty(const QString &name, const QVariant &variant)
Add new property item and register new tag.
bool registerTag(const QString &name, int min=0, int max=-1, QStringList modelTypes={})
Add new tag to this item with given name, min, max and types.
int rowOfChild(SessionItem *child) const
Returns row index of given child.
QVariant getItemValue(const QString &tag) const
Directly access value of item under given tag.
SessionItem * addGroupProperty(const QString &groupTag, const QString &groupType)
Creates new group item and register new tag, returns GroupItem.
ModelMapper * mapper()
Returns the current model mapper of this item. Creates new one if necessary.
void setDefaultTag(const QString &tag)
Set default tag.
SessionItem * takeRow(int row)
Removes row from item and returns the item.
void addTranslator(const IPathTranslator &translator)
SessionModel * model() const
Returns model of this item.
Definition: SessionItem.cpp:66
T * item(const QString &tag) const
Definition: SessionItem.h:151
SessionItem & setToolTip(const QString &tooltip)
QString modelType() const
Get model type.
SessionItem * getItem(const QString &tag="", int row=0) const
Returns item in given row of given tag.
SessionItem & setLimits(const RealLimits &value)
T * copyItem(const T *item_to_copy, SessionItem *new_parent=0, const QString &tag="")
Definition: SessionModel.h:136
T * insertItem(SessionItem *parent=nullptr, int row=-1, QString tag="")
Definition: SessionModel.h:125
QString const & name(EShape k)
Definition: particles.cpp:21