]>
Commit | Line | Data |
---|---|---|
f078d002 | 1 | // $Id$ |
2 | ||
3 | //************************************************************************** | |
4 | //* This file is property of and copyright by the ALICE HLT Project * | |
5 | //* ALICE Experiment at CERN, All rights reserved. * | |
6 | //* * | |
7 | //* Primary Authors: Timur Pocheptsov <Timur.Pocheptsov@cern.ch> * | |
8 | //* for The ALICE HLT Project. * | |
9 | //* * | |
10 | //* Permission to use, copy, modify and distribute this software and its * | |
11 | //* documentation strictly for non-commercial purposes is hereby granted * | |
12 | //* without fee, provided that the above copyright notice appears in all * | |
13 | //* copies and that both the copyright notice and this permission notice * | |
14 | //* appear in the supporting documentation. The authors make no claims * | |
15 | //* about the suitability of this software for any purpose. It is * | |
16 | //* provided "as is" without express or implied warranty. * | |
17 | //************************************************************************** | |
18 | ||
19 | /// @file AliHLTVertexFinderBase.cxx | |
20 | /// @author Timur Pocheptsov | |
21 | /// @date 2010-12-26 | |
22 | /// @brief Base class for vertex finder components | |
23 | /// | |
24 | ||
25 | #include <stdexcept> | |
26 | #include <cmath> | |
27 | ||
28 | //AliHLTExternalTrackParam uses typedes from Rtypes.h | |
29 | //but does not include it. | |
30 | #include "Rtypes.h" | |
31 | ||
32 | #include "AliHLTExternalTrackParam.h" | |
33 | #include "AliHLTGlobalBarrelTrack.h" | |
34 | #include "AliHLTVertexFinderBase.h" | |
35 | #include "AliExternalTrackParam.h" | |
36 | #include "AliESDVertex.h" | |
37 | #include "AliESDEvent.h" | |
38 | #include "AliESDtrack.h" | |
39 | #include "AliKFVertex.h" | |
40 | #include "AliESDv0.h" | |
41 | ||
42 | ClassImp(AliHLTVertexFinderBase) | |
43 | ||
44 | //_________________________________________________________________ | |
45 | bool AliHLTVertexFinderBase::AliHLTTrackInfo::IsKFFinite()const | |
46 | { | |
47 | //Check KF particle. | |
48 | ||
49 | //In C99, 'isfinite' is a macro. | |
50 | //But still, I add using directive, in case | |
51 | //it's a function in std. | |
52 | using namespace std; | |
53 | ||
54 | for (int i = 0; i < 8; ++i) | |
55 | if (!isfinite(fParticle.GetParameter(i))) | |
56 | return false; | |
57 | for (int i = 0; i < 36; ++i)//Not sure, probably, 27 is enough. | |
58 | if (!isfinite(fParticle.GetCovariance(i))) | |
59 | return false; | |
60 | ||
61 | return true; | |
62 | } | |
63 | ||
64 | //_________________________________________________________________ | |
65 | void AliHLTVertexFinderBase::ReadESDTracks() | |
66 | { | |
67 | //Try to read input tracks from AliESDEvent. | |
68 | //Use esd track's index as "track id" (fID). | |
69 | //IMPORTANT: fTrackInfos is _NOT_ cleared here, | |
70 | //must be done externally (if you need to). | |
71 | ||
72 | const TObject* iter = GetFirstInputObject(kAliHLTDataTypeESDObject); | |
73 | for (; iter; iter = GetNextInputObject()) { | |
74 | //Cast away constness before dynamic_cast. | |
75 | AliESDEvent* esd = dynamic_cast<AliESDEvent*>((TObject*)iter); | |
76 | if (!esd)//From original code. Not sure, if this is possible at all. | |
77 | continue; | |
78 | ||
79 | esd->GetStdContent(); | |
80 | ||
81 | const int nTracks = esd->GetNumberOfTracks(); | |
82 | if (nTracks) | |
83 | fTrackInfos.reserve(nTracks + fTrackInfos.size()); | |
84 | ||
85 | for (int i = 0; i < nTracks; ++i) { | |
86 | AliESDtrack* pTrack = esd->GetTrack(i); | |
87 | //This checks of track parameters are | |
88 | //from the original global vertexer. | |
89 | if (!pTrack) | |
90 | continue; | |
91 | if (pTrack->GetKinkIndex(0) > 0) | |
92 | continue; | |
93 | if (!(pTrack->GetStatus() & AliESDtrack::kTPCin)) | |
94 | continue; | |
95 | ||
96 | //i: track id, 211: pid, false: not used in primary, 0.: prim. deviation. | |
97 | AliHLTTrackInfo newTrackInfo(i, *pTrack, 211, false, 0.); | |
98 | if (!newTrackInfo.IsKFFinite()) | |
99 | continue; | |
100 | ||
101 | fTrackInfos.push_back(newTrackInfo); | |
102 | } | |
103 | //Only one esd event is taken. | |
104 | break; | |
105 | } | |
106 | } | |
107 | ||
108 | //________________________________________________________________________ | |
109 | void AliHLTVertexFinderBase::ReadHLTTracks(const AliHLTComponentDataType& blockType) | |
110 | { | |
111 | //Read HLT tracks as input. | |
112 | //IMPORTANT: fTrackInfos is _NOT_ cleared here, | |
113 | //must be done externally (if you need it). | |
114 | ||
115 | const AliHLTComponentBlockData* block = GetFirstInputBlock(blockType); | |
116 | for (; block; block = GetNextInputBlock()) { | |
117 | const AliHLTTracksData* dataPtr = static_cast<AliHLTTracksData*>(block->fPtr); | |
118 | const AliHLTExternalTrackParam* hltTrk = dataPtr->fTracklets; | |
119 | ||
120 | const int nTracks = dataPtr->fCount; | |
121 | if (nTracks) | |
122 | fTrackInfos.reserve(fTrackInfos.size() + nTracks); | |
123 | ||
124 | for (int i = 0; i < nTracks; ++i) { | |
125 | //Ugly conversion from one track format to another | |
126 | //and to AliKFParitcle then, to be changed later. | |
127 | AliHLTGlobalBarrelTrack tmpTrk(*hltTrk); | |
128 | ||
129 | AliHLTTrackInfo newTrackInfo(hltTrk->fTrackID, tmpTrk, 211, false, 0.); | |
130 | if (!newTrackInfo.IsKFFinite()) | |
131 | continue; | |
132 | ||
133 | fTrackInfos.push_back(newTrackInfo); | |
134 | ||
135 | const unsigned dSize = sizeof(AliHLTExternalTrackParam) | |
136 | + hltTrk->fNPoints * sizeof(unsigned); | |
137 | hltTrk = (AliHLTExternalTrackParam*)((char *)hltTrk + dSize); | |
138 | } | |
139 | } | |
140 | } | |
141 | ||
142 | //________________________________________________________________________ | |
143 | void AliHLTVertexFinderBase::FillESD(AliESDEvent* esd, AliKFVertex* vtx, | |
144 | const void* primData, const void* v0Data) | |
145 | { | |
146 | //Put the output of a primary finder and v0 finder to the esd event. | |
147 | //Code was taken from the original global vertexer. | |
148 | //Code is an absolute mess. | |
149 | ||
150 | typedef std::map<int, int> Map_t; | |
151 | typedef Map_t::const_iterator MapIter_t; | |
152 | ||
153 | //Map esdId -> esdTrackIndex. | |
154 | Map_t mapId; | |
155 | ||
156 | double params[3]; | |
157 | for (int i = 0; i < 3; ++i) | |
158 | params[i] = vtx->Parameters()[i]; | |
159 | double cov[6]; | |
160 | for (int i = 0; i < 6; ++i) | |
161 | cov[i] = vtx->CovarianceMatrix()[i]; | |
162 | ||
163 | AliESDVertex vESD(params, cov, vtx->GetChi2(), vtx->GetNContributors()); | |
164 | esd->SetPrimaryVertexTPC(&vESD); | |
165 | esd->SetPrimaryVertexTracks(&vESD); | |
166 | ||
167 | //Relate tracks to the primary vertex | |
168 | const PrimaryFinderBlock* prim = static_cast<const PrimaryFinderBlock*>(primData); | |
169 | const int nESDTracks = esd->GetNumberOfTracks(); | |
170 | std::vector<bool> constrainedToVtx(nESDTracks); | |
171 | ||
172 | if (prim->fFitTracksFlag) { | |
173 | for (int i = 0; i < nESDTracks; ++i) { | |
174 | if (!esd->GetTrack(i)) | |
175 | continue; | |
176 | ||
177 | mapId[esd->GetTrack(i)->GetID()] = i; | |
178 | } | |
179 | ||
180 | for (int i = 0; i < prim->fNPrimaryTracks; ++i) { | |
181 | MapIter_t it = mapId.find(prim->fPrimTrackIds[i]); | |
182 | if (it == mapId.end()) | |
183 | continue; | |
184 | const int itr = it->second; | |
185 | //100. is an argument for parameter maxd in AliESDtrack - cut on impact parameter. | |
186 | esd->GetTrack(itr)->RelateToVertex(&vESD, esd->GetMagneticField(), 100.); | |
187 | constrainedToVtx[itr] = true; | |
188 | } | |
189 | } | |
190 | ||
191 | //Add v0s. | |
192 | const int* v0s = static_cast<const int*>(v0Data); | |
193 | const int nV0s = v0s[0]; | |
194 | ++v0s; | |
195 | for (int i = 0; i < nV0s; ++i) { | |
196 | MapIter_t it = mapId.find(v0s[2 * i]); | |
197 | if (it==mapId.end()) | |
198 | continue; | |
199 | const int iTr = it->second; | |
200 | ||
201 | it = mapId.find(v0s[2 * i + 1]); | |
202 | if (it == mapId.end()) | |
203 | continue; | |
204 | const int jTr = it->second; | |
205 | ||
206 | AliESDv0 v0(*esd->GetTrack(iTr), iTr, *esd->GetTrack(jTr), jTr); | |
207 | esd->AddV0(&v0); | |
208 | // relate the tracks to the vertex | |
209 | if (prim->fFitTracksFlag) { | |
210 | if (constrainedToVtx[iTr] || constrainedToVtx[jTr]) | |
211 | continue; | |
212 | ||
213 | double pos[3]; | |
214 | double sigma[3] = {.1, .1, .1}; | |
215 | v0.XvYvZv(pos); | |
216 | AliESDVertex v0ESD(pos, sigma); | |
217 | esd->GetTrack(iTr)->RelateToVertex(&v0ESD, esd->GetMagneticField(), 100.); | |
218 | esd->GetTrack(jTr)->RelateToVertex(&v0ESD, esd->GetMagneticField(), 100.); | |
219 | constrainedToVtx[iTr] = true; | |
220 | constrainedToVtx[jTr] = true; | |
221 | } | |
222 | } | |
223 | } | |
224 | ||
225 | namespace AliHLTUtility | |
226 | { | |
227 | ||
228 | //_______________________________________________________________________ | |
229 | Parameter::Parameter() | |
230 | : fWasSet(false), | |
231 | fConstraint(none), | |
232 | fBool(0), | |
233 | fInt(0), | |
234 | fDouble(0), | |
235 | fCompound(0), | |
236 | fConstraintChecker(0) | |
237 | { | |
238 | //Default ctor. | |
239 | } | |
240 | ||
241 | //_______________________________________________________________________ | |
242 | Parameter::Parameter(bool* b) | |
243 | : fWasSet(false), | |
244 | fConstraint(none), | |
245 | fBool(b), | |
246 | fInt(0), | |
247 | fDouble(0), | |
248 | fCompound(0), | |
249 | fConstraintChecker(0) | |
250 | { | |
251 | //Parameter of type bool. | |
252 | } | |
253 | ||
254 | //_______________________________________________________________________ | |
255 | Parameter::Parameter(int* i, Constraint c) | |
256 | : fWasSet(false), | |
257 | fConstraint(c), | |
258 | fBool(0), | |
259 | fInt(i), | |
260 | fDouble(0), | |
261 | fCompound(0), | |
262 | fConstraintChecker(0) | |
263 | { | |
264 | //Parameter of type int. | |
265 | SetConstraintChecker(); | |
266 | } | |
267 | ||
268 | //_______________________________________________________________________ | |
269 | Parameter::Parameter(double* d, Constraint c) | |
270 | : fWasSet(false), | |
271 | fConstraint(c), | |
272 | fBool(0), | |
273 | fInt(0), | |
274 | fDouble(d), | |
275 | fCompound(0), | |
276 | fConstraintChecker(0) | |
277 | { | |
278 | //Parameter of type double. | |
279 | SetConstraintChecker(); | |
280 | } | |
281 | ||
282 | //_______________________________________________________________________ | |
283 | Parameter::Parameter(CompoundType *ct) | |
284 | : fWasSet(false), | |
285 | fConstraint(none), | |
286 | fBool(0), | |
287 | fInt(0), | |
288 | fDouble(0), | |
289 | fCompound(ct), | |
290 | fConstraintChecker(0) | |
291 | { | |
292 | //Parameter of more complex user-defined type. | |
293 | //All checks and conversions must be implemented | |
294 | //by user of CompoundType. | |
295 | } | |
296 | ||
297 | //_______________________________________________________________________ | |
298 | unsigned Parameter::SetParameter(unsigned argc, const char** argv, unsigned currPos) | |
299 | { | |
300 | //Set parameter from command line tokens. | |
301 | ||
302 | //It's up to compound parameter to parse. | |
303 | if (fCompound) | |
304 | return fCompound->SetParameter(argc, argv, currPos); | |
305 | ||
306 | //Now, int, bool or double must be set from a string. | |
307 | //Primitive checks are done here. | |
308 | if (currPos == argc) | |
309 | throw std::runtime_error("value expected"); | |
310 | if (fWasSet) | |
311 | throw std::runtime_error("parameter was set already"); | |
312 | ||
313 | const TString val(argv[currPos]); | |
314 | if (!val.Length()) | |
315 | throw std::runtime_error("value expected"); | |
316 | ||
317 | if (fBool) { | |
318 | if (val.Length() != 1 || (val[0] != '0' && val[0] != '1')) | |
319 | throw std::runtime_error("expected 0 or 1 for bool parameter"); | |
320 | *fBool = val[0] - '0'; | |
321 | } else if (fInt) | |
322 | *fInt = val.Atoi(); | |
323 | else | |
324 | *fDouble = val.Atof(); | |
325 | ||
326 | if (fConstraintChecker) | |
327 | fConstraintChecker(*this); | |
328 | ||
329 | fWasSet = true; | |
330 | ||
331 | //For double, int and bool - only one element of argv is processed. | |
332 | return 1; | |
333 | } | |
334 | ||
335 | //_______________________________________________________________________ | |
336 | void Parameter::SetConstraintChecker() | |
337 | { | |
338 | //Set function pointer. | |
339 | if (fConstraint == positive) | |
340 | fConstraintChecker = CheckPositive; | |
341 | else if (fConstraint == nonNegative) | |
342 | fConstraintChecker = CheckNonNegative; | |
343 | //No constraints for bool or CompoundType. | |
344 | } | |
345 | ||
346 | //Aux. functions to check constraints. | |
347 | //_______________________________________________________________________ | |
348 | void Parameter::CheckPositive(const Parameter& param) | |
349 | { | |
350 | //val > 0 | |
351 | if (param.fInt) { | |
352 | if (*param.fInt <= 0) | |
353 | throw std::runtime_error("integer parameter must be positive"); | |
354 | } else if (param.fDouble) { | |
355 | if (*param.fDouble <= 0.)//Hmmmm. | |
356 | throw std::runtime_error("double parameter must be positive"); | |
357 | } | |
358 | //For bool or CompoundType there is no check. | |
359 | } | |
360 | ||
361 | //_______________________________________________________________________ | |
362 | void Parameter::CheckNonNegative(const Parameter& param) | |
363 | { | |
364 | //val >= 0 | |
365 | if (param.fInt) { | |
366 | if (*param.fInt < 0) | |
367 | throw std::runtime_error("integer parameter must be non-negative"); | |
368 | } else if (param.fDouble) { | |
369 | if (*param.fDouble < 0.) | |
370 | throw std::runtime_error("double parameter must be non-negative"); | |
371 | } | |
372 | //For bool or CompoundType there is no check. | |
373 | } | |
374 | ||
375 | ||
376 | //Command line arguments parser. | |
377 | ||
378 | //_______________________________________________________________________ | |
379 | void CmdLineParser::Add(const TString& cmd, bool* b) | |
380 | { | |
381 | //Command with bool parameter. | |
382 | if (fParameters.find(cmd) == fParameters.end()) | |
383 | fParameters[cmd] = Parameter(b); | |
384 | } | |
385 | ||
386 | //_______________________________________________________________________ | |
387 | void CmdLineParser::Add(const TString& cmd, int* i, Parameter::Constraint c) | |
388 | { | |
389 | //Command with int parameter, with constraint. | |
390 | if (fParameters.find(cmd) == fParameters.end()) | |
391 | fParameters[cmd] = Parameter(i, c); | |
392 | } | |
393 | ||
394 | //_______________________________________________________________________ | |
395 | void CmdLineParser::Add(const TString& cmd, double* d, Parameter::Constraint c) | |
396 | { | |
397 | //Coomand with a parameter of type double, possibly with a constraint. | |
398 | if (fParameters.find(cmd) == fParameters.end()) | |
399 | fParameters[cmd] = Parameter(d, c); | |
400 | } | |
401 | ||
402 | //_______________________________________________________________________ | |
403 | void CmdLineParser::Add(const TString& cmd, CompoundType *ct) | |
404 | { | |
405 | //Command with a parameter of compound type. | |
406 | if (fParameters.find(cmd) == fParameters.end()) | |
407 | fParameters[cmd] = Parameter(ct); | |
408 | } | |
409 | ||
410 | //_______________________________________________________________________ | |
411 | void CmdLineParser::IgnoreCommand(const TString& cmd, unsigned nParams) | |
412 | { | |
413 | //Command and number of parameters to ignore. | |
414 | //I do not check, if it's in map already. | |
415 | fIgnored[cmd] = nParams; | |
416 | } | |
417 | ||
418 | //_______________________________________________________________________ | |
419 | int CmdLineParser::Parse(unsigned argc, const char** argv, unsigned currPos) | |
420 | { | |
421 | //Parse. Returns number of processed arguments. | |
422 | //Negative number - error (as it's in HLT) | |
423 | fError.Clear(); | |
424 | ||
425 | unsigned nProcessed = 0; | |
426 | while (currPos < argc) { | |
427 | //Command. | |
428 | const TString command(argv[currPos]); | |
429 | //Check, if this command must be ignored. | |
430 | SkipMapIter_t skipCmd = fIgnored.find(command); | |
431 | if (skipCmd != fIgnored.end()) { | |
432 | //Command and its parameters must be skipped | |
433 | nProcessed += 1 + skipCmd->second; | |
434 | currPos += 1 + skipCmd->second; | |
435 | continue; | |
436 | } | |
437 | ||
438 | //Corresponding parameter. | |
439 | MapIter_t it = fParameters.find(command); | |
440 | if (it == fParameters.end()) { | |
441 | fError = "Unknown command: " + command; | |
442 | return -1; | |
443 | } | |
444 | ||
445 | ++currPos; | |
446 | ++nProcessed; | |
447 | ||
448 | try { | |
449 | const unsigned n = it->second.SetParameter(argc, argv, currPos); | |
450 | nProcessed += n; | |
451 | currPos += n; | |
452 | } catch (const std::runtime_error& e) { | |
453 | fError = "Command " + command + " error: " + e.what(); | |
454 | return -1; | |
455 | } | |
456 | } | |
457 | ||
458 | return nProcessed; | |
459 | } | |
460 | ||
461 | } |