AltroChannelSelector component moved to libAliHLTRCU
[u/mrichter/AliRoot.git] / HLT / RCU / AliHLTAltroChannelSelectorComponent.cxx
CommitLineData
3e6ec852 1// $Id$
2
3/**************************************************************************
4 * This file is property of and copyright by the ALICE HLT Project *
5 * ALICE Experiment at CERN, All rights reserved. *
6 * *
7 * Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no> *
8 * for The ALICE HLT Project. *
9 * *
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 **************************************************************************/
18
19/** @file AliHLTAltroChannelSelectorComponent.cxx
20 @author Matthias Richter
21 @date
3dcae0cc 22 @brief A filter/selective readout component for Altro data. */
3e6ec852 23
24// see header file for class documentation
25// or
26// refer to README to build package
27// or
28// visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
29
e1440dab 30#include <cassert>
3e6ec852 31#include "AliHLTAltroChannelSelectorComponent.h"
4ca5858d 32#include "AliAltroDecoder.h"
33#include "AliAltroData.h"
3e6ec852 34
35/** ROOT macro for the implementation of ROOT specific class methods */
36ClassImp(AliHLTAltroChannelSelectorComponent)
37
38AliHLTAltroChannelSelectorComponent::AliHLTAltroChannelSelectorComponent()
39 :
e1440dab 40 AliHLTProcessor(),
4ca5858d 41 fSkipCorrupted(false),
42 fTalkative(false)
3e6ec852 43{
44 // see header file for class documentation
45 // or
46 // refer to README to build package
47 // or
48 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
49}
50
51AliHLTAltroChannelSelectorComponent::~AliHLTAltroChannelSelectorComponent()
52{
53 // see header file for class documentation
54}
55
56const char* AliHLTAltroChannelSelectorComponent::GetComponentID()
57{
58 // see header file for class documentation
59 return "AltroChannelSelector";
60}
61
62void AliHLTAltroChannelSelectorComponent::GetInputDataTypes(AliHLTComponentDataTypeList& list)
63{
64 // see header file for class documentation
65 list.clear();
66 list.push_back(kAliHLTDataTypeDDLRaw|kAliHLTDataOriginTPC);
64defa03 67 list.push_back(kAliHLTDataTypeHwAddr16);
3e6ec852 68}
69
70AliHLTComponentDataType AliHLTAltroChannelSelectorComponent::GetOutputDataType()
71{
72 // see header file for class documentation
73 return kAliHLTDataTypeDDLRaw|kAliHLTDataOriginTPC;
74}
75
76void AliHLTAltroChannelSelectorComponent::GetOutputDataSize(unsigned long& constBase, double& inputMultiplier)
77{
78 // see header file for class documentation
79 constBase=0;
80 inputMultiplier=1.0;
81}
82
83AliHLTComponent* AliHLTAltroChannelSelectorComponent::Spawn()
84{
85 // see header file for class documentation
86 return new AliHLTAltroChannelSelectorComponent;
87}
88
89int AliHLTAltroChannelSelectorComponent::DoInit(int argc, const char** argv)
90{
91 // see header file for class documentation
92 int iResult=0;
93 TString argument="";
e1440dab 94 bool bMissingParam=0;
3e6ec852 95 for (int i=0; i<argc && iResult>=0; i++) {
96 argument=argv[i];
97 if (argument.IsNull()) continue;
98
4ca5858d 99 // -skip-corrupted
100 if (argument.CompareTo("-skip-corrupted")==0) {
101 fSkipCorrupted=true;
102
103 // -talkative
104 } else if (argument.CompareTo("-talkative")==0) {
105 fTalkative=true;
3e6ec852 106 } else {
4ca5858d 107 HLTError("unknown argument %s", argument.Data());
3e6ec852 108 iResult=-EINVAL;
109 }
110 }
111
e1440dab 112 if (bMissingParam) {
113 HLTError("missing parameter for argument %s", argument.Data());
114 iResult=-EINVAL;
115 }
116
3e6ec852 117 return iResult;
118}
119
120int AliHLTAltroChannelSelectorComponent::DoDeinit()
121{
122 // see header file for class documentation
123 return 0;
124}
125
126int AliHLTAltroChannelSelectorComponent::DoEvent(const AliHLTComponentEventData& evtData,
127 const AliHLTComponentBlockData* blocks,
128 AliHLTComponentTriggerData& /*trigData*/,
129 AliHLTUInt8_t* outputPtr,
130 AliHLTUInt32_t& size,
131 AliHLTComponentBlockDataList& outputBlocks )
132{
133 // see header file for class documentation
134 int iResult=0;
4ca5858d 135 const int cdhSize=32;
3e6ec852 136
e1440dab 137 // process the DLL input
138 int blockno=0;
1d337632 139 const AliHLTComponentBlockData* pDesc=NULL;
140
4ca5858d 141 AliAltroDecoder* decoder=NULL;
142 for (pDesc=GetFirstInputBlock(kAliHLTDataTypeDDLRaw); pDesc!=NULL; pDesc=GetNextInputBlock(), blockno++) {
143 iResult=0;
e1440dab 144
145 // search for the active pad information
64defa03 146 AliHLTUInt16_t* pActiveHwAddressArray=NULL;
147 int iArraySize=0;
e1440dab 148 for (int i=0; i<(int)evtData.fBlockCnt; i++ ) {
64defa03 149 // search for selection data of hw address type
4ca5858d 150 // which matches the data specification of the block
151 if (blocks[i].fDataType==kAliHLTDataTypeHwAddr16 && blocks[i].fSpecification==pDesc->fSpecification) {
152 pActiveHwAddressArray=reinterpret_cast<AliHLTUInt16_t*>(blocks[i].fPtr);
153 iArraySize=blocks[i].fSize/sizeof(AliHLTUInt16_t);
154 break;
e1440dab 155 }
156 }
4ca5858d 157 if (pActiveHwAddressArray==NULL) {
158 HLTWarning("no block of type %s for specification 0x%08x available, data block unchanged",
64defa03 159 DataType2Text(kAliHLTDataTypeHwAddr16).c_str(),
1d337632 160 pDesc->fSpecification);
4ca5858d 161 iResult=-EFAULT;
162 }
163
164 if (decoder) delete decoder;
165 decoder=new AliAltroDecoder;
166 if (decoder->SetMemory(reinterpret_cast<UChar_t*>(pDesc->fPtr), pDesc->fSize)<0) {
167 HLTWarning("corrupted data block: initialization of decoder failed for block: %s specification %#x size %d",
168 DataType2Text(pDesc->fDataType).c_str(), pDesc->fSpecification, pDesc->fSize);
169 iResult=-EFAULT;
170 } else {
171 if (decoder->Decode()) {
172 HLTDebug("init decoder %p size %d", pDesc->fPtr,pDesc->fSize);
173 } else {
174 HLTWarning("corrupted data block: decoding failed for raw data block: %s specification %#x size %d",
175 DataType2Text(pDesc->fDataType).c_str(), pDesc->fSpecification, pDesc->fSize);
176 iResult=-EFAULT;
177 }
178 }
179
180 int rcuTrailerLength=decoder->GetRCUTrailerSize();
181 if (rcuTrailerLength*sizeof(AliHLTUInt32_t)>pDesc->fSize-cdhSize) {
182 HLTWarning("corrupted data block: RCU trailer length exceeds buffer size");
183 iResult=-EFAULT;
184 }
185
186 if (iResult<0) {
e1440dab 187 // forward the whole block
1d337632 188 outputBlocks.push_back(*pDesc);
3dcae0cc 189 iResult=0;
e1440dab 190 continue;
191 }
192
1d337632 193 int iSelected=0;
194 int iTotal=0;
4ca5858d 195 int iCorrupted=0;
e1440dab 196 AliHLTUInt32_t iOutputSize=0;
4ca5858d 197 AliHLTUInt32_t iNofAltro40=0;
e1440dab 198 AliHLTUInt32_t iCapacity=size;
4ca5858d 199 AliAltroData channel;
200 while (decoder->NextChannel(&channel) && iResult>=0) {
1d337632 201 iTotal++;
64defa03 202
4ca5858d 203 int hwAddress=channel.GetHadd();
e1440dab 204 int active=0;
4ca5858d 205 for (active=0; active<iArraySize; active++) {
206 if (pActiveHwAddressArray[active]==(AliHLTUInt16_t)hwAddress) {
207 break;
e1440dab 208 }
209 }
64defa03 210 if (active>=iArraySize) {
4ca5858d 211 HLTDebug("ALTRO block %#x (%d) discarded (inactive)", hwAddress, hwAddress);
e1440dab 212 continue;
213 }
214
4ca5858d 215 // no of 10 bit words is without the fill words to fill complete 40 bit words
216 // in addition, align to complete 40 bit words (the '+3')
217 // also, the 5 bytes of the Altro trailer must be added to get the full size
218 int channelSize=((channel.GetDataSize()+3)/4)*5;
219 channelSize+=5;
220 HLTDebug("ALTRO block hwAddress 0x%08x (%d) selected (active), size %d", hwAddress, hwAddress, channelSize);
221 if (iOutputSize==0) {
222 // first add the RCU trailer
223 AliHLTUInt8_t* pSrc=reinterpret_cast<AliHLTUInt8_t*>(pDesc->fPtr);
224 pSrc+=pDesc->fSize-rcuTrailerLength;
225 if ((iResult=CopyBlockToEnd(outputPtr, iCapacity, iOutputSize, pSrc, rcuTrailerLength))>=0) {
226 assert(iResult==rcuTrailerLength);
227 iOutputSize+=rcuTrailerLength;
228 } else {
229 HLTError("failed to write RCU trailer of length %d for block %d, too little space in output buffer?", rcuTrailerLength, blockno);
230 iResult=-ENOSPC;
231 break;
232 }
233 }
234
235 if ((iResult=decoder->CopyBackward(outputPtr, iCapacity-iOutputSize))>=0) {
236 if (channelSize == iResult) {
237 if (channelSize%5 == 0) {
238 iNofAltro40+=channelSize/5;
239 iOutputSize+=channelSize;
e1440dab 240 } else {
4ca5858d 241 if (fTalkative) HLTWarning("corrupted ALTRO channel: incomplete 40 bit word");
242 iCorrupted++;
243 continue;
e1440dab 244 }
4ca5858d 245 } else {
246 if (fTalkative) HLTWarning("internal error: failed to copy full channel: %d out of %d bytes", iResult, channelSize);
247 iCorrupted++;
248 continue;
e1440dab 249 }
e1440dab 250 } else {
4ca5858d 251 if (fTalkative) HLTError("failed to write ALTRO channel of length %d for block %d", channelSize, blockno);
252 // corrupted channel, but keep going
253 iCorrupted++;
254 iResult=0;
255 continue;
e1440dab 256 }
4ca5858d 257 iSelected++;
e1440dab 258 }
259 if (iResult>=0) {
260 // write the common data header
1d337632 261 if ((iResult=CopyBlockToEnd(outputPtr, iCapacity, iOutputSize, pDesc->fPtr, cdhSize))>=0) {
e1440dab 262 assert(iResult==cdhSize);
263 iOutputSize+=cdhSize;
264
265 // set new length of the data block
4ca5858d 266 AliHLTUInt32_t* pCdhSize=reinterpret_cast<AliHLTUInt32_t*>(outputPtr+iCapacity-iOutputSize);
e1440dab 267 *pCdhSize=iOutputSize;
268
4ca5858d 269 // set number of Altro words
270 AliHLTUInt32_t* pNofAltro40=reinterpret_cast<AliHLTUInt32_t*>(outputPtr+iCapacity-rcuTrailerLength);
271 *pNofAltro40=iNofAltro40;
272
e1440dab 273 // insert block descriptor
274 AliHLTComponentBlockData bd;
275 FillBlockData(bd);
276 bd.fOffset=iCapacity-iOutputSize;
277 bd.fSize=iOutputSize;
1d337632 278 bd.fDataType=pDesc->fDataType;
279 bd.fSpecification=pDesc->fSpecification;
e1440dab 280 outputBlocks.push_back(bd);
281 iCapacity-=iOutputSize;
282 } else {
283 HLTError("failed to write CDH of length %d for block %d", cdhSize, blockno);
284 break;
285 }
286 }
4ca5858d 287 HLTInfo("data block %d (0x%08x): selected %d out of %d ALTRO channel(s), %d corrupted channels skipped", blockno, pDesc->fSpecification, iSelected, iTotal, iCorrupted);
3e6ec852 288 }
4ca5858d 289 if (decoder) delete decoder;
3e6ec852 290
e1440dab 291 if (iResult<0) {
292 outputBlocks.clear();
3e6ec852 293 }
294
e1440dab 295 // !!! do not change the size since the output buffer is filled from the end !!!
296
297 return iResult;
298}
299
300int AliHLTAltroChannelSelectorComponent::CopyBlockToEnd(AliHLTUInt8_t* pTgt, unsigned capacity, unsigned position, void* pSrc, unsigned size)
301{
302 int iResult=0;
303 if (pTgt==NULL || pSrc==NULL) return -EINVAL;
304 if (capacity-position<size) return -ENOSPC;
305
306 memcpy(pTgt+(capacity-position-size), pSrc, size);
307 iResult=size;
308
3e6ec852 309 return iResult;
310}