/home/bes3soft/bes3soft/Boss/7.0.2/dist/7.0.2/Event/RawDataCnv/RawDataCnv-00-04-35/src/Util/ZddConverter.cxx

Go to the documentation of this file.
00001 #include "RawDataCnv/Util/ZddConverter.h"
00002 #include "ZddEvent/ZddBoard.h"
00003 #include "ZddEvent/ZddChannel.h"
00004 #include <cstring>
00005 #include <iomanip>  //FIXME: for debugging
00006 
00007 ZddConverter* ZddConverter::s_instance = 0;
00008 
00009 ZddConverter* ZddConverter::instance(int runMode)
00010 {
00011     if ( s_instance == 0 ) {
00012         s_instance = new ZddConverter(runMode);
00013     }
00014 
00015     return s_instance;
00016 }
00017 
00018 void ZddConverter::destroy()
00019 {
00020     if ( s_instance != 0 ) {
00021         delete s_instance;
00022         s_instance = 0;
00023     }
00024 }
00025 
00026 bool ZddConverter::convert(uint32_t* pdata, int size, Event::ZddEvent* evt)
00027 {
00028     // for debugging
00029     //std::cout << "RAW buffer size: " << size << std::endl << std::hex;
00030     //for ( int i = 0; i < size; ++i ) {
00031     //    if ( i%8 == 0 ) std::cout << "0x" << std::setw(8) << std::setfill('0') << i << ":";
00032     //    std::cout << " 0x" << std::setw(8) << std::setfill('0') << pdata[i];
00033     //    if ( i%8 == 7 ) std::cout << std::endl;
00034     //}
00035     //std::cout << std::dec << std::endl;
00037 
00038     uint32_t* pend = pdata + size;
00039 
00040     while ( pdata < pend ) {
00041         pdata = decodeBoard(pdata, evt);
00042     }
00043 
00044     if ( pdata != pend ) {
00045         std::cout << "ZddConverter: there are problems within the event data size" << std::endl;
00046         exit(1);
00047     }
00048 
00049     return true;
00050 }
00051 
00052 uint32_t* ZddConverter::decodeBoard(uint32_t* pevt, Event::ZddEvent* evt)
00053 {
00054     if ( (pevt[0] >> 28) != 10 ) {
00055         std::cout << "ZddConverter get wrong event marker!" << std::endl;
00056         exit(1);
00057     }
00058 
00059     int size = pevt[0] & 0xFFFFFFF;
00060     int board = pevt[1] >> 27;
00061     uint32_t chMask = pevt[1] & 0xFF;
00062 
00063     ZddBoard* zddBoard = new ZddBoard(evt);
00064     zddBoard->setBoardId(board);
00065     zddBoard->setCounter(pevt[2]&0xFFFFFF);
00066     zddBoard->setTimeTag(pevt[3]);
00067 
00068     int  ich = 0;
00069 
00070     uint32_t* pend = pevt + size;
00071     uint32_t* pchannel = pevt + 4;
00072 
00073     while ( pchannel < pend ) {
00074         while ( (chMask&(1<<ich)) == 0 ) {
00075             ++ich;
00076             if ( ich > 7 ) {
00077                 std::cout << "ZddConverter get wrong channel mask!" << std::endl;
00078                 exit(1);
00079             }
00080         }
00081         ZddChannel* zddCh = new ZddChannel();
00082         zddCh->setChId(board*100+ich);
00083         zddBoard->addChannel(zddCh);
00084         pchannel = decodeChannel(pchannel, zddCh);
00085         ++ich;
00086     }
00087 
00088     if ( pchannel != pend ) {
00089         std::cout << "ZddConverter: there are problems within the channel data size" << std::endl;
00090         exit(1);
00091     }
00092 
00093     return pend;
00094 }
00095 
00096 uint32_t* ZddConverter::decodeChannel(uint32_t* pch, ZddChannel* zddCh)
00097 {
00098     uint32_t* pend = pch + pch[0];
00099     uint32_t* pfrag = pch + 1;
00100 
00101     ZddFragment zddFrag;
00102 
00103     int nCtrlWord = 0;  //FIXME: check the good/skip transfrom, N < 14
00104 
00105     int lstat = 0;
00106     int index = 0;
00107     int samples = 800; // FIXME: writing 800 here is a bad idea
00108 
00109     while ( pfrag < pend ) {
00110         uint32_t& ctrl_word = pfrag[0];
00111         int  size = (ctrl_word & 0x1FFFFF) * 4;  //from words to bytes
00112 
00113         if ( ctrl_word >> 31 ) {  //fragment passed threshold
00114 
00115             if ( ctrl_word == 0xFEFEFEFE ) break; // CAEN FW bug discovered June 2014, not much serious:
00116                                                   // not really a control word, end of channel instead
00117 
00118             // previous treatment by Jaheng
00119             if ( (pfrag + size/4 + 1) > pend ) {  //FIXME: error ZDD data
00120                 std::cout << "BAD ZDD RAW DATA!" << std::endl;
00121                 //exit(1);
00122                 break;
00123             }
00124             zddFrag.start_index = index;
00125             zddFrag.length = size;
00126             zddFrag.sample = new unsigned char[size];
00127             memcpy(zddFrag.sample, pfrag+1, size);
00128             zddCh->addFragments(zddFrag);
00129             pfrag += size/4 + 1;
00130             if ( lstat < 0 ) ++nCtrlWord;
00131             lstat = 1;
00132         }
00133         else {  //fragment skipped
00134             pfrag += 1;
00135             if ( lstat > 0 ) ++ nCtrlWord;
00136             lstat = -1;
00137         }
00138 
00139         index += size;
00140 /* Following check UNNEEDED
00141         if ( nCtrlWord == 14 ) {  //FIXME: to be fixed
00142             if ( pfrag < pend ) {
00143                 zddFrag.start_index = index;
00144                 zddFrag.length = (pend-pfrag)*4;
00145                 zddFrag.sample = new unsigned char[zddFrag.length];
00146                 memcpy(zddFrag.sample, pfrag, zddFrag.length);
00147                 zddCh->addFragments(zddFrag);
00148             }
00149             break;
00150         }
00151 */
00152     }
00153 
00154     if ( index < samples ) {
00155         zddCh->setScanCode(-1); // mark channel as "Incomplete scan"
00156 //        std::cout << "ZDD MISS channelId=" << zddCh->getChId()
00157 //                  << " missing samples:" << samples-index << " marked INCOMPLETE SCAN" << std::endl;
00158     }
00159 
00160     return pend;
00161 }
00162 
00163 ZddConverter::ZddConverter(int runMode)
00164     : m_runMode(runMode)
00165 {
00166 }
00167 
00168 ZddConverter::~ZddConverter()
00169 {
00170 }

Generated on Tue Nov 29 22:58:32 2016 for BOSS_7.0.2 by  doxygen 1.4.7