]> git.uio.no Git - u/mrichter/AliRoot.git/blame - HLT/BASE/util/AliHLTFileWriter.cxx
correct compilation warnings
[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,"");
2cb9ea42 123 if (strchr(fSubDirFormat.Data(), '%')==NULL) {
124 fSubDirFormat+="%lu";
125 }
5ab95e9a 126 } else {
488eca05 127 fSubDirFormat="event%03lu";
5ab95e9a 128 }
6daf06e2 129 // no additional eventno in the filename unless set again
130 // the sub dir contains the id
131 fIdFormat="";
5ab95e9a 132
133 // -idfmt
134 } else if (argument.BeginsWith("-idfmt")) {
135 argument.ReplaceAll("-idfmt", "");
136 if (argument.BeginsWith("=")) {
137 fIdFormat=argument.Replace(0,1,"");
138 }
139
140 // -specfmt
141 } else if (argument.BeginsWith("-specfmt")) {
142 argument.ReplaceAll("-specfmt", "");
143 if (argument.BeginsWith("=")) {
144 fSpecFormat=argument.Replace(0,1,"");
145 } else {
146 fSpecFormat="_0x%08x";
147 }
148
6daf06e2 149 // -blocknofmt
150 } else if (argument.BeginsWith("-blcknofmt") ||
151 argument.BeginsWith("-blocknofmt")) {
152 // for the sake of backward compatibility we consider also the
153 // old argument with typo for a while
5ab95e9a 154 argument.ReplaceAll("-blcknofmt", "");
6daf06e2 155 argument.ReplaceAll("-blocknofmt", "");
5ab95e9a 156 if (argument.BeginsWith("=")) {
157 fBlcknoFormat=argument.Replace(0,1,"");
158 } else {
159 fBlcknoFormat="_0x%02x";
160 }
161
e74abd18 162 // -enumeration
163 } else if (argument.CompareTo("-enumerate")==0) {
164 SetMode(kEnumerate);
165
166 // -concatenate-blocks
167 } else if (argument.CompareTo("-concatenate-blocks")==0) {
168 SetMode(kConcatenateBlocks);
169
170 // -concatenate-events
171 } else if (argument.CompareTo("-concatenate-events")==0) {
172 SetMode(kConcatenateEvents);
173
6b7f77cf 174 // -write-all-events
175 } else if (argument.CompareTo("-write-all-events")==0) {
176 SetMode(kWriteAllEvents);
177
178 // -write-all-blocks
179 } else if (argument.CompareTo("-write-all-blocks")==0) {
180 SetMode(kWriteAllBlocks);
181
182 // -write-all
183 } else if (argument.CompareTo("-write-all")==0) {
184 SetMode(kWriteAllEvents);
185 SetMode(kWriteAllBlocks);
186
2cb9ea42 187 // -skip-datatype
188 } else if(argument.CompareTo("-skip-datatype")==0){
189 SetMode(kSkipDataType);
190
e74abd18 191 } else {
192 if ((iResult=ScanArgument(argc-i, &argv[i]))==-EINVAL) {
193 HLTError("unknown argument %s", argument.Data());
194 break;
195 } else if (iResult==-EPROTO) {
196 bMissingParam=1;
197 break;
13ce8aae 198 } else if (iResult>0) {
199 i+=iResult-1;
e74abd18 200 iResult=0;
201 }
202 }
203 }
204 if (bMissingParam) {
205 HLTError("missing parameter for argument %s", argument.Data());
206 iResult=-EINVAL;
207 }
208 if (iResult>=0) {
209 iResult=InitWriter();
07cd8230 210 if (!fDirectory.IsNull()) {
211 gSystem->mkdir(fDirectory);
212 }
e74abd18 213 }
214
215 return iResult;
216}
217
218int AliHLTFileWriter::InitWriter()
219{
220 // see header file for class documentation
5df0cbb9 221
222 // fCurrentFileName is used in dump event, just touched her to avoid
223 // coding convention violation RC11. The function can not be declared
224 // const since it is just the default implementation, overloaded
225 // virtual function might not be const
226 fCurrentFileName="";
e74abd18 227 return 0; // note: this doesn't mean 'error'
228}
229
6daf06e2 230int AliHLTFileWriter::ScanArgument(int /*argc*/, const char** /*argv*/)
e74abd18 231{
232 // see header file for class documentation
233
234 // there are no other arguments than the standard ones
5df0cbb9 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="";
e74abd18 240 return -EINVAL;
241}
242
243int AliHLTFileWriter::DoDeinit()
244{
245 // see header file for class documentation
246 int iResult=CloseWriter();
247 ClearMode(kEnumerate);
248 return iResult;
249}
250
251int AliHLTFileWriter::CloseWriter()
252{
253 // see header file for class documentation
5df0cbb9 254
255 // fCurrentFileName is used in dump event, just touched her to avoid
256 // coding convention violation RC11. The function can not be declared
257 // const since it is just the default implementation, overloaded
258 // virtual function might not be const
259 fCurrentFileName="";
6daf06e2 260 return 0;
e74abd18 261}
262
263int AliHLTFileWriter::DumpEvent( const AliHLTComponentEventData& evtData,
fdb4ca9e 264 AliHLTComponentTriggerData& /*trigData*/ )
e74abd18 265{
266 // see header file for class documentation
267 int iResult=0;
6b7f77cf 268 if (!IsDataEvent() && !CheckMode(kWriteAllEvents)) return 0;
269
e74abd18 270 if (CheckMode(kConcatenateEvents)==0) {
271 // reset the current file name in order to open a new file
272 // for the first block. If events are concatenated, the current
273 // file name stays in order to be opended in append mode.
274 fCurrentFileName="";
275 }
fdb4ca9e 276 const AliHLTComponentBlockData* pDesc=NULL;
277
278 int blockno=0;
6daf06e2 279 for (pDesc=GetFirstInputBlock(); pDesc!=NULL; pDesc=GetNextInputBlock(), blockno++) {
6b7f77cf 280 if (pDesc->fDataType==(kAliHLTAnyDataType|kAliHLTDataOriginPrivate) && !CheckMode(kWriteAllBlocks))
281 continue;
fdb4ca9e 282 HLTDebug("block %d out of %d", blockno, evtData.fBlockCnt);
e74abd18 283 TString filename;
fdb4ca9e 284 HLTDebug("dataspec 0x%x", pDesc->fSpecification);
285 iResult=BuildFileName(evtData.fEventID, blockno, pDesc->fDataType, pDesc->fSpecification, filename);
e74abd18 286 ios::openmode filemode=(ios::openmode)0;
287 if (fCurrentFileName.CompareTo(filename)==0) {
288 // append to the file
289 filemode=ios::app;
290 } else {
291 // store the file for the next block
292 fCurrentFileName=filename;
293 }
294 if (iResult>=0) {
295 ofstream dump(filename.Data(), filemode);
296 if (dump.good()) {
fdb4ca9e 297 dump.write((static_cast<const char*>(pDesc->fPtr)), pDesc->fSize);
298 HLTDebug("wrote %d byte(s) to file %s", pDesc->fSize, filename.Data());
e74abd18 299 } else {
300 HLTError("can not open file %s for writing", filename.Data());
301 iResult=-EBADF;
302 }
303 dump.close();
304 }
305 }
e74abd18 306 return iResult;
307}
308
5ab95e9a 309int AliHLTFileWriter::BuildFileName(const AliHLTEventID_t eventID, const int blockID,
310 const AliHLTComponentDataType& dataType,
311 const AliHLTUInt32_t specification,
312 TString& filename)
e74abd18 313{
314 // see header file for class documentation
315 int iResult=0;
316 //HLTDebug("build file name for event %d block %d", eventID, blockID);
317 filename="";
6b7f77cf 318
319 AliHLTUInt32_t eventType=gkAliEventTypeUnknown;
6b7f77cf 320
e74abd18 321 if (!fDirectory.IsNull()) {
322 filename+=fDirectory;
5ab95e9a 323 if (!filename.EndsWith("/"))
e74abd18 324 filename+="/";
325 }
5ab95e9a 326 if (!fSubDirFormat.IsNull()) {
327 filename+=Form(fSubDirFormat, eventID);
328 if (!filename.EndsWith("/"))
329 filename+="/";
330 }
331 if (filename.EndsWith("/")) {
332 gSystem->mkdir(filename);
333 }
488eca05 334
335 IsDataEvent(&eventType);
336 if (eventType==gkAliEventTypeStartOfRun && CheckMode(kWriteAllEvents)) filename+="SOR_";
337 else if (eventType==gkAliEventTypeEndOfRun && CheckMode(kWriteAllEvents)) filename+="EOR_";
338
e74abd18 339 if (!fBaseName.IsNull())
340 filename+=fBaseName;
341 else
342 filename+="event";
343 if (!CheckMode(kConcatenateEvents)) {
344 if (!CheckMode(kEnumerate)) {
5ab95e9a 345 if (eventID!=kAliHLTVoidEventID && !fIdFormat.IsNull()) {
346 filename+=Form(fIdFormat, eventID);
e74abd18 347 }
348 } else {
349 filename+=Form("_%d", GetEventCount());
350 }
351 }
352 if (blockID>=0 && !CheckMode(kConcatenateBlocks)) {
5ab95e9a 353 if (!fBlcknoFormat.IsNull())
354 filename+=Form(fBlcknoFormat, blockID);
2cb9ea42 355 if (dataType!=kAliHLTVoidDataType &&
356 !CheckMode(kSkipDataType)) {
e74abd18 357 filename+="_";
358 filename+=AliHLTComponent::DataType2Text(dataType).data();
359 }
5ab95e9a 360 if (!fSpecFormat.IsNull())
361 filename+=Form(fSpecFormat, specification);
e74abd18 362 }
363 if (!fExtension.IsNull())
364 filename+="." + fExtension;
365 filename.ReplaceAll(" ", "");
366 return iResult;
367}
368
369int AliHLTFileWriter::SetMode(Short_t mode)
370{
371 // see header file for class documentation
372 fMode|=mode;
373 //HLTDebug("mode set to 0x%x", fMode);
374 return fMode;
375}
376
377int AliHLTFileWriter::ClearMode(Short_t mode)
378{
379 // see header file for class documentation
380 fMode&=~mode;
381 //HLTDebug("mode set to 0x%x", fMode);
382 return fMode;
383}
384
5df0cbb9 385int AliHLTFileWriter::CheckMode(Short_t mode) const
e74abd18 386{
387 // see header file for class documentation
388
389 //HLTDebug("check mode 0x%x for flag 0x%x: %d", fMode, mode, (fMode&mode)!=0);
390 return (fMode&mode)!=0;
391}