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