]>
Commit | Line | Data |
---|---|---|
1 | // $Id$ | |
2 | ||
3 | /************************************************************************** | |
4 | * This file is property of and copyright by the ALICE HLT Project * | |
5 | * ALICE Experiment at CERN, All rights reserved. * | |
6 | * * | |
7 | * Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no> * | |
8 | * for The ALICE HLT Project. * | |
9 | * * | |
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 | **************************************************************************/ | |
18 | ||
19 | /** @file AliHLTFileWriter.cxx | |
20 | @author Matthias Richter | |
21 | @date | |
22 | @brief HLT file writer component implementation. */ | |
23 | ||
24 | #if __GNUC__>= 3 | |
25 | using namespace std; | |
26 | #endif | |
27 | ||
28 | #include "AliHLTFileWriter.h" | |
29 | #include <TObjArray.h> | |
30 | #include <TObjString.h> | |
31 | #include <TSystem.h> | |
32 | //#include <TMath.h> | |
33 | //#include <TFile.h> | |
34 | ||
35 | /** ROOT macro for the implementation of ROOT specific class methods */ | |
36 | ClassImp(AliHLTFileWriter) | |
37 | ||
38 | AliHLTFileWriter::AliHLTFileWriter() | |
39 | : | |
40 | AliHLTDataSink(), | |
41 | fBaseName(""), | |
42 | fExtension(""), | |
43 | fDirectory(""), | |
44 | fSubDirFormat(""), | |
45 | fIdFormat(""), | |
46 | fSpecFormat(""), | |
47 | fBlcknoFormat("_0x%02x"), | |
48 | fCurrentFileName(""), | |
49 | fMode(0) | |
50 | { | |
51 | // see header file for class documentation | |
52 | // or | |
53 | // refer to README to build package | |
54 | // or | |
55 | // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt | |
56 | } | |
57 | ||
58 | AliHLTFileWriter::~AliHLTFileWriter() | |
59 | { | |
60 | // see header file for class documentation | |
61 | ||
62 | // file list and file name list are owner of their objects and | |
63 | // delete all the objects | |
64 | } | |
65 | ||
66 | const char* AliHLTFileWriter::GetComponentID() | |
67 | { | |
68 | // see header file for class documentation | |
69 | return "FileWriter"; | |
70 | } | |
71 | ||
72 | void AliHLTFileWriter::GetInputDataTypes( vector<AliHLTComponentDataType>& list) | |
73 | { | |
74 | // see header file for class documentation | |
75 | list.clear(); | |
76 | list.push_back(kAliHLTAnyDataType); | |
77 | } | |
78 | ||
79 | AliHLTComponent* AliHLTFileWriter::Spawn() | |
80 | { | |
81 | // see header file for class documentation | |
82 | return new AliHLTFileWriter; | |
83 | } | |
84 | ||
85 | int AliHLTFileWriter::DoInit( int argc, const char** argv ) | |
86 | { | |
87 | // see header file for class documentation | |
88 | int iResult=0; | |
89 | TString argument=""; | |
90 | int bMissingParam=0; | |
91 | for (int i=0; i<argc && iResult>=0; i++) { | |
92 | argument=argv[i]; | |
93 | if (argument.IsNull()) continue; | |
94 | ||
95 | // -basename | |
96 | if (argument.CompareTo("-datafile")==0) { | |
97 | if ((bMissingParam=(++i>=argc))) break; | |
98 | fBaseName=argv[i]; | |
99 | TObjArray* pTokens=fBaseName.Tokenize("."); | |
100 | if (pTokens) { | |
101 | int iEntries=pTokens->GetEntries(); | |
102 | if (iEntries>1) { | |
103 | int i=0; | |
104 | fBaseName=((TObjString*)pTokens->At(i++))->GetString(); | |
105 | while (i<iEntries-1) { | |
106 | fBaseName+="." + ((TObjString*)pTokens->At(i++))->GetString(); | |
107 | } | |
108 | fExtension=((TObjString*)pTokens->At(i))->GetString(); | |
109 | } | |
110 | delete pTokens; | |
111 | } | |
112 | ||
113 | // -directory | |
114 | } else if (argument.CompareTo("-directory")==0) { | |
115 | if ((bMissingParam=(++i>=argc))) break; | |
116 | fDirectory=argv[i]; | |
117 | ||
118 | // -subdir | |
119 | } else if (argument.BeginsWith("-subdir")) { | |
120 | argument.ReplaceAll("-subdir", ""); | |
121 | if (argument.BeginsWith("=")) { | |
122 | fSubDirFormat=argument.Replace(0,1,""); | |
123 | } else { | |
124 | fSubDirFormat="event%03d"; | |
125 | } | |
126 | ||
127 | // -idfmt | |
128 | } else if (argument.BeginsWith("-idfmt")) { | |
129 | argument.ReplaceAll("-idfmt", ""); | |
130 | if (argument.BeginsWith("=")) { | |
131 | fIdFormat=argument.Replace(0,1,""); | |
132 | } | |
133 | ||
134 | // -specfmt | |
135 | } else if (argument.BeginsWith("-specfmt")) { | |
136 | argument.ReplaceAll("-specfmt", ""); | |
137 | if (argument.BeginsWith("=")) { | |
138 | fSpecFormat=argument.Replace(0,1,""); | |
139 | } else { | |
140 | fSpecFormat="_0x%08x"; | |
141 | } | |
142 | ||
143 | // -blcknofmt | |
144 | } else if (argument.BeginsWith("-blcknofmt")) { | |
145 | argument.ReplaceAll("-blcknofmt", ""); | |
146 | if (argument.BeginsWith("=")) { | |
147 | fBlcknoFormat=argument.Replace(0,1,""); | |
148 | } else { | |
149 | fBlcknoFormat="_0x%02x"; | |
150 | } | |
151 | ||
152 | // -enumeration | |
153 | } else if (argument.CompareTo("-enumerate")==0) { | |
154 | SetMode(kEnumerate); | |
155 | ||
156 | // -concatenate-blocks | |
157 | } else if (argument.CompareTo("-concatenate-blocks")==0) { | |
158 | SetMode(kConcatenateBlocks); | |
159 | ||
160 | // -concatenate-events | |
161 | } else if (argument.CompareTo("-concatenate-events")==0) { | |
162 | SetMode(kConcatenateEvents); | |
163 | ||
164 | } else { | |
165 | if ((iResult=ScanArgument(argc-i, &argv[i]))==-EINVAL) { | |
166 | HLTError("unknown argument %s", argument.Data()); | |
167 | break; | |
168 | } else if (iResult==-EPROTO) { | |
169 | bMissingParam=1; | |
170 | break; | |
171 | } else if (iResult>=0) { | |
172 | i+=iResult; | |
173 | iResult=0; | |
174 | } | |
175 | } | |
176 | } | |
177 | if (bMissingParam) { | |
178 | HLTError("missing parameter for argument %s", argument.Data()); | |
179 | iResult=-EINVAL; | |
180 | } | |
181 | if (iResult>=0) { | |
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) | |
185 | fIdFormat="_0x%08x"; | |
186 | } | |
187 | iResult=InitWriter(); | |
188 | } | |
189 | ||
190 | return iResult; | |
191 | } | |
192 | ||
193 | int AliHLTFileWriter::InitWriter() | |
194 | { | |
195 | // see header file for class documentation | |
196 | ||
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 | |
201 | fCurrentFileName=""; | |
202 | return 0; // note: this doesn't mean 'error' | |
203 | } | |
204 | ||
205 | int AliHLTFileWriter::ScanArgument(int argc, const char** argv) | |
206 | { | |
207 | // see header file for class documentation | |
208 | ||
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" | |
212 | } | |
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 | |
217 | fCurrentFileName=""; | |
218 | return -EINVAL; | |
219 | } | |
220 | ||
221 | int AliHLTFileWriter::DoDeinit() | |
222 | { | |
223 | // see header file for class documentation | |
224 | int iResult=CloseWriter(); | |
225 | ClearMode(kEnumerate); | |
226 | return iResult; | |
227 | } | |
228 | ||
229 | int AliHLTFileWriter::CloseWriter() | |
230 | { | |
231 | // see header file for class documentation | |
232 | ||
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 | |
237 | fCurrentFileName=""; | |
238 | return 0; // note: this doesn't mean 'error' | |
239 | } | |
240 | ||
241 | int AliHLTFileWriter::DumpEvent( const AliHLTComponentEventData& evtData, | |
242 | AliHLTComponentTriggerData& /*trigData*/ ) | |
243 | { | |
244 | // see header file for class documentation | |
245 | int iResult=0; | |
246 | if (CheckMode(kConcatenateEvents)==0) { | |
247 | // reset the current file name in order to open a new file | |
248 | // for the first block. If events are concatenated, the current | |
249 | // file name stays in order to be opended in append mode. | |
250 | fCurrentFileName=""; | |
251 | } | |
252 | const AliHLTComponentBlockData* pDesc=NULL; | |
253 | ||
254 | int blockno=0; | |
255 | for (pDesc=GetFirstInputBlock(kAliHLTAnyDataType); pDesc!=NULL; pDesc=GetNextInputBlock(), blockno++) { | |
256 | HLTDebug("block %d out of %d", blockno, evtData.fBlockCnt); | |
257 | TString filename; | |
258 | HLTDebug("dataspec 0x%x", pDesc->fSpecification); | |
259 | iResult=BuildFileName(evtData.fEventID, blockno, pDesc->fDataType, pDesc->fSpecification, filename); | |
260 | ios::openmode filemode=(ios::openmode)0; | |
261 | if (fCurrentFileName.CompareTo(filename)==0) { | |
262 | // append to the file | |
263 | filemode=ios::app; | |
264 | } else { | |
265 | // store the file for the next block | |
266 | fCurrentFileName=filename; | |
267 | } | |
268 | if (iResult>=0) { | |
269 | ofstream dump(filename.Data(), filemode); | |
270 | if (dump.good()) { | |
271 | dump.write((static_cast<const char*>(pDesc->fPtr)), pDesc->fSize); | |
272 | HLTDebug("wrote %d byte(s) to file %s", pDesc->fSize, filename.Data()); | |
273 | } else { | |
274 | HLTError("can not open file %s for writing", filename.Data()); | |
275 | iResult=-EBADF; | |
276 | } | |
277 | dump.close(); | |
278 | } | |
279 | } | |
280 | return iResult; | |
281 | } | |
282 | ||
283 | int AliHLTFileWriter::BuildFileName(const AliHLTEventID_t eventID, const int blockID, | |
284 | const AliHLTComponentDataType& dataType, | |
285 | const AliHLTUInt32_t specification, | |
286 | TString& filename) | |
287 | { | |
288 | // see header file for class documentation | |
289 | int iResult=0; | |
290 | //HLTDebug("build file name for event %d block %d", eventID, blockID); | |
291 | filename=""; | |
292 | if (!fDirectory.IsNull()) { | |
293 | filename+=fDirectory; | |
294 | if (!filename.EndsWith("/")) | |
295 | filename+="/"; | |
296 | } | |
297 | if (!fSubDirFormat.IsNull()) { | |
298 | filename+=Form(fSubDirFormat, eventID); | |
299 | if (!filename.EndsWith("/")) | |
300 | filename+="/"; | |
301 | } | |
302 | if (filename.EndsWith("/")) { | |
303 | gSystem->mkdir(filename); | |
304 | } | |
305 | if (!fBaseName.IsNull()) | |
306 | filename+=fBaseName; | |
307 | else | |
308 | filename+="event"; | |
309 | if (!CheckMode(kConcatenateEvents)) { | |
310 | if (!CheckMode(kEnumerate)) { | |
311 | if (eventID!=kAliHLTVoidEventID && !fIdFormat.IsNull()) { | |
312 | filename+=Form(fIdFormat, eventID); | |
313 | } | |
314 | } else { | |
315 | filename+=Form("_%d", GetEventCount()); | |
316 | } | |
317 | } | |
318 | if (blockID>=0 && !CheckMode(kConcatenateBlocks)) { | |
319 | if (!fBlcknoFormat.IsNull()) | |
320 | filename+=Form(fBlcknoFormat, blockID); | |
321 | if (dataType!=kAliHLTVoidDataType) { | |
322 | filename+="_"; | |
323 | filename+=AliHLTComponent::DataType2Text(dataType).data(); | |
324 | } | |
325 | if (!fSpecFormat.IsNull()) | |
326 | filename+=Form(fSpecFormat, specification); | |
327 | } | |
328 | if (!fExtension.IsNull()) | |
329 | filename+="." + fExtension; | |
330 | filename.ReplaceAll(" ", ""); | |
331 | return iResult; | |
332 | } | |
333 | ||
334 | int AliHLTFileWriter::SetMode(Short_t mode) | |
335 | { | |
336 | // see header file for class documentation | |
337 | fMode|=mode; | |
338 | //HLTDebug("mode set to 0x%x", fMode); | |
339 | return fMode; | |
340 | } | |
341 | ||
342 | int AliHLTFileWriter::ClearMode(Short_t mode) | |
343 | { | |
344 | // see header file for class documentation | |
345 | fMode&=~mode; | |
346 | //HLTDebug("mode set to 0x%x", fMode); | |
347 | return fMode; | |
348 | } | |
349 | ||
350 | int AliHLTFileWriter::CheckMode(Short_t mode) const | |
351 | { | |
352 | // see header file for class documentation | |
353 | ||
354 | //HLTDebug("check mode 0x%x for flag 0x%x: %d", fMode, mode, (fMode&mode)!=0); | |
355 | return (fMode&mode)!=0; | |
356 | } |