]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/BASE/util/AliHLTFilePublisher.cxx
documentation
[u/mrichter/AliRoot.git] / HLT / BASE / util / AliHLTFilePublisher.cxx
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   AliHLTFilePublisher.cxx
20     @author Matthias Richter
21     @date   
22     @brief  HLT file publisher component implementation. */
23
24 #if __GNUC__>= 3
25 using 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 */
34 AliHLTFilePublisher gAliHLTFilePublisher;
35
36 /** ROOT macro for the implementation of ROOT specific class methods */
37 ClassImp(AliHLTFilePublisher)
38
39 AliHLTFilePublisher::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
61 AliHLTFilePublisher::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
75 AliHLTFilePublisher& AliHLTFilePublisher::operator=(const AliHLTFilePublisher&)
76
77   // see header file for class documentation
78   HLTFatal("assignment operator untested");
79   return *this;
80 }
81
82 AliHLTFilePublisher::~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
90 const char* AliHLTFilePublisher::GetComponentID()
91 {
92   // see header file for class documentation
93   return "FilePublisher";
94 }
95
96 AliHLTComponentDataType AliHLTFilePublisher::GetOutputDataType()
97 {
98   // see header file for class documentation
99   return fDataType;
100 }
101
102 void AliHLTFilePublisher::GetOutputDataSize( unsigned long& constBase, double& inputMultiplier )
103 {
104   // see header file for class documentation
105   constBase=fMaxSize;
106   inputMultiplier=1.0;
107 }
108
109 AliHLTComponent* AliHLTFilePublisher::Spawn()
110 {
111   // see header file for class documentation
112   return new AliHLTFilePublisher;
113 }
114
115 int 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
191 int 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
202 int 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
229 int 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
238 int AliHLTFilePublisher::GetEvent( const AliHLTComponentEventData& evtData,
239               AliHLTComponentTriggerData& trigData,
240               AliHLTUInt8_t* outputPtr, 
241               AliHLTUInt32_t& size,
242               vector<AliHLTComponentBlockData>& outputBlocks )
243 {
244   int iResult=0;
245   TObjLink *lnk=NULL;
246   if (fpCurrent) lnk=fpCurrent->Next();
247   if (lnk==NULL) lnk=fFiles.FirstLink();
248   fpCurrent=lnk;
249   if (lnk) {
250     TFile* pFile=(TFile*)lnk->GetObject();
251     if (pFile) {
252       int iCopy=pFile->GetSize();
253       pFile->Seek(0);
254       if (iCopy>(int)size) {
255         iCopy=size;
256         HLTWarning("buffer to small, data of file %s truncated", pFile->GetName());
257       }
258       if (pFile->ReadBuffer((char*)outputPtr, iCopy)!=0) {
259         // ReadBuffer returns 1 in case of failure and 0 in case of success
260         iResult=-EIO;
261       } else {
262         AliHLTComponentBlockData bd;
263         FillBlockData(bd);
264         bd.fPtr=outputPtr;
265         bd.fOffset=0;
266         bd.fSize=iCopy;
267         bd.fDataType=fDataType;
268         bd.fSpecification=fSpecification;
269         outputBlocks.push_back(bd);
270         size=iCopy;
271       }
272     } else {
273       HLTError("no file available");
274       iResult=-EFAULT;
275     }
276   } else {
277     iResult=-ENOENT;
278   }
279   if (evtData.fStructSize==0 && trigData.fStructSize==0) {
280     // this is just to get rid of the warning "unused parameter"
281   }
282   return iResult;
283 }
284
285 AliHLTComponentDataType AliHLTFilePublisher::GetCurrentDataType() const
286 {
287   return fDataType;
288 }
289
290 AliHLTUInt32_t          AliHLTFilePublisher::GetCurrentSpecification() const
291 {
292   return fSpecification;
293 }