]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/BASE/util/AliHLTRawReaderPublisherComponent.cxx
e552133bce90e75ae0408bcb488bc50806aabccf
[u/mrichter/AliRoot.git] / HLT / BASE / util / AliHLTRawReaderPublisherComponent.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   AliHLTRawReaderPublisherComponent.cxx
20     @author Matthias Richter
21     @date   
22     @brief  A general tree publisher component for the AliRawReader.
23 */
24
25 // see header file for class documentation
26 // or
27 // refer to README to build package
28 // or
29 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
30
31 #include "AliHLTRawReaderPublisherComponent.h"
32 #include "AliRawReader.h"
33 #include "AliLog.h"
34 #include <cerrno>
35 #include <cassert>
36 #include <list>
37
38 /** ROOT macro for the implementation of ROOT specific class methods */
39 ClassImp(AliHLTRawReaderPublisherComponent)
40
41 AliHLTRawReaderPublisherComponent::AliHLTRawReaderPublisherComponent()
42   :
43   fMaxSize(5000000),
44   fDetector(),
45   fMinEquId(-1),
46   fMaxEquId(-1),
47   fVerbose(kFALSE),
48   fDataType(kAliHLTVoidDataType),
49   fSpecification(kAliHLTVoidDataSpec),
50   fSkipEmpty(kFALSE)
51 {
52   // see header file for class documentation
53   // or
54   // refer to README to build package
55   // or
56   // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
57 }
58
59 AliHLTRawReaderPublisherComponent::~AliHLTRawReaderPublisherComponent()
60 {
61   // see header file for class documentation
62 }
63
64 const char* AliHLTRawReaderPublisherComponent::GetComponentID()
65 {
66   // see header file for class documentation
67   return "AliRawReaderPublisher";
68 }
69
70 AliHLTComponentDataType AliHLTRawReaderPublisherComponent::GetOutputDataType()
71 {
72   // see header file for class documentation
73   return fDataType;
74 }
75
76 void AliHLTRawReaderPublisherComponent::GetOutputDataSize( unsigned long& constBase, double& inputMultiplier )
77 {
78   // see header file for class documentation
79   constBase=fMaxSize;
80   inputMultiplier=1;
81 }
82
83 AliHLTComponent* AliHLTRawReaderPublisherComponent::Spawn()
84 {
85   // see header file for class documentation
86   return new AliHLTRawReaderPublisherComponent;
87 }
88
89 int AliHLTRawReaderPublisherComponent::DoInit( int argc, const char** argv )
90 {
91   // see header file for class documentation
92   int iResult=0;
93
94   // scan arguments
95   TString argument="";
96   int bMissingParam=0;
97   for (int i=0; i<argc && iResult>=0; i++) {
98     argument=argv[i];
99     if (argument.IsNull()) continue;
100
101     // -detector
102     if (argument.CompareTo("-detector")==0) {
103       if ((bMissingParam=(++i>=argc))) break;
104       fDetector=argv[i];
105
106       // -equipmentid, -minid
107     } else if (argument.CompareTo("-equipmentid")==0 ||
108                argument.CompareTo("-minid")==0) {
109       if ((bMissingParam=(++i>=argc))) break;
110       TString parameter(argv[i]);
111       parameter.Remove(TString::kLeading, ' '); // remove all blanks
112       if (parameter.IsDigit()) {
113         fMinEquId=(AliHLTUInt32_t)parameter.Atoi();
114       } else {
115         HLTError("wrong parameter for argument %s, number expected", argument.Data());
116         iResult=-EINVAL;
117       }
118
119       // -maxid
120     } else if (argument.CompareTo("-maxid")==0) {
121       if ((bMissingParam=(++i>=argc))) break;
122       TString parameter(argv[i]);
123       parameter.Remove(TString::kLeading, ' '); // remove all blanks
124       if (parameter.IsDigit()) {
125         fMaxEquId=(AliHLTUInt32_t)parameter.Atoi();
126       } else {
127         HLTError("wrong parameter for argument %s, number expected", argument.Data());
128         iResult=-EINVAL;
129       }
130
131       // -verbose
132     } else if (argument.CompareTo("-verbose")==0) {
133       fVerbose=kTRUE;
134
135       // -skipempty
136     } else if (argument.CompareTo("-skipempty")==0) {
137       fSkipEmpty=kTRUE;
138
139       // -datatype
140     } else if (argument.CompareTo("-datatype")==0) {
141       if ((bMissingParam=(++i>=argc))) break;
142       memcpy(&fDataType.fID, argv[i], TMath::Min(kAliHLTComponentDataTypefIDsize, (Int_t)strlen(argv[i])));
143       if ((bMissingParam=(++i>=argc))) break;
144       memcpy(&fDataType.fOrigin, argv[i], TMath::Min(kAliHLTComponentDataTypefOriginSize, (Int_t)strlen(argv[i])));
145
146       // -dataspec
147     } else if (argument.CompareTo("-dataspec")==0) {
148       if ((bMissingParam=(++i>=argc))) break;
149       TString parameter(argv[i]);
150       parameter.Remove(TString::kLeading, ' '); // remove all blanks
151       if (parameter.IsDigit()) {
152         fSpecification=(AliHLTUInt32_t)parameter.Atoi();
153       } else if (parameter.BeginsWith("0x") &&
154                  parameter.Replace(0,2,"",0).IsHex()) {
155         sscanf(parameter.Data(),"%x", &fSpecification);
156       } else {
157         HLTError("wrong parameter for argument %s, number expected", argument.Data());
158         iResult=-EINVAL;
159       }
160     } else {
161       HLTError("unknown argument %s", argument.Data());
162       iResult=-EINVAL;
163     }
164   }
165   if (bMissingParam) {
166     HLTError("missing parameter for argument %s", argument.Data());
167     iResult=-EINVAL;
168   }
169
170   if (iResult<0) return iResult;
171
172   if (fMinEquId>fMaxEquId) fMaxEquId=fMinEquId;
173
174   if (fMinEquId<0) {
175     AliErrorStream() << "equipment id required, use \'-equipmentid\' option" << endl;
176     return -EINVAL;
177   }
178
179   if (!fDetector.IsNull()) {
180     AliErrorStream() << "option \'-detector\' not implemented" << endl;
181     return -ENOSYS;
182   }
183
184   AliHLTUInt32_t dummy;
185   if (fMinEquId!=fMaxEquId && GetSpecificationFromEquipmentId(0, dummy)==-ENOSYS) {
186     AliWarningStream() << "publication of multiple equipment ids needs implementation of a child and function GetSpecificationFromEquipmentId to set correct specifications" << endl;
187     //return -EINVAL;
188   }
189
190   AliRawReader* pRawReader=GetRawReader();
191   if ((pRawReader=GetRawReader())!=NULL) {
192   } else {
193     AliErrorStream() << "RawReader instance needed" << endl;
194     return -EINVAL;
195   }
196
197   return iResult;
198 }
199
200 int AliHLTRawReaderPublisherComponent::DoDeinit()
201 {
202   // see header file for class documentation
203   int iResult=0;
204   return iResult;
205 }
206
207 int AliHLTRawReaderPublisherComponent::GetEvent(const AliHLTComponentEventData& /*evtData*/, 
208                                                 AliHLTComponentTriggerData& /*trigData*/, 
209                                                 AliHLTUInt8_t* outputPtr, 
210                                                 AliHLTUInt32_t& size, 
211                                                 vector<AliHLTComponentBlockData>& outputBlocks)
212 {
213   // see header file for class documentation
214   int iResult=0;
215   unsigned int offset=0;
216   assert(outputPtr!=NULL || size==0);
217   AliRawReader* pRawReader=GetRawReader();
218   if (pRawReader) {
219     pRawReader->Reset();
220     pRawReader->SelectEquipment(-1, fMinEquId, fMaxEquId);
221     if (fVerbose) {
222       AliInfo(Form("get event from RawReader %p equipment id range [%d,%d]", pRawReader, fMinEquId, fMaxEquId));
223     } else {
224       AliDebug(0, Form("get event from RawReader %p equipment id range [%d,%d]", pRawReader, fMinEquId, fMaxEquId));
225     }
226     list<int> processedIds;
227     while (pRawReader->ReadHeader() && (iResult>=0 || iResult==-ENOSPC)) {
228       const AliRawDataHeader* pHeader=pRawReader->GetDataHeader();
229       assert(pHeader!=NULL);
230       if (pHeader==NULL) continue;
231       unsigned int readSize=pRawReader->GetDataSize()+sizeof(AliRawDataHeader);
232       int id=pRawReader->GetEquipmentId();
233       AliInfo(Form("got header for id %d, size %d", id, readSize));
234       if (fMinEquId>id || fMaxEquId<id) {
235         AliError(Form("id %d returned from RawReader is outside range [%d,%d]", id, fMinEquId, fMaxEquId));
236         continue;
237       }
238       processedIds.push_back(id);
239       if (readSize<=size-offset) {
240         memcpy(outputPtr+offset, pHeader, sizeof(AliRawDataHeader));
241         if (readSize>sizeof(AliRawDataHeader)) {
242           if (!pRawReader->ReadNext(outputPtr+offset+sizeof(AliRawDataHeader), readSize-sizeof(AliRawDataHeader))) {
243             AliError(Form("error reading %d bytes from RawReader %p", readSize-sizeof(AliRawDataHeader), pRawReader));
244             iResult=-ENODATA;
245             break;
246           }
247         }
248         AliHLTComponentBlockData bd;
249         FillBlockData( bd );
250         bd.fOffset = offset;
251         bd.fSize = readSize;
252         bd.fDataType = fDataType;
253         if (fSpecification == kAliHLTVoidDataSpec) {
254           GetSpecificationFromEquipmentId(id, bd.fSpecification);
255         } else {
256           bd.fSpecification=fSpecification;
257         }
258         outputBlocks.push_back( bd );
259       } else {
260         // we keep the loop going in order to collect the full size
261         fMaxSize=offset+readSize;
262         iResult=-ENOSPC;
263       }
264       offset+=readSize;
265     }
266     if (!fSkipEmpty && processedIds.size()!=size_t(fMaxEquId-fMinEquId+1)) {
267       // add further empty data blocks
268       AliRawDataHeader header;
269       header.fSize=sizeof(AliRawDataHeader);
270       processedIds.sort();
271       list<int>::iterator curr=processedIds.begin();
272       for (int id=fMinEquId; id<=fMaxEquId; id++) {
273         if (curr!=processedIds.end() && *curr<=id) {
274           curr++;
275         } else {
276           if (sizeof(AliRawDataHeader)<=size-offset) {
277             HLTInfo("add empty data block for equipment id %d", id);
278             memcpy(outputPtr+offset, &header, sizeof(AliRawDataHeader));
279             AliHLTComponentBlockData bd;
280             FillBlockData( bd );
281             bd.fOffset = offset;
282             bd.fSize = sizeof(AliRawDataHeader);
283             bd.fDataType = fDataType;
284             if (fSpecification == kAliHLTVoidDataSpec) {
285               GetSpecificationFromEquipmentId(id, bd.fSpecification);
286             } else {
287               bd.fSpecification=fSpecification;
288             }
289             outputBlocks.push_back( bd );
290           } else {
291             // we keep the loop going in order to collect the full size
292             fMaxSize=offset+sizeof(AliRawDataHeader);
293             iResult=-ENOSPC;
294           }
295           offset+=sizeof(AliRawDataHeader);
296         }
297       }
298     }
299     if (offset<=size) {
300       size=offset;
301     } else {
302       size=0;
303       outputBlocks.clear();
304     }
305   } else {
306     AliErrorStream() << "RawReader uninitialized" << endl;
307     iResult=-EFAULT;
308   }
309   return iResult;
310 }
311
312 int AliHLTRawReaderPublisherComponent::GetSpecificationFromEquipmentId(int id, AliHLTUInt32_t& specification) const {
313   // see header file for class documentation
314   return specification=id;
315 }