]> git.uio.no Git - u/mrichter/AliRoot.git/blame - EMCAL/AliCaloRawAnalyzerPeakFinder.cxx
Merge branch 'master_patch'
[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.
6656f267 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.
d655d7dd 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
d655d7dd 40using namespace std;
41
e37e3c84 42ClassImp( AliCaloRawAnalyzerPeakFinder )
43
bab48e74 44
45AliCaloRawAnalyzerPeakFinder::AliCaloRawAnalyzerPeakFinder() :AliCaloRawAnalyzer("Peak-Finder", "PF"),
fc7cd737 46 fPeakFinderVectors(0),
168c7b3c 47 fRunOnAlien(false),
48 fIsInitialized(false)
d655d7dd 49{
6656f267 50 // Ctor
168c7b3c 51 fAlgo= Algo::kPeakFinder;
bab48e74 52 fPeakFinderVectors = new AliCaloPeakFinderVectors() ;
53 ResetVectors();
54 LoadVectorsOCDB();
bab48e74 55}
56
bab48e74 57void
58AliCaloRawAnalyzerPeakFinder::ResetVectors()
59{
fc7cd737 60 //As name implies
168c7b3c 61 for(int i=0; i < PF::MAXSTART; i++)
d655d7dd 62 {
168c7b3c 63 for(int j=0; j < PF::SAMPLERANGE; j++ )
d655d7dd 64 {
d655d7dd 65 for(int k=0; k < 100; k++ )
66 {
67 fPFAmpVectors[i][j][k] = 0;
68 fPFTofVectors[i][j][k] = 0;
48a2e3eb 69 fPFAmpVectorsCoarse[i][j][k] = 0;
70 fPFTofVectorsCoarse[i][j][k] = 0;
d655d7dd 71 }
72 }
73 }
d655d7dd 74}
75
76
48a2e3eb 77Double_t
78AliCaloRawAnalyzerPeakFinder::ScanCoarse(const Double_t *const array, const int length ) const
79{
bab48e74 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
48a2e3eb 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
57839add 98AliCaloFitResults
99AliCaloRawAnalyzerPeakFinder::Evaluate( const vector<AliCaloBunchInfo> &bunchvector, const UInt_t altrocfg1, const UInt_t altrocfg2 )
d655d7dd 100{
1f847d9f 101 // Evaluation of amplitude and TOF
168c7b3c 102 if( fIsInitialized == false )
103 {
104 cout << __FILE__ << ":" << __LINE__ << "ERROR, peakfinder vectors not loaded" << endl;
105 return AliCaloFitResults(kInvalid, kInvalid);
106 }
107
d655d7dd 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
bab48e74 114 fAmp = 0;
d655d7dd 115 int index = SelectBunch( bunchvector, &maxampindex, &maxamp );
168c7b3c 116
d655d7dd 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 );
f57baa2d 121 short timebinOffset = maxampindex - (bunchvector.at( index ).GetLength()-1);
168c7b3c 122
2cd0ffda 123 if( maxf < fAmpCut || ( maxamp - ped) > fOverflowCut ) // (maxamp - ped) > fOverflowCut = Close to saturation (use low gain then)
d655d7dd 124 {
168c7b3c 125 return AliCaloFitResults( maxamp, ped, Ret::kCrude, maxf, timebinOffset);
2cd0ffda 126 }
127 else if ( maxf >= fAmpCut )
128 {
129 int first = 0;
130 int last = 0;
131 short maxrev = maxampindex - bunchvector.at(index).GetStartBin();
92d9f317 132 SelectSubarray( fReversed, bunchvector.at(index).GetLength(), maxrev, &first, &last, fFitArrayCut);
d655d7dd 133 int nsamples = last - first;
2cd0ffda 134
135 if( ( nsamples ) >= fNsampleCut ) // no if statement needed really; keep for readability
d655d7dd 136 {
137 int startbin = bunchvector.at(index).GetStartBin();
48a2e3eb 138 int n = last - first;
d655d7dd 139 int pfindex = n - fNsampleCut;
168c7b3c 140 pfindex = pfindex > PF::SAMPLERANGE ? PF::SAMPLERANGE : pfindex;
8ffb0474 141 int dt = maxampindex - startbin -2;
8ffb0474 142 int tmpindex = 0;
48a2e3eb 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 {
bab48e74 152 tmpindex = 1;
48a2e3eb 153 }
154 else
155 {
156 tmpindex = 2;
157 }
bab48e74 158
8ffb0474 159 double tof = 0;
168c7b3c 160 for(int k=0; k < PF::SAMPLERANGE; k++ )
8ffb0474 161 {
162 tof += fPFTofVectors[0][pfindex][k]*fReversed[ dt +k + tmpindex -1 ];
163 }
168c7b3c 164 for( int i=0; i < PF::SAMPLERANGE; i++ )
bab48e74 165 {
166 {
f67ce0df 167
bab48e74 168 fAmp += fPFAmpVectors[0][pfindex][i]*fReversed[ dt +i +tmpindex -1 ];
169 }
170 }
f67ce0df 171
bab48e74 172 if( TMath::Abs( (maxf - fAmp )/maxf ) > 0.1 )
48a2e3eb 173 {
bab48e74 174 fAmp = maxf;
48a2e3eb 175 }
48a2e3eb 176
92d9f317 177 tof = timebinOffset - 0.01*tof/fAmp - fL1Phase/TIMEBINWITH; // clock
2cd0ffda 178 Float_t chi2 = CalculateChi2(fAmp, tof-timebinOffset+maxrev, first, last);
179 Int_t ndf = last - first - 1; // nsamples - 2
168c7b3c 180 return AliCaloFitResults( maxamp, ped , Ret::kFitPar, fAmp, tof,
2cd0ffda 181 timebinOffset, chi2, ndf,
168c7b3c 182 Ret::kDummy, AliCaloFitSubarray(index, maxrev, first, last) );
d655d7dd 183 }
184 else
185 {
2cd0ffda 186 Float_t chi2 = CalculateChi2(maxf, maxrev, first, last);
187 Int_t ndf = last - first - 1; // nsamples - 2
168c7b3c 188 return AliCaloFitResults( maxamp, ped , Ret::kCrude, maxf, timebinOffset,
189 timebinOffset, chi2, ndf, Ret::kDummy, AliCaloFitSubarray(index, maxrev, first, last) );
d655d7dd 190 }
2cd0ffda 191 } // ampcut
d655d7dd 192 }
168c7b3c 193 return AliCaloFitResults(kInvalid, kInvalid);
d655d7dd 194}
195
196
bab48e74 197void
168c7b3c 198AliCaloRawAnalyzerPeakFinder::CopyVectors( const AliCaloPeakFinderVectors *const pfv )
bab48e74 199{
200 // As name implies
bab48e74 201 if ( pfv != 0)
202 {
168c7b3c 203 for(int i = 0; i < PF::MAXSTART ; i++)
bab48e74 204 {
168c7b3c 205 for( int j=0; j < PF::SAMPLERANGE; j++)
bab48e74 206 {
0cb95545 207 pfv->GetVector( i, j, fPFAmpVectors[i][j] , fPFTofVectors[i][j],
208 fPFAmpVectorsCoarse[i][j] , fPFTofVectorsCoarse[i][j] );
168c7b3c 209
bab48e74 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 !!!!!!!");
bab48e74 218 }
219}
220
221
bab48e74 222void
223AliCaloRawAnalyzerPeakFinder::LoadVectorsOCDB()
224{
225 //Loading of Peak-Finder vectors from the
226 //Offline Condition Database (OCDB)
bab48e74 227 AliCDBEntry* entry = AliCDBManager::Instance()->Get("EMCAL/Calib/PeakFinder/");
11bcc677 228
bab48e74 229 if( entry != 0 )
11bcc677 230 {
f73babef 231 //cout << __FILE__ << ":" << __LINE__ << ": Printing metadata !! " << endl;
232 //entry->PrintMetaData();
11bcc677 233 AliCaloPeakFinderVectors *pfv = (AliCaloPeakFinderVectors *)entry->GetObject();
234 if( pfv == 0 )
bab48e74 235 {
11bcc677 236 cout << __FILE__ << ":" << __LINE__ << "_ ERRROR " << endl;
237 }
11bcc677 238 CopyVectors( pfv );
1f847d9f 239
11bcc677 240 if( pfv != 0 )
241 {
242 fIsInitialized = true;
168c7b3c 243 }
11bcc677 244 }
bab48e74 245}
246
247
bab48e74 248void
249AliCaloRawAnalyzerPeakFinder::WriteRootFile() const
afae9650 250{ // Utility function to write Peak-Finder vectors to an root file
bab48e74 251 // The output is used to create an OCDB entry.
bab48e74 252 fPeakFinderVectors->PrintVectors();
bab48e74 253 TFile *f = new TFile("peakfindervectors2.root", "recreate" );
254 fPeakFinderVectors->Write();
255 f->Close();
256 delete f;
48a2e3eb 257}
168c7b3c 258
259
260void
261AliCaloRawAnalyzerPeakFinder::PrintVectors()
afae9650 262{ // Utility function to write Peak-Finder vectors
168c7b3c 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}