]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/BASE/util/AliHLTFileWriter.cxx
handle missing data headers in AliRawReaderPiublisherComponent; code cleanup and...
[u/mrichter/AliRoot.git] / HLT / BASE / util / AliHLTFileWriter.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   AliHLTFileWriter.cxx
20     @author Matthias Richter
21     @date   
22     @brief  HLT file writer component implementation. */
23
24 #if __GNUC__>= 3
25 using namespace std;
26 #endif
27
28 #include "AliHLTFileWriter.h"
29 #include <TObjArray.h>
30 #include <TObjString.h>
31 #include <TSystem.h>
32 //#include <TMath.h>
33 //#include <TFile.h>
34
35 /** ROOT macro for the implementation of ROOT specific class methods */
36 ClassImp(AliHLTFileWriter)
37
38 AliHLTFileWriter::AliHLTFileWriter()
39   :
40   AliHLTDataSink(),
41   fBaseName(""),
42   fExtension(""),
43   fDirectory(""),
44   fSubDirFormat(""),
45   fIdFormat("_0x%08x"),
46   fSpecFormat(""),
47   fBlcknoFormat("_0x%02x"),
48   fCurrentFileName(""),
49   fMode(0)
50 {
51   // see header file for class documentation
52   // or
53   // refer to README to build package
54   // or
55   // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
56 }
57
58 AliHLTFileWriter::~AliHLTFileWriter()
59 {
60   // see header file for class documentation
61
62   // file list and file name list are owner of their objects and
63   // delete all the objects
64 }
65
66 const char* AliHLTFileWriter::GetComponentID()
67 {
68   // see header file for class documentation
69   return "FileWriter";
70 }
71
72 void AliHLTFileWriter::GetInputDataTypes( vector<AliHLTComponentDataType>& list)
73 {
74   // see header file for class documentation
75   list.clear();
76   list.push_back(kAliHLTAllDataTypes);
77 }
78
79 AliHLTComponent* AliHLTFileWriter::Spawn()
80 {
81   // see header file for class documentation
82   return new AliHLTFileWriter;
83 }
84
85 int AliHLTFileWriter::DoInit( int argc, const char** argv )
86 {
87   // see header file for class documentation
88   int iResult=0;
89   TString argument="";
90   int bMissingParam=0;
91   for (int i=0; i<argc && iResult>=0; i++) {
92     argument=argv[i];
93     if (argument.IsNull()) continue;
94
95     // -basename
96     if (argument.CompareTo("-datafile")==0) {
97       if ((bMissingParam=(++i>=argc))) break;
98       fBaseName=argv[i];
99       TObjArray* pTokens=fBaseName.Tokenize(".");
100       if (pTokens) {
101         int iEntries=pTokens->GetEntries();
102         if (iEntries>1) {
103           int i=0;
104           fBaseName=((TObjString*)pTokens->At(i++))->GetString();
105           while (i<iEntries-1) {
106             fBaseName+="." + ((TObjString*)pTokens->At(i++))->GetString();
107           }
108           fExtension=((TObjString*)pTokens->At(i))->GetString();
109         }
110         delete pTokens;
111       }
112
113       // -directory
114     } else if (argument.CompareTo("-directory")==0) {
115       if ((bMissingParam=(++i>=argc))) break;
116       fDirectory=argv[i];
117
118       // -subdir
119     } else if (argument.BeginsWith("-subdir")) {
120       argument.ReplaceAll("-subdir", "");
121       if (argument.BeginsWith("=")) {
122         fSubDirFormat=argument.Replace(0,1,"");
123       } else {
124         fSubDirFormat="event%03d";
125       }
126       // no additional eventno in the filename unless set again
127       // the sub dir contains the id
128       fIdFormat="";
129
130       // -idfmt
131     } else if (argument.BeginsWith("-idfmt")) {
132       argument.ReplaceAll("-idfmt", "");
133       if (argument.BeginsWith("=")) {
134         fIdFormat=argument.Replace(0,1,"");
135       }
136
137       // -specfmt
138     } else if (argument.BeginsWith("-specfmt")) {
139       argument.ReplaceAll("-specfmt", "");
140       if (argument.BeginsWith("=")) {
141         fSpecFormat=argument.Replace(0,1,"");
142       } else {
143         fSpecFormat="_0x%08x";
144       }
145
146       // -blocknofmt
147     } else if (argument.BeginsWith("-blcknofmt") || 
148                argument.BeginsWith("-blocknofmt")) {
149       // for the sake of backward compatibility we consider also the
150       // old argument with typo for a while
151       argument.ReplaceAll("-blcknofmt", "");
152       argument.ReplaceAll("-blocknofmt", "");
153       if (argument.BeginsWith("=")) {
154         fBlcknoFormat=argument.Replace(0,1,"");
155       } else {
156         fBlcknoFormat="_0x%02x";
157       }
158
159       // -enumeration
160     } else if (argument.CompareTo("-enumerate")==0) {
161       SetMode(kEnumerate);
162
163       // -concatenate-blocks
164     } else if (argument.CompareTo("-concatenate-blocks")==0) {
165       SetMode(kConcatenateBlocks);
166
167       // -concatenate-events
168     } else if (argument.CompareTo("-concatenate-events")==0) {
169       SetMode(kConcatenateEvents);
170
171     } else {
172       if ((iResult=ScanArgument(argc-i, &argv[i]))==-EINVAL) {
173         HLTError("unknown argument %s", argument.Data());
174         break;
175       } else if (iResult==-EPROTO) {
176         bMissingParam=1;
177         break;
178       } else if (iResult>=0) {
179         i+=iResult;
180         iResult=0;
181       }
182     }
183   }
184   if (bMissingParam) {
185     HLTError("missing parameter for argument %s", argument.Data());
186     iResult=-EINVAL;
187   }
188   if (iResult>=0) {
189     iResult=InitWriter();
190   }
191
192   return iResult;
193 }
194
195 int AliHLTFileWriter::InitWriter()
196 {
197   // see header file for class documentation
198   
199   // fCurrentFileName is used in dump event, just touched her to avoid
200   // coding convention violation RC11. The function can not be declared
201   // const since it is just the default implementation, overloaded
202   // virtual function might not be const
203   fCurrentFileName="";
204   return 0; // note: this doesn't mean 'error'
205 }
206
207 int AliHLTFileWriter::ScanArgument(int /*argc*/, const char** /*argv*/)
208 {
209   // see header file for class documentation
210
211   // there are no other arguments than the standard ones
212   // fCurrentFileName is used in dump event, just touched her to avoid
213   // coding convention violation RC11. The function can not be declared
214   // const since it is just the default implementation, overloaded
215   // virtual function might not be const
216   fCurrentFileName="";
217   return -EINVAL;
218 }
219
220 int AliHLTFileWriter::DoDeinit()
221 {
222   // see header file for class documentation
223   int iResult=CloseWriter();
224   ClearMode(kEnumerate);
225   return iResult;
226 }
227
228 int AliHLTFileWriter::CloseWriter()
229 {
230   // see header file for class documentation
231
232   // fCurrentFileName is used in dump event, just touched her to avoid
233   // coding convention violation RC11. The function can not be declared
234   // const since it is just the default implementation, overloaded
235   // virtual function might not be const
236   fCurrentFileName="";
237   return 0;
238 }
239
240 int AliHLTFileWriter::DumpEvent( const AliHLTComponentEventData& evtData,
241                          AliHLTComponentTriggerData& /*trigData*/ )
242 {
243   // see header file for class documentation
244   int iResult=0;
245   if (CheckMode(kConcatenateEvents)==0) {
246     // reset the current file name in order to open a new file
247     // for the first block. If events are concatenated, the current
248     // file name stays in order to be opended in append mode.
249     fCurrentFileName="";
250   }
251   const AliHLTComponentBlockData* pDesc=NULL;
252
253   int blockno=0;
254   for (pDesc=GetFirstInputBlock(); pDesc!=NULL; pDesc=GetNextInputBlock(), blockno++) {
255     HLTDebug("block %d out of %d", blockno, evtData.fBlockCnt);
256     TString filename;
257     HLTDebug("dataspec 0x%x", pDesc->fSpecification);
258     iResult=BuildFileName(evtData.fEventID, blockno, pDesc->fDataType, pDesc->fSpecification, filename);
259     ios::openmode filemode=(ios::openmode)0;
260     if (fCurrentFileName.CompareTo(filename)==0) {
261       // append to the file
262       filemode=ios::app;
263     } else {
264       // store the file for the next block
265       fCurrentFileName=filename;
266     }
267     if (iResult>=0) {
268       ofstream dump(filename.Data(), filemode);
269       if (dump.good()) {
270         dump.write((static_cast<const char*>(pDesc->fPtr)), pDesc->fSize);
271         HLTDebug("wrote %d byte(s) to file %s", pDesc->fSize, filename.Data());
272       } else {
273         HLTError("can not open file %s for writing", filename.Data());
274         iResult=-EBADF;
275       }
276       dump.close();
277     }
278   }
279   return iResult;
280 }
281
282 int AliHLTFileWriter::BuildFileName(const AliHLTEventID_t eventID, const int blockID,
283                                     const AliHLTComponentDataType& dataType,
284                                     const AliHLTUInt32_t specification,
285                                     TString& filename)
286 {
287   // see header file for class documentation
288   int iResult=0;
289   //HLTDebug("build file name for event %d block %d", eventID, blockID);
290   filename="";
291   if (!fDirectory.IsNull()) {
292     filename+=fDirectory;
293     if (!filename.EndsWith("/"))
294       filename+="/";
295   }
296   if (!fSubDirFormat.IsNull()) {
297     filename+=Form(fSubDirFormat, eventID);
298     if (!filename.EndsWith("/"))
299       filename+="/";
300   }
301   if (filename.EndsWith("/")) {
302     gSystem->mkdir(filename);
303   }
304   if (!fBaseName.IsNull())
305     filename+=fBaseName;
306   else
307     filename+="event";
308   if (!CheckMode(kConcatenateEvents)) {
309     if (!CheckMode(kEnumerate)) {
310       if (eventID!=kAliHLTVoidEventID && !fIdFormat.IsNull()) {
311         filename+=Form(fIdFormat, eventID);
312       }
313     } else {
314       filename+=Form("_%d", GetEventCount());
315     }
316   }
317   if (blockID>=0 && !CheckMode(kConcatenateBlocks)) {
318     if (!fBlcknoFormat.IsNull())
319       filename+=Form(fBlcknoFormat, blockID);
320     if (dataType!=kAliHLTVoidDataType) {
321       filename+="_";
322       filename+=AliHLTComponent::DataType2Text(dataType).data();
323     }
324     if (!fSpecFormat.IsNull())
325       filename+=Form(fSpecFormat, specification);
326   }
327   if (!fExtension.IsNull())
328     filename+="." + fExtension;
329   filename.ReplaceAll(" ", "");
330   return iResult;
331 }
332
333 int AliHLTFileWriter::SetMode(Short_t mode) 
334 {
335   // see header file for class documentation
336   fMode|=mode;
337   //HLTDebug("mode set to 0x%x", fMode);
338   return fMode;
339 }
340
341 int AliHLTFileWriter::ClearMode(Short_t mode)
342 {
343   // see header file for class documentation
344   fMode&=~mode;
345   //HLTDebug("mode set to 0x%x", fMode);
346   return fMode;
347 }
348
349 int AliHLTFileWriter::CheckMode(Short_t mode) const
350 {
351   // see header file for class documentation
352
353   //HLTDebug("check mode 0x%x for flag 0x%x: %d", fMode, mode, (fMode&mode)!=0);
354   return (fMode&mode)!=0;
355 }