]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EMCAL/AliCaloRawAnalyzerPeakFinder.cxx
update from Per Thomas
[u/mrichter/AliRoot.git] / EMCAL / AliCaloRawAnalyzerPeakFinder.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 // The Peak-Finder algorithm
20 // The amplitude is extracted  as a
21 // weighted sum of the samples using the 
22 // best possible weights.
23 // The wights is calculated only once and the
24 // Actual extraction of amplitude and peak position
25 // Is done with a simple vector multiplication, allowing for
26 // Extreemely fast computations. 
27
28 #include "AliCaloRawAnalyzerPeakFinder.h"
29 #include "AliCaloBunchInfo.h"
30 #include "AliCaloFitResults.h"
31 #include <iostream>
32 #include "unistd.h"
33 #include "TMath.h"
34 #include "AliLog.h"
35
36 using namespace std;
37
38 ClassImp( AliCaloRawAnalyzerPeakFinder )
39
40 AliCaloRawAnalyzerPeakFinder::AliCaloRawAnalyzerPeakFinder() :AliCaloRawAnalyzer("Peak-Finder", "PF")
41 //    fTof(0), 
42 //                                                            fAmp(0)
43 {
44   //comment
45
46   fNsampleCut = 5;
47
48   for(int i=0; i < MAXSTART; i++)
49     {
50       for(int j=0; j < SAMPLERANGE; j++ )
51         {
52           fPFAmpVectors[i][j] = new double[100];
53           fPFTofVectors[i][j] = new double[100];
54           fPFAmpVectorsCoarse[i][j] = new double[100];
55           fPFTofVectorsCoarse[i][j] = new double[100];
56
57           for(int k=0; k < 100; k++ )
58             {
59               fPFAmpVectors[i][j][k] = 0; 
60               fPFTofVectors[i][j][k] = 0;
61               fPFAmpVectorsCoarse[i][j][k] = 0;
62               fPFTofVectorsCoarse[i][j][k] = 0; 
63             }
64         }
65     }
66
67   LoadVectors();
68
69 }
70
71
72 AliCaloRawAnalyzerPeakFinder::~AliCaloRawAnalyzerPeakFinder()
73 {
74   //comment
75   for(int i=0; i < MAXSTART; i++)
76     {
77       for(int j=0; j < SAMPLERANGE; j++ )
78         {
79           delete[] fPFAmpVectors[i][j];
80           delete[] fPFTofVectors[i][j];
81           delete[] fPFAmpVectorsCoarse[i][j];
82           delete[] fPFTofVectorsCoarse[i][j];
83         }
84     }
85 }
86
87
88 Double_t  
89 AliCaloRawAnalyzerPeakFinder::ScanCoarse(const Double_t *const array, const int length ) const
90 {
91   Double_t tmpTof = 0;
92   Double_t tmpAmp= 0;
93
94   for(int i=0; i < length; i++)
95     {
96       tmpTof += fPFTofVectorsCoarse[0][length][i]*array[i]; 
97       tmpAmp += fPFAmpVectorsCoarse[0][length][i]*array[i]; 
98     }
99   
100   tmpTof = tmpTof / tmpAmp ;
101   return tmpTof;
102 }
103
104
105 AliCaloFitResults 
106 AliCaloRawAnalyzerPeakFinder::Evaluate( const vector<AliCaloBunchInfo> &bunchvector, const UInt_t altrocfg1,  const UInt_t altrocfg2 )
107 {
108   // Extracting the amplitude using the Peak-Finder algorithm
109   // The amplitude is a weighted sum of the samples using 
110   // optimum weights.
111
112   short maxampindex; //index of maximum amplitude
113   short maxamp; //Maximum amplitude
114   //  fAmp = 0;
115
116   
117   fAmpA[0] = 0;
118   fAmpA[1] = 0;
119   fAmpA[2] = 0;
120   
121   
122   //  cout << __FILE__ << __LINE__ << "\tendbin = " << bunchvector.at(index).GetEndBin()  <<  "\tstartbin = " << bunchvector.at(index).GetStartBin()  << endl;
123   
124   int index = SelectBunch( bunchvector,  &maxampindex,  &maxamp );
125
126   if( index >= 0)
127     {
128       Float_t ped = ReverseAndSubtractPed( &(bunchvector.at(index))  ,  altrocfg1, altrocfg2, fReversed  );
129       Float_t maxf = TMath::MaxElement(   bunchvector.at(index).GetLength(),  fReversed );
130       
131       if(  maxf < fAmpCut  ||  ( maxamp - ped) > 900  )  // (maxamp - ped) > 900 = Close to saturation (use low gain then)
132         {
133           //      cout << __FILE__ << __LINE__ <<":, maxamp = " << maxamp << ", ped = "<< ped  << ",. maxf = "<< maxf << ", maxampindex = "<< maxampindex  << endl;
134           return  AliCaloFitResults( maxamp, ped,  -1, maxf,   maxampindex, -1, -1 );
135         }
136       
137       int first;
138       int last;
139       
140       if ( maxf > fAmpCut )
141         {
142           
143
144           SelectSubarray( fReversed,  bunchvector.at(index).GetLength(),  maxampindex -  bunchvector.at(index).GetStartBin(), &first, &last);
145           int nsamples =  last - first;
146           if( ( nsamples  )  >= fNsampleCut )
147             {
148               int startbin = bunchvector.at(index).GetStartBin();  
149               int n = last - first;  
150               int pfindex = n - fNsampleCut; 
151               pfindex = pfindex > SAMPLERANGE ? SAMPLERANGE : pfindex;
152
153               short timebinOffset = maxampindex - (bunchvector.at( index ).GetLength()-1); 
154              
155               int dt =  maxampindex - startbin -2; 
156
157               //    cout << __FILE__ << __LINE__ <<"\t The coarse estimated t0 is " << ScanCoarse( &fReversed[dt] , n ) << endl;
158             
159          
160               //     Float_t tmptof = ScanCoarse( &fReversed[dt] , n );
161               
162               //              cout << __FILE__ << __LINE__ << ",  dt = " << dt << ",\tmaxamindex = " << maxampindex << "\tstartbin = "<< startbin << endl;
163
164               for( int i=0; i < SAMPLERANGE; i++ )
165                 {
166                   for( int j = 0; j < 3; j++ )
167                     {
168                       //    fAmpA[j] += fPFAmpVectors[0][pfindex][i]*tmp[j]; 
169                       fAmpA[j] += fPFAmpVectors[0][pfindex][i]*fReversed[ dt  +i +j -1 ];
170                     }
171                 }
172               
173               double diff = 9999;
174               int tmpindex = 0;
175
176               for(int k=0; k < 3; k ++)
177                 {
178                   //              cout << __FILE__ << __LINE__ << "amp[="<< k <<"] = " << fAmpA[k] << endl;
179                   if(  TMath::Abs(fAmpA[k] - ( maxamp - ped) )  < diff)
180                     {
181                       diff = TMath::Abs(fAmpA[k] - ( maxamp - ped));
182                       tmpindex = k; 
183                     }
184                 }
185               
186               Float_t tmptof = ScanCoarse( &fReversed[dt] , n );
187               
188               if( tmptof < -1 )
189                 {
190                   tmpindex = 0;
191                 }
192               else
193                 if( tmptof  > -1 && tmptof < 100 )
194                   {
195                     tmpindex =1;
196                   }
197                 else
198                   {
199                     tmpindex = 2;
200                   }
201               double tof = 0;
202               
203               for(int k=0; k < SAMPLERANGE; k++   )
204                 {
205                   tof +=  fPFTofVectors[0][pfindex][k]*fReversed[ dt  +k + tmpindex -1 ];   
206                 }
207               
208               //              cout << __FILE__ << __LINE__ <<  "tofRaw =   "<< tof /  fAmpA[tmpindex]  << endl;
209               
210               // tof = tof /  fAmpA[tmpindex] +  (dt + startbin)*100;
211               
212               if( TMath::Abs(  (maxf - fAmpA[tmpindex])/maxf )  >   0.1 )
213                 {
214                   fAmpA[tmpindex] = maxf;
215                 }
216
217               //              timebinOffset
218
219                 //      tof = (dt + startbin + tmpindex )*100 - tof/fAmpA[tmpindex];
220                 // tof = ( timebinOffset )*100 - tof/fAmpA[tmpindex]; // ns
221               tof = timebinOffset - 0.01*tof/fAmpA[tmpindex]; // clock ticks
222               
223               //      tof = tof/fAmpA[tmpindex];
224
225   
226               return AliCaloFitResults( maxamp, ped , -1, fAmpA[tmpindex], tof, -2, -3 ); 
227             }
228           else
229             {
230               return AliCaloFitResults( maxamp, ped , -5, maxf, -6, -7, -8 ); 
231             }
232         }
233     }
234    //  cout << __FILE__ << __LINE__ <<  "WARNING, returning amp = -1 "  <<  endl;
235   return  AliCaloFitResults(-1, -1);
236 }
237
238
239 void 
240 AliCaloRawAnalyzerPeakFinder::LoadVectors()
241 {
242   //Read in the Peak finder vecors from file
243   for(int i = 0;  i < MAXSTART ; i++)
244     {
245       for( int j=0; j < SAMPLERANGE; j++)
246         {
247           char filenameCoarse[256];
248           char filename[256];
249           
250           int n = j+fNsampleCut;
251
252           //      double start = (double)i+0.5;
253           double start = (double)i+0;
254
255           sprintf(filename,        "%s/EMCAL/vectors-emcal/start%.1fN%dtau0.235fs10dt1.0.txt", getenv("ALICE_ROOT"), start, n);
256           sprintf(filenameCoarse,  "%s/EMCAL/vectors-emcal/start%.1fN%dtau0.235fs10dt3.0.txt", getenv("ALICE_ROOT"), start, n);
257
258           FILE *fp  =  fopen(filename, "r");
259           FILE *fpc =  fopen(filenameCoarse, "r");
260
261           if( fp == 0 )
262             {
263               AliFatal( Form( "could not open file: %s", filename ) );
264             }
265         
266           if(fpc == 0)
267             {
268               AliFatal( Form( "could not open file: %s", filenameCoarse ) );
269             }
270           else
271             {
272               for(int m = 0; m < n ; m++ )
273                 {
274                   cout << __FILE__ << __LINE__ << "i="<<i <<"\tj=" <<j << "\tm=" << m << endl;
275  
276                   fscanf(fp, "%lf\t", &fPFAmpVectors[i][j][m] );
277                   //              fPFAmpVectorsCoarse[i][j][m] = 1;
278                   fscanf(fpc, "%lf\t", &fPFAmpVectorsCoarse[i][j][m] );
279                 }
280               
281               fscanf(fp,   "\n" );
282               fscanf(fpc,  "\n" );
283               
284               for(int m = 0; m < n ; m++ )
285                 {
286                   //  fPFTofVectors[i][j][m] = 1;
287
288                   fscanf(fp, "%lf\t",   &fPFTofVectors[i][j][m]  );
289                   fscanf(fpc, "%lf\t",  &fPFTofVectorsCoarse[i][j][m]  );  
290                   //  fPFTofVectorsCoarse[i][j][m] = 1;  
291                 }
292               
293              
294               fclose (fp);
295               fclose (fpc);
296             }
297         }
298     }
299 }
300
301
302
303 /*
304 void
305 AliCaloRawAnalyzerPeakFinder::PolTof( const double rectof ) const
306 //
307 {
308   static Double_t p0 = -55.69;
309   static Double_t p1 = 3.178;
310   static Double_t p2 = -0.05587;
311   static Double_t p3 = 0.0003185;
312   static Double_t p4 = -7.91E-7;
313   static Double_t p5 = 7.576E-10;
314 }
315 */