coding conventions and compilation warnings
[u/mrichter/AliRoot.git] / HLT / sim / AliHLTOUTComponent.cxx
CommitLineData
c4228ec4 1// $Id$
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 AliHLTOUTComponent.cxx
20 @author Matthias Richter
21 @date
13398559 22 @brief The HLTOUT data sink component similar to HLTOUT nodes
23*/
c4228ec4 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#if __GNUC__>= 3
32using namespace std;
33#endif
34
4b113031 35#include <cassert>
13398559 36//#include <iostream>
c4228ec4 37#include "AliHLTOUTComponent.h"
4b113031 38#include "AliHLTOUT.h"
64defa03 39#include "AliHLTHOMERLibManager.h"
4b113031 40#include "AliHLTHOMERWriter.h"
a183f221 41#include "AliDAQ.h" // equipment Ids
4b113031 42#include "AliRawDataHeader.h" // Common Data Header
43#include <TDatime.h> // seed for TRandom
44#include <TRandom.h> // random int generation for DDL no
c4228ec4 45
46/** ROOT macro for the implementation of ROOT specific class methods */
47ClassImp(AliHLTOUTComponent)
48
49AliHLTOUTComponent::AliHLTOUTComponent()
50 :
4b113031 51 AliHLTOfflineDataSink(),
52 fWriters(),
53 fNofDDLs(10),
54 fIdFirstDDL(4864), // 0x13<<8
55 fWriteDigits(kTRUE),
56 fWriteRaw(kTRUE),
a183f221 57 fBuffer(),
58 fpLibManager(NULL)
c4228ec4 59{
60 // see header file for class documentation
61 // or
62 // refer to README to build package
63 // or
64 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
4b113031 65
66 // I guess DDL definitions should never change any more
67 assert(fNofDDLs==AliDAQ::NumberOfDdls("HLT"));
68 fNofDDLs=AliDAQ::NumberOfDdls("HLT");
69 assert(fIdFirstDDL==AliDAQ::DdlIDOffset("HLT"));
70 fIdFirstDDL=AliDAQ::DdlIDOffset("HLT");
c4228ec4 71}
72
73AliHLTOUTComponent::~AliHLTOUTComponent()
74{
75 // see header file for class documentation
a183f221 76 if (fpLibManager) delete fpLibManager;
77 fpLibManager=NULL;
c4228ec4 78}
79
80const char* AliHLTOUTComponent::GetComponentID()
81{
82 // see header file for class documentation
83 return "HLTOUT";
84}
85
86void AliHLTOUTComponent::GetInputDataTypes( vector<AliHLTComponentDataType>& list)
87{
88 // see header file for class documentation
89 list.clear();
90 list.push_back(kAliHLTAnyDataType);
91}
92
93AliHLTComponent* AliHLTOUTComponent::Spawn()
94{
95 // see header file for class documentation
96 return new AliHLTOUTComponent;
97}
98
99int AliHLTOUTComponent::DoInit( int argc, const char** argv )
100{
101 // see header file for class documentation
102 int iResult=0;
103 TString argument="";
104 int bMissingParam=0;
105 for (int i=0; i<argc && iResult>=0; i++) {
106 argument=argv[i];
107 if (argument.IsNull()) continue;
108
a8420176 109 // -links
110 if (argument.CompareTo("-links")==0) {
111 if ((bMissingParam=(++i>=argc))) break;
112 TString parameter(argv[i]);
113 parameter.Remove(TString::kLeading, ' '); // remove all blanks
114 if (parameter.IsDigit()) {
115 fNofDDLs=parameter.Atoi();
116 } else {
117 HLTError("wrong parameter for argument %s, number expected", argument.Data());
118 iResult=-EINVAL;
119 }
120 } else {
c4228ec4 121 HLTError("unknown argument %s", argument.Data());
a8420176 122 iResult=-EINVAL;
c4228ec4 123 break;
124 }
125 }
126 if (bMissingParam) {
127 HLTError("missing parameter for argument %s", argument.Data());
128 iResult=-EINVAL;
129 }
130 if (iResult>=0) {
131 }
132
a183f221 133 fpLibManager=new AliHLTHOMERLibManager;
134 if (fpLibManager) {
135 int writerNo=0;
136 for (writerNo=0; writerNo<fNofDDLs; writerNo++) {
137 AliHLTMonitoringWriter* pWriter=fpLibManager->OpenWriter();
138 if (pWriter) {
a8420176 139 HLTDebug("HOMER writer %p added", pWriter);
a183f221 140 fWriters.push_back(pWriter);
141 } else {
5db0e774 142 HLTError("can nor open HOMER writer");
143 iResult=-ENODEV;
a183f221 144 break;
145 }
4b113031 146 }
a183f221 147 } else {
148 iResult=-ENOMEM;
4b113031 149 }
150
c4228ec4 151 return iResult;
152}
153
154int AliHLTOUTComponent::DoDeinit()
155{
156 // see header file for class documentation
157 int iResult=0;
4b113031 158
a183f221 159 if (fpLibManager) {
160 AliHLTMonitoringWriterPVector::iterator element=fWriters.begin();
161 while (element!= fWriters.end()) {
162 assert(*element);
b9dd5a11 163 // wanted to have a dynamic_cast<AliHLTHOMERWriter*> here, but this results into
164 // undefined symbol when loading the library
a8420176 165 (*element)->Clear();
b9dd5a11 166 if (*element!=NULL) fpLibManager->DeleteWriter((AliHLTHOMERWriter*)(*element));
a183f221 167 element=fWriters.erase(element);
168 }
4b113031 169 }
c4228ec4 170
171 return iResult;
172}
173
174int AliHLTOUTComponent::DumpEvent( const AliHLTComponentEventData& evtData,
175 const AliHLTComponentBlockData* blocks,
176 AliHLTComponentTriggerData& /*trigData*/ )
177{
178 // see header file for class documentation
179 int iResult=0;
180 HLTInfo("write %d output blocks", evtData.fBlockCnt);
4b113031 181 if (iResult>=0) {
182 homer_uint64 homerHeader[kCount_64b_Words];
183 HOMERBlockDescriptor homerDescriptor(homerHeader);
184 for (int n=0; n<(int)evtData.fBlockCnt; n++ ) {
185 memset( homerHeader, 0, sizeof(homer_uint64)*kCount_64b_Words );
186 homerDescriptor.Initialize();
18b56222 187 homerDescriptor.SetType(*(reinterpret_cast<const homer_uint64*>(blocks[n].fDataType.fID)));
188 homerDescriptor.SetSubType1(*(reinterpret_cast<const homer_uint32*>(blocks[n].fDataType.fOrigin)));
4b113031 189 homerDescriptor.SetSubType2(static_cast<homer_uint64>(blocks[n].fSpecification));
a8420176 190 homerDescriptor.SetBlockSize(blocks[n].fSize);
4b113031 191 int writerNo=ShuffleWriters(fWriters, blocks[n].fSize);
13398559 192 assert(writerNo>=0 && writerNo<(int)fWriters.size());
a8420176 193 // I'm puzzled by the different headers, buffers etc. used in the
194 // HOMER writer/data. In additional, there is no type check as there
195 // are void pointers used and names mixed.
196 // It seems that HOMERBlockDescriptor is just a tool to set the
197 // different fields in the homer header, which is an array of 64 bit
198 // words.
199 fWriters[writerNo]->AddBlock(homerHeader, blocks[n].fPtr);
4b113031 200 }
201 }
202
203 return iResult;
204}
205
206int AliHLTOUTComponent::FillESD(int eventNo, AliRunLoader* runLoader, AliESDEvent* /*esd*/)
207{
208 // see header file for class documentation
209 int iResult=0;
210 if (fWriters.size()==0) return 0;
211
212 // search for the writer with the biggest data volume in order to allocate the
213 // output buffer of sufficient size
4b113031 214 vector<int> sorted;
d76bc02a 215 for (size_t i=0; i<fWriters.size(); i++) {
4b113031 216 assert(fWriters[i]);
217 if (fWriters[i]) {
a8420176 218 if (sorted.size()==0 || fWriters[i]->GetTotalMemorySize()<=fWriters[sorted[0]]->GetTotalMemorySize()) {
4b113031 219 sorted.push_back(i);
a8420176 220 } else {
221 sorted.insert(sorted.begin(), i);
4b113031 222 }
223 }
4b113031 224 }
225
226 vector<int>::iterator ddlno=sorted.begin();
227 while (ddlno!=sorted.end()) {
228 const AliHLTUInt8_t* pBuffer=NULL;
229 int bufferSize=0;
230
b9dd5a11 231 if ((bufferSize=FillOutputBuffer(eventNo, fWriters[*ddlno], pBuffer))>0) {
4b113031 232 if (fWriteDigits) WriteDigits(eventNo, runLoader, *ddlno, pBuffer, bufferSize);
233 if (fWriteRaw) WriteRawFile(eventNo, runLoader, *ddlno, pBuffer, bufferSize);
234 }
a8420176 235 fWriters[*ddlno]->Clear();
4b113031 236 ddlno++;
237 }
238 return iResult;
239}
240
d76bc02a 241int AliHLTOUTComponent::ShuffleWriters(AliHLTMonitoringWriterPVector &list, AliHLTUInt32_t /*size*/)
4b113031 242{
243 // see header file for class documentation
244 int iResult=-ENOENT;
245 assert(list.size()>0);
246 if (list.size()==0) return iResult;
247 vector<int> writers;
d76bc02a 248 size_t i=0;
4b113031 249 for (i=0; i<list.size(); i++) {
250 if (list[i]->GetTotalMemorySize()==0)
251 writers.push_back(i);
252 else if (iResult<0 ||
253 list[i]->GetTotalMemorySize()<list[iResult]->GetTotalMemorySize())
254 iResult=i;
255
256 }
257 if (writers.size()>0) {
258 iResult=writers[0];
259 if (writers.size()>0) {
260 // shuffle among the empty writers
261 TDatime dt;
262 TRandom rand;
263 rand.SetSeed(dt.Get()*(iResult+1));
264 i=rand.Integer(writers.size()-1);
265 assert(i>0 && i<writers.size()-1);
266 iResult=writers[i];
267 }
268 } else {
269 // take the writer with the least data volume
270 assert(iResult>=0);
271 }
272 return iResult;
273}
274
a183f221 275int AliHLTOUTComponent::FillOutputBuffer(int eventNo, AliHLTMonitoringWriter* pWriter, const AliHLTUInt8_t* &pBuffer)
4b113031 276{
277 // see header file for class documentation
278 int iResult=0;
d76bc02a 279 unsigned int bufferSize=0;
4b113031 280
281 // space for common data header
282 bufferSize+=sizeof(AliRawDataHeader);
a8420176 283 assert(sizeof(AliRawDataHeader)==32);
4b113031 284
285 // space for HLT event header
286 bufferSize+=sizeof(AliHLTOUT::AliHLTOUTEventHeader);
287
288 // space for payload from the writer
289 if (pWriter) bufferSize+=pWriter->GetTotalMemorySize();
290
291 if (bufferSize>fBuffer.size())
292 fBuffer.resize(bufferSize);
293
294 if (bufferSize<=fBuffer.size()) {
295 AliRawDataHeader* pCDH=reinterpret_cast<AliRawDataHeader*>(&fBuffer[0]);
296 AliHLTOUT::AliHLTOUTEventHeader* pHLTH=reinterpret_cast<AliHLTOUT::AliHLTOUTEventHeader*>(&fBuffer[sizeof(AliRawDataHeader)]);
297 memset(pCDH, 0, sizeof(AliRawDataHeader));
298 memset(pHLTH, 0, sizeof(AliHLTOUT::AliHLTOUTEventHeader));
299 pHLTH->fVersion=1;
300 if (pWriter) {
301 // copy payload
302 pWriter->Copy(&fBuffer[sizeof(AliRawDataHeader)+sizeof(AliHLTOUT::AliHLTOUTEventHeader)], 0, 0, 0, 0);
303 pHLTH->fLength=pWriter->GetTotalMemorySize();
304 // set status bit to indicate HLT payload
13398559 305 pCDH->fStatusMiniEventID|=0x1<<(AliHLTOUT::kCDHStatusFlagsOffset+AliHLTOUT::kCDHFlagsHLTPayload);
4b113031 306 }
307 pHLTH->fLength+=sizeof(AliHLTOUT::AliHLTOUTEventHeader);
308 pHLTH->fEventID=eventNo;
309
310 pCDH->fSize=sizeof(AliRawDataHeader)+pHLTH->fLength;
c4228ec4 311
4b113031 312 pBuffer=&fBuffer[0];
a8420176 313 iResult=(int)bufferSize;
4b113031 314 } else {
315 pBuffer=NULL;
316 iResult=-ENOMEM;
c4228ec4 317 }
318
319 return iResult;
320}
321
a8420176 322int AliHLTOUTComponent::WriteDigits(int /*eventNo*/, AliRunLoader* /*runLoader*/, int /*hltddl*/, const AliHLTUInt8_t* /*pBuffer*/, unsigned int /*bufferSize*/)
c4228ec4 323{
324 // see header file for class documentation
325 int iResult=0;
326 return iResult;
327}
4b113031 328
a8420176 329int AliHLTOUTComponent::WriteRawFile(int eventNo, AliRunLoader* /*runLoader*/, int hltddl, const AliHLTUInt8_t* pBuffer, unsigned int bufferSize)
4b113031 330{
331 // see header file for class documentation
332 int iResult=0;
333 const char* fileName=AliDAQ::DdlFileName("HLT", hltddl);
4b113031 334 assert(fileName!=NULL);
a8420176 335 TString filePath;
336 filePath.Form("raw%d/", eventNo);
337 filePath+=fileName;
4b113031 338 if (fileName) {
339 ios::openmode filemode=(ios::openmode)0;
340 ofstream rawfile(filePath.Data(), filemode);
341 if (rawfile.good()) {
342 if (pBuffer && bufferSize>0) {
343 rawfile.write(reinterpret_cast<const char*>(pBuffer), bufferSize);
344 } else {
345 HLTWarning("writing zero length raw data file %s");
346 }
347 HLTDebug("wrote %d byte(s) to file %s", bufferSize, filePath.Data());
348 } else {
349 HLTError("can not open file %s for writing", filePath.Data());
350 iResult=-EBADF;
351 }
352 rawfile.close();
353 }
354 return iResult;
355}