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>
34 /** the global object for component registration */
35 AliHLTFileWriter gAliHLTFileWriter;
37 /** ROOT macro for the implementation of ROOT specific class methods */
38 ClassImp(AliHLTFileWriter)
40 AliHLTFileWriter::AliHLTFileWriter()
49 // see header file for class documentation
51 // refer to README to build package
53 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
56 AliHLTFileWriter::AliHLTFileWriter(const AliHLTFileWriter&)
65 // see header file for class documentation
66 HLTFatal("copy constructor untested");
69 AliHLTFileWriter& AliHLTFileWriter::operator=(const AliHLTFileWriter&)
71 // see header file for class documentation
72 HLTFatal("assignment operator untested");
76 AliHLTFileWriter::~AliHLTFileWriter()
78 // see header file for class documentation
80 // file list and file name list are owner of their objects and
81 // delete all the objects
84 const char* AliHLTFileWriter::GetComponentID()
86 // see header file for class documentation
90 void AliHLTFileWriter::GetInputDataTypes( vector<AliHLTComponentDataType>& list)
92 // see header file for class documentation
94 list.push_back(kAliHLTAnyDataType);
97 AliHLTComponent* AliHLTFileWriter::Spawn()
99 // see header file for class documentation
100 return new AliHLTFileWriter;
103 int AliHLTFileWriter::DoInit( int argc, const char** argv )
105 // see header file for class documentation
109 for (int i=0; i<argc && iResult>=0; i++) {
111 if (argument.IsNull()) continue;
114 if (argument.CompareTo("-datafile")==0) {
115 if ((bMissingParam=(++i>=argc))) break;
117 TObjArray* pTokens=fBaseName.Tokenize(".");
119 int iEntries=pTokens->GetEntries();
122 fBaseName=((TObjString*)pTokens->At(i++))->GetString();
123 while (i<iEntries-1) {
124 fBaseName+="." + ((TObjString*)pTokens->At(i++))->GetString();
126 fExtension=((TObjString*)pTokens->At(i))->GetString();
132 } else if (argument.CompareTo("-directory")==0) {
133 if ((bMissingParam=(++i>=argc))) break;
137 } else if (argument.CompareTo("-enumerate")==0) {
140 // -concatenate-blocks
141 } else if (argument.CompareTo("-concatenate-blocks")==0) {
142 SetMode(kConcatenateBlocks);
144 // -concatenate-events
145 } else if (argument.CompareTo("-concatenate-events")==0) {
146 SetMode(kConcatenateEvents);
149 if ((iResult=ScanArgument(argc-i, &argv[i]))==-EINVAL) {
150 HLTError("unknown argument %s", argument.Data());
152 } else if (iResult==-EPROTO) {
155 } else if (iResult>=0) {
162 HLTError("missing parameter for argument %s", argument.Data());
166 iResult=InitWriter();
172 int AliHLTFileWriter::InitWriter()
174 // see header file for class documentation
176 // fCurrentFileName is used in dump event, just touched her to avoid
177 // coding convention violation RC11. The function can not be declared
178 // const since it is just the default implementation, overloaded
179 // virtual function might not be const
181 return 0; // note: this doesn't mean 'error'
184 int AliHLTFileWriter::ScanArgument(int argc, const char** argv)
186 // see header file for class documentation
188 // there are no other arguments than the standard ones
189 if (argc==0 && argv==NULL) {
190 // this is just to get rid of the warning "unused parameter"
192 // fCurrentFileName is used in dump event, just touched her to avoid
193 // coding convention violation RC11. The function can not be declared
194 // const since it is just the default implementation, overloaded
195 // virtual function might not be const
200 int AliHLTFileWriter::DoDeinit()
202 // see header file for class documentation
203 int iResult=CloseWriter();
204 ClearMode(kEnumerate);
208 int AliHLTFileWriter::CloseWriter()
210 // see header file for class documentation
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
217 return 0; // note: this doesn't mean 'error'
220 int AliHLTFileWriter::DumpEvent( const AliHLTComponentEventData& evtData,
221 const AliHLTComponentBlockData* blocks,
222 AliHLTComponentTriggerData& trigData )
224 // see header file for class documentation
226 if (CheckMode(kConcatenateEvents)==0) {
227 // reset the current file name in order to open a new file
228 // for the first block. If events are concatenated, the current
229 // file name stays in order to be opended in append mode.
232 for (int n=0; n<(int)evtData.fBlockCnt; n++ ) {
233 //HLTDebug("block %d out of %d", n, evtData.fBlockCnt);
235 HLTDebug("dataspec 0x%x", blocks[n].fSpecification);
236 iResult=BuildFileName(evtData.fEventID, n, blocks[n].fDataType, filename);
237 ios::openmode filemode=(ios::openmode)0;
238 if (fCurrentFileName.CompareTo(filename)==0) {
239 // append to the file
242 // store the file for the next block
243 fCurrentFileName=filename;
246 ofstream dump(filename.Data(), filemode);
248 dump.write((const char*)blocks[n].fPtr, blocks[n].fSize);
249 HLTDebug("wrote %d byte(s) to file %s", blocks[n].fSize, filename.Data());
251 HLTError("can not open file %s for writing", filename.Data());
257 if (trigData.fStructSize==0) {
258 // this is just to get rid of the warning "unused parameter"
263 int AliHLTFileWriter::BuildFileName(const AliHLTEventID_t eventID, const int blockID, const AliHLTComponentDataType& dataType, TString& filename)
265 // see header file for class documentation
267 //HLTDebug("build file name for event %d block %d", eventID, blockID);
269 if (!fDirectory.IsNull()) {
270 filename+=fDirectory;
271 if (!fDirectory.EndsWith("/"))
274 if (!fBaseName.IsNull())
278 if (!CheckMode(kConcatenateEvents)) {
279 if (!CheckMode(kEnumerate)) {
280 if (eventID!=kAliHLTVoidEventID) {
281 filename+=Form("_0x%08x", eventID);
284 filename+=Form("_%d", GetEventCount());
287 if (blockID>=0 && !CheckMode(kConcatenateBlocks)) {
288 filename+=Form("_0x%x", blockID);
289 if (dataType!=kAliHLTVoidDataType) {
291 filename+=AliHLTComponent::DataType2Text(dataType).data();
294 if (!fExtension.IsNull())
295 filename+="." + fExtension;
296 filename.ReplaceAll(" ", "");
300 int AliHLTFileWriter::SetMode(Short_t mode)
302 // see header file for class documentation
304 //HLTDebug("mode set to 0x%x", fMode);
308 int AliHLTFileWriter::ClearMode(Short_t mode)
310 // see header file for class documentation
312 //HLTDebug("mode set to 0x%x", fMode);
316 int AliHLTFileWriter::CheckMode(Short_t mode) const
318 // see header file for class documentation
320 //HLTDebug("check mode 0x%x for flag 0x%x: %d", fMode, mode, (fMode&mode)!=0);
321 return (fMode&mode)!=0;