]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EMCAL/AliCaloRawAnalyzer.cxx
From Jiri and Francesco: code to look at TRU L0 info
[u/mrichter/AliRoot.git] / EMCAL / AliCaloRawAnalyzer.cxx
1 /**************************************************************************
2  * This file is property of and copyright by                              *
3  * the Relativistic Heavy Ion Group (RHIG), Yale University, US, 2009     *
4  *                                                                        *
5  * Primary Author: Per Thomas Hille <perthomas.hille@yale.edu>            *
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
22 // From CALO Calorimeter RAW data (from the RCU)
23 // Contains some utilities for preparing / selecting
24 // Signals suitable for signal extraction
25 // By derived classes
26
27 #include "AliLog.h"
28 #include "AliCaloRawAnalyzer.h"
29 #include "AliCaloBunchInfo.h"
30 #include "AliCaloFitResults.h"
31 #include "TMath.h"
32 #include <iostream>
33 using namespace std;
34
35 //ClassImp(AliCaloRawAnalyzer)  
36
37 AliCaloRawAnalyzer::AliCaloRawAnalyzer(const char *name, const char *nameshort) :  TObject(),
38                                                                                    fMinTimeIndex(-1),
39                                                                                    fMaxTimeIndex(-1),
40                                                                                    fFitArrayCut(5),
41                                                                                    fAmpCut(4),
42                                                                                    fNsampleCut(5),
43                                                                                    fNsamplePed(3),
44                                                                                    fIsZerosupressed( false ),
45                                                                                    fVerbose( false )
46 {
47   //Comment 
48   sprintf(fName, "%s", name);
49   sprintf(fNameShort, "%s", nameshort);
50     
51   for(int i=0; i < MAXSAMPLES; i++ )
52     {
53       fReversed[i] = 0;
54     }
55 }
56
57 AliCaloRawAnalyzer::~AliCaloRawAnalyzer()
58 {
59
60 }
61
62
63 void 
64 AliCaloRawAnalyzer::SetTimeConstraint(const int min, const int max ) 
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
79 UShort_t 
80 AliCaloRawAnalyzer::Max(const UShort_t *data, const int length ) const
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
96 void 
97 AliCaloRawAnalyzer::SelectSubarray( const Double_t *fData, const int length, const short maxindex, int *const first,  int *const last ) const
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   
110   while(( tmplast ) <  (length-1)   && ( fData [tmplast] >  fFitArrayCut ))
111     {
112       tmplast ++;
113     }
114
115
116   *first = tmpfirst +1;
117   *last =  tmplast -1;
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
126 }
127
128
129
130 Float_t 
131 AliCaloRawAnalyzer::ReverseAndSubtractPed( const AliCaloBunchInfo *bunch, const UInt_t /*altrocfg1*/,  const UInt_t /*altrocfg2*/, double *outarray ) const
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
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
150 Float_t 
151 AliCaloRawAnalyzer::EvaluatePedestal(const UShort_t * const data, const int /*length*/ ) const
152 {
153   //  double ped = 0;
154   double tmp = 0;
155
156   if( fIsZerosupressed == false ) 
157     {
158       for(int i=0; i < fNsamplePed; i++ )
159         {
160           tmp += data[i];
161         }
162    }
163
164   return  tmp/fNsamplePed;
165  }
166
167
168 short  
169 AliCaloRawAnalyzer::Max( const AliCaloBunchInfo *const bunch , int *const maxindex ) const
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     {
187       //   *maxindex =  bunch->GetLength() -1 - tmpindex + bunch->GetStartBin(); 
188        *maxindex =  bunch->GetLength() -1 - tmpindex + bunch->GetStartBin(); 
189     }
190   
191   return  tmpmax;
192 }
193
194
195 bool  
196 AliCaloRawAnalyzer::CheckBunchEdgesForMax( const AliCaloBunchInfo *const bunch ) const
197 {
198   // a bunch is considered invalid if the maximum is in the first or last time-bin
199   short tmpmax = -1;
200   int tmpindex = -1;
201   const UShort_t *sig = bunch->GetData();
202
203   for(int i=0; i < bunch->GetLength(); i++ )
204     {
205       if( sig[i] > tmpmax )
206         {
207           tmpmax   =  sig[i];
208           tmpindex =  i; 
209         }
210     }
211   
212   bool bunchOK = true;
213   if (tmpindex == 0 || tmpindex == (bunch->GetLength()-1) )
214     {
215       bunchOK = false;
216     }
217   
218   return  bunchOK;
219 }
220
221
222 int 
223 AliCaloRawAnalyzer::SelectBunch( const vector<AliCaloBunchInfo> &bunchvector,short *const maxampbin, short *const maxamplitude ) const
224 {
225   //We select the bunch with the highest amplitude unless any time constraints is set
226   short max = -1;
227   short bunchindex = -1;
228   short maxall = -1;
229   int indx = -1;
230
231   for(unsigned int i=0; i < bunchvector.size(); i++ )
232     {
233       max = Max(  &bunchvector.at(i), &indx ); // CRAP PTH, bug fix, trouble if more than one bunches  
234       if( IsInTimeRange( indx) )
235         {
236           if( max > maxall )
237             {
238               maxall = max;
239               bunchindex = i;
240               *maxampbin     = indx;
241               *maxamplitude  = max;
242             }
243         }
244     }
245  
246   if (bunchindex >= 0) {
247     bool bunchOK = CheckBunchEdgesForMax( &bunchvector.at(bunchindex) );
248     if (! bunchOK) { 
249       bunchindex = -1; 
250     }
251   }
252
253   return  bunchindex;
254 }
255
256
257 bool 
258 AliCaloRawAnalyzer::IsInTimeRange( const int maxindex  ) const
259 {
260   // Ckeck if the index of the max ADC vaue is consistent with trigger.
261   if( ( fMinTimeIndex  < 0 && fMaxTimeIndex  < 0) ||fMaxTimeIndex  < 0 )
262     {
263       return true; 
264     }
265   return ( maxindex < fMaxTimeIndex ) && ( maxindex > fMinTimeIndex  ) ? true : false;
266 }
267
268
269 void 
270 AliCaloRawAnalyzer::PrintBunches( const vector<AliCaloBunchInfo> &bvctr ) const
271 {
272   //comment
273   cout << __FILE__ << __LINE__<< "*************** Printing Bunches *******************" << endl;
274   cout << __FILE__ << __LINE__<< "*** There are " << bvctr.size() << ", bunches" << endl;
275
276   for(unsigned int i=0; i < bvctr.size() ; i++ )
277     {
278       PrintBunch( bvctr.at(i) );
279       cout << " bunch = "  <<  i  << endl;
280     }
281   cout << __FILE__ << __LINE__<< "*************** Done ... *******************" << endl;
282 }
283
284
285 void 
286 AliCaloRawAnalyzer::PrintBunch( const AliCaloBunchInfo &bunch ) const
287 {
288   //comment
289   cout << __FILE__ << ":" << __LINE__ << endl;
290   cout << __FILE__ << __LINE__   << ", startimebin = " << bunch.GetStartBin() << ", length =" <<  bunch.GetLength() << endl;
291   const UShort_t *sig =  bunch.GetData();  
292   
293   for ( Int_t j = 0; j <  bunch.GetLength();  j++) 
294     {
295       printf("%d\t", sig[j] );
296     }
297   cout << endl; 
298 }
299
300
301 AliCaloFitResults
302 AliCaloRawAnalyzer::Evaluate( const vector<AliCaloBunchInfo>  &/*bunchvector*/, const UInt_t /*altrocfg1*/,  const UInt_t /*altrocfg2*/)
303 { // method to do the selection of what should possibly be fitted
304   // not implemented for base class
305   return AliCaloFitResults( 0, 0 );
306 }
307
308
309 int
310 AliCaloRawAnalyzer::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)
311 { // method to do the selection of what should possibly be fitted
312   int nsamples  = 0;
313   short maxampindex = 0;
314   index = SelectBunch( bunchvector,  &maxampindex,  &maxamp ); // select the bunch with the highest amplitude unless any time constraints is set
315
316   
317   if( index >= 0 && maxamp > fAmpCut) // something valid was found, and non-zero amplitude
318     {
319       // use more convenient numbering and possibly subtract pedestal
320       ped  = ReverseAndSubtractPed( &(bunchvector.at(index)),  altrocfg1, altrocfg2, fReversed  );
321       maxf = TMath::MaxElement( bunchvector.at(index).GetLength(),  fReversed );
322       
323       if ( maxf > fAmpCut ) // possibly significant signal
324         {
325           // select array around max to possibly be used in fit
326           maxrev = maxampindex - bunchvector.at(index).GetStartBin(); 
327           SelectSubarray( fReversed,  bunchvector.at(index).GetLength(),  maxrev, &first, &last);
328
329           // sanity check: maximum should not be in first or last bin
330           // if we should do a fit
331           if (first!=maxrev && last!=maxrev) {
332             // calculate how many samples we have 
333             nsamples =  last - first + 1;         
334           }
335         }
336     }
337
338   return nsamples;
339 }
340