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