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),
62 // see header file for class documentation
64 // refer to README to build package
66 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
71 AliRawReaderHLT::~AliRawReaderHLT()
73 // see header file for class documentation
77 UInt_t AliRawReaderHLT::GetType() const
79 // see header file for class documentation
80 return fpParentReader->GetType();
83 UInt_t AliRawReaderHLT::GetRunNumber() const
85 // see header file for class documentation
86 return fpParentReader->GetRunNumber();
89 const UInt_t* AliRawReaderHLT::GetEventId() const
91 // see header file for class documentation
92 return fpParentReader->GetEventId();
95 const UInt_t* AliRawReaderHLT::GetTriggerPattern() const
97 // see header file for class documentation
98 return fpParentReader->GetTriggerPattern();
101 const UInt_t* AliRawReaderHLT::GetDetectorPattern() const
103 // see header file for class documentation
104 return fpParentReader->GetDetectorPattern();
107 const UInt_t* AliRawReaderHLT::GetAttributes() const
109 // see header file for class documentation
110 return fpParentReader->GetAttributes();
113 const UInt_t* AliRawReaderHLT::GetSubEventAttributes() const
115 // see header file for class documentation
116 return fpParentReader->GetSubEventAttributes();
119 UInt_t AliRawReaderHLT::GetLDCId() const
121 // see header file for class documentation
122 return fpParentReader->GetLDCId();
125 UInt_t AliRawReaderHLT::GetGDCId() const
127 // see header file for class documentation
128 return fpParentReader->GetGDCId();
131 UInt_t AliRawReaderHLT::GetTimestamp() const
133 // see header file for class documentation
134 return fpParentReader->GetTimestamp();
137 const UInt_t* AliRawReaderHLT::GetEquipmentAttributes() const
139 // see header file for class documentation
140 return fpParentReader->GetEquipmentAttributes();
143 Int_t AliRawReaderHLT::GetEquipmentElementSize() const
145 // see header file for class documentation
146 // don't know what it really means, bu the AliRawReaderFile
148 // do the same if we have a valid equipment data set from
150 if (fEquipmentId>=0) return 0;
151 return fpParentReader->GetEquipmentElementSize();
154 Int_t AliRawReaderHLT::GetEquipmentHeaderSize() const
156 // see header file for class documentation
158 // equipment header means the additional data header?
159 // if we have a valid equipment data set from the HLT stream
160 // there is no additional header
161 if (fEquipmentId>=0) return 0;
162 return fpParentReader->GetEquipmentHeaderSize();
165 Int_t AliRawReaderHLT::GetEquipmentSize() const
167 // see header file for class documentation
168 if (fEquipmentId>=0) return fDataSize+sizeof(AliRawDataHeader);
169 return fpParentReader->GetEquipmentSize();
172 Int_t AliRawReaderHLT::GetEquipmentType() const
174 // see header file for class documentation
175 return fpParentReader->GetEquipmentType();
178 Int_t AliRawReaderHLT::GetEquipmentId() const
180 // see header file for class documentation
182 if (fEquipmentId>=0) id=fEquipmentId;
183 else id=fpParentReader->GetEquipmentId();
187 Bool_t AliRawReaderHLT::ReadHeader()
189 // see header file for class documentation
190 Bool_t result=kFALSE;
191 Bool_t firstParentCycle=fbHaveHLTData;
192 while (fbHaveHLTData=(fbHaveHLTData && ReadNextHLTData())) {
193 // all internal data variables set
194 assert(fpData!=NULL);
195 fHeader=reinterpret_cast<AliRawDataHeader*>(const_cast<AliHLTUInt8_t*>(fpData));
196 fOffset=sizeof(AliRawDataHeader);
198 if (result=IsSelected()) break;
200 firstParentCycle&=!fbHaveHLTData; // true if it just changed from true to false
202 if (firstParentCycle) {
203 firstParentCycle=kFALSE;
204 // reset and set the selection back to the original one
205 fpParentReader->Reset();
206 fpParentReader->SelectEquipment(fSelectEquipmentType, fSelectMinEquipmentId, fSelectMaxEquipmentId);
209 if (!(result=fpParentReader->ReadHeader())) {
213 fHeader=const_cast<AliRawDataHeader*>(fpParentReader->GetDataHeader());
215 // filter out all equipment ids which should be taken from the HLT stream
216 int id=fpParentReader->GetEquipmentId();
217 if (result=!IsHLTInput(id)) break;
222 Bool_t AliRawReaderHLT::ReadNextData(UChar_t*& data)
224 // see header file for class documentation
226 // this function is the backbone of the ReadNext functions, it gets the
227 // whole data block either from the HLT stream or the parent raw reader.
228 // Each call of ReadNextData directly jumps to the next data set.
231 // read new header if data already read
232 if (fPosition<fDataSize || (result=ReadHeader())) {
233 if (fpHLTOUT!=NULL) {
234 // all internal data variables set
236 data=const_cast<AliHLTUInt8_t*>(fpData+sizeof(AliRawDataHeader));
238 // no data in the HLT stream, read real data
239 //AliInfo(Form("read from parent reader: min=%d max=%d", fSelectMinEquipmentId, fSelectMaxEquipmentId));
242 result=fpParentReader->ReadNextData(data);
245 fDataSize=fpParentReader->GetDataSize();
253 fOffset=sizeof(AliRawDataHeader);
259 Bool_t AliRawReaderHLT::ReadNextInt(UInt_t& data)
261 // see header file for class documentation
262 int iCopy=sizeof(UInt_t);
265 if (fpData && (fDataSize-fOffset)>=iCopy) {
266 data=*reinterpret_cast<const UInt_t*>(fpData+fOffset);
270 } while (ReadNextData(dummy));
274 Bool_t AliRawReaderHLT::ReadNextShort(UShort_t& data)
276 // see header file for class documentation
277 int iCopy=sizeof(UShort_t);
280 if (fpData && (fDataSize-fOffset)>=iCopy) {
281 data=*reinterpret_cast<const UShort_t*>(fpData+fOffset);
285 } while (ReadNextData(dummy));
289 Bool_t AliRawReaderHLT::ReadNextChar(UChar_t& data)
291 // see header file for class documentation
292 int iCopy=sizeof(UChar_t);
295 if (fpData && (fDataSize-fOffset)>=iCopy) {
296 data=*reinterpret_cast<const UChar_t*>(fpData+fOffset);
300 } while (ReadNextData(dummy));
304 Bool_t AliRawReaderHLT::ReadNext(UChar_t* data, Int_t size)
306 // see header file for class documentation
309 if (fpData && (fDataSize-fOffset)>=size) {
310 // copy remaining data
311 int iCopy=fDataSize-fOffset;
312 if (iCopy>size) iCopy=size;
313 memcpy(data, fpData+fOffset, iCopy);
317 } while (ReadNextData(dummy));
321 Bool_t AliRawReaderHLT::Reset()
323 // see header file for class documentation
325 Bool_t result=fpParentReader->Reset();
328 // check if redirection is enabled for at least one detector in the selected range
329 fbHaveHLTData=EvaluateSelection();
334 Bool_t AliRawReaderHLT::NextEvent()
336 // see header file for class documentation
337 Bool_t result=fpParentReader->NextEvent();
345 Bool_t AliRawReaderHLT::RewindEvents()
347 // see header file for class documentation
350 return fpParentReader->RewindEvents();
353 void AliRawReaderHLT::Select(Int_t detectorID, Int_t minDDLID, Int_t maxDDLID)
355 // see header file for class documentation
356 AliRawReader::Select(detectorID, minDDLID, maxDDLID);
357 fpParentReader->Select(detectorID, minDDLID, maxDDLID);
358 fbHaveHLTData=EvaluateSelection();
361 // most likely we do not need this method since the base class directly forwards
363 // void AliRawReaderHLT::Select(const char *detectorName, Int_t minDDLID, Int_t maxDDLID)
365 // AliInfo(Form("detectorName=%s, minDDLID=%d, maxDDLID=%d", detectorName, minDDLID, maxDDLID));
366 // AliRawReader::Select(detectorName, minDDLID, maxDDLID);
367 // fpParentReader->Select(detectorName, minDDLID, maxDDLID);
370 void AliRawReaderHLT::SelectEquipment(Int_t equipmentType, Int_t minEquipmentId, Int_t maxEquipmentId)
372 // see header file for class documentation
374 //AliInfo(Form("equipmentType=%d, minEquipmentId=%d, maxEquipmentId=%d", equipmentType, minEquipmentId, maxEquipmentId));
375 AliRawReader::SelectEquipment(equipmentType, minEquipmentId, maxEquipmentId);
376 fpParentReader->SelectEquipment(equipmentType, minEquipmentId, maxEquipmentId);
377 fbHaveHLTData=EvaluateSelection();
380 void AliRawReaderHLT::SkipInvalid(Bool_t skip)
382 // see header file for class documentation
384 AliRawReader::SkipInvalid(skip);
385 fpParentReader->SkipInvalid(skip);
388 void AliRawReaderHLT::SelectEvents(Int_t type)
390 // see header file for class documentation
392 //AliInfo(Form("type=%d", type));
393 AliRawReader::SelectEvents(type);
394 fpParentReader->SelectEvents(type);
397 int AliRawReaderHLT::ScanOptions(const char* options)
399 // see header file for class documentation
401 TString optString(options);
404 TObjArray* pTokens=optString.Tokenize(" ");
406 int iEntries=pTokens->GetEntries();
407 for (int i =0; i<iEntries; i++) {
408 argument=((TObjString*)pTokens->At(i))->GetString();
409 // first scan all the other options
410 // no other options for the moment
412 // it must be a detector name
413 int detId=AliDAQ::DetectorID(argument.Data());
415 fDetectors.push_back(detId);
417 if (!fSystemOptions.IsNull()) fSystemOptions+=" ";
418 fSystemOptions+=argument;
427 Bool_t AliRawReaderHLT::ReadNextHLTData()
429 // see header file for class documentation
432 fpHLTOUT=new AliHLTOUTRawReader(fpParentReader);
433 if (result=(fpHLTOUT!=NULL)) {
434 AliHLTSystem* pSystem=GetInstance();
436 pSystem->ScanOptions(fSystemOptions.Data());
438 if (result=(fpHLTOUT->Init()>=0)) {
439 result=fpHLTOUT->SelectFirstDataBlock(kAliHLTAnyDataType, kAliHLTVoidDataSpec,
440 AliHLTModuleAgent::kRawReader)>=0;
444 // first release the data buffer
445 ReleaseHLTData(false /* keep HLTOUT instance */);
446 result=fpHLTOUT->SelectNextDataBlock()>=0;
449 AliHLTComponentDataType dt=kAliHLTVoidDataType;
450 AliHLTUInt32_t spec=kAliHLTVoidDataSpec;
451 fpHLTOUT->GetDataBlockDescription(dt, spec);
452 AliHLTUInt32_t size=0;
453 AliHLTOUTHandler* pHandler=fpHLTOUT->GetHandler();
455 if (dynamic_cast<AliHLTOUTHandlerEquId*>(pHandler)!=NULL) {
456 AliHLTOUT::AliHLTOUTLockGuard g(fpHLTOUT);
457 fEquipmentId=pHandler->ProcessData(fpHLTOUT);
459 fDataSize=pHandler->GetProcessedData(fpData);
461 result=fpHLTOUT->GetDataBuffer(fpData, size)>=0;
462 AliDebug(AliLog::kDebug, Form("forward data block from HLTOUT stream to equipment %d", fEquipmentId));
465 // remember the current handler in order to properly release the data buffer
466 fpDataHandler=pHandler;
467 AliDebug(AliLog::kDebug, Form("forward decoded data block provided by handler to equipment %d", fEquipmentId));
471 AliError(Form("handler is not of type AliHLTOUTHandlerEquId for block %x data type %s spec %#x; data block skipped",
472 fpHLTOUT->GetDataBlockIndex(), AliHLTComponent::DataType2Text(dt).c_str(), spec));
475 AliWarning(Form("no data handler found for block %x data type %s spec %#x; data block skipped",
476 fpHLTOUT->GetDataBlockIndex(), AliHLTComponent::DataType2Text(dt).c_str(), spec));
484 Bool_t AliRawReaderHLT::IsHLTInput(int ddlid)
486 // see header file for class documentation
487 vector<int>::iterator detector=fDetectors.begin();
488 for (; detector!=fDetectors.end(); detector++) {
489 int ddlOffset=AliDAQ::DdlIDOffset(*detector);
490 int nofDDLs=AliDAQ::NumberOfDdls(*detector);
491 if (ddlid>=ddlOffset && ddlid<ddlOffset+nofDDLs)
497 int AliRawReaderHLT::ReleaseHLTData(bool bReleaseHLTOUT)
499 // see header file for class documentation
501 if (fpDataHandler) fpDataHandler->ReleaseProcessedData(fpData, fDataSize);
502 else fpHLTOUT->ReleaseDataBuffer(fpData);
504 if (bReleaseHLTOUT) {
519 Bool_t AliRawReaderHLT::EvaluateSelection()
521 // see header file for class documentation
522 Bool_t bHaveHLTData=kFALSE;
523 if (bHaveHLTData=(fDetectors.size()>0)) {
524 vector<int>::iterator detector=fDetectors.begin();
525 for (; detector!=fDetectors.end(); detector++) {
526 int ddlOffset=AliDAQ::DdlIDOffset(*detector);
527 int nofDDLs=AliDAQ::NumberOfDdls(*detector);
528 if ((fSelectMinEquipmentId<0 || fSelectMinEquipmentId<ddlOffset+nofDDLs) &&
529 (fSelectMaxEquipmentId<0 || fSelectMaxEquipmentId>=ddlOffset))
532 bHaveHLTData=detector!=fDetectors.end();
537 AliRawReader* AliRawReaderHLTCreateInstance(AliRawReader* pParentReader, const char* options)
539 // see header file for class documentation
540 if (!pParentReader) return NULL;
541 return new AliRawReaderHLT(pParentReader, options);