]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - EMCAL/AliCaloRawAnalyzerPeakFinder.cxx
correct mask for V0 charge decoding in STU payload
[u/mrichter/AliRoot.git] / EMCAL / AliCaloRawAnalyzerPeakFinder.cxx
index e40d5052fb48ebb54fb7fd013385878c916ac27b..24f7df1dfba1ff74bed997f5d82c70a2983802c9 100644 (file)
@@ -1,3 +1,4 @@
+// -*- mode: c++ -*-
 /**************************************************************************
  * This file is property of and copyright by                              *
  * the Relativistic Heavy Ion Group (RHIG), Yale University, US, 2009     *
@@ -16,7 +17,6 @@
  * provided "as is" without express or implied warranty.                  *
  **************************************************************************/
 
-
 // The Peak-Finder algorithm
 // The amplitude is extracted  as a
 // weighted sum of the samples using the 
 #include "AliCDBManager.h"
 #include "TFile.h"
 #include "AliCaloPeakFinderVectors.h"
+#include <iostream>
 
 using namespace std;
 
 
+
 ClassImp( AliCaloRawAnalyzerPeakFinder )
 
 
 AliCaloRawAnalyzerPeakFinder::AliCaloRawAnalyzerPeakFinder() :AliCaloRawAnalyzer("Peak-Finder", "PF"),  
-                                                             fAmp(0),
                                                              fPeakFinderVectors(0),
-                                                             fRunOnAlien(false)
+                                                             fRunOnAlien(false),
+                                                             fIsInitialized(false)
 {
   //Comment
+  fAlgo= Algo::kPeakFinder;
   InitOCDB(fRunOnAlien);
   fPeakFinderVectors = new AliCaloPeakFinderVectors() ;
   ResetVectors();
@@ -59,8 +62,11 @@ void
 AliCaloRawAnalyzerPeakFinder::InitOCDB(bool alien) const
 {
   // Setting the default OCDB pathe depending on wether we work locally or on the GRID.
-  AliCDBManager::Instance()->SetDefaultStorage(  alien == true ? "alien://$ALICE_ROOT/OCDB" : "local://$ALICE_ROOT/OCDB");
-  AliCDBManager::Instance()->SetRun(100);
+  if( !AliCDBManager::Instance()->IsDefaultStorageSet ())
+    {
+      AliCDBManager::Instance()->SetDefaultStorage(  alien == true ? "alien://$ALICE_ROOT/OCDB" : "local://$ALICE_ROOT/OCDB" );
+      AliCDBManager::Instance()->SetRun(100);
+    }
 }
 
 
@@ -68,9 +74,9 @@ void
 AliCaloRawAnalyzerPeakFinder::ResetVectors()
 {
   //As name implies
-  for(int i=0; i < MAXSTART; i++)
+  for(int i=0; i < PF::MAXSTART; i++)
     {
-      for(int j=0; j < SAMPLERANGE; j++ )
+      for(int j=0; j < PF::SAMPLERANGE; j++ )
        {
          for(int k=0; k < 100; k++ )
            {
@@ -87,20 +93,9 @@ AliCaloRawAnalyzerPeakFinder::ResetVectors()
 AliCaloRawAnalyzerPeakFinder::~AliCaloRawAnalyzerPeakFinder()
 {
   //comment
-  for(int i=0; i < MAXSTART; i++)
-    {
-      for(int j=0; j < SAMPLERANGE; j++ )
-       {
-         delete[] fPFAmpVectors[i][j];
-         delete[] fPFTofVectors[i][j];
-         delete[] fPFAmpVectorsCoarse[i][j];
-         delete[] fPFTofVectorsCoarse[i][j];
-       }
-    }
 }
 
 
-
 Double_t  
 AliCaloRawAnalyzerPeakFinder::ScanCoarse(const Double_t *const array, const int length ) const
 {
@@ -125,6 +120,13 @@ AliCaloRawAnalyzerPeakFinder::ScanCoarse(const Double_t *const array, const int
 AliCaloFitResults 
 AliCaloRawAnalyzerPeakFinder::Evaluate( const vector<AliCaloBunchInfo> &bunchvector, const UInt_t altrocfg1,  const UInt_t altrocfg2 )
 {
+  // Evaluation of amplitude and TOF
+  if( fIsInitialized == false )
+    {
+      cout << __FILE__ << ":" << __LINE__ << "ERROR, peakfinder vectors not loaded" << endl;
+      return  AliCaloFitResults(kInvalid, kInvalid);
+    }
+
   // Extracting the amplitude using the Peak-Finder algorithm
   // The amplitude is a weighted sum of the samples using 
   // optimum weights.
@@ -133,23 +135,23 @@ AliCaloRawAnalyzerPeakFinder::Evaluate( const vector<AliCaloBunchInfo> &bunchvec
   short maxamp; //Maximum amplitude
   fAmp = 0;
   int index = SelectBunch( bunchvector,  &maxampindex,  &maxamp );
-
+  
   if( index >= 0)
     {
       Float_t ped = ReverseAndSubtractPed( &(bunchvector.at(index))  ,  altrocfg1, altrocfg2, fReversed  );
       Float_t maxf = TMath::MaxElement(   bunchvector.at(index).GetLength(),  fReversed );
       short timebinOffset = maxampindex - (bunchvector.at( index ).GetLength()-1); 
-            
       if(  maxf < fAmpCut  ||  ( maxamp - ped) > fOverflowCut  ) // (maxamp - ped) > fOverflowCut = Close to saturation (use low gain then)
        {
-         return  AliCaloFitResults( maxamp, ped, AliCaloFitResults::kCrude, maxf, timebinOffset);
+         return  AliCaloFitResults( maxamp, ped, Ret::kCrude, maxf, timebinOffset);
        }            
       else if ( maxf >= fAmpCut )
        {
          int first = 0;
          int last = 0;
          short maxrev = maxampindex  -  bunchvector.at(index).GetStartBin();     
-         SelectSubarray( fReversed,  bunchvector.at(index).GetLength(), maxrev, &first, &last);
+         SelectSubarray( fReversed,  bunchvector.at(index).GetLength(), maxrev, &first, &last, fFitArrayCut);
          int nsamples =  last - first;
 
          if( ( nsamples  )  >= fNsampleCut ) // no if statement needed really; keep for readability
@@ -157,11 +159,9 @@ AliCaloRawAnalyzerPeakFinder::Evaluate( const vector<AliCaloBunchInfo> &bunchvec
              int startbin = bunchvector.at(index).GetStartBin();  
              int n = last - first;  
              int pfindex = n - fNsampleCut; 
-             pfindex = pfindex > SAMPLERANGE ? SAMPLERANGE : pfindex;
-
+             pfindex = pfindex > PF::SAMPLERANGE ? PF::SAMPLERANGE : pfindex;
              int dt =  maxampindex - startbin -2; 
              int tmpindex = 0;
-
              Float_t tmptof = ScanCoarse( &fReversed[dt] , n );
              
              if( tmptof < -1 )
@@ -179,58 +179,56 @@ AliCaloRawAnalyzerPeakFinder::Evaluate( const vector<AliCaloBunchInfo> &bunchvec
                  }
 
              double tof = 0;
-             
-             for(int k=0; k < SAMPLERANGE; k++   )
+             for(int k=0; k < PF::SAMPLERANGE; k++   )
                {
                  tof +=  fPFTofVectors[0][pfindex][k]*fReversed[ dt  +k + tmpindex -1 ];   
                }
-           
-             for( int i=0; i < SAMPLERANGE; i++ )
+             for( int i=0; i < PF::SAMPLERANGE; i++ )
                {
                  {
+                   
                    fAmp += fPFAmpVectors[0][pfindex][i]*fReversed[ dt  +i  +tmpindex -1 ];
                  }
                }
+
              if( TMath::Abs(  (maxf - fAmp  )/maxf )  >   0.1 )
                {
                  fAmp = maxf;
                }
              
-             tof = timebinOffset - 0.01*tof/fAmp; // clock ticks
-             
-             // use local-array time for chi2 estimate
+             tof = timebinOffset - 0.01*tof/fAmp - fL1Phase/TIMEBINWITH; // clock
              Float_t chi2 = CalculateChi2(fAmp, tof-timebinOffset+maxrev, first, last);
              Int_t ndf = last - first - 1; // nsamples - 2
-             return AliCaloFitResults( maxamp, ped , AliCaloFitResults::kFitPar, fAmp, tof, 
+             return AliCaloFitResults( maxamp, ped , Ret::kFitPar, fAmp, tof, 
                                        timebinOffset, chi2, ndf,
-                                       AliCaloFitResults::kDummy, AliCaloFitSubarray(index, maxrev, first, last) );  
+                                       Ret::kDummy, AliCaloFitSubarray(index, maxrev, first, last) );  
            }
          else
            {
              Float_t chi2 = CalculateChi2(maxf, maxrev, first, last);
              Int_t ndf = last - first - 1; // nsamples - 2
-             return AliCaloFitResults( maxamp, ped , AliCaloFitResults::kCrude, maxf, timebinOffset,
-                                       timebinOffset, chi2, ndf, AliCaloFitResults::kDummy, AliCaloFitSubarray(index, maxrev, first, last) ); 
+             return AliCaloFitResults( maxamp, ped , Ret::kCrude, maxf, timebinOffset,
+                                       timebinOffset, chi2, ndf, Ret::kDummy, AliCaloFitSubarray(index, maxrev, first, last) ); 
            }
        } // ampcut
     }
-  return  AliCaloFitResults(AliCaloFitResults::kInvalid, AliCaloFitResults::kInvalid);
+  return  AliCaloFitResults(kInvalid, kInvalid);
 }
 
 
 void   
-AliCaloRawAnalyzerPeakFinder::CopyVectors(const AliCaloPeakFinderVectors *const pfv )
+AliCaloRawAnalyzerPeakFinder::CopyVectors( const AliCaloPeakFinderVectors *const pfv )
 {
   // As name implies
-
   if ( pfv != 0)
     {
-      for(int i = 0;  i < MAXSTART ; i++)
+      for(int i = 0;  i < PF::MAXSTART ; i++)
        {
-         for( int j=0; j < SAMPLERANGE; j++)  
+         for( int j=0; j < PF::SAMPLERANGE; j++)  
            {
              pfv->GetVector( i, j, fPFAmpVectors[i][j] ,  fPFTofVectors[i][j],    
-                             fPFAmpVectorsCoarse[i][j] , fPFTofVectorsCoarse[i][j]  ); 
+                             fPFAmpVectorsCoarse[i][j] , fPFTofVectorsCoarse[i][j]  ); 
+
              fPeakFinderVectors->SetVector( i, j, fPFAmpVectors[i][j], fPFTofVectors[i][j],    
                                             fPFAmpVectorsCoarse[i][j], fPFTofVectorsCoarse[i][j] );   
            }
@@ -243,7 +241,6 @@ AliCaloRawAnalyzerPeakFinder::CopyVectors(const AliCaloPeakFinderVectors *const
 }
 
 
-
 void   
 AliCaloRawAnalyzerPeakFinder::LoadVectorsOCDB()
 {
@@ -252,10 +249,21 @@ AliCaloRawAnalyzerPeakFinder::LoadVectorsOCDB()
   AliCDBEntry* entry = AliCDBManager::Instance()->Get("EMCAL/Calib/PeakFinder/");
   
   if( entry != 0 )
+  {
+    //cout << __FILE__ << ":" << __LINE__ << ": Printing metadata !! " << endl;
+    //entry->PrintMetaData();
+    AliCaloPeakFinderVectors  *pfv = (AliCaloPeakFinderVectors *)entry->GetObject(); 
+    if( pfv == 0 )
     {
-      AliCaloPeakFinderVectors  *pfv = (AliCaloPeakFinderVectors *)entry->GetObject(); 
-      CopyVectors( pfv );
-     }
+      cout << __FILE__ << ":" << __LINE__ << "_ ERRROR " << endl;
+    }
+    CopyVectors( pfv );
+    
+    if( pfv != 0 )
+    {
+      fIsInitialized = true;
+    }
+  }
 }
 
 
@@ -263,53 +271,65 @@ void
 AliCaloRawAnalyzerPeakFinder::LoadVectorsASCII()
 {
   //Read in the Peak finder vecors from ASCI files
-  for(int i = 0;  i < MAXSTART ; i++)
+  fIsInitialized= true;  
+  const Int_t buffersize = 256;
+  for(int i = 0;  i < PF::MAXSTART ; i++)
+  {
+    for( int j=0; j < PF::SAMPLERANGE; j++)
     {
-      for( int j=0; j < SAMPLERANGE; j++)
-       {
-         char filenameCoarse[256];
-         char filename[256];
-         int n = j+fNsampleCut;
-         double start = (double)i+0;
-         
-         sprintf(filename,        "%s/EMCAL/vectors-emcal/start%.1fN%dtau0.235fs10dt1.0.txt", getenv("ALICE_ROOT"), start, n);
-         sprintf(filenameCoarse,  "%s/EMCAL/vectors-emcal/start%.1fN%dtau0.235fs10dt3.0.txt", getenv("ALICE_ROOT"), start, n);
-         
-         FILE *fp  =  fopen(filename, "r");
-         FILE *fpc =  fopen(filenameCoarse, "r");
-
-         if( fp == 0 )
+      char filenameCoarse[buffersize];
+      char filename[buffersize];
+      int n = j+fNsampleCut;
+      double start = (double)i+0;
+      
+      snprintf(filename, buffersize,       "%s/EMCAL/vectors-emcal/start%.1fN%dtau0.235fs10dt1.0.txt", getenv("ALICE_ROOT"), start, n);
+      snprintf(filenameCoarse, buffersize, "%s/EMCAL/vectors-emcal/start%.1fN%dtau0.235fs10dt3.0.txt", getenv("ALICE_ROOT"), start, n);
+      
+      FILE *fp  =  fopen(filename, "r");
+      FILE *fpc =  fopen(filenameCoarse, "r");
+
+      int status = 0;
+      int statusc = 0;
+      
+      if( fp == 0 )
            {
              AliFatal( Form( "could not open file: %s", filename ) );
            }
-       
-         if(fpc == 0)
+      else if(fpc == 0)
            {
              AliFatal( Form( "could not open file: %s", filenameCoarse ) );
            }
-         else
+      else
            {
              for(int m = 0; m < n ; m++ )
                {
-                 fscanf(fp, "%lf\t", &fPFAmpVectors[i][j][m] );
-                 fscanf(fpc, "%lf\t", &fPFAmpVectorsCoarse[i][j][m] );
+                 status = fscanf(fp,  "%lf\t", &fPFAmpVectors[i][j][m] );
+                 statusc = fscanf(fpc, "%lf\t", &fPFAmpVectorsCoarse[i][j][m] );
+                 if (!status) { AliFatal( Form( "could not read file: %s", filename ) ); }
+                 if (!statusc) { AliFatal( Form( "could not read file: %s", filenameCoarse ) ); }
                }
-             fscanf(fp,   "\n" );
-             fscanf(fpc,  "\n" );
+             status = fscanf(fp,   "\n" );
+             statusc = fscanf(fpc,  "\n" );
+             if (status < 0) { AliFatal( Form( "could not read file: %s", filename ) ); }
+             if (statusc < 0) { AliFatal( Form( "could not read file: %s", filenameCoarse ) ); }
              for(int m = 0; m < n ; m++ )
                {
-                 fscanf(fp, "%lf\t",   &fPFTofVectors[i][j][m]  );
-                 fscanf(fpc, "%lf\t",  &fPFTofVectorsCoarse[i][j][m]  );  
+                 status = fscanf(fp, "%lf\t",   &fPFTofVectors[i][j][m]  );
+                 statusc = fscanf(fpc, "%lf\t",  &fPFTofVectorsCoarse[i][j][m]  );  
+                 if (!status) { AliFatal( Form( "could not read file: %s", filename ) ); }
+                 if (!statusc) { AliFatal( Form( "could not read file: %s", filenameCoarse ) ); }
                }
              
              fPeakFinderVectors->SetVector( i, j, fPFAmpVectors[i][j], fPFTofVectors[i][j],    
-                                            fPFAmpVectorsCoarse[i][j], fPFTofVectorsCoarse[i][j] );   
-                                                    
-             fclose (fp);
-             fclose (fpc);
+                                      fPFAmpVectorsCoarse[i][j], fPFTofVectorsCoarse[i][j] );   
+        
            }
-       }
+      
+      if(fp) fclose (fp );
+      if(fpc)fclose (fpc);
+      
     }
+  }
 }
 
 
@@ -324,3 +344,21 @@ AliCaloRawAnalyzerPeakFinder::WriteRootFile() const
   f->Close();
   delete f;
 }
+
+
+void 
+AliCaloRawAnalyzerPeakFinder::PrintVectors()
+{
+  for(int i=0; i < 20; i++)
+    {
+      for( int j = 0; j < PF::MAXSTART; j ++ )
+       {
+         for( int k=0; k < PF::SAMPLERANGE; k++ )
+           {
+             cout << fPFAmpVectors[j][k][i] << "\t" ;
+           }
+       }
+      cout << endl;
+    }
+  cout << __FILE__ << ":" << __LINE__ << ":.... DONE !!" << endl;
+}