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