]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PHOS/AliPHOSRawDigiProducer.cxx
2e214ba3bcae691d4d6c9248d2648951d6484072
[u/mrichter/AliRoot.git] / PHOS / AliPHOSRawDigiProducer.cxx
1 /**************************************************************************
2  * Copyright(c) 2007, ALICE Experiment at CERN, All rights reserved.      *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /* $Id$ */
17
18 //This class produces PHOS digits of one event
19 //using AliPHOSRawDecoder. 
20 //
21 //   For example:
22 //   TClonesArray *digits = new TClonesArray("AliPHOSDigit",100);
23 //   AliRawReader* rawReader = new AliRawReaderDate("2006run2211.raw");
24 //   AliPHOSRawDecoder dc(rawReader);
25 //   while (rawReader->NextEvent()) {
26 //     AliPHOSRawDigiProducer producer;
27 //     producer.MakeDigits(digits,&dc);
28 //   }
29
30 // Author: Boris Polichtchouk
31
32 // --- ROOT system ---
33 #include "TClonesArray.h"
34
35 // --- AliRoot header files ---
36 #include "AliPHOSRawDigiProducer.h"
37 #include "AliPHOSRawDecoder.h"
38 #include "AliPHOSGeometry.h"
39 #include "AliPHOSDigit.h"
40 #include "AliPHOSRecoParam.h"
41 #include "AliPHOSCalibData.h"
42 #include "AliPHOSPulseGenerator.h"
43 #include "AliLog.h"
44
45 ClassImp(AliPHOSRawDigiProducer)
46
47 AliPHOSCalibData * AliPHOSRawDigiProducer::fgCalibData  = 0 ; 
48
49 //--------------------------------------------------------------------------------------
50 AliPHOSRawDigiProducer::AliPHOSRawDigiProducer():
51   TObject(),
52   fEmcMinE(0.),
53   fCpvMinE(0.),
54   fSampleQualityCut(1.),
55   fGlobalAltroOffset(0),
56   fEmcCrystals(0),
57   fGeom(0),
58   fPulseGenerator(0)
59 {
60   // Default constructor
61 }
62 //--------------------------------------------------------------------------------------
63 AliPHOSRawDigiProducer::AliPHOSRawDigiProducer(const AliPHOSRecoParam* recoParam):
64   TObject(),
65   fEmcMinE(0.),
66   fCpvMinE(0.),
67   fSampleQualityCut(1.),
68   fGlobalAltroOffset(0),
69   fEmcCrystals(0),
70   fGeom(0),
71   fPulseGenerator(0)
72 {
73   // Constructor takes paramerters from recoParam
74
75   if(!recoParam) AliFatal("Reconstruction parameters are not set!");
76
77   fEmcMinE = recoParam->GetEMCMinE();
78   fCpvMinE = recoParam->GetCPVMinE();
79   fSampleQualityCut = recoParam->GetEMCSampleQualityCut() ;
80   fGlobalAltroOffset = recoParam->GetGlobalAltroOffset() ;
81
82   fGeom=AliPHOSGeometry::GetInstance() ;
83   if(!fGeom) fGeom = AliPHOSGeometry::GetInstance("IHEP");
84
85   fEmcCrystals=fGeom->GetNCristalsInModule()*fGeom->GetNModules() ;
86
87   fPulseGenerator = new AliPHOSPulseGenerator();
88
89   GetCalibrationParameters() ; 
90 }
91 //--------------------------------------------------------------------------------------                       
92 AliPHOSRawDigiProducer::AliPHOSRawDigiProducer(const AliPHOSRawDigiProducer &dp):
93   TObject(),
94   fEmcMinE(0.),
95   fCpvMinE(0.),
96   fSampleQualityCut(1.),
97   fGlobalAltroOffset(0),
98   fEmcCrystals(0),
99   fGeom(0),
100   fPulseGenerator(0)
101 {                                                          
102   // Copy constructor
103
104   fEmcMinE = dp.fEmcMinE ;
105   fCpvMinE = dp.fCpvMinE ;
106   fEmcCrystals = dp.fEmcCrystals ;
107   fPulseGenerator = new AliPHOSPulseGenerator();
108   fGeom = dp.fGeom ;
109 }
110 //--------------------------------------------------------------------------------------
111 AliPHOSRawDigiProducer& AliPHOSRawDigiProducer::operator= (const AliPHOSRawDigiProducer &dp)
112 {
113   // Assign operator
114
115   if(&dp == this) return *this;
116
117   fEmcMinE = dp.fEmcMinE ;
118   fCpvMinE = dp.fCpvMinE ;
119   fSampleQualityCut = dp.fSampleQualityCut ;
120   fGlobalAltroOffset = dp.fGlobalAltroOffset ;
121   fEmcCrystals = dp.fEmcCrystals ;
122   fGeom = dp.fGeom ;
123   if(fPulseGenerator) delete fPulseGenerator ;
124   fPulseGenerator = new AliPHOSPulseGenerator();
125   return  *this;
126
127 //--------------------------------------------------------------------------------------                                                   
128 AliPHOSRawDigiProducer::~AliPHOSRawDigiProducer()
129 {
130   // Desctructor
131   if(fPulseGenerator) delete fPulseGenerator ;
132   fPulseGenerator=0 ;
133 }
134 //--------------------------------------------------------------------------------------
135 void AliPHOSRawDigiProducer::MakeDigits(TClonesArray *digits, AliPHOSRawDecoder* decoder) 
136 {
137   //Makes the job.
138   //TClonesArray *digits and raw data decoder should be provided by calling function.
139
140   digits->Clear();
141  
142   Int_t    iDigit   = 0 ;
143   Int_t relId[4], absId =0;
144
145   const Double_t baseLine=1. ; //Minimal energy of digit in ADC ch. 
146   const Double_t highLowDiff=2.; //Maximal difference between High and Low channels in LG adc channels 
147
148   //Temporary array for LowGain digits
149   TClonesArray tmpLG("AliPHOSDigit",10000) ;
150   Int_t ilgDigit=0 ;
151
152
153   //Read current altro offcet from RCU
154   decoder->SetAmpOffset(fGlobalAltroOffset) ;
155
156   //Let decoder subtract pedestals in case of ZS
157   decoder->SetCalibData(fgCalibData) ;
158   
159   while (decoder->NextDigit()) {
160
161     Double_t energy=decoder->GetEnergy() ; 
162     if(energy<=baseLine) //in ADC channels
163       continue ;
164
165     //remove digits with bad shape. Decoder should calculate quality so that 
166     //in default case quality [0,1], while larger values of quality mean somehow 
167     //corrupted samples, 999 means obviously corrupted sample.
168     //It is difficult to fit samples with overflow (even setting cut on overflow values)
169     //because too few points are left to fit. So we do not evaluate samples with overflow
170     if(decoder->GetSampleQuality() > fSampleQualityCut && !(decoder->IsOverflow()))
171        continue ;
172
173     Bool_t lowGainFlag = decoder->IsLowGain();
174
175     relId[0] = decoder->GetModule();
176     relId[1] = 0;
177     relId[2] = decoder->GetRow();
178     relId[3] = decoder->GetColumn();
179     fGeom->RelToAbsNumbering(relId, absId);
180
181     Double_t time = decoder->GetTime() ;
182     time = CalibrateT(time,relId,lowGainFlag) ;
183
184     energy = CalibrateE(energy,relId,lowGainFlag) ;
185
186     if(energy <= 0.) 
187        continue;
188
189     if(lowGainFlag){
190       new(tmpLG[ilgDigit]) AliPHOSDigit(-1,absId,(Float_t)energy,(Float_t)time);
191       ilgDigit++ ; 
192     }
193     else{ 
194       if(decoder->IsOverflow()) //Keep this digit to replace it by Low Gain later.
195                                 //If there is no LogGain it wil be removed by cut on Min E
196         new((*digits)[iDigit]) AliPHOSDigit(-1,absId,-1.f,(Float_t)time);
197       else
198         new((*digits)[iDigit]) AliPHOSDigit(-1,absId,(Float_t)energy,(Float_t)time);
199       iDigit++;
200     }
201   }
202
203   //Now scan created LG and HG digits and keep only those which appeared in both lists 
204   //replace energy of HighGain digits only if there is overflow
205   //negative energy (overflow)
206   digits->Sort() ;
207   tmpLG.Sort() ;
208   Int_t iLG = 0;
209   Int_t nLG1 = tmpLG.GetEntriesFast()-1 ;
210
211   for(Int_t iDig=0 ; iDig < digits->GetEntriesFast() ; iDig++) { 
212     AliPHOSDigit * digHG = dynamic_cast<AliPHOSDigit*>(digits->At(iDig)) ;
213     if (!digHG) continue;
214     AliPHOSDigit * digLG = dynamic_cast<AliPHOSDigit*>(tmpLG.At(iLG)) ;
215     while(digLG && iLG<nLG1 && digHG->GetId()> digLG->GetId()){
216       iLG++ ;
217       digLG = dynamic_cast<AliPHOSDigit*>(tmpLG.At(iLG)) ;
218     }
219     absId=digHG->GetId() ;                                                                                                         
220     fGeom->AbsToRelNumbering(absId,relId) ;                                                                                              
221  
222     if(digLG && digHG->GetId() == digLG->GetId()){ //we found pair
223       if(digHG->GetEnergy()<0.){ //This is overflow in HG
224         digHG->SetTime(digLG->GetTime()) ;
225         digHG->SetEnergy(digLG->GetEnergy()) ;
226       } 
227       else{ //Make approximate comparison of HG and LG energies
228         Double_t de = (digHG->GetEnergy()-digLG->GetEnergy()) ; 
229         if(TMath::Abs(de)>CalibrateE(double(highLowDiff),relId,1)){ //too strong difference, remove digit
230           digits->RemoveAt(iDig) ;
231         }
232       }
233     }
234     else{ //no pair - remove
235       // temporary fix for dead LG channels
236       if(relId[2]%2==1 && relId[3]%16==4) 
237         continue ;
238       if(digHG->GetEnergy()>CalibrateE(double(5.),relId,1)) //One can not always find LG with Amp<5 ADC ch.
239         digits->RemoveAt(iDig) ;                                                                                                            
240     }
241   }
242
243   CleanDigits(digits) ;
244
245 }
246 //____________________________________________________________________________
247 Double_t AliPHOSRawDigiProducer::CalibrateE(Double_t amp, Int_t* relId, Bool_t isLowGain)
248 {
249   // Convert EMC LG amplitude to HG (multipli by ~16)
250   // Calibration parameters are taken from calibration data base 
251   if(fgCalibData){ 
252     Int_t   module = relId[0];  
253     Int_t   column = relId[3];
254     Int_t   row    = relId[2];
255     if(relId[1]==0) { // this is EMC 
256       if(isLowGain){
257         amp*= fgCalibData->GetHighLowRatioEmc(module,column,row);
258       }
259       return amp ;         
260     }         
261   }          
262   return 0;        
263 }
264 //____________________________________________________________________________
265 Double_t AliPHOSRawDigiProducer::CalibrateT(Double_t time, Int_t * relId, Bool_t /* isLowGain */)
266 {
267   //Calibrate time
268   time*=fPulseGenerator->GetRawFormatTimeTrigger() ;
269   if(fgCalibData){
270     Int_t   module = relId[0];
271     Int_t   column = relId[3];
272     Int_t   row    = relId[2];
273     if(relId[1]==0) { // this is EMC
274       time += fgCalibData->GetTimeShiftEmc(module,column,row);                   
275       return time ;             
276     }
277   }
278  
279   return -999.;
280 }
281 //____________________________________________________________________________
282 void AliPHOSRawDigiProducer::CleanDigits(TClonesArray * digits)
283 {
284   // remove digits with amplitudes below threshold.
285   // remove digits in bad channels
286   // sort digits with icreasing AbsId
287   
288   //remove digits in bad map and below threshold
289   Bool_t isBadMap = 0 ;
290   if(fgCalibData->GetNumOfEmcBadChannels()){
291     isBadMap=1 ;
292   }
293   
294   for(Int_t i=0; i<digits->GetEntriesFast(); i++){
295     AliPHOSDigit * digit = static_cast<AliPHOSDigit*>(digits->At(i)) ;
296     if(!digit)
297       continue  ;
298     if ( (IsInEMC(digit) && digit->GetEnergy() < fEmcMinE) ||
299          (IsInCPV(digit) && digit->GetEnergy() < fCpvMinE) ){
300       digits->RemoveAt(i) ;
301       continue ;
302     }
303     if(isBadMap){ //check bad map now
304       Int_t relid[4] ;
305       fGeom->AbsToRelNumbering(digit->GetId(), relid) ; 
306       if(fgCalibData->IsBadChannelEmc(relid[0],relid[3],relid[2])){
307         digits->RemoveAt(i) ;
308       }
309     }
310   }
311
312   //Compress, sort and set indexes
313   digits->Compress() ;
314 //  digits->Sort(); already sorted earlier
315   for (Int_t i = 0 ; i < digits->GetEntriesFast() ; i++) { 
316     AliPHOSDigit *digit = static_cast<AliPHOSDigit*>( digits->At(i) ) ; 
317     digit->SetIndexInList(i) ;     
318   }
319 }
320 //____________________________________________________________________________
321 Bool_t AliPHOSRawDigiProducer::IsInEMC(AliPHOSDigit * digit) const
322 {
323   // Tells if (true) or not (false) the digit is in a PHOS-EMC module
324   return digit->GetId() <= fEmcCrystals ;
325
326 }
327
328 //____________________________________________________________________________
329 Bool_t AliPHOSRawDigiProducer::IsInCPV(AliPHOSDigit * digit) const
330 {
331   // Tells if (true) or not (false) the digit is in a PHOS-CPV module
332   return digit->GetId() > fEmcCrystals ;
333 }
334 //____________________________________________________________________________
335 void AliPHOSRawDigiProducer::GetCalibrationParameters() 
336 {
337   // Set calibration parameters:
338   // if calibration database exists, they are read from database,
339   // otherwise, reconstruction stops in the constructor of AliPHOSCalibData
340   //
341   // It is a user responsilibity to open CDB before reconstruction, for example: 
342   // AliCDBStorage* storage = AliCDBManager::Instance()->GetStorage("local://CalibDB");
343
344   if (!fgCalibData){
345     fgCalibData = new AliPHOSCalibData(-1); //use AliCDBManager's run number
346   }
347   if (fgCalibData->GetCalibDataEmc() == 0)
348     AliFatal("Calibration parameters for PHOS EMC not found. Stop reconstruction.\n");
349   if (fgCalibData->GetCalibDataCpv() == 0)
350     AliFatal("Calibration parameters for PHOS CPV not found. Stop reconstruction.\n");
351 }