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