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"
37 #include "AliHLTPluginBase.h"
39 #include "AliDAQ.h" // RAW, for detector names and equipment ids
40 #include "TObjString.h"
43 /** ROOT macro for the implementation of ROOT specific class methods */
44 ClassImp(AliRawReaderHLT)
46 AliRawReaderHLT::AliRawReaderHLT(AliRawReader* pRawreader, const char* options)
49 fpParentReader(pRawreader),
62 fpPluginBase(new AliHLTPluginBase)
64 // see header file for class documentation
66 // refer to README to build package
68 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
73 AliRawReaderHLT::~AliRawReaderHLT()
75 // see header file for class documentation
77 if (fpPluginBase) delete fpPluginBase;
81 UInt_t AliRawReaderHLT::GetType() const
83 // see header file for class documentation
84 return fpParentReader->GetType();
87 UInt_t AliRawReaderHLT::GetRunNumber() const
89 // see header file for class documentation
90 return fpParentReader->GetRunNumber();
93 const UInt_t* AliRawReaderHLT::GetEventId() const
95 // see header file for class documentation
96 return fpParentReader->GetEventId();
99 const UInt_t* AliRawReaderHLT::GetTriggerPattern() const
101 // see header file for class documentation
102 return fpParentReader->GetTriggerPattern();
105 const UInt_t* AliRawReaderHLT::GetDetectorPattern() const
107 // see header file for class documentation
108 return fpParentReader->GetDetectorPattern();
111 const UInt_t* AliRawReaderHLT::GetAttributes() const
113 // see header file for class documentation
114 return fpParentReader->GetAttributes();
117 const UInt_t* AliRawReaderHLT::GetSubEventAttributes() const
119 // see header file for class documentation
120 return fpParentReader->GetSubEventAttributes();
123 UInt_t AliRawReaderHLT::GetLDCId() const
125 // see header file for class documentation
126 return fpParentReader->GetLDCId();
129 UInt_t AliRawReaderHLT::GetGDCId() const
131 // see header file for class documentation
132 return fpParentReader->GetGDCId();
135 UInt_t AliRawReaderHLT::GetTimestamp() const
137 // see header file for class documentation
138 return fpParentReader->GetTimestamp();
141 const UInt_t* AliRawReaderHLT::GetEquipmentAttributes() const
143 // see header file for class documentation
144 return fpParentReader->GetEquipmentAttributes();
147 Int_t AliRawReaderHLT::GetEquipmentElementSize() const
149 // see header file for class documentation
150 // don't know what it really means, bu the AliRawReaderFile
152 // do the same if we have a valid equipment data set from
154 if (fEquipmentId>=0) return 0;
155 return fpParentReader->GetEquipmentElementSize();
158 Int_t AliRawReaderHLT::GetEquipmentHeaderSize() const
160 // see header file for class documentation
162 // equipment header means the additional data header?
163 // if we have a valid equipment data set from the HLT stream
164 // there is no additional header
165 if (fEquipmentId>=0) return 0;
166 return fpParentReader->GetEquipmentHeaderSize();
169 Int_t AliRawReaderHLT::GetEquipmentSize() const
171 // see header file for class documentation
172 if (fEquipmentId>=0) return fDataSize+sizeof(AliRawDataHeader);
173 return fpParentReader->GetEquipmentSize();
176 Int_t AliRawReaderHLT::GetEquipmentType() const
178 // see header file for class documentation
179 return fpParentReader->GetEquipmentType();
182 Int_t AliRawReaderHLT::GetEquipmentId() const
184 // see header file for class documentation
186 if (fEquipmentId>=0) id=fEquipmentId;
187 else id=fpParentReader->GetEquipmentId();
191 Bool_t AliRawReaderHLT::ReadHeader()
193 // see header file for class documentation
194 Bool_t result=kFALSE;
195 Bool_t firstParentCycle=fbHaveHLTData;
196 while ((fbHaveHLTData=(fbHaveHLTData && ReadNextHLTData()))) {
197 // all internal data variables set
198 assert(fpData!=NULL);
199 fHeader=reinterpret_cast<AliRawDataHeader*>(const_cast<AliHLTUInt8_t*>(fpData));
200 fOffset=sizeof(AliRawDataHeader);
202 if ((result=IsSelected())) break;
204 firstParentCycle&=!fbHaveHLTData; // true if it just changed from true to false
206 if (firstParentCycle) {
207 firstParentCycle=kFALSE;
208 // reset and set the selection back to the original one
209 fpParentReader->Reset();
210 fpParentReader->SelectEquipment(fSelectEquipmentType, fSelectMinEquipmentId, fSelectMaxEquipmentId);
213 if (!(result=fpParentReader->ReadHeader())) {
217 fHeader=const_cast<AliRawDataHeader*>(fpParentReader->GetDataHeader());
218 fDataSize=fpParentReader->GetDataSize();
222 // filter out all equipment ids which should be taken from the HLT stream
223 int id=fpParentReader->GetEquipmentId();
224 if ((result=!IsHLTInput(id))) break;
229 Bool_t AliRawReaderHLT::ReadNextData(UChar_t*& data)
231 // see header file for class documentation
232 return ReadNextData(data, kTRUE);
235 Bool_t AliRawReaderHLT::ReadNextData(UChar_t*& data, Bool_t readHeader)
237 // see header file for class documentation
239 // this function is the backbone of the ReadNext functions, it gets the
240 // whole data block either from the HLT stream or the parent raw reader.
241 // Each call of ReadNextData directly jumps to the next data set.
244 // read new header if data already read
245 if (fPosition<fDataSize || (result=(readHeader && ReadHeader()))) {
246 if (fbHaveHLTData && fpHLTOUT!=NULL) {
247 // all internal data variables set
249 data=const_cast<AliHLTUInt8_t*>(fpData+sizeof(AliRawDataHeader));
250 // fpData includes the CDH, set offset behind CDH
251 fOffset=sizeof(AliRawDataHeader);
253 // no data in the HLT stream, read real data
254 //AliInfo(Form("read from parent reader: min=%d max=%d", fSelectMinEquipmentId, fSelectMaxEquipmentId));
257 result=fpParentReader->ReadNextData(data);
260 fDataSize=fpParentReader->GetDataSize();
261 // fpData is without CDH
275 Bool_t AliRawReaderHLT::ReadNextInt(UInt_t& data)
277 // see header file for class documentation
278 int iCopy=sizeof(UInt_t);
281 if (fpData && (fDataSize-fOffset)>=iCopy) {
282 data=*reinterpret_cast<const UInt_t*>(fpData+fOffset);
286 } while (ReadNextData(dummy, kTRUE));
290 Bool_t AliRawReaderHLT::ReadNextShort(UShort_t& data)
292 // see header file for class documentation
293 int iCopy=sizeof(UShort_t);
296 if (fpData && (fDataSize-fOffset)>=iCopy) {
297 data=*reinterpret_cast<const UShort_t*>(fpData+fOffset);
301 } while (ReadNextData(dummy, kTRUE));
305 Bool_t AliRawReaderHLT::ReadNextChar(UChar_t& data)
307 // see header file for class documentation
308 int iCopy=sizeof(UChar_t);
311 if (fpData && (fDataSize-fOffset)>=iCopy) {
312 data=*reinterpret_cast<const UChar_t*>(fpData+fOffset);
316 } while (ReadNextData(dummy, kTRUE));
320 Bool_t AliRawReaderHLT::ReadNext(UChar_t* data, Int_t size)
322 // see header file for class documentation
325 if (fpData && (fDataSize-fOffset)>=size) {
326 // copy remaining data
327 int iCopy=fDataSize-fOffset;
328 if (iCopy>size) iCopy=size;
329 memcpy(data, fpData+fOffset, iCopy);
333 // By convention, the ReadNextData function stays in the
334 // current block and does not switch to the next one
335 // automatically -> kFALSE
336 } while (ReadNextData(dummy, kFALSE));
340 Bool_t AliRawReaderHLT::Reset()
342 // see header file for class documentation
343 ReleaseHLTData(false/* keep HLTOUT instance */);
344 Bool_t result=fpParentReader->Reset();
347 // check if redirection is enabled for at least one detector in the selected range
348 fbHaveHLTData=EvaluateSelection();
350 // start reading HLTOUT data blocks from the beginning
356 Bool_t AliRawReaderHLT::NextEvent()
358 // see header file for class documentation
362 Bool_t result=fpParentReader->NextEvent();
370 Bool_t AliRawReaderHLT::RewindEvents()
372 // see header file for class documentation
375 return fpParentReader->RewindEvents();
378 void AliRawReaderHLT::Select(Int_t detectorID, Int_t minDDLID, Int_t maxDDLID)
380 // see header file for class documentation
381 AliRawReader::Select(detectorID, minDDLID, maxDDLID);
382 fpParentReader->Select(detectorID, minDDLID, maxDDLID);
383 fbHaveHLTData=EvaluateSelection();
386 // most likely we do not need this method since the base class directly forwards
388 // void AliRawReaderHLT::Select(const char *detectorName, Int_t minDDLID, Int_t maxDDLID)
390 // AliInfo(Form("detectorName=%s, minDDLID=%d, maxDDLID=%d", detectorName, minDDLID, maxDDLID));
391 // AliRawReader::Select(detectorName, minDDLID, maxDDLID);
392 // fpParentReader->Select(detectorName, minDDLID, maxDDLID);
395 void AliRawReaderHLT::SelectEquipment(Int_t equipmentType, Int_t minEquipmentId, Int_t maxEquipmentId)
397 // see header file for class documentation
399 //AliInfo(Form("equipmentType=%d, minEquipmentId=%d, maxEquipmentId=%d", equipmentType, minEquipmentId, maxEquipmentId));
400 AliRawReader::SelectEquipment(equipmentType, minEquipmentId, maxEquipmentId);
401 fpParentReader->SelectEquipment(equipmentType, minEquipmentId, maxEquipmentId);
402 fbHaveHLTData=EvaluateSelection();
405 void AliRawReaderHLT::SkipInvalid(Bool_t skip)
407 // see header file for class documentation
409 AliRawReader::SkipInvalid(skip);
410 fpParentReader->SkipInvalid(skip);
414 void AliRawReaderHLT::SelectEvents(Int_t type)
416 // see header file for class documentation
418 //AliInfo(Form("type=%d", type));
419 AliRawReader::SelectEvents(type);
420 fpParentReader->SelectEvents(type);
424 int AliRawReaderHLT::ScanOptions(const char* options)
426 // see header file for class documentation
428 TString optString(options);
432 TObjArray* pTokens=optString.Tokenize(" ");
434 int iEntries=pTokens->GetEntries();
435 for (int i =0; i<iEntries; i++) {
436 argument=((TObjString*)pTokens->At(i))->GetString();
437 // first scan all the other options
438 // no other options for the moment
440 // it must be a detector name
441 int detId=AliDAQ::DetectorID(argument.Data());
443 fDetectors.push_back(detId);
444 if (!detectors.IsNull()) detectors+=" ";
447 if (!fSystemOptions.IsNull()) fSystemOptions+=" ";
448 fSystemOptions+=argument;
454 if (iResult>=0 && !detectors.IsNull()) {
455 AliInfo(Form("running reconstruction from HLT data: %s", detectors.Data()));
460 Bool_t AliRawReaderHLT::ReadNextHLTData()
462 // see header file for class documentation
464 if (fbReadFirst || !fpHLTOUT) {
466 fpHLTOUT=new AliHLTOUTRawReader(fpParentReader);
467 if ((result=(fpHLTOUT!=NULL))) {
469 AliFatal("internal data error: can not get AliHLTSystem instance from plugin");
472 AliHLTSystem* pSystem=fpPluginBase->GetInstance();
474 pSystem->ScanOptions(fSystemOptions.Data());
476 if ((result=(fpHLTOUT->Init())>=0)) {
481 result=fpHLTOUT->SelectFirstDataBlock(kAliHLTAnyDataType, kAliHLTVoidDataSpec,
482 AliHLTModuleAgent::kRawReader)>=0;
486 // first release the data buffer
487 ReleaseHLTData(false /* keep HLTOUT instance */);
488 result=fpHLTOUT->SelectNextDataBlock()>=0;
491 AliHLTComponentDataType dt=kAliHLTVoidDataType;
492 AliHLTUInt32_t spec=kAliHLTVoidDataSpec;
493 fpHLTOUT->GetDataBlockDescription(dt, spec);
494 AliHLTUInt32_t size=0;
495 AliHLTOUTHandler* pHandler=fpHLTOUT->GetHandler();
497 if (dynamic_cast<AliHLTOUTHandlerEquId*>(pHandler)!=NULL) {
498 AliHLTOUT::AliHLTOUTSelectionGuard g(fpHLTOUT);
499 fEquipmentId=pHandler->ProcessData(fpHLTOUT);
501 fDataSize=pHandler->GetProcessedData(fpData);
503 result=fpHLTOUT->GetDataBuffer(fpData, size)>=0;
505 AliDebug(AliLog::kDebug, Form("forward data block from HLTOUT stream to equipment %d", fEquipmentId));
508 // remember the current handler in order to properly release the data buffer
509 fpDataHandler=pHandler;
510 AliDebug(AliLog::kDebug, Form("forward decoded data block provided by handler to equipment %d", fEquipmentId));
514 AliError(Form("handler is not of type AliHLTOUTHandlerEquId for block %x data type %s spec %#x; data block skipped",
515 fpHLTOUT->GetDataBlockIndex(), AliHLTComponent::DataType2Text(dt).c_str(), spec));
518 AliWarning(Form("no data handler found for block %x data type %s spec %#x; data block skipped",
519 fpHLTOUT->GetDataBlockIndex(), AliHLTComponent::DataType2Text(dt).c_str(), spec));
522 ReleaseHLTData(false /* keep HLTOUT instance */);
527 Bool_t AliRawReaderHLT::IsHLTInput(int ddlid)
529 // see header file for class documentation
530 vector<int>::iterator detector=fDetectors.begin();
531 for (; detector!=fDetectors.end(); detector++) {
532 int ddlOffset=AliDAQ::DdlIDOffset(*detector);
533 int nofDDLs=AliDAQ::NumberOfDdls(*detector);
534 if (ddlid>=ddlOffset && ddlid<ddlOffset+nofDDLs)
540 int AliRawReaderHLT::ReleaseHLTData(bool bReleaseHLTOUT)
542 // see header file for class documentation
544 if (fpDataHandler) fpDataHandler->ReleaseProcessedData(fpData, fDataSize);
545 else fpHLTOUT->ReleaseDataBuffer(fpData);
547 if (bReleaseHLTOUT) {
562 Bool_t AliRawReaderHLT::EvaluateSelection()
564 // see header file for class documentation
565 Bool_t bHaveHLTData=kFALSE;
566 if ((bHaveHLTData=(fDetectors.size())>0)) {
567 vector<int>::iterator detector=fDetectors.begin();
568 for (; detector!=fDetectors.end(); detector++) {
569 int ddlOffset=AliDAQ::DdlIDOffset(*detector);
570 int nofDDLs=AliDAQ::NumberOfDdls(*detector);
571 if ((fSelectMinEquipmentId<0 || fSelectMinEquipmentId<ddlOffset+nofDDLs) &&
572 (fSelectMaxEquipmentId<0 || fSelectMaxEquipmentId>=ddlOffset))
575 bHaveHLTData=detector!=fDetectors.end();
580 AliRawReader* AliRawReaderHLTCreateInstance(AliRawReader* pParentReader, const char* options)
582 // see header file for class documentation
583 if (!pParentReader) return NULL;
584 return new AliRawReaderHLT(pParentReader, options);