// // ScanData.cpp // #include "ScanData.h" #include "LogFile.h" // =================================================== // constructors // =================================================== // ----------------------------- // start, stop, step size // ----------------------------- ScanData::ScanData (double start, double stop, double stepSize, int nChan, Channels& chan) : _chanList ( &chan ) { double tmp_step, tmp_start, tmp_stop, value; // --------------------- // step size // --------------------- if ( stepSize < 0.0 ) tmp_step = - stepSize; else tmp_step = stepSize; if ( start > stop ) { tmp_start = stop; tmp_stop = start; } else { tmp_start = start; tmp_stop = stop; } // --------------------------------- // count number of points // --------------------------------- _nPoints = 0; if ( start == stop ) { _nPoints = 1; } else { _nPoints = 0; tmp_stop -= tmp_step / 10.0; value = start; int done = 0; while ( !done ) { if ( value > tmp_stop ) { done = 1; _nPoints++; } else { _nPoints++; } value += tmp_step; } } logFile << " number of points " << _nPoints << endl; // ------------------------------------------- // allocate memory for points and events // create point values. // ------------------------------------------- _points = new double[_nPoints]; _events = new double[_nPoints]; if ( _points == 0 || _events == 0 ) { _nPoints = 0; } else { value = start; for ( int i = 0; i < _nPoints; i++ ) { _points[i] = value; if ( start <= stop ) value += tmp_step; else value -= tmp_step; } _points [_nPoints - 1] = stop; } // ---------------------------------------------------- // allocate memory for the accumulated data // and projection of data for a single channel // ---------------------------------------------------- _nChannels = nChan; int size = _nPoints * _nChannels; _data = new unsigned int[size]; _scanProject = new unsigned int[_nPoints]; if ( _data == 0 || _scanProject == 0 ) { _nPoints = 0; } else { for ( int i = 0; i < size; i++ ) _data[i] = 0; } logFile << " end sacn data (size/points) " << size << " " << _nPoints << endl; } // ========================= // destructor // ========================= ScanData::~ScanData () { delete []_points; delete []_events; delete []_data; delete []_scanProject; } // ====================================== // resetPoint, nextPoint // ====================================== void ScanData::resetPoint () { _currPoint = 0; } int ScanData::nextPoint () { if ( _currPoint >= 0 && _currPoint < (_nPoints - 1) ) { ++_currPoint; return 1; } else { _currPoint = -1; return 0; } } // ============================== // nPoints // ============================== int ScanData::nPoints () { return _nPoints; } // ============================== // getEvents // ============================== double ScanData::getEvents () { if ( _currPoint >= 0 ) { return _events[_currPoint]; } else return 0.0; } // ============================== // getPoint // ============================== double ScanData::getPoint () { if ( _currPoint >= 0 ) { return _points[_currPoint]; } else return 0.0; } // ============================== // setPoint // ============================== void ScanData::setPoint (double point) { if ( _currPoint >= 0 ) { _points[_currPoint] = point; } } void ScanData::setPoint (double point, double events) { if ( _currPoint >= 0 ) { _points[_currPoint] = point; _events[_currPoint] = events; } } // ================================================= // printValues // ================================================= void ScanData::printScanPara () { for ( int i = 0; i < _nPoints; i++ ) logFile << "point " << i << " value " << _points[i] << endl; } // ========================================================= // data // ========================================================= unsigned int* ScanData::dataProjection () { if ( _currPoint != -1 ) return ( _data + ( _nChannels * _currPoint ) ) ; else return 0; } // ================================================================= // scanProjection // ================================================================= int ScanData::scanProjection (double *chanValues, int chan) { if ( chan < 0 || chan >= _nChannels ) return 0; if ( _chanList->chanStatus (chan) == CHAN_NOT_USED ) return CHAN_NOT_USED; for ( int i = 0; i < _nPoints; i++ ) { chanValues[i] = (double) _data[_nChannels * i + chan]; } return CHAN_USED; } // ================================================ // write scan data to file // ================================================ void ScanData::dataOut (ofstream& outFile) { int i, index; // -------------------------- // print points // -------------------------- outFile << "points"; for ( i = 0; i < _nPoints; i++ ) outFile << " " << _points[i]; outFile << endl; // -------------------------- // print events // -------------------------- outFile << "events"; for ( i = 0; i < _nPoints; i++ ) outFile << " " << _events[i]; outFile << endl; // ------------------------------------ // print data for every channel // ------------------------------------ for ( i = 0; i < _nChannels; i++ ) { if ( _chanList->chanStatus (i) == CHAN_USED ) { outFile << i; for ( int j = 0; j < _nPoints; j++ ) { index = _nChannels * j + i; outFile << " " << _data[index]; } outFile << endl; } } outFile.close (); } // ================================================ // write scan data to file // ================================================ void ScanData::dataOutGnu (char *fileName) { ofstream outFile (fileName, ios::out); int i, j, index; // double data; // -------------------------- // print points // -------------------------- for ( i = 0; i < _nPoints; i++ ) { outFile << _points[i]; outFile << " " << _events[i]; for ( j = 0; j < _nChannels; j++ ) { index = _nChannels * i + j; outFile << " " << _data[index]; } outFile << endl; } outFile.close (); }