Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:10:27

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 <TRef.h>
0027 
0028 #include <string>
0029 
0030 class RooFitResult;
0031 
0032 // ModelConfig kept in the RooStats namespace for backwards compatibility.
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    /// clone
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    /// Set a workspace that owns all the necessary components for the analysis.
0069    void SetWS(RooWorkspace &ws) override;
0070    //// alias for SetWS(...)
0071    virtual void SetWorkspace(RooWorkspace &ws) { SetWS(ws); }
0072 
0073    /// Remove the existing reference to a workspace and replace it with this new one.
0074    void ReplaceWS(RooWorkspace *ws) override
0075    {
0076       fRefWS = nullptr;
0077       SetWS(*ws);
0078    }
0079 
0080    /// Set the proto DataSet, add to the workspace if not already there
0081    virtual void SetProtoData(RooAbsData &data)
0082    {
0083       ImportDataInWS(data);
0084       SetProtoData(data.GetName());
0085    }
0086 
0087    /// Set the Pdf, add to the workspace if not already there
0088    virtual void SetPdf(const RooAbsPdf &pdf)
0089    {
0090       ImportPdfInWS(pdf);
0091       SetPdf(pdf.GetName());
0092    }
0093 
0094    /// Set the Prior Pdf, add to the workspace if not already there
0095    virtual void SetPriorPdf(const RooAbsPdf &pdf)
0096    {
0097       ImportPdfInWS(pdf);
0098       SetPriorPdf(pdf.GetName());
0099    }
0100 
0101    /// Specify parameters of the PDF.
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    /// Specify parameters of interest.
0111    virtual void SetParametersOfInterest(const RooArgSet &set)
0112    {
0113       if (!SetHasOnlyParameters(set, "ModelConfig::SetParametersOfInterest"))
0114          return;
0115       SetParameters(set);
0116    }
0117 
0118    /// Specify parameters
0119    /// using a list of comma-separated list of arguments already in the workspace.
0120    virtual void SetParameters(const char *argList)
0121    {
0122       if (!GetWS())
0123          return;
0124       SetParameters(GetWS()->argSet(argList));
0125    }
0126 
0127    /// Specify parameters of interest
0128    /// using a comma-separated list of arguments already in the workspace.
0129    virtual void SetParametersOfInterest(const char *argList) { SetParameters(argList); }
0130 
0131    /// Specify the nuisance parameters (parameters that are not POI).
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    /// Specify the nuisance parameters
0141    /// using a comma-separated list of arguments already in the workspace.
0142    virtual void SetNuisanceParameters(const char *argList)
0143    {
0144       if (!GetWS())
0145          return;
0146       SetNuisanceParameters(GetWS()->argSet(argList));
0147    }
0148 
0149    /// Specify the constraint parameters
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    /// Specify the constraint parameters
0158    /// through a comma-separated list of arguments already in the workspace.
0159    virtual void SetConstraintParameters(const char *argList)
0160    {
0161       if (!GetWS())
0162          return;
0163       SetConstraintParameters(GetWS()->argSet(argList));
0164    }
0165 
0166    /// Specify the observables.
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    /// specify the observables
0175    /// through a comma-separated list of arguments already in the workspace.
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    /// Specify the conditional observables
0185    /// through a comma-separated list of arguments already in the workspace.
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    /// Specify the global observables
0195    /// through a comma-separated list of arguments already in the workspace.
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    /// Specify the external constraints
0205    /// through a comma-separated list of arguments already in the workspace.
0206    virtual void SetExternalConstraints(const char *argList)
0207    {
0208       if (!GetWS())
0209          return;
0210       SetExternalConstraints(GetWS()->argSet(argList));
0211    }
0212 
0213    /// Set parameter values for a particular hypothesis if using a common PDF
0214    /// by saving a snapshot in the workspace.
0215    virtual void SetSnapshot(const RooArgSet &set);
0216 
0217    /// Specify the name of the PDF in the workspace to be used.
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    /// Specify the name of the PDF in the workspace to be used.
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    /// Specify the name of the dataset in the workspace to be used.
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    /* getter methods */
0269 
0270    /// get model PDF (return nullptr if pdf has not been specified or does not exist)
0271    RooAbsPdf *GetPdf() const { return (GetWS()) ? GetWS()->pdf(fPdfName) : nullptr; }
0272 
0273    /// get RooArgSet containing the parameter of interest (return nullptr if not existing)
0274    const RooArgSet *GetParametersOfInterest() const { return (GetWS()) ? GetWS()->set(fPOIName) : nullptr; }
0275 
0276    /// get RooArgSet containing the nuisance parameters (return nullptr if not existing)
0277    const RooArgSet *GetNuisanceParameters() const { return (GetWS()) ? GetWS()->set(fNuisParamsName) : nullptr; }
0278 
0279    /// get RooArgSet containing the constraint parameters (return nullptr if not existing)
0280    const RooArgSet *GetConstraintParameters() const { return (GetWS()) ? GetWS()->set(fConstrParamsName) : nullptr; }
0281 
0282    /// get parameters prior pdf  (return nullptr if not existing)
0283    RooAbsPdf *GetPriorPdf() const { return (GetWS()) ? GetWS()->pdf(fPriorPdfName) : nullptr; }
0284 
0285    /// get RooArgSet for observables  (return nullptr if not existing)
0286    const RooArgSet *GetObservables() const { return (GetWS()) ? GetWS()->set(fObservablesName) : nullptr; }
0287 
0288    /// get RooArgSet for conditional observables  (return nullptr if not existing)
0289    const RooArgSet *GetConditionalObservables() const
0290    {
0291       return (GetWS()) ? GetWS()->set(fConditionalObsName) : nullptr;
0292    }
0293 
0294    /// get RooArgSet for global observables  (return nullptr if not existing)
0295    const RooArgSet *GetGlobalObservables() const { return (GetWS()) ? GetWS()->set(fGlobalObsName) : nullptr; }
0296 
0297    /// get RooArgSet for global observables  (return nullptr if not existing)
0298    const RooArgSet *GetExternalConstraints() const { return (GetWS()) ? GetWS()->set(fExtConstraintsName) : nullptr; }
0299 
0300    /// get Proto data set (return nullptr if not existing)
0301    RooAbsData *GetProtoData() const { return (GetWS()) ? GetWS()->data(fProtoDataName) : nullptr; }
0302 
0303    /// get RooArgSet for parameters for a particular hypothesis  (return nullptr if not existing)
0304    const RooArgSet *GetSnapshot() const;
0305 
0306    void LoadSnapshot() const;
0307 
0308    RooWorkspace *GetWS() const override;
0309    /// alias for GetWS()
0310    RooWorkspace *GetWorkspace() const { return GetWS(); }
0311 
0312    void GuessObsAndNuisance(const RooAbsData &data, bool printModelConfig = true);
0313 
0314    /// overload the print method
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    /// helper function to check that content of a given set is exclusively parameters
0331    bool SetHasOnlyParameters(const RooArgSet &set, const char *errorMsgPrefix = nullptr);
0332 
0333    /// helper functions to define a set in the WS
0334    void DefineSetInWS(const char *name, const RooArgSet &set);
0335 
0336    /// internal function to import Pdf in WS
0337    void ImportPdfInWS(const RooAbsPdf &pdf);
0338 
0339    /// internal function to import data in WS
0340    void ImportDataInWS(RooAbsData &data);
0341 
0342    TRef fRefWS; ///< WS reference used in the file
0343 
0344    std::string fWSName; ///< name of the WS
0345 
0346    std::string fPdfName;  ///< name of  PDF in workspace
0347    std::string fDataName; ///< name of data set in workspace
0348    std::string fPOIName;  ///< name for RooArgSet specifying parameters of interest
0349 
0350    std::string fNuisParamsName;   ///< name for RooArgSet specifying nuisance parameters
0351    std::string fConstrParamsName; ///< name for RooArgSet specifying constrained parameters
0352    std::string fPriorPdfName;     ///< name for RooAbsPdf specifying a prior on the parameters
0353 
0354    std::string fConditionalObsName; ///< name for RooArgSet specifying conditional observables
0355    std::string fGlobalObsName;      ///< name for RooArgSet specifying global observables
0356    std::string fExtConstraintsName; ///< name for RooArgSet specifying external constraints
0357    std::string fProtoDataName;      ///< name for RooArgSet specifying dataset that should be used as proto-data
0358 
0359    std::string fSnapshotName; ///< name for RooArgSet that specifies a particular hypothesis
0360 
0361    std::string fObservablesName; ///< name for RooArgSet specifying observable parameters.
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); ///< A class that holds configuration information for a model using a workspace as a store
0369 };
0370 
0371 } // end namespace RooStats
0372 
0373 namespace RooFit {
0374 using ModelConfig = RooStats::ModelConfig;
0375 }
0376 
0377 #endif