PeaksWorkspace.cpp 10.2 KB
Newer Older
1
2
#include "MantidAPI/AlgorithmFactory.h"
#include "MantidAPI/Column.h"
Peterson, Peter's avatar
Peterson, Peter committed
3
#include "MantidAPI/ColumnFactory.h"
4
#include "MantidAPI/MatrixWorkspace.h"
5
6
#include "MantidNexusCPP/NeXusFile.hpp"
#include "MantidNexusCPP/NeXusException.hpp"
Peterson, Peter's avatar
Peterson, Peter committed
7
#include "MantidAPI/WorkspaceFactory.h"
8
#include "MantidAPI/WorkspaceProperty.h"
Peterson, Peter's avatar
Peterson, Peter committed
9
#include "MantidDataObjects/PeaksWorkspace.h"
10
11
12
#include "MantidDataObjects/TableColumn.h"
#include "MantidDataObjects/TableWorkspace.h"
#include "MantidDataObjects/Peak.h"
13
14
#include "MantidKernel/Quat.h"
#include "MantidKernel/V3D.h"
15
16
17
18
#include "MantidKernel/DateAndTime.h"
#include "MantidKernel/Logger.h"
#include "MantidKernel/PhysicalConstants.h"
#include "MantidKernel/Unit.h"
Peterson, Peter's avatar
Peterson, Peter committed
19
#include <algorithm>
20
#include <boost/shared_ptr.hpp>
Peterson, Peter's avatar
Peterson, Peter committed
21
22
#include <exception>
#include <fstream>
23
24
25
#include <iostream>
#include <math.h>
#include <ostream>
Peterson, Peter's avatar
Peterson, Peter committed
26
27
#include <stdio.h>
#include <stdlib.h>
28
29
30
31
32
#include <string>

using namespace Mantid::API;
using namespace Mantid::Kernel;
using namespace Mantid::Geometry;
Peterson, Peter's avatar
Peterson, Peter committed
33
34
35
36
37
38
39
40

namespace Mantid
{
namespace DataObjects
{
  /// Register the workspace as a type
  DECLARE_WORKSPACE(PeaksWorkspace );

41
//  Kernel::Logger& PeaksWorkspace::g_log = Kernel::Logger::get("PeaksWorkspace");
Peterson, Peter's avatar
Peterson, Peter committed
42
43


44
  //---------------------------------------------------------------------------------------------
Peterson, Peter's avatar
Peterson, Peter committed
45
46
47
48
  /** Constructor. Create a table with all the required columns.
   *
   * @return PeaksWorkspace object
   */
49
  PeaksWorkspace::PeaksWorkspace()
50
  : IPeaksWorkspace()
51
52
53
54
  {
    initColumns();
  }

55
56
57
58
59
60
61
62
63
64
65
  //---------------------------------------------------------------------------------------------
  /** Virtual constructor. Clone method to duplicate the peaks workspace.
   *
   * @return PeaksWorkspace object
   */
  PeaksWorkspace* PeaksWorkspace::clone() const
  {
    //Deep copy via copy construtor.
    return new PeaksWorkspace(*this);
  }

66
67
68
69
70
71
72

  //---------------------------------------------------------------------------------------------
  /** Copy constructor
   *
   * @param other :: other PeaksWorkspace to copy from
   * @return
   */
73
  PeaksWorkspace::PeaksWorkspace(const PeaksWorkspace & other)
74
  : IPeaksWorkspace(other),
75
    peaks(other.peaks)
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
  {
    initColumns();
    this->peaks = other.peaks;
  }

  //---------------------------------------------------------------------------------------------
  /** Clone a shared pointer
   *
   * @param other :: other PeaksWorkspace to copy from
   * @return copy of the peaksworkspace
   */
  boost::shared_ptr<PeaksWorkspace> PeaksWorkspace::clone()
  {
    // Copy construct and return
    return boost::shared_ptr<PeaksWorkspace>(new PeaksWorkspace(*this));
  }

  //---------------------------------------------------------------------------------------------
  /** Initialize all columns */
  void PeaksWorkspace::initColumns()
Peterson, Peter's avatar
Peterson, Peter committed
96
  {
97
98
99
100
101
102
103
104
105
106
107
108
109
    // Note: These column names must match what PeakColumn expects!
    addColumn( "int", "RunNumber");
    addColumn( "int", "DetID");
    addColumn( "double", "h");
    addColumn( "double", "k");
    addColumn( "double", "l");
    addColumn( "double", "Wavelength");
    addColumn( "double", "Energy");
    addColumn( "double", "TOF");
    addColumn( "double", "DSpacing");
    addColumn( "double", "Intens");
    addColumn( "double", "SigInt");
    addColumn( "double", "BinCount");
110
    addColumn( "str", "BankName");
111
112
113
114
    addColumn( "double", "Row");
    addColumn( "double", "Col");
    addColumn( "V3D", "QLab");
    addColumn( "V3D", "QSample");
Peterson, Peter's avatar
Peterson, Peter committed
115
116
  }

117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
  //=====================================================================================
  //=====================================================================================
  /** Comparator class for sorting peaks by one or more criteria
   */
  class PeakComparator : public std::binary_function<Peak,Peak,bool>
  {
  public:
    std::vector< std::pair<std::string, bool> > & criteria;

    /** Constructor for the comparator for sorting peaks
    * @param criteria : a vector with a list of pairs: column name, bool;
    *        where bool = true for ascending, false for descending sort.
    */
    PeakComparator(std::vector< std::pair<std::string, bool> > & criteria)
    : criteria(criteria)
    {
    }

    /** Compare two peaks using the stored criteria */
    inline bool operator()(const Peak& a, const Peak& b)
    {
      for (size_t i = 0; i < criteria.size(); i++)
      {
        std::string & col = criteria[i].first;
        bool ascending = criteria[i].second;
        bool lessThan = false;
        if (col == "BankName")
        {
          // If this criterion is equal, move on to the next one
          std::string valA = a.getBankName();
          std::string valB = b.getBankName();
          // Move on to lesser criterion if equal
          if (valA == valB)
            continue;
          lessThan = (valA < valB);
        }
        else
        {
          // General double comparison
          double valA = a.getValueByColName(col);
          double valB = b.getValueByColName(col);
          // Move on to lesser criterion if equal
          if (valA == valB)
            continue;
          lessThan = (valA < valB);
        }
        // Flip the sign of comparison if descending.
        if (ascending)
          return lessThan;
        else
          return !lessThan;

      }
      // If you reach here, all criteria were ==; so not <, so return false
      return false;
    }
  };


  //---------------------------------------------------------------------------------------------
  /** Sort the peaks by one or more criteria
   *
   * @param criteria : a vector with a list of pairs: column name, bool;
   *        where bool = true for ascending, false for descending sort.
   *        The peaks are sorted by the first criterion first, then the 2nd if equal, etc.
   */
  void PeaksWorkspace::sort(std::vector< std::pair<std::string, bool> > & criteria)
  {
    PeakComparator comparator(criteria);
    std::stable_sort(peaks.begin(), peaks.end(), comparator);
  }

Peterson, Peter's avatar
Peterson, Peter committed
189

190
  //---------------------------------------------------------------------------------------------
Peterson, Peter's avatar
Peterson, Peter committed
191
192
193
  /** Destructor */
  PeaksWorkspace::~PeaksWorkspace()
  {
194
//    ClearDeleteCalibrationData();
Peterson, Peter's avatar
Peterson, Peter committed
195
196
  }

197
  //---------------------------------------------------------------------------------------------
Peterson, Peter's avatar
Peterson, Peter committed
198

199
200
201
  //---------------------------------------------------------------------------------------------
  /** Add a column (used by constructor). */
  bool PeaksWorkspace::addColumn(const std::string& type, const std::string& name)
Peterson, Peter's avatar
Peterson, Peter committed
202
  {
Doucet, Mathieu's avatar
Doucet, Mathieu committed
203
    UNUSED_ARG(type)
204
205
    // Create the PeakColumn.
    columns.push_back( boost::shared_ptr<Mantid::DataObjects::PeakColumn>(new Mantid::DataObjects::PeakColumn( this->peaks, name) ) );
206
    // Cache the names
207
208
    columnNames.push_back(name);
    return true;
Peterson, Peter's avatar
Peterson, Peter committed
209
210
  }

211
212
  //---------------------------------------------------------------------------------------------
  /// @return the index of the column with the given name.
213
  int PeaksWorkspace::getColumnIndex(const std::string& name) const
Peterson, Peter's avatar
Peterson, Peter committed
214
  {
215
216
    for (int i=0; i < int(columns.size()); i++)
      if (columns[i]->name() == name)
217
218
        return i;
    throw std::invalid_argument("Column named " + name + " was not found in the PeaksWorkspace.");
Peterson, Peter's avatar
Peterson, Peter committed
219
220
  }

221
222
  //---------------------------------------------------------------------------------------------
  /// Gets the shared pointer to a column by index.
Doucet, Mathieu's avatar
Doucet, Mathieu committed
223
  boost::shared_ptr<Mantid::API::Column> PeaksWorkspace::getColumn(int index)
Peterson, Peter's avatar
Peterson, Peter committed
224
  {
225
    if (index >= static_cast<int>(columns.size())) throw std::invalid_argument("PeaksWorkspace::getColumn() called with invalid index.");
226
    return columns[index];
Peterson, Peter's avatar
Peterson, Peter committed
227
228
  }

229
230
231
232
233
234
235
  //---------------------------------------------------------------------------------------------
  /// Gets the shared pointer to a column by index.
  boost::shared_ptr<const Mantid::API::Column> PeaksWorkspace::getColumn(int index) const
  {
    if (index >= static_cast<int>(columns.size())) throw std::invalid_argument("PeaksWorkspace::getColumn() called with invalid index.");
    return columns[index];
  }
236

237
238
239
240
  void PeaksWorkspace::saveNexus(::NeXus::File * file ) const
  {
	  // This is not fully implemented. Currently a partial save.
	  int pNum = getNumberPeaks();
241
242
243
244
245
246
247
248
249
250
251
252
253
254

//	  //std::vector<Peak>& myPeaks = getPeaks();
//      std::vector<Peak> my1Peaks = peaks;
//	  // Peak myPeak = getPeak(0);
//	  Peak my1Peak = peaks[0];
//	  int cNum = columnCount();
//	  int rNum = rowCount();
//	  API::Column_const_sptr col = getColumn(0);
//	  std::vector<std::string> cNames = columnNames;
//  file->writeData("Column_names", columnNames ); // This is not resolved by linker
//  file->writeData("column names",cNames); // This is not resolved by linker.
//  file->writeData( "Column_1", col ); // This is not resolved by linker.

    std::vector<int> detectorID;
255
    std::vector<double> H;
256
257
	  for (size_t i=0; i < peaks.size(); i++)
	  {
258
	    Peak p = peaks[i];
259
260
      detectorID.push_back( p.getDetectorID() );  
      H.push_back( p.getH() );                    
261
262
      // etc.
	  }
263
    file->writeData("detector_id", detectorID); 
264
265
    file->writeData("H", H);
    // etc.
266
  }
Peterson, Peter's avatar
Peterson, Peter committed
267
268
}
}
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312

///\cond TEMPLATE

namespace Mantid
{
  namespace Kernel
  {

    template<> DLLExport
    Mantid::DataObjects::PeaksWorkspace_sptr IPropertyManager::getValue<Mantid::DataObjects::PeaksWorkspace_sptr>(const std::string &name) const
    {
      PropertyWithValue<Mantid::DataObjects::PeaksWorkspace_sptr>* prop =
        dynamic_cast<PropertyWithValue<Mantid::DataObjects::PeaksWorkspace_sptr>*>(getPointerToProperty(name));
      if (prop)
      {
        return *prop;
      }
      else
      {
        std::string message = "Attempt to assign property "+ name +" to incorrect type. Expected PeaksWorkspace.";
        throw std::runtime_error(message);
      }
    }

    template<> DLLExport
    Mantid::DataObjects::PeaksWorkspace_const_sptr IPropertyManager::getValue<Mantid::DataObjects::PeaksWorkspace_const_sptr>(const std::string &name) const
    {
      PropertyWithValue<Mantid::DataObjects::PeaksWorkspace_sptr>* prop =
        dynamic_cast<PropertyWithValue<Mantid::DataObjects::PeaksWorkspace_sptr>*>(getPointerToProperty(name));
      if (prop)
      {
        return prop->operator()();
      }
      else
      {
        std::string message = "Attempt to assign property "+ name +" to incorrect type. Expected const PeaksWorkspace.";
        throw std::runtime_error(message);
      }
    }

  } // namespace Kernel
} // namespace Mantid

///\endcond TEMPLATE