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