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