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