CMS 3D CMS Logo

List of all members | Public Member Functions | Private Attributes
pat::XGBooster Class Reference

#include <XGBooster.h>

Public Member Functions

void addFeature (std::string name)
 
float predict (const int iterationEnd=0)
 
float predict (const std::vector< float > &features, const int iterationEnd=0) const
 
void reset ()
 Reset feature values. More...
 
void set (std::string name, float value)
 
 XGBooster (std::string model_file)
 
 XGBooster (std::string model_file, std::string model_features)
 

Private Attributes

BoosterHandle booster_
 
std::map< std::string, unsigned int > feature_name_to_index_
 
std::vector< float > features_
 

Detailed Description

Definition at line 11 of file XGBooster.h.

Constructor & Destructor Documentation

◆ XGBooster() [1/2]

XGBooster::XGBooster ( std::string  model_file)

Definition at line 47 of file XGBooster.cc.

References booster_, and mps_update::status.

47  {
48  int status = XGBoosterCreate(nullptr, 0, &booster_);
49  if (status != 0)
50  throw std::runtime_error("Failed to create XGBooster");
51  status = XGBoosterLoadModel(booster_, model_file.c_str());
52  if (status != 0)
53  throw std::runtime_error("Failed to load XGBoost model");
54  XGBoosterSetParam(booster_, "nthread", "1");
55 }
BoosterHandle booster_
Definition: XGBooster.h:31

◆ XGBooster() [2/2]

XGBooster::XGBooster ( std::string  model_file,
std::string  model_features 
)

Definition at line 57 of file XGBooster.cc.

References addFeature(), Skims_PA_cff::content, lowptgsfeleseed::features(), geometryDiff::file, read_features(), and AlCaHLTBitMon_QueryRunRegistry::string.

57  : XGBooster(model_file) {
58  std::ifstream file(model_features);
59  if (!file.is_open())
60  throw std::runtime_error("Failed to open file: " + model_features);
61 
62  std::string content((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
63  file.close();
64 
65  std::vector<std::string> features = read_features(content);
66 
67  for (const auto& feature : features) {
68  addFeature(feature);
69  }
70 }
XGBooster(std::string model_file)
Definition: XGBooster.cc:47
std::vector< float > features(const reco::PreId &ecal, const reco::PreId &hcal, double rho, const reco::BeamSpot &spot, noZS::EcalClusterLazyTools &ecalTools)
std::vector< std::string > read_features(const std::string &content)
Definition: XGBooster.cc:16
void addFeature(std::string name)
Definition: XGBooster.cc:74

Member Function Documentation

◆ addFeature()

void XGBooster::addFeature ( std::string  name)

Features need to be entered in the order they are used in the model

Definition at line 74 of file XGBooster.cc.

References feature_name_to_index_, features_, and Skims_PA_cff::name.

Referenced by XGBooster().

74  {
75  features_.push_back(0);
77 }
std::vector< float > features_
Definition: XGBooster.h:29
std::map< std::string, unsigned int > feature_name_to_index_
Definition: XGBooster.h:30

◆ predict() [1/2]

float XGBooster::predict ( const int  iterationEnd = 0)

Definition at line 81 of file XGBooster.cc.

References feature_name_to_index_, features_, mps_fire::i, CommonMethods::isnan(), reset(), runTheMatrix::ret, and AlCaHLTBitMon_QueryRunRegistry::string.

Referenced by pat::computeSoftMvaRun3().

81  {
82  // check if all feature values are set properly
83  for (unsigned int i = 0; i < features_.size(); ++i)
84  if (std::isnan(features_.at(i))) {
85  std::string feature_name;
86  for (const auto& pair : feature_name_to_index_) {
87  if (pair.second == i) {
88  feature_name = pair.first;
89  break;
90  }
91  }
92  throw std::runtime_error("Feature is not set: " + feature_name);
93  }
94 
95  float const ret = predict(features_, iterationEnd);
96 
97  reset();
98 
99  return ret;
100 }
void reset()
Reset feature values.
Definition: XGBooster.cc:72
def isnan(num)
ret
prodAgent to be discontinued
float predict(const int iterationEnd=0)
Definition: XGBooster.cc:81
std::vector< float > features_
Definition: XGBooster.h:29
std::map< std::string, unsigned int > feature_name_to_index_
Definition: XGBooster.h:30

◆ predict() [2/2]

float XGBooster::predict ( const std::vector< float > &  features,
const int  iterationEnd = 0 
) const

Definition at line 102 of file XGBooster.cc.

References cms::cuda::assert(), booster_, feature_name_to_index_, lowptgsfeleseed::features(), mps_fire::result, runTheMatrix::ret, and offlineSlimmedPrimaryVertices_cfi::score.

102  {
103  float result{-999.};
104 
105  if (features.empty()) {
106  throw std::runtime_error("Vector of input features is empty");
107  }
108 
109  if (feature_name_to_index_.size() != features.size())
110  throw std::runtime_error("Feature size mismatch");
111 
112  DMatrixHandle dvalues;
113  XGDMatrixCreateFromMat(&features[0], 1, features.size(), 9e99, &dvalues);
114 
115  bst_ulong out_len = 0;
116  const float* score = nullptr;
117 
118  char json[256]; // Make sure the buffer is large enough to hold the resulting JSON string
119 
120  // Use snprintf to format the JSON string with the external value
121  std::snprintf(json,
122  sizeof(json),
123  R"({
124  "type": 0,
125  "training": false,
126  "iteration_begin": 0,
127  "iteration_end": %d,
128  "strict_shape": false
129  })",
130  iterationEnd);
131 
132  // Shape of output prediction
133  bst_ulong const* out_shape = nullptr;
134 
135  auto ret = XGBoosterPredictFromDMatrix(booster_, dvalues, json, &out_shape, &out_len, &score);
136 
137  if (ret == 0) {
138  assert(out_len == 1 && "Unexpected prediction format");
139  result = score[0];
140  }
141 
142  XGDMatrixFree(dvalues);
143 
144  return result;
145 }
ret
prodAgent to be discontinued
assert(be >=bs)
BoosterHandle booster_
Definition: XGBooster.h:31
nlohmann::json json
std::vector< float > features(const reco::PreId &ecal, const reco::PreId &hcal, double rho, const reco::BeamSpot &spot, noZS::EcalClusterLazyTools &ecalTools)
std::map< std::string, unsigned int > feature_name_to_index_
Definition: XGBooster.h:30

◆ reset()

void XGBooster::reset ( void  )

Reset feature values.

Definition at line 72 of file XGBooster.cc.

References features_, ntuplemaker::fill, and dqmiodatasetharvest::nan.

Referenced by predict().

72 { std::fill(features_.begin(), features_.end(), std::nan("")); }
std::vector< float > features_
Definition: XGBooster.h:29

◆ set()

void XGBooster::set ( std::string  name,
float  value 
)

Definition at line 79 of file XGBooster.cc.

References feature_name_to_index_, features_, Skims_PA_cff::name, and relativeConstraints::value.

Referenced by pat::computeSoftMvaRun3(), and fillMatchInfoForStation().

std::vector< float > features_
Definition: XGBooster.h:29
std::map< std::string, unsigned int > feature_name_to_index_
Definition: XGBooster.h:30

Member Data Documentation

◆ booster_

BoosterHandle pat::XGBooster::booster_
private

Definition at line 31 of file XGBooster.h.

Referenced by predict(), and XGBooster().

◆ feature_name_to_index_

std::map<std::string, unsigned int> pat::XGBooster::feature_name_to_index_
private

Definition at line 30 of file XGBooster.h.

Referenced by addFeature(), predict(), and set().

◆ features_

std::vector<float> pat::XGBooster::features_
private

Definition at line 29 of file XGBooster.h.

Referenced by addFeature(), predict(), reset(), and set().