CMS 3D CMS Logo

List of all members | Public Types | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes
l1t::ForestHelper Class Reference

#include <ForestHelper.h>

Public Types

enum  { VERSION = 1 }
 
typedef L1TMuonEndCapForest::DForest DForest
 
typedef L1TMuonEndCapForest::DForestColl DForestColl
 
typedef L1TMuonEndCapForest::DForestMap DForestMap
 
typedef L1TMuonEndCapForest::DTree DTree
 
typedef L1TMuonEndCapForest::DTreeNode DTreeNode
 

Public Member Functions

double evaluate (int mode, const std::vector< double > &data) const
 
 ForestHelper (L1TMuonEndCapForest *w)
 
const L1TMuonEndCapForestgetReadInstance () const
 
L1TMuonEndCapForestgetWriteInstance ()
 
void initializeFromXML (const char *dirname, const std::vector< int > &modes, int ntrees)
 
void print (std::ostream &) const
 
 ~ForestHelper ()
 

Static Public Member Functions

static ForestHelperreadAndWriteFromEventSetup (const L1TMuonEndCapForest *es)
 
static const ForestHelperreadFromEventSetup (const L1TMuonEndCapForest *es)
 

Private Member Functions

void check_write ()
 
double evalTreeRecursive (const std::vector< double > &data, const DTree &tree, int index) const
 
 ForestHelper (const L1TMuonEndCapForest *es)
 
void loadTreeFromXMLRecursive (TXMLEngine *xml, XMLNodePointer_t xnode, DTree &tree, unsigned index)
 
void useCopy ()
 

Private Attributes

const L1TMuonEndCapForestread_
 
bool we_own_write_
 
L1TMuonEndCapForestwrite_
 

Detailed Description

Definition at line 39 of file ForestHelper.h.

Member Typedef Documentation

Definition at line 45 of file ForestHelper.h.

Definition at line 46 of file ForestHelper.h.

Definition at line 47 of file ForestHelper.h.

Definition at line 44 of file ForestHelper.h.

Definition at line 43 of file ForestHelper.h.

Member Enumeration Documentation

anonymous enum
Enumerator
VERSION 

Definition at line 41 of file ForestHelper.h.

Constructor & Destructor Documentation

ForestHelper::~ForestHelper ( )

Definition at line 36 of file ForestHelper.cc.

36  {
37  if (we_own_write_ && write_) delete write_;
38 }
L1TMuonEndCapForest * write_
Definition: ForestHelper.h:75
ForestHelper::ForestHelper ( L1TMuonEndCapForest w)

Definition at line 20 of file ForestHelper.cc.

References w.

Referenced by getWriteInstance().

20  {
21  write_ = w;
22  check_write();
23  we_own_write_ = false;
24  //write_->m_version = VERSION;
25  read_ = write_;
26 }
const double w
Definition: UKUtility.cc:23
const L1TMuonEndCapForest * read_
Definition: ForestHelper.h:74
L1TMuonEndCapForest * write_
Definition: ForestHelper.h:75
ForestHelper::ForestHelper ( const L1TMuonEndCapForest es)
private

Definition at line 28 of file ForestHelper.cc.

References NULL.

28 {read_ = es; write_=NULL;}
#define NULL
Definition: scimark2.h:8
const L1TMuonEndCapForest * read_
Definition: ForestHelper.h:74
L1TMuonEndCapForest * write_
Definition: ForestHelper.h:75

Member Function Documentation

void l1t::ForestHelper::check_write ( )
inlineprivate

Definition at line 72 of file ForestHelper.h.

References write_.

72 { assert(write_); }
L1TMuonEndCapForest * write_
Definition: ForestHelper.h:75
double ForestHelper::evalTreeRecursive ( const std::vector< double > &  data,
const DTree tree,
int  index 
) const
private

Definition at line 210 of file ForestHelper.cc.

References L1TMuonEndCapForest::DTreeNode::fitVal, L1TMuonEndCapForest::DTreeNode::ileft, diffTreeTool::index, L1TMuonEndCapForest::DTreeNode::iright, L1TMuonEndCapForest::DTreeNode::splitVal, and L1TMuonEndCapForest::DTreeNode::splitVar.

210  {
211  const DTreeNode & node = tree[index];
212  if ((node.ileft == 0) && (node.iright==0)){
213  //cout << "terminal node: fitVal: " << node.fitVal << "\n";
214  return node.fitVal;
215  }
216  assert(data.size() > (unsigned) node.splitVar);
217  // by convention, nodes are either not terminal or fully terminal
218  assert(node.ileft > 0);
219  assert(node.iright > 0);
220 
221  //cout << "NODE: svar: " << node.splitVar << " sval: " << node.splitVal << " data: " << data[node.splitVar] << "fit: " << node.fitVal << "\n";
222 
223  if (data[node.splitVar] < node.splitVal){
224  //cout << "going left to " << node.ileft << "\n";
225  return evalTreeRecursive(data, tree, node.ileft);
226  } else {
227  //cout << "going right to " << node.iright << "\n";
228  return evalTreeRecursive(data, tree, node.iright);
229  }
230 }
L1TMuonEndCapForest::DTreeNode DTreeNode
Definition: ForestHelper.h:43
double evalTreeRecursive(const std::vector< double > &data, const DTree &tree, int index) const
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
Definition: tree.py:1
double ForestHelper::evaluate ( int  mode,
const std::vector< double > &  data 
) const

Definition at line 128 of file ForestHelper.cc.

References x().

128  {
129  auto it = read_->forest_map_.find(mode);
130  if (it == read_->forest_map_.end())
131  return 0;
132 
133  const DForest & dforest = read_->forest_coll_[it->second];
134 
135  double sum = 0;
136  for (auto itree = dforest.begin(); itree != dforest.end(); itree++){
137  double x = evalTreeRecursive(data, *itree, 0);
138  //cout << "forest eval to " << x << "\n";
139  sum += x;
140  }
141  return sum;
142 }
const L1TMuonEndCapForest * read_
Definition: ForestHelper.h:74
L1TMuonEndCapForest::DForest DForest
Definition: ForestHelper.h:45
double evalTreeRecursive(const std::vector< double > &data, const DTree &tree, int index) const
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
const L1TMuonEndCapForest* l1t::ForestHelper::getReadInstance ( ) const
inline

Definition at line 66 of file ForestHelper.h.

References read_.

66 {return read_;}
const L1TMuonEndCapForest * read_
Definition: ForestHelper.h:74
L1TMuonEndCapForest* l1t::ForestHelper::getWriteInstance ( )
inline

Definition at line 67 of file ForestHelper.h.

References ForestHelper(), useCopy(), and write_.

Referenced by L1TMuonEndCapForestESProducer::produce().

67 {return write_; }
L1TMuonEndCapForest * write_
Definition: ForestHelper.h:75
void ForestHelper::initializeFromXML ( const char *  dirname,
const std::vector< int > &  modes,
int  ntrees 
)

Definition at line 72 of file ForestHelper.cc.

References createBeamHaloJobs::directory, corrVsCorr::filename, mps_fire::i, createfilelist::int, ALCARECOPromptCalibProdSiPixelAli0T_cff::mode, AlCaHLTBitMon_QueryRunRegistry::string, and cmsPerfSuiteHarvest::xmldoc.

Referenced by L1TMuonEndCapForestESProducer::L1TMuonEndCapForestESProducer().

72  {
73 
74  //cout << "DEBUG: starting initializeFromXML...\n";
75 
76  assert(write_->forest_coll_.size() == 0);
77 
78  for(int i =0; i < (int) modes.size(); i++){
79  int mode = modes[i];
80  //cout << "DEBUG: initializing Decision Forest for mode=" << mode << "\n";
81 
82  //DForest tmp;
83  write_->forest_coll_.push_back(DForest());
84  DForest & dforest = write_->forest_coll_[i];
86 
87  std::stringstream ss;
88  ss << dirname << "/" << mode;
90  ss >> directory;
91 
92  for(int j=0; j < ntrees; j++){
93  std::stringstream ss;
94  ss << directory << "/" << j << ".xml";
96  ss >> filename;
97  //cout << "DEBUG: loading tree " << filename << "\n";
98 
99 
100  dforest.push_back(DTree());
101  DTree & dtree = dforest[j];
102  dtree.push_back(DTreeNode());
103 
104 
105  // First create the engine.
106  TXMLEngine* xml = new TXMLEngine();
107 
108  // Now try to parse xml file.
109  XMLDocPointer_t xmldoc = xml->ParseFile(edm::FileInPath(filename.c_str()).fullPath().c_str());
110  if (xmldoc==0){
111  delete xml;
112  continue;
113  }
114  // Get access to main node of the xml file.
115  XMLNodePointer_t mainnode = xml->DocGetRootElement(xmldoc);
116 
117  loadTreeFromXMLRecursive(xml, mainnode, dtree, 0);
118 
119  //cout << "DEBUG: parsed tree of size " << dtree.size() << " for mode " << mode << "\n";
120 
121  xml->FreeDoc(xmldoc);
122  delete xml;
123 
124  }
125  }
126 }
L1TMuonEndCapForest::DTreeNode DTreeNode
Definition: ForestHelper.h:43
void loadTreeFromXMLRecursive(TXMLEngine *xml, XMLNodePointer_t xnode, DTree &tree, unsigned index)
L1TMuonEndCapForest::DTree DTree
Definition: ForestHelper.h:44
L1TMuonEndCapForest::DForest DForest
Definition: ForestHelper.h:45
L1TMuonEndCapForest * write_
Definition: ForestHelper.h:75
void ForestHelper::loadTreeFromXMLRecursive ( TXMLEngine *  xml,
XMLNodePointer_t  xnode,
DTree tree,
unsigned  index 
)
private

Definition at line 147 of file ForestHelper.cc.

References mps_fire::i, and diffTreeTool::index.

148 {
149  assert(tree.size() > index);
150  //cout << "DEBUG: recursive call at index " << index <<"\n";
151 
152  // Get the split information from xml.
153  XMLAttrPointer_t attr = xml->GetFirstAttr(xnode);
154  std::vector<std::string> splitInfo(3);
155  for(unsigned int i=0; i<3; i++)
156  {
157  splitInfo[i] = xml->GetAttrValue(attr);
158  attr = xml->GetNextAttr(attr);
159  }
160 
161  // Convert strings into numbers.
162  std::stringstream converter;
163  Int_t splitVar;
164  Double_t splitVal;
165  Double_t fitVal;
166 
167  converter << splitInfo[0];
168  converter >> splitVar;
169  converter.str("");
170  converter.clear();
171 
172  converter << splitInfo[1];
173  converter >> splitVal;
174  converter.str("");
175  converter.clear();
176 
177  converter << splitInfo[2];
178  converter >> fitVal;
179  converter.str("");
180  converter.clear();
181 
182  //cout << "fitval: " << fitVal << "\n";
183 
184  // Store gathered splitInfo into the node object.
185  tree[index].splitVar = splitVar;
186  tree[index].splitVal = splitVal;
187  tree[index].fitVal = fitVal;
188 
189  // Get the xml daughters of the current xml node.
190  XMLNodePointer_t xleft = xml->GetChild(xnode);
191  XMLNodePointer_t xright = xml->GetNext(xleft);
192 
193  assert( ((xleft!=0)&&(xright!=0)) || ((xleft==0)&&(xright==0)) );
194 
195  // This seems potentially problematic, but leaving for now until
196  // bitwise equivalence is demonstrated...
197  if(xleft == 0 || xright == 0) return;
198 
199  // append two more nodes at end of tree and update this indices in this node:
200  tree[index].ileft = tree.size();
201  tree[index].iright = tree[index].ileft + 1;
202  tree.push_back(DTreeNode());
203  tree.push_back(DTreeNode());
204 
205  // recursively handle the next two nodes:
206  loadTreeFromXMLRecursive(xml, xleft, tree, tree[index].ileft);
207  loadTreeFromXMLRecursive(xml, xright, tree, tree[index].iright);
208 }
L1TMuonEndCapForest::DTreeNode DTreeNode
Definition: ForestHelper.h:43
void loadTreeFromXMLRecursive(TXMLEngine *xml, XMLNodePointer_t xnode, DTree &tree, unsigned index)
Definition: tree.py:1
void ForestHelper::print ( std::ostream &  myStr) const

Definition at line 42 of file ForestHelper.cc.

References KineDebug3::count(), gather_cfg::cout, L1TMuonEndCapForest::DTreeNode::fitVal, L1TMuonEndCapForest::DTreeNode::ileft, diffTreeTool::index, L1TMuonEndCapForest::DTreeNode::iright, ALCARECOPromptCalibProdSiPixelAli0T_cff::mode, L1TMuonEndCapForest::DTreeNode::splitVal, and L1TMuonEndCapForest::DTreeNode::splitVar.

42  {
43  myStr << "\nL1T EndCap Parameters \n" << std::endl;
44 
45 
46  for (int mode=0; mode<16; mode++){
47  if (mode != 15) continue;
48 
49  auto it = read_->forest_map_.find(mode);
50  if (it == read_->forest_map_.end())
51  continue;
52 
53  const DForest & dforest = read_->forest_coll_[it->second];
54 
55  int count = 0;
56  for (auto itree = dforest.begin(); itree != dforest.end(); itree++){
57  const DTree & tree = *itree;
58  cout << "DUMP: ***** Tree " << count << " with size " << tree.size() << "\n";
59  for (unsigned index=0; index<tree.size(); index++){
60  const DTreeNode & node = tree[index];
61  cout << "node " << index << " l: " << node.ileft << " r: " << node.iright << "svar: " << node.splitVar << " sval: " << node.splitVal << " fit: " << node.fitVal << "\n";
62  }
63 
64 
65  count++;
66  }
67  }
68 }
L1TMuonEndCapForest::DTreeNode DTreeNode
Definition: ForestHelper.h:43
L1TMuonEndCapForest::DTree DTree
Definition: ForestHelper.h:44
const L1TMuonEndCapForest * read_
Definition: ForestHelper.h:74
L1TMuonEndCapForest::DForest DForest
Definition: ForestHelper.h:45
Definition: tree.py:1
ForestHelper * ForestHelper::readAndWriteFromEventSetup ( const L1TMuonEndCapForest es)
static

Definition at line 14 of file ForestHelper.cc.

References useCopy(), and x().

14  {
15  ForestHelper * x = new ForestHelper(es);
16  x->useCopy();
17  return x;
18 }
ForestHelper(L1TMuonEndCapForest *w)
Definition: ForestHelper.cc:20
const ForestHelper * ForestHelper::readFromEventSetup ( const L1TMuonEndCapForest es)
static

Definition at line 10 of file ForestHelper.cc.

10  {
11  return new ForestHelper(es);
12 }
ForestHelper(L1TMuonEndCapForest *w)
Definition: ForestHelper.cc:20
void ForestHelper::useCopy ( )
private

Definition at line 30 of file ForestHelper.cc.

Referenced by getWriteInstance(), and readAndWriteFromEventSetup().

30  {
32  we_own_write_ = true;
33  read_ = write_;
34 }
const L1TMuonEndCapForest * read_
Definition: ForestHelper.h:74
L1TMuonEndCapForest * write_
Definition: ForestHelper.h:75

Member Data Documentation

const L1TMuonEndCapForest* l1t::ForestHelper::read_
private

Definition at line 74 of file ForestHelper.h.

Referenced by getReadInstance().

bool l1t::ForestHelper::we_own_write_
private

Definition at line 76 of file ForestHelper.h.

L1TMuonEndCapForest* l1t::ForestHelper::write_
private

Definition at line 75 of file ForestHelper.h.

Referenced by check_write(), and getWriteInstance().