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