LCOV - code coverage report
Current view: top level - PHOS/PHOSbase - AliPHOSRawFitterv1.cxx (source / functions) Hit Total Coverage
Test: coverage.info Lines: 1 259 0.4 %
Date: 2016-06-14 17:26:59 Functions: 1 12 8.3 %

          Line data    Source code
       1             : /**************************************************************************
       2             :  * Copyright(c) 2007, ALICE Experiment at CERN, All rights reserved.      *
       3             :  *                                                                        *
       4             :  * Author: The ALICE Off-line Project.                                    *
       5             :  * Contributors are mentioned in the code where appropriate.              *
       6             :  *                                                                        *
       7             :  * Permission to use, copy, modify and distribute this software and its   *
       8             :  * documentation strictly for non-commercial purposes is hereby granted   *
       9             :  * without fee, provided that the above copyright notice appears in all   *
      10             :  * copies and that both the copyright notice and this permission notice   *
      11             :  * appear in the supporting documentation. The authors make no claims     *
      12             :  * about the suitability of this software for any purpose. It is          *
      13             :  * provided "as is" without express or implied warranty.                  *
      14             :  **************************************************************************/
      15             : 
      16             : /* $Id: $ */
      17             : 
      18             : // This class extracts the signal parameters (energy, time, quality)
      19             : // from ALTRO samples. Energy is in ADC counts, time is in time bin units.
      20             : // A fitting algorithm evaluates the energy and the time from Minuit minimization
      21             : // 
      22             : // Typical use case:
      23             : //     AliPHOSRawFitter *fitter=new AliPHOSRawFitter();
      24             : //     fitter->SetChannelGeo(module,cellX,cellZ,caloFlag);
      25             : //     fitter->SetCalibData(fgCalibData) ;
      26             : //     fitter->Eval(sig,sigStart,sigLength);
      27             : //     Double_t amplitude = fitter.GetEnergy();
      28             : //     Double_t time      = fitter.GetTime();
      29             : //     Bool_t   isLowGain = fitter.GetCaloFlag()==0;
      30             : 
      31             : // Author: Dmitri Peressounko (Oct.2008)
      32             : // Modified: Yuri Kharlov (Jul.2009)
      33             : 
      34             : // --- ROOT system ---
      35             : #include "TArrayI.h"
      36             : #include "TList.h"
      37             : #include "TMath.h"
      38             : #include "TMinuit.h"
      39             : 
      40             : // --- AliRoot header files ---
      41             : #include "AliLog.h"
      42             : #include "AliPHOSCalibData.h"
      43             : #include "AliPHOSRawFitterv1.h"
      44             : #include "AliPHOSPulseGenerator.h"
      45             : 
      46          22 : ClassImp(AliPHOSRawFitterv1)
      47             : 
      48             : //-----------------------------------------------------------------------------
      49             : AliPHOSRawFitterv1::AliPHOSRawFitterv1():
      50           0 :   AliPHOSRawFitterv0(),
      51           0 :   fSampleParamsLow(0x0),
      52           0 :   fSampleParamsHigh(0x0),
      53           0 :   fToFit(0x0)
      54           0 : {
      55             :   //Default constructor.
      56           0 :   if(!gMinuit) 
      57           0 :     gMinuit = new TMinuit(100);
      58           0 :   fSampleParamsHigh =new TArrayD(7) ;
      59           0 :   fSampleParamsHigh->AddAt(2.174,0) ;
      60           0 :   fSampleParamsHigh->AddAt(0.106,1) ;
      61           0 :   fSampleParamsHigh->AddAt(0.173,2) ;
      62           0 :   fSampleParamsHigh->AddAt(0.06106,3) ;
      63             :   //last two parameters are pedestal and overflow
      64           0 :   fSampleParamsLow=new TArrayD(7) ;
      65           0 :   fSampleParamsLow->AddAt(2.456,0) ;
      66           0 :   fSampleParamsLow->AddAt(0.137,1) ;
      67           0 :   fSampleParamsLow->AddAt(2.276,2) ;
      68           0 :   fSampleParamsLow->AddAt(0.08246,3) ;
      69           0 :   fToFit = new TList() ;
      70           0 : }
      71             : 
      72             : //-----------------------------------------------------------------------------
      73             : AliPHOSRawFitterv1::~AliPHOSRawFitterv1()
      74           0 : {
      75             :   //Destructor.
      76             :   //Destructor
      77           0 :   if(fSampleParamsLow){
      78           0 :     delete fSampleParamsLow ; 
      79           0 :     fSampleParamsLow=0 ;
      80           0 :   }
      81           0 :   if(fSampleParamsHigh){
      82           0 :     delete fSampleParamsHigh ;
      83           0 :     fSampleParamsHigh=0;
      84           0 :   }
      85           0 :   if(fToFit){
      86           0 :     delete fToFit ;
      87           0 :     fToFit=0 ;
      88           0 :   }
      89           0 : }
      90             : 
      91             : //-----------------------------------------------------------------------------
      92             : AliPHOSRawFitterv1::AliPHOSRawFitterv1(const AliPHOSRawFitterv1 &phosFitter ):
      93           0 :   AliPHOSRawFitterv0(phosFitter),
      94           0 :   fSampleParamsLow(0x0),
      95           0 :   fSampleParamsHigh(0x0),
      96           0 :   fToFit(0x0)
      97           0 : {
      98             :   //Copy constructor.
      99           0 :   fToFit = new TList() ;
     100           0 :   fSampleParamsLow =new TArrayD(*(phosFitter.fSampleParamsLow)) ;
     101           0 :   fSampleParamsHigh=new TArrayD(*(phosFitter.fSampleParamsHigh)) ;
     102           0 : }
     103             : 
     104             : //-----------------------------------------------------------------------------
     105             : AliPHOSRawFitterv1& AliPHOSRawFitterv1::operator = (const AliPHOSRawFitterv1 &phosFitter)
     106             : {
     107             :   //Assignment operator.
     108           0 :   if(this != &phosFitter) {
     109           0 :     fToFit = new TList() ;
     110           0 :     if(fSampleParamsLow){
     111           0 :       fSampleParamsLow = phosFitter.fSampleParamsLow ;
     112           0 :       fSampleParamsHigh= phosFitter.fSampleParamsHigh ;
     113           0 :     }
     114             :     else{
     115           0 :       fSampleParamsLow =new TArrayD(*(phosFitter.fSampleParamsLow)) ; 
     116           0 :       fSampleParamsHigh=new TArrayD(*(phosFitter.fSampleParamsHigh)) ;
     117             :     }
     118             :   }
     119           0 :   return *this;
     120           0 : }
     121             : 
     122             : //-----------------------------------------------------------------------------
     123             : Bool_t AliPHOSRawFitterv1::Eval(const UShort_t *signal, Int_t sigStart, Int_t sigLength)
     124             : {
     125             :   //Extract an energy deposited in the crystal,
     126             :   //crystal' position (module,column,row),
     127             :   //time and gain (high or low).
     128             :   //First collects sample, then evaluates it and if it has
     129             :   //reasonable shape, fits it with Gamma2 function and extracts 
     130             :   //energy and time.
     131             : 
     132           0 :   if (fCaloFlag == 2 || fNBunches > 1) {
     133           0 :     fQuality = 1000;
     134           0 :     return kTRUE;
     135             :   }
     136             : 
     137             :   Float_t pedMean = 0;
     138             :   Float_t pedRMS  = 0;
     139             :   Int_t   nPed    = 0;
     140             :   const Float_t kBaseLine   = 1.0;
     141             :   const Int_t   kPreSamples = 10;
     142             :   
     143           0 :   TArrayI *fSamples = new TArrayI(sigLength); // array of sample amplitudes
     144           0 :   TArrayI *fTimes   = new TArrayI(sigLength); // array of sample time stamps
     145           0 :   for (Int_t i=0; i<sigLength; i++) {
     146           0 :     if (i<kPreSamples) {
     147           0 :       nPed++;
     148           0 :       pedMean += signal[i];
     149           0 :       pedRMS  += signal[i]*signal[i] ;
     150           0 :     }
     151           0 :     fSamples->AddAt(signal[i],sigLength-i-1);
     152           0 :     fTimes  ->AddAt(i ,i);
     153             :   }
     154             : 
     155           0 :   fEnergy = -111;
     156           0 :   fQuality= 999. ;
     157             :   const Float_t sampleMaxHG=102.332 ;  //maximal height of HG sample with given parameterization
     158             :   const Float_t sampleMaxLG=277.196 ;  //maximal height of LG sample with given parameterization
     159             :   const Float_t maxEtoFit=5 ; //fit only samples above this energy, accept all samples (with good aRMS) below it
     160             :   Double_t pedestal = 0;
     161             : 
     162           0 :   if (fPedSubtract) {
     163           0 :     if (nPed > 0) {
     164           0 :       fPedestalRMS=(pedRMS - pedMean*pedMean/nPed)/nPed ;
     165           0 :       if(fPedestalRMS > 0.) 
     166           0 :         fPedestalRMS = TMath::Sqrt(fPedestalRMS) ;
     167           0 :       fEnergy -= (Double_t)(pedMean/nPed); // pedestal subtraction
     168             :     }
     169             :     else
     170           0 :       return kFALSE;
     171           0 :   }
     172             :   else {
     173             :     //take pedestals from DB
     174           0 :     pedestal = (Double_t) fAmpOffset ;
     175           0 :     if (fCalibData) {
     176           0 :       Float_t truePed       = fCalibData->GetADCpedestalEmc(fModule, fCellZ, fCellX) ;
     177           0 :       Int_t   altroSettings = fCalibData->GetAltroOffsetEmc(fModule, fCellZ, fCellX) ;
     178           0 :       pedestal += truePed - altroSettings ;
     179           0 :     }
     180             :     else{
     181           0 :       AliWarning(Form("Can not read data from OCDB")) ;
     182             :     }
     183           0 :     fEnergy-=pedestal ;
     184             :   }
     185             : 
     186           0 :   if (fEnergy < kBaseLine) fEnergy = 0;
     187             :   //Evaluate time
     188             :   Int_t iStart = 0;
     189           0 :   while(iStart<sigLength && fSamples->At(iStart)-pedestal <kBaseLine) iStart++ ;
     190           0 :   fTime = sigStart-sigLength+iStart; 
     191             :   
     192             :   //calculate time and energy
     193             :   Int_t    maxBin=0 ;
     194             :   Int_t    maxAmp=0 ;
     195             :   Double_t aMean =0. ;
     196             :   Double_t aRMS  =0. ;
     197             :   Double_t wts   =0 ;
     198             :   Int_t    tStart=0 ;
     199             : 
     200           0 :   for (Int_t i=0; i<sigLength; i++){
     201           0 :     if(signal[i] > pedestal){
     202           0 :       Double_t de = signal[i] - pedestal ;
     203           0 :       if(de > 1.) {
     204           0 :         aMean += de*i ;
     205           0 :         aRMS  += de*i*i ;
     206           0 :         wts   += de; 
     207           0 :       }
     208           0 :       if(de > 2 && tStart==0) 
     209           0 :         tStart = i ;
     210           0 :       if(maxAmp < signal[i]){
     211             :         maxBin = i ;
     212             :         maxAmp = signal[i] ;
     213           0 :       }
     214           0 :     }
     215             :   }
     216             : 
     217           0 :   if (maxBin==sigLength-1){//bad "rising" sample
     218           0 :     fEnergy =    0. ;
     219           0 :     fTime   = -999. ;
     220           0 :     fQuality=  999. ;
     221           0 :     return kTRUE ;
     222             :   }
     223             : 
     224           0 :   fEnergy=Double_t(maxAmp)-pedestal ;
     225           0 :   fOverflow =0 ;  //look for plato on the top of sample
     226           0 :   if (fEnergy>500 &&  //this is not fluctuation of soft sample
     227           0 :      maxBin<sigLength-1 && fSamples->At(maxBin+1)==maxAmp){ //and there is a plato
     228           0 :     fOverflow = kTRUE ;
     229           0 :   }
     230             :   
     231           0 :   if (wts > 0) {
     232           0 :     aMean /= wts; 
     233           0 :     aRMS   = aRMS/wts - aMean*aMean;
     234           0 :   }
     235             : 
     236             :   //do not take too small energies
     237           0 :   if (fEnergy < kBaseLine) 
     238           0 :     fEnergy = 0;
     239             :   
     240             :   //do not test quality of too soft samples
     241           0 :   if (fEnergy < maxEtoFit){
     242           0 :     fTime = tStart;
     243           0 :     if (aRMS < 2.) //sigle peak
     244           0 :       fQuality = 999. ;
     245             :     else
     246           0 :       fQuality =   0. ;
     247           0 :     return kTRUE ;
     248             :   }
     249             :       
     250             :   // if sample has reasonable mean and RMS, try to fit it with gamma2
     251             :   
     252           0 :   gMinuit->mncler();                     // Reset Minuit's list of paramters
     253           0 :   gMinuit->SetPrintLevel(-1) ;           // No Printout
     254           0 :   gMinuit->SetFCN(AliPHOSRawFitterv1::UnfoldingChiSquare) ;  
     255             : 
     256             :   // To set the address of the minimization function 
     257             :   
     258           0 :   fToFit->Clear("nodelete") ;
     259             :   Double_t b=0,bmin=0,bmax=0 ;
     260           0 :   if      (fCaloFlag == 0){ // Low gain
     261           0 :     fSampleParamsLow->AddAt(pedestal,4) ;
     262           0 :     if (fOverflow)
     263           0 :       fSampleParamsLow->AddAt(double(maxAmp),5) ;
     264             :     else
     265           0 :       fSampleParamsLow->AddAt(double(1023),5) ;
     266           0 :     fSampleParamsLow->AddAt(double(iStart),6) ;
     267           0 :     fToFit->AddFirst((TObject*)fSampleParamsLow) ; 
     268           0 :     b=fSampleParamsLow->At(2) ;
     269             :     bmin=0.5 ;
     270             :     bmax=10. ;
     271           0 :   }
     272           0 :   else if (fCaloFlag == 1){ // High gain
     273           0 :     fSampleParamsHigh->AddAt(pedestal,4) ;
     274           0 :     if (fOverflow)
     275           0 :       fSampleParamsHigh->AddAt(double(maxAmp),5) ;
     276             :     else
     277           0 :       fSampleParamsHigh->AddAt(double(1023),5);
     278           0 :     fSampleParamsHigh->AddAt(double(iStart),6);
     279           0 :     fToFit->AddFirst((TObject*)fSampleParamsHigh) ; 
     280           0 :     b=fSampleParamsHigh->At(2) ;
     281             :     bmin=0.05 ;
     282             :     bmax=0.4 ;
     283           0 :   }
     284           0 :   fToFit->AddLast((TObject*)fSamples) ;
     285           0 :   fToFit->AddLast((TObject*)fTimes) ;
     286             :   
     287           0 :   gMinuit->SetObjectFit((TObject*)fToFit) ;         // To tranfer pointer to UnfoldingChiSquare
     288           0 :   Int_t ierflg ;
     289           0 :   gMinuit->mnparm(0, "t0",  1.*tStart, 0.01, -500., 500., ierflg) ;
     290           0 :   if(ierflg != 0){
     291             :     //    AliWarning(Form("Unable to set initial value for fit procedure : t0=%e\n",1.*tStart) ) ;
     292           0 :    fEnergy =   0. ;
     293           0 :     fTime   =-999. ;
     294           0 :     fQuality= 999. ;
     295           0 :     return kTRUE ; //will scan further
     296             :   }
     297             :   Double_t amp0=0; 
     298           0 :   if      (fCaloFlag == 0) // Low gain
     299           0 :     amp0 = fEnergy/sampleMaxLG;
     300           0 :   else if (fCaloFlag == 1) // High gain
     301           0 :     amp0 = fEnergy/sampleMaxHG;
     302             :   
     303           0 :   gMinuit->mnparm(1, "Energy", amp0 , 0.01*amp0, 0, 0, ierflg) ;
     304           0 :   if(ierflg != 0){
     305             :     //    AliWarning(Form("Unable to set initial value for fit procedure : E=%e\n", amp0)) ;
     306           0 :     fEnergy =   0. ;
     307           0 :     fTime   =-999. ;
     308           0 :     fQuality= 999. ;
     309           0 :     return kTRUE ; //will scan further
     310             :   }
     311             :   
     312           0 :   gMinuit->mnparm(2, "p2", b, 0.01*b, bmin, bmax, ierflg) ;
     313           0 :   if(ierflg != 0){                                         
     314             :     //        AliWarning(Form("Unable to set initial value for fit procedure : E=%e\n", amp0)) ;  
     315           0 :     fEnergy =   0. ;
     316           0 :     fTime   =-999. ;
     317           0 :     fQuality= 999. ;
     318           0 :     return kTRUE ; //will scan further  
     319             :   }             
     320             :   
     321           0 :   Double_t p0 = 0.0001 ; // "Tolerance" Evaluation stops when EDM = 0.0001*p0 ; The number of function call slightly
     322             :   //  depends on it. 
     323           0 :   Double_t p1 = 1.0 ;
     324           0 :   Double_t p2 = 0.0 ;
     325           0 :   gMinuit->mnexcm("SET STR", &p2, 0, ierflg) ;   // force TMinuit to reduce function calls  
     326           0 :   gMinuit->mnexcm("SET GRA", &p1, 1, ierflg) ;   // force TMinuit to use my gradient  
     327             :   //    gMinuit->SetMaxIterations(100);
     328           0 :   gMinuit->mnexcm("SET NOW", &p2 , 0, ierflg) ;  // No Warnings
     329             :   
     330           0 :   gMinuit->mnexcm("MIGRAD", &p0, 0, ierflg) ;    // minimize 
     331             :   
     332           0 :   Double_t err=0.,t0err=0. ;
     333           0 :   Double_t t0=0.,efit=0. ;
     334           0 :   gMinuit->GetParameter(0,t0, t0err) ;
     335           0 :   gMinuit->GetParameter(1,efit, err) ;
     336             :   
     337           0 :   Double_t bfit=0., berr=0. ;
     338           0 :   gMinuit->GetParameter(2,bfit,berr) ;
     339             :   
     340             :   //Calculate total energy
     341             :   //this is parameterization of dependence of pulse height on parameter b
     342           0 :   if(fCaloFlag == 0) // Low gain
     343           0 :     efit *= 99.54910 + 78.65038*bfit ;
     344           0 :   else if(fCaloFlag == 1) // High gain
     345           0 :     efit *= 80.33109 + 128.6433*bfit ;
     346             :   
     347           0 :   if(efit < 0. || efit > 10000.){
     348             :     //set energy to previously found max
     349           0 :     fTime   =-999.;
     350           0 :     fQuality= 999 ;
     351           0 :     return kTRUE;
     352             :   }                                                                             
     353             :   
     354             :   //evaluate fit quality
     355           0 :   Double_t fmin=0.,fedm=0.,errdef=0. ;
     356           0 :   Int_t npari,nparx,istat;
     357           0 :   gMinuit->mnstat(fmin,fedm,errdef,npari,nparx,istat) ;
     358           0 :   fQuality = fmin/sigLength ;
     359             :   //compare quality with some parameterization
     360           0 :   if      (fCaloFlag == 0) // Low gain
     361           0 :     fQuality /= 2.00 + 0.0020*fEnergy ;
     362           0 :   else if (fCaloFlag == 1) // High gain
     363           0 :     fQuality /= 0.75 + 0.0025*fEnergy ;
     364             :   
     365           0 :   fEnergy = efit ;
     366           0 :   fTime  += t0 - 4.024*bfit ; //-10.402*bfit+4.669*bfit*bfit ; //Correction for 70 samples
     367             : //  fTime  += sigStart;
     368             :   
     369           0 :   delete fSamples ;
     370           0 :   delete fTimes ;
     371             :   return kTRUE;
     372           0 : }
     373             : //-----------------------------------------------------------------------------
     374             : Double_t AliPHOSRawFitterv1::Gamma2(Double_t dt,Double_t en,Double_t b,TArrayD * params){  //Function for fitting samples
     375             :   //parameters:
     376             :   //dt-time after start
     377             :   //en-amplutude
     378             :   //function parameters
     379             :   
     380           0 :   Double_t ped=params->At(4) ;
     381           0 :   if(dt<0.)
     382           0 :     return ped ; //pedestal
     383             :   else
     384           0 :     return ped+en*(TMath::Power(dt,params->At(0))*TMath::Exp(-dt*params->At(1))+b*dt*dt*TMath::Exp(-dt*params->At(3))) ;
     385           0 : }
     386             : //_____________________________________________________________________________
     387             : void AliPHOSRawFitterv1::UnfoldingChiSquare(Int_t & /*nPar*/, Double_t * Grad, Double_t & fret, Double_t * x, Int_t iflag)
     388             : {
     389             :   // Number of parameters, Gradient, Chi squared, parameters, what to do
     390             : 
     391           0 :   TList * toFit= (TList*)gMinuit->GetObjectFit() ;
     392           0 :   TArrayD * params=(TArrayD*)toFit->At(0) ; 
     393           0 :   TArrayI * samples = (TArrayI*)toFit->At(1) ;
     394           0 :   TArrayI * times = (TArrayI*)toFit->At(2) ;
     395             : 
     396           0 :   fret = 0. ;     
     397           0 :   if(iflag == 2)
     398           0 :     for(Int_t iparam = 0 ; iparam < 3 ; iparam++)    
     399           0 :       Grad[iparam] = 0 ; // Will evaluate gradient
     400             :   
     401           0 :   Double_t t0=x[0] ;
     402           0 :   Double_t en=x[1] ;
     403           0 :   Double_t b=x[2] ;
     404           0 :   Double_t n=params->At(0) ;
     405           0 :   Double_t alpha=params->At(1) ;
     406           0 :   Double_t beta=params->At(3) ;
     407             :   //  Double_t ped=params->At(4) ;
     408             : 
     409           0 :   Double_t overflow=params->At(5) ;
     410           0 :   Int_t iBin = (Int_t) params->At(6) ;
     411           0 :   Int_t nSamples=TMath::Min(iBin+70,samples->GetSize()) ; //Here we set number of points to fit (70)
     412             :   // iBin - first non-zero sample 
     413           0 :   Int_t tStep=times->At(iBin+1)-times->At(iBin) ;
     414           0 :   Double_t ddt=times->At(iBin)-t0-tStep ;
     415           0 :   Double_t exp1=TMath::Exp(-alpha*ddt) ;
     416           0 :   Double_t exp2=TMath::Exp(-beta*ddt) ;
     417           0 :   Double_t dexp1=TMath::Exp(-alpha*tStep) ;
     418           0 :   Double_t dexp2=TMath::Exp(-beta*tStep) ;
     419           0 :   for(Int_t i = iBin; i<nSamples ; i++) {
     420           0 :     Double_t dt=double(times->At(i))-t0 ;
     421           0 :     Double_t fsample = double(samples->At(i)) ;
     422             :     Double_t diff=0. ;
     423           0 :     exp1*=dexp1 ;
     424           0 :     exp2*=dexp2 ;
     425             : //    if(fsample>=overflow)
     426             : //      continue ;    
     427           0 :     if(dt<=0.){
     428             :       diff=fsample ; 
     429           0 :       fret += diff*diff ;
     430           0 :       continue ;
     431             :     }
     432             :     
     433           0 :     Double_t dtn=TMath::Power(dt,n) ;
     434           0 :     Double_t dtnE=dtn*exp1 ;
     435           0 :     Double_t dt2E=dt*dt*exp2 ;
     436           0 :     Double_t fit=en*(dtnE + b*dt2E) ;
     437           0 :     if(fsample>=overflow && fit>=overflow)
     438           0 :       continue ;    
     439             : 
     440           0 :     diff = fsample - fit ;
     441           0 :     fret += diff*diff ;
     442           0 :     if(iflag == 2){  // calculate gradient
     443           0 :       Grad[0] += en*diff*(dtnE*(n/dt-alpha)+b*dt2E*(2./dt-beta))  ; //derivative over t0
     444           0 :       Grad[1] -= diff*(dtnE+b*dt2E) ;
     445           0 :       Grad[2] -= en*diff*dt2E ;
     446           0 :     }
     447             :     
     448           0 :   }
     449           0 :   if(iflag == 2)
     450           0 :     for(Int_t iparam = 0 ; iparam < 3 ; iparam++)    
     451           0 :       Grad[iparam] *= 2. ; 
     452           0 : }

Generated by: LCOV version 1.11