]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HMPID/AliHMPIDCluster.cxx
Cluster fitting improved
[u/mrichter/AliRoot.git] / HMPID / AliHMPIDCluster.cxx
1 //  **************************************************************************
2 //  * Copyright(c) 1998-1999, 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 #include "AliHMPIDCluster.h"  //class header
17 #include <TMinuit.h>         //Solve()
18 #include <TClonesArray.h>    //Solve()
19 #include <TMarker.h>         //Draw()
20 ClassImp(AliHMPIDCluster)
21 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
22 void AliHMPIDCluster::CoG()
23 {
24 // Calculates naive cluster position as a center of gravity of its digits.
25 // Arguments: none 
26 //   Returns: none
27   Int_t minPadX=999,minPadY=999,maxPadX=-1,maxPadY=-1;      //for box finding  
28   if(fDigs==0) return;                                      //no digits in this cluster
29   fX=fY=fQRaw=0;                                            //init summable parameters
30   Int_t maxQpad=-1,maxQ=-1;                                 //to calculate the pad with the highest charge
31   AliHMPIDDigit *pDig;
32   for(Int_t iDig=0;iDig<fDigs->GetEntriesFast();iDig++){    //digits loop
33     pDig=(AliHMPIDDigit*)fDigs->At(iDig);                   //get pointer to next digit
34
35     if(pDig->PadPcX() > maxPadX) maxPadX = pDig->PadPcX();  // find the minimum box that contain the cluster  MaxX                            
36     if(pDig->PadPcY() > maxPadY) maxPadY = pDig->PadPcY();  //                                                MaxY
37     if(pDig->PadPcX() < minPadX) minPadX = pDig->PadPcX();  //                                                MinX   
38     if(pDig->PadPcY() < minPadY) minPadY = pDig->PadPcY();  //                                                MinY   
39     
40     Float_t q=pDig->Q();                                    //get QDC 
41     fX += pDig->LorsX()*q;fY +=pDig->LorsY()*q;             //add digit center weighted by QDC
42     fQRaw+=q;                                               //increment total charge 
43     if(q>maxQ) {maxQpad = pDig->Pad();maxQ=(Int_t)q;}       // to find pad with highest charge
44   }//digits loop
45   
46   fBox=(maxPadX-minPadX+1)*100+maxPadY-minPadY+1;           // dimension of the box: format Xdim*100+Ydim
47   
48   if ( fQRaw != 0 )   fX/=fQRaw;fY/=fQRaw;                  //final center of gravity
49   
50  
51   if(fDigs->GetEntriesFast()>1)CorrSin();                   //correct it by sinoid   
52   
53   fQ  = fQRaw;                                              // Before starting fit procedure, Q and QRaw must be equal
54   fCh=pDig->Ch();                                           //initialize chamber number
55   fMaxQpad = maxQpad; fMaxQ=maxQ;                           //store max charge pad to the field
56   fChi2=0;                                                  // no Chi2 to find
57   fNlocMax=0;                                               // proper status from this method
58   fSt=kCoG;
59 }//CoG()
60 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
61 void AliHMPIDCluster::CorrSin() 
62 {
63 // Correction of cluster x position due to sinoid, see HMPID TDR  page 30
64 // Arguments: none
65 //   Returns: none
66   Int_t pc,px,py;
67   AliHMPIDDigit::Lors2Pad(fX,fY,pc,px,py);             //tmp digit to get it center
68   Float_t x=fX-AliHMPIDDigit::LorsX(pc,px);                    //diff between cluster x and center of the pad contaning this cluster   
69   fX+=3.31267e-2*TMath::Sin(2*TMath::Pi()/0.8*x)-2.66575e-3*TMath::Sin(4*TMath::Pi()/0.8*x)+2.80553e-3*TMath::Sin(6*TMath::Pi()/0.8*x)+0.0070;
70 }
71 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
72 void AliHMPIDCluster::Draw(Option_t*)
73 {
74   TMarker *pMark=new TMarker(X(),Y(),5); pMark->SetUniqueID(fSt);pMark->SetMarkerColor(kBlue); pMark->Draw();
75 }
76 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
77 void AliHMPIDCluster::FitFunc(Int_t &iNpars, Double_t *, Double_t &chi2, Double_t *par, Int_t )
78 {
79 // Cluster fit function 
80 // par[0]=x par[1]=y par[2]=q for the first Mathieson shape
81 // par[3]=x par[4]=y par[5]=q for the second Mathieson shape and so on up to iNpars/3 Mathieson shapes
82 // For each pad of the cluster calculates the difference between actual pad charge and the charge induced to this pad by all Mathieson distributions 
83 // Then the chi2 is calculated as the sum of this value squared for all pad in the cluster.  
84 // Arguments: iNpars - number of parameters which is number of local maxima of cluster * 3
85 //            chi2   - function result to be minimised 
86 //            par   - parameters array of size iNpars            
87 //   Returns: none  
88   AliHMPIDCluster *pClu=(AliHMPIDCluster*)gMinuit->GetObjectFit();
89   Int_t iNshape = iNpars/3;
90     
91   chi2 = 0;
92   for(Int_t i=0;i<pClu->Size();i++){                                                   //loop on all pads of the cluster
93     Double_t dQpadMath = 0;                                                            //pad charge collector  
94     for(Int_t j=0;j<iNshape;j++){                                                      //Mathiesons loop as all of them may contribute to this pad
95       dQpadMath+=par[3*j+2]*pClu->Dig(i)->IntMathieson(par[3*j],par[3*j+1]);           // par[3*j+2] is charge par[3*j] is x par[3*j+1] is y of current Mathieson
96     }
97 //    if(dQpadMath>0)chi2 +=TMath::Power((pClu->Dig(i)->Q()-dQpadMath),2)/dQpadMath;   //
98     if(dQpadMath>0)chi2 +=TMath::Power((pClu->Dig(i)->Q()-dQpadMath),2);               //
99   }                                                                                    //loop on all pads of the cluster     
100 }//FitFunction()
101 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
102 void AliHMPIDCluster::Print(Option_t* opt)const
103 {
104 //Print current cluster  
105   const char *status=0;
106   switch(fSt){
107     case        kFrm  : status="formed        "   ;break;
108     case        kUnf  : status="unfolded (fit)"   ;break;
109     case        kCoG  : status="coged         "   ;break;
110     case        kLo1  : status="locmax 1 (fit)"   ;break;
111     case        kMax  : status="exceeded (cog)"   ;break;
112     case        kNot  : status="not done (cog)"   ;break;
113     case        kEmp  : status="empty         "   ;break;
114     case        kEdg  : status="edge     (fit)"   ;break;
115     case        kSi1  : status="size 1   (cog)"   ;break;
116     case        kNoLoc: status="no LocMax(fit)"   ;break;
117     case        kAbn  : status="Abnormal fit  "   ;break;
118     
119     default:            status="??????"          ;break;   
120   }
121   Double_t ratio=0;
122   if(Q()>0&&QRaw()>0) ratio = Q()/QRaw()*100;
123   Printf("%sCLU: ch=%i                 (%7.3f,%7.3f) Q=%8.3f Qraw=%8.3f(%3.0f%%) Size=%2i DimBox=%i LocMax=%i Chi2=%7.3f   %s",
124          opt,Ch(),X(),Y(),Q(),QRaw(),ratio,Size(),fBox,fNlocMax,fChi2,status);
125   if(fDigs) fDigs->Print();    
126 }//Print()
127 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
128 Int_t AliHMPIDCluster::Solve(TClonesArray *pCluLst,Bool_t isTryUnfold)
129 {
130 //This methode is invoked when the cluster is formed to solve it. Solve the cluster means to try to unfold the cluster
131 //into the local maxima number of clusters. This methode is invoked by AliHMPIDRconstructor::Dig2Clu() on cluster by cluster basis.  
132 //At this point, cluster contains a list of digits, cluster charge and size is precalculated in AddDigit(), position is preset to (-1,-1) in ctor,
133 //status is preset to kFormed in AddDigit(), chamber-sector info is preseted to actual values in AddDigit()
134 //Method first finds number of local maxima and if it's more then one tries to unfold this cluster into local maxima number of clusters
135 //Arguments: pCluLst     - cluster list pointer where to add new cluster(s)
136 //           isTryUnfold - flag to switch on/off unfolding   
137 //  Returns: number of local maxima of original cluster
138   CoG();
139   Int_t iCluCnt=pCluLst->GetEntriesFast();                                               //get current number of clusters already stored in the list by previous operations
140   if(isTryUnfold==kFALSE || Size()==1) {                                                 //if cluster contains single pad there is no way to improve the knowledge 
141     (isTryUnfold)?fSt=kSi1:fSt=kNot;
142     new ((*pCluLst)[iCluCnt++]) AliHMPIDCluster(*this);  //add this raw cluster 
143     return 1;
144   } 
145 //Phase 0. Initialise TMinuit  
146   const Int_t kMaxLocMax=6;                                                              //max allowed number of loc max for fitting
147   TMinuit *pMinuit = new TMinuit(3*kMaxLocMax);                                          //init MINUIT with this number of parameters (3 params per mathieson)
148   pMinuit->SetObjectFit((TObject*)this);  pMinuit->SetFCN(AliHMPIDCluster::FitFunc);     //set fit function
149   Double_t aArg=-1;                                     Int_t iErrFlg;                   //tmp vars for TMinuit
150   pMinuit->mnexcm("SET PRI",&aArg,1,iErrFlg);                                            //suspend all printout from TMinuit 
151   pMinuit->mnexcm("SET NOW",&aArg,0,iErrFlg);                                            //suspend all warning printout from TMinuit
152 //Phase 1. Find number of local maxima. Strategy is to check if the current pad has QDC more then all neigbours. Also find the box contaning the cluster   
153   fNlocMax=0;
154
155  for(Int_t iDig1=0;iDig1<Size();iDig1++) {                                               //first digits loop
156     AliHMPIDDigit *pDig1 = Dig(iDig1);                                                   //take next digit    
157     Int_t iCnt = 0;                                                                      //counts how many neighbouring pads has QDC more then current one
158     for(Int_t iDig2=0;iDig2<Size();iDig2++) {                                            //loop on all digits again
159       if(iDig1==iDig2) continue;                                                         //the same digit, no need to compare 
160       AliHMPIDDigit *pDig2 = Dig(iDig2);                                                 //take second digit to compare with the first one
161       Int_t dist = TMath::Sign(Int_t(pDig1->PadChX()-pDig2->PadChX()),1)+TMath::Sign(Int_t(pDig1->PadChY()-pDig2->PadChY()),1);//distance between pads
162       if(dist==1)                                                                        //means dig2 is a neighbour of dig1
163          if(pDig2->Q()>=pDig1->Q()) iCnt++;                                              //count number of pads with Q more then Q of current pad
164     }//second digits loop
165     if(iCnt==0&&fNlocMax<kMaxLocMax){                                                    //this pad has Q more then any neighbour so it's local maximum
166       Double_t xStart=pDig1->LorsX();Double_t yStart=pDig1->LorsY();
167       Double_t xMin=xStart-AliHMPIDDigit::SizePadX();
168       Double_t xMax=xStart+AliHMPIDDigit::SizePadX();
169       Double_t yMin=yStart-AliHMPIDDigit::SizePadY();
170       Double_t yMax=yStart+AliHMPIDDigit::SizePadY();
171       pMinuit->mnparm(3*fNlocMax  ,Form("x%i",fNlocMax),xStart,0.1,xMin,xMax,iErrFlg);   // X,Y,Q initial values of the loc max pad
172       pMinuit->mnparm(3*fNlocMax+1,Form("y%i",fNlocMax),yStart,0.1,yMin,yMax,iErrFlg);   // X, Y constrained to be near the loc max
173       pMinuit->mnparm(3*fNlocMax+2,Form("q%i",fNlocMax),pDig1->Q(),0.1,0,100000,iErrFlg);// Q constrained to be positive
174       fNlocMax++;
175     }//if this pad is local maximum
176   }//first digits loop
177   
178 //Phase 2. Fit loc max number of Mathiesons or add this current cluster to the list
179 // case 1 -> no loc max found
180  if ( fNlocMax == 0) {                                                                   // case of no local maxima found: pads with same charge...
181    pMinuit->mnparm(3*fNlocMax  ,Form("x%i",fNlocMax),fX,0.1,0,0,iErrFlg);                // Init values taken from CoG() -> fX,fY,fQRaw
182    pMinuit->mnparm(3*fNlocMax+1,Form("y%i",fNlocMax),fY,0.1,0,0,iErrFlg);                //
183    pMinuit->mnparm(3*fNlocMax+2,Form("q%i",fNlocMax),fQRaw,0.1,0,100000,iErrFlg);        //
184    fNlocMax = 1;
185    fSt=kNoLoc;
186  }
187
188 // case 2 -> loc max found. Check # of loc maxima 
189  if ( fNlocMax >= kMaxLocMax)                                                            // if # of local maxima exceeds kMaxLocMax... 
190    {
191      fSt = kMax;   new ((*pCluLst)[iCluCnt++]) AliHMPIDCluster(*this);                   //...add this raw cluster  
192    }                                                                                     //or...
193  else{                                                                                   //...resonable number of local maxima to fit and user requested it
194    Double_t arglist[10];arglist[0] = 10000;arglist[1] = 1.;                              //number of steps and sigma on pads charges  
195    pMinuit->mnexcm("SIMPLEX" ,arglist,2,iErrFlg);                                        //start fitting with Simplex
196    pMinuit->mnexcm("MIGRAD" ,arglist,2,iErrFlg);                                         //fitting improved by Migrad
197    if(iErrFlg) {
198      Double_t strategy=2;
199      pMinuit->mnexcm("SET STR",&strategy,1,iErrFlg);                                     //change level of strategy 
200      if(!iErrFlg) {
201        pMinuit->mnexcm("SIMPLEX" ,arglist,2,iErrFlg);                                    //start fitting with Simplex
202        pMinuit->mnexcm("MIGRAD" ,arglist,2,iErrFlg);                                     //fitting improved by Migrad
203        Printf("Try to improve fit --> err %d",iErrFlg);
204      }
205    }        
206    if(iErrFlg) fSt=kAbn;                                                                 //no convergence of the fit...
207    Double_t dummy; TString sName;                                                        //vars to get results from Minuit
208    for(Int_t i=0;i<fNlocMax;i++){                                                        //store the local maxima parameters
209       pMinuit->mnpout(3*i   ,sName,  fX, fErrX , dummy, dummy, iErrFlg);                 // X 
210       pMinuit->mnpout(3*i+1 ,sName,  fY, fErrY , dummy, dummy, iErrFlg);                 // Y
211       pMinuit->mnpout(3*i+2 ,sName,  fQ, fErrQ , dummy, dummy, iErrFlg);                 // Q
212       pMinuit->mnstat(fChi2,dummy,dummy,iErrFlg,iErrFlg,iErrFlg);                        // Chi2 of the fit
213       if(fSt!=kAbn) {         
214         if(fNlocMax!=1)fSt=kUnf;                                                           // if unfolded
215         if(fNlocMax==1&&fSt!=kNoLoc) fSt=kLo1;                                             // if only 1 loc max
216         if ( !IsInPc()) fSt = kEdg;                                                        // if Out of Pc
217         if(fSt==kNoLoc) fNlocMax=0;                                                        // if with no loc max (pads with same charge..)
218       }
219       new ((*pCluLst)[iCluCnt++]) AliHMPIDCluster(*this);                                //add new unfolded cluster
220    }
221  }
222
223  delete pMinuit;
224  return fNlocMax;
225  
226 }//Solve()
227 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++