]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/TPCLib/AliHLTTPCDigitDumpComponent.cxx
added AliFlatExternalTrackParam to HLT global library
[u/mrichter/AliRoot.git] / HLT / TPCLib / AliHLTTPCDigitDumpComponent.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   AliHLTTPCDigitDumpComponent.cxx
20     @author Matthias Richter
21     @date   
22     @brief  Special file writer converting TPC digit input to ASCII. */
23
24 // see header file for class documentation
25 // or
26 // refer to README to build package
27 // or
28 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
29
30 #include <cassert>
31 #include "AliHLTTPCDigitDumpComponent.h"
32 #include "AliHLTTPCTransform.h"
33 #include "AliHLTTPCDigitReader.h"
34 #include "AliHLTTPCDigitReaderUnpacked.h"
35 #include "AliHLTTPCDigitReader32Bit.h"
36 #include "AliHLTTPCDefinitions.h"
37
38 /** ROOT macro for the implementation of ROOT specific class methods */
39 ClassImp(AliHLTTPCDigitDumpComponent)
40
41 AliHLTTPCDigitDumpComponent::AliHLTTPCDigitDumpComponent()
42   :
43   AliHLTFileWriter(),
44   fDigitReaderType(kDigitReader32Bit),
45   fRcuTrailerSize(2),
46   fUnsorted(true),
47   fbBulkMode(true),
48   fpReader(NULL),
49   f32BitFormat(kFALSE)
50 {
51   // see header file for class documentation
52   // or
53   // refer to README to build package
54   // or
55   // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
56 }
57
58 AliHLTTPCDigitDumpComponent::~AliHLTTPCDigitDumpComponent()
59 {
60   // see header file for class documentation
61 }
62
63 const char* AliHLTTPCDigitDumpComponent::GetComponentID()
64 {
65   // see header file for class documentation
66   return "TPCDigitDump";
67 }
68
69 void AliHLTTPCDigitDumpComponent::GetInputDataTypes( vector<AliHLTComponentDataType>& list)
70 {
71   // see header file for class documentation
72   list.clear();
73   list.push_back(kAliHLTAnyDataType);
74 }
75
76 AliHLTComponent* AliHLTTPCDigitDumpComponent::Spawn()
77 {
78   // see header file for class documentation
79   return new AliHLTTPCDigitDumpComponent;
80 }
81
82 int AliHLTTPCDigitDumpComponent::InitWriter()
83 {
84   // see header file for class documentation
85   int iResult=0;
86   switch (fDigitReaderType) {
87   case kDigitReaderUnpacked:
88     HLTInfo("create DigitReaderUnpacked");
89     fpReader=new AliHLTTPCDigitReaderUnpacked; 
90     break;
91   case kDigitReader32Bit:
92     HLTInfo("create DigitReader32Bit");
93     fpReader=new AliHLTTPCDigitReader32Bit();
94     f32BitFormat = kTRUE;
95     break;
96   }
97   if (!fpReader) {
98     HLTError("can not create digit reader of type %d", fDigitReaderType);
99     iResult=-EFAULT;
100   } else {
101     fpReader->SetUnsorted(fUnsorted);
102   }
103   return iResult;
104 }
105
106 int AliHLTTPCDigitDumpComponent::ScanArgument(int argc, const char** argv)
107 {
108   // see header file for class documentation
109   int iResult=0;
110   TString argument="";
111   bool bMissingParam=0;
112   int i=0;
113   do {
114     if (i>=argc || (argument=argv[i]).IsNull()) continue;
115
116     // -rawreadermode
117     if (argument.CompareTo("-rawreadermode")==0) {
118       if ((bMissingParam=(++i>=argc))) break;
119       HLTWarning("argument '-rawreadermode' deprecated");
120       break;
121     }
122
123     // -digitreader
124     if (argument.CompareTo("-digitreader")==0) {
125       if ((bMissingParam=(++i>=argc))) break;
126       TString param=argv[i];
127       if (param.CompareTo("unpacked", TString::kIgnoreCase)==0) {
128         fDigitReaderType=kDigitReaderUnpacked;
129       } else if (param.CompareTo("packed", TString::kIgnoreCase)==0) {
130         HLTWarning("argument 'packed' is deprecated, falling back to DigitReader32Bit");
131         fDigitReaderType=kDigitReader32Bit;
132       } else if (param.CompareTo("raw", TString::kIgnoreCase)==0) {
133         HLTWarning("argument 'raw' is deprecated, falling back to DigitReader32Bit");
134         fDigitReaderType=kDigitReader32Bit;
135       } else if (param.CompareTo("decoder", TString::kIgnoreCase)==0) {
136         HLTWarning("argument 'decoder' is deprecated, falling back to DigitReader32Bit");
137         fDigitReaderType=kDigitReader32Bit;
138       } else if (param.CompareTo("32bit", TString::kIgnoreCase)==0) {
139         fDigitReaderType=kDigitReader32Bit;
140       } else {
141         HLTError("unknown digit reader type %s", param.Data());
142         iResult=-EINVAL;
143       }
144
145       break;
146     }
147
148     // -rcutrailersize
149     if (argument.CompareTo("-rcutrailersize")==0) {
150       if ((bMissingParam=(++i>=argc))) break;
151       char *endptr=NULL;
152       fRcuTrailerSize=strtoul(argv[i], &endptr, 0);
153       if (/*endptr ||*/ fRcuTrailerSize<1) {
154         HLTError("invalid parameter '%s', %s", argv[i], endptr==NULL?"number >= 1 expected":"can not convert string to number");
155         iResult=-EINVAL;
156       }
157       break;
158     }
159
160     // -unsorted
161     if (argument.CompareTo("-unsorted")==0) {
162       fUnsorted=true;
163       break;
164     }
165
166     // -sorted
167     if (argument.CompareTo("-sorted")==0) {
168       fUnsorted=false;
169       break;
170     }
171
172     // -bulk
173     if (argument.CompareTo("-bulk")==0) {
174       fbBulkMode=true;
175       break;
176     }
177
178     // -stream
179     if (argument.CompareTo("-stream")==0) {
180       fbBulkMode=false;
181       break;
182     }
183   } while (0); // just use the do/while here to have the option of breaking
184
185   if (bMissingParam) iResult=-EPROTO;
186   else if (iResult>=0) iResult=i;
187
188   return iResult;
189 }
190
191 int AliHLTTPCDigitDumpComponent::CloseWriter()
192 {
193   // see header file for class documentation
194   if (fpReader) delete fpReader;
195   fpReader=NULL;
196   return 0;
197 }
198
199 int AliHLTTPCDigitDumpComponent::DumpEvent( const AliHLTComponentEventData& evtData,
200                                             const AliHLTComponentBlockData* /*blocks*/, 
201                                             AliHLTComponentTriggerData& /*trigData*/ )
202 {
203   // see header file for class documentation
204   int iResult=0;
205   int iPrintedSlice=-1;
206   int iPrintedPart=-1;
207   int blockno=0;
208   const AliHLTComponentBlockData* pDesc=NULL;
209
210   AliHLTTPCDigitReader* pReader=fpReader;
211   if (!pReader) return -ENODEV;
212
213   for (pDesc=GetFirstInputBlock(kAliHLTAnyDataType); pDesc!=NULL; pDesc=GetNextInputBlock(), blockno++) {
214     HLTDebug("event %Lu block %d: %s 0x%08x size %d", evtData.fEventID, blockno, DataType2Text(pDesc->fDataType).c_str(), pDesc->fSpecification, pDesc->fSize);
215
216     if (fDigitReaderType==kDigitReaderUnpacked && pDesc->fDataType!=AliHLTTPCDefinitions::fgkUnpackedRawDataType) continue;
217     else if (fDigitReaderType!=kDigitReaderUnpacked && pDesc->fDataType!=(kAliHLTDataTypeDDLRaw|kAliHLTDataOriginTPC)) continue;
218
219     TString filename;
220     iResult=BuildFileName(evtData.fEventID, blockno, pDesc->fDataType, pDesc->fSpecification, filename);
221     ios::openmode filemode=(ios::openmode)0;
222     if (fCurrentFileName.CompareTo(filename)==0) {
223       // append to the file
224       filemode=ios::app;
225     } else {
226       // store the file for the next block
227       fCurrentFileName=filename;
228     }
229     if (iResult>=0) {
230       ofstream dump(filename.Data(), filemode);
231       if (dump.good()) {
232         int part=AliHLTTPCDefinitions::GetMinPatchNr(*pDesc);
233         assert(part==AliHLTTPCDefinitions::GetMaxPatchNr(*pDesc));
234         int slice=AliHLTTPCDefinitions::GetMinSliceNr(*pDesc);
235         assert(slice==AliHLTTPCDefinitions::GetMaxSliceNr(*pDesc));
236         int firstRow=AliHLTTPCTransform::GetFirstRow(part);
237         int lastRow=AliHLTTPCTransform::GetLastRow(part);
238
239         iResult=pReader->InitBlock(pDesc->fPtr,pDesc->fSize,firstRow,lastRow,part,slice);
240
241         int iPrintedRow=-1;
242         int iPrintedPad=-1;
243         int iLastTime=-1;
244         if (fbBulkMode) {
245           while (pReader->NextChannel()) {
246             if (PrintHeaders(slice, iPrintedSlice, part, iPrintedPart, pReader, iPrintedRow, iPrintedPad, dump)) {
247               iLastTime=-1;
248             }
249             while (pReader->NextBunch()) {
250               int bunchLength=pReader->GetBunchSize();
251               
252               // Kenneth: 20-04-09. The following if have been added because of inconsistency in the 40 bit decoder and the 32 bit decoder.
253               // GetSignals() in the 40 bit decoder returns an array of UInt_t while the 32 bit one returns UShort_t
254               if(f32BitFormat == kTRUE){
255                 const  UShort_t* bunchData=pReader->GetSignalsShort();
256                 
257                 // bunch data is printed in 'reverse' order in order to produce
258                 // the same output as in stream reading mode
259                 dump << "                     Time " << pReader->GetTime()+bunchLength-1 << ":  ";
260                 for (int bin=bunchLength-1; bin>=0; bin--) {
261                   dump << "  " << bunchData[bin];
262                 }
263                 dump << "    -> Time: " << pReader->GetTime() << endl;
264               }
265               else{
266                 const  UInt_t* bunchData=pReader->GetSignals();
267                 dump << "                     Time " << pReader->GetTime()+bunchLength-1 << ":  ";
268                 for (int bin=0; bin<bunchLength; bin++) {
269                   dump << "  " << bunchData[bin];
270                 }
271                 dump << "    -> Time: " << pReader->GetTime() << endl;
272               }
273             }
274           }
275           dump << endl;
276         } else {
277         while (pReader->Next()) {
278           if ((iPrintedSlice!=-1 && iLastTime!=-1 && iLastTime!=pReader->GetTime()+1 && iLastTime!=pReader->GetTime()-1)) {
279             dump << "    -> Time: " << iLastTime << endl;
280           } else if ((iPrintedPad!=-1 && iPrintedPad!=pReader->GetPad()) ||
281                      (iPrintedRow!=-1 && iPrintedRow!=pReader->GetRow())) {
282             dump << "    -> Time: " << iLastTime << endl;
283             //dump << endl;
284           }
285
286           if (PrintHeaders(slice, iPrintedSlice, part, iPrintedPart, pReader, iPrintedRow, iPrintedPad, dump)) {
287             iLastTime=-1;
288           }
289           if (iLastTime==-1 || (iLastTime!=pReader->GetTime()+1 && iLastTime!=pReader->GetTime()-1)) {
290             dump << "                     Time " << pReader->GetTime() << ":  ";
291           }
292           iLastTime=pReader->GetTime();
293           dump << "  " << pReader->GetSignal();
294         }
295         if (iLastTime>=0) dump << "    -> Time: " << iLastTime << endl << endl;
296         }
297       } else {
298         HLTError("can not open file %s for writing", filename.Data());
299         iResult=-EBADF;
300       }
301       dump.close();
302     }
303     pReader->Reset();
304   }
305   return iResult;
306 }
307
308 int AliHLTTPCDigitDumpComponent::PrintHeaders(int slice, int &iPrintedSlice,
309                                               int part, int &iPrintedPart,
310                                               AliHLTTPCDigitReader* pReader,
311                                               int &iPrintedRow, int &iPrintedPad,
312                                               ofstream &dump) const
313 {
314   // see header file for class documentation
315   int iResult=0;
316   assert(pReader);
317   if (iPrintedSlice!=slice || iPrintedPart!=part) {
318     iPrintedSlice=slice;
319     iPrintedPart=part;
320     dump << "====================================================================" << endl;
321     dump << "    Slice: " << iPrintedSlice << "   Partition: " << iPrintedPart << endl;
322     iPrintedRow=-1;
323   }
324   if (iPrintedRow!=pReader->GetRow()) {
325     iPrintedRow=pReader->GetRow();
326     dump << "--------------------------------------------------------------------" << endl;
327     dump << "Row: " << iPrintedRow << endl;
328     iPrintedPad=-1;
329   }
330   if (iPrintedPad!=pReader->GetPad()) {
331     iPrintedPad=pReader->GetPad();
332     dump << "Row: " << iPrintedRow << "  Pad: " << iPrintedPad << "  HW address: " << pReader->GetAltroBlockHWaddr() << endl;
333     iResult=1;
334   }
335
336   return iResult;
337 }