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 /** ROOT macro for the implementation of ROOT specific class methods */
36 ClassImp(AliHLTFileWriter)
38 AliHLTFileWriter::AliHLTFileWriter()
47 fBlcknoFormat("_0x%02x"),
51 // see header file for class documentation
53 // refer to README to build package
55 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
58 AliHLTFileWriter::~AliHLTFileWriter()
60 // see header file for class documentation
62 // file list and file name list are owner of their objects and
63 // delete all the objects
66 const char* AliHLTFileWriter::GetComponentID()
68 // see header file for class documentation
72 void AliHLTFileWriter::GetInputDataTypes( vector<AliHLTComponentDataType>& list)
74 // see header file for class documentation
76 list.push_back(kAliHLTAllDataTypes);
79 AliHLTComponent* AliHLTFileWriter::Spawn()
81 // see header file for class documentation
82 return new AliHLTFileWriter;
85 int AliHLTFileWriter::DoInit( int argc, const char** argv )
87 // see header file for class documentation
91 for (int i=0; i<argc && iResult>=0; i++) {
93 if (argument.IsNull()) continue;
96 if (argument.CompareTo("-datafile")==0) {
97 if ((bMissingParam=(++i>=argc))) break;
99 TObjArray* pTokens=fBaseName.Tokenize(".");
101 int iEntries=pTokens->GetEntries();
104 fBaseName=((TObjString*)pTokens->At(i++))->GetString();
105 while (i<iEntries-1) {
106 fBaseName+="." + ((TObjString*)pTokens->At(i++))->GetString();
108 fExtension=((TObjString*)pTokens->At(i))->GetString();
114 } else if (argument.CompareTo("-directory")==0) {
115 if ((bMissingParam=(++i>=argc))) break;
119 } else if (argument.BeginsWith("-subdir")) {
120 argument.ReplaceAll("-subdir", "");
121 if (argument.BeginsWith("=")) {
122 fSubDirFormat=argument.Replace(0,1,"");
124 fSubDirFormat="event%03d";
126 // no additional eventno in the filename unless set again
127 // the sub dir contains the id
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.BeginsWith("-blocknofmt")) {
149 // for the sake of backward compatibility we consider also the
150 // old argument with typo for a while
151 argument.ReplaceAll("-blcknofmt", "");
152 argument.ReplaceAll("-blocknofmt", "");
153 if (argument.BeginsWith("=")) {
154 fBlcknoFormat=argument.Replace(0,1,"");
156 fBlcknoFormat="_0x%02x";
160 } else if (argument.CompareTo("-enumerate")==0) {
163 // -concatenate-blocks
164 } else if (argument.CompareTo("-concatenate-blocks")==0) {
165 SetMode(kConcatenateBlocks);
167 // -concatenate-events
168 } else if (argument.CompareTo("-concatenate-events")==0) {
169 SetMode(kConcatenateEvents);
172 if ((iResult=ScanArgument(argc-i, &argv[i]))==-EINVAL) {
173 HLTError("unknown argument %s", argument.Data());
175 } else if (iResult==-EPROTO) {
178 } else if (iResult>=0) {
185 HLTError("missing parameter for argument %s", argument.Data());
189 iResult=InitWriter();
195 int AliHLTFileWriter::InitWriter()
197 // see header file for class documentation
199 // fCurrentFileName is used in dump event, just touched her to avoid
200 // coding convention violation RC11. The function can not be declared
201 // const since it is just the default implementation, overloaded
202 // virtual function might not be const
204 return 0; // note: this doesn't mean 'error'
207 int AliHLTFileWriter::ScanArgument(int /*argc*/, const char** /*argv*/)
209 // see header file for class documentation
211 // there are no other arguments than the standard ones
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
220 int AliHLTFileWriter::DoDeinit()
222 // see header file for class documentation
223 int iResult=CloseWriter();
224 ClearMode(kEnumerate);
228 int AliHLTFileWriter::CloseWriter()
230 // see header file for class documentation
232 // fCurrentFileName is used in dump event, just touched her to avoid
233 // coding convention violation RC11. The function can not be declared
234 // const since it is just the default implementation, overloaded
235 // virtual function might not be const
240 int AliHLTFileWriter::DumpEvent( const AliHLTComponentEventData& evtData,
241 AliHLTComponentTriggerData& /*trigData*/ )
243 // see header file for class documentation
245 if (CheckMode(kConcatenateEvents)==0) {
246 // reset the current file name in order to open a new file
247 // for the first block. If events are concatenated, the current
248 // file name stays in order to be opended in append mode.
251 const AliHLTComponentBlockData* pDesc=NULL;
254 for (pDesc=GetFirstInputBlock(); pDesc!=NULL; pDesc=GetNextInputBlock(), blockno++) {
255 HLTDebug("block %d out of %d", blockno, evtData.fBlockCnt);
257 HLTDebug("dataspec 0x%x", pDesc->fSpecification);
258 iResult=BuildFileName(evtData.fEventID, blockno, pDesc->fDataType, pDesc->fSpecification, filename);
259 ios::openmode filemode=(ios::openmode)0;
260 if (fCurrentFileName.CompareTo(filename)==0) {
261 // append to the file
264 // store the file for the next block
265 fCurrentFileName=filename;
268 ofstream dump(filename.Data(), filemode);
270 dump.write((static_cast<const char*>(pDesc->fPtr)), pDesc->fSize);
271 HLTDebug("wrote %d byte(s) to file %s", pDesc->fSize, filename.Data());
273 HLTError("can not open file %s for writing", filename.Data());
282 int AliHLTFileWriter::BuildFileName(const AliHLTEventID_t eventID, const int blockID,
283 const AliHLTComponentDataType& dataType,
284 const AliHLTUInt32_t specification,
287 // see header file for class documentation
289 //HLTDebug("build file name for event %d block %d", eventID, blockID);
291 if (!fDirectory.IsNull()) {
292 filename+=fDirectory;
293 if (!filename.EndsWith("/"))
296 if (!fSubDirFormat.IsNull()) {
297 filename+=Form(fSubDirFormat, eventID);
298 if (!filename.EndsWith("/"))
301 if (filename.EndsWith("/")) {
302 gSystem->mkdir(filename);
304 if (!fBaseName.IsNull())
308 if (!CheckMode(kConcatenateEvents)) {
309 if (!CheckMode(kEnumerate)) {
310 if (eventID!=kAliHLTVoidEventID && !fIdFormat.IsNull()) {
311 filename+=Form(fIdFormat, eventID);
314 filename+=Form("_%d", GetEventCount());
317 if (blockID>=0 && !CheckMode(kConcatenateBlocks)) {
318 if (!fBlcknoFormat.IsNull())
319 filename+=Form(fBlcknoFormat, blockID);
320 if (dataType!=kAliHLTVoidDataType) {
322 filename+=AliHLTComponent::DataType2Text(dataType).data();
324 if (!fSpecFormat.IsNull())
325 filename+=Form(fSpecFormat, specification);
327 if (!fExtension.IsNull())
328 filename+="." + fExtension;
329 filename.ReplaceAll(" ", "");
333 int AliHLTFileWriter::SetMode(Short_t mode)
335 // see header file for class documentation
337 //HLTDebug("mode set to 0x%x", fMode);
341 int AliHLTFileWriter::ClearMode(Short_t mode)
343 // see header file for class documentation
345 //HLTDebug("mode set to 0x%x", fMode);
349 int AliHLTFileWriter::CheckMode(Short_t mode) const
351 // see header file for class documentation
353 //HLTDebug("check mode 0x%x for flag 0x%x: %d", fMode, mode, (fMode&mode)!=0);
354 return (fMode&mode)!=0;