-
Notifications
You must be signed in to change notification settings - Fork 652
Expand file tree
/
Copy pathstrangenessBuilderHelper.h
More file actions
1336 lines (1178 loc) · 53.3 KB
/
strangenessBuilderHelper.h
File metadata and controls
1336 lines (1178 loc) · 53.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
#ifndef PWGLF_UTILS_STRANGENESSBUILDERHELPER_H_
#define PWGLF_UTILS_STRANGENESSBUILDERHELPER_H_
#include <cstdlib>
#include <cmath>
#include <array>
#include "DCAFitter/DCAFitterN.h"
#include "Framework/AnalysisDataModel.h"
#include "ReconstructionDataFormats/Track.h"
#include "DetectorsBase/GeometryManager.h"
#include "CommonConstants/PhysicsConstants.h"
#include "Common/Core/trackUtilities.h"
#include "Tools/KFparticle/KFUtilities.h"
#ifndef HomogeneousField
#define HomogeneousField
#endif
/// includes KFParticle
#include "KFParticle.h"
#include "KFPTrack.h"
#include "KFPVertex.h"
#include "KFParticleBase.h"
#include "KFVertex.h"
namespace o2
{
namespace pwglf
{
//__________________________________________
// V0 group: abstraction to deal with duplicates
// in an intuitive manner
struct V0group {
std::vector<int> V0Ids; // index list to original aod::V0s
std::vector<int> collisionIds; // coll indices
int posTrackId;
int negTrackId;
uint8_t v0Type;
};
//_______________________________________________________________________
template <typename T>
std::vector<std::size_t> sort_indices_posTrack(const std::vector<T>& v)
{
std::vector<std::size_t> idx(v.size());
std::iota(idx.begin(), idx.end(), 0);
std::stable_sort(idx.begin(), idx.end(),
[&v](std::size_t i1, std::size_t i2) { return v[i1].posTrackId < v[i2].posTrackId; });
return idx;
}
//_______________________________________________________________________
template <typename T>
std::vector<std::size_t> sort_indices_negTrack(const std::vector<T>& v)
{
std::vector<std::size_t> idx(v.size());
std::iota(idx.begin(), idx.end(), 0);
std::stable_sort(idx.begin(), idx.end(),
[&v](std::size_t i1, std::size_t i2) { return v[i1].negTrackId < v[i2].negTrackId; });
return idx;
}
//_______________________________________________________________________
// this function deals with the fact that V0s provided in AO2Ds may
// be duplicated in several collisions and groups them into entries
// of type pwglf::V0group, each entry having the same neg/pos tracks
// but an array of compatible collisions. The original V0 indices
// are preserved in the resulting structure to allow for easy referencing
// back afterwards. Algorithmically, full N^2 loops and/or multiple
// find calls are avoided via sorting.
template <typename T>
std::vector<V0group> groupDuplicates(const T& V0s)
{
std::vector<V0group> v0table;
if (V0s.size() == 0) {
return v0table;
}
V0group thisV0;
thisV0.V0Ids.push_back(-1); // create one single element
thisV0.collisionIds.push_back(-1); // create one single element
for (auto const& V0 : V0s) {
thisV0.V0Ids[0] = V0.globalIndex();
thisV0.collisionIds[0] = V0.collisionId();
thisV0.posTrackId = V0.posTrackId();
thisV0.negTrackId = V0.negTrackId();
thisV0.v0Type = V0.v0Type();
v0table.push_back(thisV0);
}
// sort tracks according to positive track index to avoid excessive N^2 searches
auto posTrackSort = sort_indices_posTrack(v0table);
// create a proper list of V0s including duplicates: collisionIds is now a vector
int atPosTrackId = v0table[posTrackSort[0]].posTrackId;
std::vector<V0group> v0tableFixedPositive; // small list with fixed positive id
std::vector<V0group> v0tableGrouped; // final list with proper grouping
for (size_t iV0 = 0; iV0 < posTrackSort.size(); iV0++) {
if (atPosTrackId != v0table[posTrackSort[iV0]].posTrackId) {
// switched pos track id. Process chunk of V0s
auto negTrackSort = sort_indices_negTrack(v0tableFixedPositive);
thisV0.collisionIds.clear();
thisV0.V0Ids.clear();
thisV0.negTrackId = v0tableFixedPositive[negTrackSort[0]].negTrackId;
for (size_t iPV0 = 0; iPV0 < v0tableFixedPositive.size(); iPV0++) {
if (thisV0.negTrackId != v0tableFixedPositive[negTrackSort[iPV0]].negTrackId) {
v0tableGrouped.push_back(thisV0);
thisV0.collisionIds.clear(); // clean collision Ids
thisV0.V0Ids.clear(); // clean aod::V0s Ids
}
thisV0.V0Ids.push_back(v0tableFixedPositive[negTrackSort[iPV0]].V0Ids[0]);
thisV0.collisionIds.push_back(v0tableFixedPositive[negTrackSort[iPV0]].collisionIds[0]);
thisV0.posTrackId = v0tableFixedPositive[negTrackSort[iPV0]].posTrackId;
thisV0.negTrackId = v0tableFixedPositive[negTrackSort[iPV0]].negTrackId;
thisV0.v0Type = v0tableFixedPositive[negTrackSort[iPV0]].v0Type;
}
v0tableGrouped.push_back(thisV0); // publish last
v0tableFixedPositive.clear();
atPosTrackId = v0table[posTrackSort[iV0]].posTrackId; // move to the next pos index
}
v0tableFixedPositive.push_back(v0table[posTrackSort[iV0]]);
}
v0tableGrouped.push_back(thisV0); // publish last
LOGF(debug, "Duplicate V0s grouped. aod::V0s counted: %i, unique index pairs: %i", V0s.size(), v0tableGrouped.size());
return v0tableGrouped;
}
//__________________________________________
// V0 information storage
struct v0candidate {
// indexing
int collisionId = -1;
int negativeTrack = -1;
int positiveTrack = -1;
// daughter properties
std::array<float, 3> positiveMomentum = {0.0f, 0.0f, 0.0f};
std::array<float, 3> negativeMomentum = {0.0f, 0.0f, 0.0f};
std::array<float, 3> positivePosition = {0.0f, 0.0f, 0.0f};
std::array<float, 3> negativePosition = {0.0f, 0.0f, 0.0f};
float positiveTrackX = 0.0f;
float negativeTrackX = 0.0f;
float positiveDCAxy = 0.0f;
float negativeDCAxy = 0.0f;
// V0 properties
std::array<float, 3> momentum = {0.0f, 0.0f, 0.0f}; // necessary for KF
std::array<float, 3> position = {0.0f, 0.0f, 0.0f};
float daughterDCA = 1000.0f;
float pointingAngle = 1.0f;
float dcaToPV = 0.0f;
float v0DCAToPVxy = 0.0f;
float v0DCAToPVz = 0.0f;
// calculated masses for convenience
float massGamma;
float massK0Short;
float massLambda;
float massAntiLambda;
// stored for decay chains
float positionCovariance[6];
float momentumCovariance[6];
};
//__________________________________________
// Cascade information storage
struct cascadeCandidate {
// indexing
int collisionId = -1;
int v0Id = -1;
int negativeTrack = -1;
int positiveTrack = -1;
int bachelorTrack = -1;
// daughter properties
std::array<float, 3> positiveMomentum = {0.0f, 0.0f, 0.0f};
std::array<float, 3> negativeMomentum = {0.0f, 0.0f, 0.0f};
std::array<float, 3> bachelorMomentum = {0.0f, 0.0f, 0.0f};
std::array<float, 3> positivePosition = {0.0f, 0.0f, 0.0f};
std::array<float, 3> negativePosition = {0.0f, 0.0f, 0.0f};
std::array<float, 3> bachelorPosition = {0.0f, 0.0f, 0.0f};
float positiveDCAxy = 0.0f;
float negativeDCAxy = 0.0f;
float bachelorDCAxy = 0.0f;
float positiveTrackX = 0.0f;
float negativeTrackX = 0.0f;
float bachelorTrackX = 0.0f;
// cascade properties
int charge = -1; // default: []Minus
float cascadeDaughterDCA = 1000.0f;
float v0DaughterDCA = 1000.0f;
float pointingAngle = 0.0f;
float cascadeDCAxy = 0.0f;
float cascadeDCAz = 0.0f;
std::array<float, 3> v0Position = {0.0f, 0.0f, 0.0f};
std::array<float, 3> v0Momentum = {0.0f, 0.0f, 0.0f};
std::array<float, 3> cascadePosition = {0.0f, 0.0f, 0.0f};
std::array<float, 3> cascadeMomentum = {0.0f, 0.0f, 0.0f};
float bachBaryonCosPA = 0.0f;
float bachBaryonDCAxyToPV = 1e+3;
float massXi = 0.0f;
float massOmega = 0.0f;
// KF-specific variables
float kfV0Chi2 = 0.0f;
float kfCascadeChi2 = 0.0f;
float kfMLambda = 0.0f;
float kfTrackCovarianceV0[21];
float kfTrackCovariancePos[21];
float kfTrackCovarianceNeg[21];
// stored for decay chains
float covariance[21];
};
//__________________________________________
// builder helper class
class strangenessBuilderHelper
{
public:
strangenessBuilderHelper()
{
// standards hardcoded in builder ...
// ...but can be changed easily since fitter is public
fitter.setPropagateToPCA(true);
fitter.setMaxR(200.);
fitter.setMinParamChange(1e-3);
fitter.setMinRelChi2Change(0.9);
fitter.setMaxDZIni(1e9);
fitter.setMaxDXYIni(4.0f);
fitter.setMaxChi2(1e9);
fitter.setUseAbsDCA(true);
fitter.setWeightedFinalPCA(false);
v0selections.minCrossedRows = -1;
v0selections.dcanegtopv = -1.0f;
v0selections.dcapostopv = -1.0f;
v0selections.v0cospa = -2;
v0selections.dcav0dau = 1e+6;
v0selections.v0radius = 0.0f;
v0selections.maxDaughterEta = 2.0;
// LUT has to be loaded later
lut = nullptr;
fitter.setMatCorrType(o2::base::Propagator::MatCorrType::USEMatCorrLUT);
// mag field has to be set later
fitter.setBz(-999.9f); // will NOT make sense if not changed
};
//_______________________________________________________________________
// standard build V0 function. Populates ::v0 object
// ::v0 will be initialized to defaults if build fails
// --- useSelections: meant to maximize recovery, but beware high cost in CPU
// --- calculateProngDCAtoPV: optionally don't propagate prongs to PV, saves
// CPU, of interest when dealing with de-duplication (variable not checked)
template <bool useSelections = true, bool calculateProngDCAtoPV = true, typename TTrack, typename TTrackParametrization>
bool buildV0Candidate(int collisionIndex,
float pvX, float pvY, float pvZ,
TTrack const& positiveTrack,
TTrack const& negativeTrack,
TTrackParametrization& positiveTrackParam,
TTrackParametrization& negativeTrackParam,
bool useCollinearFit = false,
bool calculateCovariance = false,
bool acceptTPCOnly = false)
{
v0 = {}; // safe initialization: start new
if constexpr (useSelections) {
// verify track quality
if (positiveTrack.tpcNClsCrossedRows() < v0selections.minCrossedRows) {
v0 = {};
return false;
}
if (negativeTrack.tpcNClsCrossedRows() < v0selections.minCrossedRows) {
v0 = {};
return false;
}
// verify eta
if (std::fabs(positiveTrack.eta()) > v0selections.maxDaughterEta) {
v0 = {};
return false;
}
if (std::fabs(negativeTrack.eta()) > v0selections.maxDaughterEta) {
v0 = {};
return false;
}
if (!acceptTPCOnly && !positiveTrack.hasITS() && !positiveTrack.hasTRD() && !positiveTrack.hasTOF()) {
v0 = {};
return false;
}
if (!acceptTPCOnly && !negativeTrack.hasITS() && !negativeTrack.hasTRD() && !negativeTrack.hasTOF()) {
v0 = {};
return false;
}
}
if constexpr (calculateProngDCAtoPV) {
// Calculate DCA with respect to the collision associated to the V0
std::array<float, 2> dcaInfo;
// do DCA to PV on TrackPar copies and not TrackParCov
// TrackPar preferred: don't calculate multiple scattering / CovMat changes
// Spares CPU since variables not checked
o2::track::TrackPar positiveTrackParamCopy(positiveTrackParam);
o2::track::TrackPar negativeTrackParamCopy(negativeTrackParam);
dcaInfo[0] = dcaInfo[1] = 999.0f; // by default, take large value to make sure candidate accepted
o2::base::Propagator::Instance()->propagateToDCABxByBz({pvX, pvY, pvZ}, positiveTrackParamCopy, 2.f, fitter.getMatCorrType(), &dcaInfo);
v0.positiveDCAxy = dcaInfo[0];
if constexpr (useSelections) {
if (std::fabs(v0.positiveDCAxy) < v0selections.dcapostopv) {
v0 = {};
return false;
}
}
dcaInfo[0] = dcaInfo[1] = 999.0f; // by default, take large value to make sure candidate accepted
o2::base::Propagator::Instance()->propagateToDCABxByBz({pvX, pvY, pvZ}, negativeTrackParamCopy, 2.f, fitter.getMatCorrType(), &dcaInfo);
v0.negativeDCAxy = dcaInfo[0];
if constexpr (useSelections) {
if (std::fabs(v0.negativeDCAxy) < v0selections.dcanegtopv) {
v0 = {};
return false;
}
}
} else {
v0.positiveDCAxy = 0.0f; // default invalid
v0.negativeDCAxy = 0.0f; // default invalid
}
// Perform DCA fit
int nCand = 0;
fitter.setCollinear(useCollinearFit);
try {
nCand = fitter.process(positiveTrackParam, negativeTrackParam);
} catch (...) {
v0 = {};
fitter.setCollinear(false); // even if returned, reset
return false;
}
if (nCand == 0) {
v0 = {};
fitter.setCollinear(false); // even if returned, reset
return false;
}
fitter.setCollinear(false); // proper cleaning: when exiting this loop, always reset to not collinear
// Calculate DCAToPV of the V0
o2::track::TrackPar V0Temp = fitter.createParentTrackPar();
V0Temp.setAbsCharge(0); // charge zero
std::array<float, 2> dcaV0Info;
// propagate to collision vertex
dcaV0Info[0] = dcaV0Info[1] = 999.0f; // default DCA: large, use with care if propagation fails
o2::base::Propagator::Instance()->propagateToDCABxByBz({pvX, pvY, pvZ}, V0Temp, 2.f, fitter.getMatCorrType(), &dcaV0Info);
v0.v0DCAToPVxy = dcaV0Info[0];
v0.v0DCAToPVz = dcaV0Info[1];
v0.positiveTrackX = fitter.getTrack(0).getX();
v0.negativeTrackX = fitter.getTrack(1).getX();
positiveTrackParam = fitter.getTrack(0);
negativeTrackParam = fitter.getTrack(1);
positiveTrackParam.getPxPyPzGlo(v0.positiveMomentum);
negativeTrackParam.getPxPyPzGlo(v0.negativeMomentum);
positiveTrackParam.getXYZGlo(v0.positivePosition);
negativeTrackParam.getXYZGlo(v0.negativePosition);
for (int i = 0; i < 3; i++) {
// avoids misuse if mixed with KF particle use
v0.momentum[i] = v0.positiveMomentum[i] + v0.negativeMomentum[i];
}
// get decay vertex coordinates
const auto& vtx = fitter.getPCACandidate();
for (int i = 0; i < 3; i++) {
v0.position[i] = vtx[i];
}
if constexpr (useSelections) {
if (std::hypot(v0.position[0], v0.position[1]) < v0selections.v0radius) {
v0 = {};
return false;
}
}
v0.daughterDCA = TMath::Sqrt(fitter.getChi2AtPCACandidate());
if constexpr (useSelections) {
if (v0.daughterDCA > v0selections.dcav0dau) {
v0 = {};
return false;
}
}
double cosPA = RecoDecay::cpa(
std::array{pvX, pvY, pvZ},
std::array{v0.position[0], v0.position[1], v0.position[2]},
std::array{v0.positiveMomentum[0] + v0.negativeMomentum[0], v0.positiveMomentum[1] + v0.negativeMomentum[1], v0.positiveMomentum[2] + v0.negativeMomentum[2]});
if constexpr (useSelections) {
if (cosPA < v0selections.v0cospa) {
v0 = {};
return false;
}
}
v0.pointingAngle = TMath::ACos(cosPA);
v0.dcaToPV = CalculateDCAStraightToPV(
v0.position[0], v0.position[1], v0.position[2],
v0.positiveMomentum[0] + v0.negativeMomentum[0],
v0.positiveMomentum[1] + v0.negativeMomentum[1],
v0.positiveMomentum[2] + v0.negativeMomentum[2],
pvX, pvY, pvZ);
// Calculate masses
v0.massGamma = RecoDecay::m(std::array{
std::array{v0.positiveMomentum[0], v0.positiveMomentum[1], v0.positiveMomentum[2]},
std::array{v0.negativeMomentum[0], v0.negativeMomentum[1], v0.negativeMomentum[2]}},
std::array{o2::constants::physics::MassElectron, o2::constants::physics::MassElectron});
v0.massK0Short = RecoDecay::m(std::array{
std::array{v0.positiveMomentum[0], v0.positiveMomentum[1], v0.positiveMomentum[2]},
std::array{v0.negativeMomentum[0], v0.negativeMomentum[1], v0.negativeMomentum[2]}},
std::array{o2::constants::physics::MassPionCharged, o2::constants::physics::MassPionCharged});
v0.massLambda = RecoDecay::m(std::array{
std::array{v0.positiveMomentum[0], v0.positiveMomentum[1], v0.positiveMomentum[2]},
std::array{v0.negativeMomentum[0], v0.negativeMomentum[1], v0.negativeMomentum[2]}},
std::array{o2::constants::physics::MassProton, o2::constants::physics::MassPionCharged});
v0.massAntiLambda = RecoDecay::m(std::array{
std::array{v0.positiveMomentum[0], v0.positiveMomentum[1], v0.positiveMomentum[2]},
std::array{v0.negativeMomentum[0], v0.negativeMomentum[1], v0.negativeMomentum[2]}},
std::array{o2::constants::physics::MassPionCharged, o2::constants::physics::MassProton});
// calculate covariance if requested
if (calculateCovariance) {
// Calculate position covariance matrix
auto covVtxV = fitter.calcPCACovMatrix(0);
// std::array<float, 6> positionCovariance;
v0.positionCovariance[0] = covVtxV(0, 0);
v0.positionCovariance[1] = covVtxV(1, 0);
v0.positionCovariance[2] = covVtxV(1, 1);
v0.positionCovariance[3] = covVtxV(2, 0);
v0.positionCovariance[4] = covVtxV(2, 1);
v0.positionCovariance[5] = covVtxV(2, 2);
std::array<float, 21> covTpositive = {0.};
std::array<float, 21> covTnegative = {0.};
positiveTrackParam.getCovXYZPxPyPzGlo(covTpositive);
negativeTrackParam.getCovXYZPxPyPzGlo(covTnegative);
constexpr int MomInd[6] = {9, 13, 14, 18, 19, 20}; // cov matrix elements for momentum component
for (int i = 0; i < 6; i++) {
v0.momentumCovariance[i] = covTpositive[MomInd[i]] + covTnegative[MomInd[i]];
}
}
// set collision Id correctly
v0.collisionId = collisionIndex;
// information validated, V0 built successfully. Signal OK
return true;
}
//_______________________________________________________________________
// build V0 with KF function. Populates ::v0 object
// ::v0 will be initialized to defaults if build fails
template <typename TCollision, typename TTrack, typename TTrackParametrization>
bool buildV0CandidateWithKF(TCollision const& collision,
TTrack const& positiveTrack,
TTrack const& negativeTrack,
TTrackParametrization& positiveTrackParam,
TTrackParametrization& negativeTrackParam,
int kfConstructMethod = 2, // the typical used
float kfConstrainedMassValue = 0.0f, // negative: no constraint
bool kfConstrainToPrimaryVertex = true)
{
int collisionIndex = collision.globalIndex();
float pvX = collision.posX();
float pvY = collision.posY();
float pvZ = collision.posZ();
// verify track quality
if (positiveTrack.tpcNClsCrossedRows() < v0selections.minCrossedRows) {
v0 = {};
return false;
}
if (negativeTrack.tpcNClsCrossedRows() < v0selections.minCrossedRows) {
v0 = {};
return false;
}
// verify eta
if (std::fabs(positiveTrack.eta()) > v0selections.maxDaughterEta) {
v0 = {};
return false;
}
if (std::fabs(negativeTrack.eta()) > v0selections.maxDaughterEta) {
v0 = {};
return false;
}
// Calculate DCA with respect to the collision associated to the V0
std::array<float, 2> dcaInfo;
// do DCA to PV on TrackPar copies and not TrackParCov
// TrackPar preferred: don't calculate multiple scattering / CovMat changes
// Spares CPU since variables not checked
o2::track::TrackPar positiveTrackParamCopy(positiveTrackParam);
o2::track::TrackPar negativeTrackParamCopy(negativeTrackParam);
dcaInfo[0] = dcaInfo[1] = 999.0f; // by default, take large value to make sure candidate accepted
o2::base::Propagator::Instance()->propagateToDCABxByBz({pvX, pvY, pvZ}, positiveTrackParamCopy, 2.f, fitter.getMatCorrType(), &dcaInfo);
v0.positiveDCAxy = dcaInfo[0];
if (std::fabs(v0.positiveDCAxy) < v0selections.dcanegtopv) {
v0 = {};
return false;
}
dcaInfo[0] = dcaInfo[1] = 999.0f; // reset to default value
o2::base::Propagator::Instance()->propagateToDCABxByBz({pvX, pvY, pvZ}, negativeTrackParamCopy, 2.f, fitter.getMatCorrType(), &dcaInfo);
v0.negativeDCAxy = dcaInfo[0];
if (std::fabs(v0.negativeDCAxy) < v0selections.dcanegtopv) {
v0 = {};
return false;
}
//__________________________________________
//*>~<* do V0 with KF
// create KFParticle objects from trackParCovs
KFParticle kfpPos = createKFParticleFromTrackParCov(positiveTrackParam, positiveTrackParam.getCharge(), o2::constants::physics::MassElectron);
KFParticle kfpNeg = createKFParticleFromTrackParCov(negativeTrackParam, negativeTrackParam.getCharge(), o2::constants::physics::MassElectron);
KFParticle kfpPos_DecayVtx = kfpPos;
KFParticle kfpNeg_DecayVtx = kfpNeg;
const KFParticle* V0Daughters[2] = {&kfpPos, &kfpNeg};
// construct V0
KFParticle KFV0;
KFV0.SetConstructMethod(kfConstructMethod);
try {
KFV0.Construct(V0Daughters, 2);
} catch (std::runtime_error& e) {
LOG(debug) << "Failed to construct cascade V0 from daughter tracks: " << e.what();
v0 = {};
return false;
}
if (kfConstrainedMassValue > -1e-4) {
// photon constraint: this one's got no mass
KFV0.SetNonlinearMassConstraint(kfConstrainedMassValue);
}
// V0 constructed, now recovering TrackParCov for dca fitter minimization (with material correction)
KFV0.TransportToDecayVertex();
o2::track::TrackParCov v0TrackParCov = getTrackParCovFromKFP(KFV0, o2::track::PID::Lambda, 0);
v0TrackParCov.setAbsCharge(0); // to be sure
// estimate momentum of daughters (since KFParticle does not allow us to retrieve it directly...)
float xyz_decay[3] = {KFV0.GetX(), KFV0.GetY(), KFV0.GetZ()};
kfpPos_DecayVtx.TransportToPoint(xyz_decay);
kfpNeg_DecayVtx.TransportToPoint(xyz_decay);
v0.positiveMomentum = {kfpPos_DecayVtx.GetPx(), kfpPos_DecayVtx.GetPy(), kfpPos_DecayVtx.GetPz()};
v0.negativeMomentum = {kfpNeg_DecayVtx.GetPx(), kfpNeg_DecayVtx.GetPy(), kfpNeg_DecayVtx.GetPz()};
v0.daughterDCA = std::hypot(
kfpPos_DecayVtx.GetX() - kfpNeg_DecayVtx.GetX(),
kfpPos_DecayVtx.GetY() - kfpNeg_DecayVtx.GetY(),
kfpPos_DecayVtx.GetZ() - kfpNeg_DecayVtx.GetZ());
if (v0.daughterDCA > v0selections.dcav0dau) {
v0 = {};
return false;
}
// check radius
for (int i = 0; i < 3; i++) {
v0.position[i] = xyz_decay[i];
}
if (std::hypot(v0.position[0], v0.position[1]) < v0selections.v0radius) {
v0 = {};
return false;
}
KFPVertex kfpVertex = createKFPVertexFromCollision(collision);
KFParticle KFPV(kfpVertex);
// deal with pointing angle
float cosPA = cpaFromKF(KFV0, KFPV);
if (cosPA < v0selections.v0cospa) {
v0 = {};
return false;
}
v0.pointingAngle = TMath::ACos(cosPA);
v0.dcaToPV = CalculateDCAStraightToPV(
v0.position[0], v0.position[1], v0.position[2],
v0.momentum[0], v0.momentum[1], v0.momentum[2],
pvX, pvY, pvZ);
// apply topological constraint to PV if requested
// might adjust px py pz
KFParticle KFV0_PV = KFV0;
if (kfConstrainToPrimaryVertex) {
KFV0_PV.SetProductionVertex(KFPV);
}
v0.momentum = {KFV0_PV.GetPx(), KFV0_PV.GetPy(), KFV0_PV.GetPz()};
// set collision Id correctly
v0.collisionId = collisionIndex;
// Calculate masses
v0.massGamma = RecoDecay::m(std::array{
std::array{v0.positiveMomentum[0], v0.positiveMomentum[1], v0.positiveMomentum[2]},
std::array{v0.negativeMomentum[0], v0.negativeMomentum[1], v0.negativeMomentum[2]}},
std::array{o2::constants::physics::MassElectron, o2::constants::physics::MassElectron});
v0.massK0Short = RecoDecay::m(std::array{
std::array{v0.positiveMomentum[0], v0.positiveMomentum[1], v0.positiveMomentum[2]},
std::array{v0.negativeMomentum[0], v0.negativeMomentum[1], v0.negativeMomentum[2]}},
std::array{o2::constants::physics::MassPionCharged, o2::constants::physics::MassPionCharged});
v0.massLambda = RecoDecay::m(std::array{
std::array{v0.positiveMomentum[0], v0.positiveMomentum[1], v0.positiveMomentum[2]},
std::array{v0.negativeMomentum[0], v0.negativeMomentum[1], v0.negativeMomentum[2]}},
std::array{o2::constants::physics::MassProton, o2::constants::physics::MassPionCharged});
v0.massAntiLambda = RecoDecay::m(std::array{
std::array{v0.positiveMomentum[0], v0.positiveMomentum[1], v0.positiveMomentum[2]},
std::array{v0.negativeMomentum[0], v0.negativeMomentum[1], v0.negativeMomentum[2]}},
std::array{o2::constants::physics::MassPionCharged, o2::constants::physics::MassProton});
// information validated, V0 built successfully. Signal OK
return true;
}
//_______________________________________________________________________
// build Cascade from three tracks, including V0 building.
// Populates ::cascade object.
// ::cascade will be initialized to defaults if build fails
// cascade builder creating a cascade from plain tracks
template <typename TTrack>
bool buildCascadeCandidate(int collisionIndex,
float pvX, float pvY, float pvZ,
TTrack const& positiveTrack,
TTrack const& negativeTrack,
TTrack const& bachelorTrack,
bool calculateBachelorBaryonVariables = false,
bool useCascadeMomentumAtPV = false,
bool processCovariances = false)
{
// no special treatment of positive and negative tracks when building V0s for cascades
auto posTrackPar = getTrackParCov(positiveTrack);
auto negTrackPar = getTrackParCov(negativeTrack);
if (!buildV0Candidate(collisionIndex, pvX, pvY, pvZ, positiveTrack, negativeTrack, posTrackPar, negTrackPar, false, processCovariances, false)) {
return false;
}
if (!buildCascadeCandidate(collisionIndex, pvX, pvY, pvZ, v0, positiveTrack, negativeTrack, bachelorTrack, calculateBachelorBaryonVariables, useCascadeMomentumAtPV, processCovariances)) {
return false;
}
return true;
}
//_______________________________________________________________________
// cascade builder using pre-fabricated information, thus not calling
// the DCAfitter again for the V0 contained in the cascade
// If building from scratch, prefer previous version!
// Populates ::cascade object.
// ::cascade will be initialized to defaults if build fails
// cascade builder creating a cascade from plain tracks
template <typename TTrack>
bool buildCascadeCandidate(int collisionIndex,
float pvX, float pvY, float pvZ,
v0candidate const& v0input,
TTrack const& positiveTrack,
TTrack const& negativeTrack,
TTrack const& bachelorTrack,
bool calculateBachelorBaryonVariables = false,
bool useCascadeMomentumAtPV = false,
bool processCovariances = false)
{
cascade = {}; // initialize / empty (extra safety)
// verify track quality
if (positiveTrack.tpcNClsCrossedRows() < cascadeselections.minCrossedRows) {
cascade = {};
return false;
}
if (negativeTrack.tpcNClsCrossedRows() < cascadeselections.minCrossedRows) {
cascade = {};
return false;
}
if (bachelorTrack.tpcNClsCrossedRows() < cascadeselections.minCrossedRows) {
cascade = {};
return false;
}
// verify eta
if (std::fabs(positiveTrack.eta()) > cascadeselections.maxDaughterEta) {
cascade = {};
return false;
}
if (std::fabs(negativeTrack.eta()) > cascadeselections.maxDaughterEta) {
cascade = {};
return false;
}
if (std::fabs(bachelorTrack.eta()) > cascadeselections.maxDaughterEta) {
cascade = {};
return false;
}
// verify lambda mass
if (bachelorTrack.sign() < 0 && std::fabs(v0input.massLambda - 1.116) > cascadeselections.lambdaMassWindow) {
cascade = {};
return false;
}
if (bachelorTrack.sign() > 0 && std::fabs(v0input.massAntiLambda - 1.116) > cascadeselections.lambdaMassWindow) {
cascade = {};
return false;
}
if (calculateBachelorBaryonVariables) {
// Calculates properties of the V0 comprised of bachelor and baryon in the cascade
// baryon: distinguished via bachelor charge
if (bachelorTrack.sign() < 0) {
processBachBaryonVariables(pvX, pvY, pvZ, bachelorTrack, positiveTrack);
} else {
processBachBaryonVariables(pvX, pvY, pvZ, bachelorTrack, negativeTrack);
}
}
// Overall cascade charge
cascade.charge = bachelorTrack.signed1Pt() > 0 ? +1 : -1;
// bachelor DCA track to PV
// Calculate DCA with respect to the collision associated to the V0, not individual tracks
std::array<float, 2> dcaInfo;
dcaInfo[0] = dcaInfo[1] = 999.0f; // by default, take large value to make sure candidate accepted
auto bachTrackPar = getTrackPar(bachelorTrack);
o2::base::Propagator::Instance()->propagateToDCABxByBz({pvX, pvY, pvZ}, bachTrackPar, 2.f, fitter.getMatCorrType(), &dcaInfo);
cascade.bachelorDCAxy = dcaInfo[0];
if (std::fabs(cascade.bachelorDCAxy) < cascadeselections.dcabachtopv) {
cascade = {};
return false;
}
// Do actual minimization
auto lBachelorTrack = getTrackParCov(bachelorTrack);
// Set up covariance matrices (should in fact be optional)
std::array<float, 21> covV = {0.};
constexpr int MomInd[6] = {9, 13, 14, 18, 19, 20}; // cov matrix elements for momentum component
for (int i = 0; i < 6; i++) {
covV[MomInd[i]] = v0input.momentumCovariance[i];
covV[i] = v0input.positionCovariance[i];
}
auto lV0Track = o2::track::TrackParCov(
{v0input.position[0], v0input.position[1], v0input.position[2]},
{v0input.positiveMomentum[0] + v0input.negativeMomentum[0], v0input.positiveMomentum[1] + v0input.negativeMomentum[1], v0input.positiveMomentum[2] + v0input.negativeMomentum[2]},
covV, 0, true);
lV0Track.setAbsCharge(0);
lV0Track.setPID(o2::track::PID::Lambda);
//---/---/---/
// Move close to minima
int nCand = 0;
try {
nCand = fitter.process(lV0Track, lBachelorTrack);
} catch (...) {
cascade = {};
return false;
}
if (nCand == 0) {
cascade = {};
return false;
}
lV0Track = fitter.getTrack(0);
lBachelorTrack = fitter.getTrack(1);
// DCA between cascade daughters
cascade.cascadeDaughterDCA = TMath::Sqrt(fitter.getChi2AtPCACandidate());
if (cascade.cascadeDaughterDCA > cascadeselections.dcacascdau) {
cascade = {};
return false;
}
lBachelorTrack.getPxPyPzGlo(cascade.bachelorMomentum);
// get decay vertex coordinates
const auto& vtx = fitter.getPCACandidate();
for (int i = 0; i < 3; i++) {
cascade.cascadePosition[i] = vtx[i];
}
if (std::hypot(cascade.cascadePosition[0], cascade.cascadePosition[1]) < cascadeselections.cascradius) {
cascade = {};
return false;
}
double cosPA = RecoDecay::cpa(
std::array{pvX, pvY, pvZ},
std::array{cascade.cascadePosition[0], cascade.cascadePosition[1], cascade.cascadePosition[2]},
std::array{v0input.positiveMomentum[0] + v0input.negativeMomentum[0] + cascade.bachelorMomentum[0],
v0input.positiveMomentum[1] + v0input.negativeMomentum[1] + cascade.bachelorMomentum[1],
v0input.positiveMomentum[2] + v0input.negativeMomentum[2] + cascade.bachelorMomentum[2]});
if (cosPA < cascadeselections.casccospa) {
cascade = {};
return false;
}
cascade.pointingAngle = TMath::ACos(cosPA);
// Calculate DCAxy of the cascade (with bending)
auto lCascadeTrack = fitter.createParentTrackParCov();
lCascadeTrack.setAbsCharge(cascade.charge); // to be sure
lCascadeTrack.setPID(o2::track::PID::XiMinus); // FIXME: not OK for omegas
dcaInfo[0] = 999;
dcaInfo[1] = 999;
o2::base::Propagator::Instance()->propagateToDCABxByBz({pvX, pvY, pvZ}, lCascadeTrack, 2.f, fitter.getMatCorrType(), &dcaInfo);
cascade.cascadeDCAxy = dcaInfo[0];
cascade.cascadeDCAz = dcaInfo[1];
// Calculate masses a priori
cascade.massXi = RecoDecay::m(std::array{std::array{cascade.bachelorMomentum[0], cascade.bachelorMomentum[1], cascade.bachelorMomentum[2]},
std::array{v0input.positiveMomentum[0] + v0input.negativeMomentum[0], v0input.positiveMomentum[1] + v0input.negativeMomentum[1], v0input.positiveMomentum[2] + v0input.negativeMomentum[2]}},
std::array{o2::constants::physics::MassPionCharged, o2::constants::physics::MassLambda});
cascade.massOmega = RecoDecay::m(std::array{std::array{cascade.bachelorMomentum[0], cascade.bachelorMomentum[1], cascade.bachelorMomentum[2]},
std::array{v0input.positiveMomentum[0] + v0input.negativeMomentum[0], v0input.positiveMomentum[1] + v0input.negativeMomentum[1], v0input.positiveMomentum[2] + v0input.negativeMomentum[2]}},
std::array{o2::constants::physics::MassKaonCharged, o2::constants::physics::MassLambda});
// Populate information
// cascadecandidate.v0Id = v0index.globalIndex();
cascade.collisionId = collisionIndex;
cascade.positiveTrack = positiveTrack.globalIndex();
cascade.negativeTrack = negativeTrack.globalIndex();
cascade.bachelorTrack = bachelorTrack.globalIndex();
cascade.positiveTrackX = v0input.positiveTrackX;
cascade.negativeTrackX = v0input.negativeTrackX;
cascade.bachelorTrackX = lBachelorTrack.getX(); // from this minimization
cascade.v0Position[0] = v0input.position[0];
cascade.v0Position[1] = v0input.position[1];
cascade.v0Position[2] = v0input.position[2];
cascade.v0Momentum[0] = v0input.positiveMomentum[0] + v0input.negativeMomentum[0];
cascade.v0Momentum[1] = v0input.positiveMomentum[1] + v0input.negativeMomentum[1];
cascade.v0Momentum[2] = v0input.positiveMomentum[2] + v0input.negativeMomentum[2];
cascade.positiveMomentum[0] = v0input.positiveMomentum[0];
cascade.positiveMomentum[1] = v0input.positiveMomentum[1];
cascade.positiveMomentum[2] = v0input.positiveMomentum[2];
cascade.negativeMomentum[0] = v0input.negativeMomentum[0];
cascade.negativeMomentum[1] = v0input.negativeMomentum[1];
cascade.negativeMomentum[2] = v0input.negativeMomentum[2];
cascade.v0DaughterDCA = v0input.daughterDCA;
cascade.positiveDCAxy = v0input.positiveDCAxy;
cascade.negativeDCAxy = v0input.negativeDCAxy;
if (useCascadeMomentumAtPV) {
lCascadeTrack.getPxPyPzGlo(cascade.cascadeMomentum);
} else {
cascade.cascadeMomentum[0] = cascade.bachelorMomentum[0] + v0input.positiveMomentum[0] + v0input.negativeMomentum[0];
cascade.cascadeMomentum[1] = cascade.bachelorMomentum[1] + v0input.positiveMomentum[1] + v0input.negativeMomentum[1];
cascade.cascadeMomentum[2] = cascade.bachelorMomentum[2] + v0input.positiveMomentum[2] + v0input.negativeMomentum[2];
}
if (processCovariances) {
// Calculate position covariance matrix
auto covVtxV = fitter.calcPCACovMatrix(0);
// std::array<float, 6> positionCovariance;
float positionCovariance[6];
positionCovariance[0] = covVtxV(0, 0);
positionCovariance[1] = covVtxV(1, 0);
positionCovariance[2] = covVtxV(1, 1);
positionCovariance[3] = covVtxV(2, 0);
positionCovariance[4] = covVtxV(2, 1);
positionCovariance[5] = covVtxV(2, 2);
// store momentum covariance matrix
std::array<float, 21> covTv0 = {0.};
std::array<float, 21> covTbachelor = {0.};
// std::array<float, 6> momentumCovariance;
lV0Track.getCovXYZPxPyPzGlo(covTv0);
lBachelorTrack.getCovXYZPxPyPzGlo(covTbachelor);
constexpr int MomInd[6] = {9, 13, 14, 18, 19, 20}; // cov matrix elements for momentum component
for (int i = 0; i < 21; i++) {
cascade.covariance[i] = 0.0f;
}
for (int i = 0; i < 6; i++) {
cascade.covariance[i] = positionCovariance[i];
cascade.covariance[MomInd[i]] = covTv0[MomInd[i]] + covTbachelor[MomInd[i]];
}
}
// Final outcome is YES if I got here!
return true;
}
//_______________________________________________________________________
// build KF Cascade from three tracks, including V0 building.
// Populates ::cascade object.
// ::cascade will be initialized to defaults if build fails
// cascade builder creating a cascade from plain tracks
template <typename TTrack>
bool buildCascadeCandidateWithKF(int collisionIndex,
float pvX, float pvY, float pvZ,
TTrack const& positiveTrack,
TTrack const& negativeTrack,
TTrack const& bachelorTrack,
bool calculateBachelorBaryonVariables = false,
int kfConstructMethod = 2,
bool kfTuneForOmega = false,
bool kfUseV0MassConstraint = true,
bool kfUseCascadeMassConstraint = false,
bool kfDoDCAFitterPreMinimV0 = false,
bool kfDoDCAFitterPreMinimCasc = false)
{
cascade = {}; // initialize / empty (extra safety)
//*>~<*>~<*>~<*>~<*>~<*>~<*>~<*>~<*>~<*
// KF particle based rebuilding
// dispenses prior V0 generation, uses constrained (re-)fit based on bachelor charge
//*>~<*>~<*>~<*>~<*>~<*>~<*>~<*>~<*>~<*
if (positiveTrack.tpcNClsCrossedRows() < cascadeselections.minCrossedRows) {
cascade = {};
return false;
}
if (negativeTrack.tpcNClsCrossedRows() < cascadeselections.minCrossedRows) {
cascade = {};
return false;
}
if (bachelorTrack.tpcNClsCrossedRows() < cascadeselections.minCrossedRows) {
cascade = {};
return false;
}
// verify eta
if (std::fabs(positiveTrack.eta()) > cascadeselections.maxDaughterEta) {
cascade = {};
return false;
}
if (std::fabs(negativeTrack.eta()) > cascadeselections.maxDaughterEta) {
cascade = {};
return false;
}
if (std::fabs(bachelorTrack.eta()) > cascadeselections.maxDaughterEta) {
cascade = {};
return false;
}
if (calculateBachelorBaryonVariables) {
// Calculates properties of the V0 comprised of bachelor and baryon in the cascade
// baryon: distinguished via bachelor charge
if (bachelorTrack.sign() < 0) {
processBachBaryonVariables(pvX, pvY, pvZ, bachelorTrack, positiveTrack);
} else {
processBachBaryonVariables(pvX, pvY, pvZ, bachelorTrack, negativeTrack);
}
}
// Overall cascade charge
cascade.charge = bachelorTrack.signed1Pt() > 0 ? +1 : -1;
// bachelor DCA track to PV
// Calculate DCA with respect to the collision associated to the V0, not individual tracks
std::array<float, 2> dcaInfo;
dcaInfo[0] = dcaInfo[1] = 999.0f; // by default, take large value to make sure candidate accepted
auto bachTrackPar = getTrackPar(bachelorTrack);
o2::base::Propagator::Instance()->propagateToDCABxByBz({pvX, pvY, pvZ}, bachTrackPar, 2.f, fitter.getMatCorrType(), &dcaInfo);
cascade.bachelorDCAxy = dcaInfo[0];
dcaInfo[0] = dcaInfo[1] = 999.0f; // by default, take large value to make sure candidate accepted
o2::track::TrackParCov posTrackParCovForDCA = getTrackParCov(positiveTrack);
o2::base::Propagator::Instance()->propagateToDCABxByBz({pvX, pvY, pvZ}, posTrackParCovForDCA, 2.f, fitter.getMatCorrType(), &dcaInfo);
cascade.positiveDCAxy = dcaInfo[0];
dcaInfo[0] = dcaInfo[1] = 999.0f; // by default, take large value to make sure candidate accepted
o2::track::TrackParCov negTrackParCovForDCA = getTrackParCov(negativeTrack);
o2::base::Propagator::Instance()->propagateToDCABxByBz({pvX, pvY, pvZ}, negTrackParCovForDCA, 2.f, fitter.getMatCorrType(), &dcaInfo);
cascade.negativeDCAxy = dcaInfo[0];
if (std::fabs(cascade.bachelorDCAxy) < cascadeselections.dcabachtopv) {
cascade = {};
return false;
}
o2::track::TrackParCov lBachelorTrack = getTrackParCov(bachelorTrack);