Adding AliTPCcalibV0 to the Makefile (Marian)
[u/mrichter/AliRoot.git] / HLT / rec / AliRawReaderHLT.cxx
CommitLineData
7e914051 1// $Id$
9c7b5023 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 AliRawReaderHLT.cxx
20 @author Matthias Richter
21 @date
22 @brief AliRawReader implementation which replaces original input of
23 detectors with the appropriate HLT output. */
24
25// see header file for class documentation
26// or
27// refer to README to build package
28// or
29// visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
30
31#include "AliRawReaderHLT.h"
13b78f4c 32#include "AliHLTOUTRawReader.h"
33#include "AliHLTModuleAgent.h"
626bfcc1 34#include "AliHLTOUTHandler.h"
35#include "AliHLTOUTHandlerEquId.h"
a3ef3c1d 36#include "AliHLTSystem.h"
e3917543 37#include "AliLog.h"
13b78f4c 38#include "AliDAQ.h" // RAW, for detector names and equipment ids
39#include "TObjString.h"
40#include <cassert>
9c7b5023 41
42/** ROOT macro for the implementation of ROOT specific class methods */
43ClassImp(AliRawReaderHLT)
44
45AliRawReaderHLT::AliRawReaderHLT(AliRawReader* pRawreader, const char* options)
46 :
47 AliRawReader(),
a3ef3c1d 48 AliHLTReconstructorBase(),
9c7b5023 49 fpParentReader(pRawreader),
13b78f4c 50 fOptions(),
a3ef3c1d 51 fSystemOptions(),
13b78f4c 52 fpData(NULL),
53 fDataSize(0),
54 fOffset(0),
d3833f99 55 fPosition(0),
13b78f4c 56 fEquipmentId(-1),
57 fbHaveHLTData(false),
58 fDetectors(),
626bfcc1 59 fpHLTOUT(NULL),
60 fpDataHandler(NULL)
9c7b5023 61{
62 // see header file for class documentation
63 // or
64 // refer to README to build package
65 // or
66 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
67 fOptions=options;
13b78f4c 68 ScanOptions(options);
9c7b5023 69}
70
71AliRawReaderHLT::~AliRawReaderHLT()
72{
73 // see header file for class documentation
d3833f99 74 ReleaseHLTData();
9c7b5023 75}
76
77UInt_t AliRawReaderHLT::GetType() const
78{
79 // see header file for class documentation
80 return fpParentReader->GetType();
81}
82
83UInt_t AliRawReaderHLT::GetRunNumber() const
84{
85 // see header file for class documentation
86 return fpParentReader->GetRunNumber();
87}
88
89const UInt_t* AliRawReaderHLT::GetEventId() const
90{
91 // see header file for class documentation
92 return fpParentReader->GetEventId();
93}
94
95const UInt_t* AliRawReaderHLT::GetTriggerPattern() const
96{
97 // see header file for class documentation
98 return fpParentReader->GetTriggerPattern();
99}
100
101const UInt_t* AliRawReaderHLT::GetDetectorPattern() const
102{
103 // see header file for class documentation
104 return fpParentReader->GetDetectorPattern();
105}
106
107const UInt_t* AliRawReaderHLT::GetAttributes() const
108{
109 // see header file for class documentation
110 return fpParentReader->GetAttributes();
111}
112
113const UInt_t* AliRawReaderHLT::GetSubEventAttributes() const
114{
115 // see header file for class documentation
116 return fpParentReader->GetSubEventAttributes();
117}
118
119UInt_t AliRawReaderHLT::GetLDCId() const
120{
121 // see header file for class documentation
122 return fpParentReader->GetLDCId();
123}
124
125UInt_t AliRawReaderHLT::GetGDCId() const
126{
127 // see header file for class documentation
128 return fpParentReader->GetGDCId();
129}
130
131UInt_t AliRawReaderHLT::GetTimestamp() const
132{
133 // see header file for class documentation
134 return fpParentReader->GetTimestamp();
135}
136
137const UInt_t* AliRawReaderHLT::GetEquipmentAttributes() const
138{
139 // see header file for class documentation
140 return fpParentReader->GetEquipmentAttributes();
141}
142
143Int_t AliRawReaderHLT::GetEquipmentElementSize() const
144{
145 // see header file for class documentation
13b78f4c 146 // don't know what it really means, bu the AliRawReaderFile
147 // just sets it to 0
148 // do the same if we have a valid equipment data set from
149 // the HLT stream
150 if (fEquipmentId>=0) return 0;
9c7b5023 151 return fpParentReader->GetEquipmentElementSize();
152}
153
154Int_t AliRawReaderHLT::GetEquipmentHeaderSize() const
155{
156 // see header file for class documentation
13b78f4c 157
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;
9c7b5023 162 return fpParentReader->GetEquipmentHeaderSize();
163}
164
165Int_t AliRawReaderHLT::GetEquipmentSize() const
166{
167 // see header file for class documentation
13b78f4c 168 if (fEquipmentId>=0) return fDataSize+sizeof(AliRawDataHeader);
9c7b5023 169 return fpParentReader->GetEquipmentSize();
170}
171
172Int_t AliRawReaderHLT::GetEquipmentType() const
173{
174 // see header file for class documentation
175 return fpParentReader->GetEquipmentType();
176}
177
178Int_t AliRawReaderHLT::GetEquipmentId() const
179{
180 // see header file for class documentation
13b78f4c 181 Int_t id=-1;
182 if (fEquipmentId>=0) id=fEquipmentId;
183 else id=fpParentReader->GetEquipmentId();
e3917543 184 return id;
9c7b5023 185}
186
187Bool_t AliRawReaderHLT::ReadHeader()
188{
189 // see header file for class documentation
e94eb049 190 Bool_t result=kFALSE;
d3833f99 191 Bool_t firstParentCycle=fbHaveHLTData;
192 while (fbHaveHLTData=(fbHaveHLTData && ReadNextHLTData())) {
e94eb049 193 // all internal data variables set
194 assert(fpData!=NULL);
195 fHeader=reinterpret_cast<AliRawDataHeader*>(const_cast<AliHLTUInt8_t*>(fpData));
d3833f99 196 fOffset=sizeof(AliRawDataHeader);
197 fPosition=fOffset;
e94eb049 198 if (result=IsSelected()) break;
199 }
d3833f99 200 firstParentCycle&=!fbHaveHLTData; // true if it just changed from true to false
201 while (!result) {
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);
207 }
e94eb049 208
d3833f99 209 if (!(result=fpParentReader->ReadHeader())) {
210 fHeader=NULL;
211 break;
212 }
e94eb049 213 fHeader=const_cast<AliRawDataHeader*>(fpParentReader->GetDataHeader());
d3833f99 214
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;
e94eb049 218 }
e3917543 219 return result;
9c7b5023 220}
221
222Bool_t AliRawReaderHLT::ReadNextData(UChar_t*& data)
223{
224 // see header file for class documentation
13b78f4c 225
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.
d3833f99 229 Bool_t result=kTRUE;
230
231 // read new header if data already read
232 if (fPosition<fDataSize || (result=ReadHeader())) {
233 if (fpHLTOUT!=NULL) {
234 // all internal data variables set
235 result=kTRUE;
236 data=const_cast<AliHLTUInt8_t*>(fpData+sizeof(AliRawDataHeader));
13b78f4c 237 } else {
d3833f99 238 // no data in the HLT stream, read real data
239 //AliInfo(Form("read from parent reader: min=%d max=%d", fSelectMinEquipmentId, fSelectMaxEquipmentId));
240
241 // read data
242 result=fpParentReader->ReadNextData(data);
243 if (result) {
244 fpData=data;
245 fDataSize=fpParentReader->GetDataSize();
246 } else {
247 fpData=NULL;
248 fDataSize=0;
249 }
250
251 fEquipmentId=-1;
13b78f4c 252 }
d3833f99 253 fOffset=sizeof(AliRawDataHeader);
254 fPosition=fDataSize;
13b78f4c 255 }
e3917543 256 return result;
9c7b5023 257}
258
259Bool_t AliRawReaderHLT::ReadNextInt(UInt_t& data)
260{
261 // see header file for class documentation
13b78f4c 262 int iCopy=sizeof(UInt_t);
263 UChar_t* dummy=NULL;
264 do {
265 if (fpData && (fDataSize-fOffset)>=iCopy) {
266 data=*reinterpret_cast<const UInt_t*>(fpData+fOffset);
267 fOffset+=iCopy;
268 return kTRUE;
269 }
270 } while (ReadNextData(dummy));
271 return kFALSE;
9c7b5023 272}
273
274Bool_t AliRawReaderHLT::ReadNextShort(UShort_t& data)
275{
276 // see header file for class documentation
13b78f4c 277 int iCopy=sizeof(UShort_t);
278 UChar_t* dummy=NULL;
279 do {
280 if (fpData && (fDataSize-fOffset)>=iCopy) {
281 data=*reinterpret_cast<const UShort_t*>(fpData+fOffset);
282 fOffset+=iCopy;
283 return kTRUE;
284 }
285 } while (ReadNextData(dummy));
286 return kFALSE;
9c7b5023 287}
288
289Bool_t AliRawReaderHLT::ReadNextChar(UChar_t& data)
290{
291 // see header file for class documentation
13b78f4c 292 int iCopy=sizeof(UChar_t);
293 UChar_t* dummy=NULL;
294 do {
295 if (fpData && (fDataSize-fOffset)>=iCopy) {
296 data=*reinterpret_cast<const UChar_t*>(fpData+fOffset);
297 fOffset+=iCopy;
298 return kTRUE;
299 }
300 } while (ReadNextData(dummy));
301 return kFALSE;
9c7b5023 302}
303
304Bool_t AliRawReaderHLT::ReadNext(UChar_t* data, Int_t size)
305{
306 // see header file for class documentation
13b78f4c 307 UChar_t* dummy=NULL;
308 do {
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);
314 fOffset+=iCopy;
315 return kTRUE;
316 }
317 } while (ReadNextData(dummy));
318 return kFALSE;
9c7b5023 319}
320
321Bool_t AliRawReaderHLT::Reset()
322{
323 // see header file for class documentation
d3833f99 324 ReleaseHLTData();
13b78f4c 325 Bool_t result=fpParentReader->Reset();
13b78f4c 326 fEquipmentId=-1;
626bfcc1 327
d3833f99 328 // check if redirection is enabled for at least one detector in the selected range
329 fbHaveHLTData=EvaluateSelection();
626bfcc1 330
13b78f4c 331 return result;
9c7b5023 332}
333
334Bool_t AliRawReaderHLT::NextEvent()
335{
336 // see header file for class documentation
e3917543 337 Bool_t result=fpParentReader->NextEvent();
13b78f4c 338 if (result) {
339 fEventNumber++;
340 Reset();
341 }
e3917543 342 return result;
9c7b5023 343}
344
345Bool_t AliRawReaderHLT::RewindEvents()
346{
347 // see header file for class documentation
e3917543 348 fEventNumber=-1;
13b78f4c 349 Reset();
9c7b5023 350 return fpParentReader->RewindEvents();
351}
352
e3917543 353void AliRawReaderHLT::Select(Int_t detectorID, Int_t minDDLID, Int_t maxDDLID)
354{
13b78f4c 355 // see header file for class documentation
e3917543 356 AliRawReader::Select(detectorID, minDDLID, maxDDLID);
357 fpParentReader->Select(detectorID, minDDLID, maxDDLID);
d3833f99 358 fbHaveHLTData=EvaluateSelection();
e3917543 359}
360
361// most likely we do not need this method since the base class directly forwards
362// to this method
363// void AliRawReaderHLT::Select(const char *detectorName, Int_t minDDLID, Int_t maxDDLID)
364// {
365// AliInfo(Form("detectorName=%s, minDDLID=%d, maxDDLID=%d", detectorName, minDDLID, maxDDLID));
366// AliRawReader::Select(detectorName, minDDLID, maxDDLID);
367// fpParentReader->Select(detectorName, minDDLID, maxDDLID);
368// }
369
370void AliRawReaderHLT::SelectEquipment(Int_t equipmentType, Int_t minEquipmentId, Int_t maxEquipmentId)
371{
13b78f4c 372 // see header file for class documentation
373
093f4dba 374 //AliInfo(Form("equipmentType=%d, minEquipmentId=%d, maxEquipmentId=%d", equipmentType, minEquipmentId, maxEquipmentId));
d3833f99 375 AliRawReader::SelectEquipment(equipmentType, minEquipmentId, maxEquipmentId);
376 fpParentReader->SelectEquipment(equipmentType, minEquipmentId, maxEquipmentId);
377 fbHaveHLTData=EvaluateSelection();
e3917543 378}
379
380void AliRawReaderHLT::SkipInvalid(Bool_t skip)
381{
13b78f4c 382 // see header file for class documentation
383
e3917543 384 AliRawReader::SkipInvalid(skip);
385 fpParentReader->SkipInvalid(skip);
386}
387
388void AliRawReaderHLT::SelectEvents(Int_t type)
389{
13b78f4c 390 // see header file for class documentation
391
093f4dba 392 //AliInfo(Form("type=%d", type));
e3917543 393 AliRawReader::SelectEvents(type);
394 fpParentReader->SelectEvents(type);
395}
396
13b78f4c 397int AliRawReaderHLT::ScanOptions(const char* options)
398{
399 // see header file for class documentation
400 int iResult=0;
401 TString optString(options);
402 TString argument;
403 TString parameter;
404 TObjArray* pTokens=optString.Tokenize(" ");
405 if (pTokens) {
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
411
412 // it must be a detector name
413 int detId=AliDAQ::DetectorID(argument.Data());
414 if (detId>=0) {
415 fDetectors.push_back(detId);
a3ef3c1d 416 } else {
417 if (!fSystemOptions.IsNull()) fSystemOptions+=" ";
418 fSystemOptions+=argument;
13b78f4c 419 }
420 }
421 delete pTokens;
422 }
423
424 return iResult;
425}
426
427Bool_t AliRawReaderHLT::ReadNextHLTData()
428{
429 // see header file for class documentation
430 bool result=kTRUE;
431 if (!fpHLTOUT) {
432 fpHLTOUT=new AliHLTOUTRawReader(fpParentReader);
433 if (result=(fpHLTOUT!=NULL)) {
a3ef3c1d 434 AliHLTSystem* pSystem=GetInstance();
435 if (pSystem) {
436 pSystem->ScanOptions(fSystemOptions.Data());
437 }
13b78f4c 438 if (result=(fpHLTOUT->Init()>=0)) {
439 result=fpHLTOUT->SelectFirstDataBlock(kAliHLTAnyDataType, kAliHLTVoidDataSpec,
440 AliHLTModuleAgent::kRawReader)>=0;
441 }
442 }
443 } else {
626bfcc1 444 // first release the data buffer
d3833f99 445 ReleaseHLTData(false /* keep HLTOUT instance */);
446 result=fpHLTOUT->SelectNextDataBlock()>=0;
13b78f4c 447 }
448 if (result) {
626bfcc1 449 AliHLTComponentDataType dt=kAliHLTVoidDataType;
450 AliHLTUInt32_t spec=kAliHLTVoidDataSpec;
451 fpHLTOUT->GetDataBlockDescription(dt, spec);
13b78f4c 452 AliHLTUInt32_t size=0;
626bfcc1 453 AliHLTOUTHandler* pHandler=fpHLTOUT->GetHandler();
454 if (pHandler) {
455 if (dynamic_cast<AliHLTOUTHandlerEquId*>(pHandler)!=NULL) {
456 AliHLTOUT::AliHLTOUTLockGuard g(fpHLTOUT);
457 fEquipmentId=pHandler->ProcessData(fpHLTOUT);
458 fpData=NULL;
459 fDataSize=pHandler->GetProcessedData(fpData);
460 if (!fpData) {
461 result=fpHLTOUT->GetDataBuffer(fpData, size)>=0;
462 AliDebug(AliLog::kDebug, Form("forward data block from HLTOUT stream to equipment %d", fEquipmentId));
463 fDataSize=(int)size;
464 } else {
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));
468 }
e94eb049 469 return kTRUE;
626bfcc1 470 } else {
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));
473 }
474 } else {
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));
477 }
13b78f4c 478 } else {
d3833f99 479 ReleaseHLTData();
13b78f4c 480 }
e94eb049 481 return kFALSE;
13b78f4c 482}
483
484Bool_t AliRawReaderHLT::IsHLTInput(int ddlid)
485{
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)
492 return kTRUE;
493 }
494 return kFALSE;
495}
496
d3833f99 497int AliRawReaderHLT::ReleaseHLTData(bool bReleaseHLTOUT)
498{
499 // see header file for class documentation
500 if (fpHLTOUT) {
501 if (fpDataHandler) fpDataHandler->ReleaseProcessedData(fpData, fDataSize);
502 else fpHLTOUT->ReleaseDataBuffer(fpData);
503 fpDataHandler=NULL;
504 if (bReleaseHLTOUT) {
505 delete fpHLTOUT;
506 fpHLTOUT=NULL;
507 }
508 }
509
510 fpData=NULL;
511 fDataSize=0;
512 fOffset=0;
513 fPosition=0;
514 fEquipmentId=-1;
515
516 return 0;
517}
518
519Bool_t AliRawReaderHLT::EvaluateSelection()
520{
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))
530 break;
531 }
532 bHaveHLTData=detector!=fDetectors.end();
533 }
534 return bHaveHLTData;
535}
536
9c7b5023 537AliRawReader* AliRawReaderHLTCreateInstance(AliRawReader* pParentReader, const char* options)
538{
539 // see header file for class documentation
e3917543 540 if (!pParentReader) return NULL;
9c7b5023 541 return new AliRawReaderHLT(pParentReader, options);
542}