Skip to content

Commit 3eb6a75

Browse files
authored
Merge pull request #638 from kpedro88/track_errors
add option to track parameter errors
2 parents b3a4c38 + ba9ac51 commit 3eb6a75

File tree

3 files changed

+60
-39
lines changed

3 files changed

+60
-39
lines changed

docs/part3/runningthetool.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ There are a number of useful command line options which can be used to alter the
6767
- the name of the branch will be **trackedParam_*name***.
6868
- the exact behaviour depends on the method. For example, when using `MultiDimFit` with the `--algo scan`, the value of the parameter at each point in the scan will be saved while for `FitDiagnostics`, only the value at the end of the method will be saved.
6969

70+
- `--trackErrors name1[,name2,...]` will add a branch to the output tree for the error of each of the named parameters. This option supports the use of regexp via by replacing `name` with `rgx{some regular expression}`
71+
72+
- the name of the branch will be **trackedError_*name***.
73+
- the behaviour is the same as `--trackParameters` above.
74+
7075
#### Generic Minimizer Options
7176

7277
Combine uses its own minimizer class which is used to steer Minuit (via RooMinimizer) named the `CascadeMinimizer`. This allows for sequential minimization which can help in case a particular setting/algo fails. Also, the `CascadeMinimizer` knows about extra features of Combine such as *discrete* nuisance parameters.

interface/Combine.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <boost/filesystem.hpp>
77
#include "RooArgSet.h"
88
#include "RooAbsReal.h"
9+
#include "RooRealVar.h"
910
#include <boost/algorithm/string/split.hpp>
1011
#include <boost/algorithm/string/classification.hpp>
1112

@@ -73,6 +74,8 @@ class Combine {
7374
void addNuisances(const RooArgSet *);
7475
void addFloatingParameters(const RooArgSet &);
7576
void addPOI(const RooArgSet *);
77+
template <class Var>
78+
void addBranches(const std::string&, RooWorkspace*, std::vector<std::pair<Var*,float>>&, const std::string&);
7679

7780
boost::program_options::options_description statOptions_, ioOptions_, miscOptions_;
7881

@@ -119,7 +122,9 @@ class Combine {
119122
static TTree *tree_;
120123

121124
static std::vector<std::pair<RooAbsReal*,float> > trackedParametersMap_;
125+
static std::vector<std::pair<RooRealVar*,float> > trackedErrorsMap_;
122126
static std::string trackParametersNameString_;
127+
static std::string trackErrorsNameString_;
123128
static std::string textToWorkspaceString_;
124129

125130
};

src/Combine.cc

Lines changed: 50 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <algorithm>
1414
#include <unistd.h>
1515
#include <errno.h>
16+
#include <sstream>
1617

1718
#include <TCanvas.h>
1819
#include <TFile.h>
@@ -92,9 +93,11 @@ std::string setPhysicsModelParameterRangeExpression_ = "";
9293
std::string defineBackgroundOnlyModelParameterExpression_ = "";
9394

9495
std::string Combine::trackParametersNameString_="";
96+
std::string Combine::trackErrorsNameString_="";
9597
std::string Combine::textToWorkspaceString_="";
9698

9799
std::vector<std::pair<RooAbsReal*,float> > Combine::trackedParametersMap_;
100+
std::vector<std::pair<RooRealVar*,float> > Combine::trackedErrorsMap_;
98101

99102
Combine::Combine() :
100103
statOptions_("Common statistics options"),
@@ -155,6 +158,7 @@ Combine::Combine() :
155158
("genUnbinnedChannels", po::value<std::string>(&genAsUnbinned_)->default_value(genAsUnbinned_), "Flag the given channels to be generated unbinned (irrespectively of how they were flagged at workspace creation)")
156159
("text2workspace", boost::program_options::value<std::string>(&textToWorkspaceString_)->default_value(""), "Pass along options to text2workspace (default = none)")
157160
("trackParameters", boost::program_options::value<std::string>(&trackParametersNameString_)->default_value(""), "Keep track of parameters in workspace, also accepts regexp with syntax 'rgx{<my regexp>}' (default = none)")
161+
("trackErrors", boost::program_options::value<std::string>(&trackErrorsNameString_)->default_value(""), "Keep track of errors on parameters in workspace, also accepts regexp with syntax 'rgx{<my regexp>}' (default = none)")
158162
;
159163
}
160164

@@ -812,44 +816,10 @@ void Combine::run(TString hlfFile, const std::string &dataset, double &limit, do
812816

813817
tree_ = tree;
814818

815-
// Set up additional branches
816-
if(trackParametersNameString_!=""){
817-
char tmp[10240] ;
818-
strlcpy(tmp,trackParametersNameString_.c_str(),10240) ;
819-
char* token = strtok(tmp,",") ;
820-
while(token) {
821-
if (boost::starts_with(token, "rgx{") && boost::ends_with(token, "}")) {
822-
std::string tokenstr(token);
823-
std::string reg_esp = tokenstr.substr(4, tokenstr.size()-5);
824-
std::cout<<"interpreting "<<reg_esp<<" as regex "<<std::endl;
825-
std::regex rgx( reg_esp, std::regex::ECMAScript);
819+
// Set up additional branches
820+
addBranches(trackParametersNameString_,w,trackedParametersMap_,"Param");
821+
addBranches(trackErrorsNameString_,w,trackedErrorsMap_,"Error");
826822

827-
RooArgSet allParams(w->allVars());
828-
std::auto_ptr<TIterator> iter(allParams.createIterator());
829-
for (RooAbsArg *a = (RooAbsArg*) iter->Next(); a != 0; a = (RooAbsArg*) iter->Next()) {
830-
RooAbsReal *tmp = dynamic_cast<RooAbsReal *>(a);
831-
const std::string &target = tmp->GetName();
832-
std::smatch match;
833-
if (std::regex_match(target, match, rgx)) {
834-
if (tmp->isConstant()) continue;
835-
Combine::trackedParametersMap_.push_back(std::pair<RooAbsReal*,float>(tmp,tmp->getVal()));
836-
}
837-
}
838-
token = strtok(0,",") ;
839-
} else {
840-
RooAbsReal *a =(RooAbsReal*)w->obj(token);
841-
if (a == 0) throw std::invalid_argument(std::string("Parameter ")+(token)+" not in model.");
842-
Combine::trackedParametersMap_.push_back(std::pair<RooAbsReal*,float>(a,a->getVal()));
843-
token = strtok(0,",") ;
844-
}
845-
846-
}
847-
}
848-
849-
for (std::vector<std::pair<RooAbsReal*,float> >::iterator it = Combine::trackedParametersMap_.begin(); it!=Combine::trackedParametersMap_.end();it++){
850-
const char * token = (it->first)->GetName();
851-
addBranch((std::string("trackedParam_")+token).c_str(), &(it->second), (std::string("trackedParam_")+token+std::string("/F")).c_str());
852-
}
853823
// Should have the PDF at this point, if not something is really odd?
854824
if (!(mc->GetPdf())){
855825
std::cerr << " FATAL ERROR! PDF not found in ModelConfig. \n Try to build the workspace first with text2workspace.py and run with the binary output." << std::endl;
@@ -1190,8 +1160,11 @@ void Combine::commitPoint(bool expected, float quantile) {
11901160
Float_t saveQuantile = g_quantileExpected_;
11911161
g_quantileExpected_ = quantile;
11921162

1193-
for (std::vector<std::pair<RooAbsReal*,float> >::iterator it = Combine::trackedParametersMap_.begin(); it!=Combine::trackedParametersMap_.end();it++){
1194-
it->second = (it->first)->getVal();
1163+
for (auto& it : trackedParametersMap_){
1164+
it.second = (it.first)->getVal();
1165+
}
1166+
for (auto& it : trackedErrorsMap_){
1167+
it.second = (it.first)->getError();
11951168
}
11961169

11971170
if (g_fillTree_) tree_->Fill();
@@ -1283,3 +1256,41 @@ void Combine::addDiscreteNuisances(RooWorkspace *w){
12831256
}
12841257
}
12851258
}
1259+
1260+
template <class Var>
1261+
void Combine::addBranches(const std::string& trackString, RooWorkspace* w, std::vector<std::pair<Var*,float>>& trackMap, const std::string& vtype) {
1262+
if(trackString!=""){
1263+
std::stringstream ss(trackString);
1264+
std::string token;
1265+
while(std::getline(ss,token,',')) {
1266+
if (boost::starts_with(token, "rgx{") && boost::ends_with(token, "}")) {
1267+
std::string reg_esp = token.substr(4, token.size()-5);
1268+
std::cout<<"interpreting "<<reg_esp<<" as regex "<<std::endl;
1269+
std::regex rgx( reg_esp, std::regex::ECMAScript);
1270+
1271+
RooArgSet allParams(w->allVars());
1272+
std::auto_ptr<TIterator> iter(allParams.createIterator());
1273+
for (RooAbsArg *a = (RooAbsArg*) iter->Next(); a != 0; a = (RooAbsArg*) iter->Next()) {
1274+
Var *tmp = dynamic_cast<Var *>(a);
1275+
if(tmp==nullptr) continue;
1276+
const std::string &target = tmp->GetName();
1277+
std::smatch match;
1278+
if (std::regex_match(target, match, rgx)) {
1279+
if (tmp->isConstant()) continue;
1280+
trackMap.emplace_back(tmp,0.f);
1281+
}
1282+
}
1283+
} else {
1284+
Var *a =(Var*)w->obj(token.c_str());
1285+
if (a == 0) throw std::invalid_argument(vtype+" "+(token)+" not in model.");
1286+
trackMap.emplace_back(a,0.f);
1287+
}
1288+
1289+
}
1290+
}
1291+
1292+
for (auto& it : trackMap){
1293+
const char * token = (it.first)->GetName();
1294+
addBranch((std::string("tracked")+vtype+"_"+token).c_str(), &(it.second), (std::string("tracked")+vtype+"_"+token+std::string("/F")).c_str());
1295+
}
1296+
}

0 commit comments

Comments
 (0)