ed0c538f68cbcd9930a3d3db172de398e77cc8e6
[u/mrichter/AliRoot.git] / MUON / AliMUONv1.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *      SigmaEffect_thetadegrees                                                                  *
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 purpeateose. It is      *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /* $Id$ */
17
18 /////////////////////////////////////////////////////////
19 //  Manager and hits classes for set:MUON version 0    //
20 /////////////////////////////////////////////////////////
21 #include <TRandom.h>
22 #include <TF1.h>
23 #include <TClonesArray.h>
24 #include <TLorentzVector.h>   
25 #include <TVirtualMC.h>
26 #include <TParticle.h>
27
28 #include "AliConst.h" 
29 #include "AliMUONChamber.h"
30 #include "AliMUONConstants.h"
31 #include "AliMUONFactory.h"
32 #include "AliMUONHit.h"
33 #include "AliMUONTriggerCircuit.h"
34 #include "AliMUONv1.h"
35 #include "AliMagF.h"
36 #include "AliRun.h"
37 #include "AliMC.h"
38
39 ClassImp(AliMUONv1)
40  
41 //___________________________________________
42 AliMUONv1::AliMUONv1() : AliMUON()
43   ,fTrackMomentum(), fTrackPosition()
44 {
45 // Constructor
46     fChambers   = 0;
47     fStations   = 0;
48     fStepManagerVersionOld  = kFALSE;
49     fStepMaxInActiveGas     = 0.6;
50     fStepSum    =  0x0;
51     fDestepSum  =  0x0;
52     fElossRatio =  0x0;
53     fAngleEffect10   = 0x0;
54     fAngleEffectNorma= 0x0;
55
56 //___________________________________________
57 AliMUONv1::AliMUONv1(const char *name, const char *title)
58   : AliMUON(name,title), fTrackMomentum(), fTrackPosition()
59 {
60 // Constructor
61     // By default include all stations
62     fStations = new Int_t[5];
63     for (Int_t i=0; i<5; i++) fStations[i] = 1;
64
65     AliMUONFactory factory;
66     factory.Build(this, title);
67
68     fStepManagerVersionOld = kFALSE;
69
70     fStepMaxInActiveGas = 0.6;
71
72     fStepSum   = new Float_t [AliMUONConstants::NCh()];
73     fDestepSum = new Float_t [AliMUONConstants::NCh()];
74     for (Int_t i=0; i<AliMUONConstants::NCh(); i++) {
75       fStepSum[i] =0.0;
76       fDestepSum[i]=0.0;
77     }
78     // Ratio of particle mean eloss with respect MIP's Khalil Boudjemline, sep 2003, PhD.Thesis and Particle Data Book
79     fElossRatio = new TF1("ElossRatio","[0]+[1]*x+[2]*x*x+[3]*x*x*x+[4]*x*x*x*x",0.5,5.); 
80     fElossRatio->SetParameter(0,1.02138);
81     fElossRatio->SetParameter(1,-9.54149e-02);
82     fElossRatio->SetParameter(2,+7.83433e-02); 
83     fElossRatio->SetParameter(3,-9.98208e-03);
84     fElossRatio->SetParameter(4,+3.83279e-04);
85
86     // Angle effect in tracking chambers at theta =10 degres as a function of ElossRatio (Khalil BOUDJEMLINE sep 2003 Ph.D Thesis) (in micrometers)
87     fAngleEffect10 = new TF1("AngleEffect10","[0]+[1]*x+[2]*x*x",0.5,3.0);
88     fAngleEffect10->SetParameter(0, 1.90691e+02);
89     fAngleEffect10->SetParameter(1,-6.62258e+01);
90     fAngleEffect10->SetParameter(2,+1.28247e+01);
91     // Angle effect: Normalisation form theta=10 degres to theta between 0 and 10 (Khalil BOUDJEMLINE sep 2003 Ph.D Thesis)  
92     // Angle with respect to the wires assuming that chambers are perpendicular to the z axis.
93     fAngleEffectNorma = new TF1("AngleEffectNorma","[0]+[1]*x+[2]*x*x+[3]*x*x*x",0.0,10.0);
94     fAngleEffectNorma->SetParameter(0,4.148);
95     fAngleEffectNorma->SetParameter(1,-6.809e-01);
96     fAngleEffectNorma->SetParameter(2,5.151e-02);
97     fAngleEffectNorma->SetParameter(3,-1.490e-03);
98 }
99
100 //___________________________________________
101 void AliMUONv1::CreateGeometry()
102 {
103 //
104 //   Note: all chambers have the same structure, which could be 
105 //   easily parameterised. This was intentionally not done in order
106 //   to give a starting point for the implementation of the actual 
107 //   design of each station. 
108   Int_t *idtmed = fIdtmed->GetArray()-1099;
109
110 //   Distance between Stations
111 //
112      Float_t bpar[3];
113      Float_t tpar[3];
114 //      Float_t pgpar[10];
115      Float_t zpos1, zpos2, zfpos;
116      // Outer excess and inner recess for mother volume radius
117      // with respect to ROuter and RInner
118      Float_t dframep=.001; // Value for station 3 should be 6 ...
119      // Width (RdPhi) of the frame crosses for stations 1 and 2 (cm)
120 //      Float_t dframep1=.001;
121      Float_t dframep1 = 11.0;
122 //      Bool_t frameCrosses=kFALSE;     
123      Bool_t frameCrosses=kTRUE;     
124      Float_t *dum=0;
125      
126 //      Float_t dframez=0.9;
127      // Half of the total thickness of frame crosses (including DAlu)
128      // for each chamber in stations 1 and 2:
129      // 3% of X0 of composite material,
130      // but taken as Aluminium here, with same thickness in number of X0
131      Float_t dframez = 3. * 8.9 / 100;
132 //      Float_t dr;
133      Float_t dstation;
134
135 //
136 //   Rotation matrices in the x-y plane  
137      Int_t idrotm[1199];
138 //   phi=   0 deg
139      AliMatrix(idrotm[1100],  90.,   0., 90.,  90., 0., 0.);
140 //   phi=  90 deg
141      AliMatrix(idrotm[1101],  90.,  90., 90., 180., 0., 0.);
142 //   phi= 180 deg
143      AliMatrix(idrotm[1102],  90., 180., 90., 270., 0., 0.);
144 //   phi= 270 deg
145      AliMatrix(idrotm[1103],  90., 270., 90.,   0., 0., 0.);
146 //
147      Float_t phi=2*TMath::Pi()/12/2;
148
149 //
150 //   pointer to the current chamber
151 //   pointer to the current chamber
152      Int_t idAlu1=idtmed[1103]; // medium 4
153      Int_t idAlu2=idtmed[1104]; // medium 5
154 //     Int_t idAlu1=idtmed[1100];
155 //     Int_t idAlu2=idtmed[1100];
156      Int_t idAir=idtmed[1100]; // medium 1
157 //      Int_t idGas=idtmed[1105]; // medium 6 = Ar-isoC4H10 gas
158      Int_t idGas=idtmed[1108]; // medium 9 = Ar-CO2 gas (80%+20%)
159      
160
161      AliMUONChamber *iChamber, *iChamber1, *iChamber2;
162
163      if (fStations[0]) {
164          
165 //********************************************************************
166 //                            Station 1                             **
167 //********************************************************************
168 //  CONCENTRIC
169      // indices 1 and 2 for first and second chambers in the station
170      // iChamber (first chamber) kept for other quanties than Z,
171      // assumed to be the same in both chambers
172      iChamber1 = iChamber = (AliMUONChamber*) (*fChambers)[0];
173      iChamber2 =(AliMUONChamber*) (*fChambers)[1];
174      zpos1=iChamber1->Z(); 
175      zpos2=iChamber2->Z();
176      dstation = TMath::Abs(zpos2 - zpos1);
177      // DGas decreased from standard one (0.5)
178      iChamber->SetDGas(0.4); iChamber2->SetDGas(0.4);
179      // DAlu increased from standard one (3% of X0),
180      // because more electronics with smaller pads
181      iChamber->SetDAlu(3.5 * 8.9 / 100.); iChamber2->SetDAlu(3.5 * 8.9 / 100.);
182      zfpos=-(iChamber->DGas()+dframez+iChamber->DAlu())/2;
183      
184 //
185 //   Mother volume
186      tpar[0] = iChamber->RInner()-dframep; 
187      tpar[1] = (iChamber->ROuter()+dframep)/TMath::Cos(phi);
188      tpar[2] = dstation/5;
189
190      gMC->Gsvolu("S01M", "TUBE", idAir, tpar, 3);
191      gMC->Gsvolu("S02M", "TUBE", idAir, tpar, 3);
192      gMC->Gspos("S01M", 1, "ALIC", 0., 0., zpos1 , 0, "ONLY");
193      gMC->Gspos("S02M", 1, "ALIC", 0., 0., zpos2 , 0, "ONLY");     
194 // // Aluminium frames
195 // // Outer frames
196 //      pgpar[0] = 360/12/2;
197 //      pgpar[1] = 360.;
198 //      pgpar[2] = 12.;
199 //      pgpar[3] =   2;
200 //      pgpar[4] = -dframez/2;
201 //      pgpar[5] = iChamber->ROuter();
202 //      pgpar[6] = pgpar[5]+dframep1;
203 //      pgpar[7] = +dframez/2;
204 //      pgpar[8] = pgpar[5];
205 //      pgpar[9] = pgpar[6];
206 //      gMC->Gsvolu("S01O", "PGON", idAlu1, pgpar, 10);
207 //      gMC->Gsvolu("S02O", "PGON", idAlu1, pgpar, 10);
208 //      gMC->Gspos("S01O",1,"S01M", 0.,0.,-zfpos,  0,"ONLY");
209 //      gMC->Gspos("S01O",2,"S01M", 0.,0.,+zfpos,  0,"ONLY");
210 //      gMC->Gspos("S02O",1,"S02M", 0.,0.,-zfpos,  0,"ONLY");
211 //      gMC->Gspos("S02O",2,"S02M", 0.,0.,+zfpos,  0,"ONLY");
212 // //
213 // // Inner frame
214 //      tpar[0]= iChamber->RInner()-dframep1;
215 //      tpar[1]= iChamber->RInner();
216 //      tpar[2]= dframez/2;
217 //      gMC->Gsvolu("S01I", "TUBE", idAlu1, tpar, 3);
218 //      gMC->Gsvolu("S02I", "TUBE", idAlu1, tpar, 3);
219
220 //      gMC->Gspos("S01I",1,"S01M", 0.,0.,-zfpos,  0,"ONLY");
221 //      gMC->Gspos("S01I",2,"S01M", 0.,0.,+zfpos,  0,"ONLY");
222 //      gMC->Gspos("S02I",1,"S02M", 0.,0.,-zfpos,  0,"ONLY");
223 //      gMC->Gspos("S02I",2,"S02M", 0.,0.,+zfpos,  0,"ONLY");
224 //
225 // Frame Crosses
226      if (frameCrosses) {
227          // outside gas
228          // security for inside mother volume
229          bpar[0] = (iChamber->ROuter() - iChamber->RInner())
230            * TMath::Cos(TMath::ASin(dframep1 /
231                                    (iChamber->ROuter() - iChamber->RInner())))
232            / 2.0;
233          bpar[1] = dframep1/2;
234          // total thickness will be (4 * bpar[2]) for each chamber,
235          // which has to be equal to (2 * dframez) - DAlu
236          bpar[2] = (2.0 * dframez - iChamber->DAlu()) / 4.0;
237          gMC->Gsvolu("S01B", "BOX", idAlu1, bpar, 3);
238          gMC->Gsvolu("S02B", "BOX", idAlu1, bpar, 3);
239          
240          gMC->Gspos("S01B",1,"S01M", -iChamber->RInner()-bpar[0] , 0, zfpos, 
241                     idrotm[1100],"ONLY");
242          gMC->Gspos("S01B",2,"S01M",  iChamber->RInner()+bpar[0] , 0, zfpos, 
243                     idrotm[1100],"ONLY");
244          gMC->Gspos("S01B",3,"S01M", 0, -iChamber->RInner()-bpar[0] , zfpos, 
245                     idrotm[1101],"ONLY");
246          gMC->Gspos("S01B",4,"S01M", 0,  iChamber->RInner()+bpar[0] , zfpos, 
247                     idrotm[1101],"ONLY");
248          gMC->Gspos("S01B",5,"S01M", -iChamber->RInner()-bpar[0] , 0,-zfpos, 
249                     idrotm[1100],"ONLY");
250          gMC->Gspos("S01B",6,"S01M", +iChamber->RInner()+bpar[0] , 0,-zfpos, 
251                     idrotm[1100],"ONLY");
252          gMC->Gspos("S01B",7,"S01M", 0, -iChamber->RInner()-bpar[0] ,-zfpos, 
253                     idrotm[1101],"ONLY");
254          gMC->Gspos("S01B",8,"S01M", 0, +iChamber->RInner()+bpar[0] ,-zfpos, 
255                     idrotm[1101],"ONLY");
256          
257          gMC->Gspos("S02B",1,"S02M", -iChamber->RInner()-bpar[0] , 0, zfpos, 
258                     idrotm[1100],"ONLY");
259          gMC->Gspos("S02B",2,"S02M",  iChamber->RInner()+bpar[0] , 0, zfpos, 
260                     idrotm[1100],"ONLY");
261          gMC->Gspos("S02B",3,"S02M", 0, -iChamber->RInner()-bpar[0] , zfpos, 
262                     idrotm[1101],"ONLY");
263          gMC->Gspos("S02B",4,"S02M", 0,  iChamber->RInner()+bpar[0] , zfpos, 
264                     idrotm[1101],"ONLY");
265          gMC->Gspos("S02B",5,"S02M", -iChamber->RInner()-bpar[0] , 0,-zfpos, 
266                     idrotm[1100],"ONLY");
267          gMC->Gspos("S02B",6,"S02M", +iChamber->RInner()+bpar[0] , 0,-zfpos, 
268                     idrotm[1100],"ONLY");
269          gMC->Gspos("S02B",7,"S02M", 0, -iChamber->RInner()-bpar[0] ,-zfpos, 
270                     idrotm[1101],"ONLY");
271          gMC->Gspos("S02B",8,"S02M", 0, +iChamber->RInner()+bpar[0] ,-zfpos, 
272                     idrotm[1101],"ONLY");
273      }
274 //
275 //   Chamber Material represented by Alu sheet
276      tpar[0]= iChamber->RInner();
277      tpar[1]= iChamber->ROuter();
278      tpar[2] = (iChamber->DGas()+iChamber->DAlu())/2;
279      gMC->Gsvolu("S01A", "TUBE",  idAlu2, tpar, 3);
280      gMC->Gsvolu("S02A", "TUBE",idAlu2, tpar, 3);
281      gMC->Gspos("S01A", 1, "S01M", 0., 0., 0.,  0, "ONLY");
282      gMC->Gspos("S02A", 1, "S02M", 0., 0., 0.,  0, "ONLY");
283 //     
284 //   Sensitive volumes
285      // tpar[2] = iChamber->DGas();
286      tpar[2] = iChamber->DGas()/2;
287      gMC->Gsvolu("S01G", "TUBE", idGas, tpar, 3);
288      gMC->Gsvolu("S02G", "TUBE", idGas, tpar, 3);
289      gMC->Gspos("S01G", 1, "S01A", 0., 0., 0.,  0, "ONLY");
290      gMC->Gspos("S02G", 1, "S02A", 0., 0., 0.,  0, "ONLY");
291 //
292 // Frame Crosses to be placed inside gas
293      // NONE: chambers are sensitive everywhere
294 //      if (frameCrosses) {
295
296 //       dr = (iChamber->ROuter() - iChamber->RInner());
297 //       bpar[0] = TMath::Sqrt(dr*dr-dframep1*dframep1/4)/2;
298 //       bpar[1] = dframep1/2;
299 //       bpar[2] = iChamber->DGas()/2;
300 //       gMC->Gsvolu("S01F", "BOX", idAlu1, bpar, 3);
301 //       gMC->Gsvolu("S02F", "BOX", idAlu1, bpar, 3);
302          
303 //       gMC->Gspos("S01F",1,"S01G", +iChamber->RInner()+bpar[0] , 0, 0, 
304 //                  idrotm[1100],"ONLY");
305 //       gMC->Gspos("S01F",2,"S01G", -iChamber->RInner()-bpar[0] , 0, 0, 
306 //                  idrotm[1100],"ONLY");
307 //       gMC->Gspos("S01F",3,"S01G", 0, +iChamber->RInner()+bpar[0] , 0, 
308 //                  idrotm[1101],"ONLY");
309 //       gMC->Gspos("S01F",4,"S01G", 0, -iChamber->RInner()-bpar[0] , 0, 
310 //                  idrotm[1101],"ONLY");
311          
312 //       gMC->Gspos("S02F",1,"S02G", +iChamber->RInner()+bpar[0] , 0, 0, 
313 //                  idrotm[1100],"ONLY");
314 //       gMC->Gspos("S02F",2,"S02G", -iChamber->RInner()-bpar[0] , 0, 0, 
315 //                  idrotm[1100],"ONLY");
316 //       gMC->Gspos("S02F",3,"S02G", 0, +iChamber->RInner()+bpar[0] , 0, 
317 //                  idrotm[1101],"ONLY");
318 //       gMC->Gspos("S02F",4,"S02G", 0, -iChamber->RInner()-bpar[0] , 0, 
319 //                  idrotm[1101],"ONLY");
320 //      }
321      }
322      if (fStations[1]) {
323          
324 //********************************************************************
325 //                            Station 2                             **
326 //********************************************************************
327      // indices 1 and 2 for first and second chambers in the station
328      // iChamber (first chamber) kept for other quanties than Z,
329      // assumed to be the same in both chambers
330      iChamber1 = iChamber = (AliMUONChamber*) (*fChambers)[2];
331      iChamber2 =(AliMUONChamber*) (*fChambers)[3];
332      zpos1=iChamber1->Z(); 
333      zpos2=iChamber2->Z();
334      dstation = TMath::Abs(zpos2 - zpos1);
335      // DGas and DAlu not changed from standard values
336      zfpos=-(iChamber->DGas()+dframez+iChamber->DAlu())/2;
337      
338 //
339 //   Mother volume
340      tpar[0] = iChamber->RInner()-dframep; 
341      tpar[1] = (iChamber->ROuter()+dframep)/TMath::Cos(phi);
342      tpar[2] = dstation/5;
343
344      gMC->Gsvolu("S03M", "TUBE", idAir, tpar, 3);
345      gMC->Gsvolu("S04M", "TUBE", idAir, tpar, 3);
346      gMC->Gspos("S03M", 1, "ALIC", 0., 0., zpos1 , 0, "ONLY");
347      gMC->Gspos("S04M", 1, "ALIC", 0., 0., zpos2 , 0, "ONLY");
348      gMC->Gsbool("S03M", "L3DO");
349      gMC->Gsbool("S03M", "L3O1");
350      gMC->Gsbool("S03M", "L3O2");
351      gMC->Gsbool("S04M", "L3DO");
352      gMC->Gsbool("S04M", "L3O1");
353      gMC->Gsbool("S04M", "L3O2");
354
355 // // Aluminium frames
356 // // Outer frames
357 //      pgpar[0] = 360/12/2;
358 //      pgpar[1] = 360.;
359 //      pgpar[2] = 12.;
360 //      pgpar[3] =   2;
361 //      pgpar[4] = -dframez/2;
362 //      pgpar[5] = iChamber->ROuter();
363 //      pgpar[6] = pgpar[5]+dframep;
364 //      pgpar[7] = +dframez/2;
365 //      pgpar[8] = pgpar[5];
366 //      pgpar[9] = pgpar[6];
367 //      gMC->Gsvolu("S03O", "PGON", idAlu1, pgpar, 10);
368 //      gMC->Gsvolu("S04O", "PGON", idAlu1, pgpar, 10);
369 //      gMC->Gspos("S03O",1,"S03M", 0.,0.,-zfpos,  0,"ONLY");
370 //      gMC->Gspos("S03O",2,"S03M", 0.,0.,+zfpos,  0,"ONLY");
371 //      gMC->Gspos("S04O",1,"S04M", 0.,0.,-zfpos,  0,"ONLY");
372 //      gMC->Gspos("S04O",2,"S04M", 0.,0.,+zfpos,  0,"ONLY");
373 // //
374 // // Inner frame
375 //      tpar[0]= iChamber->RInner()-dframep;
376 //      tpar[1]= iChamber->RInner();
377 //      tpar[2]= dframez/2;
378 //      gMC->Gsvolu("S03I", "TUBE", idAlu1, tpar, 3);
379 //      gMC->Gsvolu("S04I", "TUBE", idAlu1, tpar, 3);
380
381 //      gMC->Gspos("S03I",1,"S03M", 0.,0.,-zfpos,  0,"ONLY");
382 //      gMC->Gspos("S03I",2,"S03M", 0.,0.,+zfpos,  0,"ONLY");
383 //      gMC->Gspos("S04I",1,"S04M", 0.,0.,-zfpos,  0,"ONLY");
384 //      gMC->Gspos("S04I",2,"S04M", 0.,0.,+zfpos,  0,"ONLY");
385 //
386 // Frame Crosses
387      if (frameCrosses) {
388          // outside gas
389          // security for inside mother volume
390          bpar[0] = (iChamber->ROuter() - iChamber->RInner())
391            * TMath::Cos(TMath::ASin(dframep1 /
392                                    (iChamber->ROuter() - iChamber->RInner())))
393            / 2.0;
394          bpar[1] = dframep1/2;
395          // total thickness will be (4 * bpar[2]) for each chamber,
396          // which has to be equal to (2 * dframez) - DAlu
397          bpar[2] = (2.0 * dframez - iChamber->DAlu()) / 4.0;
398          gMC->Gsvolu("S03B", "BOX", idAlu1, bpar, 3);
399          gMC->Gsvolu("S04B", "BOX", idAlu1, bpar, 3);
400          
401          gMC->Gspos("S03B",1,"S03M", -iChamber->RInner()-bpar[0] , 0, zfpos, 
402                     idrotm[1100],"ONLY");
403          gMC->Gspos("S03B",2,"S03M", +iChamber->RInner()+bpar[0] , 0, zfpos, 
404                     idrotm[1100],"ONLY");
405          gMC->Gspos("S03B",3,"S03M", 0, -iChamber->RInner()-bpar[0] , zfpos, 
406                     idrotm[1101],"ONLY");
407          gMC->Gspos("S03B",4,"S03M", 0, +iChamber->RInner()+bpar[0] , zfpos, 
408                     idrotm[1101],"ONLY");
409          gMC->Gspos("S03B",5,"S03M", -iChamber->RInner()-bpar[0] , 0,-zfpos, 
410                     idrotm[1100],"ONLY");
411          gMC->Gspos("S03B",6,"S03M", +iChamber->RInner()+bpar[0] , 0,-zfpos, 
412                     idrotm[1100],"ONLY");
413          gMC->Gspos("S03B",7,"S03M", 0, -iChamber->RInner()-bpar[0] ,-zfpos, 
414                     idrotm[1101],"ONLY");
415          gMC->Gspos("S03B",8,"S03M", 0, +iChamber->RInner()+bpar[0] ,-zfpos, 
416                     idrotm[1101],"ONLY");
417          
418          gMC->Gspos("S04B",1,"S04M", -iChamber->RInner()-bpar[0] , 0, zfpos, 
419                     idrotm[1100],"ONLY");
420          gMC->Gspos("S04B",2,"S04M", +iChamber->RInner()+bpar[0] , 0, zfpos, 
421                     idrotm[1100],"ONLY");
422          gMC->Gspos("S04B",3,"S04M", 0, -iChamber->RInner()-bpar[0] , zfpos, 
423                     idrotm[1101],"ONLY");
424          gMC->Gspos("S04B",4,"S04M", 0, +iChamber->RInner()+bpar[0] , zfpos, 
425                     idrotm[1101],"ONLY");
426          gMC->Gspos("S04B",5,"S04M", -iChamber->RInner()-bpar[0] , 0,-zfpos, 
427                     idrotm[1100],"ONLY");
428          gMC->Gspos("S04B",6,"S04M", +iChamber->RInner()+bpar[0] , 0,-zfpos, 
429                     idrotm[1100],"ONLY");
430          gMC->Gspos("S04B",7,"S04M", 0, -iChamber->RInner()-bpar[0] ,-zfpos, 
431                     idrotm[1101],"ONLY");
432          gMC->Gspos("S04B",8,"S04M", 0, +iChamber->RInner()+bpar[0] ,-zfpos, 
433                     idrotm[1101],"ONLY");
434      }
435 //
436 //   Chamber Material represented by Alu sheet
437      tpar[0]= iChamber->RInner();
438      tpar[1]= iChamber->ROuter();
439      tpar[2] = (iChamber->DGas()+iChamber->DAlu())/2;
440      gMC->Gsvolu("S03A", "TUBE", idAlu2, tpar, 3);
441      gMC->Gsvolu("S04A", "TUBE", idAlu2, tpar, 3);
442      gMC->Gspos("S03A", 1, "S03M", 0., 0., 0.,  0, "ONLY");
443      gMC->Gspos("S04A", 1, "S04M", 0., 0., 0.,  0, "ONLY");
444 //     
445 //   Sensitive volumes
446      // tpar[2] = iChamber->DGas();
447      tpar[2] = iChamber->DGas()/2;
448      gMC->Gsvolu("S03G", "TUBE", idGas, tpar, 3);
449      gMC->Gsvolu("S04G", "TUBE", idGas, tpar, 3);
450      gMC->Gspos("S03G", 1, "S03A", 0., 0., 0.,  0, "ONLY");
451      gMC->Gspos("S04G", 1, "S04A", 0., 0., 0.,  0, "ONLY");
452 //
453 // Frame Crosses to be placed inside gas 
454      // NONE: chambers are sensitive everywhere
455 //      if (frameCrosses) {
456
457 //       dr = (iChamber->ROuter() - iChamber->RInner());
458 //       bpar[0] = TMath::Sqrt(dr*dr-dframep1*dframep1/4)/2;
459 //       bpar[1] = dframep1/2;
460 //       bpar[2] = iChamber->DGas()/2;
461 //       gMC->Gsvolu("S03F", "BOX", idAlu1, bpar, 3);
462 //       gMC->Gsvolu("S04F", "BOX", idAlu1, bpar, 3);
463          
464 //       gMC->Gspos("S03F",1,"S03G", +iChamber->RInner()+bpar[0] , 0, 0, 
465 //                  idrotm[1100],"ONLY");
466 //       gMC->Gspos("S03F",2,"S03G", -iChamber->RInner()-bpar[0] , 0, 0, 
467 //                  idrotm[1100],"ONLY");
468 //       gMC->Gspos("S03F",3,"S03G", 0, +iChamber->RInner()+bpar[0] , 0, 
469 //                  idrotm[1101],"ONLY");
470 //       gMC->Gspos("S03F",4,"S03G", 0, -iChamber->RInner()-bpar[0] , 0, 
471 //                  idrotm[1101],"ONLY");
472          
473 //       gMC->Gspos("S04F",1,"S04G", +iChamber->RInner()+bpar[0] , 0, 0, 
474 //                  idrotm[1100],"ONLY");
475 //       gMC->Gspos("S04F",2,"S04G", -iChamber->RInner()-bpar[0] , 0, 0, 
476 //                  idrotm[1100],"ONLY");
477 //       gMC->Gspos("S04F",3,"S04G", 0, +iChamber->RInner()+bpar[0] , 0, 
478 //                  idrotm[1101],"ONLY");
479 //       gMC->Gspos("S04F",4,"S04G", 0, -iChamber->RInner()-bpar[0] , 0, 
480 //                  idrotm[1101],"ONLY");
481 //      }
482      }
483      // define the id of tracking media:
484      Int_t idCopper = idtmed[1110];
485      Int_t idGlass  = idtmed[1111];
486      Int_t idCarbon = idtmed[1112];
487      Int_t idRoha   = idtmed[1113];
488
489       // sensitive area: 40*40 cm**2
490      const Float_t ksensLength = 40.; 
491      const Float_t ksensHeight = 40.; 
492      const Float_t ksensWidth  = 0.5; // according to TDR fig 2.120 
493      const Int_t ksensMaterial = idGas;
494      const Float_t kyOverlap   = 1.5; 
495
496      // PCB dimensions in cm; width: 30 mum copper   
497      const Float_t kpcbLength  = ksensLength; 
498      const Float_t kpcbHeight  = 60.; 
499      const Float_t kpcbWidth   = 0.003;   
500      const Int_t   kpcbMaterial= idCopper;
501
502      // Insulating material: 200 mum glass fiber glued to pcb  
503      const Float_t kinsuLength = kpcbLength; 
504      const Float_t kinsuHeight = kpcbHeight; 
505      const Float_t kinsuWidth  = 0.020;   
506      const Int_t kinsuMaterial = idGlass;
507
508      // Carbon fiber panels: 200mum carbon/epoxy skin   
509      const Float_t kpanelLength = ksensLength; 
510      const Float_t kpanelHeight = ksensHeight; 
511      const Float_t kpanelWidth  = 0.020;      
512      const Int_t kpanelMaterial = idCarbon;
513
514      // rohacell between the two carbon panels   
515      const Float_t krohaLength = ksensLength; 
516      const Float_t krohaHeight = ksensHeight; 
517      const Float_t krohaWidth  = 0.5;
518      const Int_t krohaMaterial = idRoha;
519
520      // Frame around the slat: 2 sticks along length,2 along height  
521      // H: the horizontal ones 
522      const Float_t khFrameLength = kpcbLength; 
523      const Float_t khFrameHeight = 1.5; 
524      const Float_t khFrameWidth  = ksensWidth; 
525      const Int_t khFrameMaterial = idGlass;
526
527      // V: the vertical ones 
528      const Float_t kvFrameLength = 4.0; 
529      const Float_t kvFrameHeight = ksensHeight + khFrameHeight; 
530      const Float_t kvFrameWidth  = ksensWidth;
531      const Int_t kvFrameMaterial = idGlass;
532
533      // B: the horizontal border filled with rohacell 
534      const Float_t kbFrameLength = khFrameLength; 
535      const Float_t kbFrameHeight = (kpcbHeight - ksensHeight)/2. - khFrameHeight; 
536      const Float_t kbFrameWidth  = khFrameWidth;
537      const Int_t kbFrameMaterial = idRoha;
538
539      // NULOC: 30 mum copper + 200 mum vetronite (same radiation length as 14mum copper)
540      const Float_t knulocLength = 2.5; 
541      const Float_t knulocHeight = 7.5; 
542      const Float_t knulocWidth  = 0.0030 + 0.0014; // equivalent copper width of vetronite; 
543      const Int_t   knulocMaterial = idCopper;
544
545      const Float_t kslatHeight = kpcbHeight; 
546      const Float_t kslatWidth = ksensWidth + 2.*(kpcbWidth + kinsuWidth + 
547                                                2.* kpanelWidth + krohaWidth);
548      const Int_t kslatMaterial = idAir;
549      const Float_t kdSlatLength = kvFrameLength; // border on left and right 
550
551      Float_t spar[3];  
552      Int_t i, j;
553
554      // the panel volume contains the rohacell
555
556      Float_t twidth = 2 * kpanelWidth + krohaWidth; 
557      Float_t panelpar[3] = { kpanelLength/2., kpanelHeight/2., twidth/2. }; 
558      Float_t rohapar[3] = { krohaLength/2., krohaHeight/2., krohaWidth/2. }; 
559
560      // insulating material contains PCB-> gas-> 2 borders filled with rohacell
561
562      twidth = 2*(kinsuWidth + kpcbWidth) + ksensWidth;  
563      Float_t insupar[3] = { kinsuLength/2., kinsuHeight/2., twidth/2. }; 
564      twidth -= 2 * kinsuWidth; 
565      Float_t pcbpar[3] = { kpcbLength/2., kpcbHeight/2., twidth/2. }; 
566      Float_t senspar[3] = { ksensLength/2., ksensHeight/2., ksensWidth/2. }; 
567      Float_t theight = 2*khFrameHeight + ksensHeight;
568      Float_t hFramepar[3]={khFrameLength/2., theight/2., khFrameWidth/2.}; 
569      Float_t bFramepar[3]={kbFrameLength/2., kbFrameHeight/2., kbFrameWidth/2.}; 
570      Float_t vFramepar[3]={kvFrameLength/2., kvFrameHeight/2., kvFrameWidth/2.}; 
571      Float_t nulocpar[3]={knulocLength/2., knulocHeight/2., knulocWidth/2.}; 
572      Float_t xx;
573      Float_t xxmax = (kbFrameLength - knulocLength)/2.; 
574      Int_t index=0;
575      
576      if (fStations[2]) {
577          
578 //********************************************************************
579 //                            Station 3                             **
580 //********************************************************************
581      // indices 1 and 2 for first and second chambers in the station
582      // iChamber (first chamber) kept for other quanties than Z,
583      // assumed to be the same in both chambers
584      iChamber1 = iChamber = (AliMUONChamber*) (*fChambers)[4];
585      iChamber2 =(AliMUONChamber*) (*fChambers)[5];
586      zpos1=iChamber1->Z(); 
587      zpos2=iChamber2->Z();
588      dstation = TMath::Abs(zpos2 - zpos1);
589
590 //
591 //   Mother volume
592      tpar[0] = iChamber->RInner()-dframep; 
593      tpar[1] = (iChamber->ROuter()+dframep)/TMath::Cos(phi);
594      tpar[2] = dstation/5;
595
596      char *slats5Mother = "S05M";
597      char *slats6Mother = "S06M";
598      Float_t zoffs5 = 0;
599      Float_t zoffs6 = 0;
600
601      if (gAlice->GetModule("DIPO")) {
602        slats5Mother="DDIP";
603        slats6Mother="DDIP";
604
605        zoffs5 = TMath::Abs(zpos1);
606        zoffs6 = TMath::Abs(zpos2);
607      }
608
609      else {
610        gMC->Gsvolu("S05M", "TUBE", idAir, tpar, 3);
611        gMC->Gsvolu("S06M", "TUBE", idAir, tpar, 3);
612        gMC->Gspos("S05M", 1, "ALIC", 0., 0., zpos1 , 0, "ONLY");
613        gMC->Gspos("S06M", 1, "ALIC", 0., 0., zpos2 , 0, "ONLY");
614      }
615
616      // volumes for slat geometry (xx=5,..,10 chamber id): 
617      // Sxx0 Sxx1 Sxx2 Sxx3  -->   Slat Mother volumes 
618      // SxxG                          -->   Sensitive volume (gas)
619      // SxxP                          -->   PCB (copper) 
620      // SxxI                          -->   Insulator (vetronite) 
621      // SxxC                          -->   Carbon panel 
622      // SxxR                          -->   Rohacell
623      // SxxH, SxxV                    -->   Horizontal and Vertical frames (vetronite)
624      // SB5x                          -->   Volumes for the 35 cm long PCB
625      // slat dimensions: slat is a MOTHER volume!!! made of air
626
627      // only for chamber 5: slat 1 has a PCB shorter by 5cm!
628
629      Float_t tlength = 35.;
630      Float_t panelpar2[3]  = { tlength/2., panelpar[1],  panelpar[2]}; 
631      Float_t rohapar2[3]   = { tlength/2., rohapar[1],   rohapar[2]}; 
632      Float_t insupar2[3]   = { tlength/2., insupar[1],   insupar[2]}; 
633      Float_t pcbpar2[3]    = { tlength/2., pcbpar[1],    pcbpar[2]}; 
634      Float_t senspar2[3]   = { tlength/2., senspar[1],   senspar[2]}; 
635      Float_t hFramepar2[3] = { tlength/2., hFramepar[1], hFramepar[2]}; 
636      Float_t bFramepar2[3] = { tlength/2., bFramepar[1], bFramepar[2]}; 
637
638      const Int_t knSlats3 = 5;  // number of slats per quadrant
639      const Int_t knPCB3[knSlats3] = {3,3,4,3,2}; // n PCB per slat
640      const Float_t kxpos3[knSlats3] = {31., 40., 0., 0., 0.};
641      Float_t slatLength3[knSlats3]; 
642
643      // create and position the slat (mother) volumes 
644
645      char volNam5[5];
646      char volNam6[5];
647      Float_t xSlat3;
648
649      Float_t spar2[3];
650      for (i = 0; i<knSlats3; i++){
651        slatLength3[i] = kpcbLength * knPCB3[i] + 2. * kdSlatLength; 
652        xSlat3 = slatLength3[i]/2. - kvFrameLength/2. + kxpos3[i]; 
653        if (i==1 || i==0) slatLength3[i] -=  2. *kdSlatLength; // frame out in PCB with circular border 
654        Float_t ySlat31 =  ksensHeight * i - kyOverlap * i; 
655        Float_t ySlat32 = -ksensHeight * i + kyOverlap * i; 
656        spar[0] = slatLength3[i]/2.; 
657        spar[1] = kslatHeight/2.;
658        spar[2] = kslatWidth/2. * 1.01; 
659        // take away 5 cm from the first slat in chamber 5
660        Float_t xSlat32 = 0;
661        if (i==1 || i==2) { // 1 pcb is shortened by 5cm
662          spar2[0] = spar[0]-5./2.;
663          xSlat32 = xSlat3 - 5/2.;
664        }
665        else {
666          spar2[0] = spar[0];
667          xSlat32 = xSlat3;
668        }
669        spar2[1] = spar[1];
670        spar2[2] = spar[2]; 
671        Float_t dzCh3=spar[2] * 1.01;
672        // zSlat to be checked (odd downstream or upstream?)
673        Float_t zSlat = (i%2 ==0)? -spar[2] : spar[2];
674
675         if (gAlice->GetModule("DIPO")) {zSlat*=-1.;}
676
677        sprintf(volNam5,"S05%d",i);
678        gMC->Gsvolu(volNam5,"BOX",kslatMaterial,spar2,3);
679        gMC->Gspos(volNam5, i*4+1,slats5Mother, -xSlat32, ySlat31, zoffs5-zSlat-2.*dzCh3, 0, "ONLY");
680        gMC->Gspos(volNam5, i*4+2,slats5Mother, +xSlat32, ySlat31, zoffs5-zSlat+2.*dzCh3, 0, "ONLY");
681        
682         if (i>0) { 
683          gMC->Gspos(volNam5, i*4+3,slats5Mother,-xSlat32, ySlat32, zoffs5-zSlat-2.*dzCh3, 0, "ONLY");
684          gMC->Gspos(volNam5, i*4+4,slats5Mother,+xSlat32, ySlat32, zoffs5-zSlat+2.*dzCh3, 0, "ONLY");
685        }
686        sprintf(volNam6,"S06%d",i);
687        gMC->Gsvolu(volNam6,"BOX",kslatMaterial,spar,3);
688        gMC->Gspos(volNam6, i*4+1,slats6Mother,-xSlat3, ySlat31, zoffs6-zSlat-2.*dzCh3, 0, "ONLY");
689        gMC->Gspos(volNam6, i*4+2,slats6Mother,+xSlat3, ySlat31, zoffs6-zSlat+2.*dzCh3, 0, "ONLY");
690        if (i>0) { 
691          gMC->Gspos(volNam6, i*4+3,slats6Mother,-xSlat3, ySlat32, zoffs6-zSlat-2.*dzCh3, 0, "ONLY");
692          gMC->Gspos(volNam6, i*4+4,slats6Mother,+xSlat3, ySlat32, zoffs6-zSlat+2.*dzCh3, 0, "ONLY");
693        }
694      }
695
696      // create the panel volume 
697  
698      gMC->Gsvolu("S05C","BOX",kpanelMaterial,panelpar,3);
699      gMC->Gsvolu("SB5C","BOX",kpanelMaterial,panelpar2,3);
700      gMC->Gsvolu("S06C","BOX",kpanelMaterial,panelpar,3);
701
702      // create the rohacell volume 
703
704      gMC->Gsvolu("S05R","BOX",krohaMaterial,rohapar,3);
705      gMC->Gsvolu("SB5R","BOX",krohaMaterial,rohapar2,3);
706      gMC->Gsvolu("S06R","BOX",krohaMaterial,rohapar,3);
707
708      // create the insulating material volume 
709
710      gMC->Gsvolu("S05I","BOX",kinsuMaterial,insupar,3);
711      gMC->Gsvolu("SB5I","BOX",kinsuMaterial,insupar2,3);
712      gMC->Gsvolu("S06I","BOX",kinsuMaterial,insupar,3);
713
714      // create the PCB volume 
715
716      gMC->Gsvolu("S05P","BOX",kpcbMaterial,pcbpar,3);
717      gMC->Gsvolu("SB5P","BOX",kpcbMaterial,pcbpar2,3);
718      gMC->Gsvolu("S06P","BOX",kpcbMaterial,pcbpar,3);
719  
720      // create the sensitive volumes,
721      gMC->Gsvolu("S05G","BOX",ksensMaterial,dum,0);
722      gMC->Gsvolu("S06G","BOX",ksensMaterial,dum,0);
723
724
725      // create the vertical frame volume 
726
727      gMC->Gsvolu("S05V","BOX",kvFrameMaterial,vFramepar,3);
728      gMC->Gsvolu("S06V","BOX",kvFrameMaterial,vFramepar,3);
729
730      // create the horizontal frame volume 
731
732      gMC->Gsvolu("S05H","BOX",khFrameMaterial,hFramepar,3);
733      gMC->Gsvolu("SB5H","BOX",khFrameMaterial,hFramepar2,3);
734      gMC->Gsvolu("S06H","BOX",khFrameMaterial,hFramepar,3);
735
736      // create the horizontal border volume 
737
738      gMC->Gsvolu("S05B","BOX",kbFrameMaterial,bFramepar,3);
739      gMC->Gsvolu("SB5B","BOX",kbFrameMaterial,bFramepar2,3);
740      gMC->Gsvolu("S06B","BOX",kbFrameMaterial,bFramepar,3);
741
742      index=0; 
743      for (i = 0; i<knSlats3; i++){
744        sprintf(volNam5,"S05%d",i);
745        sprintf(volNam6,"S06%d",i);
746        Float_t xvFrame  = (slatLength3[i] - kvFrameLength)/2.;
747        Float_t xvFrame2  = xvFrame;
748        if ( i==1 || i ==2 ) xvFrame2 -= 5./2.;
749        // position the vertical frames 
750        if (i!=1 && i!=0) { 
751          gMC->Gspos("S05V",2*i-1,volNam5, xvFrame2, 0., 0. , 0, "ONLY");
752          gMC->Gspos("S05V",2*i  ,volNam5,-xvFrame2, 0., 0. , 0, "ONLY");
753          gMC->Gspos("S06V",2*i-1,volNam6, xvFrame, 0., 0. , 0, "ONLY");
754          gMC->Gspos("S06V",2*i  ,volNam6,-xvFrame, 0., 0. , 0, "ONLY");
755        }       
756        // position the panels and the insulating material 
757        for (j=0; j<knPCB3[i]; j++){
758          index++;
759          Float_t xx = ksensLength * (-knPCB3[i]/2.+j+.5); 
760          Float_t xx2 = xx + 5/2.; 
761          
762          Float_t zPanel = spar[2] - panelpar[2]; 
763          if ( (i==1 || i==2) && j == knPCB3[i]-1) { // 1 pcb is shortened by 5cm 
764            gMC->Gspos("SB5C",2*index-1,volNam5, xx, 0., zPanel , 0, "ONLY");
765            gMC->Gspos("SB5C",2*index  ,volNam5, xx, 0.,-zPanel , 0, "ONLY");
766            gMC->Gspos("SB5I",index    ,volNam5, xx, 0., 0      , 0, "ONLY");
767          }
768          else if ( (i==1 || i==2) && j < knPCB3[i]-1) {
769            gMC->Gspos("S05C",2*index-1,volNam5, xx2, 0., zPanel , 0, "ONLY");
770            gMC->Gspos("S05C",2*index  ,volNam5, xx2, 0.,-zPanel , 0, "ONLY");
771            gMC->Gspos("S05I",index    ,volNam5, xx2, 0., 0 , 0, "ONLY");
772          }
773          else {
774            gMC->Gspos("S05C",2*index-1,volNam5, xx, 0., zPanel , 0, "ONLY");
775            gMC->Gspos("S05C",2*index  ,volNam5, xx, 0.,-zPanel , 0, "ONLY");
776            gMC->Gspos("S05I",index    ,volNam5, xx, 0., 0 , 0, "ONLY");
777          }
778          gMC->Gspos("S06C",2*index-1,volNam6, xx, 0., zPanel , 0, "ONLY");
779          gMC->Gspos("S06C",2*index  ,volNam6, xx, 0.,-zPanel , 0, "ONLY");
780          gMC->Gspos("S06I",index,volNam6, xx, 0., 0 , 0, "ONLY");
781        } 
782      }
783      
784      // position the rohacell volume inside the panel volume
785      gMC->Gspos("S05R",1,"S05C",0.,0.,0.,0,"ONLY"); 
786      gMC->Gspos("SB5R",1,"SB5C",0.,0.,0.,0,"ONLY"); 
787      gMC->Gspos("S06R",1,"S06C",0.,0.,0.,0,"ONLY"); 
788
789      // position the PCB volume inside the insulating material volume
790      gMC->Gspos("S05P",1,"S05I",0.,0.,0.,0,"ONLY"); 
791      gMC->Gspos("SB5P",1,"SB5I",0.,0.,0.,0,"ONLY"); 
792      gMC->Gspos("S06P",1,"S06I",0.,0.,0.,0,"ONLY"); 
793      // position the horizontal frame volume inside the PCB volume
794      gMC->Gspos("S05H",1,"S05P",0.,0.,0.,0,"ONLY"); 
795      gMC->Gspos("SB5H",1,"SB5P",0.,0.,0.,0,"ONLY"); 
796      gMC->Gspos("S06H",1,"S06P",0.,0.,0.,0,"ONLY"); 
797      // position the sensitive volume inside the horizontal frame volume
798      gMC->Gsposp("S05G",1,"S05H",0.,0.,0.,0,"ONLY",senspar,3); 
799      gMC->Gsposp("S05G",1,"SB5H",0.,0.,0.,0,"ONLY",senspar2,3); 
800      gMC->Gsposp("S06G",1,"S06H",0.,0.,0.,0,"ONLY",senspar,3); 
801      // position the border volumes inside the PCB volume
802      Float_t yborder = ( kpcbHeight - kbFrameHeight ) / 2.; 
803      gMC->Gspos("S05B",1,"S05P",0., yborder,0.,0,"ONLY"); 
804      gMC->Gspos("S05B",2,"S05P",0.,-yborder,0.,0,"ONLY"); 
805      gMC->Gspos("SB5B",1,"SB5P",0., yborder,0.,0,"ONLY"); 
806      gMC->Gspos("SB5B",2,"SB5P",0.,-yborder,0.,0,"ONLY"); 
807      gMC->Gspos("S06B",1,"S06P",0., yborder,0.,0,"ONLY"); 
808      gMC->Gspos("S06B",2,"S06P",0.,-yborder,0.,0,"ONLY"); 
809
810      // create the NULOC volume and position it in the horizontal frame
811
812      gMC->Gsvolu("S05N","BOX",knulocMaterial,nulocpar,3);
813      gMC->Gsvolu("S06N","BOX",knulocMaterial,nulocpar,3);
814      index = 0;
815      Float_t xxmax2 = xxmax - 5./2.;
816      for (xx = -xxmax; xx<=xxmax; xx+=2*knulocLength) { 
817        index++; 
818        gMC->Gspos("S05N",2*index-1,"S05B", xx, 0.,-kbFrameWidth/4., 0, "ONLY");
819        gMC->Gspos("S05N",2*index  ,"S05B", xx, 0., kbFrameWidth/4., 0, "ONLY");
820        if (xx > -xxmax2 && xx< xxmax2) {
821          gMC->Gspos("S05N",2*index-1,"SB5B", xx, 0.,-kbFrameWidth/4., 0, "ONLY");
822          gMC->Gspos("S05N",2*index  ,"SB5B", xx, 0., kbFrameWidth/4., 0, "ONLY");
823        }
824        gMC->Gspos("S06N",2*index-1,"S06B", xx, 0.,-kbFrameWidth/4., 0, "ONLY");
825        gMC->Gspos("S06N",2*index  ,"S06B", xx, 0., kbFrameWidth/4., 0, "ONLY");
826      }
827      
828      // position the volumes approximating the circular section of the pipe
829      Float_t yoffs = ksensHeight/2. - kyOverlap; 
830      Float_t epsilon = 0.001; 
831      Int_t ndiv=6;
832      Float_t divpar[3];
833      Double_t dydiv= ksensHeight/ndiv;
834      Double_t ydiv = yoffs -dydiv;
835      Int_t imax=0; 
836      imax = 1; 
837      Float_t rmin = 33.; 
838      Float_t z1 = spar[2], z2=2*spar[2]*1.01; 
839      if (gAlice->GetModule("DIPO")) {z1*=-1.;}
840      for (Int_t idiv=0;idiv<ndiv; idiv++){ 
841        ydiv+= dydiv;
842        Float_t xdiv = 0.; 
843        if (ydiv<rmin) xdiv= rmin * TMath::Sin( TMath::ACos(ydiv/rmin) );
844        divpar[0] = (kpcbLength-xdiv)/2.; 
845        divpar[1] = dydiv/2. - epsilon;
846        divpar[2] = ksensWidth/2.; 
847        Float_t xvol=(kpcbLength+xdiv)/2.+1.999;
848        Float_t yvol=ydiv + dydiv/2.; 
849        //printf ("y ll = %f y ur = %f \n",yvol - divpar[1], yvol + divpar[1]); 
850        gMC->Gsposp("S05G",imax+4*idiv+1,slats5Mother,-xvol, yvol, zoffs5-z1-z2, 0, "ONLY",divpar,3);
851        gMC->Gsposp("S06G",imax+4*idiv+1,slats6Mother,-xvol, yvol, zoffs6-z1-z2, 0, "ONLY",divpar,3);
852        gMC->Gsposp("S05G",imax+4*idiv+2,slats5Mother,-xvol,-yvol, zoffs5-z1-z2, 0, "ONLY",divpar,3);
853        gMC->Gsposp("S06G",imax+4*idiv+2,slats6Mother,-xvol,-yvol, zoffs6-z1-z2, 0, "ONLY",divpar,3);
854        gMC->Gsposp("S05G",imax+4*idiv+3,slats5Mother,+xvol, yvol, zoffs5-z1+z2, 0, "ONLY",divpar,3);
855        gMC->Gsposp("S06G",imax+4*idiv+3,slats6Mother,+xvol, yvol, zoffs6-z1+z2, 0, "ONLY",divpar,3);
856        gMC->Gsposp("S05G",imax+4*idiv+4,slats5Mother,+xvol,-yvol, zoffs5-z1+z2, 0, "ONLY",divpar,3);
857        gMC->Gsposp("S06G",imax+4*idiv+4,slats6Mother,+xvol,-yvol, zoffs6-z1+z2, 0, "ONLY",divpar,3);
858      }
859      }
860      
861  if (fStations[3]) {
862
863 //********************************************************************
864 //                            Station 4                             **
865 //********************************************************************
866      // indices 1 and 2 for first and second chambers in the station
867      // iChamber (first chamber) kept for other quanties than Z,
868      // assumed to be the same in both chambers
869      iChamber1 = iChamber = (AliMUONChamber*) (*fChambers)[6];
870      iChamber2 =(AliMUONChamber*) (*fChambers)[7];
871      zpos1=iChamber1->Z(); 
872      zpos2=iChamber2->Z();
873      dstation = TMath::Abs(zpos2 - zpos1);
874 //      zfpos=-(iChamber->DGas()+dframez+iChamber->DAlu())/2; // not used any more
875      
876 //
877 //   Mother volume
878      tpar[0] = iChamber->RInner()-dframep; 
879      tpar[1] = (iChamber->ROuter()+dframep)/TMath::Cos(phi);
880      tpar[2] = dstation/4;
881
882      gMC->Gsvolu("S07M", "TUBE", idAir, tpar, 3);
883      gMC->Gsvolu("S08M", "TUBE", idAir, tpar, 3);
884      gMC->Gspos("S07M", 1, "ALIC", 0., 0., zpos1 , 0, "ONLY");
885      gMC->Gspos("S08M", 1, "ALIC", 0., 0., zpos2 , 0, "ONLY");
886      
887
888      const Int_t knSlats4 = 6;  // number of slats per quadrant
889      const Int_t knPCB4[knSlats4] = {4,4,5,5,4,3}; // n PCB per slat
890      const Float_t kxpos4[knSlats4] = {38.5, 40., 0., 0., 0., 0.};
891      Float_t slatLength4[knSlats4];     
892
893      // create and position the slat (mother) volumes 
894
895      char volNam7[5];
896      char volNam8[5];
897      Float_t xSlat4;
898      Float_t ySlat4;
899
900      for (i = 0; i<knSlats4; i++){
901        slatLength4[i] = kpcbLength * knPCB4[i] + 2. * kdSlatLength; 
902        xSlat4 = slatLength4[i]/2. - kvFrameLength/2. + kxpos4[i]; 
903        if (i==1) slatLength4[i] -=  2. *kdSlatLength; // frame out in PCB with circular border 
904        ySlat4 =  ksensHeight * i - kyOverlap *i;
905        
906        spar[0] = slatLength4[i]/2.; 
907        spar[1] = kslatHeight/2.;
908        spar[2] = kslatWidth/2.*1.01; 
909        Float_t dzCh4=spar[2]*1.01;
910        // zSlat to be checked (odd downstream or upstream?)
911        Float_t zSlat = (i%2 ==0)? spar[2] : -spar[2]; 
912        sprintf(volNam7,"S07%d",i);
913        gMC->Gsvolu(volNam7,"BOX",kslatMaterial,spar,3);
914        gMC->Gspos(volNam7, i*4+1,"S07M",-xSlat4, ySlat4, -zSlat-2.*dzCh4, 0, "ONLY");
915        gMC->Gspos(volNam7, i*4+2,"S07M",+xSlat4, ySlat4, -zSlat+2.*dzCh4, 0, "ONLY");
916        if (i>0) { 
917          gMC->Gspos(volNam7, i*4+3,"S07M",-xSlat4,-ySlat4, -zSlat-2.*dzCh4, 0, "ONLY");
918          gMC->Gspos(volNam7, i*4+4,"S07M",+xSlat4,-ySlat4, -zSlat+2.*dzCh4, 0, "ONLY");
919        }
920        sprintf(volNam8,"S08%d",i);
921        gMC->Gsvolu(volNam8,"BOX",kslatMaterial,spar,3);
922        gMC->Gspos(volNam8, i*4+1,"S08M",-xSlat4, ySlat4, -zSlat-2.*dzCh4, 0, "ONLY");
923        gMC->Gspos(volNam8, i*4+2,"S08M",+xSlat4, ySlat4, -zSlat+2.*dzCh4, 0, "ONLY");
924        if (i>0) { 
925          gMC->Gspos(volNam8, i*4+3,"S08M",-xSlat4,-ySlat4, -zSlat-2.*dzCh4, 0, "ONLY");
926          gMC->Gspos(volNam8, i*4+4,"S08M",+xSlat4,-ySlat4, -zSlat+2.*dzCh4, 0, "ONLY");
927        }
928      }
929      
930
931      // create the panel volume 
932  
933      gMC->Gsvolu("S07C","BOX",kpanelMaterial,panelpar,3);
934      gMC->Gsvolu("S08C","BOX",kpanelMaterial,panelpar,3);
935
936      // create the rohacell volume 
937
938      gMC->Gsvolu("S07R","BOX",krohaMaterial,rohapar,3);
939      gMC->Gsvolu("S08R","BOX",krohaMaterial,rohapar,3);
940
941      // create the insulating material volume 
942
943      gMC->Gsvolu("S07I","BOX",kinsuMaterial,insupar,3);
944      gMC->Gsvolu("S08I","BOX",kinsuMaterial,insupar,3);
945
946      // create the PCB volume 
947
948      gMC->Gsvolu("S07P","BOX",kpcbMaterial,pcbpar,3);
949      gMC->Gsvolu("S08P","BOX",kpcbMaterial,pcbpar,3);
950  
951      // create the sensitive volumes,
952
953      gMC->Gsvolu("S07G","BOX",ksensMaterial,dum,0);
954      gMC->Gsvolu("S08G","BOX",ksensMaterial,dum,0);
955
956      // create the vertical frame volume 
957
958      gMC->Gsvolu("S07V","BOX",kvFrameMaterial,vFramepar,3);
959      gMC->Gsvolu("S08V","BOX",kvFrameMaterial,vFramepar,3);
960
961      // create the horizontal frame volume 
962
963      gMC->Gsvolu("S07H","BOX",khFrameMaterial,hFramepar,3);
964      gMC->Gsvolu("S08H","BOX",khFrameMaterial,hFramepar,3);
965
966      // create the horizontal border volume 
967
968      gMC->Gsvolu("S07B","BOX",kbFrameMaterial,bFramepar,3);
969      gMC->Gsvolu("S08B","BOX",kbFrameMaterial,bFramepar,3);
970
971      index=0; 
972      for (i = 0; i<knSlats4; i++){
973        sprintf(volNam7,"S07%d",i);
974        sprintf(volNam8,"S08%d",i);
975        Float_t xvFrame  = (slatLength4[i] - kvFrameLength)/2.;
976        // position the vertical frames 
977        if (i!=1 && i!=0) { 
978          gMC->Gspos("S07V",2*i-1,volNam7, xvFrame, 0., 0. , 0, "ONLY");
979          gMC->Gspos("S07V",2*i  ,volNam7,-xvFrame, 0., 0. , 0, "ONLY");
980          gMC->Gspos("S08V",2*i-1,volNam8, xvFrame, 0., 0. , 0, "ONLY");
981          gMC->Gspos("S08V",2*i  ,volNam8,-xvFrame, 0., 0. , 0, "ONLY");
982        }
983        // position the panels and the insulating material 
984        for (j=0; j<knPCB4[i]; j++){
985          index++;
986          Float_t xx = ksensLength * (-knPCB4[i]/2.+j+.5); 
987
988          Float_t zPanel = spar[2] - panelpar[2]; 
989          gMC->Gspos("S07C",2*index-1,volNam7, xx, 0., zPanel , 0, "ONLY");
990          gMC->Gspos("S07C",2*index  ,volNam7, xx, 0.,-zPanel , 0, "ONLY");
991          gMC->Gspos("S08C",2*index-1,volNam8, xx, 0., zPanel , 0, "ONLY");
992          gMC->Gspos("S08C",2*index  ,volNam8, xx, 0.,-zPanel , 0, "ONLY");
993
994          gMC->Gspos("S07I",index,volNam7, xx, 0., 0 , 0, "ONLY");
995          gMC->Gspos("S08I",index,volNam8, xx, 0., 0 , 0, "ONLY");
996        } 
997      }
998
999      // position the rohacell volume inside the panel volume
1000      gMC->Gspos("S07R",1,"S07C",0.,0.,0.,0,"ONLY"); 
1001      gMC->Gspos("S08R",1,"S08C",0.,0.,0.,0,"ONLY"); 
1002
1003      // position the PCB volume inside the insulating material volume
1004      gMC->Gspos("S07P",1,"S07I",0.,0.,0.,0,"ONLY"); 
1005      gMC->Gspos("S08P",1,"S08I",0.,0.,0.,0,"ONLY"); 
1006      // position the horizontal frame volume inside the PCB volume
1007      gMC->Gspos("S07H",1,"S07P",0.,0.,0.,0,"ONLY"); 
1008      gMC->Gspos("S08H",1,"S08P",0.,0.,0.,0,"ONLY"); 
1009      // position the sensitive volume inside the horizontal frame volume
1010      gMC->Gsposp("S07G",1,"S07H",0.,0.,0.,0,"ONLY",senspar,3); 
1011      gMC->Gsposp("S08G",1,"S08H",0.,0.,0.,0,"ONLY",senspar,3); 
1012      // position the border volumes inside the PCB volume
1013      Float_t yborder = ( kpcbHeight - kbFrameHeight ) / 2.; 
1014      gMC->Gspos("S07B",1,"S07P",0., yborder,0.,0,"ONLY"); 
1015      gMC->Gspos("S07B",2,"S07P",0.,-yborder,0.,0,"ONLY"); 
1016      gMC->Gspos("S08B",1,"S08P",0., yborder,0.,0,"ONLY"); 
1017      gMC->Gspos("S08B",2,"S08P",0.,-yborder,0.,0,"ONLY"); 
1018
1019      // create the NULOC volume and position it in the horizontal frame
1020
1021      gMC->Gsvolu("S07N","BOX",knulocMaterial,nulocpar,3);
1022      gMC->Gsvolu("S08N","BOX",knulocMaterial,nulocpar,3);
1023      index = 0;
1024      for (xx = -xxmax; xx<=xxmax; xx+=2*knulocLength) { 
1025        index++; 
1026        gMC->Gspos("S07N",2*index-1,"S07B", xx, 0.,-kbFrameWidth/4., 0, "ONLY");
1027        gMC->Gspos("S07N",2*index  ,"S07B", xx, 0., kbFrameWidth/4., 0, "ONLY");
1028        gMC->Gspos("S08N",2*index-1,"S08B", xx, 0.,-kbFrameWidth/4., 0, "ONLY");
1029        gMC->Gspos("S08N",2*index  ,"S08B", xx, 0., kbFrameWidth/4., 0, "ONLY");
1030      }
1031
1032      // position the volumes approximating the circular section of the pipe
1033      Float_t yoffs = ksensHeight/2. - kyOverlap; 
1034      Float_t epsilon = 0.001; 
1035      Int_t ndiv=6;
1036      Float_t divpar[3];
1037      Double_t dydiv= ksensHeight/ndiv;
1038      Double_t ydiv = yoffs -dydiv;
1039      Int_t imax=0; 
1040      imax = 1; 
1041      Float_t rmin = 40.; 
1042      Float_t z1 = -spar[2], z2=2*spar[2]*1.01; 
1043      for (Int_t idiv=0;idiv<ndiv; idiv++){ 
1044        ydiv+= dydiv;
1045        Float_t xdiv = 0.; 
1046        if (ydiv<rmin) xdiv= rmin * TMath::Sin( TMath::ACos(ydiv/rmin) );
1047        divpar[0] = (kpcbLength-xdiv)/2.; 
1048        divpar[1] = dydiv/2. - epsilon;
1049        divpar[2] = ksensWidth/2.; 
1050        Float_t xvol=(kpcbLength+xdiv)/2.+1.999;
1051        Float_t yvol=ydiv + dydiv/2.;
1052        gMC->Gsposp("S07G",imax+4*idiv+1,"S07M", -xvol, yvol, -z1-z2, 0, "ONLY",divpar,3);
1053        gMC->Gsposp("S08G",imax+4*idiv+1,"S08M", -xvol, yvol, -z1-z2, 0, "ONLY",divpar,3);
1054        gMC->Gsposp("S07G",imax+4*idiv+2,"S07M", -xvol,-yvol, -z1-z2, 0, "ONLY",divpar,3);
1055        gMC->Gsposp("S08G",imax+4*idiv+2,"S08M", -xvol,-yvol, -z1-z2, 0, "ONLY",divpar,3);
1056        gMC->Gsposp("S07G",imax+4*idiv+3,"S07M", xvol, yvol, -z1+z2, 0, "ONLY",divpar,3);
1057        gMC->Gsposp("S08G",imax+4*idiv+3,"S08M", xvol, yvol, -z1+z2, 0, "ONLY",divpar,3);
1058        gMC->Gsposp("S07G",imax+4*idiv+4,"S07M", xvol,-yvol, -z1+z2, 0, "ONLY",divpar,3);
1059        gMC->Gsposp("S08G",imax+4*idiv+4,"S08M", xvol,-yvol, -z1+z2, 0, "ONLY",divpar,3);
1060      }
1061
1062
1063
1064
1065
1066  }
1067
1068  if (fStations[4]) {
1069      
1070
1071 //********************************************************************
1072 //                            Station 5                             **
1073 //********************************************************************
1074      // indices 1 and 2 for first and second chambers in the station
1075      // iChamber (first chamber) kept for other quanties than Z,
1076      // assumed to be the same in both chambers
1077      iChamber1 = iChamber = (AliMUONChamber*) (*fChambers)[8];
1078      iChamber2 =(AliMUONChamber*) (*fChambers)[9];
1079      zpos1=iChamber1->Z(); 
1080      zpos2=iChamber2->Z();
1081      dstation = TMath::Abs(zpos2 - zpos1);
1082 //      zfpos=-(iChamber->DGas()+dframez+iChamber->DAlu())/2; // not used any more
1083      
1084 //
1085 //   Mother volume
1086      tpar[0] = iChamber->RInner()-dframep; 
1087      tpar[1] = (iChamber->ROuter()+dframep)/TMath::Cos(phi);
1088      tpar[2] = dstation/5.;
1089
1090      gMC->Gsvolu("S09M", "TUBE", idAir, tpar, 3);
1091      gMC->Gsvolu("S10M", "TUBE", idAir, tpar, 3);
1092      gMC->Gspos("S09M", 1, "ALIC", 0., 0., zpos1 , 0, "ONLY");
1093      gMC->Gspos("S10M", 1, "ALIC", 0., 0., zpos2 , 0, "ONLY");
1094
1095
1096      const Int_t knSlats5 = 7;  // number of slats per quadrant
1097      const Int_t knPCB5[knSlats5] = {5,5,6,6,5,4,3}; // n PCB per slat
1098      const Float_t kxpos5[knSlats5] = {38.5, 40., 0., 0., 0., 0., 0.};
1099      Float_t slatLength5[knSlats5]; 
1100      char volNam9[5];
1101      char volNam10[5];
1102      Float_t xSlat5;
1103      Float_t ySlat5;
1104
1105      for (i = 0; i<knSlats5; i++){
1106        slatLength5[i] = kpcbLength * knPCB5[i] + 2. * kdSlatLength; 
1107        xSlat5 = slatLength5[i]/2. - kvFrameLength/2. +kxpos5[i]; 
1108        if (i==1 || i==0) slatLength5[i] -=  2. *kdSlatLength; // frame out in PCB with circular border 
1109        ySlat5 = ksensHeight * i - kyOverlap * i; 
1110        spar[0] = slatLength5[i]/2.; 
1111        spar[1] = kslatHeight/2.;
1112        spar[2] = kslatWidth/2. * 1.01; 
1113        Float_t dzCh5=spar[2]*1.01;
1114        // zSlat to be checked (odd downstream or upstream?)
1115        Float_t zSlat = (i%2 ==0)? -spar[2] : spar[2]; 
1116        sprintf(volNam9,"S09%d",i);
1117        gMC->Gsvolu(volNam9,"BOX",kslatMaterial,spar,3);
1118        gMC->Gspos(volNam9, i*4+1,"S09M",-xSlat5, ySlat5, -zSlat-2.*dzCh5, 0, "ONLY");
1119        gMC->Gspos(volNam9, i*4+2,"S09M",+xSlat5, ySlat5, -zSlat+2.*dzCh5, 0, "ONLY");
1120        if (i>0) { 
1121            gMC->Gspos(volNam9, i*4+3,"S09M",-xSlat5,-ySlat5, -zSlat-2.*dzCh5, 0, "ONLY");
1122            gMC->Gspos(volNam9, i*4+4,"S09M",+xSlat5,-ySlat5, -zSlat+2.*dzCh5, 0, "ONLY");
1123        }
1124        sprintf(volNam10,"S10%d",i);
1125        gMC->Gsvolu(volNam10,"BOX",kslatMaterial,spar,3);
1126        gMC->Gspos(volNam10, i*4+1,"S10M",-xSlat5, ySlat5, -zSlat-2.*dzCh5, 0, "ONLY");
1127        gMC->Gspos(volNam10, i*4+2,"S10M",+xSlat5, ySlat5, -zSlat+2.*dzCh5, 0, "ONLY");
1128        if (i>0) { 
1129            gMC->Gspos(volNam10, i*4+3,"S10M",-xSlat5,-ySlat5, -zSlat-2.*dzCh5, 0, "ONLY");
1130            gMC->Gspos(volNam10, i*4+4,"S10M",+xSlat5,-ySlat5, -zSlat+2.*dzCh5, 0, "ONLY");
1131        }
1132      }
1133
1134      // create the panel volume 
1135  
1136      gMC->Gsvolu("S09C","BOX",kpanelMaterial,panelpar,3);
1137      gMC->Gsvolu("S10C","BOX",kpanelMaterial,panelpar,3);
1138
1139      // create the rohacell volume 
1140
1141      gMC->Gsvolu("S09R","BOX",krohaMaterial,rohapar,3);
1142      gMC->Gsvolu("S10R","BOX",krohaMaterial,rohapar,3);
1143
1144      // create the insulating material volume 
1145
1146      gMC->Gsvolu("S09I","BOX",kinsuMaterial,insupar,3);
1147      gMC->Gsvolu("S10I","BOX",kinsuMaterial,insupar,3);
1148
1149      // create the PCB volume 
1150
1151      gMC->Gsvolu("S09P","BOX",kpcbMaterial,pcbpar,3);
1152      gMC->Gsvolu("S10P","BOX",kpcbMaterial,pcbpar,3);
1153  
1154      // create the sensitive volumes,
1155
1156      gMC->Gsvolu("S09G","BOX",ksensMaterial,dum,0);
1157      gMC->Gsvolu("S10G","BOX",ksensMaterial,dum,0);
1158
1159      // create the vertical frame volume 
1160
1161      gMC->Gsvolu("S09V","BOX",kvFrameMaterial,vFramepar,3);
1162      gMC->Gsvolu("S10V","BOX",kvFrameMaterial,vFramepar,3);
1163
1164      // create the horizontal frame volume 
1165
1166      gMC->Gsvolu("S09H","BOX",khFrameMaterial,hFramepar,3);
1167      gMC->Gsvolu("S10H","BOX",khFrameMaterial,hFramepar,3);
1168
1169      // create the horizontal border volume 
1170
1171      gMC->Gsvolu("S09B","BOX",kbFrameMaterial,bFramepar,3);
1172      gMC->Gsvolu("S10B","BOX",kbFrameMaterial,bFramepar,3);
1173
1174      index=0; 
1175      for (i = 0; i<knSlats5; i++){
1176        sprintf(volNam9,"S09%d",i);
1177        sprintf(volNam10,"S10%d",i);
1178        Float_t xvFrame  = (slatLength5[i] - kvFrameLength)/2.;
1179        // position the vertical frames 
1180        if (i!=1 && i!=0) { 
1181          gMC->Gspos("S09V",2*i-1,volNam9, xvFrame, 0., 0. , 0, "ONLY");
1182          gMC->Gspos("S09V",2*i  ,volNam9,-xvFrame, 0., 0. , 0, "ONLY");
1183          gMC->Gspos("S10V",2*i-1,volNam10, xvFrame, 0., 0. , 0, "ONLY");
1184          gMC->Gspos("S10V",2*i  ,volNam10,-xvFrame, 0., 0. , 0, "ONLY");
1185        }
1186        
1187        // position the panels and the insulating material 
1188        for (j=0; j<knPCB5[i]; j++){
1189          index++;
1190          Float_t xx = ksensLength * (-knPCB5[i]/2.+j+.5); 
1191
1192          Float_t zPanel = spar[2] - panelpar[2]; 
1193          gMC->Gspos("S09C",2*index-1,volNam9, xx, 0., zPanel , 0, "ONLY");
1194          gMC->Gspos("S09C",2*index  ,volNam9, xx, 0.,-zPanel , 0, "ONLY");
1195          gMC->Gspos("S10C",2*index-1,volNam10, xx, 0., zPanel , 0, "ONLY");
1196          gMC->Gspos("S10C",2*index  ,volNam10, xx, 0.,-zPanel , 0, "ONLY");
1197
1198          gMC->Gspos("S09I",index,volNam9, xx, 0., 0 , 0, "ONLY");
1199          gMC->Gspos("S10I",index,volNam10, xx, 0., 0 , 0, "ONLY");
1200        } 
1201      }
1202
1203      // position the rohacell volume inside the panel volume
1204      gMC->Gspos("S09R",1,"S09C",0.,0.,0.,0,"ONLY"); 
1205      gMC->Gspos("S10R",1,"S10C",0.,0.,0.,0,"ONLY"); 
1206
1207      // position the PCB volume inside the insulating material volume
1208      gMC->Gspos("S09P",1,"S09I",0.,0.,0.,0,"ONLY"); 
1209      gMC->Gspos("S10P",1,"S10I",0.,0.,0.,0,"ONLY"); 
1210      // position the horizontal frame volume inside the PCB volume
1211      gMC->Gspos("S09H",1,"S09P",0.,0.,0.,0,"ONLY"); 
1212      gMC->Gspos("S10H",1,"S10P",0.,0.,0.,0,"ONLY"); 
1213      // position the sensitive volume inside the horizontal frame volume
1214      gMC->Gsposp("S09G",1,"S09H",0.,0.,0.,0,"ONLY",senspar,3); 
1215      gMC->Gsposp("S10G",1,"S10H",0.,0.,0.,0,"ONLY",senspar,3); 
1216      // position the border volumes inside the PCB volume
1217      Float_t yborder = ( kpcbHeight - kbFrameHeight ) / 2.; 
1218      gMC->Gspos("S09B",1,"S09P",0., yborder,0.,0,"ONLY"); 
1219      gMC->Gspos("S09B",2,"S09P",0.,-yborder,0.,0,"ONLY"); 
1220      gMC->Gspos("S10B",1,"S10P",0., yborder,0.,0,"ONLY"); 
1221      gMC->Gspos("S10B",2,"S10P",0.,-yborder,0.,0,"ONLY"); 
1222
1223      // create the NULOC volume and position it in the horizontal frame
1224
1225      gMC->Gsvolu("S09N","BOX",knulocMaterial,nulocpar,3);
1226      gMC->Gsvolu("S10N","BOX",knulocMaterial,nulocpar,3);
1227      index = 0;
1228      for (xx = -xxmax; xx<=xxmax; xx+=2*knulocLength) { 
1229        index++; 
1230        gMC->Gspos("S09N",2*index-1,"S09B", xx, 0.,-kbFrameWidth/4., 0, "ONLY");
1231        gMC->Gspos("S09N",2*index  ,"S09B", xx, 0., kbFrameWidth/4., 0, "ONLY");
1232        gMC->Gspos("S10N",2*index-1,"S10B", xx, 0.,-kbFrameWidth/4., 0, "ONLY");
1233        gMC->Gspos("S10N",2*index  ,"S10B", xx, 0., kbFrameWidth/4., 0, "ONLY");
1234      }
1235      // position the volumes approximating the circular section of the pipe
1236      Float_t yoffs = ksensHeight/2. - kyOverlap; 
1237      Float_t epsilon = 0.001; 
1238      Int_t ndiv=6;
1239      Float_t divpar[3];
1240      Double_t dydiv= ksensHeight/ndiv;
1241      Double_t ydiv = yoffs -dydiv;
1242      Int_t imax=0; 
1243      //     for (Int_t islat=0; islat<knSlats3; islat++) imax += knPCB3[islat]; 
1244      imax = 1; 
1245      Float_t rmin = 40.; 
1246      Float_t z1 = spar[2], z2=2*spar[2]*1.01; 
1247      for (Int_t idiv=0;idiv<ndiv; idiv++){ 
1248        ydiv+= dydiv;
1249        Float_t xdiv = 0.; 
1250        if (ydiv<rmin) xdiv= rmin * TMath::Sin( TMath::ACos(ydiv/rmin) );
1251        divpar[0] = (kpcbLength-xdiv)/2.; 
1252        divpar[1] = dydiv/2. - epsilon;
1253        divpar[2] = ksensWidth/2.; 
1254        Float_t xvol=(kpcbLength+xdiv)/2. + 1.999;
1255        Float_t yvol=ydiv + dydiv/2.;
1256        gMC->Gsposp("S09G",imax+4*idiv+1,"S09M", -xvol, yvol, -z1-z2, 0, "ONLY",divpar,3);
1257        gMC->Gsposp("S10G",imax+4*idiv+1,"S10M", -xvol, yvol, -z1-z2, 0, "ONLY",divpar,3);
1258        gMC->Gsposp("S09G",imax+4*idiv+2,"S09M", -xvol,-yvol, -z1-z2, 0, "ONLY",divpar,3);
1259        gMC->Gsposp("S10G",imax+4*idiv+2,"S10M", -xvol,-yvol, -z1-z2, 0, "ONLY",divpar,3);
1260        gMC->Gsposp("S09G",imax+4*idiv+3,"S09M", +xvol, yvol, -z1+z2, 0, "ONLY",divpar,3);
1261        gMC->Gsposp("S10G",imax+4*idiv+3,"S10M", +xvol, yvol, -z1+z2, 0, "ONLY",divpar,3);
1262        gMC->Gsposp("S09G",imax+4*idiv+4,"S09M", +xvol,-yvol, -z1+z2, 0, "ONLY",divpar,3);
1263        gMC->Gsposp("S10G",imax+4*idiv+4,"S10M", +xvol,-yvol, -z1+z2, 0, "ONLY",divpar,3);
1264      }
1265
1266  }
1267  
1268 //********************************************************************
1269 //                            Trigger                               **
1270 //******************************************************************** 
1271  /* 
1272     zpos1 and zpos2 are the middle of the first and second
1273     planes of station 1 (+1m for second station):
1274     zpos1=(zpos1m+zpos1p)/2=(15999+16071)/2=16035 mm, thick/2=40 mm
1275     zpos2=(zpos2m+zpos2p)/2=(16169+16241)/2=16205 mm, thick/2=40 mm
1276     zposxm and zposxp= middles of gaz gaps within a detection plane
1277     rem: the total thickness accounts for 1 mm of al on both
1278     side of the RPCs (see zpos1 and zpos2)
1279  */
1280
1281 // vertical gap between right and left chambers (kDXZERO*2=4cm)
1282  const Float_t kDXZERO=2.; 
1283 // main distances for chamber definition in first plane/first station
1284  const Float_t kXMIN=34.;       
1285  const Float_t kXMED=51.;                                
1286  const Float_t kXMAX=272.; 
1287 // kXMAX will become 255. in real life. segmentation to be updated accordingly
1288 // (see fig.2-4 & 2-5 of Local Trigger Board PRR)
1289  const Float_t kYMIN=34.;                              
1290  const Float_t kYMAX=51.;                              
1291 // inner/outer radius of flange between beam shield. and chambers (1/station)
1292  const Float_t kRMIN[2]={50.,50.};
1293  const Float_t kRMAX[2]={64.,68.};
1294 // z position of the middle of the gas gap in mother vol 
1295  const Float_t kZm=-3.6;
1296  const Float_t kZp=+3.6;     
1297  
1298  iChamber1 = (AliMUONChamber*) (*fChambers)[10];     
1299  zpos1 = iChamber1->Z();
1300
1301 // ratio of zpos1m/zpos1p and inverse for first plane
1302  Float_t zmp=(zpos1+3.6)/(zpos1-3.6);
1303  Float_t zpm=1./zmp;
1304  
1305  Int_t icount=0; // chamber counter (0 1 2 3)
1306  
1307  for (Int_t istation=0; istation<2; istation++) { // loop on stations    
1308      for (Int_t iplane=0; iplane<2; iplane++) {   // loop on detection planes
1309          
1310          Int_t iVolNum=1; // counter Volume Number
1311          icount = Int_t(iplane*TMath::Power(2,0))+
1312              Int_t(istation*TMath::Power(2,1));
1313          
1314          char volPlane[5]; 
1315          sprintf(volPlane,"SM%d%d",istation+1,iplane+1);
1316          
1317          iChamber = (AliMUONChamber*) (*fChambers)[10+icount];
1318          Float_t zpos = iChamber->Z();       
1319          
1320 // mother volume 
1321          tpar[0] = iChamber->RInner(); 
1322          tpar[1] = iChamber->ROuter(); 
1323          tpar[2] = 4.0;    
1324          gMC->Gsvolu(volPlane,"TUBE",idAir,tpar,3);
1325          
1326 // Flange between beam shielding and RPC 
1327          tpar[0]= kRMIN[istation];
1328          tpar[1]= kRMAX[istation];
1329          tpar[2]= 4.0;
1330          
1331          char volFlange[5];
1332          sprintf(volFlange,"SF%dA",icount+1);    
1333          gMC->Gsvolu(volFlange,"TUBE",idAlu1,tpar,3);     //Al
1334          gMC->Gspos(volFlange,1,volPlane,0.,0.,0.,0,"MANY");
1335          
1336 // scaling factor
1337          Float_t zRatio = zpos / zpos1;
1338          
1339 // chamber prototype
1340          tpar[0]= 0.;
1341          tpar[1]= 0.;
1342          tpar[2]= 0.;
1343          
1344          char volAlu[5]; // Alu
1345          char volBak[5]; // Bakelite
1346          char volGaz[5]; // Gas streamer
1347          
1348          sprintf(volAlu,"SC%dA",icount+1);
1349          sprintf(volBak,"SB%dA",icount+1);
1350          sprintf(volGaz,"SG%dA",icount+1);
1351          
1352          gMC->Gsvolu(volAlu,"BOX",idAlu1,tpar,0);           // Al
1353          gMC->Gsvolu(volBak,"BOX",idtmed[1107],tpar,0);     // Bakelite
1354          gMC->Gsvolu(volGaz,"BOX",idtmed[1106],tpar,0);     // Gas streamer
1355          
1356 // chamber type A
1357          tpar[0] = -1.;
1358          tpar[1] = -1.;
1359          
1360          Float_t xA=(kDXZERO+kXMED+(kXMAX-kXMED)/2.)*zRatio;
1361          Float_t yAm=0.;
1362          Float_t yAp=0.;
1363          
1364          tpar[2] = 0.1;    
1365          gMC->Gsposp(volGaz,1,volBak,0.,0.,0.,0,"ONLY",tpar,3);
1366          tpar[2] = 0.3;
1367          gMC->Gsposp(volBak,1,volAlu,0.,0.,0.,0,"ONLY",tpar,3);
1368          
1369          tpar[2] = 0.4;
1370          tpar[0] = ((kXMAX-kXMED)/2.)*zRatio;
1371          tpar[1] = kYMIN*zRatio;
1372          
1373          gMC->Gsposp(volAlu,iVolNum++,volPlane, -xA,yAm,-kZm,0,"ONLY",tpar,3);
1374          gMC->Gsposp(volAlu,iVolNum++,volPlane,  xA,yAp,-kZp,0,"ONLY",tpar,3);
1375          gMC->Gsbool(volAlu,volFlange);
1376          
1377 // chamber type B    
1378          Float_t tpar1save=tpar[1];
1379          Float_t y1msave=yAm;
1380          Float_t y1psave=yAp;
1381          
1382          tpar[0] = ((kXMAX-kXMIN)/2.) * zRatio;
1383          tpar[1] = ((kYMAX-kYMIN)/2.) * zRatio;
1384          
1385          Float_t xB=(kDXZERO+kXMIN)*zRatio+tpar[0];
1386          Float_t yBp=(y1msave+tpar1save)*zpm+tpar[1];
1387          Float_t yBm=(y1psave+tpar1save)*zmp+tpar[1];    
1388
1389          gMC->Gsposp(volAlu,iVolNum++,volPlane, -xB, yBp,-kZp,0,"ONLY",tpar,3);
1390          gMC->Gsposp(volAlu,iVolNum++,volPlane,  xB, yBm,-kZm,0,"ONLY",tpar,3);
1391          gMC->Gsposp(volAlu,iVolNum++,volPlane, -xB,-yBp,-kZp,0,"ONLY",tpar,3);
1392          gMC->Gsposp(volAlu,iVolNum++,volPlane,  xB,-yBm,-kZm,0,"ONLY",tpar,3);
1393          
1394 // chamber type C (note : same Z than type B)
1395          tpar1save=tpar[1];
1396          y1msave=yBm;
1397          y1psave=yBp;
1398          
1399          tpar[0] = (kXMAX/2)*zRatio;
1400          tpar[1] = (kYMAX/2)*zRatio;
1401          
1402          Float_t xC=kDXZERO*zRatio+tpar[0];
1403          Float_t yCp=(y1psave+tpar1save)*1.+tpar[1];
1404          Float_t yCm=(y1msave+tpar1save)*1.+tpar[1];
1405          
1406          gMC->Gsposp(volAlu,iVolNum++,volPlane,-xC, yCp,-kZp,0,"ONLY",tpar,3);
1407          gMC->Gsposp(volAlu,iVolNum++,volPlane, xC, yCm,-kZm,0,"ONLY",tpar,3);
1408          gMC->Gsposp(volAlu,iVolNum++,volPlane,-xC,-yCp,-kZp,0,"ONLY",tpar,3);
1409          gMC->Gsposp(volAlu,iVolNum++,volPlane, xC,-yCm,-kZm,0,"ONLY",tpar,3);
1410                  
1411 // chamber type D, E and F (same size)        
1412          tpar1save=tpar[1];
1413          y1msave=yCm;
1414          y1psave=yCp;
1415          
1416          tpar[0] = (kXMAX/2.)*zRatio;
1417          tpar[1] =  kYMIN*zRatio;
1418          
1419          Float_t xD=kDXZERO*zRatio+tpar[0];
1420          Float_t yDp=(y1msave+tpar1save)*zpm+tpar[1];
1421          Float_t yDm=(y1psave+tpar1save)*zmp+tpar[1];
1422          
1423          gMC->Gsposp(volAlu,iVolNum++,volPlane, -xD, yDm,-kZm,0,"ONLY",tpar,3);
1424          gMC->Gsposp(volAlu,iVolNum++,volPlane,  xD, yDp,-kZp,0,"ONLY",tpar,3);
1425          gMC->Gsposp(volAlu,iVolNum++,volPlane, -xD,-yDm,-kZm,0,"ONLY",tpar,3);
1426          gMC->Gsposp(volAlu,iVolNum++,volPlane,  xD,-yDp,-kZp,0,"ONLY",tpar,3);
1427          
1428          tpar1save=tpar[1];
1429          y1msave=yDm;
1430          y1psave=yDp;
1431          Float_t yEp=(y1msave+tpar1save)*zpm+tpar[1];
1432          Float_t yEm=(y1psave+tpar1save)*zmp+tpar[1];
1433          
1434          gMC->Gsposp(volAlu,iVolNum++,volPlane, -xD, yEp,-kZp,0,"ONLY",tpar,3);
1435          gMC->Gsposp(volAlu,iVolNum++,volPlane,  xD, yEm,-kZm,0,"ONLY",tpar,3);
1436          gMC->Gsposp(volAlu,iVolNum++,volPlane, -xD,-yEp,-kZp,0,"ONLY",tpar,3);
1437          gMC->Gsposp(volAlu,iVolNum++,volPlane,  xD,-yEm,-kZm,0,"ONLY",tpar,3);
1438          
1439          tpar1save=tpar[1];
1440          y1msave=yEm;
1441          y1psave=yEp;
1442          Float_t yFp=(y1msave+tpar1save)*zpm+tpar[1];
1443          Float_t yFm=(y1psave+tpar1save)*zmp+tpar[1];
1444          
1445          gMC->Gsposp(volAlu,iVolNum++,volPlane, -xD, yFm,-kZm,0,"ONLY",tpar,3);
1446          gMC->Gsposp(volAlu,iVolNum++,volPlane,  xD, yFp,-kZp,0,"ONLY",tpar,3);
1447          gMC->Gsposp(volAlu,iVolNum++,volPlane, -xD,-yFm,-kZm,0,"ONLY",tpar,3);
1448          gMC->Gsposp(volAlu,iVolNum++,volPlane,  xD,-yFp,-kZp,0,"ONLY",tpar,3);
1449
1450 // Positioning plane in ALICE     
1451          gMC->Gspos(volPlane,1,"ALIC",0.,0.,zpos,0,"ONLY");
1452          
1453      } // end loop on detection planes
1454  } // end loop on stations
1455
1456 }
1457
1458  
1459 //___________________________________________
1460 void AliMUONv1::CreateMaterials()
1461 {
1462   // *** DEFINITION OF AVAILABLE MUON MATERIALS *** 
1463   //
1464   //     Ar-CO2 gas (80%+20%)
1465     Float_t ag1[3]   = { 39.95,12.01,16. };
1466     Float_t zg1[3]   = { 18.,6.,8. };
1467     Float_t wg1[3]   = { .8,.0667,.13333 };
1468     Float_t dg1      = .001821;
1469     //
1470     //     Ar-buthane-freon gas -- trigger chambers 
1471     Float_t atr1[4]  = { 39.95,12.01,1.01,19. };
1472     Float_t ztr1[4]  = { 18.,6.,1.,9. };
1473     Float_t wtr1[4]  = { .56,.1262857,.2857143,.028 };
1474     Float_t dtr1     = .002599;
1475     //
1476     //     Ar-CO2 gas 
1477     Float_t agas[3]  = { 39.95,12.01,16. };
1478     Float_t zgas[3]  = { 18.,6.,8. };
1479     Float_t wgas[3]  = { .74,.086684,.173316 };
1480     Float_t dgas     = .0018327;
1481     //
1482     //     Ar-Isobutane gas (80%+20%) -- tracking 
1483     Float_t ag[3]    = { 39.95,12.01,1.01 };
1484     Float_t zg[3]    = { 18.,6.,1. };
1485     Float_t wg[3]    = { .8,.057,.143 };
1486     Float_t dg       = .0019596;
1487     //
1488     //     Ar-Isobutane-Forane-SF6 gas (49%+7%+40%+4%) -- trigger 
1489     Float_t atrig[5] = { 39.95,12.01,1.01,19.,32.066 };
1490     Float_t ztrig[5] = { 18.,6.,1.,9.,16. };
1491     Float_t wtrig[5] = { .49,1.08,1.5,1.84,0.04 };
1492     Float_t dtrig    = .0031463;
1493     //
1494     //     bakelite 
1495
1496     Float_t abak[3] = {12.01 , 1.01 , 16.};
1497     Float_t zbak[3] = {6.     , 1.   , 8.};
1498     Float_t wbak[3] = {6.     , 6.   , 1.}; 
1499     Float_t dbak = 1.4;
1500
1501     Float_t epsil, stmin, deemax, tmaxfd, stemax;
1502
1503     Int_t iSXFLD   = gAlice->Field()->Integ();
1504     Float_t sXMGMX = gAlice->Field()->Max();
1505     //
1506     // --- Define the various materials for GEANT --- 
1507     AliMaterial(9, "ALUMINIUM$", 26.98, 13., 2.7, 8.9, 37.2);
1508     AliMaterial(10, "ALUMINIUM$", 26.98, 13., 2.7, 8.9, 37.2);
1509     AliMaterial(15, "AIR$      ", 14.61, 7.3, .001205, 30423.24, 67500);
1510     AliMixture(19, "Bakelite$", abak, zbak, dbak, -3, wbak);
1511     AliMixture(20, "ArC4H10 GAS$", ag, zg, dg, 3, wg);
1512     AliMixture(21, "TRIG GAS$", atrig, ztrig, dtrig, -5, wtrig);
1513     AliMixture(22, "ArCO2 80%$", ag1, zg1, dg1, 3, wg1);
1514     AliMixture(23, "Ar-freon $", atr1, ztr1, dtr1, 4, wtr1);
1515     AliMixture(24, "ArCO2 GAS$", agas, zgas, dgas, 3, wgas);
1516     // materials for slat: 
1517     //     Sensitive area: gas (already defined) 
1518     //     PCB: copper 
1519     //     insulating material and frame: vetronite
1520     //     walls: carbon, rohacell, carbon 
1521   Float_t aglass[5]={12.01, 28.09, 16.,   10.8,  23.};
1522   Float_t zglass[5]={ 6.,   14.,    8.,    5.,   11.};
1523   Float_t wglass[5]={ 0.5,  0.105, 0.355, 0.03,  0.01};
1524   Float_t dglass=1.74;
1525
1526   // rohacell: C9 H13 N1 O2
1527   Float_t arohac[4] = {12.01,  1.01, 14.010, 16.};
1528   Float_t zrohac[4] = { 6.,    1.,    7.,     8.};
1529   Float_t wrohac[4] = { 9.,   13.,    1.,     2.};
1530   Float_t drohac    = 0.03;
1531
1532   AliMaterial(31, "COPPER$",   63.54,    29.,   8.96,  1.4, 0.);
1533   AliMixture(32, "Vetronite$",aglass, zglass, dglass,    5, wglass);
1534   AliMaterial(33, "Carbon$",   12.01,     6.,  2.265, 18.8, 49.9);
1535   AliMixture(34, "Rohacell$", arohac, zrohac, drohac,   -4, wrohac); 
1536
1537
1538     epsil  = .001; // Tracking precision, 
1539     stemax = -1.;  // Maximum displacement for multiple scat 
1540     tmaxfd = -20.; // Maximum angle due to field deflection 
1541     deemax = -.3;  // Maximum fractional energy loss, DLS 
1542     stmin  = -.8;
1543     //
1544     //    Air 
1545     AliMedium(1, "AIR_CH_US         ", 15, 1, iSXFLD, sXMGMX, tmaxfd, stemax, deemax, epsil, stmin);
1546     //
1547     //    Aluminum 
1548
1549     AliMedium(4, "ALU_CH_US          ", 9, 0, iSXFLD, sXMGMX, tmaxfd, fMaxStepAlu, 
1550             fMaxDestepAlu, epsil, stmin);
1551     AliMedium(5, "ALU_CH_US          ", 10, 0, iSXFLD, sXMGMX, tmaxfd, fMaxStepAlu, 
1552             fMaxDestepAlu, epsil, stmin);
1553     //
1554     //    Ar-isoC4H10 gas 
1555
1556     AliMedium(6, "AR_CH_US          ", 20, 1, iSXFLD, sXMGMX, tmaxfd, fMaxStepGas, 
1557             fMaxDestepGas, epsil, stmin);
1558 //
1559     //    Ar-Isobuthane-Forane-SF6 gas 
1560
1561     AliMedium(7, "GAS_CH_TRIGGER    ", 21, 1, iSXFLD, sXMGMX, tmaxfd, stemax, deemax, epsil, stmin);
1562
1563     AliMedium(8, "BAKE_CH_TRIGGER   ", 19, 0, iSXFLD, sXMGMX, tmaxfd, fMaxStepAlu, 
1564             fMaxDestepAlu, epsil, stmin);
1565
1566     AliMedium(9, "ARG_CO2   ", 22, 1, iSXFLD, sXMGMX, tmaxfd, fMaxStepGas, 
1567             fMaxDestepAlu, epsil, stmin);
1568     // tracking media for slats: check the parameters!! 
1569     AliMedium(11, "PCB_COPPER        ", 31, 0, iSXFLD, sXMGMX, tmaxfd, 
1570               fMaxStepAlu, fMaxDestepAlu, epsil, stmin);
1571     AliMedium(12, "VETRONITE         ", 32, 0, iSXFLD, sXMGMX, tmaxfd, 
1572               fMaxStepAlu, fMaxDestepAlu, epsil, stmin);
1573     AliMedium(13, "CARBON            ", 33, 0, iSXFLD, sXMGMX, tmaxfd, 
1574               fMaxStepAlu, fMaxDestepAlu, epsil, stmin);
1575     AliMedium(14, "Rohacell          ", 34, 0, iSXFLD, sXMGMX, tmaxfd, 
1576               fMaxStepAlu, fMaxDestepAlu, epsil, stmin);
1577 }
1578
1579 //___________________________________________
1580
1581 void AliMUONv1::Init()
1582 {
1583    // 
1584    // Initialize Tracking Chambers
1585    //
1586
1587    if(fDebug) printf("\n%s: Start Init for version 1 - CPC chamber type\n\n",ClassName());
1588    Int_t i;
1589    for (i=0; i<AliMUONConstants::NCh(); i++) {
1590        ( (AliMUONChamber*) (*fChambers)[i])->Init();
1591    }
1592    
1593    //
1594    // Set the chamber (sensitive region) GEANT identifier
1595    ((AliMUONChamber*)(*fChambers)[0])->SetGid(gMC->VolId("S01G"));
1596    ((AliMUONChamber*)(*fChambers)[1])->SetGid(gMC->VolId("S02G"));
1597
1598    ((AliMUONChamber*)(*fChambers)[2])->SetGid(gMC->VolId("S03G"));
1599    ((AliMUONChamber*)(*fChambers)[3])->SetGid(gMC->VolId("S04G"));
1600
1601    ((AliMUONChamber*)(*fChambers)[4])->SetGid(gMC->VolId("S05G"));
1602    ((AliMUONChamber*)(*fChambers)[5])->SetGid(gMC->VolId("S06G"));
1603
1604    ((AliMUONChamber*)(*fChambers)[6])->SetGid(gMC->VolId("S07G"));
1605    ((AliMUONChamber*)(*fChambers)[7])->SetGid(gMC->VolId("S08G"));
1606
1607    ((AliMUONChamber*)(*fChambers)[8])->SetGid(gMC->VolId("S09G"));
1608    ((AliMUONChamber*)(*fChambers)[9])->SetGid(gMC->VolId("S10G"));
1609
1610    ((AliMUONChamber*)(*fChambers)[10])->SetGid(gMC->VolId("SG1A"));
1611    ((AliMUONChamber*)(*fChambers)[11])->SetGid(gMC->VolId("SG2A"));
1612    ((AliMUONChamber*)(*fChambers)[12])->SetGid(gMC->VolId("SG3A"));
1613    ((AliMUONChamber*)(*fChambers)[13])->SetGid(gMC->VolId("SG4A"));
1614
1615    if(fDebug) printf("\n%s: Finished Init for version 1 - CPC chamber type\n",ClassName());
1616
1617    //cp 
1618    if(fDebug) printf("\n%s: Start Init for Trigger Circuits\n",ClassName());
1619    for (i=0; i<AliMUONConstants::NTriggerCircuit(); i++) {
1620      ( (AliMUONTriggerCircuit*) (*fTriggerCircuits)[i])->Init(i);
1621    }
1622    if(fDebug) printf("%s: Finished Init for Trigger Circuits\n",ClassName());
1623    //cp
1624
1625 }
1626
1627 //_______________________________________________________________________________
1628 Int_t  AliMUONv1::GetChamberId(Int_t volId) const
1629 {
1630 // Check if the volume with specified  volId is a sensitive volume (gas) 
1631 // of some chamber and returns the chamber number;
1632 // if not sensitive volume - return 0.
1633 // ---
1634
1635   for (Int_t i = 1; i <= AliMUONConstants::NCh(); i++)
1636     if (volId==((AliMUONChamber*)(*fChambers)[i-1])->GetGid()) return i;
1637
1638   return 0;
1639 }
1640 //_______________________________________________________________________________
1641 void AliMUONv1::StepManager()
1642 {
1643   // Stepmanager for the chambers
1644
1645  if (fStepManagerVersionOld) {
1646     StepManagerOld();
1647     return;
1648   }
1649
1650   // Only charged tracks
1651   if( !(gMC->TrackCharge()) ) return; 
1652   // Only charged tracks
1653   
1654   // Only gas gap inside chamber
1655   // Tag chambers and record hits when track enters 
1656   Int_t   idvol=-1;
1657   Int_t   iChamber=0;
1658   Int_t   id=0;
1659   Int_t   copy;
1660   const  Float_t kBig = 1.e10;
1661
1662   id=gMC->CurrentVolID(copy);
1663   // printf("id == %d \n",id);
1664   for (Int_t i = 1; i <= AliMUONConstants::NCh(); i++) {
1665     if(id==((AliMUONChamber*)(*fChambers)[i-1])->GetGid()) {
1666       iChamber = i;
1667       idvol  = i-1;
1668     }
1669   }
1670   if (idvol == -1) {
1671     return;
1672   }
1673
1674    if( gMC->IsTrackEntering() ) {
1675      Float_t theta = fTrackMomentum.Theta();
1676      if ((TMath::Pi()-theta)*kRaddeg>=15.) gMC->SetMaxStep(fStepMaxInActiveGas); // We use Pi-theta because z is negative
1677   }
1678
1679 //  if (GetDebug()) {
1680 //     Float_t z = ( (AliMUONChamber*)(*fChambers)[idvol])->Z() ;
1681 //      Info("StepManager Step","Active volume found %d chamber %d Z chamber is %f ",idvol,iChamber, z);
1682 //   }  
1683   // Particule id and mass, 
1684   Int_t     ipart = gMC->TrackPid();
1685   Float_t   mass  = gMC->TrackMass();
1686
1687   fDestepSum[idvol]+=gMC->Edep();
1688   // Get current particle id (ipart), track position (pos)  and momentum (mom)
1689   if ( fStepSum[idvol]==0.0 )  gMC->TrackMomentum(fTrackMomentum);
1690   fStepSum[idvol]+=gMC->TrackStep();
1691   
1692 //   if (GetDebug()) {
1693 //     Info("StepManager Step","iChamber %d, Particle %d, theta %f phi %f mass %f StepSum %f eloss %g",
1694 //       iChamber,ipart, fTrackMomentum.Theta()*kRaddeg, fTrackMomentum.Phi()*kRaddeg, mass, fStepSum[idvol], gMC->Edep());
1695 //     Info("StepManager Step","Track Momentum %f %f %f", fTrackMomentum.X(), fTrackMomentum.Y(), fTrackMomentum.Z()) ;
1696 //     gMC->TrackPosition(fTrackPosition);
1697 //     Info("StepManager Step","Track Position %f %f %f",fTrackPosition.X(),fTrackPosition.Y(),fTrackPosition.Z()) ;
1698 //   }
1699
1700   // Track left chamber or StepSum larger than fStepMaxInActiveGas
1701   if ( gMC->IsTrackExiting() || 
1702        gMC->IsTrackStop() || 
1703        gMC->IsTrackDisappeared()||
1704        (fStepSum[idvol]>fStepMaxInActiveGas) ) {
1705     
1706     if   ( gMC->IsTrackExiting() || 
1707            gMC->IsTrackStop() || 
1708            gMC->IsTrackDisappeared() ) gMC->SetMaxStep(kBig);
1709
1710     gMC->TrackPosition(fTrackPosition);
1711     Float_t theta = fTrackMomentum.Theta();
1712     Float_t phi   = fTrackMomentum.Phi();
1713     
1714     TLorentzVector backToWire( fStepSum[idvol]/2.*sin(theta)*cos(phi),
1715                                fStepSum[idvol]/2.*sin(theta)*sin(phi),
1716                                fStepSum[idvol]/2.*cos(theta),0.0       );
1717     //     if (GetDebug()) 
1718     //       Info("StepManager Exit","Track Position %f %f %f",fTrackPosition.X(),fTrackPosition.Y(),fTrackPosition.Z()) ;
1719     //     if (GetDebug()) 
1720     //        Info("StepManager Exit ","Track backToWire %f %f %f",backToWire.X(),backToWire.Y(),backToWire.Z()) ;
1721     fTrackPosition-=backToWire;
1722     
1723     //-------------- Angle effect 
1724     // Ratio between energy loss of particle and Mip as a function of BetaGamma of particle (Energy/Mass)
1725     
1726     Float_t BetaxGamma    = fTrackMomentum.P()/mass;//  pc/mc2
1727     Float_t sigmaEffect10degrees;
1728     Float_t sigmaEffectThetadegrees;
1729     Float_t eLossParticleELossMip;
1730     Float_t yAngleEffect=0.;
1731     Float_t thetawires      =  TMath::Abs( TMath::ASin( TMath::Sin(TMath::Pi()-theta) * TMath::Sin(phi) ) );// We use Pi-theta because z is negative
1732
1733     if ( (BetaxGamma >3.2)   &&  (thetawires*kRaddeg<=15.) ) {
1734       BetaxGamma=TMath::Log(BetaxGamma);
1735       eLossParticleELossMip = fElossRatio->Eval(BetaxGamma);
1736       // 10 degrees is a reference for a model (arbitrary)
1737       sigmaEffect10degrees=fAngleEffect10->Eval(eLossParticleELossMip);// in micrometers
1738       // Angle with respect to the wires assuming that chambers are perpendicular to the z axis.
1739       sigmaEffectThetadegrees =  sigmaEffect10degrees/fAngleEffectNorma->Eval(thetawires*kRaddeg);  // For 5mm gap  
1740       if ( (iChamber==1)  ||  (iChamber==2) )  
1741         sigmaEffectThetadegrees/=(1.09833e+00+1.70000e-02*(thetawires*kRaddeg)); // The gap is different (4mm)
1742       yAngleEffect=1.e-04*gRandom->Gaus(0,sigmaEffectThetadegrees); // Error due to the angle effect in cm
1743     }
1744     
1745     
1746     // One hit per chamber
1747     GetMUONData()->AddHit(fIshunt, gAlice->GetMCApp()->GetCurrentTrackNumber(), iChamber, ipart, 
1748                           fTrackPosition.X(), fTrackPosition.Y()+yAngleEffect, fTrackPosition.Z(), 0.0, 
1749                           fTrackMomentum.P(),theta, phi, fStepSum[idvol], fDestepSum[idvol],
1750                           fTrackPosition.X(),fTrackPosition.Y(),fTrackPosition.Z());
1751 //     if (GetDebug()){
1752 //       Info("StepManager Exit","Particle exiting from chamber %d",iChamber);
1753 //       Info("StepManager Exit","StepSum %f eloss geant %g ",fStepSum[idvol],fDestepSum[idvol]);
1754 //       Info("StepManager Exit","Track Position %f %f %f",fTrackPosition.X(),fTrackPosition.Y(),fTrackPosition.Z()) ;
1755 //     }
1756     fStepSum[idvol]  =0; // Reset for the next event
1757     fDestepSum[idvol]=0; // Reset for the next event
1758   }
1759 }
1760
1761 //__________________________________________
1762 void AliMUONv1::StepManagerOld()
1763 {
1764   // Old Stepmanager for the chambers
1765   Int_t          copy, id;
1766   static Int_t   idvol;
1767   static Int_t   vol[2];
1768   Int_t          ipart;
1769   TLorentzVector pos;
1770   TLorentzVector mom;
1771   Float_t        theta,phi;
1772   Float_t        destep, step;
1773   
1774   static Float_t sstep;
1775   static Float_t eloss, eloss2, xhit, yhit, zhit, tof, tlength;
1776   const  Float_t kBig = 1.e10;
1777   static Float_t hits[15];
1778
1779   TClonesArray &lhits = *fHits;
1780
1781   //
1782   //
1783   // Only charged tracks
1784   if( !(gMC->TrackCharge()) ) return; 
1785   //
1786   // Only gas gap inside chamber
1787   // Tag chambers and record hits when track enters 
1788   id=gMC->CurrentVolID(copy);
1789   vol[0] = GetChamberId(id);
1790   idvol = vol[0] -1;
1791
1792   if (idvol == -1) return;
1793
1794   //
1795   // Get current particle id (ipart), track position (pos)  and momentum (mom) 
1796   gMC->TrackPosition(pos);
1797   gMC->TrackMomentum(mom);
1798
1799   ipart  = gMC->TrackPid();
1800
1801   //
1802   // momentum loss and steplength in last step
1803   destep = gMC->Edep();
1804   step   = gMC->TrackStep();
1805   // cout<<"------------"<<step<<endl;
1806   //
1807   // record hits when track enters ...
1808   if( gMC->IsTrackEntering()) {
1809
1810       gMC->SetMaxStep(fMaxStepGas);
1811       Double_t tc = mom[0]*mom[0]+mom[1]*mom[1];
1812       Double_t rt = TMath::Sqrt(tc);
1813       Double_t pmom = TMath::Sqrt(tc+mom[2]*mom[2]);
1814       Double_t tx = mom[0]/pmom;
1815       Double_t ty = mom[1]/pmom;
1816       Double_t tz = mom[2]/pmom;
1817       Double_t s  = ((AliMUONChamber*)(*fChambers)[idvol])
1818           ->ResponseModel()
1819           ->Pitch()/tz;
1820       theta   = Float_t(TMath::ATan2(rt,Double_t(mom[2])))*kRaddeg;
1821       phi     = Float_t(TMath::ATan2(Double_t(mom[1]),Double_t(mom[0])))*kRaddeg;
1822       hits[0] = Float_t(ipart);         // Geant3 particle type
1823       hits[1] = pos[0]+s*tx;            // X-position for hit
1824       hits[2] = pos[1]+s*ty;            // Y-position for hit
1825       hits[3] = pos[2]+s*tz;            // Z-position for hit
1826       hits[4] = theta;                  // theta angle of incidence
1827       hits[5] = phi;                    // phi angle of incidence 
1828       hits[8] = 0;//PadHits does not exist anymore  (Float_t) fNPadHits;    // first padhit
1829       hits[9] = -1;                     // last pad hit
1830       hits[10] = mom[3];                // hit momentum P
1831       hits[11] = mom[0];                // Px
1832       hits[12] = mom[1];                // Py
1833       hits[13] = mom[2];                // Pz
1834       tof=gMC->TrackTime();
1835       hits[14] = tof;                   // Time of flight
1836       tlength  = 0;
1837       eloss    = 0;
1838       eloss2   = 0;
1839       sstep=0;
1840       xhit     = pos[0];
1841       yhit     = pos[1];      
1842       zhit     = pos[2];      
1843       Chamber(idvol).ChargeCorrelationInit();
1844       // Only if not trigger chamber
1845
1846 //       printf("---------------------------\n");
1847 //       printf(">>>> Y =  %f \n",hits[2]);
1848 //       printf("---------------------------\n");
1849     
1850       
1851
1852      //  if(idvol < AliMUONConstants::NTrackingCh()) {
1853 //        //
1854 //        //  Initialize hit position (cursor) in the segmentation model 
1855 //        ((AliMUONChamber*) (*fChambers)[idvol])
1856 //            ->SigGenInit(pos[0], pos[1], pos[2]);
1857 //       } else {
1858 //        //geant3->Gpcxyz();
1859 //        //printf("In the Trigger Chamber #%d\n",idvol-9);
1860 //       }
1861   }
1862   eloss2+=destep;
1863   sstep+=step;
1864
1865   // cout<<sstep<<endl;
1866
1867   // 
1868   // Calculate the charge induced on a pad (disintegration) in case 
1869   //
1870   // Mip left chamber ...
1871   if( gMC->IsTrackExiting() || gMC->IsTrackStop() || gMC->IsTrackDisappeared()){
1872       gMC->SetMaxStep(kBig);
1873       eloss   += destep;
1874       tlength += step;
1875       
1876       Float_t x0,y0,z0;
1877       Float_t localPos[3];
1878       Float_t globalPos[3] = {pos[0], pos[1], pos[2]};
1879       gMC->Gmtod(globalPos,localPos,1); 
1880
1881       if(idvol < AliMUONConstants::NTrackingCh()) {
1882 // tracking chambers
1883           x0 = 0.5*(xhit+pos[0]);
1884           y0 = 0.5*(yhit+pos[1]);
1885           z0 = 0.5*(zhit+pos[2]);
1886       } else {
1887 // trigger chambers
1888           x0 = xhit;
1889           y0 = yhit;
1890           z0 = 0.;
1891       }
1892       
1893
1894       //      if (eloss >0)  MakePadHits(x0,y0,z0,eloss,tof,idvol);
1895       
1896           
1897       hits[6] = tlength;   // track length
1898       hits[7] = eloss2;    // de/dx energy loss
1899
1900
1901       //      if (fNPadHits > (Int_t)hits[8]) {
1902       //          hits[8] = hits[8]+1;
1903       //          hits[9] = 0: // PadHits does not exist anymore (Float_t) fNPadHits;
1904       //}
1905 //
1906 //    new hit 
1907       
1908       new(lhits[fNhits++]) 
1909           AliMUONHit(fIshunt, gAlice->GetMCApp()->GetCurrentTrackNumber(), vol,hits);
1910       eloss = 0; 
1911       //
1912       // Check additional signal generation conditions 
1913       // defined by the segmentation
1914       // model (boundary crossing conditions)
1915       // only for tracking chambers
1916   } else if 
1917       ((idvol < AliMUONConstants::NTrackingCh()) &&
1918        ((AliMUONChamber*) (*fChambers)[idvol])->SigGenCond(pos[0], pos[1], pos[2]))
1919   {
1920       ((AliMUONChamber*) (*fChambers)[idvol])
1921           ->SigGenInit(pos[0], pos[1], pos[2]);
1922       
1923       Float_t localPos[3];
1924       Float_t globalPos[3] = {pos[0], pos[1], pos[2]};
1925       gMC->Gmtod(globalPos,localPos,1); 
1926
1927       eloss    += destep;
1928
1929       // if (eloss > 0 && idvol < AliMUONConstants::NTrackingCh())
1930       //        MakePadHits(0.5*(xhit+pos[0]),0.5*(yhit+pos[1]),pos[2],eloss,tof,idvol);
1931       xhit     = pos[0];
1932       yhit     = pos[1]; 
1933       zhit     = pos[2];
1934       eloss = 0;
1935       tlength += step ;
1936       //
1937       // nothing special  happened, add up energy loss
1938   } else {        
1939       eloss   += destep;
1940       tlength += step ;
1941   }
1942 }
1943
1944