Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:11:23

0001 /*****************************************************************************
0002  * Project: RooFit                                                           *
0003  *                                                                           *
0004  * authors:                                                                  *
0005  *  Lydia Brenner (lbrenner@cern.ch), Carsten Burgard (cburgard@cern.ch)     *
0006  *  Katharina Ecker (kecker@cern.ch), Adam Kaluza      (akaluza@cern.ch)     *
0007  * Copyright (c) 2000-2007, Regents of the University of California          *
0008  *                          and Stanford University. All rights reserved.    *
0009  *                                                                           *
0010  * Redistribution and use in source and binary forms,                        *
0011  * with or without modification, are permitted according to the terms        *
0012  * listed in LICENSE (http://roofit.sourceforge.net/license.txt)
0013  *****************************************************************************/
0014 
0015 ////////////////////////////////////////////////////////////////////////////////////////////////
0016 //
0017 // RooLagrangianMorphFunc
0018 //
0019 // The RooLagrangianMorphFunc is a type of RooAbsReal that allows to morph
0020 // different input EFT samples to some arbitrary output EFT
0021 // sample, as long as the desired set of output parameters lie
0022 // within the realm spanned by the input samples. More
0023 // specifically, it expects as an input a TFile (or TDirectory)
0024 // with the following layout:
0025 //
0026 // TDirectory
0027 //  |-sample1
0028 //  | |-param_card    // TH1 EFT parameter values of sample1
0029 //  | | histogram1    // TH1 of some physics distribution
0030 //  | |-subfolder1    // a subfolder (optional)
0031 //  | | |-histogram2  // TH1 of some physics distribution
0032 //  | | |-....
0033 //  |-sample2
0034 //  | |-param_card     // TH1 of EFT parameter values of sample2
0035 //  | | histogram1     // TH1 of some physics distribution
0036 //  | |-subfolder1     // same folder structure as before
0037 //  | | |-histogram2  // TH1 of some physics distribution
0038 //  | | |-....
0039 //  |-sampleN
0040 // The RooLagrangianMorphFunc operates on this structure, extracts data
0041 // and meta-data and produces a morphing result as a RooRealSumFunc
0042 // consisting of the input histograms with appropriate prefactors.
0043 //
0044 // The histograms to be morphed can be accessed via their paths in
0045 // the respective sample, e.g. using
0046 //    "histogram"
0047 // or "subfolder1/histogram1"
0048 // or "some/deep/path/to/some/subfolder/histname"
0049 //
0050 ////////////////////////////////////////////////////////////////////////////////////////////////
0051 
0052 #ifndef ROO_LAGRANGIAN_MORPH
0053 #define ROO_LAGRANGIAN_MORPH
0054 
0055 #include "RooFit/Floats.h"
0056 #include "RooAbsReal.h"
0057 #include "RooArgList.h"
0058 #include "RooRatio.h"
0059 #include "RooRealSumFunc.h"
0060 #include "RooRealSumPdf.h"
0061 #include "RooSetProxy.h"
0062 #include "RooWrapperPdf.h"
0063 #include "TMatrixD.h"
0064 
0065 class RooWorkspace;
0066 class RooProduct;
0067 class RooRealVar;
0068 class TPair;
0069 class TFolder;
0070 class RooLagrangianMorphFunc;
0071 
0072 #include <fstream>
0073 #include <iostream>
0074 #include <string>
0075 #include <vector>
0076 
0077 class RooLagrangianMorphFunc : public RooAbsReal {
0078 
0079 public:
0080    typedef std::map<const std::string, double> ParamSet;
0081    typedef std::map<const std::string, int> FlagSet;
0082    typedef std::map<const std::string, ParamSet> ParamMap;
0083    typedef std::map<const std::string, FlagSet> FlagMap;
0084 
0085    struct Config {
0086       std::string observableName;
0087       RooRealVar *observable = nullptr;
0088       std::string fileName;
0089       ParamMap paramCards;
0090       FlagMap flagValues;
0091       std::vector<std::string> folderNames;
0092       RooArgList couplings;
0093       RooArgList decCouplings;
0094       RooArgList prodCouplings;
0095       RooArgList folders;
0096       std::vector<RooArgList *> vertices;
0097       std::vector<std::vector<const char *>> nonInterfering;
0098       bool allowNegativeYields = true;
0099       bool normalize = false;
0100    };
0101 
0102    RooLagrangianMorphFunc();
0103    RooLagrangianMorphFunc(const char *name, const char *title, const Config &config);
0104    RooLagrangianMorphFunc(const RooLagrangianMorphFunc &other, const char *newName);
0105 
0106    ~RooLagrangianMorphFunc() override;
0107 
0108    std::list<double> *binBoundaries(RooAbsRealLValue & /*obs*/, double /*xlo*/, double /*xhi*/) const override;
0109    std::list<double> *plotSamplingHint(RooAbsRealLValue & /*obs*/, double /*xlo*/, double /*xhi*/) const override;
0110    bool isBinnedDistribution(const RooArgSet &obs) const override;
0111    double evaluate() const override;
0112    TObject *clone(const char *newname) const override { return new RooLagrangianMorphFunc(*this, newname); }
0113 
0114    bool checkObservables(const RooArgSet *nset) const override;
0115    bool forceAnalyticalInt(const RooAbsArg &arg) const override;
0116    Int_t getAnalyticalIntegralWN(RooArgSet &allVars, RooArgSet &numVars, const RooArgSet *normSet,
0117                                  const char *rangeName = nullptr) const override;
0118    double analyticalIntegralWN(Int_t code, const RooArgSet *normSet, const char *rangeName = nullptr) const override;
0119    void printMetaArgs(std::ostream &os) const override;
0120    RooAbsArg::CacheMode canNodeBeCached() const override;
0121    void setCacheAndTrackHints(RooArgSet &) override;
0122 
0123    void insert(RooWorkspace *ws);
0124 
0125    void setParameters(const char *foldername);
0126    void setParameters(TH1 *paramhist);
0127    void setParameter(const char *name, double value);
0128    void setFlag(const char *name, double value);
0129    void setParameters(const ParamSet &params);
0130    void setParameters(const RooArgList *list);
0131    double getParameterValue(const char *name) const;
0132    RooRealVar *getParameter(const char *name) const;
0133    RooRealVar *getFlag(const char *name) const;
0134    bool hasParameter(const char *paramname) const;
0135    bool isParameterUsed(const char *paramname) const;
0136    bool isParameterConstant(const char *paramname) const;
0137    void setParameterConstant(const char *paramname, bool constant) const;
0138    void setParameter(const char *name, double value, double min, double max);
0139    void setParameter(const char *name, double value, double min, double max, double error);
0140    void randomizeParameters(double z);
0141    const RooArgSet *getParameterSet() const;
0142    ParamSet getMorphParameters(const char *foldername) const;
0143    ParamSet getMorphParameters() const;
0144 
0145    RooLagrangianMorphFunc *getLinear() const;
0146 
0147    int nParameters() const;
0148    int nPolynomials() const;
0149 
0150    bool isCouplingUsed(const char *couplname);
0151    const RooArgList *getCouplingSet() const;
0152    ParamSet getCouplings() const;
0153 
0154    TMatrixD getMatrix() const;
0155    TMatrixD getInvertedMatrix() const;
0156    double getCondition() const;
0157 
0158    RooRealVar *getObservable() const;
0159    RooRealVar *getBinWidth() const;
0160 
0161    void printEvaluation() const;
0162    void printCouplings() const;
0163    void printFlags() const;
0164    void printPhysics() const;
0165 
0166    RooProduct *getSumElement(const char *name) const;
0167 
0168    std::vector<std::string> getSamples() const;
0169 
0170    double expectedUncertainty() const;
0171    TH1 *createTH1(const std::string &name);
0172    TH1 *createTH1(const std::string &name, bool correlateErrors);
0173 
0174 private:
0175    class CacheElem;
0176    void init();
0177    void setup(bool ownParams = true);
0178    void disableInterference(const std::vector<const char *> &nonInterfering);
0179    void disableInterferences(const std::vector<std::vector<const char *>> &nonInterfering);
0180 
0181    bool hasCache() const;
0182    RooLagrangianMorphFunc::CacheElem *getCache() const;
0183    void updateSampleWeights();
0184 
0185    RooRealVar *setupObservable(const char *obsname, TClass *mode, TObject *inputExample);
0186 
0187 public:
0188    /// length of floating point digits precision supported by implementation.
0189    static constexpr double implementedPrecision = RooFit::SuperFloatPrecision::digits10;
0190 
0191    void writeMatrixToFile(const TMatrixD &matrix, const char *fname);
0192    void writeMatrixToStream(const TMatrixD &matrix, std::ostream &stream);
0193    TMatrixD readMatrixFromFile(const char *fname);
0194    TMatrixD readMatrixFromStream(std::istream &stream);
0195 
0196    int countSamples(std::vector<RooArgList *> &vertices);
0197    int countSamples(int nprod, int ndec, int nboth);
0198 
0199    std::map<std::string, std::string>
0200    createWeightStrings(const ParamMap &inputs, const std::vector<std::vector<std::string>> &vertices);
0201    std::map<std::string, std::string>
0202    createWeightStrings(const ParamMap &inputs, const std::vector<RooArgList *> &vertices, RooArgList &couplings);
0203    std::map<std::string, std::string>
0204    createWeightStrings(const ParamMap &inputs, const std::vector<RooArgList *> &vertices, RooArgList &couplings,
0205                        const FlagMap &flagValues, const RooArgList &flags,
0206                        const std::vector<std::vector<std::string>> &nonInterfering);
0207    RooArgSet createWeights(const ParamMap &inputs, const std::vector<RooArgList *> &vertices, RooArgList &couplings,
0208                            const FlagMap &inputFlags, const RooArgList &flags,
0209                            const std::vector<std::vector<std::string>> &nonInterfering);
0210    RooArgSet createWeights(const ParamMap &inputs, const std::vector<RooArgList *> &vertices, RooArgList &couplings);
0211 
0212    bool updateCoefficients();
0213    bool useCoefficients(const TMatrixD &inverse);
0214    bool useCoefficients(const char *filename);
0215    bool writeCoefficients(const char *filename);
0216 
0217    int countContributingFormulas() const;
0218    RooAbsReal *getSampleWeight(const char *name);
0219    void printParameters(const char *samplename) const;
0220    void printParameters() const;
0221    void printSamples() const;
0222    void printSampleWeights() const;
0223    void printWeights() const;
0224 
0225    void setScale(double val);
0226    double getScale();
0227 
0228    int nSamples() const { return _config.folderNames.size(); }
0229 
0230    RooRealSumFunc *getFunc() const;
0231    std::unique_ptr<RooWrapperPdf> createPdf() const;
0232 
0233    RooAbsPdf::ExtendMode extendMode() const;
0234    double expectedEvents(const RooArgSet *nset) const;
0235    double expectedEvents(const RooArgSet &nset) const;
0236    double expectedEvents() const;
0237    bool selfNormalized() const { return true; }
0238 
0239    void readParameters(TDirectory *f);
0240    void collectInputs(TDirectory *f);
0241 
0242    static std::unique_ptr<RooRatio> makeRatio(const char *name, const char *title, RooArgList &nr, RooArgList &dr);
0243 
0244 private:
0245    mutable RooObjCacheManager _cacheMgr; //! The cache manager
0246    double _scale = 1.0;
0247    std::map<std::string, int> _sampleMap;
0248    RooListProxy _physics;
0249    RooSetProxy _operators;
0250    RooListProxy _observables;
0251    RooListProxy _binWidths;
0252    RooListProxy _flags;
0253    Config _config;
0254    std::vector<std::vector<RooListProxy *>> _diagrams;
0255    std::vector<std::vector<std::string>> _nonInterfering;
0256 
0257    ClassDefOverride(RooLagrangianMorphFunc, 2)
0258 };
0259 
0260 #endif