3 #include <TDatabasePDG.h>
6 #include <TGeoGlobalMagField.h>
8 #include <TObjString.h>
12 #include <AliTPCROC.h>
13 #include <AliTrackPointArray.h>
14 #include <AliTrackerBase.h>
15 #include <AliCDBManager.h>
16 #include <AliTPCParam.h>
17 #include <AliGeomManager.h>
18 #include <AliTPCcalibDB.h>
19 #include <AliTPCclusterMI.h>
20 #include <AliTPCSpaceCharge3D.h>
21 #include <AliTPCROC.h>
22 #include <AliExternalTrackParam.h>
24 #include "AliToyMCEvent.h"
25 #include "AliToyMCTrack.h"
27 #include "AliToyMCEventGenerator.h"
29 ClassImp(AliToyMCEventGenerator);
32 AliToyMCEventGenerator::AliToyMCEventGenerator()
38 ,fCorrectionFile("$ALICE_ROOT/TPC/Calib/maps/SC_NeCO2_eps5_50kHz_precal.lookup.root")
39 ,fOutputFileName("toyMC.root")
42 ,fUseStepCorrection(kFALSE)
43 ,fUseMaterialBudget(kFALSE)
46 fTPCParam = AliTPCcalibDB::Instance()->GetParameters();
47 fTPCParam->ReadGeoMatrices();
50 //________________________________________________________________
51 AliToyMCEventGenerator::AliToyMCEventGenerator(const AliToyMCEventGenerator &gen)
53 ,fTPCParam(gen.fTPCParam)
56 ,fTPCCorrection(gen.fTPCCorrection)
57 ,fCorrectionFile(gen.fCorrectionFile)
58 ,fOutputFileName(gen.fOutputFileName)
61 ,fUseStepCorrection(gen.fUseStepCorrection)
62 ,fUseMaterialBudget(gen.fUseMaterialBudget)
63 ,fIsLaser(gen.fIsLaser)
68 //________________________________________________________________
69 AliToyMCEventGenerator::~AliToyMCEventGenerator()
71 delete fTPCCorrection;
74 //________________________________________________________________
75 Bool_t AliToyMCEventGenerator::DistortTrack(AliToyMCTrack &trackIn, Double_t t0)
82 fTPCParam = AliTPCcalibDB::Instance()->GetParameters();
83 fTPCParam->ReadGeoMatrices();
86 MakeITSClusters(trackIn/*,t0*/);
87 MakeTPCClusters(trackIn, t0);
88 MakeTRDClusters(trackIn/*,t0*/);
92 //________________________________________________________________
93 void AliToyMCEventGenerator::MakeITSClusters(AliToyMCTrack &trackIn/*, Double_t t0*/)
95 //Upgrade ITS parameters
96 const Int_t nITSLayers = 7;
97 const Double_t ITSRadii[nITSLayers] = {2.2, 2.8, 3.6, 20.0, 22.0, 41.0, 43.0};
98 const Double_t lengthITS[nITSLayers] = {22.4, 24.2, 26.8, 78.0, 83.6, 142.4, 148.6};
100 //resolution of the point is 10um
101 const Float_t sigmaY = 0.001;
102 const Float_t sigmaZ = 0.001;
106 const Double_t kMaxSnp = 0.85;
107 // const Double_t kMaxZ0 = fTPCParam->GetZLength();
108 const Double_t kMass = TDatabasePDG::Instance()->GetParticle("pi+")->Mass();
110 AliExternalTrackParam track(trackIn);
111 Double_t xyz[3] = {0.,0.,0.};
113 if (!AliTrackerBase::PropagateTrackTo(&track,ITSRadii[0],kMass,5,kTRUE,kMaxSnp,0,kFALSE,fUseMaterialBudget)) {
114 AliError(Form("Propagation to %.2f failed\n",ITSRadii[0]));
118 for(Int_t iLayer = 0; iLayer<nITSLayers; iLayer++){
120 if (!AliTrackerBase::PropagateTrackTo(&track,ITSRadii[iLayer],kMass,1,kTRUE,kMaxSnp,0,kFALSE,fUseMaterialBudget)) {
121 AliError(Form("Propagation to %.2f failed\n",ITSRadii[iLayer]));
124 // since I don't know how to extract the volumeid of the ITS layers I use the following strategy:
125 // - rotate the track to the next integer angle
126 // - use coordinates there and set as volumeid the integer angle
127 // - in the reco one can then rotate the cluster to the global frame using the angle stored in the volume id
130 // if (TMath::Abs(track.GetZ())>kMaxZ0) continue;
131 if (TMath::Abs(track.GetZ())>lengthITS[iLayer]/2) continue;
134 // smear the ideal positions with the cluster resolution
135 xyz[1]+=gRandom->Gaus(0,sigmaY);
136 xyz[2]+=gRandom->Gaus(0,sigmaZ);
138 Float_t xyzf[3]={xyz[0],xyz[1],xyz[2]};
140 SetPoint(xyzf,sigmaY,sigmaZ,point);
142 trackIn.AddITSPoint(point)->SetUniqueID(trackIn.GetUniqueID());
146 //________________________________________________________________
147 void AliToyMCEventGenerator::MakeTRDClusters(AliToyMCTrack &trackIn/*, Double_t t0*/)
150 //Uses current TRD parameters
151 const Int_t nTRDLayers = 6;
152 const Double_t distToMid = 3.2 + 30./2; //dist to middle of drift region (radiator + half drift region)
153 const Double_t TRDRadii[nTRDLayers] = {294.5 + distToMid, 307.1 + distToMid, 319.7 + distToMid, 332.3 + distToMid, 344.9 + distToMid, 357.5 + distToMid};
154 const Double_t lengthTRD[nTRDLayers] = {604.0, 634.0, 656.0, 686.0, 700.0, 700.0};
156 const Float_t sigmaY = 0.06;
157 const Float_t sigmaZ = 0.2;
161 const Double_t kMaxSnp = 0.85;
162 const Double_t kMaxZ0 = fTPCParam->GetZLength();
163 const Double_t kMass = TDatabasePDG::Instance()->GetParticle("pi+")->Mass();
165 AliExternalTrackParam track(trackIn);
166 Double_t xyz[3] = {0.,0.,0.};
168 if (!AliTrackerBase::PropagateTrackTo(&track,TRDRadii[0],kMass,5,kTRUE,kMaxSnp,0,kFALSE,fUseMaterialBudget)) {
169 AliError(Form("Propagation to %.2f failed\n",TRDRadii[0]));
173 for(Int_t iLayer = 0; iLayer<nTRDLayers; iLayer++){
175 if (!AliTrackerBase::PropagateTrackTo(&track,TRDRadii[iLayer],kMass,1,kTRUE,kMaxSnp,0,kFALSE,fUseMaterialBudget)) {
176 AliError(Form("Propagation to %.2f failed\n",TRDRadii[iLayer]));
181 if (TMath::Abs(track.GetZ())>kMaxZ0) continue;
182 if (TMath::Abs(track.GetZ())>lengthTRD[iLayer]/2) continue;
185 // smear the ideal positions with the cluster resolution
186 xyz[1]+=gRandom->Gaus(0,sigmaY);
187 xyz[2]+=gRandom->Gaus(0,sigmaZ);
189 Float_t xyzf[3]={xyz[0],xyz[1],xyz[2]};
191 SetPoint(xyzf,sigmaY,sigmaZ,point);
193 trackIn.AddTRDPoint(point)->SetUniqueID(trackIn.GetUniqueID());
197 //________________________________________________________________
198 void AliToyMCEventGenerator::MakeTPCClusters(AliToyMCTrack &trackIn, Double_t t0)
201 // make it big enough to hold all points
202 // store real number of generated points in the unique id
203 const Int_t nMaxPoints=3000;
204 static AliTrackPointArray pointArray0(nMaxPoints); //undistorted
205 static AliTrackPointArray pointArray1(nMaxPoints); //distorted
207 //Create space point of undistorted and distorted clusters along the propagated track trajectory
208 CreateSpacePoints(trackIn,pointArray0,pointArray1);
209 //Convert the space points into clusters in the local frame
210 //for undistorted and distorted clusters using the same function
211 ConvertTrackPointsToLocalClusters(pointArray0,trackIn,t0,0);
212 ConvertTrackPointsToLocalClusters(pointArray1,trackIn,t0,1);
216 //________________________________________________________________
217 void AliToyMCEventGenerator::CreateSpacePoints(AliToyMCTrack &trackIn,
218 AliTrackPointArray &arrUdist,
219 AliTrackPointArray &arrDist)
222 // sample the track from the inner to the outer wall of the TPC
223 // a graph is filled in local coordinates for later
226 Double_t kMaxSnp = 0.85;
227 if (fIsLaser) kMaxSnp=0.99;
228 const Double_t kMaxZ0 = fTPCParam->GetZLength();
229 const Double_t kMass = TDatabasePDG::Instance()->GetParticle("pi+")->Mass();
231 const Double_t iFCRadius = 83.5; //radius constants found in AliTPCCorrection.cxx
232 const Double_t oFCRadius = 254.5;
234 const Float_t kSigmaY=0.1;
235 const Float_t kSigmaZ=0.1;
237 AliExternalTrackParam track(trackIn);
238 //!!! TODO: make this adjustable perhaps
239 const Double_t stepSize=0.1;
240 Double_t xyz[3] = {0.,0.,0.};
241 Float_t xyzf[3] = {0.,0.,0.};
243 //!!! when does the propagation not work, how often does it happen?
244 if (!AliTrackerBase::PropagateTrackTo(&track,iFCRadius,kMass,5,kTRUE,kMaxSnp,0,kFALSE,fUseMaterialBudget) && !fIsLaser) {
245 AliError(Form("Propagation to IFC: %.2f failed\n",iFCRadius));
251 for (Double_t radius=iFCRadius; radius<oFCRadius; radius+=stepSize){
252 //!!! changed from return 0 to continue -> Please check
253 if (!AliTrackerBase::PropagateTrackTo(&track,radius,kMass,1,kTRUE,kMaxSnp,0,kFALSE,fUseMaterialBudget)) {
254 AliError(Form("Propagation to r=%.2f (snp=%.2f) failed\n",radius,track.GetSnp()));
259 //!!! Why is this smeared
261 xyzf[0]=Float_t(xyz[0]);
262 xyzf[1]=Float_t(xyz[1]);
263 xyzf[2]=Float_t(xyz[2]);
265 if (TMath::Abs(track.GetZ())>kMaxZ0) continue;
266 // if (TMath::Abs(track.GetX())<iFCRadius) continue;
267 // if (TMath::Abs(track.GetX())>oFCRadius) continue;
270 AliTrackPoint pUdist; // undistorted space point
271 AliTrackPoint pDist; // distorted space point
272 // Set undistorted point
273 SetPoint(xyzf,kSigmaY,kSigmaZ,pUdist);
274 arrUdist.AddPoint(npoints, &pUdist);
275 Int_t sector=pUdist.GetVolumeID();
277 // set distorted point
278 Float_t distPoint[3]={xyz[0],xyz[1],xyz[2]};
279 Float_t dxyz[3]={0.,0.,0.};
280 if (!fUseStepCorrection){
281 fTPCCorrection->DistortPoint(distPoint, sector);
283 fTPCCorrection->GetCorrectionIntegralDz(distPoint,sector,dxyz,5);
284 distPoint[0]-=dxyz[0];
285 distPoint[1]-=dxyz[1];
286 distPoint[2]-=dxyz[2];
288 SetPoint(distPoint,kSigmaY,kSigmaZ,pDist);
289 arrDist.AddPoint(npoints, &pDist);
294 arrUdist.SetUniqueID(npoints);
295 arrDist.SetUniqueID(npoints);
298 //________________________________________________________________
299 void AliToyMCEventGenerator::SetPoint(Float_t xyz[3], Float_t sigmaY, Float_t sigmaZ, AliTrackPoint &point)
302 // make AliTrackPoint out of AliTPCclusterMI
305 //covariance at the local frame
306 //assume 1mm distortion in y and z
308 Float_t cov[6]={0,0,0, sigmaY*sigmaY,0,sigmaZ*sigmaZ};
310 const Float_t alpha = -TMath::ATan2(xyz[1],xyz[0]);
311 const Float_t sin = TMath::Sin(alpha), cos = TMath::Cos(alpha);
314 newcov[0] = cov[0]*cos*cos + 2*cov[1]*sin*cos + cov[3]*sin*sin;
315 newcov[1] = cov[1]*(cos*cos-sin*sin) + (cov[3]-cov[0])*sin*cos;
316 newcov[2] = cov[2]*cos + cov[4]*sin;
317 newcov[3] = cov[0]*sin*sin - 2*cov[1]*sin*cos + cov[3]*cos*cos;
318 newcov[4] = cov[4]*cos - cov[2]*sin;
321 // voluem ID to add later ....
323 point.SetCov(newcov);
324 // abuse volume ID for the sector number
325 point.SetVolumeID(fTPCParam->Transform0to1(xyz,i));
327 // TODO: Add sampled dE/dx (use SetCharge)
330 //________________________________________________________________
331 void AliToyMCEventGenerator::ConvertTrackPointsToLocalClusters(AliTrackPointArray &arrPoints,
332 AliToyMCTrack &tr, Double_t t0, Int_t type)
338 const Int_t npoints=Int_t(arrPoints.GetUniqueID());
341 // create an array for graphs which are used for local interpolation
342 // we need a new graph if the sector changed since then the local frame will also change
343 TObjArray arrGraphsXY(72);
344 arrGraphsXY.SetOwner();
345 TObjArray arrGraphsXZ(72);
346 arrGraphsXZ.SetOwner();
348 //create initial graph
351 //row -> sector mapping
353 for (Int_t irow=0; irow<159; ++irow) rowMap[irow]=-1;
356 // Make from the list of global space points graphs in the local frame
357 // one graph per sector is needed
358 for (Int_t ipoint=0; ipoint<npoints; ++ipoint){
359 arrPoints.GetPoint(p,ipoint);
360 Float_t xyz[3] = {p.GetX(),p.GetY(),p.GetZ()};
361 Int_t index[3] = {0,0,0};
362 Int_t row = fTPCParam->GetPadRow(xyz,index);
363 // rotate space point to local frame
364 // the angle is given by the VolumeID which was set in CrateSpacePoints
365 const Int_t sec=p.GetVolumeID();
366 if (row<0 || (sec<36 && row>62) || row>95 ) continue;
367 Double_t angle=((sec%18)*20.+10.)/TMath::RadToDeg();
368 AliTrackPoint pRot=p.Rotate(angle);
369 const Int_t secrow=row+(sec>35)*63;
370 if (rowMap[secrow]==-1) rowMap[secrow]=sec;
371 // check if we need a new graph (sector change)
375 arrGraphsXY.AddAt(grXY,sec);
376 arrGraphsXZ.AddAt(grXZ,sec);
379 //add coordinates in local frame for later interpolation
380 grXY->SetPoint(grXY->GetN(), pRot.GetX(), pRot.GetY());
381 grXZ->SetPoint(grXZ->GetN(), pRot.GetX(), pRot.GetZ());
386 // create in the center of each row a space point by using the graph to interpolate
387 // the the center of the row. This is done in xy and xz
390 AliTPCclusterMI tempCl;
392 for (Int_t irow=0; irow<159; ++irow ){
393 const Int_t sec = rowMap[irow];
394 if (sec==-1) continue;
395 const Int_t secrow = irow<63?irow:irow-63;
396 Double_t localX = fTPCParam->GetPadRowRadii(sec,secrow);
397 // get graph for the current row
404 grXY=(TGraph*)arrGraphsXY.At(sec);
405 grXZ=(TGraph*)arrGraphsXZ.At(sec);
408 // if(grXY->GetN()>1 && grXZ->GetN()>1) { //causes segmentation violation if N==1
409 // splXY=new TSpline3("splXY",grXY);
410 // splXZ=new TSpline3("splXZ",grXZ);
413 //TODO: make a cluster also in the sector w only one space point?
415 // Double_t tempX=0., tempY = 0., tempZ = 0.;
417 // grXY->GetPoint(0,tempX,localY);
418 // grXZ->GetPoint(0,tempX,localZ);
424 // check we are in an active area
425 // if (splXY->FindX(localX)<1 || splXZ->FindX(localX)<1) continue;
426 if ( localX<grXY->GetX()[0] || localX>grXY->GetX()[grXY->GetN()-1] || localX<grXZ->GetX()[0] || localX>grXZ->GetX()[grXZ->GetN()-1]) continue;
428 //get interpolated value at the center for the pad row
430 // const Double_t localY=splXY->Eval(localX/*,0x0,"S"*/);
431 // const Double_t localZ=splXZ->Eval(localX/*,0x0,"S"*/);
432 const Double_t localY=grXY->Eval(localX/*,0x0,"S"*/);
433 const Double_t localZ=grXZ->Eval(localX/*,0x0,"S"*/);
434 Float_t xyz[3]={localX,localY,localZ};
436 if (!SetupCluster(tempCl,xyz,sec,t0)) continue;
437 tempCl.SetLabel(tr.GetUniqueID(), 0);
439 if (type==0) tr.AddSpacePoint(tempCl);
440 else tr.AddDistortedSpacePoint(tempCl);
441 // printf("SetupCluster %3d: (%.2f, %.2f, %.2f), %d, %.2f\n",irow,xyz[0],xyz[1],xyz[2],sec,t0);
451 //________________________________________________________________
452 Bool_t AliToyMCEventGenerator::SetupCluster(AliTPCclusterMI &tempCl, Float_t xyz[3], Int_t sec, Double_t t0)
458 // intrinsic cluster resolution is 1mm
459 const Double_t kSigmaY = 0.1;
460 const Double_t kSigmaZ = 0.1;
461 const Double_t kMaxZ0 = fTPCParam->GetZLength();
462 //TODO: Get this from the OCDB at some point?
463 const Double_t kDriftVel = fTPCParam->GetDriftV();
465 // smear the ideal positions with the cluster resolution
466 xyz[1]+=gRandom->Gaus(0,kSigmaY);
467 xyz[2]+=gRandom->Gaus(0,kSigmaZ);
473 tempCl.SetSigmaY2(kSigmaY*kSigmaY);
474 tempCl.SetSigmaZ2(kSigmaZ*kSigmaZ);
476 // transform from the local coordinates to the coordinates expressed in pad coordinates
477 Int_t index[3] = {0,sec,0};
478 fTPCParam->Transform2to3(xyz,index);
479 fTPCParam->Transform3to4(xyz,index);
480 fTPCParam->Transform4to8(xyz,index);
482 const Int_t row = index[2];
483 const Int_t nPads = fTPCParam->GetNPads(sec, row);
484 // pad is fractional, but it needs to be shifted from the center
485 // to the edge of the row
486 const Float_t pad = xyz[1] + nPads/2;
490 Float_t timeBin=Float_t(t0 + (kMaxZ0-TMath::Abs(tempCl.GetZ()))/kDriftVel);
491 tempCl.SetTimeBin(timeBin); // set time as t0 + drift time from dist z
492 tempCl.SetDetector(sec);
494 //check if we are in the active area
495 if (pad<0 || pad>=nPads) return kFALSE;
500 //________________________________________________________________
501 Bool_t AliToyMCEventGenerator::ConnectOutputFile()
504 // Create the output file name and tree and connect the event
507 fOutFile = new TFile(fOutputFileName.Data(),"recreate");
509 if (!fOutFile || !fOutFile->IsOpen()){
515 fOutTree = new TTree("toyMCtree","Tree with toyMC simulation");
516 fOutTree->Branch("event","AliToyMCEvent",&fEvent);
523 //________________________________________________________________
524 Bool_t AliToyMCEventGenerator::CloseOutputFile()
527 // close the output file
529 if (!fOutFile) return kFALSE;
538 //________________________________________________________________
539 void AliToyMCEventGenerator::FillTree()
542 if (fOutTree&&fEvent) fOutTree->Fill();
545 //________________________________________________________________
546 void AliToyMCEventGenerator::SetSpaceCharge(EEpsilon epsilon, EGasType gasType/*=kNeCO2_9010*/,
547 ECollRate collRate/*=k50kHz*/, ECorrection corrType/*=kLookup*/)
550 // Set the space charge conditions
552 fCorrectionFile="$ALICE_ROOT/TPC/Calib/maps/SC";
555 fCorrectionFile.Append("_NeCO2");
560 fCorrectionFile.Append("_eps5");
563 fCorrectionFile.Append("_eps10");
566 fCorrectionFile.Append("_eps20");
571 fCorrectionFile.Append("_50kHz");
576 fCorrectionFile.Append("_precal.lookup.root");
578 case kSpaceChargeFile:
579 fCorrectionFile.Append("_precal.root");
584 //________________________________________________________________
585 void AliToyMCEventGenerator::InitSpaceCharge()
588 // init the space charge conditions
589 // this should be called after the tree was connected
592 AliInfo(Form("Using space charge map file: '%s'",fCorrectionFile.Data()));
594 TString corrName("map");
596 // allow for specifying an object name for the AliTPCCorrection in the file name
597 // separated by a ':'
598 TObjArray *arr=fCorrectionFile.Tokenize(":");
599 if (arr->GetEntriesFast()>1) {
600 fCorrectionFile=arr->At(0)->GetName();
601 corrName=arr->At(1)->GetName();
606 TFile f(fCorrectionFile.Data());
607 fTPCCorrection=(AliTPCSpaceCharge3D*)f.Get("map");
610 AliInfo("Attaching space charge map file name to the tree");
611 fOutTree->GetUserInfo()->Add(new TObjString(fCorrectionFile.Data()));