Skip to content

Commit 004acaa

Browse files
committed
[WIP] Match correlated bkgs
1 parent 8c93137 commit 004acaa

File tree

7 files changed

+650
-438
lines changed

7 files changed

+650
-438
lines changed

Common/Core/RecoDecay.h

Lines changed: 52 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <tuple> // std::apply
2626
#include <utility> // std::move
2727
#include <vector> // std::vector
28+
#include <iostream> // std::ostream
2829

2930
// ROOT includes
3031
#include <TMCProcess.h> // for VMC Particle Production Process
@@ -566,8 +567,10 @@ struct RecoDecay {
566567
while (!motherFound && arrayIds[-stage].size() > 0 && (depthMax < 0 || -stage < depthMax)) {
567568
// vector of mother indices for the current stage
568569
std::vector<int64_t> arrayIdsStage{};
570+
// std::cout << "[getMother] number of mothers at stage " << stage << ": " << arrayIds[-stage].size() << std::endl;
569571
for (auto iPart : arrayIds[-stage]) { // check all the particles that were the mothers at the previous stage, o2-linter: disable=const-ref-in-for-loop (int elements)
570572
auto particleMother = particlesMC.rawIteratorAt(iPart - particlesMC.offset());
573+
// std::cout << "[getMother] particleMother.pdgCode(): " << particleMother.pdgCode() << std::endl;
571574
if (particleMother.has_mothers()) {
572575
for (auto iMother = particleMother.mothersIds().front(); iMother <= particleMother.mothersIds().back(); ++iMother) { // loop over the mother particles of the analysed particle
573576
if (std::find(arrayIdsStage.begin(), arrayIdsStage.end(), iMother) != arrayIdsStage.end()) { // if a mother is still present in the vector, do not check it again
@@ -576,10 +579,9 @@ struct RecoDecay {
576579
auto mother = particlesMC.rawIteratorAt(iMother - particlesMC.offset());
577580
// Check mother's PDG code.
578581
auto pdgParticleIMother = mother.pdgCode(); // PDG code of the mother
579-
// printf("getMother: ");
580-
// for (int i = stage; i < 0; i++) // Indent to make the tree look nice.
581-
// printf(" ");
582-
// printf("Stage %d: Mother PDG: %d, Index: %d\n", stage, pdgParticleIMother, iMother);
582+
for (int i = stage; i < 0; i++) // Indent to make the tree look nice.
583+
std::cout << " ";
584+
// std::cout << "[getMother] Stage: " << stage << " Mother PDG: " << pdgParticleIMother << ", Index: " << iMother << std::endl;
583585
if (pdgParticleIMother == pdgMother) { // exact PDG match
584586
sgn = 1;
585587
indexMother = iMother;
@@ -640,13 +642,15 @@ struct RecoDecay {
640642
}
641643

642644
bool isFinal = false; // Flag to indicate the end of recursion
645+
std::cout << "[getDaughters] Stage: " << static_cast<int>(stage) << ", Particle PDG: " << particle.pdgCode() << ", Index: " << particle.globalIndex() << ", depthMax: " << static_cast<int>(depthMax) << std::endl;
643646
if (depthMax > -1 && stage >= depthMax) { // Maximum depth has been reached (or exceeded).
644647
isFinal = true;
645648
}
646649
// Check whether there are any daughters.
647650
if (!isFinal && !particle.has_daughters()) {
648651
// If the original particle has no daughters, we do nothing and exit.
649652
if (stage == 0) {
653+
// std::cout << "[getDaughters] No daughters of the original particle " << particle.globalIndex() << std::endl;
650654
// Printf("getDaughters: No daughters of %d", index);
651655
return;
652656
}
@@ -658,24 +662,29 @@ struct RecoDecay {
658662
if (!isFinal && stage > 0) {
659663
// If the particle has daughters but is considered to be final, we label it as final.
660664
for (auto pdgI : arrPdgFinal) { // o2-linter: disable=const-ref-in-for-loop (int elements)
665+
// std::cout << "[getDaughters] Checking PDG code: " << pdgI << " with " << pdgParticle << std::endl;
661666
if (pdgParticle == std::abs(pdgI)) { // Accept antiparticles.
662667
isFinal = true;
668+
// std::cout << "[getDaughters] Particle " << particle.globalIndex() << " is final with PDG code " << pdgParticle << std::endl;
663669
break;
664670
}
665671
}
666672
}
667673
// If the particle is labelled as final, we add this particle in the list of final daughters and exit.
668674
if (isFinal) {
669675
// printf("getDaughters: ");
670-
// for (int i = 0; i < stage; i++) // Indent to make the tree look nice.
671-
// printf(" ");
676+
for (int i = 0; i < stage; i++) // Indent to make the tree look nice.
677+
std::cout << " ";
678+
// printf(" ");
672679
// printf("Stage %d: Adding %d (PDG %d) as final daughter.\n", stage, index, pdgParticle);
680+
// std::cout << "[getDaughters] Adding particle " << particle.globalIndex() << " as final daughter with PDG code " << pdgParticle << std::endl;
673681
list->push_back(particle.globalIndex());
674682
return;
675683
}
676684
// If we are here, we have to follow the daughter tree.
677685
// printf("getDaughters: ");
678-
// for (int i = 0; i < stage; i++) // Indent to make the tree look nice.
686+
for (int i = 0; i < stage; i++) // Indent to make the tree look nice.
687+
std::cout << " ";
679688
// printf(" ");
680689
// printf("Stage %d: %d (PDG %d) -> %d-%d\n", stage, index, pdgParticle, indexDaughterFirst, indexDaughterLast);
681690
// Call itself to get daughters of daughters recursively.
@@ -702,11 +711,11 @@ struct RecoDecay {
702711
/// \param nKaToPi number of kaon prongs decayed to a pion
703712
/// \param nInteractionsWithMaterial number of daughter particles that interacted with material
704713
/// \return index of the mother particle if the mother and daughters are correct, -1 otherwise
705-
template <bool acceptFlavourOscillation = false, bool checkProcess = false, bool acceptIncompleteReco = false, bool acceptTrackDecay = false, bool acceptTrackIntWithMaterial = false, std::size_t N, typename T, typename U>
714+
template <bool acceptFlavourOscillation = false, bool checkProcess = false, bool acceptIncompleteReco = false, bool acceptTrackDecay = false, bool acceptTrackIntWithMaterial = false, bool forceIncompleteReco = false, std::size_t NDaug, std::size_t NFinPartSpecies, typename T, typename U>
706715
static int getMatchedMCRec(const T& particlesMC,
707-
const std::array<U, N>& arrDaughters,
716+
const std::array<U, NDaug>& arrDaughters,
708717
int pdgMother,
709-
std::array<int, N> arrPdgDaughters,
718+
std::array<int, NFinPartSpecies> arrPdgDaughters,
710719
bool acceptAntiParticles = false,
711720
int8_t* sign = nullptr,
712721
int depthMax = 1,
@@ -722,13 +731,13 @@ struct RecoDecay {
722731
int8_t nInteractionsWithMaterialLocal = 0; // number of interactions with material
723732
int indexMother = -1; // index of the mother particle
724733
std::vector<int> arrAllDaughtersIndex; // vector of indices of all daughters of the mother of the first provided daughter
725-
std::array<int, N> arrDaughtersIndex; // array of indices of provided daughters
734+
std::array<int, NFinPartSpecies> arrDaughtersIndex; // array of indices of provided daughters
726735
if (sign) {
727736
*sign = sgn;
728737
}
729738
if constexpr (acceptFlavourOscillation) {
730739
// Loop over decay candidate prongs to spot possible oscillation decay product
731-
for (std::size_t iProng = 0; iProng < N; ++iProng) {
740+
for (std::size_t iProng = 0; iProng < NDaug; ++iProng) {
732741
if (!arrDaughters[iProng].has_mcParticle()) {
733742
return -1;
734743
}
@@ -740,7 +749,7 @@ struct RecoDecay {
740749
}
741750
}
742751
// Loop over decay candidate prongs
743-
for (std::size_t iProng = 0; iProng < N; ++iProng) {
752+
for (std::size_t iProng = 0; iProng < NDaug; ++iProng) {
744753
if (!arrDaughters[iProng].has_mcParticle()) {
745754
return -1;
746755
}
@@ -790,71 +799,78 @@ struct RecoDecay {
790799
indexMother = getMother(particlesMC, particleI, pdgMother, acceptAntiParticles, &sgn, depthMax);
791800
// Check whether mother was found.
792801
if (indexMother <= -1) {
793-
// Printf("MC Rec: Rejected: bad mother index or PDG");
802+
std::cout << "MC Rec: Rejected: bad mother index or PDG" << std::endl;
794803
return -1;
795804
}
796-
// Printf("MC Rec: Good mother: %d", indexMother);
805+
std::cout << "MC Rec: Good mother: " << indexMother << std::endl;
797806
auto particleMother = particlesMC.rawIteratorAt(indexMother - particlesMC.offset());
798807
// Check the daughter indices.
799808
if (!particleMother.has_daughters()) {
800-
// Printf("MC Rec: Rejected: bad daughter index range: %d-%d", particleMother.daughtersIds().front(), particleMother.daughtersIds().back());
809+
std::cout << "MC Rec: Rejected: bad daughter index range: " << particleMother.daughtersIds().front() << "-" << particleMother.daughtersIds().back() << std::endl;
801810
return -1;
802811
}
803812
// Check that the number of direct daughters is not larger than the number of expected final daughters.
804813
if constexpr (!acceptIncompleteReco && !checkProcess) {
805-
if (particleMother.daughtersIds().back() - particleMother.daughtersIds().front() + 1 > static_cast<int>(N)) {
806-
// LOG(info) << "MC Rec: Rejected: too many direct daughters: " << particleMother.daughtersIds().back() - particleMother.daughtersIds().front() + 1
807-
// << " (expected " << N << " final)";
808-
// Printf("MC Rec: Rejected: too many direct daughters: %d (expected %ld final)", particleMother.daughtersIds().back() - particleMother.daughtersIds().front() + 1, N);
814+
if (particleMother.daughtersIds().back() - particleMother.daughtersIds().front() + 1 > static_cast<int>(NDaug)) {
815+
std::cout << "MC Rec: Rejected: too many direct daughters: " << particleMother.daughtersIds().back() - particleMother.daughtersIds().front() + 1
816+
<< " (expected " << NDaug << " final)" << std::endl;
817+
// Printf("MC Rec: Rejected: too many direct daughters: %d (expected %ld final)", particleMother.daughtersIds().back() - particleMother.daughtersIds().front() + 1, NDaug);
809818
return -1;
810819
}
811820
}
812821
// Get the list of actual final daughters.
813822
getDaughters<checkProcess>(particleMother, &arrAllDaughtersIndex, arrPdgDaughters, depthMax);
814-
// printf("MC Rec: Mother %d has %d final daughters:", indexMother, arrAllDaughtersIndex.size());
815-
// for (auto i : arrAllDaughtersIndex) {
816-
// printf(" %d", i);
817-
// }
818-
// printf("\n");
823+
std::cout << "MC Rec: Mother " << indexMother << " has " << arrAllDaughtersIndex.size() << " final states" << std::endl;
824+
for (auto i : arrAllDaughtersIndex) {
825+
std::cout << " (" << i << " , pdg: " << particlesMC.rawIteratorAt(i - particlesMC.offset()).pdgCode() << ") , ";
826+
}
827+
std::cout << " " << std::endl;
819828
// Check whether the number of actual final daughters is equal to the number of expected final daughters (i.e. the number of provided prongs).
820-
// Printf("MC Rec: Number of final daughters: %ld (expected %ld)", arrAllDaughtersIndex.size(), N);
821-
if (!acceptIncompleteReco && arrAllDaughtersIndex.size() != N) {
822-
// LOG(info) << "MC Rec: Rejected: incorrect number of final daughters: " << arrAllDaughtersIndex.size() << " (expected " << N << ")";
823-
// Printf("MC Rec: Rejected: incorrect number of final daughters: %ld (expected %ld)", arrAllDaughtersIndex.size(), N);
829+
if (!acceptIncompleteReco && arrAllDaughtersIndex.size() != NDaug) {
830+
std::cout << "MC Rec: Number of final states " << arrAllDaughtersIndex.size() << " (expected " << NDaug << ")" << std::endl;
831+
// LOG(info) << "MC Rec: Rejected: incorrect number of final states " << arrAllDaughtersIndex.size() << " (expected " << NDaug << ")";
832+
// Printf("MC Rec: Rejected: incorrect number of final states %ld (expected %ld)", arrAllDaughtersIndex.size(), NDaug);
833+
return -1;
834+
}
835+
if (forceIncompleteReco && arrAllDaughtersIndex.size() == NDaug) {
836+
std::cout << "MC Rec: Number of final states " << arrAllDaughtersIndex.size() << "vs Number of daughters " << NDaug << ", expected incomplete reco" << std::endl;
837+
// LOG(info) << "MC Rec: Rejected: incorrect number of final states " << arrAllDaughtersIndex.size() << " (expected " << NDaug << ")";
838+
// Printf("MC Rec: Rejected: incorrect number of final states %ld (expected %ld)", arrAllDaughtersIndex.size(), NDaug);
824839
return -1;
825840
}
826841
}
827842
// Check that the daughter is in the list of final daughters.
828843
// (Check that the daughter is not a stepdaughter, i.e. particle pointing to the mother while not being its daughter.)
829844
bool isDaughterFound = false; // Is the index of this prong among the remaining expected indices of daughters?
830845
for (std::size_t iD = 0; iD < arrAllDaughtersIndex.size(); ++iD) {
846+
// std::cout << "MC Rec: Checking daughter " << iProng << " index: " << arrDaughtersIndex[iProng] << " against expected index: " << arrAllDaughtersIndex[iD] << std::endl;
831847
if (arrDaughtersIndex[iProng] == arrAllDaughtersIndex[iD]) {
832848
arrAllDaughtersIndex[iD] = -1; // Remove this index from the array of expected daughters. (Rejects twin daughters, i.e. particle considered twice as a daughter.)
833849
isDaughterFound = true;
834850
break;
835851
}
836852
}
837853
if (!isDaughterFound) {
838-
// Printf("MC Rec: Rejected: bad daughter index: %d not in the list of final daughters", arrDaughtersIndex[iProng]);
854+
std::cout << "MC Rec: Rejected: bad daughter index: " << arrDaughtersIndex[iProng] << " not in the list of final daughters" << std::endl;
839855
return -1;
840856
}
841857
// Check daughter's PDG code.
842858
auto pdgParticleI = particleI.pdgCode(); // PDG code of the ith daughter
843-
// Printf("MC Rec: Daughter %d PDG: %d", iProng, pdgParticleI);
859+
// std::cout << "MC Rec: Daughter " << iProng << " PDG: " << pdgParticleI << std::endl;
844860
bool isPdgFound = false; // Is the PDG code of this daughter among the remaining expected PDG codes?
845-
for (std::size_t iProngCp = 0; iProngCp < N; ++iProngCp) {
861+
for (std::size_t iProngCp = 0; iProngCp < NDaug; ++iProngCp) {
846862
if (pdgParticleI == coefFlavourOscillation * sgn * arrPdgDaughters[iProngCp]) {
847863
arrPdgDaughters[iProngCp] = 0; // Remove this PDG code from the array of expected ones.
848864
isPdgFound = true;
849865
break;
850866
}
851867
}
852868
if (!isPdgFound) {
853-
// Printf("MC Rec: Rejected: bad daughter PDG: %d", pdgParticleI);
869+
std::cout << "MC Rec: Rejected: bad daughter PDG: " << pdgParticleI << std::endl;
854870
return -1;
855871
}
856872
}
857-
// Printf("MC Rec: Accepted: m: %d", indexMother);
873+
std::cout << "MC Rec: Accepted: m: " << indexMother << std::endl;
858874
if (sign) {
859875
*sign = sgn;
860876
}
@@ -949,14 +965,14 @@ struct RecoDecay {
949965
}
950966
// Get the list of actual final daughters.
951967
getDaughters<checkProcess>(candidate, &arrAllDaughtersIndex, arrPdgDaughters, depthMax);
952-
// printf("MC Gen: Mother %ld has %ld final daughters:", candidate.globalIndex(), arrAllDaughtersIndex.size());
968+
// printf("MC Gen: Mother %ld has %ld final states", candidate.globalIndex(), arrAllDaughtersIndex.size());
953969
// for (auto i : arrAllDaughtersIndex) {
954970
// printf(" %d", i);
955971
// }
956972
// printf("\n");
957973
// Check whether the number of final daughters is equal to the required number.
958974
if (arrAllDaughtersIndex.size() != N) {
959-
// Printf("MC Gen: Rejected: incorrect number of final daughters: %ld (expected %ld)", arrAllDaughtersIndex.size(), N);
975+
// Printf("MC Gen: Rejected: incorrect number of final states %ld (expected %ld)", arrAllDaughtersIndex.size(), N);
960976
return false;
961977
}
962978
if constexpr (acceptFlavourOscillation) {

0 commit comments

Comments
 (0)