CGEM BOSS 6.6.5.h
BESIII Offline Software System
Loading...
Searching...
No Matches
ZddConverter.cxx
Go to the documentation of this file.
2#include "ZddEvent/ZddBoard.h"
4#include <cstring>
5#include <iomanip> //FIXME: for debugging
6
7ZddConverter* ZddConverter::s_instance = 0;
8
10{
11 if ( s_instance == 0 ) {
12 s_instance = new ZddConverter(runMode);
13 }
14
15 return s_instance;
16}
17
19{
20 if ( s_instance != 0 ) {
21 delete s_instance;
22 s_instance = 0;
23 }
24}
25
26bool ZddConverter::convert(uint32_t* pdata, int size, Event::ZddEvent* evt)
27{
28 // for debugging
29 //std::cout << "RAW buffer size: " << size << std::endl << std::hex;
30 //for ( int i = 0; i < size; ++i ) {
31 // std::cout << " 0x" << std::setw(8) << std::setfill('0') << pdata[i];
32 // if ( i%8 == 7 ) std::cout << std::endl;
33 //}
34 //std::cout << std::dec << std::endl;
35 ///////////////////////////////////////////
36
37 uint32_t* pend = pdata + size;
38
39 while ( pdata < pend ) {
40 pdata = decodeBoard(pdata, evt);
41 }
42
43 if ( pdata != pend ) {
44 std::cout << "ZddConverter: there are problems within the event data size" << std::endl;
45 exit(1);
46 }
47
48 return true;
49}
50
51uint32_t* ZddConverter::decodeBoard(uint32_t* pevt, Event::ZddEvent* evt)
52{
53 if ( (pevt[0] >> 28) != 10 ) {
54 std::cout << "ZddConverter get wrong event marker!" << std::endl;
55 exit(1);
56 }
57
58 int size = pevt[0] & 0xFFFFFFF;
59 int board = pevt[1] >> 27;
60 uint32_t chMask = pevt[1] & 0xFF;
61
62 ZddBoard* zddBoard = new ZddBoard(evt);
63 zddBoard->setBoardId(board);
64 zddBoard->setCounter(pevt[2]&0xFFFFFF);
65 zddBoard->setTimeTag(pevt[3]);
66
67 int ich = 0;
68
69 uint32_t* pend = pevt + size;
70 uint32_t* pchannel = pevt + 4;
71
72 while ( pchannel < pend ) {
73 while ( (chMask&(1<<ich)) == 0 ) {
74 ++ich;
75 if ( ich > 7 ) {
76 std::cout << "ZddConverter get wrong channel mask!" << std::endl;
77 exit(1);
78 }
79 }
80 ZddChannel* zddCh = new ZddChannel();
81 zddCh->setChId(board*100+ich);
82 zddBoard->addChannel(zddCh);
83 pchannel = decodeChannel(pchannel, zddCh);
84 ++ich;
85 }
86
87 if ( pchannel != pend ) {
88 std::cout << "ZddConverter: there are problems within the channel data size" << std::endl;
89 exit(1);
90 }
91
92 return pend;
93}
94
95uint32_t* ZddConverter::decodeChannel(uint32_t* pch, ZddChannel* zddCh)
96{
97 uint32_t* pend = pch + pch[0];
98 uint32_t* pfrag = pch + 1;
99
100 ZddFragment zddFrag;
101
102 int nCtrlWord = 0; //FIXME: check the good/skip transfrom, N < 14
103
104 int lstat = 0;
105 int index = 0;
106 while ( pfrag < pend ) {
107 uint32_t& ctrl_word = pfrag[0];
108 int size = (ctrl_word & 0x1FFFFF) * 4; //from words to bytes
109
110 if ( ctrl_word >> 31 ) { //fragment passed threshold
111 if ( (pfrag + size/4 + 1) > pend ) { //FIXME: error ZDD data
112 std::cout << "BAD ZDD RAW DATA!" << std::endl;
113 //exit(1);
114 break;
115 }
116 zddFrag.start_index = index;
117 zddFrag.length = size;
118 zddFrag.sample = new unsigned char[size];
119 memcpy(zddFrag.sample, pfrag+1, size);
120 zddCh->addFragments(zddFrag);
121 pfrag += size/4 + 1;
122 if ( lstat < 0 ) ++nCtrlWord;
123 lstat = 1;
124 }
125 else { //fragment skipped
126 pfrag += 1;
127 if ( lstat > 0 ) ++ nCtrlWord;
128 lstat = -1;
129 }
130
131 index += size;
132
133 if ( nCtrlWord == 14 ) { //FIXME: to be fixed
134 if ( pfrag < pend ) {
135 zddFrag.start_index = index;
136 zddFrag.length = (pend-pfrag)*4;
137 zddFrag.sample = new unsigned char[zddFrag.length];
138 memcpy(zddFrag.sample, pfrag, zddFrag.length);
139 zddCh->addFragments(zddFrag);
140 }
141 break;
142 }
143 }
144
145 return pend;
146}
147
148ZddConverter::ZddConverter(int runMode)
149 : m_runMode(runMode)
150{
151}
152
153ZddConverter::~ZddConverter()
154{
155}
void addChannel(ZddChannel *ch)
Definition ZddBoard.cxx:14
void setBoardId(int boardId)
Definition ZddBoard.h:29
void setCounter(int counter)
Definition ZddBoard.h:30
void setTimeTag(int timeTag)
Definition ZddBoard.h:31
void addFragments(const ZddFragment &frag)
Definition ZddChannel.h:36
void setChId(int chId)
Definition ZddChannel.h:32
static ZddConverter * instance(int runMode=2)
static void destroy()
bool convert(uint32_t *pdata, int size, Event::ZddEvent *evt)
int start_index
Definition ZddChannel.h:8
unsigned char * sample
Definition ZddChannel.h:10