-
Notifications
You must be signed in to change notification settings - Fork 122
/
ISISHistoDataListener.cpp
644 lines (572 loc) · 21.4 KB
/
ISISHistoDataListener.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
#include "MantidLiveData/ISISHistoDataListener.h"
#include "MantidAPI/LiveListenerFactory.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidAPI/AlgorithmFactory.h"
#include "MantidAPI/Algorithm.h"
#include "MantidAPI/SpectrumDetectorMapping.h"
#include "MantidAPI/WorkspaceGroup.h"
#include "MantidAPI/AnalysisDataService.h"
#include "MantidKernel/ConfigService.h"
#include "MantidKernel/Exception.h"
#include "MantidKernel/UnitFactory.h"
#include "MantidKernel/ArrayProperty.h"
#include "MantidKernel/ArrayBoundedValidator.h"
#include "MantidKernel/WarningSuppressions.h"
#include "MantidGeometry/Instrument.h"
#if (GCC_VERSION >= 40400 && GCC_VERSION < 40500) // 4.4.0 < GCC > 4.5.0
// Avoid compiler warnings on gcc 4.4 from unused static constants in isisds_command.h
GCC_DIAG_OFF(unused-variable)
#endif
#include "LoadDAE/idc.h"
#include <boost/lexical_cast.hpp>
#include <boost/make_shared.hpp>
#include <algorithm>
#include <numeric>
using namespace Mantid::API;
using namespace Mantid::Geometry;
using Mantid::Kernel::ConfigService;
namespace Mantid
{
namespace LiveData
{
DECLARE_LISTENER(ISISHistoDataListener)
namespace
{
/// static logger
Kernel::Logger g_log("ISISHistoDataListener");
}
/// Constructor
ISISHistoDataListener::ISISHistoDataListener() : ILiveListener(), isInitilized(false), m_daeHandle( NULL ), m_timeRegime(-1)
{
declareProperty(new Kernel::ArrayProperty<specid_t>("SpectraList"),
"An optional list of spectra to load. If blank, all available spectra will be loaded.");
auto validator = boost::make_shared<Kernel::ArrayBoundedValidator<int>>();
validator->setLower( 1 );
declareProperty(new Kernel::ArrayProperty<int>("Periods",validator),
"An optional list of periods to load. If blank, all available periods will be loaded.");
}
/// Destructor
ISISHistoDataListener::~ISISHistoDataListener()
{
if ( m_daeHandle )
{
IDCclose(&m_daeHandle);
}
}
/** Function called by IDC routines to report an error. Passes the error through to the logger
* @param status :: The status code of the error (disregarded)
* @param code :: The error code (disregarded)
* @param message :: The error message - passed to the logger at error level
*/
void ISISHistoDataListener::IDCReporter(int status, int code, const char* message)
{
(void) status; (void) code; // Avoid compiler warning
g_log.error(message);
}
/** Connect to the specified address and checks that is valid
* @param address The IP address and port to contact (port is ignored).
* @return True if the connection was successfully established
*/
bool ISISHistoDataListener::connect(const Poco::Net::SocketAddress& address)
{
m_daeName = address.toString();
// remove the port part
auto i = m_daeName.find(':');
if ( i != std::string::npos )
{
m_daeName.erase( i );
}
// set IDC reporter function for errors
IDCsetreportfunc(&ISISHistoDataListener::IDCReporter);
if (IDCopen(m_daeName.c_str(), 0, 0, &m_daeHandle,address.port()) != 0)
{
m_daeHandle = NULL;
return false;
}
m_numberOfPeriods = getInt("NPER");
g_log.debug() << "Number of periods " << m_numberOfPeriods << std::endl;
// Set the spectra list to load
std::vector<specid_t> spectra = getProperty("SpectraList");
if ( !spectra.empty() )
{
setSpectra( spectra );
}
// Set the period list to load
std::vector<int> periodList = getProperty("Periods");
if ( !periodList.empty() )
{
setPeriods( periodList );
}
loadSpectraMap();
loadTimeRegimes();
return true;
}
bool ISISHistoDataListener::isConnected()
{
if ( m_daeHandle == NULL ) return false;
// try to read a parameter, success means connected
int sv_dims_array[1] = { 1 }, sv_ndims = 1, buffer;
int stat = IDCgetpari(m_daeHandle, "NPER", &buffer, sv_dims_array, &sv_ndims);
return stat == 0;
}
ILiveListener::RunStatus ISISHistoDataListener::runStatus()
{
// In a run by default
return Running;
}
int ISISHistoDataListener::runNumber() const
{
// Not available it would seem - just return 0
return 0;
}
void ISISHistoDataListener::start(Kernel::DateAndTime /*startTime*/) // Ignore the start time
{
return;
}
/**
* Read the data from the DAE.
* @return :: A workspace with the data.
*/
boost::shared_ptr<Workspace> ISISHistoDataListener::extractData()
{
if ( m_timeRegime < 0 )
{
m_timeRegime = getTimeRegimeToLoad();
g_log.debug() << "Loading spectra for time regime " << m_timeRegime + 1 << std::endl;
}
if ( !m_daeHandle )
{
g_log.error("DAE is not connected");
throw Kernel::Exception::FileError("DAE is not connected ", m_daeName);
}
m_dataReset = false;
isInitilized = true;
// check that the dimensions haven't change since last time
int numberOfPeriods = getInt("NPER");
if ( numberOfPeriods != m_numberOfPeriods )
{
g_log.error("Data dimensions changed");
throw Kernel::Exception::FileError("Data dimensions changed", m_daeName);
}
loadTimeRegimes();
// buffer to read loat values in
std::vector<float> floatBuffer;
// read in the proton charge
getFloatArray( "RRPB", floatBuffer, 32);
const double protonCharge = floatBuffer[8];
// find out the number of histograms in the output workspace
const size_t numberOfHistograms = m_specList.empty() ? m_numberOfSpectra[m_timeRegime] : m_specList.size();
// Create the 2D workspace for the output
auto localWorkspace = WorkspaceFactory::Instance().create(
"Workspace2D", numberOfHistograms, m_numberOfBins[m_timeRegime] + 1, m_numberOfBins[m_timeRegime]
);
// Set the unit on the workspace to TOF
localWorkspace->getAxis(0)->unit() = Kernel::UnitFactory::Instance().create("TOF");
localWorkspace->setYUnit("Counts");
localWorkspace->updateSpectraUsing(SpectrumDetectorMapping(m_specIDs, m_detIDs));
// cut the spectra numbers into chunks
std::vector<int> index, count;
calculateIndicesForReading(index, count);
int firstPeriod = m_periodList.empty() ? 0: m_periodList.front() - 1;
// create a workspace group in case the data are multiperiod
auto workspaceGroup = API::WorkspaceGroup_sptr( new API::WorkspaceGroup );
// loop over periods and spectra and fill in the output workspace
for(int period = 0; period < m_numberOfPeriods; ++period)
{
if ( isPeriodIgnored( period ) ) continue;
if ( period > firstPeriod )
{
// create a new matrix workspace similar to the previous, copy over the instrument info
localWorkspace = WorkspaceFactory::Instance().create( localWorkspace );
workspaceGroup->addWorkspace( localWorkspace );
}
size_t workspaceIndex = 0;
for(size_t i = 0; i < index.size(); ++i)
{
getData(period, index[i], count[i], localWorkspace, workspaceIndex);
workspaceIndex += count[i];
}
if (period == firstPeriod)
{
// Only run the Child Algorithms once
runLoadInstrument( localWorkspace, getString("NAME") );
if ( m_numberOfPeriods > 1 )
{
// adding first ws to the group after loading instrument
// otherwise ws can be lost.
workspaceGroup->addWorkspace( localWorkspace );
}
// Set the total proton charge for this run
localWorkspace->mutableRun().setProtonCharge(protonCharge);
}
}
if ( m_numberOfPeriods > 1 && (m_periodList.empty() || m_periodList.size() > 1) )
{
return workspaceGroup;
}
return localWorkspace;
}
/**
* Read an integer parameter from the DAE
* @param par :: Parameter name
*/
int ISISHistoDataListener::getInt(const std::string& par) const
{
int sv_dims_array[1] = { 1 }, sv_ndims = 1, buffer;
int stat = IDCgetpari(m_daeHandle, par.c_str(), &buffer, sv_dims_array, &sv_ndims);
if ( stat != 0 )
{
g_log.error("Unable to read " + par + " from DAE " + m_daeName);
throw Kernel::Exception::FileError("Unable to read " + par + " from DAE " , m_daeName);
}
return buffer;
}
/**
* Read a string parameter from the DAE
* @param par :: Parameter name
*/
std::string ISISHistoDataListener::getString(const std::string& par) const
{
const int maxSize = 1024;
char buffer[maxSize];
int dim = maxSize, ndims = 1;
if (IDCgetparc(m_daeHandle, par.c_str(), (char*)buffer, &dim, &ndims) != 0)
{
g_log.error("Unable to read " + par + " from DAE " + m_daeName);
throw Kernel::Exception::FileError("Unable to read " + par + " from DAE " , m_daeName);
};
return std::string(buffer, dim);
}
/** Sets a list of spectra to be extracted. Default is reading all available spectra.
* @param specList :: A vector with spectra indices.
*/
void ISISHistoDataListener::setSpectra(const std::vector<specid_t>& specList)
{
// after listener has created its first workspace the spectra numbers cannot be changed
if ( !isInitilized )
{
m_specList = specList;
}
}
/** Sets a list of periods to be extracted. Default is reading all available periods.
* @param periodList :: A vector with period numbers.
*/
void ISISHistoDataListener::setPeriods(const std::vector<specid_t>& periodList)
{
// after listener has created its first workspace the period numbers cannot be changed
if ( !isInitilized )
{
m_periodList = periodList;
if ( *std::max_element( m_periodList.begin(), m_periodList.end() ) > m_numberOfPeriods )
{
throw std::invalid_argument( "Invalid period(s) specified. Maximum " + boost::lexical_cast<std::string>(m_numberOfPeriods) );
}
}
}
/**
* Read an array of floats from the DAE
* @param par :: Array name
* @param arr :: A vector to store the values
* @param dim :: Size of the array
*/
void ISISHistoDataListener::getFloatArray(const std::string& par, std::vector<float>& arr, const size_t dim)
{
int dims = static_cast<int>(dim), ndims = 1;
arr.resize( dim );
if (IDCgetparr(m_daeHandle, par.c_str(), arr.data(), &dims, &ndims) != 0)
{
g_log.error("Unable to read " + par + " from DAE " + m_daeName);
throw Kernel::Exception::FileError("Unable to read " + par + " from DAE " , m_daeName);
}
}
/**
* Read an array of ints from the DAE
* @param par :: Array name
* @param arr :: A vector to store the values
* @param dim :: Size of the array
*/
void ISISHistoDataListener::getIntArray(const std::string& par, std::vector<int>& arr, const size_t dim)
{
int dims = static_cast<int>(dim), ndims = 1;
arr.resize( dim );
if (IDCgetpari(m_daeHandle, par.c_str(), arr.data(), &dims, &ndims) != 0)
{
g_log.error("Unable to read " + par + " from DAE " + m_daeName);
throw Kernel::Exception::FileError("Unable to read " + par + " from DAE " , m_daeName);
}
}
/**
* Split up all spectra into chunks
* @param index :: Vector of first indices of a chunk.
* @param count :: Numbers of spectra in each chunk.
*/
void ISISHistoDataListener::calculateIndicesForReading(std::vector<int>& index, std::vector<int>& count)
{
const int numberOfBins = m_numberOfBins[m_timeRegime];
const int numberOfSpectra = m_numberOfSpectra[m_timeRegime];
// max number of spectra that could be read in in one go
int maxNumberOfSpectra = 1024 * 1024 / ( numberOfBins * (int)sizeof(int) );
if ( maxNumberOfSpectra == 0 )
{
maxNumberOfSpectra = 1;
}
if ( m_specList.empty() )
{
// make sure the chunk sizes < maxNumberOfSpectra
int spec = 1;
int n = numberOfSpectra;
while( n > 0 )
{
if ( n < maxNumberOfSpectra )
{
index.push_back( spec );
count.push_back( n );
break;
}
else
{
index.push_back( spec );
count.push_back( maxNumberOfSpectra );
n -= maxNumberOfSpectra;
spec += maxNumberOfSpectra;
}
}
}
else
{
// combine consecutive spectra but don't exceed the maxNumberOfSpectra
size_t i0 = 0;
specid_t spec = m_specList[i0];
for(size_t i = 1; i < m_specList.size(); ++i)
{
specid_t next = m_specList[i];
if ( next - m_specList[i-1] > 1 || static_cast<int>(i - i0) >= maxNumberOfSpectra )
{
index.push_back( spec );
count.push_back( static_cast<int>( i - i0 ) );
i0 = i;
spec = next;
}
}
index.push_back( spec );
count.push_back( static_cast<int>( m_specList.size() - i0 ) );
}
}
/**
* Read spectra from the DAE
* @param period :: Current period index
* @param index :: First spectrum index
* @param count :: Number of spectra to read
* @param workspace :: Workspace to store the data
* @param workspaceIndex :: index in workspace to store data
*/
void ISISHistoDataListener::getData(int period, int index, int count, API::MatrixWorkspace_sptr workspace, size_t workspaceIndex)
{
const int numberOfBins = m_numberOfBins[m_timeRegime];
const size_t bufferSize = count * (numberOfBins + 1) * sizeof(int);
std::vector<int> dataBuffer( bufferSize );
// Read in spectra from DAE
int ndims = 2, dims[2];
dims[0] = count;
dims[1] = numberOfBins + 1;
int spectrumIndex = index + period * (m_numberOfSpectra[m_timeRegime] + 1);
if (IDCgetdat(m_daeHandle, spectrumIndex, count, dataBuffer.data(), dims, &ndims) != 0)
{
g_log.error("Unable to read DATA from DAE " + m_daeName);
throw Kernel::Exception::FileError("Unable to read DATA from DAE " , m_daeName);
}
for(size_t i = 0; i < static_cast<size_t>(count); ++i)
{
size_t wi = workspaceIndex + i;
workspace->setX(wi, m_bins[m_timeRegime]);
MantidVec& y = workspace->dataY( wi );
MantidVec& e = workspace->dataE( wi );
workspace->getSpectrum(wi)->setSpectrumNo(index + static_cast<specid_t>(i));
size_t shift = i * (numberOfBins + 1) + 1;
y.assign( dataBuffer.begin() + shift, dataBuffer.begin() + shift + y.size() );
std::transform( y.begin(), y.end(), e.begin(), dblSqrt );
}
}
/** Populate spectra-detector map
*
*/
void ISISHistoDataListener::loadSpectraMap()
{
// Read in the number of detectors
int ndet = getInt( "NDET" );
getIntArray( "UDET", m_detIDs, ndet);
getIntArray( "SPEC", m_specIDs, ndet);
}
/** Run the Child Algorithm LoadInstrument (or LoadInstrumentFromRaw).
* @param localWorkspace :: The workspace
* @param iName :: The instrument name
*/
void ISISHistoDataListener::runLoadInstrument(MatrixWorkspace_sptr localWorkspace, const std::string& iName)
{
auto loadInst = API::AlgorithmFactory::Instance().create("LoadInstrument",-1);
if ( !loadInst ) return;
loadInst->initialize();
try
{
loadInst->setPropertyValue("InstrumentName", iName);
loadInst->setProperty<MatrixWorkspace_sptr>("Workspace",localWorkspace);
loadInst->setProperty("RewriteSpectraMap", false);
loadInst->executeAsChildAlg();
}
catch(std::invalid_argument &)
{
g_log.information("Invalid argument to LoadInstrument Child Algorithm");
}
catch (std::runtime_error&)
{
g_log.information("Unable to successfully run LoadInstrument Child Algorithm");
}
if (API::AnalysisDataService::Instance().doesExist("Anonymous"))
{
// LoadInstrument adds the workspace to ADS as Anonymous
// we don't want it there
API::AnalysisDataService::Instance().remove("Anonymous");
}
}
/**
* Determine the number of time regimes.
* Load time regime for each spectrum, bin boundaries and number of spectra for each regime.
*/
void ISISHistoDataListener::loadTimeRegimes()
{
// Expecting loadSpectraMap() to be called first.
if ( m_specIDs.empty() || m_detIDs.empty() )
{
throw std::logic_error("Spectra-detector mapping must be loaded first.");
}
// prefix for DAE command to get the number of time channels (bins)
const std::string ntcPrefix = "NTC";
// prefix for DAE command to get the time channel boundaries
const std::string rtcbPrefix = "RTCB";
// prefix for DAE command to get the number of spectra
const std::string nspPrefix = "NSP";
// At the moment we cannot get the number of time regimes from the dae.
// It will be possible in the future when Freddie adds a parameter for it.
// For now we assume that two regimes are possible. The first must always be present.
// If there is nonzero number of time channels for the second one then we have two regimes.
for(size_t tr = 0; tr < 2; ++tr)
{
const std::string regime = boost::lexical_cast<std::string>( tr + 1 );
// get number of bins in this regime
int nbins = getInt( ntcPrefix + regime );
if ( nbins == 0 )
{
if ( tr == 0 )
{
throw std::runtime_error("Didn't find any time bins for time regime 1.");
}
break;
}
// get number of spectra in this time regime
int nspec = getInt( nspPrefix + regime );
// if it's first call of this method populate the member variables
if ( m_bins.size() == tr )
{
m_numberOfBins.push_back( nbins );
m_numberOfSpectra.push_back( nspec );
// buffer to read float values in
std::vector<float> floatBuffer;
if ( tr == 0 )
{
// read in the bin boundaries
getFloatArray( rtcbPrefix + regime, floatBuffer, nbins + 1);
}
else
{
// In principle bin boundaries for all regimes should be loaded
// the same way as for tr == 0 but because of a bug in the dae software
// it only works for regime 1. What follows is a workaround.
// number of monitors
int nmon = getInt( "NMON" );
// indices of monitors in m_detIDs and m_specIDs ( +1 )
std::vector<int> monitorIndices;
getIntArray("MDET",monitorIndices,nmon);
// we make an assumtion that regime 2 is used for monitors only
if ( monitorIndices.empty() )
{
throw std::runtime_error("Time regime 2 is expected to be used for monitors but none are found.");
}
m_monitorSpectra.resize( nmon );
for(size_t i = 0; i < m_monitorSpectra.size(); ++i)
{
m_monitorSpectra[i] = m_specIDs[monitorIndices[i] - 1];
}
for(auto mon = m_monitorSpectra.begin(); mon != m_monitorSpectra.end(); ++mon)
{
g_log.debug() << "Monitor spectrum " << *mon << std::endl;
}
const std::string detRTCB = rtcbPrefix + "_" + boost::lexical_cast<std::string>( m_monitorSpectra.front() );
// read in the bin boundaries
getFloatArray( detRTCB, floatBuffer, nbins + 1);
}
// copy them into a MantidVec
m_bins.push_back(boost::make_shared<MantidVec>(floatBuffer.begin(), floatBuffer.end()));
}
else
{
// check that dimensions haven't changed
if ( nspec != m_numberOfSpectra[tr] || nbins != m_numberOfBins[tr])
{
g_log.error("Data dimensions changed");
throw Kernel::Exception::FileError("Data dimensions changed", m_daeName);
}
}
}
g_log.debug() << "Number of time regimes " << m_bins.size() << std::endl;
for(size_t i = 0; i < m_bins.size(); ++i)
{
g_log.debug() << "Number of bins in time regime " << i + 1 << " is " << m_bins[i]->size()-1 << std::endl;
}
}
/**
* Get the time regime for which the data should be loaded.
* If spectrum list isn't specified (all data) return regime 1.
* If spectrum list is given return the common regime for all
* spectra in the list. If regimes are mixed throw invalid_argument.
* @return :: 0 (regime 1) or 1 (regime 2).
*/
int ISISHistoDataListener::getTimeRegimeToLoad() const
{
if ( ! m_specList.empty() )
{
if ( m_monitorSpectra.empty() ) return 0;
int regime = -1;
for( auto specIt = m_specList.begin(); specIt != m_specList.end(); ++specIt )
{
bool isMonitor = std::find(m_monitorSpectra.begin(),m_monitorSpectra.end(), *specIt) != m_monitorSpectra.end();
int specRegime = isMonitor? 1 : 0;
if ( regime < 0 )
{
regime = specRegime;
}
else if ( specRegime != regime )
{
throw std::invalid_argument("Cannot mix spectra in different time regimes.");
}
}
return regime;
}
return 0;
}
/// Personal wrapper for sqrt to allow msvs to compile
double ISISHistoDataListener::dblSqrt(double in)
{
return sqrt( in );
}
/**
* Check if a data period should be ignored.
* @param period :: Period to check.
* @return :: True to ignore the period.
*/
bool ISISHistoDataListener::isPeriodIgnored(int period) const
{
if ( m_periodList.empty() ) return false;
return std::find( m_periodList.begin(), m_periodList.end(), period + 1 ) == m_periodList.end();
}
} // namespace LiveData
} // namespace Mantid