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 ),
57 snprintf(fName, 256, "%s", name);
58 snprintf(fNameShort,256, "%s", nameshort);
60 for(int i=0; i < ALTROMAXSAMPLES; i++ )
68 AliCaloRawAnalyzer::SetTimeConstraint(const int min, const int max )
70 //Require that the bin if the maximum ADC value is between min and max (timebin)
72 if( ( min > max ) || min > ALTROMAXSAMPLES || max > ALTROMAXSAMPLES )
74 AliWarning( Form( "Attempt to set Invalid time bin range (Min , Max) = (%d, %d), Ingored", min, max ) );
85 AliCaloRawAnalyzer::Max(const UShort_t *data, const int length ) const
87 // Get maximum of array
89 UShort_t tmpmax = data[0];
91 for(int i=0; i < length; i++)
93 if( tmpmax < data[i] )
103 AliCaloRawAnalyzer::SelectSubarray( const Double_t *data, int length, short maxindex,
104 int * first, int * last, int cut) const
106 //Selection of subset of data from one bunch that will be used for fitting or
107 //Peak finding. Go to the left and right of index of the maximum time bin
108 //Until the ADC value is less that fFitArrayCut, or derivative changes sign (data jump)
109 int tmpfirst = maxindex;
110 int tmplast = maxindex;
111 Double_t prevFirst = data[maxindex];
112 Double_t prevLast = data[maxindex];
113 bool firstJump = false;
114 bool lastJump = false;
116 while( (tmpfirst >= 0) && (data[tmpfirst] >= cut ) && (!firstJump) )
119 if (tmpfirst != maxindex) { // neighbor to maxindex can share peak with maxindex
120 if ( data[tmpfirst] >= prevFirst) {
124 prevFirst = data[tmpfirst];
128 while( (tmplast < length) && (data[tmplast] >= cut ) && (!lastJump) )
131 if (tmplast != maxindex) { // neighbor to maxindex can share peak with maxindex
132 if ( data[tmplast] >= prevLast) {
136 prevLast = data[tmplast];
140 // we keep one pre- or post- sample if we can (as in online)
141 // check though if we ended up on a 'jump', or out of bounds: if so, back up
142 if (firstJump || tmpfirst<0) tmpfirst ++;
143 if (lastJump || tmplast>=length) tmplast --;
154 AliCaloRawAnalyzer::ReverseAndSubtractPed( const AliCaloBunchInfo *bunch,
155 UInt_t /*altrocfg1*/, UInt_t /*altrocfg2*/,
156 double *outarray ) const
158 //Time sample comes in reversed order, revers them back
159 //Subtract the baseline based on content of altrocfg1 and altrocfg2.
161 Int_t length = bunch->GetLength();
162 const UShort_t *sig = bunch->GetData();
164 double ped = EvaluatePedestal( sig , length);
166 for( int i=0; i < length; i++ )
168 outarray[i] = sig[length -i -1] - ped;
177 AliCaloRawAnalyzer::EvaluatePedestal(const UShort_t * data, int /*length*/ ) const
179 // Pedestal evaluation if not zero suppressed
183 if( fIsZerosupressed == false )
185 for(int i=0; i < fNsamplePed; i++ )
191 return tmp/fNsamplePed;
196 AliCaloRawAnalyzer::Max( const AliCaloBunchInfo * bunch , int * maxindex ) const
198 // Get maximum in bunch array
202 const UShort_t *sig = bunch->GetData();
204 for(int i=0; i < bunch->GetLength(); i++ )
206 if( sig[i] > tmpmax )
215 // *maxindex = bunch->GetLength() -1 - tmpindex + bunch->GetStartBin();
216 *maxindex = bunch->GetLength() -1 - tmpindex + bunch->GetStartBin();
224 AliCaloRawAnalyzer::CheckBunchEdgesForMax( const AliCaloBunchInfo *const bunch ) const
226 // a bunch is considered invalid if the maximum is in the first or last time-bin
229 const UShort_t *sig = bunch->GetData();
231 for(int i=0; i < bunch->GetLength(); i++ )
233 if( sig[i] > tmpmax )
241 if (tmpindex == 0 || tmpindex == (bunch->GetLength()-1) )
251 AliCaloRawAnalyzer::SelectBunch( const vector<AliCaloBunchInfo> &bunchvector,
252 short * maxampbin, short * maxamplitude )
254 //We select the bunch with the highest amplitude unless any time constraints is set
256 short bunchindex = -1;
260 for(unsigned int i=0; i < bunchvector.size(); i++ )
262 max = Max( &bunchvector.at(i), &indx ); // CRAP PTH, bug fix, trouble if more than one bunches
263 if( IsInTimeRange( indx, fMaxTimeIndex, fMinTimeIndex) )
275 if (bunchindex >= 0) {
276 bool bunchOK = CheckBunchEdgesForMax( &bunchvector.at(bunchindex) );
287 AliCaloRawAnalyzer::IsInTimeRange( const int maxindex, const int maxtindx, const int mintindx ) const
289 // Ckeck if the index of the max ADC vaue is consistent with trigger.
290 if( ( mintindx < 0 && maxtindx < 0) ||maxtindx < 0 )
294 return ( maxindex < maxtindx ) && ( maxindex > mintindx ) ? true : false;
299 AliCaloRawAnalyzer::PrintBunches( const vector<AliCaloBunchInfo> &bvctr )
301 // Print bunch vector info
302 cout << __FILE__ << __LINE__<< "*************** Printing Bunches *******************" << endl;
303 cout << __FILE__ << __LINE__<< "*** There are " << bvctr.size() << ", bunches" << endl;
305 for(unsigned int i=0; i < bvctr.size() ; i++ )
307 PrintBunch( bvctr.at(i) );
308 cout << " bunch = " << i << endl;
310 cout << __FILE__ << __LINE__<< "*************** Done ... *******************" << endl;
315 AliCaloRawAnalyzer::PrintBunch( const AliCaloBunchInfo &bunch )
318 cout << __FILE__ << ":" << __LINE__ << endl;
319 cout << __FILE__ << __LINE__ << ", startimebin = " << bunch.GetStartBin() << ", length =" << bunch.GetLength() << endl;
320 const UShort_t *sig = bunch.GetData();
322 for ( Int_t j = 0; j < bunch.GetLength(); j++)
324 printf("%d\t", sig[j] );
331 AliCaloRawAnalyzer::CalculateChi2( Double_t amp, Double_t time,
332 Int_t first, Int_t last,
333 Double_t adcErr, Double_t tau) const
336 // amp - max amplitude;
337 // time - time of max amplitude;
338 // first, last - sample array indices to be used
339 // adcErr - nominal error of amplitude measurement (one value for all channels)
340 // if adcErr<0 that mean adcErr=1.
341 // tau - filter time response (in timebin units)
345 if (first == last || first<0 ) { // signal consists of single sample, chi2 estimate (0) not too well defined..
346 // or, first is negative, the indices are not valid
350 int nsamples = last - first + 1;
351 // printf(" AliCaloRawAnalyzer::CalculateChi2 : first %i last %i : nsamples %i : amp %3.2f time %3.2f \n", first, last, nsamples, amp, time);
355 Double_t dy = 0.0, xx = 0.0, f=0.0;
357 for (Int_t i=0; i<nsamples; i++) {
358 x = first + i; // timebin
359 xx = (x - time + tau) / tau; // help variable
362 f = amp * xx*xx * TMath::Exp(2 * (1 - xx )) ;
364 dy = fReversed[x] - f;
366 // printf(" AliCaloRawAnalyzer::CalculateChi2 : %i : y %f -> f %f : dy %f \n", i, fReversed[first+i], f, dy);
369 if (adcErr>0.0) { // weight chi2
370 chi2 /= (adcErr*adcErr);
377 AliCaloRawAnalyzer::CalculateMeanAndRMS(Int_t first, Int_t last,
378 Double_t & mean, Double_t & rms)
381 // first, last - sample array indices to be used
383 // mean and RMS of samples
385 // To possibly be used to differentiate good signals from bad before fitting
390 if (first == last || first<0 ) { // signal consists of single sample, chi2 estimate (0) not too well defined..
391 // or, first is negative, the indices are not valid
395 int nsamples = last - first + 1;
396 // printf(" AliCaloRawAnalyzer::CalculateMeanAndRMS : first %i last %i : nsamples %i \n", first, last, nsamples);
399 Double_t sampleSum = 0; // sum of samples
400 Double_t squaredSampleSum = 0; // sum of samples squared
402 for (Int_t i=0; i<nsamples; i++) {
404 sampleSum += fReversed[x];
405 squaredSampleSum += (fReversed[x] * fReversed[x]);
408 mean = sampleSum / nsamples;
409 Double_t squaredMean = squaredSampleSum / nsamples;
410 // The variance (rms squared) is equal to the mean of the squares minus the square of the mean..
411 rms = sqrt(squaredMean - mean*mean);
419 AliCaloRawAnalyzer::PreFitEvaluateSamples( const vector<AliCaloBunchInfo> &bunchvector,
420 UInt_t altrocfg1, UInt_t altrocfg2, Int_t & index,
421 Float_t & maxf, short & maxamp,
422 short & maxrev, Float_t & ped,
423 int & first, int & last, int acut )
425 // method to do the selection of what should possibly be fitted
428 short maxampindex = 0;
429 index = SelectBunch( bunchvector, &maxampindex, &maxamp ); // select the bunch with the highest amplitude unless any time constraints is set
432 if( index >= 0 && maxamp >= acut ) // something valid was found, and non-zero amplitude
434 // use more convenient numbering and possibly subtract pedestal
435 ped = ReverseAndSubtractPed( &(bunchvector.at(index)), altrocfg1, altrocfg2, fReversed );
436 maxf = TMath::MaxElement( bunchvector.at(index).GetLength(), fReversed );
438 if ( maxf >= acut ) // possibly significant signal
440 // select array around max to possibly be used in fit
441 maxrev = maxampindex - bunchvector.at(index).GetStartBin();
442 SelectSubarray( fReversed, bunchvector.at(index).GetLength(), maxrev, &first, &last, acut );
444 // sanity check: maximum should not be in first or last bin
445 // if we should do a fit
446 if (first!=maxrev && last!=maxrev)
448 // calculate how many samples we have
449 nsamples = last - first + 1;