]> git.uio.no Git - u/mrichter/AliRoot.git/blob - RICH/AliRICH.cxx
Minor corrections for HP, DEC and Sun; strings.h included
[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 /*
17   $Log$
18   Revision 1.22  2000/09/12 18:11:13  fca
19   zero hits area before using
20
21   Revision 1.21  2000/07/21 10:21:07  morsch
22   fNrawch   = 0; and  fNrechits = 0; in the default constructor.
23
24   Revision 1.20  2000/07/10 15:28:39  fca
25   Correction of the inheritance scheme
26
27   Revision 1.19  2000/06/30 16:29:51  dibari
28   Added kDebugLevel variable to control output size on demand
29
30   Revision 1.18  2000/06/12 15:15:46  jbarbosa
31   Cleaned up version.
32
33   Revision 1.17  2000/06/09 14:58:37  jbarbosa
34   New digitisation per particle type
35
36   Revision 1.16  2000/04/19 12:55:43  morsch
37   Newly structured and updated version (JB, AM)
38
39 */
40
41
42 ////////////////////////////////////////////////
43 //  Manager and hits classes for set:RICH     //
44 ////////////////////////////////////////////////
45
46 #include <TBRIK.h>
47 #include <TTUBE.h>
48 #include <TNode.h> 
49 #include <TRandom.h> 
50 #include <TObject.h>
51 #include <TVector.h>
52 #include <TObjArray.h>
53 #include <TArrayF.h>
54 #include <TFile.h>
55 #include <TParticle.h>
56 #include <iostream.h>
57 #include <strings.h>
58
59 #include "AliRICH.h"
60 #include "AliRICHSegmentation.h"
61 #include "AliRICHHit.h"
62 #include "AliRICHCerenkov.h"
63 #include "AliRICHPadHit.h"
64 #include "AliRICHDigit.h"
65 #include "AliRICHTransientDigit.h"
66 #include "AliRICHRawCluster.h"
67 #include "AliRICHRecHit.h"
68 #include "AliRICHHitMapA1.h"
69 #include "AliRICHClusterFinder.h"
70 #include "AliRun.h"
71 #include "AliMC.h"
72 #include "AliConst.h"
73 #include "AliPDG.h"
74 #include "AliPoints.h"
75 #include "AliCallf77.h" 
76 #include "TGeant3.h"
77
78
79 // Static variables for the pad-hit iterator routines
80 static Int_t sMaxIterPad=0;
81 static Int_t sCurIterPad=0;
82 static TClonesArray *fClusters2;
83 static TClonesArray *fHits2;
84 static TTree *TrH1;
85  
86 ClassImp(AliRICH)
87     
88 //___________________________________________
89 AliRICH::AliRICH()
90 {
91 // Default constructor for RICH manager class
92
93     fIshunt     = 0;
94     fHits       = 0;
95     fPadHits    = 0;
96     fNPadHits   = 0;
97     fNcerenkovs = 0;
98     fDchambers  = 0;
99     fCerenkovs  = 0;
100     fNdch       = 0;
101     fNrawch   = 0;
102     fNrechits = 0;
103 }
104
105 //___________________________________________
106 AliRICH::AliRICH(const char *name, const char *title)
107     : AliDetector(name,title)
108 {
109 //Begin_Html
110 /*
111   <img src="gif/alirich.gif">
112 */
113 //End_Html
114     
115     fHits       = new TClonesArray("AliRICHHit",1000  );
116     gAlice->AddHitList(fHits);
117     fPadHits    = new TClonesArray("AliRICHPadHit",100000);
118     fCerenkovs  = new TClonesArray("AliRICHCerenkov",1000);
119     gAlice->AddHitList(fCerenkovs);
120     //gAlice->AddHitList(fHits);
121     fNPadHits   = 0;
122     fNcerenkovs = 0;
123     fIshunt     = 0;
124     
125     fNdch      = new Int_t[kNCH];
126     
127     fDchambers = new TObjArray(kNCH);
128
129     fRecHits = new TObjArray(kNCH);
130     
131     Int_t i;
132    
133     for (i=0; i<kNCH ;i++) {
134         (*fDchambers)[i] = new TClonesArray("AliRICHDigit",10000); 
135         fNdch[i]=0;
136     }
137
138     fNrawch      = new Int_t[kNCH];
139     
140     fRawClusters = new TObjArray(kNCH);
141     //printf("Created fRwClusters with adress:%p",fRawClusters);
142
143     for (i=0; i<kNCH ;i++) {
144       (*fRawClusters)[i] = new TClonesArray("AliRICHRawCluster",10000); 
145       fNrawch[i]=0;
146     }
147
148     fNrechits      = new Int_t[kNCH];
149     
150     for (i=0; i<kNCH ;i++) {
151         (*fRecHits)[i] = new TClonesArray("AliRICHRecHit",1000); 
152     }
153     //printf("Created fRecHits with adress:%p",fRecHits);
154
155         
156     SetMarkerColor(kRed);
157 }
158
159 AliRICH::AliRICH(const AliRICH& RICH)
160 {
161 // Copy Constructor
162 }
163
164
165 //___________________________________________
166 AliRICH::~AliRICH()
167 {
168
169 // Destructor of RICH manager class
170
171     fIshunt  = 0;
172     delete fHits;
173     delete fPadHits;
174     delete fCerenkovs;
175 }
176
177 //___________________________________________
178 void AliRICH::AddHit(Int_t track, Int_t *vol, Float_t *hits)
179 {
180
181 //  
182 // Adds a hit to the Hits list
183 //
184
185     TClonesArray &lhits = *fHits;
186     new(lhits[fNhits++]) AliRICHHit(fIshunt,track,vol,hits);
187 }
188 //_____________________________________________________________________________
189 void AliRICH::AddCerenkov(Int_t track, Int_t *vol, Float_t *cerenkovs)
190 {
191
192 //
193 // Adds a RICH cerenkov hit to the Cerenkov Hits list
194 //
195
196     TClonesArray &lcerenkovs = *fCerenkovs;
197     new(lcerenkovs[fNcerenkovs++]) AliRICHCerenkov(fIshunt,track,vol,cerenkovs);
198     //printf ("Done for Cerenkov %d\n\n\n\n",fNcerenkovs);
199 }
200 //___________________________________________
201 void AliRICH::AddPadHit(Int_t *clhits)
202 {
203
204 //
205 // Add a RICH pad hit to the list
206 //
207
208     TClonesArray &lPadHits = *fPadHits;
209     new(lPadHits[fNPadHits++]) AliRICHPadHit(clhits);
210
211 //_____________________________________________________________________________
212 void AliRICH::AddDigits(Int_t id, Int_t *tracks, Int_t *charges, Int_t *digits)
213 {
214
215   //
216   // Add a RICH digit to the list
217   //
218
219     TClonesArray &ldigits = *((TClonesArray*)(*fDchambers)[id]);
220     new(ldigits[fNdch[id]++]) AliRICHDigit(tracks,charges,digits);
221 }
222
223 //_____________________________________________________________________________
224 void AliRICH::AddRawCluster(Int_t id, const AliRICHRawCluster& c)
225 {
226     //
227     // Add a RICH digit to the list
228     //
229
230     TClonesArray &lrawcl = *((TClonesArray*)(*fRawClusters)[id]);
231     new(lrawcl[fNrawch[id]++]) AliRICHRawCluster(c);
232 }
233
234 //_____________________________________________________________________________
235 void AliRICH::AddRecHit(Int_t id, Float_t *rechit, Float_t *photons, Int_t *padsx, Int_t* padsy)
236 {
237   
238   //
239   // Add a RICH reconstructed hit to the list
240   //
241
242     TClonesArray &lrec = *((TClonesArray*)(*fRecHits)[id]);
243     new(lrec[fNrechits[id]++]) AliRICHRecHit(id,rechit,photons,padsx,padsy);
244 }
245
246 //___________________________________________
247 void AliRICH::BuildGeometry()
248     
249 {
250   
251   //
252   // Builds a TNode geometry for event display
253   //
254     TNode *node, *top;
255     
256     const int kColorRICH = kGreen;
257     //
258     top=gAlice->GetGeometry()->GetNode("alice");
259     
260     
261     new TBRIK("S_RICH","S_RICH","void",71.09999,11.5,73.15);
262     
263     top->cd();
264     Float_t pos1[3]={0,471.8999,165.2599};
265     //Chamber(0).SetChamberTransform(pos1[0],pos1[1],pos1[2],
266     new TRotMatrix("rot993","rot993",90,0,70.69,90,19.30999,-90);
267     node = new TNode("RICH1","RICH1","S_RICH",pos1[0],pos1[1],pos1[2],"rot993");
268     
269
270     node->SetLineColor(kColorRICH);
271     fNodes->Add(node);
272     top->cd();
273     
274     Float_t pos2[3]={171,470,0};
275     //Chamber(1).SetChamberTransform(pos2[0],pos2[1],pos2[2],
276     new TRotMatrix("rot994","rot994",90,-20,90,70,0,0);
277     node = new TNode("RICH2","RICH2","S_RICH",pos2[0],pos2[1],pos2[2],"rot994");
278     
279     
280     node->SetLineColor(kColorRICH);
281     fNodes->Add(node);
282     top->cd();
283     Float_t pos3[3]={0,500,0};
284     //Chamber(2).SetChamberTransform(pos3[0],pos3[1],pos3[2],
285     new TRotMatrix("rot995","rot995",90,0,90,90,0,0);
286     node = new TNode("RICH3","RICH3","S_RICH",pos3[0],pos3[1],pos3[2],"rot995");
287     
288
289     node->SetLineColor(kColorRICH);
290     fNodes->Add(node);
291     top->cd();
292     Float_t pos4[3]={-171,470,0};
293     //Chamber(3).SetChamberTransform(pos4[0],pos4[1],pos4[2], 
294     new TRotMatrix("rot996","rot996",90,20,90,110,0,0);  
295     node = new TNode("RICH4","RICH4","S_RICH",pos4[0],pos4[1],pos4[2],"rot996");
296     
297
298     node->SetLineColor(kColorRICH);
299     fNodes->Add(node);
300     top->cd();
301     Float_t pos5[3]={161.3999,443.3999,-165.3};
302     //Chamber(4).SetChamberTransform(pos5[0],pos5[1],pos5[2],
303     new TRotMatrix("rot997","rot997",90,340,108.1999,70,18.2,70);
304     node = new TNode("RICH5","RICH5","S_RICH",pos5[0],pos5[1],pos5[2],"rot997");
305     
306     node->SetLineColor(kColorRICH);
307     fNodes->Add(node);
308     top->cd();
309     Float_t pos6[3]={0., 471.9, -165.3,};
310     //Chamber(5).SetChamberTransform(pos6[0],pos6[1],pos6[2],
311     new TRotMatrix("rot998","rot998",90,0,109.3099,90,19.30999,90);
312     node = new TNode("RICH6","RICH6","S_RICH",pos6[0],pos6[1],pos6[2],"rot998");
313     
314     
315     node->SetLineColor(kColorRICH);
316     fNodes->Add(node);
317     top->cd();
318     Float_t pos7[3]={-161.399,443.3999,-165.3};
319     //Chamber(6).SetChamberTransform(pos7[0],pos7[1],pos7[2],
320     new TRotMatrix("rot999","rot999",90,20,108.1999,110,18.2,110);
321     node = new TNode("RICH7","RICH7","S_RICH",pos7[0],pos7[1],pos7[2],"rot999");
322     node->SetLineColor(kColorRICH);
323     fNodes->Add(node); 
324     
325 }
326
327 //___________________________________________
328 void AliRICH::CreateGeometry()
329 {
330     //
331     // Create the geometry for RICH version 1
332     //
333     // Modified by:  N. Colonna (INFN - BARI, Nicola.Colonna@ba.infn.it) 
334     //               R.A. Fini  (INFN - BARI, Rosanna.Fini@ba.infn.it) 
335     //               R.A. Loconsole (Bari University, loco@riscom.ba.infn.it) 
336     //
337     //Begin_Html
338     /*
339       <img src="picts/AliRICHv1.gif">
340     */
341     //End_Html
342     //Begin_Html
343     /*
344       <img src="picts/AliRICHv1Tree.gif">
345     */
346     //End_Html
347
348   AliRICH *pRICH = (AliRICH *) gAlice->GetDetector("RICH"); 
349   AliRICHSegmentation*  segmentation;
350   AliRICHGeometry*  geometry;
351   AliRICHChamber*       iChamber;
352
353   iChamber = &(pRICH->Chamber(0));
354   segmentation=iChamber->GetSegmentationModel(0);
355   geometry=iChamber->GetGeometryModel();
356
357   Float_t distance;
358   distance = geometry->GetFreonThickness()/2 + geometry->GetQuartzThickness() + geometry->GetGapThickness();
359   geometry->SetRadiatorToPads(distance);
360     
361     
362     Int_t *idtmed = fIdtmed->GetArray()-999;
363     
364     Int_t i;
365     Float_t zs;
366     Int_t idrotm[1099];
367     Float_t par[3];
368     
369     // --- Define the RICH detector 
370     //     External aluminium box 
371     par[0] = 71.1;
372     par[1] = 11.5;                 //Original Settings
373     par[2] = 73.15;
374     /*par[0] = 73.15;
375     par[1] = 11.5;
376     par[2] = 71.1;*/
377     gMC->Gsvolu("RICH", "BOX ", idtmed[1009], par, 3);
378     
379     //     Sensitive part of the whole RICH 
380     par[0] = 64.8;
381     par[1] = 11.5;                 //Original Settings
382     par[2] = 66.55;
383     /*par[0] = 66.55;
384     par[1] = 11.5;
385     par[2] = 64.8;*/
386     gMC->Gsvolu("SRIC", "BOX ", idtmed[1000], par, 3);
387     
388     //     Honeycomb 
389     par[0] = 63.1;
390     par[1] = .188;                 //Original Settings
391     par[2] = 66.55;
392     /*par[0] = 66.55;
393     par[1] = .188;
394     par[2] = 63.1;*/
395     gMC->Gsvolu("HONE", "BOX ", idtmed[1001], par, 3);
396     
397     //     Aluminium sheet 
398     par[0] = 63.1;
399     par[1] = .025;                 //Original Settings
400     par[2] = 66.55;
401     /*par[0] = 66.5;
402     par[1] = .025;
403     par[2] = 63.1;*/
404     gMC->Gsvolu("ALUM", "BOX ", idtmed[1009], par, 3);
405     
406     //     Quartz 
407     par[0] = geometry->GetQuartzWidth()/2;
408     par[1] = geometry->GetQuartzThickness()/2;
409     par[2] = geometry->GetQuartzLength()/2;
410     /*par[0] = 63.1;
411     par[1] = .25;                  //Original Settings
412     par[2] = 65.5;*/
413     /*par[0] = geometry->GetQuartzWidth()/2;
414     par[1] = geometry->GetQuartzThickness()/2;
415     par[2] = geometry->GetQuartzLength()/2;*/
416     //printf("\n\n\n\n\n\n\n\\n\n\n\n Gap Thickness: %f %f %f\n\n\n\n\n\n\n\n\n\n\n\n\n\n",par[0],par[1],par[2]);
417     gMC->Gsvolu("QUAR", "BOX ", idtmed[1002], par, 3);
418     
419     //     Spacers (cylinders) 
420     par[0] = 0.;
421     par[1] = .5;
422     par[2] = geometry->GetFreonThickness()/2;
423     gMC->Gsvolu("SPAC", "TUBE", idtmed[1002], par, 3);
424     
425     //     Opaque quartz 
426     par[0] = 61.95;
427     par[1] = .2;                   //Original Settings
428     par[2] = 66.5;
429     /*par[0] = 66.5;
430     par[1] = .2;
431     par[2] = 61.95;*/
432     gMC->Gsvolu("OQUA", "BOX ", idtmed[1007], par, 3);
433   
434     //     Frame of opaque quartz
435     par[0] = geometry->GetOuterFreonWidth()/2;
436     par[1] = geometry->GetFreonThickness()/2;
437     par[2] = geometry->GetOuterFreonLength()/2 + 1; 
438     /*par[0] = 20.65;
439     par[1] = .5;                   //Original Settings
440     par[2] = 66.5;*/
441     /*par[0] = 66.5;
442     par[1] = .5;
443     par[2] = 20.65;*/
444     gMC->Gsvolu("OQF1", "BOX ", idtmed[1007], par, 3);
445
446     par[0] = geometry->GetInnerFreonWidth()/2;
447     par[1] = geometry->GetFreonThickness()/2;
448     par[2] = geometry->GetInnerFreonLength()/2 + 1; 
449     gMC->Gsvolu("OQF2", "BOX ", idtmed[1007], par, 3);
450     
451     //     Little bar of opaque quartz 
452     par[0] = .275;
453     par[1] = geometry->GetQuartzThickness()/2;
454     par[2] = geometry->GetInnerFreonLength()/2 - 2.4; 
455     /*par[0] = .275;
456     par[1] = .25;                   //Original Settings
457     par[2] = 63.1;*/
458     /*par[0] = 63.1;
459     par[1] = .25;
460     par[2] = .275;*/
461     gMC->Gsvolu("BARR", "BOX ", idtmed[1007], par, 3);
462     
463     //     Freon 
464     par[0] = geometry->GetOuterFreonWidth()/2;
465     par[1] = geometry->GetFreonThickness()/2;
466     par[2] = geometry->GetOuterFreonLength()/2; 
467     /*par[0] = 20.15;
468     par[1] = .5;                   //Original Settings
469     par[2] = 65.5;*/
470     /*par[0] = 65.5;
471     par[1] = .5;
472     par[2] = 20.15;*/
473     gMC->Gsvolu("FRE1", "BOX ", idtmed[1003], par, 3);
474
475     par[0] = geometry->GetInnerFreonWidth()/2;
476     par[1] = geometry->GetFreonThickness()/2;
477     par[2] = geometry->GetInnerFreonLength()/2; 
478     gMC->Gsvolu("FRE2", "BOX ", idtmed[1003], par, 3);
479     
480     //     Methane 
481     par[0] = 64.8;
482     par[1] = geometry->GetGapThickness()/2;
483     //printf("\n\n\n\n\n\n\n\\n\n\n\n Gap Thickness: %f\n\n\n\n\n\n\n\n\n\n\n\n\n\n",par[1]);
484     par[2] = 64.8;
485     gMC->Gsvolu("META", "BOX ", idtmed[1004], par, 3);
486     
487     //     Methane gap 
488     par[0] = 64.8;
489     par[1] = geometry->GetProximityGapThickness()/2;
490     //printf("\n\n\n\n\n\n\n\\n\n\n\n Gap Thickness: %f\n\n\n\n\n\n\n\n\n\n\n\n\n\n",par[1]);
491     par[2] = 64.8;
492     gMC->Gsvolu("GAP ", "BOX ", idtmed[1008], par, 3);
493     
494     //     CsI photocathode 
495     par[0] = 64.8;
496     par[1] = .25;
497     par[2] = 64.8;
498     gMC->Gsvolu("CSI ", "BOX ", idtmed[1005], par, 3);
499     
500     //     Anode grid 
501     par[0] = 0.;
502     par[1] = .001;
503     par[2] = 20.;
504     gMC->Gsvolu("GRID", "TUBE", idtmed[1006], par, 3);
505     
506     // --- Places the detectors defined with GSVOLU 
507     //     Place material inside RICH 
508     gMC->Gspos("SRIC", 1, "RICH", 0., 0., 0., 0, "ONLY");
509     
510     gMC->Gspos("ALUM", 1, "SRIC", 0., 1.276 - geometry->GetGapThickness()/2 - geometry->GetQuartzThickness() - geometry->GetFreonThickness()- .4 -.05 - .376 -.025, 0., 0, "ONLY");
511     gMC->Gspos("HONE", 1, "SRIC", 0., 1.276- geometry->GetGapThickness()/2  - geometry->GetQuartzThickness() - geometry->GetFreonThickness()- .4 -.05 - .188, 0., 0, "ONLY");
512     gMC->Gspos("ALUM", 2, "SRIC", 0., 1.276 - geometry->GetGapThickness()/2 - geometry->GetQuartzThickness() - geometry->GetFreonThickness()- .4 - .025, 0., 0, "ONLY");
513     gMC->Gspos("OQUA", 1, "SRIC", 0., 1.276 - geometry->GetGapThickness()/2 - geometry->GetQuartzThickness() - geometry->GetFreonThickness()- .2, 0., 0, "ONLY");
514     
515     AliMatrix(idrotm[1019], 0., 0., 90., 0., 90., 90.);
516     
517     Int_t nspacers = (Int_t)(TMath::Abs(geometry->GetInnerFreonLength()/14.4));
518     //printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n Spacers:%d\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n",nspacers); 
519
520     //printf("Nspacers: %d", nspacers);
521     
522     //for (i = 1; i <= 9; ++i) {
523       //zs = (5 - i) * 14.4;                       //Original settings 
524     for (i = 0; i < nspacers; i++) {
525         zs = (TMath::Abs(nspacers/2) - i) * 14.4;
526         gMC->Gspos("SPAC", i, "FRE1", 6.7, 0., zs, idrotm[1019], "ONLY");  //Original settings 
527         //gMC->Gspos("SPAC", i, "FRE1", zs, 0., 6.7, idrotm[1019], "ONLY"); 
528     }
529     //for (i = 10; i <= 18; ++i) {
530       //zs = (14 - i) * 14.4;                       //Original settings 
531     for (i = nspacers; i < nspacers*2; ++i) {
532         zs = (nspacers + TMath::Abs(nspacers/2) - i) * 14.4;
533         gMC->Gspos("SPAC", i, "FRE1", -6.7, 0., zs, idrotm[1019], "ONLY"); //Original settings  
534         //gMC->Gspos("SPAC", i, "FRE1", zs, 0., -6.7, idrotm[1019], "ONLY");  
535     }
536
537     //for (i = 1; i <= 9; ++i) {
538       //zs = (5 - i) * 14.4;                       //Original settings 
539       for (i = 0; i < nspacers; i++) {
540         zs = (TMath::Abs(nspacers/2) - i) * 14.4;
541         gMC->Gspos("SPAC", i, "FRE2", 6.7, 0., zs, idrotm[1019], "ONLY");  //Original settings 
542         //gMC->Gspos("SPAC", i, "FRE2", zs, 0., 6.7, idrotm[1019], "ONLY");
543     }
544     //for (i = 10; i <= 18; ++i) {
545       //zs = (5 - i) * 14.4;                       //Original settings 
546       for (i = nspacers; i < nspacers*2; ++i) {
547         zs = (nspacers + TMath::Abs(nspacers/2) - i) * 14.4;
548         gMC->Gspos("SPAC", i, "FRE2", -6.7, 0., zs, idrotm[1019], "ONLY");  //Original settings 
549         //gMC->Gspos("SPAC", i, "FRE2", zs, 0., -6.7, idrotm[1019], "ONLY");
550     }
551     
552     /*gMC->Gspos("FRE1", 1, "OQF1", 0., 0., 0., 0, "ONLY");
553     gMC->Gspos("FRE2", 1, "OQF2", 0., 0., 0., 0, "ONLY");
554     gMC->Gspos("OQF1", 1, "SRIC", 31.3, -4.724, 41.3, 0, "ONLY");
555     gMC->Gspos("OQF2", 2, "SRIC", 0., -4.724, 0., 0, "ONLY");
556     gMC->Gspos("OQF1", 3, "SRIC", -31.3, -4.724, -41.3, 0, "ONLY");
557     gMC->Gspos("BARR", 1, "QUAR", -21.65, 0., 0., 0, "ONLY");           //Original settings 
558     gMC->Gspos("BARR", 2, "QUAR", 21.65, 0., 0., 0, "ONLY");            //Original settings 
559     gMC->Gspos("QUAR", 1, "SRIC", 0., -3.974, 0., 0, "ONLY");
560     gMC->Gspos("GAP ", 1, "META", 0., 4.8, 0., 0, "ONLY");
561     gMC->Gspos("META", 1, "SRIC", 0., 1.276, 0., 0, "ONLY");
562     gMC->Gspos("CSI ", 1, "SRIC", 0., 6.526, 0., 0, "ONLY");*/
563
564
565     gMC->Gspos("FRE1", 1, "OQF1", 0., 0., 0., 0, "ONLY");
566     gMC->Gspos("FRE2", 1, "OQF2", 0., 0., 0., 0, "ONLY");
567     gMC->Gspos("OQF1", 1, "SRIC", geometry->GetOuterFreonWidth()/2 + geometry->GetInnerFreonWidth()/2, 1.276 - geometry->GetGapThickness()/2- geometry->GetQuartzThickness() -geometry->GetFreonThickness()/2, 0., 0, "ONLY"); //Original settings (31.3)
568     gMC->Gspos("OQF2", 2, "SRIC", 0., 1.276 - geometry->GetGapThickness()/2 - geometry->GetQuartzThickness() - geometry->GetFreonThickness()/2, 0., 0, "ONLY");          //Original settings 
569     gMC->Gspos("OQF1", 3, "SRIC", - (geometry->GetOuterFreonWidth()/2 + geometry->GetInnerFreonWidth()/2), 1.276 - geometry->GetGapThickness()/2 - geometry->GetQuartzThickness() - geometry->GetFreonThickness()/2, 0., 0, "ONLY");       //Original settings (-31.3)
570     gMC->Gspos("BARR", 1, "QUAR", -21.65, 0., 0., 0, "ONLY");           //Original settings 
571     gMC->Gspos("BARR", 2, "QUAR", 21.65, 0., 0., 0, "ONLY");            //Original settings 
572     gMC->Gspos("QUAR", 1, "SRIC", 0., 1.276 - geometry->GetGapThickness()/2 - geometry->GetQuartzThickness()/2, 0., 0, "ONLY");
573     gMC->Gspos("GAP ", 1, "META", 0., geometry->GetGapThickness()/2 - geometry->GetProximityGapThickness()/2 - 0.0001, 0., 0, "ONLY");
574     gMC->Gspos("META", 1, "SRIC", 0., 1.276, 0., 0, "ONLY");
575     gMC->Gspos("CSI ", 1, "SRIC", 0., 1.276 + geometry->GetGapThickness()/2 + .25, 0., 0, "ONLY");
576
577     //printf("Position of the gap: %f to %f\n", 1.276 + geometry->GetGapThickness()/2 - geometry->GetProximityGapThickness()/2 - .2, 1.276 + geometry->GetGapThickness()/2 - geometry->GetProximityGapThickness()/2 + .2);
578     
579     //     Place RICH inside ALICE apparatus 
580   
581     AliMatrix(idrotm[1000], 90., 0., 70.69, 90., 19.31, -90.);
582     AliMatrix(idrotm[1001], 90., -20., 90., 70., 0., 0.);
583     AliMatrix(idrotm[1002], 90., 0., 90., 90., 0., 0.);
584     AliMatrix(idrotm[1003], 90., 20., 90., 110., 0., 0.);
585     AliMatrix(idrotm[1004], 90., 340., 108.2, 70., 18.2, 70.);
586     AliMatrix(idrotm[1005], 90., 0., 109.31, 90., 19.31, 90.);
587     AliMatrix(idrotm[1006], 90., 20., 108.2, 110., 18.2, 110.);
588     
589     gMC->Gspos("RICH", 1, "ALIC", 0., 471.9, 165.26,     idrotm[1000], "ONLY");
590     gMC->Gspos("RICH", 2, "ALIC", 171., 470., 0.,        idrotm[1001], "ONLY");
591     gMC->Gspos("RICH", 3, "ALIC", 0., 500., 0.,          idrotm[1002], "ONLY");
592     gMC->Gspos("RICH", 4, "ALIC", -171., 470., 0.,       idrotm[1003], "ONLY");
593     gMC->Gspos("RICH", 5, "ALIC", 161.4, 443.4, -165.3,  idrotm[1004], "ONLY");
594     gMC->Gspos("RICH", 6, "ALIC", 0., 471.9, -165.3,     idrotm[1005], "ONLY");
595     gMC->Gspos("RICH", 7, "ALIC", -161.4, 443.4, -165.3, idrotm[1006], "ONLY");
596     
597 }
598
599
600 //___________________________________________
601 void AliRICH::CreateMaterials()
602 {
603     //
604     // *** DEFINITION OF AVAILABLE RICH MATERIALS *** 
605     // ORIGIN    : NICK VAN EIJNDHOVEN 
606     // Modified by:  N. Colonna (INFN - BARI, Nicola.Colonna@ba.infn.it) 
607     //               R.A. Fini  (INFN - BARI, Rosanna.Fini@ba.infn.it) 
608     //               R.A. Loconsole (Bari University, loco@riscom.ba.infn.it) 
609     //
610     Int_t   isxfld = gAlice->Field()->Integ();
611     Float_t sxmgmx = gAlice->Field()->Max();
612     Int_t i;
613
614     /************************************Antonnelo's Values (14-vectors)*****************************************/
615     /*
616     Float_t ppckov[14] = { 5.63e-9,5.77e-9,5.9e-9,6.05e-9,6.2e-9,6.36e-9,6.52e-9,
617                            6.7e-9,6.88e-9,7.08e-9,7.3e-9,7.51e-9,7.74e-9,8e-9 };
618     Float_t rIndexQuarz[14] = { 1.528309,1.533333,
619                                  1.538243,1.544223,1.550568,1.55777,
620                                  1.565463,1.574765,1.584831,1.597027,
621                                1.611858,1.6277,1.6472,1.6724 };
622     Float_t rIndexOpaqueQuarz[14] = { 1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1. };
623     Float_t rIndexMethane[14] = { 1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1. };
624     Float_t rIndexGrid[14] = { 1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1. };
625     Float_t abscoFreon[14] = { 179.0987,179.0987,
626                                 179.0987,179.0987,179.0987,142.92,56.65,13.95,10.43,7.07,2.03,.5773,.33496,0. };
627     //Float_t abscoFreon[14] = { 1e-5,1e-5,1e-5,1e-5,1e-5,1e-5,1e-5,1e-5,1e-5,
628         //                       1e-5,1e-5,1e-5,1e-5,1e-5 };
629     Float_t abscoQuarz[14] = { 64.035,39.98,35.665,31.262,27.527,22.815,21.04,17.52,
630                                 14.177,9.282,4.0925,1.149,.3627,.10857 };
631     Float_t abscoOpaqueQuarz[14] = { 1e-5,1e-5,1e-5,1e-5,1e-5,1e-5,1e-5,1e-5,1e-5,
632                                  1e-5,1e-5,1e-5,1e-5,1e-5 };
633     Float_t abscoCsI[14] = { 1e-4,1e-4,1e-4,1e-4,1e-4,1e-4,1e-4,1e-4,1e-4,1e-4,
634                               1e-4,1e-4,1e-4,1e-4 };
635     Float_t abscoMethane[14] = { 1e6,1e6,1e6,1e6,1e6,1e6,1e6,1e6,1e6,1e6,1e6,
636                                   1e6,1e6,1e6 };
637     Float_t abscoGrid[14] = { 1e-4,1e-4,1e-4,1e-4,1e-4,1e-4,1e-4,1e-4,1e-4,1e-4,
638                               1e-4,1e-4,1e-4,1e-4 };
639     Float_t efficAll[14] = { 1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1. };
640     Float_t efficCsI[14] = { 6e-4,.005,.0075,.01125,.045,.117,.135,.16575,
641                               .17425,.1785,.1836,.1904,.1938,.221 };
642     Float_t efficGrid[14] = { 1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1. };
643     */
644    
645     
646     /**********************************End of Antonnelo's Values**********************************/
647     
648     /**********************************Values from rich_media.f (31-vectors)**********************************/
649     
650
651     //Photons energy intervals
652     Float_t ppckov[26];
653     for (i=0;i<26;i++) 
654     {
655         ppckov[i] = (Float_t(i)*0.1+5.5)*1e-9;
656         //printf ("Energy intervals: %e\n",ppckov[i]);
657     }
658     
659     
660     //Refraction index for quarz
661     Float_t rIndexQuarz[26];
662     Float_t  e1= 10.666;
663     Float_t  e2= 18.125;
664     Float_t  f1= 46.411;
665     Float_t  f2= 228.71;
666     for (i=0;i<26;i++)
667     {
668         Float_t ene=ppckov[i]*1e9;
669         Float_t a=f1/(e1*e1 - ene*ene);
670         Float_t b=f2/(e2*e2 - ene*ene);
671         rIndexQuarz[i] = TMath::Sqrt(1. + a + b );
672         //printf ("rIndexQuarz: %e\n",rIndexQuarz[i]);
673     } 
674     
675     //Refraction index for opaque quarz, methane and grid
676     Float_t rIndexOpaqueQuarz[26];
677     Float_t rIndexMethane[26];
678     Float_t rIndexGrid[26];
679     for (i=0;i<26;i++)
680     {
681         rIndexOpaqueQuarz[i]=1;
682         rIndexMethane[i]=1.000444;
683         rIndexGrid[i]=1;
684         //printf ("rIndexOpaqueQuarz , etc: %e, %e, %e\n",rIndexOpaqueQuarz[i], rIndexMethane[i], rIndexGrid[i]=1);
685     } 
686     
687     //Absorption index for freon
688     Float_t abscoFreon[26] = {179.0987, 179.0987, 179.0987, 179.0987, 179.0987,  179.0987, 179.0987, 179.0987, 
689                                179.0987, 142.9206, 56.64957, 25.58622, 13.95293, 12.03905, 10.42953, 8.804196, 
690                                7.069031, 4.461292, 2.028366, 1.293013, .577267,   .40746,  .334964, 0., 0., 0.};
691     
692     //Absorption index for quarz
693     /*Float_t Qzt [21] = {.0,.0,.005,.04,.35,.647,.769,.808,.829,.844,.853,.858,.869,.887,.903,.902,.902,
694                         .906,.907,.907,.907};
695     Float_t Wavl2[] = {150.,155.,160.0,165.0,170.0,175.0,180.0,185.0,190.0,195.0,200.0,205.0,210.0,
696                        215.0,220.0,225.0,230.0,235.0,240.0,245.0,250.0};                                 
697     Float_t abscoQuarz[31];          
698     for (Int_t i=0;i<31;i++)
699     {
700         Float_t Xlam = 1237.79 / (ppckov[i]*1e9);
701         if (Xlam <= 160) abscoQuarz[i] = 0;
702         if (Xlam > 250) abscoQuarz[i] = 1;
703         else 
704         {
705             for (Int_t j=0;j<21;j++)
706             {
707                 //printf ("Passed\n");
708                 if (Xlam > Wavl2[j] && Xlam < Wavl2[j+1])
709                 {
710                     Float_t Dabs = (Qzt[j+1] - Qzt[j])/(Wavl2[j+1] - Wavl2[j]);
711                     Float_t Abso = Qzt[j] + Dabs*(Xlam - Wavl2[j]);
712                     abscoQuarz[i] = -5.0/(TMath::Log(Abso));
713                 } 
714             }
715         }
716         printf ("abscoQuarz: %e abscoFreon: %e for energy: %e\n",abscoQuarz[i],abscoFreon[i],ppckov[i]);
717     }*/
718
719     /*Float_t abscoQuarz[31] = {49.64211, 48.41296, 47.46989, 46.50492, 45.13682, 44.47883, 43.1929 , 41.30922, 40.5943 ,
720                                39.82956, 38.98623, 38.6247 , 38.43448, 37.41084, 36.22575, 33.74852, 30.73901, 24.25086, 
721                                17.94531, 11.88753, 5.99128,  3.83503,  2.36661,  1.53155, 1.30582, 1.08574, .8779708, 
722                                .675275, 0., 0., 0.};
723     
724     for (Int_t i=0;i<31;i++)
725     {
726         abscoQuarz[i] = abscoQuarz[i]/10;
727     }*/
728
729     Float_t abscoQuarz [26] = {105.8, 65.52, 48.58, 42.85, 35.79, 31.262, 28.598, 27.527, 25.007, 22.815, 21.004,
730                                 19.266, 17.525, 15.878, 14.177, 11.719, 9.282, 6.62, 4.0925, 2.601, 1.149, .667, .3627,
731                                 .192, .1497, .10857};
732     
733     //Absorption index for methane
734     Float_t abscoMethane[26];
735     for (i=0;i<26;i++) 
736     {
737         abscoMethane[i]=AbsoCH4(ppckov[i]*1e9); 
738         //printf("abscoMethane: %e for energy: %e\n", abscoMethane[i],ppckov[i]*1e9);
739     }
740     
741     //Absorption index for opaque quarz, csi and grid, efficiency for all and grid
742     Float_t abscoOpaqueQuarz[26];
743     Float_t abscoCsI[26];
744     Float_t abscoGrid[26];
745     Float_t efficAll[26];
746     Float_t efficGrid[26];
747     for (i=0;i<26;i++)
748     { 
749         abscoOpaqueQuarz[i]=1e-5; 
750         abscoCsI[i]=1e-4; 
751         abscoGrid[i]=1e-4; 
752         efficAll[i]=1; 
753         efficGrid[i]=1;
754         //printf ("All must be 1: %e,  %e,  %e,  %e,  %e\n",abscoOpaqueQuarz[i],abscoCsI[i],abscoGrid[i],efficAll[i],efficGrid[i]);
755     } 
756     
757     //Efficiency for csi 
758     
759     Float_t efficCsI[26] = {0.000199999995, 0.000600000028, 0.000699999975, 0.00499999989, 0.00749999983, 0.010125,
760                              0.0242999997, 0.0405000001, 0.0688500032, 0.105299994, 0.121500008, 0.141749993, 0.157949999,
761                              0.162, 0.166050002, 0.167669997, 0.174299985, 0.176789999, 0.179279998, 0.182599992, 0.18592,
762                              0.187579989, 0.189239994, 0.190899998, 0.207499996, 0.215799987};
763         
764     
765
766     //FRESNEL LOSS CORRECTION FOR PERPENDICULAR INCIDENCE AND
767     //UNPOLARIZED PHOTONS
768
769     for (i=0;i<26;i++)
770     {
771         efficCsI[i] = efficCsI[i]/(1.-Fresnel(ppckov[i]*1e9,1.,0)); 
772         //printf ("Fresnel result: %e for energy: %e\n",Fresnel(ppckov[i]*1e9,1.,0),ppckov[i]*1e9);
773     }
774         
775     /*******************************************End of rich_media.f***************************************/
776
777   
778
779     
780     
781     
782     Float_t afre[2], agri, amet[2], aqua[2], ahon, zfre[2], zgri, zhon, 
783     zmet[2], zqua[2];
784     Int_t nlmatfre;
785     Float_t densquao;
786     Int_t nlmatmet, nlmatqua;
787     Float_t wmatquao[2], rIndexFreon[26];
788     Float_t aquao[2], epsil, stmin, zquao[2];
789     Int_t nlmatquao;
790     Float_t radlal, densal, tmaxfd, deemax, stemax;
791     Float_t aal, zal, radlgri, densfre, radlhon, densgri, denshon,densqua, densmet, wmatfre[2], wmatmet[2], wmatqua[2];
792     
793     Int_t *idtmed = fIdtmed->GetArray()-999;
794     
795     TGeant3 *geant3 = (TGeant3*) gMC;
796     
797     // --- Photon energy (GeV) 
798     // --- Refraction indexes 
799     for (i = 0; i < 26; ++i) {
800       rIndexFreon[i] = ppckov[i] * .0172 * 1e9 + 1.177;
801       //rIndexFreon[i] = 1;
802         //printf ("rIndexFreon: %e \n efficCsI: %e for energy: %e\n",rIndexFreon[i], efficCsI[i], ppckov[i]);
803     }
804             
805     // --- Detection efficiencies (quantum efficiency for CsI) 
806     // --- Define parameters for honeycomb. 
807     //     Used carbon of equivalent rad. lenght 
808     
809     ahon    = 12.01;
810     zhon    = 6.;
811     denshon = 2.265;
812     radlhon = 18.8;
813     
814     // --- Parameters to include in GSMIXT, relative to Quarz (SiO2) 
815     
816     aqua[0]    = 28.09;
817     aqua[1]    = 16.;
818     zqua[0]    = 14.;
819     zqua[1]    = 8.;
820     densqua    = 2.64;
821     nlmatqua   = -2;
822     wmatqua[0] = 1.;
823     wmatqua[1] = 2.;
824     
825     // --- Parameters to include in GSMIXT, relative to opaque Quarz (SiO2) 
826     
827     aquao[0]    = 28.09;
828     aquao[1]    = 16.;
829     zquao[0]    = 14.;
830     zquao[1]    = 8.;
831     densquao    = 2.64;
832     nlmatquao   = -2;
833     wmatquao[0] = 1.;
834     wmatquao[1] = 2.;
835     
836     // --- Parameters to include in GSMIXT, relative to Freon (C6F14) 
837     
838     afre[0]    = 12.;
839     afre[1]    = 19.;
840     zfre[0]    = 6.;
841     zfre[1]    = 9.;
842     densfre    = 1.7;
843     nlmatfre   = -2;
844     wmatfre[0] = 6.;
845     wmatfre[1] = 14.;
846     
847     // --- Parameters to include in GSMIXT, relative to methane (CH4) 
848     
849     amet[0]    = 12.01;
850     amet[1]    = 1.;
851     zmet[0]    = 6.;
852     zmet[1]    = 1.;
853     densmet    = 7.17e-4;
854     nlmatmet   = -2;
855     wmatmet[0] = 1.;
856     wmatmet[1] = 4.;
857     
858     // --- Parameters to include in GSMIXT, relative to anode grid (Cu) 
859   
860     agri    = 63.54;
861     zgri    = 29.;
862     densgri = 8.96;
863     radlgri = 1.43;
864     
865     // --- Parameters to include in GSMATE related to aluminium sheet 
866     
867     aal    = 26.98;
868     zal    = 13.;
869     densal = 2.7;
870     radlal = 8.9;
871     
872     AliMaterial(1, "Air     $", 14.61, 7.3, .001205, 30420., 67500);
873     AliMaterial(6, "HON", ahon, zhon, denshon, radlhon, 0);
874     AliMaterial(16, "CSI", ahon, zhon, denshon, radlhon, 0);
875     AliMixture(20, "QUA", aqua, zqua, densqua, nlmatqua, wmatqua);
876     AliMixture(21, "QUAO", aquao, zquao, densquao, nlmatquao, wmatquao);
877     AliMixture(30, "FRE", afre, zfre, densfre, nlmatfre, wmatfre);
878     AliMixture(40, "MET", amet, zmet, densmet, nlmatmet, wmatmet);
879     AliMixture(41, "METG", amet, zmet, densmet, nlmatmet, wmatmet);
880     AliMaterial(11, "GRI", agri, zgri, densgri, radlgri, 0);
881     AliMaterial(50, "ALUM", aal, zal, densal, radlal, 0);
882     
883     tmaxfd = -10.;
884     stemax = -.1;
885     deemax = -.2;
886     epsil  = .001;
887     stmin  = -.001;
888     
889     AliMedium(1, "DEFAULT MEDIUM AIR$", 1, 0, isxfld, sxmgmx, tmaxfd, stemax, deemax, epsil, stmin);
890     AliMedium(2, "HONEYCOMB$", 6, 0, isxfld, sxmgmx, tmaxfd, stemax, deemax, epsil, stmin);
891     AliMedium(3, "QUARZO$", 20, 1, isxfld, sxmgmx, tmaxfd, stemax, deemax, epsil, stmin);
892     AliMedium(4, "FREON$", 30, 1, isxfld, sxmgmx, tmaxfd, stemax, deemax, epsil, stmin);
893     AliMedium(5, "METANO$", 40, 1, isxfld, sxmgmx, tmaxfd, stemax, deemax, epsil, stmin);
894     AliMedium(6, "CSI$", 16, 1, isxfld, sxmgmx,tmaxfd, stemax, deemax, epsil, stmin);
895     AliMedium(7, "GRIGLIA$", 11, 0, isxfld, sxmgmx, tmaxfd, stemax, deemax, epsil, stmin);
896     AliMedium(8, "QUARZOO$", 21, 1, isxfld, sxmgmx, tmaxfd, stemax, deemax, epsil, stmin);
897     AliMedium(9, "GAP$", 41, 1, isxfld, sxmgmx,tmaxfd, .1, -deemax, epsil, -stmin);
898     AliMedium(10, "ALUMINUM$", 50, 1, isxfld, sxmgmx, tmaxfd, stemax, deemax, epsil, stmin);
899     
900
901     geant3->Gsckov(idtmed[1000], 26, ppckov, abscoMethane, efficAll, rIndexMethane);
902     geant3->Gsckov(idtmed[1001], 26, ppckov, abscoMethane, efficAll, rIndexMethane);
903     geant3->Gsckov(idtmed[1002], 26, ppckov, abscoQuarz, efficAll,rIndexQuarz);
904     geant3->Gsckov(idtmed[1003], 26, ppckov, abscoFreon, efficAll,rIndexFreon);
905     geant3->Gsckov(idtmed[1004], 26, ppckov, abscoMethane, efficAll, rIndexMethane);
906     geant3->Gsckov(idtmed[1005], 26, ppckov, abscoCsI, efficCsI, rIndexMethane);
907     geant3->Gsckov(idtmed[1006], 26, ppckov, abscoGrid, efficGrid, rIndexGrid);
908     geant3->Gsckov(idtmed[1007], 26, ppckov, abscoOpaqueQuarz, efficAll, rIndexOpaqueQuarz);
909     geant3->Gsckov(idtmed[1008], 26, ppckov, abscoMethane, efficAll, rIndexMethane);
910     geant3->Gsckov(idtmed[1009], 26, ppckov, abscoGrid, efficGrid, rIndexGrid);
911 }
912
913 //___________________________________________
914
915 Float_t AliRICH::Fresnel(Float_t ene,Float_t pdoti, Bool_t pola)
916 {
917
918     //ENE(EV), PDOTI=COS(INC.ANG.), PDOTR=COS(POL.PLANE ROT.ANG.)
919     
920     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,
921                       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,
922                       7.8,7.9,8.0,8.1,8.2,8.3,8.4,8.5};
923      
924
925     Float_t csin[36] = {2.14,2.21,2.33,2.48,2.76,2.97,2.99,2.59,2.81,3.05,
926                         2.86,2.53,2.55,2.66,2.79,2.96,3.18,3.05,2.84,2.81,2.38,2.11,
927                         2.01,2.13,2.39,2.73,3.08,3.15,2.95,2.73,2.56,2.41,2.12,1.95,
928                         1.72,1.53};
929       
930     Float_t csik[36] = {0.,0.,0.,0.,0.,0.196,0.408,0.208,0.118,0.49,0.784,0.543,
931                         0.424,0.404,0.371,0.514,0.922,1.102,1.139,1.376,1.461,1.253,0.878,
932                         0.69,0.612,0.649,0.824,1.347,1.571,1.678,1.763,1.857,1.824,1.824,
933                         1.714,1.498};
934     Float_t xe=ene;
935     Int_t  j=Int_t(xe*10)-49;
936     Float_t cn=csin[j]+((csin[j+1]-csin[j])/0.1)*(xe-en[j]);
937     Float_t ck=csik[j]+((csik[j+1]-csik[j])/0.1)*(xe-en[j]);
938
939     //FORMULAE FROM HANDBOOK OF OPTICS, 33.23 OR
940     //W.R. HUNTER, J.O.S.A. 54 (1964),15 , J.O.S.A. 55(1965),1197
941
942     Float_t sinin=TMath::Sqrt(1-pdoti*pdoti);
943     Float_t tanin=sinin/pdoti;
944
945     Float_t c1=cn*cn-ck*ck-sinin*sinin;
946     Float_t c2=4*cn*cn*ck*ck;
947     Float_t aO=TMath::Sqrt(0.5*(TMath::Sqrt(c1*c1+c2)+c1));
948     Float_t b2=0.5*(TMath::Sqrt(c1*c1+c2)-c1);
949     
950     Float_t rs=((aO-pdoti)*(aO-pdoti)+b2)/((aO+pdoti)*(aO+pdoti)+b2);
951     Float_t rp=rs*((aO-sinin*tanin)*(aO-sinin*tanin)+b2)/((aO+sinin*tanin)*(aO+sinin*tanin)+b2);
952     
953
954     //CORRECTION FACTOR FOR SURFACE ROUGHNESS
955     //B.J. STAGG  APPLIED OPTICS, 30(1991),4113
956
957     Float_t sigraf=18.;
958     Float_t lamb=1240/ene;
959     Float_t fresn;
960  
961     Float_t  rO=TMath::Exp(-(4*TMath::Pi()*pdoti*sigraf/lamb)*(4*TMath::Pi()*pdoti*sigraf/lamb));
962
963     if(pola)
964     {
965         Float_t pdotr=0.8;                                 //DEGREE OF POLARIZATION : 1->P , -1->S
966         fresn=0.5*(rp*(1+pdotr)+rs*(1-pdotr));
967     }
968     else
969         fresn=0.5*(rp+rs);
970       
971     fresn = fresn*rO;
972     return(fresn);
973 }
974
975 //__________________________________________
976 Float_t AliRICH::AbsoCH4(Float_t x)
977 {
978
979     //KLOSCH,SCH4(9),WL(9),EM(9),ALENGTH(31)
980     Float_t sch4[9] = {.12,.16,.23,.38,.86,2.8,7.9,28.,80.};              //MB X 10^22
981     //Float_t wl[9] = {153.,152.,151.,150.,149.,148.,147.,146.,145};
982     Float_t em[9] = {8.1,8.158,8.212,8.267,8.322,8.378,8.435,8.493,8.55};
983     const Float_t kLosch=2.686763E19;                                      // LOSCHMIDT NUMBER IN CM-3
984     const Float_t kIgas1=100, kIgas2=0, kOxy=10., kWater=5., kPressure=750.,kTemperature=283.;                                      
985     Float_t pn=kPressure/760.;
986     Float_t tn=kTemperature/273.16;
987     
988         
989 // ------- METHANE CROSS SECTION -----------------
990 // ASTROPH. J. 214, L47 (1978)
991         
992     Float_t sm=0;
993     if (x<7.75) 
994         sm=.06e-22;
995     
996     if(x>=7.75 && x<=8.1)
997     {
998         Float_t c0=-1.655279e-1;
999         Float_t c1=6.307392e-2;
1000         Float_t c2=-8.011441e-3;
1001         Float_t c3=3.392126e-4;
1002         sm=(c0+c1*x+c2*x*x+c3*x*x*x)*1.e-18;
1003     }
1004     
1005     if (x> 8.1)
1006     {
1007         Int_t j=0;
1008         while (x<=em[j] && x>=em[j+1])
1009         {
1010             j++;
1011             Float_t a=(sch4[j+1]-sch4[j])/(em[j+1]-em[j]);
1012             sm=(sch4[j]+a*(x-em[j]))*1e-22;
1013         }
1014     }
1015     
1016     Float_t dm=(kIgas1/100.)*(1.-((kOxy+kWater)/1.e6))*kLosch*pn/tn;
1017     Float_t abslm=1./sm/dm;
1018     
1019 //    ------- ISOBUTHANE CROSS SECTION --------------
1020 //     i-C4H10 (ai) abs. length from curves in
1021 //     Lu-McDonald paper for BARI RICH workshop .
1022 //     -----------------------------------------------------------
1023     
1024     Float_t ai;
1025     Float_t absli;
1026     if (kIgas2 != 0) 
1027     {
1028         if (x<7.25)
1029             ai=100000000.;
1030         
1031         if(x>=7.25 && x<7.375)
1032             ai=24.3;
1033         
1034         if(x>=7.375)
1035             ai=.0000000001;
1036         
1037         Float_t si = 1./(ai*kLosch*273.16/293.);                    // ISOB. CRO.SEC.IN CM2
1038         Float_t di=(kIgas2/100.)*(1.-((kOxy+kWater)/1.e6))*kLosch*pn/tn;
1039         absli =1./si/di;
1040     }
1041     else
1042         absli=1.e18;
1043 //    ---------------------------------------------------------
1044 //
1045 //       transmission of O2
1046 //
1047 //       y= path in cm, x=energy in eV
1048 //       so= cross section for UV absorption in cm2
1049 //       do= O2 molecular density in cm-3
1050 //    ---------------------------------------------------------
1051     
1052     Float_t abslo;
1053     Float_t so=0;
1054     if(x>=6.0)
1055     {
1056         if(x>=6.0 && x<6.5)
1057         {
1058             so=3.392709e-13 * TMath::Exp(2.864104 *x);
1059             so=so*1e-18;
1060         }
1061         
1062         if(x>=6.5 && x<7.0) 
1063         {
1064             so=2.910039e-34 * TMath::Exp(10.3337*x);
1065             so=so*1e-18;
1066         }
1067             
1068
1069         if (x>=7.0) 
1070         {
1071             Float_t a0=-73770.76;
1072             Float_t a1=46190.69;
1073             Float_t a2=-11475.44;
1074             Float_t a3=1412.611;
1075             Float_t a4=-86.07027;
1076             Float_t a5=2.074234;
1077             so= a0+(a1*x)+(a2*x*x)+(a3*x*x*x)+(a4*x*x*x*x)+(a5*x*x*x*x*x);
1078             so=so*1e-18;
1079         }
1080         
1081         Float_t dox=(kOxy/1e6)*kLosch*pn/tn;
1082         abslo=1./so/dox;
1083     }
1084     else
1085         abslo=1.e18;
1086 //     ---------------------------------------------------------
1087 //
1088 //       transmission of H2O
1089 //
1090 //       y= path in cm, x=energy in eV
1091 //       sw= cross section for UV absorption in cm2
1092 //       dw= H2O molecular density in cm-3
1093 //     ---------------------------------------------------------
1094     
1095     Float_t abslw;
1096     
1097     Float_t b0=29231.65;
1098     Float_t b1=-15807.74;
1099     Float_t b2=3192.926;
1100     Float_t b3=-285.4809;
1101     Float_t b4=9.533944;
1102     
1103     if(x>6.75)
1104     {    
1105         Float_t sw= b0+(b1*x)+(b2*x*x)+(b3*x*x*x)+(b4*x*x*x*x);
1106         sw=sw*1e-18;
1107         Float_t dw=(kWater/1e6)*kLosch*pn/tn;
1108         abslw=1./sw/dw;
1109     }
1110     else
1111         abslw=1.e18;
1112             
1113 //    ---------------------------------------------------------
1114     
1115     Float_t alength=1./(1./abslm+1./absli+1./abslo+1./abslw);
1116     return (alength);
1117 }
1118
1119
1120
1121 //___________________________________________
1122 Int_t AliRICH::DistancetoPrimitive(Int_t , Int_t )
1123 {
1124
1125 // Default value
1126
1127     return 9999;
1128 }
1129
1130 //___________________________________________
1131 void AliRICH::MakeBranch(Option_t* option)
1132 {
1133   // Create Tree branches for the RICH.
1134     
1135     const Int_t kBufferSize = 4000;
1136     char branchname[20];
1137     
1138     
1139     AliDetector::MakeBranch(option);
1140     sprintf(branchname,"%sCerenkov",GetName());
1141     if (fCerenkovs   && gAlice->TreeH()) {
1142         gAlice->TreeH()->Branch(branchname,&fCerenkovs, kBufferSize);
1143         printf("Making Branch %s for Cerenkov Hits\n",branchname);
1144     }
1145     
1146     sprintf(branchname,"%sPadHits",GetName());
1147     if (fPadHits   && gAlice->TreeH()) {
1148         gAlice->TreeH()->Branch(branchname,&fPadHits, kBufferSize);
1149         printf("Making Branch %s for PadHits\n",branchname);
1150     }
1151     
1152 // one branch for digits per chamber
1153     Int_t i;
1154     
1155     for (i=0; i<kNCH ;i++) {
1156         sprintf(branchname,"%sDigits%d",GetName(),i+1);
1157         
1158         if (fDchambers   && gAlice->TreeD()) {
1159             gAlice->TreeD()->Branch(branchname,&((*fDchambers)[i]), kBufferSize);
1160             printf("Making Branch %s for digits in chamber %d\n",branchname,i+1);
1161         }       
1162     }
1163
1164 // one branch for raw clusters per chamber
1165   for (i=0; i<kNCH ;i++) {
1166       sprintf(branchname,"%sRawClusters%d",GetName(),i+1);
1167       
1168       if (fRawClusters   && gAlice->TreeR()) {
1169          gAlice->TreeR()->Branch(branchname,&((*fRawClusters)[i]), kBufferSize);
1170          printf("Making Branch %s for raw clusters in chamber %d\n",branchname,i+1);
1171       } 
1172   }
1173
1174   // one branch for rec hits per chamber
1175   for (i=0; i<kNCH ;i++) {
1176     sprintf(branchname,"%sRecHits%d",GetName(),i+1);
1177     
1178     if (fRecHits   && gAlice->TreeR()) {
1179       gAlice->TreeR()->Branch(branchname,&((*fRecHits)[i]), kBufferSize);
1180       printf("Making Branch %s for rec. hits in chamber %d\n",branchname,i+1);
1181     }   
1182   }
1183 }
1184
1185 //___________________________________________
1186 void AliRICH::SetTreeAddress()
1187 {
1188   // Set branch address for the Hits and Digits Tree.
1189   char branchname[20];
1190   Int_t i;
1191
1192     AliDetector::SetTreeAddress();
1193     
1194     TBranch *branch;
1195     TTree *treeH = gAlice->TreeH();
1196     TTree *treeD = gAlice->TreeD();
1197     TTree *treeR = gAlice->TreeR();
1198     
1199     if (treeH) {
1200         if (fPadHits) {
1201             branch = treeH->GetBranch("RICHPadHits");
1202             if (branch) branch->SetAddress(&fPadHits);
1203         }
1204         if (fCerenkovs) {
1205             branch = treeH->GetBranch("RICHCerenkov");
1206             if (branch) branch->SetAddress(&fCerenkovs);
1207         }
1208     }
1209     
1210     if (treeD) {
1211         for (int i=0; i<kNCH; i++) {
1212             sprintf(branchname,"%sDigits%d",GetName(),i+1);
1213             if (fDchambers) {
1214                 branch = treeD->GetBranch(branchname);
1215                 if (branch) branch->SetAddress(&((*fDchambers)[i]));
1216             }
1217         }
1218     }
1219   if (treeR) {
1220       for (i=0; i<kNCH; i++) {
1221           sprintf(branchname,"%sRawClusters%d",GetName(),i+1);
1222           if (fRawClusters) {
1223               branch = treeR->GetBranch(branchname);
1224               if (branch) branch->SetAddress(&((*fRawClusters)[i]));
1225           }
1226       }
1227       
1228       for (i=0; i<kNCH; i++) {
1229         sprintf(branchname,"%sRecHits%d",GetName(),i+1);
1230         if (fRecHits) {
1231           branch = treeR->GetBranch(branchname);
1232           if (branch) branch->SetAddress(&((*fRecHits)[i]));
1233           }
1234       }
1235       
1236   }
1237 }
1238 //___________________________________________
1239 void AliRICH::ResetHits()
1240 {
1241   // Reset number of clusters and the cluster array for this detector
1242     AliDetector::ResetHits();
1243     fNPadHits   = 0;
1244     fNcerenkovs = 0;
1245     if (fPadHits)  fPadHits->Clear();
1246     if (fCerenkovs) fCerenkovs->Clear();
1247 }
1248
1249
1250 //____________________________________________
1251 void AliRICH::ResetDigits()
1252 {
1253   //
1254   // Reset number of digits and the digits array for this detector
1255   //
1256     for ( int i=0;i<kNCH;i++ ) {
1257         if ((*fDchambers)[i])   (*fDchambers)[i]->Clear();
1258         if (fNdch)  fNdch[i]=0;
1259     }
1260 }
1261
1262 //____________________________________________
1263 void AliRICH::ResetRawClusters()
1264 {
1265   //
1266   // Reset number of raw clusters and the raw clust array for this detector
1267   //
1268     for ( int i=0;i<kNCH;i++ ) {
1269         if ((*fRawClusters)[i])    ((TClonesArray*)(*fRawClusters)[i])->Clear();
1270         if (fNrawch)  fNrawch[i]=0;
1271     }
1272 }
1273
1274 //____________________________________________
1275 void AliRICH::ResetRecHits()
1276 {
1277   //
1278   // Reset number of raw clusters and the raw clust array for this detector
1279   //
1280   
1281   for ( int i=0;i<kNCH;i++ ) {
1282         if ((*fRecHits)[i])    ((TClonesArray*)(*fRecHits)[i])->Clear();
1283         if (fNrechits)  fNrechits[i]=0;
1284     }
1285 }
1286
1287 //___________________________________________
1288 void   AliRICH::SetGeometryModel(Int_t id, AliRICHGeometry *geometry)
1289 {
1290
1291 //
1292 // Setter for the RICH geometry model
1293 //
1294
1295
1296     ((AliRICHChamber*) (*fChambers)[id])->GeometryModel(geometry);
1297 }
1298
1299 //___________________________________________
1300 void   AliRICH::SetSegmentationModel(Int_t id, AliRICHSegmentation *segmentation)
1301 {
1302
1303 //
1304 // Setter for the RICH segmentation model
1305 //
1306
1307     ((AliRICHChamber*) (*fChambers)[id])->SegmentationModel(segmentation);
1308 }
1309
1310 //___________________________________________
1311 void   AliRICH::SetResponseModel(Int_t id, AliRICHResponse *response)
1312 {
1313
1314 //
1315 // Setter for the RICH response model
1316 //
1317
1318     ((AliRICHChamber*) (*fChambers)[id])->ResponseModel(response);
1319 }
1320
1321 void   AliRICH::SetReconstructionModel(Int_t id, AliRICHClusterFinder *reconst)
1322 {
1323
1324 //
1325 // Setter for the RICH reconstruction model (clusters)
1326 //
1327
1328     ((AliRICHChamber*) (*fChambers)[id])->ReconstructionModel(reconst);
1329 }
1330
1331 void   AliRICH::SetNsec(Int_t id, Int_t nsec)
1332 {
1333
1334 //
1335 // Sets the number of padplanes
1336 //
1337
1338     ((AliRICHChamber*) (*fChambers)[id])->SetNsec(nsec);
1339 }
1340
1341
1342 //___________________________________________
1343 void AliRICH::StepManager()
1344 {
1345
1346 // Full Step Manager
1347
1348     Int_t          copy, id;
1349     static Int_t   idvol;
1350     static Int_t   vol[2];
1351     Int_t          ipart;
1352     static Float_t hits[18];
1353     static Float_t ckovData[19];
1354     TLorentzVector position;
1355     TLorentzVector momentum;
1356     Float_t        pos[3];
1357     Float_t        mom[4];
1358     Float_t        localPos[3];
1359     Float_t        localMom[4];
1360     Float_t        localTheta,localPhi;
1361     Float_t        theta,phi;
1362     Float_t        destep, step;
1363     Float_t        ranf[2];
1364     Int_t          nPads;
1365     Float_t        coscerenkov;
1366     static Float_t eloss, xhit, yhit, tlength;
1367     const  Float_t kBig=1.e10;
1368        
1369     TClonesArray &lhits = *fHits;
1370     TGeant3 *geant3 = (TGeant3*) gMC;
1371     TParticle *current = (TParticle*)(*gAlice->Particles())[gAlice->CurrentTrack()];
1372
1373  //if (current->Energy()>1)
1374    //{
1375         
1376     // Only gas gap inside chamber
1377     // Tag chambers and record hits when track enters 
1378     
1379     idvol=-1;
1380     id=gMC->CurrentVolID(copy);
1381     Float_t cherenkovLoss=0;
1382     //gAlice->KeepTrack(gAlice->CurrentTrack());
1383     
1384     gMC->TrackPosition(position);
1385     pos[0]=position(0);
1386     pos[1]=position(1);
1387     pos[2]=position(2);
1388     bzero((char *)ckovData,sizeof(ckovData)*19);
1389     ckovData[1] = pos[0];                 // X-position for hit
1390     ckovData[2] = pos[1];                 // Y-position for hit
1391     ckovData[3] = pos[2];                 // Z-position for hit
1392     //ckovData[11] = gAlice->CurrentTrack();
1393
1394     //AliRICH *RICH = (AliRICH *) gAlice->GetDetector("RICH"); 
1395     
1396     /********************Store production parameters for Cerenkov photons************************/ 
1397 //is it a Cerenkov photon? 
1398     if (gMC->TrackPid() == 50000050) {          
1399
1400       //if (gMC->VolId("GAP ")==gMC->CurrentVolID(copy))
1401         //{                    
1402           Float_t ckovEnergy = current->Energy();
1403           //energy interval for tracking
1404           if  (ckovEnergy > 5.6e-09 && ckovEnergy < 7.8e-09 )       
1405             //if (ckovEnergy > 0)
1406             {
1407               if (gMC->IsTrackEntering()){                                     //is track entering?
1408                 if (gMC->VolId("FRE1")==gMC->CurrentVolID(copy) || gMC->VolId("FRE2")==gMC->CurrentVolID(copy))
1409                   {                                                          //is it in freo?
1410                     if (geant3->Gctrak()->nstep<1){                          //is it the first step?
1411                       Int_t mother = current->GetFirstMother(); 
1412                       
1413                       //printf("Second Mother:%d\n",current->GetSecondMother());
1414                       
1415                       ckovData[10] = mother;
1416                       ckovData[11] = gAlice->CurrentTrack();
1417                       ckovData[12] = 1;             //Media where photon was produced 1->Freon, 2->Quarz
1418                       fCkovNumber++;
1419                       fFreonProd=1;
1420                       //printf("Index: %d\n",fCkovNumber);
1421                     }    //first step question
1422                   }        //freo question
1423                 
1424                 if (geant3->Gctrak()->nstep<1){                                  //is it first step?
1425                   if (gMC->VolId("QUAR")==gMC->CurrentVolID(copy))             //is it in quarz?
1426                     {
1427                       ckovData[12] = 2;
1428                     }    //quarz question
1429                 }        //first step question
1430                 
1431                 //printf("Before %d\n",fFreonProd);
1432               }   //track entering question
1433               
1434               if (ckovData[12] == 1)                                        //was it produced in Freon?
1435                 //if (fFreonProd == 1)
1436                 {
1437                   if (gMC->IsTrackEntering()){                                     //is track entering?
1438                     //printf("Got in");
1439                     if (gMC->VolId("META")==gMC->CurrentVolID(copy))                //is it in gap?      
1440                       {
1441                         //printf("Got in\n");
1442                         gMC->TrackMomentum(momentum);
1443                         mom[0]=momentum(0);
1444                         mom[1]=momentum(1);
1445                         mom[2]=momentum(2);
1446                         mom[3]=momentum(3);
1447                         // Z-position for hit
1448                         
1449                         
1450                         /**************** Photons lost in second grid have to be calculated by hand************/ 
1451                         
1452                         Float_t cophi = TMath::Cos(TMath::ATan2(mom[0], mom[1]));
1453                         Float_t t = (1. - .025 / cophi) * (1. - .05 /  cophi);
1454                         gMC->Rndm(ranf, 1);
1455                         //printf("grid calculation:%f\n",t);
1456                         if (ranf[0] > t) {
1457                           //geant3->StopTrack();
1458                           ckovData[13] = 5;
1459                           AddCerenkov(gAlice->CurrentTrack(),vol,ckovData);
1460                           //printf("Lost one in grid\n");
1461                         }
1462                         /**********************************************************************************/
1463                       }    //gap
1464                     
1465                     if (gMC->VolId("CSI ")==gMC->CurrentVolID(copy))             //is it in csi?      
1466                       {
1467                         gMC->TrackMomentum(momentum);
1468                         mom[0]=momentum(0);
1469                         mom[1]=momentum(1);
1470                         mom[2]=momentum(2);
1471                         mom[3]=momentum(3);
1472                         
1473                         /********* Photons lost by Fresnel reflection have to be calculated by hand********/ 
1474                         /***********************Cerenkov phtons (always polarised)*************************/
1475                         
1476                         Float_t cophi = TMath::Cos(TMath::ATan2(mom[0], mom[1]));
1477                         Float_t t = Fresnel(ckovEnergy*1e9,cophi,1);
1478                         gMC->Rndm(ranf, 1);
1479                         if (ranf[0] < t) {
1480                           //geant3->StopTrack();
1481                           ckovData[13] = 6;
1482                           AddCerenkov(gAlice->CurrentTrack(),vol,ckovData);
1483                           //printf("Lost by Fresnel\n");
1484                         }
1485                         /**********************************************************************************/
1486                       }
1487                   } //track entering?
1488                   
1489                   
1490                   /********************Evaluation of losses************************/
1491                   /******************still in the old fashion**********************/
1492                   
1493                   Int_t i1 = geant3->Gctrak()->nmec;            //number of physics mechanisms acting on the particle
1494                   for (Int_t i = 0; i < i1; ++i) {
1495                     //        Reflection loss 
1496                     if (geant3->Gctrak()->lmec[i] == 106) {        //was it reflected
1497                       ckovData[13]=10;
1498                       if (gMC->VolId("FRE1")==gMC->CurrentVolID(copy) || gMC->VolId("FRE2")==gMC->CurrentVolID(copy)) 
1499                         ckovData[13]=1;
1500                       if (gMC->CurrentVolID(copy) == gMC->VolId("QUAR")) 
1501                         ckovData[13]=2;
1502                       //geant3->StopTrack();
1503                       AddCerenkov(gAlice->CurrentTrack(),vol,ckovData);
1504                     } //reflection question
1505                     
1506                     
1507                     //        Absorption loss 
1508                     else if (geant3->Gctrak()->lmec[i] == 101) {              //was it absorbed?
1509                       ckovData[13]=20;
1510                       if (gMC->VolId("FRE1")==gMC->CurrentVolID(copy) || gMC->VolId("FRE2")==gMC->CurrentVolID(copy)) 
1511                         ckovData[13]=11;
1512                       if (gMC->CurrentVolID(copy) == gMC->VolId("QUAR")) 
1513                         ckovData[13]=12;
1514                       if (gMC->CurrentVolID(copy) == gMC->VolId("META")) 
1515                         ckovData[13]=13;
1516                       if (gMC->CurrentVolID(copy) == gMC->VolId("GAP ")) 
1517                         ckovData[13]=13;
1518                       
1519                       if (gMC->CurrentVolID(copy) == gMC->VolId("SRIC")) 
1520                         ckovData[13]=15;
1521                       
1522                       //        CsI inefficiency 
1523                       if (gMC->CurrentVolID(copy) == gMC->VolId("CSI ")) {
1524                         ckovData[13]=16;
1525                       }
1526                       //geant3->StopTrack();
1527                       AddCerenkov(gAlice->CurrentTrack(),vol,ckovData);
1528                       //printf("Added cerenkov %d\n",fCkovNumber);
1529                     } //absorption question 
1530                     
1531                     
1532                     //        Photon goes out of tracking scope 
1533                     else if (geant3->Gctrak()->lmec[i] == 30) {                 //is it below energy treshold?
1534                       ckovData[13]=21;
1535                       //geant3->StopTrack();
1536                       AddCerenkov(gAlice->CurrentTrack(),vol,ckovData);
1537                     }   // energy treshold question         
1538                   }  //number of mechanisms cycle
1539                   /**********************End of evaluation************************/
1540                 } //freon production question
1541             } //energy interval question
1542         //}//inside the proximity gap question
1543     } //cerenkov photon question
1544       
1545     /**************************************End of Production Parameters Storing*********************/ 
1546     
1547     
1548     /*******************************Treat photons that hit the CsI (Ckovs and Feedbacks)************/ 
1549     
1550     if (gMC->TrackPid() == 50000050 || gMC->TrackPid() == 50000051) {
1551       //printf("Cerenkov\n");
1552         if (gMC->VolId("CSI ")==gMC->CurrentVolID(copy))
1553         {
1554             
1555           if (gMC->Edep() > 0.){
1556                 gMC->TrackPosition(position);
1557                 gMC->TrackMomentum(momentum);
1558                 pos[0]=position(0);
1559                 pos[1]=position(1);
1560                 pos[2]=position(2);
1561                 mom[0]=momentum(0);
1562                 mom[1]=momentum(1);
1563                 mom[2]=momentum(2);
1564                 mom[3]=momentum(3);
1565                 Double_t tc = mom[0]*mom[0]+mom[1]*mom[1];
1566                 Double_t rt = TMath::Sqrt(tc);
1567                 theta   = Float_t(TMath::ATan2(rt,Double_t(mom[2])))*kRaddeg;
1568                 phi     = Float_t(TMath::ATan2(Double_t(mom[1]),Double_t(mom[0])))*kRaddeg;
1569                 gMC->Gmtod(pos,localPos,1);                                                                    
1570                 gMC->Gmtod(mom,localMom,2);
1571                 
1572                 gMC->CurrentVolOffID(2,copy);
1573                 vol[0]=copy;
1574                 idvol=vol[0]-1;
1575
1576                 //Int_t sector=((AliRICHChamber*) (*fChambers)[idvol])
1577                         //->Sector(localPos[0], localPos[2]);
1578                 //printf("Sector:%d\n",sector);
1579
1580                 /*if (gMC->TrackPid() == 50000051){
1581                   fFeedbacks++;
1582                   printf("Feedbacks:%d\n",fFeedbacks);
1583                 }*/     
1584                 
1585                 ((AliRICHChamber*) (*fChambers)[idvol])
1586                     ->SigGenInit(localPos[0], localPos[2], localPos[1]);
1587                 if(idvol<kNCH) {        
1588                     ckovData[0] = gMC->TrackPid();        // particle type
1589                     ckovData[1] = pos[0];                 // X-position for hit
1590                     ckovData[2] = pos[1];                 // Y-position for hit
1591                     ckovData[3] = pos[2];                 // Z-position for hit
1592                     ckovData[4] = theta;                      // theta angle of incidence
1593                     ckovData[5] = phi;                      // phi angle of incidence 
1594                     ckovData[8] = (Float_t) fNPadHits;      // first padhit
1595                     ckovData[9] = -1;                       // last pad hit
1596                     ckovData[13] = 4;                       // photon was detected
1597                     ckovData[14] = mom[0];
1598                     ckovData[15] = mom[1];
1599                     ckovData[16] = mom[2];
1600                     
1601                     destep = gMC->Edep();
1602                     gMC->SetMaxStep(kBig);
1603                     cherenkovLoss  += destep;
1604                     ckovData[7]=cherenkovLoss;
1605                     
1606                     nPads = MakePadHits(localPos[0],localPos[2],cherenkovLoss,idvol,kCerenkov);
1607                     if (fNPadHits > (Int_t)ckovData[8]) {
1608                         ckovData[8]= ckovData[8]+1;
1609                         ckovData[9]= (Float_t) fNPadHits;
1610                     }
1611
1612                     ckovData[17] = nPads;
1613                     //printf("nPads:%d",nPads);
1614                     
1615                     //TClonesArray *Hits = RICH->Hits();
1616                     AliRICHHit *mipHit =  (AliRICHHit*) (fHits->UncheckedAt(0));
1617                     if (mipHit)
1618                       {
1619                         mom[0] = current->Px();
1620                         mom[1] = current->Py();
1621                         mom[2] = current->Pz();
1622                         Float_t mipPx = mipHit->fMomX;
1623                         Float_t mipPy = mipHit->fMomY;
1624                         Float_t mipPz = mipHit->fMomZ;
1625                         
1626                         Float_t r = mom[0]*mom[0] + mom[1]*mom[1] + mom[2]*mom[2];
1627                         Float_t rt = TMath::Sqrt(r);
1628                         Float_t mipR = mipPx*mipPx + mipPy*mipPy + mipPz*mipPz; 
1629                         Float_t mipRt = TMath::Sqrt(mipR);
1630                         if ((rt*mipRt) > 0)
1631                           {
1632                             coscerenkov = (mom[0]*mipPx + mom[1]*mipPy + mom[2]*mipPz)/(rt*mipRt);
1633                           }
1634                         else
1635                           {
1636                             coscerenkov = 0;
1637                           }
1638                         Float_t cherenkov = TMath::ACos(coscerenkov);
1639                         ckovData[18]=cherenkov;
1640                       }
1641                     //if (sector != -1)
1642                     //{
1643                     AddHit(gAlice->CurrentTrack(),vol,ckovData);
1644                     AddCerenkov(gAlice->CurrentTrack(),vol,ckovData);
1645                     //}
1646                 }
1647             }
1648         }
1649     }
1650     
1651     /***********************************************End of photon hits*********************************************/
1652     
1653
1654     /**********************************************Charged particles treatment*************************************/
1655
1656     else if (gMC->TrackCharge())
1657     //else if (1 == 1)
1658       {
1659 //If MIP
1660         /*if (gMC->IsTrackEntering())
1661           {                
1662             hits[13]=20;//is track entering?
1663           }*/
1664         if (gMC->VolId("FRE1")==gMC->CurrentVolID(copy) || gMC->VolId("FRE2")==gMC->CurrentVolID(copy))
1665           {
1666             fFreonProd=1;
1667           }
1668
1669         if (gMC->VolId("GAP ")== gMC->CurrentVolID(copy)) {
1670 // Get current particle id (ipart), track position (pos)  and momentum (mom)
1671             
1672             gMC->CurrentVolOffID(3,copy);
1673             vol[0]=copy;
1674             idvol=vol[0]-1;
1675
1676             //Int_t sector=((AliRICHChamber*) (*fChambers)[idvol])
1677                         //->Sector(localPos[0], localPos[2]);
1678             //printf("Sector:%d\n",sector);
1679             
1680             gMC->TrackPosition(position);
1681             gMC->TrackMomentum(momentum);
1682             pos[0]=position(0);
1683             pos[1]=position(1);
1684             pos[2]=position(2);
1685             mom[0]=momentum(0);
1686             mom[1]=momentum(1);
1687             mom[2]=momentum(2);
1688             mom[3]=momentum(3);
1689             gMC->Gmtod(pos,localPos,1);                                                                    
1690             gMC->Gmtod(mom,localMom,2);
1691             
1692             ipart  = gMC->TrackPid();
1693             //
1694             // momentum loss and steplength in last step
1695             destep = gMC->Edep();
1696             step   = gMC->TrackStep();
1697   
1698             //
1699             // record hits when track enters ...
1700             if( gMC->IsTrackEntering()) {
1701 //              gMC->SetMaxStep(fMaxStepGas);
1702                 Double_t tc = mom[0]*mom[0]+mom[1]*mom[1];
1703                 Double_t rt = TMath::Sqrt(tc);
1704                 theta   = Float_t(TMath::ATan2(rt,Double_t(mom[2])))*kRaddeg;
1705                 phi     = Float_t(TMath::ATan2(Double_t(mom[1]),Double_t(mom[0])))*kRaddeg;
1706                 
1707
1708                 Double_t localTc = localMom[0]*localMom[0]+localMom[2]*localMom[2];
1709                 Double_t localRt = TMath::Sqrt(localTc);
1710                 localTheta   = Float_t(TMath::ATan2(localRt,Double_t(localMom[1])))*kRaddeg;                       
1711                 localPhi     = Float_t(TMath::ATan2(Double_t(localMom[2]),Double_t(localMom[0])))*kRaddeg;    
1712                 
1713                 hits[0] = Float_t(ipart);         // particle type
1714                 hits[1] = localPos[0];                 // X-position for hit
1715                 hits[2] = localPos[1];                 // Y-position for hit
1716                 hits[3] = localPos[2];                 // Z-position for hit
1717                 hits[4] = localTheta;                  // theta angle of incidence
1718                 hits[5] = localPhi;                    // phi angle of incidence 
1719                 hits[8] = (Float_t) fNPadHits;    // first padhit
1720                 hits[9] = -1;                     // last pad hit
1721                 hits[13] = fFreonProd;           // did id hit the freon?
1722                 hits[14] = mom[0];
1723                 hits[15] = mom[1];
1724                 hits[16] = mom[2];
1725
1726                 tlength = 0;
1727                 eloss   = 0;
1728                 fFreonProd = 0;
1729         
1730                 Chamber(idvol).LocaltoGlobal(localPos,hits+1);
1731            
1732                 
1733                 //To make chamber coordinates x-y had to pass localPos[0], localPos[2]
1734                 xhit    = localPos[0];
1735                 yhit    = localPos[2];
1736                 // Only if not trigger chamber
1737                 if(idvol<kNCH) {
1738                     //
1739                     //  Initialize hit position (cursor) in the segmentation model 
1740                     ((AliRICHChamber*) (*fChambers)[idvol])
1741                         ->SigGenInit(localPos[0], localPos[2], localPos[1]);
1742                 }
1743             }
1744             
1745             // 
1746             // Calculate the charge induced on a pad (disintegration) in case 
1747             //
1748             // Mip left chamber ...
1749             if( gMC->IsTrackExiting() || gMC->IsTrackStop() || gMC->IsTrackDisappeared()){
1750                 gMC->SetMaxStep(kBig);
1751                 eloss   += destep;
1752                 tlength += step;
1753                 
1754                                 
1755                 // Only if not trigger chamber
1756                 if(idvol<kNCH) {
1757                   if (eloss > 0) 
1758                     {
1759                       if(gMC->TrackPid() == kNeutron)
1760                         printf("\n\n\n\n\n Neutron Making Pad Hit!!! \n\n\n\n");
1761                       nPads = MakePadHits(xhit,yhit,eloss,idvol,kMip);
1762                       hits[17] = nPads;
1763                       //printf("nPads:%d",nPads);
1764                     }
1765                 }
1766                 
1767                 hits[6]=tlength;
1768                 hits[7]=eloss;
1769                 if (fNPadHits > (Int_t)hits[8]) {
1770                     hits[8]= hits[8]+1;
1771                     hits[9]= (Float_t) fNPadHits;
1772                 }
1773                 
1774                 //if(sector !=-1)
1775                 new(lhits[fNhits++]) AliRICHHit(fIshunt,gAlice->CurrentTrack(),vol,hits);
1776                 eloss = 0; 
1777                 //
1778                 // Check additional signal generation conditions 
1779                 // defined by the segmentation
1780                 // model (boundary crossing conditions) 
1781             } else if 
1782                 (((AliRICHChamber*) (*fChambers)[idvol])
1783                  ->SigGenCond(localPos[0], localPos[2], localPos[1]))
1784             {
1785                 ((AliRICHChamber*) (*fChambers)[idvol])
1786                     ->SigGenInit(localPos[0], localPos[2], localPos[1]);
1787                 if (eloss > 0) 
1788                   {
1789                     if(gMC->TrackPid() == kNeutron)
1790                       printf("\n\n\n\n\n Neutron Making Pad Hit!!! \n\n\n\n");
1791                     nPads = MakePadHits(xhit,yhit,eloss,idvol,kMip);
1792                     hits[17] = nPads;
1793                     //printf("Npads:%d",NPads);
1794                   }
1795                 xhit     = localPos[0];
1796                 yhit     = localPos[2]; 
1797                 eloss    = destep;
1798                 tlength += step ;
1799                 //
1800                 // nothing special  happened, add up energy loss
1801             } else {        
1802                 eloss   += destep;
1803                 tlength += step ;
1804             }
1805         }
1806       }
1807     /*************************************************End of MIP treatment**************************************/
1808    //}
1809 }
1810
1811 void AliRICH::FindClusters(Int_t nev,Int_t lastEntry)
1812 {
1813
1814 //
1815 // Loop on chambers and on cathode planes
1816 //
1817     for (Int_t icat=1;icat<2;icat++) {
1818         gAlice->ResetDigits();
1819         gAlice->TreeD()->GetEvent(1); // spurious +1 ...
1820         for (Int_t ich=0;ich<kNCH;ich++) {
1821           AliRICHChamber* iChamber=(AliRICHChamber*) (*fChambers)[ich];
1822           TClonesArray *pRICHdigits  = this->DigitsAddress(ich);
1823           if (pRICHdigits == 0)       
1824               continue;
1825           //
1826           // Get ready the current chamber stuff
1827           //
1828           AliRICHResponse* response = iChamber->GetResponseModel();
1829           AliRICHSegmentation*  seg = iChamber->GetSegmentationModel();
1830           AliRICHClusterFinder* rec = iChamber->GetReconstructionModel();
1831           if (seg) {      
1832               rec->SetSegmentation(seg);
1833               rec->SetResponse(response);
1834               rec->SetDigits(pRICHdigits);
1835               rec->SetChamber(ich);
1836               if (nev==0) rec->CalibrateCOG(); 
1837               rec->FindRawClusters();
1838           }  
1839           TClonesArray *fRch;
1840           fRch=RawClustAddress(ich);
1841           fRch->Sort();
1842         } // for ich
1843
1844         gAlice->TreeR()->Fill();
1845         TClonesArray *fRch;
1846         for (int i=0;i<kNCH;i++) {
1847             fRch=RawClustAddress(i);
1848             int nraw=fRch->GetEntriesFast();
1849             printf ("Chamber %d, raw clusters %d\n",i,nraw);
1850         }
1851         
1852         ResetRawClusters();
1853         
1854     } // for icat
1855     
1856     char hname[30];
1857     sprintf(hname,"TreeR%d",nev);
1858     gAlice->TreeR()->Write(hname,kOverwrite,0);
1859     gAlice->TreeR()->Reset();
1860     
1861     //gObjectTable->Print();
1862 }
1863
1864
1865 //______________________________________________________________________________
1866 void AliRICH::Streamer(TBuffer &R__b)
1867 {
1868     // Stream an object of class AliRICH.
1869     AliRICHChamber       *iChamber;
1870     AliRICHSegmentation  *segmentation;
1871     AliRICHResponse      *response;
1872     TClonesArray         *digitsaddress;
1873     TClonesArray         *rawcladdress;
1874     TClonesArray         *rechitaddress;
1875       
1876     if (R__b.IsReading()) {
1877         Version_t R__v = R__b.ReadVersion(); if (R__v) { }
1878         AliDetector::Streamer(R__b);
1879         R__b >> fNPadHits;
1880         R__b >> fPadHits;   // diff
1881         R__b >> fNcerenkovs;
1882         R__b >> fCerenkovs; // diff
1883         R__b >> fDchambers;
1884         R__b >> fRawClusters;
1885         R__b >> fRecHits;  //diff
1886         R__b >> fDebugLevel;  //diff
1887         R__b.ReadArray(fNdch);
1888         R__b.ReadArray(fNrawch);
1889         R__b.ReadArray(fNrechits);
1890 //
1891         R__b >> fChambers;
1892 // Stream chamber related information
1893         for (Int_t i =0; i<kNCH; i++) {
1894             iChamber=(AliRICHChamber*) (*fChambers)[i];
1895             iChamber->Streamer(R__b);
1896             segmentation=iChamber->GetSegmentationModel();
1897             segmentation->Streamer(R__b);
1898             response=iChamber->GetResponseModel();
1899             response->Streamer(R__b);     
1900             rawcladdress=(TClonesArray*) (*fRawClusters)[i];
1901             rawcladdress->Streamer(R__b);
1902             rechitaddress=(TClonesArray*) (*fRecHits)[i];
1903             rechitaddress->Streamer(R__b);
1904             digitsaddress=(TClonesArray*) (*fDchambers)[i];
1905             digitsaddress->Streamer(R__b);
1906         }
1907       R__b >> fDebugLevel;
1908       R__b >> fCkovNumber;
1909       R__b >> fCkovQuarz;
1910       R__b >> fCkovGap;
1911       R__b >> fCkovCsi;
1912       R__b >> fLostRfreo;
1913       R__b >> fLostRquar;
1914       R__b >> fLostAfreo;
1915       R__b >> fLostAquarz;
1916       R__b >> fLostAmeta;
1917       R__b >> fLostCsi;
1918       R__b >> fLostWires;
1919       R__b >> fFreonProd;
1920       R__b >> fMipx;
1921       R__b >> fMipy;
1922       R__b >> fFeedbacks;
1923       R__b >> fLostFresnel;
1924       
1925     } else {
1926         R__b.WriteVersion(AliRICH::IsA());
1927         AliDetector::Streamer(R__b);
1928         R__b << fNPadHits;
1929         R__b << fPadHits; // diff
1930         R__b << fNcerenkovs;
1931         R__b << fCerenkovs; // diff
1932         R__b << fDchambers;
1933         R__b << fRawClusters;
1934         R__b << fRecHits; //diff
1935         R__b << fDebugLevel; //diff
1936         R__b.WriteArray(fNdch, kNCH);
1937         R__b.WriteArray(fNrawch, kNCH);
1938         R__b.WriteArray(fNrechits, kNCH);
1939         //
1940         R__b << fChambers;
1941 //  Stream chamber related information
1942         for (Int_t i =0; i<kNCH; i++) {
1943             iChamber=(AliRICHChamber*) (*fChambers)[i];
1944             iChamber->Streamer(R__b);
1945             segmentation=iChamber->GetSegmentationModel();
1946             segmentation->Streamer(R__b);
1947             response=iChamber->GetResponseModel();
1948             response->Streamer(R__b);
1949             rawcladdress=(TClonesArray*) (*fRawClusters)[i];
1950             rawcladdress->Streamer(R__b);
1951             rechitaddress=(TClonesArray*) (*fRecHits)[i];
1952             rechitaddress->Streamer(R__b);
1953             digitsaddress=(TClonesArray*) (*fDchambers)[i];
1954             digitsaddress->Streamer(R__b);
1955         }
1956       R__b << fDebugLevel;
1957       R__b << fCkovNumber;
1958       R__b << fCkovQuarz;
1959       R__b << fCkovGap;
1960       R__b << fCkovCsi;
1961       R__b << fLostRfreo;
1962       R__b << fLostRquar;
1963       R__b << fLostAfreo;
1964       R__b << fLostAquarz;
1965       R__b << fLostAmeta;
1966       R__b << fLostCsi;
1967       R__b << fLostWires;
1968       R__b << fFreonProd;
1969       R__b << fMipx;
1970       R__b << fMipy;
1971       R__b << fFeedbacks;
1972       R__b << fLostFresnel;
1973     }
1974 }
1975 AliRICHPadHit* AliRICH::FirstPad(AliRICHHit*  hit,TClonesArray *clusters ) 
1976 {
1977 //
1978     // Initialise the pad iterator
1979     // Return the address of the first padhit for hit
1980     TClonesArray *theClusters = clusters;
1981     Int_t nclust = theClusters->GetEntriesFast();
1982     if (nclust && hit->fPHlast > 0) {
1983         sMaxIterPad=Int_t(hit->fPHlast);
1984         sCurIterPad=Int_t(hit->fPHfirst);
1985         return (AliRICHPadHit*) clusters->UncheckedAt(sCurIterPad-1);
1986     } else {
1987         return 0;
1988     }
1989     
1990 }
1991
1992 AliRICHPadHit* AliRICH::NextPad(TClonesArray *clusters) 
1993 {
1994
1995   // Iterates over pads
1996   
1997     sCurIterPad++;
1998     if (sCurIterPad <= sMaxIterPad) {
1999         return (AliRICHPadHit*) clusters->UncheckedAt(sCurIterPad-1);
2000     } else {
2001         return 0;
2002     }
2003 }
2004
2005
2006 void AliRICH::Digitise(Int_t nev, Int_t flag, Option_t *option,Text_t *filename)
2007 {
2008     // keep galice.root for signal and name differently the file for 
2009     // background when add! otherwise the track info for signal will be lost !
2010
2011     static Bool_t first=kTRUE;
2012     static TFile *pFile;
2013     char *addBackground = strstr(option,"Add");
2014
2015     FILE* points; //these will be the digits...
2016
2017     points=fopen("points.dat","w");
2018
2019     AliRICHChamber*       iChamber;
2020     AliRICHSegmentation*  segmentation;
2021
2022     Int_t digitse=0;
2023     Int_t trk[50];
2024     Int_t chtrk[50];  
2025     TObjArray *list=new TObjArray;
2026     static TClonesArray *pAddress=0;
2027     if(!pAddress) pAddress=new TClonesArray("TVector",1000);
2028     Int_t digits[5]; 
2029     
2030     AliRICH *pRICH = (AliRICH *) gAlice->GetDetector("RICH");
2031     AliRICHHitMap* pHitMap[10];
2032     Int_t i;
2033     for (i=0; i<10; i++) {pHitMap[i]=0;}
2034     if (addBackground ) {
2035         if(first) {
2036             fFileName=filename;
2037             cout<<"filename"<<fFileName<<endl;
2038             pFile=new TFile(fFileName);
2039             cout<<"I have opened "<<fFileName<<" file "<<endl;
2040             fHits2     = new TClonesArray("AliRICHHit",1000  );
2041             fClusters2 = new TClonesArray("AliRICHPadHit",10000);
2042             first=kFALSE;
2043         }
2044         pFile->cd();
2045         // Get Hits Tree header from file
2046         if(fHits2) fHits2->Clear();
2047         if(fClusters2) fClusters2->Clear();
2048         if(TrH1) delete TrH1;
2049         TrH1=0;
2050
2051         char treeName[20];
2052         sprintf(treeName,"TreeH%d",nev);
2053         TrH1 = (TTree*)gDirectory->Get(treeName);
2054         if (!TrH1) {
2055             printf("ERROR: cannot find Hits Tree for event:%d\n",nev);
2056         }
2057         // Set branch addresses
2058         TBranch *branch;
2059         char branchname[20];
2060         sprintf(branchname,"%s",GetName());
2061         if (TrH1 && fHits2) {
2062             branch = TrH1->GetBranch(branchname);
2063             if (branch) branch->SetAddress(&fHits2);
2064         }
2065         if (TrH1 && fClusters2) {
2066             branch = TrH1->GetBranch("RICHCluster");
2067             if (branch) branch->SetAddress(&fClusters2);
2068         }
2069     }
2070     
2071     AliRICHHitMap* hm;
2072     Int_t countadr=0;
2073     Int_t counter=0;
2074     for (i =0; i<kNCH; i++) {
2075       iChamber=(AliRICHChamber*) (*fChambers)[i];
2076       segmentation=iChamber->GetSegmentationModel(1);
2077       pHitMap[i] = new AliRICHHitMapA1(segmentation, list);
2078     }
2079     //
2080     //   Loop over tracks
2081     //
2082     
2083     TTree *treeH = gAlice->TreeH();
2084     Int_t ntracks =(Int_t) treeH->GetEntries();
2085     for (Int_t track=0; track<ntracks; track++) {
2086       gAlice->ResetHits();
2087       treeH->GetEvent(track);
2088       //
2089       //   Loop over hits
2090       for(AliRICHHit* mHit=(AliRICHHit*)pRICH->FirstHit(-1); 
2091           mHit;
2092           mHit=(AliRICHHit*)pRICH->NextHit()) 
2093         {
2094           
2095           digitse=0;
2096           
2097           Int_t   nch   = mHit->fChamber-1;  // chamber number
2098           if (nch >kNCH) continue;
2099           iChamber = &(pRICH->Chamber(nch));
2100           
2101           TParticle *current = (TParticle*)(*gAlice->Particles())[track];
2102           
2103           Int_t particle = current->GetPdgCode();
2104           
2105           //printf("Flag:%d\n",flag);
2106           //printf("Track:%d\n",track);
2107           //printf("Particle:%d\n",particle);
2108           
2109           if (flag == 0)
2110             digitse=1;
2111           
2112           if (flag == 1) 
2113             if(TMath::Abs(particle) == 211 || TMath::Abs(particle) == 111)
2114               digitse=1;
2115           
2116           if (flag == 2)
2117             if(TMath::Abs(particle)==321 || TMath::Abs(particle)==130 || TMath::Abs(particle)==310 
2118                || TMath::Abs(particle)==311)
2119               digitse=1;
2120           
2121           if (flag == 3 && TMath::Abs(particle)==2212)
2122             digitse=1;
2123           
2124           if (flag == 4 && TMath::Abs(particle)==13)
2125             digitse=1;
2126           
2127           if (flag == 5 && TMath::Abs(particle)==11)
2128             digitse=1;
2129           
2130           if (flag == 6 && TMath::Abs(particle)==2112)
2131             digitse=1;
2132           
2133           
2134           //printf ("Particle: %d, Flag: %d, Digitse: %d\n",particle,flag,digitse); 
2135           
2136           
2137           if (digitse)
2138             {
2139               
2140               //
2141               // Loop over pad hits
2142               for (AliRICHPadHit* mPad=
2143                      (AliRICHPadHit*)pRICH->FirstPad(mHit,fPadHits);
2144                    mPad;
2145                    mPad=(AliRICHPadHit*)pRICH->NextPad(fPadHits))
2146                 {
2147                   Int_t cathode  = mPad->fCathode;    // cathode number
2148                   Int_t ipx      = mPad->fPadX;       // pad number on X
2149                   Int_t ipy      = mPad->fPadY;       // pad number on Y
2150                   Int_t iqpad    = mPad->fQpad;       // charge per pad
2151                   //
2152                   //
2153                   //printf("X:%d, Y:%d, Q:%d\n",ipx,ipy,iqpad);
2154                   
2155                   Float_t thex, they;
2156                   segmentation=iChamber->GetSegmentationModel(cathode);
2157                   segmentation->GetPadCxy(ipx,ipy,thex,they);
2158                   new((*pAddress)[countadr++]) TVector(2);
2159                   TVector &trinfo=*((TVector*) (*pAddress)[countadr-1]);
2160                   trinfo(0)=(Float_t)track;
2161                   trinfo(1)=(Float_t)iqpad;
2162                   
2163                   digits[0]=ipx;
2164                   digits[1]=ipy;
2165                   digits[2]=iqpad;
2166                   
2167                   AliRICHTransientDigit* pdigit;
2168                   // build the list of fired pads and update the info
2169                   if (!pHitMap[nch]->TestHit(ipx, ipy)) {
2170                     list->AddAtAndExpand(new AliRICHTransientDigit(nch,digits),counter);
2171                     pHitMap[nch]->SetHit(ipx, ipy, counter);
2172                     counter++;
2173                     pdigit=(AliRICHTransientDigit*)list->At(list->GetLast());
2174                     // list of tracks
2175                     TObjArray *trlist=(TObjArray*)pdigit->TrackList();
2176                     trlist->Add(&trinfo);
2177                   } else {
2178                     pdigit=(AliRICHTransientDigit*) pHitMap[nch]->GetHit(ipx, ipy);
2179                     // update charge
2180                     (*pdigit).fSignal+=iqpad;
2181                     // update list of tracks
2182                     TObjArray* trlist=(TObjArray*)pdigit->TrackList();
2183                     Int_t lastEntry=trlist->GetLast();
2184                     TVector *ptrkP=(TVector*)trlist->At(lastEntry);
2185                     TVector &ptrk=*ptrkP;
2186                     Int_t lastTrack=Int_t(ptrk(0));
2187                     Int_t lastCharge=Int_t(ptrk(1));
2188                     if (lastTrack==track) {
2189                       lastCharge+=iqpad;
2190                       trlist->RemoveAt(lastEntry);
2191                       trinfo(0)=lastTrack;
2192                       trinfo(1)=lastCharge;
2193                       trlist->AddAt(&trinfo,lastEntry);
2194                     } else {
2195                       trlist->Add(&trinfo);
2196                     }
2197                     // check the track list
2198                     Int_t nptracks=trlist->GetEntriesFast();
2199                     if (nptracks > 2) {
2200                       printf("Attention - tracks:  %d (>2)\n",nptracks);
2201                       //printf("cat,nch,ix,iy %d %d %d %d  \n",icat+1,nch,ipx,ipy);
2202                       for (Int_t tr=0;tr<nptracks;tr++) {
2203                         TVector *pptrkP=(TVector*)trlist->At(tr);
2204                         TVector &pptrk=*pptrkP;
2205                         trk[tr]=Int_t(pptrk(0));
2206                         chtrk[tr]=Int_t(pptrk(1));
2207                       }
2208                     } // end if nptracks
2209                   } //  end if pdigit
2210                 } //end loop over clusters
2211             }// track type condition
2212         } // hit loop
2213     } // track loop
2214     
2215     // open the file with background
2216     
2217     if (addBackground ) {
2218       ntracks =(Int_t)TrH1->GetEntries();
2219       //printf("background - icat,ntracks1  %d %d\n",icat,ntracks);
2220       //printf("background - Start loop over tracks \n");     
2221       //
2222       //   Loop over tracks
2223       //
2224       for (Int_t trak=0; trak<ntracks; trak++) {
2225         if (fHits2)       fHits2->Clear();
2226         if (fClusters2)   fClusters2->Clear();
2227         TrH1->GetEvent(trak);
2228         //
2229         //   Loop over hits
2230         AliRICHHit* mHit;
2231         for(int j=0;j<fHits2->GetEntriesFast();++j) 
2232           {
2233             mHit=(AliRICHHit*) (*fHits2)[j];
2234             Int_t   nch   = mHit->fChamber-1;  // chamber number
2235             if (nch >6) continue;
2236             iChamber = &(pRICH->Chamber(nch));
2237             Int_t rmin = (Int_t)iChamber->RInner();
2238             Int_t rmax = (Int_t)iChamber->ROuter();
2239             //
2240             // Loop over pad hits
2241             for (AliRICHPadHit* mPad=
2242                    (AliRICHPadHit*)pRICH->FirstPad(mHit,fClusters2);
2243                  mPad;
2244                  mPad=(AliRICHPadHit*)pRICH->NextPad(fClusters2))
2245               {
2246                 Int_t cathode  = mPad->fCathode;    // cathode number
2247                 Int_t ipx      = mPad->fPadX;       // pad number on X
2248                 Int_t ipy      = mPad->fPadY;       // pad number on Y
2249                 Int_t iqpad    = mPad->fQpad;       // charge per pad
2250                 
2251                 Float_t thex, they;
2252                 segmentation=iChamber->GetSegmentationModel(cathode);
2253                 segmentation->GetPadCxy(ipx,ipy,thex,they);
2254                 Float_t rpad=TMath::Sqrt(thex*thex+they*they);
2255                 if (rpad < rmin || iqpad ==0 || rpad > rmax) continue;
2256                 new((*pAddress)[countadr++]) TVector(2);
2257                 TVector &trinfo=*((TVector*) (*pAddress)[countadr-1]);
2258                 trinfo(0)=-1;  // tag background
2259                 trinfo(1)=-1;
2260                 digits[0]=ipx;
2261                 digits[1]=ipy;
2262                 digits[2]=iqpad;
2263                 if (trak <4 && nch==0)
2264                   printf("bgr - pHitMap[nch]->TestHit(ipx, ipy),trak %d %d\n",
2265                          pHitMap[nch]->TestHit(ipx, ipy),trak);
2266                 AliRICHTransientDigit* pdigit;
2267                 // build the list of fired pads and update the info
2268                 if (!pHitMap[nch]->TestHit(ipx, ipy)) {
2269                   list->AddAtAndExpand(new AliRICHTransientDigit(nch,digits),counter);
2270                   
2271                   pHitMap[nch]->SetHit(ipx, ipy, counter);
2272                   counter++;
2273                   printf("bgr new elem in list - counter %d\n",counter);
2274                   
2275                   pdigit=(AliRICHTransientDigit*)list->At(list->GetLast());
2276                   // list of tracks
2277                   TObjArray *trlist=(TObjArray*)pdigit->TrackList();
2278                   trlist->Add(&trinfo);
2279                 } else {
2280                   pdigit=(AliRICHTransientDigit*) pHitMap[nch]->GetHit(ipx, ipy);
2281                   // update charge
2282                   (*pdigit).fSignal+=iqpad;
2283                   // update list of tracks
2284                   TObjArray* trlist=(TObjArray*)pdigit->TrackList();
2285                   Int_t lastEntry=trlist->GetLast();
2286                   TVector *ptrkP=(TVector*)trlist->At(lastEntry);
2287                   TVector &ptrk=*ptrkP;
2288                   Int_t lastTrack=Int_t(ptrk(0));
2289                   if (lastTrack==-1) {
2290                     continue;
2291                   } else {
2292                     trlist->Add(&trinfo);
2293                   }
2294                   // check the track list
2295                   Int_t nptracks=trlist->GetEntriesFast();
2296                   if (nptracks > 0) {
2297                     for (Int_t tr=0;tr<nptracks;tr++) {
2298                       TVector *pptrkP=(TVector*)trlist->At(tr);
2299                       TVector &pptrk=*pptrkP;
2300                       trk[tr]=Int_t(pptrk(0));
2301                       chtrk[tr]=Int_t(pptrk(1));
2302                     }
2303                   } // end if nptracks
2304                 } //  end if pdigit
2305               } //end loop over clusters
2306           } // hit loop
2307       } // track loop
2308             TTree *fAli=gAlice->TreeK();
2309             if (fAli) pFile =fAli->GetCurrentFile();
2310             pFile->cd();
2311     } // if Add 
2312     
2313     Int_t tracks[10];
2314     Int_t charges[10];
2315     //cout<<"Start filling digits \n "<<endl;
2316     Int_t nentries=list->GetEntriesFast();
2317     //printf(" \n \n nentries %d \n",nentries);
2318     
2319     // start filling the digits
2320     
2321     for (Int_t nent=0;nent<nentries;nent++) {
2322       AliRICHTransientDigit *address=(AliRICHTransientDigit*)list->At(nent);
2323       if (address==0) continue; 
2324       
2325       Int_t ich=address->fChamber;
2326       Int_t q=address->fSignal; 
2327       iChamber=(AliRICHChamber*) (*fChambers)[ich];
2328       AliRICHResponse * response=iChamber->GetResponseModel();
2329       Int_t adcmax= (Int_t) response->MaxAdc();
2330       
2331       
2332       // add white noise and do zero-suppression and signal truncation (new electronics,old electronics gaus 1.2,0.2)
2333       //printf("Treshold: %d\n",iChamber->fTresh->GetHitIndex(address->fPadX,address->fPadY));
2334       Int_t pedestal = iChamber->fTresh->GetHitIndex(address->fPadX,address->fPadY);
2335
2336       //printf("Pedestal:%d\n",pedestal);
2337       //Int_t pedestal=0;
2338       Float_t treshold = (pedestal + 4*1.7);
2339       
2340       Float_t meanNoise = gRandom->Gaus(1.7, 0.25);
2341       Float_t noise     = gRandom->Gaus(0, meanNoise);
2342       q+=(Int_t)(noise + pedestal);
2343       //q+=(Int_t)(noise);
2344       //          magic number to be parametrised !!! 
2345       if ( q <= treshold) 
2346         {
2347           q = q - pedestal;
2348           continue;
2349         }
2350       q = q - pedestal;
2351       if ( q >= adcmax) q=adcmax;
2352       digits[0]=address->fPadX;
2353       digits[1]=address->fPadY;
2354       digits[2]=q;
2355       
2356       TObjArray* trlist=(TObjArray*)address->TrackList();
2357       Int_t nptracks=trlist->GetEntriesFast();
2358       
2359       // this was changed to accomodate the real number of tracks
2360       if (nptracks > 10) {
2361         cout<<"Attention - tracks > 10 "<<nptracks<<endl;
2362         nptracks=10;
2363       }
2364       if (nptracks > 2) {
2365         printf("Attention - tracks > 2  %d \n",nptracks);
2366         //printf("cat,ich,ix,iy,q %d %d %d %d %d \n",
2367         //icat,ich,digits[0],digits[1],q);
2368       }
2369       for (Int_t tr=0;tr<nptracks;tr++) {
2370         TVector *ppP=(TVector*)trlist->At(tr);
2371         TVector &pp  =*ppP;
2372         tracks[tr]=Int_t(pp(0));
2373         charges[tr]=Int_t(pp(1));
2374       }      //end loop over list of tracks for one pad
2375       if (nptracks < 10 ) {
2376         for (Int_t t=nptracks; t<10; t++) {
2377           tracks[t]=0;
2378           charges[t]=0;
2379         }
2380       }
2381       //write file
2382       if (ich==2)
2383         fprintf(points,"%4d,      %4d,      %4d\n",digits[0],digits[1],digits[2]);
2384       
2385       // fill digits
2386       pRICH->AddDigits(ich,tracks,charges,digits);
2387     }   
2388     gAlice->TreeD()->Fill();
2389     
2390     list->Delete();
2391     for(Int_t ii=0;ii<kNCH;++ii) {
2392       if (pHitMap[ii]) {
2393         hm=pHitMap[ii];
2394         delete hm;
2395         pHitMap[ii]=0;
2396       }
2397     }
2398     
2399     //TTree *TD=gAlice->TreeD();
2400     //Stat_t ndig=TD->GetEntries();
2401     //cout<<"number of digits  "<<ndig<<endl;
2402     TClonesArray *fDch;
2403     for (int k=0;k<kNCH;k++) {
2404       fDch= pRICH->DigitsAddress(k);
2405       int ndigit=fDch->GetEntriesFast();
2406       printf ("Chamber %d digits %d \n",k,ndigit);
2407     }
2408     pRICH->ResetDigits();
2409     char hname[30];
2410     sprintf(hname,"TreeD%d",nev);
2411     gAlice->TreeD()->Write(hname,kOverwrite,0);
2412     
2413     // reset tree
2414     //    gAlice->TreeD()->Reset();
2415     delete list;
2416     pAddress->Clear();
2417     // gObjectTable->Print();
2418 }
2419
2420 AliRICH& AliRICH::operator=(const AliRICH& rhs)
2421 {
2422 // Assignment operator
2423     return *this;
2424     
2425 }
2426
2427 Int_t AliRICH::MakePadHits(Float_t xhit,Float_t yhit,Float_t eloss, Int_t idvol, ResponseType res)
2428 {
2429 //
2430 //  Calls the charge disintegration method of the current chamber and adds
2431 //  the simulated cluster to the root treee 
2432 //
2433     Int_t clhits[kNCH];
2434     Float_t newclust[6][500];
2435     Int_t nnew;
2436     
2437 //
2438 //  Integrated pulse height on chamber
2439     
2440     clhits[0]=fNhits+1;
2441     
2442     ((AliRICHChamber*) (*fChambers)[idvol])->DisIntegration(eloss, xhit, yhit, nnew, newclust, res);
2443     Int_t ic=0;
2444     
2445 //
2446 //  Add new clusters
2447     for (Int_t i=0; i<nnew; i++) {
2448         if (Int_t(newclust[3][i]) > 0) {
2449             ic++;
2450 // Cathode plane
2451             clhits[1] = Int_t(newclust[5][i]);
2452 //  Cluster Charge
2453             clhits[2] = Int_t(newclust[0][i]);
2454 //  Pad: ix
2455             clhits[3] = Int_t(newclust[1][i]);
2456 //  Pad: iy 
2457             clhits[4] = Int_t(newclust[2][i]);
2458 //  Pad: charge
2459             clhits[5] = Int_t(newclust[3][i]);
2460 //  Pad: chamber sector
2461             clhits[6] = Int_t(newclust[4][i]);
2462             
2463             AddPadHit(clhits);
2464         }
2465     }
2466 return nnew;
2467 }
2468