3 //**************************************************************************
4 //* This file is property of and copyright by the ALICE HLT Project *
5 //* ALICE Experiment at CERN, All rights reserved. *
7 //* Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no> *
8 //* for The ALICE HLT Project. *
10 //* Permission to use, copy, modify and distribute this software and its *
11 //* documentation strictly for non-commercial purposes is hereby granted *
12 //* without fee, provided that the above copyright notice appears in all *
13 //* copies and that both the copyright notice and this permission notice *
14 //* appear in the supporting documentation. The authors make no claims *
15 //* about the suitability of this software for any purpose. It is *
16 //* provided "as is" without express or implied warranty. *
17 //**************************************************************************
19 /** @file AliHLTAltroChannelSelectorComponent.cxx
20 @author Matthias Richter
22 @brief A filter/selective readout component for Altro data.
25 // see header file for class documentation
27 // refer to README to build package
29 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
32 #include "AliHLTAltroChannelSelectorComponent.h"
33 #include "AliAltroDecoder.h"
34 #include "AliAltroData.h"
36 /** ROOT macro for the implementation of ROOT specific class methods */
37 ClassImp(AliHLTAltroChannelSelectorComponent)
39 AliHLTAltroChannelSelectorComponent::AliHLTAltroChannelSelectorComponent()
42 fSkipCorrupted(false),
45 // see header file for class documentation
47 // refer to README to build package
49 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
52 AliHLTAltroChannelSelectorComponent::~AliHLTAltroChannelSelectorComponent()
54 // see header file for class documentation
57 const char* AliHLTAltroChannelSelectorComponent::GetComponentID()
59 // see header file for class documentation
60 return "AltroChannelSelector";
63 void AliHLTAltroChannelSelectorComponent::GetInputDataTypes(AliHLTComponentDataTypeList& list)
65 // see header file for class documentation
67 list.push_back(kAliHLTDataTypeDDLRaw|kAliHLTDataOriginTPC);
68 list.push_back(kAliHLTDataTypeHwAddr16);
71 AliHLTComponentDataType AliHLTAltroChannelSelectorComponent::GetOutputDataType()
73 // see header file for class documentation
74 return kAliHLTDataTypeDDLRaw|kAliHLTDataOriginTPC;
77 void AliHLTAltroChannelSelectorComponent::GetOutputDataSize(unsigned long& constBase, double& inputMultiplier)
79 // see header file for class documentation
84 AliHLTComponent* AliHLTAltroChannelSelectorComponent::Spawn()
86 // see header file for class documentation
87 return new AliHLTAltroChannelSelectorComponent;
90 int AliHLTAltroChannelSelectorComponent::DoInit(int argc, const char** argv)
92 // see header file for class documentation
96 for (int i=0; i<argc && iResult>=0; i++) {
98 if (argument.IsNull()) continue;
101 if (argument.CompareTo("-skip-corrupted")==0) {
105 } else if (argument.CompareTo("-talkative")==0) {
108 HLTError("unknown argument %s", argument.Data());
114 HLTError("missing parameter for argument %s", argument.Data());
121 int AliHLTAltroChannelSelectorComponent::DoDeinit()
123 // see header file for class documentation
127 int AliHLTAltroChannelSelectorComponent::DoEvent(const AliHLTComponentEventData& evtData,
128 const AliHLTComponentBlockData* blocks,
129 AliHLTComponentTriggerData& /*trigData*/,
130 AliHLTUInt8_t* outputPtr,
131 AliHLTUInt32_t& size,
132 AliHLTComponentBlockDataList& outputBlocks )
134 // see header file for class documentation
136 const int cdhSize=32;
138 if (!IsDataEvent()) {
143 // process the DLL input
145 const AliHLTComponentBlockData* pDesc=NULL;
147 AliAltroDecoder* decoder=NULL;
148 for (pDesc=GetFirstInputBlock(kAliHLTDataTypeDDLRaw); pDesc!=NULL; pDesc=GetNextInputBlock(), blockno++) {
150 if (pDesc->fSize<=32) continue;
152 // search for the active pad information
153 AliHLTUInt16_t* pActiveHwAddressArray=NULL;
155 for (int i=0; i<(int)evtData.fBlockCnt; i++ ) {
156 // search for selection data of hw address type
157 // which matches the data specification of the block
158 if (blocks[i].fDataType==kAliHLTDataTypeHwAddr16 && blocks[i].fSpecification==pDesc->fSpecification) {
159 pActiveHwAddressArray=reinterpret_cast<AliHLTUInt16_t*>(blocks[i].fPtr);
160 iArraySize=blocks[i].fSize/sizeof(AliHLTUInt16_t);
164 if (pActiveHwAddressArray==NULL) {
165 HLTWarning("no block of type %s for specification 0x%08x available, data block unchanged",
166 DataType2Text(kAliHLTDataTypeHwAddr16).c_str(),
167 pDesc->fSpecification);
171 if (decoder) delete decoder;
172 decoder=new AliAltroDecoder;
173 if (decoder->SetMemory(reinterpret_cast<UChar_t*>(pDesc->fPtr), pDesc->fSize)<0) {
174 HLTWarning("corrupted data block: initialization of decoder failed for block: %s specification %#x size %d",
175 DataType2Text(pDesc->fDataType).c_str(), pDesc->fSpecification, pDesc->fSize);
178 if (decoder->Decode()) {
179 HLTDebug("init decoder %p size %d", pDesc->fPtr,pDesc->fSize);
181 HLTWarning("corrupted data block: decoding failed for raw data block: %s specification %#x size %d",
182 DataType2Text(pDesc->fDataType).c_str(), pDesc->fSpecification, pDesc->fSize);
187 unsigned int rcuTrailerLength=0;
189 ((rcuTrailerLength=decoder->GetRCUTrailerSize())==0 ||
190 rcuTrailerLength>pDesc->fSize-cdhSize)) {
191 if (rcuTrailerLength>0) {
192 HLTWarning("corrupted data block: RCU trailer length exceeds buffer size");
194 HLTWarning("corrupted data block: RCU trailer of zero length");
200 // TODO: here the trigger has to come into play. It is up to
201 // policy if a corrupted data block should be kept (original
202 // DDL) or discarded. In any case, the block is not going to
204 HLTWarning("skipping corrupted data block for event %lu, data block %s 0x%08x", evtData.fEventID,
205 DataType2Text(pDesc->fDataType).c_str(), pDesc->fSpecification);
213 AliHLTUInt32_t iOutputSize=0;
214 AliHLTUInt32_t iNofAltro40=0;
215 AliHLTUInt32_t iCapacity=size;
216 AliAltroData channel;
218 // first add the RCU trailer
219 AliHLTUInt8_t* pSrc=reinterpret_cast<AliHLTUInt8_t*>(pDesc->fPtr);
220 pSrc+=pDesc->fSize-rcuTrailerLength;
221 if ((iResult=CopyBlockToEnd(outputPtr, iCapacity, iOutputSize, pSrc, rcuTrailerLength))>=0) {
222 assert(iResult==(int)rcuTrailerLength);
223 iOutputSize+=rcuTrailerLength;
225 HLTError("failed to write RCU trailer of length %d for block %d, too little space in output buffer?", rcuTrailerLength, blockno);
230 while (decoder->NextChannel(&channel) && iResult>=0) {
233 int hwAddress=channel.GetHadd();
235 for (active=0; active<iArraySize; active++) {
236 if (pActiveHwAddressArray[active]==(AliHLTUInt16_t)hwAddress) {
240 if (active>=iArraySize) {
241 HLTDebug("ALTRO block %#x (%d) discarded (inactive)", hwAddress, hwAddress);
245 // no of 10 bit words is without the fill words to fill complete 40 bit words
246 // in addition, align to complete 40 bit words (the '+3')
247 // also, the 5 bytes of the Altro trailer must be added to get the full size
248 int channelSize=((channel.GetDataSize()+3)/4)*5;
249 if (channelSize==0) {
250 if (fTalkative) HLTWarning("skipping zero length channel (hw address %d)", hwAddress);
255 HLTDebug("ALTRO block hwAddress 0x%08x (%d) selected (active), size %d", hwAddress, hwAddress, channelSize);
257 if ((iResult=decoder->CopyBackward(outputPtr, iCapacity-iOutputSize))>=0) {
258 if (channelSize == iResult) {
259 if (channelSize%5 == 0) {
260 iNofAltro40+=channelSize/5;
261 iOutputSize+=channelSize;
263 if (fTalkative) HLTWarning("corrupted ALTRO channel: incomplete 40 bit word (channel hw address %d)", hwAddress);
268 if (fTalkative) HLTWarning("internal error: failed to copy full channel: %d out of %d bytes (hw address %d)", iResult, channelSize, hwAddress);
273 if (fTalkative) HLTError("failed to write ALTRO channel of length %d for block %d (hw address %d)", channelSize, blockno, hwAddress);
274 // corrupted channel, but keep going
282 // write the common data header
283 if ((iResult=CopyBlockToEnd(outputPtr, iCapacity, iOutputSize, pDesc->fPtr, cdhSize))>=0) {
284 assert(iResult==cdhSize);
285 iOutputSize+=cdhSize;
287 // set new length of the data block
288 AliHLTUInt32_t* pCdhSize=reinterpret_cast<AliHLTUInt32_t*>(outputPtr+iCapacity-iOutputSize);
289 *pCdhSize=iOutputSize;
291 // set number of Altro words
292 AliHLTUInt32_t* pNofAltro40=reinterpret_cast<AliHLTUInt32_t*>(outputPtr+iCapacity-rcuTrailerLength);
293 *pNofAltro40=iNofAltro40;
295 // insert block descriptor
296 AliHLTComponentBlockData bd;
298 bd.fOffset=iCapacity-iOutputSize;
299 bd.fSize=iOutputSize;
300 bd.fDataType=pDesc->fDataType;
301 bd.fSpecification=pDesc->fSpecification;
302 outputBlocks.push_back(bd);
303 iCapacity-=iOutputSize;
305 HLTError("failed to write CDH of length %d for block %d", cdhSize, blockno);
309 HLTInfo("data block %d (0x%08x): selected %d out of %d ALTRO channel(s), %d corrupted channels skipped", blockno, pDesc->fSpecification, iSelected, iTotal, iCorrupted);
311 if (decoder) delete decoder;
314 outputBlocks.clear();
317 // !!! do not change the size since the output buffer is filled from the end !!!
322 int AliHLTAltroChannelSelectorComponent::CopyBlockToEnd(AliHLTUInt8_t* pTgt, unsigned capacity, unsigned position, void* pSrc, unsigned size)
325 if (pTgt==NULL || pSrc==NULL) return -EINVAL;
326 if (capacity-position<size) return -ENOSPC;
328 memcpy(pTgt+(capacity-position-size), pSrc, size);