CMS 3D CMS Logo

List of all members | Public Member Functions | Private Attributes
emtf::Tree Class Reference

#include <Tree.h>

Public Member Functions

void addXMLAttributes (TXMLEngine *xml, Node *node, XMLNodePointer_t np)
 
void buildTree (Int_t nodeLimit)
 
void calcError ()
 
NodefilterEvent (Event *e)
 
NodefilterEventRecursive (Node *node, Event *e)
 
void filterEvents (std::vector< Event * > &tEvents)
 
void filterEventsRecursive (Node *node)
 
Int_t getNumTerminalNodes ()
 
NodegetRootNode ()
 
void getSplitValues (std::vector< std::vector< Double_t >> &v)
 
void getSplitValuesRecursive (Node *node, std::vector< std::vector< Double_t >> &v)
 
std::list< Node * > & getTerminalNodes ()
 
void loadFromXML (const char *filename)
 
void loadFromXMLRecursive (TXMLEngine *xml, XMLNodePointer_t node, Node *tnode)
 
void rankVariables (std::vector< Double_t > &v)
 
void rankVariablesRecursive (Node *node, std::vector< Double_t > &v)
 
void saveToXML (const char *filename)
 
void saveToXMLRecursive (TXMLEngine *xml, Node *node, XMLNodePointer_t np)
 
void setRootNode (Node *sRootNode)
 
void setTerminalNodes (std::list< Node * > &sTNodes)
 
 Tree ()
 
 Tree (std::vector< std::vector< Event * > > &cEvents)
 
 ~Tree ()
 

Private Attributes

Int_t numTerminalNodes
 
Double_t rmsError
 
NoderootNode
 
std::list< Node * > terminalNodes
 

Detailed Description

Definition at line 18 of file Tree.h.

Constructor & Destructor Documentation

Tree::Tree ( )

Definition at line 28 of file Tree.cc.

References numTerminalNodes, rootNode, and terminalNodes.

29 {
30  rootNode = new Node("root");
31 
32  terminalNodes.push_back(rootNode);
33  numTerminalNodes = 1;
34 }
std::list< Node * > terminalNodes
Definition: Tree.h:55
Int_t numTerminalNodes
Definition: Tree.h:56
Node * rootNode
Definition: Tree.h:54
Tree::Tree ( std::vector< std::vector< Event * > > &  cEvents)

Definition at line 36 of file Tree.cc.

References numTerminalNodes, rootNode, emtf::Node::setEvents(), and terminalNodes.

37 {
38  rootNode = new Node("root");
39  rootNode->setEvents(cEvents);
40 
41  terminalNodes.push_back(rootNode);
42  numTerminalNodes = 1;
43 }
std::list< Node * > terminalNodes
Definition: Tree.h:55
void setEvents(std::vector< std::vector< emtf::Event * > > &sEvents)
Definition: Node.cc:203
Int_t numTerminalNodes
Definition: Tree.h:56
Node * rootNode
Definition: Tree.h:54
Tree::~Tree ( )

Definition at line 48 of file Tree.cc.

References rootNode.

49 {
50  // When the tree is destroyed it will delete all of the nodes in the tree.
51 // The deletion begins with the rootnode and continues recursively.
52  delete rootNode;
53 }
Node * rootNode
Definition: Tree.h:54

Member Function Documentation

void Tree::addXMLAttributes ( TXMLEngine *  xml,
Node node,
XMLNodePointer_t  np 
)

Definition at line 322 of file Tree.cc.

References emtf::Node::getFitValue(), emtf::Node::getSplitValue(), emtf::Node::getSplitVariable(), and numToStr().

Referenced by saveToXML(), and saveToXMLRecursive().

323 {
324  // Convert Node members into XML attributes
325  // and add them to the XMLEngine.
326  xml->NewAttr(np, 0, "splitVar", numToStr(node->getSplitVariable()).c_str());
327  xml->NewAttr(np, 0, "splitVal", numToStr(node->getSplitValue()).c_str());
328  xml->NewAttr(np, 0, "fitVal", numToStr(node->getFitValue()).c_str());
329 }
int np
Definition: AMPTWrapper.h:33
std::string numToStr(T num)
Definition: Tree.cc:311
Double_t getSplitValue()
Definition: Node.cc:135
Double_t getFitValue()
Definition: Node.cc:157
Int_t getSplitVariable()
Definition: Node.cc:145
void Tree::buildTree ( Int_t  nodeLimit)

Definition at line 108 of file Tree.cc.

References calcError(), emtf::Node::calcOptimumSplit(), emtf::Node::filterEventsToDaughters(), emtf::Node::getLeftDaughter(), emtf::Node::getRightDaughter(), numTerminalNodes, rootNode, terminalNodes, and emtf::Node::theMiracleOfChildBirth().

Referenced by L1TForest::doRegression().

109 {
110  // We greedily pick the best terminal node to split.
111  Double_t bestNodeErrorReduction = -1;
112  Node* nodeToSplit = 0;
113 
114  if(numTerminalNodes == 1)
115  {
117  calcError();
118  // std::cout << std::endl << " " << numTerminalNodes << " Nodes : " << rmsError << std::endl;
119  }
120 
121  for(std::list<Node*>::iterator it=terminalNodes.begin(); it!=terminalNodes.end(); it++)
122  {
123  if( (*it)->getErrorReduction() > bestNodeErrorReduction )
124  {
125  bestNodeErrorReduction = (*it)->getErrorReduction();
126  nodeToSplit = (*it);
127  }
128  }
129 
130  //std::cout << "nodeToSplit size = " << nodeToSplit->getNumEvents() << std::endl;
131 
132  // If all of the nodes have one event we can't add any more nodes and reduce the error.
133  if(nodeToSplit == 0) return;
134 
135  // Create daughter nodes, and link the nodes together appropriately.
136  nodeToSplit->theMiracleOfChildBirth();
137 
138  // Get left and right daughters for reference.
139  Node* left = nodeToSplit->getLeftDaughter();
140  Node* right = nodeToSplit->getRightDaughter();
141 
142  // Update the list of terminal nodes.
143  terminalNodes.remove(nodeToSplit);
144  terminalNodes.push_back(left);
145  terminalNodes.push_back(right);
147 
148  // Filter the events from the parent into the daughters.
149  nodeToSplit->filterEventsToDaughters();
150 
151  // Calculate the best splits for the new nodes.
152  left->calcOptimumSplit();
153  right->calcOptimumSplit();
154 
155  // See if the error reduces as we add more nodes.
156  calcError();
157 
158  if(numTerminalNodes % 1 == 0)
159  {
160  // std::cout << " " << numTerminalNodes << " Nodes : " << rmsError << std::endl;
161  }
162 
163  // Repeat until done.
164  if(numTerminalNodes < nodeLimit) buildTree(nodeLimit);
165 }
Node * getRightDaughter()
Definition: Node.cc:111
Node * getLeftDaughter()
Definition: Node.cc:101
std::list< Node * > terminalNodes
Definition: Tree.h:55
Int_t numTerminalNodes
Definition: Tree.h:56
void buildTree(Int_t nodeLimit)
Definition: Tree.cc:108
void calcError()
Definition: Tree.cc:92
Node * rootNode
Definition: Tree.h:54
void filterEventsToDaughters()
Definition: Node.cc:349
void calcOptimumSplit()
Definition: Node.cc:213
void theMiracleOfChildBirth()
Definition: Node.cc:334
void Tree::calcError ( )

Definition at line 92 of file Tree.cc.

References emtf::Node::getNumEvents(), rmsError, rootNode, mathSSE::sqrt(), and terminalNodes.

Referenced by buildTree().

93 {
94  // Loop through the separate predictive regions (terminal nodes) and
95  // add up the errors to get the error of the entire space.
96 
97  Double_t totalSquaredError = 0;
98 
99  for(std::list<Node*>::iterator it=terminalNodes.begin(); it!=terminalNodes.end(); it++)
100  {
101  totalSquaredError += (*it)->getTotalError();
102  }
103  rmsError = sqrt( totalSquaredError/rootNode->getNumEvents() );
104 }
std::list< Node * > terminalNodes
Definition: Tree.h:55
T sqrt(T t)
Definition: SSEVec.h:18
Int_t getNumEvents()
Definition: Node.cc:191
Double_t rmsError
Definition: Tree.h:57
Node * rootNode
Definition: Tree.h:54
Node * Tree::filterEvent ( Event e)

Definition at line 203 of file Tree.cc.

References filterEventRecursive(), and rootNode.

Referenced by L1TForest::appendCorrection().

204 {
205  // Use trees which have already been built to fit a bunch of events
206  // given by the tEvents vector.
207 
208  // Filter the event into a predictive region (terminal node).
209  Node* node = filterEventRecursive(rootNode, e);
210  return node;
211 }
Node * filterEventRecursive(Node *node, Event *e)
Definition: Tree.cc:215
Node * rootNode
Definition: Tree.h:54
Node * Tree::filterEventRecursive ( Node node,
Event e 
)

Definition at line 215 of file Tree.cc.

References emtf::Node::filterEventToDaughter().

Referenced by filterEvent().

216 {
217  // Filter the event repeatedly into the daughter nodes until it
218  // falls into a terminal node.
219 
220 
221  Node* nextNode = node->filterEventToDaughter(e);
222  if(nextNode == 0) return node;
223 
224  return filterEventRecursive(nextNode, e);
225 }
Node * filterEventRecursive(Node *node, Event *e)
Definition: Tree.cc:215
Node * filterEventToDaughter(emtf::Event *e)
Definition: Node.cc:394
void Tree::filterEvents ( std::vector< Event * > &  tEvents)

Definition at line 169 of file Tree.cc.

References filterEventsRecursive(), emtf::Node::getEvents(), and rootNode.

Referenced by L1TForest::appendCorrection().

170 {
171  // Use trees which have already been built to fit a bunch of events
172  // given by the tEvents vector.
173 
174  // Set the events to be filtered.
175  rootNode->getEvents() = std::vector< std::vector<Event*> >(1);
176  rootNode->getEvents()[0] = tEvents;
177 
178  // The tree now knows about the events it needs to fit.
179  // Filter them into a predictive region (terminal node).
181 }
std::vector< std::vector< emtf::Event * > > & getEvents()
Definition: Node.cc:198
void filterEventsRecursive(Node *node)
Definition: Tree.cc:185
Node * rootNode
Definition: Tree.h:54
void Tree::filterEventsRecursive ( Node node)

Definition at line 185 of file Tree.cc.

References emtf::Node::filterEventsToDaughters(), emtf::Node::getLeftDaughter(), and emtf::Node::getRightDaughter().

Referenced by filterEvents().

186 {
187  // Filter the events repeatedly into the daughter nodes until they
188  // fall into a terminal node.
189 
190  Node* left = node->getLeftDaughter();
191  Node* right = node->getRightDaughter();
192 
193  if(left == 0 || right == 0) return;
194 
195  node->filterEventsToDaughters();
196 
197  filterEventsRecursive(left);
198  filterEventsRecursive(right);
199 }
Node * getRightDaughter()
Definition: Node.cc:111
Node * getLeftDaughter()
Definition: Node.cc:101
void filterEventsRecursive(Node *node)
Definition: Tree.cc:185
void filterEventsToDaughters()
Definition: Node.cc:349
Int_t Tree::getNumTerminalNodes ( )

Definition at line 83 of file Tree.cc.

References numTerminalNodes.

84 {
85  return numTerminalNodes;
86 }
Int_t numTerminalNodes
Definition: Tree.h:56
Node * Tree::getRootNode ( )

Definition at line 64 of file Tree.cc.

References rootNode.

65 {
66  return rootNode;
67 }
Node * rootNode
Definition: Tree.h:54
void Tree::getSplitValues ( std::vector< std::vector< Double_t >> &  v)

Definition at line 301 of file Tree.cc.

References getSplitValuesRecursive(), rootNode, and findQualityFiles::v.

302 {
304 }
void getSplitValuesRecursive(Node *node, std::vector< std::vector< Double_t >> &v)
Definition: Tree.cc:271
Node * rootNode
Definition: Tree.h:54
void Tree::getSplitValuesRecursive ( Node node,
std::vector< std::vector< Double_t >> &  v 
)

Definition at line 271 of file Tree.cc.

References gather_cfg::cout, emtf::Node::getLeftDaughter(), emtf::Node::getRightDaughter(), emtf::Node::getSplitValue(), emtf::Node::getSplitVariable(), and findQualityFiles::v.

Referenced by getSplitValues().

272 {
273  // We recursively go through all of the nodes in the tree and find the
274  // split points used for each split variable.
275 
276  Node* left = node->getLeftDaughter();
277  Node* right = node->getRightDaughter();
278 
279  // Terminal nodes don't contribute.
280  if(left==0 || right==0) return;
281 
282  Int_t sv = node->getSplitVariable();
283  Double_t sp = node->getSplitValue();
284 
285  if(sv == -1)
286  {
287  std::cout << "ERROR: negative split variable for nonterminal node." << std::endl;
288  std::cout << "rankVarRecursive Split Variable = " << sv << std::endl;
289  }
290 
291  // Add the split point to the list for the correct split variable.
292  v[sv].push_back(sp);
293 
294  getSplitValuesRecursive(left, v);
295  getSplitValuesRecursive(right, v);
296 
297 }
Node * getRightDaughter()
Definition: Node.cc:111
Node * getLeftDaughter()
Definition: Node.cc:101
void getSplitValuesRecursive(Node *node, std::vector< std::vector< Double_t >> &v)
Definition: Tree.cc:271
Double_t getSplitValue()
Definition: Node.cc:135
Int_t getSplitVariable()
Definition: Node.cc:145
Definition: sp.h:21
std::list< Node * > & Tree::getTerminalNodes ( )

Definition at line 76 of file Tree.cc.

References terminalNodes.

Referenced by L1TForest::updateEvents(), and L1TForest::updateRegTargets().

77 {
78  return terminalNodes;
79 }
std::list< Node * > terminalNodes
Definition: Tree.h:55
void Tree::loadFromXML ( const char *  filename)

Definition at line 381 of file Tree.cc.

References loadFromXMLRecursive(), rootNode, and cmsPerfSuiteHarvest::xmldoc.

382 {
383  // First create the engine.
384  TXMLEngine* xml = new TXMLEngine;
385 
386  // Now try to parse xml file.
387  XMLDocPointer_t xmldoc = xml->ParseFile(filename);
388  if (xmldoc==0)
389  {
390  delete xml;
391  return;
392  }
393 
394  // Get access to main node of the xml file.
395  XMLNodePointer_t mainnode = xml->DocGetRootElement(xmldoc);
396 
397  // Recursively connect nodes together.
398  loadFromXMLRecursive(xml, mainnode, rootNode);
399 
400  // Release memory before exit
401  xml->FreeDoc(xmldoc);
402  delete xml;
403 }
void loadFromXMLRecursive(TXMLEngine *xml, XMLNodePointer_t node, Node *tnode)
Definition: Tree.cc:407
Node * rootNode
Definition: Tree.h:54
void Tree::loadFromXMLRecursive ( TXMLEngine *  xml,
XMLNodePointer_t  node,
Node tnode 
)

Definition at line 407 of file Tree.cc.

References emtf::Node::getLeftDaughter(), emtf::Node::getRightDaughter(), mps_fire::i, numTerminalNodes, emtf::Node::setFitValue(), emtf::Node::setSplitValue(), emtf::Node::setSplitVariable(), terminalNodes, and emtf::Node::theMiracleOfChildBirth().

Referenced by loadFromXML().

408 {
409 
410  // Get the split information from xml.
411  XMLAttrPointer_t attr = xml->GetFirstAttr(xnode);
412  std::vector<std::string> splitInfo(3);
413  for(unsigned int i=0; i<3; i++)
414  {
415  splitInfo[i] = xml->GetAttrValue(attr);
416  attr = xml->GetNextAttr(attr);
417  }
418 
419  // Convert strings into numbers.
420  std::stringstream converter;
421  Int_t splitVar;
422  Double_t splitVal;
423  Double_t fitVal;
424 
425  converter << splitInfo[0];
426  converter >> splitVar;
427  converter.str("");
428  converter.clear();
429 
430  converter << splitInfo[1];
431  converter >> splitVal;
432  converter.str("");
433  converter.clear();
434 
435  converter << splitInfo[2];
436  converter >> fitVal;
437  converter.str("");
438  converter.clear();
439 
440  // Store gathered splitInfo into the node object.
441  tnode->setSplitVariable(splitVar);
442  tnode->setSplitValue(splitVal);
443  tnode->setFitValue(fitVal);
444 
445  // Get the xml daughters of the current xml node.
446  XMLNodePointer_t xleft = xml->GetChild(xnode);
447  XMLNodePointer_t xright = xml->GetNext(xleft);
448 
449  // If there are no daughters we are done.
450  if(xleft == 0 || xright == 0) return;
451 
452  // If there are daughters link the node objects appropriately.
453  tnode->theMiracleOfChildBirth();
454  Node* tleft = tnode->getLeftDaughter();
455  Node* tright = tnode->getRightDaughter();
456 
457  // Update the list of terminal nodes.
458  terminalNodes.remove(tnode);
459  terminalNodes.push_back(tleft);
460  terminalNodes.push_back(tright);
462 
463  loadFromXMLRecursive(xml, xleft, tleft);
464  loadFromXMLRecursive(xml, xright, tright);
465 }
Node * getRightDaughter()
Definition: Node.cc:111
Node * getLeftDaughter()
Definition: Node.cc:101
std::list< Node * > terminalNodes
Definition: Tree.h:55
void loadFromXMLRecursive(TXMLEngine *xml, XMLNodePointer_t node, Node *tnode)
Definition: Tree.cc:407
void setFitValue(Double_t sFitValue)
Definition: Node.cc:152
void setSplitVariable(Int_t sSplitVar)
Definition: Node.cc:140
Int_t numTerminalNodes
Definition: Tree.h:56
void setSplitValue(Double_t sSplitValue)
Definition: Node.cc:130
void theMiracleOfChildBirth()
Definition: Node.cc:334
void Tree::rankVariables ( std::vector< Double_t > &  v)

Definition at line 263 of file Tree.cc.

References rankVariablesRecursive(), and rootNode.

264 {
266 }
Node * rootNode
Definition: Tree.h:54
void rankVariablesRecursive(Node *node, std::vector< Double_t > &v)
Definition: Tree.cc:230
void Tree::rankVariablesRecursive ( Node node,
std::vector< Double_t > &  v 
)

Definition at line 230 of file Tree.cc.

References emtf::Node::getErrorReduction(), emtf::Node::getLeftDaughter(), emtf::Node::getRightDaughter(), and emtf::Node::getSplitVariable().

Referenced by rankVariables().

231 {
232  // We recursively go through all of the nodes in the tree and find the
233  // total error reduction for each variable. The one with the most
234  // error reduction should be the most important.
235 
236  Node* left = node->getLeftDaughter();
237  Node* right = node->getRightDaughter();
238 
239  // Terminal nodes don't contribute to error reduction.
240  if(left==0 || right==0) return;
241 
242  Int_t sv = node->getSplitVariable();
243  Double_t er = node->getErrorReduction();
244 
245  //if(sv == -1)
246  //{
247  //std::cout << "ERROR: negative split variable for nonterminal node." << std::endl;
248  //std::cout << "rankVarRecursive Split Variable = " << sv << std::endl;
249  //std::cout << "rankVarRecursive Error Reduction = " << er << std::endl;
250  //}
251 
252  // Add error reduction to the current total for the appropriate
253  // variable.
254  v[sv] += er;
255 
256  rankVariablesRecursive(left, v);
257  rankVariablesRecursive(right, v);
258 
259 }
Node * getRightDaughter()
Definition: Node.cc:111
Node * getLeftDaughter()
Definition: Node.cc:101
Double_t getErrorReduction()
Definition: Node.cc:89
Int_t getSplitVariable()
Definition: Node.cc:145
void rankVariablesRecursive(Node *node, std::vector< Double_t > &v)
Definition: Tree.cc:230
void Tree::saveToXML ( const char *  filename)

Definition at line 333 of file Tree.cc.

References addXMLAttributes(), emtf::Node::getName(), rootNode, saveToXMLRecursive(), and cmsPerfSuiteHarvest::xmldoc.

Referenced by L1TForest::doRegression().

334 {
335 
336  TXMLEngine* xml = new TXMLEngine();
337 
338  // Add the root node.
339  XMLNodePointer_t root = xml->NewChild(0, 0, rootNode->getName().c_str());
340  addXMLAttributes(xml, rootNode, root);
341 
342  // Recursively write the tree to XML.
343  saveToXMLRecursive(xml, rootNode, root);
344 
345  // Make the XML Document.
346  XMLDocPointer_t xmldoc = xml->NewDoc();
347  xml->DocSetRootElement(xmldoc, root);
348 
349  // Save to file.
350  xml->SaveDoc(xmldoc, c);
351 
352  // Clean up.
353  xml->FreeDoc(xmldoc);
354  delete xml;
355 }
void addXMLAttributes(TXMLEngine *xml, Node *node, XMLNodePointer_t np)
Definition: Tree.cc:322
void saveToXMLRecursive(TXMLEngine *xml, Node *node, XMLNodePointer_t np)
Definition: Tree.cc:359
std::string getName()
Definition: Node.cc:77
Node * rootNode
Definition: Tree.h:54
void Tree::saveToXMLRecursive ( TXMLEngine *  xml,
Node node,
XMLNodePointer_t  np 
)

Definition at line 359 of file Tree.cc.

References addXMLAttributes(), emtf::Node::getLeftDaughter(), emtf::Node::getRightDaughter(), checklumidiff::l, and alignCSCRings::r.

Referenced by saveToXML().

360 {
361  Node* l = node->getLeftDaughter();
362  Node* r = node->getRightDaughter();
363 
364  if(l==0 || r==0) return;
365 
366  // Add children to the XMLEngine.
367  XMLNodePointer_t left = xml->NewChild(np, 0, "left");
368  XMLNodePointer_t right = xml->NewChild(np, 0, "right");
369 
370  // Add attributes to the children.
371  addXMLAttributes(xml, l, left);
372  addXMLAttributes(xml, r, right);
373 
374  // Recurse.
375  saveToXMLRecursive(xml, l, left);
376  saveToXMLRecursive(xml, r, right);
377 }
Node * getRightDaughter()
Definition: Node.cc:111
Node * getLeftDaughter()
Definition: Node.cc:101
void addXMLAttributes(TXMLEngine *xml, Node *node, XMLNodePointer_t np)
Definition: Tree.cc:322
int np
Definition: AMPTWrapper.h:33
void saveToXMLRecursive(TXMLEngine *xml, Node *node, XMLNodePointer_t np)
Definition: Tree.cc:359
void Tree::setRootNode ( Node sRootNode)

Definition at line 59 of file Tree.cc.

References rootNode.

60 {
61  rootNode = sRootNode;
62 }
Node * rootNode
Definition: Tree.h:54
void Tree::setTerminalNodes ( std::list< Node * > &  sTNodes)

Definition at line 71 of file Tree.cc.

References terminalNodes.

72 {
73  terminalNodes = sTNodes;
74 }
std::list< Node * > terminalNodes
Definition: Tree.h:55

Member Data Documentation

Int_t emtf::Tree::numTerminalNodes
private

Definition at line 56 of file Tree.h.

Referenced by buildTree(), getNumTerminalNodes(), loadFromXMLRecursive(), and Tree().

Double_t emtf::Tree::rmsError
private

Definition at line 57 of file Tree.h.

Referenced by calcError().

Node* emtf::Tree::rootNode
private
std::list<Node*> emtf::Tree::terminalNodes
private