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 ()
 
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 46 of file XGBooster.cc.

References booster_, and mps_update::status.

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

◆ XGBooster() [2/2]

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

Definition at line 56 of file XGBooster.cc.

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

56  : XGBooster(model_file) {
57  std::ifstream file(model_features);
58  if (!file.is_open())
59  throw std::runtime_error("Failed to open file: " + model_features);
60 
61  std::string content((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
62  file.close();
63 
64  std::vector<std::string> features = read_features(content);
65 
66  for (const auto& feature : features) {
67  addFeature(feature);
68  }
69 }
XGBooster(std::string model_file)
Definition: XGBooster.cc:46
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:15
void addFeature(std::string name)
Definition: XGBooster.cc:73

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 73 of file XGBooster.cc.

References feature_name_to_index_, features_, and Skims_PA_cff::name.

Referenced by XGBooster().

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

◆ predict()

float XGBooster::predict ( )

Definition at line 80 of file XGBooster.cc.

References cms::cuda::assert(), booster_, feature_name_to_index_, features_, mps_fire::i, CommonMethods::isnan(), reset(), mps_fire::result, runTheMatrix::ret, offlineSlimmedPrimaryVertices_cfi::score, and AlCaHLTBitMon_QueryRunRegistry::string.

Referenced by pat::computeSoftMvaRun3().

80  {
81  float result(-999.);
82 
83  // check if all feature values are set properly
84  for (unsigned int i = 0; i < features_.size(); ++i)
85  if (std::isnan(features_.at(i))) {
86  std::string feature_name;
87  for (const auto& pair : feature_name_to_index_) {
88  if (pair.second == i) {
89  feature_name = pair.first;
90  break;
91  }
92  }
93  throw std::runtime_error("Feature is not set: " + feature_name);
94  }
95 
96  DMatrixHandle dvalues;
97  XGDMatrixCreateFromMat(&features_[0], 1, features_.size(), 9e99, &dvalues);
98 
99  bst_ulong out_len = 0;
100  const float* score = nullptr;
101 
102  // config json
103  const char* json = R"({
104  "type": 0,
105  "training": false,
106  "iteration_begin": 0,
107  "iteration_end": 0,
108  "strict_shape": false
109  })";
110 
111  // Shape of output prediction
112  bst_ulong const* out_shape = nullptr;
113 
114  auto ret = XGBoosterPredictFromDMatrix(booster_, dvalues, json, &out_shape, &out_len, &score);
115 
116  XGDMatrixFree(dvalues);
117 
118  if (ret == 0) {
119  assert(out_len == 1 && "Unexpected prediction format");
120  result = score[0];
121  }
122 
123  reset();
124 
125  return result;
126 }
void reset()
Reset feature values.
Definition: XGBooster.cc:71
def isnan(num)
ret
prodAgent to be discontinued
assert(be >=bs)
BoosterHandle booster_
Definition: XGBooster.h:30
nlohmann::json json
std::vector< float > features_
Definition: XGBooster.h:28
std::map< std::string, unsigned int > feature_name_to_index_
Definition: XGBooster.h:29

◆ reset()

void XGBooster::reset ( void  )

Reset feature values.

Definition at line 71 of file XGBooster.cc.

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

Referenced by predict().

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

◆ set()

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

Definition at line 78 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:28
std::map< std::string, unsigned int > feature_name_to_index_
Definition: XGBooster.h:29

Member Data Documentation

◆ booster_

BoosterHandle pat::XGBooster::booster_
private

Definition at line 30 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 29 of file XGBooster.h.

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

◆ features_

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

Definition at line 28 of file XGBooster.h.

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