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