]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONv1.cxx
Raw2SDigits() restored and improved.
[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
25 #include "AliMUONv1.h"
26 #include "AliMUONConstants.h"
27 #include "AliMUONResponseFactory.h"
28 #include "AliMUONHit.h"
29 #include "AliMUONGeometryBuilder.h"     
30 #include "AliMUONGeometry.h"    
31 #include "AliMUONGeometryTransformer.h" 
32 #include "AliMUONGeometryModule.h"      
33 #include "AliMUONStringIntMap.h"        
34 #include "AliMUONGeometryDetElement.h"  
35
36 #include "AliMpCDB.h"
37 #include "AliMpDEManager.h"
38
39 #include "AliConst.h" 
40 #include "AliMagF.h"
41 #include "AliRun.h"
42 #include "AliMC.h"
43 #include "AliTrackReference.h"
44 #include "AliLog.h"
45
46 #include <TClonesArray.h>
47 #include <TF1.h>
48 #include <TF2.h>
49 #include <TGeoGlobalMagField.h>
50 #include <TGeoMatrix.h>
51 #include <TRandom.h>
52 #include <TRandom.h> 
53 #include <TVirtualMC.h>
54
55 #include <string>
56
57 #include "AliMUONVHitStore.h"
58
59 /// \cond CLASSIMP
60 ClassImp(AliMUONv1)
61 /// \endcond
62  
63 //___________________________________________
64 AliMUONv1::AliMUONv1() 
65   : AliMUON(),
66     fAngleEffect(kTRUE),
67     fMagEffect(kTRUE),
68     fStepMaxInActiveGas(0.6),
69     fStepSum(0x0),
70     fDestepSum(0x0),
71     fTrackMomentum(), 
72     fTrackPosition(),
73     fElossRatio(0x0),
74     fAngleEffect10(0x0),
75     fAngleEffectNorma(0x0),
76     fMagAngleEffectNorma(0x0)
77 {
78 /// Default constructor
79   
80   AliDebug(1,Form("default (empty) ctor this=%p",this));
81
82
83 //___________________________________________
84 AliMUONv1::AliMUONv1(const char *name, const char* title)
85 : AliMUON(name, title), 
86     fAngleEffect(kTRUE),
87     fMagEffect(kTRUE),
88     fStepMaxInActiveGas(0.6),
89     fStepSum(0x0),
90     fDestepSum(0x0),
91     fTrackMomentum(), 
92     fTrackPosition(),
93     fElossRatio(0x0),
94     fAngleEffect10(0x0),
95     fAngleEffectNorma(0x0),
96     fMagAngleEffectNorma(0x0)
97 {
98 /// Standard onstructor
99
100     AliDebug(1,Form("ctor this=%p",this));      
101         
102     // Load mapping
103     if ( ! AliMpCDB::LoadMpSegmentation() ) {
104       AliFatal("Could not access mapping from OCDB !");
105     }
106        
107     // By default include all stations
108
109     fStepSum   = new Float_t [AliMUONConstants::NCh()];
110     fDestepSum = new Float_t [AliMUONConstants::NCh()];
111     for (Int_t i=0; i<AliMUONConstants::NCh(); i++) {
112       fStepSum[i] =0.0;
113       fDestepSum[i]=0.0;
114     }
115     // Ratio of particle mean eloss with respect MIP's Khalil Boudjemline, sep 2003, PhD.Thesis and Particle Data Book
116     fElossRatio = new TF1("ElossRatio","[0]+[1]*x+[2]*x*x+[3]*x*x*x+[4]*x*x*x*x",0.5,5.); 
117     fElossRatio->SetParameter(0,1.02138);
118     fElossRatio->SetParameter(1,-9.54149e-02);
119     fElossRatio->SetParameter(2,+7.83433e-02); 
120     fElossRatio->SetParameter(3,-9.98208e-03);
121     fElossRatio->SetParameter(4,+3.83279e-04);
122
123     // Angle effect in tracking chambers at theta =10 degres as a function of ElossRatio (Khalil BOUDJEMLINE sep 2003 Ph.D Thesis) (in micrometers)
124     fAngleEffect10 = new TF1("AngleEffect10","[0]+[1]*x+[2]*x*x",0.5,3.0);
125     fAngleEffect10->SetParameter(0, 1.90691e+02);
126     fAngleEffect10->SetParameter(1,-6.62258e+01);
127     fAngleEffect10->SetParameter(2,+1.28247e+01);
128     // Angle effect: Normalisation form theta=10 degres to theta between 0 and 10 (Khalil BOUDJEMLINE sep 2003 Ph.D Thesis)  
129     // Angle with respect to the wires assuming that chambers are perpendicular to the z axis.
130     fAngleEffectNorma = new TF1("AngleEffectNorma","[0]+[1]*x+[2]*x*x+[3]*x*x*x",0.0,10.0);
131     fAngleEffectNorma->SetParameter(0,4.148);
132     fAngleEffectNorma->SetParameter(1,-6.809e-01);
133     fAngleEffectNorma->SetParameter(2,5.151e-02);
134     fAngleEffectNorma->SetParameter(3,-1.490e-03);
135
136     // Magnetic field effect: Normalisation form theta=16 degres (eq. 10 degrees B=0) to theta between -20 and 20 (Lamia Benhabib jun 2006 )  
137     // Angle with respect to the wires assuming that chambers are perpendicular to the z axis.
138     fMagAngleEffectNorma = new TF2("MagAngleEffectNorma","121.24/(([1]+[2]*abs(y))+[3]*abs(x-[0]*y)+[4]*abs((x-[0]*y)*(x-[0]*y))+[5]*abs((x-[0]*y)*(x-[0]*y)*(x-[0]*y))+[6]*abs((x-[0]*y)*(x-[0]*y)*(x-[0]*y)*(x-[0]*y)))",-20.0,20.0,-1.,1.);
139     fMagAngleEffectNorma->SetParameters(8.6995, 25.4022, 13.8822, 2.4717, 1.1551, -0.0624, 0.0012);
140 }
141
142 //___________________________________________
143 AliMUONv1::~AliMUONv1()
144 {
145 /// Destructor
146   
147   AliDebug(1,Form("dtor this=%p",this));
148   delete [] fStepSum;
149   delete [] fDestepSum;
150   delete fElossRatio;
151   delete fAngleEffect10;
152   delete fAngleEffectNorma; 
153   delete fMagAngleEffectNorma;
154 }
155
156 //__________________________________________________
157 void AliMUONv1::CreateGeometry()
158 {
159 /// Construct geometry using geometry builder
160
161   fGeometryBuilder->CreateGeometry();
162 }
163
164 //________________________________________________________________
165 void AliMUONv1::CreateMaterials()
166 {
167 /// Construct materials using geometry builder
168
169   fGeometryBuilder->CreateMaterials();
170 }
171
172 //________________________________________________________________
173 void AliMUONv1::AddAlignableVolumes() const
174 {
175 /// Construct materials using geometry builder
176
177   GetGeometryTransformer()->AddAlignableVolumes();
178 }
179
180
181 //___________________________________________
182 void AliMUONv1::Init()
183
184 /// Initialize geometry
185
186   AliDebug(1,"Start Init for version 1 - CPC chamber type");
187    
188   fGeometryBuilder->InitGeometry();
189   AliDebug(1,"Finished Init for version 1 - CPC chamber type");   
190  
191
192   // Build segmentation
193   // using geometry parametrisation
194   //
195   // Build response
196   //
197   AliMUONResponseFactory respFactory("default", fIsTailEffect);
198   respFactory.Build(this);
199   
200 }
201
202 //__________________________________________________________________
203 Int_t  AliMUONv1::GetGeomModuleId(Int_t volId) const
204 {
205 /// Check if the volume with specified  volId is a sensitive volume (gas) 
206 /// of some chamber and return the chamber number;
207 /// if not sensitive volume - return 0.
208
209   for (Int_t i = 0; i < AliMUONConstants::NGeomModules(); i++) {
210     if ( GetGeometry()->GetModule(i)->IsSensitiveVolume(volId) )
211       return i;
212   }    
213
214   return -1;
215 }
216
217 //_______________________________________________________________________________
218 TString  AliMUONv1::CurrentVolumePath() const
219 {
220 /// Return current volume path
221 /// (Could be removed when this function is available via gMC)
222
223   TString path = "";
224   TString name;
225   Int_t copyNo;
226   Int_t imother = 0;
227   do {
228     name = gMC->CurrentVolOffName(imother);
229     gMC->CurrentVolOffID(imother++, copyNo);
230     TString add = "/";
231     add += name;
232     add += "_";
233     add += copyNo;
234     path.Insert(0,add); 
235   }
236   while ( name != TString("ALIC") );
237   
238   return path;  
239 }
240
241 //_______________________________________________________________________________
242 void AliMUONv1::StepManager()
243 {
244 /// Step manager for the chambers
245
246   // Only charged tracks
247   if( !(gMC->TrackCharge()) ) return; 
248   // Only charged tracks
249   
250   // Only gas gap inside chamber
251   // Tag chambers and record hits when track enters 
252   static Int_t   idvol=-1, iEnter = 0;
253   Int_t   copy;
254   const  Float_t kBig = 1.e10;
255   static Double_t xyzEnter[3];
256
257   //
258   // Only gas gap inside chamber
259   // Tag chambers and record hits when track enters 
260   Int_t id=gMC->CurrentVolID(copy);
261   Int_t iGeomModule = GetGeomModuleId(id);
262   if (iGeomModule == -1) return;
263
264   // Detection elements id
265   const AliMUONGeometryModule* kGeometryModule
266     = GetGeometry()->GetModule(iGeomModule);
267   AliMUONGeometryDetElement* detElement
268     = kGeometryModule->FindBySensitiveVolume(CurrentVolumePath());
269     
270   if (!detElement && iGeomModule < AliMUONConstants::NGeomModules()-2) {
271     iGeomModule++;
272     const AliMUONGeometryModule* kGeometryModule2
273       = GetGeometry()->GetModule(iGeomModule);
274     detElement 
275       = kGeometryModule2->FindBySensitiveVolume(CurrentVolumePath());
276   }    
277
278   Int_t detElemId = 0;
279   if (detElement) detElemId = detElement->GetUniqueID(); 
280  
281   if (!detElemId) {
282     AliErrorStream() 
283          << "Geometry module id: "
284          << setw(3) << iGeomModule << "  "
285          << "Current SV: " 
286          <<  CurrentVolumePath() 
287          << "  detElemId: "
288          << setw(5) << detElemId 
289          << endl;
290     Double_t x, y, z;
291     gMC->TrackPosition(x, y, z);         
292     AliErrorStream() 
293          << "   global position: "
294          << x << ", " << y << ", " << z
295          << endl;
296     AliErrorStream() << "DetElemId not identified." << endl;
297   } 
298   
299   Int_t iChamber = AliMpDEManager::GetChamberId(detElemId) + 1; 
300   idvol = iChamber -1;
301     
302   // Filling TrackRefs file for MUON. Our Track references are the active volume of the chambers
303   if ( (gMC->IsTrackEntering() || gMC->IsTrackExiting() ) ) {
304     AliTrackReference* trackReference    
305       = AddTrackReference(gAlice->GetMCApp()->GetCurrentTrackNumber(), AliTrackReference::kMUON);
306     trackReference->SetUserId(detElemId);
307   }  
308   
309   if( gMC->IsTrackEntering() ) {
310      Float_t theta = fTrackMomentum.Theta();
311      if ((TMath::Pi()-theta)*kRaddeg>=15.) gMC->SetMaxStep(fStepMaxInActiveGas); // We use Pi-theta because z is negative
312      iEnter = 1;
313      gMC->TrackPosition(xyzEnter[0], xyzEnter[1], xyzEnter[2]); // save coordinates of entrance point
314   }
315
316    //   AliDebug(1,
317    //       Form("Active volume found %d chamber %d Z chamber is %f ",idvol,iChamber,
318    //            ( (AliMUONChamber*)(*fChambers)[idvol])->Z())) ;
319   // Particule id and mass, 
320   Int_t     ipart = gMC->TrackPid();
321   Float_t   mass  = gMC->TrackMass();
322
323   fDestepSum[idvol]+=gMC->Edep();
324   // Get current particle id (ipart), track position (pos)  and momentum (mom)
325   if ( fStepSum[idvol]==0.0 )  gMC->TrackMomentum(fTrackMomentum);
326   fStepSum[idvol]+=gMC->TrackStep();
327   
328   //  if (AliDebugLevel()) {
329   //   AliDebug(1,Form("Step, iChamber %d, Particle %d, theta %f phi %f mass %f StepSum %f eloss %g",
330   //                 iChamber,ipart, fTrackMomentum.Theta()*kRaddeg, fTrackMomentum.Phi()*kRaddeg,
331   //         mass, fStepSum[idvol], gMC->Edep()));
332   // AliDebug(1,Form("Step:Track Momentum %f %f %f", fTrackMomentum.X(), fTrackMomentum.Y(), 
333   //         fTrackMomentum.Z()));
334   // gMC->TrackPosition(fTrackPosition);
335   // AliDebug(1,Form("Step: Track Position %f %f %f",fTrackPosition.X(),
336   //         fTrackPosition.Y(),fTrackPosition.Z())) ;
337   //}
338
339   // Track left chamber or StepSum larger than fStepMaxInActiveGas
340   if ( gMC->IsTrackExiting() || 
341        gMC->IsTrackStop() || 
342        gMC->IsTrackDisappeared()||
343        (fStepSum[idvol]>fStepMaxInActiveGas) ) {
344     
345     if   ( gMC->IsTrackExiting() || 
346            gMC->IsTrackStop() || 
347            gMC->IsTrackDisappeared() ) gMC->SetMaxStep(kBig);
348     if (fDestepSum[idvol] == 0) {
349       // AZ - no energy release
350       fStepSum[idvol] = 0; // Reset for the next event
351       iEnter = 0;
352       return; 
353     }
354
355     gMC->TrackPosition(fTrackPosition);
356     Float_t theta = fTrackMomentum.Theta();
357     Float_t phi   = fTrackMomentum.Phi();
358     
359     Int_t merge = 0;
360     Double_t xyz0[3]={0}, xyz1[3]={0}, tmp[3]={0};
361     if (gMC->IsTrackExiting() && iEnter != 0) {
362       // AZ - this code is to avoid artificial hit splitting at the
363       // "fake" boundary inside the same chamber. It will still produce 
364       // 2 hits but with the same coordinates (at the wire) to allow 
365       // their merging at the digitization level.
366
367       // Only for a track going from the entrance to the exit from the volume
368       // Get local coordinates
369       gMC->Gmtod(xyzEnter, xyz0, 1); // local coord. at the entrance
370
371       fTrackPosition.Vect().GetXYZ(tmp);
372       gMC->Gmtod(tmp, xyz1, 1); // local coord. at the exit
373       Float_t dx = xyz0[0] - xyz1[0];
374       Float_t dy = xyz0[1] - xyz1[1];
375       Float_t thLoc = TMath::ATan2 (TMath::Sqrt(dx*dx+dy*dy), TMath::Abs(xyz0[2]-xyz1[2]));
376       if (thLoc * TMath::RadToDeg() < 15) merge = 1; 
377     }
378
379     if (merge) {
380       Double_t dz = -0.5;
381       if (xyz1[2] != xyz0[2]) dz = xyz0[2] / (xyz1[2] - xyz0[2]);
382       tmp[0] = xyz0[0] - (xyz1[0] - xyz0[0]) * dz; // local coord. at the wire
383       tmp[1] = xyz0[1] - (xyz1[1] - xyz0[1]) * dz;
384       tmp[2] = xyz0[2] - (xyz1[2] - xyz0[2]) * dz;
385       gMC->Gdtom(tmp, xyz1, 1); // global coord. at the wire
386       fTrackPosition.SetXYZT(xyz1[0], xyz1[1], xyz1[2], fTrackPosition.T());
387     } else {
388       TLorentzVector backToWire( fStepSum[idvol]/2.*sin(theta)*cos(phi),
389                                  fStepSum[idvol]/2.*sin(theta)*sin(phi),
390                                  fStepSum[idvol]/2.*cos(theta),0.0       );
391       fTrackPosition-=backToWire;
392       //printf(" %d %d %d %f %d \n", gMC->IsTrackExiting(), gMC->IsTrackStop(), gMC->IsTrackDisappeared(), fStepSum[idvol], iEnter);
393       //    AliDebug(1,
394       //     Form("Track Position %f %f %f",fTrackPosition.X(),fTrackPosition.Y(),fTrackPosition.Z()));
395       // AliDebug(1,
396       //     Form("Exit: Track backToWire %f %f %f",backToWire.X(),backToWire.Y(),backToWire.Z())) ;
397     }
398     
399     //-------------- Angle effect 
400     // Ratio between energy loss of particle and Mip as a function of BetaGamma of particle (Energy/Mass)
401     
402     Float_t betaxGamma    = fTrackMomentum.P()/mass;//  pc/mc2
403     Float_t sigmaEffect10degrees;
404     Float_t sigmaEffectThetadegrees;
405     Float_t eLossParticleELossMip;
406     Float_t yAngleEffect=0.;
407     Float_t thetawires      =  TMath::ASin( TMath::Sin(TMath::Pi()-theta) * TMath::Sin(phi) ) ;// We use Pi-theta because z is negative
408     Double_t bField[3] = {0};
409     fTrackPosition.Vect().GetXYZ(tmp);
410     TGeoGlobalMagField::Instance()->Field(tmp,bField);
411
412     if (fAngleEffect && !fMagEffect){
413       thetawires = TMath::Abs(thetawires);
414     if ( (betaxGamma >3.2)   &&  (thetawires*kRaddeg<=15.) ) {
415       betaxGamma=TMath::Log(betaxGamma);
416       eLossParticleELossMip = fElossRatio->Eval(betaxGamma);
417       // 10 degrees is a reference for a model (arbitrary)
418       sigmaEffect10degrees=fAngleEffect10->Eval(eLossParticleELossMip);// in micrometers
419       // Angle with respect to the wires assuming that chambers are perpendicular to the z axis.
420       sigmaEffectThetadegrees =  sigmaEffect10degrees/fAngleEffectNorma->Eval(thetawires*kRaddeg);  // For 5mm gap  
421       if ( (iChamber==1)  ||  (iChamber==2) )  
422         sigmaEffectThetadegrees/=(1.09833e+00+1.70000e-02*(thetawires*kRaddeg)); // The gap is different (4mm)
423       yAngleEffect=1.e-04*gRandom->Gaus(0,sigmaEffectThetadegrees); // Error due to the angle effect in cm
424     }
425     }
426     else if (fAngleEffect && fMagEffect) {
427       if ( (betaxGamma >3.2)   &&  (TMath::Abs(thetawires*kRaddeg)<=15.) ) {
428         betaxGamma=TMath::Log(betaxGamma);
429         eLossParticleELossMip = fElossRatio->Eval(betaxGamma);
430         // 10 degrees is a reference for a model (arbitrary)
431         sigmaEffect10degrees=fAngleEffect10->Eval(eLossParticleELossMip);// in micrometers
432         // Angle with respect to the wires assuming that chambers are perpendicular to the z axis.
433         sigmaEffectThetadegrees =  sigmaEffect10degrees/fMagAngleEffectNorma->Eval(thetawires*kRaddeg,bField[0]/10.);  // For 5mm gap  
434       if ( (iChamber==1)  ||  (iChamber==2) )  
435         sigmaEffectThetadegrees/=(1.09833e+00+1.70000e-02*(thetawires*kRaddeg)); // The gap is different (4mm)
436         yAngleEffect=1.e-04*gRandom->Gaus(0,sigmaEffectThetadegrees); // Error due to the angle effect in cm
437       }
438     }
439
440     AliMUONHit hit(fIshunt, 
441                    gAlice->GetMCApp()->GetCurrentTrackNumber(), 
442                    detElemId, ipart,
443                    fTrackPosition.X(), 
444                    fTrackPosition.Y()+yAngleEffect, 
445                    fTrackPosition.Z(), 
446                    gMC->TrackTime(),
447                    fTrackMomentum.P(),
448                    theta, 
449                    phi, 
450                    fStepSum[idvol], 
451                    fDestepSum[idvol],                        
452                    fTrackPosition.X(),
453                    fTrackPosition.Y(),
454                    fTrackPosition.Z());
455     
456     fHitStore->Add(hit);
457
458     fStepSum[idvol]  =0; // Reset for the next event
459     fDestepSum[idvol]=0; // Reset for the next event
460     iEnter = 0;
461   }
462 }