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