]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/BASE/AliHLTFileWriter.cxx
b6795d945fda51105adb96e4b7f6a8958bb4a6c1
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTFileWriter.cxx
1 // $Id$
2
3 /**************************************************************************
4  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
5  *                                                                        *
6  * Authors: Matthias Richter <Matthias.Richter@ift.uib.no>                *
7  *          for The ALICE Off-line Project.                               *
8  *                                                                        *
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  **************************************************************************/
17
18 /** @file   AliHLTFileWriter.cxx
19     @author Matthias Richter
20     @date   
21     @brief  HLT file writer component implementation. */
22
23 #if __GNUC__>= 3
24 using namespace std;
25 #endif
26
27 #include "AliHLTFileWriter.h"
28 #include <TObjArray.h>
29 #include <TObjString.h>
30 #include <TMath.h>
31 #include <TFile.h>
32
33 /** ROOT macro for the implementation of ROOT specific class methods */
34 ClassImp(AliHLTFileWriter)
35
36 AliHLTFileWriter::AliHLTFileWriter()
37   :
38   fBaseName(""),
39   fExtension(""),
40   fDirectory(""),
41   fCurrentFileName(""),
42   fMode(0)
43 {
44 }
45
46 AliHLTFileWriter::AliHLTFileWriter(const AliHLTFileWriter&)
47   :
48   fBaseName(""),
49   fExtension(""),
50   fDirectory(""),
51   fCurrentFileName(""),
52   fMode(0)
53 {
54   HLTFatal("copy constructor untested");
55 }
56
57 AliHLTFileWriter& AliHLTFileWriter::operator=(const AliHLTFileWriter&)
58
59   HLTFatal("assignment operator untested");
60   return *this;
61 }
62
63 AliHLTFileWriter::~AliHLTFileWriter()
64 {
65   // file list and file name list are owner of their objects and
66   // delete all the objects
67 }
68
69 const char* AliHLTFileWriter::GetComponentID()
70 {
71   return "FileWriter";
72 }
73
74 void AliHLTFileWriter::GetInputDataTypes( vector<AliHLTComponentDataType>& list)
75 {
76   list.clear();
77   list.push_back(kAliHLTAnyDataType);
78 }
79
80 AliHLTComponent* AliHLTFileWriter::Spawn()
81 {
82   return new AliHLTFileWriter;
83 }
84
85 int AliHLTFileWriter::DoInit( int argc, const char** argv )
86 {
87   int iResult=0;
88   TString argument="";
89   int bMissingParam=0;
90   for (int i=0; i<argc && iResult>=0; i++) {
91     argument=argv[i];
92     if (argument.IsNull()) continue;
93
94     // -basename
95     if (argument.CompareTo("-datafile")==0) {
96       if ((bMissingParam=(++i>=argc))) break;
97       fBaseName=argv[i];
98       TObjArray* pTokens=fBaseName.Tokenize(".");
99       if (pTokens) {
100         int iEntries=pTokens->GetEntries();
101         if (iEntries>1) {
102           int i=0;
103           fBaseName=((TObjString*)pTokens->At(i++))->GetString();
104           while (i<iEntries-1) {
105             fBaseName+="." + ((TObjString*)pTokens->At(i++))->GetString();
106           }
107           fExtension=((TObjString*)pTokens->At(i))->GetString();
108         }
109         delete pTokens;
110       }
111
112       // -directory
113     } else if (argument.CompareTo("-directory")==0) {
114       if ((bMissingParam=(++i>=argc))) break;
115       fDirectory=argv[i];
116
117       // -enumeration
118     } else if (argument.CompareTo("-enumerate")==0) {
119       SetMode(kEnumerate);
120
121       // -concatenate-blocks
122     } else if (argument.CompareTo("-concatenate-blocks")==0) {
123       SetMode(kConcatenateBlocks);
124
125       // -concatenate-events
126     } else if (argument.CompareTo("-concatenate-events")==0) {
127       SetMode(kConcatenateEvents);
128
129     } else {
130       if ((iResult=ScanArgument(argc-i, &argv[i]))==-EINVAL) {
131         HLTError("unknown argument %s", argument.Data());
132         break;
133       } else if (iResult==-EPROTO) {
134         bMissingParam=1;
135         break;
136       } else if (iResult>=0) {
137         i+=iResult;
138         iResult=0;
139       }
140     }
141   }
142   if (bMissingParam) {
143     HLTError("missing parameter for argument %s", argument.Data());
144     iResult=-EINVAL;
145   }
146   if (iResult>=0) {
147     iResult=InitWriter();
148   }
149
150   return iResult;
151 }
152
153 int AliHLTFileWriter::InitWriter()
154 {
155   return 0; // note: this doesn't mean 'error'
156 }
157
158 int AliHLTFileWriter::ScanArgument(int argc, const char** argv)
159 {
160   // there are no other arguments than the standard ones
161   return -EINVAL;
162 }
163
164 int AliHLTFileWriter::DoDeinit()
165 {
166   HLTDebug("");
167   int iResult=CloseWriter();
168   ClearMode(kEnumerate);
169   return iResult;
170 }
171
172 int AliHLTFileWriter::CloseWriter()
173 {
174   return 0; // note: this doesn't mean 'error'
175 }
176
177 int AliHLTFileWriter::DumpEvent( const AliHLTComponentEventData& evtData,
178                          const AliHLTComponentBlockData* blocks, 
179                          AliHLTComponentTriggerData& trigData )
180 {
181   int iResult=0;
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.
186     fCurrentFileName="";
187   }
188   for (int n=0; n<(int)evtData.fBlockCnt; n++ ) {
189     //HLTDebug("block %d out of %d", n, evtData.fBlockCnt);
190     TString filename;
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
196       filemode=ios::app;
197     } else {
198       // store the file for the next block
199       fCurrentFileName=filename;
200     }
201     if (iResult>=0) {
202       ofstream dump(filename.Data(), filemode);
203       if (dump.good()) {
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());
206       } else {
207         HLTError("can not open file %s for writing", filename.Data());
208         iResult=-EBADF;
209       }
210       dump.close();
211     }
212   }
213   return iResult;
214 }
215
216 int AliHLTFileWriter::BuildFileName(const AliHLTEventID_t eventID, const int blockID, const AliHLTComponentDataType& dataType, TString& filename)
217 {
218   int iResult=0;
219   //HLTDebug("build file name for event %d block %d", eventID, blockID);
220   filename="";
221   if (!fDirectory.IsNull()) {
222     filename+=fDirectory;
223     if (!fDirectory.EndsWith("/"))
224       filename+="/";
225   }
226   if (!fBaseName.IsNull())
227     filename+=fBaseName;
228   else
229     filename+="event";
230   if (!CheckMode(kConcatenateEvents)) {
231     if (!CheckMode(kEnumerate)) {
232       if (eventID!=kAliHLTVoidEventID) {
233         filename+=Form("_0x%08x", eventID);
234       }
235     } else {
236       filename+=Form("_%d", GetEventCount());
237     }
238   }
239   if (blockID>=0 && !CheckMode(kConcatenateBlocks)) {
240     filename+=Form("_0x%x", blockID);
241     if (dataType!=kAliHLTVoidDataType) {
242       filename+="_";
243       filename+=AliHLTComponent::DataType2Text(dataType).data();
244     }
245   }
246   if (!fExtension.IsNull())
247     filename+="." + fExtension;
248   filename.ReplaceAll(" ", "");
249   return iResult;
250 }
251
252 int AliHLTFileWriter::SetMode(Short_t mode) 
253 {
254   fMode|=mode;
255   //HLTDebug("mode set to 0x%x", fMode);
256   return fMode;
257 }
258
259 int AliHLTFileWriter::ClearMode(Short_t mode)
260 {
261   fMode&=~mode;
262   //HLTDebug("mode set to 0x%x", fMode);
263   return fMode;
264 }
265
266 int AliHLTFileWriter::CheckMode(Short_t mode)
267 {
268   //HLTDebug("check mode 0x%x for flag 0x%x: %d", fMode, mode, (fMode&mode)!=0);
269   return (fMode&mode)!=0;
270 }