// // Channels.cpp // #include "Channels.h" // ========================================================= // Constructor / Destructor // ========================================================= Channels::Channels () { _max_chan = 0; _chanList = 0; _words = 0; _chan_mask = 0; } Channels::~Channels () { if ( _chanList != 0 ) delete []_chanList; if ( _chan_mask != 0 ) delete []_chan_mask; } // ================================================ // set number of channels (readout channels // ================================================ void Channels::setnChan (int nChan) { // --------------------------------------- // memory for used channel list // --------------------------------------- _max_chan = nChan; if ( _chanList != 0 ) delete []_chanList; else _chanList = new int[nChan]; // ------------------------------------------------------ // words to store bit pattern for channels to use // ------------------------------------------------------ _words = 0; int sizeInt = (int) (sizeof (int) * 8); _words = nChan / sizeInt; if ( _words * sizeInt < nChan ) _words += 1; if ( _chan_mask != 0 ) delete []_chan_mask; else _chan_mask = new int[_words]; if ( _chan_mask == 0 || _chanList == 0) { _max_chan = 0; _nr_of_chan = 0; _words = 0; return; } else { Channels::resetChanList (); } } // ======================================================== // set Channels (start, stop, stepSize) // ======================================================== void Channels::removeChan (int start, int stop, int stepSize) { int chan = start; int tmp; while ( chan < _max_chan && chan <= stop ) { int bit = chan % ( sizeof (int) * 8); int word = chan / ( sizeof (int) * 8 ); tmp = 0x1 << bit; tmp = ~tmp; _chan_mask[word] &= tmp; chan += stepSize; } } // ======================================================== // set Channels (start, stop, stepSize) // ======================================================== void Channels::addChan (int start, int stop, int stepSize) { int chan = start; while ( chan < _max_chan && chan <= stop ) { int bit = chan % ( sizeof (int) * 8); int word = chan / ( sizeof (int) * 8 ); _chan_mask[word] |= ( 0x1 << bit ); chan += stepSize; } } // ======================================================== // set Channels, individual channel // ======================================================== void Channels::addChan (int chanNumber) { if ( chanNumber >= 0 || chanNumber < _max_chan ) { int bit = chanNumber % ( sizeof (int) * 8); int word = chanNumber / ( sizeof (int) * 8 ); _chan_mask[word] |= ( 0x1 << bit ); } } // ======================================================== // make List // ======================================================== void Channels::makeChanList () { _nr_of_chan = 0; for ( int i = 0; i < _max_chan; i++ ) { int bit = i % ( sizeof (int) * 8); int word = i / ( sizeof (int) * 8 ); if ( ( (_chan_mask[word] >> bit) & 0x1 ) != 0) { _chanList [_nr_of_chan] = i; ++_nr_of_chan; } } } // ======================================================== // reset // ======================================================== void Channels::resetChanList () { _nr_of_chan = 0; int i; for ( i = 0; i < _max_chan; i++ ) _chanList [i] = 0; for ( i = 0; i < _words; i++ ) _chan_mask[i] = 0; } // ============================================== // channel Status // ============================================== int Channels::chanStatus (int chanNumber) { if ( chanNumber >= 0 || chanNumber < _max_chan ) { int bit = chanNumber % ( sizeof (int) * 8); int word = chanNumber / ( sizeof (int) * 8 ); if ( ( (_chan_mask[word] >> bit) & 0x1 ) != 0) return CHAN_USED; else return CHAN_NOT_USED; } return 0; } // ==================================================== // return number of channels to use // ==================================================== int Channels::nChanList () { return ( _nr_of_chan ); } // ================================================= // get channel numbers from channel list // ================================================= int Channels::chanList (int index) { if ( index >= 0 && index < _nr_of_chan ) return ( _chanList[index] ); else return ( -1 ); }