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 AliHLTOUTComponent.cxx
20 // @author Matthias Richter
22 // @brief The HLTOUT data sink component similar to HLTOUT nodes
23 // @note Used in the AliRoot environment only.
31 #include "AliHLTOUTComponent.h"
32 #include "AliHLTOUT.h"
33 #include "AliHLTHOMERLibManager.h"
34 #include "AliHLTHOMERWriter.h"
35 #include "AliDAQ.h" // equipment Ids
36 #include "AliRawDataHeader.h" // Common Data Header
37 #include <TDatime.h> // seed for TRandom
38 #include <TRandom.h> // random int generation for DDL no
43 /** ROOT macro for the implementation of ROOT specific class methods */
44 ClassImp(AliHLTOUTComponent)
46 AliHLTOUTComponent::AliHLTOUTComponent()
47 : AliHLTOfflineDataSink()
50 , fIdFirstDDL(7680) // 0x1e<<8
53 , fDigitFileName("HLT.Digits.root")
56 , fppDigitArrays(NULL)
60 // see header file for class documentation
62 // refer to README to build package
64 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
66 // I guess DDL definitions should never change any more
67 assert(fNofDDLs==AliDAQ::NumberOfDdls("HLT"));
68 fNofDDLs=AliDAQ::NumberOfDdls("HLT");
70 /* AliDAQ::DdlIDOffset returns wrong offset for HLT links
71 assert(fIdFirstDDL==AliDAQ::DdlIDOffset("HLT"));
72 fIdFirstDDL=AliDAQ::DdlIDOffset("HLT");
76 int AliHLTOUTComponent::fgOptions=kWriteRawFiles|kWriteDigits;
78 AliHLTOUTComponent::~AliHLTOUTComponent()
80 // see header file for class documentation
81 if (fpLibManager) delete fpLibManager;
85 const char* AliHLTOUTComponent::GetComponentID()
87 // see header file for class documentation
91 void AliHLTOUTComponent::GetInputDataTypes( vector<AliHLTComponentDataType>& list)
93 // see header file for class documentation
95 list.push_back(kAliHLTAnyDataType);
98 AliHLTComponent* AliHLTOUTComponent::Spawn()
100 // see header file for class documentation
101 return new AliHLTOUTComponent;
104 int AliHLTOUTComponent::DoInit( int argc, const char** argv )
106 // see header file for class documentation
108 if ((iResult=ConfigureFromArgumentString(argc, argv))<0) return iResult;
110 // Make sure there is no library manager before we try and create a new one.
116 // Create a new library manager and allocate the appropriate number of
117 // HOMER writers for the HLTOUT component.
118 fpLibManager=new AliHLTHOMERLibManager;
121 for (writerNo=0; writerNo<fNofDDLs; writerNo++) {
122 AliHLTMonitoringWriter* pWriter=fpLibManager->OpenWriter();
124 HLTDebug("HOMER writer %p added", pWriter);
125 fWriters.push_back(pWriter);
127 HLTError("can not open HOMER writer");
139 int AliHLTOUTComponent::ScanConfigurationArgument(int argc, const char** argv)
141 // see header file for class documentation
142 if (argc<=0) return 0;
144 TString argument=argv[i];
148 // specify number of ddl links
149 if (argument.CompareTo("-links")==0) {
150 if (++i>=argc) return -EPROTO;
151 TString parameter(argv[i]);
152 parameter.Remove(TString::kLeading, ' '); // remove all blanks
153 if (parameter.IsDigit()) {
154 fNofDDLs=parameter.Atoi();
156 HLTError("wrong parameter for argument %s, number expected", argument.Data());
164 if (argument.CompareTo("-digitfile")==0) {
165 if (++i>=argc) return -EPROTO;
166 fDigitFileName=argv[i];
173 if (argument.Contains(key)) {
174 argument.ReplaceAll(key, "");
175 if (argument.IsNull()) {
176 fgOptions|=kWriteRawFiles;
177 } else if (argument.CompareTo("=off")==0) {
178 fgOptions&=~kWriteRawFiles;
179 } else if (argument.CompareTo("=on")==0) {
180 fgOptions|=kWriteRawFiles;
182 HLTError("invalid parameter for argument %s: possible %s=off/%s=on", key, key, key);
191 if (argument.Contains(key)) {
192 argument.ReplaceAll(key, "");
193 if (argument.IsNull()) {
194 fgOptions|=kWriteDigits;
195 } else if (argument.CompareTo("=off")==0) {
196 fgOptions&=~kWriteDigits;
197 } else if (argument.CompareTo("=on")==0) {
198 fgOptions|=kWriteDigits;
200 HLTError("invalid parameter for argument %s: possible %s=off/%s=on", key, key, key);
211 int AliHLTOUTComponent::DoDeinit()
213 // see header file for class documentation
217 AliHLTMonitoringWriterPVector::iterator element=fWriters.begin();
218 while (element!= fWriters.end()) {
220 // wanted to have a dynamic_cast<AliHLTHOMERWriter*> here, but this results into
221 // undefined symbol when loading the library
222 if (*element!=NULL) {
224 fpLibManager->DeleteWriter((AliHLTHOMERWriter*)(*element));
226 HLTError("writer instance is NULL");
228 element=fWriters.erase(element);
242 fpDigitFile->Close();
247 if (fppDigitArrays) {
248 for (int i=0; i<fNofDDLs; i++) {
249 if (fppDigitArrays[i]) delete fppDigitArrays[i];
251 delete[] fppDigitArrays;
258 int AliHLTOUTComponent::DumpEvent( const AliHLTComponentEventData& evtData,
259 const AliHLTComponentBlockData* blocks,
260 AliHLTComponentTriggerData& /*trigData*/ )
262 // see header file for class documentation
264 HLTInfo("write %d output block(s)", evtData.fBlockCnt);
267 AliHLTUInt32_t eventType=gkAliEventTypeUnknown;
268 bool bIsDataEvent=IsDataEvent(&eventType);
270 homer_uint64 homerHeader[kCount_64b_Words];
271 HOMERBlockDescriptor homerDescriptor(homerHeader);
272 for (int n=0; n<(int)evtData.fBlockCnt; n++ ) {
273 if (blocks[n].fDataType==kAliHLTDataTypeEvent ||
274 blocks[n].fDataType==kAliHLTDataTypeSOR ||
275 blocks[n].fDataType==kAliHLTDataTypeEOR ||
276 blocks[n].fDataType==kAliHLTDataTypeComConf ||
277 blocks[n].fDataType==kAliHLTDataTypeUpdtDCS)
279 // the special events have to be ignored.
283 (blocks[n].fDataType!=kAliHLTDataTypeComponentTable))
285 // In simulation, there are no SOR and EOR events created. Thats
286 // why all data blocks of those events are currently ignored.
287 // Strictly speaking, components should not create output blocks
288 // on the SOR/EOR event
290 // Exeptions: some blocks are added, the buffer must be prepared and
291 // kept since the pointers will be invalid
292 // - kAliHLTDataTypeComponentTable component table entries
295 memset( homerHeader, 0, sizeof(homer_uint64)*kCount_64b_Words );
296 homerDescriptor.Initialize();
297 // for some traditional reason the TCPDumpSubscriber swaps the bytes
298 // of the data type id and data type origin. Actually I do not understand
299 // the corresponding code line
300 // homerBlock.SetType( blocks[n].fDataType.fID );
301 // this compiles in the PubSub framework and in addition does a byte swap
303 homer_uint64 origin=0;
304 memcpy(&id, blocks[n].fDataType.fID, sizeof(homer_uint64));
305 memcpy(((AliHLTUInt8_t*)&origin)+sizeof(homer_uint32), blocks[n].fDataType.fOrigin, sizeof(homer_uint32));
306 homerDescriptor.SetType(AliHLTOUT::ByteSwap64(id));
307 homerDescriptor.SetSubType1(AliHLTOUT::ByteSwap64(origin));
308 homerDescriptor.SetSubType2(blocks[n].fSpecification);
309 homerDescriptor.SetBlockSize(blocks[n].fSize);
311 writerNo=ShuffleWriters(fWriters, blocks[n].fSize);
313 assert(writerNo>=0 && writerNo<(int)fWriters.size());
314 // I'm puzzled by the different headers, buffers etc. used in the
315 // HOMER writer/data. In additional, there is no type check as there
316 // are void pointers used and names mixed.
317 // It seems that HOMERBlockDescriptor is just a tool to set the
318 // different fields in the homer header, which is an array of 64 bit
320 fWriters[writerNo]->AddBlock(homerHeader, blocks[n].fPtr);
325 if (iResult>=0 && !bIsDataEvent && fNofDDLs>=2) {
326 // data blocks from a special event are kept to be added to the
327 // following event. In the current implementation at least 2 DDLs
328 // are required to allow to keep the blocks of the SOR event and
329 // include it in the first event. If only one writer is available
330 // the blocks are ignored. For the moment this is not expexted to
331 // be a problem since components should not gererate anything on
332 // SOR/EOR. The only case is the list of AliHLTComponentTableEntry
333 // transmitted for component statistics in debug mode.
334 if (fReservedWriter>=0) {
335 HLTWarning("overriding previous buffer of non-data event data blocks");
337 const AliHLTUInt8_t* pBuffer=NULL;
339 // TODO: not yet clear whether it is smart to send the event id of
340 // this special event or if it should be set from the id of the
341 // following event where the data will be added
342 if (blockCount>0 && (bufferSize=FillOutputBuffer(evtData.fEventID, fWriters[writerNo], pBuffer))>0) {
343 fReservedWriter=writerNo;
344 fReservedData=bufferSize;
346 fWriters[writerNo]->Clear();
347 } else if (iResult>=0 && !bIsDataEvent && fNofDDLs<2 && blockCount>0) {
348 HLTWarning("ignoring %d block(s) for special event of type %d: at least 2 DDLs are required", blockCount, eventType);
351 if (iResult>=0 && bIsDataEvent) {
352 iResult=Write(GetEventCount(), GetRunLoader());
359 int AliHLTOUTComponent::FillESD(int eventNo, AliRunLoader* runLoader, AliESDEvent* /*esd*/)
361 // see header file for class documentation
362 // 2010-04-14 nothing to do any more. The data is written at the end of
367 int AliHLTOUTComponent::Write(int eventNo, AliRunLoader* runLoader)
369 // see header file for class documentation
372 if (fWriters.size()==0) return 0;
374 if (fReservedWriter>=0) {
375 if (fgOptions&kWriteDigits) WriteDigitArray(fReservedWriter, &fBuffer[0], fReservedData);
376 if (fgOptions&kWriteRawFiles) WriteRawFile(eventNo, runLoader, fReservedWriter, &fBuffer[0], fReservedData);
380 // search for the writer with the biggest data volume in order to allocate the
381 // output buffer of sufficient size
383 for (size_t i=0; i<fWriters.size(); i++) {
384 if ((int)i==fReservedWriter) continue;
387 if (sorted.size()==0 || fWriters[i]->GetTotalMemorySize()<=fWriters[sorted[0]]->GetTotalMemorySize()) {
390 sorted.insert(sorted.begin(), i);
396 vector<int>::iterator ddlno=sorted.begin();
397 while (ddlno!=sorted.end()) {
398 const AliHLTUInt8_t* pBuffer=NULL;
401 if ((bufferSize=FillOutputBuffer(eventNo, fWriters[*ddlno], pBuffer))>0) {
402 if (fgOptions&kWriteDigits) WriteDigitArray(*ddlno, pBuffer, bufferSize);
403 if (fgOptions&kWriteRawFiles) WriteRawFile(eventNo, runLoader, *ddlno, pBuffer, bufferSize);
405 fWriters[*ddlno]->Clear();
408 if (fgOptions&kWriteDigits) WriteDigits(eventNo, runLoader);
412 int AliHLTOUTComponent::ShuffleWriters(AliHLTMonitoringWriterPVector &list, AliHLTUInt32_t /*size*/)
414 // see header file for class documentation
416 assert(list.size()>0);
417 if (list.size()==0) return iResult;
420 for (i=0; i<list.size(); i++) {
421 if ((int)i==fReservedWriter) continue;
422 if (list[i]->GetTotalMemorySize()==0)
423 writers.push_back(i);
424 else if (iResult<0 ||
425 list[i]->GetTotalMemorySize()<list[iResult]->GetTotalMemorySize())
429 if (writers.size()>0) {
431 if (writers.size()>0) {
432 // shuffle among the empty writers
435 rand.SetSeed(dt.Get()*(iResult+1));
436 i=rand.Integer(writers.size()-1);
437 assert(i>0 && i<writers.size()-1);
441 // take the writer with the least data volume
447 int AliHLTOUTComponent::FillOutputBuffer(int eventNo, AliHLTMonitoringWriter* pWriter, const AliHLTUInt8_t* &pBuffer)
449 // see header file for class documentation
451 unsigned int bufferSize=0;
453 // space for common data header
454 bufferSize+=sizeof(AliRawDataHeader);
455 assert(sizeof(AliRawDataHeader)==32);
457 // space for HLT event header
458 bufferSize+=sizeof(AliHLTOUT::AliHLTOUTEventHeader);
460 // space for payload from the writer
461 if (pWriter) bufferSize+=pWriter->GetTotalMemorySize();
463 // payload data must be aligned to 32bit
464 bufferSize=(bufferSize+3)/4;
467 if (bufferSize>fBuffer.size())
468 fBuffer.resize(bufferSize);
470 // reset the last 32bit word, rest will be overwritten
471 memset(&fBuffer[bufferSize-4], 0, 4);
473 if (bufferSize<=fBuffer.size()) {
474 AliRawDataHeader* pCDH=reinterpret_cast<AliRawDataHeader*>(&fBuffer[0]);
475 AliHLTOUT::AliHLTOUTEventHeader* pHLTH=reinterpret_cast<AliHLTOUT::AliHLTOUTEventHeader*>(&fBuffer[sizeof(AliRawDataHeader)]);
476 *pCDH = AliRawDataHeader(); // Fill with default values.
477 memset(pHLTH, 0, sizeof(AliHLTOUT::AliHLTOUTEventHeader));
481 pWriter->Copy(&fBuffer[sizeof(AliRawDataHeader)+sizeof(AliHLTOUT::AliHLTOUTEventHeader)], 0, 0, 0, 0);
482 pHLTH->fLength=pWriter->GetTotalMemorySize();
483 // set status bit to indicate HLT payload
484 pCDH->fStatusMiniEventID|=0x1<<(AliHLTOUT::kCDHStatusFlagsOffset+AliHLTOUT::kCDHFlagsHLTPayload);
486 pHLTH->fLength+=sizeof(AliHLTOUT::AliHLTOUTEventHeader);
487 // pHLTH->fEventIDLow is already set to zero in memset above.
488 pHLTH->fEventIDLow = eventNo;
489 // version does not really matter since we do not add decision data
490 pHLTH->fVersion=AliHLTOUT::kVersion1;
492 pCDH->fSize=bufferSize;
493 pCDH->fStatusMiniEventID|=0x1<<(AliHLTOUT::kCDHStatusFlagsOffset + AliHLTOUT::kCDHFlagsHLTPayload);
496 iResult=(int)bufferSize;
505 int AliHLTOUTComponent::WriteDigitArray(int hltddl, const AliHLTUInt8_t* pBuffer, unsigned int bufferSize)
507 // see header file for class documentation
509 assert(hltddl<fNofDDLs);
510 if (hltddl>=fNofDDLs) return -ERANGE;
512 if (!fppDigitArrays) {
513 fppDigitArrays=new TArrayC*[fNofDDLs];
514 if (fppDigitArrays) {
515 for (int i=0; i<fNofDDLs; i++) {
516 fppDigitArrays[i]=new TArrayC(0);
520 if (fppDigitArrays && fppDigitArrays[hltddl]) {
521 fppDigitArrays[hltddl]->Set(bufferSize, reinterpret_cast<const Char_t*>(pBuffer));
528 int AliHLTOUTComponent::WriteDigits(int /*eventNo*/, AliRunLoader* /*runLoader*/)
530 // see header file for class documentation
533 fpDigitFile=new TFile(fDigitFileName, "RECREATE");
535 if (fpDigitFile && !fpDigitFile->IsZombie()) {
537 fpDigitTree=new TTree("rawhltout","HLTOUT raw data");
538 if (fpDigitTree && fppDigitArrays) {
539 for (int i=0; i<fNofDDLs; i++) {
540 const char* branchName=AliDAQ::DdlFileName("HLT", i);
541 if (fppDigitArrays[i]) fpDigitTree->Branch(branchName, "TArrayC", &fppDigitArrays[i], 32000/*just as the default*/, 0);
546 int res=fpDigitTree->Fill();
547 HLTDebug("writing digit tree: %d", res);
549 res=fpDigitTree->Write("",TObject::kOverwrite);
550 HLTDebug("writing digit tree: %d", res);
551 if (fppDigitArrays) for (int i=0; i<fNofDDLs; i++) {
552 if (fppDigitArrays[i]) fppDigitArrays[i]->Set(0);
556 const char* errorMsg="";
557 if (GetEventCount()==5) {
558 errorMsg=" (suppressing further error messages)";
560 if (GetEventCount()<5) {
561 HLTError("can not open HLT digit file %s%s", fDigitFileName.Data(), errorMsg);
568 int AliHLTOUTComponent::WriteRawFile(int eventNo, AliRunLoader* /*runLoader*/, int hltddl, const AliHLTUInt8_t* pBuffer, unsigned int bufferSize)
570 // see header file for class documentation
572 const char* fileName=AliDAQ::DdlFileName("HLT", hltddl);
573 assert(fileName!=NULL);
575 filePath.Form("raw%d/", eventNo);
578 ios::openmode filemode=(ios::openmode)0;
579 ofstream rawfile(filePath.Data(), filemode);
580 if (rawfile.good()) {
581 if (pBuffer && bufferSize>0) {
582 rawfile.write(reinterpret_cast<const char*>(pBuffer), bufferSize);
584 HLTWarning("writing zero length raw data file %s");
586 HLTDebug("wrote %d byte(s) to file %s", bufferSize, filePath.Data());
588 HLTError("can not open file %s for writing", filePath.Data());
596 void AliHLTOUTComponent::SetGlobalOption(unsigned int options)
598 // see header file for class documentation
602 void AliHLTOUTComponent::ClearGlobalOption(unsigned int options)
604 // see header file for class documentation