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 AliRawReaderHLT.cxx
20 @author Matthias Richter
22 @brief AliRawReader implementation which replaces original input of
23 detectors with the appropriate HLT output. */
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
31 #include "AliRawReaderHLT.h"
32 #include "AliHLTOUTRawReader.h"
33 #include "AliHLTModuleAgent.h"
34 #include "AliHLTOUTHandler.h"
35 #include "AliHLTOUTHandlerEquId.h"
36 #include "AliHLTSystem.h"
38 #include "AliDAQ.h" // RAW, for detector names and equipment ids
39 #include "TObjString.h"
42 /** ROOT macro for the implementation of ROOT specific class methods */
43 ClassImp(AliRawReaderHLT)
45 AliRawReaderHLT::AliRawReaderHLT(AliRawReader* pRawreader, const char* options)
48 AliHLTReconstructorBase(),
49 fpParentReader(pRawreader),
63 // see header file for class documentation
65 // refer to README to build package
67 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
72 AliRawReaderHLT::~AliRawReaderHLT()
74 // see header file for class documentation
78 UInt_t AliRawReaderHLT::GetType() const
80 // see header file for class documentation
81 return fpParentReader->GetType();
84 UInt_t AliRawReaderHLT::GetRunNumber() const
86 // see header file for class documentation
87 return fpParentReader->GetRunNumber();
90 const UInt_t* AliRawReaderHLT::GetEventId() const
92 // see header file for class documentation
93 return fpParentReader->GetEventId();
96 const UInt_t* AliRawReaderHLT::GetTriggerPattern() const
98 // see header file for class documentation
99 return fpParentReader->GetTriggerPattern();
102 const UInt_t* AliRawReaderHLT::GetDetectorPattern() const
104 // see header file for class documentation
105 return fpParentReader->GetDetectorPattern();
108 const UInt_t* AliRawReaderHLT::GetAttributes() const
110 // see header file for class documentation
111 return fpParentReader->GetAttributes();
114 const UInt_t* AliRawReaderHLT::GetSubEventAttributes() const
116 // see header file for class documentation
117 return fpParentReader->GetSubEventAttributes();
120 UInt_t AliRawReaderHLT::GetLDCId() const
122 // see header file for class documentation
123 return fpParentReader->GetLDCId();
126 UInt_t AliRawReaderHLT::GetGDCId() const
128 // see header file for class documentation
129 return fpParentReader->GetGDCId();
132 UInt_t AliRawReaderHLT::GetTimestamp() const
134 // see header file for class documentation
135 return fpParentReader->GetTimestamp();
138 const UInt_t* AliRawReaderHLT::GetEquipmentAttributes() const
140 // see header file for class documentation
141 return fpParentReader->GetEquipmentAttributes();
144 Int_t AliRawReaderHLT::GetEquipmentElementSize() const
146 // see header file for class documentation
147 // don't know what it really means, bu the AliRawReaderFile
149 // do the same if we have a valid equipment data set from
151 if (fEquipmentId>=0) return 0;
152 return fpParentReader->GetEquipmentElementSize();
155 Int_t AliRawReaderHLT::GetEquipmentHeaderSize() const
157 // see header file for class documentation
159 // equipment header means the additional data header?
160 // if we have a valid equipment data set from the HLT stream
161 // there is no additional header
162 if (fEquipmentId>=0) return 0;
163 return fpParentReader->GetEquipmentHeaderSize();
166 Int_t AliRawReaderHLT::GetEquipmentSize() const
168 // see header file for class documentation
169 if (fEquipmentId>=0) return fDataSize+sizeof(AliRawDataHeader);
170 return fpParentReader->GetEquipmentSize();
173 Int_t AliRawReaderHLT::GetEquipmentType() const
175 // see header file for class documentation
176 return fpParentReader->GetEquipmentType();
179 Int_t AliRawReaderHLT::GetEquipmentId() const
181 // see header file for class documentation
183 if (fEquipmentId>=0) id=fEquipmentId;
184 else id=fpParentReader->GetEquipmentId();
188 Bool_t AliRawReaderHLT::ReadHeader()
190 // see header file for class documentation
191 Bool_t result=kFALSE;
192 Bool_t firstParentCycle=fbHaveHLTData;
193 while (fbHaveHLTData=(fbHaveHLTData && ReadNextHLTData())) {
194 // all internal data variables set
195 assert(fpData!=NULL);
196 fHeader=reinterpret_cast<AliRawDataHeader*>(const_cast<AliHLTUInt8_t*>(fpData));
197 fOffset=sizeof(AliRawDataHeader);
199 if (result=IsSelected()) break;
201 firstParentCycle&=!fbHaveHLTData; // true if it just changed from true to false
203 if (firstParentCycle) {
204 firstParentCycle=kFALSE;
205 // reset and set the selection back to the original one
206 fpParentReader->Reset();
207 fpParentReader->SelectEquipment(fSelectEquipmentType, fSelectMinEquipmentId, fSelectMaxEquipmentId);
210 if (!(result=fpParentReader->ReadHeader())) {
214 fHeader=const_cast<AliRawDataHeader*>(fpParentReader->GetDataHeader());
216 // filter out all equipment ids which should be taken from the HLT stream
217 int id=fpParentReader->GetEquipmentId();
218 if (result=!IsHLTInput(id)) break;
223 Bool_t AliRawReaderHLT::ReadNextData(UChar_t*& data)
225 // see header file for class documentation
227 // this function is the backbone of the ReadNext functions, it gets the
228 // whole data block either from the HLT stream or the parent raw reader.
229 // Each call of ReadNextData directly jumps to the next data set.
232 // read new header if data already read
233 if (fPosition<fDataSize || (result=ReadHeader())) {
234 if (fbHaveHLTData && fpHLTOUT!=NULL) {
235 // all internal data variables set
237 data=const_cast<AliHLTUInt8_t*>(fpData+sizeof(AliRawDataHeader));
239 // no data in the HLT stream, read real data
240 //AliInfo(Form("read from parent reader: min=%d max=%d", fSelectMinEquipmentId, fSelectMaxEquipmentId));
243 result=fpParentReader->ReadNextData(data);
246 fDataSize=fpParentReader->GetDataSize();
254 fOffset=sizeof(AliRawDataHeader);
260 Bool_t AliRawReaderHLT::ReadNextInt(UInt_t& data)
262 // see header file for class documentation
263 int iCopy=sizeof(UInt_t);
266 if (fpData && (fDataSize-fOffset)>=iCopy) {
267 data=*reinterpret_cast<const UInt_t*>(fpData+fOffset);
271 } while (ReadNextData(dummy));
275 Bool_t AliRawReaderHLT::ReadNextShort(UShort_t& data)
277 // see header file for class documentation
278 int iCopy=sizeof(UShort_t);
281 if (fpData && (fDataSize-fOffset)>=iCopy) {
282 data=*reinterpret_cast<const UShort_t*>(fpData+fOffset);
286 } while (ReadNextData(dummy));
290 Bool_t AliRawReaderHLT::ReadNextChar(UChar_t& data)
292 // see header file for class documentation
293 int iCopy=sizeof(UChar_t);
296 if (fpData && (fDataSize-fOffset)>=iCopy) {
297 data=*reinterpret_cast<const UChar_t*>(fpData+fOffset);
301 } while (ReadNextData(dummy));
305 Bool_t AliRawReaderHLT::ReadNext(UChar_t* data, Int_t size)
307 // see header file for class documentation
310 if (fpData && (fDataSize-fOffset)>=size) {
311 // copy remaining data
312 int iCopy=fDataSize-fOffset;
313 if (iCopy>size) iCopy=size;
314 memcpy(data, fpData+fOffset, iCopy);
318 } while (ReadNextData(dummy));
322 Bool_t AliRawReaderHLT::Reset()
324 // see header file for class documentation
325 ReleaseHLTData(false/* keep HLTOUT instance */);
326 Bool_t result=fpParentReader->Reset();
329 // check if redirection is enabled for at least one detector in the selected range
330 fbHaveHLTData=EvaluateSelection();
332 // start reading HLTOUT data blocks from the beginning
338 Bool_t AliRawReaderHLT::NextEvent()
340 // see header file for class documentation
344 Bool_t result=fpParentReader->NextEvent();
352 Bool_t AliRawReaderHLT::RewindEvents()
354 // see header file for class documentation
357 return fpParentReader->RewindEvents();
360 void AliRawReaderHLT::Select(Int_t detectorID, Int_t minDDLID, Int_t maxDDLID)
362 // see header file for class documentation
363 AliRawReader::Select(detectorID, minDDLID, maxDDLID);
364 fpParentReader->Select(detectorID, minDDLID, maxDDLID);
365 fbHaveHLTData=EvaluateSelection();
368 // most likely we do not need this method since the base class directly forwards
370 // void AliRawReaderHLT::Select(const char *detectorName, Int_t minDDLID, Int_t maxDDLID)
372 // AliInfo(Form("detectorName=%s, minDDLID=%d, maxDDLID=%d", detectorName, minDDLID, maxDDLID));
373 // AliRawReader::Select(detectorName, minDDLID, maxDDLID);
374 // fpParentReader->Select(detectorName, minDDLID, maxDDLID);
377 void AliRawReaderHLT::SelectEquipment(Int_t equipmentType, Int_t minEquipmentId, Int_t maxEquipmentId)
379 // see header file for class documentation
381 //AliInfo(Form("equipmentType=%d, minEquipmentId=%d, maxEquipmentId=%d", equipmentType, minEquipmentId, maxEquipmentId));
382 AliRawReader::SelectEquipment(equipmentType, minEquipmentId, maxEquipmentId);
383 fpParentReader->SelectEquipment(equipmentType, minEquipmentId, maxEquipmentId);
384 fbHaveHLTData=EvaluateSelection();
387 void AliRawReaderHLT::SkipInvalid(Bool_t skip)
389 // see header file for class documentation
391 AliRawReader::SkipInvalid(skip);
392 fpParentReader->SkipInvalid(skip);
395 void AliRawReaderHLT::SelectEvents(Int_t type)
397 // see header file for class documentation
399 //AliInfo(Form("type=%d", type));
400 AliRawReader::SelectEvents(type);
401 fpParentReader->SelectEvents(type);
404 int AliRawReaderHLT::ScanOptions(const char* options)
406 // see header file for class documentation
408 TString optString(options);
411 TObjArray* pTokens=optString.Tokenize(" ");
413 int iEntries=pTokens->GetEntries();
414 for (int i =0; i<iEntries; i++) {
415 argument=((TObjString*)pTokens->At(i))->GetString();
416 // first scan all the other options
417 // no other options for the moment
419 // it must be a detector name
420 int detId=AliDAQ::DetectorID(argument.Data());
422 fDetectors.push_back(detId);
424 if (!fSystemOptions.IsNull()) fSystemOptions+=" ";
425 fSystemOptions+=argument;
434 Bool_t AliRawReaderHLT::ReadNextHLTData()
436 // see header file for class documentation
438 if (fbReadFirst || !fpHLTOUT) {
440 fpHLTOUT=new AliHLTOUTRawReader(fpParentReader);
441 if (result=(fpHLTOUT!=NULL)) {
442 AliHLTSystem* pSystem=GetInstance();
444 pSystem->ScanOptions(fSystemOptions.Data());
446 if (result=(fpHLTOUT->Init()>=0)) {
451 result=fpHLTOUT->SelectFirstDataBlock(kAliHLTAnyDataType, kAliHLTVoidDataSpec,
452 AliHLTModuleAgent::kRawReader)>=0;
456 // first release the data buffer
457 ReleaseHLTData(false /* keep HLTOUT instance */);
458 result=fpHLTOUT->SelectNextDataBlock()>=0;
461 AliHLTComponentDataType dt=kAliHLTVoidDataType;
462 AliHLTUInt32_t spec=kAliHLTVoidDataSpec;
463 fpHLTOUT->GetDataBlockDescription(dt, spec);
464 AliHLTUInt32_t size=0;
465 AliHLTOUTHandler* pHandler=fpHLTOUT->GetHandler();
467 if (dynamic_cast<AliHLTOUTHandlerEquId*>(pHandler)!=NULL) {
468 AliHLTOUT::AliHLTOUTLockGuard g(fpHLTOUT);
469 fEquipmentId=pHandler->ProcessData(fpHLTOUT);
471 fDataSize=pHandler->GetProcessedData(fpData);
473 result=fpHLTOUT->GetDataBuffer(fpData, size)>=0;
475 AliDebug(AliLog::kDebug, Form("forward data block from HLTOUT stream to equipment %d", fEquipmentId));
478 // remember the current handler in order to properly release the data buffer
479 fpDataHandler=pHandler;
480 AliDebug(AliLog::kDebug, Form("forward decoded data block provided by handler to equipment %d", fEquipmentId));
484 AliError(Form("handler is not of type AliHLTOUTHandlerEquId for block %x data type %s spec %#x; data block skipped",
485 fpHLTOUT->GetDataBlockIndex(), AliHLTComponent::DataType2Text(dt).c_str(), spec));
488 AliWarning(Form("no data handler found for block %x data type %s spec %#x; data block skipped",
489 fpHLTOUT->GetDataBlockIndex(), AliHLTComponent::DataType2Text(dt).c_str(), spec));
492 ReleaseHLTData(false /* keep HLTOUT instance */);
497 Bool_t AliRawReaderHLT::IsHLTInput(int ddlid)
499 // see header file for class documentation
500 vector<int>::iterator detector=fDetectors.begin();
501 for (; detector!=fDetectors.end(); detector++) {
502 int ddlOffset=AliDAQ::DdlIDOffset(*detector);
503 int nofDDLs=AliDAQ::NumberOfDdls(*detector);
504 if (ddlid>=ddlOffset && ddlid<ddlOffset+nofDDLs)
510 int AliRawReaderHLT::ReleaseHLTData(bool bReleaseHLTOUT)
512 // see header file for class documentation
514 if (fpDataHandler) fpDataHandler->ReleaseProcessedData(fpData, fDataSize);
515 else fpHLTOUT->ReleaseDataBuffer(fpData);
517 if (bReleaseHLTOUT) {
532 Bool_t AliRawReaderHLT::EvaluateSelection()
534 // see header file for class documentation
535 Bool_t bHaveHLTData=kFALSE;
536 if (bHaveHLTData=(fDetectors.size()>0)) {
537 vector<int>::iterator detector=fDetectors.begin();
538 for (; detector!=fDetectors.end(); detector++) {
539 int ddlOffset=AliDAQ::DdlIDOffset(*detector);
540 int nofDDLs=AliDAQ::NumberOfDdls(*detector);
541 if ((fSelectMinEquipmentId<0 || fSelectMinEquipmentId<ddlOffset+nofDDLs) &&
542 (fSelectMaxEquipmentId<0 || fSelectMaxEquipmentId>=ddlOffset))
545 bHaveHLTData=detector!=fDetectors.end();
550 AliRawReader* AliRawReaderHLTCreateInstance(AliRawReader* pParentReader, const char* options)
552 // see header file for class documentation
553 if (!pParentReader) return NULL;
554 return new AliRawReaderHLT(pParentReader, options);