Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
177 changes: 167 additions & 10 deletions PWGJE/Core/JetTaggingUtilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

Expand Down Expand Up @@ -123,24 +124,24 @@ struct BJetSVParams {
bool isBHadron(int pc)
{
using o2::constants::physics::Pdg;
std::vector<int> bPdG = {Pdg::kB0, Pdg::kBPlus, 10511, 10521, 513, 523, 10513, 10523, 20513, 20523, 20513, 20523, 515, 525, Pdg::kBS, 10531, 533, 10533,
20533, 535, 541, 10541, 543, 10543, 20543, 545, 551, 10551, 100551, 110551, 200551, 210551, 553, 10553, 20553,
30553, 100553, 110553, 120553, 130553, 200553, 210553, 220553, 300553, 9000533, 9010553, 555, 10555, 20555,
100555, 110555, 120555, 200555, 557, 100557, Pdg::kLambdaB0, 5112, 5212, 5222, 5114, 5214, 5224, 5132, Pdg::kXiB0, 5312, 5322,
5314, 5324, 5332, 5334, 5142, 5242, 5412, 5422, 5414, 5424, 5342, 5432, 5434, 5442, 5444, 5512, 5522, 5514, 5524,
5532, 5534, 5542, 5544, 5554};
static constexpr auto bPdG = std::to_array<int>({Pdg::kB0, Pdg::kBPlus, 10511, 10521, 513, 523, 10513, 10523, 20513, 20523, 20513, 20523, 515, 525, Pdg::kBS, 10531, 533, 10533,
20533, 535, 541, 10541, 543, 10543, 20543, 545, 551, 10551, 100551, 110551, 200551, 210551, 553, 10553, 20553,
30553, 100553, 110553, 120553, 130553, 200553, 210553, 220553, 300553, 9000553, 9010553, 555, 10555, 20555,
100555, 110555, 120555, 200555, 557, 100557, Pdg::kLambdaB0, 5112, 5212, 5222, 5114, 5214, 5224, 5132, Pdg::kXiB0, 5312, 5322,
5314, 5324, 5332, 5334, 5142, 5242, 5412, 5422, 5414, 5424, 5342, 5432, 5434, 5442, 5444, 5512, 5522, 5514, 5524,
5532, 5534, 5542, 5544, 5554});

return (std::find(bPdG.begin(), bPdG.end(), std::abs(pc)) != bPdG.end());
}
//________________________________________________________________________
bool isCHadron(int pc)
{
using o2::constants::physics::Pdg;
std::vector<int> bPdG = {Pdg::kDPlus, Pdg::kD0, Pdg::kD0StarPlus, Pdg::kD0Star0, 413, 423, 10413, 10423, 20431, 20423, Pdg::kD2StarPlus, Pdg::kD2Star0, Pdg::kDS, 10431, Pdg::kDSStar, Pdg::kDS1, 20433, Pdg::kDS2Star, 441,
10441, 100441, Pdg::kJPsi, 10443, Pdg::kChiC1, 100443, 30443, 9000443, 9010443, 9020443, 445, 100445, Pdg::kLambdaCPlus, Pdg::kSigmaCPlusPlus, 4212, Pdg::kSigmaC0,
4224, 4214, 4114, Pdg::kXiCPlus, Pdg::kXiC0, 4322, 4312, 4324, 4314, Pdg::kOmegaC0, 4334, 4412, Pdg::kXiCCPlusPlus, 4414, 4424, 4432, 4434, 4444};
static constexpr auto cPdG = std::to_array<int>({Pdg::kDPlus, Pdg::kD0, Pdg::kD0StarPlus, Pdg::kD0Star0, 413, 423, 10413, 10423, 20413, 20423, Pdg::kD2StarPlus, Pdg::kD2Star0, Pdg::kDS, 10431, Pdg::kDSStar, Pdg::kDS1, 20433, Pdg::kDS2Star, 441,
10441, 100441, Pdg::kJPsi, 10443, Pdg::kChiC1, 100443, 30443, 9000443, 9010443, 9020443, 445, 100445, Pdg::kLambdaCPlus, Pdg::kSigmaCPlusPlus, 4212, Pdg::kSigmaC0,
4224, 4214, 4114, Pdg::kXiCPlus, Pdg::kXiC0, 4322, 4312, 4324, 4314, Pdg::kOmegaC0, 4334, 4412, Pdg::kXiCCPlusPlus, 4414, 4424, 4432, 4434, 4444});

return (std::find(bPdG.begin(), bPdG.end(), std::abs(pc)) != bPdG.end());
return (std::find(cPdG.begin(), cPdG.end(), std::abs(pc)) != cPdG.end());
}

/**
Expand Down Expand Up @@ -486,6 +487,162 @@ int16_t getJetFlavorHadron(AnyJet const& jet, AllMCParticles const& mcparticles)
return JetTaggingSpecies::lightflavour; // Light flavor jet
}

namespace detail
{
template <typename AllMCParticles, typename Particle>
int64_t beautyQuarkOrigin(Particle const& particle, std::unordered_map<int64_t, int64_t>& origins)
{
const auto id = particle.globalIndex();
if (auto [it, inserted] = origins.try_emplace(id, id); !inserted) {
return it->second;
}

int64_t origin = std::numeric_limits<int64_t>::max();
if (particle.has_mothers()) {
for (auto const& mother : particle.template mothers_as<AllMCParticles>()) {
if (mother.pdgCode() == particle.pdgCode()) {
origin = std::min(origin, beautyQuarkOrigin<AllMCParticles>(mother, origins));
}
}
}
if (origin == std::numeric_limits<int64_t>::max()) {
origin = id;
}
origins.at(id) = origin;
return origin;
}

inline bool isBottomonium(int pdgCode)
{
return (std::abs(pdgCode) / 10) % 1000 == 55;
}

template <typename AllMCParticles, typename Particle>
bool hasBottomoniumAncestor(Particle const& particle, std::unordered_map<int64_t, bool>& ancestry)
{
const auto id = particle.globalIndex();
if (auto [it, inserted] = ancestry.try_emplace(id, false); !inserted) {
return it->second;
}
if (particle.has_mothers()) {
for (auto const& mother : particle.template mothers_as<AllMCParticles>()) {
const int pdgCode = mother.pdgCode();
if ((isBottomonium(pdgCode) && isBHadron(pdgCode)) || hasBottomoniumAncestor<AllMCParticles>(mother, ancestry)) {
ancestry.at(id) = true;
return true;
}
}
}
return false;
}

// An open-beauty meson with positive PDG code contains anti-b, whereas a
// positive beauty baryon contains b. Use this to resolve opposite-sign mothers.
inline int openBeautyQuarkPdg(int pdgCode)
{
const bool isBaryon = (std::abs(pdgCode) / 1000) % 10 != 0;
return (pdgCode > 0) == isBaryon ? 5 : -5;
}

struct BeautyHadronOrigin {
int64_t hadronId;
int64_t quarkId;
};

template <typename AllMCParticles, typename Particle>
BeautyHadronOrigin openBeautyHadronOrigin(Particle const& particle, std::unordered_map<int64_t, int64_t>& quarkOrigins, std::unordered_set<int64_t>& openBeautyAncestors)
{
std::unordered_set<int64_t> visited;
std::unordered_set<int64_t> matchingQuarkOrigins;
int64_t hadronOrigin = std::numeric_limits<int64_t>::max();

auto visit = [&](auto const& self, auto const& hadron, int quarkPdg) -> void {
if (!visited.insert(hadron.globalIndex()).second) {
return;
}
const bool hiddenAncestor = isBottomonium(hadron.pdgCode());
if (!hiddenAncestor) {
quarkPdg = openBeautyQuarkPdg(hadron.pdgCode());
}
bool hasOpenBeautyMother = false;
if (hadron.has_mothers()) {
for (auto const& mother : hadron.template mothers_as<AllMCParticles>()) {
if (isBHadron(mother.pdgCode())) {
if (!isBottomonium(mother.pdgCode())) {
openBeautyAncestors.insert(mother.globalIndex());
hasOpenBeautyMother = true;
}
self(self, mother, quarkPdg);
} else if (mother.pdgCode() == quarkPdg) {
matchingQuarkOrigins.insert(beautyQuarkOrigin<AllMCParticles>(mother, quarkOrigins));
}
}
}
// With no unambiguous quark link, keep the earliest hadron in the chain.
// In particular, two open-beauty daughters can share a bottomonium mother.
if (!hasOpenBeautyMother && !hiddenAncestor) {
hadronOrigin = std::min(hadronOrigin, hadron.globalIndex());
}
};

visit(visit, particle, 0);
if (hadronOrigin == std::numeric_limits<int64_t>::max()) {
hadronOrigin = particle.globalIndex();
}
const int64_t quarkOrigin = matchingQuarkOrigins.size() == 1 ? *matchingQuarkOrigins.begin() : -1;
return {hadronOrigin, quarkOrigin};
}
} // namespace detail

struct JetFlavourMultipleB {
int16_t origin = JetTaggingSpecies::lightflavour;
int nOpenB = 0;
bool hasClosedB = false;
bool hasOpenBFromClosedB = false;
};

/// Classify by hadrons and record open- and closed-beauty information in the jet cone.
template <typename AnyJet, typename AllMCParticles>
JetFlavourMultipleB getJetFlavorHadronMultipleB(AnyJet const& jet, AllMCParticles const& mcparticles)
{
JetFlavourMultipleB result;
bool charmHadron = false;
std::unordered_map<int64_t, int64_t> quarkOrigins;
std::unordered_map<int64_t, bool> bottomoniumAncestry;
std::unordered_map<int64_t, int64_t> openBeautyCandidates;
std::unordered_set<int64_t> openBeautyAncestors;

for (auto const& mcpart : mcparticles) {
const int pdgCode = mcpart.pdgCode();
const bool beautyHadron = isBHadron(pdgCode);
if ((beautyHadron || isCHadron(pdgCode)) && jetutilities::deltaR(jet, mcpart) < jet.r() / 100.f) {
if (!beautyHadron) {
charmHadron = true;
} else if (detail::isBottomonium(pdgCode)) {
result.hasClosedB = true;
} else {
const auto origin = detail::openBeautyHadronOrigin<AllMCParticles>(mcpart, quarkOrigins, openBeautyAncestors);
openBeautyCandidates.emplace(mcpart.globalIndex(), origin.quarkId >= 0 ? origin.quarkId : origin.hadronId);
result.hasOpenBFromClosedB = result.hasOpenBFromClosedB || detail::hasBottomoniumAncestor<AllMCParticles>(mcpart, bottomoniumAncestry);
}
}
}

std::unordered_set<int64_t> inConeOrigins;
for (auto const& [id, origin] : openBeautyCandidates) {
if (!openBeautyAncestors.contains(id)) {
inConeOrigins.insert(origin);
}
}
result.nOpenB = static_cast<int>(inConeOrigins.size());
if (!openBeautyCandidates.empty() || result.hasClosedB) {
result.origin = JetTaggingSpecies::beauty;
} else if (charmHadron) {
result.origin = JetTaggingSpecies::charm;
}
return result;
}

/**
* return acceptance of track about DCA xy and z due to cut for QualityTracks
*/
Expand Down
11 changes: 9 additions & 2 deletions PWGJE/DataModel/JetTagging.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,22 @@ namespace jettagging
namespace flavourdef
{
DECLARE_SOA_COLUMN(Origin, origin, int8_t);
DECLARE_SOA_COLUMN(NOpenB, nOpenB, int); //! Independent open-beauty hadrons in the jet cone, or -1 if truth is unavailable
DECLARE_SOA_COLUMN(HasClosedB, hasClosedB, bool); //! Whether a bottomonium is in the jet cone
DECLARE_SOA_COLUMN(HasOpenBFromClosedB, hasOpenBFromClosedB, bool); //! Whether an open-beauty hadron in the cone has a bottomonium ancestor
} // namespace flavourdef
DECLARE_SOA_COLUMN(BitTaggedjet, bitTaggedjet, uint16_t);
DECLARE_SOA_COLUMN(JetProb, jetProb, float);
DECLARE_SOA_COLUMN(ScoreML, scoreML, float);
DECLARE_SOA_DYNAMIC_COLUMN(IsTagged, isTagged, [](uint16_t bit, BJetTaggingMethod method) -> bool { return TESTBIT(bit, method); });
} // namespace jettagging

#define JETFLAVOURDEF_TABLE_DEF(_jet_type_, _name_, _description_) \
DECLARE_SOA_TABLE(_jet_type_##FlavourDef, "AOD", _description_ "FlavourDef", jettagging::flavourdef::Origin);
#define JETFLAVOURDEF_TABLE_DEF(_jet_type_, _name_, _description_) \
DECLARE_SOA_TABLE(_jet_type_##FlavourDef, "AOD", _description_ "FlavourDef", jettagging::flavourdef::Origin); \
DECLARE_SOA_TABLE(_jet_type_##FlavourDefMultipleB, "AOD", _description_ "FlavourDefMB", \
jettagging::flavourdef::NOpenB, \
jettagging::flavourdef::HasClosedB, \
jettagging::flavourdef::HasOpenBFromClosedB);

#define JETTAGGING_TABLE_DEF(_jet_type_, _name_, _description_) \
namespace _name_##util \
Expand Down
48 changes: 45 additions & 3 deletions PWGJE/TableProducer/heavyFlavourDefinition.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <Framework/AnalysisTask.h>
#include <Framework/Configurable.h>
#include <Framework/InitContext.h>
#include <Framework/Logger.h>
#include <Framework/runDataProcessing.h>

#include <cstdint>
Expand All @@ -31,11 +32,14 @@ using namespace o2;
using namespace o2::framework;
using namespace o2::framework::expressions;

template <typename JetTableMCD, typename JetTableMCP, typename JetFlavourDefMCD, typename JetFlavourDefMCP>
template <typename JetTableMCD, typename JetTableMCP, typename JetFlavourDefMCD, typename JetFlavourDefMCP,
typename JetFlavourDefMultipleBMCD, typename JetFlavourDefMultipleBMCP>
struct HeavyFlavourDefinitionTask {

Produces<JetFlavourDefMCD> flavourTableMCD;
Produces<JetFlavourDefMCP> flavourTableMCP;
Produces<JetFlavourDefMultipleBMCD> flavourTableMultipleBMCD;
Produces<JetFlavourDefMultipleBMCP> flavourTableMultipleBMCP;

Configurable<float> maxDeltaR{"maxDeltaR", 0.25, "maximum distance of jet axis from flavour initiating parton"};
Configurable<bool> searchUpToQuark{"searchUpToQuark", true, "Finding first mother in particles to quark"};
Expand All @@ -46,6 +50,12 @@ struct HeavyFlavourDefinitionTask {

void init(InitContext const&)
{
if (doprocessMCDByDistanceMultipleB && (doprocessMCDByConstituents || doprocessMCDByDistance)) {
LOGF(fatal, "processMCDByDistanceMultipleB requires disabling the other MCD flavour processes");
}
if (doprocessMCPByDistanceMultipleB && (doprocessMCPByConstituents || doprocessMCPByDistance)) {
LOGF(fatal, "processMCPByDistanceMultipleB requires disabling the other MCP flavour processes");
}
}

void processDummy(aod::JetCollisions const&)
Expand Down Expand Up @@ -83,6 +93,24 @@ struct HeavyFlavourDefinitionTask {
}
PROCESS_SWITCH(HeavyFlavourDefinitionTask, processMCDByDistance, "Fill definition of flavour for mcd jets using distance of jet with particles", false);

void processMCDByDistanceMultipleB(soa::Join<aod::JCollisions, aod::JCollisionPIs, aod::JMcCollisionLbs>::iterator const& collision, soa::Join<JetTableMCD, aod::ChargedMCDetectorLevelJetsMatchedToChargedMCParticleLevelJets> const& mcdjets, soa::Join<JetTableMCP, aod::ChargedMCParticleLevelJetsMatchedToChargedMCDetectorLevelJets> const& /*mcpjets*/, aod::JetParticles const& particles) // it used only for charged jets now
{
for (auto const& mcdjet : mcdjets) {
jettaggingutilities::JetFlavourMultipleB flavour{JetTaggingSpecies::none, -1, false, false};
int8_t origin = JetTaggingSpecies::none;
if (collision.has_mcCollision() && mcdjet.has_matchedJetGeo()) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this condition isnt met then origin is set to none instead of -1. Is this intended?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @nzardosh, thanks for reviewing. Yes, -1 in other processes will be anyway overwritten by none if there is no matched MCP jet.

auto const particlesPerColl = particles.sliceBy(particlesPerCollision, collision.mcCollisionId());
for (auto const& mcpjet : mcdjet.template matchedJetGeo_as<soa::Join<JetTableMCP, aod::ChargedMCParticleLevelJetsMatchedToChargedMCDetectorLevelJets>>()) {
flavour = jettaggingutilities::getJetFlavorHadronMultipleB(mcpjet, particlesPerColl);
origin = searchUpToQuark ? jettaggingutilities::getJetFlavor(mcpjet, particlesPerColl) : flavour.origin;
}
}
flavourTableMCD(origin);
flavourTableMultipleBMCD(flavour.nOpenB, flavour.hasClosedB, flavour.hasOpenBFromClosedB);
}
}
PROCESS_SWITCH(HeavyFlavourDefinitionTask, processMCDByDistanceMultipleB, "Fill definition of flavour for mcd jets using distance of jet with particles, considering multiple-b jets", false);

void processMCPByConstituents(JetTableMCP const& mcpjets, aod::JetParticles const& particles)
{
for (auto const& mcpjet : mcpjets) {
Expand All @@ -105,10 +133,24 @@ struct HeavyFlavourDefinitionTask {
}
}
PROCESS_SWITCH(HeavyFlavourDefinitionTask, processMCPByDistance, "Fill definition of flavour for mcp jets using distance of jet with particles", false);

void processMCPByDistanceMultipleB(aod::JetMcCollision const& /*mcCollision*/, JetTableMCP const& mcpjets, aod::JetParticles const& particles)
{
for (auto const& mcpjet : mcpjets) {
const auto flavour = jettaggingutilities::getJetFlavorHadronMultipleB(mcpjet, particles);
const int8_t origin = searchUpToQuark ? jettaggingutilities::getJetFlavor(mcpjet, particles) : flavour.origin;
flavourTableMCP(origin);
flavourTableMultipleBMCP(flavour.nOpenB, flavour.hasClosedB, flavour.hasOpenBFromClosedB);
}
}
PROCESS_SWITCH(HeavyFlavourDefinitionTask, processMCPByDistanceMultipleB, "Fill definition of flavour for mcp jets using distance of jet with particles, considering multiple-b jets", false);
};

using JetHfDefinitionCharged = HeavyFlavourDefinitionTask<soa::Join<aod::ChargedMCDetectorLevelJets, aod::ChargedMCDetectorLevelJetConstituents>, soa::Join<aod::ChargedMCParticleLevelJets, aod::ChargedMCParticleLevelJetConstituents>, aod::ChargedMCDetectorLevelJetFlavourDef, aod::ChargedMCParticleLevelJetFlavourDef>;
// using JetHfDefinitionFull = HeavyFlavourDefinitionTask<soa::Join<aod::FullMCDetectorLevelJets, aod::FullMCDetectorLevelJetConstituents>, soa::Join<aod::FullMCParticleLevelJets, aod::FullMCParticleLevelJetConstituents>, aod::FullMCDetectorLevelJetFlavourDef, aod::FullMCParticleLevelJetFlavourDef>;
using JetHfDefinitionCharged = HeavyFlavourDefinitionTask<
soa::Join<aod::ChargedMCDetectorLevelJets, aod::ChargedMCDetectorLevelJetConstituents>,
soa::Join<aod::ChargedMCParticleLevelJets, aod::ChargedMCParticleLevelJetConstituents>,
aod::ChargedMCDetectorLevelJetFlavourDef, aod::ChargedMCParticleLevelJetFlavourDef,
aod::ChargedMCDetectorLevelJetFlavourDefMultipleB, aod::ChargedMCParticleLevelJetFlavourDefMultipleB>;

WorkflowSpec defineDataProcessing(ConfigContext const& cfgc)
{
Expand Down
4 changes: 2 additions & 2 deletions PWGJE/TableProducer/jetGnnDefinition.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ struct JetGnnDefinitionTask {
LOG(fatal) << "GNN definition requires jets ordered by collision with valid collision indices.";
}
std::vector<int32_t> gnnTrackIds;
if (!acceptedCollision || jet.tracksIds().size() <= 1 || !isAcceptedJet<JetTracksExt>(jet)) {
if (!acceptedCollision || !isAcceptedJet<JetTracksExt>(jet)) {
gnnTrackIndicesTable(gnnTrackIds);
continue;
}
Expand All @@ -181,7 +181,7 @@ struct JetGnnDefinitionTask {
std::vector<std::vector<float>> trackFeatures;
std::vector<std::size_t> constituentIndices;
jettaggingutilities::analyzeJetTrackInfo4GNN(jet, tracks, trackFeatures, trackPtMin, trackDcaXYMax, trackDcaZMax, nJetConst, &constituentIndices);
if (constituentIndices.size() <= 1) {
if (constituentIndices.empty()) {
gnnTrackIndicesTable(gnnTrackIds);
continue;
}
Expand Down
12 changes: 6 additions & 6 deletions PWGJE/TableProducer/jetGnnInference.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ struct JetGnnInferenceTask {
template <typename Jet>
void fillHistograms(Jet const& jet, float db, std::vector<float> const* trackScores = nullptr)
{
if (jet.pt() < jetPtMin) {
if (!isAcceptedJet<JetTracksExt>(jet)) {
return;
}
float dbRange;
Expand Down Expand Up @@ -355,7 +355,7 @@ struct JetGnnInferenceTask {
// Do not reapply inference-side cuts or select a different track subset.
auto const gnnTracks = jet.template tracksGnn_as<JetGnnTracksTable>();
trackScores->assign(gnnTracks.size(), invalidGnnScore);
if (gnnTracks.size() < nTracksMin) {
if (gnnTracks.size() == 0 || gnnTracks.size() < nTracksMin) {
return invalidGnnScore;
}
if (gnnTracks.size() > static_cast<std::size_t>(nJetConst.value)) {
Expand All @@ -375,12 +375,12 @@ struct JetGnnInferenceTask {
LOG(fatal) << "Track-origin inference requires a task with a track prediction output.";
}
} else {
if (jet.tracksIds().size() <= 1 || !isAcceptedJet<Tracks>(jet)) {
if (!isAcceptedJet<Tracks>(jet)) {
return invalidGnnScore;
}
jettaggingutilities::analyzeJetTrackInfo4GNN(jet, tracks, trkFeat, trackPtMin, trackDcaXYMax, trackDcaZMax, nJetConst);
if (trkFeat.size() <= 1) {
LOGF(debug, "GNN inference, fewer than two selected tracks (%d)", jet.globalIndex());
if (trkFeat.empty() || trkFeat.size() < nTracksMin) {
LOGF(debug, "GNN inference, no selected tracks or fewer than nTracksMin (%d)", jet.globalIndex());
return invalidGnnScore;
}
}
Expand Down Expand Up @@ -433,7 +433,7 @@ struct JetGnnInferenceTask {
{
bool acceptedCollision = isAcceptedCollision(collision);
// Preserve a one-to-one positional join with Jets, including jets rejected
// by collision/jet selection or with fewer than two GNN input tracks.
// by collision/jet selection or with insufficient GNN input tracks.
for (auto const& jet : jets) {
if (taggingTable.lastIndex() + 1 != jet.globalIndex()) {
LOG(fatal) << "GNN inference requires jets ordered by collision with valid collision indices.";
Expand Down
Loading
Loading