2 /**************************************************************************
3 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
5 * Author: The ALICE Off-line Project. *
6 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
20 //_________________________________________________________________________
21 // Utility Class for handling Raw data
22 // Does all transitions from Digits to Raw and vice versa,
23 // for simu and reconstruction
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.
29 //*-- Author: Marco van Leeuwen (LBL)
30 //*-- Major refactoring by Per Thomas Hille
32 //#include "AliCDBManager.h"
33 #include "AliEMCALRawUtils.h"
35 #include "AliRunLoader.h"
36 #include "AliAltroBuffer.h"
37 #include "AliRawReader.h"
38 #include "AliCaloRawStreamV3.h"
40 #include "AliEMCALRecParam.h"
41 #include "AliEMCALLoader.h"
42 #include "AliEMCALGeometry.h"
43 #include "AliEMCALDigit.h"
44 #include "AliEMCALRawDigit.h"
46 #include "AliCaloCalibPedestal.h"
47 #include "AliCaloBunchInfo.h"
48 #include "AliCaloFitResults.h"
49 #include "AliEMCALTriggerRawDigitMaker.h"
50 #include "AliEMCALTriggerSTURawStream.h"
51 #include "AliEMCALTriggerData.h"
52 #include "AliCaloConstants.h"
53 #include "AliCaloRawAnalyzer.h"
54 #include "AliCaloRawAnalyzerFactory.h"
55 #include "AliEMCALRawResponse.h"
58 using namespace EMCAL;
61 ClassImp(AliEMCALRawUtils)
64 AliEMCALRawUtils::AliEMCALRawUtils( Algo::fitAlgorithm fitAlgo) : fNoiseThreshold(3),
68 fRemoveBadChannels(kFALSE),
69 fFittingAlgorithm(fitAlgo),
74 fTriggerRawDigitMaker(0x0)
75 { // ctor; set up fit algo etc
76 SetFittingAlgorithm(fitAlgo);
77 const TObjArray* maps = AliEMCALRecParam::GetMappings();
78 if(!maps) AliFatal("Cannot retrieve ALTRO mappings!!");
79 for(Int_t i = 0; i < 4; i++)
81 fMapping[i] = (AliAltroMapping*)maps->At(i);
84 AliRunLoader *rl = AliRunLoader::Instance();
85 if (rl && rl->GetAliRun())
87 AliEMCAL * emcal = dynamic_cast<AliEMCAL*>(rl->GetAliRun()->GetDetector("EMCAL"));
90 fGeom = emcal->GetGeometry();
94 AliDebug(1, Form("Using default geometry in raw reco"));
95 fGeom = AliEMCALGeometry::GetInstance(AliEMCALGeometry::GetDefaultGeometryName());
100 AliDebug(1, Form("Using default geometry in raw reco"));
101 fGeom = AliEMCALGeometry::GetInstance(AliEMCALGeometry::GetDefaultGeometryName());
104 if(!fGeom) AliFatal(Form("Could not get geometry!"));
105 fTriggerRawDigitMaker = new AliEMCALTriggerRawDigitMaker();
109 AliEMCALRawUtils::~AliEMCALRawUtils()
113 delete fTriggerRawDigitMaker;
117 void AliEMCALRawUtils::Digits2Raw()
119 // convert digits of the current event to raw data
120 AliRunLoader *rl = AliRunLoader::Instance();
121 AliEMCALLoader *loader = dynamic_cast<AliEMCALLoader*>(rl->GetDetectorLoader("EMCAL"));
122 loader->LoadDigits("EMCAL");
124 TClonesArray* digits = loader->Digits() ;
127 Warning("Digits2Raw", "no digits found !");
131 static const Int_t nDDL = 12*2; // 12 SM hardcoded for now. Buffers allocated dynamically, when needed, so just need an upper limit here
132 AliAltroBuffer* buffers[nDDL];
133 for (Int_t i=0; i < nDDL; i++)
136 TArrayI adcValuesLow( TIMEBINS );
137 TArrayI adcValuesHigh( TIMEBINS );
139 // loop over digits (assume ordered digits)
140 for (Int_t iDigit = 0; iDigit < digits->GetEntries(); iDigit++)
142 AliEMCALDigit* digit = dynamic_cast<AliEMCALDigit *>(digits->At(iDigit)) ;
145 AliFatal("NULL Digit");
149 if (digit->GetAmplitude() < AliEMCALRawResponse::GetRawFormatThreshold() )
160 fGeom->GetCellIndex(digit->GetId(), nSM, nModule, nIphi, nIeta);
161 fGeom->GetCellPhiEtaIndexInSModule(nSM, nModule, nIphi, nIeta,iphi, ieta) ;
163 //Check which is the RCU, 0 or 1, of the cell.
165 if (0<=iphi&&iphi<8) iRCU=0; // first cable row
166 else if (8<=iphi&&iphi<16 && 0<=ieta&&ieta<24) iRCU=0; // first half;
167 else if(8<=iphi&&iphi<16 && 24<=ieta&&ieta<48) iRCU=1; // second half;
169 else if(16<=iphi&&iphi<24) iRCU=1; // third cable row
171 if (nSM%2==1) iRCU = 1 - iRCU; // swap for odd=C side, to allow us to cable both sides the same
174 Fatal("Digits2Raw()","Non-existent RCU number: %d", iRCU);
177 Int_t iDDL = NRCUSPERMODULE*nSM + iRCU;
178 if (iDDL < 0 || iDDL >= nDDL){
179 Fatal("Digits2Raw()","Non-existent DDL board number: %d", iDDL);
182 if (buffers[iDDL] == 0)
184 // open new file and write dummy header
185 TString fileName = AliDAQ::DdlFileName("EMCAL",iDDL);
186 //Select mapping file RCU0A, RCU0C, RCU1A, RCU1C
187 Int_t iRCUside=iRCU+(nSM%2)*2;
188 //iRCU=0 and even (0) SM -> RCU0A.data 0
189 //iRCU=1 and even (0) SM -> RCU1A.data 1
190 //iRCU=0 and odd (1) SM -> RCU0C.data 2
191 //iRCU=1 and odd (1) SM -> RCU1C.data 3
192 buffers[iDDL] = new AliAltroBuffer(fileName.Data(),fMapping[iRCUside]);
193 buffers[iDDL]->WriteDataHeader(kTRUE, kFALSE); //Dummy;
196 // out of time range signal (?)
197 if (digit->GetTimeR() > TIMEBINMAX ) {
198 AliInfo("Signal is out of time range.\n");
199 buffers[iDDL]->FillBuffer((Int_t)digit->GetAmplitude());
200 buffers[iDDL]->FillBuffer( TIMEBINS ); // time bin
201 buffers[iDDL]->FillBuffer(3); // bunch length
202 buffers[iDDL]->WriteTrailer(3, ieta, iphi, nSM); // trailer
203 // calculate the time response function
205 Bool_t lowgain = AliEMCALRawResponse::RawSampledResponse(digit->GetTimeR(), digit->GetAmplitude(),
206 adcValuesHigh.GetArray(), adcValuesLow.GetArray()) ;
209 buffers[iDDL]->WriteChannel(ieta, iphi, 0, TIMEBINS, adcValuesLow.GetArray(), AliEMCALRawResponse::GetRawFormatThreshold() );
211 buffers[iDDL]->WriteChannel(ieta,iphi, 1, TIMEBINS, adcValuesHigh.GetArray(), AliEMCALRawResponse::GetRawFormatThreshold() );
213 }// iDDL under the limits
217 // write headers and close files
218 for (Int_t i=0; i < nDDL; i++) {
221 buffers[i]->WriteDataHeader(kFALSE, kFALSE);
226 loader->UnloadDigits();
231 void AliEMCALRawUtils::AddDigit(TClonesArray *digitsArr, Int_t id, Int_t lowGain, Float_t amp, Float_t time, Float_t chi2, Int_t ndf)
234 AliEMCALDigit *digit = 0, *tmpdigit = 0;
235 TIter nextdigit(digitsArr);
237 while (digit == 0 && (tmpdigit = (AliEMCALDigit*) nextdigit()))
239 if (tmpdigit->GetId() == id) digit = tmpdigit;
242 if (!digit) { // no digit existed for this tower; create one
243 Int_t type = AliEMCALDigit::kHG; // use enum in AliEMCALDigit
247 type = AliEMCALDigit::kLGnoHG;
250 Int_t idigit = digitsArr->GetEntries();
251 new((*digitsArr)[idigit]) AliEMCALDigit( -1, -1, id, amp, time, type, idigit, chi2, ndf);
252 AliDebug(2,Form("Add digit Id %d for the first time, type %d", id, type));
253 }//digit added first time
255 { // a digit already exists, check range
256 // (use high gain if signal < cut value, otherwise low gain)
258 { // new digit is low gain
259 if (digit->GetAmplitude() > OVERFLOWCUT )
260 { // use if previously stored (HG) digit is out of range
261 digit->SetAmplitude( HGLGFACTOR * amp);
262 digit->SetTime(time);
263 digit->SetType(AliEMCALDigit::kLG);
264 AliDebug(2,Form("Add LG digit ID %d for the second time, type %d", digit->GetId(), digit->GetType()));
266 }//new low gain digit
267 else { // new digit is high gain
269 if (amp < OVERFLOWCUT )
270 { // new digit is high gain; use if not out of range
271 digit->SetAmplitude(amp);
272 digit->SetTime(time);
273 digit->SetType(AliEMCALDigit::kHG);
274 AliDebug(2,Form("Add HG digit ID %d for the second time, type %d", digit->GetId(), digit->GetType()));
277 { // HG out of range, just change flag value to show that HG did exist
278 digit->SetType(AliEMCALDigit::kLG);
279 AliDebug(2,Form("Change LG digit to HG, ID %d, type %d", digit->GetId(), digit->GetType()));
281 }//new high gain digit
282 }//digit existed replace it
286 void AliEMCALRawUtils::Raw2Digits(AliRawReader* reader,TClonesArray *digitsArr, const AliCaloCalibPedestal* pedbadmap, TClonesArray *digitsTRG, AliEMCALTriggerData* trgData)
288 //conversion of raw data to digits
289 if(digitsArr) digitsArr->Clear("C");
290 if (!digitsArr) { Error("Raw2Digits", "no digits found !");return;}
291 if (!reader) {Error("Raw2Digits", "no raw reader found !");return;}
292 AliEMCALTriggerSTURawStream inSTU(reader);
293 AliCaloRawStreamV3 in(reader,"EMCAL",fMapping);
294 reader->Select("EMCAL",0,43); // 43 = AliEMCALGeoParams::fgkLastAltroDDL
295 fTriggerRawDigitMaker->Reset();
296 fTriggerRawDigitMaker->SetIO(reader, in, inSTU, digitsTRG, trgData);
297 fRawAnalyzer->SetIsZeroSuppressed(true); // TMP - should use stream->IsZeroSuppressed(), or altro cfg registers later
300 Int_t caloFlag = 0; // low, high gain, or TRU, or LED ref.
302 Float_t bcTimePhaseCorr = 0; // for BC-based L1 phase correction
303 Int_t bcMod4 = (reader->GetBCID() % 4); // LHC uses 40 MHz, EMCal uses 10 MHz clock
305 //AliCDBManager* man = AliCDBManager::Instance();
306 //Int_t runNumber = man->GetRun();
308 Int_t runNumber = reader->GetRunNumber();
310 if ((runNumber >130850 ) && (bcMod4==0 || bcMod4==1))
311 bcTimePhaseCorr = -1e-7; // subtract 100 ns for certain BC values
315 while (in.NextChannel())
317 caloFlag = in.GetCaloFlag();
318 if (caloFlag > 2) continue; // Work with ALTRO and FALTRO
319 if(caloFlag < 2 && fRemoveBadChannels && pedbadmap->IsBadChannel(in.GetModule(),in.GetColumn(),in.GetRow()))
323 vector<AliCaloBunchInfo> bunchlist;
324 while (in.NextBunch())
326 bunchlist.push_back( AliCaloBunchInfo(in.GetStartTimeBin(), in.GetBunchLength(), in.GetSignals() ) );
328 if (bunchlist.size() == 0) continue;
331 Int_t id = fGeom->GetAbsCellIdFromCellIndexes(in.GetModule(), in.GetRow(), in.GetColumn()) ;
332 lowGain = in.IsLowGain();
333 fRawAnalyzer->SetL1Phase( in.GetL1Phase() );
334 AliCaloFitResults res = fRawAnalyzer->Evaluate( bunchlist, in.GetAltroCFG1(), in.GetAltroCFG2());
335 if(res.GetAmp() >= fNoiseThreshold )
337 AddDigit(digitsArr, id, lowGain, res.GetAmp(), res.GetTime()+bcTimePhaseCorr, res.GetChi2(), res.GetNdf() );
342 fTriggerRawDigitMaker->Add( bunchlist );
344 } // end while over channel
345 } //end while over DDL's, of input stream
346 fTriggerRawDigitMaker->PostProcess();
347 TrimDigits(digitsArr);
351 void AliEMCALRawUtils::TrimDigits(TClonesArray *digitsArr)
352 { // rm entries with LGnoHG (unphysical), out of time window, and too bad chi2
353 AliEMCALDigit *digit = 0;
355 Int_t nDigits = digitsArr->GetEntriesFast();
356 TIter nextdigit(digitsArr);
357 while ((digit = (AliEMCALDigit*) nextdigit())) {
358 if (digit->GetType() == AliEMCALDigit::kLGnoHG) {
359 AliDebug(1,Form("Remove digit with id %d, LGnoHG",digit->GetId()));
360 digitsArr->Remove(digit);
362 else if(fTimeMin > digit->GetTime() || fTimeMax < digit->GetTime()) {
363 digitsArr->Remove(digit);
364 AliDebug(1,Form("Remove digit with id %d, Bad Time %e",digit->GetId(), digit->GetTime()));
366 else if (0 > digit->GetChi2()) {
367 digitsArr->Remove(digit);
368 AliDebug(1,Form("Remove digit with id %d, Bad Chi2 %e",digit->GetId(), digit->GetChi2()));
371 digit->SetIndexInList(n);
376 digitsArr->Compress();
377 AliDebug(1,Form("N Digits before trimming : %d; after array compression %d",nDigits,digitsArr->GetEntriesFast()));
381 void AliEMCALRawUtils::SetFittingAlgorithm(Int_t fitAlgo)
382 { // select which fitting algo should be used
383 delete fRawAnalyzer; // delete doesn't do anything if the pointer is 0x0
384 fRawAnalyzer = AliCaloRawAnalyzerFactory::CreateAnalyzer( fitAlgo );
385 fRawAnalyzer->SetNsampleCut(5); // requirement for fits to be done, for the new methods
386 fRawAnalyzer->SetOverflowCut ( OVERFLOWCUT );
387 fRawAnalyzer->SetAmpCut(fNoiseThreshold);
388 fRawAnalyzer->SetFitArrayCut(fNoiseThreshold);