]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TRD/AliTRD.cxx
Adding renormalisation of the combinatotial bgk for peripheral events
[u/mrichter/AliRoot.git] / TRD / AliTRD.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 /* $Id$ */
17
18 ///////////////////////////////////////////////////////////////////////////////
19 //                                                                           //
20 //  Transition Radiation Detector                                            //
21 //  This class contains the basic functions for the Transition Radiation     //
22 //  Detector.                                                                //
23 //                                                                           //
24 ///////////////////////////////////////////////////////////////////////////////
25
26 #include <stdlib.h>
27 #include <Riostream.h>
28
29 #include <TFile.h>
30 #include <TGeometry.h>
31 #include <TLorentzVector.h>
32 #include <TMath.h>
33 #include <TNode.h>
34 #include <TPGON.h> 
35 #include <TParticle.h>
36 #include <TROOT.h>
37 #include <TTree.h>
38 #include <TVirtualMC.h>
39  
40 #include "AliConst.h"
41 #include "AliDigit.h"
42 #include "AliLoader.h"
43 #include "AliMagF.h"
44 #include "AliRun.h"
45 #include "AliTRD.h"
46 #include "AliTRDcluster.h"
47 #include "AliTRDdigit.h"
48 #include "AliTRDdigitizer.h"
49 #include "AliTRDdigitsManager.h"
50 #include "AliTRDgeometryFull.h"
51 #include "AliTRDgeometryHole.h"
52 #include "AliTRDhit.h"
53 #include "AliTRDpoints.h"
54 #include "AliTRDrecPoint.h"
55 #include "AliTRDtrackHits.h"  
56 #include "AliTrackReference.h"
57 #include "AliMC.h"
58
59 ClassImp(AliTRD)
60  
61 //_____________________________________________________________________________
62 AliTRD::AliTRD()
63 {
64   //
65   // Default constructor
66   //
67
68   fIshunt        = 0;
69   fGasMix        = 0;
70   fHits          = 0;
71   fDigits        = 0;
72
73   fRecPoints     = 0;
74   fNRecPoints    = 0;
75
76   fGeometry      = 0;
77
78   fGasDensity    = 0;
79   fFoilDensity   = 0;
80
81   fDrawTR        = 0;
82   fDisplayType   = 0;
83  
84   fTrackHits     = 0; 
85   fHitType       = 0; 
86
87 }
88  
89 //_____________________________________________________________________________
90 AliTRD::AliTRD(const char *name, const char *title)
91        : AliDetector(name,title)
92 {
93   //
94   // Standard constructor for the TRD
95   //
96
97   // Check that FRAME is there otherwise we have no place where to
98   // put TRD
99   AliModule* frame = gAlice->GetModule("FRAME");
100   if (!frame) {
101     Error("Ctor","TRD needs FRAME to be present\n");
102     exit(1);
103   } 
104
105   // Define the TRD geometry
106   if ((frame->IsVersion() == 0) ||
107       (frame->IsVersion() == 1)) {
108     fGeometry = new AliTRDgeometryFull();
109   }
110   else {
111     Error("Ctor","Could not find valid FRAME version\n");
112     exit(1);
113   }
114
115   // Allocate the hit array
116   fHits           = new TClonesArray("AliTRDhit"     ,405);
117   gAlice->GetMCApp()->AddHitList(fHits);
118
119   // Allocate the digits array
120   fDigits         = 0;
121
122   // Allocate the rec point array
123   fRecPoints     = new TObjArray(400);
124   fNRecPoints    = 0;
125    
126   fIshunt        = 0;
127   fGasMix        = 1;
128
129   fGasDensity    = 0;
130   fFoilDensity   = 0;
131
132   fDrawTR        = 0;
133   fDisplayType   = 0;
134
135   fTrackHits     = 0;
136   fHitType       = 2;
137
138   SetMarkerColor(kWhite);   
139
140 }
141
142 //_____________________________________________________________________________
143 AliTRD::AliTRD(const AliTRD &trd):AliDetector(trd)
144 {
145   //
146   // Copy constructor
147   //
148
149   ((AliTRD &) trd).Copy(*this);
150
151 }
152
153 //_____________________________________________________________________________
154 AliTRD::~AliTRD()
155 {
156   //
157   // TRD destructor
158   //
159
160   fIshunt = 0;
161
162   if (fGeometry) {
163     delete fGeometry;
164     fGeometry  = 0;
165   }
166   if (fHits) {
167     delete fHits;
168     fHits      = 0;
169   }
170   if (fRecPoints) {
171     delete fRecPoints;
172     fRecPoints = 0;
173   }
174   if (fTrackHits) {
175     delete fTrackHits;
176     fTrackHits = 0;
177   }
178
179 }
180
181 //_____________________________________________________________________________
182 void AliTRD::AddCluster(Float_t *pos, Int_t det, Float_t amp
183                       , Int_t *tracks, Float_t *sig, Int_t iType)
184 {
185   //
186   // Add a cluster for the TRD
187   //
188
189   AliTRDcluster *c = new AliTRDcluster();
190
191   c->SetDetector(det);
192   c->AddTrackIndex(tracks);
193   c->SetQ(amp);
194   c->SetY(pos[0]);
195   c->SetZ(pos[1]);
196   c->SetSigmaY2(sig[0]);   
197   c->SetSigmaZ2(sig[1]);
198   c->SetLocalTimeBin(((Int_t) pos[2]));
199
200   switch (iType) {
201   case 0:
202     c->Set2pad();
203     break;
204   case 1:
205     c->Set3pad();
206     break;
207   case 2:
208     c->Set4pad();
209     break;
210   case 3:
211     c->Set5pad();
212     break;
213   case 4:
214     c->SetLarge();
215     break;
216   };
217
218   fRecPoints->Add(c);
219
220 }
221
222 //_____________________________________________________________________________
223 void AliTRD::Hits2Digits()
224 {
225   //
226   // Create digits
227   //
228   AliTRDdigitizer digitizer("TRDdigitizer","TRD digitizer class");
229   digitizer.SetDebug(GetDebug());
230   
231   // Initialization
232   digitizer.InitDetector();
233     
234   if (!fLoader->TreeH()) fLoader->LoadHits("read");
235   fLoader->LoadDigits("recreate");
236   AliRunLoader* runLoader = fLoader->GetRunLoader(); 
237
238   for (Int_t iEvent = 0; iEvent < runLoader->GetNumberOfEvents(); iEvent++) {
239     digitizer.Open(runLoader->GetFileName().Data(), iEvent);
240     digitizer.MakeDigits();
241     digitizer.WriteDigits();
242   }
243
244   fLoader->UnloadHits();
245   fLoader->UnloadDigits();
246 }
247
248 //_____________________________________________________________________________
249 void AliTRD::Hits2SDigits()
250 {
251   //
252   // Create summable digits
253   //
254   AliTRDdigitizer digitizer("TRDdigitizer","TRD digitizer class");
255   // For the summable digits
256   digitizer.SetSDigits(kTRUE);
257   digitizer.SetDebug(GetDebug());
258
259   // Initialization
260   digitizer.InitDetector();
261     
262   if (!fLoader->TreeH()) fLoader->LoadHits("read");
263   fLoader->LoadSDigits("recreate");
264   AliRunLoader* runLoader = fLoader->GetRunLoader(); 
265
266   for (Int_t iEvent = 0; iEvent < runLoader->GetNumberOfEvents(); iEvent++) {
267     digitizer.Open(runLoader->GetFileName().Data(), iEvent);
268     digitizer.MakeDigits();
269     digitizer.WriteDigits();
270   }
271
272   fLoader->UnloadHits();
273   fLoader->UnloadSDigits();
274   }
275
276 //_____________________________________________________________________________
277 AliDigitizer* AliTRD::CreateDigitizer(AliRunDigitizer* manager) const
278 {
279   return new AliTRDdigitizer(manager);
280 }
281
282 //_____________________________________________________________________________
283 void AliTRD::SDigits2Digits()
284 {
285   //
286   // Create final digits from summable digits
287   //
288
289    // Create the TRD digitizer
290   AliTRDdigitizer digitizer("TRDdigitizer","TRD digitizer class");  
291   digitizer.SetDebug(GetDebug());
292
293   // Set the parameter
294   digitizer.SetEvent(gAlice->GetEvNumber());
295
296   // Initialization
297   digitizer.InitDetector();
298
299   // Read the s-digits via digits manager
300   AliTRDdigitsManager sdigitsManager;
301  
302   sdigitsManager.SetDebug(GetDebug());
303   sdigitsManager.SetSDigits(kTRUE);
304   sdigitsManager.CreateArrays();
305   
306   if (!fLoader->TreeS()) 
307     if (fLoader->LoadSDigits("read"))
308      {
309        Error("SDigits2Digits","Error while reading SDigits for event %d",gAlice->GetEvNumber());
310        return;
311      }
312   if (!fLoader->TreeS()) return;
313   
314   sdigitsManager.ReadDigits(fLoader->TreeS());
315
316   // Add the s-digits to the input list 
317   digitizer.AddSDigitsManager(&sdigitsManager);
318
319   // Convert the s-digits to normal digits
320   digitizer.SDigits2Digits();
321
322   // Store the digits
323   if (!fLoader->TreeD()) fLoader->MakeTree("D");
324   if (digitizer.MakeBranch(fLoader->TreeD())){
325     digitizer.WriteDigits();
326   }
327
328 }
329
330 //_____________________________________________________________________________
331 void AliTRD::AddHit(Int_t track, Int_t det, Float_t *hits, Int_t q
332                   , Bool_t inDrift)
333 {
334   //
335   // Add a hit for the TRD
336   // 
337   // The data structure is set according to fHitType:
338   //   bit0: standard TClonesArray
339   //   bit1: compressed trackHits structure
340   //
341
342   if (fHitType & 1) {
343     TClonesArray &lhits = *fHits;
344     new(lhits[fNhits++]) AliTRDhit(fIshunt,track,det,hits,q);
345   }
346
347   if (fHitType > 1) {
348     AddHit2(track,det,hits,q,inDrift);
349   }
350
351 }
352
353 //_____________________________________________________________________________
354 void AliTRD::BuildGeometry()
355 {
356   //
357   // Create the ROOT TNode geometry for the TRD
358   //
359
360   TNode *node, *top;
361   TPGON *pgon;
362
363   Float_t rmin, rmax;
364   Float_t zmax1, zmax2;
365
366   Int_t   iPlan;
367  
368   const Int_t kColorTRD = 46;
369   
370   // Find the top node alice
371   top = gAlice->GetGeometry()->GetNode("alice");
372   
373   if      (fDisplayType == 0) {
374
375     pgon = new TPGON("S_TRD","TRD","void",0,360,AliTRDgeometry::Nsect(),4);
376     rmin = AliTRDgeometry::Rmin();
377     rmax = AliTRDgeometry::Rmax();
378     pgon->DefineSection(0,-AliTRDgeometry::Zmax1(),rmax,rmax);
379     pgon->DefineSection(1,-AliTRDgeometry::Zmax2(),rmin,rmax);
380     pgon->DefineSection(2, AliTRDgeometry::Zmax2(),rmin,rmax);
381     pgon->DefineSection(3, AliTRDgeometry::Zmax1(),rmax,rmax);
382     top->cd();
383     node = new TNode("TRD","TRD","S_TRD",0,0,0,"");
384     node->SetLineColor(kColorTRD);
385     fNodes->Add(node);
386
387   }
388   else if (fDisplayType == 1) {
389
390     Char_t name[7];
391
392     Float_t slope = (AliTRDgeometry::Zmax1() - AliTRDgeometry::Zmax2())
393                   / (AliTRDgeometry::Rmax()  - AliTRDgeometry::Rmin());
394
395     rmin  = AliTRDgeometry::Rmin() + AliTRDgeometry::CraHght();
396     rmax  = rmin                   + AliTRDgeometry::CdrHght();
397
398     Float_t thickness = rmin - AliTRDgeometry::Rmin();
399     zmax2 = AliTRDgeometry::Zmax2() + slope * thickness;
400     zmax1 = zmax2 + slope * AliTRDgeometry::DrThick();
401
402     for (iPlan = 0; iPlan < AliTRDgeometry::Nplan(); iPlan++) {
403
404       sprintf(name,"S_TR1%d",iPlan);
405       pgon  = new TPGON(name,"TRD","void",0,360,AliTRDgeometry::Nsect(),4);
406       pgon->DefineSection(0,-zmax1,rmax,rmax);
407       pgon->DefineSection(1,-zmax2,rmin,rmax);
408       pgon->DefineSection(2, zmax2,rmin,rmax);
409       pgon->DefineSection(3, zmax1,rmax,rmax);
410       top->cd();
411       node = new TNode("TRD","TRD",name,0,0,0,"");
412       node->SetLineColor(kColorTRD);
413       fNodes->Add(node);
414
415       Float_t height = AliTRDgeometry::Cheight() + AliTRDgeometry::Cspace(); 
416       rmin  = rmin  + height;
417       rmax  = rmax  + height;
418       zmax1 = zmax1 + slope * height;
419       zmax2 = zmax2 + slope * height;
420
421     }
422
423     thickness += AliTRDgeometry::DrThick();
424     rmin  = AliTRDgeometry::Rmin() + thickness;
425     rmax  = rmin + AliTRDgeometry::AmThick();
426     zmax2 = AliTRDgeometry::Zmax2() + slope * thickness;
427     zmax1 = zmax2 + slope * AliTRDgeometry::AmThick();
428
429     for (iPlan = 0; iPlan < AliTRDgeometry::Nplan(); iPlan++) {
430
431       sprintf(name,"S_TR2%d",iPlan);
432       pgon  = new TPGON(name,"TRD","void",0,360,AliTRDgeometry::Nsect(),4);
433       pgon->DefineSection(0,-zmax1,rmax,rmax);
434       pgon->DefineSection(1,-zmax2,rmin,rmax);
435       pgon->DefineSection(2, zmax2,rmin,rmax);
436       pgon->DefineSection(3, zmax1,rmax,rmax);
437       top->cd();
438       node = new TNode("TRD","TRD",name,0,0,0,"");
439       node->SetLineColor(kColorTRD);
440       fNodes->Add(node);
441
442       Float_t height = AliTRDgeometry::Cheight() + AliTRDgeometry::Cspace(); 
443       rmin  = rmin  + height;
444       rmax  = rmax  + height;
445       zmax1 = zmax1 + slope * height;
446       zmax2 = zmax2 + slope * height;
447
448     }
449
450   }
451
452 }
453  
454 //_____________________________________________________________________________
455 void AliTRD::Copy(TObject &trd)
456 {
457   //
458   // Copy function
459   //
460
461   ((AliTRD &) trd).fGasMix      = fGasMix;
462   ((AliTRD &) trd).fGeometry    = fGeometry;       
463   ((AliTRD &) trd).fRecPoints   = fRecPoints;
464   ((AliTRD &) trd).fNRecPoints  = fNRecPoints;
465   ((AliTRD &) trd).fGasDensity  = fGasDensity;
466   ((AliTRD &) trd).fFoilDensity = fFoilDensity;
467   ((AliTRD &) trd).fDrawTR      = fDrawTR;
468   ((AliTRD &) trd).fDisplayType = fDisplayType;
469   ((AliTRD &) trd).fHitType     = fHitType;
470
471   //AliDetector::Copy(trd);
472
473 }
474
475 //_____________________________________________________________________________
476 void AliTRD::CreateGeometry()
477 {
478   //
479   // Creates the volumes for the TRD chambers
480   //
481
482   // Check that FRAME is there otherwise we have no place where to put the TRD
483   AliModule* frame = gAlice->GetModule("FRAME");
484   if (!frame) {
485     printf(" The TRD needs the FRAME to be defined first\n");
486     return;
487   }
488
489   fGeometry->CreateGeometry(fIdtmed->GetArray() - 1299);
490
491 }
492  
493 //_____________________________________________________________________________
494 void AliTRD::CreateMaterials()
495 {
496   //
497   // Create the materials for the TRD
498   // Origin Y.Foka
499   //
500
501   Int_t   isxfld = gAlice->Field()->Integ();
502   Float_t sxmgmx = gAlice->Field()->Max();
503   
504   // For polyethilene (CH2) 
505   Float_t ape[2] = { 12., 1. };
506   Float_t zpe[2] = {  6., 1. };
507   Float_t wpe[2] = {  1., 2. };
508   Float_t dpe    = 0.95;
509
510   // For mylar (C5H4O2) 
511   Float_t amy[3] = { 12., 1., 16. };
512   Float_t zmy[3] = {  6., 1.,  8. };
513   Float_t wmy[3] = {  5., 4.,  2. };
514   Float_t dmy    = 1.39;
515
516   // For CO2 
517   Float_t aco[2] = { 12., 16. };
518   Float_t zco[2] = {  6.,  8. };
519   Float_t wco[2] = {  1.,  2. };
520   Float_t dco    = 0.001977;
521
522   // For water
523   Float_t awa[2] = {  1., 16. };
524   Float_t zwa[2] = {  1.,  8. };
525   Float_t wwa[2] = {  2.,  1. };
526   Float_t dwa    = 1.0;
527
528   // For isobutane (C4H10)
529   Float_t ais[2] = { 12.,  1. };
530   Float_t zis[2] = {  6.,  1. };
531   Float_t wis[2] = {  4., 10. };
532   Float_t dis    = 0.00267;
533
534   // For plexiglas (C5H8O2)
535   Float_t apg[3] = { 12.011 ,  1.0    , 15.9994 };
536   Float_t zpg[3] = {  6.0   ,  1.0    ,  8.0    };
537   Float_t wpg[3] = {  5.0   ,  8.0    ,  2.0    };
538   Float_t dpg    = 1.18; 
539
540   // For Xe/CO2-gas-mixture 
541   // Xe-content of the Xe/CO2-mixture (85% / 15%) 
542   Float_t fxc    = .85;
543   // Xe-content of the Xe/Isobutane-mixture (97% / 3%) 
544   Float_t fxi    = .97;
545   Float_t dxe    = .005858;
546   
547   // General tracking parameter
548   Float_t tmaxfd = -10.;
549   Float_t stemax = -1e10;
550   Float_t deemax = -0.1;
551   Float_t epsil  =  1e-4;
552   Float_t stmin  = -0.001;
553   
554   Float_t absl, radl, d, buf[1];
555   Float_t agm[2], zgm[2], wgm[2];
556   Float_t dgm1, dgm2;
557   Int_t   nbuf;
558   
559   //////////////////////////////////////////////////////////////////////////
560   //     Define Materials 
561   //////////////////////////////////////////////////////////////////////////
562
563   AliMaterial( 1, "Al"   ,  26.98, 13.0, 2.7     ,     8.9 ,    37.2);
564   AliMaterial( 2, "Air"  ,  14.61,  7.3, 0.001205, 30420.0 , 67500.0);
565   AliMaterial( 4, "Xe"   , 131.29, 54.0, dxe     ,  1447.59,     0.0);
566   AliMaterial( 5, "Cu"   ,  63.54, 29.0, 8.96    ,     1.43,    14.8);
567   AliMaterial( 6, "C"    ,  12.01,  6.0, 2.265   ,    18.8 ,    74.4);
568   AliMaterial(12, "G10"  ,  20.00, 10.0, 1.7     ,    19.4 ,   999.0);
569   AliMaterial(15, "Sn"   , 118.71, 50.0, 7.31    ,     1.21,    14.8);
570   AliMaterial(16, "Si"   ,  28.09, 14.0, 2.33    ,     9.36,    37.2);
571   AliMaterial(17, "Epoxy",  17.75,  8.9, 1.8     ,    21.82,   999.0);
572
573   // Mixtures 
574   AliMixture(3, "Polyethilene",   ape, zpe, dpe, -2, wpe);
575   AliMixture(7, "Mylar",          amy, zmy, dmy, -3, wmy);
576   AliMixture(8, "CO2",            aco, zco, dco, -2, wco);
577   AliMixture(9, "Isobutane",      ais, zis, dis, -2, wis);
578   AliMixture(13,"Water",          awa, zwa, dwa, -2, wwa);
579   AliMixture(14,"Plexiglas",      apg, zpg, dpg, -3, wpg);
580
581   // Gas mixtures
582   Char_t namate[21]="";
583   // Xe/CO2-mixture
584   // Get properties of Xe 
585   gMC->Gfmate((*fIdmate)[4], namate, agm[0], zgm[0], d, radl, absl, buf, nbuf);
586   // Get properties of CO2 
587   gMC->Gfmate((*fIdmate)[8], namate, agm[1], zgm[1], d, radl, absl, buf, nbuf);
588   // Create gas mixture 
589   wgm[0] = fxc;
590   wgm[1] = 1. - fxc;
591   dgm1   = wgm[0] * dxe + wgm[1] * dco;
592   AliMixture(10, "Gas mixture 1", agm, zgm, dgm1,  2, wgm);
593   // Xe/Isobutane-mixture
594   // Get properties of Xe 
595   gMC->Gfmate((*fIdmate)[4], namate, agm[0], zgm[0], d, radl, absl, buf, nbuf);
596   // Get properties of Isobutane
597   gMC->Gfmate((*fIdmate)[9], namate, agm[1], zgm[1], d, radl, absl, buf, nbuf);
598   // Create gas mixture 
599   wgm[0] = fxi;
600   wgm[1] = 1. - fxi;
601   dgm2   = wgm[0] * dxe + wgm[1] * dis;
602   AliMixture(11, "Gas mixture 2", agm, zgm, dgm2,  2, wgm);
603  
604   //////////////////////////////////////////////////////////////////////////
605   //     Tracking Media Parameters 
606   //////////////////////////////////////////////////////////////////////////
607
608   // Al Frame 
609   AliMedium(1, "Al Frame",   1, 0, isxfld, sxmgmx
610                 , tmaxfd, stemax, deemax, epsil, stmin);
611   // Air 
612   AliMedium(2, "Air",        2, 0, isxfld, sxmgmx
613                 , tmaxfd, stemax, deemax, epsil, stmin);
614   // Polyethilene 
615   AliMedium(3, "Radiator",   3, 0, isxfld, sxmgmx
616                 , tmaxfd, stemax, deemax, epsil, stmin);
617   // Xe 
618   AliMedium(4, "Xe",         4, 1, isxfld, sxmgmx
619                 , tmaxfd, stemax, deemax, epsil, stmin);
620   // Cu pads 
621   AliMedium(5, "Padplane",   5, 1, isxfld, sxmgmx
622                 , tmaxfd, stemax, deemax, epsil, stmin);
623   // Fee + cables 
624   AliMedium(6, "Readout",    1, 0, isxfld, sxmgmx
625                 , tmaxfd, stemax, deemax, epsil, stmin);
626   // C frame 
627   AliMedium(7, "C Frame",    6, 0, isxfld, sxmgmx
628                 , tmaxfd, stemax, deemax, epsil, stmin);
629   // Mylar foils 
630   AliMedium(8, "Mylar",      7, 0, isxfld, sxmgmx
631                 , tmaxfd, stemax, deemax, epsil, stmin);
632   if (fGasMix == 1) {
633     // Gas-mixture (Xe/CO2) 
634     AliMedium(9, "Gas-mix",   10, 1, isxfld, sxmgmx
635                   , tmaxfd, stemax, deemax, epsil, stmin);
636   }
637   else {
638     // Gas-mixture (Xe/Isobutane) 
639     AliMedium(9, "Gas-mix",   11, 1, isxfld, sxmgmx
640                   , tmaxfd, stemax, deemax, epsil, stmin);
641   }
642   // Nomex-honeycomb (use carbon for the time being) 
643   AliMedium(10, "Nomex",      6, 0, isxfld, sxmgmx
644                 , tmaxfd, stemax, deemax, epsil, stmin);
645   // Kapton foils (use Mylar for the time being) 
646   AliMedium(11, "Kapton",     7, 0, isxfld, sxmgmx
647                 , tmaxfd, stemax, deemax, epsil, stmin);
648   // Gas-filling of the radiator 
649   AliMedium(12, "CO2",        8, 0, isxfld, sxmgmx
650                 , tmaxfd, stemax, deemax, epsil, stmin);
651   // G10-plates
652   AliMedium(13, "G10-plates",12, 0, isxfld, sxmgmx
653                 , tmaxfd, stemax, deemax, epsil, stmin);
654   // Cooling water
655   AliMedium(14, "Water",     13, 0, isxfld, sxmgmx
656                 , tmaxfd, stemax, deemax, epsil, stmin);
657   // Rohacell (plexiglas) for the radiator
658   AliMedium(15, "Rohacell",  14, 0, isxfld, sxmgmx
659                 , tmaxfd, stemax, deemax, epsil, stmin);
660   // Al layer in MCMs
661   AliMedium(16, "MCM-Al"  ,   1, 0, isxfld, sxmgmx
662                 , tmaxfd, stemax, deemax, epsil, stmin);
663   // Sn layer in MCMs
664   AliMedium(17, "MCM-Sn"  ,  15, 0, isxfld, sxmgmx
665                 , tmaxfd, stemax, deemax, epsil, stmin);
666   // Cu layer in MCMs
667   AliMedium(18, "MCM-Cu"  ,   5, 0, isxfld, sxmgmx
668                 , tmaxfd, stemax, deemax, epsil, stmin);
669   // G10 layer in MCMs
670   AliMedium(19, "MCM-G10" ,  12, 0, isxfld, sxmgmx
671                 , tmaxfd, stemax, deemax, epsil, stmin);
672   // Si in readout chips
673   AliMedium(20, "Chip-Si" ,  16, 0, isxfld, sxmgmx
674                 , tmaxfd, stemax, deemax, epsil, stmin);
675   // Epoxy in readout chips
676   AliMedium(21, "Chip-Ep" ,  17, 0, isxfld, sxmgmx
677                 , tmaxfd, stemax, deemax, epsil, stmin);
678   // PE in connectors
679   AliMedium(22, "Conn-PE" ,   3, 0, isxfld, sxmgmx
680                 , tmaxfd, stemax, deemax, epsil, stmin);
681   // Cu in connectors
682   AliMedium(23, "Chip-Cu" ,   5, 0, isxfld, sxmgmx
683                 , tmaxfd, stemax, deemax, epsil, stmin);
684   // Al of cooling pipes
685   AliMedium(24, "Cooling" ,   1, 0, isxfld, sxmgmx
686                 , tmaxfd, stemax, deemax, epsil, stmin);
687   // Cu in services
688   AliMedium(25, "Serv-Cu" ,   5, 0, isxfld, sxmgmx
689                 , tmaxfd, stemax, deemax, epsil, stmin);
690
691   // Save the density values for the TRD absorbtion
692   fFoilDensity = dmy;
693   if (fGasMix == 1)
694     fGasDensity = dgm1;
695   else
696     fGasDensity = dgm2;
697
698 }
699
700 //_____________________________________________________________________________
701 void AliTRD::DrawModule() const
702 {
703   //
704   // Draw a shaded view of the Transition Radiation Detector version 0
705   //
706
707   // Set everything unseen
708   gMC->Gsatt("*"   ,"SEEN",-1);
709   
710   // Set ALIC mother transparent
711   gMC->Gsatt("ALIC","SEEN", 0);
712   
713   // Set the volumes visible
714   if (fGeometry->IsVersion() == 0) {
715     gMC->Gsatt("B071","SEEN", 0);
716     gMC->Gsatt("B074","SEEN", 0);
717     gMC->Gsatt("B075","SEEN", 0);
718     gMC->Gsatt("B077","SEEN", 0);
719     gMC->Gsatt("BTR1","SEEN", 0);
720     gMC->Gsatt("BTR2","SEEN", 0);
721     gMC->Gsatt("BTR3","SEEN", 0);
722     gMC->Gsatt("UTR1","SEEN", 0);
723     gMC->Gsatt("UTR2","SEEN", 0);
724     gMC->Gsatt("UTR3","SEEN", 0);
725   }
726   else {
727     gMC->Gsatt("B071","SEEN", 0);
728     gMC->Gsatt("B074","SEEN", 0);
729     gMC->Gsatt("B075","SEEN", 0);
730     gMC->Gsatt("B077","SEEN", 0);
731     gMC->Gsatt("BTR1","SEEN", 0);
732     gMC->Gsatt("BTR2","SEEN", 0);
733     gMC->Gsatt("BTR3","SEEN", 0);
734     gMC->Gsatt("UTR1","SEEN", 0);
735     if (fGeometry->GetPHOShole())
736       gMC->Gsatt("UTR2","SEEN", 0);
737     if (fGeometry->GetRICHhole())
738       gMC->Gsatt("UTR3","SEEN", 0);
739   }
740 //   gMC->Gsatt("UCII","SEEN", 0);
741 //   gMC->Gsatt("UCIM","SEEN", 0);
742 //   gMC->Gsatt("UCIO","SEEN", 0);
743 //   gMC->Gsatt("UL02","SEEN", 1);
744 //   gMC->Gsatt("UL05","SEEN", 1);
745 //   gMC->Gsatt("UL06","SEEN", 1);
746   
747   gMC->Gdopt("hide", "on");
748   gMC->Gdopt("shad", "on");
749   gMC->Gsatt("*", "fill", 7);
750   gMC->SetClipBox(".");
751   gMC->SetClipBox("*", 0, 2000, -2000, 2000, -2000, 2000);
752   gMC->DefaultRange();
753   gMC->Gdraw("alic", 40, 30, 0, 12, 9.4, .021, .021);
754   gMC->Gdhead(1111, "Transition Radiation Detector");
755   gMC->Gdman(18, 4, "MAN");
756
757 }
758
759 //_____________________________________________________________________________
760 Int_t AliTRD::DistancetoPrimitive(Int_t , Int_t ) const
761 {
762   //
763   // Distance between the mouse and the TRD detector on the screen
764   // Dummy routine
765   
766   return 9999;
767
768 }
769  
770 //_____________________________________________________________________________
771 void AliTRD::Init()
772 {
773   //
774   // Initialize the TRD detector after the geometry has been created
775   //
776
777   Int_t i;
778
779   if (fDebug) {
780     printf("\n%s: ",ClassName());
781     for (i = 0; i < 35; i++) printf("*");
782     printf(" TRD_INIT ");
783     for (i = 0; i < 35; i++) printf("*");
784     printf("\n");
785   }
786
787   if (fGeometry->IsVersion() == 1) {
788     printf("%s: Full geometry version initialized\n",ClassName());
789     if (fGeometry->GetPHOShole())
790       printf("%s: Leave space in front of PHOS free\n",ClassName());
791     if (fGeometry->GetRICHhole())
792       printf("%s: Leave space in front of RICH free\n",ClassName());
793   }
794   else {
795     printf("%s: Not a valid geometry\n",ClassName());
796   }
797   
798   if (fGasMix == 1) {
799     printf("%s: Gas Mixture: 85%% Xe + 15%% CO2\n",ClassName());
800   }
801   else {
802     printf("%s: Gas Mixture: 97%% Xe + 3%% Isobutane\n",ClassName());
803   }
804
805 }
806
807 //_____________________________________________________________________________
808 void AliTRD::LoadPoints(Int_t )
809 {
810   //
811   // Store x, y, z of all hits in memory.
812   // Hit originating from TR photons are given a different color
813   //
814
815   //if (!fDrawTR) {
816   //  AliDetector::LoadPoints(track);
817   //  return;
818   //}
819
820   if ((fHits == 0) && (fTrackHits == 0)) return;
821
822   Int_t nhits;
823   if (fHitType < 2) {
824     nhits = fHits->GetEntriesFast();
825   }
826   else {
827     nhits = fTrackHits->GetEntriesFast();
828   } 
829   if (nhits == 0) return;
830
831   Int_t tracks = gAlice->GetMCApp()->GetNtrack();
832   if (fPoints == 0) fPoints = new TObjArray(tracks);
833
834   AliTRDhit *ahit;
835   
836   Int_t    *ntrkE = new Int_t[tracks];
837   Int_t    *ntrkT = new Int_t[tracks];
838   Int_t    *limiE = new Int_t[tracks];
839   Int_t    *limiT = new Int_t[tracks];
840   Float_t **coorE = new Float_t*[tracks];
841   Float_t **coorT = new Float_t*[tracks];
842   for(Int_t i = 0; i < tracks; i++) {
843     ntrkE[i] = 0;
844     ntrkT[i] = 0;
845     coorE[i] = 0;
846     coorT[i] = 0;
847     limiE[i] = 0;
848     limiT[i] = 0;
849   }
850   
851   AliTRDpoints *points = 0;
852   Float_t      *fp     = 0;
853   Int_t         trk;
854   Int_t         chunk  = nhits / 4 + 1;
855
856   // Loop over all the hits and store their position
857   ahit = (AliTRDhit *) FirstHit(-1);
858   while (ahit) {
859
860     // dEdx hits
861     if (ahit->GetCharge() >= 0) {
862
863       trk = ahit->GetTrack();
864       if (ntrkE[trk] == limiE[trk]) {
865         // Initialise a new track
866         fp = new Float_t[3*(limiE[trk]+chunk)];
867         if (coorE[trk]) {
868           memcpy(fp,coorE[trk],sizeof(Float_t)*3*limiE[trk]);
869           delete [] coorE[trk];
870         }
871         limiE[trk] += chunk;
872         coorE[trk]  = fp;
873       } 
874       else {
875         fp = coorE[trk];
876       }
877       fp[3*ntrkE[trk]  ] = ahit->X();
878       fp[3*ntrkE[trk]+1] = ahit->Y();
879       fp[3*ntrkE[trk]+2] = ahit->Z();
880       ntrkE[trk]++;
881
882     }
883     // TR photon hits
884     else if ((ahit->GetCharge() < 0) && (fDrawTR)) {
885
886       trk = ahit->GetTrack();
887       if (ntrkT[trk] == limiT[trk]) {
888         // Initialise a new track
889         fp = new Float_t[3*(limiT[trk]+chunk)];
890         if (coorT[trk]) {
891           memcpy(fp,coorT[trk],sizeof(Float_t)*3*limiT[trk]);
892           delete [] coorT[trk];
893         }
894         limiT[trk] += chunk;
895         coorT[trk]  = fp;
896       } 
897       else {
898         fp = coorT[trk];
899       }
900       fp[3*ntrkT[trk]  ] = ahit->X();
901       fp[3*ntrkT[trk]+1] = ahit->Y();
902       fp[3*ntrkT[trk]+2] = ahit->Z();
903       ntrkT[trk]++;
904
905     }
906
907     ahit = (AliTRDhit *) NextHit();
908
909   }
910
911   for (trk = 0; trk < tracks; ++trk) {
912
913     if (ntrkE[trk] || ntrkT[trk]) {
914
915       points = new AliTRDpoints();
916       points->SetDetector(this);
917       points->SetParticle(trk);
918
919       // Set the dEdx points
920       if (ntrkE[trk]) {
921         points->SetMarkerColor(GetMarkerColor());
922         points->SetMarkerSize(GetMarkerSize());
923         points->SetPolyMarker(ntrkE[trk],coorE[trk],GetMarkerStyle());
924         delete [] coorE[trk];
925         coorE[trk] = 0;
926       }
927
928       // Set the TR photon points
929       if (ntrkT[trk]) {
930         points->SetTRpoints(ntrkT[trk],coorT[trk]);
931         delete [] coorT[trk];
932         coorT[trk] = 0;
933       }
934
935       fPoints->AddAt(points,trk);
936
937     }
938
939   }
940
941   delete [] coorE;
942   delete [] coorT;
943   delete [] ntrkE;
944   delete [] ntrkT;
945   delete [] limiE;
946   delete [] limiT;
947
948 }
949
950 //_____________________________________________________________________________
951 void AliTRD::MakeBranch(Option_t* option)
952 {
953   //
954   // Create Tree branches for the TRD digits.
955   //
956
957   Int_t  buffersize = 4000;
958   Char_t branchname[15];
959   sprintf(branchname,"%s",GetName());
960
961   const char *cD = strstr(option,"D");
962
963   AliDetector::MakeBranch(option);
964
965   if (fDigits && gAlice->TreeD() && cD) {
966     MakeBranchInTree(gAlice->TreeD(),branchname,&fDigits,buffersize,0);
967   }     
968
969   if (fHitType > 1) {
970     MakeBranch2(option,0); 
971   }
972
973 }
974
975 //_____________________________________________________________________________
976 void AliTRD::ResetDigits()
977 {
978   //
979   // Reset number of digits and the digits array for this detector
980   //
981
982   fNdigits = 0;
983   if (fDigits) fDigits->Clear();
984
985 }
986
987 //_____________________________________________________________________________
988 void AliTRD::ResetRecPoints()
989 {
990   //
991   // Reset number of reconstructed points and the point array
992   //
993
994   if (fRecPoints) {
995     fNRecPoints = 0;
996     Int_t nentr = fRecPoints->GetEntriesFast();
997     for (Int_t i = 0; i < nentr; i++) delete fRecPoints->RemoveAt(i);
998   }
999
1000 }
1001
1002 //_____________________________________________________________________________
1003 void AliTRD::SetTreeAddress()
1004 {
1005   //
1006   // Set the branch addresses for the trees.
1007   //
1008
1009   Char_t branchname[15];
1010
1011   if ( fLoader->TreeH() && (fHits == 0x0))  fHits = new TClonesArray("AliTRDhit",405);
1012   AliDetector::SetTreeAddress();
1013
1014   TBranch *branch;
1015   TTree   *treeR = fLoader->TreeR();
1016
1017   if (treeR) {
1018     sprintf(branchname,"%scluster",GetName());
1019     if (fRecPoints == 0x0) fRecPoints  = new TObjArray(400);
1020     if (fRecPoints) {
1021       branch = treeR->GetBranch(branchname);
1022       if (branch) {
1023         branch->SetAddress(&fRecPoints);
1024       }
1025     }
1026   }
1027
1028   if (fHitType > 0) {
1029     SetTreeAddress2();    
1030   }
1031
1032 }
1033
1034 //_____________________________________________________________________________
1035 void AliTRD::SetGasMix(Int_t imix)
1036 {
1037   //
1038   // Defines the gas mixture (imix=0:  Xe/Isobutane imix=1: Xe/CO2)
1039   //
1040   
1041   if ((imix < 0) || (imix > 1)) {
1042     printf("Wrong input value: %d\n",imix);
1043     printf("Use standard setting\n");
1044     fGasMix = 1;
1045     return;
1046   }
1047
1048   fGasMix = imix;
1049
1050 }
1051
1052 //_____________________________________________________________________________
1053 void AliTRD::SetPHOShole()
1054 {
1055   //
1056   // Selects a geometry with a hole in front of the PHOS
1057   //
1058
1059   fGeometry->SetPHOShole();
1060
1061 }
1062
1063 //_____________________________________________________________________________
1064 void AliTRD::SetRICHhole()
1065 {
1066   //
1067   // Selects a geometry with a hole in front of the RICH
1068   //
1069
1070   fGeometry->SetRICHhole();
1071
1072 }
1073
1074 //_____________________________________________________________________________
1075 AliTRD &AliTRD::operator=(const AliTRD &trd)
1076 {
1077   //
1078   // Assignment operator
1079   //
1080
1081   if (this != &trd) ((AliTRD &) trd).Copy(*this);
1082   return *this;
1083
1084
1085
1086 //_____________________________________________________________________________
1087 void AliTRD::FinishPrimary()
1088 {
1089   //
1090   // Store the hits in the containers after all primaries are finished
1091   //
1092
1093   if (fTrackHits) { 
1094     fTrackHits->FlushHitStack();
1095   }
1096
1097 }
1098
1099 //_____________________________________________________________________________
1100 void AliTRD::RemapTrackHitIDs(Int_t *map)
1101 {
1102   //
1103   // Remap the track IDs
1104   //
1105
1106   if (!fTrackHits) {
1107     return;
1108   }
1109
1110   if (fTrackHits) {
1111     TClonesArray *arr = fTrackHits->GetArray();;
1112     for (Int_t i = 0; i < arr->GetEntriesFast(); i++){
1113       AliTrackHitsParamV2 *info = (AliTrackHitsParamV2 *) (arr->At(i));
1114       info->fTrackID = map[info->fTrackID];
1115     }
1116   }
1117
1118 }
1119
1120 //_____________________________________________________________________________
1121 void AliTRD::ResetHits()
1122 {
1123   //
1124   // Reset the hits
1125   //
1126
1127   AliDetector::ResetHits();
1128   if (fTrackHits) {
1129     fTrackHits->Clear();
1130   }
1131
1132 }
1133
1134 //_____________________________________________________________________________
1135 AliHit* AliTRD::FirstHit(Int_t track)
1136 {
1137   //
1138   // Return the first hit of a track
1139   //
1140
1141   if (fHitType > 1) {
1142     return FirstHit2(track);
1143   }
1144
1145   return AliDetector::FirstHit(track);
1146
1147 }
1148
1149 //_____________________________________________________________________________
1150 AliHit* AliTRD::NextHit()
1151 {
1152   //
1153   // Returns the next hit of a track
1154   //
1155
1156   if (fHitType > 1) {
1157     return NextHit2();
1158   }
1159
1160   return AliDetector::NextHit();
1161
1162 }
1163
1164 //_____________________________________________________________________________
1165 AliHit* AliTRD::FirstHit2(Int_t track) 
1166 {
1167   //
1168   // Initializes the hit iterator.
1169   // Returns the address of the first hit of a track.
1170   // If <track> >= 0 the track is read from disk,
1171   // while if <track> < 0 the first hit of the current
1172   // track is returned.
1173   //
1174
1175   if (track >= 0) {
1176     gAlice->ResetHits();
1177     TreeH()->GetEvent(track);
1178   }
1179   
1180   if (fTrackHits) {
1181     fTrackHits->First();
1182     return (AliHit*) fTrackHits->GetHit();
1183   }
1184   else {
1185     return 0;
1186   }
1187
1188 }
1189
1190 //_____________________________________________________________________________
1191 AliHit* AliTRD::NextHit2()
1192 {
1193   //
1194   // Returns the next hit of the current track
1195   //
1196
1197   if (fTrackHits) {
1198     fTrackHits->Next();
1199     return (AliHit *) fTrackHits->GetHit();
1200   }
1201   else {
1202     return 0;
1203   }
1204
1205 }
1206
1207 //_____________________________________________________________________________
1208 void AliTRD::MakeBranch2(Option_t *option, const char* )
1209 {
1210   //
1211   // Create a new branch in the current Root tree.
1212   // The branch of fHits is automatically split.
1213   //
1214
1215   if (fHitType < 2) {
1216     return;
1217   }
1218
1219   char branchname[10];
1220   sprintf(branchname,"%s2",GetName());
1221
1222   // Get the pointer to the header
1223   const char *cH = strstr(option,"H");
1224  
1225   if (!fTrackHits) {
1226     fTrackHits = new AliTRDtrackHits();
1227    }
1228
1229
1230   if (fTrackHits && TreeH() && cH) 
1231    {
1232     TreeH()->Branch(branchname,"AliTRDtrackHits",&fTrackHits,fBufferSize,99);
1233     Info("MakeBranch2","Making Branch %s for trackhits",branchname);
1234    }
1235 }
1236
1237 //_____________________________________________________________________________
1238 void AliTRD::SetTreeAddress2()
1239 {
1240   //
1241   // Set the branch address for the trackHits tree
1242   //
1243   if (GetDebug())  Info("SetTreeAddress2","");
1244
1245   TBranch *branch;
1246   char branchname[20];
1247   sprintf(branchname,"%s2",GetName());
1248   
1249   // Branch address for hit tree
1250   TTree *treeH = TreeH();
1251   if ((treeH) && (fHitType > 0)) {
1252     branch = treeH->GetBranch(branchname);
1253     if (branch) 
1254      {
1255        branch->SetAddress(&fTrackHits);
1256        if (GetDebug())
1257          Info("SetTreeAddress2","Success.");
1258      }
1259     else
1260      {
1261        if (GetDebug())
1262          Info("SetTreeAddress2","Can NOT get the branch %s",branchname);
1263      }
1264   }
1265
1266 }
1267
1268 //_____________________________________________________________________________
1269 void AliTRD::AddHit2(Int_t track, Int_t det, Float_t *hits, Int_t q
1270                    , Bool_t inDrift)
1271 {
1272   //
1273   // Add a hit to the list
1274   //
1275
1276   Int_t rtrack;
1277
1278   if (fIshunt) {
1279     Int_t primary = gAlice->GetMCApp()->GetPrimary(track);
1280     gAlice->GetMCApp()->Particle(primary)->SetBit(kKeepBit);
1281     rtrack = primary;
1282   } 
1283   else {
1284     rtrack = track;
1285     gAlice->GetMCApp()->FlagTrack(track);
1286   }
1287
1288   if ((fTrackHits) && (fHitType > 0)) {
1289     fTrackHits->AddHitTRD(det,rtrack,hits[0],hits[1],hits[2],q,inDrift);
1290   }
1291
1292 }
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629