2 /**************************************************************************
3 * This file is property of and copyright by *
4 * the Relativistic Heavy Ion Group (RHIG), Yale University, US, 2009 *
6 * Primary Author: Per Thomas Hille <perthomas.hille@yale.edu> *
8 * Contributors are mentioned in the code where appropriate. *
9 * Please report bugs to perthomas.hille@yale.edu *
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 **************************************************************************/
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.
29 #include "AliCaloRawAnalyzerPeakFinder.h"
30 #include "AliCaloBunchInfo.h"
31 #include "AliCaloFitResults.h"
34 #include "AliCDBEntry.h"
35 #include "AliCDBManager.h"
37 #include "AliCaloPeakFinderVectors.h"
42 ClassImp( AliCaloRawAnalyzerPeakFinder )
45 AliCaloRawAnalyzerPeakFinder::AliCaloRawAnalyzerPeakFinder() :AliCaloRawAnalyzer("Peak-Finder", "PF"),
47 fPeakFinderVectors(0),
52 fAlgo= Algo::kPeakFinder;
53 InitOCDB(fRunOnAlien);
54 fPeakFinderVectors = new AliCaloPeakFinderVectors() ;
61 AliCaloRawAnalyzerPeakFinder::InitOCDB(bool alien) const
63 // Setting the default OCDB pathe depending on wether we work locally or on the GRID.
64 AliCDBManager::Instance()->SetDefaultStorage( alien == true ? "alien://$ALICE_ROOT/OCDB" : "local://$ALICE_ROOT/OCDB");
65 AliCDBManager::Instance()->SetRun(100);
70 AliCaloRawAnalyzerPeakFinder::ResetVectors()
73 for(int i=0; i < PF::MAXSTART; i++)
75 for(int j=0; j < PF::SAMPLERANGE; j++ )
77 for(int k=0; k < 100; k++ )
79 fPFAmpVectors[i][j][k] = 0;
80 fPFTofVectors[i][j][k] = 0;
81 fPFAmpVectorsCoarse[i][j][k] = 0;
82 fPFTofVectorsCoarse[i][j][k] = 0;
89 AliCaloRawAnalyzerPeakFinder::~AliCaloRawAnalyzerPeakFinder()
96 AliCaloRawAnalyzerPeakFinder::ScanCoarse(const Double_t *const array, const int length ) const
98 // Fisrt (coarce) estimate of Amplitude using the Peak-Finder.
99 // The output of the first iteration is sued to select vectors
100 // for the second iteration.
105 for(int i=0; i < length; i++)
107 tmpTof += fPFTofVectorsCoarse[0][length][i]*array[i];
108 tmpAmp += fPFAmpVectorsCoarse[0][length][i]*array[i];
111 tmpTof = tmpTof / tmpAmp ;
117 AliCaloRawAnalyzerPeakFinder::Evaluate( const vector<AliCaloBunchInfo> &bunchvector, const UInt_t altrocfg1, const UInt_t altrocfg2 )
119 if( fIsInitialized == false )
121 cout << __FILE__ << ":" << __LINE__ << "ERROR, peakfinder vectors not loaded" << endl;
122 return AliCaloFitResults(kInvalid, kInvalid);
125 // Extracting the amplitude using the Peak-Finder algorithm
126 // The amplitude is a weighted sum of the samples using
129 short maxampindex; //index of maximum amplitude
130 short maxamp; //Maximum amplitude
132 int index = SelectBunch( bunchvector, &maxampindex, &maxamp );
136 Float_t ped = ReverseAndSubtractPed( &(bunchvector.at(index)) , altrocfg1, altrocfg2, fReversed );
137 Float_t maxf = TMath::MaxElement( bunchvector.at(index).GetLength(), fReversed );
138 short timebinOffset = maxampindex - (bunchvector.at( index ).GetLength()-1);
140 if( maxf < fAmpCut || ( maxamp - ped) > fOverflowCut ) // (maxamp - ped) > fOverflowCut = Close to saturation (use low gain then)
142 return AliCaloFitResults( maxamp, ped, Ret::kCrude, maxf, timebinOffset);
144 else if ( maxf >= fAmpCut )
148 short maxrev = maxampindex - bunchvector.at(index).GetStartBin();
149 SelectSubarray( fReversed, bunchvector.at(index).GetLength(), maxrev, &first, &last);
150 int nsamples = last - first;
152 if( ( nsamples ) >= fNsampleCut ) // no if statement needed really; keep for readability
154 int startbin = bunchvector.at(index).GetStartBin();
155 int n = last - first;
156 int pfindex = n - fNsampleCut;
157 pfindex = pfindex > PF::SAMPLERANGE ? PF::SAMPLERANGE : pfindex;
159 int dt = maxampindex - startbin -2;
163 Float_t tmptof = ScanCoarse( &fReversed[dt] , n );
170 if( tmptof > -1 && tmptof < 100 )
181 for(int k=0; k < PF::SAMPLERANGE; k++ )
183 tof += fPFTofVectors[0][pfindex][k]*fReversed[ dt +k + tmpindex -1 ];
186 for( int i=0; i < PF::SAMPLERANGE; i++ )
189 fAmp += fPFAmpVectors[0][pfindex][i]*fReversed[ dt +i +tmpindex -1 ];
192 if( TMath::Abs( (maxf - fAmp )/maxf ) > 0.1 )
194 // cout << __FILE__ << ":" << __LINE__ << "WARNING: amp was" << fAmp <<", but was changed to "<< maxf << endl;
198 tof = timebinOffset - 0.01*tof/fAmp; // clock ticks
199 // use local-array time for chi2 estimate
200 Float_t chi2 = CalculateChi2(fAmp, tof-timebinOffset+maxrev, first, last);
201 Int_t ndf = last - first - 1; // nsamples - 2
202 return AliCaloFitResults( maxamp, ped , Ret::kFitPar, fAmp, tof,
203 timebinOffset, chi2, ndf,
204 Ret::kDummy, AliCaloFitSubarray(index, maxrev, first, last) );
208 Float_t chi2 = CalculateChi2(maxf, maxrev, first, last);
209 Int_t ndf = last - first - 1; // nsamples - 2
210 return AliCaloFitResults( maxamp, ped , Ret::kCrude, maxf, timebinOffset,
211 timebinOffset, chi2, ndf, Ret::kDummy, AliCaloFitSubarray(index, maxrev, first, last) );
215 return AliCaloFitResults(kInvalid, kInvalid);
220 AliCaloRawAnalyzerPeakFinder::CopyVectors( const AliCaloPeakFinderVectors *const pfv )
225 for(int i = 0; i < PF::MAXSTART ; i++)
227 for( int j=0; j < PF::SAMPLERANGE; j++)
229 pfv->GetVector( i, j, fPFAmpVectors[i][j] , fPFTofVectors[i][j],
230 fPFAmpVectorsCoarse[i][j] , fPFTofVectorsCoarse[i][j] );
232 fPeakFinderVectors->SetVector( i, j, fPFAmpVectors[i][j], fPFTofVectors[i][j],
233 fPFAmpVectorsCoarse[i][j], fPFTofVectorsCoarse[i][j] );
239 AliFatal( "pfv = ZERO !!!!!!!");
245 AliCaloRawAnalyzerPeakFinder::LoadVectorsOCDB()
247 //Loading of Peak-Finder vectors from the
248 //Offline Condition Database (OCDB)
249 AliCDBEntry* entry = AliCDBManager::Instance()->Get("EMCAL/Calib/PeakFinder/");
250 cout << __FILE__ << ":" << __LINE__ << ": Printing metadata !! " << endl;
251 entry->PrintMetaData();
255 AliCaloPeakFinderVectors *pfv = (AliCaloPeakFinderVectors *)entry->GetObject();
258 cout << __FILE__ << ":" << __LINE__ << "_ ERRROR " << endl;
265 fIsInitialized = true;
272 AliCaloRawAnalyzerPeakFinder::LoadVectorsASCII()
274 //Read in the Peak finder vecors from ASCI files
275 fIsInitialized= true;
276 const Int_t buffersize = 256;
277 for(int i = 0; i < PF::MAXSTART ; i++)
279 for( int j=0; j < PF::SAMPLERANGE; j++)
281 char filenameCoarse[buffersize];
282 char filename[buffersize];
283 int n = j+fNsampleCut;
284 double start = (double)i+0;
286 snprintf(filename, buffersize, "%s/EMCAL/vectors-emcal/start%.1fN%dtau0.235fs10dt1.0.txt", getenv("ALICE_ROOT"), start, n);
287 snprintf(filenameCoarse, buffersize, "%s/EMCAL/vectors-emcal/start%.1fN%dtau0.235fs10dt3.0.txt", getenv("ALICE_ROOT"), start, n);
289 FILE *fp = fopen(filename, "r");
290 FILE *fpc = fopen(filenameCoarse, "r");
294 AliFatal( Form( "could not open file: %s", filename ) );
298 AliFatal( Form( "could not open file: %s", filenameCoarse ) );
302 for(int m = 0; m < n ; m++ )
304 fscanf(fp, "%lf\t", &fPFAmpVectors[i][j][m] );
305 fscanf(fpc, "%lf\t", &fPFAmpVectorsCoarse[i][j][m] );
309 for(int m = 0; m < n ; m++ )
311 fscanf(fp, "%lf\t", &fPFTofVectors[i][j][m] );
312 fscanf(fpc, "%lf\t", &fPFTofVectorsCoarse[i][j][m] );
315 fPeakFinderVectors->SetVector( i, j, fPFAmpVectors[i][j], fPFTofVectors[i][j],
316 fPFAmpVectorsCoarse[i][j], fPFTofVectorsCoarse[i][j] );
327 AliCaloRawAnalyzerPeakFinder::WriteRootFile() const
329 // Utility function to write Peak-Finder vectors to an root file
330 // The output is used to create an OCDB entry.
331 fPeakFinderVectors->PrintVectors();
332 TFile *f = new TFile("peakfindervectors2.root", "recreate" );
333 fPeakFinderVectors->Write();
340 AliCaloRawAnalyzerPeakFinder::PrintVectors()
342 for(int i=0; i < 20; i++)
344 for( int j = 0; j < PF::MAXSTART; j ++ )
346 for( int k=0; k < PF::SAMPLERANGE; k++ )
348 cout << fPFAmpVectors[j][k][i] << "\t" ;
353 cout << __FILE__ << ":" << __LINE__ << ":.... DONE !!" << endl;