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 **************************************************************************/
18 //_________________________________________________________________________
19 // Utility Class for handling Raw data
20 // Does all transitions from Digits to Raw and vice versa,
21 // for simu and reconstruction
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.
27 //*-- Author: Marco van Leeuwen (LBL)
29 #include "AliEMCALRawUtils.h"
39 #include "AliRunLoader.h"
40 class AliCaloAltroMapping;
41 #include "AliAltroBuffer.h"
42 #include "AliRawReader.h"
43 #include "AliCaloRawStreamV3.h"
46 #include "AliEMCALRecParam.h"
47 #include "AliEMCALLoader.h"
48 #include "AliEMCALGeometry.h"
49 class AliEMCALDigitizer;
50 #include "AliEMCALDigit.h"
51 #include "AliEMCALRawDigit.h"
53 #include "AliCaloCalibPedestal.h"
54 #include "AliCaloFastAltroFitv0.h"
55 #include "AliCaloNeuralFit.h"
56 #include "AliCaloBunchInfo.h"
57 #include "AliCaloFitResults.h"
58 #include "AliCaloRawAnalyzerFastFit.h"
59 #include "AliCaloRawAnalyzerNN.h"
60 #include "AliCaloRawAnalyzerLMS.h"
61 #include "AliCaloRawAnalyzerPeakFinder.h"
62 #include "AliCaloRawAnalyzerCrude.h"
64 ClassImp(AliEMCALRawUtils)
66 // Signal shape parameters
67 Int_t AliEMCALRawUtils::fgTimeBins = 256; // number of sampling bins of the raw RO signal (we typically use 15-50; theoretical max is 1k+)
68 Double_t AliEMCALRawUtils::fgTimeBinWidth = 100E-9 ; // each sample is 100 ns
69 Double_t AliEMCALRawUtils::fgTimeTrigger = 1.5E-6 ; // 15 time bins ~ 1.5 musec
71 // some digitization constants
72 Int_t AliEMCALRawUtils::fgThreshold = 1;
73 Int_t AliEMCALRawUtils::fgDDLPerSuperModule = 2; // 2 ddls per SuperModule
74 Int_t AliEMCALRawUtils::fgPedestalValue = 0; // pedestal value for digits2raw, default generate ZS data
75 Double_t AliEMCALRawUtils::fgFEENoise = 3.; // 3 ADC channels of noise (sampled)
77 AliEMCALRawUtils::AliEMCALRawUtils(fitAlgorithm fitAlgo)
78 : fHighLowGainFactor(0.), fOrder(0), fTau(0.), fNoiseThreshold(0),
79 fNPedSamples(0), fGeom(0), fOption(""),
80 fRemoveBadChannels(kTRUE),fFittingAlgorithm(0),fUseFALTRO(kFALSE),fRawAnalyzer(0)
83 //These are default parameters.
84 //Can be re-set from without with setter functions
85 //Already set in the OCDB and passed via setter in the AliEMCALReconstructor
86 fHighLowGainFactor = 16. ; // Adjusted for a low gain range of 82 GeV (10 bits)
87 fOrder = 2; // Order of gamma fn
88 fTau = 2.35; // in units of timebin, from CERN 2007 testbeam
89 fNoiseThreshold = 3; // 3 ADC counts is approx. noise level
90 fNPedSamples = 4; // Less than this value => likely pedestal samples
91 fRemoveBadChannels = kFALSE; // Do not remove bad channels before fitting
92 fUseFALTRO = kTRUE; // Get the trigger FALTRO information and pass it to digits.
93 SetFittingAlgorithm(fitAlgo);
95 //Get Mapping RCU files from the AliEMCALRecParam
96 const TObjArray* maps = AliEMCALRecParam::GetMappings();
97 if(!maps) AliFatal("Cannot retrieve ALTRO mappings!!");
99 for(Int_t i = 0; i < 4; i++) {
100 fMapping[i] = (AliAltroMapping*)maps->At(i);
103 //To make sure we match with the geometry in a simulation file,
104 //let's try to get it first. If not, take the default geometry
105 AliRunLoader *rl = AliRunLoader::Instance();
106 if (rl && rl->GetAliRun() && rl->GetAliRun()->GetDetector("EMCAL")) {
107 fGeom = dynamic_cast<AliEMCAL*>(rl->GetAliRun()->GetDetector("EMCAL"))->GetGeometry();
109 AliInfo(Form("Using default geometry in raw reco"));
110 fGeom = AliEMCALGeometry::GetInstance(AliEMCALGeometry::GetDefaultGeometryName());
113 if(!fGeom) AliFatal(Form("Could not get geometry!"));
117 //____________________________________________________________________________
118 AliEMCALRawUtils::AliEMCALRawUtils(AliEMCALGeometry *pGeometry, fitAlgorithm fitAlgo)
119 : fHighLowGainFactor(0.), fOrder(0), fTau(0.), fNoiseThreshold(0),
120 fNPedSamples(0), fGeom(pGeometry), fOption(""),
121 fRemoveBadChannels(kTRUE),fFittingAlgorithm(0),fUseFALTRO(kFALSE),fRawAnalyzer()
124 // Initialize with the given geometry - constructor required by HLT
125 // HLT does not use/support AliRunLoader(s) instances
126 // This is a minimum intervention solution
127 // Comment by MPloskon@lbl.gov
130 //These are default parameters.
131 //Can be re-set from without with setter functions
132 //Already set in the OCDB and passed via setter in the AliEMCALReconstructor
133 fHighLowGainFactor = 16. ; // adjusted for a low gain range of 82 GeV (10 bits)
134 fOrder = 2; // order of gamma fn
135 fTau = 2.35; // in units of timebin, from CERN 2007 testbeam
136 fNoiseThreshold = 3; // 3 ADC counts is approx. noise level
137 fNPedSamples = 4; // Less than this value => likely pedestal samples
138 fRemoveBadChannels = kFALSE; // Do not remove bad channels before fitting
139 fUseFALTRO = kTRUE; // Get the trigger FALTRO information and pass it to digits.
140 SetFittingAlgorithm(fitAlgo);
142 //Get Mapping RCU files from the AliEMCALRecParam
143 const TObjArray* maps = AliEMCALRecParam::GetMappings();
144 if(!maps) AliFatal("Cannot retrieve ALTRO mappings!!");
146 for(Int_t i = 0; i < 4; i++) {
147 fMapping[i] = (AliAltroMapping*)maps->At(i);
150 if(!fGeom) AliFatal(Form("Could not get geometry!"));
154 //____________________________________________________________________________
155 AliEMCALRawUtils::AliEMCALRawUtils(const AliEMCALRawUtils& rawU)
157 fHighLowGainFactor(rawU.fHighLowGainFactor),
160 fNoiseThreshold(rawU.fNoiseThreshold),
161 fNPedSamples(rawU.fNPedSamples),
163 fOption(rawU.fOption),
164 fRemoveBadChannels(rawU.fRemoveBadChannels),
165 fFittingAlgorithm(rawU.fFittingAlgorithm),
166 fUseFALTRO(rawU.fUseFALTRO),
167 fRawAnalyzer(rawU.fRawAnalyzer)
170 fMapping[0] = rawU.fMapping[0];
171 fMapping[1] = rawU.fMapping[1];
172 fMapping[2] = rawU.fMapping[2];
173 fMapping[3] = rawU.fMapping[3];
176 //____________________________________________________________________________
177 AliEMCALRawUtils& AliEMCALRawUtils::operator =(const AliEMCALRawUtils &rawU)
179 //assignment operator
182 fHighLowGainFactor = rawU.fHighLowGainFactor;
183 fOrder = rawU.fOrder;
185 fNoiseThreshold = rawU.fNoiseThreshold;
186 fNPedSamples = rawU.fNPedSamples;
188 fOption = rawU.fOption;
189 fRemoveBadChannels = rawU.fRemoveBadChannels;
190 fFittingAlgorithm = rawU.fFittingAlgorithm;
191 fUseFALTRO = rawU.fUseFALTRO;
192 fRawAnalyzer = rawU.fRawAnalyzer;
193 fMapping[0] = rawU.fMapping[0];
194 fMapping[1] = rawU.fMapping[1];
195 fMapping[2] = rawU.fMapping[2];
196 fMapping[3] = rawU.fMapping[3];
203 //____________________________________________________________________________
204 AliEMCALRawUtils::~AliEMCALRawUtils() {
209 //____________________________________________________________________________
210 void AliEMCALRawUtils::Digits2Raw()
212 // convert digits of the current event to raw data
214 AliRunLoader *rl = AliRunLoader::Instance();
215 AliEMCALLoader *loader = dynamic_cast<AliEMCALLoader*>(rl->GetDetectorLoader("EMCAL"));
218 loader->LoadDigits("EMCAL");
220 TClonesArray* digits = loader->Digits() ;
223 Warning("Digits2Raw", "no digits found !");
227 static const Int_t nDDL = 12*2; // 12 SM hardcoded for now. Buffers allocated dynamically, when needed, so just need an upper limit here
228 AliAltroBuffer* buffers[nDDL];
229 for (Int_t i=0; i < nDDL; i++)
232 TArrayI adcValuesLow(fgTimeBins);
233 TArrayI adcValuesHigh(fgTimeBins);
235 // loop over digits (assume ordered digits)
236 for (Int_t iDigit = 0; iDigit < digits->GetEntries(); iDigit++) {
237 AliEMCALDigit* digit = dynamic_cast<AliEMCALDigit *>(digits->At(iDigit)) ;
238 if (digit->GetAmp() < fgThreshold)
248 fGeom->GetCellIndex(digit->GetId(), nSM, nModule, nIphi, nIeta);
249 fGeom->GetCellPhiEtaIndexInSModule(nSM, nModule, nIphi, nIeta,iphi, ieta) ;
251 //Check which is the RCU, 0 or 1, of the cell.
254 if (0<=iphi&&iphi<8) iRCU=0; // first cable row
255 else if (8<=iphi&&iphi<16 && 0<=ieta&&ieta<24) iRCU=0; // first half;
258 else if(8<=iphi&&iphi<16 && 24<=ieta&&ieta<48) iRCU=1; // second half;
260 else if(16<=iphi&&iphi<24) iRCU=1; // third cable row
262 if (nSM%2==1) iRCU = 1 - iRCU; // swap for odd=C side, to allow us to cable both sides the same
265 Fatal("Digits2Raw()","Non-existent RCU number: %d", iRCU);
268 Int_t iDDL = fgDDLPerSuperModule* nSM + iRCU;
270 Fatal("Digits2Raw()","Non-existent DDL board number: %d", iDDL);
272 if (buffers[iDDL] == 0) {
273 // open new file and write dummy header
274 TString fileName = AliDAQ::DdlFileName("EMCAL",iDDL);
275 //Select mapping file RCU0A, RCU0C, RCU1A, RCU1C
276 Int_t iRCUside=iRCU+(nSM%2)*2;
277 //iRCU=0 and even (0) SM -> RCU0A.data 0
278 //iRCU=1 and even (0) SM -> RCU1A.data 1
279 //iRCU=0 and odd (1) SM -> RCU0C.data 2
280 //iRCU=1 and odd (1) SM -> RCU1C.data 3
281 //cout<<" nSM "<<nSM<<"; iRCU "<<iRCU<<"; iRCUside "<<iRCUside<<endl;
282 buffers[iDDL] = new AliAltroBuffer(fileName.Data(),fMapping[iRCUside]);
283 buffers[iDDL]->WriteDataHeader(kTRUE, kFALSE); //Dummy;
286 // out of time range signal (?)
287 if (digit->GetTimeR() > GetRawFormatTimeMax() ) {
288 AliInfo("Signal is out of time range.\n");
289 buffers[iDDL]->FillBuffer((Int_t)digit->GetAmp());
290 buffers[iDDL]->FillBuffer(GetRawFormatTimeBins() ); // time bin
291 buffers[iDDL]->FillBuffer(3); // bunch length
292 buffers[iDDL]->WriteTrailer(3, ieta, iphi, nSM); // trailer
293 // calculate the time response function
295 Bool_t lowgain = RawSampledResponse(digit->GetTimeR(), digit->GetAmp(), adcValuesHigh.GetArray(), adcValuesLow.GetArray()) ;
297 buffers[iDDL]->WriteChannel(ieta, iphi, 0, GetRawFormatTimeBins(), adcValuesLow.GetArray(), fgThreshold);
299 buffers[iDDL]->WriteChannel(ieta,iphi, 1, GetRawFormatTimeBins(), adcValuesHigh.GetArray(), fgThreshold);
303 // write headers and close files
304 for (Int_t i=0; i < nDDL; i++) {
307 buffers[i]->WriteDataHeader(kFALSE, kFALSE);
312 loader->UnloadDigits();
315 //____________________________________________________________________________
316 void AliEMCALRawUtils::Raw2Digits(AliRawReader* reader,TClonesArray *digitsArr, const AliCaloCalibPedestal* pedbadmap, TClonesArray *digitsTRG)
318 // convert raw data of the current event to digits
323 Error("Raw2Digits", "no digits found !");
327 Error("Raw2Digits", "no raw reader found !");
331 AliCaloRawStreamV3 in(reader,"EMCAL",fMapping);
332 // Select EMCAL DDL's;
333 reader->Select("EMCAL",0,43); // 43 = AliEMCALGeoParams::fgkLastAltroDDL
335 // fRawAnalyzer setup
336 fRawAnalyzer->SetNsampleCut(5); // requirement for fits to be done
337 fRawAnalyzer->SetAmpCut(fNoiseThreshold);
338 fRawAnalyzer->SetFitArrayCut(fNoiseThreshold);
339 fRawAnalyzer->SetIsZeroSuppressed(true); // TMP - should use stream->IsZeroSuppressed(), or altro cfg registers later
341 // channel info parameters
343 Int_t caloFlag = 0; // low, high gain, or TRU, or LED ref.
345 // start loop over input stream
346 while (in.NextDDL()) {
348 // if ( in.GetDDLNumber() != 0 && in.GetDDLNumber() != 2 ) continue;
350 while (in.NextChannel()) {
353 Int_t hhwAdd = in.GetHWAddress();
354 UShort_t iiBranch = ( hhwAdd >> 11 ) & 0x1; // 0/1
355 UShort_t iiFEC = ( hhwAdd >> 7 ) & 0xF;
356 UShort_t iiChip = ( hhwAdd >> 4 ) & 0x7;
357 UShort_t iiChannel = hhwAdd & 0xF;
359 if ( !( iiBranch == 0 && iiFEC == 1 && iiChip == 3 && ( iiChannel >= 8 && iiChannel <= 15 ) ) && !( iiBranch == 1 && iiFEC == 0 && in.GetColumn() == 0 ) ) continue;
362 //Check if the signal is high or low gain and then do the fit,
363 //if it is from TRU or LEDMon do not fit
364 caloFlag = in.GetCaloFlag();
365 // if (caloFlag != 0 && caloFlag != 1) continue;
366 if (caloFlag > 2) continue; // Work with ALTRO and FALTRO
368 //Do not fit bad channels of ALTRO
369 if(caloFlag < 2 && fRemoveBadChannels && pedbadmap->IsBadChannel(in.GetModule(),in.GetColumn(),in.GetRow())) {
370 //printf("Tower from SM %d, column %d, row %d is BAD!!! Skip \n", in.GetModule(),in.GetColumn(),in.GetRow());
374 vector<AliCaloBunchInfo> bunchlist;
375 while (in.NextBunch()) {
376 bunchlist.push_back( AliCaloBunchInfo(in.GetStartTimeBin(), in.GetBunchLength(), in.GetSignals() ) );
377 } // loop over bunches
380 if ( caloFlag < 2 ){ // ALTRO
384 short timeEstimate = 0;
385 Float_t ampEstimate = 0;
386 Bool_t fitDone = kFALSE;
388 if ( fFittingAlgorithm == kFastFit || fFittingAlgorithm == kNeuralNet || fFittingAlgorithm == kLMS || fFittingAlgorithm == kPeakFinder || fFittingAlgorithm == kCrude) {
389 // all functionality to determine amp and time etc is encapsulated inside the Evaluate call for these methods
390 AliCaloFitResults fitResults = fRawAnalyzer->Evaluate( bunchlist, in.GetAltroCFG1(), in.GetAltroCFG2());
392 amp = fitResults.GetAmp();
393 time = fitResults.GetTime();
394 timeEstimate = fitResults.GetMaxTimebin();
395 ampEstimate = fitResults.GetMaxSig();
396 if (fitResults.GetStatus() == AliCaloFitResults::kFitPar) {
400 else { // for the other methods we for now use the functionality of
401 // AliCaloRawAnalyzer as well, to select samples and prepare for fits,
402 // if it looks like there is something to fit
405 Float_t pedEstimate = 0;
409 Int_t bunchIndex = 0;
411 // The PreFitEvaluateSamples + later call to FitRaw will hopefully
412 // be replaced by a single Evaluate call or so soon, like for the other
413 // methods, but this should be good enough for evaluation of
414 // the methods for now (Jan. 2010)
416 int nsamples = fRawAnalyzer->PreFitEvaluateSamples( bunchlist, in.GetAltroCFG1(), in.GetAltroCFG2(), bunchIndex, ampEstimate, maxADC, timeEstimate, pedEstimate, first, last);
418 if (ampEstimate > fNoiseThreshold) { // something worth looking at
420 time = timeEstimate; // maxrev in AliCaloRawAnalyzer speak; comes with an offset w.r.t. real timebin
421 Int_t timebinOffset = bunchlist.at(bunchIndex).GetStartBin() - (bunchlist.at(bunchIndex).GetLength()-1);
424 if ( nsamples > 1 ) { // possibly something to fit
425 FitRaw(first, last, amp, time, fitDone);
426 time += timebinOffset;
427 timeEstimate += timebinOffset;
430 } // ampEstimate check
431 } // method selection
433 if ( fitDone ) { // brief sanity check of fit results
434 Float_t ampAsymm = (amp - ampEstimate)/(amp + ampEstimate);
435 Float_t timeDiff = time - timeEstimate;
436 if ( (TMath::Abs(ampAsymm) > 0.1) || (TMath::Abs(timeDiff) > 2) ) {
437 // AliDebug(2,Form("Fit results amp %f time %f not consistent with expectations amp %f time %d", amp, time, ampEstimate, timeEstimate));
439 // for now just overwrite the fit results with the simple/initial estimate
446 if (amp > fNoiseThreshold && amp<fgkRawSignalOverflow) { // something to be stored
447 if ( ! fitDone) { // smear ADC with +- 0.5 uniform (avoid discrete effects)
448 amp += (0.5 - gRandom->Rndm()); // Rndm generates a number in ]0,1]
451 Int_t id = fGeom->GetAbsCellIdFromCellIndexes(in.GetModule(), in.GetRow(), in.GetColumn()) ;
452 lowGain = in.IsLowGain();
454 // go from time-bin units to physical time fgtimetrigger
455 time = time * GetRawFormatTimeBinWidth(); // skip subtraction of fgTimeTrigger?
457 AliDebug(2,Form("id %d lowGain %d amp %g", id, lowGain, amp));
458 // printf("Added tower: SM %d, row %d, column %d, amp %3.2f\n",in.GetModule(), in.GetRow(), in.GetColumn(),amp);
459 // round off amplitude value to nearest integer
460 AddDigit(digitsArr, id, lowGain, TMath::Nint(amp), time);
466 // if (maxTimeBin && gSig->GetN() > maxTimeBin + 10) gSig->Set(maxTimeBin + 10); // set actual max size of TGraph
467 Int_t hwAdd = in.GetHWAddress();
468 UShort_t iRCU = in.GetDDLNumber() % 2; // 0/1
469 UShort_t iBranch = ( hwAdd >> 11 ) & 0x1; // 0/1
471 // Now find TRU number
472 Int_t itru = 3 * in.GetModule() + ( (iRCU << 1) | iBranch ) - 1;
474 AliDebug(1,Form("Found TRG digit in TRU: %2d ADC: %2d",itru,in.GetColumn()));
478 Bool_t isOK = fGeom->GetAbsFastORIndexFromTRU(itru, in.GetColumn(), idtrg);
480 Int_t timeSamples[256]; for (Int_t j=0;j<256;j++) timeSamples[j] = 0;
483 for (std::vector<AliCaloBunchInfo>::iterator itVectorData = bunchlist.begin(); itVectorData != bunchlist.end(); itVectorData++)
485 AliCaloBunchInfo bunch = *(itVectorData);
487 const UShort_t* sig = bunch.GetData();
488 Int_t startBin = bunch.GetStartBin();
490 for (Int_t iS = 0; iS < bunch.GetLength(); iS++)
492 Int_t time = startBin--;
495 if ( amp ) timeSamples[nSamples++] = ( ( time << 12 ) & 0xFF000 ) | ( amp & 0xFFF );
499 if (nSamples && isOK) AddDigit(digitsTRG, idtrg, timeSamples, nSamples);
501 } // end while over channel
502 } //end while over DDL's, of input stream
507 //____________________________________________________________________________
508 void AliEMCALRawUtils::AddDigit(TClonesArray *digitsArr, Int_t id, Int_t timeSamples[], Int_t nSamples)
510 new((*digitsArr)[digitsArr->GetEntriesFast()]) AliEMCALRawDigit(id, timeSamples, nSamples);
512 // Int_t idx = digitsArr->GetEntriesFast()-1;
513 // AliEMCALRawDigit* d = (AliEMCALRawDigit*)digitsArr->At(idx);
516 //____________________________________________________________________________
517 void AliEMCALRawUtils::AddDigit(TClonesArray *digitsArr, Int_t id, Int_t lowGain, Int_t amp, Float_t time) {
520 // This routine checks whether a digit exists already for this tower
521 // and then decides whether to use the high or low gain info
523 // Called by Raw2Digits
525 AliEMCALDigit *digit = 0, *tmpdigit = 0;
526 TIter nextdigit(digitsArr);
527 while (digit == 0 && (tmpdigit = (AliEMCALDigit*) nextdigit())) {
528 if (tmpdigit->GetId() == id)
532 if (!digit) { // no digit existed for this tower; create one
533 if (lowGain && amp > fgkOverflowCut)
534 amp = Int_t(fHighLowGainFactor * amp);
535 Int_t idigit = digitsArr->GetEntries();
536 new((*digitsArr)[idigit]) AliEMCALDigit( -1, -1, id, amp, time, idigit) ;
538 else { // a digit already exists, check range
539 // (use high gain if signal < cut value, otherwise low gain)
540 if (lowGain) { // new digit is low gain
541 if (digit->GetAmp() > fgkOverflowCut) { // use if stored digit is out of range
542 digit->SetAmp(Int_t(fHighLowGainFactor * amp));
543 digit->SetTime(time);
546 else if (amp < fgkOverflowCut) { // new digit is high gain; use if not out of range
548 digit->SetTime(time);
553 //____________________________________________________________________________
554 void AliEMCALRawUtils::FitRaw(const Int_t firstTimeBin, const Int_t lastTimeBin, Float_t & amp, Float_t & time, Bool_t & fitDone) const
555 { // Fits the raw signal time distribution
557 //--------------------------------------------------
558 //Do the fit, different fitting algorithms available
559 //--------------------------------------------------
560 int nsamples = lastTimeBin - firstTimeBin + 1;
563 switch(fFittingAlgorithm) {
566 if (nsamples < 3) { return; } // nothing much to fit
567 //printf("Standard fitter \n");
569 // Create Graph to hold data we will fit
570 TGraph *gSig = new TGraph( nsamples);
571 for (int i=0; i<nsamples; i++) {
572 Int_t timebin = firstTimeBin + i;
573 gSig->SetPoint(i, timebin, fRawAnalyzer->GetReversed(timebin));
576 TF1 * signalF = new TF1("signal", RawResponseFunction, 0, GetRawFormatTimeBins(), 5);
577 signalF->SetParameters(10.,5.,fTau,fOrder,0.); //set all defaults once, just to be safe
578 signalF->SetParNames("amp","t0","tau","N","ped");
579 signalF->FixParameter(2,fTau); // tau in units of time bin
580 signalF->FixParameter(3,fOrder); // order
581 signalF->FixParameter(4, 0); // pedestal should be subtracted when we get here
582 signalF->SetParameter(1, time);
583 signalF->SetParameter(0, amp);
584 // set rather loose parameter limits
585 signalF->SetParLimits(0, 0.5*amp, 2*amp );
586 signalF->SetParLimits(1, time - 4, time + 4);
589 gSig->Fit(signalF, "QROW"); // Note option 'W': equal errors on all points
590 // assign fit results
591 amp = signalF->GetParameter(0);
592 time = signalF->GetParameter(1);
594 // cross-check with ParabolaFit to see if the results make sense
595 FitParabola(gSig, amp); // amp is possibly updated
598 catch (const std::exception & e) {
599 AliError( Form("TGraph Fit exception %s", e.what()) );
600 // stay with default amp and time in case of exception, i.e. no special action required
605 //printf("Std : Amp %f, time %g\n",amp, time);
606 delete gSig; // delete TGraph
610 //----------------------------
613 if (nsamples < 3) { return; } // nothing much to fit
614 //printf("LogFit \n");
616 // Create Graph to hold data we will fit
617 TGraph *gSigLog = new TGraph( nsamples);
618 for (int i=0; i<nsamples; i++) {
619 Int_t timebin = firstTimeBin + i;
620 gSigLog->SetPoint(timebin, timebin, TMath::Log(fRawAnalyzer->GetReversed(timebin) ) );
623 TF1 * signalFLog = new TF1("signalLog", RawResponseFunctionLog, 0, GetRawFormatTimeBins(), 5);
624 signalFLog->SetParameters(2.3, 5.,fTau,fOrder,0.); //set all defaults once, just to be safe
625 signalFLog->SetParNames("amplog","t0","tau","N","ped");
626 signalFLog->FixParameter(2,fTau); // tau in units of time bin
627 signalFLog->FixParameter(3,fOrder); // order
628 signalFLog->FixParameter(4, 0); // pedestal should be subtracted when we get here
629 signalFLog->SetParameter(1, time);
631 signalFLog->SetParameter(0, TMath::Log(amp));
634 gSigLog->Fit(signalFLog, "QROW"); // Note option 'W': equal errors on all points
636 // assign fit results
637 Double_t amplog = signalFLog->GetParameter(0); //Not Amp, but Log of Amp
638 amp = TMath::Exp(amplog);
639 time = signalFLog->GetParameter(1);
643 //printf("LogFit: Amp %f, time %g\n",amp, time);
647 //----------------------------
649 //----------------------------
650 }//switch fitting algorithms
655 //__________________________________________________________________
656 void AliEMCALRawUtils::FitParabola(const TGraph *gSig, Float_t & amp) const
658 //BEG YS alternative methods to calculate the amplitude
659 Double_t * ymx = gSig->GetX() ;
660 Double_t * ymy = gSig->GetY() ;
662 Double_t ymMaxX[kN] = {0., 0., 0.} ;
663 Double_t ymMaxY[kN] = {0., 0., 0.} ;
665 // find the maximum amplitude
667 for (Int_t ymi = 0; ymi < gSig->GetN(); ymi++) {
668 if (ymy[ymi] > ymMaxY[0] ) {
669 ymMaxY[0] = ymy[ymi] ; //<========== This is the maximum amplitude
670 ymMaxX[0] = ymx[ymi] ;
674 // find the maximum by fitting a parabola through the max and the two adjacent samples
675 if ( ymiMax < gSig->GetN()-1 && ymiMax > 0) {
676 ymMaxY[1] = ymy[ymiMax+1] ;
677 ymMaxY[2] = ymy[ymiMax-1] ;
678 ymMaxX[1] = ymx[ymiMax+1] ;
679 ymMaxX[2] = ymx[ymiMax-1] ;
680 if (ymMaxY[0]*ymMaxY[1]*ymMaxY[2] > 0) {
681 //fit a parabola through the 3 points y= a+bx+x*x*x
689 for (Int_t i = 0; i < kN ; i++) {
692 sx2 += ymMaxX[i]*ymMaxX[i] ;
693 sx3 += ymMaxX[i]*ymMaxX[i]*ymMaxX[i] ;
694 sx4 += ymMaxX[i]*ymMaxX[i]*ymMaxX[i]*ymMaxX[i] ;
695 sxy += ymMaxX[i]*ymMaxY[i] ;
696 sx2y += ymMaxX[i]*ymMaxX[i]*ymMaxY[i] ;
698 Double_t cN = (sx2y*kN-sy*sx2)*(sx3*sx-sx2*sx2)-(sx2y*sx-sxy*sx2)*(sx3*kN-sx*sx2);
699 Double_t cD = (sx4*kN-sx2*sx2)*(sx3*sx-sx2*sx2)-(sx4*sx-sx3*sx2)*(sx3*kN-sx*sx2) ;
700 Double_t c = cN / cD ;
701 Double_t b = ((sx2y*kN-sy*sx2)-c*(sx4*kN-sx2*sx2))/(sx3*kN-sx*sx2) ;
702 Double_t a = (sy-b*sx-c*sx2)/kN ;
703 Double_t xmax = -b/(2*c) ;
704 ymax = a + b*xmax + c*xmax*xmax ;//<========== This is the maximum amplitude
709 Double_t diff = TMath::Abs(1-ymMaxY[0]/amp) ;
712 //printf("Yves : Amp %f, time %g\n",amp, time);
717 //__________________________________________________________________
718 Double_t AliEMCALRawUtils::RawResponseFunction(Double_t *x, Double_t *par)
720 // Matches version used in 2007 beam test
722 // Shape of the electronics raw reponse:
723 // It is a semi-gaussian, 2nd order Gamma function of the general form
725 // xx = (t - t0 + tau) / tau [xx is just a convenient help variable]
726 // F = A * (xx**N * exp( N * ( 1 - xx) ) for xx >= 0
730 // A: par[0] // Amplitude = peak value
737 Double_t tau =par[2];
739 Double_t ped = par[4];
740 Double_t xx = ( x[0] - par[1] + tau ) / tau ;
745 signal = ped + par[0] * TMath::Power(xx , n) * TMath::Exp(n * (1 - xx )) ;
750 //__________________________________________________________________
751 Double_t AliEMCALRawUtils::RawResponseFunctionLog(Double_t *x, Double_t *par)
753 // Matches version used in 2007 beam test
755 // Shape of the electronics raw reponse:
756 // It is a semi-gaussian, 2nd order Gamma function of the general form
758 // xx = (t - t0 + tau) / tau [xx is just a convenient help variable]
759 // F = A * (xx**N * exp( N * ( 1 - xx) ) for xx >= 0
763 // Log[A]: par[0] // Amplitude = peak value
770 Double_t tau =par[2];
772 //Double_t ped = par[4]; // not used
773 Double_t xx = ( x[0] - par[1] + tau ) / tau ;
776 signal = par[0] - n*TMath::Log(TMath::Abs(xx)) + n * (1 - xx ) ;
778 signal = par[0] + n*TMath::Log(xx) + n * (1 - xx ) ;
783 //__________________________________________________________________
784 Bool_t AliEMCALRawUtils::RawSampledResponse(const Double_t dtime, const Double_t damp, Int_t * adcH, Int_t * adcL, const Int_t keyErr) const
786 // for a start time dtime and an amplitude damp given by digit,
787 // calculates the raw sampled response AliEMCAL::RawResponseFunction
789 Bool_t lowGain = kFALSE ;
791 // A: par[0] // Amplitude = peak value
797 TF1 signalF("signal", RawResponseFunction, 0, GetRawFormatTimeBins(), 5);
798 signalF.SetParameter(0, damp) ;
799 signalF.SetParameter(1, (dtime + fgTimeTrigger)/fgTimeBinWidth) ;
800 signalF.SetParameter(2, fTau) ;
801 signalF.SetParameter(3, fOrder);
802 signalF.SetParameter(4, fgPedestalValue);
804 Double_t signal=0.0, noise=0.0;
805 for (Int_t iTime = 0; iTime < GetRawFormatTimeBins(); iTime++) {
806 signal = signalF.Eval(iTime) ;
808 // Next lines commeted for the moment but in principle it is not necessary to add
809 // extra noise since noise already added at the digits level.
811 //According to Terry Awes, 13-Apr-2008
812 //add gaussian noise in quadrature to each sample
813 //Double_t noise = gRandom->Gaus(0.,fgFEENoise);
814 //signal = sqrt(signal*signal + noise*noise);
816 // March 17,09 for fast fit simulations by Alexei Pavlinov.
817 // Get from PHOS analysis. In some sense it is open questions.
819 noise = gRandom->Gaus(0.,fgFEENoise);
823 adcH[iTime] = static_cast<Int_t>(signal + 0.5) ;
824 if ( adcH[iTime] > fgkRawSignalOverflow ){ // larger than 10 bits
825 adcH[iTime] = fgkRawSignalOverflow ;
829 signal /= fHighLowGainFactor;
831 adcL[iTime] = static_cast<Int_t>(signal + 0.5) ;
832 if ( adcL[iTime] > fgkRawSignalOverflow) // larger than 10 bits
833 adcL[iTime] = fgkRawSignalOverflow ;
838 //__________________________________________________________________
839 void AliEMCALRawUtils::SetFittingAlgorithm(Int_t fitAlgo)
841 //Set fitting algorithm and initialize it if this same algorithm was not set before.
842 //printf("**** Set Algorithm , number %d ****\n",fitAlgo);
844 if(fitAlgo == fFittingAlgorithm && fRawAnalyzer) {
845 //Do nothing, this same algorithm already set before.
846 //printf("**** Algorithm already set before, number %d, %s ****\n",fitAlgo, fRawAnalyzer->GetName());
849 //Initialize the requested algorithm
850 if(fitAlgo != fFittingAlgorithm || !fRawAnalyzer) {
851 //printf("**** Init Algorithm , number %d ****\n",fitAlgo);
853 fFittingAlgorithm = fitAlgo;
854 if (fRawAnalyzer) delete fRawAnalyzer; // delete prev. analyzer if existed.
856 if (fitAlgo == kFastFit) {
857 fRawAnalyzer = new AliCaloRawAnalyzerFastFit();
859 else if (fitAlgo == kNeuralNet) {
860 fRawAnalyzer = new AliCaloRawAnalyzerNN();
862 else if (fitAlgo == kLMS) {
863 fRawAnalyzer = new AliCaloRawAnalyzerLMS();
865 else if (fitAlgo == kPeakFinder) {
866 fRawAnalyzer = new AliCaloRawAnalyzerPeakFinder();
868 else if (fitAlgo == kCrude) {
869 fRawAnalyzer = new AliCaloRawAnalyzerCrude();
872 fRawAnalyzer = new AliCaloRawAnalyzer();