Bug fixed after effc++ corrections
[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 "AliHMPIDCluster.h" //CkovAngle()
26 #include <TRotation.h>       //TracePhot()
27 #include <TH1D.h>            //HoughResponse()
28 #include <TClonesArray.h>    //CkovAngle()
29 #include <AliESDtrack.h>     //CkovAngle()
30
31   Int_t     fPhotCnt;                           // counter of photons candidate
32   Int_t    *fPhotFlag;                          // flags of photon candidates
33   Double_t *fPhotCkov;                          // Ckov angles of photon candidates, [rad]
34   Double_t *fPhotPhi;                           // phis of photons candidates, [rad]
35   Double_t *fPhotWei;                           // weigths of photon candidates
36   Double_t  fCkovSigma2;                        // sigma2 of the reconstructed ring
37
38   Bool_t    fIsWEIGHT;                          // flag to consider weight procedure
39   Float_t   fDTheta;                            // Step for sliding window
40   Float_t   fWindowWidth;                       // Hough width of sliding window
41   
42   Double_t  fRingArea;                          // area of a given ring
43   Double_t  fRingAcc;                           // fraction of the ring accepted by geometry
44   TVector3  fTrkDir;                            // track direction in LORS at RAD
45   TVector2  fTrkPos;                            // track positon in LORS at RAD
46   TVector2  fMipPos;                            // mip positon for a given track
47   TVector2  fPc;                                // track position at PC
48   
49   AliHMPIDParam *fParam;                        // Pointer to AliHMPIDParam
50
51
52 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
53 AliHMPIDRecon::AliHMPIDRecon():
54   TTask("RichRec","RichPat"),
55   fPhotCnt(-1),
56   fPhotFlag(0x0),
57   fPhotCkov(0x0),
58   fPhotPhi(0x0),
59   fPhotWei(0x0),
60   fCkovSigma2(0),
61   fIsWEIGHT(kFALSE),
62   fDTheta(0.001),
63   fWindowWidth(0.045),
64   fRingArea(0),
65   fRingAcc(0),
66   fTrkDir(0,0,1),  // Just for test
67   fTrkPos(30,40),  // Just for test
68   fMipPos(0,0),
69   fPc(0,0),
70   fParam(AliHMPIDParam::Instance())
71 {
72 //..
73 //init of data members
74 //..
75   
76   fParam->SetRefIdx(fParam->MeanIdxRad()); // initialization of ref index to a default one
77 }
78 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
79 void AliHMPIDRecon::InitVars(Int_t n)
80 {
81 //..
82 //Init some variables
83 //..
84   if(n<0) return;
85   fPhotFlag = new Int_t[n];
86   fPhotCkov = new Double_t[n];
87   fPhotPhi  = new Double_t[n];
88   fPhotWei  = new Double_t[n];
89 //
90 }
91 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
92 void AliHMPIDRecon::DeleteVars()const
93 {
94 //..
95 //Delete variables
96 //..
97   delete fPhotFlag;
98   delete fPhotCkov;
99   delete fPhotPhi;
100   delete fPhotWei;
101 }
102 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
103 void AliHMPIDRecon::CkovAngle(AliESDtrack *pTrk,TClonesArray *pCluLst,Double_t nmean,Double_t qthre)
104 {
105 // Pattern recognition method based on Hough transform
106 // Arguments:   pTrk     - track for which Ckov angle is to be found
107 //              pCluLst  - list of clusters for this chamber   
108 //   Returns:            - track ckov angle, [rad], 
109     
110   Int_t nClusTot = pCluLst->GetEntries();
111   if(nClusTot>fParam->MultCut()) fIsWEIGHT = kTRUE; // offset to take into account bkg in reconstruction
112   else                           fIsWEIGHT = kFALSE;
113
114   InitVars(nClusTot);
115   
116   Float_t xRa,yRa,th,ph;
117   pTrk->GetHMPIDtrk(xRa,yRa,th,ph);        //initialize this track: th and ph angles at middle of RAD 
118   SetTrack(xRa,yRa,th,ph);
119
120   fParam->SetRefIdx(nmean);
121
122   Float_t dMin=999,mipX=-1,mipY=-1;Int_t chId=-1,mipId=-1,mipQ=-1;                                                                           
123   fPhotCnt=0;                                                      
124   for (Int_t iClu=0; iClu<pCluLst->GetEntriesFast();iClu++){//clusters loop
125     AliHMPIDCluster *pClu=(AliHMPIDCluster*)pCluLst->UncheckedAt(iClu);                       //get pointer to current cluster    
126     chId=pClu->Ch();
127     if(pClu->Q()>qthre){                                                                      //charge compartible with MIP clusters      
128       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
129       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
130     }else{                                                                                    //charge compatible with photon cluster
131       Double_t thetaCer,phiCer;
132       if(FindPhotCkov(pClu->X(),pClu->Y(),thetaCer,phiCer)){                                  //find ckov angle for this  photon candidate
133         fPhotCkov[fPhotCnt]=thetaCer;                                                         //actual theta Cerenkov (in TRS)
134         fPhotPhi [fPhotCnt]=phiCer;                                                           //actual phi   Cerenkov (in TRS): -pi to come back to "unusual" ref system (X,Y,-Z)
135         //PH        Printf("photon n. %i reconstructed theta = %f",fPhotCnt,fPhotCkov[fPhotCnt]);
136         fPhotCnt++;                                                                           //increment counter of photon candidates
137       }
138     }
139   }//clusters loop
140   fMipPos.Set(mipX,mipY);
141   if(fPhotCnt<=3) pTrk->SetHMPIDsignal(kNoPhotAccept);                                        //no reconstruction with <=3 photon candidates
142   Int_t iNrec=FlagPhot(HoughResponse());                                                      //flag photons according to individual theta ckov with respect to most probable
143   pTrk->SetHMPIDmip(mipX,mipY,mipQ,iNrec);                                                    //store mip info 
144
145   if(mipId==-1)              {pTrk->SetHMPIDsignal(kMipQdcCut);  return;}                     //no clusters with QDC more the threshold at all
146   if(dMin>fParam->DistCut()) {pTrk->SetHMPIDsignal(kMipDistCut); return;}                     //closest cluster with enough charge is still too far from intersection
147   pTrk->SetHMPIDcluIdx(chId,mipId);                                                           //set index of cluster
148   if(iNrec<1){
149     pTrk->SetHMPIDsignal(kNoPhotAccept);                                                      //no photon candidates are accepted
150   }
151   else {
152     Double_t thetaC = FindRingCkov(pCluLst->GetEntries());                                    //find the best reconstructed theta Cherenkov
153 //    FindRingGeom(thetaC,2);
154     pTrk->SetHMPIDsignal(thetaC);                                                             //store theta Cherenkov
155     pTrk->SetHMPIDchi2(fCkovSigma2);                                                          //store errors squared
156   }
157
158   DeleteVars();
159 }//CkovAngle()
160 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
161 Bool_t AliHMPIDRecon::FindPhotCkov(Double_t cluX,Double_t cluY,Double_t &thetaCer,Double_t &phiCer)
162 {
163 // Finds Cerenkov angle  for this photon candidate
164 // Arguments: cluX,cluY - position of cadidate's cluster  
165 // Returns: Cerenkov angle 
166
167   TVector3 dirCkov;
168   
169   Double_t zRad= -0.5*fParam->RadThick()-0.5*fParam->WinThick();     //z position of middle of RAD
170   TVector3 rad(fTrkPos.X(),fTrkPos.Y(),zRad);                        //impact point at middle of RAD
171   TVector3  pc(cluX,cluY,0.5*fParam->WinThick()+fParam->GapIdx());   //mip at PC
172   Double_t cluR = TMath::Sqrt((cluX-fTrkPos.X())*(cluX-fTrkPos.X())+
173                               (cluY-fTrkPos.Y())*(cluY-fTrkPos.Y()));//ref. distance impact RAD-CLUSTER   
174   Double_t phi=(pc-rad).Phi();                                       //phi of photon
175     
176   Double_t ckov1=0;
177   Double_t ckov2=0.75+fTrkDir.Theta();                        //start to find theta cerenkov in DRS
178   const Double_t kTol=0.01;
179   Int_t iIterCnt = 0;
180   while(1){
181     if(iIterCnt>=50) return kFALSE;
182     Double_t ckov=0.5*(ckov1+ckov2);
183     dirCkov.SetMagThetaPhi(1,ckov,phi);
184     TVector2 posC=TraceForward(dirCkov);                      //trace photon with actual angles
185     Double_t dist=cluR-(posC-fTrkPos).Mod();                  //get distance between trial point and cluster position
186     if(posC.X()==-999) dist = - 999;                          //total reflection problem
187     iIterCnt++;                                               //counter step
188     if     (dist> kTol) ckov1=ckov;                           //cluster @ larger ckov
189     else if(dist<-kTol) ckov2=ckov;                           //cluster @ smaller ckov
190     else{                                                     //precision achived: ckov in DRS found
191       dirCkov.SetMagThetaPhi(1,ckov,phi);                     //
192       Lors2Trs(dirCkov,thetaCer,phiCer);                       //find ckov (in TRS:the effective Cherenkov angle!)
193       return kTRUE;
194     }
195   }
196 }//FindPhotTheta()
197 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
198 TVector2 AliHMPIDRecon::TraceForward(TVector3 dirCkov)const
199 {
200   //Trace forward a photon from (x,y) up to PC
201   // Arguments: dirCkov photon vector in LORS
202   //   Returns: pos of traced photon at PC
203   
204   TVector2 pos(-999,-999);
205   Double_t thetaCer = dirCkov.Theta();
206   if(thetaCer > TMath::ASin(1./fParam->GetRefIdx())) return pos;          //total refraction on WIN-GAP boundary
207   Double_t zRad= -0.5*fParam->RadThick()-0.5*fParam->WinThick();          //z position of middle of RAD
208   TVector3  posCkov(fTrkPos.X(),fTrkPos.Y(),zRad);                        //RAD: photon position is track position @ middle of RAD 
209   Propagate(dirCkov,posCkov,           -0.5*fParam->WinThick());          //go to RAD-WIN boundary  
210   Refract  (dirCkov,         fParam->GetRefIdx(),fParam->WinIdx());       //RAD-WIN refraction
211   Propagate(dirCkov,posCkov,            0.5*fParam->WinThick());          //go to WIN-GAP boundary
212   Refract  (dirCkov,         fParam->WinIdx(),fParam->GapIdx());          //WIN-GAP refraction
213   Propagate(dirCkov,posCkov,0.5*fParam->WinThick()+fParam->GapThick());   //go to PC
214   pos.Set(posCkov.X(),posCkov.Y());
215   return pos;
216 }//TraceForward()
217 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
218 void AliHMPIDRecon::Lors2Trs(TVector3 dirCkov,Double_t &thetaCer,Double_t &phiCer)const
219 {
220   //Theta Cerenkov reconstruction 
221   // Arguments: dirCkov photon vector in LORS
222   //   Returns: thetaCer of photon in TRS
223   //              phiCer of photon in TRS
224 //  TVector3 dirTrk;
225 //  dirTrk.SetMagThetaPhi(1,fTrkDir.Theta(),fTrkDir.Phi());
226 //  Double_t thetaCer = TMath::ACos(dirCkov*dirTrk);
227   TRotation mtheta;   mtheta.RotateY(-fTrkDir.Theta());
228   TRotation mphi;       mphi.RotateZ(-fTrkDir.Phi());
229   TRotation mrot=mtheta*mphi;
230   TVector3 dirCkovTRS;
231   dirCkovTRS=mrot*dirCkov;
232   phiCer  = dirCkovTRS.Phi();                                          //actual value of the phi of the photon
233   thetaCer= dirCkovTRS.Theta();                                        //actual value of thetaCerenkov of the photon
234 }
235 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
236 void AliHMPIDRecon::Trs2Lors(TVector3 dirCkov,Double_t &thetaCer,Double_t &phiCer)const
237 {
238   //Theta Cerenkov reconstruction 
239   // Arguments: dirCkov photon vector in TRS
240   //   Returns: thetaCer of photon in LORS
241   //              phiCer of photon in LORS
242   TRotation mtheta;   mtheta.RotateY(fTrkDir.Theta());
243   TRotation mphi;       mphi.RotateZ(fTrkDir.Phi());
244   TRotation mrot=mphi*mtheta;
245   TVector3 dirCkovLORS;
246   dirCkovLORS=mrot*dirCkov;
247   phiCer  = dirCkovLORS.Phi();                                          //actual value of the phi of the photon
248   thetaCer= dirCkovLORS.Theta();                                        //actual value of thetaCerenkov of the photon
249 }
250 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
251 void AliHMPIDRecon::FindRingGeom(Double_t ckovAng,Int_t level)
252 {
253 // Find area covered in the PC acceptance
254 // Arguments: ckovAng - cerenkov angle
255 //            level   - precision in finding area and portion of ring accepted (multiple of 50)
256 //   Returns: area of the ring in cm^2 for given theta ckov
257    
258   Int_t kN=50*level;
259   Int_t nPoints = 0;
260   Double_t area=0;
261   
262   Bool_t first=kFALSE;
263   TVector2 pos1;
264   
265   for(Int_t i=0;i<kN;i++){
266    if(!first) {
267       pos1=TracePhot(ckovAng,Double_t(TMath::TwoPi()*(i+1)/kN));                                    //find a good trace for the first photon
268      if(pos1.X()==-999) continue;                                                                   //no area: open ring                  
269      if(!fParam->IsInside(pos1.X(),pos1.Y(),0)) {
270        pos1 = IntWithEdge(fMipPos,pos1);                                                            // find the very first intersection...
271      } else {
272        if(!AliHMPIDParam::IsInDead(pos1.X(),pos1.Y())) nPoints++;                                   //photon is accepted if not in dead zone
273      }
274      first=kTRUE;
275      continue;
276    }
277    TVector2 pos2=TracePhot(ckovAng,Double_t(TMath::TwoPi()*(i+1)/kN));                              //trace the next photon
278    if(pos2.X()==-999) continue;                                                                     //no area: open ring             
279    if(!fParam->IsInside(pos2.X(),pos2.Y(),0)) {
280      pos2 = IntWithEdge(fMipPos,pos2);
281    } else {
282      if(!AliHMPIDParam::IsInDead(pos2.X(),pos2.Y())) nPoints++;                                     //photon is accepted if not in dead zone
283    }
284    area+=TMath::Abs((pos1-fMipPos).X()*(pos2-fMipPos).Y()-(pos1-fMipPos).Y()*(pos2-fMipPos).X());   //add area of the triangle...            
285    pos1 = pos2;
286   }
287 //---  find area and length of the ring;
288   fRingAcc = (Double_t)nPoints/(Double_t)kN;
289   area*=0.5;
290   fRingArea = area;
291 }//FindRingGeom()
292 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
293 TVector2 AliHMPIDRecon::IntWithEdge(TVector2 p1,TVector2 p2)const
294 {
295 // It finds the intersection of the line for 2 points traced as photons
296 // and the edge of a given PC
297 // Arguments: 2 points obtained tracing the photons
298 //   Returns: intersection point with detector (PC) edges
299
300   Double_t xmin = (p1.X()<p2.X())? p1.X():p2.X(); 
301   Double_t xmax = (p1.X()<p2.X())? p2.X():p1.X(); 
302   Double_t ymin = (p1.Y()<p2.Y())? p1.Y():p2.Y(); 
303   Double_t ymax = (p1.Y()<p2.Y())? p2.Y():p1.Y(); 
304   
305   Double_t m = TMath::Tan((p2-p1).Phi());
306   TVector2 pint;
307   //intersection with low  X
308   pint.Set((Double_t)(p1.X() + (0-p1.Y())/m),0.);
309   if(pint.X()>=0 && pint.X()<=fParam->SizeAllX() &&
310      pint.X()>=xmin && pint.X()<=xmax            &&
311      pint.Y()>=ymin && pint.Y()<=ymax) return pint;
312   //intersection with high X  
313   pint.Set((Double_t)(p1.X() + (fParam->SizeAllY()-p1.Y())/m),(Double_t)(fParam->SizeAllY()));
314   if(pint.X()>=0 && pint.X()<=fParam->SizeAllX() &&
315      pint.X()>=xmin && pint.X()<=xmax            &&
316      pint.Y()>=ymin && pint.Y()<=ymax) return pint;
317   //intersection with left Y  
318   pint.Set(0.,(Double_t)(p1.Y() + m*(0-p1.X())));
319   if(pint.Y()>=0 && pint.Y()<=fParam->SizeAllY() &&
320      pint.Y()>=ymin && pint.Y()<=ymax            &&
321      pint.X()>=xmin && pint.X()<=xmax) return pint;
322   //intersection with righ Y  
323   pint.Set((Double_t)(fParam->SizeAllX()),(Double_t)(p1.Y() + m*(fParam->SizeAllX()-p1.X())));
324   if(pint.Y()>=0 && pint.Y()<=fParam->SizeAllY() &&
325      pint.Y()>=ymin && pint.Y()<=ymax            &&
326      pint.X()>=xmin && pint.X()<=xmax) return pint;
327   return p1;
328 }//IntWithEdge()
329 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
330 Double_t AliHMPIDRecon::FindRingCkov(Int_t)
331 {
332 // 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
333 // collecting errors for all single Ckov candidates thetas. (Assuming they are independent)  
334 // Arguments: iNclus- total number of clusters in chamber for background estimation
335 //    Return: best estimation of track Theta ckov
336
337   Double_t wei = 0.;
338   Double_t weightThetaCerenkov = 0.;
339
340   Double_t ckovMin=9999.,ckovMax=0.;
341   Double_t sigma2 = 0;   //to collect error squared for this ring
342   
343   for(Int_t i=0;i<fPhotCnt;i++){//candidates loop
344     if(fPhotFlag[i] == 2){
345       if(fPhotCkov[i]<ckovMin) ckovMin=fPhotCkov[i];                         //find max and min Theta ckov from all candidates within probable window
346       if(fPhotCkov[i]>ckovMax) ckovMax=fPhotCkov[i]; 
347       weightThetaCerenkov += fPhotCkov[i]*fPhotWei[i];
348       wei += fPhotWei[i];                                                    //collect weight as sum of all candidate weghts   
349       
350       sigma2 += 1./fParam->Sigma2(fTrkDir.Theta(),fTrkDir.Phi(),fPhotCkov[i],fPhotPhi[i]);
351     }
352   }//candidates loop
353   
354   if(sigma2>0) fCkovSigma2=1./sigma2;
355   else         fCkovSigma2=1e10;  
356   
357   if(wei != 0.) weightThetaCerenkov /= wei; else weightThetaCerenkov = 0.;
358   return weightThetaCerenkov;
359 }//FindCkovRing()
360 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
361 Int_t AliHMPIDRecon::FlagPhot(Double_t ckov)
362 {
363 // Flag photon candidates if their individual ckov angle is inside the window around ckov angle returned by  HoughResponse()
364 // Arguments: ckov- value of most probable ckov angle for track as returned by HoughResponse()
365 //   Returns: number of photon candidates happened to be inside the window
366
367 // Photon Flag:  Flag = 0 initial set; 
368 //               Flag = 1 good candidate (charge compatible with photon); 
369 //               Flag = 2 photon used for the ring;
370   
371   Int_t steps = (Int_t)((ckov )/ fDTheta); //how many times we need to have fDTheta to fill the distance between 0  and thetaCkovHough
372
373   Double_t tmin = (Double_t)(steps - 1)*fDTheta;
374   Double_t tmax = (Double_t)(steps)*fDTheta;
375   Double_t tavg = 0.5*(tmin+tmax);
376
377   tmin = tavg - 0.5*fWindowWidth;  tmax = tavg + 0.5*fWindowWidth;
378
379   Int_t iInsideCnt = 0; //count photons which Theta ckov inside the window
380   for(Int_t i=0;i<fPhotCnt;i++){//photon candidates loop
381     fPhotFlag[i] = 0;
382     if(fPhotCkov[i] >= tmin && fPhotCkov[i] <= tmax)    { 
383       fPhotFlag[i]=2;     
384       iInsideCnt++;
385     }
386   }
387   return iInsideCnt;
388 }//FlagPhot()
389 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
390 TVector2 AliHMPIDRecon::TracePhot(Double_t ckovThe,Double_t ckovPhi)const
391 {
392 // Trace a single Ckov photon from emission point somewhere in radiator up to photocathode taking into account ref indexes of materials it travereses
393 // Arguments: ckovThe,ckovPhi- photon ckov angles in TRS, [rad]
394 //   Returns: distance between photon point on PC and track projection  
395   
396   Double_t theta,phi;
397   TVector3  dirTRS,dirLORS;   
398   dirTRS.SetMagThetaPhi(1,ckovThe,ckovPhi);                     //photon in TRS
399   Trs2Lors(dirTRS,theta,phi);
400   dirLORS.SetMagThetaPhi(1,theta,phi);                          //photon in LORS
401   return TraceForward(dirLORS);                                 //now foward tracing
402 }//TracePhot()
403 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
404 void AliHMPIDRecon::Propagate(const TVector3 dir,TVector3 &pos,Double_t z)const
405 {
406 // Finds an intersection point between a line and XY plane shifted along Z.
407 // Arguments:  dir,pos   - vector along the line and any point of the line
408 //             z         - z coordinate of plain 
409 //   Returns:  none
410 //   On exit:  pos is the position if this intesection if any
411   static TVector3 nrm(0,0,1); 
412          TVector3 pnt(0,0,z);
413   
414   TVector3 diff=pnt-pos;
415   Double_t sint=(nrm*diff)/(nrm*dir);
416   pos+=sint*dir;
417 }//Propagate()
418 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
419 void AliHMPIDRecon::Refract(TVector3 &dir,Double_t n1,Double_t n2)const
420 {
421 // Refract direction vector according to Snell law
422 // Arguments: 
423 //            n1 - ref idx of first substance
424 //            n2 - ref idx of second substance
425 //   Returns: none
426 //   On exit: dir is new direction
427   Double_t sinref=(n1/n2)*TMath::Sin(dir.Theta());
428   if(TMath::Abs(sinref)>1.) dir.SetXYZ(-999,-999,-999);
429   else             dir.SetTheta(TMath::ASin(sinref));
430 }//Refract()
431 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
432 Double_t AliHMPIDRecon::HoughResponse()
433 {
434 //
435 //    fIdxMip = mipId;
436
437 //       
438   Double_t kThetaMax=0.75;
439   Int_t nChannels = (Int_t)(kThetaMax/fDTheta+0.5);
440   TH1D *phots   = new TH1D("Rphot"  ,"phots"         ,nChannels,0,kThetaMax);
441   TH1D *photsw  = new TH1D("RphotWeighted" ,"photsw" ,nChannels,0,kThetaMax);
442   TH1D *resultw = new TH1D("resultw","resultw"       ,nChannels,0,kThetaMax);
443   Int_t nBin = (Int_t)(kThetaMax/fDTheta);
444   Int_t nCorrBand = (Int_t)(fWindowWidth/(2*fDTheta));
445   
446   for (Int_t i=0; i< fPhotCnt; i++){//photon cadidates loop
447     Double_t angle = fPhotCkov[i];  if(angle<0||angle>kThetaMax) continue;
448     phots->Fill(angle);
449     Int_t bin = (Int_t)(0.5+angle/(fDTheta));
450     Double_t weight=1.;
451     if(fIsWEIGHT){
452       Double_t lowerlimit = ((Double_t)bin)*fDTheta - 0.5*fDTheta;  Double_t upperlimit = ((Double_t)bin)*fDTheta + 0.5*fDTheta;
453       FindRingGeom(lowerlimit);
454       Double_t areaLow  = GetRingArea();
455       FindRingGeom(upperlimit);
456       Double_t areaHigh = GetRingArea();
457       Double_t diffArea = areaHigh - areaLow;
458       if(diffArea>0) weight = 1./diffArea;
459     }
460     photsw->Fill(angle,weight);
461     fPhotWei[i]=weight;
462   }//photon candidates loop 
463    
464   for (Int_t i=1; i<=nBin;i++){
465     Int_t bin1= i-nCorrBand;
466     Int_t bin2= i+nCorrBand;
467     if(bin1<1) bin1=1;
468     if(bin2>nBin)bin2=nBin;
469     Double_t sumPhots=phots->Integral(bin1,bin2);
470     if(sumPhots<3) continue;                            // if less then 3 photons don't trust to this ring
471     Double_t sumPhotsw=photsw->Integral(bin1,bin2);
472     resultw->Fill((Double_t)((i+0.5)*fDTheta),sumPhotsw);
473   } 
474 // evaluate the "BEST" theta ckov as the maximum value of histogramm
475   Double_t *pVec = resultw->GetArray();
476   Int_t locMax = TMath::LocMax(nBin,pVec);
477   delete phots;delete photsw;delete resultw; // Reset and delete objects
478   
479   return (Double_t)(locMax*fDTheta+0.5*fDTheta); //final most probable track theta ckov   
480 }//HoughResponse()
481 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++