CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_14/src/HiggsAnalysis/CombinedLimit/interface/CachingNLL.h

Go to the documentation of this file.
00001 #ifndef HiggsAnalysis_CombinedLimit_CachingNLL_h
00002 #define HiggsAnalysis_CombinedLimit_CachingNLL_h
00003 
00004 #include <memory>
00005 #include <map>
00006 #include <RooAbsPdf.h>
00007 #include <RooAddPdf.h>
00008 #include <RooRealSumPdf.h>
00009 #include <RooProdPdf.h>
00010 #include <RooAbsData.h>
00011 #include <RooArgSet.h>
00012 #include <RooSetProxy.h>
00013 #include <RooRealVar.h>
00014 #include <RooSimultaneous.h>
00015 #include <RooGaussian.h>
00016 
00017 // Part zero: ArgSet checker
00018 namespace cacheutils {
00019     class ArgSetChecker {
00020         public:
00021             ArgSetChecker() {}
00022             ArgSetChecker(const RooAbsCollection &set) ;
00023             bool changed(bool updateIfChanged=false) ;
00024         private:
00025             std::vector<RooRealVar *> vars_;
00026             std::vector<double> vals_;
00027     };
00028 
00029 // Part zero point five: Cache of pdf values for different parameters
00030     class ValuesCache {
00031         public:
00032             ValuesCache(const RooAbsReal &pdf, const RooArgSet &obs, int size=MaxItems_);
00033             ValuesCache(const RooAbsCollection &params, int size=MaxItems_);
00034             ~ValuesCache();
00035             // search for the item corresponding to the current values of the parameters.
00036             // if available, return (&values, true)
00037             // if not available, return (&room, false)
00038             // and it will be up to the caller code to fill the room the new item
00039             std::pair<std::vector<Double_t> *, bool> get(); 
00040             void clear();
00041         private:
00042             struct Item {
00043                 Item(const RooAbsCollection &set)   : checker(set),   good(false) {}
00044                 Item(const ArgSetChecker    &check) : checker(check), good(false) {}
00045                 std::vector<Double_t> values;
00046                 ArgSetChecker         checker;
00047                 bool                  good;
00048             };
00049             int size_, maxSize_;
00050             enum { MaxItems_ = 3 };
00051             Item *items[MaxItems_];
00052     };
00053 // Part one: cache all values of a pdf
00054 class CachingPdf {
00055     public:
00056         CachingPdf(RooAbsReal *pdf, const RooArgSet *obs) ;
00057         CachingPdf(const CachingPdf &other) ;
00058         ~CachingPdf() ;
00059         const std::vector<Double_t> & eval(const RooAbsData &data) ;
00060         const RooAbsReal *pdf() const { return pdf_; }
00061         void  setDataDirty() { lastData_ = 0; }
00062     private:
00063         const RooArgSet *obs_;
00064         RooAbsReal *pdfOriginal_;
00065         RooArgSet  pdfPieces_;
00066         RooAbsReal *pdf_;
00067         const RooAbsData *lastData_;
00068         ValuesCache cache_;
00069         void realFill_(const RooAbsData &data, std::vector<Double_t> &values) ;
00070 };
00071 
00072 class CachingAddNLL : public RooAbsReal {
00073     public:
00074         CachingAddNLL(const char *name, const char *title, RooAbsPdf *pdf, RooAbsData *data) ;
00075         CachingAddNLL(const CachingAddNLL &other, const char *name = 0) ;
00076         virtual ~CachingAddNLL() ;
00077         virtual CachingAddNLL *clone(const char *name = 0) const ;
00078         virtual Double_t evaluate() const ;
00079         virtual Bool_t isDerived() const { return kTRUE; }
00080         virtual Double_t defaultErrorLevel() const { return 0.5; }
00081         void setData(const RooAbsData &data) ;
00082         virtual RooArgSet* getObservables(const RooArgSet* depList, Bool_t valueOnly = kTRUE) const ;
00083         virtual RooArgSet* getParameters(const RooArgSet* depList, Bool_t stripDisconnected = kTRUE) const ;
00084         double  sumWeights() const { return sumWeights_; }
00085         const RooAbsPdf *pdf() const { return pdf_; }
00086         void setZeroPoint() { zeroPoint_ = -this->getVal(); setValueDirty(); }
00087         void clearZeroPoint() { zeroPoint_ = 0.0; setValueDirty();  }
00088         RooSetProxy & params() { return params_; }
00089     private:
00090         void setup_();
00091         RooAbsPdf *pdf_;
00092         RooSetProxy params_;
00093         const RooAbsData *data_;
00094         std::vector<Double_t>  weights_;
00095         double               sumWeights_;
00096         mutable std::vector<RooAbsReal*> coeffs_;
00097         mutable std::vector<CachingPdf>  pdfs_;
00098         mutable std::vector<RooAbsReal*> integrals_;
00099         mutable std::vector<Double_t> partialSum_;
00100         mutable bool isRooRealSum_;
00101         double zeroPoint_;
00102 };
00103 
00104 class CachingSimNLL  : public RooAbsReal {
00105     public:
00106         CachingSimNLL(RooSimultaneous *pdf, RooAbsData *data, const RooArgSet *nuis=0) ;
00107         CachingSimNLL(const CachingSimNLL &other, const char *name = 0) ;
00108         ~CachingSimNLL() ;
00109         virtual CachingSimNLL *clone(const char *name = 0) const ;
00110         virtual Double_t evaluate() const ;
00111         virtual Bool_t isDerived() const { return kTRUE; }
00112         virtual Double_t defaultErrorLevel() const { return 0.5; }
00113         void setData(const RooAbsData &data) ;
00114         virtual RooArgSet* getObservables(const RooArgSet* depList, Bool_t valueOnly = kTRUE) const ;
00115         virtual RooArgSet* getParameters(const RooArgSet* depList, Bool_t stripDisconnected = kTRUE) const ;
00116         void splitWithWeights(const RooAbsData &data, const RooAbsCategory& splitCat, Bool_t createEmptyDataSets) ;
00117         static void setNoDeepLogEvalError(bool noDeep) { noDeepLEE_ = noDeep; }
00118         void setZeroPoint() ; 
00119         void clearZeroPoint() ;
00120         friend class CachingAddNLL;
00121     private:
00122         class SimpleGaussianConstraint : public RooGaussian {
00123             public:
00124                 SimpleGaussianConstraint(const RooGaussian &g) : RooGaussian(g, "") {}
00125                 double getLogValFast() const { 
00126                     Double_t arg = x - mean;  
00127                     Double_t sig = sigma ;
00128                     return -0.5*arg*arg/(sig*sig);
00129                 }
00130         };
00131 
00132         void setup_();
00133         RooSimultaneous   *pdfOriginal_;
00134         const RooAbsData  *dataOriginal_;
00135         const RooArgSet   *nuis_;
00136         RooSetProxy        params_;
00137         RooArgSet piecesForCloning_;
00138         std::auto_ptr<RooSimultaneous>  factorizedPdf_;
00139         std::vector<RooAbsPdf *>        constrainPdfs_;
00140         std::vector<SimpleGaussianConstraint *>  constrainPdfsFast_;
00141         std::vector<CachingAddNLL*>     pdfs_;
00142         std::auto_ptr<TList>            dataSets_;
00143         std::vector<RooDataSet *>       datasets_;
00144         static bool noDeepLEE_;
00145         static bool hasError_;
00146         std::vector<double> constrainZeroPoints_;
00147         std::vector<double> constrainZeroPointsFast_;
00148 };
00149 
00150 }
00151 #endif