JetCorrel correction of coding violations, and other style and memory consumption...
[u/mrichter/AliRoot.git] / EMCAL / AliCaloRawAnalyzer.cxx
CommitLineData
d655d7dd 1/**************************************************************************
2 * This file is property of and copyright by *
3 * the Relativistic Heavy Ion Group (RHIG), Yale University, US, 2009 *
4 * *
e37e3c84 5 * Primary Author: Per Thomas Hille <perthomas.hille@yale.edu> *
d655d7dd 6 * *
7 * Contributors are mentioned in the code where appropriate. *
8 * Please report bugs to p.t.hille@fys.uio.no *
9 * *
10 * Permission to use, copy, modify and distribute this software and its *
11 * documentation strictly for non-commercial purposes is hereby granted *
12 * without fee, provided that the above copyright notice appears in all *
13 * copies and that both the copyright notice and this permission notice *
14 * appear in the supporting documentation. The authors make no claims *
15 * about the suitability of this software for any purpose. It is *
16 * provided "as is" without express or implied warranty. *
17 **************************************************************************/
18
19
20// Base class for extraction
21// of signal amplitude and peak position
57839add 22// From CALO Calorimeter RAW data (from the RCU)
d655d7dd 23// Contains some utilities for preparing / selecting
24// Signals suitable for signal extraction
25// By derived classes
26
27#include "AliLog.h"
57839add 28#include "AliCaloRawAnalyzer.h"
29#include "AliCaloBunchInfo.h"
30#include "AliCaloFitResults.h"
31#include "TMath.h"
d655d7dd 32#include <iostream>
33using namespace std;
34
48a2e3eb 35//ClassImp(AliCaloRawAnalyzer)
36
37AliCaloRawAnalyzer::AliCaloRawAnalyzer(const char *name, const char *nameshort) : TObject(),
38 fMinTimeIndex(-1),
39 fMaxTimeIndex(-1),
40 fFitArrayCut(5),
41 fAmpCut(4),
42 fNsampleCut(5),
f57baa2d 43 fNsamplePed(3),
48a2e3eb 44 fIsZerosupressed( false ),
45 fVerbose( false )
d655d7dd 46{
e37e3c84 47 //Comment
48 sprintf(fName, "%s", name);
48a2e3eb 49 sprintf(fNameShort, "%s", nameshort);
50
d655d7dd 51 for(int i=0; i < MAXSAMPLES; i++ )
52 {
53 fReversed[i] = 0;
54 }
55}
56
57839add 57AliCaloRawAnalyzer::~AliCaloRawAnalyzer()
d655d7dd 58{
59
60}
61
62
63void
57839add 64AliCaloRawAnalyzer::SetTimeConstraint(const int min, const int max )
d655d7dd 65{
66 //Require that the bin if the maximum ADC value is between min and max (timebin)
67 if( ( min > max ) || min > MAXSAMPLES || max > MAXSAMPLES )
68 {
69 AliWarning( Form( "Attempt to set Invalid time bin range (Min , Max) = (%d, %d), Ingored", min, max ) );
70 }
71 else
72 {
73 fMinTimeIndex = min;
74 fMaxTimeIndex = max;
75 }
76}
77
78
79UShort_t
57839add 80AliCaloRawAnalyzer::Max(const UShort_t *data, const int length ) const
d655d7dd 81{
82 //------------
83 UShort_t tmpmax = data[0];
84
85 for(int i=0; i < length; i++)
86 {
87 if( tmpmax < data[i] )
88 {
89 tmpmax = data[i];
90 }
91 }
92 return tmpmax;
93}
94
95
96void
57839add 97AliCaloRawAnalyzer::SelectSubarray( const Double_t *fData, const int length, const short maxindex, int *const first, int *const last ) const
d655d7dd 98{
99 //Selection of subset of data from one bunch that will be used for fitting or
100 //Peak finding. Go to the left and right of index of the maximum time bin
101 //Untile the ADC value is less that fFitArrayCut
102 int tmpfirst = maxindex;
103 int tmplast = maxindex;
104
105 while(( tmpfirst ) > 0 && ( fData[tmpfirst] > fFitArrayCut ))
106 {
107 tmpfirst -- ;
108 }
109
f57baa2d 110 while(( tmplast ) < (length-1) && ( fData [tmplast] > fFitArrayCut ))
d655d7dd 111 {
112 tmplast ++;
113 }
f57baa2d 114
115
d655d7dd 116 *first = tmpfirst +1;
117 *last = tmplast -1;
f57baa2d 118
119 int nsamples = *last - *first + 1;
120 if (nsamples < fNsampleCut) {
121 // keep edge bins (below threshold) also, for low # of samples case
122 *first = tmpfirst;
123 *last = tmplast;
124 }
125
d655d7dd 126}
127
128
129
130Float_t
57839add 131AliCaloRawAnalyzer::ReverseAndSubtractPed( const AliCaloBunchInfo *bunch, const UInt_t /*altrocfg1*/, const UInt_t /*altrocfg2*/, double *outarray ) const
d655d7dd 132{
133 //Time sample comes in reversed order, revers them back
134 //Subtract the baseline based on content of altrocfg1 and altrocfg2.
135 Int_t length = bunch->GetLength();
136 const UShort_t *sig = bunch->GetData();
137
57839add 138 double ped = EvaluatePedestal( sig , length);
139
140 for( int i=0; i < length; i++ )
141 {
142 outarray[i] = sig[length -i -1] - ped;
143 }
144
145 return ped;
146}
147
148
149
150Float_t
151AliCaloRawAnalyzer::EvaluatePedestal(const UShort_t * const data, const int /*length*/ ) const
152{
153 // double ped = 0;
d655d7dd 154 double tmp = 0;
155
156 if( fIsZerosupressed == false )
157 {
f57baa2d 158 for(int i=0; i < fNsamplePed; i++ )
d655d7dd 159 {
57839add 160 tmp += data[i];
d655d7dd 161 }
f57baa2d 162 }
e37e3c84 163
f57baa2d 164 return tmp/fNsamplePed;
165 }
d655d7dd 166
167
168short
57839add 169AliCaloRawAnalyzer::Max( const AliCaloBunchInfo *const bunch , int *const maxindex ) const
d655d7dd 170{
171 //comment
172 short tmpmax = -1;
173 int tmpindex = -1;
174 const UShort_t *sig = bunch->GetData();
175
176 for(int i=0; i < bunch->GetLength(); i++ )
177 {
178 if( sig[i] > tmpmax )
179 {
180 tmpmax = sig[i];
181 tmpindex = i;
182 }
183 }
184
185 if(maxindex != 0 )
186 {
48a2e3eb 187 // *maxindex = bunch->GetLength() -1 - tmpindex + bunch->GetStartBin();
188 *maxindex = bunch->GetLength() -1 - tmpindex + bunch->GetStartBin();
d655d7dd 189 }
190
191 return tmpmax;
192}
193
194
195int
57839add 196AliCaloRawAnalyzer::SelectBunch( const vector<AliCaloBunchInfo> &bunchvector,short *const maxampbin, short *const maxamplitude ) const
d655d7dd 197{
198 //We select the bunch with the highest amplitude unless any time constraints is set
199 short max = -1;
200 short bunchindex = -1;
201 short maxall = -1;
202 int indx = -1;
203
204 for(unsigned int i=0; i < bunchvector.size(); i++ )
205 {
48a2e3eb 206 max = Max( &bunchvector.at(i), &indx ); // CRAP PTH, bug fix, trouble if more than one bunches
d655d7dd 207 if( IsInTimeRange( indx) )
208 {
209 if( max > maxall )
210 {
211 maxall = max;
212 bunchindex = i;
48a2e3eb 213 *maxampbin = indx;
214 *maxamplitude = max;
d655d7dd 215 }
216 }
217 }
218
48a2e3eb 219
220 // *maxampbin = indx;
221 // *maxamplitude = max;
222
d655d7dd 223 return bunchindex;
224}
225
226
227bool
57839add 228AliCaloRawAnalyzer::IsInTimeRange( const int maxindex ) const
d655d7dd 229{
230 // Ckeck if the index of the max ADC vaue is consistent with trigger.
231 if( ( fMinTimeIndex < 0 && fMaxTimeIndex < 0) ||fMaxTimeIndex < 0 )
232 {
233 return true;
234 }
235 return ( maxindex < fMaxTimeIndex ) && ( maxindex > fMinTimeIndex ) ? true : false;
236}
237
238
239void
57839add 240AliCaloRawAnalyzer::PrintBunches( const vector<AliCaloBunchInfo> &bvctr ) const
d655d7dd 241{
242 //comment
243 cout << __FILE__ << __LINE__<< "*************** Printing Bunches *******************" << endl;
244 cout << __FILE__ << __LINE__<< "*** There are " << bvctr.size() << ", bunches" << endl;
245
246 for(unsigned int i=0; i < bvctr.size() ; i++ )
247 {
248 PrintBunch( bvctr.at(i) );
249 cout << " bunch = " << i << endl;
250 }
251 cout << __FILE__ << __LINE__<< "*************** Done ... *******************" << endl;
252}
253
254
255void
57839add 256AliCaloRawAnalyzer::PrintBunch( const AliCaloBunchInfo &bunch ) const
d655d7dd 257{
258 //comment
259 cout << __FILE__ << ":" << __LINE__ << endl;
260 cout << __FILE__ << __LINE__ << ", startimebin = " << bunch.GetStartBin() << ", length =" << bunch.GetLength() << endl;
261 const UShort_t *sig = bunch.GetData();
262
263 for ( Int_t j = 0; j < bunch.GetLength(); j++)
264 {
265 printf("%d\t", sig[j] );
266 }
267 cout << endl;
268}
269
48a2e3eb 270
57839add 271AliCaloFitResults
272AliCaloRawAnalyzer::Evaluate( const vector<AliCaloBunchInfo> &/*bunchvector*/, const UInt_t /*altrocfg1*/, const UInt_t /*altrocfg2*/)
273{ // method to do the selection of what should possibly be fitted
274 // not implemented for base class
275 return AliCaloFitResults( 0, 0, 0, 0, 0, 0, 0 );
276}
277
48a2e3eb 278
57839add 279int
f57baa2d 280AliCaloRawAnalyzer::PreFitEvaluateSamples( const vector<AliCaloBunchInfo> &bunchvector, const UInt_t altrocfg1, const UInt_t altrocfg2, Int_t & index, Float_t & maxf, short & maxamp, short & maxrev, Float_t & ped, int & first, int & last)
57839add 281{ // method to do the selection of what should possibly be fitted
282 int nsamples = 0;
f57baa2d 283 short maxampindex = 0;
57839add 284 index = SelectBunch( bunchvector, &maxampindex, &maxamp ); // select the bunch with the highest amplitude unless any time constraints is set
285
286
287 if( index >= 0 && maxamp > fAmpCut) // something valid was found, and non-zero amplitude
288 {
289 // use more convenient numbering and possibly subtract pedestal
290 ped = ReverseAndSubtractPed( &(bunchvector.at(index)), altrocfg1, altrocfg2, fReversed );
291 maxf = TMath::MaxElement( bunchvector.at(index).GetLength(), fReversed );
292
293 if ( maxf > fAmpCut ) // possibly significant signal
294 {
295 // select array around max to possibly be used in fit
f57baa2d 296 maxrev = maxampindex - bunchvector.at(index).GetStartBin();
297 SelectSubarray( fReversed, bunchvector.at(index).GetLength(), maxrev, &first, &last);
57839add 298
299 // sanity check: maximum should not be in first or last bin
300 // if we should do a fit
f57baa2d 301 if (first!=maxrev && last!=maxrev) {
57839add 302 // calculate how many samples we have
303 nsamples = last - first + 1;
304 }
305 }
306 }
307
308 return nsamples;
309}
310