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