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