]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PHOS/AliPHOSRawDigiProducer.cxx
All time calibration moved to AliPHOSRawDigiProducer
[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():TObject(),
51   fEmcMinE(0.),fCpvMinE(0.),fSampleQualityCut(1.),
52   fEmcCrystals(0),fGeom(0),fPulseGenerator(0){
53
54 }
55 //--------------------------------------------------------------------------------------
56 AliPHOSRawDigiProducer::AliPHOSRawDigiProducer(const AliPHOSRecoParam* parEmc, const AliPHOSRecoParam* parCpv):TObject(),
57   fEmcMinE(0.),fCpvMinE(0.),fSampleQualityCut(1.),fEmcCrystals(0),fGeom(0),fPulseGenerator(0){
58
59   if(!parEmc) AliFatal("Reconstruction parameters for EMC not set!");
60   if(!parCpv) AliFatal("Reconstruction parameters for CPV not set!");
61
62   fEmcMinE = parEmc->GetMinE();
63   fCpvMinE = parCpv->GetMinE();
64
65   fSampleQualityCut = parEmc->GetSampleQualityCut() ;
66
67   fGeom=AliPHOSGeometry::GetInstance() ;
68   if(!fGeom) fGeom = AliPHOSGeometry::GetInstance("IHEP");
69
70   fEmcCrystals=fGeom->GetNCristalsInModule()*fGeom->GetNModules() ;
71
72   fPulseGenerator = new AliPHOSPulseGenerator();
73
74   GetCalibrationParameters() ; 
75 }
76 //--------------------------------------------------------------------------------------                       
77 AliPHOSRawDigiProducer::AliPHOSRawDigiProducer(const AliPHOSRawDigiProducer &dp):TObject(),
78   fEmcMinE(0.),fCpvMinE(0.),fSampleQualityCut(1.),fEmcCrystals(0),fGeom(0),fPulseGenerator(0){                                                          
79
80   fEmcMinE = dp.fEmcMinE ;
81   fCpvMinE = dp.fCpvMinE ;
82   fEmcCrystals = dp.fEmcCrystals ;
83   fPulseGenerator = new AliPHOSPulseGenerator();
84   fGeom = dp.fGeom ;
85 }
86 //--------------------------------------------------------------------------------------
87 AliPHOSRawDigiProducer& AliPHOSRawDigiProducer::operator= (const AliPHOSRawDigiProducer &dp){
88   if(&dp == this) return *this;
89
90   fEmcMinE = dp.fEmcMinE ;
91   fCpvMinE = dp.fCpvMinE ;
92   fSampleQualityCut = dp.fSampleQualityCut ;
93   fEmcCrystals = dp.fEmcCrystals ;
94   fGeom = dp.fGeom ;
95   if(fPulseGenerator) delete fPulseGenerator ;
96   fPulseGenerator = new AliPHOSPulseGenerator();
97   return  *this;
98
99 //--------------------------------------------------------------------------------------                                                   
100 AliPHOSRawDigiProducer::~AliPHOSRawDigiProducer(){
101  if(fPulseGenerator) delete fPulseGenerator ;
102  fPulseGenerator=0 ;
103 }
104 //--------------------------------------------------------------------------------------
105 void AliPHOSRawDigiProducer::MakeDigits(TClonesArray *digits, AliPHOSRawDecoder* decoder) 
106 {
107   //Makes the job.
108   //TClonesArray *digits and raw data decoder should be provided by calling function.
109
110   digits->Clear();
111  
112   Int_t    iDigit   = 0 ;
113   Int_t relId[4], absId =0;
114
115   const Double_t baseLine=1. ; //Minimal energy of digit in ADC ch. 
116   const Double_t highLowDiff=2. ; //Maximal difference between High and Low channels in LG adc channels 
117
118   //Temporary array for LowGain digits
119   TClonesArray tmpLG("AliPHOSDigit",10000) ;
120   Int_t ilgDigit=0 ;
121   
122   while (decoder->NextDigit()) {
123
124     Double_t energy=decoder->GetEnergy() ; 
125     if(energy<=baseLine) //in ADC channels
126       continue ;
127
128     //remove digits with bas shape. Decoder should calculate quality so that 
129     //in default case quality [0,1], while larger values of quality mean somehow 
130     //corrupted samples, 999 means obviously corrupted sample.
131     if(decoder->GetSampleQuality() > fSampleQualityCut )
132        continue ;
133
134     Bool_t lowGainFlag = decoder->IsLowGain();
135
136     relId[0] = decoder->GetModule();
137     relId[1] = 0;
138     relId[2] = decoder->GetRow();
139     relId[3] = decoder->GetColumn();
140     fGeom->RelToAbsNumbering(relId, absId);
141
142     Double_t time = decoder->GetTime() ;
143     time = CalibrateT(time,relId,lowGainFlag) ;
144
145     energy = CalibrateE(energy,relId,lowGainFlag) ;
146
147     if(energy <= 0.) 
148        continue;
149
150     if(lowGainFlag){
151       new(tmpLG[ilgDigit]) AliPHOSDigit(-1,absId,(Float_t)energy,(Float_t)time);
152       ilgDigit++ ; 
153     }
154     else{ 
155       if(decoder->IsOverflow()) //Keep this digit to replace it by Low Gain later.
156                                 //If there is no LogGain it wil be removed by cut on Min E
157         new((*digits)[iDigit]) AliPHOSDigit(-1,absId,-1.f,(Float_t)time);
158       else
159         new((*digits)[iDigit]) AliPHOSDigit(-1,absId,(Float_t)energy,(Float_t)time);
160       iDigit++;
161     }
162   }
163
164   //Now scan created LG and HG digits and keep only those which appeared in both lists 
165   //replace energy of HighGain digits only if there is overflow
166   //negative energy (overflow)
167   digits->Sort() ;
168   tmpLG.Sort() ;
169   Int_t iLG = 0;
170   Int_t nLG1 = tmpLG.GetEntriesFast()-1 ;
171
172   for(Int_t iDig=0 ; iDig < digits->GetEntriesFast() ; iDig++) { 
173     AliPHOSDigit * digHG = dynamic_cast<AliPHOSDigit*>(digits->At(iDig)) ;
174     if (!digHG) continue;
175     AliPHOSDigit * digLG = dynamic_cast<AliPHOSDigit*>(tmpLG.At(iLG)) ;
176     while(iLG<nLG1 && digHG->GetId()> digLG->GetId()){
177       iLG++ ;
178       digLG = dynamic_cast<AliPHOSDigit*>(tmpLG.At(iLG)) ;
179     }
180     if(digLG && digHG->GetId() == digLG->GetId()){ //we found pair
181       if(digHG->GetEnergy()<0.){ //This is overflow in HG
182         digHG->SetTime(digLG->GetTime()) ;
183         digHG->SetEnergy(digLG->GetEnergy()) ;
184       } 
185       else{ //Make approximate comparison of HG and LG energies
186         Double_t de = (digHG->GetEnergy()-digLG->GetEnergy())/16./0.005 ; //aproximate difference in LG ADC ch. 
187         if(TMath::Abs(de)>highLowDiff){ //too strong difference, remove digit
188           digits->RemoveAt(iDig) ;
189         }
190       }
191     }
192     else{ //no pair - remove
193       // temporary fix for dead LG channels
194       Int_t absId=digHG->GetId() ;
195       Int_t relId[4] ;
196       fGeom->AbsToRelNumbering(absId,relId) ;
197       if(relId[2]%2==1 && relId[3]%16==4) 
198         continue ;
199       if(digHG->GetEnergy()>16*5*0.005) //LG too low to exist
200         digits->RemoveAt(iDig) ;                                                                                                            
201     }
202   }
203
204   CleanDigits(digits) ;
205
206 }
207 //____________________________________________________________________________
208 Double_t AliPHOSRawDigiProducer::CalibrateE(Double_t amp, Int_t* relId, Bool_t isLowGain)
209 {
210   // Convert EMC measured amplitude into real energy.
211   // Calibration parameters are taken from calibration data base for raw data,
212   // or from digitizer parameters for simulated data.                        
213   if(fgCalibData){ 
214     Int_t   module = relId[0];  
215     Int_t   column = relId[3];
216     Int_t   row    = relId[2];
217     if(relId[1]==0) { // this is EMC 
218       if(isLowGain){
219         amp*= fgCalibData->GetHighLowRatioEmc(module,column,row);
220       }
221       amp *= fgCalibData->GetADCchannelEmc(module,column,row);                                                          
222       return amp ;         
223     }         
224   }          
225   return 0;        
226 }
227 //____________________________________________________________________________
228 Double_t AliPHOSRawDigiProducer::CalibrateT(Double_t time, Int_t * relId, Bool_t /* isLowGain */)
229 {
230   //Calibrate time
231   time*=fPulseGenerator->GetRawFormatTimeTrigger() ;
232   if(fgCalibData){
233     Int_t   module = relId[0];
234     Int_t   column = relId[3];
235     Int_t   row    = relId[2];
236     if(relId[1]==0) { // this is EMC
237       time += fgCalibData->GetTimeShiftEmc(module,column,row);                   
238       return time ;             
239     }
240   }
241  
242   return -999.;
243 }
244 //____________________________________________________________________________
245 void AliPHOSRawDigiProducer::CleanDigits(TClonesArray * digits)
246 {
247   // remove digits with amplitudes below threshold.
248   // remove digits in bad channels
249   // sort digits with icreasing AbsId
250   
251   //remove digits in bad map and below threshold
252   Bool_t isBadMap = 0 ;
253   if(fgCalibData->GetNumOfEmcBadChannels()){
254     isBadMap=1 ;
255   }
256   
257   for(Int_t i=0; i<digits->GetEntriesFast(); i++){
258     AliPHOSDigit * digit = static_cast<AliPHOSDigit*>(digits->At(i)) ;
259     if(!digit)
260       continue  ;
261     if ( (IsInEMC(digit) && digit->GetEnergy() < fEmcMinE) ||
262          (IsInCPV(digit) && digit->GetEnergy() < fCpvMinE) ){
263       digits->RemoveAt(i) ;
264       continue ;
265     }
266     if(isBadMap){ //check bad map now
267       Int_t relid[4] ;
268       fGeom->AbsToRelNumbering(digit->GetId(), relid) ; 
269       if(fgCalibData->IsBadChannelEmc(relid[0],relid[2],relid[3])){
270         digits->RemoveAt(i) ;
271       }
272     }
273   }
274
275   //Compress, sort and set indexes
276   digits->Compress() ;
277 //  digits->Sort(); already sorted earlier
278   for (Int_t i = 0 ; i < digits->GetEntriesFast() ; i++) { 
279     AliPHOSDigit *digit = static_cast<AliPHOSDigit*>( digits->At(i) ) ; 
280     digit->SetIndexInList(i) ;     
281   }
282 }
283 //____________________________________________________________________________
284 Bool_t AliPHOSRawDigiProducer::IsInEMC(AliPHOSDigit * digit) const
285 {
286   // Tells if (true) or not (false) the digit is in a PHOS-EMC module
287   return digit->GetId() <= fEmcCrystals ;
288
289 }
290
291 //____________________________________________________________________________
292 Bool_t AliPHOSRawDigiProducer::IsInCPV(AliPHOSDigit * digit) const
293 {
294   // Tells if (true) or not (false) the digit is in a PHOS-CPV module
295   return digit->GetId() > fEmcCrystals ;
296 }
297 //____________________________________________________________________________
298 void AliPHOSRawDigiProducer::GetCalibrationParameters() 
299 {
300   // Set calibration parameters:
301   // if calibration database exists, they are read from database,
302   // otherwise, reconstruction stops in the constructor of AliPHOSCalibData
303   //
304   // It is a user responsilibity to open CDB before reconstruction, for example: 
305   // AliCDBStorage* storage = AliCDBManager::Instance()->GetStorage("local://CalibDB");
306
307   if (!fgCalibData){
308     fgCalibData = new AliPHOSCalibData(-1); //use AliCDBManager's run number
309   }
310   if (fgCalibData->GetCalibDataEmc() == 0)
311     AliFatal("Calibration parameters for PHOS EMC not found. Stop reconstruction.\n");
312   if (fgCalibData->GetCalibDataCpv() == 0)
313     AliFatal("Calibration parameters for PHOS CPV not found. Stop reconstruction.\n");
314 }