Skip to content

Commit

Permalink
add regex for floatNuisances
Browse files Browse the repository at this point in the history
  • Loading branch information
rkansal47 committed Jul 19, 2023
1 parent f641692 commit 73256a5
Showing 1 changed file with 79 additions and 2 deletions.
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

0 comments on commit 73256a5

Please sign in to comment.