1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
17 /* History of cvs commits:
20 * Revision 1.10 2007/12/06 13:58:11 hristov
21 * Additional pritection. Do not delete the mapping, it is owned by another class
23 * Revision 1.9 2007/12/06 02:19:51 jklay
24 * incorporated fitting procedure from testbeam analysis into AliRoot
26 * Revision 1.8 2007/12/05 02:30:51 jklay
27 * modification to read Altro mappings into AliEMCALRecParam and pass to AliEMCALRawUtils from AliEMCALReconstructor; add option to AliEMCALRawUtils to set old RCU format (for testbeam) or not
29 * Revision 1.7 2007/11/14 15:51:46 gustavo
30 * Take out few unnecessary prints
32 * Revision 1.6 2007/11/01 01:23:51 mvl
33 * Removed call to SetOldRCUFormat, which is only needed for testbeam data
35 * Revision 1.5 2007/11/01 01:20:33 mvl
36 * Further improvement of peak finding; more robust fit
38 * Revision 1.4 2007/10/31 17:15:24 mvl
39 * Fixed bug in raw data unpacking; Added pedestal to signal fit; Added logic to deal with high/low gain
41 * Revision 1.3 2007/09/27 08:36:46 mvl
42 * More robust setting of fit range in FitRawSignal (P. Hristov)
44 * Revision 1.2 2007/09/03 20:55:35 jklay
45 * EMCAL e-by-e reconstruction methods from Cvetan
47 * Revision 1.1 2007/03/17 19:56:38 mvl
48 * Moved signal shape routines from AliEMCAL to separate class AliEMCALRawUtils to streamline raw data reconstruction code.
51 //*-- Author: Marco van Leeuwen (LBL)
52 #include "AliEMCALRawUtils.h"
60 #include "AliRunLoader.h"
61 #include "AliCaloAltroMapping.h"
62 #include "AliAltroBuffer.h"
63 #include "AliRawReader.h"
64 #include "AliCaloRawStream.h"
67 #include "AliEMCALRecParam.h"
68 #include "AliEMCALLoader.h"
69 #include "AliEMCALGeometry.h"
70 #include "AliEMCALDigitizer.h"
71 #include "AliEMCALDigit.h"
74 ClassImp(AliEMCALRawUtils)
76 // Signal shape parameters
77 Double_t AliEMCALRawUtils::fgTimeBinWidth = 100E-9 ; // each sample is 100 ns
78 Double_t AliEMCALRawUtils::fgTimeTrigger = 1.5E-6 ; // 15 time bins ~ 1.5 musec
80 // some digitization constants
81 Int_t AliEMCALRawUtils::fgThreshold = 1;
82 Int_t AliEMCALRawUtils::fgDDLPerSuperModule = 2; // 2 ddls per SuperModule
83 Int_t AliEMCALRawUtils::fgPedestalValue = 32; // pedestal value for digits2raw
84 Double_t AliEMCALRawUtils::fgFEENoise = 3.; // 3 ADC channels of noise (sampled)
86 AliEMCALRawUtils::AliEMCALRawUtils()
87 : fHighLowGainFactor(0.), fOrder(0), fTau(0.), fNoiseThreshold(0),
88 fNPedSamples(0), fGeom(0), fOption("")
91 //These are default parameters.
92 //Can be re-set from without with setter functions
93 fHighLowGainFactor = 16. ; // adjusted for a low gain range of 82 GeV (10 bits)
94 fOrder = 2; // order of gamma fn
95 fTau = 2.35; // in units of timebin, from CERN 2007 testbeam
99 //Get Mapping RCU files from the AliEMCALRecParam
100 const TObjArray* maps = AliEMCALRecParam::GetMappings();
101 if(!maps) AliFatal("Cannot retrieve ALTRO mappings!!");
103 for(Int_t i = 0; i < 2; i++) {
104 fMapping[i] = (AliAltroMapping*)maps->At(i);
107 //To make sure we match with the geometry in a simulation file,
108 //let's try to get it first. If not, take the default geometry
109 AliRunLoader *rl = AliRunLoader::GetRunLoader();
110 if (rl->GetAliRun() && rl->GetAliRun()->GetDetector("EMCAL")) {
111 fGeom = dynamic_cast<AliEMCAL*>(rl->GetAliRun()->GetDetector("EMCAL"))->GetGeometry();
113 AliInfo(Form("Using default geometry in raw reco"));
114 fGeom = AliEMCALGeometry::GetInstance(AliEMCALGeometry::GetDefaultGeometryName());
117 if(!fGeom) AliFatal(Form("Could not get geometry!"));
121 //____________________________________________________________________________
122 AliEMCALRawUtils::AliEMCALRawUtils(const AliEMCALRawUtils& rawU)
124 fHighLowGainFactor(rawU.fHighLowGainFactor),
127 fNoiseThreshold(rawU.fNoiseThreshold),
128 fNPedSamples(rawU.fNPedSamples),
130 fOption(rawU.fOption)
133 fMapping[0] = rawU.fMapping[0];
134 fMapping[1] = rawU.fMapping[1];
137 //____________________________________________________________________________
138 AliEMCALRawUtils& AliEMCALRawUtils::operator =(const AliEMCALRawUtils &rawU)
140 //assignment operator
143 fHighLowGainFactor = rawU.fHighLowGainFactor;
144 fOrder = rawU.fOrder;
146 fNoiseThreshold = rawU.fNoiseThreshold;
147 fNPedSamples = rawU.fNPedSamples;
149 fOption = rawU.fOption;
150 fMapping[0] = rawU.fMapping[0];
151 fMapping[1] = rawU.fMapping[1];
158 //____________________________________________________________________________
159 AliEMCALRawUtils::~AliEMCALRawUtils() {
163 //____________________________________________________________________________
164 void AliEMCALRawUtils::Digits2Raw()
166 // convert digits of the current event to raw data
168 AliRunLoader *rl = AliRunLoader::GetRunLoader();
169 AliEMCALLoader *loader = dynamic_cast<AliEMCALLoader*>(rl->GetDetectorLoader("EMCAL"));
172 loader->LoadDigits("EMCAL");
174 TClonesArray* digits = loader->Digits() ;
177 Warning("Digits2Raw", "no digits found !");
181 static const Int_t nDDL = 12*2; // 12 SM hardcoded for now. Buffers allocated dynamically, when needed, so just need an upper limit here
182 AliAltroBuffer* buffers[nDDL];
183 for (Int_t i=0; i < nDDL; i++)
186 Int_t adcValuesLow[fgkTimeBins];
187 Int_t adcValuesHigh[fgkTimeBins];
189 // loop over digits (assume ordered digits)
190 for (Int_t iDigit = 0; iDigit < digits->GetEntries(); iDigit++) {
191 AliEMCALDigit* digit = dynamic_cast<AliEMCALDigit *>(digits->At(iDigit)) ;
192 if (digit->GetAmp() < fgThreshold)
202 fGeom->GetCellIndex(digit->GetId(), nSM, nModule, nIphi, nIeta);
203 fGeom->GetCellPhiEtaIndexInSModule(nSM, nModule, nIphi, nIeta,iphi, ieta) ;
205 //Check which is the RCU of the cell.
208 if (0<=iphi&&iphi<8) iRCU=0; // first cable row
209 else if (8<=iphi&&iphi<16 && 0<=ieta&&ieta<24) iRCU=0; // first half;
212 else if(8<=iphi&&iphi<16 && 24<=ieta&&ieta<48) iRCU=1; // second half;
214 else if(16<=iphi&&iphi<24) iRCU=1; // third cable row
216 Fatal("Digits2Raw()","Non-existent RCU number: %d", iRCU);
219 Int_t iDDL = fgDDLPerSuperModule* nSM + iRCU;
221 Fatal("Digits2Raw()","Non-existent DDL board number: %d", iDDL);
223 if (buffers[iDDL] == 0) {
224 // open new file and write dummy header
225 TString fileName = AliDAQ::DdlFileName("EMCAL",iDDL);
226 buffers[iDDL] = new AliAltroBuffer(fileName.Data(),fMapping[iRCU]);
227 buffers[iDDL]->WriteDataHeader(kTRUE, kFALSE); //Dummy;
230 // out of time range signal (?)
231 if (digit->GetTimeR() > GetRawFormatTimeMax() ) {
232 AliInfo("Signal is out of time range.\n");
233 buffers[iDDL]->FillBuffer((Int_t)digit->GetAmp());
234 buffers[iDDL]->FillBuffer(GetRawFormatTimeBins() ); // time bin
235 buffers[iDDL]->FillBuffer(3); // bunch length
236 buffers[iDDL]->WriteTrailer(3, ieta, iphi, nSM); // trailer
237 // calculate the time response function
239 Bool_t lowgain = RawSampledResponse(digit->GetTimeR(), digit->GetAmp(), adcValuesHigh, adcValuesLow) ;
241 buffers[iDDL]->WriteChannel(ieta, iphi, 0, GetRawFormatTimeBins(), adcValuesLow, fgThreshold);
243 buffers[iDDL]->WriteChannel(ieta,iphi, 1, GetRawFormatTimeBins(), adcValuesHigh, fgThreshold);
247 // write headers and close files
248 for (Int_t i=0; i < nDDL; i++) {
251 buffers[i]->WriteDataHeader(kFALSE, kFALSE);
256 loader->UnloadDigits();
259 //____________________________________________________________________________
260 void AliEMCALRawUtils::Raw2Digits(AliRawReader* reader,TClonesArray *digitsArr)
262 // convert raw data of the current event to digits
267 Error("Raw2Digits", "no digits found !");
271 Error("Raw2Digits", "no raw reader found !");
275 AliCaloRawStream in(reader,"EMCAL",fMapping);
276 // Select EMCAL DDL's;
277 reader->Select("EMCAL");
279 TString option = GetOption();
280 if (option.Contains("OldRCUFormat"))
281 in.SetOldRCUFormat(kTRUE); // Needed for testbeam data
283 in.SetOldRCUFormat(kFALSE);
286 //Updated fitting routine from 2007 beam test takes into account
287 //possibility of two peaks in data and selects first one for fitting
288 //Also sets some of the starting parameters based on the shape of the
289 //given raw signal being fit
291 TF1 * signalF = new TF1("signal", RawResponseFunction, 0, GetRawFormatTimeBins(), 5);
292 signalF->SetParameters(10.,0.,fTau,fOrder,5.); //set all defaults once, just to be safe
293 signalF->SetParNames("amp","t0","tau","N","ped");
294 signalF->SetParameter(2,fTau); // tau in units of time bin
295 signalF->SetParLimits(2,2,-1);
296 signalF->SetParameter(3,fOrder); // order
297 signalF->SetParLimits(3,2,-1);
303 //Graph to hold data we will fit (should be converted to an array
304 //later to speed up processing
305 TGraph * gSig = new TGraph(GetRawFormatTimeBins());
310 while (readOk && in.GetModule() < 0)
311 readOk = in.Next(); // Go to first digit
317 id = fGeom->GetAbsCellIdFromCellIndexes(in.GetModule(), in.GetRow(), in.GetColumn()) ;
318 lowGain = in.IsLowGain();
319 Int_t maxTime = in.GetTime(); // timebins come in reverse order
320 if (maxTime < 0 || maxTime >= GetRawFormatTimeBins()) {
321 AliWarning(Form("Invalid time bin %d",maxTime));
322 maxTime = GetRawFormatTimeBins();
324 gSig->Set(maxTime+1);
325 // There is some kind of zero-suppression in the raw data,
326 // so set up the TGraph in advance
327 for (Int_t i=0; i < maxTime; i++) {
328 gSig->SetPoint(i, i , 0);
333 if (in.GetTime() >= gSig->GetN()) {
334 AliWarning("Too many time bins");
335 gSig->Set(in.GetTime());
337 col = in.GetColumn();
340 gSig->SetPoint(in.GetTime(), in.GetTime(), in.GetSignal()) ;
341 if (in.GetTime() > maxTime)
342 maxTime = in.GetTime();
344 } while ((readOk = in.Next()) && !in.IsNewHWAddress());
346 FitRaw(gSig, signalF, amp, time) ;
348 if (amp > 0 && amp < 10000) { //check both high and low end of
349 //result, 10000 is somewhat arbitrary
350 AliDebug(2,Form("id %d lowGain %d amp %g", id, lowGain, amp));
351 //cout << "col " << col-40 << " row " << row-8 << " lowGain " << lowGain << " amp " << amp << endl;
352 AddDigit(digitsArr, id, lowGain, (Int_t)amp, time);
356 for (Int_t index = 0; index < gSig->GetN(); index++) {
357 gSig->SetPoint(index, index, 0) ;
359 // Reset starting parameters for fit function
360 signalF->SetParameters(10.,0.,fTau,fOrder,5.); //reset all defaults just to be safe
362 }; // EMCAL entries loop
370 //____________________________________________________________________________
371 void AliEMCALRawUtils::AddDigit(TClonesArray *digitsArr, Int_t id, Int_t lowGain, Int_t amp, Float_t time) {
374 // This routine checks whether a digit exists already for this tower
375 // and then decides whether to use the high or low gain info
377 // Called by Raw2Digits
379 AliEMCALDigit *digit = 0, *tmpdigit = 0;
381 TIter nextdigit(digitsArr);
382 while (digit == 0 && (tmpdigit = (AliEMCALDigit*) nextdigit())) {
383 if (tmpdigit->GetId() == id)
387 if (!digit) { // no digit existed for this tower; create one
389 amp = Int_t(fHighLowGainFactor * amp);
390 Int_t idigit = digitsArr->GetEntries();
391 new((*digitsArr)[idigit]) AliEMCALDigit( -1, -1, id, amp, time, idigit) ;
393 else { // a digit already exists, check range
394 // (use high gain if signal < cut value, otherwise low gain)
395 if (lowGain) { // new digit is low gain
396 if (digit->GetAmp() > fgkOverflowCut) { // use if stored digit is out of range
397 digit->SetAmp(Int_t(fHighLowGainFactor * amp));
398 digit->SetTime(time);
401 else if (amp < fgkOverflowCut) { // new digit is high gain; use if not out of range
403 digit->SetTime(time);
408 //____________________________________________________________________________
409 void AliEMCALRawUtils::FitRaw(TGraph * gSig, TF1* signalF, Float_t & amp, Float_t & time)
411 // Fits the raw signal time distribution; from AliEMCALGetter
417 for (Int_t index = 0; index < fNPedSamples; index++) {
418 Double_t ttime, signal;
419 gSig->GetPoint(index, ttime, signal) ;
429 AliWarning("Could not determine pedestal");
430 ped = 10; // put some small value as first guess
436 Float_t max_fit = gSig->GetN();
437 Float_t min_after_sig = 9999;
438 Int_t tmin_after_sig = gSig->GetN();
439 Int_t n_ped_after_sig = 0;
441 for (Int_t i=fNPedSamples; i < gSig->GetN(); i++) {
442 Double_t ttime, signal;
443 gSig->GetPoint(i, ttime, signal) ;
444 if (!max_found && signal > max) {
448 else if ( max > ped + fNoiseThreshold ) {
450 min_after_sig = signal;
454 if ( signal < min_after_sig) {
455 min_after_sig = signal;
458 if (i > tmin_after_sig + 5) { // Two close peaks; end fit at minimum
459 max_fit = tmin_after_sig;
462 if ( signal < ped + fNoiseThreshold)
464 if (n_ped_after_sig >= 5) { // include 5 pedestal bins after peak
471 if ( max - ped > fNoiseThreshold ) { // else its noise
472 AliDebug(2,Form("Fitting max %d ped %d", max, ped));
473 signalF->SetRange(0,max_fit);
476 signalF->SetParLimits(2,1,3);
478 signalF->SetParameter(4, ped) ;
479 signalF->SetParameter(1, i_max);
480 signalF->SetParameter(0, max);
482 gSig->Fit(signalF, "QROW"); // Note option 'W': equal errors on all points
483 amp = signalF->GetParameter(0);
484 time = signalF->GetParameter(1)*GetRawFormatTimeBinWidth() - fgTimeTrigger;
488 //__________________________________________________________________
489 Double_t AliEMCALRawUtils::RawResponseFunction(Double_t *x, Double_t *par)
491 // Matches version used in 2007 beam test
493 // Shape of the electronics raw reponse:
494 // It is a semi-gaussian, 2nd order Gamma function of the general form
496 // t' = (t - t0 + tau) / tau
497 // F = A * t**N * exp( N * ( 1 - t) ) for t >= 0
501 // A: par[0] // Amplitude = peak value
508 Double_t tau =par[2];
510 Double_t ped = par[4];
511 Double_t xx = ( x[0] - par[1] + tau ) / tau ;
516 signal = ped + par[0] * TMath::Power(xx , N) * TMath::Exp(N * (1 - xx )) ;
521 //__________________________________________________________________
522 Bool_t AliEMCALRawUtils::RawSampledResponse(
523 const Double_t dtime, const Double_t damp, Int_t * adcH, Int_t * adcL) const
525 // for a start time dtime and an amplitude damp given by digit,
526 // calculates the raw sampled response AliEMCAL::RawResponseFunction
528 Bool_t lowGain = kFALSE ;
530 // A: par[0] // Amplitude = peak value
536 TF1 signalF("signal", RawResponseFunction, 0, GetRawFormatTimeMax(), 5);
537 signalF.SetParameter(0, damp) ;
538 signalF.SetParameter(1, dtime + fgTimeTrigger) ;
539 signalF.SetParameter(2, fTau) ;
540 signalF.SetParameter(3, fOrder);
541 signalF.SetParameter(4, fgPedestalValue);
543 for (Int_t iTime = 0; iTime < GetRawFormatTimeBins(); iTime++) {
544 Double_t time = iTime * GetRawFormatTimeBinWidth() ;
545 Double_t signal = signalF.Eval(time) ;
547 //According to Terry Awes, 13-Apr-2008
548 //add gaussian noise in quadrature to each sample
549 //Double_t noise = gRandom->Gaus(0.,fgFEENoise);
550 //signal = sqrt(signal*signal + noise*noise);
552 adcH[iTime] = static_cast<Int_t>(signal + 0.5) ;
553 if ( adcH[iTime] > fgkRawSignalOverflow ){ // larger than 10 bits
554 adcH[iTime] = fgkRawSignalOverflow ;
558 signal /= fHighLowGainFactor;
560 adcL[iTime] = static_cast<Int_t>(signal + 0.5) ;
561 if ( adcL[iTime] > fgkRawSignalOverflow) // larger than 10 bits
562 adcL[iTime] = fgkRawSignalOverflow ;