3 #include "AliHLTStandardIncludes.h"
4 #include "AliHLTRootTypes.h"
8 #include "AliHLTTrack.h"
9 #include "AliHLTKalmanTrack.h"
10 #include "AliHLTBenchmark.h"
11 #include "AliHLTMemHandler.h"
12 #include "AliHLTFileHandler.h"
13 #include "AliHLTDataHandler.h"
14 #include "AliHLTTransform.h"
15 #include "AliHLTSpacePointData.h"
16 #include "AliHLTDigitData.h"
17 #include "AliHLTLogging.h"
18 #include "AliHLTTrackArray.h"
19 #include "AliHLTTrackSegmentData.h"
20 #include "AliHLTInterMerger.h"
21 #include "AliHLTTrackMerger.h"
22 #include "AliHLTKalman.h"
28 ClassImp(AliHLTKalman)
30 AliHLTKalman::AliHLTKalman(Char_t *datapath, Int_t *slice, Int_t min_clusters = 0){
43 sprintf(fPath,"%s",datapath);
44 fMinPointsOnTrack = 0; //min_clusters;
48 // NB! fNrow under only for single-patch, must include other possibilities
49 // later on. ?? Maybe better also to put it in an Init-function
51 fRow[0][1] = AliHLTTransform::GetLastRow(-1);
57 AliHLTKalman::~AliHLTKalman()
60 if (fBenchmark) delete fBenchmark;
63 void AliHLTKalman::Init()
65 fBenchmark = new AliHLTBenchmark();
68 void AliHLTKalman::LoadTracks(Int_t event, Bool_t sp)
70 // Load space points and tracks from conformal tracker
71 // Must also be possible to take seeds (and clusters) from Hough-transform??
73 Double_t initTime,cpuTime;
74 initTime = GetCpuTime();
75 fBenchmark->Start("Load tracks");
79 AliHLTFileHandler *clusterfile[36][6];
80 for(Int_t s=fMinSlice; s<=fMaxSlice; s++)
82 for(Int_t p=0; p<AliHLTTransform::GetNPatches(); p++)
91 clusterfile[s][p] = new AliHLTFileHandler();
93 sprintf(fname,"%s/points_%d_%d.raw",fPath,s,patch);
95 sprintf(fname,"%s/points_%d_%d_%d.raw",fPath,event,s,patch);
96 if(!clusterfile[s][p]->SetBinaryInput(fname))
98 LOG(AliHLTLog::kError,"AliHLTEvaluation::Setup","File Open")
99 <<"Inputfile "<<fname<<" does not exist"<<ENDLOG;
100 delete clusterfile[s][p];
101 clusterfile[s][p] = 0;
104 fClusters[s][p] = (AliHLTSpacePointData*)clusterfile[s][p]->Allocate();
105 clusterfile[s][p]->Binary2Memory(fNcl[s][p],fClusters[s][p]);
106 clusterfile[s][p]->CloseBinaryInput();
109 delete clusterfile[s][p];
114 //sprintf(fname,"%s/kalmantracks_%d.raw",fPath,event);
115 sprintf(fname,"%s/tracks_%d.raw",fPath,event);
116 AliHLTFileHandler *tfile = new AliHLTFileHandler();
117 if(!tfile->SetBinaryInput(fname)){
118 LOG(AliHLTLog::kError,"AliHLTEvaluation::Setup","File Open")
119 <<"Inputfile "<<fname<<" does not exist"<<ENDLOG;
124 fTracks = new AliHLTTrackArray();
125 tfile->Binary2TrackArray(fTracks);
126 tfile->CloseBinaryInput();
128 fBenchmark->Stop("Load tracks");
129 cpuTime = GetCpuTime() - initTime;
130 LOG(AliHLTLog::kInformational,"AliHLTKalman::LoadTracks()","Timing")
131 <<"Loading tracks in "<<cpuTime*1000<<" ms"<<ENDLOG;
135 void AliHLTKalman::ProcessTracks()
137 // Run the Kalman filter algorithm on the loaded tracks.
138 // If the track is OK, the loaded track is saved in file kalmantracks_0.raw
139 // The kalman filter variables (that is the state vector, covariance matrix
140 // and chi2) is written to root-file kalmantracks.root.
141 // Should be extended to correct the track parameters of the loaded track
142 // or save the kalmantrack instead of the loaded track.??
145 Double_t initTime,cpuTime;
146 initTime = GetCpuTime();
148 fBenchmark->Start("Process tracks");
152 fKalmanTracks = new AliHLTTrackArray();
154 // Make a ntuple to store state vector, covariance matrix and chisquare
155 // Will eventually not need a TTree??
156 TNtuple *kalmanTree = new TNtuple("kalmanTree","kalmantracks","x0:x1:x2:x3:x4:c0:c1:c2:c3:c4:c5:c6:c7:c8:c9:c10:c11:c12:c13:c14:chisq");
159 // Go through the tracks from conformal or hough tracker
160 for (Int_t iTrack = 0; iTrack < fTracks->GetNTracks(); iTrack++)
162 /*Double_t initTime,cpuTime;
163 initTime = GetCpuTime();
164 fBenchmark->Start("Process tracks");*/
166 AliHLTTrack *track = (AliHLTTrack*)fTracks->GetCheckedTrack(iTrack);
167 if (!track) continue;
168 if (track->GetNumberOfPoints() < fMinPointsOnTrack) continue;
170 AliHLTKalmanTrack *kalmantrack = new AliHLTKalmanTrack();
174 /*if (InitKalmanTrack(kalmantrack, track) == 0)
180 if (MakeKalmanSeed(kalmantrack,track) ==0)
186 if (Propagate(kalmantrack, track) == 0)
191 if (save) {// cout << track->GetPt() << endl;
193 kalmantrack->GetStateVector(x);
195 kalmantrack->GetCovariance(c);
196 Float_t chisq = kalmantrack->GetChisq();
219 // Add the track to the trackarray
220 AliHLTTrack *outtrack = (AliHLTTrack*)fKalmanTracks->NextTrack();
221 outtrack->Set(track);
222 // SET THE PARAMETERS ACCORDING TO KALMAN FILTER
223 outtrack->SetTgl(x[3]);
224 // The factor 2 in the expression for Pt is not included in the similar offline expression. However
225 // it should be like this if I use a factor 1/2 in the calculation of par4??
226 //outtrack->SetPt(1/(2*TMath::Abs(1e-9*TMath::Abs(x[4])/x[4] + x[4])/(0.0029980*AliHLTTransform::GetBField())));
227 //outtrack->SetPt(1/(TMath::Abs(x[4])/0.0029980*AliHLTTransform::GetBField()));
228 outtrack->SetPsi(x[2]);
229 //outtrack->Set(track);
231 // Fill the ntuple with the state vector, covariance matrix and
233 kalmanTree->Fill(meas);
239 /*fBenchmark->Stop("Process tracks");
240 cpuTime = GetCpuTime() - initTime;
241 LOG(AliHLTLog::kInformational,"AliHLTKalman::ProcessTracks()","Timing")
242 <<"Processed track "<<iTrack<<" in "<<cpuTime*1000<<" ms"<<ENDLOG;*/
245 fBenchmark->Stop("Process tracks");
246 cpuTime = GetCpuTime() - initTime;
247 LOG(AliHLTLog::kInformational,"AliHLTKalman::ProcessTracks()","Timing")
248 <<"Process tracks in "<<cpuTime*1000<<" ms"<<ENDLOG;
253 sprintf(tname,"%s/kalmantracks_%d.raw",fWriteOutPath,fEvent);
254 AliHLTMemHandler *mem = new AliHLTMemHandler();
255 mem->SetBinaryOutput(tname);
256 mem->TrackArray2Binary(fKalmanTracks);
257 mem->CloseBinaryOutput();
261 // This will be removed??
262 TFile *out = new TFile("kalmantracks.root","recreate");
270 Int_t AliHLTKalman::MakeKalmanSeed(AliHLTKalmanTrack *kalmantrack, AliHLTTrack *track)
272 Int_t num_of_clusters = track->GetNumberOfPoints();
274 UInt_t *hitnum = track->GetHitNumbers();
278 Int_t slice0 = (id>>25) & 0x7f;
279 Int_t patch0 = (id>>22) & 0x7;
280 UInt_t pos0 = id&0x3fffff;
281 AliHLTSpacePointData *points0 = fClusters[slice0][patch0];
283 id = hitnum[Int_t(num_of_clusters/2)];
284 Int_t slice1 = (id>>25) & 0x7f;
285 Int_t patch1 = (id>>22) & 0x7;
286 UInt_t pos1 = id&0x3fffff;
287 AliHLTSpacePointData *points1 = fClusters[slice1][patch1];
289 id = hitnum[num_of_clusters-1];
290 Int_t slice2 = (id>>25) & 0x7f;
291 Int_t patch2 = (id>>22) & 0x7;
292 UInt_t pos2 = id&0x3fffff;
293 AliHLTSpacePointData *points2 = fClusters[slice2][patch2];
295 return kalmantrack->MakeSeed(track, points0, pos0, slice0, points1, pos1, slice1, points2, pos2, slice2);
298 Int_t AliHLTKalman::InitKalmanTrack(AliHLTKalmanTrack *kalmantrack, AliHLTTrack *track)
300 UInt_t *hitnum = track->GetHitNumbers();
304 Int_t slice = (id>>25) & 0x7f;
305 Int_t patch = (id>>22) & 0x7;
306 UInt_t pos = id&0x3fffff;
307 AliHLTSpacePointData *points = fClusters[slice][patch];
309 return kalmantrack->Init(track, points, pos, slice);
312 Int_t AliHLTKalman::Propagate(AliHLTKalmanTrack *kalmantrack, AliHLTTrack *track)
314 // This function propagtes the kalmantrack to the next cluster of the loaded
316 Int_t num_of_clusters = track->GetNumberOfPoints();
317 UInt_t *hitnum = track->GetHitNumbers();
321 for (Int_t icl = 1; icl < num_of_clusters; icl++)
325 Int_t slice = (id>>25) & 0x7f;
326 Int_t patch = (id>>22) & 0x7;
327 UInt_t pos = id&0x3fffff;
329 AliHLTSpacePointData *points = fClusters[slice][patch];
330 if (!points) continue;
331 if (kalmantrack->Propagate(points,pos,slice) == 0)
338 // If too many clusters are missing, the track is no good
339 if (badpoint >= UInt_t(num_of_clusters*0.8)) return 0;
343 Double_t AliHLTKalman::GetCpuTime()
345 //Return the Cputime in seconds.
347 gettimeofday( &tv, NULL );
348 return tv.tv_sec+(((Double_t)tv.tv_usec)/1000000.);
349 //return (Double_t)(clock()) / CLOCKS_PER_SEC;