]> git.uio.no Git - u/mrichter/AliRoot.git/blame - EMCAL/AliEMCALRawUtils.cxx
Corrections for the secondaries - knocked out from the material
[u/mrichter/AliRoot.git] / EMCAL / AliEMCALRawUtils.cxx
CommitLineData
ee299369 1/**************************************************************************
2 * Copyright(c) 1998-1999, 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$ */
ee299369 17
e5bbbc4e 18//_________________________________________________________________________
19// Utility Class for handling Raw data
20// Does all transitions from Digits to Raw and vice versa,
21// for simu and reconstruction
22//
23// Note: the current version is still simplified. Only
24// one raw signal per digit is generated; either high-gain or low-gain
25// Need to add concurrent high and low-gain info in the future
26// No pedestal is added to the raw signal.
ee299369 27//*-- Author: Marco van Leeuwen (LBL)
e5bbbc4e 28
ee299369 29#include "AliEMCALRawUtils.h"
21cad85c 30
4fe71e02 31#include "TF1.h"
32#include "TGraph.h"
e5bbbc4e 33class TSystem;
21cad85c 34
e5bbbc4e 35class AliLog;
72c58de0 36#include "AliRun.h"
ee299369 37#include "AliRunLoader.h"
e5bbbc4e 38class AliCaloAltroMapping;
ee299369 39#include "AliAltroBuffer.h"
40#include "AliRawReader.h"
32cd4c24 41#include "AliCaloRawStreamV3.h"
ee299369 42#include "AliDAQ.h"
21cad85c 43
feedcab9 44#include "AliEMCALRecParam.h"
ee299369 45#include "AliEMCALLoader.h"
46#include "AliEMCALGeometry.h"
e5bbbc4e 47class AliEMCALDigitizer;
ee299369 48#include "AliEMCALDigit.h"
20b636fc 49#include "AliEMCAL.h"
5e3106bc 50#include "AliCaloCalibPedestal.h"
9f467289 51#include "AliCaloFastAltroFitv0.h"
c8603a2b 52#include "AliCaloNeuralFit.h"
16605c06 53#include "AliCaloBunchInfo.h"
54#include "AliCaloFitResults.h"
7683df1d 55#include "AliCaloRawAnalyzerFastFit.h"
56#include "AliCaloRawAnalyzerNN.h"
16605c06 57#include "AliCaloRawAnalyzerLMS.h"
58#include "AliCaloRawAnalyzerPeakFinder.h"
59#include "AliCaloRawAnalyzerCrude.h"
9f467289 60
ee299369 61ClassImp(AliEMCALRawUtils)
21cad85c 62
ee299369 63// Signal shape parameters
89d338a6 64Int_t AliEMCALRawUtils::fgTimeBins = 256; // number of sampling bins of the raw RO signal (we typically use 15-50; theoretical max is 1k+)
e5bbbc4e 65Double_t AliEMCALRawUtils::fgTimeBinWidth = 100E-9 ; // each sample is 100 ns
09974781 66Double_t AliEMCALRawUtils::fgTimeTrigger = 1.5E-6 ; // 15 time bins ~ 1.5 musec
ee299369 67
68// some digitization constants
69Int_t AliEMCALRawUtils::fgThreshold = 1;
70Int_t AliEMCALRawUtils::fgDDLPerSuperModule = 2; // 2 ddls per SuperModule
e5bbbc4e 71Int_t AliEMCALRawUtils::fgPedestalValue = 32; // pedestal value for digits2raw
72Double_t AliEMCALRawUtils::fgFEENoise = 3.; // 3 ADC channels of noise (sampled)
ee299369 73
16605c06 74AliEMCALRawUtils::AliEMCALRawUtils(fitAlgorithm fitAlgo)
b4133f05 75 : fHighLowGainFactor(0.), fOrder(0), fTau(0.), fNoiseThreshold(0),
9f467289 76 fNPedSamples(0), fGeom(0), fOption(""),
16605c06 77 fRemoveBadChannels(kTRUE),fFittingAlgorithm(0),fRawAnalyzer(0)
8cb998bd 78{
b4133f05 79
80 //These are default parameters.
81 //Can be re-set from without with setter functions
9f467289 82 //Already set in the OCDB and passed via setter in the AliEMCALReconstructor
ee299369 83 fHighLowGainFactor = 16. ; // adjusted for a low gain range of 82 GeV (10 bits)
b4133f05 84 fOrder = 2; // order of gamma fn
85 fTau = 2.35; // in units of timebin, from CERN 2007 testbeam
7643e728 86 fNoiseThreshold = 3; // 3 ADC counts is approx. noise level
87 fNPedSamples = 4; // less than this value => likely pedestal samples
9f467289 88 fRemoveBadChannels = kTRUE; //Remove bad channels before fitting
4fe71e02 89 SetFittingAlgorithm(fitAlgo);
16605c06 90
65bdc82f 91 //Get Mapping RCU files from the AliEMCALRecParam
92 const TObjArray* maps = AliEMCALRecParam::GetMappings();
93 if(!maps) AliFatal("Cannot retrieve ALTRO mappings!!");
94
21cad85c 95 for(Int_t i = 0; i < 4; i++) {
65bdc82f 96 fMapping[i] = (AliAltroMapping*)maps->At(i);
97 }
98
72c58de0 99 //To make sure we match with the geometry in a simulation file,
100 //let's try to get it first. If not, take the default geometry
33c3c91a 101 AliRunLoader *rl = AliRunLoader::Instance();
c61fe3b4 102 if(!rl) AliError("Cannot find RunLoader!");
72c58de0 103 if (rl->GetAliRun() && rl->GetAliRun()->GetDetector("EMCAL")) {
104 fGeom = dynamic_cast<AliEMCAL*>(rl->GetAliRun()->GetDetector("EMCAL"))->GetGeometry();
105 } else {
106 AliInfo(Form("Using default geometry in raw reco"));
937d0661 107 fGeom = AliEMCALGeometry::GetInstance(AliEMCALGeometry::GetDefaultGeometryName());
65bdc82f 108 }
109
72c58de0 110 if(!fGeom) AliFatal(Form("Could not get geometry!"));
111
65bdc82f 112}
113
114//____________________________________________________________________________
16605c06 115AliEMCALRawUtils::AliEMCALRawUtils(AliEMCALGeometry *pGeometry, fitAlgorithm fitAlgo)
5544799a 116 : fHighLowGainFactor(0.), fOrder(0), fTau(0.), fNoiseThreshold(0),
9f467289 117 fNPedSamples(0), fGeom(pGeometry), fOption(""),
16605c06 118 fRemoveBadChannels(kTRUE),fFittingAlgorithm(0),fRawAnalyzer()
5544799a 119{
120 //
121 // Initialize with the given geometry - constructor required by HLT
122 // HLT does not use/support AliRunLoader(s) instances
123 // This is a minimum intervention solution
124 // Comment by MPloskon@lbl.gov
125 //
126
127 //These are default parameters.
128 //Can be re-set from without with setter functions
9f467289 129 //Already set in the OCDB and passed via setter in the AliEMCALReconstructor
5544799a 130 fHighLowGainFactor = 16. ; // adjusted for a low gain range of 82 GeV (10 bits)
131 fOrder = 2; // order of gamma fn
132 fTau = 2.35; // in units of timebin, from CERN 2007 testbeam
7643e728 133 fNoiseThreshold = 3; // 3 ADC counts is approx. noise level
134 fNPedSamples = 4; // less than this value => likely pedestal samples
9f467289 135 fRemoveBadChannels = kTRUE; //Remove bad channels before fitting
4fe71e02 136 SetFittingAlgorithm(fitAlgo);
137
138
5544799a 139 //Get Mapping RCU files from the AliEMCALRecParam
140 const TObjArray* maps = AliEMCALRecParam::GetMappings();
141 if(!maps) AliFatal("Cannot retrieve ALTRO mappings!!");
142
21cad85c 143 for(Int_t i = 0; i < 4; i++) {
5544799a 144 fMapping[i] = (AliAltroMapping*)maps->At(i);
145 }
146
147 if(!fGeom) AliFatal(Form("Could not get geometry!"));
148
149}
150
151//____________________________________________________________________________
65bdc82f 152AliEMCALRawUtils::AliEMCALRawUtils(const AliEMCALRawUtils& rawU)
153 : TObject(),
154 fHighLowGainFactor(rawU.fHighLowGainFactor),
b4133f05 155 fOrder(rawU.fOrder),
156 fTau(rawU.fTau),
157 fNoiseThreshold(rawU.fNoiseThreshold),
158 fNPedSamples(rawU.fNPedSamples),
65bdc82f 159 fGeom(rawU.fGeom),
9f467289 160 fOption(rawU.fOption),
161 fRemoveBadChannels(rawU.fRemoveBadChannels),
16605c06 162 fFittingAlgorithm(rawU.fFittingAlgorithm),
163 fRawAnalyzer(rawU.fRawAnalyzer)
65bdc82f 164{
165 //copy ctor
166 fMapping[0] = rawU.fMapping[0];
167 fMapping[1] = rawU.fMapping[1];
21cad85c 168 fMapping[2] = rawU.fMapping[2];
169 fMapping[3] = rawU.fMapping[3];
65bdc82f 170}
171
172//____________________________________________________________________________
173AliEMCALRawUtils& AliEMCALRawUtils::operator =(const AliEMCALRawUtils &rawU)
174{
175 //assignment operator
176
177 if(this != &rawU) {
178 fHighLowGainFactor = rawU.fHighLowGainFactor;
b4133f05 179 fOrder = rawU.fOrder;
180 fTau = rawU.fTau;
181 fNoiseThreshold = rawU.fNoiseThreshold;
182 fNPedSamples = rawU.fNPedSamples;
65bdc82f 183 fGeom = rawU.fGeom;
184 fOption = rawU.fOption;
9f467289 185 fRemoveBadChannels = rawU.fRemoveBadChannels;
186 fFittingAlgorithm = rawU.fFittingAlgorithm;
16605c06 187 fRawAnalyzer = rawU.fRawAnalyzer;
65bdc82f 188 fMapping[0] = rawU.fMapping[0];
189 fMapping[1] = rawU.fMapping[1];
21cad85c 190 fMapping[2] = rawU.fMapping[2];
191 fMapping[3] = rawU.fMapping[3];
65bdc82f 192 }
193
194 return *this;
195
ee299369 196}
65bdc82f 197
ee299369 198//____________________________________________________________________________
199AliEMCALRawUtils::~AliEMCALRawUtils() {
e5bbbc4e 200 //dtor
65bdc82f 201
ee299369 202}
65bdc82f 203
ee299369 204//____________________________________________________________________________
65bdc82f 205void AliEMCALRawUtils::Digits2Raw()
ee299369 206{
207 // convert digits of the current event to raw data
208
33c3c91a 209 AliRunLoader *rl = AliRunLoader::Instance();
ee299369 210 AliEMCALLoader *loader = dynamic_cast<AliEMCALLoader*>(rl->GetDetectorLoader("EMCAL"));
211
212 // get the digits
213 loader->LoadDigits("EMCAL");
214 loader->GetEvent();
215 TClonesArray* digits = loader->Digits() ;
216
217 if (!digits) {
218 Warning("Digits2Raw", "no digits found !");
219 return;
220 }
65bdc82f 221
ee299369 222 static const Int_t nDDL = 12*2; // 12 SM hardcoded for now. Buffers allocated dynamically, when needed, so just need an upper limit here
223 AliAltroBuffer* buffers[nDDL];
224 for (Int_t i=0; i < nDDL; i++)
225 buffers[i] = 0;
226
e2c2134b 227 TArrayI adcValuesLow(fgTimeBins);
228 TArrayI adcValuesHigh(fgTimeBins);
ee299369 229
ee299369 230 // loop over digits (assume ordered digits)
231 for (Int_t iDigit = 0; iDigit < digits->GetEntries(); iDigit++) {
232 AliEMCALDigit* digit = dynamic_cast<AliEMCALDigit *>(digits->At(iDigit)) ;
233 if (digit->GetAmp() < fgThreshold)
234 continue;
235
236 //get cell indices
237 Int_t nSM = 0;
238 Int_t nIphi = 0;
239 Int_t nIeta = 0;
240 Int_t iphi = 0;
241 Int_t ieta = 0;
242 Int_t nModule = 0;
65bdc82f 243 fGeom->GetCellIndex(digit->GetId(), nSM, nModule, nIphi, nIeta);
244 fGeom->GetCellPhiEtaIndexInSModule(nSM, nModule, nIphi, nIeta,iphi, ieta) ;
ee299369 245
21cad85c 246 //Check which is the RCU, 0 or 1, of the cell.
ee299369 247 Int_t iRCU = -111;
248 //RCU0
249 if (0<=iphi&&iphi<8) iRCU=0; // first cable row
250 else if (8<=iphi&&iphi<16 && 0<=ieta&&ieta<24) iRCU=0; // first half;
251 //second cable row
252 //RCU1
253 else if(8<=iphi&&iphi<16 && 24<=ieta&&ieta<48) iRCU=1; // second half;
254 //second cable row
255 else if(16<=iphi&&iphi<24) iRCU=1; // third cable row
21cad85c 256
257 if (nSM%2==1) iRCU = 1 - iRCU; // swap for odd=C side, to allow us to cable both sides the same
258
e36e3bcf 259 if (iRCU<0)
260 Fatal("Digits2Raw()","Non-existent RCU number: %d", iRCU);
ee299369 261
262 //Which DDL?
263 Int_t iDDL = fgDDLPerSuperModule* nSM + iRCU;
264 if (iDDL >= nDDL)
265 Fatal("Digits2Raw()","Non-existent DDL board number: %d", iDDL);
266
267 if (buffers[iDDL] == 0) {
268 // open new file and write dummy header
269 TString fileName = AliDAQ::DdlFileName("EMCAL",iDDL);
21cad85c 270 //Select mapping file RCU0A, RCU0C, RCU1A, RCU1C
271 Int_t iRCUside=iRCU+(nSM%2)*2;
272 //iRCU=0 and even (0) SM -> RCU0A.data 0
273 //iRCU=1 and even (0) SM -> RCU1A.data 1
274 //iRCU=0 and odd (1) SM -> RCU0C.data 2
275 //iRCU=1 and odd (1) SM -> RCU1C.data 3
276 //cout<<" nSM "<<nSM<<"; iRCU "<<iRCU<<"; iRCUside "<<iRCUside<<endl;
277 buffers[iDDL] = new AliAltroBuffer(fileName.Data(),fMapping[iRCUside]);
ee299369 278 buffers[iDDL]->WriteDataHeader(kTRUE, kFALSE); //Dummy;
279 }
280
281 // out of time range signal (?)
282 if (digit->GetTimeR() > GetRawFormatTimeMax() ) {
283 AliInfo("Signal is out of time range.\n");
284 buffers[iDDL]->FillBuffer((Int_t)digit->GetAmp());
285 buffers[iDDL]->FillBuffer(GetRawFormatTimeBins() ); // time bin
286 buffers[iDDL]->FillBuffer(3); // bunch length
287 buffers[iDDL]->WriteTrailer(3, ieta, iphi, nSM); // trailer
288 // calculate the time response function
289 } else {
e2c2134b 290 Bool_t lowgain = RawSampledResponse(digit->GetTimeR(), digit->GetAmp(), adcValuesHigh.GetArray(), adcValuesLow.GetArray()) ;
ee299369 291 if (lowgain)
e2c2134b 292 buffers[iDDL]->WriteChannel(ieta, iphi, 0, GetRawFormatTimeBins(), adcValuesLow.GetArray(), fgThreshold);
ee299369 293 else
e2c2134b 294 buffers[iDDL]->WriteChannel(ieta,iphi, 1, GetRawFormatTimeBins(), adcValuesHigh.GetArray(), fgThreshold);
ee299369 295 }
296 }
297
298 // write headers and close files
299 for (Int_t i=0; i < nDDL; i++) {
300 if (buffers[i]) {
301 buffers[i]->Flush();
302 buffers[i]->WriteDataHeader(kFALSE, kFALSE);
303 delete buffers[i];
304 }
305 }
65bdc82f 306
ee299369 307 loader->UnloadDigits();
308}
309
310//____________________________________________________________________________
16605c06 311void AliEMCALRawUtils::Raw2Digits(AliRawReader* reader,TClonesArray *digitsArr, const AliCaloCalibPedestal* pedbadmap)
ee299369 312{
65bdc82f 313 // convert raw data of the current event to digits
ee299369 314
c47157cd 315 digitsArr->Clear();
ee299369 316
c47157cd 317 if (!digitsArr) {
ee299369 318 Error("Raw2Digits", "no digits found !");
319 return;
320 }
321 if (!reader) {
322 Error("Raw2Digits", "no raw reader found !");
323 return;
324 }
325
32cd4c24 326 AliCaloRawStreamV3 in(reader,"EMCAL",fMapping);
ee299369 327 // Select EMCAL DDL's;
7643e728 328 reader->Select("EMCAL",0,43); // 43 = AliEMCALGeoParams::fgkLastAltroDDL
feedcab9 329
16605c06 330 // fRawAnalyzer setup
331 fRawAnalyzer->SetAmpCut(fNoiseThreshold);
332 fRawAnalyzer->SetFitArrayCut(fNoiseThreshold);
333 fRawAnalyzer->SetIsZeroSuppressed(true); // TMP - should use stream->IsZeroSuppressed(), or altro cfg registers later
ee299369 334
16605c06 335 // channel info parameters
ee299369 336 Int_t lowGain = 0;
e5bbbc4e 337 Int_t caloFlag = 0; // low, high gain, or TRU, or LED ref.
ee299369 338
32cd4c24 339 // start loop over input stream
340 while (in.NextDDL()) {
341 while (in.NextChannel()) {
7643e728 342
343 //Check if the signal is high or low gain and then do the fit,
16605c06 344 //if it is from TRU or LEDMon do not fit
7643e728 345 caloFlag = in.GetCaloFlag();
346 if (caloFlag != 0 && caloFlag != 1) continue;
347
5e3106bc 348 //Do not fit bad channels
9f467289 349 if(fRemoveBadChannels && pedbadmap->IsBadChannel(in.GetModule(),in.GetColumn(),in.GetRow())) {
5e3106bc 350 //printf("Tower from SM %d, column %d, row %d is BAD!!! Skip \n", in.GetModule(),in.GetColumn(),in.GetRow());
351 continue;
352 }
353
16605c06 354 vector<AliCaloBunchInfo> bunchlist;
32cd4c24 355 while (in.NextBunch()) {
16605c06 356 bunchlist.push_back( AliCaloBunchInfo(in.GetStartTimeBin(), in.GetBunchLength(), in.GetSignals() ) );
357 } // loop over bunches
7643e728 358
16605c06 359 Float_t time = 0;
360 Float_t amp = 0;
361
7683df1d 362 if ( fFittingAlgorithm == kFastFit || fFittingAlgorithm == kNeuralNet || fFittingAlgorithm == kLMS || fFittingAlgorithm == kPeakFinder || fFittingAlgorithm == kCrude) {
16605c06 363 // all functionality to determine amp and time etc is encapsulated inside the Evaluate call for these methods
364 AliCaloFitResults fitResults = fRawAnalyzer->Evaluate( bunchlist, in.GetAltroCFG1(), in.GetAltroCFG2());
365
366 amp = fitResults.GetAmp();
367 time = fitResults.GetTof();
368 }
369 else { // for the other methods we for now use the functionality of
370 // AliCaloRawAnalyzer as well, to select samples and prepare for fits,
371 // if it looks like there is something to fit
372
373 // parameters init.
374 Float_t ampEstimate = 0;
375 short maxADC = 0;
376 short timeEstimate = 0;
377 Float_t pedEstimate = 0;
378 Int_t first = 0;
379 Int_t last = 0;
380 Int_t bunchIndex = 0;
381 //
382 // The PreFitEvaluateSamples + later call to FitRaw will hopefully
383 // be replaced by a single Evaluate call or so soon, like for the other
384 // methods, but this should be good enough for evaluation of
385 // the methods for now (Jan. 2010)
386 //
387 int nsamples = fRawAnalyzer->PreFitEvaluateSamples( bunchlist, in.GetAltroCFG1(), in.GetAltroCFG2(), bunchIndex, ampEstimate, maxADC, timeEstimate, pedEstimate, first, last);
7643e728 388
16605c06 389 if (ampEstimate > fNoiseThreshold) { // something worth looking at
7643e728 390
16605c06 391 time = timeEstimate;
392 amp = ampEstimate;
393
394 if ( nsamples > 1 ) { // possibly something to fit
395 FitRaw(first, last, amp, time);
9f467289 396 }
16605c06 397
398 if ( amp>0 && time>0 ) { // brief sanity check of fit results
399
400 // check fit results: should be consistent with initial estimates
401 // more magic numbers, but very loose cuts, for now..
402 // We have checked that amp and ampEstimate values are positive so division for assymmetry
403 // calculation should be OK/safe
404 Float_t ampAsymm = (amp - ampEstimate)/(amp + ampEstimate);
405 if ( (TMath::Abs(ampAsymm) > 0.1) ) {
406 AliDebug(2,Form("Fit results amp %f time %f not consistent with expectations ped %f max-ped %f time %d",
407 amp, time, pedEstimate, ampEstimate, timeEstimate));
408
409 // what should do we do then? skip this channel or assign the simple estimate?
410 // for now just overwrite the fit results with the simple estimate
411 amp = ampEstimate;
412 time = timeEstimate;
413 } // asymm check
414 } // amp & time check
415 } // ampEstimate check
416 } // method selection
417
418 if (amp > fNoiseThreshold) { // something to be stored
419 Int_t id = fGeom->GetAbsCellIdFromCellIndexes(in.GetModule(), in.GetRow(), in.GetColumn()) ;
7643e728 420 lowGain = in.IsLowGain();
421
16605c06 422 // go from time-bin units to physical time fgtimetrigger
423 time = time * GetRawFormatTimeBinWidth(); // skip subtraction of fgTimeTrigger?
7643e728 424
425 AliDebug(2,Form("id %d lowGain %d amp %g", id, lowGain, amp));
426 // printf("Added tower: SM %d, row %d, column %d, amp %3.2f\n",in.GetModule(), in.GetRow(), in.GetColumn(),amp);
427 // round off amplitude value to nearest integer
428 AddDigit(digitsArr, id, lowGain, TMath::Nint(amp), time);
429 }
430
32cd4c24 431 } // end while over channel
432 } //end while over DDL's, of input stream
16605c06 433
ee299369 434 return ;
435}
436
437//____________________________________________________________________________
82cbdfca 438void AliEMCALRawUtils::AddDigit(TClonesArray *digitsArr, Int_t id, Int_t lowGain, Int_t amp, Float_t time) {
439 //
440 // Add a new digit.
441 // This routine checks whether a digit exists already for this tower
442 // and then decides whether to use the high or low gain info
443 //
444 // Called by Raw2Digits
445
446 AliEMCALDigit *digit = 0, *tmpdigit = 0;
82cbdfca 447 TIter nextdigit(digitsArr);
448 while (digit == 0 && (tmpdigit = (AliEMCALDigit*) nextdigit())) {
449 if (tmpdigit->GetId() == id)
450 digit = tmpdigit;
451 }
452
453 if (!digit) { // no digit existed for this tower; create one
a7ec7165 454 if (lowGain && amp > fgkOverflowCut)
82cbdfca 455 amp = Int_t(fHighLowGainFactor * amp);
456 Int_t idigit = digitsArr->GetEntries();
457 new((*digitsArr)[idigit]) AliEMCALDigit( -1, -1, id, amp, time, idigit) ;
458 }
459 else { // a digit already exists, check range
b4133f05 460 // (use high gain if signal < cut value, otherwise low gain)
82cbdfca 461 if (lowGain) { // new digit is low gain
b4133f05 462 if (digit->GetAmp() > fgkOverflowCut) { // use if stored digit is out of range
82cbdfca 463 digit->SetAmp(Int_t(fHighLowGainFactor * amp));
464 digit->SetTime(time);
465 }
466 }
b4133f05 467 else if (amp < fgkOverflowCut) { // new digit is high gain; use if not out of range
82cbdfca 468 digit->SetAmp(amp);
469 digit->SetTime(time);
470 }
471 }
472}
473
474//____________________________________________________________________________
16605c06 475void AliEMCALRawUtils::FitRaw(const Int_t firstTimeBin, const Int_t lastTimeBin, Float_t & amp, Float_t & time) const
476{ // Fits the raw signal time distribution
477
478 //--------------------------------------------------
479 //Do the fit, different fitting algorithms available
480 //--------------------------------------------------
481 int nsamples = lastTimeBin - firstTimeBin + 1;
ee299369 482
16605c06 483 switch(fFittingAlgorithm) {
484 case kStandard:
485 {
7683df1d 486 if (nsamples < 3) { return; } // nothing much to fit
16605c06 487 //printf("Standard fitter \n");
7683df1d 488
16605c06 489 // Create Graph to hold data we will fit
7683df1d 490 TGraph *gSig = new TGraph( nsamples);
491 for (int i=0; i<nsamples; i++) {
492 Int_t timebin = firstTimeBin + i;
493 gSig->SetPoint(timebin, timebin, fRawAnalyzer->GetReversed(timebin));
494 }
495
16605c06 496 TF1 * signalF = new TF1("signal", RawResponseFunction, 0, GetRawFormatTimeBins(), 5);
497 signalF->SetParameters(10.,5.,fTau,fOrder,0.); //set all defaults once, just to be safe
498 signalF->SetParNames("amp","t0","tau","N","ped");
499 signalF->FixParameter(2,fTau); // tau in units of time bin
500 signalF->FixParameter(3,fOrder); // order
501 signalF->FixParameter(4, 0); // pedestal should be subtracted when we get here
502 signalF->SetParameter(1, time);
503 signalF->SetParameter(0, amp);
504
505 gSig->Fit(signalF, "QROW"); // Note option 'W': equal errors on all points
506
507 // assign fit results
508 amp = signalF->GetParameter(0);
509 time = signalF->GetParameter(1);
e9dbb64a 510
16605c06 511 delete signalF;
512
513 // cross-check with ParabolaFit to see if the results make sense
514 FitParabola(gSig, amp); // amp is possibly updated
82cbdfca 515
16605c06 516 //printf("Std : Amp %f, time %g\n",amp, time);
7683df1d 517 delete gSig; // delete TGraph
16605c06 518
519 break;
520 }//kStandard Fitter
521 //----------------------------
7683df1d 522 case kLogFit:
16605c06 523 {
7683df1d 524 if (nsamples < 3) { return; } // nothing much to fit
525 //printf("LogFit \n");
526
527 // Create Graph to hold data we will fit
528 TGraph *gSigLog = new TGraph( nsamples);
529 for (int i=0; i<nsamples; i++) {
530 Int_t timebin = firstTimeBin + i;
531 gSigLog->SetPoint(timebin, timebin, TMath::Log(fRawAnalyzer->GetReversed(timebin) ) );
7643e728 532 }
7683df1d 533
534 TF1 * signalFLog = new TF1("signalLog", RawResponseFunctionLog, 0, GetRawFormatTimeBins(), 5);
535 signalFLog->SetParameters(2.3, 5.,fTau,fOrder,0.); //set all defaults once, just to be safe
536 signalFLog->SetParNames("amplog","t0","tau","N","ped");
537 signalFLog->FixParameter(2,fTau); // tau in units of time bin
538 signalFLog->FixParameter(3,fOrder); // order
539 signalFLog->FixParameter(4, 0); // pedestal should be subtracted when we get here
540 signalFLog->SetParameter(1, time);
541 if (amp>=1) {
542 signalFLog->SetParameter(0, TMath::Log(amp));
16605c06 543 }
7683df1d 544
545 gSigLog->Fit(signalFLog, "QROW"); // Note option 'W': equal errors on all points
546
547 // assign fit results
548 Double_t amplog = signalFLog->GetParameter(0); //Not Amp, but Log of Amp
549 amp = TMath::Exp(amplog);
550 time = signalFLog->GetParameter(1);
551
552 delete signalFLog;
553 //printf("LogFit: Amp %f, time %g\n",amp, time);
554 delete gSigLog;
16605c06 555 break;
7683df1d 556 } //kLogFit
557 //----------------------------
558
16605c06 559 //----------------------------
560 }//switch fitting algorithms
fb070798 561
16605c06 562 return;
563}
8cb998bd 564
16605c06 565//__________________________________________________________________
566void AliEMCALRawUtils::FitParabola(const TGraph *gSig, Float_t & amp) const
567{
568 //BEG YS alternative methods to calculate the amplitude
569 Double_t * ymx = gSig->GetX() ;
570 Double_t * ymy = gSig->GetY() ;
571 const Int_t kN = 3 ;
572 Double_t ymMaxX[kN] = {0., 0., 0.} ;
573 Double_t ymMaxY[kN] = {0., 0., 0.} ;
574 Double_t ymax = 0. ;
575 // find the maximum amplitude
576 Int_t ymiMax = 0 ;
577 for (Int_t ymi = 0; ymi < gSig->GetN(); ymi++) {
578 if (ymy[ymi] > ymMaxY[0] ) {
579 ymMaxY[0] = ymy[ymi] ; //<========== This is the maximum amplitude
580 ymMaxX[0] = ymx[ymi] ;
581 ymiMax = ymi ;
582 }
583 }
584 // find the maximum by fitting a parabola through the max and the two adjacent samples
585 if ( ymiMax < gSig->GetN()-1 && ymiMax > 0) {
586 ymMaxY[1] = ymy[ymiMax+1] ;
587 ymMaxY[2] = ymy[ymiMax-1] ;
588 ymMaxX[1] = ymx[ymiMax+1] ;
589 ymMaxX[2] = ymx[ymiMax-1] ;
590 if (ymMaxY[0]*ymMaxY[1]*ymMaxY[2] > 0) {
591 //fit a parabola through the 3 points y= a+bx+x*x*x
592 Double_t sy = 0 ;
593 Double_t sx = 0 ;
594 Double_t sx2 = 0 ;
595 Double_t sx3 = 0 ;
596 Double_t sx4 = 0 ;
597 Double_t sxy = 0 ;
598 Double_t sx2y = 0 ;
599 for (Int_t i = 0; i < kN ; i++) {
600 sy += ymMaxY[i] ;
601 sx += ymMaxX[i] ;
602 sx2 += ymMaxX[i]*ymMaxX[i] ;
603 sx3 += ymMaxX[i]*ymMaxX[i]*ymMaxX[i] ;
604 sx4 += ymMaxX[i]*ymMaxX[i]*ymMaxX[i]*ymMaxX[i] ;
605 sxy += ymMaxX[i]*ymMaxY[i] ;
606 sx2y += ymMaxX[i]*ymMaxX[i]*ymMaxY[i] ;
607 }
608 Double_t cN = (sx2y*kN-sy*sx2)*(sx3*sx-sx2*sx2)-(sx2y*sx-sxy*sx2)*(sx3*kN-sx*sx2);
609 Double_t cD = (sx4*kN-sx2*sx2)*(sx3*sx-sx2*sx2)-(sx4*sx-sx3*sx2)*(sx3*kN-sx*sx2) ;
610 Double_t c = cN / cD ;
611 Double_t b = ((sx2y*kN-sy*sx2)-c*(sx4*kN-sx2*sx2))/(sx3*kN-sx*sx2) ;
612 Double_t a = (sy-b*sx-c*sx2)/kN ;
613 Double_t xmax = -b/(2*c) ;
614 ymax = a + b*xmax + c*xmax*xmax ;//<========== This is the maximum amplitude
615 }
616 }
617
618 Double_t diff = TMath::Abs(1-ymMaxY[0]/amp) ;
619 if (diff > 0.1)
620 amp = ymMaxY[0] ;
621 //printf("Yves : Amp %f, time %g\n",amp, time);
622 //END YS
ee299369 623 return;
624}
16605c06 625
ee299369 626//__________________________________________________________________
627Double_t AliEMCALRawUtils::RawResponseFunction(Double_t *x, Double_t *par)
628{
8cb998bd 629 // Matches version used in 2007 beam test
630 //
ee299369 631 // Shape of the electronics raw reponse:
632 // It is a semi-gaussian, 2nd order Gamma function of the general form
633 //
7643e728 634 // xx = (t - t0 + tau) / tau [xx is just a convenient help variable]
635 // F = A * (xx**N * exp( N * ( 1 - xx) ) for xx >= 0
636 // F = 0 for xx < 0
ee299369 637 //
638 // parameters:
8cb998bd 639 // A: par[0] // Amplitude = peak value
640 // t0: par[1]
641 // tau: par[2]
642 // N: par[3]
643 // ped: par[4]
ee299369 644 //
645 Double_t signal ;
8cb998bd 646 Double_t tau =par[2];
e5bbbc4e 647 Double_t n =par[3];
8cb998bd 648 Double_t ped = par[4];
649 Double_t xx = ( x[0] - par[1] + tau ) / tau ;
ee299369 650
5a056daa 651 if (xx <= 0)
8cb998bd 652 signal = ped ;
ee299369 653 else {
e5bbbc4e 654 signal = ped + par[0] * TMath::Power(xx , n) * TMath::Exp(n * (1 - xx )) ;
ee299369 655 }
656 return signal ;
657}
658
7683df1d 659//__________________________________________________________________
660Double_t AliEMCALRawUtils::RawResponseFunctionLog(Double_t *x, Double_t *par)
661{
662 // Matches version used in 2007 beam test
663 //
664 // Shape of the electronics raw reponse:
665 // It is a semi-gaussian, 2nd order Gamma function of the general form
666 //
667 // xx = (t - t0 + tau) / tau [xx is just a convenient help variable]
668 // F = A * (xx**N * exp( N * ( 1 - xx) ) for xx >= 0
669 // F = 0 for xx < 0
670 //
671 // parameters:
672 // Log[A]: par[0] // Amplitude = peak value
673 // t0: par[1]
674 // tau: par[2]
675 // N: par[3]
676 // ped: par[4]
677 //
678 Double_t signal ;
679 Double_t tau =par[2];
680 Double_t n =par[3];
681 //Double_t ped = par[4]; // not used
682 Double_t xx = ( x[0] - par[1] + tau ) / tau ;
683
684 if (xx < 0)
685 signal = par[0] - n*TMath::Log(TMath::Abs(xx)) + n * (1 - xx ) ;
686 else {
687 signal = par[0] + n*TMath::Log(xx) + n * (1 - xx ) ;
688 }
689 return signal ;
690}
691
ee299369 692//__________________________________________________________________
4fe71e02 693Bool_t AliEMCALRawUtils::RawSampledResponse(
694const Double_t dtime, const Double_t damp, Int_t * adcH, Int_t * adcL) const
ee299369 695{
696 // for a start time dtime and an amplitude damp given by digit,
697 // calculates the raw sampled response AliEMCAL::RawResponseFunction
698
ee299369 699 Bool_t lowGain = kFALSE ;
700
48a56166 701 // A: par[0] // Amplitude = peak value
702 // t0: par[1]
703 // tau: par[2]
704 // N: par[3]
705 // ped: par[4]
706
56e13066 707 TF1 signalF("signal", RawResponseFunction, 0, GetRawFormatTimeBins(), 5);
48a56166 708 signalF.SetParameter(0, damp) ;
56e13066 709 signalF.SetParameter(1, (dtime + fgTimeTrigger)/fgTimeBinWidth) ;
b4133f05 710 signalF.SetParameter(2, fTau) ;
711 signalF.SetParameter(3, fOrder);
fe93d365 712 signalF.SetParameter(4, fgPedestalValue);
ee299369 713
714 for (Int_t iTime = 0; iTime < GetRawFormatTimeBins(); iTime++) {
4fe71e02 715 Double_t signal = signalF.Eval(iTime) ;
fe93d365 716
7643e728 717 // Next lines commeted for the moment but in principle it is not necessary to add
ff10f540 718 // extra noise since noise already added at the digits level.
7643e728 719
fe93d365 720 //According to Terry Awes, 13-Apr-2008
721 //add gaussian noise in quadrature to each sample
09974781 722 //Double_t noise = gRandom->Gaus(0.,fgFEENoise);
fe93d365 723 //signal = sqrt(signal*signal + noise*noise);
724
e2c2134b 725 // March 17,09 for fast fit simulations by Alexei Pavlinov.
4fe71e02 726 // Get from PHOS analysis. In some sense it is open questions.
727 //Double_t noise = gRandom->Gaus(0.,fgFEENoise);
728 //signal += noise;
7643e728 729
ee299369 730 adcH[iTime] = static_cast<Int_t>(signal + 0.5) ;
b4133f05 731 if ( adcH[iTime] > fgkRawSignalOverflow ){ // larger than 10 bits
732 adcH[iTime] = fgkRawSignalOverflow ;
ee299369 733 lowGain = kTRUE ;
734 }
735
736 signal /= fHighLowGainFactor;
737
738 adcL[iTime] = static_cast<Int_t>(signal + 0.5) ;
b4133f05 739 if ( adcL[iTime] > fgkRawSignalOverflow) // larger than 10 bits
740 adcL[iTime] = fgkRawSignalOverflow ;
ee299369 741 }
742 return lowGain ;
743}
4fe71e02 744
745//__________________________________________________________________
746void AliEMCALRawUtils::SetFittingAlgorithm(Int_t fitAlgo)
747{
748 //Set fitting algorithm and initialize it if this same algorithm was not set before.
749
750 if(fitAlgo == fFittingAlgorithm && fRawAnalyzer) {
751 //Do nothing, this same algorithm already set before.
752 //printf("**** Algorithm already set before, number %d, %s ****\n",fitAlgo, fRawAnalyzer->GetName());
753 return;
754 }
755 //Initialize the requested algorithm
756 if(fitAlgo != fFittingAlgorithm || !fRawAnalyzer) {
757 //printf("**** Init Algorithm , number %d ****\n",fitAlgo);
758
759 fFittingAlgorithm = fitAlgo;
760 if (fRawAnalyzer) delete fRawAnalyzer; // delete prev. analyzer if existed.
761
762 if (fitAlgo == kFastFit) {
763 fRawAnalyzer = new AliCaloRawAnalyzerFastFit();
764 }
765 else if (fitAlgo == kNeuralNet) {
766 fRawAnalyzer = new AliCaloRawAnalyzerNN();
767 }
768 else if (fitAlgo == kLMS) {
769 fRawAnalyzer = new AliCaloRawAnalyzerLMS();
770 }
771 else if (fitAlgo == kPeakFinder) {
772 fRawAnalyzer = new AliCaloRawAnalyzerPeakFinder();
773 }
774 else if (fitAlgo == kCrude) {
775 fRawAnalyzer = new AliCaloRawAnalyzerCrude();
776 }
777 else {
778 fRawAnalyzer = new AliCaloRawAnalyzer();
779 }
780 }
781
782}
783
784