12 #include <cdc/modules/cdcPacker/CDCPackerModule.h>
13 #include <cdc/modules/cdcPacker/CDCChannelData.h>
14 #include <cdc/dataobjects/CDCHit.h>
15 #include <cdc/dataobjects/CDCRawHit.h>
16 #include <cdc/dbobjects/CDCChannelMap.h>
18 #include <framework/logging/Logger.h>
19 #include <framework/utilities/FileSystem.h>
21 #include <framework/database/DBArray.h>
30 #define NUM_CDC_COPPER 75
43 setDescription(
"Generate RawCDC object from CDCHit");
44 setPropertyFlags(c_ParallelProcessingCertified);
46 addParam(
"rawCDCName", m_rawCDCName,
"Name of the RawCDC List name..",
string(
""));
47 addParam(
"cdcRawHitName", m_cdcRawHitName,
"Name of the CDCRawHit (Suppressed mode).",
string(
""));
48 addParam(
"cdcHitName", m_cdcHitName,
"Name of the CDCHit List name..",
string(
""));
49 addParam(
"fadcThreshold", m_fadcThreshold,
"Threshold voltage (mV).", 10);
50 addParam(
"xmlMapFileName", m_xmlMapFileName,
"path+name of the xml file",
51 string(
"/cdc/data/ch_map.dat"));
52 addParam(
"enableStoreRawCDC", m_enableStoreCDCRawHit,
"Enable to store to the RawCDC object",
true);
53 addParam(
"enablePrintOut", m_enablePrintOut,
"Enable to print out the data to the terminal",
true);
54 addParam(
"enableDatabase", m_enableDatabase,
"Enable database to read the channel map.",
true);
56 m_channelMapFromDB.addCallback(
this, &CDCPackerModule::loadMap);
59 CDCPackerModule::~CDCPackerModule()
63 void CDCPackerModule::initialize()
66 B2INFO(
"CDCPacker: initialize() Called.");
68 m_rawCDCs.registerInDataStore(m_rawCDCName);
71 storeCDCRawHits.registerInDataStore();
75 storeDigit.registerInDataStore();
79 B2INFO(
"CDCPacker: FADC threshold: " << m_fadcThreshold);
85 void CDCPackerModule::beginRun()
87 B2INFO(
"CDCPacker: beginRun() called.");
90 int CDCPackerModule::getFEEID(
int copper_id,
int slot_id)
99 return (copper_id * 4 + slot_id);
104 return (copper_id + slot_id * NUM_CDC_COPPER);
109 return ((copper_id / 15) * 60 + (copper_id % 15) + slot_id * 15);
114 void CDCPackerModule::event()
122 int tot_chdata_bytes[302];
123 memset(tot_chdata_bytes, 0,
sizeof(
int) * 302);
125 const int ch_data_bytes = 8;
127 std::vector<CDCChannelData> chData;
130 for (
const auto& hit : cdcHits) {
131 int eWire = (int)(hit.getID());
132 int sly = eWire / 4096;
133 int ily = (eWire % 4096) / 512;
134 int iwire = (eWire % 512);
135 short tdc = hit.getTDCCount();
136 short adc = hit.getADCCount();
137 unsigned short tot = hit.getTOT();
141 if (m_fee_board[ sly ][ ily ][ iwire] < 0 || m_fee_ch[ sly ][ ily ][ iwire] < 0) {
142 printf(
"Hit WireID %8d SL %3d IL %3d WI %4d BOARD %3d CH %3d\n",
143 (
int)(hit.getID()), sly, ily , iwire,
144 m_fee_board[ sly ][ ily ][ iwire], m_fee_ch[ sly ][ ily ][ iwire]);
148 if (hit.is2ndHit() ==
false) {
150 tot_chdata_bytes[ m_fee_board[ sly ][ ily ][ iwire] ] += ch_data_bytes;
151 CDCChannelData chd(m_fee_board[sly][ily][iwire], m_fee_ch[sly][ily][iwire], 8, tot, adc, tdc);
152 chData.push_back(chd);
155 const int boardId = m_fee_board[sly][ily][iwire];
156 const int channel = m_fee_ch[sly][ily][iwire];
157 auto fi = std::find_if(chData.begin(), chData.end(),
159 return (ch.getBoard() == boardId && ch.getChannel() == channel);
161 if (fi != chData.end()) {
162 tot_chdata_bytes[ m_fee_board[ sly ][ ily ][ iwire] ] += 2;
163 const size_t index = std::distance(chData.begin(), fi);
164 chData[index].setTDC2ndHit(tdc);
170 for (
const auto& c : chData) {
171 int board = c.getBoard();
172 int ch = c.getChannel();
173 bool flag = c.is2ndHit();
174 int len = c.getDataLength();
175 if (!((len == 8 && flag ==
false) ||
176 (len == 10 && flag ==
true))) {
177 B2ERROR(
"inconsistent data object board : " << board <<
" ch " << ch);
185 rawcprpacker_info.
eve_num = m_event;
186 rawcprpacker_info.
tt_ctime = 0x7123456;
187 rawcprpacker_info.
tt_utime = 0xF1234567;
190 const int packet_header_words = 3;
192 for (
int i = 0 ; i < NUM_CDC_COPPER; i++) {
194 rawcprpacker_info.
node_id = 0x02000000 + i + 1;
195 int nwords[4] = {0, 0, 0, 0};
196 int* buf[4] = {0, 0, 0, 0};
198 for (
int j = 0; j < 4; j++) {
199 int fee_id = getFEEID(i, j);
200 nwords[ j ] = ((tot_chdata_bytes[ fee_id ] + 3) / 4) + packet_header_words;
202 buf[ j ] =
new int[ nwords[ j ] ];
205 const char type = 0x20;
206 const char ver = 0x0;
207 const short trigTime = 0x0;
208 const short dataLength = nwords[j] * 4 - packet_header_words * 4;
209 const int trigNum = m_event;
211 *(buf[j] + 0) = (type << 24) | (ver << 16) | fee_id;
212 *(buf[j] + 1) = ((trigTime << 16) | dataLength);
213 *(buf[j] + 2) = trigNum;
215 short* sbuf = (
short*)(buf[j] + 3);
217 bool halfOffset =
false;
218 short reservedValue = 0xcccc;
219 for (
const auto& c : chData) {
220 const int board = c.getBoard();
221 const int ch = c.getChannel();
222 const int len = c.getDataLength();
223 if (board == fee_id) {
225 if (halfOffset ==
false) {
226 *sbuf++ = c.getTOT();
227 *sbuf++ = ((ch << 8) | len);
228 *sbuf++ = c.getTDCCount();
229 *sbuf++ = c.getADCCount();
231 reservedValue = c.getTDCCount2ndHit();
235 *sbuf++ = ((ch << 8) | len);
236 *sbuf++ = reservedValue;
237 *sbuf++ = c.getADCCount();
238 *sbuf++ = c.getTOT();
240 *sbuf++ = c.getTDCCount2ndHit();
241 *sbuf++ = c.getTDCCount();
244 reservedValue = c.getTDCCount();
249 if (halfOffset ==
true) {
251 *sbuf = reservedValue;
255 RawCDC* raw_cdc = m_rawCDCs.appendNew();
262 for (
int j = 0; j < 4; j++) {
263 if (buf[j] != NULL)
delete [] buf[j];
270 void CDCPackerModule::endRun()
272 B2INFO(
"CDCPacker : End run.");
275 void CDCPackerModule::terminate()
277 B2INFO(
"CDCPacker : Terminated.");
281 const WireID CDCPackerModule::getWireID(
int iBoard,
int iCh)
283 return m_map[iBoard][iCh];
286 void CDCPackerModule::loadMap()
292 for (
int i = 0 ; i < 9; ++i) {
293 for (
int j = 0 ; j < 8; ++j) {
294 for (
int k = 0 ; k < 384; ++k) {
295 m_fee_board[i][j][k] = -1;
296 m_fee_ch[i][j][k] = -1;
301 if (m_enableDatabase ==
false) {
304 std::string fileName = FileSystem::findFile(m_xmlMapFileName);
305 std::cout << fileName << std::endl;
306 if (fileName ==
"") {
307 B2ERROR(
"CDCPacker can't fine a filename: " << m_xmlMapFileName);
312 ifs.open(fileName.c_str());
321 ifs >> isl >> il >> iw >> iBoard >> iCh;
322 if (isl >= 9)
continue;
323 const WireID wireId(isl, il, iw);
324 m_map[iBoard][iCh] = wireId;
325 m_fee_board[isl][il][iw] = iBoard;
326 m_fee_ch[isl][il][iw] = iCh;
330 for (
const auto& cm : m_channelMapFromDB) {
332 if (isl >= 9)
continue;
333 const int il = cm.getILayer();
334 const int iw = cm.getIWire();
335 const int iBoard = cm.getBoardID();
336 const int iCh = cm.getBoardChannel();
337 const WireID wireId(isl, il, iw);
338 m_map[iBoard][iCh] = wireId;
339 m_fee_board[isl][il][iw] = iBoard;
340 m_fee_ch[isl][il][iw] = iCh;