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