@@ -45,37 +45,17 @@ Forest::Forest() { events = std::vector<std::vector<Event*>>(1); }
45
45
46
46
Forest::Forest (std::vector<Event*>& trainingEvents) { setTrainingEvents (trainingEvents); }
47
47
48
- // ///////////////////////////////////////////////////////////////////////
49
- // _______________________Destructor____________________________________//
50
- // ////////////////////////////////////////////////////////////////////////
51
-
52
- Forest::~Forest () {
53
- // When the forest is destroyed it will delete the trees as well as the
54
- // events from the training and testing sets.
55
- // The user may want the events to remain after they destroy the forest
56
- // this should be changed in future upgrades.
57
-
58
- for (unsigned int i = 0 ; i < trees.size (); i++) {
59
- if (trees[i])
60
- delete trees[i];
61
- }
62
- }
63
-
64
48
Forest::Forest (const Forest& forest) {
65
- transform (forest.trees .cbegin (), forest.trees .cend (), back_inserter (trees), [](const Tree* tree) {
66
- return new Tree (*tree);
49
+ transform (forest.trees .cbegin (), forest.trees .cend (), back_inserter (trees), [](const std::unique_ptr< Tree>& tree) {
50
+ return std::make_unique< Tree> (*tree);
67
51
});
68
52
}
69
53
70
54
Forest& Forest::operator =(const Forest& forest) {
71
- for (unsigned int i = 0 ; i < trees.size (); i++) {
72
- if (trees[i])
73
- delete trees[i];
74
- }
75
55
trees.resize (0 );
76
-
77
- transform (forest.trees .cbegin (), forest.trees .cend (), back_inserter (trees), [](const Tree* tree) {
78
- return new Tree (*tree);
56
+ trees. reserve (forest. trees . size ());
57
+ transform (forest.trees .cbegin (), forest.trees .cend (), back_inserter (trees), [](const std::unique_ptr< Tree>& tree) {
58
+ return std::make_unique< Tree> (*tree);
79
59
});
80
60
return *this ;
81
61
}
@@ -113,7 +93,7 @@ std::vector<Event*> Forest::getTrainingEvents() { return events[0]; }
113
93
// return the ith tree
114
94
Tree* Forest::getTree (unsigned int i) {
115
95
if (/* i>=0 && */ i < trees.size ())
116
- return trees[i];
96
+ return trees[i]. get () ;
117
97
else {
118
98
// std::cout << i << "is an invalid input for getTree. Out of range." << std::endl;
119
99
return nullptr ;
@@ -124,7 +104,7 @@ Tree* Forest::getTree(unsigned int i) {
124
104
// ______________________Various_Helpful_Functions______________________//
125
105
// ////////////////////////////////////////////////////////////////////////
126
106
127
- unsigned int Forest::size () {
107
+ unsigned int Forest::size () const {
128
108
// Return the number of trees in the forest.
129
109
return trees.size ();
130
110
}
@@ -139,7 +119,7 @@ unsigned int Forest::size() {
139
119
// ----------------------------------------------------------------------
140
120
// ////////////////////////////////////////////////////////////////////////
141
121
142
- void Forest::listEvents (std::vector<std::vector<Event*>>& e) {
122
+ void Forest::listEvents (std::vector<std::vector<Event*>>& e) const {
143
123
// Simply list the events in each event vector. We have multiple copies
144
124
// of the events vector. Each copy is sorted according to a different
145
125
// determining variable.
@@ -178,7 +158,7 @@ bool compareEventsById(Event* e1, Event* e2) {
178
158
// ----------------------------------------------------------------------
179
159
// ////////////////////////////////////////////////////////////////////////
180
160
181
- void Forest::sortEventVectors (std::vector<std::vector<Event*>>& e) {
161
+ void Forest::sortEventVectors (std::vector<std::vector<Event*>>& e) const {
182
162
// When a node chooses the optimum split point and split variable it needs
183
163
// the events to be sorted according to the variable it is considering.
184
164
@@ -192,7 +172,7 @@ void Forest::sortEventVectors(std::vector<std::vector<Event*>>& e) {
192
172
// ----------------------------------------------------------------------
193
173
// ////////////////////////////////////////////////////////////////////////
194
174
195
- void Forest::rankVariables (std::vector<int >& rank) {
175
+ void Forest::rankVariables (std::vector<int >& rank) const {
196
176
// This function ranks the determining variables according to their importance
197
177
// in determining the fit. Use a low learning rate for better results.
198
178
// Separates completely useless variables from useful ones well,
@@ -242,7 +222,7 @@ void Forest::rankVariables(std::vector<int>& rank) {
242
222
// ----------------------------------------------------------------------
243
223
// ////////////////////////////////////////////////////////////////////////
244
224
245
- void Forest::saveSplitValues (const char * savefilename) {
225
+ void Forest::saveSplitValues (const char * savefilename) const {
246
226
// This function gathers all of the split values from the forest and puts them into lists.
247
227
248
228
std::ofstream splitvaluefile;
@@ -377,8 +357,8 @@ void Forest::doRegression(int nodeLimit,
377
357
378
358
for (unsigned int i = 0 ; i < (unsigned )treeLimit; i++) {
379
359
// std::cout << "++Building Tree " << i << "... " << std::endl;
380
- Tree* tree = new Tree (events);
381
- trees.push_back (tree );
360
+ trees. emplace_back (std::make_unique< Tree> (events) );
361
+ Tree* tree = trees.back (). get ( );
382
362
tree->buildTree (nodeLimit);
383
363
384
364
// Update the targets for the next tree to fit.
@@ -426,7 +406,7 @@ void Forest::predictEvents(std::vector<Event*>& eventsp, unsigned int numtrees)
426
406
void Forest::appendCorrection (std::vector<Event*>& eventsp, int treenum) {
427
407
// Update the prediction by appending the next correction.
428
408
429
- Tree* tree = trees[treenum];
409
+ Tree* tree = trees[treenum]. get () ;
430
410
tree->filterEvents (eventsp);
431
411
432
412
// Update the events with their new prediction.
@@ -463,7 +443,7 @@ void Forest::predictEvent(Event* e, unsigned int numtrees) {
463
443
void Forest::appendCorrection (Event* e, int treenum) {
464
444
// Update the prediction by appending the next correction.
465
445
466
- Tree* tree = trees[treenum];
446
+ Tree* tree = trees[treenum]. get () ;
467
447
Node* terminalNode = tree->filterEvent (e);
468
448
469
449
// Update the event with its new prediction.
@@ -478,12 +458,13 @@ void Forest::loadForestFromXML(const char* directory, unsigned int numTrees) {
478
458
// Load a forest that has already been created and stored into XML somewhere.
479
459
480
460
// Initialize the vector of trees.
481
- trees = std::vector<Tree*>(numTrees);
461
+ trees.resize (numTrees);
462
+ trees.shrink_to_fit ();
482
463
483
464
// Load the Forest.
484
465
// std::cout << std::endl << "Loading Forest from XML ... " << std::endl;
485
466
for (unsigned int i = 0 ; i < numTrees; i++) {
486
- trees[i] = new Tree ();
467
+ trees[i] = std::make_unique< Tree> ();
487
468
488
469
std::stringstream ss;
489
470
ss << directory << " /" << i << " .xml" ;
@@ -499,17 +480,12 @@ void Forest::loadFromCondPayload(const L1TMuonEndCapForest::DForest& forest) {
499
480
// Initialize the vector of trees.
500
481
unsigned int numTrees = forest.size ();
501
482
502
- // clean-up leftovers from previous initialization (if any)
503
- for (unsigned int i = 0 ; i < trees.size (); i++) {
504
- if (trees[i])
505
- delete trees[i];
506
- }
507
-
508
- trees = std::vector<Tree*>(numTrees);
483
+ trees.resize (numTrees);
484
+ trees.shrink_to_fit ();
509
485
510
486
// Load the Forest.
511
487
for (unsigned int i = 0 ; i < numTrees; i++) {
512
- trees[i] = new Tree ();
488
+ trees[i] = std::make_unique< Tree> ();
513
489
trees[i]->loadFromCondPayload (forest[i]);
514
490
}
515
491
}
@@ -556,7 +532,8 @@ void Forest::doStochasticRegression(
556
532
557
533
// Prepare some things.
558
534
sortEventVectors (events);
559
- trees = std::vector<Tree*>(treeLimit);
535
+ trees.resize (treeLimit);
536
+ trees.shrink_to_fit ();
560
537
561
538
// See how long the regression takes.
562
539
TStopwatch timer;
@@ -572,15 +549,15 @@ void Forest::doStochasticRegression(
572
549
for (unsigned int i = 0 ; i < (unsigned )treeLimit; i++) {
573
550
// Build the tree using a random subsample.
574
551
prepareRandomSubsample (fraction);
575
- trees[i] = new Tree (subSample);
552
+ trees[i] = std::make_unique< Tree> (subSample);
576
553
trees[i]->buildTree (nodeLimit);
577
554
578
555
// Fit all of the events based upon the tree we built using
579
556
// the subsample of events.
580
557
trees[i]->filterEvents (events[0 ]);
581
558
582
559
// Update the targets for the next tree to fit.
583
- updateRegTargets (trees[i], learningRate, l);
560
+ updateRegTargets (trees[i]. get () , learningRate, l);
584
561
585
562
// Save trees to xml in some directory.
586
563
std::ostringstream ss;
0 commit comments