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