Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add regex for floatNuisances #851

Merged
merged 5 commits into from
Oct 10, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 79 additions & 2 deletions src/Combine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -589,14 +589,91 @@ void Combine::run(TString hlfFile, const std::string &dataset, double &limit, do
}

if (floatNuisances_ != "") {
RooArgSet toFloat((floatNuisances_=="all")?*nuisances:(w->argSet(floatNuisances_.c_str())));
// expand regexps
while (floatNuisances_.find("rgx{") != std::string::npos) {
size_t pos1 = floatNuisances_.find("rgx{");
size_t pos2 = floatNuisances_.find("}",pos1);
std::string prestr = floatNuisances_.substr(0,pos1);
std::string poststr = floatNuisances_.substr(pos2+1,floatNuisances_.size()-pos2);
std::string reg_esp = floatNuisances_.substr(pos1+4,pos2-pos1-4);

//std::cout<<"interpreting "<<reg_esp<<" as regex "<<std::endl;
std::regex rgx( reg_esp, std::regex::ECMAScript);

std::string matchingParams="";
std::unique_ptr<TIterator> iter(nuisances->createIterator());
for (RooAbsArg *a = (RooAbsArg*) iter->Next(); a != 0; a = (RooAbsArg*) iter->Next()) {
const std::string &target = a->GetName();
std::smatch match;
if (std::regex_match(target, match, rgx)) {
matchingParams = matchingParams + target + ",";
}
}

floatNuisances_ = prestr+matchingParams+poststr;
floatNuisances_ = boost::replace_all_copy(floatNuisances_, ",,", ",");

}

// expand regexps
while (floatNuisances_.find("var{") != std::string::npos) {
size_t pos1 = floatNuisances_.find("var{");
size_t pos2 = floatNuisances_.find("}",pos1);
std::string prestr = floatNuisances_.substr(0,pos1);
std::string poststr = floatNuisances_.substr(pos2+1,floatNuisances_.size()-pos2);
std::string reg_esp = floatNuisances_.substr(pos1+4,pos2-pos1-4);

// std::cout<<"interpreting "<<reg_esp<<" as regex "<<std::endl;
std::regex rgx( reg_esp, std::regex::ECMAScript);

std::string matchingParams="";
std::unique_ptr<TIterator> iter(w->componentIterator());
for (RooAbsArg *a = (RooAbsArg*) iter->Next(); a != 0; a = (RooAbsArg*) iter->Next()) {

if ( ! (a->IsA()->InheritsFrom(RooRealVar::Class()) || a->IsA()->InheritsFrom(RooCategory::Class()))) continue;

const std::string &target = a->GetName();
// std::cout<<"var "<<target<<std::endl;
std::smatch match;
if (std::regex_match(target, match, rgx)) {
matchingParams = matchingParams + target + ",";
}
}

floatNuisances_ = prestr+matchingParams+poststr;
floatNuisances_ = boost::replace_all_copy(floatNuisances_, ",,", ",");

}

//RooArgSet toFloat((floatNuisances_=="all")?*nuisances:(w->argSet(floatNuisances_.c_str())));
RooArgSet toFloat;
if (floatNuisances_=="all") {
toFloat.add(*nuisances);
} else {
std::vector<std::string> nuisToFloat;
boost::split(nuisToFloat, floatNuisances_, boost::is_any_of(","), boost::token_compress_on);
for (int k=0; k<(int)nuisToFloat.size(); k++) {
if (nuisToFloat[k]=="") continue;
else if(nuisToFloat[k]=="all") {
toFloat.add(*nuisances);
continue;
}
else if (!w->fundArg(nuisToFloat[k].c_str())) {
std::cout<<"WARNING: cannot float nuisance parameter "<<nuisToFloat[k].c_str()<<" if it doesn't exist!"<<std::endl;
continue;
}
const RooAbsArg *arg = (RooAbsArg*)w->fundArg(nuisToFloat[k].c_str());
toFloat.add(*arg);
}
}

if (verbose > 0) {
std::cout << "Set floating the following parameters: "; toFloat.Print("");
Logger::instance().log(std::string(Form("Combine.cc: %d -- Set floating the following parameters: ",__LINE__)),Logger::kLogLevelInfo,__func__);
std::unique_ptr<TIterator> iter(toFloat.createIterator());
for (RooAbsArg *a = (RooAbsArg*) iter->Next(); a != 0; a = (RooAbsArg*) iter->Next()) {
Logger::instance().log(std::string(Form("Combine.cc: %d %s ",__LINE__,a->GetName())),Logger::kLogLevelInfo,__func__);
}
}
}
utils::setAllConstant(toFloat, false);
}
Expand Down