Always delete TObjArrays created by TString::Tokenize (Ruben)
[u/mrichter/AliRoot.git] / MUON / AliMUONv1.cxx
CommitLineData
a9e2aefa 1/**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
0c527060 3 * *
a9e2aefa 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 *
2c799aa2 12 * about the suitability of this software for any purpeateose. It is *
a9e2aefa 13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
15
88cb7938 16/* $Id$ */
a9e2aefa 17
3d1463c8 18//-----------------------------------------------------------------------------
d19b6003 19// Class AliMUONv1
20// --------------------
21// AliDetector class for MUON subsystem which implements
22// functions for simulation
3d1463c8 23//-----------------------------------------------------------------------------
acc838fd 24
30178c30 25#include "AliMUONv1.h"
88cb7938 26#include "AliMUONConstants.h"
b7ef3c96 27#include "AliMUONResponseFactory.h"
a9e2aefa 28#include "AliMUONHit.h"
d4bb94a1 29#include "AliMUONGeometryBuilder.h"
b7ef3c96 30#include "AliMUONGeometry.h"
31#include "AliMUONGeometryTransformer.h"
e118b27e 32#include "AliMUONGeometryModule.h"
7ecf374b 33#include "AliMUONStringIntMap.h"
e118b27e 34#include "AliMUONGeometryDetElement.h"
7ecf374b 35
331a617a 36#include "AliMpCDB.h"
273e54a3 37#include "AliMpDEManager.h"
38
7ecf374b 39#include "AliConst.h"
88cb7938 40#include "AliMagF.h"
41#include "AliRun.h"
5d12ce38 42#include "AliMC.h"
d95fe39e 43#include "AliTrackReference.h"
8c343c7c 44#include "AliLog.h"
a9e2aefa 45
f7a1cc68 46#include <TClonesArray.h>
7ecf374b 47#include <TF1.h>
d7d39669 48#include <TF2.h>
f7a1cc68 49#include <TGeoGlobalMagField.h>
50#include <TGeoMatrix.h>
51#include <TRandom.h>
7ecf374b 52#include <TRandom.h>
53#include <TVirtualMC.h>
7ecf374b 54
edee5e63 55#include <string>
56
bb2bab35 57#include "AliMUONVHitStore.h"
58
cdd730d0 59using std::endl;
60using std::cout;
61using std::setw;
5398f946 62/// \cond CLASSIMP
a9e2aefa 63ClassImp(AliMUONv1)
5398f946 64/// \endcond
a9e2aefa 65
66//___________________________________________
30178c30 67AliMUONv1::AliMUONv1()
68 : AliMUON(),
d4bb94a1 69 fAngleEffect(kTRUE),
d7d39669 70 fMagEffect(kTRUE),
d4bb94a1 71 fStepMaxInActiveGas(0.6),
72 fStepSum(0x0),
73 fDestepSum(0x0),
74 fTrackMomentum(),
75 fTrackPosition(),
76 fElossRatio(0x0),
77 fAngleEffect10(0x0),
d7d39669 78 fAngleEffectNorma(0x0),
79 fMagAngleEffectNorma(0x0)
a9e2aefa 80{
d19b6003 81/// Default constructor
82
83 AliDebug(1,Form("default (empty) ctor this=%p",this));
1391e633 84}
d4bb94a1 85
a9e2aefa 86//___________________________________________
9bf406e1 87AliMUONv1::AliMUONv1(const char *name, const char* title)
88: AliMUON(name, title),
d4bb94a1 89 fAngleEffect(kTRUE),
d7d39669 90 fMagEffect(kTRUE),
d4bb94a1 91 fStepMaxInActiveGas(0.6),
92 fStepSum(0x0),
93 fDestepSum(0x0),
94 fTrackMomentum(),
95 fTrackPosition(),
96 fElossRatio(0x0),
97 fAngleEffect10(0x0),
d7d39669 98 fAngleEffectNorma(0x0),
99 fMagAngleEffectNorma(0x0)
a9e2aefa 100{
d19b6003 101/// Standard onstructor
d4bb94a1 102
d19b6003 103 AliDebug(1,Form("ctor this=%p",this));
edee5e63 104
331a617a 105 // Load mapping
106 if ( ! AliMpCDB::LoadMpSegmentation() ) {
107 AliFatal("Could not access mapping from OCDB !");
108 }
109
ba030c0e 110 // By default include all stations
c33d9661 111
1391e633 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);
d7d39669 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);
a9e2aefa 143}
144
145//___________________________________________
acc838fd 146AliMUONv1::~AliMUONv1()
a9e2aefa 147{
d19b6003 148/// Destructor
149
150 AliDebug(1,Form("dtor this=%p",this));
d4bb94a1 151 delete [] fStepSum;
152 delete [] fDestepSum;
153 delete fElossRatio;
154 delete fAngleEffect10;
8109731a 155 delete fAngleEffectNorma;
d7d39669 156 delete fMagAngleEffectNorma;
acc838fd 157}
b17c0c87 158
acc838fd 159//__________________________________________________
160void AliMUONv1::CreateGeometry()
161{
d19b6003 162/// Construct geometry using geometry builder
a9e2aefa 163
d4bb94a1 164 fGeometryBuilder->CreateGeometry();
acc838fd 165}
a9e2aefa 166
acc838fd 167//________________________________________________________________
a9e2aefa 168void AliMUONv1::CreateMaterials()
169{
d19b6003 170/// Construct materials using geometry builder
acc838fd 171
d4bb94a1 172 fGeometryBuilder->CreateMaterials();
a9e2aefa 173}
174
c28b5041 175//________________________________________________________________
450ca6fb 176void AliMUONv1::UpdateInternalGeometry()
177{
178/// Update geometry after applying mis-alignment
179
008b1449 180 // Load mapping
181 if ( ! AliMpCDB::LoadMpSegmentation() ) {
182 AliFatal("Could not access mapping from OCDB !");
183 }
184
450ca6fb 185 fGeometryBuilder->UpdateInternalGeometry();
186}
187
188//________________________________________________________________
c28b5041 189void AliMUONv1::AddAlignableVolumes() const
190{
191/// Construct materials using geometry builder
192
193 GetGeometryTransformer()->AddAlignableVolumes();
194}
195
196
acc838fd 197//___________________________________________
a9e2aefa 198void AliMUONv1::Init()
a713db22 199{
d19b6003 200/// Initialize geometry
201
edee5e63 202 AliDebug(1,"Start Init for version 1 - CPC chamber type");
002920d1 203
edee5e63 204 fGeometryBuilder->InitGeometry();
205 AliDebug(1,"Finished Init for version 1 - CPC chamber type");
b7ef3c96 206
207
b7ef3c96 208 // Build segmentation
209 // using geometry parametrisation
210 //
b7ef3c96 211 // Build response
edee5e63 212 //
43d7561d 213 AliMUONResponseFactory respFactory("default", fIsTailEffect);
b7ef3c96 214 respFactory.Build(this);
215
a9e2aefa 216}
1391e633 217
d4bb94a1 218//__________________________________________________________________
273e54a3 219Int_t AliMUONv1::GetGeomModuleId(Int_t volId) const
d4bb94a1 220{
d19b6003 221/// Check if the volume with specified volId is a sensitive volume (gas)
5398f946 222/// of some chamber and return the chamber number;
d19b6003 223/// if not sensitive volume - return 0.
d4bb94a1 224
273e54a3 225 for (Int_t i = 0; i < AliMUONConstants::NGeomModules(); i++) {
b7ef3c96 226 if ( GetGeometry()->GetModule(i)->IsSensitiveVolume(volId) )
273e54a3 227 return i;
b7ef3c96 228 }
d4bb94a1 229
273e54a3 230 return -1;
d4bb94a1 231}
333f7cda 232
233//_______________________________________________________________________________
234TString AliMUONv1::CurrentVolumePath() const
235{
d19b6003 236/// Return current volume path
237/// (Could be removed when this function is available via gMC)
333f7cda 238
239 TString path = "";
240 TString name;
241 Int_t copyNo;
242 Int_t imother = 0;
243 do {
244 name = gMC->CurrentVolOffName(imother);
245 gMC->CurrentVolOffID(imother++, copyNo);
246 TString add = "/";
247 add += name;
7ecf374b 248 add += "_";
333f7cda 249 add += copyNo;
250 path.Insert(0,add);
251 }
252 while ( name != TString("ALIC") );
253
254 return path;
255}
256
1391e633 257//_______________________________________________________________________________
c33d9661 258void AliMUONv1::StepManager()
259{
d19b6003 260/// Step manager for the chambers
c33d9661 261
262 // Only charged tracks
d59b2ec3 263 if( !(gMC->TrackCharge()) ) return;
1391e633 264 // Only charged tracks
265
c33d9661 266 // Only gas gap inside chamber
267 // Tag chambers and record hits when track enters
d95fe39e 268 static Int_t idvol=-1, iEnter = 0;
1391e633 269 Int_t copy;
270 const Float_t kBig = 1.e10;
d95fe39e 271 static Double_t xyzEnter[3];
acc838fd 272
273 //
274 // Only gas gap inside chamber
275 // Tag chambers and record hits when track enters
273e54a3 276 Int_t id=gMC->CurrentVolID(copy);
277 Int_t iGeomModule = GetGeomModuleId(id);
278 if (iGeomModule == -1) return;
acc838fd 279
d95fe39e 280 // Detection elements id
281 const AliMUONGeometryModule* kGeometryModule
273e54a3 282 = GetGeometry()->GetModule(iGeomModule);
d95fe39e 283 AliMUONGeometryDetElement* detElement
284 = kGeometryModule->FindBySensitiveVolume(CurrentVolumePath());
273e54a3 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 }
374ebd7d 293
d95fe39e 294 Int_t detElemId = 0;
295 if (detElement) detElemId = detElement->GetUniqueID();
333f7cda 296
d95fe39e 297 if (!detElemId) {
273e54a3 298 AliErrorStream()
299 << "Geometry module id: "
300 << setw(3) << iGeomModule << " "
d95fe39e 301 << "Current SV: "
302 << CurrentVolumePath()
303 << " detElemId: "
304 << setw(5) << detElemId
305 << endl;
306 Double_t x, y, z;
307 gMC->TrackPosition(x, y, z);
273e54a3 308 AliErrorStream()
309 << " global position: "
d95fe39e 310 << x << ", " << y << ", " << z
311 << endl;
273e54a3 312 AliErrorStream() << "DetElemId not identified." << endl;
313 }
314
315 Int_t iChamber = AliMpDEManager::GetChamberId(detElemId) + 1;
316 idvol = iChamber -1;
333f7cda 317
d59b2ec3 318 // Filling TrackRefs file for MUON. Our Track references are the active volume of the chambers
d95fe39e 319 if ( (gMC->IsTrackEntering() || gMC->IsTrackExiting() ) ) {
320 AliTrackReference* trackReference
e6add757 321 = AddTrackReference(gAlice->GetMCApp()->GetCurrentTrackNumber(), AliTrackReference::kMUON);
d95fe39e 322 trackReference->SetUserId(detElemId);
323 }
d59b2ec3 324
d95fe39e 325 if( gMC->IsTrackEntering() ) {
d59b2ec3 326 Float_t theta = fTrackMomentum.Theta();
d631bec8 327 if ( fIsMaxStep && (TMath::Pi()-theta)*kRaddeg>=15. ) {
328 gMC->SetMaxStep(fStepMaxInActiveGas); // We use Pi-theta because z is negative
329 }
c5ce4e69 330 iEnter = 1;
331 gMC->TrackPosition(xyzEnter[0], xyzEnter[1], xyzEnter[2]); // save coordinates of entrance point
d59b2ec3 332 }
333
5a0e88a7 334 // AliDebug(1,
335 // Form("Active volume found %d chamber %d Z chamber is %f ",idvol,iChamber,
336 // ( (AliMUONChamber*)(*fChambers)[idvol])->Z())) ;
d59b2ec3 337 // Particule id and mass,
338 Int_t ipart = gMC->TrackPid();
339 Float_t mass = gMC->TrackMass();
340
341 fDestepSum[idvol]+=gMC->Edep();
342 // Get current particle id (ipart), track position (pos) and momentum (mom)
343 if ( fStepSum[idvol]==0.0 ) gMC->TrackMomentum(fTrackMomentum);
344 fStepSum[idvol]+=gMC->TrackStep();
345
5a0e88a7 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], gMC->Edep()));
350 // AliDebug(1,Form("Step:Track Momentum %f %f %f", fTrackMomentum.X(), fTrackMomentum.Y(),
351 // fTrackMomentum.Z()));
352 // gMC->TrackPosition(fTrackPosition);
353 // AliDebug(1,Form("Step: Track Position %f %f %f",fTrackPosition.X(),
354 // fTrackPosition.Y(),fTrackPosition.Z())) ;
355 //}
d59b2ec3 356
357 // Track left chamber or StepSum larger than fStepMaxInActiveGas
358 if ( gMC->IsTrackExiting() ||
359 gMC->IsTrackStop() ||
360 gMC->IsTrackDisappeared()||
361 (fStepSum[idvol]>fStepMaxInActiveGas) ) {
362
d631bec8 363 if ( fIsMaxStep &&
364 ( gMC->IsTrackExiting() ||
365 gMC->IsTrackStop() ||
366 gMC->IsTrackDisappeared() ) ) gMC->SetMaxStep(kBig);
c5ce4e69 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 }
d59b2ec3 373
374 gMC->TrackPosition(fTrackPosition);
375 Float_t theta = fTrackMomentum.Theta();
376 Float_t phi = fTrackMomentum.Phi();
377
795b3f56 378 Int_t merge = 0;
379 Double_t xyz0[3]={0}, xyz1[3]={0}, tmp[3]={0};
c5ce4e69 380 if (gMC->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
c5ce4e69 388 gMC->Gmtod(xyzEnter, xyz0, 1); // local coord. at the entrance
389
390 fTrackPosition.Vect().GetXYZ(tmp);
391 gMC->Gmtod(tmp, xyz1, 1); // local coord. at the exit
795b3f56 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 }
c5ce4e69 397
795b3f56 398 if (merge) {
c5ce4e69 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 gMC->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", gMC->IsTrackExiting(), gMC->IsTrackStop(), gMC->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 }
d59b2ec3 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.;
d7d39669 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);
f7a1cc68 429 TGeoGlobalMagField::Instance()->Field(tmp,bField);
d59b2ec3 430
d7d39669 431 if (fAngleEffect && !fMagEffect){
432 thetawires = TMath::Abs(thetawires);
d59b2ec3 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 }
d7d39669 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 }
d59b2ec3 458
d7d39669 459 AliMUONHit hit(fIshunt,
460 gAlice->GetMCApp()->GetCurrentTrackNumber(),
461 detElemId, ipart,
462 fTrackPosition.X(),
463 fTrackPosition.Y()+yAngleEffect,
464 fTrackPosition.Z(),
465 gMC->TrackTime(),
466 fTrackMomentum.P(),
467 theta,
468 phi,
469 fStepSum[idvol],
470 fDestepSum[idvol],
471 fTrackPosition.X(),
472 fTrackPosition.Y(),
473 fTrackPosition.Z());
474
bb2bab35 475 fHitStore->Add(hit);
5a0e88a7 476
d59b2ec3 477 fStepSum[idvol] =0; // Reset for the next event
478 fDestepSum[idvol]=0; // Reset for the next event
c5ce4e69 479 iEnter = 0;
d59b2ec3 480 }
481}