BmnRoot 26.10.0-233-gb9bbe585
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
BmnGlobalTracking.cxx
Go to the documentation of this file.
1/** BmnGlobalTracking.cxx
2 * \author Sergey Merts <Sergey.Merts@gmail.com>
3 * @since 2014
4 * @version 1.0
5 **/
6
7#include "BmnGlobalTracking.h"
8
9#include "BmnEventHeader.h"
10#include "BmnFieldMap.h"
11#include "BmnGemStripHit.h"
12#include "TFile.h"
13#include "TVectorD.h"
14
15#include <TKey.h>
16#include <algorithm>
17#include <iterator>
18#include <map>
19#include <vector>
20
21using namespace TMath;
22
23const Int_t numChambers = 59;
24// Maximal z of the first track parameter to accept a track for global tracking, cm.
25// Run 9: first GEM station ends at z = 66.2 cm, the second one starts at z = 91.2 cm.
26const Float_t kMaxZFirst = 70.0;
27const std::vector<std::string> PARTICLES = {"pi", "K", "p", "d", "t", "He3"};
28const std::vector<std::string> ZONES = {"clean", "overlap", "accepted"};
29
31 : fCscHits(nullptr)
32 , fTof1Hits(nullptr)
33 , fTof2Hits(nullptr)
34 , fStsTracks(nullptr)
35 , fGlobalTracks(nullptr)
36 , fScWall(nullptr)
37 , fNdet(nullptr)
38 , fInnerTrackBranchName("StsVector")
39 , fPeriod(8)
40 , fEventNo(0)
41 , fVertex(nullptr)
42 , fKalman(nullptr)
43 , fTime(0.0)
46 , fNInnerTracks(0)
48{}
49
51{
52 if (fGlobalTracks) {
53 fGlobalTracks->Delete();
54 delete fGlobalTracks;
55 }
56}
57
59{
60 if (fVerbose > 1)
61 cout << "BmnGlobalTracking::Init started\n";
62
64
65 FairRootManager* ioman = FairRootManager::Instance();
66 if (!ioman)
67 Fatal("Init", "FairRootManager is not instantiated");
68
69 fCscHits = (TClonesArray*)ioman->GetObject("BmnCSCHit");
70 fTof1Hits = (TClonesArray*)ioman->GetObject("BmnTof400Hit");
71 fTof2Hits = (TClonesArray*)ioman->GetObject("BmnTof700Hit");
72
73 fVertex = (CbmVertex*)ioman->GetObject("MpdVertex.");
74 fScWall = (BmnScWallEvent*)ioman->GetObject("ScWallEvent");
75 fNdet = (BmnNdetEvent*)ioman->GetObject("NdetEvent");
76
77 fStsTracks = (TClonesArray*)ioman->GetObject(fInnerTrackBranchName);
78 if (fStsTracks) {
79 printf("BmnGlobalTracking::Init(): branch %s was found!\n", fInnerTrackBranchName.Data());
80 fGlobalTracks = new TClonesArray("BmnGlobalTrack", 100); // out
81 ioman->Register("BmnGlobalTrack", "GLOBAL", fGlobalTracks, kTRUE);
82 } else {
83 printf("BmnGlobalTracking::Init(): branch with inner tracks was not found! Task will be deactivated \n");
84 SetActive(kFALSE);
85 return kERROR;
86 }
87
88 if (fPeriod == 8) {
89 TString workdir = getenv("VMCWORKDIR");
90
91 LoadAllAkimaCorrections(workdir + "/input/OutputStages_FitResults_combined_tof400_akima_v2_it16.root", kTRUE);
92 LoadAllAkimaCorrections(workdir + "/input/OutputStages_FitResults_combined_tof700_akima_it12.root", kFALSE);
93 }
94
95 if (fVerbose > 1)
96 cout << "BmnGlobalTracking::Init finished\n";
97 return kSUCCESS;
98}
99
101{
102 TStopwatch sw;
103 sw.Start();
104
105 if (!IsActive())
106 return;
107
108 fEventNo++;
109 fGlobalTracks->Delete();
110
111 if (fVerbose > 1)
112 cout << "\n======================== Global tracking exec started =====================\n" << endl;
113
114 if (!fVertex)
115 return;
116 if (fVertex->GetNTracks() < 2)
117 return;
118
119 for (Int_t i = 0; i < fStsTracks->GetEntriesFast(); ++i) {
120 CbmStsTrack* cbmTrack = (CbmStsTrack*)fStsTracks->At(i);
121
123
124 // Only tracks starting in VSP, FSD or the first GEM station are kept (z_first < kMaxZFirst)
125 // and at least 5 STS hits are required.
126 if (fPeriod == 9) {
127 if (cbmTrack->GetNStsHits() < 5)
128 continue;
129 if (cbmTrack->GetParamFirst()->GetZ() > kMaxZFirst)
130 continue;
131 }
132
133 BmnGlobalTrack globTr;
134
135 globTr.SetGemTrackIndex(i);
136 globTr.SetParamFirst(*(cbmTrack->GetParamFirst()));
137 globTr.SetParamLast(*(cbmTrack->GetParamLast()));
138 globTr.SetNHits(cbmTrack->GetNStsHits());
139 globTr.SetNDF(cbmTrack->GetNDF());
140 globTr.SetChi2(cbmTrack->GetChi2());
141 FairTrackParam par(*(globTr.GetParamLast()));
142 // we use now pion hypothesis for propagation to vertex, it is better than protons for low momenta (probably...)
143 Int_t pdg = (globTr.GetP() > 0) ? 211 : -211;
144 Double_t len = 0.0;
145 const Float_t tZ = -0.01942; // z-coordinate of target for RUN-8
146 if (fKalman->TGeoTrackPropagate(&par, tZ, pdg, nullptr, &len) == kBMNERROR)
147 continue;
148 globTr.SetLength(len);
149
150 FairTrackParam parF(*(globTr.GetParamFirst()));
151 if (fKalman->TGeoTrackPropagate(&parF, fVertex->GetZ(), pdg, nullptr, nullptr) == kBMNERROR)
152 continue;
153 Float_t dca = Sqrt(Sq(fVertex->GetX() - parF.GetX()) + Sq(fVertex->GetY() - parF.GetY()));
154 globTr.SetDCAInVertex(dca);
155
156 BmnHit virtualVertexHit;
157 virtualVertexHit.SetXYZ(fVertex->GetX(), fVertex->GetY(), fVertex->GetZ());
158 virtualVertexHit.SetDxyz(Sqrt(fVertex->GetCovariance(0, 0)), Sqrt(fVertex->GetCovariance(1, 1)),
159 Sqrt(fVertex->GetCovariance(2, 2)));
160
161 Double_t chi = 0;
162 fKalman->Update(&parF, &virtualVertexHit, chi);
163 globTr.SetChi2InVertex(chi);
164 globTr.SetParamFirst(parF);
165
167 new ((*fGlobalTracks)[fGlobalTracks->GetEntriesFast()]) BmnGlobalTrack(globTr);
168 }
169
170 MatchingTOF(kTRUE); // tof400
171 MatchingTOF(kFALSE); // tof700
172 // MatchingScWall();
173 MatchingNdet();
174
175 sw.Stop();
176 fTime += sw.RealTime();
177
178 if (fVerbose > 1)
179 cout << "\n======================== Global tracking exec finished ====================\n" << endl;
180}
181
183{
184 Int_t tofID = (tof400) ? 1 : 2;
185 TString tofName = (tof400) ? "TOF-400" : "TOF-700";
186 if (fVerbose)
187 cout << "Matching of " << tofName << " started" << endl;
188 TClonesArray* tofHits = (tof400) ? fTof1Hits : fTof2Hits;
189 if (!tofHits)
190 return kBMNERROR;
191
192 for (Int_t i = 0; i < fGlobalTracks->GetEntriesFast(); i++)
193 ((BmnGlobalTrack*)fGlobalTracks->At(i))->SetUsing(kFALSE);
194
195 multimap<Float_t, pair<Int_t, Int_t>> distPairs = FindPairsTof(fGlobalTracks, tofHits, tof400);
196
197 for (auto const& distPair : distPairs) {
198 // Float_t dist = distPair.first;
199 pair<Int_t, Int_t> idxes = distPair.second;
200 BmnGlobalTrack* track = (BmnGlobalTrack*)fGlobalTracks->At(idxes.first);
201 if (track->IsUsed())
202 continue;
203 BmnHit* hit = (BmnHit*)tofHits->At(idxes.second);
204 if (hit->IsUsed())
205 continue;
206
207 Double_t len = 0.0;
208 FairTrackParam par(*(track->GetParamLast()));
209 Int_t pdg = (track->GetP() > 0) ? 2212 : -211;
210 fKalman->TGeoTrackPropagate(&par, hit->GetZ(), pdg, nullptr, &len);
211 len += track->GetLength();
212 Float_t mom = track->GetP();
213 Float_t beta = len / hit->GetTimeStamp() / (TMath::C() * 1e-7);
214 Float_t mass2 = mom * mom * (1.0 / beta / beta - 1.0);
215 Float_t xExtr = par.GetX();
216 Float_t yExtr = par.GetY();
217 Int_t chamber = ((hit->GetDetectorID() & 0x0000FF00) >> 8) - 1;
218 Float_t xExtr_stage2 = xExtr + GetAkimaCorrection(chamber, mom, kTRUE, "stage2", tof400);
219 Float_t yExtr_stage2 = yExtr + GetAkimaCorrection(chamber, mom, kFALSE, "stage2", tof400);
220 Float_t sigmaX_stage2 = GetAkimaSigma(chamber, mom, kTRUE, "stage2", tof400);
221 Float_t sigmaY_stage2 = GetAkimaSigma(chamber, mom, kFALSE, "stage2", tof400);
222
223 // for kCommon
224 Float_t xDist = xExtr_stage2 - hit->GetX();
225 Float_t yDist = yExtr_stage2 - hit->GetY();
226 bool isWithinSigma = (Abs(xDist) < 3 * sigmaX_stage2) && (Abs(yDist) < 3 * sigmaY_stage2);
227 if (isWithinSigma) {
228 hit->SetLength(len);
229 track->SetUsing(kTRUE);
230 hit->SetUsing(kTRUE);
231 track->SetNHits(track->GetNHits() + 1);
232 if (tof400)
234 else
236
237 if (tof400)
238 track->SetTof1HitIndex(idxes.second, static_cast<BmnGlobalTrack::ParticleType>(0));
239 else
240 track->SetTof2HitIndex(idxes.second, static_cast<BmnGlobalTrack::ParticleType>(0));
241 track->SetMass2(tofID, static_cast<BmnGlobalTrack::ParticleType>(0), mass2);
242 track->SetResX(tofID, static_cast<BmnGlobalTrack::ParticleType>(0), xDist);
243 track->SetResY(tofID, static_cast<BmnGlobalTrack::ParticleType>(0), yDist);
244 }
245
246 std::string zone = "accepted";
247 for (Int_t hypId = 1; hypId < BmnGlobalTrack::kNumParticleTypes; ++hypId) { // all hypothises except kCommon
248 TString particle = track->GetParticleName(static_cast<BmnGlobalTrack::ParticleType>(hypId));
249 Float_t xCorr_stage3 = GetAkimaCorrection(chamber, mom, kTRUE, "stage3", tof400, particle.Data(), zone);
250 Float_t yCorr_stage3 = GetAkimaCorrection(chamber, mom, kFALSE, "stage3", tof400, particle.Data(), zone);
251 Float_t sigmaX_stage3 = GetAkimaSigma(chamber, mom, kTRUE, "stage3", tof400, particle.Data(), zone);
252 Float_t sigmaY_stage3 = GetAkimaSigma(chamber, mom, kFALSE, "stage3", tof400, particle.Data(), zone);
253
254 xDist = xExtr_stage2 + xCorr_stage3 - hit->GetX();
255 yDist = yExtr_stage2 + yCorr_stage3 - hit->GetY();
256 isWithinSigma = (Abs(xDist) < 3 * sigmaX_stage3) && (Abs(yDist) < 3 * sigmaY_stage3);
257 if (isWithinSigma) {
258 if (tof400)
259 track->SetTof1HitIndex(idxes.second, static_cast<BmnGlobalTrack::ParticleType>(hypId));
260 else
261 track->SetTof2HitIndex(idxes.second, static_cast<BmnGlobalTrack::ParticleType>(hypId));
262 track->SetMass2(tofID, static_cast<BmnGlobalTrack::ParticleType>(hypId), mass2);
263 track->SetResX(tofID, static_cast<BmnGlobalTrack::ParticleType>(hypId), xDist);
264 track->SetResY(tofID, static_cast<BmnGlobalTrack::ParticleType>(hypId), yDist);
265 }
266 }
267 }
268 return kBMNSUCCESS;
269}
270
272{
273 if (fVerbose)
274 cout << "Matching of ScWall started " << fScWall << endl;
275 if (!fScWall)
276 return kBMNERROR;
277
278 for (Int_t trIdx = 0; trIdx < fGlobalTracks->GetEntriesFast(); ++trIdx) {
280 FairTrackParam par(*(tr->GetParamLast()));
281 Double_t minDX = DBL_MAX;
282 Double_t minDY = DBL_MAX;
283 Float_t xCut = 20.0; // cm
284 Float_t yCut = 20.0; // cm
285 Int_t minIdx = -1;
286
287 Float_t z = 741.5;
288 Int_t nCells = 174; // BmnScWallEvent::fgkMaxCells; //174
289 Int_t pdg = (tr->GetP() > 0) ? 2212 : -211;
290
291 if (fKalman->TGeoTrackPropagate(&par, z, pdg, nullptr, nullptr) == kBMNERROR)
292 continue;
293
294 for (Int_t i = 1; i <= nCells; ++i) {
295 BmnScWallCell* cell = fScWall->GetCell(i);
296 if (cell->GetSignal() < 0.001)
297 continue; // skip empty cells
298
299 Float_t dX = par.GetX() - cell->GetX();
300 Float_t dY = par.GetY() - cell->GetY();
301
302 if (Abs(dX) < xCut && Abs(dY) < yCut && Abs(dX) < Abs(minDX) && Abs(dY) < Abs(minDY)) {
303 minDX = dX;
304 minDY = dY;
305 minIdx = i;
306 }
307 }
308 // cout << minDX << " " << minDY << endl;
309
310 if (minIdx == -1)
311 continue;
312 tr->SetScWallCellId(minIdx);
313 tr->SetScWallSignal(fScWall->GetCell(minIdx)->GetSignal());
314 }
315 return kBMNSUCCESS;
316}
317
319{
320 if (fVerbose)
321 cout << "Matching of Ndet started " << fNdet << endl;
322 if (!fNdet)
323 return kBMNERROR;
324 auto* cells = fNdet->GetCells();
325 if (cells->GetEntriesFast() == 0)
326 return kBMNSUCCESS;
327
328 for (Int_t trIdx = 0; trIdx < fGlobalTracks->GetEntriesFast(); ++trIdx) {
330 FairTrackParam par(*(tr->GetParamLast()));
331
332 for (int ice = 0, nce = cells->GetEntriesFast(); ice < nce; ++ice) {
333 auto* cell = static_cast<BmnNdetCell*>(cells->At(ice));
334 if (!cell)
335 continue;
336 Int_t pdg = (tr->GetP() > 0) ? 2212 : -211;
337
338 if (fKalman->TGeoTrackPropagate(&par, cell->GetZ(), pdg, nullptr, nullptr) == kBMNERROR)
339 continue;
340
341 Float_t dX = par.GetX() - cell->GetX();
342 Float_t dY = par.GetY() - cell->GetY();
343
344 if (Abs(dX) < 5 * cell->GetDx() && Abs(dY) < 5 * cell->GetDy()) { // TODO remove 5* (after alignment)
345 tr->SetNdetHitIndex(ice);
346 // break;
347 }
348 }
349 }
350 return kBMNSUCCESS;
351}
352
353multimap<Float_t, pair<Int_t, Int_t>> BmnGlobalTracking::FindPairsTof(TClonesArray* tracks,
354 TClonesArray* hits,
355 Bool_t tof400)
356{
357
358 multimap<Float_t, pair<Int_t, Int_t>> distancesPairs;
359
360 Double_t minZ = 10000.0;
361 for (Int_t hitIdx = 0; hitIdx < hits->GetEntriesFast(); ++hitIdx) {
362 BmnHit* hit = (BmnHit*)hits->At(hitIdx);
363 if (hit->GetZ() < minZ)
364 minZ = hit->GetZ();
365 }
366
367 Float_t maxDist = 10.0; // 10 cm to reduce size of multimap
368
369 for (Int_t trIdx = 0; trIdx < tracks->GetEntriesFast(); ++trIdx) {
370 BmnGlobalTrack* tr = (BmnGlobalTrack*)tracks->At(trIdx);
371 FairTrackParam parMinZ(*(tr->GetParamLast()));
372 Int_t pdg = (tr->GetP() > 0) ? 2212 : -211;
373 if (fKalman->TGeoTrackPropagate(&parMinZ, minZ, pdg, nullptr, nullptr) == kBMNERROR)
374 continue;
375 for (Int_t hitIdx = 0; hitIdx < hits->GetEntriesFast(); ++hitIdx) {
376
377 BmnHit* hit = (BmnHit*)hits->At(hitIdx);
378 FairTrackParam param = parMinZ;
379 if (fKalman->TGeoTrackPropagate(&param, hit->GetZ(), pdg, nullptr, nullptr) == kBMNERROR)
380 continue;
381 Float_t mom = 1.0 / param.GetQp();
382 Int_t chamber = ((hit->GetDetectorID() & 0x0000FF00) >> 8) - 1;
383 Float_t xMomCorr = GetAkimaCorrection(chamber, mom, kTRUE, "stage1", tof400);
384 Float_t yMomCorr = GetAkimaCorrection(chamber, mom, kFALSE, "stage1", tof400);
385
386 Float_t dX = param.GetX() + xMomCorr - hit->GetX();
387 Float_t dY = param.GetY() + yMomCorr - hit->GetY();
388 Float_t dist = Sqrt(dX * dX + dY * dY);
389
390 if (dist < maxDist)
391 distancesPairs.insert(pair<Float_t, pair<Int_t, Int_t>>(dist, pair<Int_t, Int_t>(trIdx, hitIdx)));
392 }
393 }
394
395 return distancesPairs;
396}
397
399{
400 FairTrackParam par = *(tr->GetParamFirst());
401 Int_t pdg = (par.GetQp() > 0) ? 2212 : -211;
402 fKalman->TGeoTrackPropagate(&par, -200, pdg, nullptr, nullptr);
403 Double_t Alpha_in = ATan(par.GetTx());
404 // Double_t Bdl = 2.856; //Abs(MagFieldIntegral(par, -200.0, 1000.0, 1.0) * 0.001);
405 Double_t Bdl = Abs(MagFieldIntegral(par, -200.0, 600.0, 1.0) * 0.001);
406 fKalman->TGeoTrackPropagate(&par, 600, pdg, nullptr, nullptr);
407 Double_t Alpha_out = ATan(par.GetTx());
408 // if (tr->GetUpstreamTrackIndex() != -1) {
409 // BmnTrack* upTrack = (BmnTrack*)fUpstreamTracks->At(tr->GetUpstreamTrackIndex());
410 // if (upTrack != nullptr)
411 // Alpha_in = ATan(upTrack->GetParamLast()->GetTx());
412 // }
413 Double_t momInt = 0.3 * Bdl / (Sin(Alpha_out) - Sin(Alpha_in));
414 // Double_t momInt = 0.3 * Bdl / (Alpha_out - Alpha_in);
415 if (!IsNaN(momInt) && Abs(momInt) > 0.01 && Abs(momInt) < 100) {
416 tr->GetParamFirst()->SetQp(1.0 / momInt);
417 }
418 // cout << Alpha_in << " " << Alpha_out << " " << Bdl << " " << momInt << endl;
419
420 return kBMNSUCCESS;
421}
422
424{
425 delete fKalman;
426 printf("Work time of BmnGlobalTracking: %4.2f sec.\n", fTime);
428}
429
431{
432 /*
433 field in kG
434 step in cm
435 */
436 Int_t pdg = (par.GetQp() > 0.) ? 2212 : -211;
437 FairField* field = FairRunAna::Instance()->GetField();
438 fKalman->TGeoTrackPropagate(&par, zMin, pdg, NULL, NULL, kTRUE);
439 Double_t z = par.GetZ();
440 Double_t integral = 0.0;
441 while (z < zMax) {
442 z += step;
443 fKalman->TGeoTrackPropagate(&par, z, pdg, NULL, NULL, kTRUE);
444 integral += field->GetBy(par.GetX(), par.GetY(), par.GetZ());
445 }
446 integral *= step;
447 return integral;
448}
449
451{
452 printf("================================================================================\n");
453 printf("======= Statistics of Global tracking\n");
454 printf("======= Number of input inner tracks: %ld\n", fNInnerTracks);
455 printf("======= Number of input inner tracks with 4+ hits extrapolated to the Vertex: %ld (%4.1f%%)\n",
457 printf("======= Number of tracks matched with TOF-400: %ld\n", fNMatchedTof400);
458 printf("======= Number of tracks matched with TOF-700: %ld\n", fNMatchedTof700);
459 printf("================================================================================\n");
460}
461
462void BmnGlobalTracking::OnlineWrite(const std::unique_ptr<TTree>& resultTree)
463{
464 if (!IsActive())
465 return;
466
467 if (fStsTracks) {
468 resultTree->Branch("BmnGlobalTrack", &fGlobalTracks);
469 }
470 resultTree->Fill();
471}
472
474{
475 std::cout << "\n=== Loading Akima corrections for " << (isTOF400 ? "TOF400" : "TOF700") << " ===" << std::endl;
476 std::cout << "=== File: " << filename << " ===" << std::endl;
477
478 AkimaContainer& cont = isTOF400 ? fAkima400 : fAkima700;
479
480 TFile* file = TFile::Open(filename);
481 if (!file || file->IsZombie()) {
482 std::cerr << "ERROR: Cannot open file: " << filename << std::endl;
483 return;
484 }
485
486 for (Int_t chamber = 0; chamber < numChambers; ++chamber) {
487 TString chamberPath = Form("chamber_%d", chamber);
488 TDirectory* chamberDir = dynamic_cast<TDirectory*>(file->Get(chamberPath));
489 if (!chamberDir)
490 continue;
491
492 TDirectory* stage1Dir = dynamic_cast<TDirectory*>(chamberDir->Get("stage1"));
493 if (stage1Dir) {
494 TDirectory* dxPosDir = dynamic_cast<TDirectory*>(stage1Dir->Get("dx_positive"));
495 if (dxPosDir) {
496 TIter next(dxPosDir->GetListOfKeys());
497 TKey* key;
498 while ((key = (TKey*)next())) {
499 TObject* obj = key->ReadObj();
500 if (obj->InheritsFrom("AkimaFunction")) {
501 cont.dx_stage1_pos[chamber] = (AkimaFunction*)obj;
502 cont.loaded_count++;
503 break;
504 }
505 }
506 }
507
508 TDirectory* dxNegDir = dynamic_cast<TDirectory*>(stage1Dir->Get("dx_negative"));
509 if (dxNegDir) {
510 TIter next(dxNegDir->GetListOfKeys());
511 TKey* key;
512 while ((key = (TKey*)next())) {
513 TObject* obj = key->ReadObj();
514 if (obj->InheritsFrom("AkimaFunction")) {
515 cont.dx_stage1_neg[chamber] = (AkimaFunction*)obj;
516 cont.loaded_count++;
517 break;
518 }
519 }
520 }
521
522 TDirectory* dyPosDir = dynamic_cast<TDirectory*>(stage1Dir->Get("dy_positive"));
523 if (dyPosDir) {
524 TIter next(dyPosDir->GetListOfKeys());
525 TKey* key;
526 while ((key = (TKey*)next())) {
527 TObject* obj = key->ReadObj();
528 if (obj->InheritsFrom("AkimaFunction")) {
529 cont.dy_stage1_pos[chamber] = (AkimaFunction*)obj;
530 cont.loaded_count++;
531 break;
532 }
533 }
534 }
535
536 TDirectory* dyNegDir = dynamic_cast<TDirectory*>(stage1Dir->Get("dy_negative"));
537 if (dyNegDir) {
538 TIter next(dyNegDir->GetListOfKeys());
539 TKey* key;
540 while ((key = (TKey*)next())) {
541 TObject* obj = key->ReadObj();
542 if (obj->InheritsFrom("AkimaFunction")) {
543 cont.dy_stage1_neg[chamber] = (AkimaFunction*)obj;
544 cont.loaded_count++;
545 break;
546 }
547 }
548 }
549 }
550
551 TDirectory* stage2Dir = dynamic_cast<TDirectory*>(chamberDir->Get("stage2"));
552 if (stage2Dir) {
553 TDirectory* dxPosDir = dynamic_cast<TDirectory*>(stage2Dir->Get("dx_positive"));
554 if (dxPosDir) {
555 TIter next(dxPosDir->GetListOfKeys());
556 TKey* key;
557 while ((key = (TKey*)next())) {
558 TObject* obj = key->ReadObj();
559 if (obj->InheritsFrom("AkimaFunction")) {
560 AkimaFunction* func = (AkimaFunction*)obj;
561 TString name = func->GetName();
562 if (name.Contains("mean")) {
563 cont.dx_stage2_mean_pos[chamber] = func;
564 cont.loaded_count++;
565 } else if (name.Contains("sigma")) {
566 cont.dx_stage2_sigma_pos[chamber] = func;
567 cont.loaded_count++;
568 }
569 }
570 }
571 }
572
573 TDirectory* dxNegDir = dynamic_cast<TDirectory*>(stage2Dir->Get("dx_negative"));
574 if (dxNegDir) {
575 TIter next(dxNegDir->GetListOfKeys());
576 TKey* key;
577 while ((key = (TKey*)next())) {
578 TObject* obj = key->ReadObj();
579 if (obj->InheritsFrom("AkimaFunction")) {
580 AkimaFunction* func = (AkimaFunction*)obj;
581 TString name = func->GetName();
582 if (name.Contains("mean")) {
583 cont.dx_stage2_mean_neg[chamber] = func;
584 cont.loaded_count++;
585 } else if (name.Contains("sigma")) {
586 cont.dx_stage2_sigma_neg[chamber] = func;
587 cont.loaded_count++;
588 }
589 }
590 }
591 }
592
593 TDirectory* dyPosDir = dynamic_cast<TDirectory*>(stage2Dir->Get("dy_positive"));
594 if (dyPosDir) {
595 TIter next(dyPosDir->GetListOfKeys());
596 TKey* key;
597 while ((key = (TKey*)next())) {
598 TObject* obj = key->ReadObj();
599 if (obj->InheritsFrom("AkimaFunction")) {
600 AkimaFunction* func = (AkimaFunction*)obj;
601 TString name = func->GetName();
602 if (name.Contains("mean")) {
603 cont.dy_stage2_mean_pos[chamber] = func;
604 cont.loaded_count++;
605 } else if (name.Contains("sigma")) {
606 cont.dy_stage2_sigma_pos[chamber] = func;
607 cont.loaded_count++;
608 }
609 }
610 }
611 }
612
613 TDirectory* dyNegDir = dynamic_cast<TDirectory*>(stage2Dir->Get("dy_negative"));
614 if (dyNegDir) {
615 TIter next(dyNegDir->GetListOfKeys());
616 TKey* key;
617 while ((key = (TKey*)next())) {
618 TObject* obj = key->ReadObj();
619 if (obj->InheritsFrom("AkimaFunction")) {
620 AkimaFunction* func = (AkimaFunction*)obj;
621 TString name = func->GetName();
622 if (name.Contains("mean")) {
623 cont.dy_stage2_mean_neg[chamber] = func;
624 cont.loaded_count++;
625 } else if (name.Contains("sigma")) {
626 cont.dy_stage2_sigma_neg[chamber] = func;
627 cont.loaded_count++;
628 }
629 }
630 }
631 }
632 }
633
634 TDirectory* stage3Dir = dynamic_cast<TDirectory*>(chamberDir->Get("stage3"));
635 if (stage3Dir) {
636 for (const auto& particle : PARTICLES) {
637 TDirectory* particleDir = dynamic_cast<TDirectory*>(stage3Dir->Get(particle.c_str()));
638 if (!particleDir)
639 continue;
640
641 for (const auto& zone : ZONES) {
642 TString dxDirName = Form("dx_%s_%s_positive", particle.c_str(), zone.c_str());
643 TDirectory* dxDir = dynamic_cast<TDirectory*>(particleDir->Get(dxDirName));
644 if (dxDir) {
645 TIter next(dxDir->GetListOfKeys());
646 TKey* key;
647 while ((key = (TKey*)next())) {
648 TObject* obj = key->ReadObj();
649 if (obj->InheritsFrom("AkimaFunction")) {
650 AkimaFunction* func = (AkimaFunction*)obj;
651 TString name = func->GetName();
652 Stage3Key k = {chamber, particle, zone};
653 if (name.Contains("mean")) {
654 cont.dx_stage3_mean[k] = func;
655 cont.loaded_count++;
656 } else if (name.Contains("sigma")) {
657 cont.dx_stage3_sigma[k] = func;
658 cont.loaded_count++;
659 }
660 }
661 }
662 }
663
664 TString dyDirName = Form("dy_%s_%s_positive", particle.c_str(), zone.c_str());
665 TDirectory* dyDir = dynamic_cast<TDirectory*>(particleDir->Get(dyDirName));
666 if (dyDir) {
667 TIter next(dyDir->GetListOfKeys());
668 TKey* key;
669 while ((key = (TKey*)next())) {
670 TObject* obj = key->ReadObj();
671 if (obj->InheritsFrom("AkimaFunction")) {
672 AkimaFunction* func = (AkimaFunction*)obj;
673 TString name = func->GetName();
674 Stage3Key k = {chamber, particle, zone};
675 if (name.Contains("mean")) {
676 cont.dy_stage3_mean[k] = func;
677 cont.loaded_count++;
678 } else if (name.Contains("sigma")) {
679 cont.dy_stage3_sigma[k] = func;
680 cont.loaded_count++;
681 }
682 }
683 }
684 }
685 }
686 }
687 }
688 }
689
690 file->Close();
691 std::cout << "Loaded " << cont.loaded_count << " AkimaFunction objects for " << (isTOF400 ? "TOF400" : "TOF700")
692 << std::endl;
693}
694
696 Float_t qp,
697 Bool_t isDx,
698 const TString& stage,
699 Bool_t isTOF400,
700 const TString& particle,
701 const TString& zone)
702{
703 if (chamber < 0 || chamber >= numChambers)
704 return 0.0;
705
706 AkimaContainer& cont = isTOF400 ? fAkima400 : fAkima700;
707 qp = AdjustQP(qp, isTOF400);
708 std::string region = GetRegionFromQP(qp);
709
710 if (stage == "stage1") {
711 if (isDx) {
712 if (region == "negative") {
713 auto it = cont.dx_stage1_neg.find(chamber);
714 if (it != cont.dx_stage1_neg.end() && it->second)
715 return -it->second->Eval(qp);
716 it = cont.dx_stage1_pos.find(chamber);
717 if (it != cont.dx_stage1_pos.end() && it->second)
718 return -it->second->Eval(-qp);
719 } else {
720 auto it = cont.dx_stage1_pos.find(chamber);
721 if (it != cont.dx_stage1_pos.end() && it->second)
722 return -it->second->Eval(qp);
723 }
724 } else {
725 if (region == "negative") {
726 auto it = cont.dy_stage1_neg.find(chamber);
727 if (it != cont.dy_stage1_neg.end() && it->second)
728 return -it->second->Eval(qp);
729 auto it_pos = cont.dy_stage1_pos.find(chamber);
730 if (it_pos != cont.dy_stage1_pos.end() && it_pos->second)
731 return -it_pos->second->Eval(-qp);
732 } else {
733 auto it = cont.dy_stage1_pos.find(chamber);
734 if (it != cont.dy_stage1_pos.end() && it->second)
735 return -it->second->Eval(qp);
736 }
737 }
738 } else if (stage == "stage2") {
739 if (isDx) {
740 if (region == "negative") {
741 auto it = cont.dx_stage2_mean_neg.find(chamber);
742 if (it != cont.dx_stage2_mean_neg.end() && it->second)
743 return -it->second->Eval(qp);
744 auto it_pos = cont.dx_stage2_mean_pos.find(chamber);
745 if (it_pos != cont.dx_stage2_mean_pos.end() && it_pos->second)
746 return -it_pos->second->Eval(-qp);
747 } else {
748 auto it = cont.dx_stage2_mean_pos.find(chamber);
749 if (it != cont.dx_stage2_mean_pos.end() && it->second)
750 return -it->second->Eval(qp);
751 }
752 } else {
753 if (region == "negative") {
754 auto it = cont.dy_stage2_mean_neg.find(chamber);
755 if (it != cont.dy_stage2_mean_neg.end() && it->second)
756 return -it->second->Eval(qp);
757 auto it_pos = cont.dy_stage2_mean_pos.find(chamber);
758 if (it_pos != cont.dy_stage2_mean_pos.end() && it_pos->second)
759 return -it_pos->second->Eval(-qp);
760 } else {
761 auto it = cont.dy_stage2_mean_pos.find(chamber);
762 if (it != cont.dy_stage2_mean_pos.end() && it->second)
763 return -it->second->Eval(qp);
764 }
765 }
766 } else if (stage == "stage3") {
767 if (qp < 0)
768 return GetAkimaCorrection(chamber, -qp, isDx, "stage2", isTOF400, "", "");
769 if (particle == "" || zone == "")
770 return GetAkimaCorrection(chamber, qp, isDx, "stage2", isTOF400);
771
772 Stage3Key key = {chamber, particle.Data(), zone.Data()};
773
774 if (isDx) {
775 auto it = cont.dx_stage3_mean.find(key);
776 if (it != cont.dx_stage3_mean.end() && it->second)
777 return -it->second->Eval(qp);
778 } else {
779 auto it = cont.dy_stage3_mean.find(key);
780 if (it != cont.dy_stage3_mean.end() && it->second)
781 return -it->second->Eval(qp);
782 }
783 return GetAkimaCorrection(chamber, qp, isDx, "stage2", isTOF400);
784 }
785
786 return 0.0;
787}
788
790 Float_t qp,
791 Bool_t isDx,
792 const TString& stage,
793 Bool_t isTOF400,
794 const TString& particle,
795 const TString& zone)
796{
797 if (chamber < 0 || chamber >= numChambers)
798 return 1.0;
799
800 AkimaContainer& cont = isTOF400 ? fAkima400 : fAkima700;
801 qp = AdjustQP(qp, isTOF400);
802 std::string region = GetRegionFromQP(qp);
803
804 if (stage == "stage2") {
805 if (isDx) {
806 if (region == "negative") {
807 auto it = cont.dx_stage2_sigma_neg.find(chamber);
808 if (it != cont.dx_stage2_sigma_neg.end() && it->second) {
809 Float_t sigma = it->second->Eval(qp);
810 return (sigma > 0) ? sigma : 1.0;
811 }
812 auto it_pos = cont.dx_stage2_sigma_pos.find(chamber);
813 if (it_pos != cont.dx_stage2_sigma_pos.end() && it_pos->second) {
814 Float_t sigma = it_pos->second->Eval(-qp);
815 return (sigma > 0) ? sigma : 1.0;
816 }
817 } else {
818 auto it = cont.dx_stage2_sigma_pos.find(chamber);
819 if (it != cont.dx_stage2_sigma_pos.end() && it->second) {
820 Float_t sigma = it->second->Eval(qp);
821 return (sigma > 0) ? sigma : 1.0;
822 }
823 }
824 } else {
825 if (region == "negative") {
826 auto it = cont.dy_stage2_sigma_neg.find(chamber);
827 if (it != cont.dy_stage2_sigma_neg.end() && it->second) {
828 Float_t sigma = it->second->Eval(qp);
829 return (sigma > 0) ? sigma : 1.0;
830 }
831 auto it_pos = cont.dy_stage2_sigma_pos.find(chamber);
832 if (it_pos != cont.dy_stage2_sigma_pos.end() && it_pos->second) {
833 Float_t sigma = it_pos->second->Eval(-qp);
834 return (sigma > 0) ? sigma : 1.0;
835 }
836 } else {
837 auto it = cont.dy_stage2_sigma_pos.find(chamber);
838 if (it != cont.dy_stage2_sigma_pos.end() && it->second) {
839 Float_t sigma = it->second->Eval(qp);
840 return (sigma > 0) ? sigma : 1.0;
841 }
842 }
843 }
844 return 1.0;
845 } else if (stage == "stage3") {
846 if (qp < 0)
847 return GetAkimaSigma(chamber, -qp, isDx, "stage2", isTOF400, "", "");
848 if (particle == "" || zone == "")
849 return GetAkimaSigma(chamber, qp, isDx, "stage2", isTOF400);
850
851 Stage3Key key = {chamber, particle.Data(), zone.Data()};
852
853 if (isDx) {
854 auto it = cont.dx_stage3_sigma.find(key);
855 if (it != cont.dx_stage3_sigma.end() && it->second) {
856 Float_t sigma = it->second->Eval(qp);
857 return (sigma > 0) ? sigma : 1.0;
858 }
859 } else {
860 auto it = cont.dy_stage3_sigma.find(key);
861 if (it != cont.dy_stage3_sigma.end() && it->second) {
862 Float_t sigma = it->second->Eval(qp);
863 return (sigma > 0) ? sigma : 1.0;
864 }
865 }
866 return GetAkimaSigma(chamber, qp, isDx, "stage2", isTOF400);
867 }
868
869 return 1.0;
870}
871
873{
874 return (qp < 0) ? "negative" : "positive";
875}
876
878{
879 if (isTOF400) {
880 if (qp < 0 && qp > -0.5)
881 return -0.5;
882 if (qp > 0 && qp < 0.5)
883 return 0.5;
884 } else {
885 if (qp < 0 && qp > -0.7)
886 return -0.7;
887 if (qp > 0 && qp < 0.7)
888 return 0.7;
889 }
890 return qp;
891}
BmnStatus
Define status code.
Definition BmnEnums.h:93
@ kBMNERROR
Error.
Definition BmnEnums.h:95
@ kBMNSUCCESS
Success.
Definition BmnEnums.h:94
const std::vector< std::string > PARTICLES
const Float_t kMaxZFirst
const std::vector< std::string > ZONES
vector< Double_t > dist(vector< Double_t > qp, Double_t mu)
Definition BmnMath.cxx:869
const UShort_t numChambers
int i
Definition P4_F32vec4.h:22
bool Bool_t
int Int_t
float Float_t
constexpr Bool_t kFALSE
double Double_t
constexpr Bool_t kTRUE
const char Option_t
void Fatal(const char *location, const char *msgfmt,...)
char * Form(const char *fmt,...)
BmnGlobalTrack.h.
void SetMass2(Int_t tofID, Float_t m2)
void SetResX(Int_t tofID, Float_t res)
TString GetParticleName(ParticleType type)
void SetNdetHitIndex(Int_t iNdet)
void SetDCAInVertex(Float_t chi)
void SetChi2InVertex(Float_t chi)
void SetGemTrackIndex(Int_t iGem)
Modifiers.
void SetTof2HitIndex(Int_t iTof2Hit)
void SetScWallSignal(Float_t sig)
void SetScWallCellId(Int_t iScWall)
void SetResY(Int_t tofID, Float_t res)
void SetTof1HitIndex(Int_t iTof1Hit)
TClonesArray * fTof2Hits
BmnKalmanFilter * fKalman
vertex information
BmnGlobalTracking()
Constructor.
Float_t AdjustQP(Float_t qp, Bool_t isTOF400=false)
virtual ~BmnGlobalTracking()
Destructor.
AkimaContainer fAkima400
Float_t GetAkimaSigma(Int_t chamber, Float_t qp, Bool_t isDx, const TString &stage, Bool_t isTOF400, const TString &particle="", const TString &zone="")
virtual void Exec(Option_t *opt)
Inherited from FairTask.
std::string GetRegionFromQP(Float_t qp)
BmnScWallEvent * fScWall
TClonesArray * fStsTracks
TClonesArray * fCscHits
virtual void OnlineWrite(const std::unique_ptr< TTree > &resultTree)
Write task resul to tree.
Float_t GetAkimaCorrection(Int_t chamber, Float_t qp, Bool_t isDx, const TString &stage, Bool_t isTOF400, const TString &particle="", const TString &zone="")
multimap< Float_t, pair< Int_t, Int_t > > FindPairsTof(TClonesArray *tracks, TClonesArray *hits, Bool_t tof400)
BmnStatus MatchingTOF(Bool_t tof400)
AkimaContainer fAkima700
virtual InitStatus Init()
Inherited from FairTask.
BmnStatus UpdateMomentum(BmnGlobalTrack *tr)
void LoadAllAkimaCorrections(const TString &filename, Bool_t isTOF400)
TClonesArray * fGlobalTracks
Double_t MagFieldIntegral(FairTrackParam &par, Double_t zMin, Double_t zMax, Double_t step)
virtual void Finish()
Inherited from FairTask.
TClonesArray * fTof1Hits
void SetLength(Double_t len)
Definition BmnHit.h:50
void SetUsing(Bool_t use)
Definition BmnHit.h:47
Bool_t IsUsed() const
Definition BmnHit.h:30
Class for Bmn Ndet data container in event.
Class for Bmn ScWall module data container in event.
float GetSignal() const
Deposited Signal.
Class for Bmn ScWall data container in event.
void SetChi2(Double_t chi2)
Definition BmnTrack.h:76
Float_t GetLength() const
Definition BmnTrack.h:61
void SetParamLast(FairTrackParam &par)
Definition BmnTrack.h:72
FairTrackParam * GetParamFirst()
Definition BmnTrack.h:63
Int_t GetNHits() const
Accessors.
Definition BmnTrack.h:49
Bool_t IsUsed()
Definition BmnTrack.h:86
Double_t GetP()
Definition BmnTrack.h:67
void SetNHits(Int_t n)
Definition BmnTrack.h:80
void SetUsing(Bool_t flag)
Definition BmnTrack.h:88
void SetParamFirst(FairTrackParam &par)
Modifiers.
Definition BmnTrack.h:70
FairTrackParam * GetParamLast()
Definition BmnTrack.h:65
void SetLength(Double_t length)
Definition BmnTrack.h:84
void SetNDF(Int_t ndf)
Definition BmnTrack.h:78
FairTrackParam * GetParamLast()
Definition CbmStsTrack.h:70
Int_t GetNStsHits() const
Accessors.
Definition CbmStsTrack.h:60
Double_t GetChi2() const
Definition CbmStsTrack.h:66
FairTrackParam * GetParamFirst()
Definition CbmStsTrack.h:69
Int_t GetNDF() const
Definition CbmStsTrack.h:67
CbmVertex.h.
Definition CbmVertex.h:21
T * Get(const char *namecycle)
virtual TObject * Get(const char *namecycle)
virtual TList * GetListOfKeys() const
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
void Close(Option_t *option="") override
virtual TObject * ReadObj()
const char * GetName() const override
Int_t GetEntriesFast() const
TObject * At(Int_t idx) const override
R__ALWAYS_INLINE Bool_t IsZombie() const
virtual Bool_t InheritsFrom(const char *classname) const
Double_t RealTime()
void Start(Bool_t reset=kTRUE)
void Stop()
const char * Data() const
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
double beta(double x, double y)
Bool_t IsNaN(Double_t x)
constexpr Double_t C()
Double_t ATan(Double_t)
Double_t Sq(Double_t x)
Double_t Sqrt(Double_t x)
Double_t Abs(Double_t d)
Double_t Sin(Double_t)
map< Int_t, AkimaFunction * > dy_stage2_sigma_pos
map< Int_t, AkimaFunction * > dx_stage2_sigma_pos
map< Stage3Key, AkimaFunction * > dy_stage3_sigma
map< Int_t, AkimaFunction * > dy_stage2_mean_pos
map< Int_t, AkimaFunction * > dy_stage1_pos
map< Int_t, AkimaFunction * > dx_stage1_pos
map< Int_t, AkimaFunction * > dy_stage2_mean_neg
map< Int_t, AkimaFunction * > dx_stage2_mean_pos
map< Stage3Key, AkimaFunction * > dx_stage3_mean
map< Stage3Key, AkimaFunction * > dx_stage3_sigma
map< Int_t, AkimaFunction * > dy_stage2_sigma_neg
map< Stage3Key, AkimaFunction * > dy_stage3_mean
map< Int_t, AkimaFunction * > dy_stage1_neg
map< Int_t, AkimaFunction * > dx_stage1_neg
map< Int_t, AkimaFunction * > dx_stage2_mean_neg
map< Int_t, AkimaFunction * > dx_stage2_sigma_neg