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