New convention on numbering and LRS
[u/mrichter/AliRoot.git] / RICH / AliRICH.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 #include "AliRICH.h"
17 #include "AliRICHParam.h"
18 #include "AliRICHChamber.h"
19 #include "AliRICHClusterFinder.h"
20 #include <TArrayF.h>
21 #include <TGeometry.h>
22 #include <TBRIK.h>
23 #include <TTUBE.h>
24 #include <TFile.h>
25 #include <TNode.h> 
26 #include <TObjArray.h>
27 #include <TParticle.h>
28 #include <AliStack.h>
29 #include <AliMagF.h>
30 #include <AliRun.h>
31 #include <AliRunDigitizer.h>
32 #include <AliMC.h>
33 #include <AliESD.h>
34 #include <TVirtualMC.h>
35 #include <TH1F.h>
36 #include <TH2F.h>
37 #include <TStopwatch.h>
38  
39 ClassImp(AliRICHhit)
40 //__________________________________________________________________________________________________
41 void AliRICHhit::Print(Option_t*)const
42 {
43   ::Info("hit","Ch=%1i, TID=%6i, eloss=%9.3f eV, in-out dist=%9.4f, OUT(%7.2f,%7.2f,%7.2f)"
44       ,fChamber,fTrack,fEloss*1e9,Length(),fOutX3.X(),fOutX3.Y(),fOutX3.Z());
45 }
46 //__________________________________________________________________________________________________
47 ClassImp(AliRICHdigit)
48 //__________________________________________________________________________________________________
49 void AliRICHdigit::Print(Option_t*)const
50 {
51   ::Info("digit","cfm=%9i, cs=%2i, x=%3i, y=%3i, q=%8.3f, TID1=%5i, TID2=%5i, TID3=%5i",
52                   fCFM,fChamber,fPadX,fPadY,fQdc,fTracks[0],fTracks[1],fTracks[2]);
53 }
54 //__________________________________________________________________________________________________
55 ClassImp(AliRICHcluster)
56 //__________________________________________________________________________________________________
57 void AliRICHcluster::Print(Option_t*)const
58 {
59   char *status=0;
60   switch(fStatus){
61     case      kRaw: status="raw"     ;break;
62     case kResolved: status="resolved";break;
63     case    kEmpty: status="empty"   ;break;
64   }
65   if(fDigits)    
66     ::Info("cluster","cfm=%10i, cs=%2i, SiMa=%6i, Shape=%5i, x=%7.3f, y=%7.3f, Q=%6i, %s with %i digits",
67                              fCFM,fChamber,fSize,fShape,fX,fY,fQdc,status,fDigits->GetEntriesFast());
68   else
69     ::Info("cluster","cfm=%10i, cs=%2i, SiMa=%6i, Shape=%5i, x=%7.3f, y=%7.3f, Q=%6i, %s with %i digits",
70                              fCFM,fChamber,fSize,fShape,fX,fY,fQdc,status,0);
71     
72 }
73 //__________________________________________________________________________________________________
74 ClassImp(AliRICH)    
75 //__________________________________________________________________________________________________
76 // RICH manager class   
77 //BEGIN_HTML
78 /*
79   <img src="gif/alirich.gif">
80 */
81 //END_HTML
82 //__________________________________________________________________________________________________
83 AliRICH::AliRICH():AliDetector(),fpParam(0),  fSdigits(0),fNsdigits(0),fDigitsNew(0),fClusters(0) 
84 {
85 //Default ctor should not contain any new operators
86 //AliDetector ctor deals with Hits and Digits  
87   for(int i=0;i<kNchambers;i++) fNdigitsNew[i]  =0;
88   for(int i=0;i<kNchambers;i++) fNclusters[i]=0;
89 //  fCounters.ResizeTo(20); fCounters.Zero();
90 }//AliRICH::AliRICH()
91 //__________________________________________________________________________________________________
92 AliRICH::AliRICH(const char *name, const char *title)
93         :AliDetector(name,title),fpParam(new AliRICHParam),fSdigits(0),fNsdigits(0),fDigitsNew(0),fClusters(0)
94 {
95 //Named ctor
96   if(GetDebug())Info("named ctor","Start.");
97 //AliDetector ctor deals with Hits and Digits (reset them to 0, does not create them)
98   CreateHits();          gAlice->GetMCApp()->AddHitList(fHits);
99   fCounters.ResizeTo(20); fCounters.Zero();
100   if(GetDebug())Info("named ctor","Stop.");
101 }//AliRICH::AliRICH(const char *name, const char *title)
102 //__________________________________________________________________________________________________
103 AliRICH::~AliRICH()
104 {
105 //dtor
106   if(GetDebug()) Info("dtor","Start.");
107
108   if(fpParam)    delete fpParam;
109   
110   if(fHits)      delete fHits;
111   if(fSdigits)   delete fSdigits;
112   if(fDigits)    delete fDigits;
113   if(fDigitsNew) {fDigitsNew->Delete();   delete fDigitsNew;}
114   if(fClusters)  {fClusters->Delete();    delete fClusters;}
115   if(GetDebug()) Info("dtor","Stop.");    
116 }//AliRICH::~AliRICH()
117 //__________________________________________________________________________________________________
118 void AliRICH::Hits2SDigits()
119 {
120 // Create a list of sdigits corresponding to list of hits. Every hit generates one or more sdigits.
121   if(GetDebug()) Info("Hit2SDigits","Start.");
122   for(Int_t iEventN=0;iEventN<GetLoader()->GetRunLoader()->GetAliRun()->GetEventsPerRun();iEventN++){//events loop
123     GetLoader()->GetRunLoader()->GetEvent(iEventN);//get next event
124   
125     if(!GetLoader()->TreeH()) GetLoader()->LoadHits();    GetLoader()->GetRunLoader()->LoadHeader(); 
126                                                           GetLoader()->GetRunLoader()->LoadKinematics();//from
127     if(!GetLoader()->TreeS()) GetLoader()->MakeTree("S"); MakeBranch("S");//to
128           
129     for(Int_t iPrimN=0;iPrimN<GetLoader()->TreeH()->GetEntries();iPrimN++){//prims loop
130       GetLoader()->TreeH()->GetEntry(iPrimN);
131       for(Int_t iHitN=0;iHitN<Hits()->GetEntries();iHitN++){//hits loop 
132         AliRICHhit *pHit=(AliRICHhit*)Hits()->At(iHitN);//get current hit                
133         TVector2 x2 = C(pHit->C())->Glob2Loc(pHit->OutX3());//hit position in the chamber local system
134         Int_t iTotQdc=P()->TotQdc(x2,pHit->Eloss());//total charge produced by hit, 0 if hit in dead zone
135         if(iTotQdc==0) continue;
136         TVector area=P()->Loc2Area(x2);//determine affected pads, dead zones analysed inside
137         if(GetDebug()) Info("Hits2SDigits","hit(%6.2f,%6.2f)->area(%3.0f,%3.0f)-(%3.0f,%3.0f) QDC=%4i",x2.X(),x2.Y(),area[0],area[1],area[2],area[3],iTotQdc);
138         TVector pad(2);
139         for(pad[1]=area[1];pad[1]<=area[3];pad[1]++)//affected pads loop
140           for(pad[0]=area[0];pad[0]<=area[2];pad[0]++){                    
141             Double_t padQdc=iTotQdc*P()->FracQdc(x2,pad);
142             if(GetDebug()) Info("Hits2SDigits","current pad(%3.0f,%3.0f) with QDC  =%6.2f",pad[0],pad[1],padQdc);
143             if(padQdc>0.1) AddSDigit(pHit->C(),pad,padQdc,GetLoader()->GetRunLoader()->Stack()->Particle(pHit->GetTrack())->GetPdgCode(),pHit->GetTrack());
144           }//affected pads loop 
145       }//hits loop
146     }//prims loop
147     GetLoader()->TreeS()->Fill();
148     GetLoader()->WriteSDigits("OVERWRITE");
149     ResetSDigits();
150   }//events loop  
151   GetLoader()->UnloadHits(); GetLoader()->GetRunLoader()->UnloadHeader(); GetLoader()->GetRunLoader()->UnloadKinematics();
152   GetLoader()->UnloadSDigits();  
153   if(GetDebug()) Info("Hit2SDigits","Stop.");
154 }//Hits2SDigits()
155 //__________________________________________________________________________________________________
156 void AliRICH::BuildGeometry() 
157 {
158 //Builds a TNode geometry for event display
159   if(GetDebug())Info("BuildGeometry","Start.");
160   
161   TNode *node, *subnode, *top;
162   top=gAlice->GetGeometry()->GetNode("alice");
163   
164   new TBRIK("S_RICH","S_RICH","void",71.09999,11.5,73.15);
165
166   Float_t wid=P()->SectorSizeX();
167   Float_t len=P()->SectorSizeY();
168   new TBRIK("PHOTO","PHOTO","void",wid/2,0.1,len/2);
169   
170   for(int i=1;i<=kNchambers;i++){
171     top->cd();
172     node = new TNode(Form("RICH%i",i),Form("RICH%i",i),"S_RICH",C(i)->X(),C(i)->Y(),C(i)->Z(),C(i)->RotMatrixName());
173     node->SetLineColor(kRed);
174     node->cd();
175     subnode = new TNode("PHOTO1","PHOTO1","PHOTO",wid+P()->DeadZone(),5,len/2+P()->DeadZone()/2,"");
176     subnode->SetLineColor(kGreen);
177     fNodes->Add(subnode);
178     subnode = new TNode("PHOTO1","PHOTO1","PHOTO",0,5,len/2+P()->DeadZone()/2,"");
179     subnode->SetLineColor(kGreen);
180     fNodes->Add(subnode);
181     subnode = new TNode("PHOTO1","PHOTO1","PHOTO",-wid-P()->DeadZone(),5,len/2+P()->DeadZone()/2,"");
182     subnode->SetLineColor(kGreen);
183     fNodes->Add(subnode);
184     subnode = new TNode("PHOTO1","PHOTO1","PHOTO",wid+P()->DeadZone(),5,-len/2-P()->DeadZone()/2,"");
185     subnode->SetLineColor(kGreen);
186     fNodes->Add(subnode);
187     subnode = new TNode("PHOTO1","PHOTO1","PHOTO",0,5,-len/2 -P()->DeadZone()/2,"");
188     subnode->SetLineColor(kGreen);
189     fNodes->Add(subnode);
190     subnode = new TNode("PHOTO1","PHOTO1","PHOTO",-wid-P()->DeadZone(),5,-len/2 - P()->DeadZone()/2,"");
191     subnode->SetLineColor(kGreen);
192     fNodes->Add(subnode);
193     fNodes->Add(node);
194   }  
195   if(GetDebug())Info("BuildGeometry","Stop.");    
196 }//void AliRICH::BuildGeometry()
197
198 //______________________________________________________________________________
199 void AliRICH::CreateMaterials()
200 {
201 // Definition of available RICH materials  
202 #include "Opticals.h"
203         
204   Float_t a=0,z=0,den=0,radl=0,absl=0;
205   Float_t tmaxfd=-10.0, deemax=-0.2, stemax=-0.1,epsil=0.001, stmin=-0.001; 
206   Int_t   isxfld = gAlice->Field()->Integ();
207   Float_t sxmgmx = gAlice->Field()->Max();
208   Int_t material;
209     
210   AliMaterial(material=1, "Air     $",a=14.61,z=7.3, den=0.001205,radl=30420.0,absl=67500);//(Air)
211   AliMedium(1, "DEFAULT MEDIUM AIR$",material, 0, isxfld, sxmgmx, tmaxfd, stemax, deemax, epsil, stmin);
212   
213   AliMaterial( 6, "HON",      a=12.01,z=6.0, den=0.1,     radl=18.8,   absl=0);    //(C)-equivalent radl
214   AliMedium(2, "HONEYCOMB$", 6, 0, isxfld, sxmgmx, tmaxfd, stemax, deemax, epsil, stmin);
215   
216   AliMaterial(16, "CSI",      a=12.01,z=6.0, den=0.1,     radl=18.8,   absl=0);    //CsI-radl equivalent
217   AliMedium(kCSI, "CSI$", 16, 1, isxfld, sxmgmx,tmaxfd, stemax, deemax, epsil, stmin);
218   
219   AliMaterial(11, "GRI",      a=63.54,z=29.0,den=8.96,    radl=1.43,   absl=0);    //anode grid (Cu) 
220   AliMedium(7, "GRID$", 11, 0, isxfld, sxmgmx, tmaxfd, stemax, deemax, epsil, stmin);
221   
222   AliMaterial(50, "ALUM",     a=26.98,z=13.0,den=2.699,     radl=8.9,    absl=0);    //aluminium sheet (Al)
223   AliMedium(10, "ALUMINUM$", 50, 1, isxfld, sxmgmx, tmaxfd, stemax, deemax, epsil, stmin);
224   
225   AliMaterial(material=31, "COPPER$",  a=63.54,z=29.0,den=8.96,    radl=1.4,    absl=0);    //(Cu)
226   AliMedium(12, "PCB_COPPER", material, 0, isxfld, sxmgmx, tmaxfd, stemax, deemax, epsil, stmin);
227   
228   Float_t  aQuartz[2]={28.09,16.0};  Float_t  zQuartz[2]={14.00, 8.0};  Float_t  wmatQuartz[2]={1,2};
229   AliMixture (20, "QUA",aQuartz,zQuartz,den=2.64,-2, wmatQuartz);//Quarz (SiO2) - trasnparent 
230   AliMedium(3, "QUARTZ$", 20, 1, isxfld, sxmgmx, tmaxfd, stemax, deemax, epsil, stmin);
231   
232   AliMixture (21, "QUAO",aQuartz, zQuartz, den=2.64, -2, wmatQuartz);//Quarz (SiO2) - opaque
233   AliMedium(8, "QUARTZO$", 21, 1, isxfld, sxmgmx, tmaxfd, stemax, deemax, epsil, stmin);
234   
235   Float_t  aFreon[2]={12,19};  Float_t  zFreon[2]={6,9};  Float_t wmatFreon[2]={6,14};
236   AliMixture (material=30, "C6F14",aFreon,zFreon,den=1.68,-2,wmatFreon);//Freon (C6F14) 
237   AliMedium(4, "C6F14$",material, 1, isxfld, sxmgmx, tmaxfd, stemax, deemax, epsil, stmin);
238   
239   Float_t aMethane[2]={12.01,1}; Float_t zMethane[2]={6,1}; Float_t wmatMethane[2]={1,4};
240   AliMixture (material=40, "CH4", aMethane, zMethane, den=7.17e-4,-2, wmatMethane);//methane (CH4)     
241   AliMedium(kCH4, "CH4$"   , material, 1, isxfld, sxmgmx, tmaxfd, stemax, deemax, epsil, stmin);  
242   AliMedium(kGAP, "CH4GAP$", material, 1, isxfld, sxmgmx,tmaxfd, 0.1, -deemax, epsil, -stmin);
243   
244   if(P()->IsRadioSrc()){
245     AliMaterial(material=45, "STEEL$",  a=63.54,z=29.0,den=8.96,    radl=1.4,    absl=0);    //Steel
246     AliMedium(kSteel, "STEEL", material, 0, isxfld, sxmgmx, tmaxfd, stemax, deemax, epsil, stmin);
247   
248     AliMaterial(material=46, "PERPEX$",  a=63.54,z=29.0,den=8.96,    radl=1.4,    absl=0);    //Perpex
249     AliMedium(kPerpex, "PERPEX", material, 0, isxfld, sxmgmx, tmaxfd, stemax, deemax, epsil, stmin);
250     
251     AliMaterial(material=47, "STRONZIUM$",  a=87.62,z=38.0,den=2.54,    radl=4.24,    absl=0); //Sr90
252     AliMedium(kSr90, "STRONZIUM", material, 0, isxfld, sxmgmx, tmaxfd, stemax, deemax, epsil, stmin);
253   }
254   
255   Float_t aGlass[5]={12.01, 28.09, 16.,   10.8,  23.};
256   Float_t zGlass[5]={ 6.,   14.,    8.,    5.,   11.};
257   Float_t wGlass[5]={ 0.5,  0.105, 0.355, 0.03,  0.01};
258   AliMixture(material=32, "GLASS",aGlass, zGlass, den=1.74, 5, wGlass);//Glass 50%C+10.5%Si+35.5%O+3% + 1%
259   AliMedium(11, "GLASS", material, 0, isxfld, sxmgmx, tmaxfd, stemax, deemax, epsil, stmin);
260             
261   Int_t *idtmed = fIdtmed->GetArray()-999;
262   gMC->SetCerenkov(idtmed[1000], kNbins, aPckov, aAbsCH4,    aQeAll, aIdxCH4);
263   gMC->SetCerenkov(idtmed[1001], kNbins, aPckov, aAbsCH4,    aQeAll, aIdxCH4);
264   gMC->SetCerenkov(idtmed[1002], kNbins, aPckov, aAbsSiO2,   aQeAll, aIdxSiO2);
265   gMC->SetCerenkov(idtmed[1003], kNbins, aPckov, aAbsC6F14,  aQeAll, aIdxC6F14);
266   gMC->SetCerenkov(idtmed[1004], kNbins, aPckov, aAbsCH4,    aQeAll, aIdxCH4);
267   gMC->SetCerenkov(idtmed[1005], kNbins, aPckov, aAbsCsI,    aQeCsI, aIdxCH4);
268   gMC->SetCerenkov(idtmed[1006], kNbins, aPckov, aAbsGrid,   aQeAll, aIdxGrid);
269   gMC->SetCerenkov(idtmed[1007], kNbins, aPckov, aAbsOpSiO2, aQeAll, aIdxOpSiO2);
270   gMC->SetCerenkov(idtmed[1008], kNbins, aPckov, aAbsCH4,    aQeAll, aIdxCH4);
271   gMC->SetCerenkov(idtmed[1009], kNbins, aPckov, aAbsGrid,   aQeAll, aIdxGrid);
272   gMC->SetCerenkov(idtmed[1010], kNbins, aPckov, aAbsOpSiO2, aQeAll, aIdxOpSiO2);
273     
274 }//void AliRICH::CreateMaterials()
275 //__________________________________________________________________________________________________
276 Float_t AliRICH::Fresnel(Float_t ene,Float_t pdoti, Bool_t pola)const
277 {
278
279     //ENE(EV), PDOTI=COS(INC.ANG.), PDOTR=COS(POL.PLANE ROT.ANG.)
280     
281     Float_t en[36] = {5.0,5.1,5.2,5.3,5.4,5.5,5.6,5.7,5.8,5.9,6.0,6.1,6.2,
282                       6.3,6.4,6.5,6.6,6.7,6.8,6.9,7.0,7.1,7.2,7.3,7.4,7.5,7.6,7.7,
283                       7.8,7.9,8.0,8.1,8.2,8.3,8.4,8.5};
284     Float_t csin[36] = {2.14,2.21,2.33,2.48,2.76,2.97,2.99,2.59,2.81,3.05,
285                         2.86,2.53,2.55,2.66,2.79,2.96,3.18,3.05,2.84,2.81,2.38,2.11,
286                         2.01,2.13,2.39,2.73,3.08,3.15,2.95,2.73,2.56,2.41,2.12,1.95,
287                         1.72,1.53};
288     Float_t csik[36] = {0.,0.,0.,0.,0.,0.196,0.408,0.208,0.118,0.49,0.784,0.543,
289                         0.424,0.404,0.371,0.514,0.922,1.102,1.139,1.376,1.461,1.253,0.878,
290                         0.69,0.612,0.649,0.824,1.347,1.571,1.678,1.763,1.857,1.824,1.824,
291                         1.714,1.498};
292     Float_t xe=ene;
293     Int_t  j=Int_t(xe*10)-49;
294     Float_t cn=csin[j]+((csin[j+1]-csin[j])/0.1)*(xe-en[j]);
295     Float_t ck=csik[j]+((csik[j+1]-csik[j])/0.1)*(xe-en[j]);
296
297     //FORMULAE FROM HANDBOOK OF OPTICS, 33.23 OR
298     //W.R. HUNTER, J.O.S.A. 54 (1964),15 , J.O.S.A. 55(1965),1197
299
300     Float_t sinin=TMath::Sqrt(1-pdoti*pdoti);
301     Float_t tanin=sinin/pdoti;
302
303     Float_t c1=cn*cn-ck*ck-sinin*sinin;
304     Float_t c2=4*cn*cn*ck*ck;
305     Float_t aO=TMath::Sqrt(0.5*(TMath::Sqrt(c1*c1+c2)+c1));
306     Float_t b2=0.5*(TMath::Sqrt(c1*c1+c2)-c1);
307     
308     Float_t rs=((aO-pdoti)*(aO-pdoti)+b2)/((aO+pdoti)*(aO+pdoti)+b2);
309     Float_t rp=rs*((aO-sinin*tanin)*(aO-sinin*tanin)+b2)/((aO+sinin*tanin)*(aO+sinin*tanin)+b2);
310     
311
312     //CORRECTION FACTOR FOR SURFACE ROUGHNESS
313     //B.J. STAGG  APPLIED OPTICS, 30(1991),4113
314
315     Float_t sigraf=18.;
316     Float_t lamb=1240/ene;
317     Float_t fresn;
318  
319     Float_t  rO=TMath::Exp(-(4*TMath::Pi()*pdoti*sigraf/lamb)*(4*TMath::Pi()*pdoti*sigraf/lamb));
320
321     if(pola)
322     {
323         Float_t pdotr=0.8;                                 //DEGREE OF POLARIZATION : 1->P , -1->S
324         fresn=0.5*(rp*(1+pdotr)+rs*(1-pdotr));
325     }
326     else
327         fresn=0.5*(rp+rs);
328       
329     fresn = fresn*rO;
330     return(fresn);
331 }//Fresnel()
332 //__________________________________________________________________________________________________
333 Float_t AliRICH::AbsoCH4(Float_t x)const
334 {
335 //Evaluate the absorbtion lenght of CH4
336   Float_t sch4[9] = {.12,.16,.23,.38,.86,2.8,7.9,28.,80.};              //MB X 10^22
337   Float_t em[9] = {8.1,8.158,8.212,8.267,8.322,8.378,8.435,8.493,8.55};
338   const Float_t kLoschmidt=2.686763e19;                                      // LOSCHMIDT NUMBER IN CM-3
339   const Float_t kPressure=750.,kTemperature=283.;                                      
340   const Float_t kPn=kPressure/760.;
341   const Float_t kTn=kTemperature/273.16;
342   const Float_t kC0=-1.655279e-1;
343   const Float_t kC1=6.307392e-2;
344   const Float_t kC2=-8.011441e-3;
345   const Float_t kC3=3.392126e-4;
346                 
347   Float_t crossSection=0;                        
348   if (x<7.75) 
349     crossSection=.06e-22;
350   else if(x>=7.75 && x<=8.1){                 //------ METHANE CROSS SECTION cm-2 ASTROPH. J. 214, L47 (1978)                                               
351         crossSection=(kC0+kC1*x+kC2*x*x+kC3*x*x*x)*1.e-18;
352   }else if (x> 8.1){
353     Int_t j=0;
354     while (x<=em[j] || x>=em[j+1]){
355       j++;
356       Float_t a=(sch4[j+1]-sch4[j])/(em[j+1]-em[j]);
357       crossSection=(sch4[j]+a*(x-em[j]))*1e-22;
358     }
359   }//if
360     
361     Float_t density=kLoschmidt*kPn/kTn; //CH4 molecular density 1/cm-3
362     return 1./(density*crossSection);
363 }//AbsoCH4()
364 //__________________________________________________________________________________________________
365 void AliRICH::MakeBranch(Option_t* option)
366 {
367 //Create Tree branches for the RICH.
368   if(GetDebug())Info("MakeBranch","Start with option= %s.",option);
369     
370   const Int_t kBufferSize = 4000;
371       
372   const char *cH = strstr(option,"H");
373   const char *cD = strstr(option,"D");
374   const char *cR = strstr(option,"R");
375   const char *cS = strstr(option,"S");
376
377   if(cH&&TreeH()){//H
378     CreateHits();      //branch will be created in AliDetector::MakeBranch
379   }//H     
380   AliDetector::MakeBranch(option);//this is after cH because we need to guarantee that fHits array is created
381       
382   if(cS&&fLoader->TreeS()){//S  
383     CreateSDigits();   MakeBranchInTree(fLoader->TreeS(),"RICH",&fSdigits,kBufferSize,0) ;
384   }//S
385    
386   if(cD&&fLoader->TreeD()){//D
387     CreateDigits();
388     for(Int_t i=0;i<kNchambers;i++){ 
389       MakeBranchInTree(fLoader->TreeD(),Form("%s%d",GetName(),i+1),&((*fDigitsNew)[i]),kBufferSize,0);
390     }
391   }//D
392   
393   if(cR&&fLoader->TreeR()){//R
394     CreateClusters();
395     for(Int_t i=0;i<kNchambers;i++)
396       MakeBranchInTree(fLoader->TreeR(),Form("%sClusters%d",GetName(),i+1), &((*fClusters)[i]), kBufferSize, 0);    
397   }//R
398   if(GetDebug())Info("MakeBranch","Stop.");   
399 }//void AliRICH::MakeBranch(Option_t* option)
400 //__________________________________________________________________________________________________
401 void AliRICH::SetTreeAddress()
402 {
403 //Set branch address for the Hits and Digits Tree.
404   if(GetDebug())Info("SetTreeAddress","Start.");
405       
406   TBranch *branch;
407     
408   if(fLoader->TreeH()){//H
409     if(GetDebug())Info("SetTreeAddress","tree H is requested.");
410     CreateHits();//branch map will be in AliDetector::SetTreeAddress    
411   }//H
412   AliDetector::SetTreeAddress();//this is after TreeH because we need to guarantee that fHits array is created
413
414   if(fLoader->TreeS()){//S
415     if(GetDebug())Info("SetTreeAddress","tree S is requested.");
416     branch=fLoader->TreeS()->GetBranch(GetName());        if(branch){CreateSDigits();   branch->SetAddress(&fSdigits);}
417   }//S
418     
419   if(fLoader->TreeD()){//D    
420     if(GetDebug())Info("SetTreeAddress","tree D is requested.");
421     for(int i=0;i<kNchambers;i++){      
422       branch=fLoader->TreeD()->GetBranch(Form("%s%d",GetName(),i+1)); 
423       if(branch){CreateDigits(); branch->SetAddress(&((*fDigitsNew)[i]));}
424     }
425   }//D
426     
427   if(fLoader->TreeR()){//R
428     if(GetDebug())Info("SetTreeAddress","tree R is requested.");
429     for(int i=0;i<kNchambers;i++){         
430       branch=fLoader->TreeR()->GetBranch(Form("%sClusters%d" ,GetName(),i+1));
431       if(branch){CreateClusters(); branch->SetAddress(&((*fClusters)[i]));}
432     }
433   }//R
434   if(GetDebug())Info("SetTreeAddress","Stop.");
435 }//void AliRICH::SetTreeAddress()
436 //__________________________________________________________________________________________________
437 void AliRICH::Print(Option_t *option)const
438 {
439 //Debug printout
440   TObject::Print(option);
441   P()->Print();
442   fCounters.Print();
443 }//void AliRICH::Print(Option_t *option)const
444 //__________________________________________________________________________________________________
445 void AliRICH::CreateGeometry()
446 {
447 //Creates detailed geometry simulation (currently GEANT volumes tree)         
448   if(GetDebug())Info("CreateGeometry","Start main.");
449 //Opaque quartz thickness
450   Float_t oquaThickness = .5;
451 //CsI dimensions
452   Float_t pcX=P()->PcSizeX();
453   Float_t pcY=P()->PcSizeY();
454   
455   Int_t *idtmed = fIdtmed->GetArray()-999;
456     
457   Int_t i;
458   Float_t zs;
459   Int_t idrotm[1099];
460   Float_t par[3];
461   par[0]=68.8;par[1]=13   ;par[2]=70.86;  gMC->Gsvolu("RICH","BOX ",(*fIdtmed)[kAl], par,3);//External aluminium box 
462   par[0]=66.3;par[1]=13   ;par[2]=68.35;  gMC->Gsvolu("SRIC","BOX ",(*fIdtmed)[kAir],par,3);//Air 
463   par[0]=66.3;par[1]=0.025;par[2]=68.35;  gMC->Gsvolu("ALUM","BOX ",(*fIdtmed)[kAl], par,3);//Aluminium sheet 
464 //Air 2 (cutting the lower part of the box)
465 //  par[0]=1.25;    par[1] = 3;    par[2] = 70.86;   gMC->Gsvolu("AIR2", "BOX ", idtmed[1000], par, 3);
466 //Air 3 (cutting the lower part of the box)
467 //  par[0]=66.3;    par[1] = 3;  par[2] = 1.2505;    gMC->Gsvolu("AIR3", "BOX ", idtmed[1000], par, 3);
468 //Honeycomb 
469   par[0]=66.3;par[1]=0.188;  par[2] = 68.35;       gMC->Gsvolu("HONE", "BOX ", idtmed[1001], par, 3);
470
471   //par[0] = 66.5; par[1] = .025; par[2] = 63.1;
472 //Quartz 
473   par[0]=P()->QuartzWidth()/2;par[1]=P()->QuartzThickness()/2;par[2]=P()->QuartzLength()/2;
474   gMC->Gsvolu("QUAR", "BOX ", idtmed[1002], par, 3);
475 //Spacers (cylinders) 
476   par[0]=0.;par[1]=.5;par[2]=P()->FreonThickness()/2;  gMC->Gsvolu("SPAC", "TUBE", idtmed[1002], par, 3);    
477 //Feet (freon slabs supports)
478   par[0] = .7;  par[1] = .3;  par[2] = 1.9;        gMC->Gsvolu("FOOT", "BOX", idtmed[1009], par, 3);
479 //Opaque quartz 
480   par[0]=P()->QuartzWidth()/2;par[1]= .2;par[2]=P()->QuartzLength()/2;
481   gMC->Gsvolu("OQUA", "BOX ", idtmed[1007], par, 3);
482 //Frame of opaque quartz
483   par[0]=P()->OuterFreonWidth()/2;par[1]=P()->FreonThickness()/2;par[2]=P()->OuterFreonLength()/2; 
484   gMC->Gsvolu("OQF1", "BOX ", idtmed[1007], par, 3);
485   par[0]=P()->InnerFreonWidth()/2;par[1]=P()->FreonThickness()/2;par[2]=P()->InnerFreonLength()/2; 
486   gMC->Gsvolu("OQF2", "BOX ", idtmed[1007], par, 3);
487 //Freon 
488   par[0]=P()->OuterFreonWidth()/2 - oquaThickness;
489   par[1]=P()->FreonThickness()/2;
490   par[2]=P()->OuterFreonLength()/2 - 2*oquaThickness; 
491   gMC->Gsvolu("FRE1", "BOX ", idtmed[1003], par, 3);
492
493   par[0]=P()->InnerFreonWidth()/2 - oquaThickness;
494   par[1]=P()->FreonThickness()/2;
495   par[2]=P()->InnerFreonLength()/2 - 2*oquaThickness; 
496   gMC->Gsvolu("FRE2", "BOX ", idtmed[1003], par, 3);    
497 //Methane 
498   par[0]=pcX/2;par[1]=P()->GapThickness()/2;par[2]=pcY/2;         gMC->Gsvolu("META","BOX ",idtmed[1004], par, 3);
499 //Methane gap 
500   par[0]=pcX/2;par[1]=P()->GapAmp()/2;par[2]=pcY/2;gMC->Gsvolu("GAP ","BOX ",(*fIdtmed)[kGAP],par,3);
501 //CsI PC
502   par[0]=pcX/2;par[1]=.25;par[2]=pcY/2;  gMC->Gsvolu("CSI ", "BOX ", (*fIdtmed)[kCSI], par, 3);
503 //Anode grid 
504   par[0] = 0.;par[1] = .001;par[2] = 20.;  gMC->Gsvolu("GRID", "TUBE", idtmed[1006], par, 3);
505
506 //Wire supports
507 //Bar of metal
508   par[0]=pcX/2;par[1]=1.05;par[2]=1.05;  gMC->Gsvolu("WSMe", "BOX ", idtmed[1009], par, 3);
509 //Ceramic pick up (base)
510   par[0]=pcX/2;par[1]= .25;par[2]=1.05;  gMC->Gsvolu("WSG1", "BOX ", idtmed[1010], par, 3);
511 //Ceramic pick up (head)
512   par[0] = pcX/2;par[1] = .1;par[2] = .1;  gMC->Gsvolu("WSG2", "BOX ", idtmed[1010], par, 3);
513
514 //Aluminium supports for methane and CsI
515 //Short bar
516   par[0]=pcX/2;par[1]=P()->GapThickness()/2 + .25; par[2] = (68.35 - pcY/2)/2;
517   gMC->Gsvolu("SMSH", "BOX", idtmed[1009], par, 3);
518 //Long bar
519   par[0]=(66.3 - pcX/2)/2;par[1]=P()->GapThickness()/2+.25;par[2]=pcY/2+68.35-pcY/2;
520   gMC->Gsvolu("SMLG", "BOX", idtmed[1009], par, 3);
521     
522 //Aluminium supports for freon
523 //Short bar
524   par[0] = P()->QuartzWidth()/2; par[1] = .3; par[2] = (68.35 - P()->QuartzLength()/2)/2;
525   gMC->Gsvolu("SFSH", "BOX", idtmed[1009], par, 3);    
526 //Long bar
527   par[0] = (66.3 - P()->QuartzWidth()/2)/2; par[1] = .3;
528   par[2] = P()->QuartzLength()/2 + 68.35 - P()->QuartzLength()/2;
529   gMC->Gsvolu("SFLG", "BOX", idtmed[1009], par, 3);    
530 //PCB backplane
531   par[0] = pcX/2;par[1] = .25; par[2] = pcY/4 -.5025;  gMC->Gsvolu("PCB ", "BOX", idtmed[1011], par, 3);
532
533 //Backplane supports
534 //Aluminium slab
535   par[0] = 33.15;par[1] = 2;par[2] = 21.65;  gMC->Gsvolu("BACK", "BOX", idtmed[1009], par, 3);    
536 //Big hole
537   par[0] = 9.05; par[1] = 2; par[2] = 4.4625;  gMC->Gsvolu("BKHL", "BOX", idtmed[1000], par, 3);
538 //Small hole
539   par[0] = 5.7;par[1] = 2;par[2] = 4.4625;  gMC->Gsvolu("BKHS", "BOX", idtmed[1000], par, 3);
540 //Place holes inside backplane support
541   gMC->Gspos("BKHS", 1, "BACK", .8 + 5.7,0., .6 + 4.4625, 0, "ONLY");
542   gMC->Gspos("BKHS", 2, "BACK", -.8 - 5.7,0., .6 + 4.4625, 0, "ONLY");
543   gMC->Gspos("BKHS", 3, "BACK", .8 + 5.7,0., -.6 - 4.4625, 0, "ONLY");
544   gMC->Gspos("BKHS", 4, "BACK", -.8 - 5.7,0., -.6 - 4.4625, 0, "ONLY");
545   gMC->Gspos("BKHS", 5, "BACK", .8 + 5.7,0., .6 + 8.925 + 1.2 + 4.4625, 0, "ONLY");
546   gMC->Gspos("BKHS", 6, "BACK", -.8 - 5.7,0., .6 + 8.925 + 1.2 + 4.4625, 0, "ONLY");
547   gMC->Gspos("BKHS", 7, "BACK", .8 + 5.7,0., -.6 - 8.925 - 1.2 - 4.4625, 0, "ONLY");
548   gMC->Gspos("BKHS", 8, "BACK", -.8 - 5.7,0., -.6 - 8.925 - 1.2 - 4.4625, 0, "ONLY");
549   gMC->Gspos("BKHL", 1, "BACK", .8 + 11.4 + 1.6 + 9.05, 0., .6 + 4.4625, 0, "ONLY");
550   gMC->Gspos("BKHL", 2, "BACK", -.8 - 11.4 - 1.6 - 9.05, 0., .6 + 4.4625, 0, "ONLY");
551   gMC->Gspos("BKHL", 3, "BACK", .8 + 11.4 + 1.6 + 9.05, 0., -.6 - 4.4625, 0, "ONLY");
552   gMC->Gspos("BKHL", 4, "BACK", -.8 - 11.4 - 1.6 - 9.05, 0., -.6 - 4.4625, 0, "ONLY");
553   gMC->Gspos("BKHL", 5, "BACK", .8 + 11.4+ 1.6 + 9.05, 0., .6 + 8.925 + 1.2 + 4.4625, 0, "ONLY");
554   gMC->Gspos("BKHL", 6, "BACK", -.8 - 11.4 - 1.6 - 9.05, 0., .6 + 8.925 + 1.2 + 4.4625, 0, "ONLY");
555   gMC->Gspos("BKHL", 7, "BACK", .8 + 11.4 + 1.6 + 9.05, 0., -.6 - 8.925 - 1.2 - 4.4625, 0, "ONLY");
556   gMC->Gspos("BKHL", 8, "BACK", -.8 - 11.4 - 1.6 - 9.05, 0., -.6 - 8.925 - 1.2 - 4.4625, 0, "ONLY");
557 //Place material inside RICH 
558   gMC->Gspos("SRIC", 1, "RICH", 0.,0., 0., 0, "ONLY");
559 //  gMC->Gspos("AIR2", 1, "RICH", 66.3 + 1.2505, 1.276-P()->GapThickness()/2-P()->QuartzThickness()-P()->FreonThickness()- .4 - .6 - .05 - .376 -.5 - 3.35, 0., 0, "ONLY");
560 //  gMC->Gspos("AIR2", 2, "RICH", -66.3 - 1.2505,1.276-P()->GapThickness()/2-P()->QuartzThickness()-P()->FreonThickness()- .4 - .6 - .05 - .376 -.5 - 3.35, 0., 0, "ONLY");
561 //  gMC->Gspos("AIR3", 1, "RICH", 0., 1.276-P()->GapThickness()/2 - P()->QuartzThickness() - P()->FreonThickness()- .4 - .6 - .05 - .376 -.5 - 3.35, -68.35 - 1.25, 0, "ONLY");
562 //  gMC->Gspos("AIR3", 2, "RICH", 0., 1.276 - P()->GapThickness()/2 - P()->QuartzThickness() - P()->FreonThickness()- .4 - .6 - .05 - .376 -.5 - 3.35,  68.35 + 1.25, 0, "ONLY");
563   gMC->Gspos("ALUM", 1, "SRIC", 0., 1.276 - P()->GapThickness()/2 - P()->QuartzThickness() - P()->FreonThickness()- .4 - .6 - .05 - .376 -.025, 0., 0, "ONLY");
564   gMC->Gspos("HONE", 1, "SRIC", 0., 1.276- P()->GapThickness()/2  - P()->QuartzThickness() - P()->FreonThickness()- .4 - .6 - .05 - .188, 0., 0, "ONLY");
565   gMC->Gspos("ALUM", 2, "SRIC", 0., 1.276 - P()->GapThickness()/2 - P()->QuartzThickness() - P()->FreonThickness()- .4 - .6 - .025, 0., 0, "ONLY");
566   gMC->Gspos("FOOT", 1, "SRIC", 64.95, 1.276 - P()->GapThickness()/2 - P()->QuartzThickness() - P()->FreonThickness()- .4 - .3, 36.9, 0, "ONLY");
567   gMC->Gspos("FOOT", 2, "SRIC", 21.65, 1.276 - P()->GapThickness()/2 - P()->QuartzThickness() - P()->FreonThickness()- .4 - .3 , 36.9, 0, "ONLY");
568   gMC->Gspos("FOOT", 3, "SRIC", -21.65, 1.276 - P()->GapThickness()/2 - P()->QuartzThickness() - P()->FreonThickness()- .4 - .3, 36.9, 0, "ONLY");
569   gMC->Gspos("FOOT", 4, "SRIC", -64.95, 1.276 - P()->GapThickness()/2 - P()->QuartzThickness() - P()->FreonThickness()- .4 - .3, 36.9, 0, "ONLY");
570   gMC->Gspos("FOOT", 5, "SRIC", 64.95, 1.276 - P()->GapThickness()/2 - P()->QuartzThickness() - P()->FreonThickness()- .4 - .3, -36.9, 0, "ONLY");
571   gMC->Gspos("FOOT", 6, "SRIC", 21.65, 1.276 - P()->GapThickness()/2 - P()->QuartzThickness() - P()->FreonThickness()- .4 - .3, -36.9, 0, "ONLY");
572   gMC->Gspos("FOOT", 7, "SRIC", -21.65, 1.276 - P()->GapThickness()/2 - P()->QuartzThickness() - P()->FreonThickness()- .4 - .3, -36.9, 0, "ONLY");
573   gMC->Gspos("FOOT", 8, "SRIC", -64.95, 1.276 - P()->GapThickness()/2 - P()->QuartzThickness() - P()->FreonThickness()- .4 - .3, -36.9, 0, "ONLY");
574   gMC->Gspos("OQUA", 1, "SRIC", 0., 1.276 - P()->GapThickness()/2 - P()->QuartzThickness() - P()->FreonThickness()- .2, 0., 0, "ONLY");
575 // Methane supports
576   gMC->Gspos("SMLG", 1, "SRIC", pcX/2 + (66.3 - pcX/2)/2, 1.276 + .25, 0., 0, "ONLY");
577   gMC->Gspos("SMLG", 2, "SRIC", - pcX/2 - (66.3 - pcX/2)/2, 1.276 + .25, 0., 0, "ONLY");
578   gMC->Gspos("SMSH", 1, "SRIC", 0., 1.276 + .25, pcY/2 + (68.35 - pcY/2)/2, 0, "ONLY");
579   gMC->Gspos("SMSH", 2, "SRIC", 0., 1.276 + .25, - pcY/2 - (68.35 - pcY/2)/2, 0, "ONLY");
580 //Freon supports
581   Float_t suppY = 1.276 - P()->GapThickness()/2- P()->QuartzThickness() -P()->FreonThickness() - .2 + .3; //y position of freon supports
582   gMC->Gspos("SFLG", 1, "SRIC", P()->QuartzWidth()/2 + (66.3 - P()->QuartzWidth()/2)/2, suppY, 0., 0, "ONLY");
583   gMC->Gspos("SFLG", 2, "SRIC", - P()->QuartzWidth()/2 - (66.3 - P()->QuartzWidth()/2)/2, suppY, 0., 0, "ONLY");
584   gMC->Gspos("SFSH", 1, "SRIC", 0., suppY, P()->QuartzLength()/2 + (68.35 - P()->QuartzLength()/2)/2, 0, "ONLY");
585   gMC->Gspos("SFSH", 2, "SRIC", 0., suppY, - P()->QuartzLength()/2 - (68.35 - P()->QuartzLength()/2)/2, 0, "ONLY");
586   AliMatrix(idrotm[1019], 0., 0., 90., 0., 90., 90.);
587 //Place spacers
588   Int_t nspacers = 30;
589   for (i = 0; i < nspacers/3; i++) {
590     zs = -11.6/2 + (TMath::Abs(nspacers/6) - i) * 12.2;
591     gMC->Gspos("SPAC", i, "FRE1", 10.5, 0., zs, idrotm[1019], "ONLY");  //Original settings 
592   }
593   for (i = nspacers/3; i < (nspacers*2)/3; i++) {
594     zs = -11.6/2 + (nspacers/3 + TMath::Abs(nspacers/6) - i) * 12.2;
595     gMC->Gspos("SPAC", i, "FRE1", 0, 0., zs, idrotm[1019], "ONLY");  //Original settings 
596   }
597   for (i = (nspacers*2)/3; i < nspacers; ++i) {
598     zs = -11.6/2 + ((nspacers*2)/3 + TMath::Abs(nspacers/6) - i) * 12.2;
599     gMC->Gspos("SPAC", i, "FRE1", -10.5, 0., zs, idrotm[1019], "ONLY"); //Original settings  
600   }
601   for (i = 0; i < nspacers/3; i++) {
602     zs = -11.6/2 + (TMath::Abs(nspacers/6) - i) * 12.2;
603     gMC->Gspos("SPAC", i, "FRE2", 10.5, 0., zs, idrotm[1019], "ONLY");  //Original settings 
604   }
605   for (i = nspacers/3; i < (nspacers*2)/3; i++) {
606     zs = -11.6/2 + (nspacers/3 + TMath::Abs(nspacers/6) - i) * 12.2;
607     gMC->Gspos("SPAC", i, "FRE2", 0, 0., zs, idrotm[1019], "ONLY");  //Original settings 
608   }
609   for (i = (nspacers*2)/3; i < nspacers; ++i) {
610     zs = -11.6/2 + ((nspacers*2)/3 + TMath::Abs(nspacers/6) - i) * 12.2;
611     gMC->Gspos("SPAC", i, "FRE2", -10.5, 0., zs, idrotm[1019], "ONLY"); //Original settings  
612   }
613   gMC->Gspos("FRE1", 1, "OQF1", 0., 0., 0., 0, "ONLY");
614   gMC->Gspos("FRE2", 1, "OQF2", 0., 0., 0., 0, "ONLY");
615   gMC->Gspos("OQF1", 1, "SRIC", P()->OuterFreonWidth()/2 + P()->InnerFreonWidth()/2 + 2, 1.276 - P()->GapThickness()/2- P()->QuartzThickness() -P()->FreonThickness()/2, 0., 0, "ONLY"); //Original settings (31.3)
616   gMC->Gspos("OQF2", 2, "SRIC", 0., 1.276 - P()->GapThickness()/2 - P()->QuartzThickness() - P()->FreonThickness()/2, 0., 0, "ONLY");          //Original settings 
617   gMC->Gspos("OQF1", 3, "SRIC", - (P()->OuterFreonWidth()/2 + P()->InnerFreonWidth()/2) - 2, 1.276 - P()->GapThickness()/2 - P()->QuartzThickness() - P()->FreonThickness()/2, 0., 0, "ONLY");       //Original settings (-31.3)
618   gMC->Gspos("QUAR", 1, "SRIC", 0., 1.276 - P()->GapThickness()/2 - P()->QuartzThickness()/2, 0., 0, "ONLY");
619   gMC->Gspos("GAP ", 1, "META", 0., P()->GapThickness()/2 - P()->GapAmp()/2 - 0.0001, 0., 0, "ONLY");
620   gMC->Gspos("META", 1, "SRIC", 0., 1.276, 0., 0, "ONLY");
621   gMC->Gspos("CSI ", 1, "SRIC", 0., 1.276 + P()->GapThickness()/2 + .25, 0., 0, "ONLY");
622 //Wire support placing
623   gMC->Gspos("WSG2", 1, "GAP ", 0., P()->GapAmp()/2 - .1, 0., 0, "ONLY");
624   gMC->Gspos("WSG1", 1, "CSI ", 0., 0., 0., 0, "ONLY");
625   gMC->Gspos("WSMe", 1, "SRIC ", 0., 1.276 + P()->GapThickness()/2 + .5 + 1.05, 0., 0, "ONLY");
626 //Backplane placing
627   gMC->Gspos("BACK", 1, "SRIC ", -33.15, 1.276 + P()->GapThickness()/2 + .5 + 2.1 + 2, 43.3, 0, "ONLY");
628   gMC->Gspos("BACK", 2, "SRIC ", 33.15, 1.276 + P()->GapThickness()/2 + .5 + 2.1 + 2 , 43.3, 0, "ONLY");
629   gMC->Gspos("BACK", 3, "SRIC ", -33.15, 1.276 + P()->GapThickness()/2 + .5 + 2.1 + 2, 0., 0, "ONLY");
630   gMC->Gspos("BACK", 4, "SRIC ", 33.15, 1.276 + P()->GapThickness()/2 + .5 + 2.1 + 2, 0., 0, "ONLY");
631   gMC->Gspos("BACK", 5, "SRIC ", 33.15, 1.276 + P()->GapThickness()/2 + .5 + 2.1 + 2, -43.3, 0, "ONLY");
632   gMC->Gspos("BACK", 6, "SRIC ", -33.15, 1.276 + P()->GapThickness()/2 + .5 + 2.1 + 2, -43.3, 0, "ONLY");
633 //PCB placing
634   gMC->Gspos("PCB ", 1, "SRIC ", 0.,  1.276 + P()->GapThickness()/2 + .5 + 1.05, pcX/4 + .5025 + 2.5, 0, "ONLY");
635   gMC->Gspos("PCB ", 2, "SRIC ", 0.,  1.276 + P()->GapThickness()/2 + .5 + 1.05, -pcX/4 - .5025 - 2.5, 0, "ONLY");
636   
637 //place chambers into mother volume ALIC
638   for(int i=1;i<=kNchambers;i++){
639     AliMatrix(idrotm[1000+i],C(i)->ThetaXd(),C(i)->PhiXd(),
640                              C(i)->ThetaYd(),C(i)->PhiYd(),
641                              C(i)->ThetaZd(),C(i)->PhiZd());
642     gMC->Gspos("RICH",i,"ALIC",C(i)->X(),C(i)->Y(),C(i)->Z(),idrotm[1000+i], "ONLY");
643   }
644
645          
646   if(GetDebug())Info("CreateGeometry","Stop main.");  
647 }//void AliRICH::CreateGeometry()
648 //__________________________________________________________________________________________________
649 void AliRICH::Reconstruct()const
650 {
651   AliRICHClusterFinder finder(const_cast<AliRICH*>(this));
652   finder.Exec();
653 }
654 //__________________________________________________________________________________________________
655 void AliRICH::ControlPlots()
656
657 //Creates a set of hists to control the results of simulation
658      
659   TH1F *pHxD=0,*pHyD=0,*pNumClusH1=0,
660                    *pQdcH1=0,       *pSizeH1=0,
661                    *pPureMipQdcH1=0,*pPureMipSizeH1=0,
662                    *pPureCerQdcH1=0,*pPureCerSizeH1=0,
663                    *pPureFeeQdcH1=0,*pPureFeeSizeH1=0,
664                    *pMipQdcH1=0,    *pPhotQdcH1=0;  
665   TH2F *pMapH2=0,*pPureMipMapH2=0,*pPureCerMapH2=0,*pPureFeeMapH2=0;
666   
667   Bool_t isDig =!GetLoader()->LoadDigits();
668   Bool_t isClus=!GetLoader()->LoadRecPoints();
669
670   if(!isDig && !isClus){Error("ControlPlots","No digits and clusters! Nothing to do.");return;}
671   
672   TStopwatch sw;TDatime time;
673     
674   TFile *pFile = new TFile("$(HOME)/RCP.root","RECREATE");   
675   
676   if(isDig){
677     cout<<"Digits available\n";
678     pHxD=new TH1F("HitDigitDiffX","Hit-Digits diff X all chambers;diff [cm]",100,-10,10); 
679     pHyD=new TH1F("HitDigitDiffY","Hit-Digits diff Y all chambers;diff [cm]",100,-10,10); 
680   }//isDig
681   
682   if(isClus){ 
683     cout<<"Clusters available\n";
684     pNumClusH1=new TH1F("NumClusPerEvent","Number of clusters per event;number",50,0,49);
685     
686     pQdcH1        =new TH1F("ClusQdc",   "Cluster Charge all chambers;q [QDC]",R()->P()->MaxQdc(),0,R()->P()->MaxQdc());
687     pSizeH1       =new TH1F("ClusSize",  "Cluster size all chambers;size [number of pads in cluster]",100,0,100);
688     pMapH2        =new TH2F("ClusMap",   "Cluster map;x [cm];y [cm]",1000,0,R()->P()->PcSizeX(),1000,0,R()->P()->PcSizeY());
689   
690     pMipQdcH1     =new TH1F("QdcMip"      ,"MIP Cluster Charge all chambers;q [QDC]",R()->P()->MaxQdc(),0,R()->P()->MaxQdc());
691     pPhotQdcH1    =new TH1F("QdcPhot"     ,"Cer+Fee Cluster Charge all chambers;q [QDC]",R()->P()->MaxQdc(),0,R()->P()->MaxQdc());
692         
693     pPureMipQdcH1 =new TH1F("QdcPureMip"  ,"MIP only Cluster Charge all chambers;q [QDC]",R()->P()->MaxQdc(),0,R()->P()->MaxQdc());
694     pPureMipSizeH1=new TH1F("SizePureMip" ,"MIP only Cluster size all chambers;size [number of pads in cluster]",100,0,100);
695     pPureMipMapH2 =new TH2F("MapPureMip"  ,"MIP only Cluster map;x [cm];y [cm]",1000,0,R()->P()->PcSizeX(),1000,0,R()->P()->PcSizeY());
696   
697     pPureCerQdcH1 =new TH1F("QdcPureCer"  ,"Cerenkov only Cluster Charge all chambers;q [QDC]",R()->P()->MaxQdc(),0,R()->P()->MaxQdc());
698     pPureCerSizeH1=new TH1F("SizePureCer" ,"Cernekov only Cluster size all chambers;size [number of pads in cluster]",100,0,100);
699     pPureCerMapH2 =new TH2F("MapPureCer"  ,"Cerenkov only Cluster map;x [cm];y [cm]",1000,0,R()->P()->PcSizeX(),1000,0,R()->P()->PcSizeY());
700     
701     pPureFeeQdcH1 =new TH1F("QdcPureFee"  ,"Feedback only Cluster Charge all chambers;q [QDC]",R()->P()->MaxQdc(),0,R()->P()->MaxQdc());
702     pPureFeeSizeH1=new TH1F("SizePureFee" ,"Feedback only Cluster size all chambers;size [number of pads in cluster]",100,0,100);
703     pPureFeeMapH2 =new TH2F("MapPureFee"  ,"Feedback only Cluster map;x [cm];y [cm]",1000,0,R()->P()->PcSizeX(),1000,0,R()->P()->PcSizeY());
704   }//isClus
705   
706   for(Int_t iEvtN=0;iEvtN < GetLoader()->GetRunLoader()->GetAliRun()->GetEventsPerRun();iEvtN++){//events loop
707     GetLoader()->GetRunLoader()->GetEvent(iEvtN);    //gets current event
708     
709     if(!GetLoader()->TreeH()) GetLoader()->LoadHits();
710     for(Int_t iPrimN=0;iPrimN < GetLoader()->TreeH()->GetEntries();iPrimN++){//prims loop
711        GetLoader()->TreeH()->GetEntry(iPrimN);      
712     }
713     
714     if(isClus) GetLoader()->TreeR()->GetEntry(0);
715     if(isDig)  GetLoader()->TreeD()->GetEntry(0);  
716     
717     for(Int_t iChamN=1;iChamN<=7;iChamN++){//chambers loop
718       if(isClus){
719         Int_t iNclusCham=Clusters(iChamN)->GetEntries(); if(iNclusCham) pNumClusH1->Fill(iNclusCham);//number of clusters per event
720         for(Int_t iClusN=0;iClusN<iNclusCham;iClusN++){//clusters loop
721           AliRICHcluster *pClus=(AliRICHcluster*)Clusters(iChamN)->At(iClusN);
722                                        pQdcH1        ->Fill(pClus->Q());   
723                                        pSizeH1       ->Fill(pClus->Size());  
724                                        pMapH2        ->Fill(pClus->X(),pClus->Y()); //common
725                                        
726            if(pClus->IsSingleMip())     {pPureMipQdcH1 ->Fill(pClus->Q());
727                                        pPureMipSizeH1->Fill(pClus->Size());
728                                        pPureMipMapH2 ->Fill(pClus->X(),pClus->Y());}//Pure Mips
729                                        
730            if(pClus->IsSingleCerenkov()){pPureCerQdcH1 ->Fill(pClus->Q());
731                                        pPureCerSizeH1->Fill(pClus->Size());
732                                        pPureCerMapH2 ->Fill(pClus->X(),pClus->Y());}//Pure Cerenkovs
733                                        
734            if(pClus->IsSingleFeedback()){pPureFeeQdcH1 ->Fill(pClus->Q());
735                                        pPureFeeSizeH1->Fill(pClus->Size());
736                                        pPureFeeMapH2 ->Fill(pClus->X(),pClus->Y());}//Pure Feedbacks
737            
738            if(pClus->IsMip()) {pMipQdcH1 ->Fill(pClus->Q());} //MIP+ other contributions
739            if(!pClus->IsPureMip())     pPhotQdcH1->Fill(pClus->Q());  //not MIP
740         }//clusters loop
741       }//isClus
742       if(isDig){
743         for(Int_t iDigN=0;iDigN<R()->Digits(iChamN)->GetEntries();iDigN++){//digits loop
744           AliRICHdigit *pDig=(AliRICHdigit*)R()->Digits(iChamN)->At(iDigN);
745           AliRICHhit   *pHit=Hit(pDig->GetTrack(0));
746           TVector2 hitV2=R()->C(iChamN)->Glob2Loc(pHit->OutX3()); TVector2 digV2=R()->P()->Pad2Loc(pDig->Pad());
747           pHxD->Fill(hitV2.X()-digV2.X()); pHyD->Fill(hitV2.Y()-digV2.Y());
748         }//digits loop
749       }//isDig
750     }//chambers loop
751     Info("ControlPlots","Event %i processed.",iEvtN);
752   }//events loop 
753   
754   if(isDig)  GetLoader()->UnloadDigits();
755   if(isClus) GetLoader()->UnloadRecPoints();
756   
757   pFile->Write(); delete pFile;
758   sw.Print();time.Print();
759 }//ControlPlots()
760 //__________________________________________________________________________________________________
761 AliRICHhit* AliRICH::Hit(Int_t tid)
762 {
763 //defines which hit provided by given tid for the currently loaded event
764   R()->GetLoader()->LoadHits();
765   for(Int_t iPrimN=0;iPrimN<R()->GetLoader()->TreeH()->GetEntries();iPrimN++){//prims loop      
766     R()->GetLoader()->TreeH()->GetEntry(iPrimN);
767     for(Int_t iHitN=0;iHitN<R()->Hits()->GetEntries();iHitN++){
768       AliRICHhit *pHit=(AliRICHhit*)R()->Hits()->At(iHitN);
769       if(tid==pHit->Track()) {R()->GetLoader()->UnloadHits();return pHit;}
770     }//hits
771   }//prims loop
772   R()->GetLoader()->UnloadHits();
773   return 0;
774 }
775 //__________________________________________________________________________________________________
776 void AliRICH::PrintHits(Int_t iEvtN)
777 {
778 //Prints a list of RICH hits for a given event. Default is event number 0.
779   Info("PrintHits","List of RICH hits for event %i",iEvtN);
780   R()->GetLoader()->GetRunLoader()->GetEvent(iEvtN);    
781   if(R()->GetLoader()->LoadHits()) return;
782   
783   Int_t iTotalHits=0;
784   for(Int_t iPrimN=0;iPrimN<R()->GetLoader()->TreeH()->GetEntries();iPrimN++){//prims loop
785     R()->GetLoader()->TreeH()->GetEntry(iPrimN);      
786     R()->Hits()->Print();
787     iTotalHits+=R()->Hits()->GetEntries();
788   }
789   R()->GetLoader()->UnloadHits();
790   Info("PrintHits","totally %i hits",iTotalHits);
791 }
792 //__________________________________________________________________________________________________
793 void AliRICH::PrintSDigits(Int_t iEvtN)
794 {
795 //prints a list of RICH sdigits  for a given event
796   Info("PrintSDigits","List of RICH sdigits for event %i",iEvtN);
797   R()->GetLoader()->GetRunLoader()->GetEvent(iEvtN);    
798   if(R()->GetLoader()->LoadSDigits()) return;
799   
800   R()->GetLoader()->TreeS()->GetEntry(0);
801   R()->SDigits()->Print();
802   R()->GetLoader()->UnloadSDigits();
803   Info("PrintSDigits","totally %i sdigits",R()->SDigits()->GetEntries());
804 }
805 //__________________________________________________________________________________________________
806 void AliRICH::PrintDigits(Int_t iEvtN)
807 {
808 //prints a list of RICH digits  for a given event
809   Info("PrintDigits","List of RICH digits for event %i",iEvtN);
810   R()->GetLoader()->GetRunLoader()->GetEvent(iEvtN);    
811   if(R()->GetLoader()->LoadDigits()) return;
812   
813   Int_t iTotalDigits=0;
814   R()->GetLoader()->TreeD()->GetEntry(0);
815   for(Int_t iChamber=1;iChamber<=kNchambers;iChamber++){
816     R()->Digits(iChamber)->Print();
817     iTotalDigits+=R()->Digits(iChamber)->GetEntries();
818   }
819   R()->GetLoader()->UnloadDigits();
820   Info("PrintDigits","totally %i Digits",iTotalDigits);
821 }
822 //__________________________________________________________________________________________________
823 void AliRICH::PrintClusters(Int_t iEvtN)
824 {
825 //prints a list of RICH clusters  for a given event
826   Info("PrintClusters","List of RICH clusters for event %i",iEvtN);
827   R()->GetLoader()->GetRunLoader()->GetEvent(iEvtN);    
828   if(R()->GetLoader()->LoadRecPoints()) return;
829   
830   Int_t iTotalClusters=0;
831   R()->GetLoader()->TreeR()->GetEntry(0);
832   for(Int_t iChamber=1;iChamber<=kNchambers;iChamber++){
833     R()->Clusters(iChamber)->Print();
834     iTotalClusters+=R()->Clusters(iChamber)->GetEntries();
835   }
836   R()->GetLoader()->UnloadRecPoints();
837   Info("PrintClusters","totally %i clusters",iTotalClusters);
838 }
839 //__________________________________________________________________________________________________
840 void AliRICH::FillESD(AliESD *pESD)const
841 {
842 //This methode fills AliESDtrack with information from RICH  
843   Info("FillESD","Start with %i tracks",pESD->GetNumberOfTracks());
844   const Double_t masses[5]={0.000511,0.105658,0.139567,0.493677,0.93828};//electron,muon,pion,kaon,proton
845   const Double_t refIndex = 1.29052;
846
847   Double_t thetaExp = 0.7;
848   Double_t thetaTh[5];
849   Double_t sinThetaThNorm;
850   Double_t sigmaThetaTh[5];
851   Double_t height[5];
852   Double_t totalHeight=0;
853   
854   for(Int_t iTrackN=0;iTrackN<pESD->GetNumberOfTracks();iTrackN++){//ESD tracks loop
855     AliESDtrack *pTrack = pESD->GetTrack(iTrackN);
856     
857     pTrack->Print("");
858 //    TVector2 x2=P()->HelixCross(pTrack);//returns cross point of track with RICH PC in LRS
859     Double_t pmod = pTrack->GetP();
860     
861     for(Int_t iPart=4;iPart>=0;iPart--){
862       Double_t cosThetaTh = TMath::Sqrt(masses[iPart]*masses[iPart]+pmod*pmod)/(refIndex*pmod);
863       if(cosThetaTh>=1) {pTrack->ResetRICH(); break;}
864       thetaTh[iPart] = TMath::ACos(cosThetaTh);
865       sinThetaThNorm = TMath::Sin(thetaTh[iPart])/TMath::Sqrt(1-1/(refIndex*refIndex));
866       sigmaThetaTh[iPart] = (0.014*(1/sinThetaThNorm-1) + 0.0043)*1.25;
867       height[iPart] = TMath::Gaus(thetaExp,thetaTh[iPart],sigmaThetaTh[iPart]);
868       totalHeight +=height[iPart];
869     }
870     
871     pTrack->SetRICHsignal(thetaExp);
872     Double_t richPID[5];
873     for(Int_t iPart=0;iPart<5;iPart++) richPID[iPart] = height[iPart]/totalHeight;    
874     pTrack->SetRICHpid(richPID); 
875   }//ESD tracks loop
876 }