]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EMCAL/AliCaloRawAnalyzerPeakFinder.cxx
fix warning: unused variables
[u/mrichter/AliRoot.git] / EMCAL / AliCaloRawAnalyzerPeakFinder.cxx
1 // -*- mode: c++ -*-
2 /**************************************************************************
3  * This file is property of and copyright by                              *
4  * the Relativistic Heavy Ion Group (RHIG), Yale University, US, 2009     *
5  *                                                                        *
6  * Primary Author: Per Thomas Hille  <perthomas.hille@yale.edu>           *
7  *                                                                        *
8  * Contributors are mentioned in the code where appropriate.              *
9  * Please report bugs to   perthomas.hille@yale.edu                       *
10  *                                                                        *
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  **************************************************************************/
19
20 // The Peak-Finder algorithm
21 // The amplitude is extracted  as a
22 // weighted sum of the samples using the 
23 // best possible weights.
24 // The weights are calculated only once and the
25 // actual extraction of amplitude and peak position
26 // is done with a simple vector multiplication, allowing
27 // extremely fast computations.
28
29 #include "AliCaloRawAnalyzerPeakFinder.h"
30 #include "AliCaloBunchInfo.h"
31 #include "AliCaloFitResults.h"
32 #include "TMath.h"
33 #include "AliLog.h"
34 #include "AliCDBEntry.h"
35 #include "AliCDBManager.h"
36 #include "TFile.h"
37 #include "AliCaloPeakFinderVectors.h"
38 #include <iostream>
39
40 using namespace std;
41
42 ClassImp( AliCaloRawAnalyzerPeakFinder )
43
44
45 AliCaloRawAnalyzerPeakFinder::AliCaloRawAnalyzerPeakFinder() :AliCaloRawAnalyzer("Peak-Finder", "PF"),  
46                                                               fPeakFinderVectors(0),
47                                                               fRunOnAlien(false),
48                                                               fIsInitialized(false)
49 {
50   // Ctor
51   fAlgo= Algo::kPeakFinder;
52   fPeakFinderVectors = new AliCaloPeakFinderVectors() ;
53   ResetVectors();
54   LoadVectorsOCDB();
55 }
56
57 void  
58 AliCaloRawAnalyzerPeakFinder::ResetVectors()
59 {
60   //As name implies
61   for(int i=0; i < PF::MAXSTART; i++)
62     {
63       for(int j=0; j < PF::SAMPLERANGE; j++ )
64         {
65           for(int k=0; k < 100; k++ )
66             {
67               fPFAmpVectors[i][j][k] = 0; 
68               fPFTofVectors[i][j][k] = 0;
69               fPFAmpVectorsCoarse[i][j][k] = 0;
70               fPFTofVectorsCoarse[i][j][k] = 0; 
71             }
72         }
73     }
74 }
75
76
77 Double_t  
78 AliCaloRawAnalyzerPeakFinder::ScanCoarse(const Double_t *const array, const int length ) const
79 {
80   // Fisrt (coarce) estimate of Amplitude using the Peak-Finder.
81   // The output of the first iteration is sued to select vectors 
82   // for the second iteration.
83
84   Double_t tmpTof = 0;
85   Double_t tmpAmp= 0;
86
87   for(int i=0; i < length; i++)
88     {
89       tmpTof += fPFTofVectorsCoarse[0][length][i]*array[i]; 
90       tmpAmp += fPFAmpVectorsCoarse[0][length][i]*array[i]; 
91     }
92   
93   tmpTof = tmpTof / tmpAmp ;
94   return tmpTof;
95 }
96
97
98 AliCaloFitResults 
99 AliCaloRawAnalyzerPeakFinder::Evaluate( const vector<AliCaloBunchInfo> &bunchvector, const UInt_t altrocfg1,  const UInt_t altrocfg2 )
100 {
101   // Evaluation of amplitude and TOF
102   if( fIsInitialized == false )
103     {
104       cout << __FILE__ << ":" << __LINE__ << "ERROR, peakfinder vectors not loaded" << endl;
105       return  AliCaloFitResults(kInvalid, kInvalid);
106     }
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   int index = SelectBunch( bunchvector,  &maxampindex,  &maxamp );
116   
117   if( index >= 0)
118     {
119       Float_t ped = ReverseAndSubtractPed( &(bunchvector.at(index))  ,  altrocfg1, altrocfg2, fReversed  );
120       Float_t maxf = TMath::MaxElement(   bunchvector.at(index).GetLength(),  fReversed );
121       short timebinOffset = maxampindex - (bunchvector.at( index ).GetLength()-1); 
122  
123       if(  maxf < fAmpCut  ||  ( maxamp - ped) > fOverflowCut  ) // (maxamp - ped) > fOverflowCut = Close to saturation (use low gain then)
124         {
125           return  AliCaloFitResults( maxamp, ped, Ret::kCrude, maxf, timebinOffset);
126         }            
127       else if ( maxf >= fAmpCut )
128         {
129           int first = 0;
130           int last = 0;
131           short maxrev = maxampindex  -  bunchvector.at(index).GetStartBin();     
132           SelectSubarray( fReversed,  bunchvector.at(index).GetLength(), maxrev, &first, &last, fFitArrayCut);
133           int nsamples =  last - first;
134
135           if( ( nsamples  )  >= fNsampleCut ) // no if statement needed really; keep for readability
136             {
137               int startbin = bunchvector.at(index).GetStartBin();  
138               int n = last - first;  
139               int pfindex = n - fNsampleCut; 
140               pfindex = pfindex > PF::SAMPLERANGE ? PF::SAMPLERANGE : pfindex;
141               int dt =  maxampindex - startbin -2; 
142               int tmpindex = 0;
143               Float_t tmptof = ScanCoarse( &fReversed[dt] , n );
144               
145               if( tmptof < -1 )
146                 {
147                   tmpindex = 0;
148                 }
149               else
150                 if( tmptof  > -1 && tmptof < 100 )
151                   {
152                     tmpindex = 1;
153                   }
154                 else
155                   {
156                     tmpindex = 2;
157                   }
158
159               double tof = 0;
160               for(int k=0; k < PF::SAMPLERANGE; k++   )
161                 {
162                   tof +=  fPFTofVectors[0][pfindex][k]*fReversed[ dt  +k + tmpindex -1 ];   
163                 }
164               for( int i=0; i < PF::SAMPLERANGE; i++ )
165                 {
166                   {
167                     
168                     fAmp += fPFAmpVectors[0][pfindex][i]*fReversed[ dt  +i  +tmpindex -1 ];
169                   }
170                 }
171
172               if( TMath::Abs(  (maxf - fAmp  )/maxf )  >   0.1 )
173                 {
174                   fAmp = maxf;
175                 }
176               
177               tof = timebinOffset - 0.01*tof/fAmp - fL1Phase/TIMEBINWITH; // clock
178               Float_t chi2 = CalculateChi2(fAmp, tof-timebinOffset+maxrev, first, last);
179               Int_t ndf = last - first - 1; // nsamples - 2
180               return AliCaloFitResults( maxamp, ped , Ret::kFitPar, fAmp, tof, 
181                                         timebinOffset, chi2, ndf,
182                                         Ret::kDummy, AliCaloFitSubarray(index, maxrev, first, last) );  
183             }
184           else
185             {
186               Float_t chi2 = CalculateChi2(maxf, maxrev, first, last);
187               Int_t ndf = last - first - 1; // nsamples - 2
188               return AliCaloFitResults( maxamp, ped , Ret::kCrude, maxf, timebinOffset,
189                                         timebinOffset, chi2, ndf, Ret::kDummy, AliCaloFitSubarray(index, maxrev, first, last) ); 
190             }
191         } // ampcut
192     }
193   return  AliCaloFitResults(kInvalid, kInvalid);
194 }
195
196
197 void   
198 AliCaloRawAnalyzerPeakFinder::CopyVectors( const AliCaloPeakFinderVectors *const pfv )
199 {
200   // As name implies
201   if ( pfv != 0)
202     {
203       for(int i = 0;  i < PF::MAXSTART ; i++)
204         {
205           for( int j=0; j < PF::SAMPLERANGE; j++)  
206             {
207               pfv->GetVector( i, j, fPFAmpVectors[i][j] ,  fPFTofVectors[i][j],    
208                               fPFAmpVectorsCoarse[i][j] , fPFTofVectorsCoarse[i][j]  ); 
209
210               fPeakFinderVectors->SetVector( i, j, fPFAmpVectors[i][j], fPFTofVectors[i][j],    
211                                              fPFAmpVectorsCoarse[i][j], fPFTofVectorsCoarse[i][j] );   
212             }
213         }
214     }
215   else
216     {
217       AliFatal( "pfv = ZERO !!!!!!!");
218     } 
219 }
220
221
222 void   
223 AliCaloRawAnalyzerPeakFinder::LoadVectorsOCDB()
224 {
225   //Loading of Peak-Finder  vectors from the 
226   //Offline Condition Database  (OCDB)
227   AliCDBEntry* entry = AliCDBManager::Instance()->Get("EMCAL/Calib/PeakFinder/");
228   
229   if( entry != 0 )
230   {
231     //cout << __FILE__ << ":" << __LINE__ << ": Printing metadata !! " << endl;
232     //entry->PrintMetaData();
233     AliCaloPeakFinderVectors  *pfv = (AliCaloPeakFinderVectors *)entry->GetObject(); 
234     if( pfv == 0 )
235     {
236       cout << __FILE__ << ":" << __LINE__ << "_ ERRROR " << endl;
237     }
238     CopyVectors( pfv );
239     
240     if( pfv != 0 )
241     {
242       fIsInitialized = true;
243     }
244   }
245 }
246
247
248 void   
249 AliCaloRawAnalyzerPeakFinder::WriteRootFile() const
250 { // Utility function to write Peak-Finder vectors to an root file
251   // The output is used to create an OCDB entry.
252   fPeakFinderVectors->PrintVectors();
253   TFile *f = new TFile("peakfindervectors2.root",  "recreate" );
254   fPeakFinderVectors->Write();
255   f->Close();
256   delete f;
257 }
258
259
260 void 
261 AliCaloRawAnalyzerPeakFinder::PrintVectors()
262 { // Utility function to write Peak-Finder vectors 
263   for(int i=0; i < 20; i++)
264     {
265       for( int j = 0; j < PF::MAXSTART; j ++ )
266         {
267           for( int k=0; k < PF::SAMPLERANGE; k++ )
268             {
269               cout << fPFAmpVectors[j][k][i] << "\t" ;
270             }
271         }
272       cout << endl;
273     }
274   cout << __FILE__ << ":" << __LINE__ << ":.... DONE !!" << endl;
275 }