]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/BASE/AliHLTFilePublisher.cxx
- changes according to coding conventions and documentation
[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 void AliHLTFilePublisher::GetInputDataTypes( vector<AliHLTComponentDataType>& list)
80 {
81   list.clear();
82 }
83
84 AliHLTComponentDataType AliHLTFilePublisher::GetOutputDataType()
85 {
86   return (AliHLTComponentDataType){ sizeof(AliHLTComponentDataType), kAliHLTVoidDataTypeID, kAliHLTVoidDataOrigin};
87 }
88
89 void AliHLTFilePublisher::GetOutputDataSize( unsigned long& constBase, double& inputMultiplier )
90 {
91   constBase=fMaxSize;
92   inputMultiplier=1.0;
93 }
94
95 AliHLTComponent* AliHLTFilePublisher::Spawn()
96 {
97   return new AliHLTFilePublisher;
98 }
99
100 int AliHLTFilePublisher::DoInit( int argc, const char** argv )
101 {
102   int iResult=0;
103   TString argument="";
104   int bMissingParam=0;
105   for (int i; i<argc; i++) {
106     argument=argv[i];
107
108     // -datafile
109     if (argument.CompareTo("-datafile")==0) {
110       if ((bMissingParam=(++i>=argc))) break;
111       TObjString* parameter=new TObjString(argv[i]);
112       if (parameter) {
113         fFileNames.Add(parameter);
114       } else {
115         iResult=-ENOMEM;
116       }
117
118       // -datafilelist
119     } else if (argument.CompareTo("-datafilelist")==0) {
120       if ((bMissingParam=(++i>=argc))) break;
121       HLTWarning("-datafilelist option not yet implemented");
122
123       // -datatype
124     } else if (argument.CompareTo("-datatype")==0) {
125       if ((bMissingParam=(++i>=argc))) break;
126       memcpy(&fDataType.fID, argv[i], TMath::Min(kAliHLTComponentDataTypefIDsize, (Int_t)strlen(argv[i])));
127
128       // -dataspec
129     } else if (argument.CompareTo("-dataspec")==0) {
130       if ((bMissingParam=(++i>=argc))) break;
131       TString parameter(argv[i]);
132       if (parameter.IsDigit()) {
133         fSpecification=(AliHLTUInt32_t)parameter.Atoi();
134       } else {
135         HLTError("wrong parameter for argument %s, number expected", argument.Data());
136         iResult=-EINVAL;
137       }
138       // -dataorigin
139     } else if (argument.CompareTo("-dataorigin")==0) {
140       if ((bMissingParam=(++i>=argc))) break;
141       memcpy(&fDataType.fOrigin, argv[i], TMath::Min(kAliHLTComponentDataTypefOriginSize,(Int_t)strlen(argv[i])));
142     } else {
143       HLTError("unknown argument %s", argument.Data());
144       iResult=-EINVAL;
145     }
146   }
147   if (bMissingParam) {
148     HLTError("missing parameter for argument %s", argument.Data());
149     iResult=-EINVAL;
150   }
151   if (fFileNames.GetSize()==0) {
152     HLTError("the publisher needs at least one file argument");
153     iResult=-EINVAL;
154   }
155   if (iResult>=0) iResult=OpenFiles();
156   if (iResult<0) {
157     fFileNames.Clear();
158   }
159   return iResult;
160 }
161
162 int AliHLTFilePublisher::OpenFiles()
163 {
164   int iResult=0;
165   TObjLink *lnk=fFileNames.FirstLink();
166   while (lnk && iResult>=0) {
167     TObjString* pFileName=(TObjString*)lnk->GetObject();
168     if (pFileName) {
169       TString fullFN= pFileName->GetString() + "?filetype=raw";
170       TFile* pFile = new TFile(fullFN);
171       if (pFile) {
172         if (pFile->IsZombie()==0) {
173           fFiles.Add(pFile);
174           if (pFile->GetSize()>fMaxSize) fMaxSize=pFile->GetSize();
175         } else {
176           HLTError("can not open file %s", (pFileName->GetString()).Data());
177           fFiles.Clear();
178           iResult=-ENOENT;
179         }
180       }
181     }
182     lnk = lnk->Next();
183   }
184
185   return iResult;
186 }
187
188 int AliHLTFilePublisher::DoDeinit()
189 {
190   int iResult=0;
191   fFileNames.Clear();
192   fFiles.Clear();
193   return iResult;
194 }
195
196 int AliHLTFilePublisher::GetEvent( const AliHLTComponentEventData& evtData,
197               AliHLTComponentTriggerData& trigData,
198               AliHLTUInt8_t* outputPtr, 
199               AliHLTUInt32_t& size,
200               vector<AliHLTComponentBlockData>& outputBlocks )
201 {
202   int iResult=0;
203   TFile * pFile=NULL;
204   TObjLink *lnk;
205   if (fpCurrent) lnk=fpCurrent->Next();
206   if (lnk==NULL) lnk=fFiles.FirstLink();
207   fpCurrent=lnk;
208   if (lnk) {
209     TFile* pFile=(TFile*)lnk->GetObject();
210     if (pFile) {
211       int iCopy=pFile->GetSize();
212       if (iCopy>size) {
213         iCopy=size;
214         HLTWarning("buffer to small, data of file %s truncated", pFile->GetName());
215       }
216       if (pFile->ReadBuffer((char*)outputPtr, iCopy)!=0) {
217         // ReadBuffer returns 1 in case of failure and 0 in case of success
218         iResult=-EIO;
219       } else {
220         AliHLTComponentBlockData bd;
221         FillBlockData(bd);
222         bd.fPtr=outputPtr;
223         bd.fOffset=0;
224         bd.fSize=iCopy;
225         bd.fDataType=fDataType;
226         bd.fSpecification=fSpecification;
227         outputBlocks.push_back(bd);
228         size=iCopy;
229       }
230     } else {
231       iResult=-EFAULT;
232     }
233   } else {
234     iResult=-ENOENT;
235   }
236   return iResult;
237 }