Skip to content

Commit cbcd15b

Browse files
committed
Merge pull request #158 from adavidzh/limit-boundary-checks
Bug fix for #138.
2 parents bcd353f + 5062fa5 commit cbcd15b

File tree

5 files changed

+55
-22
lines changed

5 files changed

+55
-22
lines changed

interface/CloseCoutSentry.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ class CloseCoutSentry {
1313
// break through any sentry, even the ones above myself (for critical error messages, or debug)
1414
static void breakFree() ;
1515
FILE *trueStdOut();
16+
static FILE *trueStdOutGlobal();
1617
private:
1718
bool silent_;
1819
static int fdOut_, fdErr_;
1920
static bool open_;
2021
// always clear, even if I was not the one closing it
2122
void static reallyClear() ;
2223
static FILE *trueStdOut_;
24+
static CloseCoutSentry *owner_;
2325
bool stdOutIsMine_;
2426
};
2527

interface/utils.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <vector>
55
#include <string>
6+
#include <unordered_map>
67
struct RooDataHist;
78
struct RooAbsData;
89
struct RooAbsPdf;
@@ -93,8 +94,8 @@ namespace utils {
9394
// Set range of physics model parameters
9495
void setModelParameterRanges( const std::string & setPhysicsModelParameterRangeExpression, const RooArgSet & params);
9596

96-
bool checkParameterBoundary( const RooRealVar &);
97-
bool checkParameterBoundaries( const RooArgSet &);
97+
bool isParameterAtBoundary( const RooRealVar &);
98+
bool anyParameterAtBoundaries( const RooArgSet &, int verbosity);
9899

99100
void reorderCombinations(std::vector<std::vector<int> > &, const std::vector<int> &, const std::vector<int> &);
100101
std::vector<std::vector<int> > generateCombinations(const std::vector<int> &vec);

src/CascadeMinimizer.cc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,10 +278,11 @@ bool CascadeMinimizer::minimize(int verbose, bool cascade)
278278
nllParams->remove(CascadeMinimizerGlobalConfigs::O().pdfCategories);
279279
nllParams->remove(CascadeMinimizerGlobalConfigs::O().parametersOfInterest);
280280

281-
bool boundariesOk = utils::checkParameterBoundaries(*nllParams);
282-
if(!boundariesOk){
283-
std::cout << " [WARNING] After the fit some parameters are at their boundary (see above)." << std::endl;
284-
std::cout << " [WARNING] Are you sure your model is correct?" << std::endl;
281+
bool boundariesNotOk = utils::anyParameterAtBoundaries(*nllParams, verbose);
282+
if(boundariesNotOk && verbose >= 1){
283+
fprintf(CloseCoutSentry::trueStdOutGlobal(),
284+
" [WARNING] After the fit some parameters are at their boundary.\n"
285+
" [WARNING] Are you sure your model is correct?\n");
285286
}
286287

287288
return ret;

src/CloseCoutSentry.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ bool CloseCoutSentry::open_ = true;
77
int CloseCoutSentry::fdOut_ = 0;
88
int CloseCoutSentry::fdErr_ = 0;
99
FILE * CloseCoutSentry::trueStdOut_ = 0;
10+
CloseCoutSentry *CloseCoutSentry::owner_ = 0;
11+
1012

1113
CloseCoutSentry::CloseCoutSentry(bool silent) :
1214
silent_(silent), stdOutIsMine_(false)
@@ -20,6 +22,7 @@ CloseCoutSentry::CloseCoutSentry(bool silent) :
2022
}
2123
freopen("/dev/null", "w", stdout);
2224
freopen("/dev/null", "w", stderr);
25+
owner_ = this;
2326
} else {
2427
silent_ = false;
2528
}
@@ -47,6 +50,7 @@ void CloseCoutSentry::reallyClear()
4750
sprintf(buf, "/dev/fd/%d", fdOut_); freopen(buf, "w", stdout);
4851
sprintf(buf, "/dev/fd/%d", fdErr_); freopen(buf, "w", stderr);
4952
open_ = true;
53+
owner_ = 0;
5054
fdOut_ = fdErr_ = 0;
5155
}
5256
}
@@ -56,6 +60,12 @@ void CloseCoutSentry::breakFree()
5660
reallyClear();
5761
}
5862

63+
FILE *CloseCoutSentry::trueStdOutGlobal()
64+
{
65+
if (!owner_) return stdout;
66+
return owner_->trueStdOut();
67+
}
68+
5969
FILE *CloseCoutSentry::trueStdOut()
6070
{
6171
if (open_) return stdout;

src/utils.cc

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <sstream>
77
#include <cmath>
88
#include <vector>
9+
#include <unordered_map>
910
#include <string>
1011
#include <memory>
1112
#include <typeinfo>
@@ -414,9 +415,11 @@ void utils::copyAttributes(const RooAbsArg &from, RooAbsArg &to) {
414415
if (!attribs.empty()) {
415416
for (std::set<std::string>::const_iterator it = attribs.begin(), ed = attribs.end(); it != ed; ++it) to.setAttribute(it->c_str());
416417
}
417-
const std::map<std::string, std::string> strattribs = from.stringAttributes();
418+
const std::map
419+
<std::string, std::string> strattribs = from.stringAttributes();
418420
if (!strattribs.empty()) {
419-
for (std::map<std::string,std::string>::const_iterator it = strattribs.begin(), ed = strattribs.end(); it != ed; ++it) to.setStringAttribute(it->first.c_str(), it->second.c_str());
421+
for (std::map
422+
<std::string,std::string>::const_iterator it = strattribs.begin(), ed = strattribs.end(); it != ed; ++it) to.setStringAttribute(it->first.c_str(), it->second.c_str());
420423
}
421424
}
422425

@@ -695,7 +698,8 @@ std::vector<std::vector<int> > utils::generateCombinations(const std::vector<int
695698
}
696699

697700

698-
bool utils::checkParameterBoundary( const RooRealVar &param ){
701+
bool utils::isParameterAtBoundary( const RooRealVar &param ){
702+
699703
double vMin = param.getMin();
700704
double vMax = param.getMax();
701705
double val = param.getVal();
@@ -708,25 +712,40 @@ bool utils::checkParameterBoundary( const RooRealVar &param ){
708712
float nSigma=1.0;
709713

710714
if(pullMin < nSigma || pullMax < nSigma){
711-
CloseCoutSentry::breakFree();
712-
std::cout << " [WARNING] Found "<<param.GetName()<< " at " << std::min(pullMin,pullMax) << " sigma of one of its boundaries:" << std::endl;
713-
std::cout << " "; param.Print();
714-
return false;
715+
return true;
715716
}
716-
717-
return true;
717+
718+
return false;
718719
}
719720

720-
bool utils::checkParameterBoundaries( const RooArgSet &params ){
721-
722-
bool isNoneBad = true;
721+
722+
bool utils::anyParameterAtBoundaries( const RooArgSet &params, int verbosity ){
723+
724+
static std::unordered_map<std::string, unsigned char> timesFoundAtBoundary;
725+
bool isAnyBad = false;
723726

724727
RooLinkedListIter iter = params.iterator(); int i = 0;
725728
for (RooRealVar *a = (RooRealVar *) iter.Next(); a != 0; a = (RooRealVar *) iter.Next(), ++i) {
726-
bool isBad = checkParameterBoundary(*a);
727-
isNoneBad &= isBad;
729+
730+
bool isBad = isParameterAtBoundary(*a);
731+
732+
if(isBad){
733+
std::string varName((*a).GetName());
734+
735+
if( verbosity >= 9 || (timesFoundAtBoundary[varName] < 3 && verbosity > -1) ){
736+
fprintf(CloseCoutSentry::trueStdOutGlobal()," [WARNING] Found [%s] at boundary. \n", (*a).GetName());
737+
std::cout << " "; (*a).Print();
738+
}
739+
740+
timesFoundAtBoundary[varName]++;
741+
}
742+
743+
isAnyBad |= isBad;
728744
}
729745

730-
return isNoneBad;
746+
// for( std::unordered_map<std::string, unsigned char>::value_type e : timesFoundAtBoundary ){
747+
// printf("e %s -> %i\n", e.first.c_str(), e.second);
748+
// }
749+
750+
return isAnyBad;
731751
}
732-

0 commit comments

Comments
 (0)