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