]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PHOS/AliPHOSRawDigiProducer.cxx
Fix from Yves, if fitting is not good, recalculate from parabola; reject bad channels...
[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 AliPHOSRawFitter. 
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 "AliPHOSRawFitterv0.h"
38 #include "AliPHOSGeometry.h"
39 #include "AliPHOSDigit.h"
40 #include "AliPHOSCalibData.h"
41 #include "AliPHOSPulseGenerator.h"
42 #include "AliCaloRawStreamV3.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   fSampleToSec(0.),
56   fEmcCrystals(0),
57   fGeom(0),
58   fPulseGenerator(0),
59   fRawReader(0),
60   fRawStream(0)
61 {
62   // Default constructor
63
64   fGeom=AliPHOSGeometry::GetInstance() ;
65   if(!fGeom) fGeom = AliPHOSGeometry::GetInstance("IHEP");
66
67   fEmcCrystals=fGeom->GetNCristalsInModule()*fGeom->GetNModules() ;
68   fPulseGenerator = new AliPHOSPulseGenerator();
69   GetCalibrationParameters() ; 
70
71 }
72 //--------------------------------------------------------------------------------------
73 AliPHOSRawDigiProducer::AliPHOSRawDigiProducer(AliRawReader *rawReader,
74                                                AliAltroMapping **mapping):
75   TObject(),
76   fEmcMinE(0.),
77   fCpvMinE(0.),
78   fSampleQualityCut(1.),
79   fSampleToSec(0.),
80   fEmcCrystals(0),
81   fGeom(0),
82   fPulseGenerator(0),
83   fRawReader(rawReader),
84   fRawStream(0)
85 {
86   // Default constructor
87
88   fGeom=AliPHOSGeometry::GetInstance() ;
89   if(!fGeom) fGeom = AliPHOSGeometry::GetInstance("IHEP");
90
91   fEmcCrystals=fGeom->GetNCristalsInModule()*fGeom->GetNModules() ;
92   fPulseGenerator = new AliPHOSPulseGenerator();
93   GetCalibrationParameters() ; 
94
95   fRawStream = new AliCaloRawStreamV3(rawReader,"PHOS",mapping);
96
97 }
98 //--------------------------------------------------------------------------------------
99 AliPHOSRawDigiProducer::AliPHOSRawDigiProducer(const AliPHOSRawDigiProducer &dp):
100   TObject(),
101   fEmcMinE(0.),
102   fCpvMinE(0.),
103   fSampleQualityCut(1.),
104   fSampleToSec(0.),
105   fEmcCrystals(0),
106   fGeom(0),
107   fPulseGenerator(0),
108   fRawReader(0),
109   fRawStream(0)
110 {                                                          
111   // Copy constructor
112
113   fEmcMinE = dp.fEmcMinE ;
114   fCpvMinE = dp.fCpvMinE ;
115   fSampleQualityCut = dp.fSampleQualityCut;
116   fSampleToSec = dp.fSampleToSec ;
117   fEmcCrystals = dp.fEmcCrystals ;
118   fPulseGenerator = new AliPHOSPulseGenerator();
119   fGeom = dp.fGeom ;
120 }
121 //--------------------------------------------------------------------------------------
122 AliPHOSRawDigiProducer& AliPHOSRawDigiProducer::operator= (const AliPHOSRawDigiProducer &dp)
123 {
124   // Assign operator
125
126   if(&dp == this) return *this;
127
128   fEmcMinE = dp.fEmcMinE ;
129   fCpvMinE = dp.fCpvMinE ;
130   fSampleQualityCut = dp.fSampleQualityCut ;
131   fSampleToSec = dp.fSampleToSec ;
132   fEmcCrystals = dp.fEmcCrystals ;
133   fGeom = dp.fGeom ;
134   if(fPulseGenerator) delete fPulseGenerator ;
135   fPulseGenerator = new AliPHOSPulseGenerator();
136   return  *this;
137
138 //--------------------------------------------------------------------------------------
139 AliPHOSRawDigiProducer::~AliPHOSRawDigiProducer()
140 {
141   // Destructor
142   if(fPulseGenerator) delete fPulseGenerator ;
143   fPulseGenerator=0 ;
144   delete fRawStream;
145 }
146 //--------------------------------------------------------------------------------------
147 void AliPHOSRawDigiProducer::MakeDigits(TClonesArray *digits, AliPHOSRawFitterv0* fitter) 
148 {
149   //Makes the job.
150   //TClonesArray *digits and raw data fitter should be provided by calling function.
151
152   digits->Clear();
153  
154   Int_t iDigit=0 ;
155   Int_t relId[4], absId=-1, caloFlag=-1;
156   
157   const Double_t baseLine=1. ; //Minimal energy of digit in ADC ch. 
158
159   //Calculate conversion coeff. from Sample time step to seconds
160   //If OCDB contains negative or zero value - use one from RCU trailer
161   //Negative value in OCDB is used only for simulation of raw digits
162   if(fgCalibData->GetSampleTimeStep()>0.)
163     fSampleToSec=fgCalibData->GetSampleTimeStep() ;
164   else
165     fSampleToSec=fRawStream->GetTSample() ;
166   
167   //Temporary array for LowGain digits
168   TClonesArray tmpLG("AliPHOSDigit",10000) ;
169   Int_t ilgDigit=0 ;
170
171   //Let fitter subtract pedestals in case of ZS
172   fitter->SetCalibData(fgCalibData) ;
173   
174   while (fRawStream->NextDDL()) {
175     while (fRawStream->NextChannel()) {
176       relId[0] = 5 - fRawStream->GetModule() ; // counts from 1 to 5
177       relId[1] = 0;
178       relId[2] = fRawStream->GetCellX()  + 1; // counts from 1 to 64
179       relId[3] = fRawStream->GetCellZ()  + 1; // counts from 1 to 56
180       caloFlag = fRawStream->GetCaloFlag();   // 0=LG, 1=HG, 2=TRU
181       
182       if(caloFlag!=0 && caloFlag!=1) continue; //TRU data!
183       
184       fitter->SetChannelGeo(relId[0],relId[2],relId[3],caloFlag);
185
186       if(fitter->GetAmpOffset()==0 && fitter->GetAmpThreshold()==0) {
187         short value = fRawStream->GetAltroCFG1();
188         bool ZeroSuppressionEnabled = (value >> 15) & 0x1;
189         if(ZeroSuppressionEnabled) {
190           short offset = (value >> 10) & 0xf;
191           short threshold = value & 0x3ff;
192           fitter->SubtractPedestals(kFALSE);
193           fitter->SetAmpOffset(offset);
194           fitter->SetAmpThreshold(threshold);
195         }
196       }
197       
198       fGeom->RelToAbsNumbering(relId, absId);
199       
200       fitter->SetNBunches(0);
201       while (fRawStream->NextBunch()) { //Take first in time banch
202         const UShort_t *sig = fRawStream->GetSignals();
203         Int_t sigStart  = fRawStream->GetStartTimeBin();
204         Int_t sigLength = fRawStream->GetBunchLength();
205         fitter->Eval(sig,sigStart,sigLength);
206       } // End of NextBunch()
207
208       
209       Double_t energy = fitter->GetEnergy() ; 
210       Double_t time   = fitter->GetTime() ;
211       if(energy<=baseLine) //in ADC channels
212         continue ;
213
214       //remove digits with bad shape. Fitter should calculate quality so that 
215       //in default case quality [0,1], while larger values of quality mean somehow 
216       //corrupted samples, 999 means obviously corrupted sample.
217       //It is difficult to fit samples with overflow (even setting cut on overflow values)
218       //because too few points are left to fit. So we do not evaluate samples with overflow
219
220       if(fitter->GetSignalQuality() > fSampleQualityCut && !(fitter->IsOverflow()))
221         continue ;
222       
223       energy = CalibrateE(energy,relId,!caloFlag) ;
224 //       time   = CalibrateT(time,relId,lowGainFlag) ;
225
226       //convert time from sample bin units to s
227       time*=fSampleToSec ;
228       
229       if(energy <= 0.) 
230         continue;
231       
232       if (caloFlag == AliCaloRawStreamV3::kLowGain) {
233         new(tmpLG[ilgDigit]) AliPHOSDigit(-1,absId,(Float_t)energy,(Float_t)time);
234         ilgDigit++ ; 
235       }
236       else if (caloFlag == AliCaloRawStreamV3::kHighGain) {
237         if(fitter->IsOverflow()) //Keep this digit to replace it by Low Gain later.
238           //If there is no LogGain it wil be removed by cut on Min E
239           new((*digits)[iDigit]) AliPHOSDigit(-1,absId,-1.f,(Float_t)time);
240         else
241           new((*digits)[iDigit]) AliPHOSDigit(-1,absId,(Float_t)energy,(Float_t)time);
242         iDigit++;
243       }
244     } // End of NextChannel()
245
246     //Now scan created LG and HG digits and keep only those which appeared in both lists 
247     //replace energy of HighGain digits only if there is overflow
248     //negative energy (overflow)
249     digits->Sort() ;
250     tmpLG.Sort() ;
251     Int_t iLG = 0;
252     Int_t nLG1 = tmpLG.GetEntriesFast()-1 ;
253     
254     for(Int_t iDig=0 ; iDig < digits->GetEntriesFast() ; iDig++) { 
255       AliPHOSDigit * digHG = dynamic_cast<AliPHOSDigit*>(digits->At(iDig)) ;
256       if (!digHG) continue;
257       AliPHOSDigit * digLG = dynamic_cast<AliPHOSDigit*>(tmpLG.At(iLG)) ;
258       while(digLG && iLG<nLG1 && digHG->GetId()> digLG->GetId()){
259         iLG++ ;
260         digLG = dynamic_cast<AliPHOSDigit*>(tmpLG.At(iLG)) ;
261       }
262       absId=digHG->GetId() ;
263       fGeom->AbsToRelNumbering(absId,relId) ;
264  
265       if(digLG && digHG->GetId() == digLG->GetId()){ //we found pair
266         if(digHG->GetEnergy()<0.){ //This is overflow in HG
267           digHG->SetTime(digLG->GetTime()) ;
268           digHG->SetEnergy(digLG->GetEnergy()) ;
269         }
270       }
271       else{ //no pair - remove
272         if(digHG->GetEnergy()<0.) //no pair, in saturation
273           digits->RemoveAt(iDig) ;                                                          
274       }
275     }
276   } // End of NextDDL()
277
278   CleanDigits(digits) ;
279   
280 }
281 //____________________________________________________________________________
282 Double_t AliPHOSRawDigiProducer::CalibrateE(Double_t amp, Int_t* relId, Bool_t isLowGain)
283 {
284   // Convert EMC LG amplitude to HG (multipli by ~16)
285   // Calibration parameters are taken from calibration data base 
286   if(fgCalibData){ 
287     Int_t   module = relId[0];  
288     Int_t   column = relId[3];
289     Int_t   row    = relId[2];
290     if(relId[1]==0) { // this is EMC 
291       if(isLowGain){
292         amp*= fgCalibData->GetHighLowRatioEmc(module,column,row);
293       }
294       return amp ;         
295     }         
296   }          
297   return 0;        
298 }
299 //____________________________________________________________________________
300 Double_t AliPHOSRawDigiProducer::CalibrateT(Double_t time, Int_t * relId, Bool_t /* isLowGain */)
301 {
302   //Calibrate time
303   time*=fPulseGenerator->GetRawFormatTimeTrigger() ;
304   if(fgCalibData){
305     Int_t   module = relId[0];
306     Int_t   column = relId[3];
307     Int_t   row    = relId[2];
308     if(relId[1]==0) { // this is EMC
309       time += fgCalibData->GetTimeShiftEmc(module,column,row);                   
310       return time ;             
311     }
312   }
313  
314   return -999.;
315 }
316 //____________________________________________________________________________
317 void AliPHOSRawDigiProducer::CleanDigits(TClonesArray * digits)
318 {
319   // remove digits with amplitudes below threshold.
320   // remove digits in bad channels
321   // sort digits with icreasing AbsId
322   
323   //remove digits in bad map and below threshold
324   Bool_t isBadMap = 0 ;
325   if(fgCalibData->GetNumOfEmcBadChannels()){
326     isBadMap=1 ;
327   }
328   
329   for(Int_t i=0; i<digits->GetEntriesFast(); i++){
330     AliPHOSDigit * digit = static_cast<AliPHOSDigit*>(digits->At(i)) ;
331     if(!digit)
332       continue  ;
333     if ( (IsInEMC(digit) && digit->GetEnergy() < fEmcMinE) ||
334          (IsInCPV(digit) && digit->GetEnergy() < fCpvMinE) ){
335       digits->RemoveAt(i) ;
336       continue ;
337     }
338     if(isBadMap){ //check bad map now
339       Int_t relid[4] ;
340       fGeom->AbsToRelNumbering(digit->GetId(), relid) ; 
341       if(fgCalibData->IsBadChannelEmc(relid[0],relid[3],relid[2])){
342         digits->RemoveAt(i) ;
343       }
344     }
345   }
346
347   //Compress, sort and set indexes
348   digits->Compress() ;
349 //  digits->Sort(); already sorted earlier
350   for (Int_t i = 0 ; i < digits->GetEntriesFast() ; i++) { 
351     AliPHOSDigit *digit = static_cast<AliPHOSDigit*>( digits->At(i) ) ; 
352     digit->SetIndexInList(i) ;     
353   }
354 }
355 //____________________________________________________________________________
356 Bool_t AliPHOSRawDigiProducer::IsInEMC(AliPHOSDigit * digit) const
357 {
358   // Tells if (true) or not (false) the digit is in a PHOS-EMC module
359   return digit->GetId() <= fEmcCrystals ;
360
361 }
362
363 //____________________________________________________________________________
364 Bool_t AliPHOSRawDigiProducer::IsInCPV(AliPHOSDigit * digit) const
365 {
366   // Tells if (true) or not (false) the digit is in a PHOS-CPV module
367   return digit->GetId() > fEmcCrystals ;
368 }
369 //____________________________________________________________________________
370 void AliPHOSRawDigiProducer::GetCalibrationParameters() 
371 {
372   // Set calibration parameters:
373   // if calibration database exists, they are read from database,
374   // otherwise, reconstruction stops in the constructor of AliPHOSCalibData
375   //
376   // It is a user responsilibity to open CDB before reconstruction, for example: 
377   // AliCDBStorage* storage = AliCDBManager::Instance()->GetStorage("local://CalibDB");
378
379   if (!fgCalibData){
380     fgCalibData = new AliPHOSCalibData(-1); //use AliCDBManager's run number
381   }
382   if (fgCalibData->GetCalibDataEmc() == 0)
383     AliFatal("Calibration parameters for PHOS EMC not found. Stop reconstruction.\n");
384   if (fgCalibData->GetCalibDataCpv() == 0)
385     AliFatal("Calibration parameters for PHOS CPV not found. Stop reconstruction.\n");
386 }