]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EMCAL/AliCaloRawAnalyzerPeakFinder.cxx
Fix for Coverity #10278
[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 wights is calculated only once and the
25 // Actual extraction of amplitude and peak position
26 // Is done with a simple vector multiplication, allowing for
27 // Extreemely 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 //#include "AliEMCALRawUtils.h"
40
41 using namespace std;
42
43
44 ClassImp( AliCaloRawAnalyzerPeakFinder )
45
46
47 AliCaloRawAnalyzerPeakFinder::AliCaloRawAnalyzerPeakFinder() :AliCaloRawAnalyzer("Peak-Finder", "PF"),  
48 //    fAmp(0),
49                                                               fPeakFinderVectors(0),
50                                                               fRunOnAlien(false),
51                                                               fIsInitialized(false)
52 {
53   //Comment
54   fAlgo= Algo::kPeakFinder;
55   InitOCDB(fRunOnAlien);
56   fPeakFinderVectors = new AliCaloPeakFinderVectors() ;
57   ResetVectors();
58   LoadVectorsOCDB();
59 }
60
61
62 void 
63 AliCaloRawAnalyzerPeakFinder::InitOCDB(bool alien) const
64 {
65   // Setting the default OCDB pathe depending on wether we work locally or on the GRID.
66   AliCDBManager::Instance()->SetDefaultStorage(  alien == true ? "alien://$ALICE_ROOT/OCDB" : "local://$ALICE_ROOT/OCDB");
67   AliCDBManager::Instance()->SetRun(100);
68 }
69
70
71 void  
72 AliCaloRawAnalyzerPeakFinder::ResetVectors()
73 {
74   //As name implies
75   for(int i=0; i < PF::MAXSTART; i++)
76     {
77       for(int j=0; j < PF::SAMPLERANGE; j++ )
78         {
79           for(int k=0; k < 100; k++ )
80             {
81               fPFAmpVectors[i][j][k] = 0; 
82               fPFTofVectors[i][j][k] = 0;
83               fPFAmpVectorsCoarse[i][j][k] = 0;
84               fPFTofVectorsCoarse[i][j][k] = 0; 
85             }
86         }
87     }
88 }
89
90
91 AliCaloRawAnalyzerPeakFinder::~AliCaloRawAnalyzerPeakFinder()
92 {
93   //comment
94 }
95
96
97 Double_t  
98 AliCaloRawAnalyzerPeakFinder::ScanCoarse(const Double_t *const array, const int length ) const
99 {
100   // Fisrt (coarce) estimate of Amplitude using the Peak-Finder.
101   // The output of the first iteration is sued to select vectors 
102   // for the second iteration.
103
104   Double_t tmpTof = 0;
105   Double_t tmpAmp= 0;
106
107   for(int i=0; i < length; i++)
108     {
109       tmpTof += fPFTofVectorsCoarse[0][length][i]*array[i]; 
110       tmpAmp += fPFAmpVectorsCoarse[0][length][i]*array[i]; 
111     }
112   
113   tmpTof = tmpTof / tmpAmp ;
114   return tmpTof;
115 }
116
117
118 AliCaloFitResults 
119 AliCaloRawAnalyzerPeakFinder::Evaluate( const vector<AliCaloBunchInfo> &bunchvector, const UInt_t altrocfg1,  const UInt_t altrocfg2 )
120 {
121   if( fIsInitialized == false )
122     {
123       cout << __FILE__ << ":" << __LINE__ << "ERROR, peakfinder vectors not loaded" << endl;
124       return  AliCaloFitResults(kInvalid, kInvalid);
125     }
126
127   // Extracting the amplitude using the Peak-Finder algorithm
128   // The amplitude is a weighted sum of the samples using 
129   // optimum weights.
130
131   short maxampindex; //index of maximum amplitude
132   short maxamp; //Maximum amplitude
133   fAmp = 0;
134   int index = SelectBunch( bunchvector,  &maxampindex,  &maxamp );
135   
136   if( index >= 0)
137     {
138       Float_t ped = ReverseAndSubtractPed( &(bunchvector.at(index))  ,  altrocfg1, altrocfg2, fReversed  );
139       Float_t maxf = TMath::MaxElement(   bunchvector.at(index).GetLength(),  fReversed );
140       short timebinOffset = maxampindex - (bunchvector.at( index ).GetLength()-1); 
141  
142       if(  maxf < fAmpCut  ||  ( maxamp - ped) > fOverflowCut  ) // (maxamp - ped) > fOverflowCut = Close to saturation (use low gain then)
143         {
144           return  AliCaloFitResults( maxamp, ped, Ret::kCrude, maxf, timebinOffset);
145         }            
146       else if ( maxf >= fAmpCut )
147         {
148           int first = 0;
149           int last = 0;
150           short maxrev = maxampindex  -  bunchvector.at(index).GetStartBin();     
151           SelectSubarray( fReversed,  bunchvector.at(index).GetLength(), maxrev, &first, &last, fFitArrayCut);
152           int nsamples =  last - first;
153
154           if( ( nsamples  )  >= fNsampleCut ) // no if statement needed really; keep for readability
155             {
156               int startbin = bunchvector.at(index).GetStartBin();  
157               int n = last - first;  
158               int pfindex = n - fNsampleCut; 
159               pfindex = pfindex > PF::SAMPLERANGE ? PF::SAMPLERANGE : pfindex;
160
161               int dt =  maxampindex - startbin -2; 
162               int tmpindex = 0;
163
164
165               Float_t tmptof = ScanCoarse( &fReversed[dt] , n );
166               
167               if( tmptof < -1 )
168                 {
169                   tmpindex = 0;
170                 }
171               else
172                 if( tmptof  > -1 && tmptof < 100 )
173                   {
174                     tmpindex = 1;
175                   }
176                 else
177                   {
178                     tmpindex = 2;
179                   }
180
181               double tof = 0;
182             
183               for(int k=0; k < PF::SAMPLERANGE; k++   )
184                 {
185                   tof +=  fPFTofVectors[0][pfindex][k]*fReversed[ dt  +k + tmpindex -1 ];   
186                 }
187             
188               for( int i=0; i < PF::SAMPLERANGE; i++ )
189                 {
190                   {
191                     fAmp += fPFAmpVectors[0][pfindex][i]*fReversed[ dt  +i  +tmpindex -1 ];
192                   }
193                 }
194               if( TMath::Abs(  (maxf - fAmp  )/maxf )  >   0.1 )
195                 {
196                   //      cout << __FILE__ << ":" << __LINE__ << "WARNING: amp was" << fAmp <<", but was changed to "<< maxf << endl;
197                   fAmp = maxf;
198                 }
199               
200               //      tof = timebinOffset - 0.01*tof/fAmp; // clock ticks
201               tof = timebinOffset - 0.01*tof/fAmp - fL1Phase/TIMEBINWITH; // clock
202
203               // use local-array time for chi2 estimate
204               Float_t chi2 = CalculateChi2(fAmp, tof-timebinOffset+maxrev, first, last);
205               Int_t ndf = last - first - 1; // nsamples - 2
206               return AliCaloFitResults( maxamp, ped , Ret::kFitPar, fAmp, tof, 
207                                         timebinOffset, chi2, ndf,
208                                         Ret::kDummy, AliCaloFitSubarray(index, maxrev, first, last) );  
209             }
210           else
211             {
212               Float_t chi2 = CalculateChi2(maxf, maxrev, first, last);
213               Int_t ndf = last - first - 1; // nsamples - 2
214               return AliCaloFitResults( maxamp, ped , Ret::kCrude, maxf, timebinOffset,
215                                         timebinOffset, chi2, ndf, Ret::kDummy, AliCaloFitSubarray(index, maxrev, first, last) ); 
216             }
217         } // ampcut
218     }
219   return  AliCaloFitResults(kInvalid, kInvalid);
220 }
221
222
223 void   
224 AliCaloRawAnalyzerPeakFinder::CopyVectors( const AliCaloPeakFinderVectors *const pfv )
225 {
226   // As name implies
227   if ( pfv != 0)
228     {
229       for(int i = 0;  i < PF::MAXSTART ; i++)
230         {
231           for( int j=0; j < PF::SAMPLERANGE; j++)  
232             {
233               pfv->GetVector( i, j, fPFAmpVectors[i][j] ,  fPFTofVectors[i][j],    
234                               fPFAmpVectorsCoarse[i][j] , fPFTofVectorsCoarse[i][j]  ); 
235
236               fPeakFinderVectors->SetVector( i, j, fPFAmpVectors[i][j], fPFTofVectors[i][j],    
237                                              fPFAmpVectorsCoarse[i][j], fPFTofVectorsCoarse[i][j] );   
238             }
239         }
240     }
241   else
242     {
243       AliFatal( "pfv = ZERO !!!!!!!");
244     } 
245 }
246
247
248 void   
249 AliCaloRawAnalyzerPeakFinder::LoadVectorsOCDB()
250 {
251   //Loading of Peak-Finder  vectors from the 
252   //Offline Condition Database  (OCDB)
253   AliCDBEntry* entry = AliCDBManager::Instance()->Get("EMCAL/Calib/PeakFinder/");
254   
255   if( entry != 0 )
256   {
257     
258     cout << __FILE__ << ":" << __LINE__ << ": Printing metadata !! " << endl;
259     entry->PrintMetaData();
260
261     AliCaloPeakFinderVectors  *pfv = (AliCaloPeakFinderVectors *)entry->GetObject(); 
262     if( pfv == 0 )
263     {
264       cout << __FILE__ << ":" << __LINE__ << "_ ERRROR " << endl;
265     }
266     
267     CopyVectors( pfv );
268     
269     if( pfv != 0 )
270     {
271       fIsInitialized = true;
272     }
273   }
274   
275 }
276
277
278 void 
279 AliCaloRawAnalyzerPeakFinder::LoadVectorsASCII()
280 {
281   //Read in the Peak finder vecors from ASCI files
282   fIsInitialized= true;  
283   const Int_t buffersize = 256;
284   for(int i = 0;  i < PF::MAXSTART ; i++)
285   {
286     for( int j=0; j < PF::SAMPLERANGE; j++)
287     {
288       char filenameCoarse[buffersize];
289       char filename[buffersize];
290       int n = j+fNsampleCut;
291       double start = (double)i+0;
292       
293       snprintf(filename, buffersize,       "%s/EMCAL/vectors-emcal/start%.1fN%dtau0.235fs10dt1.0.txt", getenv("ALICE_ROOT"), start, n);
294       snprintf(filenameCoarse, buffersize, "%s/EMCAL/vectors-emcal/start%.1fN%dtau0.235fs10dt3.0.txt", getenv("ALICE_ROOT"), start, n);
295       
296       FILE *fp  =  fopen(filename, "r");
297       FILE *fpc =  fopen(filenameCoarse, "r");
298       
299       if( fp == 0 )
300             {
301               AliFatal( Form( "could not open file: %s", filename ) );
302             }
303       else if(fpc == 0)
304             {
305               AliFatal( Form( "could not open file: %s", filenameCoarse ) );
306             }
307       else
308             {
309               for(int m = 0; m < n ; m++ )
310         {
311           fscanf(fp,  "%lf\t", &fPFAmpVectors[i][j][m] );
312           fscanf(fpc, "%lf\t", &fPFAmpVectorsCoarse[i][j][m] );
313         }
314               fscanf(fp,   "\n" );
315               fscanf(fpc,  "\n" );
316               for(int m = 0; m < n ; m++ )
317         {
318           fscanf(fp, "%lf\t",   &fPFTofVectors[i][j][m]  );
319           fscanf(fpc, "%lf\t",  &fPFTofVectorsCoarse[i][j][m]  );  
320         }
321               
322               fPeakFinderVectors->SetVector( i, j, fPFAmpVectors[i][j], fPFTofVectors[i][j],    
323                                       fPFAmpVectorsCoarse[i][j], fPFTofVectorsCoarse[i][j] );   
324         
325             }
326       
327       if(fp) fclose (fp );
328       if(fpc)fclose (fpc);
329       
330     }
331   }
332 }
333
334
335 void   
336 AliCaloRawAnalyzerPeakFinder::WriteRootFile() const
337 {
338   // Utility function to write Peak-Finder vectors to an root file
339   // The output is used to create an OCDB entry.
340   fPeakFinderVectors->PrintVectors();
341   TFile *f = new TFile("peakfindervectors2.root",  "recreate" );
342   fPeakFinderVectors->Write();
343   f->Close();
344   delete f;
345 }
346
347
348 void 
349 AliCaloRawAnalyzerPeakFinder::PrintVectors()
350 {
351   for(int i=0; i < 20; i++)
352     {
353       for( int j = 0; j < PF::MAXSTART; j ++ )
354         {
355           for( int k=0; k < PF::SAMPLERANGE; k++ )
356             {
357               cout << fPFAmpVectors[j][k][i] << "\t" ;
358             }
359         }
360       cout << endl;
361     }
362   cout << __FILE__ << ":" << __LINE__ << ":.... DONE !!" << endl;
363 }