- added libPHOSshuttle to PHOS libraries
[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(""),
45 fIdFormat(""),
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();
76 list.push_back(kAliHLTAnyDataType);
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) {
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
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 }
126
127 // -idfmt
128 } else if (argument.BeginsWith("-idfmt")) {
129 argument.ReplaceAll("-idfmt", "");
130 if (argument.BeginsWith("=")) {
131 fIdFormat=argument.Replace(0,1,"");
132 }
133
134 // -specfmt
135 } else if (argument.BeginsWith("-specfmt")) {
136 argument.ReplaceAll("-specfmt", "");
137 if (argument.BeginsWith("=")) {
138 fSpecFormat=argument.Replace(0,1,"");
139 } else {
140 fSpecFormat="_0x%08x";
141 }
142
143 // -blcknofmt
144 } else if (argument.BeginsWith("-blcknofmt")) {
145 argument.ReplaceAll("-blcknofmt", "");
146 if (argument.BeginsWith("=")) {
147 fBlcknoFormat=argument.Replace(0,1,"");
148 } else {
149 fBlcknoFormat="_0x%02x";
150 }
151
e74abd18 152 // -enumeration
153 } else if (argument.CompareTo("-enumerate")==0) {
154 SetMode(kEnumerate);
155
156 // -concatenate-blocks
157 } else if (argument.CompareTo("-concatenate-blocks")==0) {
158 SetMode(kConcatenateBlocks);
159
160 // -concatenate-events
161 } else if (argument.CompareTo("-concatenate-events")==0) {
162 SetMode(kConcatenateEvents);
163
164 } else {
165 if ((iResult=ScanArgument(argc-i, &argv[i]))==-EINVAL) {
166 HLTError("unknown argument %s", argument.Data());
167 break;
168 } else if (iResult==-EPROTO) {
169 bMissingParam=1;
170 break;
171 } else if (iResult>=0) {
172 i+=iResult;
173 iResult=0;
174 }
175 }
176 }
177 if (bMissingParam) {
178 HLTError("missing parameter for argument %s", argument.Data());
179 iResult=-EINVAL;
180 }
181 if (iResult>=0) {
5ab95e9a 182 if (fIdFormat.IsNull() && fSubDirFormat.IsNull()) {
183 // set the default format string for the id if it is not set and
184 // no sub dirs set (the sub dir than contains the id)
185 fIdFormat="_0x%08x";
186 }
e74abd18 187 iResult=InitWriter();
188 }
189
190 return iResult;
191}
192
193int AliHLTFileWriter::InitWriter()
194{
195 // see header file for class documentation
5df0cbb9 196
197 // fCurrentFileName is used in dump event, just touched her to avoid
198 // coding convention violation RC11. The function can not be declared
199 // const since it is just the default implementation, overloaded
200 // virtual function might not be const
201 fCurrentFileName="";
e74abd18 202 return 0; // note: this doesn't mean 'error'
203}
204
205int AliHLTFileWriter::ScanArgument(int argc, const char** argv)
206{
207 // see header file for class documentation
208
209 // there are no other arguments than the standard ones
210 if (argc==0 && argv==NULL) {
211 // this is just to get rid of the warning "unused parameter"
212 }
5df0cbb9 213 // fCurrentFileName is used in dump event, just touched her to avoid
214 // coding convention violation RC11. The function can not be declared
215 // const since it is just the default implementation, overloaded
216 // virtual function might not be const
217 fCurrentFileName="";
e74abd18 218 return -EINVAL;
219}
220
221int AliHLTFileWriter::DoDeinit()
222{
223 // see header file for class documentation
224 int iResult=CloseWriter();
225 ClearMode(kEnumerate);
226 return iResult;
227}
228
229int AliHLTFileWriter::CloseWriter()
230{
231 // see header file for class documentation
5df0cbb9 232
233 // fCurrentFileName is used in dump event, just touched her to avoid
234 // coding convention violation RC11. The function can not be declared
235 // const since it is just the default implementation, overloaded
236 // virtual function might not be const
237 fCurrentFileName="";
e74abd18 238 return 0; // note: this doesn't mean 'error'
239}
240
241int AliHLTFileWriter::DumpEvent( const AliHLTComponentEventData& evtData,
fdb4ca9e 242 AliHLTComponentTriggerData& /*trigData*/ )
e74abd18 243{
244 // see header file for class documentation
245 int iResult=0;
246 if (CheckMode(kConcatenateEvents)==0) {
247 // reset the current file name in order to open a new file
248 // for the first block. If events are concatenated, the current
249 // file name stays in order to be opended in append mode.
250 fCurrentFileName="";
251 }
fdb4ca9e 252 const AliHLTComponentBlockData* pDesc=NULL;
253
254 int blockno=0;
255 for (pDesc=GetFirstInputBlock(kAliHLTAnyDataType); pDesc!=NULL; pDesc=GetNextInputBlock(), blockno++) {
256 HLTDebug("block %d out of %d", blockno, evtData.fBlockCnt);
e74abd18 257 TString filename;
fdb4ca9e 258 HLTDebug("dataspec 0x%x", pDesc->fSpecification);
259 iResult=BuildFileName(evtData.fEventID, blockno, pDesc->fDataType, pDesc->fSpecification, filename);
e74abd18 260 ios::openmode filemode=(ios::openmode)0;
261 if (fCurrentFileName.CompareTo(filename)==0) {
262 // append to the file
263 filemode=ios::app;
264 } else {
265 // store the file for the next block
266 fCurrentFileName=filename;
267 }
268 if (iResult>=0) {
269 ofstream dump(filename.Data(), filemode);
270 if (dump.good()) {
fdb4ca9e 271 dump.write((static_cast<const char*>(pDesc->fPtr)), pDesc->fSize);
272 HLTDebug("wrote %d byte(s) to file %s", pDesc->fSize, filename.Data());
e74abd18 273 } else {
274 HLTError("can not open file %s for writing", filename.Data());
275 iResult=-EBADF;
276 }
277 dump.close();
278 }
279 }
e74abd18 280 return iResult;
281}
282
5ab95e9a 283int AliHLTFileWriter::BuildFileName(const AliHLTEventID_t eventID, const int blockID,
284 const AliHLTComponentDataType& dataType,
285 const AliHLTUInt32_t specification,
286 TString& filename)
e74abd18 287{
288 // see header file for class documentation
289 int iResult=0;
290 //HLTDebug("build file name for event %d block %d", eventID, blockID);
291 filename="";
292 if (!fDirectory.IsNull()) {
293 filename+=fDirectory;
5ab95e9a 294 if (!filename.EndsWith("/"))
e74abd18 295 filename+="/";
296 }
5ab95e9a 297 if (!fSubDirFormat.IsNull()) {
298 filename+=Form(fSubDirFormat, eventID);
299 if (!filename.EndsWith("/"))
300 filename+="/";
301 }
302 if (filename.EndsWith("/")) {
303 gSystem->mkdir(filename);
304 }
e74abd18 305 if (!fBaseName.IsNull())
306 filename+=fBaseName;
307 else
308 filename+="event";
309 if (!CheckMode(kConcatenateEvents)) {
310 if (!CheckMode(kEnumerate)) {
5ab95e9a 311 if (eventID!=kAliHLTVoidEventID && !fIdFormat.IsNull()) {
312 filename+=Form(fIdFormat, eventID);
e74abd18 313 }
314 } else {
315 filename+=Form("_%d", GetEventCount());
316 }
317 }
318 if (blockID>=0 && !CheckMode(kConcatenateBlocks)) {
5ab95e9a 319 if (!fBlcknoFormat.IsNull())
320 filename+=Form(fBlcknoFormat, blockID);
e74abd18 321 if (dataType!=kAliHLTVoidDataType) {
322 filename+="_";
323 filename+=AliHLTComponent::DataType2Text(dataType).data();
324 }
5ab95e9a 325 if (!fSpecFormat.IsNull())
326 filename+=Form(fSpecFormat, specification);
e74abd18 327 }
328 if (!fExtension.IsNull())
329 filename+="." + fExtension;
330 filename.ReplaceAll(" ", "");
331 return iResult;
332}
333
334int AliHLTFileWriter::SetMode(Short_t mode)
335{
336 // see header file for class documentation
337 fMode|=mode;
338 //HLTDebug("mode set to 0x%x", fMode);
339 return fMode;
340}
341
342int AliHLTFileWriter::ClearMode(Short_t mode)
343{
344 // see header file for class documentation
345 fMode&=~mode;
346 //HLTDebug("mode set to 0x%x", fMode);
347 return fMode;
348}
349
5df0cbb9 350int AliHLTFileWriter::CheckMode(Short_t mode) const
e74abd18 351{
352 // see header file for class documentation
353
354 //HLTDebug("check mode 0x%x for flag 0x%x: %d", fMode, mode, (fMode&mode)!=0);
355 return (fMode&mode)!=0;
356}