LogManager.h 8.54 KB
Newer Older
1
2
3
4
5
#ifndef MANTID_API_LOGMANAGER_H_
#define MANTID_API_LOGMANAGER_H_

#include "MantidAPI/DllConfig.h"
#include "MantidKernel/Cache.h"
Hahn, Steven's avatar
Hahn, Steven committed
6
#include "MantidKernel/make_unique.h"
7
8
9
10
11
#include "MantidKernel/PropertyManager.h"
#include "MantidKernel/Statistics.h"
#include "MantidKernel/TimeSplitter.h"
#include <vector>

12
namespace NeXus {
Nick Draper's avatar
Nick Draper committed
13
class File;
14
15
}

16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
namespace Mantid {
namespace Kernel {
template <typename TYPE> class TimeSeriesProperty;
}

namespace API {

/**
   This class contains the information about the log entries


   @author Martyn Gigg, Tessella plc
   @date 02/10/201

   Copyright &copy; 2010 ISIS Rutherford Appleton Laboratory, NScD Oak Ridge
   National Laboratory & European Spallation Source

   This file is part of Mantid.

   Mantid is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   Mantid is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.

   File change history is stored at: <https://github.com/mantidproject/mantid>.
   Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class MANTID_API_DLL LogManager {
public:
  /// Destructor. Doesn't need to be virtual as long as nothing inherits from
  /// this class.
55
  virtual ~LogManager() = default;
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

  //-------------------------------------------------------------
  /// Set the run start and end
  void setStartAndEndTime(const Kernel::DateAndTime &start,
                          const Kernel::DateAndTime &end);
  /// Return the run start time
  const Kernel::DateAndTime startTime() const;
  /// Return the run end time
  const Kernel::DateAndTime endTime() const;
  //-------------------------------------------------------------

  /// Filter the logs by time
  virtual void filterByTime(const Kernel::DateAndTime start,
                            const Kernel::DateAndTime stop);
  /// Split the logs based on the given intervals
  virtual void splitByTime(Kernel::TimeSplitterType &splitter,
                           std::vector<LogManager *> outputs) const;
  /// Filter the run by the given boolean log
  void filterByLog(const Kernel::TimeSeriesProperty<bool> &filter);

  /// Return an approximate memory size for the object in bytes
  virtual size_t getMemorySize() const;

  /// Add data to the object in the form of a property
Hahn, Steven's avatar
Hahn, Steven committed
80
81
82
83
84
85
86
  /// @deprecated new code should use smart pointers
  void addProperty(Kernel::Property *prop, bool overwrite = false) {
    addProperty(std::unique_ptr<Kernel::Property>(prop), overwrite);
  };
  /// Add data to the object in the form of a property
  void addProperty(std::unique_ptr<Kernel::Property> prop,
                   bool overwrite = false);
87
88
89
90
91
92
93
94
95
96
97
98
  /// Add a property of given type
  template <class TYPE>
  void addProperty(const std::string &name, const TYPE &value,
                   bool overwrite = false);

  template <class TYPE>
  void addProperty(const std::string &name, const TYPE &value,
                   const std::string &units, bool overwrite = false);

  /// Does the property exist on the object
  bool hasProperty(const std::string &name) const;
  /// Remove a named property
99
  void removeProperty(const std::string &name, bool delProperty = true);
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
  /**
   * Return all of the current properties
   * @returns A vector of the current list of properties
   */
  inline const std::vector<Kernel::Property *> &getProperties() const {
    return m_manager.getProperties();
  }
  /// Returns a property as a time series property. It will throw if it is not
  /// valid
  template <typename T>
  Kernel::TimeSeriesProperty<T> *
  getTimeSeriesProperty(const std::string &name) const;
  /// Get the value of a property as the given TYPE. Throws if the type is not
  /// correct
  template <typename HeldType>
  HeldType getPropertyValueAsType(const std::string &name) const;
  /// Returns a property as a single double value from its name
  double getPropertyAsSingleValue(
      const std::string &name,
      Kernel::Math::StatisticType statistic = Kernel::Math::Mean) const;
  /// Returns the named property as a pointer
  Kernel::Property *getProperty(const std::string &name) const;

Hahn, Steven's avatar
Hahn, Steven committed
123
124
125
126
127
128
129
130
  /**
   * Add a log entry
   * @param p :: A pointer to the property containing the log entry
   * @deprecated new code should use smart pointers
   */
  void addLogData(Kernel::Property *p) {
    addLogData(std::unique_ptr<Kernel::Property>(p));
  }
131
132
133
134
  /**
   * Add a log entry
   * @param p :: A pointer to the property containing the log entry
   */
Hahn, Steven's avatar
Hahn, Steven committed
135
136
137
  void addLogData(std::unique_ptr<Kernel::Property> p) {
    addProperty(std::move(p));
  }
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
  /**
   * Access a single log entry
   * @param name :: The name of the log entry to retrieve
   * @returns A pointer to a property containing the log entry
   */
  Kernel::Property *getLogData(const std::string &name) const {
    return getProperty(name);
  }
  /**
   * Access all log entries
   * @returns A list of all of the log entries
   */
  const std::vector<Kernel::Property *> &getLogData() const {
    return getProperties();
  }
  /**
   * Remove a named log entry
   * @param name :: The name of the entry to remove
   * @param delproperty :: If true, delete the log entry
   */
  void removeLogData(const std::string &name, const bool delproperty = true) {
    return removeProperty(name, delproperty);
  }
  /**
   * @param name :: The name of the property
   * @param statistic :: Defines how to calculate the single value from series
   * (default=Mean)
   * @return A log as a single value using the given statistic type
   */
  double getLogAsSingleValue(
      const std::string &name,
      Kernel::Math::StatisticType statistic = Kernel::Math::Mean) const {
    return getPropertyAsSingleValue(name, statistic);
171
172
  }

173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
  /// Empty the values out of all TimeSeriesProperty logs
  void clearTimeSeriesLogs();
  /// Empty all but the last value out of all TimeSeriesProperty logs
  void clearOutdatedTimeSeriesLogValues();

  /// Save the run to a NeXus file with a given group name
  virtual void saveNexus(::NeXus::File *file, const std::string &group,
                         bool keepOpen = false) const;
  /// Load the run from a NeXus file with a given group name
  virtual void loadNexus(::NeXus::File *file, const std::string &group,
                         bool keepOpen = false);
  /// Clear the logs
  void clearLogs();

protected:
  /// A pointer to a property manager
  Kernel::PropertyManager m_manager;
  /// Name of the log entry containing the proton charge when retrieved using
  /// getProtonCharge
  static const char *PROTON_CHARGE_LOG_NAME;

private:
  /// Cache type for single value logs
  typedef Kernel::Cache<std::pair<std::string, Kernel::Math::StatisticType>,
                        double> SingleValueCache;
  /// Cache for the retrieved single values
  mutable SingleValueCache m_singleValueCache;
};
/// shared pointer to the logManager base class
typedef boost::shared_ptr<LogManager> LogManager_sptr;
/// shared pointer to the logManager base class (const version)
typedef boost::shared_ptr<const LogManager> LogManager_const_sptr;

/**
 * Add a property of a specified type (Simply creates a Kernel::Property of that
 * type
 * @param name :: The name of the type
 * @param value :: The value of the property
 * @param overwrite :: If true, a current value is overwritten. (Default: False)
 */
template <class TYPE>
void LogManager::addProperty(const std::string &name, const TYPE &value,
                             bool overwrite) {
Hahn, Steven's avatar
Hahn, Steven committed
216
217
218
  addProperty(
      Mantid::Kernel::make_unique<Kernel::PropertyWithValue<TYPE>>(name, value),
      overwrite);
219
}
220

221
222
223
224
225
226
227
228
229
230
231
232
/**
 * Add a property of a specified type (Simply creates a Kernel::Property of that
 * type)
 *  and set its units.
 * @param name :: The name of the type
 * @param value :: The value of the property
 * @param units :: a string giving the units of the property.
 * @param overwrite :: If true, a current value is overwritten. (Default: False)
 */
template <class TYPE>
void LogManager::addProperty(const std::string &name, const TYPE &value,
                             const std::string &units, bool overwrite) {
Hahn, Steven's avatar
Hahn, Steven committed
233
234
  auto newProp =
      Mantid::Kernel::make_unique<Kernel::PropertyWithValue<TYPE>>(name, value);
235
  newProp->setUnits(units);
Hahn, Steven's avatar
Hahn, Steven committed
236
  addProperty(std::move(newProp), overwrite);
237
238
}
}
239
240
}

241
#endif // MANTID_API_LOGMANAGER_H_