3 /**************************************************************************
4 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
6 * Authors: Matthias Richter <Matthias.Richter@ift.uib.no> *
7 * for The ALICE Off-line Project. *
9 * Permission to use, copy, modify and distribute this software and its *
10 * documentation strictly for non-commercial purposes is hereby granted *
11 * without fee, provided that the above copyright notice appears in all *
12 * copies and that both the copyright notice and this permission notice *
13 * appear in the supporting documentation. The authors make no claims *
14 * about the suitability of this software for any purpose. It is *
15 * provided "as is" without express or implied warranty. *
16 **************************************************************************/
18 /** @file AliHLTFileWriter.cxx
19 @author Matthias Richter
21 @brief HLT file writer component implementation. */
27 #include "AliHLTFileWriter.h"
28 #include <TObjArray.h>
29 #include <TObjString.h>
33 /** ROOT macro for the implementation of ROOT specific class methods */
34 ClassImp(AliHLTFileWriter)
36 AliHLTFileWriter::AliHLTFileWriter()
46 AliHLTFileWriter::AliHLTFileWriter(const AliHLTFileWriter&)
54 HLTFatal("copy constructor untested");
57 AliHLTFileWriter& AliHLTFileWriter::operator=(const AliHLTFileWriter&)
59 HLTFatal("assignment operator untested");
63 AliHLTFileWriter::~AliHLTFileWriter()
65 // file list and file name list are owner of their objects and
66 // delete all the objects
69 const char* AliHLTFileWriter::GetComponentID()
74 void AliHLTFileWriter::GetInputDataTypes( vector<AliHLTComponentDataType>& list)
77 list.push_back(kAliHLTAnyDataType);
80 AliHLTComponent* AliHLTFileWriter::Spawn()
82 return new AliHLTFileWriter;
85 int AliHLTFileWriter::DoInit( int argc, const char** argv )
90 for (int i=0; i<argc && iResult>=0; i++) {
92 if (argument.IsNull()) continue;
95 if (argument.CompareTo("-datafile")==0) {
96 if ((bMissingParam=(++i>=argc))) break;
98 TObjArray* pTokens=fBaseName.Tokenize(".");
100 int iEntries=pTokens->GetEntries();
103 fBaseName=((TObjString*)pTokens->At(i++))->GetString();
104 while (i<iEntries-1) {
105 fBaseName+="." + ((TObjString*)pTokens->At(i++))->GetString();
107 fExtension=((TObjString*)pTokens->At(i))->GetString();
113 } else if (argument.CompareTo("-directory")==0) {
114 if ((bMissingParam=(++i>=argc))) break;
118 } else if (argument.CompareTo("-enumerate")==0) {
121 // -concatenate-blocks
122 } else if (argument.CompareTo("-concatenate-blocks")==0) {
123 SetMode(kConcatenateBlocks);
125 // -concatenate-events
126 } else if (argument.CompareTo("-concatenate-events")==0) {
127 SetMode(kConcatenateEvents);
130 if ((iResult=ScanArgument(argc-i, &argv[i]))==-EINVAL) {
131 HLTError("unknown argument %s", argument.Data());
133 } else if (iResult==-EPROTO) {
136 } else if (iResult>=0) {
143 HLTError("missing parameter for argument %s", argument.Data());
147 iResult=InitWriter();
153 int AliHLTFileWriter::InitWriter()
155 return 0; // note: this doesn't mean 'error'
158 int AliHLTFileWriter::ScanArgument(int argc, const char** argv)
160 // there are no other arguments than the standard ones
164 int AliHLTFileWriter::DoDeinit()
167 int iResult=CloseWriter();
168 ClearMode(kEnumerate);
172 int AliHLTFileWriter::CloseWriter()
174 return 0; // note: this doesn't mean 'error'
177 int AliHLTFileWriter::DumpEvent( const AliHLTComponentEventData& evtData,
178 const AliHLTComponentBlockData* blocks,
179 AliHLTComponentTriggerData& trigData )
182 if (CheckMode(kConcatenateEvents)==0) {
183 // reset the current file name in order to open a new file
184 // for the first block. If events are concatenated, the current
185 // file name stays in order to be opended in append mode.
188 for (int n=0; n<(int)evtData.fBlockCnt; n++ ) {
189 //HLTDebug("block %d out of %d", n, evtData.fBlockCnt);
191 HLTDebug("dataspec 0x%x", blocks[n].fSpecification);
192 iResult=BuildFileName(evtData.fEventID, n, blocks[n].fDataType, filename);
193 ios::openmode filemode=(ios::openmode)0;
194 if (fCurrentFileName.CompareTo(filename)==0) {
195 // append to the file
198 // store the file for the next block
199 fCurrentFileName=filename;
202 ofstream dump(filename.Data(), filemode);
204 dump.write((const char*)blocks[n].fPtr, blocks[n].fSize);
205 HLTDebug("wrote %d byte(s) to file %s", blocks[n].fSize, filename.Data());
207 HLTError("can not open file %s for writing", filename.Data());
216 int AliHLTFileWriter::BuildFileName(const AliHLTEventID_t eventID, const int blockID, const AliHLTComponentDataType& dataType, TString& filename)
219 //HLTDebug("build file name for event %d block %d", eventID, blockID);
221 if (!fDirectory.IsNull()) {
222 filename+=fDirectory;
223 if (!fDirectory.EndsWith("/"))
226 if (!fBaseName.IsNull())
230 if (!CheckMode(kConcatenateEvents)) {
231 if (!CheckMode(kEnumerate)) {
232 if (eventID!=kAliHLTVoidEventID) {
233 filename+=Form("_0x%08x", eventID);
236 filename+=Form("_%d", GetEventCount());
239 if (blockID>=0 && !CheckMode(kConcatenateBlocks)) {
240 filename+=Form("_0x%x", blockID);
241 if (dataType!=kAliHLTVoidDataType) {
243 filename+=AliHLTComponent::DataType2Text(dataType).data();
246 if (!fExtension.IsNull())
247 filename+="." + fExtension;
248 filename.ReplaceAll(" ", "");
252 int AliHLTFileWriter::SetMode(Short_t mode)
255 //HLTDebug("mode set to 0x%x", fMode);
259 int AliHLTFileWriter::ClearMode(Short_t mode)
262 //HLTDebug("mode set to 0x%x", fMode);
266 int AliHLTFileWriter::CheckMode(Short_t mode)
268 //HLTDebug("check mode 0x%x for flag 0x%x: %d", fMode, mode, (fMode&mode)!=0);
269 return (fMode&mode)!=0;