add external setter for MC parents list
[u/mrichter/AliRoot.git] / EMCAL / AliEMCALRawUtils.cxx
CommitLineData
168c7b3c 1// -*- mode: c++ -*-
ee299369 2/**************************************************************************
3 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * *
5 * Author: The ALICE Off-line Project. *
6 * Contributors are mentioned in the code where appropriate. *
7 * *
8 * Permission to use, copy, modify and distribute this software and its *
9 * documentation strictly for non-commercial purposes is hereby granted *
10 * without fee, provided that the above copyright notice appears in all *
11 * copies and that both the copyright notice and this permission notice *
12 * appear in the supporting documentation. The authors make no claims *
13 * about the suitability of this software for any purpose. It is *
14 * provided "as is" without express or implied warranty. *
15 **************************************************************************/
16
17/* $Id$ */
ee299369 18
de39a0ff 19
e5bbbc4e 20//_________________________________________________________________________
21// Utility Class for handling Raw data
22// Does all transitions from Digits to Raw and vice versa,
23// for simu and reconstruction
24//
25// Note: the current version is still simplified. Only
26// one raw signal per digit is generated; either high-gain or low-gain
27// Need to add concurrent high and low-gain info in the future
28// No pedestal is added to the raw signal.
ee299369 29//*-- Author: Marco van Leeuwen (LBL)
77eac271 30//*-- Major refactoring by Per Thomas Hille
92d9f317 31
ee299369 32#include "AliEMCALRawUtils.h"
72c58de0 33#include "AliRun.h"
ee299369 34#include "AliRunLoader.h"
ee299369 35#include "AliAltroBuffer.h"
36#include "AliRawReader.h"
32cd4c24 37#include "AliCaloRawStreamV3.h"
ee299369 38#include "AliDAQ.h"
feedcab9 39#include "AliEMCALRecParam.h"
ee299369 40#include "AliEMCALLoader.h"
41#include "AliEMCALGeometry.h"
ee299369 42#include "AliEMCALDigit.h"
916f1e76 43#include "AliEMCALRawDigit.h"
20b636fc 44#include "AliEMCAL.h"
5e3106bc 45#include "AliCaloCalibPedestal.h"
16605c06 46#include "AliCaloBunchInfo.h"
47#include "AliCaloFitResults.h"
de39a0ff 48#include "AliEMCALTriggerRawDigitMaker.h"
49#include "AliEMCALTriggerSTURawStream.h"
50#include "AliEMCALTriggerData.h"
92d9f317 51#include "AliCaloConstants.h"
52#include "AliCaloRawAnalyzer.h"
53#include "AliCaloRawAnalyzerFactory.h"
1f847d9f 54#include "AliEMCALRawResponse.h"
9f467289 55
92d9f317 56using namespace CALO;
57using namespace EMCAL;
ee299369 58
0267cfa6 59using std::vector;
92d9f317 60ClassImp(AliEMCALRawUtils)
61
b4133f05 62
92d9f317 63AliEMCALRawUtils::AliEMCALRawUtils( Algo::fitAlgorithm fitAlgo) : fNoiseThreshold(3),
64 fNPedSamples(4),
65 fGeom(0),
66 fOption(""),
67 fRemoveBadChannels(kFALSE),
77eac271 68 fFittingAlgorithm(fitAlgo),
92d9f317 69 fTimeMin(-1.),
70 fTimeMax(1.),
71 fUseFALTRO(kTRUE),
72 fRawAnalyzer(0),
73 fTriggerRawDigitMaker(0x0)
dab4328c 74{ // ctor; set up fit algo etc
4fe71e02 75 SetFittingAlgorithm(fitAlgo);
65bdc82f 76 const TObjArray* maps = AliEMCALRecParam::GetMappings();
77 if(!maps) AliFatal("Cannot retrieve ALTRO mappings!!");
77eac271 78 for(Int_t i = 0; i < 4; i++)
79 {
80 fMapping[i] = (AliAltroMapping*)maps->At(i);
81 }
82
33c3c91a 83 AliRunLoader *rl = AliRunLoader::Instance();
77eac271 84 if (rl && rl->GetAliRun())
85 {
a2339fea 86 AliEMCAL * emcal = dynamic_cast<AliEMCAL*>(rl->GetAliRun()->GetDetector("EMCAL"));
77eac271 87 if(emcal)
88 {
89 fGeom = emcal->GetGeometry();
90 }
91 else
92 {
93 AliDebug(1, Form("Using default geometry in raw reco"));
94 fGeom = AliEMCALGeometry::GetInstance(AliEMCALGeometry::GetDefaultGeometryName());
95 }
96 }
97 else
98 {
a2339fea 99 AliDebug(1, Form("Using default geometry in raw reco"));
100 fGeom = AliEMCALGeometry::GetInstance(AliEMCALGeometry::GetDefaultGeometryName());
101 }
92d9f317 102
5544799a 103 if(!fGeom) AliFatal(Form("Could not get geometry!"));
77eac271 104 fTriggerRawDigitMaker = new AliEMCALTriggerRawDigitMaker();
5544799a 105}
106
65bdc82f 107
92d9f317 108AliEMCALRawUtils::~AliEMCALRawUtils()
65bdc82f 109{
e5bbbc4e 110 //dtor
48b634ff 111 delete fRawAnalyzer;
112 delete fTriggerRawDigitMaker;
ee299369 113}
65bdc82f 114
92d9f317 115
65bdc82f 116void AliEMCALRawUtils::Digits2Raw()
ee299369 117{
118 // convert digits of the current event to raw data
33c3c91a 119 AliRunLoader *rl = AliRunLoader::Instance();
ee299369 120 AliEMCALLoader *loader = dynamic_cast<AliEMCALLoader*>(rl->GetDetectorLoader("EMCAL"));
ee299369 121 loader->LoadDigits("EMCAL");
122 loader->GetEvent();
123 TClonesArray* digits = loader->Digits() ;
124
125 if (!digits) {
126 Warning("Digits2Raw", "no digits found !");
127 return;
128 }
a2339fea 129
ee299369 130 static const Int_t nDDL = 12*2; // 12 SM hardcoded for now. Buffers allocated dynamically, when needed, so just need an upper limit here
131 AliAltroBuffer* buffers[nDDL];
132 for (Int_t i=0; i < nDDL; i++)
133 buffers[i] = 0;
a2339fea 134
92d9f317 135 TArrayI adcValuesLow( TIMEBINS );
136 TArrayI adcValuesHigh( TIMEBINS );
de39a0ff 137
ee299369 138 // loop over digits (assume ordered digits)
92d9f317 139 for (Int_t iDigit = 0; iDigit < digits->GetEntries(); iDigit++)
140 {
141 AliEMCALDigit* digit = dynamic_cast<AliEMCALDigit *>(digits->At(iDigit)) ;
142 if(!digit)
143 {
144 AliFatal("NULL Digit");
145 }
146 else
147 {
1f847d9f 148 if (digit->GetAmplitude() < AliEMCALRawResponse::GetRawFormatThreshold() )
92d9f317 149 {
150 continue;
151 }
152 //get cell indices
153 Int_t nSM = 0;
154 Int_t nIphi = 0;
155 Int_t nIeta = 0;
156 Int_t iphi = 0;
157 Int_t ieta = 0;
158 Int_t nModule = 0;
159 fGeom->GetCellIndex(digit->GetId(), nSM, nModule, nIphi, nIeta);
160 fGeom->GetCellPhiEtaIndexInSModule(nSM, nModule, nIphi, nIeta,iphi, ieta) ;
de39a0ff 161
92d9f317 162 //Check which is the RCU, 0 or 1, of the cell.
163 Int_t iRCU = -111;
77eac271 164 if (0<=iphi&&iphi<8) iRCU=0; // first cable row
165 else if (8<=iphi&&iphi<16 && 0<=ieta&&ieta<24) iRCU=0; // first half;
166 else if(8<=iphi&&iphi<16 && 24<=ieta&&ieta<48) iRCU=1; // second half;
167 //second cable row
168 else if(16<=iphi&&iphi<24) iRCU=1; // third cable row
169
170 if (nSM%2==1) iRCU = 1 - iRCU; // swap for odd=C side, to allow us to cable both sides the same
171
172 if (iRCU<0)
173 Fatal("Digits2Raw()","Non-existent RCU number: %d", iRCU);
de39a0ff 174
77eac271 175 //Which DDL?
176 Int_t iDDL = NRCUSPERMODULE*nSM + iRCU;
177 if (iDDL < 0 || iDDL >= nDDL){
178 Fatal("Digits2Raw()","Non-existent DDL board number: %d", iDDL);
179 }
180 else{
181 if (buffers[iDDL] == 0)
182 {
183 // open new file and write dummy header
184 TString fileName = AliDAQ::DdlFileName("EMCAL",iDDL);
185 //Select mapping file RCU0A, RCU0C, RCU1A, RCU1C
a2339fea 186 Int_t iRCUside=iRCU+(nSM%2)*2;
187 //iRCU=0 and even (0) SM -> RCU0A.data 0
188 //iRCU=1 and even (0) SM -> RCU1A.data 1
189 //iRCU=0 and odd (1) SM -> RCU0C.data 2
190 //iRCU=1 and odd (1) SM -> RCU1C.data 3
77eac271 191 buffers[iDDL] = new AliAltroBuffer(fileName.Data(),fMapping[iRCUside]);
a2339fea 192 buffers[iDDL]->WriteDataHeader(kTRUE, kFALSE); //Dummy;
193 }
194
195 // out of time range signal (?)
92d9f317 196 if (digit->GetTimeR() > TIMEBINMAX ) {
a2339fea 197 AliInfo("Signal is out of time range.\n");
198 buffers[iDDL]->FillBuffer((Int_t)digit->GetAmplitude());
92d9f317 199 buffers[iDDL]->FillBuffer( TIMEBINS ); // time bin
a2339fea 200 buffers[iDDL]->FillBuffer(3); // bunch length
201 buffers[iDDL]->WriteTrailer(3, ieta, iphi, nSM); // trailer
202 // calculate the time response function
203 } else {
1f847d9f 204 Bool_t lowgain = AliEMCALRawResponse::RawSampledResponse(digit->GetTimeR(), digit->GetAmplitude(),
205 adcValuesHigh.GetArray(), adcValuesLow.GetArray()) ;
206
207 if (lowgain)
208 buffers[iDDL]->WriteChannel(ieta, iphi, 0, TIMEBINS, adcValuesLow.GetArray(), AliEMCALRawResponse::GetRawFormatThreshold() );
a2339fea 209 else
1f847d9f 210 buffers[iDDL]->WriteChannel(ieta,iphi, 1, TIMEBINS, adcValuesHigh.GetArray(), AliEMCALRawResponse::GetRawFormatThreshold() );
a2339fea 211 }
212 }// iDDL under the limits
de39a0ff 213 }//digit exists
214 }//Digit loop
a2339fea 215
ee299369 216 // write headers and close files
217 for (Int_t i=0; i < nDDL; i++) {
218 if (buffers[i]) {
219 buffers[i]->Flush();
220 buffers[i]->WriteDataHeader(kFALSE, kFALSE);
221 delete buffers[i];
222 }
223 }
de39a0ff 224
ee299369 225 loader->UnloadDigits();
226}
227
92d9f317 228
77eac271 229
92d9f317 230void AliEMCALRawUtils::AddDigit(TClonesArray *digitsArr, Int_t id, Int_t lowGain, Float_t amp, Float_t time, Float_t chi2, Int_t ndf)
231{
77eac271 232 // comment
92d9f317 233 AliEMCALDigit *digit = 0, *tmpdigit = 0;
234 TIter nextdigit(digitsArr);
235
236 while (digit == 0 && (tmpdigit = (AliEMCALDigit*) nextdigit()))
237 {
238 if (tmpdigit->GetId() == id) digit = tmpdigit;
239 }
240
241 if (!digit) { // no digit existed for this tower; create one
242 Int_t type = AliEMCALDigit::kHG; // use enum in AliEMCALDigit
243 if (lowGain)
244 {
245 amp *= HGLGFACTOR;
246 type = AliEMCALDigit::kLGnoHG;
247 }
248
249 Int_t idigit = digitsArr->GetEntries();
250 new((*digitsArr)[idigit]) AliEMCALDigit( -1, -1, id, amp, time, type, idigit, chi2, ndf);
251 AliDebug(2,Form("Add digit Id %d for the first time, type %d", id, type));
252 }//digit added first time
253 else
254 { // a digit already exists, check range
255 // (use high gain if signal < cut value, otherwise low gain)
256 if (lowGain)
257 { // new digit is low gain
258 if (digit->GetAmplitude() > OVERFLOWCUT )
259 { // use if previously stored (HG) digit is out of range
260 digit->SetAmplitude( HGLGFACTOR * amp);
261 digit->SetTime(time);
262 digit->SetType(AliEMCALDigit::kLG);
263 AliDebug(2,Form("Add LG digit ID %d for the second time, type %d", digit->GetId(), digit->GetType()));
264 }
265 }//new low gain digit
266 else { // new digit is high gain
267
268 if (amp < OVERFLOWCUT )
269 { // new digit is high gain; use if not out of range
270 digit->SetAmplitude(amp);
271 digit->SetTime(time);
272 digit->SetType(AliEMCALDigit::kHG);
273 AliDebug(2,Form("Add HG digit ID %d for the second time, type %d", digit->GetId(), digit->GetType()));
274 }
275 else
276 { // HG out of range, just change flag value to show that HG did exist
277 digit->SetType(AliEMCALDigit::kLG);
278 AliDebug(2,Form("Change LG digit to HG, ID %d, type %d", digit->GetId(), digit->GetType()));
279 }
280 }//new high gain digit
281 }//digit existed replace it
282}
283
284
de39a0ff 285void AliEMCALRawUtils::Raw2Digits(AliRawReader* reader,TClonesArray *digitsArr, const AliCaloCalibPedestal* pedbadmap, TClonesArray *digitsTRG, AliEMCALTriggerData* trgData)
ee299369 286{
77eac271 287 //conversion of raw data to digits
de39a0ff 288 if(digitsArr) digitsArr->Clear("C");
92d9f317 289 if (!digitsArr) { Error("Raw2Digits", "no digits found !");return;}
290 if (!reader) {Error("Raw2Digits", "no raw reader found !");return;}
de39a0ff 291 AliEMCALTriggerSTURawStream inSTU(reader);
de39a0ff 292 AliCaloRawStreamV3 in(reader,"EMCAL",fMapping);
7643e728 293 reader->Select("EMCAL",0,43); // 43 = AliEMCALGeoParams::fgkLastAltroDDL
de39a0ff 294 fTriggerRawDigitMaker->Reset();
295 fTriggerRawDigitMaker->SetIO(reader, in, inSTU, digitsTRG, trgData);
16605c06 296 fRawAnalyzer->SetIsZeroSuppressed(true); // TMP - should use stream->IsZeroSuppressed(), or altro cfg registers later
92d9f317 297
829ba234 298 Int_t lowGain = 0;
e5bbbc4e 299 Int_t caloFlag = 0; // low, high gain, or TRU, or LED ref.
a2339fea 300
74867dce 301 Float_t bcTimePhaseCorr = 0; // for BC-based L1 phase correction
302 Int_t bcMod4 = (reader->GetBCID() % 4); // LHC uses 40 MHz, EMCal uses 10 MHz clock
303 if (bcMod4==0 || bcMod4==1) {
304 bcTimePhaseCorr = -1e-7; // subtract 100 ns for certain BC values
305 }
306
92d9f317 307 while (in.NextDDL())
308 {
92d9f317 309 while (in.NextChannel())
310 {
92d9f317 311 caloFlag = in.GetCaloFlag();
312 if (caloFlag > 2) continue; // Work with ALTRO and FALTRO
313 if(caloFlag < 2 && fRemoveBadChannels && pedbadmap->IsBadChannel(in.GetModule(),in.GetColumn(),in.GetRow()))
314 {
315 continue;
316 }
317 vector<AliCaloBunchInfo> bunchlist;
318 while (in.NextBunch())
319 {
320 bunchlist.push_back( AliCaloBunchInfo(in.GetStartTimeBin(), in.GetBunchLength(), in.GetSignals() ) );
321 }
322 if (bunchlist.size() == 0) continue;
323 if ( caloFlag < 2 )
324 { // ALTRO
325 Int_t id = fGeom->GetAbsCellIdFromCellIndexes(in.GetModule(), in.GetRow(), in.GetColumn()) ;
326 lowGain = in.IsLowGain();
327 fRawAnalyzer->SetL1Phase( in.GetL1Phase() );
328 AliCaloFitResults res = fRawAnalyzer->Evaluate( bunchlist, in.GetAltroCFG1(), in.GetAltroCFG2());
329 if(res.GetAmp() >= fNoiseThreshold )
330 {
74867dce 331 AddDigit(digitsArr, id, lowGain, res.GetAmp(), res.GetTime()+bcTimePhaseCorr, res.GetChi2(), res.GetNdf() );
92d9f317 332 }
333 }//ALTRO
334 else if(fUseFALTRO)
335 {// Fake ALTRO
336 fTriggerRawDigitMaker->Add( bunchlist );
337 }//Fake ALTRO
338 } // end while over channel
339 } //end while over DDL's, of input stream
de39a0ff 340 fTriggerRawDigitMaker->PostProcess();
f4a4dc82 341 TrimDigits(digitsArr);
ee299369 342}
343
f4a4dc82 344
f4a4dc82 345void AliEMCALRawUtils::TrimDigits(TClonesArray *digitsArr)
dab4328c 346{ // rm entries with LGnoHG (unphysical), out of time window, and too bad chi2
f4a4dc82 347 AliEMCALDigit *digit = 0;
348 Int_t n = 0;
349 Int_t nDigits = digitsArr->GetEntriesFast();
350 TIter nextdigit(digitsArr);
351 while ((digit = (AliEMCALDigit*) nextdigit())) {
f4a4dc82 352 if (digit->GetType() == AliEMCALDigit::kLGnoHG) {
92d9f317 353 AliDebug(1,Form("Remove digit with id %d, LGnoHG",digit->GetId()));
f4a4dc82 354 digitsArr->Remove(digit);
82cbdfca 355 }
f4a4dc82 356 else if(fTimeMin > digit->GetTime() || fTimeMax < digit->GetTime()) {
357 digitsArr->Remove(digit);
92d9f317 358 AliDebug(1,Form("Remove digit with id %d, Bad Time %e",digit->GetId(), digit->GetTime()));
82cbdfca 359 }
2cd0ffda 360 else if (0 > digit->GetChi2()) {
361 digitsArr->Remove(digit);
92d9f317 362 AliDebug(1,Form("Remove digit with id %d, Bad Chi2 %e",digit->GetId(), digit->GetChi2()));
2cd0ffda 363 }
f4a4dc82 364 else {
365 digit->SetIndexInList(n);
366 n++;
367 }
368 }//while
369
370 digitsArr->Compress();
371 AliDebug(1,Form("N Digits before trimming : %d; after array compression %d",nDigits,digitsArr->GetEntriesFast()));
16605c06 372}
8cb998bd 373
16605c06 374
4fe71e02 375void AliEMCALRawUtils::SetFittingAlgorithm(Int_t fitAlgo)
dab4328c 376{ // select which fitting algo should be used
48b634ff 377 delete fRawAnalyzer; // delete doesn't do anything if the pointer is 0x0
92d9f317 378 fRawAnalyzer = AliCaloRawAnalyzerFactory::CreateAnalyzer( fitAlgo );
379 fRawAnalyzer->SetNsampleCut(5); // requirement for fits to be done, for the new methods
380 fRawAnalyzer->SetOverflowCut ( OVERFLOWCUT );
381 fRawAnalyzer->SetAmpCut(fNoiseThreshold);
382 fRawAnalyzer->SetFitArrayCut(fNoiseThreshold);
4fe71e02 383}
384
385
92d9f317 386