]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PHOS/AliPHOSRawDecoderv1.cxx
Bad channels map and HG/LG are calculated in STANDALONE run only;
[u/mrichter/AliRoot.git] / PHOS / AliPHOSRawDecoderv1.cxx
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 decodes the stream of ALTRO samples to extract
19 // the PHOS "digits" of current event. Uses fitting procedure
20 // to separate reasonable samples
21 // 
22 // Typical use case:
23 //     AliRawReader* rf = new AliRawReaderDate("2006run2211.raw");
24 //     AliPHOSRawDecoder dc(rf);
25 //     while (rf->NextEvent()) {
26 //       dc.SubtractPedestals(kTRUE);
27 //       while ( dc.NextDigit() ) {
28 //         Int_t module = dc.GetModule();
29 //         Int_t column = dc.GetColumn();
30 //         Int_t row = dc.GetRow();
31 //         Double_t amplitude = dc.GetEnergy();
32 //         Double_t time = dc.GetTime();
33 //         Bool_t IsLowGain = dc.IsLowGain();
34 //            ..........
35 //       }
36 //     }
37
38 // Author: Dmitri Peressounko
39
40 // --- ROOT system ---
41 #include "TList.h"
42 #include "TMath.h"
43 #include "TMinuit.h"
44
45 #include "TCanvas.h"
46 #include "TH1.h"
47 #include "TH2.h"
48 #include "TF1.h"
49 #include "TROOT.h"
50
51 // --- AliRoot header files ---
52 //#include "AliLog.h"
53 #include "AliPHOSRawDecoderv1.h"
54 #include "AliPHOSPulseGenerator.h"
55
56
57 ClassImp(AliPHOSRawDecoderv1)
58
59 //-----------------------------------------------------------------------------
60   AliPHOSRawDecoderv1::AliPHOSRawDecoderv1():AliPHOSRawDecoder(),
61 fSampleParamsLow(0x0),fSampleParamsHigh(0x0),fToFit(0x0)
62 {
63   //Default constructor.
64 }
65
66 //-----------------------------------------------------------------------------
67 AliPHOSRawDecoderv1::AliPHOSRawDecoderv1(AliRawReader* rawReader,  AliAltroMapping **mapping):
68   AliPHOSRawDecoder(rawReader,mapping),
69   fSampleParamsLow(0x0),fSampleParamsHigh(0x0),fToFit(0x0)
70 {
71   //Construct a decoder object.
72   //Is is user responsibility to provide next raw event 
73   //using AliRawReader::NextEvent().
74
75   if(!gMinuit) 
76     gMinuit = new TMinuit(100);
77   fSampleParamsHigh =new TArrayD(7) ;
78   fSampleParamsHigh->AddAt(2.174,0) ;
79   fSampleParamsHigh->AddAt(0.106,1) ;
80   fSampleParamsHigh->AddAt(0.173,2) ;
81   fSampleParamsHigh->AddAt(0.06106,3) ;
82   //last two parameters are pedestal and overflow
83   fSampleParamsLow=new TArrayD(7) ;
84   fSampleParamsLow->AddAt(2.456,0) ;
85   fSampleParamsLow->AddAt(0.137,1) ;
86   fSampleParamsLow->AddAt(2.276,2) ;
87   fSampleParamsLow->AddAt(0.08246,3) ;
88   fToFit = new TList() ;
89 }
90
91 //-----------------------------------------------------------------------------
92 AliPHOSRawDecoderv1::~AliPHOSRawDecoderv1()
93 {
94   //Destructor.
95   if(fSampleParamsLow){
96     delete fSampleParamsLow ; 
97     fSampleParamsLow=0 ;
98   }
99   if(fSampleParamsHigh){
100     delete fSampleParamsHigh ;
101     fSampleParamsHigh=0;
102   }
103   if(fToFit){
104     delete fToFit ;
105     fToFit=0 ;
106   }
107 }
108
109 //-----------------------------------------------------------------------------
110 AliPHOSRawDecoderv1::AliPHOSRawDecoderv1(const AliPHOSRawDecoderv1 &phosDecoder ):
111   AliPHOSRawDecoder(phosDecoder), 
112   fSampleParamsLow(0x0),fSampleParamsHigh(0x0),fToFit(0x0)
113 {
114   //Copy constructor.
115   fToFit = new TList() ;
116   fSampleParamsLow =new TArrayD(*(phosDecoder.fSampleParamsLow)) ;
117   fSampleParamsHigh=new TArrayD(*(phosDecoder.fSampleParamsHigh)) ;
118 }
119
120 //-----------------------------------------------------------------------------
121 AliPHOSRawDecoderv1& AliPHOSRawDecoderv1::operator = (const AliPHOSRawDecoderv1 &phosDecoder)
122 {
123   //Assignment operator.
124
125   //  if(this != &phosDecoder) {
126   //  }
127   fToFit = new TList() ;
128   if(fSampleParamsLow){
129     fSampleParamsLow = phosDecoder.fSampleParamsLow ;
130     fSampleParamsHigh= phosDecoder.fSampleParamsHigh ;
131   }
132   else{
133     fSampleParamsLow =new TArrayD(*(phosDecoder.fSampleParamsLow)) ; 
134     fSampleParamsHigh=new TArrayD(*(phosDecoder.fSampleParamsHigh)) ;
135   }
136   return *this;
137 }
138
139 //-----------------------------------------------------------------------------
140 Bool_t AliPHOSRawDecoderv1::NextDigit()
141 {
142   //Extract an energy deposited in the crystal,
143   //crystal' position (module,column,row),
144   //time and gain (high or low).
145   //First collects sample, then evaluates it and if it has
146   //reasonable shape, fits it with Gamma2 function and extracts 
147   //energy and time.
148
149 //Debug=====================
150 //  TCanvas * c = 0; //(TCanvas*)gROOT->FindObjectAny("CSample") ;
151 //  if(!c)
152 //    c = new TCanvas("CSample","CSample") ;
153 // 
154 //  TH1D * h = 0 ; //(TH1D*)gROOT->FindObjectAny("hSample") ;
155 //  if(!h)
156 //    h=new TH1D("hSample","",200,0.,200.) ;
157 // 
158 //  TF1 * fff = 0 ; //(TF1*)gROOT->FindObjectAny("fff") ;
159 //  if(!fff)
160 //    fff = new TF1("fff","[0]+[1]*((abs(x-[2]))^[3]*exp(-(x-[2])*[4])+[5]*(x-[2])*(x-[2])*exp(-(x-[2])*[6]))",0.,1000.) ;
161 //End debug===========
162   
163   AliCaloRawStream* in = fCaloStream;
164   
165   Int_t    iBin     = fSamples->GetSize() ;
166   Int_t    tLength  = 0;
167   fEnergy = -111;
168   Float_t pedMean = 0;
169   Int_t   nPed = 0;
170   Float_t baseLine = 1.0;
171   const Float_t nPreSamples = 10;
172   fQuality= 999. ;
173   const Float_t sampleMaxHG=102.332 ;  //maximal height of HG sample with given parameterization
174   const Float_t sampleMaxLG=277.196 ;  //maximal height of HG sample with given parameterization
175   const Float_t maxEtoFit=5 ; //fit only samples above this energy, accept all samples (with good aRMS) below it
176
177   while ( in->Next() ) { 
178
179     if(!tLength) {
180       tLength = in->GetTimeLength();
181       if(tLength!=fSamples->GetSize()) {
182         delete fSamples ;
183         delete fTimes ;
184         fSamples = new TArrayI(tLength);
185         fTimes = new TArrayI(tLength);
186         iBin= fSamples->GetSize() ;
187       }
188       else{
189         fSamples->Reset() ;
190       }
191     }
192     
193     // Fit the full sample
194     if((in->IsNewHWAddress() && iBin != fSamples->GetSize()) //new HW address
195        ||(iBin<=0)) {  //or new signal in same address
196
197       //First remember new sample
198       fNewLowGainFlag = in->IsLowGain();                                                                                                        
199       fNewModule = in->GetModule()+1;                                                                                                           
200       fNewRow    = in->GetRow()   +1;                                                                                                           
201       fNewColumn = in->GetColumn()+1;                                                                                                           
202       fNewAmp = in->GetSignal() ;
203       fNewTime=in->GetTime() ;  
204   
205       //new handle already collected 
206       Double_t pedestal =0. ;
207       if(fPedSubtract){ 
208         if (nPed > 0)
209           pedestal = (Double_t)(pedMean/nPed); 
210         else
211           return kFALSE;
212       }
213
214       //calculate time and energy
215       Int_t maxBin=0 ;
216       Int_t maxAmp=0 ;
217       Double_t aMean=0. ;                                                                                                                  
218       Double_t aRMS=0. ;                                                                                                                   
219       Double_t wts=0 ;                                                                                                                       
220       Int_t tStart = 0 ;                                                                                                                   
221       for(Int_t i=iBin; i<fSamples->GetSize(); i++){
222         if(fSamples->At(i)>0){                                                                                                             
223           Double_t de=fSamples->At(i)-pedestal ;                                                                                           
224           if(de>1.){
225             aMean+=de*i ;                                                                                                                      
226             aRMS+=de*i*i ;                                                                                                                    
227             wts+=de; 
228           }                                                                                                                         
229           if(de>2 && tStart==0) 
230             tStart=i ;                                                                                                                     
231           if(maxAmp<fSamples->At(i)){
232             maxBin=i ;
233             maxAmp=fSamples->At(i) ;
234           }
235         }
236       }
237       if(maxBin==fSamples->GetSize()-1){//bad "rising" sample
238         fEnergy=0. ;
239         fTime=-999.;
240         fQuality= 999. ;
241         return kTRUE ;
242       }
243       fEnergy=Double_t(maxAmp)-pedestal ;
244       fOverflow =0 ;  //look for plato on the top of sample
245       if(fEnergy>500 &&  //this is not fluctuation of soft sample
246          maxBin<fSamples->GetSize()-1 && fSamples->At(maxBin+1)==maxAmp){ //and there is a plato
247          fOverflow = kTRUE ;
248       }
249       
250       if(wts>0){
251         aMean/=wts; 
252         aRMS=aRMS/wts-aMean*aMean;
253       }
254
255       //do not take too small energies
256       if(fEnergy < baseLine) 
257          fEnergy = 0;
258
259       //do not test quality of too soft samples
260       if(fEnergy<maxEtoFit){
261         fTime=fTimes->At(tStart);
262         if(aRMS<2.) //sigle peak
263           fQuality=999. ;
264         else
265           fQuality= 0. ;                                                                                                                   
266         return kTRUE ;                                                                                                                     
267       } 
268
269       
270 //Debug:=====Draw sample
271 //if(fEnergy>pedestal+10.){
272 //if(fLowGainFlag && fEnergy>2){
273 //  if(!c)
274 //    if(!fLowGainFlag && fRow==32 && fColumn==18){
275 //    TCanvas *c = new TCanvas("CSample","CSample") ;
276 //    c->cd() ;
277 //    h->Draw() ;
278 //    c->Update() ;
279 // printf("fEnergy=%f, aRMS=%f \n",fEnergy,aRMS) ;   
280 //getchar() ;
281 //}
282 //======================
283
284       //IF sample has reasonable mean and RMS, try to fit it with gamma2
285         
286         gMinuit->mncler();                     // Reset Minuit's list of paramters
287         gMinuit->SetPrintLevel(-1) ;           // No Printout
288         gMinuit->SetFCN(AliPHOSRawDecoderv1::UnfoldingChiSquare) ;  
289         // To set the address of the minimization function 
290         
291        fToFit->Clear() ;
292        Double_t b,bmin,bmax ;
293        if(fLowGainFlag){
294          fSampleParamsLow->AddAt(pedestal,4) ;
295          if(fOverflow)
296            fSampleParamsLow->AddAt(double(maxAmp),5) ;
297          else
298            fSampleParamsLow->AddAt(double(1023),5) ;
299          fSampleParamsLow->AddAt(double(iBin),6) ;
300          fToFit->AddFirst((TObject*)fSampleParamsLow) ; 
301          b=fSampleParamsLow->At(2) ;
302          bmin=0.5 ;
303          bmax=10. ;
304        }
305        else{
306          fSampleParamsHigh->AddAt(pedestal,4) ;
307          if(fOverflow)
308            fSampleParamsHigh->AddAt(double(maxAmp),5) ;
309          else
310            fSampleParamsHigh->AddAt(double(1023),5);
311          fSampleParamsHigh->AddAt(double(iBin),6);
312          fToFit->AddFirst((TObject*)fSampleParamsHigh) ; 
313          b=fSampleParamsHigh->At(2) ;
314          bmin=0.05 ;
315          bmax=0.4 ;
316         }
317         fToFit->AddLast((TObject*)fSamples) ;
318         fToFit->AddLast((TObject*)fTimes) ;
319
320         gMinuit->SetObjectFit((TObject*)fToFit) ;         // To tranfer pointer to UnfoldingChiSquare
321         Int_t ierflg ;
322         gMinuit->mnparm(0, "t0",  1.*tStart, 0.01, -500., 500., ierflg) ;
323         if(ierflg != 0){
324 //        AliWarning(Form("Unable to set initial value for fit procedure : t0=%e\n",1.*tStart) ) ;
325           fEnergy=0. ;
326           fTime=-999. ;
327           fQuality=999 ;
328           return kTRUE ; //will scan further
329         }
330         Double_t amp0; 
331         if(fLowGainFlag)
332           amp0=fEnergy/sampleMaxLG;
333         else
334           amp0=fEnergy/sampleMaxHG;
335
336         gMinuit->mnparm(1, "Energy", amp0 , 0.01*amp0, 0, 0, ierflg) ;
337         if(ierflg != 0){
338 //        AliWarning(Form("Unable to set initial value for fit procedure : E=%e\n", amp0)) ;
339           fEnergy=0. ;
340           fTime=-999. ;
341           fQuality=999 ;
342           return kTRUE ; //will scan further
343         }
344
345         gMinuit->mnparm(2, "p2", b, 0.01*b, bmin, bmax, ierflg) ;
346         if(ierflg != 0){                                         
347 //        AliWarning(Form("Unable to set initial value for fit procedure : E=%e\n", amp0)) ;  
348           fEnergy=0. ;           
349           fTime=-999. ;         
350           fQuality=999 ;       
351           return kTRUE ; //will scan further  
352         }             
353  
354         
355         Double_t p0 = 0.0001 ; // "Tolerance" Evaluation stops when EDM = 0.0001*p0 ; The number of function call slightly
356         //  depends on it. 
357         Double_t p1 = 1.0 ;
358         Double_t p2 = 0.0 ;
359         gMinuit->mnexcm("SET STR", &p2, 0, ierflg) ;   // force TMinuit to reduce function calls  
360         gMinuit->mnexcm("SET GRA", &p1, 1, ierflg) ;   // force TMinuit to use my gradient  
361         //      gMinuit->SetMaxIterations(100);
362         gMinuit->mnexcm("SET NOW", &p2 , 0, ierflg) ;  // No Warnings
363         
364         gMinuit->mnexcm("MIGRAD", &p0, 0, ierflg) ;    // minimize 
365         
366         Double_t err,t0err ;
367         Double_t t0,efit ;
368         gMinuit->GetParameter(0,t0, t0err) ;    
369         gMinuit->GetParameter(1,efit, err) ;    
370
371         Double_t bfit, berr ;
372         gMinuit->GetParameter(2,bfit,berr) ;
373
374         //Calculate total energy
375         //this isparameterization of depetendence of pulse height on parameter b
376         if(fLowGainFlag)
377           efit*=99.54910 + 78.65038*bfit ;
378         else
379           efit*=80.33109+128.6433*bfit ;
380
381         if(efit<0. || efit > 10000.){                                                                          
382 //set energy to previously found max
383 //          fEnergy=0 ; //bad sample                                                    
384           fTime=-999.;                                                                
385           fQuality=999 ;                                                              
386           return kTRUE;
387         }                                                                             
388  
389         //evaluate fit quality
390         Double_t fmin,fedm,errdef ;
391         Int_t npari,nparx,istat;
392         gMinuit->mnstat(fmin,fedm,errdef,npari,nparx,istat) ;
393         fQuality=fmin/(fSamples->GetSize()-iBin) ;
394         //compare quality with some parameterization
395         if(fLowGainFlag){
396           fQuality/=2.+0.002*fEnergy ;
397         }
398         else{
399           fQuality/=0.75+0.0025*fEnergy ;
400         }
401
402 //Debug================
403 //        Double_t n,alpha,beta ;
404 //        Double_t en ;
405 //       if(fLowGainFlag){
406 //          n=fSampleParamsLow->At(0) ;
407 //          alpha=fSampleParamsLow->At(1) ;
408 //          beta=fSampleParamsLow->At(3) ;
409 //          en=efit/(99.54910 + 78.65038*bfit) ;
410 //        }
411 //        else{
412 //          n=fSampleParamsHigh->At(0) ;
413 //          alpha=fSampleParamsHigh->At(1) ;
414 //          beta=fSampleParamsHigh->At(3) ;
415 //          en=efit/(80.33109+128.6433*bfit) ;
416 //        }
417 //
418 ////    if( fQuality > 1 && fEnergy > 20. && !fOverflow){
419 ////    if(!fLowGainFlag && fRow==32 && fColumn==18){
420 //{
421 //    printf("Col=%d, row=%d, qual=%f, E=%f, t0=%f, b=%f\n",fColumn,fRow,fQuality,efit,t0,bfit) ;
422 //    printf("    Energy = %f \n",fEnergy) ;
423 //    TCanvas * c = new TCanvas("samp") ;
424 //    c->cd() ;
425 //    h->Draw() ;
426 //    if(fLowGainFlag){
427 //      fff->SetParameters(pedestal,en,t0,n,alpha,bfit,beta) ;
428 //    }
429 //    else{
430 //     fff->SetParameters(pedestal,en,t0,n,alpha,bfit,beta) ;
431 //    }
432 //////    for(Int_t i=1;i<=h->GetNbinsX(); i++){
433 ////       Double_t x=h->GetBinCenter(i) ;
434 ////       h->SetBinContent(i,h->GetBinContent(i)-fff->Eval(x)) ;
435 ////    }
436 ////    h->SetMinimum(-15.) ;
437 ////    h->SetMaximum(15.) ;
438 //    h->Draw() ;
439 //    fff->Draw("same") ;
440 //    c->Update();
441 //    getchar() ;
442 //    }
443 //====================
444
445       fEnergy=efit ;
446       fTime=t0-4.024*bfit ; //-10.402*bfit+4.669*bfit*bfit ; //Correction for 70 samples
447 //      fTime=t0+2.8*bfit ; //-10.402*bfit+4.669*bfit*bfit ; //Correction for 50 samples
448 //      fQuality = bfit ;
449       
450       return kTRUE;
451     }
452     
453     fLowGainFlag = in->IsLowGain();
454     fModule = in->GetModule()+1;
455     fRow    = in->GetRow()   +1;
456     fColumn = in->GetColumn()+1;
457
458     //add previouly taken if coincides
459     if(fLowGainFlag==fNewLowGainFlag && fModule==fNewModule &&
460        fRow==fNewRow && fColumn==fNewColumn){
461        iBin--;                                                                                                                                
462        if(fPedSubtract && fNewTime < nPreSamples) {                                                                                    
463          pedMean += in->GetSignal();                                                                                                          
464          nPed++;                                                                                                                              
465        }                                                                                                                                      
466        fSamples->AddAt(fNewAmp,iBin);                                                                                                 
467        fTimes->AddAt(fNewTime,iBin);                                                                                                     
468     
469        //Mark that we already take it
470        fNewModule=-1 ;
471     }
472     
473     // Fill array with samples
474     iBin--;
475     if(fPedSubtract && (in->GetTime() < nPreSamples)) {
476       pedMean += in->GetSignal();
477       nPed++;
478     }
479     fSamples->AddAt(in->GetSignal(),iBin);
480     fTimes->AddAt(in->GetTime(),iBin);
481  
482 //Debug==============
483 //    h->SetBinContent(in->GetTime(),in->GetSignal()) ;
484 //EndDebug==============
485     
486   } // in.Next()
487   
488   return kFALSE;
489 }
490 //_____________________________________________________________________________
491 void AliPHOSRawDecoderv1::UnfoldingChiSquare(Int_t & /*nPar*/, Double_t * Grad, Double_t & fret, Double_t * x, Int_t iflag)
492 {
493   // Number of parameters, Gradient, Chi squared, parameters, what to do
494
495   TList * toFit= (TList*)gMinuit->GetObjectFit() ;
496   TArrayD * params=(TArrayD*)toFit->At(0) ; 
497   TArrayI * samples = (TArrayI*)toFit->At(1) ;
498   TArrayI * times = (TArrayI*)toFit->At(2) ;
499
500   fret = 0. ;     
501   if(iflag == 2)
502     for(Int_t iparam = 0 ; iparam < 3 ; iparam++)    
503       Grad[iparam] = 0 ; // Will evaluate gradient
504   
505   Double_t t0=x[0] ;
506   Double_t en=x[1] ;
507   Double_t b=x[2] ;
508   Double_t n=params->At(0) ;
509   Double_t alpha=params->At(1) ;
510   Double_t beta=params->At(3) ;
511   Double_t ped=params->At(4) ;
512
513   Double_t overflow=params->At(5) ;
514   Int_t iBin = (Int_t) params->At(6) ;
515   Int_t nSamples=TMath::Min(iBin+70,samples->GetSize()) ; //Here we set number of points to fit (70)
516   // iBin - first non-zero sample 
517   Int_t tStep=times->At(iBin+1)-times->At(iBin) ;
518   Double_t ddt=times->At(iBin)-t0-tStep ;
519   Double_t exp1=TMath::Exp(-alpha*ddt) ;
520   Double_t exp2=TMath::Exp(-beta*ddt) ;
521   Double_t dexp1=TMath::Exp(-alpha*tStep) ;
522   Double_t dexp2=TMath::Exp(-beta*tStep) ;
523   for(Int_t i = iBin; i<nSamples ; i++) {
524     Double_t dt=double(times->At(i))-t0 ;
525     Double_t fsample = double(samples->At(i)) ;
526     if(fsample>=overflow)
527       continue ;
528     Double_t diff ;
529     exp1*=dexp1 ;
530     exp2*=dexp2 ;
531     if(dt<=0.){
532       diff=fsample - ped ; 
533       fret += diff*diff ;
534       continue ;
535     }
536     Double_t dtn=TMath::Power(dt,n) ;
537     Double_t dtnE=dtn*exp1 ;
538     Double_t dt2E=dt*dt*exp2 ;
539     Double_t fit=ped+en*(dtnE + b*dt2E) ;
540 //    if(fit>=overflow){
541 //      diff=fsample-overflow ;
542 //      fret += diff*diff ;
543 //      //zero gradient here
544 //    }
545 //    else{
546       diff = fsample - fit ;
547       fret += diff*diff ;
548       if(iflag == 2){  // calculate gradient
549         Grad[0] += en*diff*(dtnE*(n/dt-alpha)+b*dt2E*(2./dt-beta))  ; //derivative over t0
550         Grad[1] -= diff*(dtnE+b*dt2E) ;
551         Grad[2] -= en*diff*dt2E ;
552       }
553 //    }
554   }
555   if(iflag == 2)
556     for(Int_t iparam = 0 ; iparam < 3 ; iparam++)    
557       Grad[iparam] *= 2. ; 
558 }
559 //-----------------------------------------------------------------------------
560 Double_t AliPHOSRawDecoderv1::Gamma2(Double_t dt,Double_t en,Double_t b,TArrayD * params){  //Function for fitting samples
561   //parameters:
562   //dt-time after start
563   //en-amplutude
564   //function parameters
565   
566   Double_t ped=params->At(4) ;
567   if(dt<0.)
568     return ped ; //pedestal
569   else
570     return ped+en*(TMath::Power(dt,params->At(0))*TMath::Exp(-dt*params->At(1))+b*dt*dt*TMath::Exp(-dt*params->At(3))) ;
571 }
572