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