J. Barbosa's spot reconstruction algorithm.
[u/mrichter/AliRoot.git] / RICH / AliRICHDetect.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   $Log$
18 */
19
20
21 #include "AliRICH.h"
22 #include "AliRICHPoints.h"
23 #include "AliRICHDetect.h"
24 #include "DataStructures.h"
25 #include "AliRun.h"
26 #include "TParticle.h"
27 #include "TMath.h"
28 #include "TRandom.h"
29
30
31
32 ClassImp(AliRICHDetect)
33 //___________________________________________
34 AliRICHDetect::AliRICHDetect() : TObject()
35 {
36     //fChambers = 0;
37 }
38
39 //___________________________________________
40 AliRICHDetect::AliRICHDetect(const char *name, const char *title)
41     : TObject()
42 {
43     
44     /*fChambers = new TObjArray(7);
45     for (Int_t i=0; i<7; i++) {
46     
47         (*fChambers)[i] = new AliRICHchamber();  
48         
49     } */     
50 }
51
52
53 void AliRICHDetect::Detect()
54 {       
55     
56   //printf("Detection started!\n");
57   Float_t OMEGA,steptheta,stepphi,x,y,cx,cy,l,aux1,aux2,aux3,maxi,maxj,maxk,max;
58   //Float_t theta,phi,realomega,realtheta;
59   Int_t i,j,k;
60   
61   //const Float_t Noise_Level=0;          //Noise Level in percentage of mesh points
62   //const Float_t t=0.6;                        //Softening of Noise Correction (factor)
63   
64   const Float_t Pii=3.1415927;          
65   
66   const Float_t h=10;                       //Distance from Radiator to Pads in pads
67   
68   
69   const Int_t dimensiontheta=100;               //Matrix dimension for angle Detection
70   const Int_t dimensionphi=100;
71   const Int_t dimensionOMEGA=100;
72   
73   //const Float_t SPOTp=.2;             //Percentage of spot action
74   //const Int_t np=500;         //Number of points to reconstruct elipse 
75   const Float_t maxOMEGA=65*Pii/180;            //Maximum Cherenkov angle to identify
76   
77   Int_t Point[dimensiontheta][dimensionphi][dimensionOMEGA];
78   //Int_t Point1[dimensiontheta][dimensionphi][dimensionOMEGA];
79   
80   steptheta=Pii/dimensiontheta;
81   stepphi=Pii/dimensionphi;
82
83   AliRICHChamber*       iChamber;
84   
85   AliRICH *RICH  = (AliRICH*)gAlice->GetDetector("RICH");
86   Int_t ntracks = (Int_t)gAlice->TreeH()->GetEntries();
87   //Int_t ntrks = gAlice->GetNtrack();
88   
89   Float_t trackglob[3];
90   Float_t trackloc[3];
91
92   //printf("Got ntracks:%d\n",ntracks);
93   /*TVector *xp = new TVector(1000);
94   TVector *yp = new TVector(1000);
95   TVector *zp = new TVector(1000);
96   TVector *ptrk = new TVector(1000);
97   TVector *phit = new TVector(1000);*/
98   
99   //printf("Area de uma elipse com teta 0 e Omega 45:%f",Area(0,45));
100     
101         
102   for (Int_t track=0; track<ntracks;track++) {
103     gAlice->ResetHits();
104     gAlice->TreeH()->GetEvent(track);
105     TClonesArray *Hits  = RICH->Hits();
106     if (Hits == 0) return;
107     Int_t nhits = Hits->GetEntriesFast();
108     if (nhits == 0) continue;
109     Int_t nent=(Int_t)gAlice->TreeD()->GetEntries();
110     gAlice->TreeD()->GetEvent(nent-1);
111     AliRICHHit *mHit = 0;
112     AliRICHDigit *points = 0;
113     //Int_t npoints=0;
114     
115     Int_t counter=0;
116     //Initialization
117     for(i=0;i<dimensiontheta;i++)
118       {
119         for(j=0;j<dimensionphi;j++)
120           {
121             for(k=0;k<dimensionOMEGA;k++)
122               {
123                 counter++;
124                 Point[i][j][k]=0;
125                 //printf("Dimensions theta:%d, phi:%d, omega:%d",dimensiontheta,dimensionphi,dimensionOMEGA);
126                 //printf("Resetting %d %d %d, time %d\n",i,j,k,counter);
127                 //-Noise_Level*(Area(i*Pii/(18*dimension),k*maxOMEGA/dimension)-Area((i-1)*Pii/(18*dimension),(k-1)*maxOMEGA/dimension));
128                 //printf("n-%f",-Noise_Level*(Area(i*Pii/(18*dimension),k*maxOMEGA/dimension)-Area((i-1)*Pii/(18*dimension),(k-1)*maxOMEGA/dimension)));
129               }
130           }
131       }
132     mHit = (AliRICHHit*) Hits->UncheckedAt(0);
133     //printf("Aqui vou eu\n");
134     Int_t nch  = mHit->fChamber;
135     //printf("Aqui fui eu\n");
136     trackglob[0] = mHit->fX;
137     trackglob[1] = mHit->fY;
138     trackglob[2] = mHit->fZ;
139
140     cx=trackglob[0];
141     cy=trackglob[2];
142     
143     
144     //printf("Chamber processed:%d\n",nch);
145     printf("Center processed: %3.1f %3.1f %3.1f\n",trackglob[0],trackglob[1],trackglob[2]);
146
147     iChamber = &(RICH->Chamber(nch-1));
148     
149     //printf("Nch:%d\n",nch);
150
151     iChamber->GlobaltoLocal(trackglob,trackloc);
152     
153     printf("Transformation 1: %3.1f %3.1f %3.1f\n",trackloc[0],trackloc[1],trackloc[2]);
154
155
156     iChamber->LocaltoGlobal(trackloc,trackglob);
157        
158     printf("Transformation 2: %3.1f %3.1f %3.1f\n",trackglob[0],trackglob[1],trackglob[2]);
159     
160     
161      
162
163     TClonesArray *Digits = RICH->DigitsAddress(nch-1);   
164     Int_t ndigits = Digits->GetEntriesFast();
165     
166     //printf("Got %d digits\n",ndigits);
167
168     //printf("Starting calculations\n");
169     
170     for(Float_t theta=0;theta<Pii/18;theta+=steptheta)
171       {                 
172         for(Float_t phi=0;phi<=Pii/3;phi+=stepphi)
173           {                    
174             for (Int_t dig=0;dig<ndigits;dig++)
175               { 
176                 points=(AliRICHDigit*) Digits->UncheckedAt(dig);
177                 
178                 x=points->fPadX-cx;
179                 y=points->fPadY-cy;
180                 //printf("Loaded digit %d with coordinates x:%f, y%f\n",dig,x,y);
181                 //cout<<"x="<<x<<" y="<<y<<endl;
182                 
183                 if (sqrt(pow(x,2)+pow(y,2))<h*tan(theta+maxOMEGA)*3/4)
184                   {
185                     
186                     l=h/cos(theta);
187                     
188                     aux1=-y*sin(phi)+x*cos(phi);
189                     aux2=y*cos(phi)+x*sin(phi);
190                     aux3=( pow(aux1,2)+pow(cos(theta)*aux2 ,2))/pow(sin(theta)*aux2+l,2);
191                     //cout<<"aux1="<<aux1<<" aux2="<<aux2<<" aux3="<<aux3;
192                     
193                     OMEGA=atan(sqrt(aux3));
194                     //printf("Omega: %f\n",OMEGA);
195                     
196                     //cout<<"\ni="<<i<<" theta="<<Int_t(2*theta*dimension/Pii)<<" phi="<<Int_t(2*phi*dimension/Pii)<<" OMEGA="<<Int_t(2*OMEGA*dimension/Pii)<<endl<<endl;
197                     //{Int_t lixo;cin>>lixo;}
198                     if(OMEGA<maxOMEGA)Point[Int_t(2*theta*dimensiontheta/Pii)][Int_t(2*phi*dimensionphi/Pii)][Int_t(OMEGA*dimensionOMEGA/maxOMEGA)]+=1; 
199                     //if(OMEGA<maxOMEGA)Point[Int_t(theta)][Int_t(phi)][Int_t(OMEGA)]+=1;
200                   }
201                 }
202           }
203       } 
204     
205     
206     
207     //SPOT execute twice
208     /*for(s=1;i<=2;s++)
209       {
210         //buffer copy
211         for(i=0;i<=dimensiontheta;i++)
212           for(j=0;j<=dimensionphi;j++)
213             for(k=0;k<=dimensionOMEGA;k++)
214               Point1[i][j][k]=Point[i][j][k];   
215         
216         cout<<"COM SPOT!"<<endl;{Int_t lixo;cin>>lixo;}                                 
217         //SPOT algorithm                        
218         for(i=1;i<dimensiontheta;i++)
219           for(j=1;j<dimensionphi;j++)
220             for(k=1;k<dimensionOMEGA;k++)
221               {
222                 if((Point[i][k][j]>Point[i-1][k][j])&&(Point[i][k][j]>Point[i+1][k][j])&&
223                    (Point[i][k][j]>Point[i][k-1][j])&&(Point[i][k][j]>Point[i][k+1][j])&&
224                    (Point[i][k][j]>Point[i][k][j-1])&&(Point[i][k][j]>Point[i][k][j+1]))
225                   {
226                     //cout<<"SPOT"<<endl;
227                     //Execute SPOT on point                                                                                             
228                     Point1[i][j][k]+=int(SPOTp*(Point[i-1][k][j]+Point[i+1][k][j]+Point[i][k-1][j]+Point[i][k+1][j]+Point[i][k][j-1]+Point[i][k][j+1]));    
229                     Point1[i-1][k][j]=int(SPOTp*Point[i-1][k][j]);
230                     Point1[i+1][k][j]=Int_t(SPOTp*Point[i+1][k][j]);
231                     Point1[i][k-1][j]=Int_t(SPOTp*Point[i][k-1][j]);
232                     Point1[i][k+1][j]=Int_t(SPOTp*Point[i][k+1][j]);
233                     Point1[i][k][j-1]=Int_t(SPOTp*Point[i][k][j-1]);
234                     Point1[i][k][j+1]=Int_t(SPOTp*Point[i][k][j+1]);
235                   }
236               }
237         //copy from buffer copy
238         for(i=1;i<dimensiontheta;i++)
239           for(j=1;j<dimensionphi;j++)
240             for(k=1;k<dimensionOMEGA;k++)
241               Point[i][j][k]=Point1[i][j][k];                                                                           
242           
243           }*/
244     
245     
246     //Identification is equivalent to maximum determination
247     max=0;maxi=0;maxj=0;maxk=0;
248     
249     //cout<<"Proceeding to Identification"<<endl;
250     
251     for(i=1;i<dimensiontheta-3;i++)
252       for(j=1;j<=dimensionphi-3;j++)
253         for(k=0;k<=dimensionOMEGA;k++)
254           if(Point[i][j][k]>max)
255             {
256               //cout<<"maxi="<<i*90/dimension<<" maxj="<<j*90/dimension<<" maxk="<<k*maxOMEGA/dimension*180/Pii<<" max="<<max<<endl;
257               maxi=i;maxj=j;maxk=k;
258               max=Point[i][j][k];
259               //printf("Max Omega %f, Max Theta %f, Max Phi %f\n",maxk,maxi,maxj);
260             }
261     
262     //printf("Detected angle for height %3.1f and for center %3.1f %3.1f:%f\n",h,cx,cy,maxk*Pii/(dimensiontheta*4));
263     //printf("Detected angle for height %3.1f and for center %3.1f %3.1f:%f\n",h,cx,cy,maxk);
264
265
266     //fscanf(omegas,"%f",&realomega);
267     //fscanf(thetas,"%f",&realtheta);
268     //printf("Real Omega: %f",realomega);                       
269     //cout<<"Detected:theta="<<maxi*90/dimensiontheta<<"phi="<<maxj*90/dimensionphi<<"OMEGA="<<maxk*maxOMEGA/dimensionOMEGA*180/Pii<<" OmegaError="<<fabs(maxk*maxOMEGA/dimensionOMEGA*180/Pii-realomega)<<" ThetaError="<<fabs(maxi*90/dimensiontheta-realtheta)<<endl<<endl;          
270     
271     //fprintf(results,"Center Coordinates, cx=%6.2f cy=%6.2f, Real Omega=%6.2f, Detected Omega=%6.2f, Omega Error=%6.2f Theta Error=%6.2f\n",cx,cy,realomega,maxk*maxOMEGA/dimensionOMEGA*180/Pii,fabs(maxk*maxOMEGA/dimensionOMEGA*180/Pii-realomega),fabs(maxi*90/dimensiontheta-realtheta));
272     
273     /*for(j=0;j<np;j++)
274       Pointpp(maxj*90/dimensiontheta,maxi*90/dimensionphi,maxk*maxOMEGA/dimensionOMEGA*180/Pii,cx,cy);//Generates a point on the elipse*/                   
275
276
277     //Start filling rec. hits
278     
279     Float_t rechit[5];
280     
281     rechit[0] = (Float_t)( maxi*Pii/(dimensiontheta*4));
282     rechit[1]   = (Float_t)( maxj*Pii/(dimensionphi*4));
283     rechit[2] = (Float_t)( maxk*Pii/(dimensionOMEGA*4));
284     //rechit[0] = (Float_t)( maxi);
285     //rechit[1]   = (Float_t)( maxj);
286     //rechit[2] = (Float_t)( maxk);
287     rechit[3] = cx;
288     rechit[4] = cy;
289     
290     //printf ("track %d, theta %f, phi %f, omega %f\n\n\n",track,rechit[0],rechit[1],rechit[2]);
291     
292     // fill rechits
293     RICH->AddRecHit(nch-1,rechit);
294   }                     
295   //printf("\n\n\n\n");
296   gAlice->TreeR()->Fill();
297   //TTree *TR=gAlice->TreeR();
298   //Stat_t ndig=TR->GetEntries();
299   TClonesArray *fRec;
300   for (i=0;i<7;i++) {
301     fRec=RICH->RecHitsAddress(i);
302     int ndig=fRec->GetEntriesFast();
303     printf ("Chamber %d, rings %d\n",i,ndig);
304   }
305   //printf("Number of rec. hits: %d",ndig);
306   RICH->ResetRecHits();
307   //char hname[30];
308   //sprintf(hname,"TreeR%d",track);
309   //gAlice->TreeR()->Write(hname);
310         
311 }
312
313 Float_t AliRICHDetect:: Area(Float_t theta,Float_t OMEGA)
314 {
315     
316     Float_t area;
317     const Float_t h=9.25;                       //Distance from Radiator to Pads in pads
318     
319     area=TMath::Pi()*pow(h*tan(OMEGA),2)/pow(pow(cos(theta),2)-pow(tan(OMEGA)*sin(theta),2),3/2);
320     
321     return (area);
322 }
323
324 /*Int_t ***AliRICHDetect::i3tensor(long nrl, long nrh, long ncl, long nch, long ndl, long ndh)
325 // allocate a Float_t 3tensor with range t[nrl..nrh][ncl..nch][ndl..ndh] 
326 {
327 long i,j,nrow=nrh-nrl+1,ncol=nch-ncl+1,ndep=ndh-ndl+1;
328 Int_t ***t;
329
330 // allocate pointers to pointers to rows 
331 t=(Int_t ***) malloc((size_t)((nrow+NR_END)*sizeof(Int_t**)));
332 if (!t) printf("allocation failure 1 in f3tensor()");
333 t += NR_END;
334 t -= nrl;
335
336 // allocate pointers to rows and set pointers to them 
337 t[nrl]=(Int_t **) malloc((size_t)((nrow*ncol+NR_END)*sizeof(Int_t*)));
338 if (!t[nrl]) printf("allocation failure 2 in f3tensor()");
339 t[nrl] += NR_END;
340 t[nrl] -= ncl;
341
342 // allocate rows and set pointers to them 
343 t[nrl][ncl]=(Int_t *) malloc((size_t)((nrow*ncol*ndep+NR_END)*sizeof(Int_t)));
344 if (!t[nrl][ncl]) printf("allocation failure 3 in f3tensor()");
345 t[nrl][ncl] += NR_END;
346 t[nrl][ncl] -= ndl;
347
348 for(j=ncl+1;j<=nch;j++) t[nrl][j]=t[nrl][j-1]+ndep;
349 for(i=nrl+1;i<=nrh;i++) {
350 t[i]=t[i-1]+ncol;
351 t[i][ncl]=t[i-1][ncl]+ncol*ndep;
352 for(j=ncl+1;j<=nch;j++) t[i][j]=t[i][j-1]+ndep;
353 }
354
355 // return pointer to array of pointers to rows 
356 return t;
357 }*/
358
359 /*void Pointpp(Float_t alfa,Float_t theta,Float_t OMEGA,Float_t cx,Float_t cy)
360   {
361   Int_t s;
362   Float_t fiducial=h*tan((OMEGA+theta)*Pii/180),l=h/cos(theta*Pii/180),xtrial,y,c0,c1,c2;
363   
364   //cout<<"fiducial="<<fiducial<<endl;
365   
366   c0=0;c1=0;c2=0;
367   while((c1*c1-4*c2*c0)<=0)
368   {     
369   //Choose which side to go...
370   if(aleat(1)>.5) s=1; else s=-1;
371   //Trial a y
372   y=s*aleat(fiducial);          
373   Float_t alfa1=alfa*Pii/180;
374   Float_t theta1=theta*Pii/180;
375   Float_t OMEGA1=OMEGA*Pii/180;
376   //Solve the eq for a trial x
377   c0=-pow(y*cos(alfa1)*cos(theta1),2)-pow(y*sin(alfa1),2)+pow(l*tan(OMEGA1),2)+2*l*y*cos(alfa1)*sin(theta1)*pow(tan(OMEGA1),2)+pow(y*cos(alfa1)*sin(theta1)*tan(OMEGA1),2);
378   c1=2*y*cos(alfa1)*sin(alfa1)-2*y*cos(alfa1)*pow(cos(theta1),2)*sin(alfa1)+2*l*sin(alfa1)*sin(theta1)*pow(tan(OMEGA1),2)+2*y*cos(alfa1)*sin(alfa1)*pow(sin(theta1),2)*pow(tan(OMEGA1),2);
379   c2=-pow(cos(alfa1),2)-pow(cos(theta1)*sin(alfa1),2)+pow(sin(alfa1)*sin(theta1)*tan(OMEGA1),2);
380   //cout<<"Trial: y="<<y<<"c0="<<c0<<" c1="<<c1<<" c2="<<c2<<endl;
381   }
382   //Choose which side to go...
383   if(aleat(1)>.5) s=1; else s=-1;
384   xtrial=cx+(-c1+s*sqrt(c1*c1-4*c2*c0))/(2*c2);
385   //cout<<"x="<<xtrial<<" y="<<cy+y<<endl;
386   fprintf(final,"%f %f\n",xtrial,cy+y);
387   }*/
388
389
390
391
392
393