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"
35 #include "AliAltroBunch.h"
38 /** ROOT macro for the implementation of ROOT specific class methods */
39 ClassImp(AliHLTAltroChannelSelectorComponent)
41 AliHLTAltroChannelSelectorComponent::AliHLTAltroChannelSelectorComponent()
51 // see header file for class documentation
53 // refer to README to build package
55 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
58 AliHLTAltroChannelSelectorComponent::~AliHLTAltroChannelSelectorComponent()
60 // see header file for class documentation
63 const char* AliHLTAltroChannelSelectorComponent::GetComponentID()
65 // see header file for class documentation
66 return "AltroChannelSelector";
69 void AliHLTAltroChannelSelectorComponent::GetInputDataTypes(AliHLTComponentDataTypeList& list)
71 // see header file for class documentation
73 list.push_back(kAliHLTDataTypeDDLRaw|kAliHLTDataOriginTPC);
74 list.push_back(kAliHLTDataTypeHwAddr16);
77 AliHLTComponentDataType AliHLTAltroChannelSelectorComponent::GetOutputDataType()
79 // see header file for class documentation
80 return kAliHLTDataTypeDDLRaw|kAliHLTDataOriginTPC;
83 void AliHLTAltroChannelSelectorComponent::GetOutputDataSize(unsigned long& constBase, double& inputMultiplier)
85 // see header file for class documentation
90 AliHLTComponent* AliHLTAltroChannelSelectorComponent::Spawn()
92 // see header file for class documentation
93 return new AliHLTAltroChannelSelectorComponent;
96 int AliHLTAltroChannelSelectorComponent::DoInit(int argc, const char** argv)
98 // see header file for class documentation
101 bool bMissingParam=0;
104 for (; i<argc && iResult>=0; i++) {
107 if (argument.IsNull()) continue;
109 // -skip-corrupted, just for backward compatibility, not announced
110 if (argument.CompareTo("-skip-corrupted")==0) {
114 } else if (argument.CompareTo("-keep-corrupted")==0) {
115 fSkipCorrupted=false;
118 } else if (argument.CompareTo("-talkative")==0) {
122 } else if (argument.CompareTo("-start-timebin")==0) {
123 if ((bMissingParam=(++i>=argc))) break;
124 fStartTimeBin = strtoul( argv[i], &cpErr ,0);
128 } else if (argument.CompareTo("-end-timebin")==0) {
129 if ((bMissingParam=(++i>=argc))) break;
130 fEndTimeBin = strtoul( argv[i], &cpErr ,0);
134 } else if (argument.CompareTo("-signal-threshold")==0) {
135 if ((bMissingParam=(++i>=argc))) break;
136 fSignalThreshold = strtoul( argv[i], &cpErr ,0);
140 } else if (argument.CompareTo("-rms-threshold")==0) {
141 if ((bMissingParam=(++i>=argc))) break;
142 fRMSThreshold = strtoul( argv[i], &cpErr ,0);
146 HLTError("unknown argument %s", argument.Data());
151 if (cpErr && *cpErr) {
152 HLTError("Cannot convert specifier '%s' for argument '%s'", argv[i], argument.Data());
154 } else if (bMissingParam) {
155 HLTError("missing parameter for argument %s", argument.Data());
162 int AliHLTAltroChannelSelectorComponent::DoDeinit()
164 // see header file for class documentation
168 int AliHLTAltroChannelSelectorComponent::DoEvent(const AliHLTComponentEventData& evtData,
169 const AliHLTComponentBlockData* blocks,
170 AliHLTComponentTriggerData& /*trigData*/,
171 AliHLTUInt8_t* outputPtr,
172 AliHLTUInt32_t& size,
173 AliHLTComponentBlockDataList& outputBlocks )
175 // see header file for class documentation
177 const int cdhSize=32;
179 if (!IsDataEvent()) {
184 // process the DLL input
186 const AliHLTComponentBlockData* pDesc=NULL;
188 AliAltroDecoder* decoder=NULL;
189 for (pDesc=GetFirstInputBlock(kAliHLTDataTypeDDLRaw); pDesc!=NULL; pDesc=GetNextInputBlock(), blockno++) {
191 if (pDesc->fSize<=32) {
195 // search for the active pad information
196 AliHLTUInt16_t* pActiveHwAddressArray=NULL;
198 if (fSignalThreshold==0 && fRMSThreshold==0) {
199 for (int i=0; i<(int)evtData.fBlockCnt; i++ ) {
200 // search for selection data of hw address type
201 // which matches the data specification of the block
202 if (blocks[i].fDataType==kAliHLTDataTypeHwAddr16 && blocks[i].fSpecification==pDesc->fSpecification) {
203 pActiveHwAddressArray=reinterpret_cast<AliHLTUInt16_t*>(blocks[i].fPtr);
204 iArraySize=blocks[i].fSize/sizeof(AliHLTUInt16_t);
208 if (pActiveHwAddressArray==NULL) {
209 HLTWarning("no block of type %s for specification 0x%08x available, data block skipped",
210 DataType2Text(kAliHLTDataTypeHwAddr16).c_str(),
211 pDesc->fSpecification);
216 if (decoder) delete decoder;
217 decoder=new AliAltroDecoder;
218 if (decoder->SetMemory(reinterpret_cast<UChar_t*>(pDesc->fPtr), pDesc->fSize)<0) {
219 HLTWarning("corrupted data block: initialization of decoder failed for block: %s specification %#x size %d",
220 DataType2Text(pDesc->fDataType).c_str(), pDesc->fSpecification, pDesc->fSize);
223 if (decoder->Decode()) {
224 HLTDebug("init decoder %p size %d", pDesc->fPtr,pDesc->fSize);
226 HLTWarning("corrupted data block: decoding failed for raw data block: %s specification %#x size %d",
227 DataType2Text(pDesc->fDataType).c_str(), pDesc->fSpecification, pDesc->fSize);
232 unsigned int rcuTrailerLength=0;
234 ((rcuTrailerLength=decoder->GetRCUTrailerSize())==0 ||
235 rcuTrailerLength>pDesc->fSize-cdhSize)) {
236 if (rcuTrailerLength>0) {
237 HLTWarning("corrupted data block: RCU trailer length exceeds buffer size");
239 HLTWarning("corrupted data block: RCU trailer of zero length");
245 // TODO: here the trigger has to come into play. It is up to
246 // policy if a corrupted data block should be kept (original
247 // DDL) or discarded. In any case, the block is not going to
249 HLTWarning("skipping corrupted data block for event %lu, data block %s 0x%08x", evtData.fEventID,
250 DataType2Text(pDesc->fDataType).c_str(), pDesc->fSpecification);
258 AliHLTUInt32_t iOutputSize=0;
259 AliHLTUInt32_t iNofAltro40=0;
260 AliHLTUInt32_t iCapacity=size;
261 AliAltroData channel;
262 AliAltroBunch altrobunch;
264 // first add the RCU trailer
265 AliHLTUInt8_t* pSrc=reinterpret_cast<AliHLTUInt8_t*>(pDesc->fPtr);
266 pSrc+=pDesc->fSize-rcuTrailerLength;
267 if ((iResult=CopyBlockToEnd(outputPtr, iCapacity, iOutputSize, pSrc, rcuTrailerLength))>=0) {
268 assert(iResult==(int)rcuTrailerLength);
269 iOutputSize+=rcuTrailerLength;
271 HLTError("failed to write RCU trailer of length %d for block %d, too little space in output buffer?", rcuTrailerLength, blockno);
276 while (decoder->NextChannel(&channel) && iResult>=0) {
279 int hwAddress=channel.GetHadd();
280 if (fSignalThreshold!=0) {
281 // treshold by adc counts
282 unsigned int sumSignals=0;
283 unsigned int maxSignal=0;
284 unsigned int nofSignals=0;
285 while(channel.NextBunch(&altrobunch)){
286 const UInt_t *bunchData=altrobunch.GetData();
287 unsigned int time=altrobunch.GetStartTimeBin();
288 for(Int_t i=0;i<altrobunch.GetBunchSize();i++){
289 if(bunchData[i]>0){// disregarding 0 data.
290 if(time+i>=fStartTimeBin && time+i<=fEndTimeBin){
291 sumSignals+=bunchData[i];
292 if (maxSignal<bunchData[i]) maxSignal=bunchData[i];
298 if (nofSignals==0 || maxSignal<=(sumSignals/nofSignals)+fSignalThreshold) {
302 } else if (fRMSThreshold!=0) {
303 // treshold by adc counts
304 unsigned int sumSignals=0;
305 unsigned int maxSignal=0;
306 unsigned int nofSignals=0;
307 while(channel.NextBunch(&altrobunch)){
308 const UInt_t *bunchData=altrobunch.GetData();
309 unsigned int time=altrobunch.GetStartTimeBin();
310 for(Int_t i=0;i<altrobunch.GetBunchSize();i++){
311 if(bunchData[i]>0){// disregarding 0 data.
312 if(time+i>=fStartTimeBin && time+i<=fEndTimeBin){
313 sumSignals+=bunchData[i]*bunchData[i];
314 if (maxSignal<bunchData[i]) maxSignal=bunchData[i];
320 if (nofSignals==0 || maxSignal<=TMath::Sqrt(sumSignals/nofSignals)*fRMSThreshold) {
326 for (active=0; active<iArraySize; active++) {
327 if (pActiveHwAddressArray[active]==(AliHLTUInt16_t)hwAddress) {
331 if (active>=iArraySize) {
332 HLTDebug("ALTRO block %#x (%d) discarded (inactive)", hwAddress, hwAddress);
337 // no of 10 bit words is without the fill words to fill complete 40 bit words
338 // in addition, align to complete 40 bit words (the '+3')
339 // also, the 5 bytes of the Altro trailer must be added to get the full size
340 int channelSize=((channel.GetDataSize()+3)/4)*5;
341 if (channelSize==0) {
342 if (fTalkative) HLTWarning("skipping zero length channel (hw address %d)", hwAddress);
347 HLTDebug("ALTRO block hwAddress 0x%08x (%d) selected (active), size %d", hwAddress, hwAddress, channelSize);
349 if ((iResult=decoder->CopyBackward(outputPtr, iCapacity-iOutputSize))>=0) {
350 if (channelSize == iResult) {
351 if (channelSize%5 == 0) {
352 iNofAltro40+=channelSize/5;
353 iOutputSize+=channelSize;
355 if (fTalkative) HLTWarning("corrupted ALTRO channel: incomplete 40 bit word (channel hw address %d)", hwAddress);
360 if (fTalkative) HLTWarning("internal error: failed to copy full channel: %d out of %d bytes (hw address %d)", iResult, channelSize, hwAddress);
365 if (fTalkative) HLTError("failed to write ALTRO channel of length %d for block %d (hw address %d)", channelSize, blockno, hwAddress);
366 // corrupted channel, but keep going
374 // write the common data header
375 if ((iResult=CopyBlockToEnd(outputPtr, iCapacity, iOutputSize, pDesc->fPtr, cdhSize))>=0) {
376 assert(iResult==cdhSize);
377 iOutputSize+=cdhSize;
379 // set new length of the data block
380 AliHLTUInt32_t* pCdhSize=reinterpret_cast<AliHLTUInt32_t*>(outputPtr+iCapacity-iOutputSize);
381 *pCdhSize=iOutputSize;
383 // set number of Altro words
384 AliHLTUInt32_t* pNofAltro40=reinterpret_cast<AliHLTUInt32_t*>(outputPtr+iCapacity-rcuTrailerLength);
385 *pNofAltro40=iNofAltro40;
387 // insert block descriptor
388 AliHLTComponentBlockData bd;
390 bd.fOffset=iCapacity-iOutputSize;
391 bd.fSize=iOutputSize;
392 bd.fDataType=pDesc->fDataType;
393 bd.fSpecification=pDesc->fSpecification;
394 outputBlocks.push_back(bd);
395 iCapacity-=iOutputSize;
397 HLTError("failed to write CDH of length %d for block %d", cdhSize, blockno);
401 if (fTalkative) HLTImportant("data block %d (0x%08x): selected %d out of %d ALTRO channel(s), %d corrupted channels skipped", blockno, pDesc->fSpecification, iSelected, iTotal, iCorrupted);
403 if (decoder) delete decoder;
406 outputBlocks.clear();
409 // all data blocks need to be moved to the beginning of the
410 // buffer because PubSub is not able to handle data blocks entirely
411 // at the end of the buffer. The problem is that the component always
412 // indicates to use the full size of the buffer
413 if (outputBlocks.size()>0) {
414 int offset=outputBlocks.back().fOffset;
416 memmove(outputPtr, outputPtr+offset, size);
417 for (AliHLTComponentBlockDataList::iterator block=outputBlocks.begin();
418 block!=outputBlocks.end();
420 block->fOffset-=offset;
427 int AliHLTAltroChannelSelectorComponent::CopyBlockToEnd(AliHLTUInt8_t* pTgt, unsigned capacity, unsigned position, void* pSrc, unsigned size)
430 if (pTgt==NULL || pSrc==NULL) return -EINVAL;
431 if (capacity-position<size) return -ENOSPC;
433 memcpy(pTgt+(capacity-position-size), pSrc, size);