Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-12 10:09:07

0001 /*
0002  * Project: RooFit
0003  * Authors:
0004  *   Kyle Cranmer,
0005  *   Lorenzo Moneta,
0006  *   Gregory Schott,
0007  *   Wouter Verkerke,
0008  *   Sven Kreiss
0009  *
0010  * Copyright (c) 2023, CERN
0011  *
0012  * Redistribution and use in source and binary forms,
0013  * with or without modification, are permitted according to the terms
0014  * listed in LICENSE (http://roofit.sourceforge.net/license.txt)
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 <string>
0027 
0028 class RooFitResult;
0029 
0030 // ModelConfig kept in the RooStats namespace for backwards compatibility.
0031 namespace RooStats {
0032 
0033 ///< A class that holds configuration information for a model using a workspace as a store
0034 class ModelConfig final : public TNamed, public RooWorkspaceHandle {
0035 
0036 public:
0037    ModelConfig(RooWorkspace *ws = nullptr)
0038    {
0039       if (ws)
0040          SetWS(*ws);
0041    }
0042 
0043    ModelConfig(const char *name, RooWorkspace *ws = nullptr) : TNamed(name, name)
0044    {
0045       if (ws)
0046          SetWS(*ws);
0047    }
0048 
0049    ModelConfig(const char *name, const char *title, RooWorkspace *ws = nullptr) : TNamed(name, title)
0050    {
0051       if (ws)
0052          SetWS(*ws);
0053    }
0054 
0055    /// clone
0056    ModelConfig *Clone(const char *name = "") const override
0057    {
0058       ModelConfig *mc = new ModelConfig(*this);
0059       if (strcmp(name, "") == 0) {
0060          mc->SetName(this->GetName());
0061       } else {
0062          mc->SetName(name);
0063       }
0064       return mc;
0065    }
0066 
0067    /// Set a workspace that owns all the necessary components for the analysis.
0068    void SetWS(RooWorkspace &ws) override;
0069    //// alias for SetWS(...)
0070    void SetWorkspace(RooWorkspace &ws) { SetWS(ws); }
0071 
0072    /// Remove the existing reference to a workspace and replace it with this new one.
0073    void ReplaceWS(RooWorkspace *ws) override
0074    {
0075       fRefWS = nullptr;
0076       SetWS(*ws);
0077    }
0078 
0079    /// Set the proto DataSet, add to the workspace if not already there
0080    void SetProtoData(RooAbsData &data)
0081    {
0082       ImportDataInWS(data);
0083       SetProtoData(data.GetName());
0084    }
0085 
0086    /// Set the Pdf, add to the workspace if not already there
0087    void SetPdf(const RooAbsPdf &pdf)
0088    {
0089       ImportPdfInWS(pdf);
0090       SetPdf(pdf.GetName());
0091    }
0092 
0093    /// Set the Prior Pdf, add to the workspace if not already there
0094    void SetPriorPdf(const RooAbsPdf &pdf)
0095    {
0096       ImportPdfInWS(pdf);
0097       SetPriorPdf(pdf.GetName());
0098    }
0099 
0100    /// Specify parameters of the PDF.
0101    void SetParameters(const RooArgSet &set)
0102    {
0103       if (!SetHasOnlyParameters(set, "ModelConfig::SetParameters"))
0104          return;
0105       fPOIName = std::string(GetName()) + "_POI";
0106       DefineSetInWS(fPOIName.c_str(), set);
0107    }
0108 
0109    /// Specify parameters of interest.
0110    void SetParametersOfInterest(const RooArgSet &set)
0111    {
0112       if (!SetHasOnlyParameters(set, "ModelConfig::SetParametersOfInterest"))
0113          return;
0114       SetParameters(set);
0115    }
0116 
0117    /// Specify parameters
0118    /// using a list of comma-separated list of arguments already in the workspace.
0119    void SetParameters(const char *argList)
0120    {
0121       if (!GetWS())
0122          return;
0123       SetParameters(GetWS()->argSet(argList));
0124    }
0125 
0126    /// Specify parameters of interest
0127    /// using a comma-separated list of arguments already in the workspace.
0128    void SetParametersOfInterest(const char *argList) { SetParameters(argList); }
0129 
0130    /// Specify the nuisance parameters (parameters that are not POI).
0131    void SetNuisanceParameters(const RooArgSet &set)
0132    {
0133       if (!SetHasOnlyParameters(set, "ModelConfig::SetNuisanceParameters"))
0134          return;
0135       fNuisParamsName = std::string(GetName()) + "_NuisParams";
0136       DefineSetInWS(fNuisParamsName.c_str(), set);
0137    }
0138 
0139    /// Specify the nuisance parameters
0140    /// using a comma-separated list of arguments already in the workspace.
0141    void SetNuisanceParameters(const char *argList)
0142    {
0143       if (!GetWS())
0144          return;
0145       SetNuisanceParameters(GetWS()->argSet(argList));
0146    }
0147 
0148    /// Specify the constraint parameters
0149    void SetConstraintParameters(const RooArgSet &set)
0150    {
0151       if (!SetHasOnlyParameters(set, "ModelConfig::SetConstrainedParameters"))
0152          return;
0153       fConstrParamsName = std::string(GetName()) + "_ConstrainedParams";
0154       DefineSetInWS(fConstrParamsName.c_str(), set);
0155    }
0156    /// Specify the constraint parameters
0157    /// through a comma-separated list of arguments already in the workspace.
0158    void SetConstraintParameters(const char *argList)
0159    {
0160       if (!GetWS())
0161          return;
0162       SetConstraintParameters(GetWS()->argSet(argList));
0163    }
0164 
0165    /// Specify the observables.
0166    void SetObservables(const RooArgSet &set)
0167    {
0168       if (!SetHasOnlyParameters(set, "ModelConfig::SetObservables"))
0169          return;
0170       fObservablesName = std::string(GetName()) + "_Observables";
0171       DefineSetInWS(fObservablesName.c_str(), set);
0172    }
0173    /// specify the observables
0174    /// through a comma-separated list of arguments already in the workspace.
0175    void SetObservables(const char *argList)
0176    {
0177       if (!GetWS())
0178          return;
0179       SetObservables(GetWS()->argSet(argList));
0180    }
0181 
0182    void SetConditionalObservables(const RooArgSet &set);
0183    /// Specify the conditional observables
0184    /// through a comma-separated list of arguments already in the workspace.
0185    void SetConditionalObservables(const char *argList)
0186    {
0187       if (!GetWS())
0188          return;
0189       SetConditionalObservables(GetWS()->argSet(argList));
0190    }
0191 
0192    void SetGlobalObservables(const RooArgSet &set);
0193    /// Specify the global observables
0194    /// through a comma-separated list of arguments already in the workspace.
0195    void SetGlobalObservables(const char *argList)
0196    {
0197       if (!GetWS())
0198          return;
0199       SetGlobalObservables(GetWS()->argSet(argList));
0200    }
0201 
0202    void SetExternalConstraints(const RooArgSet &set);
0203    /// Specify the external constraints
0204    /// through a comma-separated list of arguments already in the workspace.
0205    void SetExternalConstraints(const char *argList)
0206    {
0207       if (!GetWS())
0208          return;
0209       SetExternalConstraints(GetWS()->argSet(argList));
0210    }
0211 
0212    /// Set parameter values for a particular hypothesis if using a common PDF
0213    /// by saving a snapshot in the workspace.
0214    void SetSnapshot(const RooArgSet &set);
0215 
0216    /// Specify the name of the PDF in the workspace to be used.
0217    void SetPdf(const char *name)
0218    {
0219       if (!GetWS())
0220          return;
0221 
0222       if (GetWS()->pdf(name)) {
0223          fPdfName = name;
0224       } else {
0225          std::stringstream ss;
0226          ss << "pdf " << name << " does not exist in workspace";
0227          const std::string errorMsg = ss.str();
0228          coutE(ObjectHandling) << errorMsg << std::endl;
0229          throw std::runtime_error(errorMsg);
0230       }
0231    }
0232 
0233    /// Specify the name of the PDF in the workspace to be used.
0234    void SetPriorPdf(const char *name)
0235    {
0236       if (!GetWS())
0237          return;
0238 
0239       if (GetWS()->pdf(name)) {
0240          fPriorPdfName = name;
0241       } else {
0242          std::stringstream ss;
0243          ss << "pdf " << name << " does not exist in workspace";
0244          const std::string errorMsg = ss.str();
0245          coutE(ObjectHandling) << errorMsg << std::endl;
0246          throw std::runtime_error(errorMsg);
0247       }
0248    }
0249 
0250    /// Specify the name of the dataset in the workspace to be used.
0251    void SetProtoData(const char *name)
0252    {
0253       if (!GetWS())
0254          return;
0255 
0256       if (GetWS()->data(name)) {
0257          fProtoDataName = name;
0258       } else {
0259          std::stringstream ss;
0260          ss << "dataset " << name << " does not exist in workspace";
0261          const std::string errorMsg = ss.str();
0262          coutE(ObjectHandling) << errorMsg << std::endl;
0263          throw std::runtime_error(errorMsg);
0264       }
0265    }
0266 
0267    /* getter methods */
0268 
0269    /// get model PDF (return nullptr if pdf has not been specified or does not exist)
0270    RooAbsPdf *GetPdf() const { return (GetWS()) ? GetWS()->pdf(fPdfName) : nullptr; }
0271 
0272    /// get RooArgSet containing the parameter of interest (return nullptr if not existing)
0273    const RooArgSet *GetParametersOfInterest() const { return (GetWS()) ? GetWS()->set(fPOIName) : nullptr; }
0274 
0275    /// get RooArgSet containing the nuisance parameters (return nullptr if not existing)
0276    const RooArgSet *GetNuisanceParameters() const { return (GetWS()) ? GetWS()->set(fNuisParamsName) : nullptr; }
0277 
0278    /// get RooArgSet containing the constraint parameters (return nullptr if not existing)
0279    const RooArgSet *GetConstraintParameters() const { return (GetWS()) ? GetWS()->set(fConstrParamsName) : nullptr; }
0280 
0281    /// get parameters prior pdf  (return nullptr if not existing)
0282    RooAbsPdf *GetPriorPdf() const { return (GetWS()) ? GetWS()->pdf(fPriorPdfName) : nullptr; }
0283 
0284    /// get RooArgSet for observables  (return nullptr if not existing)
0285    const RooArgSet *GetObservables() const { return (GetWS()) ? GetWS()->set(fObservablesName) : nullptr; }
0286 
0287    /// get RooArgSet for conditional observables  (return nullptr if not existing)
0288    const RooArgSet *GetConditionalObservables() const
0289    {
0290       return (GetWS()) ? GetWS()->set(fConditionalObsName) : nullptr;
0291    }
0292 
0293    /// get RooArgSet for global observables  (return nullptr if not existing)
0294    const RooArgSet *GetGlobalObservables() const { return (GetWS()) ? GetWS()->set(fGlobalObsName) : nullptr; }
0295 
0296    /// get RooArgSet for global observables  (return nullptr if not existing)
0297    const RooArgSet *GetExternalConstraints() const { return (GetWS()) ? GetWS()->set(fExtConstraintsName) : nullptr; }
0298 
0299    /// get Proto data set (return nullptr if not existing)
0300    RooAbsData *GetProtoData() const { return (GetWS()) ? GetWS()->data(fProtoDataName) : nullptr; }
0301 
0302    /// get RooArgSet for parameters for a particular hypothesis  (return nullptr if not existing)
0303    const RooArgSet *GetSnapshot() const;
0304 
0305    void LoadSnapshot() const;
0306 
0307    RooWorkspace *GetWS() const override;
0308    /// alias for GetWS()
0309    RooWorkspace *GetWorkspace() const { return GetWS(); }
0310 
0311    void GuessObsAndNuisance(const RooArgSet &obsSet, bool printModelConfig = true);
0312 
0313    inline void GuessObsAndNuisance(const RooDataSet &data, bool printModelConfig = true)
0314    {
0315       return GuessObsAndNuisance(*data.get(), printModelConfig);
0316    }
0317 
0318    /// overload the print method
0319    void Print(Option_t *option = "") const override;
0320 
0321    template <typename... CmdArgs_t>
0322    std::unique_ptr<RooAbsReal> createNLL(RooAbsData &data, CmdArgs_t const &...cmdArgs) const
0323    {
0324       return createNLLImpl(data, *RooFit::Detail::createCmdList(&cmdArgs...));
0325    }
0326 
0327    template <typename... CmdArgs_t>
0328    std::unique_ptr<RooFitResult> fitTo(RooAbsData &data, CmdArgs_t const &...cmdArgs) const
0329    {
0330       return fitToImpl(data, *RooFit::Detail::createCmdList(&cmdArgs...));
0331    }
0332 
0333 protected:
0334    /// helper function to check that content of a given set is exclusively parameters
0335    bool SetHasOnlyParameters(const RooArgSet &set, const char *errorMsgPrefix = nullptr);
0336 
0337    /// helper functions to define a set in the WS
0338    void DefineSetInWS(const char *name, const RooArgSet &set);
0339 
0340    /// internal function to import Pdf in WS
0341    void ImportPdfInWS(const RooAbsPdf &pdf);
0342 
0343    /// internal function to import data in WS
0344    void ImportDataInWS(RooAbsData &data);
0345 
0346    // The reference to the workspace is not persistent because it is always
0347    // filled when the workspace is read (see RooWorkspace::Streamer()).
0348    RooWorkspace *fRefWS = nullptr; ///<! WS reference used in the file
0349 
0350    std::string fPdfName;  ///< name of  PDF in workspace
0351    std::string fDataName; ///< name of data set in workspace
0352    std::string fPOIName;  ///< name for RooArgSet specifying parameters of interest
0353 
0354    std::string fNuisParamsName;   ///< name for RooArgSet specifying nuisance parameters
0355    std::string fConstrParamsName; ///< name for RooArgSet specifying constrained parameters
0356    std::string fPriorPdfName;     ///< name for RooAbsPdf specifying a prior on the parameters
0357 
0358    std::string fConditionalObsName; ///< name for RooArgSet specifying conditional observables
0359    std::string fGlobalObsName;      ///< name for RooArgSet specifying global observables
0360    std::string fExtConstraintsName; ///< name for RooArgSet specifying external constraints
0361    std::string fProtoDataName;      ///< name for RooArgSet specifying dataset that should be used as proto-data
0362 
0363    std::string fSnapshotName; ///< name for RooArgSet that specifies a particular hypothesis
0364 
0365    std::string fObservablesName; ///< name for RooArgSet specifying observable parameters.
0366 
0367 private:
0368    std::unique_ptr<RooAbsReal> createNLLImpl(RooAbsData &data, const RooLinkedList &cmdList) const;
0369    std::unique_ptr<RooFitResult> fitToImpl(RooAbsData &data, const RooLinkedList &cmdList) const;
0370 
0371    ClassDefOverride(ModelConfig, 7);
0372 };
0373 
0374 } // end namespace RooStats
0375 
0376 namespace RooFit {
0377 using ModelConfig = RooStats::ModelConfig;
0378 }
0379 
0380 #endif