]> git.uio.no Git - u/mrichter/AliRoot.git/blame - EMCAL/AliEMCALRawUtils.cxx
Simplifications from S.Ailo
[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
92d9f317 59ClassImp(AliEMCALRawUtils)
60
b4133f05 61
92d9f317 62AliEMCALRawUtils::AliEMCALRawUtils( Algo::fitAlgorithm fitAlgo) : fNoiseThreshold(3),
63 fNPedSamples(4),
64 fGeom(0),
65 fOption(""),
66 fRemoveBadChannels(kFALSE),
77eac271 67 fFittingAlgorithm(fitAlgo),
92d9f317 68 fTimeMin(-1.),
69 fTimeMax(1.),
70 fUseFALTRO(kTRUE),
71 fRawAnalyzer(0),
72 fTriggerRawDigitMaker(0x0)
73{
4fe71e02 74 SetFittingAlgorithm(fitAlgo);
65bdc82f 75 const TObjArray* maps = AliEMCALRecParam::GetMappings();
76 if(!maps) AliFatal("Cannot retrieve ALTRO mappings!!");
77eac271 77 for(Int_t i = 0; i < 4; i++)
78 {
79 fMapping[i] = (AliAltroMapping*)maps->At(i);
80 }
81
33c3c91a 82 AliRunLoader *rl = AliRunLoader::Instance();
77eac271 83 if (rl && rl->GetAliRun())
84 {
a2339fea 85 AliEMCAL * emcal = dynamic_cast<AliEMCAL*>(rl->GetAliRun()->GetDetector("EMCAL"));
77eac271 86 if(emcal)
87 {
88 fGeom = emcal->GetGeometry();
89 }
90 else
91 {
92 AliDebug(1, Form("Using default geometry in raw reco"));
93 fGeom = AliEMCALGeometry::GetInstance(AliEMCALGeometry::GetDefaultGeometryName());
94 }
95 }
96 else
97 {
a2339fea 98 AliDebug(1, Form("Using default geometry in raw reco"));
99 fGeom = AliEMCALGeometry::GetInstance(AliEMCALGeometry::GetDefaultGeometryName());
100 }
92d9f317 101
5544799a 102 if(!fGeom) AliFatal(Form("Could not get geometry!"));
77eac271 103 fTriggerRawDigitMaker = new AliEMCALTriggerRawDigitMaker();
5544799a 104}
105
65bdc82f 106
92d9f317 107AliEMCALRawUtils::~AliEMCALRawUtils()
65bdc82f 108{
e5bbbc4e 109 //dtor
ee299369 110}
65bdc82f 111
92d9f317 112
65bdc82f 113void AliEMCALRawUtils::Digits2Raw()
ee299369 114{
115 // convert digits of the current event to raw data
33c3c91a 116 AliRunLoader *rl = AliRunLoader::Instance();
ee299369 117 AliEMCALLoader *loader = dynamic_cast<AliEMCALLoader*>(rl->GetDetectorLoader("EMCAL"));
ee299369 118 loader->LoadDigits("EMCAL");
119 loader->GetEvent();
120 TClonesArray* digits = loader->Digits() ;
121
122 if (!digits) {
123 Warning("Digits2Raw", "no digits found !");
124 return;
125 }
a2339fea 126
ee299369 127 static const Int_t nDDL = 12*2; // 12 SM hardcoded for now. Buffers allocated dynamically, when needed, so just need an upper limit here
128 AliAltroBuffer* buffers[nDDL];
129 for (Int_t i=0; i < nDDL; i++)
130 buffers[i] = 0;
a2339fea 131
92d9f317 132 TArrayI adcValuesLow( TIMEBINS );
133 TArrayI adcValuesHigh( TIMEBINS );
de39a0ff 134
ee299369 135 // loop over digits (assume ordered digits)
92d9f317 136 for (Int_t iDigit = 0; iDigit < digits->GetEntries(); iDigit++)
137 {
138 AliEMCALDigit* digit = dynamic_cast<AliEMCALDigit *>(digits->At(iDigit)) ;
139 if(!digit)
140 {
141 AliFatal("NULL Digit");
142 }
143 else
144 {
1f847d9f 145 if (digit->GetAmplitude() < AliEMCALRawResponse::GetRawFormatThreshold() )
92d9f317 146 {
147 continue;
148 }
149 //get cell indices
150 Int_t nSM = 0;
151 Int_t nIphi = 0;
152 Int_t nIeta = 0;
153 Int_t iphi = 0;
154 Int_t ieta = 0;
155 Int_t nModule = 0;
156 fGeom->GetCellIndex(digit->GetId(), nSM, nModule, nIphi, nIeta);
157 fGeom->GetCellPhiEtaIndexInSModule(nSM, nModule, nIphi, nIeta,iphi, ieta) ;
de39a0ff 158
92d9f317 159 //Check which is the RCU, 0 or 1, of the cell.
160 Int_t iRCU = -111;
77eac271 161 if (0<=iphi&&iphi<8) iRCU=0; // first cable row
162 else if (8<=iphi&&iphi<16 && 0<=ieta&&ieta<24) iRCU=0; // first half;
163 else if(8<=iphi&&iphi<16 && 24<=ieta&&ieta<48) iRCU=1; // second half;
164 //second cable row
165 else if(16<=iphi&&iphi<24) iRCU=1; // third cable row
166
167 if (nSM%2==1) iRCU = 1 - iRCU; // swap for odd=C side, to allow us to cable both sides the same
168
169 if (iRCU<0)
170 Fatal("Digits2Raw()","Non-existent RCU number: %d", iRCU);
de39a0ff 171
77eac271 172 //Which DDL?
173 Int_t iDDL = NRCUSPERMODULE*nSM + iRCU;
174 if (iDDL < 0 || iDDL >= nDDL){
175 Fatal("Digits2Raw()","Non-existent DDL board number: %d", iDDL);
176 }
177 else{
178 if (buffers[iDDL] == 0)
179 {
180 // open new file and write dummy header
181 TString fileName = AliDAQ::DdlFileName("EMCAL",iDDL);
182 //Select mapping file RCU0A, RCU0C, RCU1A, RCU1C
a2339fea 183 Int_t iRCUside=iRCU+(nSM%2)*2;
184 //iRCU=0 and even (0) SM -> RCU0A.data 0
185 //iRCU=1 and even (0) SM -> RCU1A.data 1
186 //iRCU=0 and odd (1) SM -> RCU0C.data 2
187 //iRCU=1 and odd (1) SM -> RCU1C.data 3
77eac271 188 buffers[iDDL] = new AliAltroBuffer(fileName.Data(),fMapping[iRCUside]);
a2339fea 189 buffers[iDDL]->WriteDataHeader(kTRUE, kFALSE); //Dummy;
190 }
191
192 // out of time range signal (?)
92d9f317 193 if (digit->GetTimeR() > TIMEBINMAX ) {
a2339fea 194 AliInfo("Signal is out of time range.\n");
195 buffers[iDDL]->FillBuffer((Int_t)digit->GetAmplitude());
92d9f317 196 buffers[iDDL]->FillBuffer( TIMEBINS ); // time bin
a2339fea 197 buffers[iDDL]->FillBuffer(3); // bunch length
198 buffers[iDDL]->WriteTrailer(3, ieta, iphi, nSM); // trailer
199 // calculate the time response function
200 } else {
1f847d9f 201 Bool_t lowgain = AliEMCALRawResponse::RawSampledResponse(digit->GetTimeR(), digit->GetAmplitude(),
202 adcValuesHigh.GetArray(), adcValuesLow.GetArray()) ;
203
204 if (lowgain)
205 buffers[iDDL]->WriteChannel(ieta, iphi, 0, TIMEBINS, adcValuesLow.GetArray(), AliEMCALRawResponse::GetRawFormatThreshold() );
a2339fea 206 else
1f847d9f 207 buffers[iDDL]->WriteChannel(ieta,iphi, 1, TIMEBINS, adcValuesHigh.GetArray(), AliEMCALRawResponse::GetRawFormatThreshold() );
a2339fea 208 }
209 }// iDDL under the limits
de39a0ff 210 }//digit exists
211 }//Digit loop
a2339fea 212
ee299369 213 // write headers and close files
214 for (Int_t i=0; i < nDDL; i++) {
215 if (buffers[i]) {
216 buffers[i]->Flush();
217 buffers[i]->WriteDataHeader(kFALSE, kFALSE);
218 delete buffers[i];
219 }
220 }
de39a0ff 221
ee299369 222 loader->UnloadDigits();
223}
224
92d9f317 225
77eac271 226
92d9f317 227void AliEMCALRawUtils::AddDigit(TClonesArray *digitsArr, Int_t id, Int_t lowGain, Float_t amp, Float_t time, Float_t chi2, Int_t ndf)
228{
77eac271 229 // comment
92d9f317 230 AliEMCALDigit *digit = 0, *tmpdigit = 0;
231 TIter nextdigit(digitsArr);
232
233 while (digit == 0 && (tmpdigit = (AliEMCALDigit*) nextdigit()))
234 {
235 if (tmpdigit->GetId() == id) digit = tmpdigit;
236 }
237
238 if (!digit) { // no digit existed for this tower; create one
239 Int_t type = AliEMCALDigit::kHG; // use enum in AliEMCALDigit
240 if (lowGain)
241 {
242 amp *= HGLGFACTOR;
243 type = AliEMCALDigit::kLGnoHG;
244 }
245
246 Int_t idigit = digitsArr->GetEntries();
247 new((*digitsArr)[idigit]) AliEMCALDigit( -1, -1, id, amp, time, type, idigit, chi2, ndf);
248 AliDebug(2,Form("Add digit Id %d for the first time, type %d", id, type));
249 }//digit added first time
250 else
251 { // a digit already exists, check range
252 // (use high gain if signal < cut value, otherwise low gain)
253 if (lowGain)
254 { // new digit is low gain
255 if (digit->GetAmplitude() > OVERFLOWCUT )
256 { // use if previously stored (HG) digit is out of range
257 digit->SetAmplitude( HGLGFACTOR * amp);
258 digit->SetTime(time);
259 digit->SetType(AliEMCALDigit::kLG);
260 AliDebug(2,Form("Add LG digit ID %d for the second time, type %d", digit->GetId(), digit->GetType()));
261 }
262 }//new low gain digit
263 else { // new digit is high gain
264
265 if (amp < OVERFLOWCUT )
266 { // new digit is high gain; use if not out of range
267 digit->SetAmplitude(amp);
268 digit->SetTime(time);
269 digit->SetType(AliEMCALDigit::kHG);
270 AliDebug(2,Form("Add HG digit ID %d for the second time, type %d", digit->GetId(), digit->GetType()));
271 }
272 else
273 { // HG out of range, just change flag value to show that HG did exist
274 digit->SetType(AliEMCALDigit::kLG);
275 AliDebug(2,Form("Change LG digit to HG, ID %d, type %d", digit->GetId(), digit->GetType()));
276 }
277 }//new high gain digit
278 }//digit existed replace it
279}
280
281
de39a0ff 282void AliEMCALRawUtils::Raw2Digits(AliRawReader* reader,TClonesArray *digitsArr, const AliCaloCalibPedestal* pedbadmap, TClonesArray *digitsTRG, AliEMCALTriggerData* trgData)
ee299369 283{
77eac271 284 //conversion of raw data to digits
de39a0ff 285 if(digitsArr) digitsArr->Clear("C");
92d9f317 286 if (!digitsArr) { Error("Raw2Digits", "no digits found !");return;}
287 if (!reader) {Error("Raw2Digits", "no raw reader found !");return;}
de39a0ff 288 AliEMCALTriggerSTURawStream inSTU(reader);
de39a0ff 289 AliCaloRawStreamV3 in(reader,"EMCAL",fMapping);
7643e728 290 reader->Select("EMCAL",0,43); // 43 = AliEMCALGeoParams::fgkLastAltroDDL
de39a0ff 291 fTriggerRawDigitMaker->Reset();
292 fTriggerRawDigitMaker->SetIO(reader, in, inSTU, digitsTRG, trgData);
16605c06 293 fRawAnalyzer->SetIsZeroSuppressed(true); // TMP - should use stream->IsZeroSuppressed(), or altro cfg registers later
92d9f317 294
829ba234 295 Int_t lowGain = 0;
e5bbbc4e 296 Int_t caloFlag = 0; // low, high gain, or TRU, or LED ref.
a2339fea 297
74867dce 298 Float_t bcTimePhaseCorr = 0; // for BC-based L1 phase correction
299 Int_t bcMod4 = (reader->GetBCID() % 4); // LHC uses 40 MHz, EMCal uses 10 MHz clock
300 if (bcMod4==0 || bcMod4==1) {
301 bcTimePhaseCorr = -1e-7; // subtract 100 ns for certain BC values
302 }
303
92d9f317 304 while (in.NextDDL())
305 {
92d9f317 306 while (in.NextChannel())
307 {
92d9f317 308 caloFlag = in.GetCaloFlag();
309 if (caloFlag > 2) continue; // Work with ALTRO and FALTRO
310 if(caloFlag < 2 && fRemoveBadChannels && pedbadmap->IsBadChannel(in.GetModule(),in.GetColumn(),in.GetRow()))
311 {
312 continue;
313 }
314 vector<AliCaloBunchInfo> bunchlist;
315 while (in.NextBunch())
316 {
317 bunchlist.push_back( AliCaloBunchInfo(in.GetStartTimeBin(), in.GetBunchLength(), in.GetSignals() ) );
318 }
319 if (bunchlist.size() == 0) continue;
320 if ( caloFlag < 2 )
321 { // ALTRO
322 Int_t id = fGeom->GetAbsCellIdFromCellIndexes(in.GetModule(), in.GetRow(), in.GetColumn()) ;
323 lowGain = in.IsLowGain();
324 fRawAnalyzer->SetL1Phase( in.GetL1Phase() );
325 AliCaloFitResults res = fRawAnalyzer->Evaluate( bunchlist, in.GetAltroCFG1(), in.GetAltroCFG2());
326 if(res.GetAmp() >= fNoiseThreshold )
327 {
74867dce 328 AddDigit(digitsArr, id, lowGain, res.GetAmp(), res.GetTime()+bcTimePhaseCorr, res.GetChi2(), res.GetNdf() );
92d9f317 329 }
330 }//ALTRO
331 else if(fUseFALTRO)
332 {// Fake ALTRO
333 fTriggerRawDigitMaker->Add( bunchlist );
334 }//Fake ALTRO
335 } // end while over channel
336 } //end while over DDL's, of input stream
de39a0ff 337 fTriggerRawDigitMaker->PostProcess();
f4a4dc82 338 TrimDigits(digitsArr);
ee299369 339}
340
f4a4dc82 341
f4a4dc82 342void AliEMCALRawUtils::TrimDigits(TClonesArray *digitsArr)
343{
f4a4dc82 344 AliEMCALDigit *digit = 0;
345 Int_t n = 0;
346 Int_t nDigits = digitsArr->GetEntriesFast();
347 TIter nextdigit(digitsArr);
348 while ((digit = (AliEMCALDigit*) nextdigit())) {
f4a4dc82 349 if (digit->GetType() == AliEMCALDigit::kLGnoHG) {
92d9f317 350 AliDebug(1,Form("Remove digit with id %d, LGnoHG",digit->GetId()));
f4a4dc82 351 digitsArr->Remove(digit);
82cbdfca 352 }
f4a4dc82 353 else if(fTimeMin > digit->GetTime() || fTimeMax < digit->GetTime()) {
354 digitsArr->Remove(digit);
92d9f317 355 AliDebug(1,Form("Remove digit with id %d, Bad Time %e",digit->GetId(), digit->GetTime()));
82cbdfca 356 }
2cd0ffda 357 else if (0 > digit->GetChi2()) {
358 digitsArr->Remove(digit);
92d9f317 359 AliDebug(1,Form("Remove digit with id %d, Bad Chi2 %e",digit->GetId(), digit->GetChi2()));
2cd0ffda 360 }
f4a4dc82 361 else {
362 digit->SetIndexInList(n);
363 n++;
364 }
365 }//while
366
367 digitsArr->Compress();
368 AliDebug(1,Form("N Digits before trimming : %d; after array compression %d",nDigits,digitsArr->GetEntriesFast()));
16605c06 369}
8cb998bd 370
16605c06 371
4fe71e02 372void AliEMCALRawUtils::SetFittingAlgorithm(Int_t fitAlgo)
373{
92d9f317 374 fRawAnalyzer = AliCaloRawAnalyzerFactory::CreateAnalyzer( fitAlgo );
375 fRawAnalyzer->SetNsampleCut(5); // requirement for fits to be done, for the new methods
376 fRawAnalyzer->SetOverflowCut ( OVERFLOWCUT );
377 fRawAnalyzer->SetAmpCut(fNoiseThreshold);
378 fRawAnalyzer->SetFitArrayCut(fNoiseThreshold);
4fe71e02 379}
380
381
92d9f317 382