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()
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(kAliHLTAnyDataType);
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";
128 } else if (argument.BeginsWith("-idfmt")) {
129 argument.ReplaceAll("-idfmt", "");
130 if (argument.BeginsWith("=")) {
131 fIdFormat=argument.Replace(0,1,"");
135 } else if (argument.BeginsWith("-specfmt")) {
136 argument.ReplaceAll("-specfmt", "");
137 if (argument.BeginsWith("=")) {
138 fSpecFormat=argument.Replace(0,1,"");
140 fSpecFormat="_0x%08x";
144 } else if (argument.BeginsWith("-blcknofmt")) {
145 argument.ReplaceAll("-blcknofmt", "");
146 if (argument.BeginsWith("=")) {
147 fBlcknoFormat=argument.Replace(0,1,"");
149 fBlcknoFormat="_0x%02x";
153 } else if (argument.CompareTo("-enumerate")==0) {
156 // -concatenate-blocks
157 } else if (argument.CompareTo("-concatenate-blocks")==0) {
158 SetMode(kConcatenateBlocks);
160 // -concatenate-events
161 } else if (argument.CompareTo("-concatenate-events")==0) {
162 SetMode(kConcatenateEvents);
165 if ((iResult=ScanArgument(argc-i, &argv[i]))==-EINVAL) {
166 HLTError("unknown argument %s", argument.Data());
168 } else if (iResult==-EPROTO) {
171 } else if (iResult>=0) {
178 HLTError("missing parameter for argument %s", argument.Data());
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)
187 iResult=InitWriter();
193 int AliHLTFileWriter::InitWriter()
195 // see header file for class documentation
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
202 return 0; // note: this doesn't mean 'error'
205 int AliHLTFileWriter::ScanArgument(int argc, const char** argv)
207 // see header file for class documentation
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"
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
221 int AliHLTFileWriter::DoDeinit()
223 // see header file for class documentation
224 int iResult=CloseWriter();
225 ClearMode(kEnumerate);
229 int AliHLTFileWriter::CloseWriter()
231 // see header file for class documentation
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
238 return 0; // note: this doesn't mean 'error'
241 int AliHLTFileWriter::DumpEvent( const AliHLTComponentEventData& evtData,
242 const AliHLTComponentBlockData* blocks,
243 AliHLTComponentTriggerData& trigData )
245 // see header file for class documentation
247 if (CheckMode(kConcatenateEvents)==0) {
248 // reset the current file name in order to open a new file
249 // for the first block. If events are concatenated, the current
250 // file name stays in order to be opended in append mode.
253 for (int n=0; n<(int)evtData.fBlockCnt; n++ ) {
254 //HLTDebug("block %d out of %d", n, evtData.fBlockCnt);
256 //HLTDebug("dataspec 0x%x", blocks[n].fSpecification);
257 iResult=BuildFileName(evtData.fEventID, n, blocks[n].fDataType, blocks[n].fSpecification, filename);
258 ios::openmode filemode=(ios::openmode)0;
259 if (fCurrentFileName.CompareTo(filename)==0) {
260 // append to the file
263 // store the file for the next block
264 fCurrentFileName=filename;
267 ofstream dump(filename.Data(), filemode);
269 dump.write((static_cast<const char*>(blocks[n].fPtr)), blocks[n].fSize);
270 HLTDebug("wrote %d byte(s) to file %s", blocks[n].fSize, filename.Data());
272 HLTError("can not open file %s for writing", filename.Data());
278 if (trigData.fStructSize==0) {
279 // this is just to get rid of the warning "unused parameter"
284 int AliHLTFileWriter::BuildFileName(const AliHLTEventID_t eventID, const int blockID,
285 const AliHLTComponentDataType& dataType,
286 const AliHLTUInt32_t specification,
289 // see header file for class documentation
291 //HLTDebug("build file name for event %d block %d", eventID, blockID);
293 if (!fDirectory.IsNull()) {
294 filename+=fDirectory;
295 if (!filename.EndsWith("/"))
298 if (!fSubDirFormat.IsNull()) {
299 filename+=Form(fSubDirFormat, eventID);
300 if (!filename.EndsWith("/"))
303 if (filename.EndsWith("/")) {
304 gSystem->mkdir(filename);
306 if (!fBaseName.IsNull())
310 if (!CheckMode(kConcatenateEvents)) {
311 if (!CheckMode(kEnumerate)) {
312 if (eventID!=kAliHLTVoidEventID && !fIdFormat.IsNull()) {
313 filename+=Form(fIdFormat, eventID);
316 filename+=Form("_%d", GetEventCount());
319 if (blockID>=0 && !CheckMode(kConcatenateBlocks)) {
320 if (!fBlcknoFormat.IsNull())
321 filename+=Form(fBlcknoFormat, blockID);
322 if (dataType!=kAliHLTVoidDataType) {
324 filename+=AliHLTComponent::DataType2Text(dataType).data();
326 if (!fSpecFormat.IsNull())
327 filename+=Form(fSpecFormat, specification);
329 if (!fExtension.IsNull())
330 filename+="." + fExtension;
331 filename.ReplaceAll(" ", "");
335 int AliHLTFileWriter::SetMode(Short_t mode)
337 // see header file for class documentation
339 //HLTDebug("mode set to 0x%x", fMode);
343 int AliHLTFileWriter::ClearMode(Short_t mode)
345 // see header file for class documentation
347 //HLTDebug("mode set to 0x%x", fMode);
351 int AliHLTFileWriter::CheckMode(Short_t mode) const
353 // see header file for class documentation
355 //HLTDebug("check mode 0x%x for flag 0x%x: %d", fMode, mode, (fMode&mode)!=0);
356 return (fMode&mode)!=0;