File indexing completed on 2025-01-18 10:10:27
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 #ifndef RooFit_ModelConfig_h
0018 #define RooFit_ModelConfig_h
0019
0020 #include <RooAbsData.h>
0021 #include <RooAbsPdf.h>
0022 #include <RooArgSet.h>
0023 #include <RooGlobalFunc.h>
0024 #include <RooWorkspaceHandle.h>
0025
0026 #include <TRef.h>
0027
0028 #include <string>
0029
0030 class RooFitResult;
0031
0032
0033 namespace RooStats {
0034
0035 class ModelConfig final : public TNamed, public RooWorkspaceHandle {
0036
0037 public:
0038 ModelConfig(RooWorkspace *ws = nullptr)
0039 {
0040 if (ws)
0041 SetWS(*ws);
0042 }
0043
0044 ModelConfig(const char *name, RooWorkspace *ws = nullptr) : TNamed(name, name)
0045 {
0046 if (ws)
0047 SetWS(*ws);
0048 }
0049
0050 ModelConfig(const char *name, const char *title, RooWorkspace *ws = nullptr) : TNamed(name, title)
0051 {
0052 if (ws)
0053 SetWS(*ws);
0054 }
0055
0056
0057 ModelConfig *Clone(const char *name = "") const override
0058 {
0059 ModelConfig *mc = new ModelConfig(*this);
0060 if (strcmp(name, "") == 0) {
0061 mc->SetName(this->GetName());
0062 } else {
0063 mc->SetName(name);
0064 }
0065 return mc;
0066 }
0067
0068
0069 void SetWS(RooWorkspace &ws) override;
0070
0071 virtual void SetWorkspace(RooWorkspace &ws) { SetWS(ws); }
0072
0073
0074 void ReplaceWS(RooWorkspace *ws) override
0075 {
0076 fRefWS = nullptr;
0077 SetWS(*ws);
0078 }
0079
0080
0081 virtual void SetProtoData(RooAbsData &data)
0082 {
0083 ImportDataInWS(data);
0084 SetProtoData(data.GetName());
0085 }
0086
0087
0088 virtual void SetPdf(const RooAbsPdf &pdf)
0089 {
0090 ImportPdfInWS(pdf);
0091 SetPdf(pdf.GetName());
0092 }
0093
0094
0095 virtual void SetPriorPdf(const RooAbsPdf &pdf)
0096 {
0097 ImportPdfInWS(pdf);
0098 SetPriorPdf(pdf.GetName());
0099 }
0100
0101
0102 virtual void SetParameters(const RooArgSet &set)
0103 {
0104 if (!SetHasOnlyParameters(set, "ModelConfig::SetParameters"))
0105 return;
0106 fPOIName = std::string(GetName()) + "_POI";
0107 DefineSetInWS(fPOIName.c_str(), set);
0108 }
0109
0110
0111 virtual void SetParametersOfInterest(const RooArgSet &set)
0112 {
0113 if (!SetHasOnlyParameters(set, "ModelConfig::SetParametersOfInterest"))
0114 return;
0115 SetParameters(set);
0116 }
0117
0118
0119
0120 virtual void SetParameters(const char *argList)
0121 {
0122 if (!GetWS())
0123 return;
0124 SetParameters(GetWS()->argSet(argList));
0125 }
0126
0127
0128
0129 virtual void SetParametersOfInterest(const char *argList) { SetParameters(argList); }
0130
0131
0132 virtual void SetNuisanceParameters(const RooArgSet &set)
0133 {
0134 if (!SetHasOnlyParameters(set, "ModelConfig::SetNuisanceParameters"))
0135 return;
0136 fNuisParamsName = std::string(GetName()) + "_NuisParams";
0137 DefineSetInWS(fNuisParamsName.c_str(), set);
0138 }
0139
0140
0141
0142 virtual void SetNuisanceParameters(const char *argList)
0143 {
0144 if (!GetWS())
0145 return;
0146 SetNuisanceParameters(GetWS()->argSet(argList));
0147 }
0148
0149
0150 virtual void SetConstraintParameters(const RooArgSet &set)
0151 {
0152 if (!SetHasOnlyParameters(set, "ModelConfig::SetConstrainedParameters"))
0153 return;
0154 fConstrParamsName = std::string(GetName()) + "_ConstrainedParams";
0155 DefineSetInWS(fConstrParamsName.c_str(), set);
0156 }
0157
0158
0159 virtual void SetConstraintParameters(const char *argList)
0160 {
0161 if (!GetWS())
0162 return;
0163 SetConstraintParameters(GetWS()->argSet(argList));
0164 }
0165
0166
0167 virtual void SetObservables(const RooArgSet &set)
0168 {
0169 if (!SetHasOnlyParameters(set, "ModelConfig::SetObservables"))
0170 return;
0171 fObservablesName = std::string(GetName()) + "_Observables";
0172 DefineSetInWS(fObservablesName.c_str(), set);
0173 }
0174
0175
0176 virtual void SetObservables(const char *argList)
0177 {
0178 if (!GetWS())
0179 return;
0180 SetObservables(GetWS()->argSet(argList));
0181 }
0182
0183 virtual void SetConditionalObservables(const RooArgSet &set);
0184
0185
0186 virtual void SetConditionalObservables(const char *argList)
0187 {
0188 if (!GetWS())
0189 return;
0190 SetConditionalObservables(GetWS()->argSet(argList));
0191 }
0192
0193 virtual void SetGlobalObservables(const RooArgSet &set);
0194
0195
0196 virtual void SetGlobalObservables(const char *argList)
0197 {
0198 if (!GetWS())
0199 return;
0200 SetGlobalObservables(GetWS()->argSet(argList));
0201 }
0202
0203 void SetExternalConstraints(const RooArgSet &set);
0204
0205
0206 virtual void SetExternalConstraints(const char *argList)
0207 {
0208 if (!GetWS())
0209 return;
0210 SetExternalConstraints(GetWS()->argSet(argList));
0211 }
0212
0213
0214
0215 virtual void SetSnapshot(const RooArgSet &set);
0216
0217
0218 virtual void SetPdf(const char *name)
0219 {
0220 if (!GetWS())
0221 return;
0222
0223 if (GetWS()->pdf(name)) {
0224 fPdfName = name;
0225 } else {
0226 std::stringstream ss;
0227 ss << "pdf " << name << " does not exist in workspace";
0228 const std::string errorMsg = ss.str();
0229 coutE(ObjectHandling) << errorMsg << std::endl;
0230 throw std::runtime_error(errorMsg);
0231 }
0232 }
0233
0234
0235 virtual void SetPriorPdf(const char *name)
0236 {
0237 if (!GetWS())
0238 return;
0239
0240 if (GetWS()->pdf(name)) {
0241 fPriorPdfName = name;
0242 } else {
0243 std::stringstream ss;
0244 ss << "pdf " << name << " does not exist in workspace";
0245 const std::string errorMsg = ss.str();
0246 coutE(ObjectHandling) << errorMsg << std::endl;
0247 throw std::runtime_error(errorMsg);
0248 }
0249 }
0250
0251
0252 virtual void SetProtoData(const char *name)
0253 {
0254 if (!GetWS())
0255 return;
0256
0257 if (GetWS()->data(name)) {
0258 fProtoDataName = name;
0259 } else {
0260 std::stringstream ss;
0261 ss << "dataset " << name << " does not exist in workspace";
0262 const std::string errorMsg = ss.str();
0263 coutE(ObjectHandling) << errorMsg << std::endl;
0264 throw std::runtime_error(errorMsg);
0265 }
0266 }
0267
0268
0269
0270
0271 RooAbsPdf *GetPdf() const { return (GetWS()) ? GetWS()->pdf(fPdfName) : nullptr; }
0272
0273
0274 const RooArgSet *GetParametersOfInterest() const { return (GetWS()) ? GetWS()->set(fPOIName) : nullptr; }
0275
0276
0277 const RooArgSet *GetNuisanceParameters() const { return (GetWS()) ? GetWS()->set(fNuisParamsName) : nullptr; }
0278
0279
0280 const RooArgSet *GetConstraintParameters() const { return (GetWS()) ? GetWS()->set(fConstrParamsName) : nullptr; }
0281
0282
0283 RooAbsPdf *GetPriorPdf() const { return (GetWS()) ? GetWS()->pdf(fPriorPdfName) : nullptr; }
0284
0285
0286 const RooArgSet *GetObservables() const { return (GetWS()) ? GetWS()->set(fObservablesName) : nullptr; }
0287
0288
0289 const RooArgSet *GetConditionalObservables() const
0290 {
0291 return (GetWS()) ? GetWS()->set(fConditionalObsName) : nullptr;
0292 }
0293
0294
0295 const RooArgSet *GetGlobalObservables() const { return (GetWS()) ? GetWS()->set(fGlobalObsName) : nullptr; }
0296
0297
0298 const RooArgSet *GetExternalConstraints() const { return (GetWS()) ? GetWS()->set(fExtConstraintsName) : nullptr; }
0299
0300
0301 RooAbsData *GetProtoData() const { return (GetWS()) ? GetWS()->data(fProtoDataName) : nullptr; }
0302
0303
0304 const RooArgSet *GetSnapshot() const;
0305
0306 void LoadSnapshot() const;
0307
0308 RooWorkspace *GetWS() const override;
0309
0310 RooWorkspace *GetWorkspace() const { return GetWS(); }
0311
0312 void GuessObsAndNuisance(const RooAbsData &data, bool printModelConfig = true);
0313
0314
0315 void Print(Option_t *option = "") const override;
0316
0317 template <typename... CmdArgs_t>
0318 std::unique_ptr<RooAbsReal> createNLL(RooAbsData &data, CmdArgs_t const &...cmdArgs) const
0319 {
0320 return createNLLImpl(data, *RooFit::Detail::createCmdList(&cmdArgs...));
0321 }
0322
0323 template <typename... CmdArgs_t>
0324 std::unique_ptr<RooFitResult> fitTo(RooAbsData &data, CmdArgs_t const &...cmdArgs)
0325 {
0326 return fitToImpl(data, *RooFit::Detail::createCmdList(&cmdArgs...));
0327 }
0328
0329 protected:
0330
0331 bool SetHasOnlyParameters(const RooArgSet &set, const char *errorMsgPrefix = nullptr);
0332
0333
0334 void DefineSetInWS(const char *name, const RooArgSet &set);
0335
0336
0337 void ImportPdfInWS(const RooAbsPdf &pdf);
0338
0339
0340 void ImportDataInWS(RooAbsData &data);
0341
0342 TRef fRefWS;
0343
0344 std::string fWSName;
0345
0346 std::string fPdfName;
0347 std::string fDataName;
0348 std::string fPOIName;
0349
0350 std::string fNuisParamsName;
0351 std::string fConstrParamsName;
0352 std::string fPriorPdfName;
0353
0354 std::string fConditionalObsName;
0355 std::string fGlobalObsName;
0356 std::string fExtConstraintsName;
0357 std::string fProtoDataName;
0358
0359 std::string fSnapshotName;
0360
0361 std::string fObservablesName;
0362
0363 private:
0364 std::unique_ptr<RooAbsReal> createNLLImpl(RooAbsData &data, const RooLinkedList &cmdList) const;
0365 std::unique_ptr<RooFitResult> fitToImpl(RooAbsData &data, const RooLinkedList &cmdList);
0366
0367 ClassDefOverride(ModelConfig,
0368 6);
0369 };
0370
0371 }
0372
0373 namespace RooFit {
0374 using ModelConfig = RooStats::ModelConfig;
0375 }
0376
0377 #endif