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