]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/BASE/util/AliHLTRawReaderPublisherComponent.cxx
01e26e67c1af8cc2fa1e8e36d3fd1c8491f5115f
[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
37 /** global instance for agent registration */
38 AliHLTRawReaderPublisherComponent gAliHLTRawReaderPublisherComponent;
39
40 /** ROOT macro for the implementation of ROOT specific class methods */
41 ClassImp(AliHLTRawReaderPublisherComponent)
42
43 AliHLTRawReaderPublisherComponent::AliHLTRawReaderPublisherComponent()
44   :
45   fMaxSize(5000000),
46   fDetector(),
47   fMinEquId(-1),
48   fMaxEquId(-1),
49   fVerbose(kFALSE),
50   fDataType(kAliHLTVoidDataType),
51   fSpecification(kAliHLTVoidDataSpec)
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   return fDataType;
74 }
75
76 void AliHLTRawReaderPublisherComponent::GetOutputDataSize( unsigned long& constBase, double& inputMultiplier )
77 {
78   constBase=fMaxSize;
79   inputMultiplier=1;
80 }
81
82 AliHLTComponent* AliHLTRawReaderPublisherComponent::Spawn()
83 {
84   // see header file for class documentation
85   return new AliHLTRawReaderPublisherComponent;
86 }
87
88 int AliHLTRawReaderPublisherComponent::DoInit( int argc, const char** argv )
89 {
90   // see header file for class documentation
91   int iResult=0;
92
93   // scan arguments
94   TString argument="";
95   int bMissingParam=0;
96   for (int i=0; i<argc && iResult>=0; i++) {
97     argument=argv[i];
98     if (argument.IsNull()) continue;
99
100     // -detector
101     if (argument.CompareTo("-detector")==0) {
102       if ((bMissingParam=(++i>=argc))) break;
103       fDetector=argv[i];
104
105       // -equipmentid, -minid
106     } else if (argument.CompareTo("-equipmentid")==0 ||
107                argument.CompareTo("-minid")==0) {
108       if ((bMissingParam=(++i>=argc))) break;
109       TString parameter(argv[i]);
110       parameter.Remove(TString::kLeading, ' '); // remove all blanks
111       if (parameter.IsDigit()) {
112         fMinEquId=(AliHLTUInt32_t)parameter.Atoi();
113       } else {
114         HLTError("wrong parameter for argument %s, number expected", argument.Data());
115         iResult=-EINVAL;
116       }
117
118       // -maxid
119     } else if (argument.CompareTo("-maxid")==0) {
120       if ((bMissingParam=(++i>=argc))) break;
121       TString parameter(argv[i]);
122       parameter.Remove(TString::kLeading, ' '); // remove all blanks
123       if (parameter.IsDigit()) {
124         fMaxEquId=(AliHLTUInt32_t)parameter.Atoi();
125       } else {
126         HLTError("wrong parameter for argument %s, number expected", argument.Data());
127         iResult=-EINVAL;
128       }
129
130       // -verbose
131     } else if (argument.CompareTo("-verbose")==0) {
132       fVerbose=kTRUE;
133
134       // -datatype
135     } else if (argument.CompareTo("-datatype")==0) {
136       if ((bMissingParam=(++i>=argc))) break;
137       memcpy(&fDataType.fID, argv[i], TMath::Min(kAliHLTComponentDataTypefIDsize, (Int_t)strlen(argv[i])));
138       if ((bMissingParam=(++i>=argc))) break;
139       memcpy(&fDataType.fOrigin, argv[i], TMath::Min(kAliHLTComponentDataTypefOriginSize, (Int_t)strlen(argv[i])));
140
141       // -dataspec
142     } else if (argument.CompareTo("-dataspec")==0) {
143       if ((bMissingParam=(++i>=argc))) break;
144       TString parameter(argv[i]);
145       parameter.Remove(TString::kLeading, ' '); // remove all blanks
146       if (parameter.IsDigit()) {
147         fSpecification=(AliHLTUInt32_t)parameter.Atoi();
148       } else if (parameter.BeginsWith("0x") &&
149                  parameter.Replace(0,2,"",0).IsHex()) {
150         sscanf(parameter.Data(),"%x", &fSpecification);
151       } else {
152         HLTError("wrong parameter for argument %s, number expected", argument.Data());
153         iResult=-EINVAL;
154       }
155     } else {
156       HLTError("unknown argument %s", argument.Data());
157       iResult=-EINVAL;
158     }
159   }
160   if (bMissingParam) {
161     HLTError("missing parameter for argument %s", argument.Data());
162     iResult=-EINVAL;
163   }
164
165   if (iResult<0) return iResult;
166
167   if (fMinEquId>fMaxEquId) fMaxEquId=fMinEquId;
168
169   if (fMinEquId<0) {
170     AliErrorStream() << "equipment id required, use \'-equipmentid\' option" << endl;
171     return -EINVAL;
172   }
173
174   if (!fDetector.IsNull()) {
175     AliErrorStream() << "option \'-detector\' not implemented" << endl;
176     return -ENOSYS;
177   }
178
179   AliHLTUInt32_t dummy;
180   if (fMinEquId!=fMaxEquId && GetSpecificationFromEquipmentId(0, dummy)==-ENOSYS) {
181     AliWarningStream() << "publication of multiple equipment ids needs implementation of a child and function GetSpecificationFromEquipmentId to set correct specifications" << endl;
182     //return -EINVAL;
183   }
184
185   AliRawReader* pRawReader=GetRawReader();
186   if ((pRawReader=GetRawReader())!=NULL) {
187   } else {
188     AliErrorStream() << "RawReader instance needed" << endl;
189     return -EINVAL;
190   }
191
192   return iResult;
193 }
194
195 int AliHLTRawReaderPublisherComponent::DoDeinit()
196 {
197   // see header file for class documentation
198   int iResult=0;
199   return iResult;
200 }
201
202 int AliHLTRawReaderPublisherComponent::GetEvent(const AliHLTComponentEventData& evtData, 
203                                                 AliHLTComponentTriggerData& trigData, 
204                                                 AliHLTUInt8_t* outputPtr, 
205                                                 AliHLTUInt32_t& size, 
206                                                 vector<AliHLTComponentBlockData>& outputBlocks)
207 {
208   // see header file for class documentation
209   int iResult=0;
210   int offset=0;
211   AliHLTUInt8_t* pTgt=outputPtr;
212   assert(outputPtr!=NULL || size==0);
213   AliRawReader* pRawReader=GetRawReader();
214   if (pRawReader) {
215     pRawReader->Reset();
216     pRawReader->SelectEquipment(-1, fMinEquId, fMaxEquId);
217     AliInfo(Form("get event from RawReader %p equipment id range [%d,%d]", pRawReader, fMinEquId, fMaxEquId));
218     while (pRawReader->ReadHeader() && (iResult>=0 || iResult==-ENOSPC)) {
219       const AliRawDataHeader* pHeader=pRawReader->GetDataHeader();
220       assert(pHeader!=NULL);
221       if (pHeader==NULL) continue;
222       int readSize=pRawReader->GetDataSize()+sizeof(AliRawDataHeader);
223       int id=pRawReader->GetEquipmentId();
224       AliInfo(Form("got header for id %d, size %d", id, readSize));
225       if (fMinEquId>id || fMaxEquId<id) {
226         AliError(Form("id %d returned from RawReader is outside range [%d,%d]", id, fMinEquId, fMaxEquId));
227         continue;
228       }
229       if (readSize<=size-offset) {
230         memcpy(pTgt, pHeader, sizeof(AliRawDataHeader));
231         pTgt+=sizeof(AliRawDataHeader);
232         if (readSize>0) {
233           if (!pRawReader->ReadNext(pTgt, readSize-sizeof(AliRawDataHeader))) {
234             AliError(Form("error reading %d bytes from RawReader %p", readSize-sizeof(AliRawDataHeader), pRawReader));
235             iResult=-ENODATA;
236             break;
237           }
238           pTgt+=readSize-sizeof(AliRawDataHeader);
239         }
240       } else {
241         // we keep the loop going in order to collect the full size
242         fMaxSize=offset+readSize;
243         iResult=-ENOSPC;
244       }
245       if (iResult>=0) {
246         AliHLTComponentBlockData bd;
247         FillBlockData( bd );
248         bd.fOffset = offset;
249         bd.fSize = readSize;
250         bd.fDataType = fDataType;
251         if (fSpecification == kAliHLTVoidDataSpec) {
252           GetSpecificationFromEquipmentId(id, bd.fSpecification);
253         } else {
254           bd.fSpecification=fSpecification;
255         }
256         outputBlocks.push_back( bd );
257       }
258       offset+=readSize;
259     }
260     if (offset<=size) size=offset;
261   } else {
262     AliErrorStream() << "RawReader uninitialized" << endl;
263     iResult=-EFAULT;
264   }
265   return iResult;
266 }
267
268 int AliHLTRawReaderPublisherComponent::GetSpecificationFromEquipmentId(int id, AliHLTUInt32_t& specification) const {
269   return specification=id;
270 }