Skip to content

Commit 62b1c59

Browse files
[PWGHF] Add table with ML outputs for reco MC (#10063)
Co-authored-by: ALICE Action Bot <alibuild@cern.ch>
1 parent 90b4d10 commit 62b1c59

File tree

5 files changed

+72
-38
lines changed

5 files changed

+72
-38
lines changed

PWGHF/DataModel/CandidateReconstructionTables.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,8 @@ enum DecayType { DplusToPiKPi = 0,
949949
XicToPKPi,
950950
N3ProngDecays }; // always keep N3ProngDecays at the end
951951

952+
static const int DstarToPiKPiBkg = DecayType::N3ProngDecays;
953+
952954
// Ds± → K± K∓ π± or D± → K± K∓ π±
953955

954956
enum DecayChannelDToKKPi {

PWGHF/TableProducer/candidateCreator3Prong.cxx

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -787,11 +787,6 @@ struct HfCandidateCreator3ProngExpressions {
787787
o2::framework::Configurable<bool> matchKinkedDecayTopology{"matchKinkedDecayTopology", false, "Match also candidates with tracks that decay with kinked topology"};
788788
o2::framework::Configurable<bool> matchInteractionsWithMaterial{"matchInteractionsWithMaterial", false, "Match also candidates with tracks that interact with material"};
789789

790-
bool createDplus{false};
791-
bool createDs{false};
792-
bool createLc{false};
793-
bool createXic{false};
794-
795790
HfEventSelectionMc hfEvSelMc; // mc event selection and monitoring
796791
using BCsInfo = soa::Join<aod::BCs, aod::Timestamps, aod::BcSels>;
797792
HistogramRegistry registry{"registry"};
@@ -817,28 +812,11 @@ struct HfCandidateCreator3ProngExpressions {
817812
for (const DeviceSpec& device : workflows.devices) {
818813
if (device.name.compare("hf-candidate-creator-3prong") == 0) {
819814
hfEvSelMc.configureFromDevice(device);
820-
for (const auto& option : device.options) {
821-
if (option.name.compare("createDplus") == 0) {
822-
createDplus = option.defaultValue.get<bool>();
823-
} else if (option.name.compare("createDs") == 0) {
824-
createDs = option.defaultValue.get<bool>();
825-
} else if (option.name.compare("createLc") == 0) {
826-
createLc = option.defaultValue.get<bool>();
827-
} else if (option.name.compare("createXic") == 0) {
828-
createXic = option.defaultValue.get<bool>();
829-
}
830-
}
831815
break;
832816
}
833817
}
834818

835819
hfEvSelMc.addHistograms(registry); // particles monitoring
836-
837-
LOGP(info, "Flags for candidate creation from the reco workflow:");
838-
LOGP(info, " --> createDplus = {}", createDplus);
839-
LOGP(info, " --> createDs = {}", createDs);
840-
LOGP(info, " --> createLc = {}", createLc);
841-
LOGP(info, " --> createXic = {}", createXic);
842820
}
843821

844822
/// Performs MC matching.
@@ -897,7 +875,7 @@ struct HfCandidateCreator3ProngExpressions {
897875
}
898876

899877
// D± → π± K∓ π±
900-
if (createDplus) {
878+
if (flag == 0) {
901879
if (matchKinkedDecayTopology && matchInteractionsWithMaterial) {
902880
indexRec = RecoDecay::getMatchedMCRec<false, false, false, true, true>(mcParticles, arrayDaughters, Pdg::kDPlus, std::array{+kPiPlus, -kKPlus, +kPiPlus}, true, &sign, 2, &nKinkedTracks, &nInteractionsWithMaterial);
903881
} else if (matchKinkedDecayTopology && !matchInteractionsWithMaterial) {
@@ -913,7 +891,7 @@ struct HfCandidateCreator3ProngExpressions {
913891
}
914892

915893
// Ds± → K± K∓ π± and D± → K± K∓ π±
916-
if (flag == 0 && createDs) {
894+
if (flag == 0) {
917895
bool isDplus = false;
918896
if (matchKinkedDecayTopology && matchInteractionsWithMaterial) {
919897
indexRec = RecoDecay::getMatchedMCRec<false, false, false, true, true>(mcParticles, arrayDaughters, Pdg::kDS, std::array{+kKPlus, -kKPlus, +kPiPlus}, true, &sign, 2, &nKinkedTracks, &nInteractionsWithMaterial);
@@ -958,8 +936,21 @@ struct HfCandidateCreator3ProngExpressions {
958936
}
959937
}
960938

939+
// D* → D0π → Kππ
940+
if (flag == 0) {
941+
if (matchKinkedDecayTopology) {
942+
indexRec = RecoDecay::getMatchedMCRec<false, false, false, true>(mcParticles, arrayDaughters, Pdg::kDStar, std::array{+kPiPlus, +kPiPlus, -kKPlus}, true, &sign, 2, &nKinkedTracks);
943+
} else {
944+
indexRec = RecoDecay::getMatchedMCRec(mcParticles, arrayDaughters, Pdg::kDStar, std::array{+kPiPlus, +kPiPlus, -kKPlus}, true, &sign, 2);
945+
}
946+
if (indexRec > -1) {
947+
flag = sign * (1 << DstarToPiKPiBkg);
948+
channel = 1;
949+
}
950+
}
951+
961952
// Λc± → p± K∓ π±
962-
if (flag == 0 && createLc) {
953+
if (flag == 0) {
963954
if (matchKinkedDecayTopology && matchInteractionsWithMaterial) {
964955
indexRec = RecoDecay::getMatchedMCRec<false, false, false, true, true>(mcParticles, arrayDaughters, Pdg::kLambdaCPlus, std::array{+kProton, -kKPlus, +kPiPlus}, true, &sign, 2, &nKinkedTracks, &nInteractionsWithMaterial);
965956
} else if (matchKinkedDecayTopology && !matchInteractionsWithMaterial) {
@@ -994,7 +985,7 @@ struct HfCandidateCreator3ProngExpressions {
994985
}
995986

996987
// Ξc± → p± K∓ π±
997-
if (flag == 0 && createXic) {
988+
if (flag == 0) {
998989
if (matchKinkedDecayTopology && matchInteractionsWithMaterial) {
999990
indexRec = RecoDecay::getMatchedMCRec<false, false, false, true, true>(mcParticles, arrayDaughters, Pdg::kXiCPlus, std::array{+kProton, -kKPlus, +kPiPlus}, true, &sign, 2, &nKinkedTracks, &nInteractionsWithMaterial);
1000991
} else if (matchKinkedDecayTopology && !matchInteractionsWithMaterial) {
@@ -1049,7 +1040,7 @@ struct HfCandidateCreator3ProngExpressions {
10491040
}
10501041
continue;
10511042
}
1052-
hf_mc_gen::fillMcMatchGen3Prong(mcParticles, mcParticlesPerMcColl, rowMcMatchGen, rejectBackground, createDplus, createDs, createLc, createXic);
1043+
hf_mc_gen::fillMcMatchGen3Prong(mcParticles, mcParticlesPerMcColl, rowMcMatchGen, rejectBackground);
10531044
}
10541045
}
10551046

PWGHF/TableProducer/candidateCreatorMcGen.cxx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@ struct HfCandidateCreatorMcGen {
4646
Configurable<bool> fillB0{"fillB0", false, "fill table for B0 candidates"};
4747
Configurable<bool> rejectBackground2Prong{"rejectBackground2Prong", false, "Reject particles from PbPb background for 2 prong candidates"};
4848
Configurable<bool> rejectBackground3Prong{"rejectBackground3Prong", false, "Reject particles from PbPb background for 3 prong candidates"};
49-
Configurable<bool> createDplus{"createDplus", false, "Create D+ in 3 prong"};
50-
Configurable<bool> createDs{"createDs", false, "Create Ds in 3 prong"};
51-
Configurable<bool> createLc{"createLc", false, "Create Lc in 3 prong"};
52-
Configurable<bool> createXic{"createXic", false, "Create Xic in 3 prong"};
5349

5450
Preslice<aod::McParticles> mcParticlesPerMcCollision = aod::mcparticle::mcCollisionId;
5551

@@ -63,7 +59,7 @@ struct HfCandidateCreatorMcGen {
6359
hf_mc_gen::fillMcMatchGen2Prong(mcParticles, mcParticlesPerMcColl, rowMcMatchGen2Prong, rejectBackground2Prong);
6460
}
6561
if (fill3Prong) {
66-
hf_mc_gen::fillMcMatchGen3Prong(mcParticles, mcParticlesPerMcColl, rowMcMatchGen3Prong, rejectBackground3Prong, createDplus, createDs, createLc, createXic);
62+
hf_mc_gen::fillMcMatchGen3Prong(mcParticles, mcParticlesPerMcColl, rowMcMatchGen3Prong, rejectBackground3Prong);
6763
}
6864
}
6965
if (fillBplus) {

PWGHF/TableProducer/treeCreatorDplusToPiKPi.cxx

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,13 @@ DECLARE_SOA_COLUMN(Ct, ct, float);
7979
// Events
8080
DECLARE_SOA_COLUMN(IsEventReject, isEventReject, int); //! Event rejection flag
8181
DECLARE_SOA_COLUMN(RunNumber, runNumber, int); //! Run number
82+
// ML scores
83+
DECLARE_SOA_COLUMN(MlScore0, mlScore0, float); //! ML score of the first configured index
84+
DECLARE_SOA_COLUMN(MlScore1, mlScore1, float); //! ML score of the second configured index
8285
} // namespace full
86+
DECLARE_SOA_TABLE(HfCandDpMls, "AOD", "HFCANDDPML",
87+
full::MlScore0,
88+
full::MlScore1)
8389

8490
DECLARE_SOA_TABLE(HfCandDpLites, "AOD", "HFCANDDPLITE",
8591
hf_cand::Chi2PCA,
@@ -233,26 +239,31 @@ struct HfTreeCreatorDplusToPiKPi {
233239
Produces<o2::aod::HfCandDpFullEvs> rowCandidateFullEvents;
234240
Produces<o2::aod::HfCandDpFullPs> rowCandidateFullParticles;
235241
Produces<o2::aod::HfCandDpLites> rowCandidateLite;
242+
Produces<o2::aod::HfCandDpMls> rowCandidateMl;
236243

237244
Configurable<int> selectionFlagDplus{"selectionFlagDplus", 1, "Selection Flag for Dplus"};
238245
Configurable<bool> fillCandidateLiteTable{"fillCandidateLiteTable", false, "Switch to fill lite table with candidate properties"};
239246
// parameters for production of training samples
240247
Configurable<bool> fillOnlySignal{"fillOnlySignal", false, "Flag to fill derived tables with signal for ML trainings"};
248+
Configurable<bool> fillOnlySignalMl{"fillOnlySignalMl", false, "Flag to fill derived tables with MC and ML info"};
241249
Configurable<bool> fillOnlyBackground{"fillOnlyBackground", false, "Flag to fill derived tables with background for ML trainings"};
242250
Configurable<float> downSampleBkgFactor{"downSampleBkgFactor", 1., "Fraction of background candidates to keep for ML trainings"};
243251
Configurable<float> ptMaxForDownSample{"ptMaxForDownSample", 10., "Maximum pt for the application of the downsampling factor"};
252+
Configurable<std::vector<int>> classMl{"classMlindexes", {0, 2}, "Indexes of ML bkg and non-prompt scores."};
244253

245254
HfHelper hfHelper;
246255

247256
using SelectedCandidatesMc = soa::Filtered<soa::Join<aod::HfCand3Prong, aod::HfCand3ProngMcRec, aod::HfSelDplusToPiKPi>>;
248257
using MatchedGenCandidatesMc = soa::Filtered<soa::Join<aod::McParticles, aod::HfCand3ProngMcGen>>;
258+
using SelectedCandidatesMcWithMl = soa::Filtered<soa::Join<aod::HfCand3Prong, aod::HfCand3ProngMcRec, aod::HfSelDplusToPiKPi, aod::HfMlDplusToPiKPi>>;
249259
using TracksWPid = soa::Join<aod::Tracks, aod::TracksPidPi, aod::PidTpcTofFullPi, aod::TracksPidKa, aod::PidTpcTofFullKa>;
250260

251261
Filter filterSelectCandidates = aod::hf_sel_candidate_dplus::isSelDplusToPiKPi >= selectionFlagDplus;
252262
Filter filterMcGenMatching = nabs(o2::aod::hf_cand_3prong::flagMcMatchGen) == static_cast<int8_t>(BIT(aod::hf_cand_3prong::DecayType::DplusToPiKPi));
253263

254264
Partition<SelectedCandidatesMc> reconstructedCandSig = nabs(aod::hf_cand_3prong::flagMcMatchRec) == static_cast<int8_t>(BIT(aod::hf_cand_3prong::DecayType::DplusToPiKPi)) || nabs(aod::hf_cand_3prong::flagMcMatchRec) == static_cast<int8_t>(BIT(aod::hf_cand_3prong::DecayType::DsToKKPi)); // DecayType::DsToKKPi is used to flag both Ds± → K± K∓ π± and D± → K± K∓ π±
255265
Partition<SelectedCandidatesMc> reconstructedCandBkg = nabs(aod::hf_cand_3prong::flagMcMatchRec) != static_cast<int8_t>(BIT(aod::hf_cand_3prong::DecayType::DplusToPiKPi));
266+
Partition<SelectedCandidatesMcWithMl> reconstructedCandSigMl = nabs(aod::hf_cand_3prong::flagMcMatchRec) == static_cast<int8_t>(BIT(aod::hf_cand_3prong::DecayType::DplusToPiKPi)) || nabs(aod::hf_cand_3prong::flagMcMatchRec) == static_cast<int8_t>(BIT(aod::hf_cand_3prong::DecayType::DsToKKPi)) || nabs(aod::hf_cand_3prong::flagMcMatchRec) == static_cast<int8_t>(BIT(aod::hf_cand_3prong::DstarToPiKPiBkg)); // DecayType::DsToKKPi is used to flag both Ds± → K± K∓ π± and D± → K± K∓ π±
256267

257268
void init(InitContext const&)
258269
{
@@ -271,7 +282,7 @@ struct HfTreeCreatorDplusToPiKPi {
271282
runNumber);
272283
}
273284

274-
template <bool doMc = false, typename T>
285+
template <bool doMc = false, bool doMl = false, typename T>
275286
void fillCandidateTable(const T& candidate)
276287
{
277288
int8_t flagMc = 0;
@@ -283,6 +294,16 @@ struct HfTreeCreatorDplusToPiKPi {
283294
channelMc = candidate.flagMcDecayChanRec();
284295
}
285296

297+
std::vector<float> outputMl = {-999., -999.};
298+
if constexpr (doMl) {
299+
for (unsigned int iclass = 0; iclass < classMl->size(); iclass++) {
300+
outputMl[iclass] = candidate.mlProbDplusToPiKPi()[classMl->at(iclass)];
301+
}
302+
rowCandidateMl(
303+
outputMl[0],
304+
outputMl[1]);
305+
}
306+
286307
auto prong0 = candidate.template prong0_as<TracksWPid>();
287308
auto prong1 = candidate.template prong1_as<TracksWPid>();
288309
auto prong2 = candidate.template prong2_as<TracksWPid>();
@@ -450,6 +471,7 @@ struct HfTreeCreatorDplusToPiKPi {
450471
aod::McCollisions const&,
451472
SelectedCandidatesMc const& candidates,
452473
MatchedGenCandidatesMc const& particles,
474+
SelectedCandidatesMcWithMl const&,
453475
TracksWPid const&)
454476
{
455477
// Filling event properties
@@ -468,6 +490,22 @@ struct HfTreeCreatorDplusToPiKPi {
468490
for (const auto& candidate : reconstructedCandSig) {
469491
fillCandidateTable<true>(candidate);
470492
}
493+
} else if (fillOnlySignalMl) {
494+
rowCandidateMl.reserve(reconstructedCandSigMl.size());
495+
if (fillCandidateLiteTable) {
496+
rowCandidateLite.reserve(reconstructedCandSigMl.size());
497+
} else {
498+
rowCandidateFull.reserve(reconstructedCandSigMl.size());
499+
}
500+
for (const auto& candidate : reconstructedCandSigMl) {
501+
if (downSampleBkgFactor < 1.) {
502+
float pseudoRndm = candidate.ptProng0() * 1000. - (int64_t)(candidate.ptProng0() * 1000);
503+
if (candidate.pt() < ptMaxForDownSample && pseudoRndm >= downSampleBkgFactor) {
504+
continue;
505+
}
506+
}
507+
fillCandidateTable<true, true>(candidate);
508+
}
471509
} else if (fillOnlyBackground) {
472510
if (fillCandidateLiteTable) {
473511
rowCandidateLite.reserve(reconstructedCandBkg.size());

PWGHF/Utils/utilsMcGen.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ void fillMcMatchGen2Prong(T const& mcParticles, U const& mcParticlesPerMcColl, V
7979
}
8080

8181
template <typename T, typename U, typename V>
82-
void fillMcMatchGen3Prong(T const& mcParticles, U const& mcParticlesPerMcColl, V& rowMcMatchGen, bool rejectBackground, bool createDplus, bool createDs, bool createLc, bool createXic)
82+
void fillMcMatchGen3Prong(T const& mcParticles, U const& mcParticlesPerMcColl, V& rowMcMatchGen, bool rejectBackground)
8383
{
8484
using namespace o2::constants::physics;
8585

@@ -104,14 +104,14 @@ void fillMcMatchGen3Prong(T const& mcParticles, U const& mcParticlesPerMcColl, V
104104
}
105105

106106
// D± → π± K∓ π±
107-
if (createDplus) {
107+
if (flag == 0) {
108108
if (RecoDecay::isMatchedMCGen(mcParticles, particle, Pdg::kDPlus, std::array{+kPiPlus, -kKPlus, +kPiPlus}, true, &sign, 2)) {
109109
flag = sign * (1 << o2::aod::hf_cand_3prong::DecayType::DplusToPiKPi);
110110
}
111111
}
112112

113113
// Ds± → K± K∓ π± and D± → K± K∓ π±
114-
if (flag == 0 && createDs) {
114+
if (flag == 0) {
115115
bool isDplus = false;
116116
if (RecoDecay::isMatchedMCGen(mcParticles, particle, Pdg::kDS, std::array{+kKPlus, -kKPlus, +kPiPlus}, true, &sign, 2)) {
117117
// DecayType::DsToKKPi is used to flag both Ds± → K± K∓ π± and D± → K± K∓ π±
@@ -139,8 +139,15 @@ void fillMcMatchGen3Prong(T const& mcParticles, U const& mcParticlesPerMcColl, V
139139
}
140140
}
141141

142+
// D*± → D0(bar) π±
143+
if (flag == 0) {
144+
if (RecoDecay::isMatchedMCGen(mcParticles, particle, Pdg::kDStar, std::array{+kPiPlus, +kPiPlus, -kKPlus}, true, &sign, 2)) {
145+
flag = sign * (1 << o2::aod::hf_cand_3prong::DstarToPiKPiBkg);
146+
}
147+
}
148+
142149
// Λc± → p± K∓ π±
143-
if (flag == 0 && createLc) {
150+
if (flag == 0) {
144151
if (RecoDecay::isMatchedMCGen(mcParticles, particle, Pdg::kLambdaCPlus, std::array{+kProton, -kKPlus, +kPiPlus}, true, &sign, 2)) {
145152
flag = sign * (1 << o2::aod::hf_cand_3prong::DecayType::LcToPKPi);
146153

@@ -163,7 +170,7 @@ void fillMcMatchGen3Prong(T const& mcParticles, U const& mcParticlesPerMcColl, V
163170
}
164171

165172
// Ξc± → p± K∓ π±
166-
if (flag == 0 && createXic) {
173+
if (flag == 0) {
167174
if (RecoDecay::isMatchedMCGen(mcParticles, particle, Pdg::kXiCPlus, std::array{+kProton, -kKPlus, +kPiPlus}, true, &sign, 2)) {
168175
flag = sign * (1 << o2::aod::hf_cand_3prong::DecayType::XicToPKPi);
169176
}

0 commit comments

Comments
 (0)