test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ForestHelper.cc
Go to the documentation of this file.
3 
4 #include <iostream>
5 #include <cassert>
6 
7 using namespace l1t;
8 using namespace std;
9 
11  return new ForestHelper(es);
12 }
13 
15  ForestHelper * x = new ForestHelper(es);
16  x->useCopy();
17  return x;
18 }
19 
21  write_ = w;
22  check_write();
23  we_own_write_ = false;
24  //write_->m_version = VERSION;
25  read_ = write_;
26 }
27 
28 ForestHelper::ForestHelper(const L1TMuonEndCapForest * es) {read_ = es; write_=NULL;}
29 
31  write_ = new L1TMuonEndCapForest(*read_);
32  we_own_write_ = true;
33  read_ = write_;
34 }
35 
37  if (we_own_write_ && write_) delete write_;
38 }
39 
40 
41 // print all the L1 GT stable parameters
42 void ForestHelper::print(std::ostream& myStr) const {
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 }
69 
70 
71 
72 void ForestHelper::initializeFromXML(const char * dirname, const std::vector<int> & modes, int ntrees){
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];
85  write_->forest_map_[mode]=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 }
127 
128 double ForestHelper::evaluate(int mode, const std::vector<double> & data) const {
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 }
143 
144 
145 
146 
147 void ForestHelper::loadTreeFromXMLRecursive(TXMLEngine* xml, XMLNodePointer_t xnode, DTree & tree, unsigned 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 }
209 
210 double ForestHelper::evalTreeRecursive(const std::vector<double> & data, const DTree & tree, int index) const{
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 }
int i
Definition: DBlmapReader.cc:9
void loadTreeFromXMLRecursive(TXMLEngine *xml, XMLNodePointer_t xnode, DTree &tree, unsigned index)
static ForestHelper * readAndWriteFromEventSetup(const L1TMuonEndCapForest *es)
Definition: ForestHelper.cc:14
const double w
Definition: UKUtility.cc:23
assert(m_qm.get())
#define NULL
Definition: scimark2.h:8
void initializeFromXML(const char *dirname, const std::vector< int > &modes, int ntrees)
Definition: ForestHelper.cc:72
string xmldoc
Some module&#39;s global variables.
T x() const
Cartesian x coordinate.
L1TMuonEndCapForest::DTree DTree
Definition: ForestHelper.h:44
ForestHelper(L1TMuonEndCapForest *w)
Definition: ForestHelper.cc:20
int j
Definition: DBlmapReader.cc:9
void print(std::ostream &) const
Definition: ForestHelper.cc:42
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
double evaluate(int mode, const std::vector< double > &data) const
tuple filename
Definition: lut2db_cfg.py:20
tuple cout
Definition: gather_cfg.py:145
static const ForestHelper * readFromEventSetup(const L1TMuonEndCapForest *es)
Definition: ForestHelper.cc:10