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 */
24 // see header file for class documentation
26 // refer to README to build package
28 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
36 #include "AliHLTOUTComponent.h"
37 #include "AliHLTOUT.h"
38 #include "AliHLTHOMERWriter.h"
39 #include "AliDAQ.h" // equipment Ids
40 #include "AliRawDataHeader.h" // Common Data Header
41 #include <TDatime.h> // seed for TRandom
42 #include <TRandom.h> // random int generation for DDL no
44 /** ROOT macro for the implementation of ROOT specific class methods */
45 ClassImp(AliHLTOUTComponent)
47 AliHLTOUTComponent::AliHLTOUTComponent()
49 AliHLTOfflineDataSink(),
52 fIdFirstDDL(4864), // 0x13<<8
58 // see header file for class documentation
60 // refer to README to build package
62 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
64 // I guess DDL definitions should never change any more
65 assert(fNofDDLs==AliDAQ::NumberOfDdls("HLT"));
66 fNofDDLs=AliDAQ::NumberOfDdls("HLT");
67 assert(fIdFirstDDL==AliDAQ::DdlIDOffset("HLT"));
68 fIdFirstDDL=AliDAQ::DdlIDOffset("HLT");
71 AliHLTOUTComponent::~AliHLTOUTComponent()
73 // see header file for class documentation
74 if (fpLibManager) delete fpLibManager;
78 const char* AliHLTOUTComponent::GetComponentID()
80 // see header file for class documentation
84 void AliHLTOUTComponent::GetInputDataTypes( vector<AliHLTComponentDataType>& list)
86 // see header file for class documentation
88 list.push_back(kAliHLTAnyDataType);
91 AliHLTComponent* AliHLTOUTComponent::Spawn()
93 // see header file for class documentation
94 return new AliHLTOUTComponent;
97 int AliHLTOUTComponent::DoInit( int argc, const char** argv )
99 // see header file for class documentation
103 for (int i=0; i<argc && iResult>=0; i++) {
105 if (argument.IsNull()) continue;
108 HLTError("unknown argument %s", argument.Data());
113 HLTError("missing parameter for argument %s", argument.Data());
119 fpLibManager=new AliHLTHOMERLibManager;
122 for (writerNo=0; writerNo<fNofDDLs; writerNo++) {
123 AliHLTMonitoringWriter* pWriter=fpLibManager->OpenWriter();
125 fWriters.push_back(pWriter);
138 int AliHLTOUTComponent::DoDeinit()
140 // see header file for class documentation
144 AliHLTMonitoringWriterPVector::iterator element=fWriters.begin();
145 while (element!= fWriters.end()) {
147 // wanted to have a dynamic_cast<AliHLTHOMERWriter*> here, but this results into
148 // undefined symbol when loading the library
149 if (*element!=NULL) fpLibManager->DeleteWriter((AliHLTHOMERWriter*)(*element));
150 element=fWriters.erase(element);
157 int AliHLTOUTComponent::DumpEvent( const AliHLTComponentEventData& evtData,
158 const AliHLTComponentBlockData* blocks,
159 AliHLTComponentTriggerData& /*trigData*/ )
161 // see header file for class documentation
163 HLTInfo("write %d output blocks", evtData.fBlockCnt);
166 homer_uint64 homerHeader[kCount_64b_Words];
167 HOMERBlockDescriptor homerDescriptor(homerHeader);
168 for (int n=0; n<(int)evtData.fBlockCnt; n++ ) {
169 memset( homerHeader, 0, sizeof(homer_uint64)*kCount_64b_Words );
170 homerDescriptor.Initialize();
171 homerDescriptor.SetType(reinterpret_cast<homer_uint64>(blocks[n].fDataType.fID));
172 homerDescriptor.SetSubType1(reinterpret_cast<homer_uint64>(blocks[n].fDataType.fOrigin));
173 homerDescriptor.SetSubType2(static_cast<homer_uint64>(blocks[n].fSpecification));
174 int writerNo=ShuffleWriters(fWriters, blocks[n].fSize);
175 assert(writerNo>=0 && writerNo<fWriters.size());
176 fWriters[writerNo]->AddBlock(&homerDescriptor, blocks[n].fPtr);
183 int AliHLTOUTComponent::FillESD(int eventNo, AliRunLoader* runLoader, AliESDEvent* /*esd*/)
185 // see header file for class documentation
187 if (fWriters.size()==0) return 0;
189 // search for the writer with the biggest data volume in order to allocate the
190 // output buffer of sufficient size
192 for (int i=0; i<fWriters.size(); i++) {
195 if (sorted.size()>=0 && fWriters[i]->GetTotalMemorySize()>fWriters[sorted[0]]->GetTotalMemorySize()) {
196 sorted.insert(sorted.begin(), i);
203 vector<int>::iterator ddlno=sorted.begin();
204 while (ddlno!=sorted.end()) {
205 const AliHLTUInt8_t* pBuffer=NULL;
208 if ((bufferSize=FillOutputBuffer(eventNo, fWriters[*ddlno], pBuffer))>0) {
209 if (fWriteDigits) WriteDigits(eventNo, runLoader, *ddlno, pBuffer, bufferSize);
210 if (fWriteRaw) WriteRawFile(eventNo, runLoader, *ddlno, pBuffer, bufferSize);
217 int AliHLTOUTComponent::ShuffleWriters(AliHLTMonitoringWriterPVector &list, AliHLTUInt32_t size)
219 // see header file for class documentation
221 assert(list.size()>0);
222 if (list.size()==0) return iResult;
225 for (i=0; i<list.size(); i++) {
226 if (list[i]->GetTotalMemorySize()==0)
227 writers.push_back(i);
228 else if (iResult<0 ||
229 list[i]->GetTotalMemorySize()<list[iResult]->GetTotalMemorySize())
233 if (writers.size()>0) {
235 if (writers.size()>0) {
236 // shuffle among the empty writers
239 rand.SetSeed(dt.Get()*(iResult+1));
240 i=rand.Integer(writers.size()-1);
241 assert(i>0 && i<writers.size()-1);
245 // take the writer with the least data volume
251 int AliHLTOUTComponent::FillOutputBuffer(int eventNo, AliHLTMonitoringWriter* pWriter, const AliHLTUInt8_t* &pBuffer)
253 // see header file for class documentation
257 // space for common data header
258 bufferSize+=sizeof(AliRawDataHeader);
259 assert(sizeof(AliRawDataHeader)==24);
261 // space for HLT event header
262 bufferSize+=sizeof(AliHLTOUT::AliHLTOUTEventHeader);
264 // space for payload from the writer
265 if (pWriter) bufferSize+=pWriter->GetTotalMemorySize();
267 if (bufferSize>fBuffer.size())
268 fBuffer.resize(bufferSize);
270 if (bufferSize<=fBuffer.size()) {
271 AliRawDataHeader* pCDH=reinterpret_cast<AliRawDataHeader*>(&fBuffer[0]);
272 AliHLTOUT::AliHLTOUTEventHeader* pHLTH=reinterpret_cast<AliHLTOUT::AliHLTOUTEventHeader*>(&fBuffer[sizeof(AliRawDataHeader)]);
273 memset(pCDH, 0, sizeof(AliRawDataHeader));
274 memset(pHLTH, 0, sizeof(AliHLTOUT::AliHLTOUTEventHeader));
278 pWriter->Copy(&fBuffer[sizeof(AliRawDataHeader)+sizeof(AliHLTOUT::AliHLTOUTEventHeader)], 0, 0, 0, 0);
279 pHLTH->fLength=pWriter->GetTotalMemorySize();
280 // set status bit to indicate HLT payload
281 pCDH->fStatusMiniEventID|=0x1<<(AliHLTOUT::fgkCDHStatusFlagsOffset+AliHLTOUT::fgkCDHFlagsHLTPayload);
283 pHLTH->fLength+=sizeof(AliHLTOUT::AliHLTOUTEventHeader);
284 pHLTH->fEventID=eventNo;
286 pCDH->fSize=sizeof(AliRawDataHeader)+pHLTH->fLength;
297 int AliHLTOUTComponent::WriteDigits(int eventNo, AliRunLoader* runLoader, int hltddl, const AliHLTUInt8_t* pBuffer, int bufferSize)
299 // see header file for class documentation
304 int AliHLTOUTComponent::WriteRawFile(int eventNo, AliRunLoader* runLoader, int hltddl, const AliHLTUInt8_t* pBuffer, int bufferSize)
306 // see header file for class documentation
308 const char* fileName=AliDAQ::DdlFileName("HLT", hltddl);
309 assert(fileName!=NULL);
310 TString filePath(fileName);
312 ios::openmode filemode=(ios::openmode)0;
313 ofstream rawfile(filePath.Data(), filemode);
314 if (rawfile.good()) {
315 if (pBuffer && bufferSize>0) {
316 rawfile.write(reinterpret_cast<const char*>(pBuffer), bufferSize);
318 HLTWarning("writing zero length raw data file %s");
320 HLTDebug("wrote %d byte(s) to file %s", bufferSize, filePath.Data());
322 HLTError("can not open file %s for writing", filePath.Data());