]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PHOS/AliPHOSRawDigiProducer.cxx
DAs upgraded to AliZDCRawStream class
[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 measured amplitude into real energy.
250   // Calibration parameters are taken from calibration data base for raw data,
251   // or from digitizer parameters for simulated data.                        
252   if(fgCalibData){ 
253     Int_t   module = relId[0];  
254     Int_t   column = relId[3];
255     Int_t   row    = relId[2];
256     if(relId[1]==0) { // this is EMC 
257       if(isLowGain){
258         amp*= fgCalibData->GetHighLowRatioEmc(module,column,row);
259       }
260       amp *= fgCalibData->GetADCchannelEmc(module,column,row);                                                          
261       return amp ;         
262     }         
263   }          
264   return 0;        
265 }
266 //____________________________________________________________________________
267 Double_t AliPHOSRawDigiProducer::CalibrateT(Double_t time, Int_t * relId, Bool_t /* isLowGain */)
268 {
269   //Calibrate time
270   time*=fPulseGenerator->GetRawFormatTimeTrigger() ;
271   if(fgCalibData){
272     Int_t   module = relId[0];
273     Int_t   column = relId[3];
274     Int_t   row    = relId[2];
275     if(relId[1]==0) { // this is EMC
276       time += fgCalibData->GetTimeShiftEmc(module,column,row);                   
277       return time ;             
278     }
279   }
280  
281   return -999.;
282 }
283 //____________________________________________________________________________
284 void AliPHOSRawDigiProducer::CleanDigits(TClonesArray * digits)
285 {
286   // remove digits with amplitudes below threshold.
287   // remove digits in bad channels
288   // sort digits with icreasing AbsId
289   
290   //remove digits in bad map and below threshold
291   Bool_t isBadMap = 0 ;
292   if(fgCalibData->GetNumOfEmcBadChannels()){
293     isBadMap=1 ;
294   }
295   
296   for(Int_t i=0; i<digits->GetEntriesFast(); i++){
297     AliPHOSDigit * digit = static_cast<AliPHOSDigit*>(digits->At(i)) ;
298     if(!digit)
299       continue  ;
300     if ( (IsInEMC(digit) && digit->GetEnergy() < fEmcMinE) ||
301          (IsInCPV(digit) && digit->GetEnergy() < fCpvMinE) ){
302       digits->RemoveAt(i) ;
303       continue ;
304     }
305     if(isBadMap){ //check bad map now
306       Int_t relid[4] ;
307       fGeom->AbsToRelNumbering(digit->GetId(), relid) ; 
308       if(fgCalibData->IsBadChannelEmc(relid[0],relid[3],relid[2])){
309         digits->RemoveAt(i) ;
310       }
311     }
312   }
313
314   //Compress, sort and set indexes
315   digits->Compress() ;
316 //  digits->Sort(); already sorted earlier
317   for (Int_t i = 0 ; i < digits->GetEntriesFast() ; i++) { 
318     AliPHOSDigit *digit = static_cast<AliPHOSDigit*>( digits->At(i) ) ; 
319     digit->SetIndexInList(i) ;     
320   }
321 }
322 //____________________________________________________________________________
323 Bool_t AliPHOSRawDigiProducer::IsInEMC(AliPHOSDigit * digit) const
324 {
325   // Tells if (true) or not (false) the digit is in a PHOS-EMC module
326   return digit->GetId() <= fEmcCrystals ;
327
328 }
329
330 //____________________________________________________________________________
331 Bool_t AliPHOSRawDigiProducer::IsInCPV(AliPHOSDigit * digit) const
332 {
333   // Tells if (true) or not (false) the digit is in a PHOS-CPV module
334   return digit->GetId() > fEmcCrystals ;
335 }
336 //____________________________________________________________________________
337 void AliPHOSRawDigiProducer::GetCalibrationParameters() 
338 {
339   // Set calibration parameters:
340   // if calibration database exists, they are read from database,
341   // otherwise, reconstruction stops in the constructor of AliPHOSCalibData
342   //
343   // It is a user responsilibity to open CDB before reconstruction, for example: 
344   // AliCDBStorage* storage = AliCDBManager::Instance()->GetStorage("local://CalibDB");
345
346   if (!fgCalibData){
347     fgCalibData = new AliPHOSCalibData(-1); //use AliCDBManager's run number
348   }
349   if (fgCalibData->GetCalibDataEmc() == 0)
350     AliFatal("Calibration parameters for PHOS EMC not found. Stop reconstruction.\n");
351   if (fgCalibData->GetCalibDataCpv() == 0)
352     AliFatal("Calibration parameters for PHOS CPV not found. Stop reconstruction.\n");
353 }