]>
Commit | Line | Data |
---|---|---|
9ce4bf4a | 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" | |
3cde846d | 28 | #include <TObjArray.h> |
9ce4bf4a | 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 | : | |
f644d3f1 | 38 | AliHLTDataSink(), |
9ce4bf4a | 39 | fBaseName(""), |
3cde846d | 40 | fExtension(""), |
9ce4bf4a | 41 | fDirectory(""), |
3cde846d | 42 | fCurrentFileName(""), |
43 | fMode(0) | |
9ce4bf4a | 44 | { |
70ed7d01 | 45 | // see header file for class documentation |
46 | // or | |
47 | // refer to README to build package | |
48 | // or | |
49 | // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt | |
9ce4bf4a | 50 | } |
51 | ||
52 | AliHLTFileWriter::AliHLTFileWriter(const AliHLTFileWriter&) | |
53 | : | |
f644d3f1 | 54 | AliHLTDataSink(), |
9ce4bf4a | 55 | fBaseName(""), |
3cde846d | 56 | fExtension(""), |
9ce4bf4a | 57 | fDirectory(""), |
3cde846d | 58 | fCurrentFileName(""), |
59 | fMode(0) | |
9ce4bf4a | 60 | { |
70ed7d01 | 61 | // see header file for class documentation |
9ce4bf4a | 62 | HLTFatal("copy constructor untested"); |
63 | } | |
64 | ||
65 | AliHLTFileWriter& AliHLTFileWriter::operator=(const AliHLTFileWriter&) | |
66 | { | |
70ed7d01 | 67 | // see header file for class documentation |
9ce4bf4a | 68 | HLTFatal("assignment operator untested"); |
69 | return *this; | |
70 | } | |
71 | ||
72 | AliHLTFileWriter::~AliHLTFileWriter() | |
73 | { | |
70ed7d01 | 74 | // see header file for class documentation |
75 | ||
9ce4bf4a | 76 | // file list and file name list are owner of their objects and |
77 | // delete all the objects | |
78 | } | |
79 | ||
80 | const char* AliHLTFileWriter::GetComponentID() | |
81 | { | |
70ed7d01 | 82 | // see header file for class documentation |
9ce4bf4a | 83 | return "FileWriter"; |
84 | } | |
85 | ||
86 | void AliHLTFileWriter::GetInputDataTypes( vector<AliHLTComponentDataType>& list) | |
87 | { | |
70ed7d01 | 88 | // see header file for class documentation |
9ce4bf4a | 89 | list.clear(); |
90 | list.push_back(kAliHLTAnyDataType); | |
91 | } | |
92 | ||
93 | AliHLTComponent* AliHLTFileWriter::Spawn() | |
94 | { | |
70ed7d01 | 95 | // see header file for class documentation |
9ce4bf4a | 96 | return new AliHLTFileWriter; |
97 | } | |
98 | ||
99 | int AliHLTFileWriter::DoInit( int argc, const char** argv ) | |
100 | { | |
70ed7d01 | 101 | // see header file for class documentation |
9ce4bf4a | 102 | int iResult=0; |
103 | TString argument=""; | |
104 | int bMissingParam=0; | |
105 | for (int i=0; i<argc && iResult>=0; i++) { | |
106 | argument=argv[i]; | |
3cde846d | 107 | if (argument.IsNull()) continue; |
9ce4bf4a | 108 | |
109 | // -basename | |
110 | if (argument.CompareTo("-datafile")==0) { | |
111 | if ((bMissingParam=(++i>=argc))) break; | |
112 | fBaseName=argv[i]; | |
3cde846d | 113 | TObjArray* pTokens=fBaseName.Tokenize("."); |
114 | if (pTokens) { | |
115 | int iEntries=pTokens->GetEntries(); | |
116 | if (iEntries>1) { | |
117 | int i=0; | |
118 | fBaseName=((TObjString*)pTokens->At(i++))->GetString(); | |
119 | while (i<iEntries-1) { | |
120 | fBaseName+="." + ((TObjString*)pTokens->At(i++))->GetString(); | |
121 | } | |
122 | fExtension=((TObjString*)pTokens->At(i))->GetString(); | |
123 | } | |
124 | delete pTokens; | |
125 | } | |
9ce4bf4a | 126 | |
127 | // -directory | |
128 | } else if (argument.CompareTo("-directory")==0) { | |
129 | if ((bMissingParam=(++i>=argc))) break; | |
130 | fDirectory=argv[i]; | |
131 | ||
132 | // -enumeration | |
3cde846d | 133 | } else if (argument.CompareTo("-enumerate")==0) { |
134 | SetMode(kEnumerate); | |
9ce4bf4a | 135 | |
3cde846d | 136 | // -concatenate-blocks |
137 | } else if (argument.CompareTo("-concatenate-blocks")==0) { | |
138 | SetMode(kConcatenateBlocks); | |
139 | ||
140 | // -concatenate-events | |
141 | } else if (argument.CompareTo("-concatenate-events")==0) { | |
142 | SetMode(kConcatenateEvents); | |
9ce4bf4a | 143 | |
144 | } else { | |
145 | if ((iResult=ScanArgument(argc-i, &argv[i]))==-EINVAL) { | |
146 | HLTError("unknown argument %s", argument.Data()); | |
147 | break; | |
148 | } else if (iResult==-EPROTO) { | |
149 | bMissingParam=1; | |
150 | break; | |
151 | } else if (iResult>=0) { | |
152 | i+=iResult; | |
153 | iResult=0; | |
154 | } | |
155 | } | |
156 | } | |
157 | if (bMissingParam) { | |
158 | HLTError("missing parameter for argument %s", argument.Data()); | |
159 | iResult=-EINVAL; | |
160 | } | |
3cde846d | 161 | if (iResult>=0) { |
162 | iResult=InitWriter(); | |
163 | } | |
164 | ||
9ce4bf4a | 165 | return iResult; |
166 | } | |
167 | ||
3cde846d | 168 | int AliHLTFileWriter::InitWriter() |
169 | { | |
70ed7d01 | 170 | // see header file for class documentation |
3cde846d | 171 | return 0; // note: this doesn't mean 'error' |
172 | } | |
173 | ||
9ce4bf4a | 174 | int AliHLTFileWriter::ScanArgument(int argc, const char** argv) |
175 | { | |
70ed7d01 | 176 | // see header file for class documentation |
177 | ||
9ce4bf4a | 178 | // there are no other arguments than the standard ones |
f644d3f1 | 179 | if (argc==0 && argv==NULL) { |
180 | // this is just to get rid of the warning "unused parameter" | |
181 | } | |
9ce4bf4a | 182 | return -EINVAL; |
183 | } | |
184 | ||
185 | int AliHLTFileWriter::DoDeinit() | |
186 | { | |
70ed7d01 | 187 | // see header file for class documentation |
3cde846d | 188 | int iResult=CloseWriter(); |
189 | ClearMode(kEnumerate); | |
9ce4bf4a | 190 | return iResult; |
191 | } | |
192 | ||
3cde846d | 193 | int AliHLTFileWriter::CloseWriter() |
194 | { | |
70ed7d01 | 195 | // see header file for class documentation |
3cde846d | 196 | return 0; // note: this doesn't mean 'error' |
197 | } | |
198 | ||
9ce4bf4a | 199 | int AliHLTFileWriter::DumpEvent( const AliHLTComponentEventData& evtData, |
200 | const AliHLTComponentBlockData* blocks, | |
201 | AliHLTComponentTriggerData& trigData ) | |
202 | { | |
70ed7d01 | 203 | // see header file for class documentation |
9ce4bf4a | 204 | int iResult=0; |
3cde846d | 205 | if (CheckMode(kConcatenateEvents)==0) { |
206 | // reset the current file name in order to open a new file | |
207 | // for the first block. If events are concatenated, the current | |
208 | // file name stays in order to be opended in append mode. | |
209 | fCurrentFileName=""; | |
210 | } | |
9ce4bf4a | 211 | for (int n=0; n<(int)evtData.fBlockCnt; n++ ) { |
212 | //HLTDebug("block %d out of %d", n, evtData.fBlockCnt); | |
213 | TString filename; | |
3cde846d | 214 | HLTDebug("dataspec 0x%x", blocks[n].fSpecification); |
9ce4bf4a | 215 | iResult=BuildFileName(evtData.fEventID, n, blocks[n].fDataType, filename); |
3cde846d | 216 | ios::openmode filemode=(ios::openmode)0; |
217 | if (fCurrentFileName.CompareTo(filename)==0) { | |
218 | // append to the file | |
219 | filemode=ios::app; | |
220 | } else { | |
221 | // store the file for the next block | |
222 | fCurrentFileName=filename; | |
223 | } | |
9ce4bf4a | 224 | if (iResult>=0) { |
3cde846d | 225 | ofstream dump(filename.Data(), filemode); |
9ce4bf4a | 226 | if (dump.good()) { |
227 | dump.write((const char*)blocks[n].fPtr, blocks[n].fSize); | |
228 | HLTDebug("wrote %d byte(s) to file %s", blocks[n].fSize, filename.Data()); | |
3cde846d | 229 | } else { |
230 | HLTError("can not open file %s for writing", filename.Data()); | |
231 | iResult=-EBADF; | |
9ce4bf4a | 232 | } |
233 | dump.close(); | |
234 | } | |
235 | } | |
f644d3f1 | 236 | if (trigData.fStructSize==0) { |
237 | // this is just to get rid of the warning "unused parameter" | |
238 | } | |
9ce4bf4a | 239 | return iResult; |
240 | } | |
241 | ||
242 | int AliHLTFileWriter::BuildFileName(const AliHLTEventID_t eventID, const int blockID, const AliHLTComponentDataType& dataType, TString& filename) | |
243 | { | |
70ed7d01 | 244 | // see header file for class documentation |
9ce4bf4a | 245 | int iResult=0; |
246 | //HLTDebug("build file name for event %d block %d", eventID, blockID); | |
3cde846d | 247 | filename=""; |
248 | if (!fDirectory.IsNull()) { | |
249 | filename+=fDirectory; | |
250 | if (!fDirectory.EndsWith("/")) | |
251 | filename+="/"; | |
252 | } | |
253 | if (!fBaseName.IsNull()) | |
254 | filename+=fBaseName; | |
255 | else | |
256 | filename+="event"; | |
257 | if (!CheckMode(kConcatenateEvents)) { | |
258 | if (!CheckMode(kEnumerate)) { | |
259 | if (eventID!=kAliHLTVoidEventID) { | |
260 | filename+=Form("_0x%08x", eventID); | |
261 | } | |
262 | } else { | |
263 | filename+=Form("_%d", GetEventCount()); | |
264 | } | |
265 | } | |
266 | if (blockID>=0 && !CheckMode(kConcatenateBlocks)) { | |
267 | filename+=Form("_0x%x", blockID); | |
268 | if (dataType!=kAliHLTVoidDataType) { | |
269 | filename+="_"; | |
270 | filename+=AliHLTComponent::DataType2Text(dataType).data(); | |
271 | } | |
272 | } | |
273 | if (!fExtension.IsNull()) | |
274 | filename+="." + fExtension; | |
275 | filename.ReplaceAll(" ", ""); | |
9ce4bf4a | 276 | return iResult; |
277 | } | |
3cde846d | 278 | |
279 | int AliHLTFileWriter::SetMode(Short_t mode) | |
280 | { | |
70ed7d01 | 281 | // see header file for class documentation |
3cde846d | 282 | fMode|=mode; |
283 | //HLTDebug("mode set to 0x%x", fMode); | |
284 | return fMode; | |
285 | } | |
286 | ||
287 | int AliHLTFileWriter::ClearMode(Short_t mode) | |
288 | { | |
70ed7d01 | 289 | // see header file for class documentation |
3cde846d | 290 | fMode&=~mode; |
291 | //HLTDebug("mode set to 0x%x", fMode); | |
292 | return fMode; | |
293 | } | |
294 | ||
295 | int AliHLTFileWriter::CheckMode(Short_t mode) | |
296 | { | |
70ed7d01 | 297 | // see header file for class documentation |
298 | ||
3cde846d | 299 | //HLTDebug("check mode 0x%x for flag 0x%x: %d", fMode, mode, (fMode&mode)!=0); |
300 | return (fMode&mode)!=0; | |
301 | } |