14 size_t readVariables(tinyxml2::XMLElement*
root,
const char*
key, std::vector<std::string>&
names) {
18 if (
root !=
nullptr) {
19 for (tinyxml2::XMLElement*
e =
root->FirstChildElement(
key);
e !=
nullptr;
e =
e->NextSiblingElement(
key)) {
20 names.push_back(
e->Attribute(
"Expression"));
28 bool isTerminal(tinyxml2::XMLElement* node) {
30 for (tinyxml2::XMLElement*
e = node->FirstChildElement(
"Node");
e !=
nullptr;
e =
e->NextSiblingElement(
"Node")) {
36 unsigned int countIntermediateNodes(tinyxml2::XMLElement* node) {
37 unsigned int count = 0;
38 for (tinyxml2::XMLElement*
e = node->FirstChildElement(
"Node");
e !=
nullptr;
e =
e->NextSiblingElement(
"Node")) {
39 count += countIntermediateNodes(
e);
44 unsigned int countTerminalNodes(tinyxml2::XMLElement* node) {
45 unsigned int count = 0;
46 for (tinyxml2::XMLElement*
e = node->FirstChildElement(
"Node");
e !=
nullptr;
e =
e->NextSiblingElement(
"Node")) {
47 count += countTerminalNodes(
e);
53 tinyxml2::XMLElement* node,
58 bool isAdaClassifier) {
59 bool nodeIsTerminal = isTerminal(node);
63 node->QueryDoubleAttribute(
"res", &response);
66 node->QueryDoubleAttribute(
"nType", &response);
68 if (isAdaClassifier) {
69 node->QueryDoubleAttribute(
"purity", &response);
71 node->QueryDoubleAttribute(
"res", &response);
76 tree.Responses().push_back(response);
78 int thisidx =
tree.CutIndices().size();
84 node->QueryIntAttribute(
"IVar", &selector);
85 node->QueryFloatAttribute(
"Cut", &cutval);
86 node->QueryBoolAttribute(
"cType", &ctype);
88 tree.CutIndices().push_back(static_cast<unsigned char>(selector));
93 cutval = std::nextafter(cutval, std::numeric_limits<float>::lowest());
95 tree.CutVals().push_back(cutval);
96 tree.LeftIndices().push_back(0);
97 tree.RightIndices().push_back(0);
99 tinyxml2::XMLElement* left =
nullptr;
100 tinyxml2::XMLElement* right =
nullptr;
101 for (tinyxml2::XMLElement*
e = node->FirstChildElement(
"Node");
e !=
nullptr;
e =
e->NextSiblingElement(
"Node")) {
102 if (*(
e->Attribute(
"pos")) ==
'l')
104 else if (*(
e->Attribute(
"pos")) ==
'r')
111 tree.LeftIndices()[thisidx] = isTerminal(left) ? -
tree.Responses().size() :
tree.CutIndices().size();
112 addNode(
tree, left,
scale, isRegression, useYesNoLeaf, adjustboundary, isAdaClassifier);
114 tree.RightIndices()[thisidx] = isTerminal(right) ? -
tree.Responses().size() :
tree.CutIndices().size();
115 addNode(
tree, right,
scale, isRegression, useYesNoLeaf, adjustboundary, isAdaClassifier);
123 tinyxml2::XMLDocument xmlDoc;
127 if (
hasEnding(weightsFileFullPath,
".xml")) {
128 xmlDoc.LoadFile(weightsFileFullPath.c_str());
129 }
else if (
hasEnding(weightsFileFullPath,
".gz") ||
hasEnding(weightsFileFullPath,
".gzip")) {
135 tinyxml2::XMLElement*
root = xmlDoc.FirstChildElement(
"MethodSetup");
136 readVariables(
root->FirstChildElement(
"Variables"),
"Variable",
varNames);
139 std::map<std::string, std::string>
info;
140 tinyxml2::XMLElement* infoElem = xmlDoc.FirstChildElement(
"MethodSetup")->FirstChildElement(
"GeneralInfo");
141 if (infoElem ==
nullptr) {
142 throw cms::Exception(
"XMLError") <<
"No GeneralInfo found in " << weightsFileFullPath <<
" !!\n";
144 for (tinyxml2::XMLElement*
e = infoElem->FirstChildElement(
"Info");
e !=
nullptr;
145 e =
e->NextSiblingElement(
"Info")) {
148 e->QueryStringAttribute(
"name", &
name);
149 e->QueryStringAttribute(
"value", &
value);
154 std::map<std::string, std::string>
options;
155 tinyxml2::XMLElement* optionsElem = xmlDoc.FirstChildElement(
"MethodSetup")->FirstChildElement(
"Options");
156 if (optionsElem ==
nullptr) {
157 throw cms::Exception(
"XMLError") <<
"No Options found in " << weightsFileFullPath <<
" !!\n";
159 for (tinyxml2::XMLElement*
e = optionsElem->FirstChildElement(
"Option");
e !=
nullptr;
160 e =
e->NextSiblingElement(
"Option")) {
162 e->QueryStringAttribute(
"name", &
name);
167 int rootTrainingVersion(0);
168 if (
info.find(
"ROOT Release") !=
info.end()) {
170 rootTrainingVersion = std::stoi(
s.substr(
s.find(
'[') + 1,
s.find(
']') -
s.find(
'[') - 1));
174 std::vector<double> boostWeights;
175 tinyxml2::XMLElement* weightsElem = xmlDoc.FirstChildElement(
"MethodSetup")->FirstChildElement(
"Weights");
176 if (weightsElem ==
nullptr) {
177 throw cms::Exception(
"XMLError") <<
"No Weights found in " << weightsFileFullPath <<
" !!\n";
179 bool hasTrees =
false;
180 for (tinyxml2::XMLElement*
e = weightsElem->FirstChildElement(
"BinaryTree");
e !=
nullptr;
181 e =
e->NextSiblingElement(
"BinaryTree")) {
184 e->QueryDoubleAttribute(
"boostWeight", &
w);
185 boostWeights.push_back(
w);
188 throw cms::Exception(
"XMLError") <<
"No BinaryTrees found in " << weightsFileFullPath <<
" !!\n";
191 bool isRegression =
info[
"AnalysisType"] ==
"Regression";
195 bool isAdaClassifier = !isRegression &&
options[
"BoostType"] !=
"Grad";
196 bool useYesNoLeaf = isAdaClassifier &&
options[
"UseYesNoLeaf"] ==
"True";
200 bool adjustBoundaries =
201 (rootTrainingVersion >= ROOT_VERSION(5, 34, 20) && rootTrainingVersion < ROOT_VERSION(6, 0, 0)) ||
202 rootTrainingVersion >= ROOT_VERSION(6, 2, 0);
204 auto forest = std::make_unique<GBRForest>();
205 forest->SetInitialResponse(isRegression ? boostWeights[0] : 0.);
208 if (isAdaClassifier) {
209 for (
double w : boostWeights) {
214 forest->Trees().reserve(boostWeights.size());
217 for (tinyxml2::XMLElement*
e = weightsElem->FirstChildElement(
"BinaryTree");
e !=
nullptr;
218 e =
e->NextSiblingElement(
"BinaryTree")) {
219 double scale = isAdaClassifier ? boostWeights[itree] / norm : 1.0;
221 tinyxml2::XMLElement*
root =
e->FirstChildElement(
"Node");
222 forest->Trees().push_back(
GBRTree(countIntermediateNodes(
root), countTerminalNodes(
root)));
223 auto&
tree = forest->Trees().back();
225 addNode(
tree,
root,
scale, isRegression, useYesNoLeaf, adjustBoundaries, isAdaClassifier);
228 if (
tree.CutIndices().empty()) {
229 tree.CutIndices().push_back(0);
230 tree.CutVals().push_back(0);
231 tree.LeftIndices().push_back(0);
232 tree.RightIndices().push_back(0);
256 std::unique_ptr<GBRForest> gbrForest;
268 std::vector<std::string>&
varNames) {