29 using parent_mask_t =
unsigned long long;
31 parent_mask_t matchMask(
const std::vector<MatchedParent>& matches) {
32 parent_mask_t mask = 0x0;
33 for (
const auto&
match: matches) {
34 constexpr size_t max_idx = std::numeric_limits<
decltype(mask)>::digits;
35 if (
match.parent_index > max_idx) {
36 throw std::runtime_error(
37 "parent index overflowed the match mask "
38 "[index: " + std::to_string(
match.parent_index) +
39 " , max_mask: " + std::to_string(max_idx) +
"]");
41 mask |= (0x1u <<
match.parent_index);
47 std::string
join(
const std::vector<std::string>& v,
const std::string& sep =
", ") {
49 for (
unsigned int pos = 0;
pos <
v.size();
pos++) {
50 out.append(
v.at(pos));
51 if (pos + 1 <
v.size())
out.append(sep);
57 std::vector<std::string>
out;
58 for (
const auto& v:
container)
out.push_back(std::to_string(v));
64 std::vector<std::string>
output;
65 for (
unsigned int child_n = 0; child_n <
p->nChildren(); child_n++) {
66 output.push_back(std::to_string(
p->child(child_n)->pdgId()));
68 if (
output.empty())
return "none";
69 return "[" +
join(output) +
"]";
76 if (
msg.level() > level)
return;
77 unsigned int n_cascade_candidates = 0;
78 for (
auto& cascade: cascades_raw) {
79 n_cascade_candidates += cascade->size();
82 "n_targets: " << targets->size() <<
", "
83 "n_parents: " << truth->size() <<
", "
84 "n_cascade_candidates: " << n_cascade_candidates <<
87 void logIPMap(MsgStream&
msg,
const IPMap& ipmap,
const MSG::Level level = MSG::VERBOSE)
89 if (
msg.level() > level)
return;
90 for (
const auto& [barcode, children]: ipmap) {
101 std::map<int, std::set<int>> findAllDescendants(
int parent,
const Barcodex& barcodex, std::set<int> history = {})
103 using return_t = std::map<int,std::set<int>>;
104 auto itr = barcodex.find(parent);
105 if (itr == barcodex.end()) {
107 throw std::runtime_error(
108 "can't find barcode " + std::to_string(parent) +
" history:"
109 " {" +
join(hist) +
"}");
111 const std::set<int>&
children = itr->second;
113 if (!history.insert(parent).second) {
115 throw std::runtime_error(
"found cycle, tried to add " + std::to_string(parent) +
" to {" +
join(hist) +
"}");
117 return_t all_children;
118 for (
int child: children) {
122 for (
auto& [dec, dh]: findAllDescendants(child, barcodex, history)) {
123 all_children[dec].merge(dh);
133 if (barkids.size() > 1) {
134 std::set<int> pdg_ids;
135 TLorentzVector sum_p4;
138 sum_p4 += dupkid->p4();
139 pdg_ids.insert(dupkid->pdgId());
140 if (dupkid->nChildren() > child->
nChildren()) child = dupkid;
142 if (pdg_ids.size() != 1) {
143 throw std::runtime_error(
"same barcode, different pdgid: [" +
join(
stringify(pdg_ids)) +
"]");
145 if (
float dr = child->
p4().DeltaR(sum_p4); dr > 0.001) {
146 throw std::runtime_error(
"Same barcode, different vector: { deltaR: " + std::to_string(dr) +
", pdgid: " + std::to_string(child->
pdgId()) +
"}"
155 for (
unsigned int parent_n = 0; parent_n <
p->nParents(); parent_n++) {
157 if (!parent)
throw std::runtime_error(
"broken truth record");
158 if (
parent->pdgId() ==
p->pdgId())
return false;
167 if (
int n_parents =
p->nParents(); n_parents != 1) {
168 throw std::logic_error(
"can't get parent [n_parents: " + std::to_string(n_parents) +
"]");
174 return (
parent->hasCharm() ||
parent->hasBottom()) &&
p->isChLepton();
178 return parent->hasBottom() &&
p->hasCharm();
186 m_pids(pids.begin(), pids.end()),
194 unsigned char n_match = 0;
195 for (
const auto& parent: parents) {
196 for (
const auto& pid:
m_pids) {
197 if (parent.cascade_pids.contains(pid)) n_match++;
200 m_dec(target) = n_match;
242 std::string pfx = jc +
"." +
m_prefix.value();
272 return StatusCode::SUCCESS;
284 using uc_t =
unsigned char;
295 if (targets->empty())
return StatusCode::SUCCESS;
300 std::vector<const xAOD::TruthParticle*> psort;
302 if (!parentids.contains(p->pdgId()))
continue;
303 if (!isOriginal(p))
continue;
307 std::sort(psort.begin(), psort.end(),[](
const auto* p1,
const auto* p2) {return p1->m() > p2->m();});
309 constexpr size_t max_idx = std::numeric_limits<parent_mask_t>::digits;
310 if (psort.size() > max_idx) {
312 "Found too many parent particles to store in parent match mask "
313 "truncating the parent collection [max: " << max_idx <<
", "
314 "n: " << psort.size() <<
"]");
315 psort.resize(max_idx);
319 std::vector<SG::ReadHandle<TPC>> cascades_raw;
321 cascades_raw.emplace_back(key, cxt);
323 logInputs(msgStream(), targets, phandle, cascades_raw);
331 for (
auto& cascade: cascades_raw) {
334 logIPMap(
msg(), ipmap);
336 ATH_MSG_DEBUG(
"merged cascade contains " << barcodex.size() <<
" particles");
341 std::unordered_map<const J*, std::vector<MatchedParent>> labeled_targets;
342 unsigned int n_parents = 0;
343 for (
const auto* p: psort) {
344 unsigned int parent_index = n_parents++;
347 for (
auto& [cbar, histbars]: findAllDescendants(
m_uid(*p), barcodex)) {
348 IPMap::mapped_type& barkids = ipmap.at(cbar);
350 std::vector<std::pair<float, const J*>> drs;
352 const J* drsMinMatch = 0;
353 for (
const auto* j: *targets) {
354 if(j->p4().DeltaR(child->
p4()) < drsMinDR) {
355 drsMinDR=j->p4().DeltaR(child->
p4());
363 match.deltaR = drsMinDR;
364 match.parent_index = parent_index;
366 for (
auto& histbar: histbars) {
367 match.cascade_pids.insert(selectChild(ipmap.at(histbar))->pdgId());
370 labeled_targets[drsMinMatch].push_back(std::move(
match));
380 for (
const J* j: *targets) {
381 if (labeled_targets.contains(j)) {
382 const std::vector<MatchedParent>& matches = labeled_targets.at(j);
383 auto min_dr = [](
auto& p1,
auto& p2) {
384 return p1.deltaR < p2.deltaR;
387 matches.begin(), matches.end(), min_dr);
389 pdgid(*j) = p->pdgId();
391 auto*
container =
dynamic_cast<const TPC*
>(p->container());
394 nMatched(*j) = matches.size();
395 mask(*j) = matchMask(matches);
397 matchPdgId(*j) = child->
pdgId();
398 matchChildCount(*j) = child->
nChildren();
399 auto* matchedContainer =
dynamic_cast<const TPC*
>(child->
container());
400 matchLink(*j) =
JL(*matchedContainer, child->
index());
402 cascadeCount.decorate(*j, matches);
412 matchChildCount(*j) = 0;
413 matchLink(*j) =
JL();
415 cascadeCount.decorateDefault(*j);
425 dec.lock(targets.
get());
428 return StatusCode::SUCCESS;
449 return StatusCode::SUCCESS;
459 auto cascadeWants = [
466 if (
int n_parents = p->nParents(); n_parents == 1) {
467 if (vsl && isSoftLepton(p))
return false;
468 if (vsc && isSoftCharm(p))
return false;
470 if (targid.contains(p->pdgId()))
return true;
471 if (b && p->hasBottom())
return true;
472 if (c && p->hasCharm())
return true;
478 auto insert = [
this, &barcodex, &ipmap](
const auto* p) ->
auto& {
479 ipmap[
m_uid(*p)].insert(p);
480 return barcodex[
m_uid(*p)];
484 if (cascadeWants(p)) {
485 auto& child_set = insert(p);
486 for (
unsigned int child_n = 0; child_n < p->nChildren(); child_n++) {
497 if(ok_missing.contains(p->pdgId())) {
500 auto problem = std::format(
501 "null truth child [barcode={},pdg_id={},child={}of{}]",
503 m_uid(*p), p->pdgId(), child_n, p->nChildren());
505 if (warn_missing.contains(p->pdgId())) {
509 throw std::runtime_error(problem);
512 }
else if (cascadeWants(c)) {
515 child_set.insert(
m_uid(*c));
Scalar deltaR(const MatrixBase< Derived > &vec) const
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_WARNING(x)
ATLAS-specific HepMC functions.
Handle class for adding a decoration to an object.
std::string stringify(T obj)
#define ATLAS_THREAD_SAFE
An algorithm that can be simultaneously executed in multiple threads.
void decorate(const SG::AuxElement &target, const std::vector< MatchedParent > &parents) const
SG::AuxElement::Decorator< unsigned char > m_dec
CascadeCountDecorator(const std::string &name, const std::vector< int > &pids)
void decorateDefault(const SG::AuxElement &target) const
void lock(const xAOD::IParticleContainer *target) const
std::vector< int > m_pids
Base class for elements of a container that can have aux data.
const SG::AuxVectorData * container() const
Return the container holding this element.
size_t index() const
Return the index of this element within its container.
Helper class to provide constant type-safe access to aux data.
const_pointer_type get() const
Dereference the pointer, but don't cache anything.
Handle class for adding a decoration to an object.
SG::WriteDecorHandleKeyArray< JC > m_cascade_count_writer_keys
void addTruthContainer(Barcodex &, IPMap &, const TPC &) const
Gaudi::Property< std::unordered_set< int > > m_allow_missing_children_pdgids
Gaudi::Property< bool > m_add_c
Gaudi::Property< bool > m_veto_soft_lepton
Gaudi::Property< bool > m_add_b
SG::WriteDecorHandleKey< JC > m_target_index_key
TruthParentDecoratorAlg(const std::string &name, ISvcLocator *loc)
std::vector< CascadeCountDecorator > m_cascade_count_decorators
SG::WriteDecorHandleKey< JC > m_target_n_matched_key
std::atomic< unsigned long long > m_total_children
Gaudi::Property< bool > m_use_barcode
SG::WriteDecorHandleKey< JC > m_match_children_key
Gaudi::Property< cascade_counter_property_t > m_counts_matching_cascade
SG::WriteDecorHandleKey< JC > m_target_pdgid_key
Gaudi::Property< std::vector< int > > m_cascade_pdgids
Gaudi::Property< std::string > m_prefix
std::map< int, std::set< const xAOD::TruthParticle * > > IPMap
Gaudi::Property< bool > m_veto_soft_charm
SG::ReadHandleKeyArray< TPC > m_cascades_key
SG::WriteDecorHandleKey< JC > m_match_link_key
virtual StatusCode initialize() override
Gaudi::Property< std::vector< int > > m_parent_pdgids
Gaudi::Property< float > m_missing_children_fraction_warning_threshold
SG::WriteDecorHandleKey< JC > m_target_match_mask_key
virtual StatusCode execute(const EventContext &) const override
std::map< int, std::set< int > > Barcodex
SG::WriteDecorHandleKey< JC > m_target_link_key
SG::WriteDecorHandleKey< JC > m_target_dr_truth_key
SG::ReadHandleKey< JC > m_target_container_key
SG::ConstAccessor< int > m_uid
std::atomic< unsigned long long > m_missing_n_warned
SG::WriteDecorHandleKey< JC > m_match_pdgid_key
xAOD::IParticleContainer JC
SG::ReadHandleKey< TPC > m_parents_key
virtual StatusCode finalize() override
Gaudi::Property< std::unordered_set< int > > m_warn_missing_children_pdgids
Gaudi::Property< float > m_match_delta_r
xAOD::TruthParticleContainer TPC
std::atomic< unsigned long long > m_missing_n_ignored
int pdgId() const
PDG ID code.
size_t nChildren() const
Number of children of this particle.
virtual FourMom_t p4() const override final
The full 4-momentum of the particle.
bool match(std::string s1, std::string s2)
match the individual directories of two strings
std::string join(const std::vector< std::string > &v, const char c=',')
void sort(typename DataModel_detail::iterator< DVL > beg, typename DataModel_detail::iterator< DVL > end)
Specialization of sort for DataVector/List.
TruthParticle_v1 TruthParticle
Typedef to implementation.
TruthParticleContainer_v1 TruthParticleContainer
Declare the latest version of the truth particle container.
DataVector< IParticle > IParticleContainer
Simple convenience declaration of IParticleContainer.
const xAOD::TruthParticle * parent
std::set< int > cascade_pids
const xAOD::TruthParticle * child
unsigned int parent_index