9 #include <cdc/modules/cdcUnpacker/CDCUnpackerModule.h>
11 #include <cdc/dbobjects/CDCChannelMap.h>
13 #include <framework/datastore/DataStore.h>
14 #include <framework/logging/Logger.h>
15 #include <framework/utilities/FileSystem.h>
17 #include <framework/database/DBArray.h>
34 CDCUnpackerModule::CDCUnpackerModule() :
Module()
69 if ((*m_channelMapFromDB).isValid()) {
72 B2FATAL(
"Channel map is not valid");
76 B2INFO(
"CDCUnpacker: initialize() Called.");
92 B2INFO(
"CDCUnpacker: beginRun() called.");
103 B2INFO(
"CDCUnpacker: event() started.");
121 const int nEntries =
m_rawCDCs.getEntries();
123 B2DEBUG(99,
"nEntries of RawCDCs : " << nEntries);
125 for (
int i = 0; i < nEntries; ++i) {
126 const int subDetectorId =
m_rawCDCs[i]->GetNodeID(0);
127 const int iNode = (subDetectorId & 0xFFFFFF);
128 const int nEntriesRawCDC =
m_rawCDCs[i]->GetNumEntries();
130 B2DEBUG(99,
LogVar(
"nEntries of rawCDC[i]", nEntriesRawCDC));
132 for (
int j = 0; j < nEntriesRawCDC; ++j) {
133 int trigType =
m_rawCDCs[i]->GetTRGType(j);
136 int MaxNumOfCh =
m_rawCDCs[i]->GetMaxNumOfCh(j);
138 if (MaxNumOfCh == 4) readoutName =
"COPPER";
139 else if (MaxNumOfCh == 48) readoutName =
"PCIe40";
141 B2FATAL(
"CDC UnpackerModule: Invalid value of GetMaxNumOfCh from raw data: " <<
LogVar(
"Number of ch: ",
144 for (
int k = 0; k < MaxNumOfCh; ++k) {
145 nWords[k] =
m_rawCDCs[i]->GetDetectorNwords(j, k);
146 if (MaxNumOfCh == 48 &&
m_rawCDCs[i]->CheckOnlineRemovedDataBit(j, k) ==
true) {
148 B2FATAL(
"The data is not removed for the bad channel (" << j <<
"," << k <<
") with error flag in ff55 trailer! ");
150 data32tab[k] = (
int*)
m_rawCDCs[i]->GetDetectorBuffer(j, k);
157 for (
int iFiness = 0; iFiness < MaxNumOfCh; ++iFiness) {
158 int* ibuf = data32tab[iFiness];
159 const int nWord = nWords[iFiness];
160 B2DEBUG(99,
LogVar(
"nWords (from " + readoutName +
" header)", nWord));
163 B2INFO(
"CDCUnpacker : Print out CDC data block.");
167 const int c_headearWords = 3;
168 if (nWord < c_headearWords) {
170 B2WARNING(
"CDCUnpacker : No CDC block header.");
176 B2INFO(
"CDCUnpacker : RawDataBlock(CDC) : Block # "
179 <<
LogVar(
"Finness", iFiness));
186 B2WARNING(
"Invalid board " << std::hex <<
m_boardId << std::dec <<
" readout buffer block: " << i <<
" block channel: " << iFiness);
190 B2WARNING(
"Unrecoverable board " << std::hex <<
m_boardId);
200 const int swDataLength = dataLength * 2;
203 if (dataLength != (nWord - c_headearWords)) {
205 B2ERROR(
"Inconsistent data size between " + readoutName +
" and CDC FEE."
206 <<
LogVar(
"data length", dataLength) <<
LogVar(
"nWord", nWord)
207 <<
LogVar(
"Node ID", iNode) <<
LogVar(
"Finness ID", iFiness));
210 B2WARNING(
"Inconsistent data size between " + readoutName +
" and CDC FEE."
211 <<
LogVar(
"data length", dataLength) <<
LogVar(
"nWord", nWord)
212 <<
LogVar(
"Node ID", iNode) <<
LogVar(
"Finness ID", iFiness));
217 B2INFO(
"CDCUnpacker : " <<
LogVar(
"Data size", dataLength));
225 B2INFO(
"CDCUnpacker : " <<
LogVar(
"Board", board) <<
LogVar(
"Trigger number", trgNumber)
226 <<
LogVar(
"Trigger time ", trgTime));
235 B2INFO(
"CDCUnpacker : Raw data mode.");
240 for (
int it = 0; it < dataLength; ++it) {
241 int index = it + c_headearWords;
243 m_buffer.push_back(
static_cast<unsigned short>((ibuf[index] & 0xffff0000) >> 16));
244 m_buffer.push_back(
static_cast<unsigned short>(ibuf[index] & 0xffff));
247 const int fadcTdcChannels = 48;
248 const int nSamples = swDataLength / (2 * fadcTdcChannels);
250 std::vector<unsigned short> fadcs;
251 std::vector<unsigned short> tdcs;
253 for (
int iCh = 0; iCh < fadcTdcChannels; ++iCh) {
254 const int offset = fadcTdcChannels;
255 unsigned short fadcSum = 0;
256 unsigned short tdc1 = 0x7fff;
257 unsigned short tdc2 = 0x7fff;
259 for (
int iSample = 0; iSample < nSamples; ++iSample) {
262 unsigned short fadc =
m_buffer.at(iCh + 2 * fadcTdcChannels * iSample);
269 unsigned short tdc =
m_buffer.at(iCh + 2 * fadcTdcChannels * iSample + offset) & 0x7fff;
270 unsigned short tdcIsValid = (
m_buffer.at(iCh + 2 * fadcTdcChannels * iSample + offset) & 0x8000) >> 15;
271 if (tdcIsValid == 1) {
280 fadcs.push_back(fadc);
284 const unsigned short status = 0;
285 m_CDCRawHitWaveForms.appendNew(status, trgNumber, iNode, iFiness, board, iCh, iSample, trgTime, fadc, tdc);
290 if (tdc1 != 0x7fff) {
294 if (trgTime < tdc1) {
295 tdc1 = (trgTime | 0x8000) - tdc1;
297 tdc1 = trgTime - tdc1;
315 printf(
"FADC ch %2d : ", iCh);
316 for (
int iSample = 0; iSample < nSamples; ++iSample) {
317 printf(
"%4x ", fadcs.at(iSample));
321 printf(
"TDC ch %2d : ", iCh);
322 for (
int iSample = 0; iSample < nSamples; ++iSample) {
323 printf(
"%4x ", tdcs.at(iSample));
330 }
else if (dataType == 2) {
332 B2INFO(
"CDCUnpacker : Suppressed mode.");
337 for (
int it = 0; it < dataLength; ++it) {
338 int index = it + c_headearWords;
339 m_buffer.push_back(
static_cast<unsigned short>((ibuf[index] & 0xffff0000) >> 16));
340 m_buffer.push_back(
static_cast<unsigned short>(ibuf[index] & 0xffff));
343 const size_t bufSize =
m_buffer.size();
344 for (
size_t it = 0; it < bufSize;) {
345 unsigned short header =
m_buffer.at(it);
346 unsigned short ch = (header & 0xff00) >> 8;
347 unsigned short length = (header & 0xff) / 2;
349 if (header == 0xff02) {
354 if (!((length == 4) || (length == 5))) {
356 B2ERROR(
"CDCUnpacker : data length should be 4 or 5 words."
357 <<
LogVar(
"data length", length)
358 <<
LogVar(
"board id", board)
359 <<
LogVar(
"channel", ch));
362 B2WARNING(
"CDCUnpacker : data length should be 4 or 5 words."
363 <<
LogVar(
"data length", length)
364 <<
LogVar(
"board id", board)
365 <<
LogVar(
"channel", ch));
370 unsigned short tot =
m_buffer.at(it + 1);
371 unsigned short fadcSum =
m_buffer.at(it + 2);
374 int diff = fadcSum - (*m_adcPedestalFromDB)->getPedestal(board, ch);
378 fadcSum =
static_cast<unsigned short>(diff);
381 unsigned short tdc1 = 0;
382 unsigned short tdc2 = 0;
383 unsigned short tdcFlag = 0;
387 }
else if (length == 5) {
389 tdc2 =
m_buffer.at(it + 4) & 0x7fff;
390 tdcFlag = (
m_buffer.at(it + 4) & 0x8000) >> 15;
392 B2ERROR(
"CDCUnpacker : Undefined data length (should be 4 or 5 short words) ");
396 B2WARNING(
"Invalid channel "
400 <<
LogVar(
"length", length)
410 printf(
"%4x %4x %4x %4x %4x %4x %4x \n", ch, length, tot, fadcSum, tdc1, tdc2, tdcFlag);
412 if (length == 4 || length == 5) {
415 const unsigned short status = trigType;
437 m_CDCRawHits.appendNew(status, trgNumber, iNode, iFiness, board, ch,
438 trgTime, fadcSum, tdc1, tdc2, tot);
442 B2WARNING(
"Undefined board id is fired: " <<
LogVar(
"board id", board) <<
" " <<
LogVar(
"channel", ch));
449 B2WARNING(
"CDCUnpacker : Undefined CDC Data Block : Block # " <<
LogVar(
"block id", i));
460 int tdc = hit.getTDCCount();
461 if (hit.is2ndHit()) {
470 hit.setTDCCount(
static_cast<unsigned short>(tdc));
478 B2INFO(
"CDCUnpacker : End run.");
485 B2INFO(
"CDCUnpacker : Terminated.");
495 return m_map[iBoard][iCh];
505 std::cout << fileName << std::endl;
506 if (fileName ==
"") {
507 B2ERROR(
"CDC unpacker can't find a filename: " <<
LogVar(
"file name", fileName));
513 ifs.open(fileName.c_str());
521 ifs >> isl >> icl >> iw >> iBoard >> iCh;
522 const WireID wireId(isl, icl, iw);
523 m_map[iBoard][iCh] = wireId;
528 const int il = cm.getILayer();
529 const int iw = cm.getIWire();
530 const int iBoard = cm.getBoardID();
531 const int iCh = cm.getBoardChannel();
532 const WireID wireId(isl, il, iw);
533 m_map[iBoard][iCh] = wireId;
542 if (!(*m_adcPedestalFromDB).isValid()) {
552 for (
int j = 0; j < nwords; ++j) {
553 printf(
" %.8x", buf[j]);
554 if ((j + 1) % 10 == 0) {
Class containing the result of the unpacker in raw data and the result of the digitizer in simulation...
void set2ndHitFlag()
Setter for 2nd hit flag.
void setOtherHitIndices(CDCHit *otherHit)
Setter for the other hit indices.
StoreArray< RawCDC > m_rawCDCs
Input array for CDC Raw.
bool m_enableDatabase
Enable/Disable to read the channel map from the database.
int m_boardId
Front end board ID.
std::vector< unsigned short > m_buffer
Short ward buffer of CDC event block.
int m_channelTrig
Channel for the trigger.
bool m_dataSizeError
True if data size error between CDCFE and COPPER has been already reported.
std::string m_rawCDCName
Name of the RawCDC dataobject (suppressed mode).
void initialize() override
Initializes the Module.
DBArray< CDCChannelMap > * m_channelMapFromDB
Channel map retrieved from DB.
StoreArray< CDCRawHitWaveForm > m_CDCRawHitWaveForms
Raw hit waveforms.
void printBuffer(int *buf, int nwords)
Print out the CDC data block in hex.
int getTriggerNumber()
Getter for trigger number.
void event() override
Event action (main routine).
StoreArray< CDCRawHit > m_CDCRawHits
Raw hits.
bool m_subtractTrigTiming
Enable/Disable to subtract the trigger timing from TDCs.
bool m_dataLengthError
True if data length error has been already reported.
void endRun() override
End run action.
void terminate() override
Termination action.
int m_fadcThreshold
FADC threshold.
int getTriggerTime()
Getter for trigger time in nsec.
int m_tdcOffset
TDC offset (nsec).
std::string m_xmlMapFileName
Name of the assignment map of FE board channel to the cell.
WireID m_map[300][48]
Assignment map of FE board channel to the cell.
int getDataType()
Getter for CDC data mode.
std::string m_cdcRawHitName
Name of the CDCRawHit dataobject (suppressed mode).
bool m_pedestalSubtraction
Whether pedestal is subtracted (true) or not (false).
void beginRun() override
Begin run action.
bool m_recoverBoardIdError
Recover boardID error if true, skip information otherwise.
DBObjPtr< CDCADCDeltaPedestals > * m_adcPedestalFromDB
ADC delta pedestal.
int m_boardIDTrig
Board ID for the trigger.
bool m_enableStoreCDCRawHit
Enable/Disable to store CDCRawHit.
int m_tdcAuxOffset
TDC auxiliary offset (nsec).
int getDataLength()
Getter for data length in byte.
void loadMap()
Load FE channel to cell ID map.
WireID getWireID(int iBoard, int iCh) const
Getter of Wire ID.
bool m_enablePrintOut
Enable/Disable to print out the data to the terminal.
bool isValidBoardChannel(WireID wireId)
Check if the hit wire is valid or not.
std::string m_cdcRawHitWaveFormName
Name of the CDCRawHit dataobject (raw data mode).
void setCDCPacketHeader(const int *buf)
Set CDC Packet header.
virtual ~CDCUnpackerModule()
Destructor of the module.
bool m_enable2ndHit
Enable/Disable to 2nd hit output.
int getBoardId()
Getter for FE board ID.
std::string m_cdcHitName
Tree name of the CDCHit object.
void setADCPedestal()
Set DBobject of ADC delta pedestal.
StoreArray< CDCHit > m_CDCHits
CDC hits.
Class for accessing arrays of objects in the database.
Class for accessing objects in the database.
static std::string findFile(const std::string &path, bool silent=false)
Search for given file or directory in local or central release directory, and return absolute path if...
void setDescription(const std::string &description)
Sets the description of the module.
void setPropertyFlags(unsigned int propertyFlags)
Sets the flags for the module properties.
@ c_ParallelProcessingCertified
This module can be run in parallel processing mode safely (All I/O must be done through the data stor...
Class to identify a wire inside the CDC.
unsigned short getISuperLayer() const
Getter for Super-Layer.
Class to store variables with their name which were sent to the logging service.
void addParam(const std::string &name, T ¶mVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Abstract base class for different kinds of events.