 |
Belle II Software
release-05-01-25
|
2 #include <analysis/modules/ChargedParticleIdentificator/ChargedPidMVAModule.h>
5 #include <mva/interface/Interface.h>
6 #include <analysis/VariableManager/Utility.h>
7 #include <analysis/dataobjects/Particle.h>
8 #include <analysis/dataobjects/ParticleList.h>
11 #include <mdst/dataobjects/ECLCluster.h>
19 setDescription(
"This module evaluates the response of an MVA trained for binary charged particle identification between two hypotheses, S and B. For a given input set of (S,B) mass hypotheses, it takes the Particle objects in the appropriate charged stable particle's ParticleLists, calculates the MVA score using the appropriate xml weight file, and adds it as ExtraInfo to the Particle objects.");
25 "The input signal mass hypothesis' pdgId.",
29 "The input background mass hypothesis' pdgId.",
33 "The input list of ParticleList names.",
34 std::vector<std::string>());
37 "The name of the database payload object with the MVA weights.",
38 std::string(
"ChargedPidMVAWeights"));
41 "Specify whether to use an ECL-only training of the MVA.",
68 " of the signal mass hypothesis is not that of a valid particle in Const::chargedStableSet! Aborting...");
72 " of the background mass hypothesis is not that of a valid particle in Const::chargedStableSet! Aborting...");
80 for (
size_t iDet(0); iDet < Const::PIDDetectors::set().size(); ++iDet) {
81 m_score_varname +=
"_" + std::to_string(Const::PIDDetectors::set()[iDet]);
95 if (!pList) { B2FATAL(
"ParticleList: " << name <<
" could not be found. Aborting..."); }
98 int pdg = abs(pList->getPDGCode());
102 B2FATAL(
"PDG: " << pList->getPDGCode() <<
" of ParticleList: " << pList->getParticleListName() <<
103 " is not that of a valid particle in Const::chargedStableSet! Aborting...");
111 B2DEBUG(11,
"ParticleList: " << pList->getParticleListName() <<
" - N = " << pList->getListSize() <<
" particles.");
113 for (
unsigned int ipart(0); ipart < pList->getListSize(); ++ipart) {
115 Particle* particle = pList->getParticle(ipart);
117 B2DEBUG(11,
"\tParticle [" << ipart <<
"]");
121 const ECLCluster* eclCluster = particle->getECLCluster();
123 B2WARNING(
"\tParticle has invalid Track-ECLCluster relation, skip MVA application...");
129 auto theta = eclCluster->
getTheta();
130 auto p = particle->getP();
136 const auto cutstr = (!cuts->empty()) ? cuts->at(index) :
"";
138 B2DEBUG(11,
"\t\tcharge = " << particle->getCharge());
139 B2DEBUG(11,
"\t\tclusterTheta = " << theta <<
" [rad]");
140 B2DEBUG(11,
"\t\tp = " << p <<
" [GeV/c]");
141 B2DEBUG(11,
"\t\tBrems corrected = " << particle->hasExtraInfo(
"bremsCorrectedPhotonEnergy"));
142 B2DEBUG(11,
"\t\tWeightfile idx = " << index <<
" - (clusterTheta, p) = (" << jth <<
", " << ip <<
")");
143 if (!cutstr.empty()) {
144 B2DEBUG(11,
"\tCategory cut: " << cutstr);
149 B2DEBUG(11,
"\tMVA variables:");
152 for (
unsigned int ivar(0); ivar < nvars; ++ivar) {
156 auto var = varobj->function(particle);
159 var = (std::isnan(var)) ? -999.0 : var;
161 B2DEBUG(11,
"\t\tvar[" << ivar <<
"] : " << varobj->name <<
" = " << var);
167 B2DEBUG(12,
"\tMVA spectators:");
170 for (
unsigned int ispec(0); ispec < nspecs; ++ispec) {
174 auto spec = specobj->function(particle);
176 B2DEBUG(12,
"\t\tspec[" << ispec <<
"] : " << specobj->name <<
" = " << spec);
178 m_datasets.at(index)->m_spectators[ispec] = spec;
183 if (!cutstr.empty()) {
187 if (!cut->check(particle)) {
188 B2WARNING(
"\tParticle didn't pass MVA category cut, skip MVA application...");
196 B2DEBUG(11,
"\tMVA score = " << score);
211 B2INFO(
"Load supported MVA interfaces for charged particle identification...");
217 B2INFO(
"\tLoading weightfiles from the payload class for SIGNAL particle hypothesis: " <<
m_sig_pdg);
220 auto nfiles = serialized_weightfiles->size();
222 B2INFO(
"\tConstruct the MVA experts and datasets from N = " << nfiles <<
" weightfiles...");
231 for (
unsigned int idx(0); idx < nfiles; idx++) {
233 B2DEBUG(12,
"\t\tweightfile[" << idx <<
"]");
236 std::stringstream ss(serialized_weightfiles->at(idx));
240 weightfile.getOptions(general_options);
244 m_variables[idx] = manager.getVariables(general_options.m_variables);
245 m_spectators[idx] = manager.getVariables(general_options.m_spectators);
247 B2DEBUG(12,
"\t\tRetrieved N = " << general_options.m_variables.size()
248 <<
" variables, N = " << general_options.m_spectators.size()
252 m_experts[idx] = supported_interfaces[general_options.m_method]->getExpert();
255 B2DEBUG(12,
"\t\tweightfile loaded successfully into expert[" << idx <<
"]!");
258 std::vector<float> v(general_options.m_variables.size(), 0.0);
259 std::vector<float> s(general_options.m_spectators.size(), 0.0);
260 m_datasets[idx] = std::make_unique<MVA::SingleDataset>(general_options, v, 1.0, s);
262 B2DEBUG(12,
"\t\tdataset[" << idx <<
"] created successfully!");
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...
DatasetsList m_datasets
List of MVA::SingleDataset objects.
std::unique_ptr< DBObjPtr< ChargedPidMVAWeights > > m_weightfiles_representation
Interface to get the database payload with the MVA weight files.
static std::map< std::string, AbstractInterface * > getSupportedInterfaces()
Returns interfaces supported by the MVA Interface.
std::string m_payload_name
The name of the database payload object with the MVA weights.
void setDescription(const std::string &description)
Sets the description of the module.
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
@ c_ParallelProcessingCertified
This module can be run in parallel processing mode safely (All I/O must be done through the data stor...
virtual ~ChargedPidMVAModule()
Destructor, use this to clean up anything you created in the constructor.
ChargedPidMVAModule()
Constructor, for setting module description and parameters.
ExpertsList m_experts
List of MVA::Expert objects.
double getTheta() const
Return Corrected Theta of Shower (radian).
StoreObjPtr< EventMetaData > m_event_metadata
The event information.
VariablesLists m_variables
List of lists of feature variables.
std::string m_payload_name
The name of the database payload object with the MVA weights.
int m_sig_pdg
The input signal mass hypothesis' pdgId.
void setPropertyFlags(unsigned int propertyFlags)
Sets the flags for the module properties.
std::vector< std::string > m_particle_lists
The input list of names of ParticleList objects to which MVA weights will be applied.
std::string m_score_varname
The lookup name of the MVA score variable, given the input S, B mass hypotheses for the algorithm.
Abstract base class for different kinds of events.
Type-safe access to single objects in the data store.
std::vector< std::string > m_particle_lists
The input list of ParticleList names.
bool m_ecl_only
Flag to specify if we use an ECL-only based training.
virtual void beginRun() override
Called once before a new run begins.
virtual void event() override
Called once for each event.
General options which are shared by all MVA trainings.
static void initSupportedInterfaces()
Static function which initliazes all supported interfaces, has to be called once before getSupportedI...
bool m_ecl_only
Flag to specify if we use an ECL-only based training.
Class to store reconstructed particles.
void addParam(const std::string &name, T ¶mVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
static Weightfile loadFromStream(std::istream &stream)
Static function which deserializes a Weightfile from a stream.
int m_bkg_pdg
The input background mass hypothesis' pdgId.
VariablesLists m_spectators
List of lists of spectator variables.
virtual void initialize() override
Use this to initialize resources or memory your module needs.
Global list of available variables.
static Manager & Instance()
get singleton instance.