HTA (Hidden track algorithm) improved+minors
[u/mrichter/AliRoot.git] / HMPID / AliHMPIDRecon.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 //////////////////////////////////////////////////////////////////////////
17 //                                                                      //
18 // AliHMPIDRecon                                                         //
19 //                                                                      //
20 // HMPID class to perfom pattern recognition based on Hough transfrom    //
21 // for single chamber                                                   //
22 //////////////////////////////////////////////////////////////////////////
23
24 #include "AliHMPIDRecon.h"   //class header
25 #include "AliHMPIDParam.h"   //CkovAngle()
26 #include "AliHMPIDCluster.h" //CkovAngle()
27 #include <TMinuit.h>         //FitEllipse()
28 #include <TRotation.h>       //TracePhot()
29 #include <TH1D.h>            //HoughResponse()
30 #include <TClonesArray.h>    //CkovAngle()
31 #include <AliESDtrack.h>     //CkovAngle()
32
33 const Double_t AliHMPIDRecon::fgkRadThick=1.5;
34 const Double_t AliHMPIDRecon::fgkWinThick=0.5;
35 const Double_t AliHMPIDRecon::fgkGapThick=8.0;
36 const Double_t AliHMPIDRecon::fgkWinIdx  =1.5787;
37 const Double_t AliHMPIDRecon::fgkGapIdx  =1.0005;
38
39 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
40 AliHMPIDRecon::AliHMPIDRecon():TTask("RichRec","RichPat"),
41   fRadNmean(1.292),  
42   fPhotCnt(-1),
43   fCkovSigma2(0),
44   fIsWEIGHT(kFALSE),
45   fDTheta(0.001),
46   fWindowWidth(0.045),
47   fTrkDir(TVector3(0,0,1)),fTrkPos(TVector2(30,40))  
48 {
49 // main ctor
50   for (Int_t i=0; i<3000; i++) {
51     fPhotFlag[i] =  0;
52     fPhotCkov[i] = -1;
53     fPhotPhi [i] = -1;
54     fPhotWei [i] =  0;
55   }
56 //hidden algorithm
57   fMipX=fMipY=fThTrkFit=fPhTrkFit=fCkovFit=-999;
58   fIdxMip=fNClu=0;
59   for (Int_t i=0; i<1000; i++) {
60     fXClu[i] = fYClu[i] = 0;
61   }
62 }
63 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
64 void AliHMPIDRecon::CkovAngle(AliESDtrack *pTrk,TClonesArray *pCluLst,Double_t nmean)
65 {
66 // Pattern recognition method based on Hough transform
67 // Arguments:   pTrk     - track for which Ckov angle is to be found
68 //              pCluLst  - list of clusters for this chamber   
69 //   Returns:            - track ckov angle, [rad], 
70     
71   AliHMPIDParam *pParam=AliHMPIDParam::Instance();
72   
73   if(pCluLst->GetEntries()>pParam->MultCut()) fIsWEIGHT = kTRUE; // offset to take into account bkg in reconstruction
74   else                                        fIsWEIGHT = kFALSE;
75
76   Float_t xRa,yRa,th,ph;
77   pTrk->GetHMPIDtrk(xRa,yRa,th,ph);        //initialize this track: th and ph angles at middle of RAD 
78   SetTrack(xRa,yRa,th,ph);
79
80   fRadNmean=nmean;
81
82   Float_t dMin=999,mipX=-1,mipY=-1;Int_t chId=-1,mipId=-1,mipQ=-1;                                                                           
83   fPhotCnt=0;                                                      
84   for (Int_t iClu=0; iClu<pCluLst->GetEntriesFast();iClu++){//clusters loop
85     AliHMPIDCluster *pClu=(AliHMPIDCluster*)pCluLst->UncheckedAt(iClu);                       //get pointer to current cluster    
86     chId=pClu->Ch();
87     if(pClu->Q()>pParam->QCut()){                                                             //charge compartible with MIP clusters      
88       Float_t dX=fPc.X()-pClu->X(),dY=fPc.Y()-pClu->Y(),d =TMath::Sqrt(dX*dX+dY*dY);          //distance between current cluster and intersection point
89       if( d < dMin) {mipId=iClu; dMin=d;mipX=pClu->X();mipY=pClu->Y();mipQ=(Int_t)pClu->Q();} //current cluster is closer, overwrite data for min cluster
90     }else{                                                                                    //charge compatible with photon cluster
91       Double_t thetaCer,phiCer;
92       if(FindPhotCkov(pClu->X(),pClu->Y(),thetaCer,phiCer)){                                  //find ckov angle for this  photon candidate
93         fPhotCkov[fPhotCnt]=thetaCer;                                                         //actual theta Cerenkov (in TRS)
94         fPhotPhi [fPhotCnt]=phiCer;                                                           //actual phi   Cerenkov (in TRS): -pi to come back to "unusual" ref system (X,Y,-Z)
95         fPhotCnt++;                                                                           //increment counter of photon candidates
96       }
97     }
98   }//clusters loop
99   Int_t iNacc=FlagPhot(HoughResponse());                                                      //flag photons according to individual theta ckov with respect to most probable
100   pTrk->SetHMPIDmip(mipX,mipY,mipQ,iNacc);                                                    //store mip info 
101
102   if(mipId==-1)              {pTrk->SetHMPIDsignal(kMipQdcCut);  return;}                     //no clusters with QDC more the threshold at all
103   if(dMin>pParam->DistCut()) {pTrk->SetHMPIDsignal(kMipDistCut); return;}                     //closest cluster with enough charge is still too far from intersection
104   pTrk->SetHMPIDcluIdx(chId,mipId);                                                           //set index of cluster
105   if(iNacc<1)    pTrk->SetHMPIDsignal(kNoPhotAccept);                                         //no photon candidates is accepted
106   else           pTrk->SetHMPIDsignal(FindRingCkov(pCluLst->GetEntries()));                   //find best Theta ckov for ring i.e. track
107
108   pTrk->SetHMPIDchi2(fCkovSigma2);                                                            //errors squared 
109
110 }//CkovAngle()
111 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
112 Bool_t AliHMPIDRecon::FindPhotCkov(Double_t cluX,Double_t cluY,Double_t &thetaCer,Double_t &phiCer)
113 {
114 // Finds Cerenkov angle  for this photon candidate
115 // Arguments: cluX,cluY - position of cadidate's cluster  
116 // Returns: Cerenkov angle 
117
118   TVector3 dirCkov;
119   
120   Double_t zRad= -0.5*fgkRadThick-0.5*fgkWinThick;                   //z position of middle of RAD
121   TVector3 rad(fTrkPos.X(),fTrkPos.Y(),zRad);                        //impact point at middle of RAD
122   TVector3  pc(cluX,cluY,0.5*fgkWinThick+fgkGapIdx);                 //mip at PC
123   Double_t cluR = TMath::Sqrt((cluX-fTrkPos.X())*(cluX-fTrkPos.X())+
124                               (cluY-fTrkPos.Y())*(cluY-fTrkPos.Y()));//ref. distance impact RAD-CLUSTER   
125   Double_t phi=(pc-rad).Phi();                                       //phi of photon
126     
127   Double_t ckov1=0;
128   Double_t ckov2=0.75+fTrkDir.Theta();                        //start to find theta cerenkov in DRS
129   const Double_t kTol=0.01;
130   Int_t iIterCnt = 0;
131   while(1){
132     if(iIterCnt>=50) return kFALSE;
133     Double_t ckov=0.5*(ckov1+ckov2);
134     dirCkov.SetMagThetaPhi(1,ckov,phi);
135     TVector2 posC=TraceForward(dirCkov);                      //trace photon with actual angles
136     Double_t dist=cluR-(posC-fTrkPos).Mod();                  //get distance between trial point and cluster position
137     if(posC.X()==-999) dist = - 999;                          //total reflection problem
138     iIterCnt++;                                               //counter step
139     if     (dist> kTol) ckov1=ckov;                           //cluster @ larger ckov
140     else if(dist<-kTol) ckov2=ckov;                           //cluster @ smaller ckov
141     else{                                                     //precision achived: ckov in DRS found
142       dirCkov.SetMagThetaPhi(1,ckov,phi);                     //
143       RecPhot(dirCkov,thetaCer,phiCer);                       //find ckov (in TRS:the effective Cherenkov angle!)
144       return kTRUE;
145     }
146   }
147 }//FindPhotTheta()
148 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
149 TVector2 AliHMPIDRecon::TraceForward(TVector3 dirCkov)const
150 {
151   //Trace forward a photon from (x,y) up to PC
152   // Arguments: dirCkov photon vector in LORS
153   //   Returns: pos of traced photon at PC
154   TVector2 pos(-999,-999);
155   Double_t thetaCer = dirCkov.Theta();
156   if(thetaCer > TMath::ASin(1./fRadNmean))  return pos;         //total refraction on WIN-GAP boundary
157   Double_t zRad= -0.5*fgkRadThick-0.5*fgkWinThick;              //z position of middle of RAD
158   TVector3  posCkov(fTrkPos.X(),fTrkPos.Y(),zRad);              //RAD: photon position is track position @ middle of RAD 
159   Propagate(dirCkov,posCkov,           -0.5*fgkWinThick);       //go to RAD-WIN boundary  
160   Refract  (dirCkov,         fRadNmean,fgkWinIdx);              //RAD-WIN refraction
161   Propagate(dirCkov,posCkov,            0.5*fgkWinThick);       //go to WIN-GAP boundary
162   Refract  (dirCkov,         fgkWinIdx,fgkGapIdx);              //WIN-GAP refraction
163   Propagate(dirCkov,posCkov,0.5*fgkWinThick+fgkGapThick);       //go to PC
164   pos.Set(posCkov.X(),posCkov.Y());
165   return pos;
166 }//TraceForward()
167 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
168 void AliHMPIDRecon::RecPhot(TVector3 dirCkov,Double_t &thetaCer,Double_t &phiCer)
169 {
170   //Theta Cerenkov reconstruction 
171   // Arguments: (x,y) of initial point in LORS, dirCkov photon vector in LORS
172   //   Returns: thetaCer theta cerenkov reconstructed
173 //  TVector3 dirTrk;
174 //  dirTrk.SetMagThetaPhi(1,fTrkDir.Theta(),fTrkDir.Phi());
175 //  Double_t thetaCer = TMath::ACos(dirCkov*dirTrk);
176   TRotation mtheta;   mtheta.RotateY(- fTrkDir.Theta());
177   TRotation mphi;       mphi.RotateZ(- fTrkDir.Phi());
178   TRotation mrot=mtheta*mphi;
179   TVector3 dirCkovTRS;
180   dirCkovTRS=mrot*dirCkov;
181   phiCer  = dirCkovTRS.Phi();                                          //actual value of the phi of the photon
182   thetaCer= dirCkovTRS.Theta();                                        //actual value of thetaCerenkov of the photon
183 }
184 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
185 Double_t AliHMPIDRecon::FindRingArea(Double_t ckovAng)const
186 {
187 // Find area inside the cerenkov ring which lays inside PCs
188 // Arguments: ckovAng - cerenkov angle    
189 //   Returns: area of the ring in cm^2 for given theta ckov
190    
191   const Int_t kN=100;
192   Double_t area=0;
193   for(Int_t i=0;i<kN;i++){
194     TVector2 pos1=TracePhot(ckovAng,Double_t(TMath::TwoPi()*i    /kN));//trace this photon 
195     TVector2 pos2=TracePhot(ckovAng,Double_t(TMath::TwoPi()*(i+1)/kN));//trace the next photon 
196     area+=(pos1-fTrkPos)*(pos2-fTrkPos);                               //add area of the triangle... 
197   }
198   return area;
199 }//FindRingArea()
200 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
201 Double_t AliHMPIDRecon::FindRingCkov(Int_t)
202 {
203 // Loops on all Ckov candidates and estimates the best Theta Ckov for a ring formed by those candidates. Also estimates an error for that Theat Ckov
204 // collecting errors for all single Ckov candidates thetas. (Assuming they are independent)  
205 // Arguments: iNclus- total number of clusters in chamber for background estimation
206 //    Return: best estimation of track Theta ckov
207
208   Double_t wei = 0.;
209   Double_t weightThetaCerenkov = 0.;
210
211   Double_t ckovMin=9999.,ckovMax=0.;
212   Double_t sigma2 = 0;   //to collect error squared for this ring
213   
214   for(Int_t i=0;i<fPhotCnt;i++){//candidates loop
215     if(fPhotFlag[i] == 2){
216       if(fPhotCkov[i]<ckovMin) ckovMin=fPhotCkov[i];                         //find max and min Theta ckov from all candidates within probable window
217       if(fPhotCkov[i]>ckovMax) ckovMax=fPhotCkov[i]; 
218       weightThetaCerenkov += fPhotCkov[i]*fPhotWei[i];
219       wei += fPhotWei[i];                                                    //collect weight as sum of all candidate weghts   
220       
221       sigma2 += 1./Sigma2(fPhotCkov[i],fPhotPhi[i]);
222     }
223   }//candidates loop
224   
225   if(sigma2>0) fCkovSigma2=1./sigma2;
226   else         fCkovSigma2=1e10;  
227   
228   if(wei != 0.) weightThetaCerenkov /= wei; else weightThetaCerenkov = 0.;
229   return weightThetaCerenkov;
230 }//FindCkovRing()
231 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
232 Int_t AliHMPIDRecon::FlagPhot(Double_t ckov)
233 {
234 // Flag photon candidates if their individual ckov angle is inside the window around ckov angle returned by  HoughResponse()
235 // Arguments: ckov- value of most probable ckov angle for track as returned by HoughResponse()
236 //   Returns: number of photon candidates happened to be inside the window
237
238 // Photon Flag:  Flag = 0 initial set; 
239 //               Flag = 1 good candidate (charge compatible with photon); 
240 //               Flag = 2 photon used for the ring;
241   
242   Int_t steps = (Int_t)((ckov )/ fDTheta); //how many times we need to have fDTheta to fill the distance between 0  and thetaCkovHough
243
244   Double_t tmin = (Double_t)(steps - 1)*fDTheta;
245   Double_t tmax = (Double_t)(steps)*fDTheta;
246   Double_t tavg = 0.5*(tmin+tmax);
247
248   tmin = tavg - 0.5*fWindowWidth;  tmax = tavg + 0.5*fWindowWidth;
249
250   Int_t iInsideCnt = 0; //count photons which Theta ckov inside the window
251   for(Int_t i=0;i<fPhotCnt;i++){//photon candidates loop
252     if(fPhotCkov[i] >= tmin && fPhotCkov[i] <= tmax)    { 
253       fPhotFlag[i]=2;     
254       iInsideCnt++;
255     }
256   }
257   return iInsideCnt;
258 }//FlagPhot()
259 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
260 TVector2 AliHMPIDRecon::TracePhot(Double_t ckovThe,Double_t ckovPhi)const
261 {
262 // Trace a single Ckov photon from emission point somewhere in radiator up to photocathode taking into account ref indexes of materials it travereses
263 // Arguments: ckovThe,ckovPhi- photon ckov angles in DRS, [rad]    
264 //   Returns: distance between photon point on PC and track projection  
265   TRotation mtheta;   mtheta.RotateY(fTrkDir.Theta());
266   TRotation mphi;       mphi.RotateZ(fTrkDir.Phi());  
267   TRotation mrot=mphi*mtheta;
268   TVector3  dirCkov,dirCkovTors;   
269
270   dirCkovTors.SetMagThetaPhi(1,ckovThe,ckovPhi);                    //initially photon is directed according to requested ckov angle
271   dirCkov=mrot*dirCkovTors;                                         //now we know photon direction in LORS
272   return TraceForward(dirCkov);
273 }//TracePhot()
274 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
275 void AliHMPIDRecon::Propagate(const TVector3 dir,TVector3 &pos,Double_t z)const
276 {
277 // Finds an intersection point between a line and XY plane shifted along Z.
278 // Arguments:  dir,pos   - vector along the line and any point of the line
279 //             z         - z coordinate of plain 
280 //   Returns:  none
281 //   On exit:  pos is the position if this intesection if any
282   static TVector3 nrm(0,0,1); 
283          TVector3 pnt(0,0,z);
284   
285   TVector3 diff=pnt-pos;
286   Double_t sint=(nrm*diff)/(nrm*dir);
287   pos+=sint*dir;
288 }//Propagate()
289 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
290 void AliHMPIDRecon::Refract(TVector3 &dir,Double_t n1,Double_t n2)const
291 {
292 // Refract direction vector according to Snell law
293 // Arguments: 
294 //            n1 - ref idx of first substance
295 //            n2 - ref idx of second substance
296 //   Returns: none
297 //   On exit: dir is new direction
298   Double_t sinref=(n1/n2)*TMath::Sin(dir.Theta());
299   if(sinref>1.)    dir.SetXYZ(-999,-999,-999);
300   else             dir.SetTheta(TMath::ASin(sinref));
301 }//Refract()
302 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
303 Double_t AliHMPIDRecon::HoughResponse()
304 {
305 //
306 //    fIdxMip = mipId;
307
308 //       
309   Double_t kThetaMax=0.75;
310   Int_t nChannels = (Int_t)(kThetaMax/fDTheta+0.5);
311   TH1D *phots   = new TH1D("Rphot"  ,"phots"         ,nChannels,0,kThetaMax);
312   TH1D *photsw  = new TH1D("RphotWeighted" ,"photsw" ,nChannels,0,kThetaMax);
313   TH1D *resultw = new TH1D("resultw","resultw"       ,nChannels,0,kThetaMax);
314   Int_t nBin = (Int_t)(kThetaMax/fDTheta);
315   Int_t nCorrBand = (Int_t)(fWindowWidth/(2*fDTheta));
316   
317   for (Int_t i=0; i< fPhotCnt; i++){//photon cadidates loop
318     Double_t angle = fPhotCkov[i];  if(angle<0||angle>kThetaMax) continue;
319     phots->Fill(angle);
320     Int_t bin = (Int_t)(0.5+angle/(fDTheta));
321     Double_t weight=1.;
322     if(fIsWEIGHT){
323       Double_t lowerlimit = ((Double_t)bin)*fDTheta - 0.5*fDTheta;  Double_t upperlimit = ((Double_t)bin)*fDTheta + 0.5*fDTheta;   
324       Double_t diffArea = FindRingArea(upperlimit)-FindRingArea(lowerlimit);
325       if(diffArea>0) weight = 1./diffArea;
326     }
327     photsw->Fill(angle,weight);
328     fPhotWei[i]=weight;
329   }//photon candidates loop 
330    
331   for (Int_t i=1; i<=nBin;i++){
332     Int_t bin1= i-nCorrBand;
333     Int_t bin2= i+nCorrBand;
334     if(bin1<1) bin1=1;
335     if(bin2>nBin)bin2=nBin;
336     Double_t sumPhots=phots->Integral(bin1,bin2);
337     if(sumPhots<3) continue;                            // if less then 3 photons don't trust to this ring
338     Double_t sumPhotsw=photsw->Integral(bin1,bin2);
339     resultw->Fill((Double_t)((i+0.5)*fDTheta),sumPhotsw);
340   } 
341 // evaluate the "BEST" theta ckov as the maximum value of histogramm
342   Double_t *pVec = resultw->GetArray();
343   Int_t locMax = TMath::LocMax(nBin,pVec);
344   phots->Delete();photsw->Delete();resultw->Delete(); // Reset and delete objects
345   
346   return (Double_t)(locMax*fDTheta+0.5*fDTheta); //final most probable track theta ckov   
347 }//HoughResponse()
348 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
349 Double_t AliHMPIDRecon::Sigma2(Double_t ckovTh, Double_t ckovPh)const
350 {
351 // Analithical calculation of total error (as a sum of localization, geometrical and chromatic errors) on Cerenkov angle for a given Cerenkov photon 
352 // created by a given MIP. Fromulae according to CERN-EP-2000-058 
353 // Arguments: Cerenkov and azimuthal angles for Cerenkov photon, [radians]
354 //            dip and azimuthal angles for MIP taken at the entrance to radiator, [radians]        
355 //            MIP beta
356 //   Returns: absolute error on Cerenkov angle, [radians]    
357   
358   TVector3 v(-999,-999,-999);
359   Double_t trkBeta = 1./(TMath::Cos(ckovTh)*fRadNmean);
360
361   v.SetX(SigLoc (ckovTh,ckovPh,trkBeta));
362   v.SetY(SigGeom(ckovTh,ckovPh,trkBeta));
363   v.SetZ(SigCrom(ckovTh,ckovPh,trkBeta));
364
365   return v.Mag2();
366 }
367 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
368 Double_t AliHMPIDRecon::SigLoc(Double_t thetaC, Double_t phiC,Double_t betaM)const
369 {
370 // Analithical calculation of localization error (due to finite segmentation of PC) on Cerenkov angle for a given Cerenkov photon 
371 // created by a given MIP. Fromulae according to CERN-EP-2000-058 
372 // Arguments: Cerenkov and azimuthal angles for Cerenkov photon, [radians]
373 //            dip and azimuthal angles for MIP taken at the entrance to radiator, [radians]        
374 //            MIP beta
375 //   Returns: absolute error on Cerenkov angle, [radians]    
376   Double_t phiDelta = phiC - fTrkDir.Phi();
377
378   Double_t alpha =TMath::Cos(fTrkDir.Theta())-TMath::Tan(thetaC)*TMath::Cos(phiDelta)*TMath::Sin(fTrkDir.Theta());
379   Double_t k = 1.-fRadNmean*fRadNmean+alpha*alpha/(betaM*betaM);
380   if (k<0) return 1e10;
381
382   Double_t mu =TMath::Sin(fTrkDir.Theta())*TMath::Sin(fTrkDir.Phi())+TMath::Tan(thetaC)*(TMath::Cos(fTrkDir.Theta())*TMath::Cos(phiDelta)*TMath::Sin(fTrkDir.Phi())+TMath::Sin(phiDelta)*TMath::Cos(fTrkDir.Phi()));
383   Double_t e  =TMath::Sin(fTrkDir.Theta())*TMath::Cos(fTrkDir.Phi())+TMath::Tan(thetaC)*(TMath::Cos(fTrkDir.Theta())*TMath::Cos(phiDelta)*TMath::Cos(fTrkDir.Phi())-TMath::Sin(phiDelta)*TMath::Sin(fTrkDir.Phi()));
384
385   Double_t kk = betaM*TMath::Sqrt(k)/(8*alpha);
386   Double_t dtdxc = kk*(k*(TMath::Cos(phiDelta)*TMath::Cos(fTrkDir.Phi())-TMath::Cos(fTrkDir.Theta())*TMath::Sin(phiDelta)*TMath::Sin(fTrkDir.Phi()))-(alpha*mu/(betaM*betaM))*TMath::Sin(fTrkDir.Theta())*TMath::Sin(phiDelta));
387   Double_t dtdyc = kk*(k*(TMath::Cos(phiDelta)*TMath::Sin(fTrkDir.Phi())+TMath::Cos(fTrkDir.Theta())*TMath::Sin(phiDelta)*TMath::Cos(fTrkDir.Phi()))+(alpha* e/(betaM*betaM))*TMath::Sin(fTrkDir.Theta())*TMath::Sin(phiDelta));
388
389   return  TMath::Sqrt(0.2*0.2*dtdxc*dtdxc + 0.25*0.25*dtdyc*dtdyc);
390 }
391 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
392 Double_t AliHMPIDRecon::SigCrom(Double_t thetaC, Double_t phiC,Double_t betaM)const
393 {
394 // Analithical calculation of chromatic error (due to lack of knowledge of Cerenkov photon energy) on Cerenkov angle for a given Cerenkov photon 
395 // created by a given MIP. Fromulae according to CERN-EP-2000-058 
396 // Arguments: Cerenkov and azimuthal angles for Cerenkov photon, [radians]
397 //            dip and azimuthal angles for MIP taken at the entrance to radiator, [radians]        
398 //            MIP beta
399 //   Returns: absolute error on Cerenkov angle, [radians]    
400   Double_t phiDelta = phiC - fTrkDir.Phi();
401   Double_t alpha =TMath::Cos(fTrkDir.Theta())-TMath::Tan(thetaC)*TMath::Cos(phiDelta)*TMath::Sin(fTrkDir.Theta());
402
403   Double_t dtdn = TMath::Cos(fTrkDir.Theta())*fRadNmean*betaM*betaM/(alpha*TMath::Tan(thetaC));
404             
405   Double_t f = 0.00928*(7.75-5.635)/TMath::Sqrt(12.);
406
407   return f*dtdn;
408 }//SigCrom()
409 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
410 Double_t AliHMPIDRecon::SigGeom(Double_t thetaC, Double_t phiC,Double_t betaM)const
411 {
412 // Analithical calculation of geometric error (due to lack of knowledge of creation point in radiator) on Cerenkov angle for a given Cerenkov photon 
413 // created by a given MIP. Formulae according to CERN-EP-2000-058 
414 // Arguments: Cerenkov and azimuthal angles for Cerenkov photon, [radians]
415 //            dip and azimuthal angles for MIP taken at the entrance to radiator, [radians]        
416 //            MIP beta
417 //   Returns: absolute error on Cerenkov angle, [radians]    
418
419   Double_t phiDelta = phiC - fTrkDir.Phi();
420   Double_t alpha =TMath::Cos(fTrkDir.Theta())-TMath::Tan(thetaC)*TMath::Cos(phiDelta)*TMath::Sin(fTrkDir.Theta());
421
422   Double_t k = 1.-fRadNmean*fRadNmean+alpha*alpha/(betaM*betaM);
423   if (k<0) return 1e10;
424
425   Double_t eTr = 0.5*1.5*betaM*TMath::Sqrt(k)/(8*alpha);
426   Double_t lambda = 1.-TMath::Sin(fTrkDir.Theta())*TMath::Sin(fTrkDir.Theta())*TMath::Sin(phiC)*TMath::Sin(phiC);
427
428   Double_t c = 1./(1.+ eTr*k/(alpha*alpha*TMath::Cos(thetaC)*TMath::Cos(thetaC)));
429   Double_t i = betaM*TMath::Tan(thetaC)*lambda*TMath::Power(k,1.5);
430   Double_t ii = 1.+eTr*betaM*i;
431
432   Double_t err = c * (i/(alpha*alpha*8) +  ii*(1.-lambda) / ( alpha*alpha*8*betaM*(1.+eTr)) );
433   Double_t trErr = 1.5/(TMath::Sqrt(12.)*TMath::Cos(fTrkDir.Theta()));
434
435   return trErr*err;
436 }//SigGeom()
437 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
438 //
439 // From here HTA....
440 //
441 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
442 Int_t AliHMPIDRecon::CkovHiddenTrk(AliESDtrack *pTrk,TClonesArray *pCluLst,Double_t nmean)
443 {
444 // Pattern recognition method without any infos from tracking:HTA (Hidden Track Algorithm)...
445 // The method finds in the chmber the cluster with the highest charge
446 // compatibile with a MIP, then the strategy is applied
447 // Arguments:  pTrk     - pointer to ESD track
448 //             pCluLs   - list of clusters for a given chamber 
449 //             nmean    - mean freon ref. index
450 //   Returns:           - 0=ok,1=not fitted 
451   
452   AliHMPIDParam *pParam=AliHMPIDParam::Instance();
453     
454   fRadNmean=nmean;
455
456   Float_t mipX=-1,mipY=-1;Int_t mipId=-1,mipQ=-1;                                                                           
457   fPhotCnt=0;                                                      
458   Double_t qRef = 0;
459   fNClu = pCluLst->GetEntriesFast();
460   for (Int_t iClu=0;iClu<fNClu;iClu++){                                                       //clusters loop
461     AliHMPIDCluster *pClu=(AliHMPIDCluster*)pCluLst->UncheckedAt(iClu);                       //get pointer to current cluster    
462     fXClu[iClu] = pClu->X();fYClu[iClu] = pClu->Y();                                          //store x,y for fitting procedure
463     if(pClu->Q()>qRef){                                                                       //searching the highest charge to select a MIP      
464       qRef = pClu->Q();
465       mipId=iClu; mipX=pClu->X();mipY=pClu->Y();mipQ=(Int_t)pClu->Q();
466     }                                                                                    
467   }//clusters loop
468
469   if(qRef>pParam->QCut()){                                                                       //charge compartible with MIP clusters      
470     fIdxMip = mipId;
471     fMipX = mipX; fMipY=mipY; fMipQ = qRef;
472     if(!DoRecHiddenTrk()) return 1;                                                               //Do track and ring reconstruction,if problems returns 1
473     pTrk->SetHMPIDtrk(fRadX,fRadY,fThTrkFit,fPhTrkFit);                                          //store track intersection info
474     pTrk->SetHMPIDmip(fMipX,fMipY,(Int_t)fMipQ,fNClu);                                           //store mip info 
475     pTrk->SetHMPIDcluIdx(pCluLst->GetUniqueID(),fIdxMip);                                        //set cham number and index of cluster
476     pTrk->SetHMPIDsignal(fCkovFit);                                                              //find best Theta ckov for ring i.e. track
477   }
478   return 0;
479 }//CkovHiddenTrk()
480 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
481 Bool_t AliHMPIDRecon::DoRecHiddenTrk()
482 {
483 // Pattern recognition method without any infos from tracking...
484 // First a preclustering filter to avoid part of the noise
485 // Then only ellipsed-rings are fitted (no possibility, 
486 // for the moment, to reconstruct very inclined tracks)
487 // Finally a fitting with (th,ph) free, starting by very close values
488 // previously evaluated.
489 // Arguments:   none
490 //   Returns:   none
491   Double_t phiRec;
492   CluPreFilter();
493   if(!FitEllipse(phiRec)) {return kFALSE;}
494   return FitFree(phiRec);
495 }
496 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
497 void AliHMPIDRecon::CluPreFilter()
498 {
499 // Filter of bkg clusters
500 // based on elliptical-shapes...
501 //
502     ;
503 }
504 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
505 Bool_t AliHMPIDRecon::FitEllipse(Double_t &phiRec)
506 {
507 //Fit a set of clusters with an analitical conical section function:
508   //
509   // Ax^2 + B*y^2 + 2Hxy + 2Gx + 2Fy + 1 = 0   ---> conical section
510   //
511   //  H*H - A*B > 0 hyperbola
512   //            < 0 ellipse
513   //            = 0 parabola
514   //
515   // tan 2alfa = 2H/(A-B)  alfa=angle of rotation
516   //
517   // coordinate of the centre of the conical section:
518   //   x = x' + a
519   //   y = y' + b
520   //
521   //       HF - BG
522   //  a = ---------
523   //       AB - H^2
524   //
525   //       HG - AF
526   //  b = --------
527   //       AB - H^2
528   
529   Double_t cA,cB,cF,cG,cH;
530   Double_t aArg=-1;      Int_t iErrFlg;                                                //tmp vars for TMinuit
531
532   if(!gMinuit) gMinuit = new TMinuit(5);                                               //init MINUIT with this number of parameters (5 params)
533   gMinuit->mncler();                                                                   // reset Minuit list of paramters
534   gMinuit->SetObjectFit((TObject*)this);  gMinuit->SetFCN(AliHMPIDRecon::FunMinEl);    //set fit function
535   gMinuit->mnexcm("SET PRI",&aArg,1,iErrFlg);                                          //suspend all printout from TMinuit 
536   gMinuit->mnexcm("SET NOW",&aArg,0,iErrFlg);                                          //suspend all warning printout from TMinuit
537   
538   Double_t d1,d2,d3;
539   TString sName;
540
541   gMinuit->mnparm(0," A ",1,0.01,0,0,iErrFlg);
542   gMinuit->mnparm(1," B ",1,0.01,0,0,iErrFlg);
543   gMinuit->mnparm(2," H ",1,0.01,0,0,iErrFlg);
544   gMinuit->mnparm(3," G ",1,0.01,0,0,iErrFlg);
545   gMinuit->mnparm(4," F ",1,0.01,0,0,iErrFlg);
546
547   gMinuit->mnexcm("SIMPLEX" ,&aArg,0,iErrFlg);
548   gMinuit->mnexcm("MIGRAD" ,&aArg,0,iErrFlg);   
549   gMinuit->mnpout(0,sName,cA,d1,d2,d3,iErrFlg);
550   gMinuit->mnpout(1,sName,cB,d1,d2,d3,iErrFlg);
551   gMinuit->mnpout(2,sName,cH,d1,d2,d3,iErrFlg);
552   gMinuit->mnpout(3,sName,cG,d1,d2,d3,iErrFlg);
553   gMinuit->mnpout(4,sName,cF,d1,d2,d3,iErrFlg);
554   delete gMinuit;
555
556   Double_t i2 = cA*cB-cH*cH;                                       //quartic invariant : i2 > 0  ellipse, i2 < 0 hyperbola
557   Double_t aX = (cH*cF-cB*cG)/i2;                                  //x centre of the canonical section 
558   Double_t bY = (cH*cG-cA*cF)/i2;                                  //y centre of the canonical section 
559   Double_t alfa1 = TMath::ATan(2*cH/(cA-cB));                      //alpha = angle of rotation of the conical section
560   if(alfa1<0) alfa1+=TMath::Pi(); 
561   alfa1*=0.5;
562   Double_t alfa2 = alfa1+TMath::Pi();
563   Double_t phiref = TMath::ATan2(bY-fMipY,aX-fMipX);               //evaluate in a unique way the angle of rotation comapring it
564   if(phiref<0) phiref+=TMath::TwoPi();                             //with the vector that poinst to the centre from the mip 
565   if(i2<0) phiref+=TMath::Pi();
566   if(phiref>TMath::TwoPi()) phiref-=TMath::TwoPi();
567
568 //  Printf(" alfa1 %f",alfa1*TMath::RadToDeg());
569 //  Printf(" alfa2 %f",alfa2*TMath::RadToDeg());
570 //  Printf(" firef %f",phiref*TMath::RadToDeg());
571   if(TMath::Abs(alfa1-phiref)<TMath::Abs(alfa2-phiref)) phiRec = alfa1; else phiRec = alfa2;  
572   
573 //  cout << " phi reconstructed " << phiRec*TMath::RadToDeg() << endl;
574   return (i2>0);
575 //
576 }
577 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
578 Bool_t AliHMPIDRecon::FitFree(Double_t phiRec)
579 {
580 // Fit performed by minimizing RMS/sqrt(n) of the
581 // photons reconstructed. First phi is fixed and theta
582 // is fouond, then (th,ph) of the track
583 // as free parameters
584 // Arguments:    PhiRec phi of the track
585 //   Returns:    none
586   Double_t aArg=-1;  Int_t iErrFlg;                                                    //tmp vars for TMinuit
587   if(!gMinuit) gMinuit = new TMinuit(2);                                               //init MINUIT with this number of parameters (5 params)
588   gMinuit->mncler();                                                                   // reset Minuit list of paramters
589   gMinuit->SetObjectFit((TObject*)this);  gMinuit->SetFCN(AliHMPIDRecon::FunMinPhot);  //set fit function
590   gMinuit->mnexcm("SET PRI",&aArg,1,iErrFlg);                                          //suspend all printout from TMinuit 
591   gMinuit->mnexcm("SET NOW",&aArg,0,iErrFlg);                                          //suspend all warning printout from TMinuit
592   
593   Double_t d1,d2,d3;
594   TString sName;
595   Double_t th,ph;
596   
597   gMinuit->mnexcm("SET PRI",&aArg,1,iErrFlg);                                          //suspend all printout from TMinuit
598   gMinuit->mnexcm("SET NOW",&aArg,0,iErrFlg);
599
600   gMinuit->mnparm(0," theta ",  0.01,0.01,0,TMath::PiOver2(),iErrFlg);
601   gMinuit->mnparm(1," phi   ",phiRec,0.01,0,TMath::TwoPi()  ,iErrFlg);
602   
603   gMinuit->FixParameter(1);
604   gMinuit->mnexcm("SIMPLEX" ,&aArg,0,iErrFlg);   
605   gMinuit->mnexcm("MIGRAD"  ,&aArg,0,iErrFlg);
606   gMinuit->Release(1);  
607   gMinuit->mnexcm("MIGRAD"  ,&aArg,0,iErrFlg);
608   
609   gMinuit->mnpout(0,sName,th,d1,d2,d3,iErrFlg);
610   gMinuit->mnpout(1,sName,ph,d1,d2,d3,iErrFlg);   
611
612   Double_t outPar[2] = {th,ph}; Double_t g; Double_t f;Int_t flag = 3;
613   gMinuit->Eval(2, &g, f, outPar,flag);  
614
615   SetTrkFit(th,ph);
616   
617   return kTRUE;
618 }
619 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
620 Double_t AliHMPIDRecon::FunConSect(Double_t *c,Double_t x,Double_t y)
621 {
622   return c[0]*x*x+c[1]*y*y+2*c[2]*x*y+2*c[3]*x+2*c[4]*y+1;
623 }
624 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
625 void AliHMPIDRecon::FunMinEl(Int_t &/* */,Double_t* /* */,Double_t &f,Double_t *par,Int_t /* */)
626 {
627   AliHMPIDRecon *pRec=(AliHMPIDRecon*)gMinuit->GetObjectFit();
628   Double_t minFun = 0;
629   Int_t np = pRec->NClu();
630   for(Int_t i=0;i<np;i++) {
631     if(i==pRec->IdxMip()) continue;
632     Double_t el = pRec->FunConSect(par,pRec->XClu(i),pRec->YClu(i));
633     minFun +=el*el;
634   }
635   f = minFun;
636 }
637 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
638 void AliHMPIDRecon::FunMinPhot(Int_t &/* */,Double_t* /* */,Double_t &f,Double_t *par,Int_t iflag)
639 {
640   AliHMPIDRecon *pRec=(AliHMPIDRecon*)gMinuit->GetObjectFit();
641   Double_t sizeCh = 0.5*fgkRadThick+fgkWinThick+fgkGapThick;
642   Double_t thTrk = par[0]; 
643   Double_t phTrk = par[1]; 
644   Double_t xrad = pRec->MipX() - sizeCh*TMath::Tan(thTrk)*TMath::Cos(phTrk);
645   Double_t yrad = pRec->MipY() - sizeCh*TMath::Tan(thTrk)*TMath::Sin(phTrk);
646   pRec->SetRadXY(xrad,yrad);
647   pRec->SetTrack(xrad,yrad,thTrk,phTrk);
648
649   Double_t meanCkov=0;
650   Double_t meanCkov2=0;
651   Double_t thetaCer,phiCer;
652   for(Int_t i=0;i<pRec->NClu();i++) {
653     pRec->FindPhotCkov(pRec->XClu(i),pRec->YClu(i),thetaCer,phiCer);  
654
655     meanCkov  += thetaCer;
656     meanCkov2 += thetaCer*thetaCer;
657   }
658   meanCkov/=pRec->NClu();
659   Double_t rms = TMath::Sqrt(meanCkov2/pRec->NClu() - meanCkov*meanCkov);
660   f = rms/TMath::Sqrt(pRec->NClu());
661   Printf(" mean %f rms/sqrt(n) %f",meanCkov,f);  
662   if(iflag==3) pRec->SetCkovFit(meanCkov);
663   
664 }//FunMinPhot()
665 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
666 //
667 // ended Hidden track algorithm....
668 //
669 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++