]> git.uio.no Git - u/mrichter/AliRoot.git/blame - HLT/BASE/util/AliHLTFileWriter.cxx
- singleton functionality added for component and configuration handler
[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>
5ab95e9a 31#include <TSystem.h>
5df0cbb9 32//#include <TMath.h>
33//#include <TFile.h>
e74abd18 34
e74abd18 35/** ROOT macro for the implementation of ROOT specific class methods */
36ClassImp(AliHLTFileWriter)
37
38AliHLTFileWriter::AliHLTFileWriter()
39 :
40 AliHLTDataSink(),
41 fBaseName(""),
42 fExtension(""),
43 fDirectory(""),
5ab95e9a 44 fSubDirFormat(""),
6daf06e2 45 fIdFormat("_0x%08x"),
5ab95e9a 46 fSpecFormat(""),
652cf9d2 47 fBlcknoFormat("_0x%02x"),
e74abd18 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
e74abd18 58AliHLTFileWriter::~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
66const char* AliHLTFileWriter::GetComponentID()
67{
68 // see header file for class documentation
69 return "FileWriter";
70}
71
72void AliHLTFileWriter::GetInputDataTypes( vector<AliHLTComponentDataType>& list)
73{
74 // see header file for class documentation
75 list.clear();
6daf06e2 76 list.push_back(kAliHLTAllDataTypes);
e74abd18 77}
78
79AliHLTComponent* AliHLTFileWriter::Spawn()
80{
81 // see header file for class documentation
82 return new AliHLTFileWriter;
83}
84
85int 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) {
f7561f8d 103 int n=0;
104 fBaseName=((TObjString*)pTokens->At(n++))->GetString();
105 while (n<iEntries-1) {
106 fBaseName+="." + ((TObjString*)pTokens->At(n++))->GetString();
e74abd18 107 }
f7561f8d 108 fExtension=((TObjString*)pTokens->At(n))->GetString();
e74abd18 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
5ab95e9a 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 }
6daf06e2 126 // no additional eventno in the filename unless set again
127 // the sub dir contains the id
128 fIdFormat="";
5ab95e9a 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
6daf06e2 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
5ab95e9a 151 argument.ReplaceAll("-blcknofmt", "");
6daf06e2 152 argument.ReplaceAll("-blocknofmt", "");
5ab95e9a 153 if (argument.BeginsWith("=")) {
154 fBlcknoFormat=argument.Replace(0,1,"");
155 } else {
156 fBlcknoFormat="_0x%02x";
157 }
158
e74abd18 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();
07cd8230 190 if (!fDirectory.IsNull()) {
191 gSystem->mkdir(fDirectory);
192 }
e74abd18 193 }
194
195 return iResult;
196}
197
198int AliHLTFileWriter::InitWriter()
199{
200 // see header file for class documentation
5df0cbb9 201
202 // fCurrentFileName is used in dump event, just touched her to avoid
203 // coding convention violation RC11. The function can not be declared
204 // const since it is just the default implementation, overloaded
205 // virtual function might not be const
206 fCurrentFileName="";
e74abd18 207 return 0; // note: this doesn't mean 'error'
208}
209
6daf06e2 210int AliHLTFileWriter::ScanArgument(int /*argc*/, const char** /*argv*/)
e74abd18 211{
212 // see header file for class documentation
213
214 // there are no other arguments than the standard ones
5df0cbb9 215 // fCurrentFileName is used in dump event, just touched her to avoid
216 // coding convention violation RC11. The function can not be declared
217 // const since it is just the default implementation, overloaded
218 // virtual function might not be const
219 fCurrentFileName="";
e74abd18 220 return -EINVAL;
221}
222
223int AliHLTFileWriter::DoDeinit()
224{
225 // see header file for class documentation
226 int iResult=CloseWriter();
227 ClearMode(kEnumerate);
228 return iResult;
229}
230
231int AliHLTFileWriter::CloseWriter()
232{
233 // see header file for class documentation
5df0cbb9 234
235 // fCurrentFileName is used in dump event, just touched her to avoid
236 // coding convention violation RC11. The function can not be declared
237 // const since it is just the default implementation, overloaded
238 // virtual function might not be const
239 fCurrentFileName="";
6daf06e2 240 return 0;
e74abd18 241}
242
243int AliHLTFileWriter::DumpEvent( const AliHLTComponentEventData& evtData,
fdb4ca9e 244 AliHLTComponentTriggerData& /*trigData*/ )
e74abd18 245{
246 // see header file for class documentation
247 int iResult=0;
248 if (CheckMode(kConcatenateEvents)==0) {
249 // reset the current file name in order to open a new file
250 // for the first block. If events are concatenated, the current
251 // file name stays in order to be opended in append mode.
252 fCurrentFileName="";
253 }
fdb4ca9e 254 const AliHLTComponentBlockData* pDesc=NULL;
255
256 int blockno=0;
6daf06e2 257 for (pDesc=GetFirstInputBlock(); pDesc!=NULL; pDesc=GetNextInputBlock(), blockno++) {
fdb4ca9e 258 HLTDebug("block %d out of %d", blockno, evtData.fBlockCnt);
e74abd18 259 TString filename;
fdb4ca9e 260 HLTDebug("dataspec 0x%x", pDesc->fSpecification);
261 iResult=BuildFileName(evtData.fEventID, blockno, pDesc->fDataType, pDesc->fSpecification, filename);
e74abd18 262 ios::openmode filemode=(ios::openmode)0;
263 if (fCurrentFileName.CompareTo(filename)==0) {
264 // append to the file
265 filemode=ios::app;
266 } else {
267 // store the file for the next block
268 fCurrentFileName=filename;
269 }
270 if (iResult>=0) {
271 ofstream dump(filename.Data(), filemode);
272 if (dump.good()) {
fdb4ca9e 273 dump.write((static_cast<const char*>(pDesc->fPtr)), pDesc->fSize);
274 HLTDebug("wrote %d byte(s) to file %s", pDesc->fSize, filename.Data());
e74abd18 275 } else {
276 HLTError("can not open file %s for writing", filename.Data());
277 iResult=-EBADF;
278 }
279 dump.close();
280 }
281 }
e74abd18 282 return iResult;
283}
284
5ab95e9a 285int AliHLTFileWriter::BuildFileName(const AliHLTEventID_t eventID, const int blockID,
286 const AliHLTComponentDataType& dataType,
287 const AliHLTUInt32_t specification,
288 TString& filename)
e74abd18 289{
290 // see header file for class documentation
291 int iResult=0;
292 //HLTDebug("build file name for event %d block %d", eventID, blockID);
293 filename="";
294 if (!fDirectory.IsNull()) {
295 filename+=fDirectory;
5ab95e9a 296 if (!filename.EndsWith("/"))
e74abd18 297 filename+="/";
298 }
5ab95e9a 299 if (!fSubDirFormat.IsNull()) {
300 filename+=Form(fSubDirFormat, eventID);
301 if (!filename.EndsWith("/"))
302 filename+="/";
303 }
304 if (filename.EndsWith("/")) {
305 gSystem->mkdir(filename);
306 }
e74abd18 307 if (!fBaseName.IsNull())
308 filename+=fBaseName;
309 else
310 filename+="event";
311 if (!CheckMode(kConcatenateEvents)) {
312 if (!CheckMode(kEnumerate)) {
5ab95e9a 313 if (eventID!=kAliHLTVoidEventID && !fIdFormat.IsNull()) {
314 filename+=Form(fIdFormat, eventID);
e74abd18 315 }
316 } else {
317 filename+=Form("_%d", GetEventCount());
318 }
319 }
320 if (blockID>=0 && !CheckMode(kConcatenateBlocks)) {
5ab95e9a 321 if (!fBlcknoFormat.IsNull())
322 filename+=Form(fBlcknoFormat, blockID);
e74abd18 323 if (dataType!=kAliHLTVoidDataType) {
324 filename+="_";
325 filename+=AliHLTComponent::DataType2Text(dataType).data();
326 }
5ab95e9a 327 if (!fSpecFormat.IsNull())
328 filename+=Form(fSpecFormat, specification);
e74abd18 329 }
330 if (!fExtension.IsNull())
331 filename+="." + fExtension;
332 filename.ReplaceAll(" ", "");
333 return iResult;
334}
335
336int AliHLTFileWriter::SetMode(Short_t mode)
337{
338 // see header file for class documentation
339 fMode|=mode;
340 //HLTDebug("mode set to 0x%x", fMode);
341 return fMode;
342}
343
344int AliHLTFileWriter::ClearMode(Short_t mode)
345{
346 // see header file for class documentation
347 fMode&=~mode;
348 //HLTDebug("mode set to 0x%x", fMode);
349 return fMode;
350}
351
5df0cbb9 352int AliHLTFileWriter::CheckMode(Short_t mode) const
e74abd18 353{
354 // see header file for class documentation
355
356 //HLTDebug("check mode 0x%x for flag 0x%x: %d", fMode, mode, (fMode&mode)!=0);
357 return (fMode&mode)!=0;
358}