42d8cfa8623ddd641d7a8573dd6a6decf82e9f28
[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.12  2001/02/27 22:15:03  jbarbosa
19   Removed compiler warning.
20
21   Revision 1.11  2001/02/27 15:21:46  jbarbosa
22   Transition to SDigits.
23
24   Revision 1.10  2001/02/13 20:39:06  jbarbosa
25   Changes to make it work with new IO.
26
27   Revision 1.9  2001/01/22 21:39:11  jbarbosa
28   Several tune-ups
29
30   Revision 1.8  2000/11/15 15:52:53  jbarbosa
31   Turned on spot algorithm.
32
33   Revision 1.7  2000/11/01 15:37:05  jbarbosa
34   Updated to use its own rec. point object.
35
36   Revision 1.6  2000/10/02 21:28:12  fca
37   Removal of useless dependecies via forward declarations
38
39   Revision 1.5  2000/06/30 16:30:28  dibari
40   Disabled writing to rechits.
41
42   Revision 1.4  2000/06/15 15:46:59  jbarbosa
43   Corrected compilation errors on HP-UX (replaced pow with TMath::Power)
44
45   Revision 1.3  2000/06/13 13:15:41  jbarbosa
46   Still some code cleanup done (variable names)
47
48   Revision 1.2  2000/06/12 15:19:30  jbarbosa
49   Cleaned up version.
50
51   Revision 1.1  2000/04/19 13:05:14  morsch
52   J. Barbosa's spot reconstruction algorithm.
53
54 */
55
56
57 #include "AliRICH.h"
58 #include "AliRICHPoints.h"
59 #include "AliRICHDetect.h"
60 #include "AliRICHHit.h"
61 #include "AliRICHDigit.h"
62 #include "AliRICHSegmentationV0.h"
63 #include "AliRun.h"
64 #include "TParticle.h"
65 #include "TTree.h"
66 #include "TMath.h"
67 #include "TRandom.h"
68 #include "TH3.h"
69 #include "TH2.h"
70 #include "TCanvas.h"
71
72 #include "malloc.h"
73
74
75 ClassImp(AliRICHDetect)
76 //___________________________________________
77 AliRICHDetect::AliRICHDetect() : TObject()
78 {
79
80 // Default constructor 
81
82 }
83
84 //___________________________________________
85 AliRICHDetect::AliRICHDetect(const char *name, const char *title)
86     : TObject()
87 {
88
89
90   fc1= new TCanvas("c1","Reconstructed points",50,50,300,350);
91   fc1->Divide(2,2);
92   fc2= new TCanvas("c2","Reconstructed points after SPOT",50,50,300,350);
93   fc2->Divide(2,2); 
94   fc3= new TCanvas("c3","Used Digits",50,50,300,350);
95   //fc3->Divide(2,1); 
96
97 }
98
99 //___________________________________________
100 AliRICHDetect::~AliRICHDetect()
101 {
102     
103 // Destructor
104
105 }
106
107
108 void AliRICHDetect::Detect(Int_t nev)
109 {       
110     
111 //
112 // Detection algorithm
113
114
115   //printf("Detection started!\n");
116   Float_t omega,omega1,theta1,steptheta,stepphi,x,y,z,cx,cy,l,aux1,aux2,aux3,max,radius=0,meanradius=0;
117   Int_t maxi,maxj,maxk;
118   //Float_t theta,phi,realomega,realtheta;
119   Float_t binomega, bintheta, binphi;
120   Int_t intomega, inttheta, intphi;
121   Int_t i,j,k;
122
123   AliRICH *pRICH  = (AliRICH*)gAlice->GetDetector("RICH");
124   AliRICHSegmentationV0*  segmentation;
125   AliRICHChamber*       iChamber;
126   AliRICHGeometry*  geometry;
127   
128   iChamber = &(pRICH->Chamber(0));
129   segmentation=(AliRICHSegmentationV0*) iChamber->GetSegmentationModel(0);
130   geometry=iChamber->GetGeometryModel();
131  
132   
133   //const Float_t Noise_Level=0;                       //Noise Level in percentage of mesh points
134   //const Float_t t=0.6;                               //Softening of Noise Correction (factor)
135   
136   const Float_t kPi=TMath::Pi();                
137   
138   const Float_t kHeight=geometry->GetRadiatorToPads(); //Distance from Radiator to Pads in centimeters
139   //printf("Distance to Pads:%f\n",kHeight);
140  
141   const Int_t kSpot=0;                                 //number of passes with spot algorithm
142   
143   const Int_t kDimensionTheta=30;                      //Matrix dimension for angle Detection
144   const Int_t kDimensionPhi=45;
145   const Int_t kDimensionOmega=100;
146   
147   const Float_t SPOTp=1;                              //Percentage of spot action
148   const Float_t kMinOmega=20*kPi/180;
149   const Float_t kMaxOmega=70*kPi/180;                 //Maximum Cherenkov angle to identify
150   const Float_t kMinTheta=0;
151   const Float_t kMaxTheta=15*kPi/180;   
152   //const Float_t kMaxTheta=0.1;
153   const Float_t kMinPhi=0;
154   const Float_t kMaxPhi=360*kPi/180;
155
156  
157   Float_t kCorr=0.61;                              //Correction factor, accounting for aberration, refractive index, etc.
158   //const Float_t kCorr=.9369;                        //from 0 incidence  
159   //const Float_t kCorr=1;
160
161   //TRandom* random=0;
162
163   Float_t rechit[6];                                 //Reconstructed point data
164
165   
166
167   //printf("Creating matrices\n");
168   //Float_t point[kDimensionTheta][kDimensionPhi][kDimensionOmega];
169   //Float_t point1[kDimensionTheta][kDimensionPhi][kDimensionOmega];
170   //printf("Created matrices\n");
171
172   Int_t ***point = i3tensor(0,kDimensionTheta,0,kDimensionPhi,0,kDimensionOmega);
173   Int_t ***point1 = i3tensor(0,kDimensionTheta,0,kDimensionPhi,0,kDimensionOmega);
174   
175   //Int_t **point  = new Int_t[kDimensionTheta][kDimensionPhi][kDimensionOmega];
176   //Int_t **point1 = new Int_t[kDimensionTheta][kDimensionPhi][kDimensionOmega];
177
178   steptheta=(kMaxTheta-kMinTheta)/kDimensionTheta;
179   stepphi=(kMaxPhi-kMinPhi)/kDimensionPhi;
180
181   static TH3F *Points = new TH3F("Points","Reconstructed points 3D",kDimensionTheta,0,kDimensionTheta,kDimensionPhi,0,kDimensionPhi,kDimensionOmega,0,kDimensionOmega);
182   static TH2F *ThetaPhi = new TH2F("ThetaPhi","Theta-Phi projection",kDimensionTheta,0,kDimensionTheta,kDimensionPhi,0,kDimensionPhi);
183   static TH2F *OmegaTheta = new TH2F("OmegaTheta","Omega-Theta projection",kDimensionTheta,0,kDimensionTheta,kDimensionOmega,0,kDimensionOmega);
184   static TH2F *OmegaPhi = new TH2F("OmegaPhi","Omega-Phi projection",kDimensionPhi,0,kDimensionPhi,kDimensionOmega,0,kDimensionOmega);
185   static TH3F *SpotPoints = new TH3F("Points","Reconstructed points 3D, spot",kDimensionTheta,0,kDimensionTheta,kDimensionPhi,0,kDimensionPhi,kDimensionOmega,0,kDimensionOmega);
186   static TH2F *SpotThetaPhi = new TH2F("ThetaPhi","Theta-Phi projection, spot",kDimensionTheta,0,kDimensionTheta,kDimensionPhi,0,kDimensionPhi);
187   static TH2F *SpotOmegaTheta = new TH2F("OmegaTheta","Omega-Theta projection, spot",kDimensionTheta,0,kDimensionTheta,kDimensionOmega,0,kDimensionOmega);
188   static TH2F *SpotOmegaPhi = new TH2F("OmegaPhi","Omega-Phi projection, spot",kDimensionPhi,0,kDimensionPhi,kDimensionOmega,0,kDimensionOmega);
189   static TH2F *DigitsXY = new TH2F("DigitsXY","Pads used for reconstruction",150,-25,25,150,-25,25);
190   Points->SetXTitle("theta");
191   Points->SetYTitle("phi");
192   Points->SetZTitle("omega");
193   ThetaPhi->SetXTitle("theta");
194   ThetaPhi->SetYTitle("phi");
195   OmegaTheta->SetXTitle("theta");
196   OmegaTheta->SetYTitle("omega");
197   OmegaPhi->SetXTitle("phi");
198   OmegaPhi->SetYTitle("omega");
199   SpotPoints->SetXTitle("theta");
200   SpotPoints->SetYTitle("phi");
201   SpotPoints->SetZTitle("omega");
202   SpotThetaPhi->SetXTitle("theta");
203   SpotThetaPhi->SetYTitle("phi");
204   SpotOmegaTheta->SetXTitle("theta");
205   SpotOmegaTheta->SetYTitle("omega");
206   SpotOmegaPhi->SetXTitle("phi");
207   SpotOmegaPhi->SetYTitle("omega");
208
209   Int_t ntracks = (Int_t)gAlice->TreeH()->GetEntries();
210   //Int_t ntrks = gAlice->GetNtrack();
211   
212   Float_t trackglob[3];
213   Float_t trackloc[3];
214
215   //printf("Area de uma elipse com teta 0 e Omega 45:%f",Area(0,45));
216     
217   Int_t track;
218         
219   for (track=0; track<ntracks;track++) {
220     gAlice->ResetHits();
221     gAlice->TreeH()->GetEvent(track);
222     TClonesArray *pHits  = pRICH->Hits();
223     if (pHits == 0) return;
224     Int_t nhits = pHits->GetEntriesFast();
225     if (nhits == 0) continue;
226     //Int_t nent=(Int_t)gAlice->TreeD()->GetEntries();
227     gAlice->TreeD()->GetEvent(0);
228     AliRICHHit *mHit = 0;
229     AliRICHDigit *points = 0;
230     //Int_t npoints=0;
231     
232     Int_t counter=0, counter1=0;
233     //Initialization
234     for(i=0;i<kDimensionTheta;i++)
235       {
236         for(j=0;j<kDimensionPhi;j++)
237           {
238             for(k=0;k<kDimensionOmega;k++)
239               {
240                 counter++;
241                 point[i][j][k]=0;
242                 //printf("Dimensions theta:%d, phi:%d, omega:%d",kDimensionTheta,kDimensionPhi,kDimensionOmega);
243                 //printf("Resetting %d %d %d, time %d\n",i,j,k,counter);
244                 //-Noise_Level*(Area(i*kPi/(18*dimension),k*kMaxOmega/dimension)-Area((i-1)*kPi/(18*dimension),(k-1)*kMaxOmega/dimension));
245                 //printf("n-%f",-Noise_Level*(Area(i*kPi/(18*dimension),k*kMaxOmega/dimension)-Area((i-1)*kPi/(18*dimension),(k-1)*kMaxOmega/dimension)));
246               }
247           }
248       }
249     mHit = (AliRICHHit*) pHits->UncheckedAt(0);
250     //printf("Aqui vou eu\n");
251     Int_t nch  = mHit->fChamber;
252     //printf("Aqui fui eu\n");
253     trackglob[0] = mHit->X();
254     trackglob[1] = mHit->Y();
255     trackglob[2] = mHit->Z();
256
257     printf("Chamber processed:%d\n",nch);
258
259     printf("Reconstructing particle at (global coordinates): %3.1f %3.1f %3.1f,\n",trackglob[0],trackglob[1],trackglob[2]);
260
261     iChamber = &(pRICH->Chamber(nch-1));
262     
263     //printf("Nch:%d\n",nch);
264
265     iChamber->GlobaltoLocal(trackglob,trackloc);
266     
267     printf("Reconstructing particle at (local coordinates) : %3.1f %3.1f %3.1f\n",trackloc[0],trackloc[1],trackloc[2]);
268
269
270     iChamber->LocaltoGlobal(trackloc,trackglob);
271        
272     //printf("Transformation 2: %3.1f %3.1f %3.1f\n",trackglob[0],trackglob[1],trackglob[2]);
273     
274     cx=trackloc[0];
275     cy=trackloc[2];
276      
277
278     TClonesArray *pDigits = pRICH->DigitsAddress(nch-1);   
279     Int_t ndigits = pDigits->GetEntriesFast();
280     
281     //printf("Got %d digits\n",ndigits);
282
283     counter=0;
284     printf("Starting calculations\n");
285     for(Float_t theta=0;theta<kMaxTheta;theta+=steptheta)
286       {         
287         //printf(".");
288         for(Float_t phi=0;phi<=kMaxPhi;phi+=stepphi)
289           {             
290             //printf("Phi:%3.1f\n", phi*180/kPi);
291             counter1=0;
292             for (Int_t dig=0;dig<ndigits;dig++)
293               { 
294                 points=(AliRICHDigit*) pDigits->UncheckedAt(dig);
295                 segmentation->GetPadC(points->fPadX, points->fPadY,x, y, z);
296                 x=x-cx;
297                 y=y-cy;
298                 radius=TMath::Sqrt(TMath::Power(x,2)+TMath::Power(y,2));
299
300                 if(radius>4)
301                   {
302                     //if(theta==0 && phi==0)
303                       //{
304                         //printf("Radius: %f, Max Radius: %f\n",radius,kCorr*kHeight*tan(theta+kMaxOmega)*3/4);
305                         meanradius+=radius;
306                         counter++;
307                       //}
308                     
309                     if (radius<2*kHeight*tan(theta+kMaxOmega)*3/4)
310                       {
311                         
312                         if(phi==0)
313                           {
314                             //printf("Radius: %f, Max Radius: %f\n",radius,2*kHeight*tan(theta+kMaxOmega)*3/4);
315                             //printf("Loaded digit %d with coordinates x:%f, y%f\n",dig,x,y);
316                             //printf("Using digit %d, for theta %f\n",dig,theta);
317                           }
318                         
319                         counter1++;
320
321                         l=kHeight/cos(theta);
322                         
323                         //x=x*kCorr;
324                         //y=y*kCorr;
325                         /*if(SnellAngle(theta+omega)<999)
326                           {
327                             //printf("(Angle)/(Snell angle):%f\n",(theta+omega)/SnellAngle(theta+omega));
328                             x=x*(theta+omega)/SnellAngle(theta+omega);
329                             y=y*(theta+omega)/SnellAngle(theta+omega);
330                           }
331                         else
332                           {
333                             x=0;
334                             y=0;
335                           }*/
336
337                         //main calculation
338
339                         DigitsXY->Fill(x,y,(float) 1);
340
341                         theta1=SnellAngle(theta)*1.5;
342                 
343                         aux1=-y*sin(phi)+x*cos(phi);
344                         aux2=y*cos(phi)+x*sin(phi);
345                         aux3=( TMath::Power(aux1,2)+TMath::Power(cos(theta1)*aux2 ,2))/TMath::Power(sin(theta1)*aux2+l,2);
346                         omega=atan(sqrt(aux3));
347                         
348                         //omega is distorted, theta1 is distorted
349
350                         if(InvSnellAngle(theta+omega)<999)
351                           {
352                             omega1=InvSnellAngle(omega+theta1) - theta;
353                             //theta1=InvSnellAngle(omega+theta) - omega1;
354                             //omega1=kCorr*omega;
355                             
356                             kCorr=InvSnellAngle(omega+theta)/(omega+theta);
357                             theta1=kCorr*theta/1.4;
358                             //if(phi==0)
359                               //printf("Omega:%f Theta:%f Omega1:%f Theta1:%f ISA(o+t):%f ISA(t):%f\n",omega*180/kPi,theta*180/kPi,omega1*180/kPi,theta1*180/kPi,InvSnellAngle(omega+theta)*180/kPi,InvSnellAngle(theta)*180/kPi);
360                           }
361                         else
362                           {
363                             omega1=0;
364                             theta1=0;
365                           }
366                         
367                         //printf("Omega:%f\n",omega);
368
369
370                         //if(SnellAngle(theta+omega)<999)
371                           //printf("(Angle)/(Snell angle):%f\n",(theta+omega)/SnellAngle(theta+omega));
372                         if(theta==0 && phi==0)
373                           {
374                             //printf("Omega: %f Corrected Omega: %f\n",omega, omega/kCorr);
375                             //omega=omega/kCorr;
376                           }
377                         
378                         //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;
379                         //{Int_t lixo;cin>>lixo;}
380                         if(omega1<kMaxOmega && omega1>kMinOmega)
381                           {
382                             //printf("Omega found:%f\n",omega);
383                             omega1=omega1-kMinOmega;
384                             
385                             //printf("Omega: %f Theta: %3.1f Phi:%3.1f\n",omega, theta*180/kPi, phi*180/kPi);
386
387                             bintheta=theta1*kDimensionTheta/kMaxTheta;
388                             binphi=phi*kDimensionPhi/kMaxPhi;
389                             binomega=omega1*kDimensionOmega/(kMaxOmega-kMinOmega);
390
391                             if(Int_t(bintheta+0.5)==Int_t(bintheta))
392                               inttheta=Int_t(bintheta);
393                             else
394                               inttheta=Int_t(bintheta+0.5);
395
396                             if(Int_t(binomega+0.5)==Int_t(binomega))
397                               intomega=Int_t(binomega);
398                             else
399                               intomega=Int_t(binomega+0.5);
400                             
401                             if(Int_t(binphi+0.5)==Int_t(binphi))
402                               intphi=Int_t(binphi);
403                             else
404                               intphi=Int_t(binphi+0.5);
405                                                  
406                             //printf("Point added at %d %d %d\n",inttheta,intphi,intomega);
407                             point[inttheta][intphi][intomega]+=1;
408                             //printf("Omega stored:%d\n",intomega);
409                             Points->Fill(inttheta,intphi,intomega,(float) 1);
410                             ThetaPhi->Fill(inttheta,intphi,(float) 1);
411                             OmegaTheta->Fill(inttheta,intomega,(float) 1);
412                             OmegaPhi->Fill(intphi,intomega,(float) 1);
413                             //printf("Filling at %d %d %d\n",Int_t(theta*kDimensionTheta/kMaxTheta),Int_t(phi*kDimensionPhi/kMaxPhi),Int_t(omega*kDimensionOmega/kMaxOmega));
414                           }
415                         //if(omega<kMaxOmega)point[Int_t(theta)][Int_t(phi)][Int_t(omega)]+=1;
416                       }
417                   }
418               }
419           }
420         //printf("Used %d digits for theta %3.1f\n",counter1, theta*180/kPi);
421       }
422
423     meanradius=meanradius/counter;
424     printf("Mean radius:%f, counter:%d\n",meanradius,counter);
425     rechit[5]=meanradius;
426     printf("Used %d digits\n",counter1);
427     //printf("\n");
428
429     if(nev<20)
430       {
431         if(nev==0)
432           {
433             fc1->cd(1);
434             Points->Draw();
435             fc1->cd(2);
436             ThetaPhi->Draw();
437             fc1->cd(3);
438             OmegaTheta->Draw();
439             fc1->cd(4);
440             OmegaPhi->Draw();
441             fc3->cd();
442             DigitsXY->Draw();
443           }
444         else
445           {
446             //fc1->cd(1);
447             //Points->Draw("same");
448             //fc1->cd(2);
449             //ThetaPhi->Draw("same");
450             //fc1->cd(3);
451             //OmegaTheta->Draw("same");
452             //fc1->cd(4);
453             //OmegaPhi->Draw("same");   
454           }
455       }
456         
457     
458     //SPOT execute twice
459     for(Int_t s=0;s<kSpot;s++)
460       {
461         printf("     Applying Spot algorithm, pass %d\n", s);
462         
463         //buffer copy
464         for(i=0;i<=kDimensionTheta;i++)
465           {
466             for(j=0;j<=kDimensionPhi;j++)
467               {
468                 for(k=0;k<=kDimensionOmega;k++)
469                   {
470                     point1[i][j][k]=point[i][j][k];     
471                   }
472               }
473           }
474
475         //SPOT algorithm                        
476         for(i=1;i<kDimensionTheta-1;i++)
477           {
478             for(j=1;j<kDimensionPhi-1;j++)
479               {
480                 for(k=1;k<kDimensionOmega-1;k++)
481                   {
482                     if((point[i][k][j]>point[i-1][k][j])&&(point[i][k][j]>point[i+1][k][j])&&
483                        (point[i][k][j]>point[i][k-1][j])&&(point[i][k][j]>point[i][k+1][j])&&
484                        (point[i][k][j]>point[i][k][j-1])&&(point[i][k][j]>point[i][k][j+1]))
485                       {
486                         //cout<<"SPOT"<<endl;
487                         //Execute SPOT on point                                                                                         
488                         point1[i][j][k]+=Int_t(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]));    
489                         point1[i-1][k][j]=Int_t(SPOTp*point[i-1][k][j]);
490                         point1[i+1][k][j]=Int_t(SPOTp*point[i+1][k][j]);
491                         point1[i][k-1][j]=Int_t(SPOTp*point[i][k-1][j]);
492                         point1[i][k+1][j]=Int_t(SPOTp*point[i][k+1][j]);
493                         point1[i][k][j-1]=Int_t(SPOTp*point[i][k][j-1]);
494                         point1[i][k][j+1]=Int_t(SPOTp*point[i][k][j+1]);
495                       }
496                   }
497               }
498           }
499         
500         //copy from buffer copy
501         counter1=0;
502         for(i=1;i<kDimensionTheta;i++)
503           {
504             for(j=1;j<kDimensionPhi;j++)
505               {
506                 for(k=1;k<kDimensionOmega;k++)
507                   {
508                     point[i][j][k]=point1[i][j][k];
509                     if(nev<20)
510                       {
511                         if(s==kSpot-1)
512                           {
513                             if(point1[i][j][k] != 0)
514                               {
515                                 SpotPoints->Fill(i,j,k,(float) point1[i][j][k]);
516                                 //printf("Random number %f\n",random->Rndm(2));
517                                 //if(random->Rndm() < .2)
518                                   //{
519                                 SpotThetaPhi->Fill(i,j,(float) point1[i][j][k]);
520                                 SpotOmegaTheta->Fill(i,k,(float) point1[i][j][k]);
521                                 SpotOmegaPhi->Fill(j,k,(float) point1[i][j][k]);
522                                     counter1++;
523                                   //}
524                                 //printf("Filling at %d %d %d value %f\n",i,j,k,(float) point1[i][j][k]);
525                               }
526                           }
527                       }
528                     //if(point1[i][j][k] != 0)
529                       //printf("Last transfer point: %d, point1, %d\n",point[i][j][k],point1[i][j][k]);
530                   }
531               }
532           }
533       }
534     
535     //printf("Filled %d cells\n",counter1);
536
537     if(nev<20)
538       {
539         if(nev==0)
540           {
541             fc2->cd(1);
542             SpotPoints->Draw();
543             fc2->cd(2);
544             SpotThetaPhi->Draw();
545             fc2->cd(3);
546             SpotOmegaTheta->Draw();
547             fc2->cd(4);
548             SpotOmegaPhi->Draw();
549           }
550         else
551           {
552             //fc2->cd(1);
553             //SpotPoints->Draw("same");
554             //fc2->cd(2);
555             //SpotThetaPhi->Draw("same");
556             //fc2->cd(3);
557             //SpotOmegaTheta->Draw("same");
558             //fc2->cd(4);
559             //SpotOmegaPhi->Draw("same");       
560           }
561       }
562     
563     
564     //Identification is equivalent to maximum determination
565     max=0;maxi=0;maxj=0;maxk=0;
566     
567     printf("     Proceeding to identification");
568     
569     for(i=0;i<kDimensionTheta;i++)
570       for(j=0;j<kDimensionPhi;j++)
571         for(k=0;k<kDimensionOmega;k++)
572             if(point[i][j][k]>max)
573               {
574                 //cout<<"maxi="<<i*90/dimension<<" maxj="<<j*90/dimension<<" maxk="<<k*kMaxOmega/dimension*180/kPi<<" max="<<max<<endl;
575                 maxi=i;maxj=j;maxk=k;
576                 max=point[i][j][k];
577                 printf(".");
578                 //printf("Max Omega %d, Max Theta %d, Max Phi %d (%d counts)\n",maxk,maxi,maxj,max);
579               }
580     printf("\n");
581     
582     Float_t FinalOmega = maxk*(kMaxOmega-kMinOmega)/kDimensionOmega; 
583     Float_t FinalTheta = maxi*kMaxTheta/kDimensionTheta;
584     Float_t FinalPhi = maxj*kMaxPhi/kDimensionPhi;
585
586     FinalOmega += kMinOmega;
587     
588     //printf("Detected angle for height %3.1f and for center %3.1f %3.1f:%f\n",h,cx,cy,maxk*kPi/(kDimensionTheta*4));
589     printf("     Indentified angles: cerenkov - %f, theta - %3.1f, phi - %3.1f (%f activation)\n", FinalOmega, FinalTheta*180/kPi, FinalPhi*180/kPi, max);
590     //printf("Detected angle for height %3.1f and for center %3.1f %3.1f:%f\n",kHeight,cx,cy,maxk);
591
592     //fscanf(omegas,"%f",&realomega);
593     //fscanf(thetas,"%f",&realtheta);
594     //printf("Real Omega: %f",realomega);                       
595     //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;           
596     
597     //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));
598     
599     /*for(j=0;j<np;j++)
600       pointpp(maxj*90/kDimensionTheta,maxi*90/kDimensionPhi,maxk*kMaxOmega/kDimensionOmega*180/kPi,cx,cy);//Generates a point on the elipse*/               
601
602
603     //Start filling rec. hits
604     
605     rechit[0] = FinalTheta;
606     rechit[1] = 90*kPi/180 + FinalPhi;
607     rechit[2] = FinalOmega;
608     rechit[3] = cx;
609     rechit[4] = cy;
610
611     //CreatePoints(FinalTheta, 270*kPi/180 + FinalPhi, FinalOmega, kHeight);
612        
613     //printf ("track %d, theta %f, phi %f, omega %f\n\n\n",track,rechit[0],rechit[1],rechit[2]);
614     
615     // fill rechits
616     pRICH->AddRecHit3D(nch-1,rechit);
617     //printf("rechit %f %f %f %f %f\n",rechit[0],rechit[1],rechit[2],rechit[3],rechit[4]);
618     //printf("Chamber:%d",nch);
619   }                     
620   //printf("\n\n\n\n");
621   gAlice->TreeR()->Fill();
622   TClonesArray *fRec;
623   for (i=0;i<kNCH;i++) {
624     fRec=pRICH->RecHitsAddress3D(i);
625     int ndig=fRec->GetEntriesFast();
626     printf ("Chamber %d, rings %d\n",i+1,ndig);
627   }
628   pRICH->ResetRecHits3D();
629
630   free_i3tensor(point,0,kDimensionTheta,0,kDimensionPhi,0,kDimensionOmega);
631   free_i3tensor(point1,0,kDimensionTheta,0,kDimensionPhi,0,kDimensionOmega);
632 }
633
634
635
636 Float_t AliRICHDetect:: Area(Float_t theta,Float_t omega)
637 {
638
639 //
640 // Calculates area of an ellipse for given incidence angles    
641
642
643     Float_t area;
644     const Float_t kHeight=9.25;                       //Distance from Radiator to Pads in pads
645     
646     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);
647     
648     return (area);
649 }
650
651
652 Int_t ***AliRICHDetect::i3tensor(long nrl, long nrh, long ncl, long nch, long ndl, long ndh)
653 // allocate a Int_t 3tensor with range t[nrl..nrh][ncl..nch][ndl..ndh] 
654 {
655   long i,j,nrow=nrh-nrl+1,ncol=nch-ncl+1,ndep=ndh-ndl+1;
656   Int_t ***t;
657   
658   int NR_END=1; 
659
660   // allocate pointers to pointers to rows 
661   t=(Int_t ***) malloc((size_t)((nrow+NR_END)*sizeof(Int_t**)));
662   if (!t) printf("allocation failure 1 in f3tensor()");
663   t += NR_END;
664   t -= nrl;
665   
666   // allocate pointers to rows and set pointers to them 
667   t[nrl]=(Int_t **) malloc((size_t)((nrow*ncol+NR_END)*sizeof(Int_t*)));
668   if (!t[nrl]) printf("allocation failure 2 in f3tensor()");
669   t[nrl] += NR_END;
670   t[nrl] -= ncl;
671   
672   // allocate rows and set pointers to them 
673   t[nrl][ncl]=(Int_t *) malloc((size_t)((nrow*ncol*ndep+NR_END)*sizeof(Int_t)));
674   if (!t[nrl][ncl]) printf("allocation failure 3 in f3tensor()");
675   t[nrl][ncl] += NR_END;
676   t[nrl][ncl] -= ndl;
677   
678   for(j=ncl+1;j<=nch;j++) t[nrl][j]=t[nrl][j-1]+ndep;
679   for(i=nrl+1;i<=nrh;i++) {
680     t[i]=t[i-1]+ncol;
681     t[i][ncl]=t[i-1][ncl]+ncol*ndep;
682     for(j=ncl+1;j<=nch;j++) t[i][j]=t[i][j-1]+ndep;
683   }
684   
685   // return pointer to array of pointers to rows 
686   return t;
687 }
688
689 void AliRICHDetect::free_i3tensor(int ***t, long nrl, long nrh, long ncl, long nch,long ndl, long ndh)
690 // free a Int_t f3tensor allocated by i3tensor()
691 {
692   int NR_END=1; 
693
694   free((char*) (t[nrl][ncl]+ndl-NR_END));
695   free((char*) (t[nrl]+ncl-NR_END));
696   free((char*) (t+nrl-NR_END));
697 }
698
699
700 Float_t AliRICHDetect:: SnellAngle(Float_t iangle)
701
702
703 // Compute the Snell angle
704
705   Float_t nfreon  = 1.295;
706   Float_t nquartz = 1.585;
707   Float_t ngas = 1;
708
709   Float_t sinrangle;
710   Float_t rangle;
711   Float_t a1, a2;
712
713   a1=nfreon/nquartz;
714   a2=nquartz/ngas;
715
716   sinrangle = a1*a2*sin(iangle);
717
718   if(sinrangle>1.) {
719      rangle = 999.;
720      return rangle;
721   }
722   
723   rangle = asin(sinrangle);  
724   return rangle;
725 }
726
727 Float_t AliRICHDetect:: InvSnellAngle(Float_t rangle)
728
729
730 // Compute the inverse Snell angle
731
732   Float_t nfreon  = 1.295;
733   Float_t nquartz = 1.585;
734   Float_t ngas = 1;
735
736   Float_t siniangle;
737   Float_t iangle;
738   Float_t a1,a2;
739
740   a1=nfreon/nquartz;
741   a2=nquartz/ngas;
742
743   siniangle = sin(rangle)/(a1*a2);
744   iangle = asin(siniangle);
745
746   if(siniangle>1.) {
747      iangle = 999.;
748      return iangle;
749   }
750   
751   iangle = asin(siniangle);
752   return iangle;
753 }
754
755
756
757 //________________________________________________________________________________
758 void AliRICHDetect::CreatePoints(Float_t theta, Float_t phi, Float_t omega, Float_t h)
759 {
760   
761   // Create points along the ellipse equation
762   
763   Int_t s1,s2;
764   Float_t fiducial=h*TMath::Tan(omega+theta), l=h/TMath::Cos(theta), xtrial, y=0, c0, c1, c2;
765   //TRandom *random=new TRandom();
766   
767   static TH2F *REllipse = new TH2F("REllipse","Reconstructed ellipses",150,-25,25,150,-25,25);
768
769   for(Float_t i=0;i<1000;i++)
770     {
771       
772       Float_t counter=0;
773       
774       c0=0;c1=0;c2=0;
775       while((c1*c1-4*c2*c0)<=0 && counter<1000)
776         {
777           //Choose which side to go...
778           if(i>250 && i<750) s1=1; 
779           //if (gRandom->Rndm(1)>.5) s1=1;
780           else s1=-1;
781           //printf("s1:%d\n",s1);
782           //Trial a y
783           y=s1*i*gRandom->Rndm(Int_t(fiducial/50));
784           //printf("Fiducial %f  for omega:%f theta:%f phi:%f\n",fiducial,omega,theta,fphi);
785           Float_t alfa1=theta;
786           Float_t theta1=phi;
787           Float_t omega1=omega;
788           
789           //Solve the eq for a trial x
790           c0=-TMath::Power(y*TMath::Cos(alfa1)*TMath::Cos(theta1),2)-TMath::Power(y*TMath::Sin(alfa1),2)+TMath::Power(l*TMath::Tan(omega1),2)+2*l*y*TMath::Cos(alfa1)*TMath::Sin(theta1)*TMath::Power(TMath::Tan(omega1),2)+TMath::Power(y*TMath::Cos(alfa1)*TMath::Sin(theta1)*TMath::Tan(omega1),2);
791           c1=2*y*TMath::Cos(alfa1)*TMath::Sin(alfa1)-2*y*TMath::Cos(alfa1)*TMath::Power(TMath::Cos(theta1),2)*TMath::Sin(alfa1)+2*l*TMath::Sin(alfa1)*TMath::Sin(theta1)*TMath::Power(TMath::Tan(omega1),2)+2*y*TMath::Cos(alfa1)*TMath::Sin(alfa1)*TMath::Power(TMath::Sin(theta1),2)*TMath::Power(TMath::Tan(omega1),2);
792           c2=-TMath::Power(TMath::Cos(alfa1),2)-TMath::Power(TMath::Cos(theta1)*TMath::Sin(alfa1),2)+TMath::Power(TMath::Sin(alfa1)*TMath::Sin(theta1)*TMath::Tan(omega1),2);
793           //cout<<"Trial: y="<<y<<"c0="<<c0<<" c1="<<c1<<" c2="<<c2<<endl;
794           //printf("Result:%f\n\n",c1*c1-4*c2*c0);
795           //i+=.01;
796           counter +=1;
797         }
798       
799       if (counter>=1000)
800         y=0; 
801
802       //Choose which side to go...
803       //if(gRandom->Rndm(1)>.5) s=1; 
804       //else s=-1;
805       if(i>500) s2=1;
806       //if (gRandom->Rndm(1)>.5) s2=1;
807       else s2=-1;
808       xtrial=(-c1+s2*TMath::Sqrt(c1*c1-4*c2*c0))/(2*c2);
809       //cout<<"x="<<xtrial<<" y="<<cy+y<<endl;
810       //printf("Coordinates: %f %f\n",xtrial,fCy+y);
811
812       REllipse->Fill(xtrial,y);
813
814       //printf("Coordinates: %f %f %f\n",vectorGlob[0],vectorGlob[1],vectorGlob[2]);
815     }
816   
817   fc3->cd(2);
818   REllipse->Draw();
819 }