]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/PHOS/AliHLTPHOSRawAnalyzerComponent.cxx
eff C++
[u/mrichter/AliRoot.git] / HLT / PHOS / AliHLTPHOSRawAnalyzerComponent.cxx
1 /**************************************************************************
2  * This file is property of and copyright by the Experimental Nuclear     *
3  * Physics Group, Dep. of Physics                                         *
4  * University of Oslo, Norway, 2007                                       *
5  *                                                                        * 
6  * Author: Per Thomas Hille <perthi@fys.uio.no> for the ALICE HLT Project.*
7  * Contributors are mentioned in the code where appropriate.              *
8  * Please report bugs to perthi@fys.uio.no                                * 
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 #include "AliHLTPHOSRawAnalyzer.h"
20 #include "AliHLTPHOSRawAnalyzerComponent.h"
21 #include "AliHLTPHOSRcuCellEnergyDataStruct.h"
22 #include "AliHLTPHOSMapper.h"
23 #include "AliHLTPHOSSanityInspector.h"
24 #include "AliHLTPHOSBaseline.h"
25 #include "TFile.h"
26 #include "TTree.h"
27 #include "TClonesArray.h"
28 #include "TH2F.h"
29 #include "AliAltroDecoder.h"    // decoder for altro payload
30 #include "AliAltroData.h"       // container for altro payload
31 #include "AliAltroBunch.h"      // container for altro bunches
32
33
34 AliHLTPHOSRawAnalyzerComponent::AliHLTPHOSRawAnalyzerComponent():AliHLTPHOSRcuProcessor(), 
35                                                                  fAnalyzerPtr(0), 
36                                                                  fSendChannelData(kFALSE),
37                                                                  fOutPtr(0), 
38                                                                  fMapperPtr(0), 
39                                                                  fSanityInspectorPtr(0),
40                                                                  fUseBaselineSubtraction(false), 
41                                                                  fDecoderPtr(0),  
42                                                                  fAltroDataPtr(0),
43                                                                  fAltroBunchPtr(0), 
44                                                         
45                                                                  fDoPushRawData(false),
46                                                                  fDoMakeDigits(0), 
47                                                                  fDigitMakerPtr(0),
48                                                                  
49                                                                  fDigitContainerPtr(0),
50                                                                  fDoSelectiveReadOut(false),
51                                                                  fSelectedChannelsList(0),
52                                                                  fDoCheckDataSize(false),
53                                                                  fNCorruptedBlocks(0),
54                                                                  fNOKBlocks(0)
55                                                                  //fRawMemoryReader(0), fPHOSRawStream(0) 
56 {
57   //comment
58   fMapperPtr = new AliHLTPHOSMapper();
59   fAltroDataPtr = new AliAltroData();
60   fAltroBunchPtr = new AliAltroBunch();
61   fDecoderPtr = new AliAltroDecoder();
62   fSanityInspectorPtr = new AliHLTPHOSSanityInspector();
63   fSelectedChannelsList = new AliHLTUInt16_t[N_XCOLUMNS_RCU*N_ZROWS_RCU*N_GAINS];
64 }
65
66
67 AliHLTPHOSRawAnalyzerComponent::~AliHLTPHOSRawAnalyzerComponent()
68 {
69   Deinit();
70 }
71
72
73
74 int 
75 AliHLTPHOSRawAnalyzerComponent::Deinit()
76 {
77   //comment
78   if(fMapperPtr)
79     {
80       delete  fMapperPtr;
81       fMapperPtr = 0;
82     }
83   if(fAltroDataPtr)
84     {
85       delete fAltroDataPtr;
86       fAltroDataPtr = 0;
87     }
88   if(fAltroBunchPtr)
89     {
90       delete fAltroBunchPtr;
91       fAltroBunchPtr = 0;
92     }
93   if(fDecoderPtr)
94     {
95       delete fDecoderPtr;
96       fDecoderPtr = 0;
97     }
98   Logging(kHLTLogInfo, "HLT", "PHOS", ",AliHLTPHOSRawAnalyzerComponen Deinit");
99   return 0;
100 }
101
102
103 const char* 
104 AliHLTPHOSRawAnalyzerComponent::GetComponentID()
105 {
106   //comment
107   return "AliPhosTestRaw";
108 }
109
110
111 void
112 AliHLTPHOSRawAnalyzerComponent::GetInputDataTypes( vector<AliHLTComponentDataType>& list)
113 {
114   //comment
115   list.clear();
116   list.push_back( AliHLTPHOSDefinitions::fgkDDLPackedRawDataType | kAliHLTDataOriginPHOS);
117 }
118
119 AliHLTComponentDataType 
120 AliHLTPHOSRawAnalyzerComponent::GetOutputDataType()
121 {
122   //comment
123   return kAliHLTMultipleDataType;
124   //  return AliHLTPHOSDefinitions::fgkDigitDataType;
125 }
126
127 int 
128 AliHLTPHOSRawAnalyzerComponent::GetOutputDataTypes(AliHLTComponentDataTypeList& tgtList)
129 {
130   // Added by OD
131   // see header file for class documentation
132   tgtList.clear();
133   tgtList.push_back(AliHLTPHOSDefinitions::fgkCellEnergyDataType);
134    tgtList.push_back(kAliHLTDataTypeHwAddr16);
135   return tgtList.size();
136 }
137
138 void
139 AliHLTPHOSRawAnalyzerComponent::GetOutputDataSize(unsigned long& constBase, double& inputMultiplier )
140 {
141   //comment
142   constBase = 30;
143   inputMultiplier = 4;
144 }
145
146
147
148
149 void 
150 AliHLTPHOSRawAnalyzerComponent::FillDataArray(UInt_t *data, const AliAltroData * /*altrodataptr */, const int /* channel */)
151 {
152   ResetDataPtr(0, ALTRO_MAX_SAMPLES);
153   bool islastbunch = true;
154
155   while( fAltroDataPtr->NextBunch(fAltroBunchPtr) == true)
156     {
157       const UInt_t *tmpdata  = fAltroBunchPtr->GetData();
158    
159       if(islastbunch == true)
160         {
161           data[0] = fAltroBunchPtr->GetEndTimeBin();
162           islastbunch = false;
163         }
164
165       int tmpstartbin =  fAltroBunchPtr->GetStartTimeBin();
166       int tmpendbin =  fAltroBunchPtr->GetEndTimeBin();
167       int tmplength = tmpendbin -  tmpstartbin;
168
169       for(int i = 0; i < tmplength ; i++)
170         { 
171           data[i+tmpstartbin] = tmpdata[i];
172         }
173     }
174
175
176   /*
177   cout <<__FILE__ <<" : " <<__LINE__  << "the resulting array is"<<endl;
178   
179   for(int i=0; i<  data[0]; i++)
180     {
181       if(i != 0 && i %16 == 0)
182         {
183           cout << endl;
184         }
185       cout <<data[i] << "\t" ;
186     }
187   cout << endl;
188   */
189 }
190
191
192 void 
193 AliHLTPHOSRawAnalyzerComponent::GetFirstBunch(AliAltroData * /*altrodata */,  AliAltroBunch  * /*altrobunch */)
194 {
195   while( fAltroDataPtr->NextBunch(fAltroBunchPtr) == true)
196     {
197       
198     }
199 }
200
201
202 int 
203 AliHLTPHOSRawAnalyzerComponent::DoEvent( const AliHLTComponentEventData& evtData, const AliHLTComponentBlockData* blocks, AliHLTComponentTriggerData& /*trigData*/, 
204                                          AliHLTUInt8_t* outputPtr, AliHLTUInt32_t& size, vector<AliHLTComponentBlockData>& outputBlocks )
205 {
206   //  cout << "Event" << fPhosEventCount  << endl;
207
208   UInt_t offset            = 0; 
209   UInt_t mysize            = 0;
210   UInt_t tSize             = 0;
211   Float_t baseline         = 0;
212   AliHLTUInt8_t* outBPtr;
213   outBPtr = outputPtr;
214   const AliHLTComponentBlockData* iter = NULL; 
215   unsigned long ndx;
216   Int_t *rawDataBufferPos = (Int_t *)outputPtr; 
217   Int_t nSamples = 0;
218   UInt_t specification = 0;
219   bool droppedRaw = true;
220   if(fDoPushRawData) {droppedRaw = false;}
221   
222   for( ndx = 0; ndx < evtData.fBlockCnt; ndx++ )
223     {
224       iter = blocks+ndx;
225       if ( iter->fDataType != AliHLTPHOSDefinitions::fgkDDLPackedRawDataType )
226         {
227           continue; 
228         }
229
230       Int_t tmpChannelCnt     = 0;
231       mysize = 0;
232       offset = tSize;
233       Int_t crazyness = 0;
234       mysize += sizeof(AliHLTPHOSRcuCellEnergyDataStruct);
235       tSize += mysize;
236
237       if(tSize > size)
238         {
239           HLTError("Buffer overflow: Trying to write data of size: %d bytes. Output buffer available: %d bytes.", tSize, size);
240           return -ENOBUFS;
241         }  
242
243       specification = specification|iter->fSpecification;
244       fDecoderPtr->SetMemory(reinterpret_cast<UChar_t*>( iter->fPtr ), iter->fSize);
245       fDecoderPtr->Decode();
246       fOutPtr =  (AliHLTPHOSRcuCellEnergyDataStruct*)outBPtr;
247       fOutPtr->fRcuX = fRcuX;
248       fOutPtr->fRcuZ = fRcuZ;
249       fOutPtr->fModuleID =fModuleID;
250       
251       rawDataBufferPos += (tSize)/sizeof(Int_t); 
252            
253       while( fDecoderPtr->NextChannel(fAltroDataPtr) == true )
254         {          
255           FillDataArray(fTmpChannelData, fAltroDataPtr, tmpChannelCnt); 
256
257           if(  fAltroDataPtr->GetDataSize() != 0 )
258             {
259               GetFirstBunch(fAltroDataPtr, fAltroBunchPtr);
260               nSamples = fAltroBunchPtr->GetBunchSize();
261               //              cout <<__FILE__ <<" : " <<__LINE__  << ",  the size of the first bunch is " << nSamples <<endl;
262               crazyness = fSanityInspectorPtr->CheckInsanity((const UInt_t*)fAltroBunchPtr->GetData(), (const Int_t)(fAltroBunchPtr->GetBunchSize()));
263               fAnalyzerPtr->SetData(fAltroBunchPtr->GetData(), fAltroBunchPtr->GetBunchSize());   
264               fAnalyzerPtr->Evaluate(0, fAltroBunchPtr->GetBunchSize());  
265               fOutPtr->fValidData[tmpChannelCnt].fZ  = fMapperPtr->fHw2geomapPtr[fAltroDataPtr->GetHadd()].fZRow;
266               fOutPtr->fValidData[tmpChannelCnt].fX  = fMapperPtr->fHw2geomapPtr[fAltroDataPtr->GetHadd()].fXCol; 
267               fOutPtr->fValidData[tmpChannelCnt].fGain  = fMapperPtr->fHw2geomapPtr[fAltroDataPtr->GetHadd()].fGain; 
268                       
269               if(fUseBaselineSubtraction)
270                 {
271                   baseline = fBaselines[fOutPtr->fValidData[tmpChannelCnt].fX][fOutPtr->fValidData[tmpChannelCnt].fZ][ fOutPtr->fValidData[tmpChannelCnt].fGain];
272                 }
273                       
274               fOutPtr->fValidData[tmpChannelCnt].fEnergy  = (float)fAnalyzerPtr->GetEnergy() - baseline;
275               fOutPtr->fValidData[tmpChannelCnt].fTime    = (float)fAnalyzerPtr->GetTiming();
276               fOutPtr->fValidData[tmpChannelCnt].fCrazyness = (int)crazyness;
277
278               if(fDoPushRawData == true && droppedRaw == false)
279                 {
280                   int tmpsize = fTmpChannelData[0];
281                   //      cout << __FILE__ << ":" << __LINE__ << "channel = " << tmpChannelCnt <<  " size  ="<< tmpsize << endl;
282                   mysize += (tmpsize + 1)*sizeof(Int_t);
283                   tSize += (tmpsize  + 1)*sizeof(Int_t);;
284
285                   if(tSize > size)
286                     {
287                       HLTError("Buffer overflow: Trying to write data of size: %d bytes. Output buffer available: %d bytes. Dropping raw data.", tSize, size);
288                       droppedRaw = true;
289                       tSize -= mysize;
290                     }
291                   else
292                     {
293                       *rawDataBufferPos = tmpsize;
294                       
295                       for(int sample = 0; sample < tmpsize; sample++)
296                         {
297                           rawDataBufferPos++;
298                           *(rawDataBufferPos) = fTmpChannelData[sample]; 
299                         }
300                       rawDataBufferPos++;
301                               
302                     }
303                 }
304               tmpChannelCnt ++;
305             }
306         }
307
308       if(fDoPushRawData && droppedRaw == false)
309         {
310           fOutPtr->fHasRawData = true;
311         }
312       else
313         {
314           fOutPtr->fHasRawData = false;
315         }
316
317       fOutPtr->fCnt  = tmpChannelCnt;
318       fOutPtr->fSize = mysize;
319       AliHLTComponentBlockData bdCellEnergy;
320       FillBlockData( bdCellEnergy );
321       bdCellEnergy.fOffset = offset;
322       bdCellEnergy.fSize = mysize;
323       bdCellEnergy.fDataType = AliHLTPHOSDefinitions::fgkCellEnergyDataType;
324       bdCellEnergy.fSpecification = specification;
325       outputBlocks.push_back( bdCellEnergy );
326       outBPtr += mysize;
327
328       if( tSize > size )
329         {
330           Logging( kHLTLogFatal, "HLT::AliHLTPHOSRawAnalyzerComponent::DoEvent", "Too much data",
331                    "Data written over allowed buffer. Amount written: %lu, allowed amount: %lu."
332                    , tSize, size );
333           return EMSGSIZE;
334         }
335       
336     }
337   
338   //  *rawDataBufferPos = 0;  
339   fPhosEventCount++; 
340   return 0;
341 }//end DoEvent
342
343
344
345 int
346 AliHLTPHOSRawAnalyzerComponent::DoInit( int argc, const char** argv )
347
348   //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
349   // fAnalyzerPtr->SetCorrectBaselineUsingFirstFiveSamples();
350   //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
351
352
353   //See base class for documentation
354   fSendChannelData = kFALSE;
355   fPrintInfo = kFALSE;
356   int iResult=0;
357   TString argument="";
358   const int nSigmas = 3;
359   fMapperPtr = new AliHLTPHOSMapper();
360  
361   if(fMapperPtr->GetIsInitializedMapping() == false)
362     {
363       Logging(kHLTLogFatal, __FILE__ , IntToChar(  __LINE__ ) , "AliHLTPHOSMapper::Could not initial mapping from file %s, aborting", fMapperPtr->GetFilePath());
364       return -4;
365     }
366
367   char tmpbaselinfile[256];
368   if(fUtilitiesPtr->ScanSingleNameArgument(argc, argv, "-baselinefile", tmpbaselinfile) == true  )
369     {
370       SetBaselines(tmpbaselinfile);
371     }
372   
373   char tmpSelectiveThresholdfile[256];
374   if(fUtilitiesPtr->ScanSingleNameArgument(argc, argv, "-baselinefile", tmpSelectiveThresholdfile) == true  )
375     {
376       fDoSelectiveReadOut = true;
377       SetSelectiveReadOutThresholds(tmpSelectiveThresholdfile, nSigmas);
378     }
379
380   //fDoSelectiveReadOut = fUtilitiesPtr->ScanSingleNameArgument(argc, argv, "-selectivereadout");
381
382   fDoPushRawData = fUtilitiesPtr->ScanSingleArgument(argc, argv, "-pushrawdata");
383
384   fDoCheckDataSize = fUtilitiesPtr->ScanSingleArgument(argc, argv, "-checkdatasize");
385  
386   iResult = ScanArguments(argc, argv);
387   return iResult;
388 }
389
390
391 void
392 AliHLTPHOSRawAnalyzerComponent::Reset(AliHLTPHOSRcuCellEnergyDataStruct* /* cellDataPtr */)
393 {
394   ResetDataPtr(0, ALTRO_MAX_SAMPLES);
395 } // end Reset
396
397
398 void
399 AliHLTPHOSRawAnalyzerComponent::ResetDataPtr(int startindex, int sampleCnt)
400 {
401   //comment
402   for(int i = startindex ; i< sampleCnt; i++)
403     {
404       fTmpChannelData[i] = 0;
405     }
406 }
407
408
409 void 
410 AliHLTPHOSRawAnalyzerComponent::SetBaselines(const char* file)
411 {
412   //comment
413   fUseBaselineSubtraction = true;
414   AliHLTPHOSBaseline *baseline = 0;
415   TFile *baselineFile = TFile::Open(file);
416   TTree *baselineTree = (TTree*)baselineFile->Get("baselineTree");
417   TClonesArray *baselineArray = new TClonesArray("AliHLTPHOSBaseline", 7168);
418   baselineTree->SetBranchAddress("Baselines", &baselineArray);
419   baselineTree->GetEntry(0);
420   for(Int_t i = 0; i < baselineArray->GetEntriesFast(); i++)
421     {
422       baseline = (AliHLTPHOSBaseline*)baselineArray->At(i);
423       if((baseline->GetX() < (Int_t)((fRcuX + 1)*N_XCOLUMNS_RCU)) && (baseline->GetX() >= (Int_t)(fRcuX*N_XCOLUMNS_RCU)))
424         {
425           if((baseline->GetZ() < (Int_t)((fRcuZ + 1)*N_ZROWS_RCU)) && (baseline->GetZ() >= (Int_t)(fRcuZ*N_ZROWS_RCU)))
426             {
427               fBaselines[baseline->GetX() - fRcuX*N_XCOLUMNS_RCU][baseline->GetZ() - fRcuZ*N_ZROWS_RCU][baseline->GetGain()] = baseline->GetBaseline();
428             }
429         }
430     }
431   baselineFile->Close();
432   delete baselineFile;
433   baselineFile = 0;
434 }
435
436 void 
437 AliHLTPHOSRawAnalyzerComponent::SetSelectiveReadOutThresholds(const char* filepath, Int_t nSigmas)
438 {
439   //See header file for documentation
440   TFile *histFile = new TFile(filepath);
441   TH2F *lgHist = (TH2F*)histFile->Get("RMSLGMapHist");
442   TH2F *hgHist = (TH2F*)histFile->Get("RMSHGMapHist");
443
444   for(int x = 0; x < N_XCOLUMNS_MOD; x++)
445     {
446       for(int z = 0; z < N_ZROWS_MOD; z++)
447         {
448           fSelectiveReadOutThresholds[x][z][LOW_GAIN] = lgHist->GetBinContent(x, z) * nSigmas;
449           fSelectiveReadOutThresholds[x][z][HIGH_GAIN] = hgHist->GetBinContent(x, z) * nSigmas;
450         }
451     }
452 }