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"
39 //#include "AliEMCALRawUtils.h"
44 ClassImp( AliCaloRawAnalyzerPeakFinder )
47 AliCaloRawAnalyzerPeakFinder::AliCaloRawAnalyzerPeakFinder() :AliCaloRawAnalyzer("Peak-Finder", "PF"),
49 fPeakFinderVectors(0),
54 fAlgo= Algo::kPeakFinder;
55 InitOCDB(fRunOnAlien);
56 fPeakFinderVectors = new AliCaloPeakFinderVectors() ;
63 AliCaloRawAnalyzerPeakFinder::InitOCDB(bool alien) const
65 // Setting the default OCDB pathe depending on wether we work locally or on the GRID.
68 // if( AliCDBManager::Instance()->HasStorage("alien://$ALICE_ROOT/OCDB") == false && AliCDBManager::Instance()->HasStorage("local://$ALICE_ROOT/OCDB") == false )
69 if( !AliCDBManager::Instance()->IsDefaultStorageSet ())
71 AliCDBManager::Instance()->SetDefaultStorage( alien == true ? "alien://$ALICE_ROOT/OCDB" : "local://$ALICE_ROOT/OCDB" );
72 AliCDBManager::Instance()->SetRun(100);
78 AliCaloRawAnalyzerPeakFinder::ResetVectors()
81 for(int i=0; i < PF::MAXSTART; i++)
83 for(int j=0; j < PF::SAMPLERANGE; j++ )
85 for(int k=0; k < 100; k++ )
87 fPFAmpVectors[i][j][k] = 0;
88 fPFTofVectors[i][j][k] = 0;
89 fPFAmpVectorsCoarse[i][j][k] = 0;
90 fPFTofVectorsCoarse[i][j][k] = 0;
97 AliCaloRawAnalyzerPeakFinder::~AliCaloRawAnalyzerPeakFinder()
104 AliCaloRawAnalyzerPeakFinder::ScanCoarse(const Double_t *const array, const int length ) const
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.
113 for(int i=0; i < length; i++)
115 tmpTof += fPFTofVectorsCoarse[0][length][i]*array[i];
116 tmpAmp += fPFAmpVectorsCoarse[0][length][i]*array[i];
119 tmpTof = tmpTof / tmpAmp ;
125 AliCaloRawAnalyzerPeakFinder::Evaluate( const vector<AliCaloBunchInfo> &bunchvector, const UInt_t altrocfg1, const UInt_t altrocfg2 )
127 if( fIsInitialized == false )
129 cout << __FILE__ << ":" << __LINE__ << "ERROR, peakfinder vectors not loaded" << endl;
130 return AliCaloFitResults(kInvalid, kInvalid);
133 // Extracting the amplitude using the Peak-Finder algorithm
134 // The amplitude is a weighted sum of the samples using
137 short maxampindex; //index of maximum amplitude
138 short maxamp; //Maximum amplitude
140 int index = SelectBunch( bunchvector, &maxampindex, &maxamp );
144 Float_t ped = ReverseAndSubtractPed( &(bunchvector.at(index)) , altrocfg1, altrocfg2, fReversed );
145 Float_t maxf = TMath::MaxElement( bunchvector.at(index).GetLength(), fReversed );
146 short timebinOffset = maxampindex - (bunchvector.at( index ).GetLength()-1);
148 if( maxf < fAmpCut || ( maxamp - ped) > fOverflowCut ) // (maxamp - ped) > fOverflowCut = Close to saturation (use low gain then)
150 return AliCaloFitResults( maxamp, ped, Ret::kCrude, maxf, timebinOffset);
152 else if ( maxf >= fAmpCut )
156 short maxrev = maxampindex - bunchvector.at(index).GetStartBin();
157 SelectSubarray( fReversed, bunchvector.at(index).GetLength(), maxrev, &first, &last, fFitArrayCut);
158 int nsamples = last - first;
160 if( ( nsamples ) >= fNsampleCut ) // no if statement needed really; keep for readability
162 int startbin = bunchvector.at(index).GetStartBin();
163 int n = last - first;
164 int pfindex = n - fNsampleCut;
165 pfindex = pfindex > PF::SAMPLERANGE ? PF::SAMPLERANGE : pfindex;
167 int dt = maxampindex - startbin -2;
171 Float_t tmptof = ScanCoarse( &fReversed[dt] , n );
178 if( tmptof > -1 && tmptof < 100 )
189 for(int k=0; k < PF::SAMPLERANGE; k++ )
191 tof += fPFTofVectors[0][pfindex][k]*fReversed[ dt +k + tmpindex -1 ];
194 for( int i=0; i < PF::SAMPLERANGE; i++ )
197 fAmp += fPFAmpVectors[0][pfindex][i]*fReversed[ dt +i +tmpindex -1 ];
200 if( TMath::Abs( (maxf - fAmp )/maxf ) > 0.1 )
202 // cout << __FILE__ << ":" << __LINE__ << "WARNING: amp was" << fAmp <<", but was changed to "<< maxf << endl;
206 // tof = timebinOffset - 0.01*tof/fAmp; // clock ticks
207 tof = timebinOffset - 0.01*tof/fAmp - fL1Phase/TIMEBINWITH; // clock
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
212 return AliCaloFitResults( maxamp, ped , Ret::kFitPar, fAmp, tof,
213 timebinOffset, chi2, ndf,
214 Ret::kDummy, AliCaloFitSubarray(index, maxrev, first, last) );
218 Float_t chi2 = CalculateChi2(maxf, maxrev, first, last);
219 Int_t ndf = last - first - 1; // nsamples - 2
220 return AliCaloFitResults( maxamp, ped , Ret::kCrude, maxf, timebinOffset,
221 timebinOffset, chi2, ndf, Ret::kDummy, AliCaloFitSubarray(index, maxrev, first, last) );
225 return AliCaloFitResults(kInvalid, kInvalid);
230 AliCaloRawAnalyzerPeakFinder::CopyVectors( const AliCaloPeakFinderVectors *const pfv )
235 for(int i = 0; i < PF::MAXSTART ; i++)
237 for( int j=0; j < PF::SAMPLERANGE; j++)
239 // cout << __FILE__ << ":" << __LINE__ << ": TPX !!; i= "<< i << " j = "<< j << endl;
240 // pfv->GetVector( i, j, fPFAmpVectors[i][j] , fPFTofVectors[i][j],
241 // fPFAmpVectorsCoarse[i][j] , fPFTofVectorsCoarse[i][j] );
243 fPeakFinderVectors->SetVector( i, j, fPFAmpVectors[i][j], fPFTofVectors[i][j],
244 fPFAmpVectorsCoarse[i][j], fPFTofVectorsCoarse[i][j] );
250 AliFatal( "pfv = ZERO !!!!!!!");
256 AliCaloRawAnalyzerPeakFinder::LoadVectorsOCDB()
258 //Loading of Peak-Finder vectors from the
259 //Offline Condition Database (OCDB)
260 AliCDBEntry* entry = AliCDBManager::Instance()->Get("EMCAL/Calib/PeakFinder/");
265 cout << __FILE__ << ":" << __LINE__ << ": Printing metadata !! " << endl;
266 entry->PrintMetaData();
268 cout << __FILE__ << ":" << __LINE__ << ": Finnsihed Printing metadata !! " << endl;
270 AliCaloPeakFinderVectors *pfv = (AliCaloPeakFinderVectors *)entry->GetObject();
271 cout << __FILE__ << ":" << __LINE__ << ": got pfv !! " << endl;
275 cout << __FILE__ << ":" << __LINE__ << "_ ERRROR " << endl;
278 cout << __FILE__ << ":" << __LINE__ << ": TP0 !! " << endl;
281 cout << __FILE__ << ":" << __LINE__ << ": TP1 !! " << endl;
285 fIsInitialized = true;
293 AliCaloRawAnalyzerPeakFinder::LoadVectorsASCII()
295 //Read in the Peak finder vecors from ASCI files
296 fIsInitialized= true;
297 const Int_t buffersize = 256;
298 for(int i = 0; i < PF::MAXSTART ; i++)
300 for( int j=0; j < PF::SAMPLERANGE; j++)
302 char filenameCoarse[buffersize];
303 char filename[buffersize];
304 int n = j+fNsampleCut;
305 double start = (double)i+0;
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);
310 FILE *fp = fopen(filename, "r");
311 FILE *fpc = fopen(filenameCoarse, "r");
315 AliFatal( Form( "could not open file: %s", filename ) );
319 AliFatal( Form( "could not open file: %s", filenameCoarse ) );
323 for(int m = 0; m < n ; m++ )
325 fscanf(fp, "%lf\t", &fPFAmpVectors[i][j][m] );
326 fscanf(fpc, "%lf\t", &fPFAmpVectorsCoarse[i][j][m] );
330 for(int m = 0; m < n ; m++ )
332 fscanf(fp, "%lf\t", &fPFTofVectors[i][j][m] );
333 fscanf(fpc, "%lf\t", &fPFTofVectorsCoarse[i][j][m] );
336 fPeakFinderVectors->SetVector( i, j, fPFAmpVectors[i][j], fPFTofVectors[i][j],
337 fPFAmpVectorsCoarse[i][j], fPFTofVectorsCoarse[i][j] );
350 AliCaloRawAnalyzerPeakFinder::WriteRootFile() const
352 // Utility function to write Peak-Finder vectors to an root file
353 // The output is used to create an OCDB entry.
354 fPeakFinderVectors->PrintVectors();
355 TFile *f = new TFile("peakfindervectors2.root", "recreate" );
356 fPeakFinderVectors->Write();
363 AliCaloRawAnalyzerPeakFinder::PrintVectors()
365 for(int i=0; i < 20; i++)
367 for( int j = 0; j < PF::MAXSTART; j ++ )
369 for( int k=0; k < PF::SAMPLERANGE; k++ )
371 cout << fPFAmpVectors[j][k][i] << "\t" ;
376 cout << __FILE__ << ":" << __LINE__ << ":.... DONE !!" << endl;