Bug fix. Distance from detector to vertex now calculated correctly
[u/mrichter/AliRoot.git] / PHOS / AliPHOSRawDigiProducer.cxx
CommitLineData
7bc140a5 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 ---
88fb5e50 33#include "TClonesArray.h"
34
7bc140a5 35// --- AliRoot header files ---
88fb5e50 36#include "AliPHOSRawDigiProducer.h"
37#include "AliPHOSRawDecoder.h"
38#include "AliPHOSGeometry.h"
39#include "AliPHOSDigit.h"
c0394bfb 40#include "AliPHOSRecoParam.h"
41#include "AliPHOSCalibData.h"
39569d36 42#include "AliPHOSPulseGenerator.h"
c0394bfb 43#include "AliLog.h"
88fb5e50 44
45ClassImp(AliPHOSRawDigiProducer)
46
c0394bfb 47AliPHOSCalibData * AliPHOSRawDigiProducer::fgCalibData = 0 ;
48
49//--------------------------------------------------------------------------------------
7e88424f 50AliPHOSRawDigiProducer::AliPHOSRawDigiProducer():
51 TObject(),
52 fEmcMinE(0.),
53 fCpvMinE(0.),
54 fSampleQualityCut(1.),
bafc1087 55 fGlobalAltroOffset(0),
7e88424f 56 fEmcCrystals(0),
57 fGeom(0),
58 fPulseGenerator(0)
59{
60 // Default constructor
c0394bfb 61}
62//--------------------------------------------------------------------------------------
7e88424f 63AliPHOSRawDigiProducer::AliPHOSRawDigiProducer(const AliPHOSRecoParam* recoParam):
64 TObject(),
65 fEmcMinE(0.),
66 fCpvMinE(0.),
67 fSampleQualityCut(1.),
bafc1087 68 fGlobalAltroOffset(0),
7e88424f 69 fEmcCrystals(0),
70 fGeom(0),
71 fPulseGenerator(0)
72{
73 // Constructor takes paramerters from recoParam
c0394bfb 74
7e88424f 75 if(!recoParam) AliFatal("Reconstruction parameters are not set!");
c0394bfb 76
7e88424f 77 fEmcMinE = recoParam->GetEMCMinE();
78 fCpvMinE = recoParam->GetCPVMinE();
7e88424f 79 fSampleQualityCut = recoParam->GetEMCSampleQualityCut() ;
bafc1087 80 fGlobalAltroOffset = recoParam->GetGlobalAltroOffset() ;
70d93620 81
c0394bfb 82 fGeom=AliPHOSGeometry::GetInstance() ;
83 if(!fGeom) fGeom = AliPHOSGeometry::GetInstance("IHEP");
84
85 fEmcCrystals=fGeom->GetNCristalsInModule()*fGeom->GetNModules() ;
86
39569d36 87 fPulseGenerator = new AliPHOSPulseGenerator();
88
c0394bfb 89 GetCalibrationParameters() ;
90}
91//--------------------------------------------------------------------------------------
7e88424f 92AliPHOSRawDigiProducer::AliPHOSRawDigiProducer(const AliPHOSRawDigiProducer &dp):
93 TObject(),
94 fEmcMinE(0.),
95 fCpvMinE(0.),
96 fSampleQualityCut(1.),
bafc1087 97 fGlobalAltroOffset(0),
7e88424f 98 fEmcCrystals(0),
99 fGeom(0),
100 fPulseGenerator(0)
101{
102 // Copy constructor
c0394bfb 103
104 fEmcMinE = dp.fEmcMinE ;
105 fCpvMinE = dp.fCpvMinE ;
106 fEmcCrystals = dp.fEmcCrystals ;
39569d36 107 fPulseGenerator = new AliPHOSPulseGenerator();
c0394bfb 108 fGeom = dp.fGeom ;
109}
110//--------------------------------------------------------------------------------------
7e88424f 111AliPHOSRawDigiProducer& AliPHOSRawDigiProducer::operator= (const AliPHOSRawDigiProducer &dp)
112{
113 // Assign operator
114
c0394bfb 115 if(&dp == this) return *this;
116
117 fEmcMinE = dp.fEmcMinE ;
118 fCpvMinE = dp.fCpvMinE ;
70d93620 119 fSampleQualityCut = dp.fSampleQualityCut ;
bafc1087 120 fGlobalAltroOffset = dp.fGlobalAltroOffset ;
c0394bfb 121 fEmcCrystals = dp.fEmcCrystals ;
122 fGeom = dp.fGeom ;
39569d36 123 if(fPulseGenerator) delete fPulseGenerator ;
124 fPulseGenerator = new AliPHOSPulseGenerator();
c0394bfb 125 return *this;
126}
39569d36 127//--------------------------------------------------------------------------------------
7e88424f 128AliPHOSRawDigiProducer::~AliPHOSRawDigiProducer()
129{
130 // Desctructor
131 if(fPulseGenerator) delete fPulseGenerator ;
132 fPulseGenerator=0 ;
39569d36 133}
7bc140a5 134//--------------------------------------------------------------------------------------
88fb5e50 135void AliPHOSRawDigiProducer::MakeDigits(TClonesArray *digits, AliPHOSRawDecoder* decoder)
136{
7bc140a5 137 //Makes the job.
138 //TClonesArray *digits and raw data decoder should be provided by calling function.
139
88fb5e50 140 digits->Clear();
141
88fb5e50 142 Int_t iDigit = 0 ;
88fb5e50 143 Int_t relId[4], absId =0;
144
c0394bfb 145 const Double_t baseLine=1. ; //Minimal energy of digit in ADC ch.
a7c985dc 146 const Double_t highLowDiff=2.; //Maximal difference between High and Low channels in LG adc channels
88fb5e50 147
c0394bfb 148 //Temporary array for LowGain digits
149 TClonesArray tmpLG("AliPHOSDigit",10000) ;
150 Int_t ilgDigit=0 ;
bafc1087 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) ;
c0394bfb 158
159 while (decoder->NextDigit()) {
34827459 160
c0394bfb 161 Double_t energy=decoder->GetEnergy() ;
162 if(energy<=baseLine) //in ADC channels
163 continue ;
1267d56d 164
7e88424f 165 //remove digits with bad shape. Decoder should calculate quality so that
70d93620 166 //in default case quality [0,1], while larger values of quality mean somehow
167 //corrupted samples, 999 means obviously corrupted sample.
a7c985dc 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()))
70d93620 171 continue ;
172
c0394bfb 173 Bool_t lowGainFlag = decoder->IsLowGain();
88fb5e50 174
175 relId[0] = decoder->GetModule();
176 relId[1] = 0;
177 relId[2] = decoder->GetRow();
178 relId[3] = decoder->GetColumn();
c0394bfb 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){
7899109f 190 new(tmpLG[ilgDigit]) AliPHOSDigit(-1,absId,(Float_t)energy,(Float_t)time);
c0394bfb 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
7899109f 196 new((*digits)[iDigit]) AliPHOSDigit(-1,absId,-1.f,(Float_t)time);
c0394bfb 197 else
7899109f 198 new((*digits)[iDigit]) AliPHOSDigit(-1,absId,(Float_t)energy,(Float_t)time);
199 iDigit++;
c0394bfb 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 ;
39569d36 210
c0394bfb 211 for(Int_t iDig=0 ; iDig < digits->GetEntriesFast() ; iDig++) {
212 AliPHOSDigit * digHG = dynamic_cast<AliPHOSDigit*>(digits->At(iDig)) ;
7899109f 213 if (!digHG) continue;
c0394bfb 214 AliPHOSDigit * digLG = dynamic_cast<AliPHOSDigit*>(tmpLG.At(iLG)) ;
a7c985dc 215 while(digLG && iLG<nLG1 && digHG->GetId()> digLG->GetId()){
c0394bfb 216 iLG++ ;
217 digLG = dynamic_cast<AliPHOSDigit*>(tmpLG.At(iLG)) ;
218 }
605cb8bb 219 absId=digHG->GetId() ;
a7c985dc 220 fGeom->AbsToRelNumbering(absId,relId) ;
221
7899109f 222 if(digLG && digHG->GetId() == digLG->GetId()){ //we found pair
c0394bfb 223 if(digHG->GetEnergy()<0.){ //This is overflow in HG
7899109f 224 digHG->SetTime(digLG->GetTime()) ;
225 digHG->SetEnergy(digLG->GetEnergy()) ;
c0394bfb 226 }
227 else{ //Make approximate comparison of HG and LG energies
a7c985dc 228 Double_t de = (digHG->GetEnergy()-digLG->GetEnergy()) ;
229 if(TMath::Abs(de)>CalibrateE(double(highLowDiff),relId,1)){ //too strong difference, remove digit
c0394bfb 230 digits->RemoveAt(iDig) ;
88fb5e50 231 }
232 }
88fb5e50 233 }
c0394bfb 234 else{ //no pair - remove
235 // temporary fix for dead LG channels
c0394bfb 236 if(relId[2]%2==1 && relId[3]%16==4)
237 continue ;
a7c985dc 238 if(digHG->GetEnergy()>CalibrateE(double(5.),relId,1)) //One can not always find LG with Amp<5 ADC ch.
39569d36 239 digits->RemoveAt(iDig) ;
c0394bfb 240 }
241 }
242
243 CleanDigits(digits) ;
244
245}
246//____________________________________________________________________________
247Double_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);
88fb5e50 259 }
c0394bfb 260 amp *= fgCalibData->GetADCchannelEmc(module,column,row);
261 return amp ;
262 }
263 }
264 return 0;
265}
266//____________________________________________________________________________
267Double_t AliPHOSRawDigiProducer::CalibrateT(Double_t time, Int_t * relId, Bool_t /* isLowGain */)
268{
269 //Calibrate time
39569d36 270 time*=fPulseGenerator->GetRawFormatTimeTrigger() ;
c0394bfb 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//____________________________________________________________________________
284void 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) ;
a65517aa 308 if(fgCalibData->IsBadChannelEmc(relid[0],relid[3],relid[2])){
c0394bfb 309 digits->RemoveAt(i) ;
88fb5e50 310 }
311 }
88fb5e50 312 }
313
c0394bfb 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//____________________________________________________________________________
323Bool_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//____________________________________________________________________________
331Bool_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//____________________________________________________________________________
337void 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");
88fb5e50 353}