@@ -45,37 +45,17 @@ Forest::Forest() { events = std::vector<std::vector<Event*>>(1); }
4545
4646Forest::Forest (std::vector<Event*>& trainingEvents) { setTrainingEvents (trainingEvents); }
4747
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-
6448Forest::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);
6751 });
6852}
6953
7054Forest& Forest::operator =(const Forest& forest) {
71- for (unsigned int i = 0 ; i < trees.size (); i++) {
72- if (trees[i])
73- delete trees[i];
74- }
7555 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);
7959 });
8060 return *this ;
8161}
@@ -113,7 +93,7 @@ std::vector<Event*> Forest::getTrainingEvents() { return events[0]; }
11393// return the ith tree
11494Tree* Forest::getTree (unsigned int i) {
11595 if (/* i>=0 && */ i < trees.size ())
116- return trees[i];
96+ return trees[i]. get () ;
11797 else {
11898 // std::cout << i << "is an invalid input for getTree. Out of range." << std::endl;
11999 return nullptr ;
@@ -124,7 +104,7 @@ Tree* Forest::getTree(unsigned int i) {
124104// ______________________Various_Helpful_Functions______________________//
125105// ////////////////////////////////////////////////////////////////////////
126106
127- unsigned int Forest::size () {
107+ unsigned int Forest::size () const {
128108 // Return the number of trees in the forest.
129109 return trees.size ();
130110}
@@ -139,7 +119,7 @@ unsigned int Forest::size() {
139119// ----------------------------------------------------------------------
140120// ////////////////////////////////////////////////////////////////////////
141121
142- void Forest::listEvents (std::vector<std::vector<Event*>>& e) {
122+ void Forest::listEvents (std::vector<std::vector<Event*>>& e) const {
143123 // Simply list the events in each event vector. We have multiple copies
144124 // of the events vector. Each copy is sorted according to a different
145125 // determining variable.
@@ -178,7 +158,7 @@ bool compareEventsById(Event* e1, Event* e2) {
178158// ----------------------------------------------------------------------
179159// ////////////////////////////////////////////////////////////////////////
180160
181- void Forest::sortEventVectors (std::vector<std::vector<Event*>>& e) {
161+ void Forest::sortEventVectors (std::vector<std::vector<Event*>>& e) const {
182162 // When a node chooses the optimum split point and split variable it needs
183163 // the events to be sorted according to the variable it is considering.
184164
@@ -192,7 +172,7 @@ void Forest::sortEventVectors(std::vector<std::vector<Event*>>& e) {
192172// ----------------------------------------------------------------------
193173// ////////////////////////////////////////////////////////////////////////
194174
195- void Forest::rankVariables (std::vector<int >& rank) {
175+ void Forest::rankVariables (std::vector<int >& rank) const {
196176 // This function ranks the determining variables according to their importance
197177 // in determining the fit. Use a low learning rate for better results.
198178 // Separates completely useless variables from useful ones well,
@@ -242,7 +222,7 @@ void Forest::rankVariables(std::vector<int>& rank) {
242222// ----------------------------------------------------------------------
243223// ////////////////////////////////////////////////////////////////////////
244224
245- void Forest::saveSplitValues (const char * savefilename) {
225+ void Forest::saveSplitValues (const char * savefilename) const {
246226 // This function gathers all of the split values from the forest and puts them into lists.
247227
248228 std::ofstream splitvaluefile;
@@ -377,8 +357,8 @@ void Forest::doRegression(int nodeLimit,
377357
378358 for (unsigned int i = 0 ; i < (unsigned )treeLimit; i++) {
379359 // 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 ( );
382362 tree->buildTree (nodeLimit);
383363
384364 // Update the targets for the next tree to fit.
@@ -426,7 +406,7 @@ void Forest::predictEvents(std::vector<Event*>& eventsp, unsigned int numtrees)
426406void Forest::appendCorrection (std::vector<Event*>& eventsp, int treenum) {
427407 // Update the prediction by appending the next correction.
428408
429- Tree* tree = trees[treenum];
409+ Tree* tree = trees[treenum]. get () ;
430410 tree->filterEvents (eventsp);
431411
432412 // Update the events with their new prediction.
@@ -463,7 +443,7 @@ void Forest::predictEvent(Event* e, unsigned int numtrees) {
463443void Forest::appendCorrection (Event* e, int treenum) {
464444 // Update the prediction by appending the next correction.
465445
466- Tree* tree = trees[treenum];
446+ Tree* tree = trees[treenum]. get () ;
467447 Node* terminalNode = tree->filterEvent (e);
468448
469449 // Update the event with its new prediction.
@@ -478,12 +458,13 @@ void Forest::loadForestFromXML(const char* directory, unsigned int numTrees) {
478458 // Load a forest that has already been created and stored into XML somewhere.
479459
480460 // Initialize the vector of trees.
481- trees = std::vector<Tree*>(numTrees);
461+ trees.resize (numTrees);
462+ trees.shrink_to_fit ();
482463
483464 // Load the Forest.
484465 // std::cout << std::endl << "Loading Forest from XML ... " << std::endl;
485466 for (unsigned int i = 0 ; i < numTrees; i++) {
486- trees[i] = new Tree ();
467+ trees[i] = std::make_unique< Tree> ();
487468
488469 std::stringstream ss;
489470 ss << directory << " /" << i << " .xml" ;
@@ -499,17 +480,12 @@ void Forest::loadFromCondPayload(const L1TMuonEndCapForest::DForest& forest) {
499480 // Initialize the vector of trees.
500481 unsigned int numTrees = forest.size ();
501482
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 ();
509485
510486 // Load the Forest.
511487 for (unsigned int i = 0 ; i < numTrees; i++) {
512- trees[i] = new Tree ();
488+ trees[i] = std::make_unique< Tree> ();
513489 trees[i]->loadFromCondPayload (forest[i]);
514490 }
515491}
@@ -556,7 +532,8 @@ void Forest::doStochasticRegression(
556532
557533 // Prepare some things.
558534 sortEventVectors (events);
559- trees = std::vector<Tree*>(treeLimit);
535+ trees.resize (treeLimit);
536+ trees.shrink_to_fit ();
560537
561538 // See how long the regression takes.
562539 TStopwatch timer;
@@ -572,15 +549,15 @@ void Forest::doStochasticRegression(
572549 for (unsigned int i = 0 ; i < (unsigned )treeLimit; i++) {
573550 // Build the tree using a random subsample.
574551 prepareRandomSubsample (fraction);
575- trees[i] = new Tree (subSample);
552+ trees[i] = std::make_unique< Tree> (subSample);
576553 trees[i]->buildTree (nodeLimit);
577554
578555 // Fit all of the events based upon the tree we built using
579556 // the subsample of events.
580557 trees[i]->filterEvents (events[0 ]);
581558
582559 // Update the targets for the next tree to fit.
583- updateRegTargets (trees[i], learningRate, l);
560+ updateRegTargets (trees[i]. get () , learningRate, l);
584561
585562 // Save trees to xml in some directory.
586563 std::ostringstream ss;
0 commit comments