]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/BASE/AliHLTFilePublisher.cxx
- improvements in AliHLTFilePublisher/Writer
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTFilePublisher.cxx
1 // $Id$
2
3 /**************************************************************************
4  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
5  *                                                                        *
6  * Authors: Matthias Richter <Matthias.Richter@ift.uib.no>                *
7  *          for The ALICE Off-line Project.                               *
8  *                                                                        *
9  * Permission to use, copy, modify and distribute this software and its   *
10  * documentation strictly for non-commercial purposes is hereby granted   *
11  * without fee, provided that the above copyright notice appears in all   *
12  * copies and that both the copyright notice and this permission notice   *
13  * appear in the supporting documentation. The authors make no claims     *
14  * about the suitability of this software for any purpose. It is          *
15  * provided "as is" without express or implied warranty.                  *
16  **************************************************************************/
17
18 /** @file   AliHLTFilePublisher.cxx
19     @author Matthias Richter
20     @date   
21     @brief  HLT file publisher component implementation. */
22
23 #if __GNUC__>= 3
24 using namespace std;
25 #endif
26
27 #include "AliHLTFilePublisher.h"
28 #include <TObjString.h>
29 #include <TMath.h>
30 #include <TFile.h>
31
32 /** ROOT macro for the implementation of ROOT specific class methods */
33 ClassImp(AliHLTFilePublisher)
34
35 AliHLTFilePublisher::AliHLTFilePublisher()
36   :
37   fFileNames(),
38   fFiles(),
39   fpCurrent(NULL),
40   fDataType(kAliHLTVoidDataType),
41   fSpecification(~(AliHLTUInt32_t)0),
42   fMaxSize(0)
43 {
44   // make the lists owners of their objects in order to automatically
45   // de-allocate the objects
46   fFileNames.SetOwner();
47   fFiles.SetOwner();
48 }
49
50 AliHLTFilePublisher::AliHLTFilePublisher(const AliHLTFilePublisher&)
51   :
52   fFileNames(),
53   fFiles(),
54   fpCurrent(NULL),
55   fDataType(kAliHLTVoidDataType),
56   fSpecification(0),
57   fMaxSize(0)
58 {
59   HLTFatal("copy constructor untested");
60 }
61
62 AliHLTFilePublisher& AliHLTFilePublisher::operator=(const AliHLTFilePublisher&)
63
64   HLTFatal("assignment operator untested");
65   return *this;
66 }
67
68 AliHLTFilePublisher::~AliHLTFilePublisher()
69 {
70   // file list and file name list are owner of their objects and
71   // delete all the objects
72 }
73
74 const char* AliHLTFilePublisher::GetComponentID()
75 {
76   return "FilePublisher";
77 }
78
79 AliHLTComponentDataType AliHLTFilePublisher::GetOutputDataType()
80 {
81   AliHLTComponentDataType dt =
82     {sizeof(AliHLTComponentDataType),
83      kAliHLTVoidDataTypeID,
84      kAliHLTVoidDataOrigin};
85   return dt;
86 }
87
88 void AliHLTFilePublisher::GetOutputDataSize( unsigned long& constBase, double& inputMultiplier )
89 {
90   constBase=fMaxSize;
91   inputMultiplier=1.0;
92 }
93
94 AliHLTComponent* AliHLTFilePublisher::Spawn()
95 {
96   return new AliHLTFilePublisher;
97 }
98
99 int AliHLTFilePublisher::DoInit( int argc, const char** argv )
100 {
101   //HLTDebug("%d %s", argc, argv[0]);
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
109     // -datafile
110     if (argument.CompareTo("-datafile")==0) {
111       if ((bMissingParam=(++i>=argc))) break;
112       TObjString* parameter=new TObjString(argv[i]);
113       if (parameter) {
114         fFileNames.Add(parameter);
115       } else {
116         iResult=-ENOMEM;
117       }
118
119       // -datafilelist
120     } else if (argument.CompareTo("-datafilelist")==0) {
121       if ((bMissingParam=(++i>=argc))) break;
122       HLTWarning("-datafilelist option not yet implemented");
123
124       // -datatype
125     } else if (argument.CompareTo("-datatype")==0) {
126       if ((bMissingParam=(++i>=argc))) break;
127       memcpy(&fDataType.fID, argv[i], TMath::Min(kAliHLTComponentDataTypefIDsize, (Int_t)strlen(argv[i])));
128       if ((bMissingParam=(++i>=argc))) break;
129       memcpy(&fDataType.fOrigin, argv[i], TMath::Min(kAliHLTComponentDataTypefOriginSize, (Int_t)strlen(argv[i])));
130
131       // -dataspec
132     } else if (argument.CompareTo("-dataspec")==0) {
133       if ((bMissingParam=(++i>=argc))) break;
134       TString parameter(argv[i]);
135       parameter.Remove(TString::kLeading, ' '); // remove all blanks
136       if (parameter.IsDigit()) {
137         fSpecification=(AliHLTUInt32_t)parameter.Atoi();
138       } else if (parameter.BeginsWith("0x") &&
139                  parameter.Replace(0,2,"",0).IsHex()) {
140         sscanf(parameter.Data(),"%x", &fSpecification);
141       } else {
142         HLTError("wrong parameter for argument %s, number expected", argument.Data());
143         iResult=-EINVAL;
144       }
145     } else {
146       if ((iResult=ScanArgument(argc-i, &argv[i]))==-EINVAL) {
147         HLTError("unknown argument %s", argument.Data());
148         break;
149       } else if (iResult==-EPROTO) {
150         bMissingParam=1;
151         break;
152       } else if (iResult>=0) {
153         i+=iResult;
154         iResult=0;
155       }
156     }
157   }
158   if (bMissingParam) {
159     HLTError("missing parameter for argument %s", argument.Data());
160     iResult=-EINVAL;
161   }
162   if (fFileNames.GetSize()==0) {
163     HLTError("the publisher needs at least one file argument");
164     iResult=-EINVAL;
165   }
166   if (iResult>=0) iResult=OpenFiles();
167   if (iResult<0) {
168     fFileNames.Clear();
169   }
170   return iResult;
171 }
172
173 int AliHLTFilePublisher::ScanArgument(int argc, const char** argv)
174 {
175   // there are no other arguments than the standard ones
176   return -EINVAL;
177 }
178
179 int AliHLTFilePublisher::OpenFiles()
180 {
181   int iResult=0;
182   TObjLink *lnk=fFileNames.FirstLink();
183   while (lnk && iResult>=0) {
184     TObjString* pFileName=(TObjString*)lnk->GetObject();
185     if (pFileName) {
186       TString fullFN= pFileName->GetString() + "?filetype=raw";
187       TFile* pFile = new TFile(fullFN);
188       if (pFile) {
189         if (pFile->IsZombie()==0) {
190           fFiles.Add(pFile);
191           if (pFile->GetSize()>fMaxSize) fMaxSize=pFile->GetSize();
192         } else {
193           HLTError("can not open file %s", (pFileName->GetString()).Data());
194           fFiles.Clear();
195           iResult=-ENOENT;
196         }
197       }
198     }
199     lnk = lnk->Next();
200   }
201
202   return iResult;
203 }
204
205 int AliHLTFilePublisher::DoDeinit()
206 {
207   int iResult=0;
208   fFileNames.Clear();
209   fFiles.Clear();
210   return iResult;
211 }
212
213 int AliHLTFilePublisher::GetEvent( const AliHLTComponentEventData& evtData,
214               AliHLTComponentTriggerData& trigData,
215               AliHLTUInt8_t* outputPtr, 
216               AliHLTUInt32_t& size,
217               vector<AliHLTComponentBlockData>& outputBlocks )
218 {
219   int iResult=0;
220   TObjLink *lnk=NULL;
221   if (fpCurrent) lnk=fpCurrent->Next();
222   if (lnk==NULL) lnk=fFiles.FirstLink();
223   fpCurrent=lnk;
224   if (lnk) {
225     TFile* pFile=(TFile*)lnk->GetObject();
226     if (pFile) {
227       int iCopy=pFile->GetSize();
228       pFile->Seek(0);
229       if (iCopy>(int)size) {
230         iCopy=size;
231         HLTWarning("buffer to small, data of file %s truncated", pFile->GetName());
232       }
233       if (pFile->ReadBuffer((char*)outputPtr, iCopy)!=0) {
234         // ReadBuffer returns 1 in case of failure and 0 in case of success
235         iResult=-EIO;
236       } else {
237         AliHLTComponentBlockData bd;
238         FillBlockData(bd);
239         bd.fPtr=outputPtr;
240         bd.fOffset=0;
241         bd.fSize=iCopy;
242         bd.fDataType=fDataType;
243         bd.fSpecification=fSpecification;
244         outputBlocks.push_back(bd);
245         size=iCopy;
246       }
247     } else {
248       HLTError("no file available");
249       iResult=-EFAULT;
250     }
251   } else {
252     iResult=-ENOENT;
253   }
254   return iResult;
255 }