]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONv1.cxx
Cosmetics (Chrsitian)
[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 1    //
20 /////////////////////////////////////////////////////////
21
22 #include <TRandom.h>
23 #include <TF1.h>
24 #include <TClonesArray.h>
25 #include <TRandom.h> 
26 #include <TVirtualMC.h>
27 #include <TGeoMatrix.h>
28
29 #include "AliMUONv1.h"
30 #include "AliConst.h" 
31 #include "AliMUONChamber.h"
32 #include "AliMUONConstants.h"
33 #include "AliMUONFactoryV2.h"
34 #include "AliMUONHit.h"
35 #include "AliMUONTriggerCircuit.h"
36 #include "AliMUONGeometryBuilder.h"     
37 #include "AliMUONGeometryModule.h"      
38 #include "AliMUONGeometrySVMap.h"       
39 #include "AliMUONGeometryDetElement.h"  
40 #include "AliMagF.h"
41 #include "AliRun.h"
42 #include "AliMC.h"
43 #include "AliLog.h"
44
45 ClassImp(AliMUONv1)
46  
47 //___________________________________________
48 AliMUONv1::AliMUONv1() 
49   : AliMUON(),
50     fStepManagerVersionOld(kFALSE),
51     fStepManagerVersionDE(kFALSE),
52     fAngleEffect(kTRUE),
53     fStepMaxInActiveGas(0.6),
54     fStepSum(0x0),
55     fDestepSum(0x0),
56     fTrackMomentum(), 
57     fTrackPosition(),
58     fElossRatio(0x0),
59     fAngleEffect10(0x0),
60     fAngleEffectNorma(0x0)
61 {
62 // Default constructor
63
64
65 //___________________________________________
66 AliMUONv1::AliMUONv1(const char *name, const char *title)
67   : AliMUON(name,title), 
68     fStepManagerVersionOld(kFALSE),
69     fStepManagerVersionDE(kFALSE),
70     fAngleEffect(kTRUE),
71     fStepMaxInActiveGas(0.6),
72     fStepSum(0x0),
73     fDestepSum(0x0),
74     fTrackMomentum(), 
75     fTrackPosition(),
76     fElossRatio(0x0),
77     fAngleEffect10(0x0),
78     fAngleEffectNorma(0x0)
79 {
80 // Standard onstructor
81
82     // By default include all stations
83
84     fStepSum   = new Float_t [AliMUONConstants::NCh()];
85     fDestepSum = new Float_t [AliMUONConstants::NCh()];
86     for (Int_t i=0; i<AliMUONConstants::NCh(); i++) {
87       fStepSum[i] =0.0;
88       fDestepSum[i]=0.0;
89     }
90     // Ratio of particle mean eloss with respect MIP's Khalil Boudjemline, sep 2003, PhD.Thesis and Particle Data Book
91     fElossRatio = new TF1("ElossRatio","[0]+[1]*x+[2]*x*x+[3]*x*x*x+[4]*x*x*x*x",0.5,5.); 
92     fElossRatio->SetParameter(0,1.02138);
93     fElossRatio->SetParameter(1,-9.54149e-02);
94     fElossRatio->SetParameter(2,+7.83433e-02); 
95     fElossRatio->SetParameter(3,-9.98208e-03);
96     fElossRatio->SetParameter(4,+3.83279e-04);
97
98     // Angle effect in tracking chambers at theta =10 degres as a function of ElossRatio (Khalil BOUDJEMLINE sep 2003 Ph.D Thesis) (in micrometers)
99     fAngleEffect10 = new TF1("AngleEffect10","[0]+[1]*x+[2]*x*x",0.5,3.0);
100     fAngleEffect10->SetParameter(0, 1.90691e+02);
101     fAngleEffect10->SetParameter(1,-6.62258e+01);
102     fAngleEffect10->SetParameter(2,+1.28247e+01);
103     // Angle effect: Normalisation form theta=10 degres to theta between 0 and 10 (Khalil BOUDJEMLINE sep 2003 Ph.D Thesis)  
104     // Angle with respect to the wires assuming that chambers are perpendicular to the z axis.
105     fAngleEffectNorma = new TF1("AngleEffectNorma","[0]+[1]*x+[2]*x*x+[3]*x*x*x",0.0,10.0);
106     fAngleEffectNorma->SetParameter(0,4.148);
107     fAngleEffectNorma->SetParameter(1,-6.809e-01);
108     fAngleEffectNorma->SetParameter(2,5.151e-02);
109     fAngleEffectNorma->SetParameter(3,-1.490e-03);
110 }
111
112 //_____________________________________________________________________________
113 AliMUONv1::AliMUONv1(const AliMUONv1& right) 
114   : AliMUON(right) 
115 {  
116   // copy constructor (not implemented)
117
118   AliFatal("Copy constructor not provided.");
119 }
120
121 //___________________________________________
122 AliMUONv1::~AliMUONv1()
123 {
124 // Destructor
125
126   delete [] fStepSum;
127   delete [] fDestepSum;
128   delete fElossRatio;
129   delete fAngleEffect10;
130   delete fAngleEffectNorma; 
131 }
132
133 //_____________________________________________________________________________
134 AliMUONv1& AliMUONv1::operator=(const AliMUONv1& right)
135 {
136   // assignement operator (not implemented)
137
138   // check assignement to self
139   if (this == &right) return *this;
140
141   AliFatal("Assignement operator not provided.");
142     
143   return *this;  
144 }    
145
146 //__________________________________________________
147 void AliMUONv1::CreateGeometry()
148 {
149 //
150 // Construct geometry using geometry builder
151 //
152
153   fGeometryBuilder->CreateGeometry();
154 }
155
156 //________________________________________________________________
157 void AliMUONv1::CreateMaterials()
158 {
159 //
160 // Construct materials using geometry builder
161 //
162
163   fGeometryBuilder->CreateMaterials();
164 }
165
166 //___________________________________________
167 void AliMUONv1::Init()
168
169    AliDebug(1,"Start Init for version 1 - CPC chamber type");
170    Int_t i;
171
172  
173    //
174    // Initialize geometry
175    //
176    fGeometryBuilder->InitGeometry();
177    AliDebug(1,"Finished Init for version 1 - CPC chamber type");   
178
179    if (fSegmentationType == 1) {
180      fFactory = new AliMUONFactory("Old MUON Factory");
181      printf("\n Old Segmentation \n");
182    }
183
184    if (fSegmentationType == 2) {
185      fFactory = new AliMUONFactoryV2("New MUON Factory");
186      printf("\n New Segmentation \n");
187    } 
188
189    fFactory->Build(this, "default");
190
191    //
192    // Initialize segmentation
193    //
194    if (!fSegmentationType) {
195      AliFatal("No Segmentation Type defined.");
196      return;
197    }
198
199    if (fSegmentationType == 1) {
200    for (i=0; i<AliMUONConstants::NCh(); i++) 
201        ( (AliMUONChamber*) (*fChambers)[i])->Init();
202    }
203
204    if (fSegmentationType == 2) {
205      for (i=0; i<AliMUONConstants::NCh(); i++) 
206        ( (AliMUONChamber*) (*fChambers)[i])->Init(fSegmentationType);// new segmentation
207    }
208  
209    // trigger circuit
210    // cp 
211    for (i=0; i<AliMUONConstants::NTriggerCircuit(); i++) 
212      ( (AliMUONTriggerCircuit*) (*fTriggerCircuits)[i])->Init(i);
213    
214
215
216
217
218 }
219
220 //__________________________________________________________________
221 Int_t  AliMUONv1::GetChamberId(Int_t volId) const
222 {
223 // Check if the volume with specified  volId is a sensitive volume (gas) 
224 // of some chamber and returns the chamber number;
225 // if not sensitive volume - return 0.
226 // ---
227
228 /*
229   for (Int_t i = 1; i <= AliMUONConstants::NCh(); i++)
230     if (volId==((AliMUONChamber*)(*fChambers)[i-1])->GetGid()) return i;
231 */
232   for (Int_t i = 1; i <= AliMUONConstants::NCh(); i++)
233     if ( ((AliMUONChamber*)(*fChambers)[i-1])->IsSensId(volId) ) return i;
234
235   return 0;
236 }
237
238 //_______________________________________________________________________________
239 TString  AliMUONv1::CurrentVolumePath() const
240 {
241 // Returns current volume path
242 // (Could be removed when this function is available via gMC)
243 // ---       
244
245   TString path = "";
246   TString name;
247   Int_t copyNo;
248   Int_t imother = 0;
249   do {
250     name = gMC->CurrentVolOffName(imother);
251     gMC->CurrentVolOffID(imother++, copyNo);
252     TString add = "/";
253     add += name;
254     add += ".";
255     add += copyNo;
256     path.Insert(0,add); 
257   }
258   while ( name != TString("ALIC") );
259   
260   return path;  
261 }
262
263 //_______________________________________________________________________________
264 void AliMUONv1::StepManager()
265 {
266   // Stepmanager for the chambers
267   // TBR
268
269  if (fStepManagerVersionDE) {
270     StepManager2();
271     return;
272   }
273
274  if (fStepManagerVersionOld) {
275     StepManagerOld();
276     return;
277   }
278
279   // Only charged tracks
280   if( !(gMC->TrackCharge()) ) return; 
281   // Only charged tracks
282   
283   // Only gas gap inside chamber
284   // Tag chambers and record hits when track enters 
285   static Int_t   idvol=-1;
286   Int_t   iChamber=0;
287   Int_t   id=0;
288   Int_t   copy;
289   const  Float_t kBig = 1.e10;
290
291
292   //
293   // Only gas gap inside chamber
294   // Tag chambers and record hits when track enters 
295   id=gMC->CurrentVolID(copy);
296   iChamber = GetChamberId(id);
297   idvol = iChamber -1;
298
299   if (idvol == -1) return;
300
301   // Filling TrackRefs file for MUON. Our Track references are the active volume of the chambers
302   if ( (gMC->IsTrackEntering() || gMC->IsTrackExiting() ) )     
303     AddTrackReference(gAlice->GetMCApp()->GetCurrentTrackNumber());
304   
305    if( gMC->IsTrackEntering() ) {
306      Float_t theta = fTrackMomentum.Theta();
307      if ((TMath::Pi()-theta)*kRaddeg>=15.) gMC->SetMaxStep(fStepMaxInActiveGas); // We use Pi-theta because z is negative
308   }
309
310 //  if (GetDebug()) {
311 //     Float_t z = ( (AliMUONChamber*)(*fChambers)[idvol])->Z() ;
312 //      Info("StepManager Step","Active volume found %d chamber %d Z chamber is %f ",idvol,iChamber, z);
313 //   }  
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 (GetDebug()) {
324 //     Info("StepManager Step","iChamber %d, Particle %d, theta %f phi %f mass %f StepSum %f eloss %g",
325 //       iChamber,ipart, fTrackMomentum.Theta()*kRaddeg, fTrackMomentum.Phi()*kRaddeg, mass, fStepSum[idvol], gMC->Edep());//     Info("StepManager Step","Track Momentum %f %f %f", fTrackMomentum.X(), fTrackMomentum.Y(), fTrackMomentum.Z()) ;
326 //     gMC->TrackPosition(fTrackPosition);
327 //     Info("StepManager Step","Track Position %f %f %f",fTrackPosition.X(),fTrackPosition.Y(),fTrackPosition.Z()) ;
328 //   }
329
330   // Track left chamber or StepSum larger than fStepMaxInActiveGas
331   if ( gMC->IsTrackExiting() || 
332        gMC->IsTrackStop() || 
333        gMC->IsTrackDisappeared()||
334        (fStepSum[idvol]>fStepMaxInActiveGas) ) {
335     
336     if   ( gMC->IsTrackExiting() || 
337            gMC->IsTrackStop() || 
338            gMC->IsTrackDisappeared() ) gMC->SetMaxStep(kBig);
339
340     gMC->TrackPosition(fTrackPosition);
341     Float_t theta = fTrackMomentum.Theta();
342     Float_t phi   = fTrackMomentum.Phi();
343     
344     TLorentzVector backToWire( fStepSum[idvol]/2.*sin(theta)*cos(phi),
345                                fStepSum[idvol]/2.*sin(theta)*sin(phi),
346                                fStepSum[idvol]/2.*cos(theta),0.0       );
347     //     if (GetDebug()) 
348     //       Info("StepManager Exit","Track Position %f %f %f",fTrackPosition.X(),fTrackPosition.Y(),fTrackPosition.Z()) ;
349     //     if (GetDebug()) 
350     //        Info("StepManager Exit ","Track backToWire %f %f %f",backToWire.X(),backToWire.Y(),backToWire.Z()) ;
351     fTrackPosition-=backToWire;
352     
353     //-------------- Angle effect 
354     // Ratio between energy loss of particle and Mip as a function of BetaGamma of particle (Energy/Mass)
355     
356     Float_t betaxGamma    = fTrackMomentum.P()/mass;//  pc/mc2
357     Float_t sigmaEffect10degrees;
358     Float_t sigmaEffectThetadegrees;
359     Float_t eLossParticleELossMip;
360     Float_t yAngleEffect=0.;
361     Float_t thetawires      =  TMath::Abs( TMath::ASin( TMath::Sin(TMath::Pi()-theta) * TMath::Sin(phi) ) );// We use Pi-theta because z is negative
362
363
364     if (fAngleEffect){
365     if ( (betaxGamma >3.2)   &&  (thetawires*kRaddeg<=15.) ) {
366       betaxGamma=TMath::Log(betaxGamma);
367       eLossParticleELossMip = fElossRatio->Eval(betaxGamma);
368       // 10 degrees is a reference for a model (arbitrary)
369       sigmaEffect10degrees=fAngleEffect10->Eval(eLossParticleELossMip);// in micrometers
370       // Angle with respect to the wires assuming that chambers are perpendicular to the z axis.
371       sigmaEffectThetadegrees =  sigmaEffect10degrees/fAngleEffectNorma->Eval(thetawires*kRaddeg);  // For 5mm gap  
372       if ( (iChamber==1)  ||  (iChamber==2) )  
373         sigmaEffectThetadegrees/=(1.09833e+00+1.70000e-02*(thetawires*kRaddeg)); // The gap is different (4mm)
374       yAngleEffect=1.e-04*gRandom->Gaus(0,sigmaEffectThetadegrees); // Error due to the angle effect in cm
375     }
376     }
377     
378     // Detection elements ids
379     AliMUONGeometryModule* geometry
380       = Chamber(iChamber-1).GetGeometry();
381
382     AliMUONGeometryDetElement* detElement
383       = geometry->FindBySensitiveVolume(CurrentVolumePath());
384
385     Int_t detElemId = 0;
386     if (detElement) detElemId = detElement->GetUniqueID(); 
387  
388     if (!detElemId) {
389       cerr << "Chamber id: "
390            << setw(3) << iChamber << "  "
391            << "Current SV: " 
392            <<  CurrentVolumePath() 
393            << "  detElemId: "
394            << setw(5) << detElemId 
395            << endl;
396       Double_t x, y, z;
397       gMC->TrackPosition(x, y, z);         
398       cerr << "   global position: "
399            << x << ", " << y << ", " << z
400            << endl;
401       AliWarning("DetElemId not identified.");
402     }  
403     
404     // One hit per chamber
405     GetMUONData()->AddHit(fIshunt, 
406                           gAlice->GetMCApp()->GetCurrentTrackNumber(), 
407                           iChamber, ipart,
408                           fTrackPosition.X(), 
409                           fTrackPosition.Y()+yAngleEffect, 
410                           fTrackPosition.Z(), 
411                           gMC->TrackTime(),
412                           fTrackMomentum.P(),
413                           theta, 
414                           phi, 
415                           fStepSum[idvol], 
416                           fDestepSum[idvol],                        
417                           fTrackPosition.X(),
418                           fTrackPosition.Y(),
419                           fTrackPosition.Z());
420
421 //     if (GetDebug()){
422 //       Info("StepManager Exit","Particle exiting from chamber %d",iChamber);
423 //       Info("StepManager Exit","StepSum %f eloss geant %g ",fStepSum[idvol],fDestepSum[idvol]);
424 //       Info("StepManager Exit","Track Position %f %f %f",fTrackPosition.X(),fTrackPosition.Y(),fTrackPosition.Z()) ;
425 //     }
426     fStepSum[idvol]  =0; // Reset for the next event
427     fDestepSum[idvol]=0; // Reset for the next event
428   }
429 }
430
431 //_______________________________________________________________________________
432 void AliMUONv1::StepManager2()
433 {
434   // Stepmanager for the chambers
435
436  if (fStepManagerVersionOld) {
437     StepManagerOld2();
438     return;
439   }
440
441   // Only charged tracks
442   if( !(gMC->TrackCharge()) ) return; 
443   // Only charged tracks
444   
445   // Only gas gap inside chamber
446   // Tag chambers and record hits when track enters 
447   static Int_t   idvol=-1;
448   Int_t   iChamber=0;
449   Int_t   id=0;
450   Int_t   copy;
451   const  Float_t kBig = 1.e10;
452
453
454   //
455   // Only gas gap inside chamber
456   // Tag chambers and record hits when track enters 
457   id=gMC->CurrentVolID(copy);
458   iChamber = GetChamberId(id);
459   idvol = iChamber -1;
460
461   if (idvol == -1) return;
462   
463   // Filling TrackRefs file for MUON. Our Track references are the active volume of the chambers
464   if ( (gMC->IsTrackEntering() || gMC->IsTrackExiting() ) )     
465     AddTrackReference(gAlice->GetMCApp()->GetCurrentTrackNumber());
466   
467    if( gMC->IsTrackEntering() ) {
468      Float_t theta = fTrackMomentum.Theta();
469      if ((TMath::Pi()-theta)*kRaddeg>=15.) gMC->SetMaxStep(fStepMaxInActiveGas); // We use Pi-theta because z is negative
470   }
471
472 //  if (GetDebug()) {
473 //     Float_t z = ( (AliMUONChamber*)(*fChambers)[idvol])->Z() ;
474 //      Info("StepManager Step","Active volume found %d chamber %d Z chamber is %f ",idvol,iChamber, z);
475 //   }  
476   // Particule id and mass, 
477   Int_t     ipart = gMC->TrackPid();
478   Float_t   mass  = gMC->TrackMass();
479
480   fDestepSum[idvol]+=gMC->Edep();
481   // Get current particle id (ipart), track position (pos)  and momentum (mom)
482   if ( fStepSum[idvol]==0.0 )  gMC->TrackMomentum(fTrackMomentum);
483   fStepSum[idvol]+=gMC->TrackStep();
484   
485 //   if (GetDebug()) {
486 //     Info("StepManager Step","iChamber %d, Particle %d, theta %f phi %f mass %f StepSum %f eloss %g",
487 //       iChamber,ipart, fTrackMomentum.Theta()*kRaddeg, fTrackMomentum.Phi()*kRaddeg, mass, fStepSum[idvol], gMC->Edep());//     Info("StepManager Step","Track Momentum %f %f %f", fTrackMomentum.X(), fTrackMomentum.Y(), fTrackMomentum.Z()) ;
488 //     gMC->TrackPosition(fTrackPosition);
489 //     Info("StepManager Step","Track Position %f %f %f",fTrackPosition.X(),fTrackPosition.Y(),fTrackPosition.Z()) ;
490 //   }
491
492   // Track left chamber or StepSum larger than fStepMaxInActiveGas
493   if ( gMC->IsTrackExiting() || 
494        gMC->IsTrackStop() || 
495        gMC->IsTrackDisappeared()||
496        (fStepSum[idvol]>fStepMaxInActiveGas) ) {
497     
498     if   ( gMC->IsTrackExiting() || 
499            gMC->IsTrackStop() || 
500            gMC->IsTrackDisappeared() ) gMC->SetMaxStep(kBig);
501
502     gMC->TrackPosition(fTrackPosition);
503     Float_t theta = fTrackMomentum.Theta();
504     Float_t phi   = fTrackMomentum.Phi();
505     
506     TLorentzVector backToWire( fStepSum[idvol]/2.*sin(theta)*cos(phi),
507                                fStepSum[idvol]/2.*sin(theta)*sin(phi),
508                                fStepSum[idvol]/2.*cos(theta),0.0       );
509     //     if (GetDebug()) 
510     //       Info("StepManager Exit","Track Position %f %f %f",fTrackPosition.X(),fTrackPosition.Y(),fTrackPosition.Z()) ;
511     //     if (GetDebug()) 
512     //        Info("StepManager Exit ","Track backToWire %f %f %f",backToWire.X(),backToWire.Y(),backToWire.Z()) ;
513     fTrackPosition-=backToWire;
514     
515     //-------------- Angle effect 
516     // Ratio between energy loss of particle and Mip as a function of BetaGamma of particle (Energy/Mass)
517     
518     Float_t betaxGamma    = fTrackMomentum.P()/mass;//  pc/mc2
519     Float_t sigmaEffect10degrees;
520     Float_t sigmaEffectThetadegrees;
521     Float_t eLossParticleELossMip;
522     Float_t yAngleEffect=0.;
523     Float_t thetawires      =  TMath::Abs( TMath::ASin( TMath::Sin(TMath::Pi()-theta) * TMath::Sin(phi) ) );// We use Pi-theta because z is negative
524
525
526     if (fAngleEffect){
527     if ( (betaxGamma >3.2)   &&  (thetawires*kRaddeg<=15.) ) {
528       betaxGamma=TMath::Log(betaxGamma);
529       eLossParticleELossMip = fElossRatio->Eval(betaxGamma);
530       // 10 degrees is a reference for a model (arbitrary)
531       sigmaEffect10degrees=fAngleEffect10->Eval(eLossParticleELossMip);// in micrometers
532       // Angle with respect to the wires assuming that chambers are perpendicular to the z axis.
533       sigmaEffectThetadegrees =  sigmaEffect10degrees/fAngleEffectNorma->Eval(thetawires*kRaddeg);  // For 5mm gap  
534       if ( (iChamber==1)  ||  (iChamber==2) )  
535         sigmaEffectThetadegrees/=(1.09833e+00+1.70000e-02*(thetawires*kRaddeg)); // The gap is different (4mm)
536       yAngleEffect=1.e-04*gRandom->Gaus(0,sigmaEffectThetadegrees); // Error due to the angle effect in cm
537     }
538     }
539     
540     // Detection elements ids
541     AliMUONGeometryModule* geometry
542       = Chamber(iChamber-1).GetGeometry();
543
544     AliMUONGeometryDetElement* detElement
545       = geometry->FindBySensitiveVolume(CurrentVolumePath());
546
547     Int_t detElemId = 0;
548     if (detElement) detElemId = detElement->GetUniqueID(); 
549  
550     if (!detElemId) {
551       cerr << "Chamber id: "
552            << setw(3) << iChamber << "  "
553            << "Current SV: " 
554            <<  CurrentVolumePath() 
555            << "  detElemId: "
556            << setw(5) << detElemId 
557            << endl;
558       Double_t x, y, z;
559       gMC->TrackPosition(x, y, z);         
560       cerr << "   global position: "
561            << x << ", " << y << ", " << z
562            << endl;
563       AliError("DetElemId not identified.");
564     }  
565     
566     // One hit per chamber
567     GetMUONData()->AddHit2(fIshunt, 
568                           gAlice->GetMCApp()->GetCurrentTrackNumber(), 
569                           detElemId, ipart,
570                           fTrackPosition.X(), 
571                           fTrackPosition.Y()+yAngleEffect, 
572                           fTrackPosition.Z(), 
573                           gMC->TrackTime(),
574                           fTrackMomentum.P(),
575                           theta, 
576                           phi, 
577                           fStepSum[idvol], 
578                           fDestepSum[idvol],                        
579                           fTrackPosition.X(),
580                           fTrackPosition.Y(),
581                           fTrackPosition.Z());
582
583 //     if (GetDebug()){
584 //       Info("StepManager Exit","Particle exiting from chamber %d",iChamber);
585 //       Info("StepManager Exit","StepSum %f eloss geant %g ",fStepSum[idvol],fDestepSum[idvol]);
586 //       Info("StepManager Exit","Track Position %f %f %f",fTrackPosition.X(),fTrackPosition.Y(),fTrackPosition.Z()) ;
587 //     }
588     fStepSum[idvol]  =0; // Reset for the next event
589     fDestepSum[idvol]=0; // Reset for the next event
590   }
591 }
592
593 //__________________________________________
594 void AliMUONv1::StepManagerOld()
595 {
596   // Old Stepmanager for the chambers
597   // TBR
598   
599   Int_t          copy, id;
600   static Int_t   idvol =-1;
601   static Int_t   vol[2];
602   Int_t          ipart;
603   TLorentzVector pos;
604   TLorentzVector mom;
605   Float_t        theta,phi;
606   Float_t        destep, step;
607   
608   static Float_t sstep;
609   static Float_t eloss, eloss2, xhit, yhit, zhit, tof, tlength;
610   const  Float_t kBig = 1.e10;
611   static Float_t hits[15];
612
613   TClonesArray &lhits = *fHits;
614
615   //
616   //
617   // Only charged tracks
618   if( !(gMC->TrackCharge()) ) return; 
619   //
620   // Only gas gap inside chamber
621   // Tag chambers and record hits when track enters 
622   id=gMC->CurrentVolID(copy);
623   vol[0] = GetChamberId(id);
624   idvol = vol[0] -1;
625
626   if (idvol == -1) return;
627
628   //
629   // Get current particle id (ipart), track position (pos)  and momentum (mom) 
630   gMC->TrackPosition(pos);
631   gMC->TrackMomentum(mom);
632
633   ipart  = gMC->TrackPid();
634
635   //
636   // momentum loss and steplength in last step
637   destep = gMC->Edep();
638   step   = gMC->TrackStep();
639   // cout<<"------------"<<step<<endl;
640   //
641   // record hits when track enters ...
642   if( gMC->IsTrackEntering()) {
643
644       gMC->SetMaxStep(fMaxStepGas);
645       Double_t tc = mom[0]*mom[0]+mom[1]*mom[1];
646       Double_t rt = TMath::Sqrt(tc);
647       Double_t pmom = TMath::Sqrt(tc+mom[2]*mom[2]);
648       Double_t tx = mom[0]/pmom;
649       Double_t ty = mom[1]/pmom;
650       Double_t tz = mom[2]/pmom;
651       Double_t s  = ((AliMUONChamber*)(*fChambers)[idvol])
652           ->ResponseModel()
653           ->Pitch()/tz;
654       theta   = Float_t(TMath::ATan2(rt,Double_t(mom[2])))*kRaddeg;
655       phi     = Float_t(TMath::ATan2(Double_t(mom[1]),Double_t(mom[0])))*kRaddeg;
656       hits[0] = Float_t(ipart);         // Geant3 particle type
657       hits[1] = pos[0]+s*tx;            // X-position for hit
658       hits[2] = pos[1]+s*ty;            // Y-position for hit
659       hits[3] = pos[2]+s*tz;            // Z-position for hit
660       hits[4] = theta;                  // theta angle of incidence
661       hits[5] = phi;                    // phi angle of incidence 
662       hits[8] = 0;//PadHits does not exist anymore  (Float_t) fNPadHits;    // first padhit
663       hits[9] = -1;                     // last pad hit
664       hits[10] = mom[3];                // hit momentum P
665       hits[11] = mom[0];                // Px
666       hits[12] = mom[1];                // Py
667       hits[13] = mom[2];                // Pz
668       tof=gMC->TrackTime();
669       hits[14] = tof;                   // Time of flight
670       tlength  = 0;
671       eloss    = 0;
672       eloss2   = 0;
673       sstep=0;
674       xhit     = pos[0];
675       yhit     = pos[1];      
676       zhit     = pos[2];      
677       Chamber(idvol).ChargeCorrelationInit();
678       // Only if not trigger chamber
679
680 //       printf("---------------------------\n");
681 //       printf(">>>> Y =  %f \n",hits[2]);
682 //       printf("---------------------------\n");
683     
684       
685
686      //  if(idvol < AliMUONConstants::NTrackingCh()) {
687 //        //
688 //        //  Initialize hit position (cursor) in the segmentation model 
689 //        ((AliMUONChamber*) (*fChambers)[idvol])
690 //            ->SigGenInit(pos[0], pos[1], pos[2]);
691 //       } else {
692 //        //geant3->Gpcxyz();
693 //        //printf("In the Trigger Chamber #%d\n",idvol-9);
694 //       }
695   }
696   eloss2+=destep;
697   sstep+=step;
698
699   // cout<<sstep<<endl;
700
701   // 
702   // Calculate the charge induced on a pad (disintegration) in case 
703   //
704   // Mip left chamber ...
705   if( gMC->IsTrackExiting() || gMC->IsTrackStop() || gMC->IsTrackDisappeared()){
706       gMC->SetMaxStep(kBig);
707       eloss   += destep;
708       tlength += step;
709       
710       Float_t x0,y0,z0;
711       Float_t localPos[3];
712       Float_t globalPos[3] = {pos[0], pos[1], pos[2]};
713       gMC->Gmtod(globalPos,localPos,1); 
714
715       if(idvol < AliMUONConstants::NTrackingCh()) {
716 // tracking chambers
717           x0 = 0.5*(xhit+pos[0]);
718           y0 = 0.5*(yhit+pos[1]);
719           z0 = 0.5*(zhit+pos[2]);
720       } else {
721 // trigger chambers
722           x0 = xhit;
723           y0 = yhit;
724           z0 = 0.;
725       }
726       
727
728       //      if (eloss >0)  MakePadHits(x0,y0,z0,eloss,tof,idvol);
729       
730           
731       hits[6] = tlength;   // track length
732       hits[7] = eloss2;    // de/dx energy loss
733
734
735       //      if (fNPadHits > (Int_t)hits[8]) {
736       //          hits[8] = hits[8]+1;
737       //          hits[9] = 0: // PadHits does not exist anymore (Float_t) fNPadHits;
738       //}
739 //
740 //    new hit 
741       
742       new(lhits[fNhits++]) 
743         AliMUONHit(fIshunt, gAlice->GetMCApp()->GetCurrentTrackNumber(), vol, hits, kTRUE);
744       eloss = 0; 
745       //
746       // Check additional signal generation conditions 
747       // defined by the segmentation
748       // model (boundary crossing conditions)
749       // only for tracking chambers
750   } else if 
751       ((idvol < AliMUONConstants::NTrackingCh()) &&
752        ((AliMUONChamber*) (*fChambers)[idvol])->SigGenCond(pos[0], pos[1], pos[2]))
753   {
754       ((AliMUONChamber*) (*fChambers)[idvol])
755           ->SigGenInit(pos[0], pos[1], pos[2]);
756       
757       Float_t localPos[3];
758       Float_t globalPos[3] = {pos[0], pos[1], pos[2]};
759       gMC->Gmtod(globalPos,localPos,1); 
760
761       eloss    += destep;
762
763       // if (eloss > 0 && idvol < AliMUONConstants::NTrackingCh())
764       //        MakePadHits(0.5*(xhit+pos[0]),0.5*(yhit+pos[1]),pos[2],eloss,tof,idvol);
765       xhit     = pos[0];
766       yhit     = pos[1]; 
767       zhit     = pos[2];
768       eloss = 0;
769       tlength += step ;
770       //
771       // nothing special  happened, add up energy loss
772   } else {        
773       eloss   += destep;
774       tlength += step ;
775   }
776 }
777
778 //__________________________________________
779 void AliMUONv1::StepManagerOld2()
780 {
781   // Old Stepmanager for the chambers
782   Int_t          copy, id;
783   static Int_t   idvol =-1;
784   static Int_t   vol[2];
785   Int_t          ipart;
786   TLorentzVector pos;
787   TLorentzVector mom;
788   Float_t        theta,phi;
789   Float_t        destep, step;
790   
791   static Float_t sstep;
792   static Float_t eloss, eloss2, xhit, yhit, zhit, tof, tlength;
793   const  Float_t kBig = 1.e10;
794   static Float_t hits[15];
795
796   TClonesArray &lhits = *fHits;
797
798   //
799   //
800   // Only charged tracks
801   if( !(gMC->TrackCharge()) ) return; 
802   //
803   // Only gas gap inside chamber
804   // Tag chambers and record hits when track enters 
805   id=gMC->CurrentVolID(copy);
806
807   Int_t iChamber = GetChamberId(id);
808   idvol =  iChamber-1;
809
810   if (idvol == -1) return;
811
812   // Detection elements id
813   AliMUONGeometryModule* geometry
814     = Chamber(iChamber-1).GetGeometry();
815
816   AliMUONGeometryDetElement* detElement
817     = geometry->FindBySensitiveVolume(CurrentVolumePath());
818
819   Int_t detElemId = 0;
820   if (detElement) detElemId = detElement->GetUniqueID(); 
821  
822   if (!detElemId) {
823     cerr << "Chamber id: "
824          << setw(3) << iChamber << "  "
825          << "Current SV: " 
826          <<  CurrentVolumePath() 
827          << "  detElemId: "
828          << setw(5) << detElemId 
829          << endl;
830     Double_t x, y, z;
831     gMC->TrackPosition(x, y, z);           
832     cerr << "   global position: "
833          << x << ", " << y << ", " << z
834          << endl;
835     AliError("DetElemId not identified.");
836   }  
837   vol[0] = detElemId;
838     
839   //
840   // Get current particle id (ipart), track position (pos)  and momentum (mom) 
841   gMC->TrackPosition(pos);
842   gMC->TrackMomentum(mom);
843
844   ipart  = gMC->TrackPid();
845
846   //
847   // momentum loss and steplength in last step
848   destep = gMC->Edep();
849   step   = gMC->TrackStep();
850   // cout<<"------------"<<step<<endl;
851   //
852   // record hits when track enters ...
853   if( gMC->IsTrackEntering()) {
854
855       gMC->SetMaxStep(fMaxStepGas);
856       Double_t tc = mom[0]*mom[0]+mom[1]*mom[1];
857       Double_t rt = TMath::Sqrt(tc);
858       Double_t pmom = TMath::Sqrt(tc+mom[2]*mom[2]);
859       Double_t tx = mom[0]/pmom;
860       Double_t ty = mom[1]/pmom;
861       Double_t tz = mom[2]/pmom;
862       Double_t s  = ((AliMUONChamber*)(*fChambers)[idvol])
863           ->ResponseModel()
864           ->Pitch()/tz;
865       theta   = Float_t(TMath::ATan2(rt,Double_t(mom[2])))*kRaddeg;
866       phi     = Float_t(TMath::ATan2(Double_t(mom[1]),Double_t(mom[0])))*kRaddeg;
867       hits[0] = Float_t(ipart);         // Geant3 particle type
868       hits[1] = pos[0]+s*tx;            // X-position for hit
869       hits[2] = pos[1]+s*ty;            // Y-position for hit
870       hits[3] = pos[2]+s*tz;            // Z-position for hit
871       hits[4] = theta;                  // theta angle of incidence
872       hits[5] = phi;                    // phi angle of incidence 
873       hits[8] = 0;//PadHits does not exist anymore  (Float_t) fNPadHits;    // first padhit
874       hits[9] = -1;                     // last pad hit
875       hits[10] = mom[3];                // hit momentum P
876       hits[11] = mom[0];                // Px
877       hits[12] = mom[1];                // Py
878       hits[13] = mom[2];                // Pz
879       tof=gMC->TrackTime();
880       hits[14] = tof;                   // Time of flight
881       tlength  = 0;
882       eloss    = 0;
883       eloss2   = 0;
884       sstep=0;
885       xhit     = pos[0];
886       yhit     = pos[1];      
887       zhit     = pos[2];      
888       Chamber(idvol).ChargeCorrelationInit();
889       // Only if not trigger chamber
890
891 //       printf("---------------------------\n");
892 //       printf(">>>> Y =  %f \n",hits[2]);
893 //       printf("---------------------------\n");
894     
895       
896
897      //  if(idvol < AliMUONConstants::NTrackingCh()) {
898 //        //
899 //        //  Initialize hit position (cursor) in the segmentation model 
900 //        ((AliMUONChamber*) (*fChambers)[idvol])
901 //            ->SigGenInit(pos[0], pos[1], pos[2]);
902 //       } else {
903 //        //geant3->Gpcxyz();
904 //        //printf("In the Trigger Chamber #%d\n",idvol-9);
905 //       }
906   }
907   eloss2+=destep;
908   sstep+=step;
909
910   // cout<<sstep<<endl;
911
912   // 
913   // Calculate the charge induced on a pad (disintegration) in case 
914   //
915   // Mip left chamber ...
916   if( gMC->IsTrackExiting() || gMC->IsTrackStop() || gMC->IsTrackDisappeared()){
917       gMC->SetMaxStep(kBig);
918       eloss   += destep;
919       tlength += step;
920       
921       Float_t x0,y0,z0;
922       Float_t localPos[3];
923       Float_t globalPos[3] = {pos[0], pos[1], pos[2]};
924       gMC->Gmtod(globalPos,localPos,1); 
925
926       if(idvol < AliMUONConstants::NTrackingCh()) {
927 // tracking chambers
928           x0 = 0.5*(xhit+pos[0]);
929           y0 = 0.5*(yhit+pos[1]);
930           z0 = 0.5*(zhit+pos[2]);
931       } else {
932 // trigger chambers
933           x0 = xhit;
934           y0 = yhit;
935           z0 = 0.;
936       }
937       
938
939       //      if (eloss >0)  MakePadHits(x0,y0,z0,eloss,tof,idvol);
940       
941           
942       hits[6] = tlength;   // track length
943       hits[7] = eloss2;    // de/dx energy loss
944
945
946       //      if (fNPadHits > (Int_t)hits[8]) {
947       //          hits[8] = hits[8]+1;
948       //          hits[9] = 0: // PadHits does not exist anymore (Float_t) fNPadHits;
949       //}
950 //
951 //    new hit 
952       
953       new(lhits[fNhits++]) 
954           AliMUONHit(fIshunt, gAlice->GetMCApp()->GetCurrentTrackNumber(), vol,hits,
955                      true);
956       eloss = 0; 
957       //
958       // Check additional signal generation conditions 
959       // defined by the segmentation
960       // model (boundary crossing conditions)
961       // only for tracking chambers
962   } else if 
963       ((idvol < AliMUONConstants::NTrackingCh()) &&
964        ((AliMUONChamber*) (*fChambers)[idvol])->SigGenCond(pos[0], pos[1], pos[2]))
965   {
966       ((AliMUONChamber*) (*fChambers)[idvol])
967           ->SigGenInit(pos[0], pos[1], pos[2]);
968       
969       Float_t localPos[3];
970       Float_t globalPos[3] = {pos[0], pos[1], pos[2]};
971       gMC->Gmtod(globalPos,localPos,1); 
972
973       eloss    += destep;
974
975       // if (eloss > 0 && idvol < AliMUONConstants::NTrackingCh())
976       //        MakePadHits(0.5*(xhit+pos[0]),0.5*(yhit+pos[1]),pos[2],eloss,tof,idvol);
977       xhit     = pos[0];
978       yhit     = pos[1]; 
979       zhit     = pos[2];
980       eloss = 0;
981       tlength += step ;
982       //
983       // nothing special  happened, add up energy loss
984   } else {        
985       eloss   += destep;
986       tlength += step ;
987   }
988 }