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