]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/BASE/util/AliHLTDataGenerator.cxx
added data generator to util library
[u/mrichter/AliRoot.git] / HLT / BASE / util / AliHLTDataGenerator.cxx
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   AliHLTDataGenerator.cxx
20     @author Matthias Richter
21     @date   
22     @brief  HLT file publisher component implementation. */
23
24 #if __GNUC__>= 3
25 using namespace std;
26 #endif
27
28 #include "AliHLTDataGenerator.h"
29 #include "TString.h"
30
31 /** the global object for component registration */
32 AliHLTDataGenerator gAliHLTDataGenerator;
33
34 /** ROOT macro for the implementation of ROOT specific class methods */
35 ClassImp(AliHLTDataGenerator)
36
37 AliHLTDataGenerator::AliHLTDataGenerator()
38   :
39   AliHLTDataSource(),
40   fDataType(kAliHLTVoidDataType),
41   fSpecification(~(AliHLTUInt32_t)0),
42   fSize(0),
43   fCurrSize(0),
44   fRange(0),
45   fDivisor(0),
46   fSubtractor(0),
47   fModulo(0)
48 {
49   // see header file for class documentation
50   // or
51   // refer to README to build package
52   // or
53   // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
54
55   // make the lists owners of their objects in order to automatically
56   // de-allocate the objects
57 }
58
59 AliHLTDataGenerator::~AliHLTDataGenerator()
60 {
61   // see header file for class documentation
62
63 }
64
65 const char* AliHLTDataGenerator::GetComponentID()
66 {
67   // see header file for class documentation
68   return "DataGenerator";
69 }
70
71 AliHLTComponentDataType AliHLTDataGenerator::GetOutputDataType()
72 {
73   // see header file for class documentation
74   return kAliHLTMultipleDataType;
75 }
76
77 int AliHLTDataGenerator::GetOutputDataTypes(vector<AliHLTComponentDataType>& tgtList)
78 {
79   int count=0;
80   tgtList.clear();
81   tgtList.push_back(fDataType);
82   return count;
83 }
84
85 void AliHLTDataGenerator::GetOutputDataSize( unsigned long& constBase, double& inputMultiplier )
86 {
87   // see header file for class documentation
88   constBase=fCurrSize+fRange;
89   inputMultiplier=1.0;
90 }
91
92 AliHLTComponent* AliHLTDataGenerator::Spawn()
93 {
94   // see header file for class documentation
95   return new AliHLTDataGenerator;
96 }
97
98 int AliHLTDataGenerator::DoInit( int argc, const char** argv )
99 {
100   // see header file for class documentation
101
102   //HLTDebug("%d %s", argc, argv[0]);
103   int iResult=0;
104   TString argument="";
105   int bMissingParam=0;
106   for (int i=0; i<argc && iResult>=0; i++) {
107     argument=argv[i];
108     if (argument.IsNull()) continue;
109
110     // -datatype
111     if (argument.CompareTo("-datatype")==0) {
112       if ((bMissingParam=(++i>=argc))) break;
113       memcpy(&fDataType.fID, argv[i], TMath::Min(kAliHLTComponentDataTypefIDsize, (Int_t)strlen(argv[i])));
114       if ((bMissingParam=(++i>=argc))) break;
115       memcpy(&fDataType.fOrigin, argv[i], TMath::Min(kAliHLTComponentDataTypefOriginSize, (Int_t)strlen(argv[i])));
116
117       // -dataspec
118     } else if (argument.CompareTo("-dataspec")==0) {
119       if ((bMissingParam=(++i>=argc))) break;
120       TString parameter(argv[i]);
121       parameter.Remove(TString::kLeading, ' '); // remove all blanks
122       if (parameter.IsDigit()) {
123         fSpecification=(AliHLTUInt32_t)parameter.Atoi();
124       } else if (parameter.BeginsWith("0x") &&
125                  parameter.Replace(0,2,"",0).IsHex()) {
126         sscanf(parameter.Data(),"%x", &fSpecification);
127       } else {
128         HLTError("wrong parameter for argument %s, number expected", argument.Data());
129         iResult=-EINVAL;
130       }
131       // -size
132     } else if (argument.CompareTo("-size")==0) {
133       if ((bMissingParam=(++i>=argc))) break;
134       if ((iResult=ScanSizeArgument(fSize, argv[i]))==-ERANGE) {
135         HLTError("wrong parameter for argument %s, number expected", argument.Data());
136         iResult=-EINVAL;
137       }
138       // -range
139     } else if (argument.CompareTo("-range")==0) {
140       if ((bMissingParam=(++i>=argc))) break;
141       if ((iResult=ScanSizeArgument(fRange, argv[i]))==-ERANGE) {
142         HLTError("wrong parameter for argument %s, number expected", argument.Data());
143         iResult=-EINVAL;
144       }
145       // -divisor
146     } else if (argument.CompareTo("-divisor")==0) {
147       if ((bMissingParam=(++i>=argc))) break;
148       if ((iResult=ScanSizeArgument(fDivisor, argv[i]))==-ERANGE) {
149         HLTError("wrong parameter for argument %s, number expected", argument.Data());
150         iResult=-EINVAL;
151       }
152       // -offset
153     } else if (argument.CompareTo("-offset")==0) {
154       if ((bMissingParam=(++i>=argc))) break;
155       if ((iResult=ScanSizeArgument(fSubtractor, argv[i]))==-ERANGE) {
156         HLTError("wrong parameter for argument %s, number expected", argument.Data());
157         iResult=-EINVAL;
158       }
159       // -modulo
160     } else if (argument.CompareTo("-modulo")==0) {
161       if ((bMissingParam=(++i>=argc))) break;
162       if ((iResult=ScanSizeArgument(fModulo, argv[i]))==-ERANGE) {
163         HLTError("wrong parameter for argument %s, number expected", argument.Data());
164         iResult=-EINVAL;
165       }
166     } else {
167       if ((iResult=ScanArgument(argc-i, &argv[i]))==-EINVAL) {
168         HLTError("unknown argument %s", argument.Data());
169         break;
170       } else if (iResult==-EPROTO) {
171         bMissingParam=1;
172         break;
173       } else if (iResult>=0) {
174         i+=iResult;
175         iResult=0;
176       }
177     }
178   }
179
180   if (bMissingParam) {
181     HLTError("missing parameter for argument %s", argument.Data());
182     iResult=-EINVAL;
183   }
184
185   fCurrSize=fSize;
186
187   return iResult;
188 }
189
190 int AliHLTDataGenerator::ScanSizeArgument(AliHLTUInt32_t &size, const char* arg)
191 {
192   int iResult=0;
193   if (arg) {
194     TString parameter(arg);
195     AliHLTUInt32_t base=1;
196     parameter.Remove(TString::kLeading, ' '); // remove all blanks
197     if (parameter.EndsWith("k")) {
198       base=0x400; // one k
199       parameter.Remove(TString::kTrailing, 'k');
200     } else if (parameter.EndsWith("M")) {
201       base=0x100000; // one M
202       parameter.Remove(TString::kTrailing, 'M');
203     }
204     if (parameter.IsDigit()) {
205       size=(AliHLTUInt32_t)parameter.Atoi()*base;
206     } else {
207       iResult=-ERANGE;
208     }
209   } else {
210     iResult=-EINVAL;
211   }
212   return iResult;
213 }
214
215 int AliHLTDataGenerator::ScanArgument(int argc, const char** argv)
216 {
217   // see header file for class documentation
218
219   // there are no other arguments than the standard ones
220   if (argc==0 && argv==NULL) {
221     // this is just to get rid of the warning "unused parameter"
222   }
223   return -EPROTO;
224 }
225
226 int AliHLTDataGenerator::DoDeinit()
227 {
228   // see header file for class documentation
229   int iResult=0;
230   return iResult;
231 }
232
233 int AliHLTDataGenerator::GetEvent( const AliHLTComponentEventData& /*evtData*/,
234                                    AliHLTComponentTriggerData& /*trigData*/,
235                                    AliHLTUInt8_t* outputPtr, 
236                                    AliHLTUInt32_t& size,
237                                    vector<AliHLTComponentBlockData>& outputBlocks )
238 {
239   int iResult=0;
240   AliHLTUInt32_t generated=fCurrSize;
241   if (generated<=size ) {
242     AliHLTComponentBlockData bd;
243     FillBlockData(bd);
244     bd.fPtr=outputPtr;
245     bd.fOffset=0;
246     bd.fSize=generated;
247     bd.fDataType=fDataType;
248     bd.fSpecification=fSpecification;
249     outputBlocks.push_back(bd);
250     size=generated;
251
252     if (fModulo>0 && ((GetEventCount()+1)%fModulo)==0) {
253       // manipulate the size
254       if (fDivisor>0) {
255         fCurrSize/=fDivisor;
256         if (fCurrSize==0) fCurrSize=fSize; //reset
257       }
258       if (fSubtractor>0) {
259         if (fCurrSize<fSubtractor) {
260           fCurrSize=fSize; // reset
261         } else {
262           fCurrSize-=fSubtractor;
263         }
264       }
265       HLTDebug("manipulated output size: %d", fCurrSize);
266     }
267
268   } else {
269     iResult=-ENOSPC;
270   }
271
272   return iResult;
273 }