]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/BASE/AliHLTFileWriter.cxx
more work on coding conventions and effC++
[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   AliHLTDataSink(),
39   fBaseName(""),
40   fExtension(""),
41   fDirectory(""),
42   fCurrentFileName(""),
43   fMode(0)
44 {
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
50 }
51
52 AliHLTFileWriter::AliHLTFileWriter(const AliHLTFileWriter&)
53   :
54   AliHLTDataSink(),
55   fBaseName(""),
56   fExtension(""),
57   fDirectory(""),
58   fCurrentFileName(""),
59   fMode(0)
60 {
61   // see header file for class documentation
62   HLTFatal("copy constructor untested");
63 }
64
65 AliHLTFileWriter& AliHLTFileWriter::operator=(const AliHLTFileWriter&)
66
67   // see header file for class documentation
68   HLTFatal("assignment operator untested");
69   return *this;
70 }
71
72 AliHLTFileWriter::~AliHLTFileWriter()
73 {
74   // see header file for class documentation
75
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 {
82   // see header file for class documentation
83   return "FileWriter";
84 }
85
86 void AliHLTFileWriter::GetInputDataTypes( vector<AliHLTComponentDataType>& list)
87 {
88   // see header file for class documentation
89   list.clear();
90   list.push_back(kAliHLTAnyDataType);
91 }
92
93 AliHLTComponent* AliHLTFileWriter::Spawn()
94 {
95   // see header file for class documentation
96   return new AliHLTFileWriter;
97 }
98
99 int AliHLTFileWriter::DoInit( int argc, const char** argv )
100 {
101   // see header file for class documentation
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];
107     if (argument.IsNull()) continue;
108
109     // -basename
110     if (argument.CompareTo("-datafile")==0) {
111       if ((bMissingParam=(++i>=argc))) break;
112       fBaseName=argv[i];
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       }
126
127       // -directory
128     } else if (argument.CompareTo("-directory")==0) {
129       if ((bMissingParam=(++i>=argc))) break;
130       fDirectory=argv[i];
131
132       // -enumeration
133     } else if (argument.CompareTo("-enumerate")==0) {
134       SetMode(kEnumerate);
135
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);
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   }
161   if (iResult>=0) {
162     iResult=InitWriter();
163   }
164
165   return iResult;
166 }
167
168 int AliHLTFileWriter::InitWriter()
169 {
170   // see header file for class documentation
171   return 0; // note: this doesn't mean 'error'
172 }
173
174 int AliHLTFileWriter::ScanArgument(int argc, const char** argv)
175 {
176   // see header file for class documentation
177
178   // there are no other arguments than the standard ones
179   if (argc==0 && argv==NULL) {
180     // this is just to get rid of the warning "unused parameter"
181   }
182   return -EINVAL;
183 }
184
185 int AliHLTFileWriter::DoDeinit()
186 {
187   // see header file for class documentation
188   int iResult=CloseWriter();
189   ClearMode(kEnumerate);
190   return iResult;
191 }
192
193 int AliHLTFileWriter::CloseWriter()
194 {
195   // see header file for class documentation
196   return 0; // note: this doesn't mean 'error'
197 }
198
199 int AliHLTFileWriter::DumpEvent( const AliHLTComponentEventData& evtData,
200                          const AliHLTComponentBlockData* blocks, 
201                          AliHLTComponentTriggerData& trigData )
202 {
203   // see header file for class documentation
204   int iResult=0;
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   }
211   for (int n=0; n<(int)evtData.fBlockCnt; n++ ) {
212     //HLTDebug("block %d out of %d", n, evtData.fBlockCnt);
213     TString filename;
214     HLTDebug("dataspec 0x%x", blocks[n].fSpecification);
215     iResult=BuildFileName(evtData.fEventID, n, blocks[n].fDataType, filename);
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     }
224     if (iResult>=0) {
225       ofstream dump(filename.Data(), filemode);
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());
229       } else {
230         HLTError("can not open file %s for writing", filename.Data());
231         iResult=-EBADF;
232       }
233       dump.close();
234     }
235   }
236   if (trigData.fStructSize==0) {
237     // this is just to get rid of the warning "unused parameter"
238   }
239   return iResult;
240 }
241
242 int AliHLTFileWriter::BuildFileName(const AliHLTEventID_t eventID, const int blockID, const AliHLTComponentDataType& dataType, TString& filename)
243 {
244   // see header file for class documentation
245   int iResult=0;
246   //HLTDebug("build file name for event %d block %d", eventID, blockID);
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(" ", "");
276   return iResult;
277 }
278
279 int AliHLTFileWriter::SetMode(Short_t mode) 
280 {
281   // see header file for class documentation
282   fMode|=mode;
283   //HLTDebug("mode set to 0x%x", fMode);
284   return fMode;
285 }
286
287 int AliHLTFileWriter::ClearMode(Short_t mode)
288 {
289   // see header file for class documentation
290   fMode&=~mode;
291   //HLTDebug("mode set to 0x%x", fMode);
292   return fMode;
293 }
294
295 int AliHLTFileWriter::CheckMode(Short_t mode)
296 {
297   // see header file for class documentation
298
299   //HLTDebug("check mode 0x%x for flag 0x%x: %d", fMode, mode, (fMode&mode)!=0);
300   return (fMode&mode)!=0;
301 }