Skip to content

Commit 6c9b313

Browse files
committed
[WIP] Match correlated bkgs
1 parent 43c76ca commit 6c9b313

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
@@ -24,6 +24,7 @@
2424
#include <cstdio>
2525
#include <utility> // std::move
2626
#include <vector> // std::vector
27+
#include <iostream> // std::ostream
2728

2829
// ROOT includes
2930
#include <TMCProcess.h> // for VMC Particle Production Process
@@ -552,8 +553,10 @@ struct RecoDecay {
552553
while (!motherFound && arrayIds[-stage].size() > 0 && (depthMax < 0 || -stage < depthMax)) {
553554
// vector of mother indices for the current stage
554555
std::vector<int64_t> arrayIdsStage{};
556+
// std::cout << "[getMother] number of mothers at stage " << stage << ": " << arrayIds[-stage].size() << std::endl;
555557
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)
556558
auto particleMother = particlesMC.rawIteratorAt(iPart - particlesMC.offset());
559+
// std::cout << "[getMother] particleMother.pdgCode(): " << particleMother.pdgCode() << std::endl;
557560
if (particleMother.has_mothers()) {
558561
for (auto iMother = particleMother.mothersIds().front(); iMother <= particleMother.mothersIds().back(); ++iMother) { // loop over the mother particles of the analysed particle
559562
if (std::find(arrayIdsStage.begin(), arrayIdsStage.end(), iMother) != arrayIdsStage.end()) { // if a mother is still present in the vector, do not check it again
@@ -562,10 +565,9 @@ struct RecoDecay {
562565
auto mother = particlesMC.rawIteratorAt(iMother - particlesMC.offset());
563566
// Check mother's PDG code.
564567
auto pdgParticleIMother = mother.pdgCode(); // PDG code of the mother
565-
// printf("getMother: ");
566-
// for (int i = stage; i < 0; i++) // Indent to make the tree look nice.
567-
// printf(" ");
568-
// printf("Stage %d: Mother PDG: %d, Index: %d\n", stage, pdgParticleIMother, iMother);
568+
for (int i = stage; i < 0; i++) // Indent to make the tree look nice.
569+
std::cout << " ";
570+
// std::cout << "[getMother] Stage: " << stage << " Mother PDG: " << pdgParticleIMother << ", Index: " << iMother << std::endl;
569571
if (pdgParticleIMother == pdgMother) { // exact PDG match
570572
sgn = 1;
571573
indexMother = iMother;
@@ -626,13 +628,15 @@ struct RecoDecay {
626628
}
627629

628630
bool isFinal = false; // Flag to indicate the end of recursion
631+
std::cout << "[getDaughters] Stage: " << static_cast<int>(stage) << ", Particle PDG: " << particle.pdgCode() << ", Index: " << particle.globalIndex() << ", depthMax: " << static_cast<int>(depthMax) << std::endl;
629632
if (depthMax > -1 && stage >= depthMax) { // Maximum depth has been reached (or exceeded).
630633
isFinal = true;
631634
}
632635
// Check whether there are any daughters.
633636
if (!isFinal && !particle.has_daughters()) {
634637
// If the original particle has no daughters, we do nothing and exit.
635638
if (stage == 0) {
639+
// std::cout << "[getDaughters] No daughters of the original particle " << particle.globalIndex() << std::endl;
636640
// Printf("getDaughters: No daughters of %d", index);
637641
return;
638642
}
@@ -644,24 +648,29 @@ struct RecoDecay {
644648
if (!isFinal && stage > 0) {
645649
// If the particle has daughters but is considered to be final, we label it as final.
646650
for (auto pdgI : arrPdgFinal) { // o2-linter: disable=const-ref-in-for-loop (int elements)
651+
// std::cout << "[getDaughters] Checking PDG code: " << pdgI << " with " << pdgParticle << std::endl;
647652
if (pdgParticle == std::abs(pdgI)) { // Accept antiparticles.
648653
isFinal = true;
654+
// std::cout << "[getDaughters] Particle " << particle.globalIndex() << " is final with PDG code " << pdgParticle << std::endl;
649655
break;
650656
}
651657
}
652658
}
653659
// If the particle is labelled as final, we add this particle in the list of final daughters and exit.
654660
if (isFinal) {
655661
// printf("getDaughters: ");
656-
// for (int i = 0; i < stage; i++) // Indent to make the tree look nice.
657-
// printf(" ");
662+
for (int i = 0; i < stage; i++) // Indent to make the tree look nice.
663+
std::cout << " ";
664+
// printf(" ");
658665
// printf("Stage %d: Adding %d (PDG %d) as final daughter.\n", stage, index, pdgParticle);
666+
// std::cout << "[getDaughters] Adding particle " << particle.globalIndex() << " as final daughter with PDG code " << pdgParticle << std::endl;
659667
list->push_back(particle.globalIndex());
660668
return;
661669
}
662670
// If we are here, we have to follow the daughter tree.
663671
// printf("getDaughters: ");
664-
// for (int i = 0; i < stage; i++) // Indent to make the tree look nice.
672+
for (int i = 0; i < stage; i++) // Indent to make the tree look nice.
673+
std::cout << " ";
665674
// printf(" ");
666675
// printf("Stage %d: %d (PDG %d) -> %d-%d\n", stage, index, pdgParticle, indexDaughterFirst, indexDaughterLast);
667676
// Call itself to get daughters of daughters recursively.
@@ -688,11 +697,11 @@ struct RecoDecay {
688697
/// \param nKaToPi number of kaon prongs decayed to a pion
689698
/// \param nInteractionsWithMaterial number of daughter particles that interacted with material
690699
/// \return index of the mother particle if the mother and daughters are correct, -1 otherwise
691-
template <bool acceptFlavourOscillation = false, bool checkProcess = false, bool acceptIncompleteReco = false, bool acceptTrackDecay = false, bool acceptTrackIntWithMaterial = false, std::size_t N, typename T, typename U>
700+
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>
692701
static int getMatchedMCRec(const T& particlesMC,
693-
const std::array<U, N>& arrDaughters,
702+
const std::array<U, NDaug>& arrDaughters,
694703
int pdgMother,
695-
std::array<int, N> arrPdgDaughters,
704+
std::array<int, NFinPartSpecies> arrPdgDaughters,
696705
bool acceptAntiParticles = false,
697706
int8_t* sign = nullptr,
698707
int depthMax = 1,
@@ -708,13 +717,13 @@ struct RecoDecay {
708717
int8_t nInteractionsWithMaterialLocal = 0; // number of interactions with material
709718
int indexMother = -1; // index of the mother particle
710719
std::vector<int> arrAllDaughtersIndex; // vector of indices of all daughters of the mother of the first provided daughter
711-
std::array<int, N> arrDaughtersIndex; // array of indices of provided daughters
720+
std::array<int, NFinPartSpecies> arrDaughtersIndex; // array of indices of provided daughters
712721
if (sign) {
713722
*sign = sgn;
714723
}
715724
if constexpr (acceptFlavourOscillation) {
716725
// Loop over decay candidate prongs to spot possible oscillation decay product
717-
for (std::size_t iProng = 0; iProng < N; ++iProng) {
726+
for (std::size_t iProng = 0; iProng < NDaug; ++iProng) {
718727
if (!arrDaughters[iProng].has_mcParticle()) {
719728
return -1;
720729
}
@@ -726,7 +735,7 @@ struct RecoDecay {
726735
}
727736
}
728737
// Loop over decay candidate prongs
729-
for (std::size_t iProng = 0; iProng < N; ++iProng) {
738+
for (std::size_t iProng = 0; iProng < NDaug; ++iProng) {
730739
if (!arrDaughters[iProng].has_mcParticle()) {
731740
return -1;
732741
}
@@ -776,71 +785,78 @@ struct RecoDecay {
776785
indexMother = getMother(particlesMC, particleI, pdgMother, acceptAntiParticles, &sgn, depthMax);
777786
// Check whether mother was found.
778787
if (indexMother <= -1) {
779-
// Printf("MC Rec: Rejected: bad mother index or PDG");
788+
std::cout << "MC Rec: Rejected: bad mother index or PDG" << std::endl;
780789
return -1;
781790
}
782-
// Printf("MC Rec: Good mother: %d", indexMother);
791+
std::cout << "MC Rec: Good mother: " << indexMother << std::endl;
783792
auto particleMother = particlesMC.rawIteratorAt(indexMother - particlesMC.offset());
784793
// Check the daughter indices.
785794
if (!particleMother.has_daughters()) {
786-
// Printf("MC Rec: Rejected: bad daughter index range: %d-%d", particleMother.daughtersIds().front(), particleMother.daughtersIds().back());
795+
std::cout << "MC Rec: Rejected: bad daughter index range: " << particleMother.daughtersIds().front() << "-" << particleMother.daughtersIds().back() << std::endl;
787796
return -1;
788797
}
789798
// Check that the number of direct daughters is not larger than the number of expected final daughters.
790799
if constexpr (!acceptIncompleteReco && !checkProcess) {
791-
if (particleMother.daughtersIds().back() - particleMother.daughtersIds().front() + 1 > static_cast<int>(N)) {
792-
// LOG(info) << "MC Rec: Rejected: too many direct daughters: " << particleMother.daughtersIds().back() - particleMother.daughtersIds().front() + 1
793-
// << " (expected " << N << " final)";
794-
// Printf("MC Rec: Rejected: too many direct daughters: %d (expected %ld final)", particleMother.daughtersIds().back() - particleMother.daughtersIds().front() + 1, N);
800+
if (particleMother.daughtersIds().back() - particleMother.daughtersIds().front() + 1 > static_cast<int>(NDaug)) {
801+
std::cout << "MC Rec: Rejected: too many direct daughters: " << particleMother.daughtersIds().back() - particleMother.daughtersIds().front() + 1
802+
<< " (expected " << NDaug << " final)" << std::endl;
803+
// Printf("MC Rec: Rejected: too many direct daughters: %d (expected %ld final)", particleMother.daughtersIds().back() - particleMother.daughtersIds().front() + 1, NDaug);
795804
return -1;
796805
}
797806
}
798807
// Get the list of actual final daughters.
799808
getDaughters<checkProcess>(particleMother, &arrAllDaughtersIndex, arrPdgDaughters, depthMax);
800-
// printf("MC Rec: Mother %d has %d final daughters:", indexMother, arrAllDaughtersIndex.size());
801-
// for (auto i : arrAllDaughtersIndex) {
802-
// printf(" %d", i);
803-
// }
804-
// printf("\n");
809+
std::cout << "MC Rec: Mother " << indexMother << " has " << arrAllDaughtersIndex.size() << " final states" << std::endl;
810+
for (auto i : arrAllDaughtersIndex) {
811+
std::cout << " (" << i << " , pdg: " << particlesMC.rawIteratorAt(i - particlesMC.offset()).pdgCode() << ") , ";
812+
}
813+
std::cout << " " << std::endl;
805814
// Check whether the number of actual final daughters is equal to the number of expected final daughters (i.e. the number of provided prongs).
806-
// Printf("MC Rec: Number of final daughters: %ld (expected %ld)", arrAllDaughtersIndex.size(), N);
807-
if (!acceptIncompleteReco && arrAllDaughtersIndex.size() != N) {
808-
// LOG(info) << "MC Rec: Rejected: incorrect number of final daughters: " << arrAllDaughtersIndex.size() << " (expected " << N << ")";
809-
// Printf("MC Rec: Rejected: incorrect number of final daughters: %ld (expected %ld)", arrAllDaughtersIndex.size(), N);
815+
if (!acceptIncompleteReco && arrAllDaughtersIndex.size() != NDaug) {
816+
std::cout << "MC Rec: Number of final states " << arrAllDaughtersIndex.size() << " (expected " << NDaug << ")" << std::endl;
817+
// LOG(info) << "MC Rec: Rejected: incorrect number of final states " << arrAllDaughtersIndex.size() << " (expected " << NDaug << ")";
818+
// Printf("MC Rec: Rejected: incorrect number of final states %ld (expected %ld)", arrAllDaughtersIndex.size(), NDaug);
819+
return -1;
820+
}
821+
if (forceIncompleteReco && arrAllDaughtersIndex.size() == NDaug) {
822+
std::cout << "MC Rec: Number of final states " << arrAllDaughtersIndex.size() << "vs Number of daughters " << NDaug << ", expected incomplete reco" << std::endl;
823+
// LOG(info) << "MC Rec: Rejected: incorrect number of final states " << arrAllDaughtersIndex.size() << " (expected " << NDaug << ")";
824+
// Printf("MC Rec: Rejected: incorrect number of final states %ld (expected %ld)", arrAllDaughtersIndex.size(), NDaug);
810825
return -1;
811826
}
812827
}
813828
// Check that the daughter is in the list of final daughters.
814829
// (Check that the daughter is not a stepdaughter, i.e. particle pointing to the mother while not being its daughter.)
815830
bool isDaughterFound = false; // Is the index of this prong among the remaining expected indices of daughters?
816831
for (std::size_t iD = 0; iD < arrAllDaughtersIndex.size(); ++iD) {
832+
// std::cout << "MC Rec: Checking daughter " << iProng << " index: " << arrDaughtersIndex[iProng] << " against expected index: " << arrAllDaughtersIndex[iD] << std::endl;
817833
if (arrDaughtersIndex[iProng] == arrAllDaughtersIndex[iD]) {
818834
arrAllDaughtersIndex[iD] = -1; // Remove this index from the array of expected daughters. (Rejects twin daughters, i.e. particle considered twice as a daughter.)
819835
isDaughterFound = true;
820836
break;
821837
}
822838
}
823839
if (!isDaughterFound) {
824-
// Printf("MC Rec: Rejected: bad daughter index: %d not in the list of final daughters", arrDaughtersIndex[iProng]);
840+
std::cout << "MC Rec: Rejected: bad daughter index: " << arrDaughtersIndex[iProng] << " not in the list of final daughters" << std::endl;
825841
return -1;
826842
}
827843
// Check daughter's PDG code.
828844
auto pdgParticleI = particleI.pdgCode(); // PDG code of the ith daughter
829-
// Printf("MC Rec: Daughter %d PDG: %d", iProng, pdgParticleI);
845+
// std::cout << "MC Rec: Daughter " << iProng << " PDG: " << pdgParticleI << std::endl;
830846
bool isPdgFound = false; // Is the PDG code of this daughter among the remaining expected PDG codes?
831-
for (std::size_t iProngCp = 0; iProngCp < N; ++iProngCp) {
847+
for (std::size_t iProngCp = 0; iProngCp < NDaug; ++iProngCp) {
832848
if (pdgParticleI == coefFlavourOscillation * sgn * arrPdgDaughters[iProngCp]) {
833849
arrPdgDaughters[iProngCp] = 0; // Remove this PDG code from the array of expected ones.
834850
isPdgFound = true;
835851
break;
836852
}
837853
}
838854
if (!isPdgFound) {
839-
// Printf("MC Rec: Rejected: bad daughter PDG: %d", pdgParticleI);
855+
std::cout << "MC Rec: Rejected: bad daughter PDG: " << pdgParticleI << std::endl;
840856
return -1;
841857
}
842858
}
843-
// Printf("MC Rec: Accepted: m: %d", indexMother);
859+
std::cout << "MC Rec: Accepted: m: " << indexMother << std::endl;
844860
if (sign) {
845861
*sign = sgn;
846862
}
@@ -935,14 +951,14 @@ struct RecoDecay {
935951
}
936952
// Get the list of actual final daughters.
937953
getDaughters<checkProcess>(candidate, &arrAllDaughtersIndex, arrPdgDaughters, depthMax);
938-
// printf("MC Gen: Mother %ld has %ld final daughters:", candidate.globalIndex(), arrAllDaughtersIndex.size());
954+
// printf("MC Gen: Mother %ld has %ld final states", candidate.globalIndex(), arrAllDaughtersIndex.size());
939955
// for (auto i : arrAllDaughtersIndex) {
940956
// printf(" %d", i);
941957
// }
942958
// printf("\n");
943959
// Check whether the number of final daughters is equal to the required number.
944960
if (arrAllDaughtersIndex.size() != N) {
945-
// Printf("MC Gen: Rejected: incorrect number of final daughters: %ld (expected %ld)", arrAllDaughtersIndex.size(), N);
961+
// Printf("MC Gen: Rejected: incorrect number of final states %ld (expected %ld)", arrAllDaughtersIndex.size(), N);
946962
return false;
947963
}
948964
if constexpr (acceptFlavourOscillation) {

0 commit comments

Comments
 (0)