]> git.uio.no Git - u/mrichter/AliRoot.git/blame_incremental - TPC/Upgrade/AliToyMCEventGenerator.cxx
First Steps for tracking from seed of associated clusters:
[u/mrichter/AliRoot.git] / TPC / Upgrade / AliToyMCEventGenerator.cxx
... / ...
CommitLineData
1#include <iostream>
2
3#include <TDatabasePDG.h>
4#include <TRandom.h>
5#include <TH2F.h>
6#include <TGeoGlobalMagField.h>
7#include <TSpline.h>
8#include <TObjString.h>
9
10#include <AliLog.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>
21
22#include "AliToyMCEvent.h"
23#include "AliToyMCTrack.h"
24
25#include "AliToyMCEventGenerator.h"
26
27ClassImp(AliToyMCEventGenerator);
28
29
30AliToyMCEventGenerator::AliToyMCEventGenerator()
31 :TObject()
32 ,fTPCParam(0x0)
33 ,fEvent(0x0)
34 ,fSpaceCharge(0x0)
35 ,fSpaceChargeFile("$ALICE_ROOT/TPC/Calib/maps/SC_NeCO2_eps5_50kHz_precal.root")
36 ,fOutputFileName("toyMC.root")
37 ,fOutFile(0x0)
38 ,fOutTree(0x0)
39 ,fUseStepCorrection(kFALSE)
40 ,fUseMaterialBudget(kFALSE)
41{
42 fTPCParam = AliTPCcalibDB::Instance()->GetParameters();
43 fTPCParam->ReadGeoMatrices();
44}
45//________________________________________________________________
46AliToyMCEventGenerator::AliToyMCEventGenerator(const AliToyMCEventGenerator &gen)
47 :TObject(gen)
48 ,fTPCParam(gen.fTPCParam)
49 ,fEvent(0x0)
50 ,fSpaceCharge(gen.fSpaceCharge)
51 ,fSpaceChargeFile(gen.fSpaceChargeFile)
52 ,fOutputFileName(gen.fOutputFileName)
53 ,fOutFile(0x0)
54 ,fOutTree(0x0)
55 ,fUseStepCorrection(gen.fUseStepCorrection)
56 ,fUseMaterialBudget(gen.fUseMaterialBudget)
57{
58 //
59}
60//________________________________________________________________
61AliToyMCEventGenerator::~AliToyMCEventGenerator()
62{
63 delete fSpaceCharge;
64}
65
66//________________________________________________________________
67Bool_t AliToyMCEventGenerator::DistortTrack(AliToyMCTrack &trackIn, Double_t t0)
68{
69 //
70 //
71 //
72
73 if(!fTPCParam) {
74 fTPCParam = AliTPCcalibDB::Instance()->GetParameters();
75 fTPCParam->ReadGeoMatrices();
76 }
77
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
83
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);
90
91 return 1;
92}
93
94//________________________________________________________________
95void AliToyMCEventGenerator::CreateSpacePoints(AliToyMCTrack &trackIn,
96 AliTrackPointArray &arrUdist,
97 AliTrackPointArray &arrDist)
98{
99 //
100 // sample the track from the inner to the outer wall of the TPC
101 // a graph is filled in local coordinates for later
102 //
103
104 const Double_t kMaxSnp = 0.85;
105 const Double_t kMaxZ0 = fTPCParam->GetZLength();
106 const Double_t kMass = TDatabasePDG::Instance()->GetParticle("pi+")->Mass();
107
108 const Double_t iFCRadius = 83.5; //radius constants found in AliTPCCorrection.cxx
109 const Double_t oFCRadius = 254.5;
110
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.};
116
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));
120 return;
121 }
122
123 Int_t npoints=0;
124
125 for (Double_t radius=iFCRadius; radius<oFCRadius; radius+=stepSize){
126
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));
130 continue;
131 }
132 track.GetXYZ(xyz);
133
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);
138
139 xyzf[0]=Float_t(xyz[0]);
140 xyzf[1]=Float_t(xyz[1]);
141 xyzf[2]=Float_t(xyz[2]);
142
143 if (TMath::Abs(track.GetZ())>kMaxZ0) continue;
144 // if (TMath::Abs(track.GetX())<iFCRadius) continue;
145 // if (TMath::Abs(track.GetX())>oFCRadius) continue;
146
147
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();
154
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);
160 } else {
161 fSpaceCharge->GetCorrectionIntegralDz(distPoint,sector,dxyz,5);
162 distPoint[0]-=dxyz[0];
163 distPoint[1]-=dxyz[1];
164 distPoint[2]-=dxyz[2];
165 }
166 SetPoint(distPoint, pDist);
167 arrDist.AddPoint(npoints, &pDist);
168
169 ++npoints;
170 }
171
172 arrUdist.SetUniqueID(npoints);
173 arrDist.SetUniqueID(npoints);
174}
175
176//________________________________________________________________
177void AliToyMCEventGenerator::SetPoint(Float_t xyz[3], AliTrackPoint &point)
178{
179 //
180 // make AliTrackPoint out of AliTPCclusterMI
181 //
182
183 //covariance at the local frame
184 //assume 1mm distortion in y and z
185 Int_t i[3]={0,0,0};
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};
189
190 const Float_t alpha = -TMath::ATan2(xyz[1],xyz[0]);
191 const Float_t sin = TMath::Sin(alpha), cos = TMath::Cos(alpha);
192
193 Float_t newcov[6];
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;
199 newcov[5] = cov[5];
200
201 // voluem ID to add later ....
202 point.SetXYZ(xyz);
203 point.SetCov(newcov);
204 // abuse volume ID for the sector number
205 point.SetVolumeID(fTPCParam->Transform0to1(xyz,i));
206
207 // TODO: Add sampled dE/dx (use SetCharge)
208}
209
210//________________________________________________________________
211void AliToyMCEventGenerator::ConvertTrackPointsToLocalClusters(AliTrackPointArray &arrPoints,
212 AliToyMCTrack &tr, Double_t t0, Int_t type)
213{
214 //
215 //
216 //
217
218
219 const Int_t npoints=Int_t(arrPoints.GetUniqueID());
220 Int_t secOld=-1;
221
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();
228 AliTrackPoint p;
229 //create initial graph
230 TGraph *grXY=0x0;
231 TGraph *grXZ=0x0;
232 //row -> sector mapping
233 Int_t rowMap[159];
234 for (Int_t irow=0; irow<159; ++irow) rowMap[irow]=-1;
235
236 // 1. Step
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)
253 if (secOld!=sec){
254 grXY=new TGraph;
255 grXZ=new TGraph;
256 arrGraphsXY.AddAt(grXY,sec);
257 arrGraphsXZ.AddAt(grXZ,sec);
258 }
259
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());
263 secOld=sec;
264 }
265
266 // 2. Step
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
269 TSpline3 *splXY=0x0;
270 TSpline3 *splXZ=0x0;
271 AliTPCclusterMI tempCl;
272 secOld=-1;
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
279 if (sec!=secOld){
280 delete splXY;
281 splXY=0x0;
282 delete splXZ;
283 splXZ=0x0;
284
285 grXY=(TGraph*)arrGraphsXY.At(sec);
286 grXZ=(TGraph*)arrGraphsXZ.At(sec);
287 if (!grXY) continue;
288
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);
292 }
293 else {
294 //TODO: make a cluster also in the sector w only one space point?
295 continue;
296 // Double_t tempX=0., tempY = 0., tempZ = 0.;
297
298 // grXY->GetPoint(0,tempX,localY);
299 // grXZ->GetPoint(0,tempX,localZ);
300 }
301
302 }
303 secOld=sec;
304
305 // check we are in an active area
306 if (splXY->FindX(localX)<1 || splXZ->FindX(localX)<1) continue;
307
308 //get interpolated value at the center for the pad row
309 // using splines
310 const Double_t localY=splXY->Eval(localX/*,0x0,"S"*/);
311 const Double_t localZ=splXZ->Eval(localX/*,0x0,"S"*/);
312 Float_t xyz[3]={localX,localY,localZ};
313
314 if (!SetupCluster(tempCl,xyz,sec,t0)) continue;
315 tempCl.SetLabel(tr.GetUniqueID(), 0);
316
317 if (type==0) tr.AddSpacePoint(tempCl);
318 else tr.AddDistortedSpacePoint(tempCl);
319// printf("SetupCluster %3d: (%.2f, %.2f, %.2f), %d, %.2f\n",irow,xyz[0],xyz[1],xyz[2],sec,t0);
320 }
321
322 delete splXY;
323 splXY=0x0;
324 delete splXZ;
325 splXZ=0x0;
326
327}
328
329//________________________________________________________________
330Bool_t AliToyMCEventGenerator::SetupCluster(AliTPCclusterMI &tempCl, Float_t xyz[3], Int_t sec, Double_t t0)
331{
332 //
333 //
334 //
335
336 const Double_t kSigmaY = 0.1;
337 const Double_t kSigmaZ = 0.1;
338 const Double_t kMaxZ0 = fTPCParam->GetZLength();
339 //TODO: Get this from the OCDB at some point?
340 const Double_t kDriftVel = fTPCParam->GetDriftV();
341
342 tempCl.SetX(xyz[0]);
343 tempCl.SetY(xyz[1]);
344 tempCl.SetZ(xyz[2]);
345
346 tempCl.SetSigmaY2(kSigmaY*kSigmaY);
347 tempCl.SetSigmaZ2(kSigmaZ*kSigmaZ);
348
349 // transform from the local coordinates to the coordinates expressed in pad coordinates
350 Int_t index[3] = {0,sec,0};
351 fTPCParam->Transform2to3(xyz,index);
352 fTPCParam->Transform3to4(xyz,index);
353 fTPCParam->Transform4to8(xyz,index);
354
355 const Int_t row = index[2];
356 const Int_t nPads = fTPCParam->GetNPads(sec, row);
357 // pad is fractional, but it needs to be shifted from the center
358 // to the edge of the row
359 const Float_t pad = xyz[1] + nPads/2;
360
361 tempCl.SetRow(row);
362 tempCl.SetPad(pad);
363 Float_t timeBin=Float_t(t0 + (kMaxZ0-TMath::Abs(tempCl.GetZ()))/kDriftVel);
364 tempCl.SetTimeBin(timeBin); // set time as t0 + drift time from dist z
365 tempCl.SetDetector(sec);
366
367 //check if we are in the active area
368 if (pad<0 || pad>=nPads) return kFALSE;
369
370 return kTRUE;
371}
372
373//________________________________________________________________
374Bool_t AliToyMCEventGenerator::ConnectOutputFile()
375{
376 //
377 // Create the output file name and tree and connect the event
378 //
379
380 fOutFile = new TFile(fOutputFileName.Data(),"recreate");
381
382 if (!fOutFile || !fOutFile->IsOpen()){
383 delete fOutFile;
384 fOutFile=0x0;
385 return kFALSE;
386 }
387
388 fOutTree = new TTree("toyMCtree","Tree with toyMC simulation");
389 fOutTree->Branch("event","AliToyMCEvent",&fEvent);
390
391 return kTRUE;
392}
393
394//________________________________________________________________
395Bool_t AliToyMCEventGenerator::CloseOutputFile()
396{
397 //
398 // close the output file
399 //
400 if (!fOutFile) return kFALSE;
401 fOutFile->Write();
402 fOutFile->Close();
403 delete fOutFile;
404 fOutFile=0x0;
405
406 return kTRUE;
407}
408
409//________________________________________________________________
410void AliToyMCEventGenerator::FillTree()
411{
412 // fill the tree
413 if (fOutTree) fOutTree->Fill();
414}
415
416//________________________________________________________________
417void AliToyMCEventGenerator::SetSpaceCharge(EEpsilon epsilon, EGasType gasType/*=kNeCO2_9010*/, ECollRate collRate/*=k50kHz*/)
418{
419 //
420 // Set the space charge conditions
421 //
422 fSpaceChargeFile="$ALICE_ROOT/TPC/Calib/maps/SC";
423 switch (gasType) {
424 case kNeCO2_9010:
425 fSpaceChargeFile.Append("_NeCO2");
426 break;
427 }
428 switch (epsilon) {
429 case kEps5:
430 fSpaceChargeFile.Append("_eps5");
431 break;
432 case kEps10:
433 fSpaceChargeFile.Append("_eps10");
434 break;
435 case kEps20:
436 fSpaceChargeFile.Append("_eps20");
437 break;
438 }
439 switch (collRate) {
440 case k50kHz:
441 fSpaceChargeFile.Append("_50kHz");
442 break;
443 }
444 fSpaceChargeFile.Append("_precal.root");
445}
446
447//________________________________________________________________
448void AliToyMCEventGenerator::InitSpaceCharge()
449{
450 //
451 // init the space charge conditions
452 // this should be called after the tree was connected
453 //
454
455 AliInfo(Form("Using space charge map file: '%s'",fSpaceChargeFile.Data()));
456
457 TFile f(fSpaceChargeFile.Data());
458 fSpaceCharge=(AliTPCSpaceCharge3D*)f.Get("map");
459
460 if (fOutTree){
461 AliInfo("Attaching space charge map file name to the tree");
462 fOutTree->GetUserInfo()->Add(new TObjString(fSpaceChargeFile.Data()));
463 }
464}