2 /**************************************************************************
3 * This file is property of and copyright by *
4 * the Relativistic Heavy Ion Group (RHIG), Yale University, US, 2009 *
6 * Primary Author: Per Thomas Hille <perthomas.hille@yale.edu> *
8 * Contributors are mentioned in the code where appropriate. *
9 * Please report bugs to p.t.hille@fys.uio.no *
11 * Permission to use, copy, modify and distribute this software and its *
12 * documentation strictly for non-commercial purposes is hereby granted *
13 * without fee, provided that the above copyright notice appears in all *
14 * copies and that both the copyright notice and this permission notice *
15 * appear in the supporting documentation. The authors make no claims *
16 * about the suitability of this software for any purpose. It is *
17 * provided "as is" without express or implied warranty. *
18 **************************************************************************/
21 // Base class for extraction
22 // of signal amplitude and peak position
23 // From CALO Calorimeter RAW data (from the RCU)
24 // Contains some utilities for preparing / selecting
25 // Signals suitable for signal extraction
29 #include "AliCaloRawAnalyzer.h"
30 #include "AliCaloBunchInfo.h"
31 #include "AliCaloFitResults.h"
36 ClassImp(AliCaloRawAnalyzer)
38 AliCaloRawAnalyzer::AliCaloRawAnalyzer(const char *name, const char *nameshort) : TObject(),
46 fIsZerosupressed( false ),
55 snprintf(fName, 256,"%s", name);
56 snprintf(fNameShort,256, "%s", nameshort);
57 // sprintf(fName ,"%s", name);
58 // sprintf(fNameShort, "%s", nameshort);
60 for(int i=0; i < ALTROMAXSAMPLES; i++ )
66 AliCaloRawAnalyzer::~AliCaloRawAnalyzer()
73 AliCaloRawAnalyzer::SetTimeConstraint(const int min, const int max )
75 //Require that the bin if the maximum ADC value is between min and max (timebin)
76 if( ( min > max ) || min > ALTROMAXSAMPLES || max > ALTROMAXSAMPLES )
78 AliWarning( Form( "Attempt to set Invalid time bin range (Min , Max) = (%d, %d), Ingored", min, max ) );
89 AliCaloRawAnalyzer::Max(const UShort_t *data, const int length ) const
92 UShort_t tmpmax = data[0];
94 for(int i=0; i < length; i++)
96 if( tmpmax < data[i] )
106 AliCaloRawAnalyzer::SelectSubarray( const Double_t *data, const int length, const short maxindex,int *const first, int *const last, const int cut) const
108 //Selection of subset of data from one bunch that will be used for fitting or
109 //Peak finding. Go to the left and right of index of the maximum time bin
110 //Until the ADC value is less that fFitArrayCut, or derivative changes sign (data jump)
111 int tmpfirst = maxindex;
112 int tmplast = maxindex;
113 Double_t prevFirst = data[maxindex];
114 Double_t prevLast = data[maxindex];
115 bool firstJump = false;
116 bool lastJump = false;
118 while( (tmpfirst >= 0) && (data[tmpfirst] >= cut ) && (!firstJump) )
121 if (tmpfirst != maxindex) { // neighbor to maxindex can share peak with maxindex
122 if ( data[tmpfirst] >= prevFirst) {
126 prevFirst = data[tmpfirst];
130 while( (tmplast < length) && (data[tmplast] >= cut ) && (!lastJump) )
133 if (tmplast != maxindex) { // neighbor to maxindex can share peak with maxindex
134 if ( data[tmplast] >= prevLast) {
138 prevLast = data[tmplast];
142 // we keep one pre- or post- sample if we can (as in online)
143 // check though if we ended up on a 'jump', or out of bounds: if so, back up
144 if (firstJump || tmpfirst<0) tmpfirst ++;
145 if (lastJump || tmplast>=length) tmplast --;
155 AliCaloRawAnalyzer::ReverseAndSubtractPed( const AliCaloBunchInfo *bunch, const UInt_t /*altrocfg1*/, const UInt_t /*altrocfg2*/, double *outarray ) const
157 //Time sample comes in reversed order, revers them back
158 //Subtract the baseline based on content of altrocfg1 and altrocfg2.
159 Int_t length = bunch->GetLength();
160 const UShort_t *sig = bunch->GetData();
162 double ped = EvaluatePedestal( sig , length);
164 for( int i=0; i < length; i++ )
166 outarray[i] = sig[length -i -1] - ped;
175 AliCaloRawAnalyzer::EvaluatePedestal(const UShort_t * const data, const int /*length*/ ) const
180 if( fIsZerosupressed == false )
182 for(int i=0; i < fNsamplePed; i++ )
188 return tmp/fNsamplePed;
193 AliCaloRawAnalyzer::Max( const AliCaloBunchInfo *const bunch , int *const maxindex ) const
198 const UShort_t *sig = bunch->GetData();
200 for(int i=0; i < bunch->GetLength(); i++ )
202 if( sig[i] > tmpmax )
211 // *maxindex = bunch->GetLength() -1 - tmpindex + bunch->GetStartBin();
212 *maxindex = bunch->GetLength() -1 - tmpindex + bunch->GetStartBin();
220 AliCaloRawAnalyzer::CheckBunchEdgesForMax( const AliCaloBunchInfo *const bunch ) const
222 // a bunch is considered invalid if the maximum is in the first or last time-bin
225 const UShort_t *sig = bunch->GetData();
227 for(int i=0; i < bunch->GetLength(); i++ )
229 if( sig[i] > tmpmax )
237 if (tmpindex == 0 || tmpindex == (bunch->GetLength()-1) )
247 AliCaloRawAnalyzer::SelectBunch( const vector<AliCaloBunchInfo> &bunchvector, short *const maxampbin, short *const maxamplitude )
249 //We select the bunch with the highest amplitude unless any time constraints is set
251 short bunchindex = -1;
255 for(unsigned int i=0; i < bunchvector.size(); i++ )
257 max = Max( &bunchvector.at(i), &indx ); // CRAP PTH, bug fix, trouble if more than one bunches
258 if( IsInTimeRange( indx, fMaxTimeIndex, fMinTimeIndex) )
270 if (bunchindex >= 0) {
271 bool bunchOK = CheckBunchEdgesForMax( &bunchvector.at(bunchindex) );
282 AliCaloRawAnalyzer::IsInTimeRange( const int maxindex, const int maxtindx, const int mintindx ) const
284 // Ckeck if the index of the max ADC vaue is consistent with trigger.
285 if( ( mintindx < 0 && maxtindx < 0) ||maxtindx < 0 )
289 return ( maxindex < maxtindx ) && ( maxindex > mintindx ) ? true : false;
294 AliCaloRawAnalyzer::PrintBunches( const vector<AliCaloBunchInfo> &bvctr )
297 cout << __FILE__ << __LINE__<< "*************** Printing Bunches *******************" << endl;
298 cout << __FILE__ << __LINE__<< "*** There are " << bvctr.size() << ", bunches" << endl;
300 for(unsigned int i=0; i < bvctr.size() ; i++ )
302 PrintBunch( bvctr.at(i) );
303 cout << " bunch = " << i << endl;
305 cout << __FILE__ << __LINE__<< "*************** Done ... *******************" << endl;
310 AliCaloRawAnalyzer::PrintBunch( const AliCaloBunchInfo &bunch )
313 cout << __FILE__ << ":" << __LINE__ << endl;
314 cout << __FILE__ << __LINE__ << ", startimebin = " << bunch.GetStartBin() << ", length =" << bunch.GetLength() << endl;
315 const UShort_t *sig = bunch.GetData();
317 for ( Int_t j = 0; j < bunch.GetLength(); j++)
319 printf("%d\t", sig[j] );
326 AliCaloRawAnalyzer::CalculateChi2(const Double_t amp, const Double_t time,
327 const Int_t first, const Int_t last,
328 const Double_t adcErr,
329 const Double_t tau) const
332 // amp - max amplitude;
333 // time - time of max amplitude;
334 // first, last - sample array indices to be used
335 // adcErr - nominal error of amplitude measurement (one value for all channels)
336 // if adcErr<0 that mean adcErr=1.
337 // tau - filter time response (in timebin units)
341 if (first == last || first<0 ) { // signal consists of single sample, chi2 estimate (0) not too well defined..
342 // or, first is negative, the indices are not valid
346 int nsamples = last - first + 1;
347 // printf(" AliCaloRawAnalyzer::CalculateChi2 : first %i last %i : nsamples %i : amp %3.2f time %3.2f \n", first, last, nsamples, amp, time);
351 Double_t dy = 0.0, xx = 0.0, f=0.0;
353 for (Int_t i=0; i<nsamples; i++) {
354 x = first + i; // timebin
355 xx = (x - time + tau) / tau; // help variable
358 f = amp * xx*xx * TMath::Exp(2 * (1 - xx )) ;
360 dy = fReversed[x] - f;
362 // printf(" AliCaloRawAnalyzer::CalculateChi2 : %i : y %f -> f %f : dy %f \n", i, fReversed[first+i], f, dy);
365 if (adcErr>0.0) { // weight chi2
366 chi2 /= (adcErr*adcErr);
373 AliCaloRawAnalyzer::CalculateMeanAndRMS(const Int_t first, const Int_t last,
374 Double_t & mean, Double_t & rms)
377 // first, last - sample array indices to be used
379 // mean and RMS of samples
381 // To possibly be used to differentiate good signals from bad before fitting
386 if (first == last || first<0 ) { // signal consists of single sample, chi2 estimate (0) not too well defined..
387 // or, first is negative, the indices are not valid
391 int nsamples = last - first + 1;
392 // printf(" AliCaloRawAnalyzer::CalculateMeanAndRMS : first %i last %i : nsamples %i \n", first, last, nsamples);
395 Double_t sampleSum = 0; // sum of samples
396 Double_t squaredSampleSum = 0; // sum of samples squared
398 for (Int_t i=0; i<nsamples; i++) {
400 sampleSum += fReversed[x];
401 squaredSampleSum += (fReversed[x] * fReversed[x]);
404 mean = sampleSum / nsamples;
405 Double_t squaredMean = squaredSampleSum / nsamples;
406 // The variance (rms squared) is equal to the mean of the squares minus the square of the mean..
407 rms = sqrt(squaredMean - mean*mean);
415 AliCaloRawAnalyzer::PreFitEvaluateSamples( const vector<AliCaloBunchInfo> &bunchvector, const UInt_t altrocfg1,
416 const UInt_t altrocfg2, Int_t & index, Float_t & maxf, short & maxamp,
417 short & maxrev, Float_t & ped, int & first, int & last,const int acut )
418 { // method to do the selection of what should possibly be fitted
420 short maxampindex = 0;
421 index = SelectBunch( bunchvector, &maxampindex, &maxamp ); // select the bunch with the highest amplitude unless any time constraints is set
424 if( index >= 0 && maxamp >= acut ) // something valid was found, and non-zero amplitude
426 // use more convenient numbering and possibly subtract pedestal
427 ped = ReverseAndSubtractPed( &(bunchvector.at(index)), altrocfg1, altrocfg2, fReversed );
428 maxf = TMath::MaxElement( bunchvector.at(index).GetLength(), fReversed );
430 if ( maxf >= acut ) // possibly significant signal
432 // select array around max to possibly be used in fit
433 maxrev = maxampindex - bunchvector.at(index).GetStartBin();
434 SelectSubarray( fReversed, bunchvector.at(index).GetLength(), maxrev, &first, &last, acut );
436 // sanity check: maximum should not be in first or last bin
437 // if we should do a fit
438 if (first!=maxrev && last!=maxrev)
440 // calculate how many samples we have
441 nsamples = last - first + 1;