|
13 | 13 | #include <algorithm> |
14 | 14 | #include <unistd.h> |
15 | 15 | #include <errno.h> |
| 16 | +#include <sstream> |
16 | 17 |
|
17 | 18 | #include <TCanvas.h> |
18 | 19 | #include <TFile.h> |
@@ -92,9 +93,11 @@ std::string setPhysicsModelParameterRangeExpression_ = ""; |
92 | 93 | std::string defineBackgroundOnlyModelParameterExpression_ = ""; |
93 | 94 |
|
94 | 95 | std::string Combine::trackParametersNameString_=""; |
| 96 | +std::string Combine::trackErrorsNameString_=""; |
95 | 97 | std::string Combine::textToWorkspaceString_=""; |
96 | 98 |
|
97 | 99 | std::vector<std::pair<RooAbsReal*,float> > Combine::trackedParametersMap_; |
| 100 | +std::vector<std::pair<RooRealVar*,float> > Combine::trackedErrorsMap_; |
98 | 101 |
|
99 | 102 | Combine::Combine() : |
100 | 103 | statOptions_("Common statistics options"), |
@@ -155,6 +158,7 @@ Combine::Combine() : |
155 | 158 | ("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)") |
156 | 159 | ("text2workspace", boost::program_options::value<std::string>(&textToWorkspaceString_)->default_value(""), "Pass along options to text2workspace (default = none)") |
157 | 160 | ("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)") |
158 | 162 | ; |
159 | 163 | } |
160 | 164 |
|
@@ -812,44 +816,10 @@ void Combine::run(TString hlfFile, const std::string &dataset, double &limit, do |
812 | 816 |
|
813 | 817 | tree_ = tree; |
814 | 818 |
|
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"); |
826 | 822 |
|
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 | | - } |
853 | 823 | // Should have the PDF at this point, if not something is really odd? |
854 | 824 | if (!(mc->GetPdf())){ |
855 | 825 | 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) { |
1190 | 1160 | Float_t saveQuantile = g_quantileExpected_; |
1191 | 1161 | g_quantileExpected_ = quantile; |
1192 | 1162 |
|
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(); |
1195 | 1168 | } |
1196 | 1169 |
|
1197 | 1170 | if (g_fillTree_) tree_->Fill(); |
@@ -1283,3 +1256,41 @@ void Combine::addDiscreteNuisances(RooWorkspace *w){ |
1283 | 1256 | } |
1284 | 1257 | } |
1285 | 1258 | } |
| 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