3 #include <TDatabasePDG.h>
6 #include <TGeoGlobalMagField.h>
8 #include <TObjString.h>
11 #include <AliTPCROC.h>
12 #include <AliTrackPointArray.h>
13 #include <AliTrackerBase.h>
14 #include <AliCDBManager.h>
15 #include <AliTPCParam.h>
16 #include <AliGeomManager.h>
17 #include <AliTPCcalibDB.h>
18 #include <AliTPCclusterMI.h>
19 #include <AliTPCSpaceCharge3D.h>
20 #include <AliTPCROC.h>
22 #include "AliToyMCEvent.h"
23 #include "AliToyMCTrack.h"
25 #include "AliToyMCEventGenerator.h"
27 ClassImp(AliToyMCEventGenerator);
30 AliToyMCEventGenerator::AliToyMCEventGenerator()
35 ,fSpaceChargeFile("$ALICE_ROOT/TPC/Calib/maps/SC_NeCO2_eps5_50kHz_precal.root")
36 ,fOutputFileName("toyMC.root")
39 ,fUseStepCorrection(kFALSE)
40 ,fUseMaterialBudget(kFALSE)
42 fTPCParam = AliTPCcalibDB::Instance()->GetParameters();
43 fTPCParam->ReadGeoMatrices();
45 //________________________________________________________________
46 AliToyMCEventGenerator::AliToyMCEventGenerator(const AliToyMCEventGenerator &gen)
48 ,fTPCParam(gen.fTPCParam)
50 ,fSpaceCharge(gen.fSpaceCharge)
51 ,fSpaceChargeFile(gen.fSpaceChargeFile)
52 ,fOutputFileName(gen.fOutputFileName)
55 ,fUseStepCorrection(gen.fUseStepCorrection)
56 ,fUseMaterialBudget(gen.fUseMaterialBudget)
60 //________________________________________________________________
61 AliToyMCEventGenerator::~AliToyMCEventGenerator()
66 //________________________________________________________________
67 Bool_t AliToyMCEventGenerator::DistortTrack(AliToyMCTrack &trackIn, Double_t t0)
74 fTPCParam = AliTPCcalibDB::Instance()->GetParameters();
75 fTPCParam->ReadGeoMatrices();
78 // make it big enough to hold all points
79 // store real number of generated points in the unique id
80 const Int_t nMaxPoints=3000;
81 AliTrackPointArray pointArray0(nMaxPoints); //undistorted
82 AliTrackPointArray pointArray1(nMaxPoints); //distorted
84 //Create space point of undistorted and distorted clusters along the propagated track trajectory
85 CreateSpacePoints(trackIn,pointArray0,pointArray1);
86 //Convert the space points into clusters in the local frame
87 //for undistorted and distorted clusters using the same function
88 ConvertTrackPointsToLocalClusters(pointArray0,trackIn,t0,0);
89 ConvertTrackPointsToLocalClusters(pointArray1,trackIn,t0,1);
94 //________________________________________________________________
95 void AliToyMCEventGenerator::CreateSpacePoints(AliToyMCTrack &trackIn,
96 AliTrackPointArray &arrUdist,
97 AliTrackPointArray &arrDist)
100 // sample the track from the inner to the outer wall of the TPC
101 // a graph is filled in local coordinates for later
104 const Double_t kMaxSnp = 0.85;
105 const Double_t kMaxZ0 = fTPCParam->GetZLength();
106 const Double_t kMass = TDatabasePDG::Instance()->GetParticle("pi+")->Mass();
108 const Double_t iFCRadius = 83.5; //radius constants found in AliTPCCorrection.cxx
109 const Double_t oFCRadius = 254.5;
111 AliToyMCTrack track(trackIn);
112 //!!! TODO: make this adjustable perhaps
113 const Double_t stepSize=0.1;
114 Double_t xyz[3] = {0.,0.,0.};
115 Float_t xyzf[3] = {0.,0.,0.};
117 //!!! when does the propagation not work, how often does it happen?
118 if (!AliTrackerBase::PropagateTrackTo(&track,iFCRadius,kMass,5,kTRUE,kMaxSnp,0,kFALSE,fUseMaterialBudget)) {
119 AliError(Form("Propagation to IFC: %.2f failed\n",iFCRadius));
125 for (Double_t radius=iFCRadius; radius<oFCRadius; radius+=stepSize){
127 //!!! changed from return 0 to continue -> Please check
128 if (!AliTrackerBase::PropagateTrackTo(&track,radius,kMass,1,kTRUE,kMaxSnp,0,kFALSE,fUseMaterialBudget)) {
129 AliError(Form("Propagation to %.2f failed\n",radius));
134 //!!! Why is this smeared
135 // xyz[0]+=gRandom->Gaus(0,0.000005);
136 // xyz[1]+=gRandom->Gaus(0,0.000005);
137 // xyz[2]+=gRandom->Gaus(0,0.000005);
139 xyzf[0]=Float_t(xyz[0]);
140 xyzf[1]=Float_t(xyz[1]);
141 xyzf[2]=Float_t(xyz[2]);
143 if (TMath::Abs(track.GetZ())>kMaxZ0) continue;
144 // if (TMath::Abs(track.GetX())<iFCRadius) continue;
145 // if (TMath::Abs(track.GetX())>oFCRadius) continue;
148 AliTrackPoint pUdist; // undistorted space point
149 AliTrackPoint pDist; // distorted space point
150 // Set undistorted point
151 SetPoint(xyzf,pUdist);
152 arrUdist.AddPoint(npoints, &pUdist);
153 Int_t sector=pUdist.GetVolumeID();
155 // set distorted point
156 Float_t distPoint[3]={xyz[0],xyz[1],xyz[2]};
157 Float_t dxyz[3]={0.,0.,0.};
158 if (!fUseStepCorrection){
159 fSpaceCharge->DistortPoint(distPoint, sector);
161 fSpaceCharge->GetCorrectionIntegralDz(distPoint,sector,dxyz,5);
162 distPoint[0]-=dxyz[0];
163 distPoint[1]-=dxyz[1];
164 distPoint[2]-=dxyz[2];
166 SetPoint(distPoint, pDist);
167 arrDist.AddPoint(npoints, &pDist);
172 arrUdist.SetUniqueID(npoints);
173 arrDist.SetUniqueID(npoints);
176 //________________________________________________________________
177 void AliToyMCEventGenerator::SetPoint(Float_t xyz[3], AliTrackPoint &point)
180 // make AliTrackPoint out of AliTPCclusterMI
183 //covariance at the local frame
184 //assume 1mm distortion in y and z
186 const Double_t kSigmaY=0.1;
187 const Double_t kSigmaZ=0.1;
188 Float_t cov[6]={0,0,0, kSigmaY*kSigmaY,0,kSigmaZ*kSigmaZ};
190 const Float_t alpha = -TMath::ATan2(xyz[1],xyz[0]);
191 const Float_t sin = TMath::Sin(alpha), cos = TMath::Cos(alpha);
194 newcov[0] = cov[0]*cos*cos + 2*cov[1]*sin*cos + cov[3]*sin*sin;
195 newcov[1] = cov[1]*(cos*cos-sin*sin) + (cov[3]-cov[0])*sin*cos;
196 newcov[2] = cov[2]*cos + cov[4]*sin;
197 newcov[3] = cov[0]*sin*sin - 2*cov[1]*sin*cos + cov[3]*cos*cos;
198 newcov[4] = cov[4]*cos - cov[2]*sin;
201 // voluem ID to add later ....
203 point.SetCov(newcov);
204 // abuse volume ID for the sector number
205 point.SetVolumeID(fTPCParam->Transform0to1(xyz,i));
207 // TODO: Add sampled dE/dx (use SetCharge)
210 //________________________________________________________________
211 void AliToyMCEventGenerator::ConvertTrackPointsToLocalClusters(AliTrackPointArray &arrPoints,
212 AliToyMCTrack &tr, Double_t t0, Int_t type)
219 const Int_t npoints=Int_t(arrPoints.GetUniqueID());
222 // create an array for graphs which are used for local interpolation
223 // we need a new graph if the sector changed since then the local frame will also change
224 TObjArray arrGraphsXY(72);
225 arrGraphsXY.SetOwner();
226 TObjArray arrGraphsXZ(72);
227 arrGraphsXZ.SetOwner();
229 //create initial graph
232 //row -> sector mapping
234 for (Int_t irow=0; irow<159; ++irow) rowMap[irow]=-1;
237 // Make from the list of global space points graphs in the local frame
238 // one graph per sector is needed
239 for (Int_t ipoint=0; ipoint<npoints; ++ipoint){
240 arrPoints.GetPoint(p,ipoint);
241 Float_t xyz[3] = {p.GetX(),p.GetY(),p.GetZ()};
242 Int_t index[3] = {0,0,0};
243 Int_t row = fTPCParam->GetPadRow(xyz,index);
244 // rotate space point to local frame
245 // the angle is given by the VolumeID which was set in CrateSpacePoints
246 const Int_t sec=p.GetVolumeID();
247 if (row<0 || (sec<36 && row>62) || row>95 ) continue;
248 Double_t angle=((sec%18)*20.+10.)/TMath::RadToDeg();
249 AliTrackPoint pRot=p.Rotate(angle);
250 const Int_t secrow=row+(sec>35)*63;
251 if (rowMap[secrow]==-1) rowMap[secrow]=sec;
252 // check if we need a new graph (sector change)
256 arrGraphsXY.AddAt(grXY,sec);
257 arrGraphsXZ.AddAt(grXZ,sec);
260 //add coordinates in local frame for later interpolation
261 grXY->SetPoint(grXY->GetN(), pRot.GetX(), pRot.GetY());
262 grXZ->SetPoint(grXZ->GetN(), pRot.GetX(), pRot.GetZ());
267 // create in the center of each row a space point by using the graph to interpolate
268 // the the center of the row. This is done in xy and xz
271 AliTPCclusterMI tempCl;
273 for (Int_t irow=0; irow<159; ++irow ){
274 const Int_t sec = rowMap[irow];
275 if (sec==-1) continue;
276 const Int_t secrow = irow<63?irow:irow-63;
277 Double_t localX = fTPCParam->GetPadRowRadii(sec,secrow);
278 // get graph for the current row
285 grXY=(TGraph*)arrGraphsXY.At(sec);
286 grXZ=(TGraph*)arrGraphsXZ.At(sec);
289 // if(grXY->GetN()>1 && grXZ->GetN()>1) { //causes segmentation violation if N==1
290 // splXY=new TSpline3("splXY",grXY);
291 // splXZ=new TSpline3("splXZ",grXZ);
294 //TODO: make a cluster also in the sector w only one space point?
296 // Double_t tempX=0., tempY = 0., tempZ = 0.;
298 // grXY->GetPoint(0,tempX,localY);
299 // grXZ->GetPoint(0,tempX,localZ);
305 // check we are in an active area
306 // if (splXY->FindX(localX)<1 || splXZ->FindX(localX)<1) continue;
307 if ( localX<grXY->GetX()[0] || localX>grXY->GetX()[grXY->GetN()-1] || localX<grXZ->GetX()[0] || localX>grXZ->GetX()[grXZ->GetN()-1]) continue;
309 //get interpolated value at the center for the pad row
311 // const Double_t localY=splXY->Eval(localX/*,0x0,"S"*/);
312 // const Double_t localZ=splXZ->Eval(localX/*,0x0,"S"*/);
313 const Double_t localY=grXY->Eval(localX/*,0x0,"S"*/);
314 const Double_t localZ=grXZ->Eval(localX/*,0x0,"S"*/);
315 Float_t xyz[3]={localX,localY,localZ};
317 if (!SetupCluster(tempCl,xyz,sec,t0)) continue;
318 tempCl.SetLabel(tr.GetUniqueID(), 0);
320 if (type==0) tr.AddSpacePoint(tempCl);
321 else tr.AddDistortedSpacePoint(tempCl);
322 // printf("SetupCluster %3d: (%.2f, %.2f, %.2f), %d, %.2f\n",irow,xyz[0],xyz[1],xyz[2],sec,t0);
332 //________________________________________________________________
333 Bool_t AliToyMCEventGenerator::SetupCluster(AliTPCclusterMI &tempCl, Float_t xyz[3], Int_t sec, Double_t t0)
339 const Double_t kSigmaY = 0.1;
340 const Double_t kSigmaZ = 0.1;
341 const Double_t kMaxZ0 = fTPCParam->GetZLength();
342 //TODO: Get this from the OCDB at some point?
343 const Double_t kDriftVel = fTPCParam->GetDriftV();
349 tempCl.SetSigmaY2(kSigmaY*kSigmaY);
350 tempCl.SetSigmaZ2(kSigmaZ*kSigmaZ);
352 // transform from the local coordinates to the coordinates expressed in pad coordinates
353 Int_t index[3] = {0,sec,0};
354 fTPCParam->Transform2to3(xyz,index);
355 fTPCParam->Transform3to4(xyz,index);
356 fTPCParam->Transform4to8(xyz,index);
358 const Int_t row = index[2];
359 const Int_t nPads = fTPCParam->GetNPads(sec, row);
360 // pad is fractional, but it needs to be shifted from the center
361 // to the edge of the row
362 const Float_t pad = xyz[1] + nPads/2;
366 Float_t timeBin=Float_t(t0 + (kMaxZ0-TMath::Abs(tempCl.GetZ()))/kDriftVel);
367 tempCl.SetTimeBin(timeBin); // set time as t0 + drift time from dist z
368 tempCl.SetDetector(sec);
370 //check if we are in the active area
371 if (pad<0 || pad>=nPads) return kFALSE;
376 //________________________________________________________________
377 Bool_t AliToyMCEventGenerator::ConnectOutputFile()
380 // Create the output file name and tree and connect the event
383 fOutFile = new TFile(fOutputFileName.Data(),"recreate");
385 if (!fOutFile || !fOutFile->IsOpen()){
391 fOutTree = new TTree("toyMCtree","Tree with toyMC simulation");
392 fOutTree->Branch("event","AliToyMCEvent",&fEvent);
397 //________________________________________________________________
398 Bool_t AliToyMCEventGenerator::CloseOutputFile()
401 // close the output file
403 if (!fOutFile) return kFALSE;
412 //________________________________________________________________
413 void AliToyMCEventGenerator::FillTree()
416 if (fOutTree) fOutTree->Fill();
419 //________________________________________________________________
420 void AliToyMCEventGenerator::SetSpaceCharge(EEpsilon epsilon, EGasType gasType/*=kNeCO2_9010*/, ECollRate collRate/*=k50kHz*/)
423 // Set the space charge conditions
425 fSpaceChargeFile="$ALICE_ROOT/TPC/Calib/maps/SC";
428 fSpaceChargeFile.Append("_NeCO2");
433 fSpaceChargeFile.Append("_eps5");
436 fSpaceChargeFile.Append("_eps10");
439 fSpaceChargeFile.Append("_eps20");
444 fSpaceChargeFile.Append("_50kHz");
447 fSpaceChargeFile.Append("_precal.root");
450 //________________________________________________________________
451 void AliToyMCEventGenerator::InitSpaceCharge()
454 // init the space charge conditions
455 // this should be called after the tree was connected
458 AliInfo(Form("Using space charge map file: '%s'",fSpaceChargeFile.Data()));
460 TFile f(fSpaceChargeFile.Data());
461 fSpaceCharge=(AliTPCSpaceCharge3D*)f.Get("map");
464 AliInfo("Attaching space charge map file name to the tree");
465 fOutTree->GetUserInfo()->Add(new TObjString(fSpaceChargeFile.Data()));