Skip to content

Commit 9db0110

Browse files
committed
[RF] Some code modernization of RooFit result
* smart pointers for transient members * more `std::string` instead of TString
1 parent 9e421f6 commit 9db0110

File tree

2 files changed

+23
-37
lines changed

2 files changed

+23
-37
lines changed

roofit/roofitcore/inc/RooFitResult.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@
3333
class RooArgSet ;
3434
class RooAbsPdf ;
3535
class RooPlot;
36-
class TObject ;
3736
class TH2 ;
38-
typedef RooArgSet* pRooArgSet ;
3937

4038
class RooFitResult : public TNamed, public RooPrintable, public RooDirItem {
4139
public:
@@ -185,11 +183,11 @@ class RooFitResult : public TNamed, public RooPrintable, public RooDirItem {
185183
RooArgList* _initPars = nullptr; ///< List of floating parameters with initial values
186184
RooArgList* _finalPars = nullptr; ///< List of floating parameters with final values
187185

188-
mutable RooArgList* _globalCorr = nullptr; ///<! List of global correlation coefficients
186+
mutable std::unique_ptr<RooArgList> _globalCorr; ///<! List of global correlation coefficients
189187
mutable TList _corrMatrix ; ///<! Correlation matrix (list of RooArgLists)
190188

191-
mutable RooArgList *_randomPars = nullptr; ///<! List of floating parameters with most recent random perturbation applied
192-
mutable TMatrixF* _Lt = nullptr; ///<! triangular matrix used for generate random perturbations
189+
mutable std::unique_ptr<RooArgList> _randomPars; ///<! List of floating parameters with most recent random perturbation applied
190+
mutable std::unique_ptr<TMatrixF> _Lt; ///<! triangular matrix used for generate random perturbations
193191

194192
TMatrixDSym* _CM = nullptr; ///< Correlation matrix
195193
TMatrixDSym* _VM = nullptr; ///< Covariance matrix

roofit/roofitcore/src/RooFitResult.cxx

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,10 @@ RooFitResult::RooFitResult(const RooFitResult &other)
9292

9393
other._finalPars->snapshot(*_finalPars);
9494
if (other._randomPars) {
95-
_randomPars = new RooArgList;
95+
_randomPars = std::make_unique<RooArgList>();
9696
other._randomPars->snapshot(*_randomPars);
9797
}
98-
if (other._Lt) _Lt = new TMatrix(*other._Lt);
98+
if (other._Lt) _Lt = std::make_unique<TMatrix>(*other._Lt);
9999
if (other._VM) _VM = new TMatrixDSym(*other._VM) ;
100100
if (other._CM) _CM = new TMatrixDSym(*other._CM) ;
101101
if (other._GC) _GC = new TVectorD(*other._GC) ;
@@ -114,9 +114,6 @@ RooFitResult::~RooFitResult()
114114
if (_constPars) delete _constPars ;
115115
if (_initPars) delete _initPars ;
116116
if (_finalPars) delete _finalPars ;
117-
if (_globalCorr) delete _globalCorr;
118-
if (_randomPars) delete _randomPars;
119-
if (_Lt) delete _Lt;
120117
if (_CM) delete _CM ;
121118
if (_VM) delete _VM ;
122119
if (_GC) delete _GC ;
@@ -334,10 +331,10 @@ RooPlot *RooFitResult::plotOn(RooPlot *frame, const char *parName1, const char *
334331
const RooArgList& RooFitResult::randomizePars() const
335332
{
336333
Int_t nPar= _finalPars->size();
337-
if(nullptr == _randomPars) { // first-time initialization
334+
if(!_randomPars) { // first-time initialization
338335
assert(nullptr != _finalPars);
339336
// create the list of random values to fill
340-
_randomPars = new RooArgList;
337+
_randomPars = std::make_unique<RooArgList>();
341338
_finalPars->snapshot(*_randomPars);
342339
// calculate the elements of the upper-triangular matrix L that gives Lt*L = C
343340
// where Lt is the transpose of L (the "square-root method")
@@ -360,7 +357,7 @@ const RooArgList& RooFitResult::randomizePars() const
360357
}
361358
}
362359
// remember Lt
363-
_Lt= new TMatrix(TMatrix::kTransposed,L);
360+
_Lt= std::make_unique<TMatrix>(TMatrix::kTransposed,L);
364361
}
365362
else {
366363
// reset to the final fit values
@@ -455,7 +452,7 @@ const RooArgList* RooFitResult::globalCorr()
455452
fillLegacyCorrMatrix() ;
456453
}
457454

458-
return _globalCorr ;
455+
return _globalCorr.get();
459456
}
460457

461458

@@ -622,39 +619,28 @@ void RooFitResult::fillLegacyCorrMatrix() const
622619
if (!_CM) return ;
623620

624621
// Delete eventual previous correlation data holders
625-
if (_globalCorr) delete _globalCorr ;
626622
_corrMatrix.Delete();
627623

628624
// Build holding arrays for correlation coefficients
629-
_globalCorr = new RooArgList("globalCorrelations") ;
625+
_globalCorr = std::make_unique<RooArgList>("globalCorrelations") ;
630626

631627
for(RooAbsArg* arg : *_initPars) {
632628
// Create global correlation value holder
633-
TString gcName("GC[") ;
634-
gcName.Append(arg->GetName()) ;
635-
gcName.Append("]") ;
636-
TString gcTitle(arg->GetTitle()) ;
637-
gcTitle.Append(" Global Correlation") ;
638-
_globalCorr->addOwned(std::make_unique<RooRealVar>(gcName.Data(),gcTitle.Data(),0.));
629+
std::string argName = arg->GetName();
630+
std::string argTitle = arg->GetTitle();
631+
std::string gcName = "GC[" + argName + "]";
632+
std::string gcTitle = argTitle + " Global Correlation";
633+
_globalCorr->addOwned(std::make_unique<RooRealVar>(gcName.c_str(),gcTitle.c_str(),0.));
639634

640635
// Create array with correlation holders for this parameter
641-
TString name("C[") ;
642-
name.Append(arg->GetName()) ;
643-
name.Append(",*]") ;
644-
RooArgList* corrMatrixRow = new RooArgList(name.Data()) ;
636+
RooArgList* corrMatrixRow = new RooArgList(("C[" + argName + ",*]").c_str()) ;
645637
_corrMatrix.Add(corrMatrixRow) ;
646638
for(RooAbsArg* arg2 : *_initPars) {
647639

648-
TString cName("C[") ;
649-
cName.Append(arg->GetName()) ;
650-
cName.Append(",") ;
651-
cName.Append(arg2->GetName()) ;
652-
cName.Append("]") ;
653-
TString cTitle("Correlation between ") ;
654-
cTitle.Append(arg->GetName()) ;
655-
cTitle.Append(" and ") ;
656-
cTitle.Append(arg2->GetName()) ;
657-
corrMatrixRow->addOwned(std::make_unique<RooRealVar>(cName.Data(),cTitle.Data(),0.));
640+
std::string arg2Name = arg2->GetName();
641+
std::string cName = "C[" + argName + "," + arg2Name + "]";
642+
std::string cTitle = "Correlation between " + argName + " and " + arg2Name;
643+
corrMatrixRow->addOwned(std::make_unique<RooRealVar>(cName.c_str(),cTitle.c_str(),0.));
658644
}
659645
}
660646

@@ -1315,7 +1301,9 @@ void RooFitResult::Streamer(TBuffer &R__b)
13151301
R__b >> _constPars;
13161302
R__b >> _initPars;
13171303
R__b >> _finalPars;
1318-
R__b >> _globalCorr;
1304+
RooArgList* globalCorr;
1305+
R__b >> globalCorr;
1306+
_globalCorr.reset(globalCorr);
13191307
_corrMatrix.Streamer(R__b);
13201308
R__b.CheckByteCount(R__s, R__c, RooFitResult::IsA());
13211309

0 commit comments

Comments
 (0)