]> git.uio.no Git - u/mrichter/AliRoot.git/blame - HLT/BASE/util/AliHLTFilePublisher.cxx
components return -ENOSPC if output buffer too small, second trial by the AliRoot...
[u/mrichter/AliRoot.git] / HLT / BASE / util / AliHLTFilePublisher.cxx
CommitLineData
4ddfc222 1// $Id$
2
3/**************************************************************************
9be2600f 4 * This file is property of and copyright by the ALICE HLT Project *
5 * ALICE Experiment at CERN, All rights reserved. *
4ddfc222 6 * *
9be2600f 7 * Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no> *
8 * for The ALICE HLT Project. *
4ddfc222 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 AliHLTFilePublisher.cxx
20 @author Matthias Richter
21 @date
22 @brief HLT file publisher component implementation. */
23
24#if __GNUC__>= 3
25using namespace std;
26#endif
27
28#include "AliHLTFilePublisher.h"
29#include <TObjString.h>
30#include <TMath.h>
31#include <TFile.h>
32
33/** the global object for component registration */
34AliHLTFilePublisher gAliHLTFilePublisher;
35
36/** ROOT macro for the implementation of ROOT specific class methods */
37ClassImp(AliHLTFilePublisher)
38
39AliHLTFilePublisher::AliHLTFilePublisher()
40 :
41 AliHLTDataSource(),
42 fFileNames(),
43 fFiles(),
44 fpCurrent(NULL),
45 fDataType(kAliHLTVoidDataType),
46 fSpecification(~(AliHLTUInt32_t)0),
47 fMaxSize(0)
48{
49 // see header file for class documentation
50 // or
51 // refer to README to build package
52 // or
53 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
54
55 // make the lists owners of their objects in order to automatically
56 // de-allocate the objects
57 fFileNames.SetOwner();
58 fFiles.SetOwner();
59}
60
61AliHLTFilePublisher::AliHLTFilePublisher(const AliHLTFilePublisher&)
62 :
63 AliHLTDataSource(),
64 fFileNames(),
65 fFiles(),
66 fpCurrent(NULL),
67 fDataType(kAliHLTVoidDataType),
68 fSpecification(0),
69 fMaxSize(0)
70{
71 // see header file for class documentation
72 HLTFatal("copy constructor untested");
73}
74
75AliHLTFilePublisher& AliHLTFilePublisher::operator=(const AliHLTFilePublisher&)
76{
77 // see header file for class documentation
78 HLTFatal("assignment operator untested");
79 return *this;
80}
81
82AliHLTFilePublisher::~AliHLTFilePublisher()
83{
84 // see header file for class documentation
85
86 // file list and file name list are owner of their objects and
87 // delete all the objects
88}
89
90const char* AliHLTFilePublisher::GetComponentID()
91{
92 // see header file for class documentation
93 return "FilePublisher";
94}
95
96AliHLTComponentDataType AliHLTFilePublisher::GetOutputDataType()
97{
98 // see header file for class documentation
99 return fDataType;
100}
101
102void AliHLTFilePublisher::GetOutputDataSize( unsigned long& constBase, double& inputMultiplier )
103{
104 // see header file for class documentation
105 constBase=fMaxSize;
106 inputMultiplier=1.0;
107}
108
109AliHLTComponent* AliHLTFilePublisher::Spawn()
110{
111 // see header file for class documentation
112 return new AliHLTFilePublisher;
113}
114
115int AliHLTFilePublisher::DoInit( int argc, const char** argv )
116{
117 // see header file for class documentation
118
119 //HLTDebug("%d %s", argc, argv[0]);
120 int iResult=0;
121 TString argument="";
122 int bMissingParam=0;
123 for (int i=0; i<argc && iResult>=0; i++) {
124 argument=argv[i];
125 if (argument.IsNull()) continue;
126
127 // -datafile
128 if (argument.CompareTo("-datafile")==0) {
129 if ((bMissingParam=(++i>=argc))) break;
130 TObjString* parameter=new TObjString(argv[i]);
131 if (parameter) {
132 fFileNames.Add(parameter);
133 } else {
134 iResult=-ENOMEM;
135 }
136
137 // -datafilelist
138 } else if (argument.CompareTo("-datafilelist")==0) {
139 if ((bMissingParam=(++i>=argc))) break;
140 HLTWarning("-datafilelist option not yet implemented");
141
142 // -datatype
143 } else if (argument.CompareTo("-datatype")==0) {
144 if ((bMissingParam=(++i>=argc))) break;
145 memcpy(&fDataType.fID, argv[i], TMath::Min(kAliHLTComponentDataTypefIDsize, (Int_t)strlen(argv[i])));
146 if ((bMissingParam=(++i>=argc))) break;
147 memcpy(&fDataType.fOrigin, argv[i], TMath::Min(kAliHLTComponentDataTypefOriginSize, (Int_t)strlen(argv[i])));
148
149 // -dataspec
150 } else if (argument.CompareTo("-dataspec")==0) {
151 if ((bMissingParam=(++i>=argc))) break;
152 TString parameter(argv[i]);
153 parameter.Remove(TString::kLeading, ' '); // remove all blanks
154 if (parameter.IsDigit()) {
155 fSpecification=(AliHLTUInt32_t)parameter.Atoi();
156 } else if (parameter.BeginsWith("0x") &&
157 parameter.Replace(0,2,"",0).IsHex()) {
158 sscanf(parameter.Data(),"%x", &fSpecification);
159 } else {
160 HLTError("wrong parameter for argument %s, number expected", argument.Data());
161 iResult=-EINVAL;
162 }
163 } else {
164 if ((iResult=ScanArgument(argc-i, &argv[i]))==-EINVAL) {
165 HLTError("unknown argument %s", argument.Data());
166 break;
167 } else if (iResult==-EPROTO) {
168 bMissingParam=1;
169 break;
170 } else if (iResult>=0) {
171 i+=iResult;
172 iResult=0;
173 }
174 }
175 }
176 if (bMissingParam) {
177 HLTError("missing parameter for argument %s", argument.Data());
178 iResult=-EINVAL;
179 }
180 if (fFileNames.GetSize()==0) {
181 HLTError("the publisher needs at least one file argument");
182 iResult=-EINVAL;
183 }
184 if (iResult>=0) iResult=OpenFiles();
185 if (iResult<0) {
186 fFileNames.Clear();
187 }
188 return iResult;
189}
190
191int AliHLTFilePublisher::ScanArgument(int argc, const char** argv)
192{
193 // see header file for class documentation
194
195 // there are no other arguments than the standard ones
196 if (argc==0 && argv==NULL) {
197 // this is just to get rid of the warning "unused parameter"
198 }
199 return -EPROTO;
200}
201
202int AliHLTFilePublisher::OpenFiles()
203{
204 // see header file for class documentation
205 int iResult=0;
206 TObjLink *lnk=fFileNames.FirstLink();
207 while (lnk && iResult>=0) {
208 TObjString* pFileName=(TObjString*)lnk->GetObject();
209 if (pFileName) {
210 TString fullFN= pFileName->GetString() + "?filetype=raw";
211 TFile* pFile = new TFile(fullFN);
212 if (pFile) {
213 if (pFile->IsZombie()==0) {
214 fFiles.Add(pFile);
215 if (pFile->GetSize()>fMaxSize) fMaxSize=pFile->GetSize();
216 } else {
217 HLTError("can not open file %s", (pFileName->GetString()).Data());
218 fFiles.Clear();
219 iResult=-ENOENT;
220 }
221 }
222 }
223 lnk = lnk->Next();
224 }
225
226 return iResult;
227}
228
229int AliHLTFilePublisher::DoDeinit()
230{
231 // see header file for class documentation
232 int iResult=0;
233 fFileNames.Clear();
234 fFiles.Clear();
235 return iResult;
236}
237
b6800be0 238int AliHLTFilePublisher::GetEvent( const AliHLTComponentEventData& /*evtData*/,
239 AliHLTComponentTriggerData& /*trigData*/,
4ddfc222 240 AliHLTUInt8_t* outputPtr,
241 AliHLTUInt32_t& size,
242 vector<AliHLTComponentBlockData>& outputBlocks )
243{
244 int iResult=0;
b6800be0 245 TObjLink *lnk=fpCurrent;
4ddfc222 246 if (lnk==NULL) lnk=fFiles.FirstLink();
247 fpCurrent=lnk;
248 if (lnk) {
249 TFile* pFile=(TFile*)lnk->GetObject();
250 if (pFile) {
251 int iCopy=pFile->GetSize();
252 pFile->Seek(0);
b6800be0 253 if (iCopy<=(int)size) {
4ddfc222 254 if (pFile->ReadBuffer((char*)outputPtr, iCopy)!=0) {
255 // ReadBuffer returns 1 in case of failure and 0 in case of success
256 iResult=-EIO;
257 } else {
258 AliHLTComponentBlockData bd;
259 FillBlockData(bd);
260 bd.fPtr=outputPtr;
261 bd.fOffset=0;
262 bd.fSize=iCopy;
263 bd.fDataType=fDataType;
264 bd.fSpecification=fSpecification;
265 outputBlocks.push_back(bd);
266 size=iCopy;
267 }
b6800be0 268 } else {
269 // output buffer too small, update GetOutputDataSize for the second trial
270 fMaxSize=iCopy;
271 iResult=-ENOSPC;
272 }
4ddfc222 273 } else {
274 HLTError("no file available");
275 iResult=-EFAULT;
276 }
277 } else {
278 iResult=-ENOENT;
279 }
b6800be0 280 if (iResult>=0 && fpCurrent) fpCurrent=fpCurrent->Next();
281
4ddfc222 282 return iResult;
283}
284
285AliHLTComponentDataType AliHLTFilePublisher::GetCurrentDataType() const
286{
287 return fDataType;
288}
289
290AliHLTUInt32_t AliHLTFilePublisher::GetCurrentSpecification() const
291{
292 return fSpecification;
293}