]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/PHOS/AliHLTPHOSRawAnalyzerComponentv2.cxx
d5cc6164cef77b2c8596430662ec687327947a1f
[u/mrichter/AliRoot.git] / HLT / PHOS / AliHLTPHOSRawAnalyzerComponentv2.cxx
1 // $Id$
2
3 /**************************************************************************
4  * This file is property of and copyright by the ALICE HLT Project        * 
5  * All rights reserved.                                                   *
6  *                                                                        *
7  * Primary Authors: Per Thomas Hille, Oystein Djuvsland                   *
8  *                                                                        *
9  * Permission to use, copy, modify and distribute this software and its   *
10  * documentation strictly for non-commercial purposes is hereby granted   *
11  * without fee, provided that the above copyright notice appears in all   *
12  * copies and that both the copyright notice and this permission notice   *
13  * appear in the supporting documentation. The authors make no claims     *
14  * about the suitability of this software for any purpose. It is          * 
15  * provided "as is" without express or implied warranty.                  *
16  **************************************************************************/
17
18 #include "AliHLTPHOSRawAnalyzer.h"
19 #include "AliHLTPHOSRawAnalyzerComponentv2.h"
20 #include "AliHLTPHOSChannelDataHeaderStruct.h"
21 #include "AliHLTPHOSChannelDataStruct.h"
22 #include "AliHLTPHOSMapper.h"
23 #include "AliHLTPHOSSanityInspector.h"
24 #include  "AliAltroDecoder.h"
25 #include  "AliAltroData.h"   
26 #include  "AliAltroBunch.h"  
27
28
29 AliHLTPHOSRawAnalyzerComponentv2::AliHLTPHOSRawAnalyzerComponentv2():
30   AliHLTPHOSRcuProcessor(), 
31   fAnalyzerPtr(0), 
32   fMapperPtr(0), 
33   fSanityInspectorPtr(0),
34   fDecoderPtr(0),  
35   fAltroDataPtr(0),
36   fAltroBunchPtr(0),
37   fAlgorithm(0),
38   fOffset(0),
39   fBunchSizeCut(0),
40   fMinPeakPosition(0),
41   fMaxPeakPosition(100)
42 {
43   //comment
44   fMapperPtr = new AliHLTPHOSMapper();
45   fAltroDataPtr = new AliAltroData();
46   fAltroBunchPtr = new AliAltroBunch();
47   fDecoderPtr = new AliAltroDecoder();
48   fSanityInspectorPtr = new AliHLTPHOSSanityInspector();
49 }
50
51
52 AliHLTPHOSRawAnalyzerComponentv2::~AliHLTPHOSRawAnalyzerComponentv2()
53 {
54   //comment
55   Deinit();
56 }
57
58
59
60 int 
61 AliHLTPHOSRawAnalyzerComponentv2::Deinit()
62 {
63   //comment
64   if(fAnalyzerPtr)
65     {
66       delete fAnalyzerPtr;
67       fAnalyzerPtr = 0;
68     }
69   if(fMapperPtr)
70     {
71       delete  fMapperPtr;
72       fMapperPtr = 0;
73     }
74   if(fAltroDataPtr)
75     {
76       delete fAltroDataPtr;
77       fAltroDataPtr = 0;
78     }
79   if(fAltroBunchPtr)
80     {
81       delete fAltroBunchPtr;
82       fAltroBunchPtr = 0;
83     }
84   if(fDecoderPtr)
85     {
86       delete fDecoderPtr;
87       fDecoderPtr = 0;
88     }
89   return 0;
90 }
91
92 const char* 
93 AliHLTPHOSRawAnalyzerComponentv2::GetComponentID()
94 {
95   //comment
96   return "PhosRawAnalyzerv2";
97 }
98
99
100 void
101 AliHLTPHOSRawAnalyzerComponentv2::GetInputDataTypes( vector<AliHLTComponentDataType>& list)
102 {
103   //comment
104   list.clear();
105   list.push_back( AliHLTPHOSDefinitions::fgkDDLPackedRawDataType | kAliHLTDataOriginPHOS);
106 }
107
108 AliHLTComponentDataType 
109 AliHLTPHOSRawAnalyzerComponentv2::GetOutputDataType()
110 {
111   //comment
112   return AliHLTPHOSDefinitions::fgkChannelDataType;
113 }
114
115 void
116 AliHLTPHOSRawAnalyzerComponentv2::GetOutputDataSize(unsigned long& constBase, double& inputMultiplier )
117 {
118   //comment
119   constBase = sizeof(AliHLTPHOSChannelDataHeaderStruct);
120   inputMultiplier = 0.5;
121 }
122
123 int 
124 AliHLTPHOSRawAnalyzerComponentv2::DoEvent( const AliHLTComponentEventData& evtData, const AliHLTComponentBlockData* blocks, AliHLTComponentTriggerData& /*trigData*/, 
125                                          AliHLTUInt8_t* outputPtr, AliHLTUInt32_t& size, vector<AliHLTComponentBlockData>& outputBlocks )
126 {
127   //comment
128   Int_t blockSize          = 0;
129   UInt_t totSize           = 0;
130
131   const AliHLTComponentBlockData* iter = NULL; 
132   unsigned long ndx;
133
134   for( ndx = 0; ndx < evtData.fBlockCnt; ndx++ )
135     {
136       iter = blocks+ndx;
137       if ( iter->fDataType != AliHLTPHOSDefinitions::fgkDDLPackedRawDataType  )
138         {
139           HLTDebug("Data block is not of type fgkDDLPackedRawDataType");
140           continue; 
141         }
142
143       blockSize = DoIt(iter, outputPtr, size, totSize); // Processing the block
144       if(blockSize == -1) // If the processing returns -1 we are out of buffer and return an error msg.
145         {
146           return -ENOBUFS;
147         }
148
149       totSize += blockSize; //Keeping track of the used size
150       // HLTDebug("Output data size: %d - Input data size: %d", totSize, iter->fSize);
151
152       //Filling the block data
153       AliHLTComponentBlockData bdChannelData;
154       FillBlockData( bdChannelData );
155       bdChannelData.fOffset = 0; //FIXME
156       bdChannelData.fSize = blockSize;
157       bdChannelData.fDataType = AliHLTPHOSDefinitions::fgkChannelDataType;
158       bdChannelData.fSpecification = iter->fSpecification;
159       outputBlocks.push_back(bdChannelData);
160
161       outputPtr += blockSize; //Updating position of the output buffer
162     }
163
164   fPhosEventCount++; 
165   size = totSize; //telling the framework how much buffer space we have used.
166   
167   return 0;
168 }//end DoEvent
169
170 Int_t
171 AliHLTPHOSRawAnalyzerComponentv2::DoIt(const AliHLTComponentBlockData* iter, AliHLTUInt8_t* outputPtr, const AliHLTUInt32_t size, UInt_t& totSize)
172 {
173  
174   //comment
175   Int_t crazyness          = 0;
176   Int_t nSamples           = 0;
177   Short_t channelCount     = 0;
178
179   // Firs we want to write a header to the output
180   AliHLTPHOSChannelDataHeaderStruct *channelDataHeaderPtr = reinterpret_cast<AliHLTPHOSChannelDataHeaderStruct*>(outputPtr); 
181
182   // Then comes the channel data
183   AliHLTPHOSChannelDataStruct *channelDataPtr = reinterpret_cast<AliHLTPHOSChannelDataStruct*>(outputPtr+sizeof(AliHLTPHOSChannelDataHeaderStruct)); 
184
185   //Adding to the total size of data written
186   totSize += sizeof(AliHLTPHOSChannelDataHeaderStruct);
187  
188   //Decoding data
189   fDecoderPtr->SetMemory(reinterpret_cast<UChar_t*>( iter->fPtr ), iter->fSize);
190   fDecoderPtr->Decode();
191       
192   //Looping over data
193   while(fDecoderPtr->NextChannel(fAltroDataPtr) == true )
194     {          
195       if(fAltroDataPtr->GetDataSize() != 0 )
196         {
197           //Want to get the "first in time" "bunch"
198           while(fAltroDataPtr->NextBunch(fAltroBunchPtr) == true) {}
199           
200           //Skip strangely short bunches
201           if(fAltroBunchPtr->GetBunchSize() >  fBunchSizeCut)
202             {
203               totSize += sizeof(AliHLTPHOSChannelDataStruct);
204               if(totSize > size)
205                 {
206                   HLTError("Buffer overflow: Trying to write data of size: %d bytes. Output buffer available: %d bytes.", totSize, size);
207                   return -1;
208                 }
209               
210               nSamples = fAltroBunchPtr->GetBunchSize();
211               
212               //crazyness = fSanityInspectorPtr->CheckInsanity(static_cast<const UInt_t*>(fAltroBunchPtr->GetData()), static_cast<const Int_t>(fAltroBunchPtr->GetBunchSize()));
213               
214               //Evalute signal amplitude and timing
215               fAnalyzerPtr->SetData(fAltroBunchPtr->GetData(), fAltroBunchPtr->GetBunchSize());   
216               fAnalyzerPtr->Evaluate(0, fAltroBunchPtr->GetBunchSize());  
217               
218               //Checking for sane timing...
219               if(fAnalyzerPtr->GetTiming() > fMinPeakPosition && fAnalyzerPtr->GetTiming() < fMaxPeakPosition)
220                 {
221                   // Writing to the output buffer
222                   channelDataPtr->fChannelID = fMapperPtr->GetChannelID(iter->fSpecification, fAltroDataPtr->GetHadd());
223                   //channelDataPtr->fChannelID = fMapperPtr->GetChannelID(1, fAltroDataPtr->GetHadd());
224                   channelDataPtr->fEnergy = static_cast<Float_t>(fAnalyzerPtr->GetEnergy()) - fOffset;
225                   channelDataPtr->fTime = static_cast<Float_t>(fAnalyzerPtr->GetTiming());
226                   channelDataPtr->fCrazyness = static_cast<Short_t>(crazyness);
227                   channelCount++;
228                   channelDataPtr++; // Updating position of the free output.
229                 }
230             }
231         }
232     }
233
234   //Writing the header
235   channelDataHeaderPtr->fNChannels  = channelCount;
236   channelDataHeaderPtr->fAlgorithm  = fAlgorithm;
237   channelDataHeaderPtr->fInfo       = 0;
238   channelDataHeaderPtr->fHasRawData = false;
239
240   HLTDebug("Number of channels: %d", channelCount);
241
242   //returning the size used
243   return sizeof(AliHLTPHOSChannelDataStruct)*channelCount + sizeof(AliHLTPHOSChannelDataHeaderStruct);
244 }
245
246 int
247 AliHLTPHOSRawAnalyzerComponentv2::DoInit( int argc, const char** argv )
248
249
250   //See base class for documentation
251   fPrintInfo = kFALSE;
252   int iResult=0;
253   fMapperPtr = new AliHLTPHOSMapper();
254
255   for(int i = 0; i < argc; i++)
256     {
257       if(!strcmp("-offset", argv[i]))
258         {
259           fOffset = atoi(argv[i+1]);
260         }
261       if(!strcmp("-bunchsizecut", argv[i]))
262         {
263           fBunchSizeCut = atoi(argv[i+1]);
264         }
265       if(!strcmp("-minpeakposition", argv[i]))
266         {
267           fMinPeakPosition = atoi(argv[i+1]);
268         }
269       if(!strcmp("-maxpeakposition", argv[i]))
270         {
271           fMaxPeakPosition = atoi(argv[i+1]);
272         }  
273     }
274  
275   if(fMapperPtr->GetIsInitializedMapping() == false)
276     {
277       Logging(kHLTLogFatal, __FILE__ , IntToChar(  __LINE__ ) , "AliHLTPHOSMapper::Could not initialise mapping from file %s, aborting", fMapperPtr->GetFilePath());
278       return -4;
279     }
280
281   return iResult;
282 }