9#include <analysis/modules/BestCandidateSelection/BestCandidateSelectionModule.h>
11#include <analysis/utility/ValueIndexPairSorting.h>
13#include <analysis/VariableManager/Utility.h>
15#include <analysis/DecayDescriptor/DecayDescriptor.h>
17#include <framework/logging/Logger.h>
18#include <framework/utilities/MakeROOTCompatible.h>
31 setDescription(R
"DOC(Sort particles by the value of a given ``variable``
32in the input list and optionally remove particles after the nth position.
34Per default particles are sorted in descending order but it can be switched to
35an ascending order by setting ``selectLowest=True``. The convenience functions
36`modularAnalysis.rankByHighest` and `modularAnalysis.rankByLowest` set this
37parameter automatically based on their names.
39Particles will receive an extra-info field containing their rank as an integer
40starting at 1 (best). The name of this extra-info field defaults to
41``${variable}_rank`` but can be chosen freely using the ``outputVariable``
44The ranking also takes antiparticles into account, so there will only be one
45B+- candidate with ``rank=1``. The remaining list is sorted from best to worst
46candidate (each charge, e.g. B+/B-, separately). The sorting is guaranteed
47to be stable between particle and anti particle list: particles with the same
48value for ``variable`` will keep their relative order. That is, a particle "A"
49which was before another particle "B" in the same list and has the same value
50for ``variable`` will also stay before "B" after sorting.
52If ``allowMultiRank=False`` (the default) candidates with same value of
53``variable`` will have different ranks. If ``allowMultiRank=True`` they will
56IF ``numBest>0`` only candidates with this rank or better will remain in the
57output list. If ``allowMultiRank=True`` that means that there can be more than
58``numBest`` candidates in the output list if they share ranks.
64 addParam(
"variable",
m_variableName,
"Variable which defines the candidate ranking (see ``selectLowest`` for ordering)");
65 addParam(
"selectLowest",
m_selectLowest,
"If true, candidate with lower values of ``variable`` are better, otherwise higher is better",
false);
67 addParam(
"numBest",
m_numBest,
"Keep only particles with this rank or better. If ``allowMultiRank=False`` this is "
68 "identical to the maximum amount of candidates left in the list. Otherwise there may be more candidates if "
69 "some share the same rank (0: keep all)", 0);
70 addParam(
"cut",
m_cutParameter,
"Only candidates passing the cut will be ranked. The others will have rank -1.", std::string(
""));
72 "Name for created variable, which contains the rank for the particle. If not provided, the standard name ``${variable}_rank`` is used.");
73 addParam(
"overwriteRank",
m_overwriteRank,
"If true, the extraInfo of rank is overwritten when the particle has already the extraInfo.",
false);
86 B2ERROR(
"Variable '" <<
m_variableName <<
"' is not available in Variable::Manager!");
89 B2ERROR(
"Variable '" <<
m_variableName <<
"' has wrong data type! It must be either double or integer.");
92 B2ERROR(
"value of numBest must be >= 0!");
99 const string listLabel = ddpart->
getLabel();
103 B2FATAL(
"You are trying to apply a best-candidate-selection on the list " <<
m_inputListName <<
104 " but the label 'all' is protected for lists of final-state particles." <<
105 " It could introduce *very* dangerous bugs.");
106 }
else if (listLabel ==
"MC" or listLabel ==
"V0") {
108 B2FATAL(
"You are trying to apply a best-candidate-selection on the list " <<
m_inputListName <<
109 " but the label " << listLabel <<
" is protected and can not be reduced.");
127 B2WARNING(
"Input list " <<
m_inputList.getName() <<
" was not created?");
135 typedef std::pair<double, unsigned int> ValueIndexPair;
136 std::vector<ValueIndexPair> valueToIndex;
137 const unsigned int numParticles =
m_inputList->getListSize();
138 valueToIndex.reserve(numParticles);
142 if (std::holds_alternative<double>(var_result)) {
143 value = std::get<double>(var_result);
144 }
else if (std::holds_alternative<int>(var_result)) {
145 value = std::get<int>(var_result);
147 valueToIndex.emplace_back(value, p.getArrayIndex());
153 std::stable_sort(valueToIndex.begin(), valueToIndex.end(), ValueIndexPairSorting::lowerPair<ValueIndexPair>);
155 std::stable_sort(valueToIndex.begin(), valueToIndex.end(), ValueIndexPairSorting::higherPair<ValueIndexPair>);
161 double previous_val{0};
162 bool first_candidate{
true};
163 for (
const auto& candidate : valueToIndex) {
165 if (!
m_cut->check(p)) {
170 if (first_candidate) {
171 first_candidate =
false;
186 previous_val = candidate.first;
bool m_allowMultiRank
Give the same rank to candidates with the same value.
virtual void initialize() override
Initialize the module (set up datastore)
virtual void event() override
Process an event.
std::string m_variableName
Variable which defines the candidate ranking.
StoreArray< Particle > m_particles
StoreArray of Particle objects.
bool m_selectLowest
Select the candidate with the lowest value (instead of highest).
virtual ~BestCandidateSelectionModule() override
Destructor.
std::unique_ptr< Variable::Cut > m_cut
cut object which performs the cuts
StoreObjPtr< ParticleList > m_inputList
input particle list
const Variable::Manager::Var * m_variable
Variable which defines the candidate ranking.
std::string m_cutParameter
Selection for candidates to be ranked.
int m_numBest
Number of best candidates to keep.
bool m_overwriteRank
If true, the extraInfo of rank is overwritten when the particle has already the extraInfo.
BestCandidateSelectionModule()
Constructor.
std::string m_outputVariableName
Name of generated Ranking-Variable, if specified by user.
std::string m_inputListName
name of input particle list.
The ParticleType class for identifying different particle types.
static const ParticleSet finalStateParticlesSet
set of final set particles that can be created by the ParticleLoader
Represents a particle in the DecayDescriptor.
int getPDGCode() const
Return PDG code.
std::string getLabel() const
The label of this particle, "default" returned, when no label set.
The DecayDescriptor stores information about a decay tree or parts of a decay tree.
bool init(const std::string &str)
Initialise the DecayDescriptor from given string.
const DecayDescriptorParticle * getMother() const
return mother.
static std::unique_ptr< GeneralCut > compile(const std::string &cut)
Creates an instance of a cut and returns a unique_ptr to it, if you need a copy-able object instead y...
static std::string makeROOTCompatible(std::string str)
Remove special characters that ROOT dislikes in branch names, e.g.
void setDescription(const std::string &description)
Sets the description of the module.
void setPropertyFlags(unsigned int propertyFlags)
Sets the flags for the module properties.
@ c_ParallelProcessingCertified
This module can be run in parallel processing mode safely (All I/O must be done through the data stor...
Class to store reconstructed particles.
bool isRequired(const std::string &name="")
Ensure this array/object has been registered previously.
const Var * getVariable(std::string name)
Get the variable belonging to the given key.
static Manager & Instance()
get singleton instance.
void addParam(const std::string &name, T ¶mVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Abstract base class for different kinds of events.
VariableDataType variabletype
data type of variable
FunctionPtr function
Pointer to function.