3 /**************************************************************************
4 * This file is property of and copyright by the ALICE HLT Project *
5 * ALICE Experiment at CERN, All rights reserved. *
7 * Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no> *
8 * for The ALICE HLT Project. *
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 **************************************************************************/
19 /** @file AliHLTFileWriter.cxx
20 @author Matthias Richter
22 @brief HLT file writer component implementation. */
28 #include "AliHLTFileWriter.h"
29 #include <TObjArray.h>
30 #include <TObjString.h>
35 /** the global object for component registration */
36 AliHLTFileWriter gAliHLTFileWriter;
38 /** ROOT macro for the implementation of ROOT specific class methods */
39 ClassImp(AliHLTFileWriter)
41 AliHLTFileWriter::AliHLTFileWriter()
54 // see header file for class documentation
56 // refer to README to build package
58 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
61 AliHLTFileWriter::~AliHLTFileWriter()
63 // see header file for class documentation
65 // file list and file name list are owner of their objects and
66 // delete all the objects
69 const char* AliHLTFileWriter::GetComponentID()
71 // see header file for class documentation
75 void AliHLTFileWriter::GetInputDataTypes( vector<AliHLTComponentDataType>& list)
77 // see header file for class documentation
79 list.push_back(kAliHLTAnyDataType);
82 AliHLTComponent* AliHLTFileWriter::Spawn()
84 // see header file for class documentation
85 return new AliHLTFileWriter;
88 int AliHLTFileWriter::DoInit( int argc, const char** argv )
90 // see header file for class documentation
94 for (int i=0; i<argc && iResult>=0; i++) {
96 if (argument.IsNull()) continue;
99 if (argument.CompareTo("-datafile")==0) {
100 if ((bMissingParam=(++i>=argc))) break;
102 TObjArray* pTokens=fBaseName.Tokenize(".");
104 int iEntries=pTokens->GetEntries();
107 fBaseName=((TObjString*)pTokens->At(i++))->GetString();
108 while (i<iEntries-1) {
109 fBaseName+="." + ((TObjString*)pTokens->At(i++))->GetString();
111 fExtension=((TObjString*)pTokens->At(i))->GetString();
117 } else if (argument.CompareTo("-directory")==0) {
118 if ((bMissingParam=(++i>=argc))) break;
122 } else if (argument.BeginsWith("-subdir")) {
123 argument.ReplaceAll("-subdir", "");
124 if (argument.BeginsWith("=")) {
125 fSubDirFormat=argument.Replace(0,1,"");
127 fSubDirFormat="event%03d";
131 } else if (argument.BeginsWith("-idfmt")) {
132 argument.ReplaceAll("-idfmt", "");
133 if (argument.BeginsWith("=")) {
134 fIdFormat=argument.Replace(0,1,"");
138 } else if (argument.BeginsWith("-specfmt")) {
139 argument.ReplaceAll("-specfmt", "");
140 if (argument.BeginsWith("=")) {
141 fSpecFormat=argument.Replace(0,1,"");
143 fSpecFormat="_0x%08x";
147 } else if (argument.BeginsWith("-blcknofmt")) {
148 argument.ReplaceAll("-blcknofmt", "");
149 if (argument.BeginsWith("=")) {
150 fBlcknoFormat=argument.Replace(0,1,"");
152 fBlcknoFormat="_0x%02x";
156 } else if (argument.CompareTo("-enumerate")==0) {
159 // -concatenate-blocks
160 } else if (argument.CompareTo("-concatenate-blocks")==0) {
161 SetMode(kConcatenateBlocks);
163 // -concatenate-events
164 } else if (argument.CompareTo("-concatenate-events")==0) {
165 SetMode(kConcatenateEvents);
168 if ((iResult=ScanArgument(argc-i, &argv[i]))==-EINVAL) {
169 HLTError("unknown argument %s", argument.Data());
171 } else if (iResult==-EPROTO) {
174 } else if (iResult>=0) {
181 HLTError("missing parameter for argument %s", argument.Data());
185 if (fIdFormat.IsNull() && fSubDirFormat.IsNull()) {
186 // set the default format string for the id if it is not set and
187 // no sub dirs set (the sub dir than contains the id)
190 iResult=InitWriter();
196 int AliHLTFileWriter::InitWriter()
198 // see header file for class documentation
200 // fCurrentFileName is used in dump event, just touched her to avoid
201 // coding convention violation RC11. The function can not be declared
202 // const since it is just the default implementation, overloaded
203 // virtual function might not be const
205 return 0; // note: this doesn't mean 'error'
208 int AliHLTFileWriter::ScanArgument(int argc, const char** argv)
210 // see header file for class documentation
212 // there are no other arguments than the standard ones
213 if (argc==0 && argv==NULL) {
214 // this is just to get rid of the warning "unused parameter"
216 // fCurrentFileName is used in dump event, just touched her to avoid
217 // coding convention violation RC11. The function can not be declared
218 // const since it is just the default implementation, overloaded
219 // virtual function might not be const
224 int AliHLTFileWriter::DoDeinit()
226 // see header file for class documentation
227 int iResult=CloseWriter();
228 ClearMode(kEnumerate);
232 int AliHLTFileWriter::CloseWriter()
234 // see header file for class documentation
236 // fCurrentFileName is used in dump event, just touched her to avoid
237 // coding convention violation RC11. The function can not be declared
238 // const since it is just the default implementation, overloaded
239 // virtual function might not be const
241 return 0; // note: this doesn't mean 'error'
244 int AliHLTFileWriter::DumpEvent( const AliHLTComponentEventData& evtData,
245 const AliHLTComponentBlockData* blocks,
246 AliHLTComponentTriggerData& trigData )
248 // see header file for class documentation
250 if (CheckMode(kConcatenateEvents)==0) {
251 // reset the current file name in order to open a new file
252 // for the first block. If events are concatenated, the current
253 // file name stays in order to be opended in append mode.
256 for (int n=0; n<(int)evtData.fBlockCnt; n++ ) {
257 //HLTDebug("block %d out of %d", n, evtData.fBlockCnt);
259 HLTDebug("dataspec 0x%x", blocks[n].fSpecification);
260 iResult=BuildFileName(evtData.fEventID, n, blocks[n].fDataType, blocks[n].fSpecification, filename);
261 ios::openmode filemode=(ios::openmode)0;
262 if (fCurrentFileName.CompareTo(filename)==0) {
263 // append to the file
266 // store the file for the next block
267 fCurrentFileName=filename;
270 ofstream dump(filename.Data(), filemode);
272 dump.write((static_cast<const char*>(blocks[n].fPtr))+blocks[n].fOffset, blocks[n].fSize);
273 HLTDebug("wrote %d byte(s) to file %s", blocks[n].fSize, filename.Data());
275 HLTError("can not open file %s for writing", filename.Data());
281 if (trigData.fStructSize==0) {
282 // this is just to get rid of the warning "unused parameter"
287 int AliHLTFileWriter::BuildFileName(const AliHLTEventID_t eventID, const int blockID,
288 const AliHLTComponentDataType& dataType,
289 const AliHLTUInt32_t specification,
292 // see header file for class documentation
294 //HLTDebug("build file name for event %d block %d", eventID, blockID);
296 if (!fDirectory.IsNull()) {
297 filename+=fDirectory;
298 if (!filename.EndsWith("/"))
301 if (!fSubDirFormat.IsNull()) {
302 filename+=Form(fSubDirFormat, eventID);
303 if (!filename.EndsWith("/"))
306 if (filename.EndsWith("/")) {
307 gSystem->mkdir(filename);
309 if (!fBaseName.IsNull())
313 if (!CheckMode(kConcatenateEvents)) {
314 if (!CheckMode(kEnumerate)) {
315 if (eventID!=kAliHLTVoidEventID && !fIdFormat.IsNull()) {
316 filename+=Form(fIdFormat, eventID);
319 filename+=Form("_%d", GetEventCount());
322 if (blockID>=0 && !CheckMode(kConcatenateBlocks)) {
323 if (!fBlcknoFormat.IsNull())
324 filename+=Form(fBlcknoFormat, blockID);
325 if (dataType!=kAliHLTVoidDataType) {
327 filename+=AliHLTComponent::DataType2Text(dataType).data();
329 if (!fSpecFormat.IsNull())
330 filename+=Form(fSpecFormat, specification);
332 if (!fExtension.IsNull())
333 filename+="." + fExtension;
334 filename.ReplaceAll(" ", "");
338 int AliHLTFileWriter::SetMode(Short_t mode)
340 // see header file for class documentation
342 //HLTDebug("mode set to 0x%x", fMode);
346 int AliHLTFileWriter::ClearMode(Short_t mode)
348 // see header file for class documentation
350 //HLTDebug("mode set to 0x%x", fMode);
354 int AliHLTFileWriter::CheckMode(Short_t mode) const
356 // see header file for class documentation
358 //HLTDebug("check mode 0x%x for flag 0x%x: %d", fMode, mode, (fMode&mode)!=0);
359 return (fMode&mode)!=0;