Skip to content

Commit feeb5f3

Browse files
svFitHistogramAdapter: make histogram names unique to avoid memory conflicts/leaks
1 parent 8141eff commit feeb5f3

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

BuildFile.xml

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<use name="TauAnalysis/SVfitTF"/>
2+
<use name="boost"/>
23
<use name="root"/>
34
<use name="roofit"/>
45
<Flags CPPDEFINES="USE_SVFITTF"/>

interface/svFitHistogramAdapter.h

+4
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ namespace classic_svFit
5050

5151
bool isValidSolution() const;
5252

53+
private:
54+
static int nInstances;
55+
std::string uniqueName;
56+
5357
mutable TH1* histogram_ = nullptr;
5458
};
5559

src/svFitHistogramAdapter.cc

+14-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
#include <TMath.h>
44
#include <TFile.h>
5+
#include <TObject.h>
56

67
#include <numeric>
78

9+
#include <boost/algorithm/string/replace.hpp>
10+
811
using namespace classic_svFit;
912

1013
TH1* HistogramTools::compHistogramDensity(TH1 const* histogram)
@@ -113,7 +116,10 @@ TH1* HistogramTools::makeHistogram(const std::string& histogramName, double xMin
113116
return histogram;
114117
}
115118

116-
SVfitQuantity::SVfitQuantity()
119+
int SVfitQuantity::nInstances = 0;
120+
121+
SVfitQuantity::SVfitQuantity() :
122+
uniqueName("_SVfitQuantity_"+std::to_string(++SVfitQuantity::nInstances))
117123
{
118124
}
119125

@@ -126,11 +132,17 @@ void SVfitQuantity::bookHistogram(const LorentzVector& vis1P4, const LorentzVect
126132
{
127133
if (histogram_ != nullptr) delete histogram_;
128134
histogram_ = createHistogram(vis1P4, vis2P4, met);
135+
histogram_->SetName((histogram_->GetName()+uniqueName).c_str());
129136
}
130137

131138
void SVfitQuantity::writeHistogram() const
132139
{
133-
if (histogram_ != nullptr) histogram_->Write();
140+
if (histogram_ != nullptr)
141+
{
142+
std::string histogramName = histogram_->GetName();
143+
boost::replace_all(histogramName, uniqueName, "");
144+
histogram_->Write(histogramName.c_str(), TObject::kWriteDelete);
145+
}
134146
}
135147

136148
void SVfitQuantity::fillHistogram(

0 commit comments

Comments
 (0)