]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONv1.cxx
Moving AliMUONTriggerEfficiencyCells from sim to base
[u/mrichter/AliRoot.git] / MUON / AliMUONv1.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 purpeateose. It is      *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /* $Id$ */
17
18 // --------------------
19 // Class AliMUONv1
20 // --------------------
21 // AliDetector class for MUON subsystem which implements
22 // functions for simulation 
23
24 #include "AliMUONv1.h"
25 #include "AliMUONConstants.h"
26 #include "AliMUONSegFactory.h"
27 #include "AliMUONResponseFactory.h"
28 #include "AliMUONSegmentation.h"
29 #include "AliMUONHit.h"
30 #include "AliMUONTriggerCircuit.h"
31 #include "AliMUONTriggerCircuitNew.h"
32 #include "AliMUONTriggerCrateStore.h"
33 #include "AliMUONGeometryBuilder.h"     
34 #include "AliMUONGeometry.h"    
35 #include "AliMUONGeometryTransformer.h" 
36 #include "AliMUONGeometryModule.h"      
37 #include "AliMUONStringIntMap.h"        
38 #include "AliMUONGeometryDetElement.h"  
39
40 #include "AliConst.h" 
41 #include "AliMagF.h"
42 #include "AliRun.h"
43 #include "AliMC.h"
44 #include "AliLog.h"
45
46 #include <TRandom.h>
47 #include <TF1.h>
48 #include <TClonesArray.h>
49 #include <TRandom.h> 
50 #include <TVirtualMC.h>
51 #include <TGeoMatrix.h>
52
53 #include <string>
54
55 ClassImp(AliMUONv1)
56  
57 //___________________________________________
58 AliMUONv1::AliMUONv1() 
59   : AliMUON(),
60     fStepManagerVersionOld(kTRUE),
61     fStepManagerVersionDE(kTRUE),
62     fAngleEffect(kTRUE),
63     fStepMaxInActiveGas(0.6),
64     fStepSum(0x0),
65     fDestepSum(0x0),
66     fTrackMomentum(), 
67     fTrackPosition(),
68     fElossRatio(0x0),
69     fAngleEffect10(0x0),
70     fAngleEffectNorma(0x0)
71 {
72 /// Default constructor
73   
74   AliDebug(1,Form("default (empty) ctor this=%p",this));
75
76
77 //___________________________________________
78 AliMUONv1::AliMUONv1(const char *name, const char *title,
79                      const char* sDigitizerClassName,
80                      const char* digitizerClassName)
81 : AliMUON(name,title,sDigitizerClassName,digitizerClassName), 
82     fStepManagerVersionOld(kTRUE),
83     fStepManagerVersionDE(kTRUE),
84     fAngleEffect(kTRUE),
85     fStepMaxInActiveGas(0.6),
86     fStepSum(0x0),
87     fDestepSum(0x0),
88     fTrackMomentum(), 
89     fTrackPosition(),
90     fElossRatio(0x0),
91     fAngleEffect10(0x0),
92     fAngleEffectNorma(0x0)
93 {
94 /// Standard onstructor
95
96     AliDebug(1,Form("ctor this=%p",this));      
97         
98     // By default include all stations
99
100     fStepSum   = new Float_t [AliMUONConstants::NCh()];
101     fDestepSum = new Float_t [AliMUONConstants::NCh()];
102     for (Int_t i=0; i<AliMUONConstants::NCh(); i++) {
103       fStepSum[i] =0.0;
104       fDestepSum[i]=0.0;
105     }
106     // Ratio of particle mean eloss with respect MIP's Khalil Boudjemline, sep 2003, PhD.Thesis and Particle Data Book
107     fElossRatio = new TF1("ElossRatio","[0]+[1]*x+[2]*x*x+[3]*x*x*x+[4]*x*x*x*x",0.5,5.); 
108     fElossRatio->SetParameter(0,1.02138);
109     fElossRatio->SetParameter(1,-9.54149e-02);
110     fElossRatio->SetParameter(2,+7.83433e-02); 
111     fElossRatio->SetParameter(3,-9.98208e-03);
112     fElossRatio->SetParameter(4,+3.83279e-04);
113
114     // Angle effect in tracking chambers at theta =10 degres as a function of ElossRatio (Khalil BOUDJEMLINE sep 2003 Ph.D Thesis) (in micrometers)
115     fAngleEffect10 = new TF1("AngleEffect10","[0]+[1]*x+[2]*x*x",0.5,3.0);
116     fAngleEffect10->SetParameter(0, 1.90691e+02);
117     fAngleEffect10->SetParameter(1,-6.62258e+01);
118     fAngleEffect10->SetParameter(2,+1.28247e+01);
119     // Angle effect: Normalisation form theta=10 degres to theta between 0 and 10 (Khalil BOUDJEMLINE sep 2003 Ph.D Thesis)  
120     // Angle with respect to the wires assuming that chambers are perpendicular to the z axis.
121     fAngleEffectNorma = new TF1("AngleEffectNorma","[0]+[1]*x+[2]*x*x+[3]*x*x*x",0.0,10.0);
122     fAngleEffectNorma->SetParameter(0,4.148);
123     fAngleEffectNorma->SetParameter(1,-6.809e-01);
124     fAngleEffectNorma->SetParameter(2,5.151e-02);
125     fAngleEffectNorma->SetParameter(3,-1.490e-03);
126 }
127
128 //_____________________________________________________________________________
129 AliMUONv1::AliMUONv1(const AliMUONv1& right) 
130   : AliMUON(right) 
131 {  
132 /// Copy constructor (not implemented)
133
134   AliFatal("Copy constructor not provided.");
135 }
136
137 //___________________________________________
138 AliMUONv1::~AliMUONv1()
139 {
140 /// Destructor
141   
142   AliDebug(1,Form("dtor this=%p",this));
143   delete [] fStepSum;
144   delete [] fDestepSum;
145   delete fElossRatio;
146   delete fAngleEffect10;
147   delete fAngleEffectNorma; 
148 }
149
150 //_____________________________________________________________________________
151 AliMUONv1& AliMUONv1::operator=(const AliMUONv1& right)
152 {
153 /// Assignement operator (not implemented)
154
155   // check assignement to self
156   if (this == &right) return *this;
157
158   AliFatal("Assignement operator not provided.");
159     
160   return *this;  
161 }    
162
163 //__________________________________________________
164 void AliMUONv1::CreateGeometry()
165 {
166 /// Construct geometry using geometry builder
167
168   fGeometryBuilder->CreateGeometry();
169 }
170
171 //________________________________________________________________
172 void AliMUONv1::CreateMaterials()
173 {
174 /// Construct materials using geometry builder
175
176   fGeometryBuilder->CreateMaterials();
177 }
178
179 //___________________________________________
180 void AliMUONv1::Init()
181
182 /// Initialize geometry
183
184   AliDebug(1,"Start Init for version 1 - CPC chamber type");
185    
186   fGeometryBuilder->InitGeometry();
187   AliDebug(1,"Finished Init for version 1 - CPC chamber type");   
188  
189
190   std::string ftype(GetTitle());
191
192   // Build segmentation
193   // using geometry parametrisation
194   //
195   AliMUONSegFactory segFactory(GetGeometryTransformer());
196   fSegmentation = segFactory.CreateSegmentation(ftype);
197
198   if (!fSegmentation) {
199     AliFatal(Form("Wrong factory type : %s",ftype.c_str()));
200   }        
201
202   // Build response
203   //
204   AliMUONResponseFactory respFactory("default");
205   respFactory.Build(this);
206   
207
208   // Initialize segmentation
209   //
210   fSegmentation->Init();
211
212   // Initialize trigger circuits
213   //
214   for (Int_t i=0; i<AliMUONConstants::NTriggerCircuit(); i++)  {
215     AliMUONTriggerCircuit* c = (AliMUONTriggerCircuit*)(fTriggerCircuits->At(i));
216     c->Init(i);
217   }
218   
219   AliMUONTriggerCrateStore store;
220   store.ReadFromFile();
221   for (Int_t i=0; i<AliMUONConstants::NTriggerCircuit(); i++)  
222   {
223     AliMUONTriggerCircuitNew* c = (AliMUONTriggerCircuitNew*)(fTriggerCircuitsNew->At(i));
224     c->Init(i,store);
225   }
226   
227 }
228
229 //__________________________________________________________________
230 Int_t  AliMUONv1::GetChamberId(Int_t volId) const
231 {
232 /// Check if the volume with specified  volId is a sensitive volume (gas) 
233 /// of some chamber and returns the chamber number;
234 /// if not sensitive volume - return 0.
235
236   for (Int_t i = 0; i < AliMUONConstants::NCh(); i++) {
237     if ( GetGeometry()->GetModule(i)->IsSensitiveVolume(volId) )
238       return i+1;
239   }    
240
241   return 0;
242 }
243
244 //_______________________________________________________________________________
245 TString  AliMUONv1::CurrentVolumePath() const
246 {
247 /// Return current volume path
248 /// (Could be removed when this function is available via gMC)
249
250   TString path = "";
251   TString name;
252   Int_t copyNo;
253   Int_t imother = 0;
254   do {
255     name = gMC->CurrentVolOffName(imother);
256     gMC->CurrentVolOffID(imother++, copyNo);
257     TString add = "/";
258     add += name;
259     add += "_";
260     add += copyNo;
261     path.Insert(0,add); 
262   }
263   while ( name != TString("ALIC") );
264   
265   return path;  
266 }
267
268 //_______________________________________________________________________________
269 void AliMUONv1::StepManager()
270 {
271 /// Step manager for the chambers
272 /// TBR
273
274  if (fStepManagerVersionDE) {
275     StepManager2();
276     return;
277   }
278
279
280   // Only charged tracks
281   if( !(gMC->TrackCharge()) ) return; 
282   // Only charged tracks
283   
284   // Only gas gap inside chamber
285   // Tag chambers and record hits when track enters 
286   static Int_t   idvol=-1;
287   Int_t   iChamber=0;
288   Int_t   id=0;
289   Int_t   copy;
290   const  Float_t kBig = 1.e10;
291
292
293   //
294   // Only gas gap inside chamber
295   // Tag chambers and record hits when track enters 
296   id=gMC->CurrentVolID(copy);
297   iChamber = GetChamberId(id);
298   idvol = iChamber -1;
299
300   if (idvol == -1) return;
301
302   // Filling TrackRefs file for MUON. Our Track references are the active volume of the chambers
303   if ( (gMC->IsTrackEntering() || gMC->IsTrackExiting() ) )     
304     AddTrackReference(gAlice->GetMCApp()->GetCurrentTrackNumber());
305   
306    if( gMC->IsTrackEntering() ) {
307      Float_t theta = fTrackMomentum.Theta();
308      if ((TMath::Pi()-theta)*kRaddeg>=15.) gMC->SetMaxStep(fStepMaxInActiveGas); // We use Pi-theta because z is negative
309   }
310
311    //  AliDebug(1,
312    //    Form("Active volume found %d chamber %d Z chamber is %f ",idvol,iChamber,
313    //    ( (AliMUONChamber*)(*fChambers)[idvol])->Z()));
314    // Particule id and mass, 
315   Int_t     ipart = gMC->TrackPid();
316   Float_t   mass  = gMC->TrackMass();
317
318   fDestepSum[idvol]+=gMC->Edep();
319   // Get current particle id (ipart), track position (pos)  and momentum (mom)
320   if ( fStepSum[idvol]==0.0 )  gMC->TrackMomentum(fTrackMomentum);
321   fStepSum[idvol]+=gMC->TrackStep();
322   
323   //  if(AliDebugLevel()) {
324   //  AliDebug(1,
325   //         Form("iChamber %d, Particle %d, theta %f phi %f mass %f StepSum %f eloss %g",
326   //              iChamber,ipart, fTrackMomentum.Theta()*kRaddeg, fTrackMomentum.Phi()*kRaddeg, 
327   //              mass, fStepSum[idvol], gMC->Edep()));
328   //  AliDebug(1,
329   //         Form("Track Momentum %f %f %f", fTrackMomentum.X(), fTrackMomentum.Y(), 
330   //              fTrackMomentum.Z()));
331   //  gMC->TrackPosition(fTrackPosition);
332   //  AliDebug(1,
333   //         Form("Track Position %f %f %f",fTrackPosition.X(),fTrackPosition.Y(),
334   //      fTrackPosition.Z())) ;
335   //   }
336
337   // Track left chamber or StepSum larger than fStepMaxInActiveGas
338   if ( gMC->IsTrackExiting() || 
339        gMC->IsTrackStop() || 
340        gMC->IsTrackDisappeared()||
341        (fStepSum[idvol]>fStepMaxInActiveGas) ) {
342     
343     if   ( gMC->IsTrackExiting() || 
344            gMC->IsTrackStop() || 
345            gMC->IsTrackDisappeared() ) gMC->SetMaxStep(kBig);
346
347     gMC->TrackPosition(fTrackPosition);
348     Float_t theta = fTrackMomentum.Theta();
349     Float_t phi   = fTrackMomentum.Phi();
350     
351     TLorentzVector backToWire( fStepSum[idvol]/2.*sin(theta)*cos(phi),
352                                fStepSum[idvol]/2.*sin(theta)*sin(phi),
353                                fStepSum[idvol]/2.*cos(theta),0.0       );
354     //     AliDebug(1,Form("Exit: Track Position %f %f %f",fTrackPosition.X(),
355     //                     fTrackPosition.Y(),fTrackPosition.Z())) ;
356     //     AliDebug(1,Form("Exit: Track backToWire %f %f %f",backToWire.X(),
357     //                     backToWire.Y(),backToWire.Z()) ;
358     fTrackPosition-=backToWire;
359     
360     //-------------- Angle effect 
361     // Ratio between energy loss of particle and Mip as a function of BetaGamma of particle (Energy/Mass)
362     
363     Float_t betaxGamma    = fTrackMomentum.P()/mass;//  pc/mc2
364     Float_t sigmaEffect10degrees;
365     Float_t sigmaEffectThetadegrees;
366     Float_t eLossParticleELossMip;
367     Float_t yAngleEffect=0.;
368     Float_t thetawires      =  TMath::Abs( TMath::ASin( TMath::Sin(TMath::Pi()-theta) * TMath::Sin(phi) ) );// We use Pi-theta because z is negative
369
370
371     if (fAngleEffect){
372     if ( (betaxGamma >3.2)   &&  (thetawires*kRaddeg<=15.) ) {
373       betaxGamma=TMath::Log(betaxGamma);
374       eLossParticleELossMip = fElossRatio->Eval(betaxGamma);
375       // 10 degrees is a reference for a model (arbitrary)
376       sigmaEffect10degrees=fAngleEffect10->Eval(eLossParticleELossMip);// in micrometers
377       // Angle with respect to the wires assuming that chambers are perpendicular to the z axis.
378       sigmaEffectThetadegrees =  sigmaEffect10degrees/fAngleEffectNorma->Eval(thetawires*kRaddeg);  // For 5mm gap  
379       if ( (iChamber==1)  ||  (iChamber==2) )  
380         sigmaEffectThetadegrees/=(1.09833e+00+1.70000e-02*(thetawires*kRaddeg)); // The gap is different (4mm)
381       yAngleEffect=1.e-04*gRandom->Gaus(0,sigmaEffectThetadegrees); // Error due to the angle effect in cm
382     }
383     }
384     
385     // Detection elements ids
386     const AliMUONGeometryModule* kGeometryModule
387       = GetGeometry()->GetModule(iChamber-1);
388
389     AliMUONGeometryDetElement* detElement
390       = kGeometryModule->FindBySensitiveVolume(CurrentVolumePath());
391
392     Int_t detElemId = 0;
393     if (detElement) detElemId = detElement->GetUniqueID(); 
394  
395     if (!detElemId) {
396       cerr << "Chamber id: "
397            << setw(3) << iChamber << "  "
398            << "Current SV: " 
399            <<  CurrentVolumePath() 
400            << "  detElemId: "
401            << setw(5) << detElemId 
402            << endl;
403       Double_t x, y, z;
404       gMC->TrackPosition(x, y, z);         
405       cerr << "   global position: "
406            << x << ", " << y << ", " << z
407            << endl;
408       AliWarning("DetElemId not identified.");
409     }  
410     
411     // One hit per chamber
412     GetMUONData()->AddHit(fIshunt, 
413                           gAlice->GetMCApp()->GetCurrentTrackNumber(), 
414                           iChamber, ipart,
415                           fTrackPosition.X(), 
416                           fTrackPosition.Y()+yAngleEffect, 
417                           fTrackPosition.Z(), 
418                           gMC->TrackTime(),
419                           fTrackMomentum.P(),
420                           theta, 
421                           phi, 
422                           fStepSum[idvol], 
423                           fDestepSum[idvol],                        
424                           fTrackPosition.X(),
425                           fTrackPosition.Y(),
426                           fTrackPosition.Z());
427
428 //     if (AliDebugLevel()){
429 //       AliDebug(1,Form("Exit: Particle exiting from chamber %d",iChamber));
430 //       AliDebug(1,Form("Exit: StepSum %f eloss geant %g ",fStepSum[idvol],fDestepSum[idvol]));
431 //       AliDebug(1,Form("Exit: Track Position %f %f %f",fTrackPosition.X(),fTrackPosition.Y(),fTrackPosition.Z())) ;
432 //     }
433     fStepSum[idvol]  =0; // Reset for the next event
434     fDestepSum[idvol]=0; // Reset for the next event
435   }
436 }
437
438 //_______________________________________________________________________________
439 void AliMUONv1::StepManager2()
440 {
441 /// Step manager for the chambers
442
443   // Only charged tracks
444   if( !(gMC->TrackCharge()) ) return; 
445   // Only charged tracks
446   
447   // Only gas gap inside chamber
448   // Tag chambers and record hits when track enters 
449   static Int_t   idvol=-1, iEnter = 0;
450   Int_t   iChamber=0;
451   Int_t   id=0;
452   Int_t   copy;
453   const  Float_t kBig = 1.e10;
454   static Double_t xyzEnter[3];
455
456   //
457   // Only gas gap inside chamber
458   // Tag chambers and record hits when track enters 
459   id=gMC->CurrentVolID(copy);
460   iChamber = GetChamberId(id);
461   idvol = iChamber -1;
462
463   if (idvol == -1) return;
464   
465   // Filling TrackRefs file for MUON. Our Track references are the active volume of the chambers
466   if ( (gMC->IsTrackEntering() || gMC->IsTrackExiting() ) )     
467     AddTrackReference(gAlice->GetMCApp()->GetCurrentTrackNumber());
468   
469    if( gMC->IsTrackEntering() ) {
470      Float_t theta = fTrackMomentum.Theta();
471      if ((TMath::Pi()-theta)*kRaddeg>=15.) gMC->SetMaxStep(fStepMaxInActiveGas); // We use Pi-theta because z is negative
472      iEnter = 1;
473      gMC->TrackPosition(xyzEnter[0], xyzEnter[1], xyzEnter[2]); // save coordinates of entrance point
474   }
475
476    //   AliDebug(1,
477    //       Form("Active volume found %d chamber %d Z chamber is %f ",idvol,iChamber,
478    //            ( (AliMUONChamber*)(*fChambers)[idvol])->Z())) ;
479   // Particule id and mass, 
480   Int_t     ipart = gMC->TrackPid();
481   Float_t   mass  = gMC->TrackMass();
482
483   fDestepSum[idvol]+=gMC->Edep();
484   // Get current particle id (ipart), track position (pos)  and momentum (mom)
485   if ( fStepSum[idvol]==0.0 )  gMC->TrackMomentum(fTrackMomentum);
486   fStepSum[idvol]+=gMC->TrackStep();
487   
488   //  if (AliDebugLevel()) {
489   //   AliDebug(1,Form("Step, iChamber %d, Particle %d, theta %f phi %f mass %f StepSum %f eloss %g",
490   //                 iChamber,ipart, fTrackMomentum.Theta()*kRaddeg, fTrackMomentum.Phi()*kRaddeg,
491   //         mass, fStepSum[idvol], gMC->Edep()));
492   // AliDebug(1,Form("Step:Track Momentum %f %f %f", fTrackMomentum.X(), fTrackMomentum.Y(), 
493   //         fTrackMomentum.Z()));
494   // gMC->TrackPosition(fTrackPosition);
495   // AliDebug(1,Form("Step: Track Position %f %f %f",fTrackPosition.X(),
496   //         fTrackPosition.Y(),fTrackPosition.Z())) ;
497   //}
498
499   // Track left chamber or StepSum larger than fStepMaxInActiveGas
500   if ( gMC->IsTrackExiting() || 
501        gMC->IsTrackStop() || 
502        gMC->IsTrackDisappeared()||
503        (fStepSum[idvol]>fStepMaxInActiveGas) ) {
504     
505     if   ( gMC->IsTrackExiting() || 
506            gMC->IsTrackStop() || 
507            gMC->IsTrackDisappeared() ) gMC->SetMaxStep(kBig);
508     if (fDestepSum[idvol] == 0) {
509       // AZ - no energy release
510       fStepSum[idvol] = 0; // Reset for the next event
511       iEnter = 0;
512       return; 
513     }
514
515     gMC->TrackPosition(fTrackPosition);
516     Float_t theta = fTrackMomentum.Theta();
517     Float_t phi   = fTrackMomentum.Phi();
518     
519     Int_t merge = 0;
520     Double_t xyz0[3]={0}, xyz1[3]={0}, tmp[3]={0};
521     if (gMC->IsTrackExiting() && iEnter != 0) {
522       // AZ - this code is to avoid artificial hit splitting at the
523       // "fake" boundary inside the same chamber. It will still produce 
524       // 2 hits but with the same coordinates (at the wire) to allow 
525       // their merging at the digitization level.
526
527       // Only for a track going from the entrance to the exit from the volume
528       // Get local coordinates
529       gMC->Gmtod(xyzEnter, xyz0, 1); // local coord. at the entrance
530
531       fTrackPosition.Vect().GetXYZ(tmp);
532       gMC->Gmtod(tmp, xyz1, 1); // local coord. at the exit
533       Float_t dx = xyz0[0] - xyz1[0];
534       Float_t dy = xyz0[1] - xyz1[1];
535       Float_t thLoc = TMath::ATan2 (TMath::Sqrt(dx*dx+dy*dy), TMath::Abs(xyz0[2]-xyz1[2]));
536       if (thLoc * TMath::RadToDeg() < 15) merge = 1; 
537     }
538
539     if (merge) {
540       Double_t dz = -0.5;
541       if (xyz1[2] != xyz0[2]) dz = xyz0[2] / (xyz1[2] - xyz0[2]);
542       tmp[0] = xyz0[0] - (xyz1[0] - xyz0[0]) * dz; // local coord. at the wire
543       tmp[1] = xyz0[1] - (xyz1[1] - xyz0[1]) * dz;
544       tmp[2] = xyz0[2] - (xyz1[2] - xyz0[2]) * dz;
545       gMC->Gdtom(tmp, xyz1, 1); // global coord. at the wire
546       fTrackPosition.SetXYZT(xyz1[0], xyz1[1], xyz1[2], fTrackPosition.T());
547     } else {
548       TLorentzVector backToWire( fStepSum[idvol]/2.*sin(theta)*cos(phi),
549                                  fStepSum[idvol]/2.*sin(theta)*sin(phi),
550                                  fStepSum[idvol]/2.*cos(theta),0.0       );
551       fTrackPosition-=backToWire;
552       //printf(" %d %d %d %f %d \n", gMC->IsTrackExiting(), gMC->IsTrackStop(), gMC->IsTrackDisappeared(), fStepSum[idvol], iEnter);
553       //    AliDebug(1,
554       //     Form("Track Position %f %f %f",fTrackPosition.X(),fTrackPosition.Y(),fTrackPosition.Z()));
555       // AliDebug(1,
556       //     Form("Exit: Track backToWire %f %f %f",backToWire.X(),backToWire.Y(),backToWire.Z())) ;
557     }
558     
559     //-------------- Angle effect 
560     // Ratio between energy loss of particle and Mip as a function of BetaGamma of particle (Energy/Mass)
561     
562     Float_t betaxGamma    = fTrackMomentum.P()/mass;//  pc/mc2
563     Float_t sigmaEffect10degrees;
564     Float_t sigmaEffectThetadegrees;
565     Float_t eLossParticleELossMip;
566     Float_t yAngleEffect=0.;
567     Float_t thetawires      =  TMath::Abs( TMath::ASin( TMath::Sin(TMath::Pi()-theta) * TMath::Sin(phi) ) );// We use Pi-theta because z is negative
568
569
570     if (fAngleEffect){
571     if ( (betaxGamma >3.2)   &&  (thetawires*kRaddeg<=15.) ) {
572       betaxGamma=TMath::Log(betaxGamma);
573       eLossParticleELossMip = fElossRatio->Eval(betaxGamma);
574       // 10 degrees is a reference for a model (arbitrary)
575       sigmaEffect10degrees=fAngleEffect10->Eval(eLossParticleELossMip);// in micrometers
576       // Angle with respect to the wires assuming that chambers are perpendicular to the z axis.
577       sigmaEffectThetadegrees =  sigmaEffect10degrees/fAngleEffectNorma->Eval(thetawires*kRaddeg);  // For 5mm gap  
578       if ( (iChamber==1)  ||  (iChamber==2) )  
579         sigmaEffectThetadegrees/=(1.09833e+00+1.70000e-02*(thetawires*kRaddeg)); // The gap is different (4mm)
580       yAngleEffect=1.e-04*gRandom->Gaus(0,sigmaEffectThetadegrees); // Error due to the angle effect in cm
581     }
582     }
583     
584     // Detection elements ids
585     const AliMUONGeometryModule* kGeometryModule
586       = GetGeometry()->GetModule(iChamber-1);
587
588     AliMUONGeometryDetElement* detElement
589       = kGeometryModule->FindBySensitiveVolume(CurrentVolumePath());
590
591     Int_t detElemId = 0;
592     if (detElement) detElemId = detElement->GetUniqueID(); 
593  
594     if (!detElemId) {
595       cerr << "Chamber id: "
596            << setw(3) << iChamber << "  "
597            << "Current SV: " 
598            <<  CurrentVolumePath() 
599            << "  detElemId: "
600            << setw(5) << detElemId 
601            << endl;
602       Double_t x, y, z;
603       gMC->TrackPosition(x, y, z);         
604       cerr << "   global position: "
605            << x << ", " << y << ", " << z
606            << endl;
607       AliError("DetElemId not identified.");
608     }  
609     
610     // One hit per chamber
611     GetMUONData()->AddHit2(fIshunt, 
612                           gAlice->GetMCApp()->GetCurrentTrackNumber(), 
613                           detElemId, ipart,
614                           fTrackPosition.X(), 
615                           fTrackPosition.Y()+yAngleEffect, 
616                           fTrackPosition.Z(), 
617                           gMC->TrackTime(),
618                           fTrackMomentum.P(),
619                           theta, 
620                           phi, 
621                           fStepSum[idvol], 
622                           fDestepSum[idvol],                        
623                           fTrackPosition.X(),
624                           fTrackPosition.Y(),
625                           fTrackPosition.Z());
626
627     //       AliDebug(1,Form("Exit: Particle exiting from chamber %d",iChamber));
628     //       AliDebug(1,Form("Exit: StepSum %f eloss geant %g ",fStepSum[idvol],fDestepSum[idvol]));
629     //       AliDebug(1,Form("Exit: Track Position %f %f %f",fTrackPosition.X(),fTrackPosition.Y(),fTrackPosition.Z()) ;
630
631     fStepSum[idvol]  =0; // Reset for the next event
632     fDestepSum[idvol]=0; // Reset for the next event
633     iEnter = 0;
634   }
635 }