17 size_t readVariables(tinyxml2::XMLElement*
root,
const char*
key, std::vector<std::string>&
names) {
21 if (
root !=
nullptr) {
22 for (tinyxml2::XMLElement*
e =
root->FirstChildElement(
key);
e !=
nullptr;
e =
e->NextSiblingElement(
key)) {
23 names.push_back(
e->Attribute(
"Expression"));
31 bool isTerminal(tinyxml2::XMLElement* node) {
33 for (tinyxml2::XMLElement*
e = node->FirstChildElement(
"Node");
e !=
nullptr;
e =
e->NextSiblingElement(
"Node")) {
39 unsigned int countIntermediateNodes(tinyxml2::XMLElement* node) {
40 unsigned int count = 0;
41 for (tinyxml2::XMLElement*
e = node->FirstChildElement(
"Node");
e !=
nullptr;
e =
e->NextSiblingElement(
"Node")) {
42 count += countIntermediateNodes(
e);
47 unsigned int countTerminalNodes(tinyxml2::XMLElement* node) {
48 unsigned int count = 0;
49 for (tinyxml2::XMLElement*
e = node->FirstChildElement(
"Node");
e !=
nullptr;
e =
e->NextSiblingElement(
"Node")) {
50 count += countTerminalNodes(
e);
56 tinyxml2::XMLElement* node,
61 bool isAdaClassifier) {
62 bool nodeIsTerminal = isTerminal(node);
66 node->QueryDoubleAttribute(
"res", &response);
69 node->QueryDoubleAttribute(
"nType", &response);
71 if (isAdaClassifier) {
72 node->QueryDoubleAttribute(
"purity", &response);
74 node->QueryDoubleAttribute(
"res", &response);
79 tree.Responses().push_back(response);
81 int thisidx =
tree.CutIndices().size();
87 node->QueryIntAttribute(
"IVar", &selector);
88 node->QueryFloatAttribute(
"Cut", &cutval);
89 node->QueryBoolAttribute(
"cType", &ctype);
91 tree.CutIndices().push_back(static_cast<unsigned char>(selector));
96 cutval = std::nextafter(cutval, std::numeric_limits<float>::lowest());
98 tree.CutVals().push_back(cutval);
99 tree.LeftIndices().push_back(0);
100 tree.RightIndices().push_back(0);
102 tinyxml2::XMLElement* left =
nullptr;
103 tinyxml2::XMLElement* right =
nullptr;
104 for (tinyxml2::XMLElement*
e = node->FirstChildElement(
"Node");
e !=
nullptr;
e =
e->NextSiblingElement(
"Node")) {
105 if (*(
e->Attribute(
"pos")) ==
'l')
107 else if (*(
e->Attribute(
"pos")) ==
'r')
114 tree.LeftIndices()[thisidx] = isTerminal(left) ? -
tree.Responses().size() :
tree.CutIndices().size();
115 addNode(
tree, left,
scale, isRegression, useYesNoLeaf, adjustboundary, isAdaClassifier);
117 tree.RightIndices()[thisidx] = isTerminal(right) ? -
tree.Responses().size() :
tree.CutIndices().size();
118 addNode(
tree, right,
scale, isRegression, useYesNoLeaf, adjustboundary, isAdaClassifier);
127 TFile gbrForestFile(weightsFileFullPath.c_str());
128 std::unique_ptr<GBRForest>
up(gbrForestFile.Get<
GBRForest>(
"gbrForest"));
129 gbrForestFile.Close(
"nodelete");
136 tinyxml2::XMLDocument xmlDoc;
140 if (
hasEnding(weightsFileFullPath,
".xml")) {
141 xmlDoc.LoadFile(weightsFileFullPath.c_str());
142 }
else if (
hasEnding(weightsFileFullPath,
".gz") ||
hasEnding(weightsFileFullPath,
".gzip")) {
148 tinyxml2::XMLElement*
root = xmlDoc.FirstChildElement(
"MethodSetup");
149 readVariables(
root->FirstChildElement(
"Variables"),
"Variable",
varNames);
152 std::map<std::string, std::string>
info;
153 tinyxml2::XMLElement* infoElem = xmlDoc.FirstChildElement(
"MethodSetup")->FirstChildElement(
"GeneralInfo");
154 if (infoElem ==
nullptr) {
155 throw cms::Exception(
"XMLError") <<
"No GeneralInfo found in " << weightsFileFullPath <<
" !!\n";
157 for (tinyxml2::XMLElement*
e = infoElem->FirstChildElement(
"Info");
e !=
nullptr;
158 e =
e->NextSiblingElement(
"Info")) {
161 e->QueryStringAttribute(
"name", &
name);
162 e->QueryStringAttribute(
"value", &
value);
167 std::map<std::string, std::string>
options;
168 tinyxml2::XMLElement* optionsElem = xmlDoc.FirstChildElement(
"MethodSetup")->FirstChildElement(
"Options");
169 if (optionsElem ==
nullptr) {
170 throw cms::Exception(
"XMLError") <<
"No Options found in " << weightsFileFullPath <<
" !!\n";
172 for (tinyxml2::XMLElement*
e = optionsElem->FirstChildElement(
"Option");
e !=
nullptr;
173 e =
e->NextSiblingElement(
"Option")) {
175 e->QueryStringAttribute(
"name", &
name);
180 int rootTrainingVersion(0);
181 if (
info.find(
"ROOT Release") !=
info.end()) {
183 rootTrainingVersion = std::stoi(
s.substr(
s.find(
'[') + 1,
s.find(
']') -
s.find(
'[') - 1));
187 std::vector<double> boostWeights;
188 tinyxml2::XMLElement* weightsElem = xmlDoc.FirstChildElement(
"MethodSetup")->FirstChildElement(
"Weights");
189 if (weightsElem ==
nullptr) {
190 throw cms::Exception(
"XMLError") <<
"No Weights found in " << weightsFileFullPath <<
" !!\n";
192 bool hasTrees =
false;
193 for (tinyxml2::XMLElement*
e = weightsElem->FirstChildElement(
"BinaryTree");
e !=
nullptr;
194 e =
e->NextSiblingElement(
"BinaryTree")) {
197 e->QueryDoubleAttribute(
"boostWeight", &
w);
198 boostWeights.push_back(
w);
201 throw cms::Exception(
"XMLError") <<
"No BinaryTrees found in " << weightsFileFullPath <<
" !!\n";
204 bool isRegression =
info[
"AnalysisType"] ==
"Regression";
208 bool isAdaClassifier = !isRegression &&
options[
"BoostType"] !=
"Grad";
209 bool useYesNoLeaf = isAdaClassifier &&
options[
"UseYesNoLeaf"] ==
"True";
213 bool adjustBoundaries =
214 (rootTrainingVersion >= ROOT_VERSION(5, 34, 20) && rootTrainingVersion < ROOT_VERSION(6, 0, 0)) ||
215 rootTrainingVersion >= ROOT_VERSION(6, 2, 0);
217 auto forest = std::make_unique<GBRForest>();
218 forest->SetInitialResponse(isRegression ? boostWeights[0] : 0.);
221 if (isAdaClassifier) {
222 for (
double w : boostWeights) {
227 forest->Trees().reserve(boostWeights.size());
230 for (tinyxml2::XMLElement*
e = weightsElem->FirstChildElement(
"BinaryTree");
e !=
nullptr;
231 e =
e->NextSiblingElement(
"BinaryTree")) {
232 double scale = isAdaClassifier ? boostWeights[itree] / norm : 1.0;
234 tinyxml2::XMLElement*
root =
e->FirstChildElement(
"Node");
235 forest->Trees().push_back(
GBRTree(countIntermediateNodes(
root), countTerminalNodes(
root)));
236 auto&
tree = forest->Trees().back();
238 addNode(
tree,
root,
scale, isRegression, useYesNoLeaf, adjustBoundaries, isAdaClassifier);
241 if (
tree.CutIndices().empty()) {
242 tree.CutIndices().push_back(0);
243 tree.CutVals().push_back(0);
244 tree.LeftIndices().push_back(0);
245 tree.RightIndices().push_back(0);
269 std::unique_ptr<GBRForest> gbrForest;
281 std::vector<std::string>&
varNames) {