Back to home page

EIC code displayed by LXR

 
 

    


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

0001 /*****************************************************************************
0002  * Project: RooFit                                                           *
0003  * Package: RooFitCore                                                       *
0004  *    File: $Id$
0005  * Authors:                                                                  *
0006  *   WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu       *
0007  *   DK, David Kirkby,    UC Irvine,         dkirkby@uci.edu                 *
0008  *                                                                           *
0009  * Copyright (c) 2000-2005, Regents of the University of California          *
0010  *                          and Stanford University. All rights reserved.    *
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 #ifndef ROO_TREE_DATA_STORE
0017 #define ROO_TREE_DATA_STORE
0018 
0019 #include "RooAbsDataStore.h"
0020 
0021 #include <string_view>
0022 
0023 #include <vector>
0024 #include <list>
0025 #include <string>
0026 
0027 class RooAbsArg ;
0028 class RooArgList ;
0029 class TTree ;
0030 class RooFormulaVar ;
0031 class RooArgSet ;
0032 
0033 
0034 class RooTreeDataStore : public RooAbsDataStore {
0035 public:
0036 
0037   RooTreeDataStore() ;
0038   RooTreeDataStore(TTree* t, const RooArgSet& vars, const char* wgtVarName=nullptr) ;
0039 
0040   // Empty constructor
0041   RooTreeDataStore(RooStringView name, RooStringView title, const RooArgSet& vars, const char* wgtVarName=nullptr) ;
0042   RooAbsDataStore* clone(const char* newname=nullptr) const override { return new RooTreeDataStore(*this,newname) ; }
0043   RooAbsDataStore* clone(const RooArgSet& vars, const char* newname=nullptr) const override { return new RooTreeDataStore(*this,vars,newname) ; }
0044 
0045   std::unique_ptr<RooAbsDataStore> reduce(RooStringView name, RooStringView title,
0046                           const RooArgSet& vars, const RooFormulaVar* cutVar, const char* cutRange,
0047                           std::size_t nStart, std::size_t nStop) override;
0048 
0049   // Constructor from TTree
0050   RooTreeDataStore(RooStringView name, RooStringView title, const RooArgSet& vars, TTree& t, const char* selExpr=nullptr, const char* wgtVarName=nullptr) ;
0051 
0052   // Constructor from DataStore
0053   RooTreeDataStore(RooStringView name, RooStringView title, const RooArgSet& vars, const RooAbsDataStore& tds, const char* selExpr=nullptr, const char* wgtVarName=nullptr) ;
0054 
0055   RooTreeDataStore(RooStringView name, RooStringView title, RooAbsDataStore& tds,
0056                    const RooArgSet& vars, const RooFormulaVar* cutVar, const char* cutRange,
0057                    Int_t nStart, Int_t nStop, const char* wgtVarName=nullptr) ;
0058 
0059   RooTreeDataStore(const RooTreeDataStore& other, const char* newname=nullptr) ;
0060   RooTreeDataStore(const RooTreeDataStore& other, const RooArgSet& vars, const char* newname=nullptr) ;
0061   ~RooTreeDataStore() override ;
0062 
0063 
0064   // Write current row
0065   Int_t fill() override ;
0066 
0067   // Retrieve a row
0068   using RooAbsDataStore::get ;
0069   const RooArgSet* get(Int_t index) const override ;
0070   using RooAbsDataStore::weight ;
0071   double weight() const override ;
0072   double weightError(RooAbsData::ErrorType etype=RooAbsData::Poisson) const override ;
0073   void weightError(double& lo, double& hi, RooAbsData::ErrorType etype=RooAbsData::Poisson) const override ;
0074   bool isWeighted() const override { return (_wgtVar!=nullptr||_extWgtArray!=nullptr) ; }
0075 
0076   RooAbsData::RealSpans getBatches(std::size_t first, std::size_t len) const override {
0077     //TODO
0078     std::cerr << "This functionality is not yet implemented for tree data stores." << std::endl;
0079     throw std::logic_error("getBatches() not implemented in RooTreeDataStore.");
0080     (void)first; (void)len;
0081     return {};
0082   }
0083   std::span<const double> getWeightBatch(std::size_t first, std::size_t len) const override;
0084 
0085   // Change observable name
0086   bool changeObservableName(const char* from, const char* to) override ;
0087 
0088   // Add one column
0089   RooAbsArg* addColumn(RooAbsArg& var, bool adjustRange=true) override ;
0090 
0091   // Merge column-wise
0092   RooAbsDataStore* merge(const RooArgSet& allvars, std::list<RooAbsDataStore*> dstoreList) override ;
0093 
0094   // Add rows
0095   void append(RooAbsDataStore& other) override ;
0096 
0097   // General & bookkeeping methods
0098   double sumEntries() const override ;
0099   Int_t numEntries() const override ;
0100   void reset() override ;
0101 
0102   // Buffer redirection routines used in inside RooAbsOptTestStatistics
0103   void attachBuffers(const RooArgSet& extObs) override ;
0104   void resetBuffers() override ;
0105   void restoreAlternateBuffers() ;
0106 
0107   // Tree access
0108   TTree& tree() { return *_tree ; }
0109   const TTree* tree() const override { return _tree ; }
0110 
0111   // Forwarded from TTree
0112   Stat_t GetEntries() const;
0113   void Reset(Option_t* option=nullptr);
0114   Int_t Fill();
0115   Int_t GetEntry(Int_t entry = 0, Int_t getall = 0);
0116 
0117   void   Draw(Option_t* option = "") override ;
0118 
0119   // Constant term  optimizer interface
0120   void cacheArgs(const RooAbsArg* owner, RooArgSet& varSet, const RooArgSet* nset=nullptr, bool skipZeroWeights=false) override ;
0121   const RooAbsArg* cacheOwner() override { return _cacheOwner ; }
0122   void setArgStatus(const RooArgSet& set, bool active) override ;
0123   void resetCache() override ;
0124 
0125   void loadValues(const TTree *t, const RooFormulaVar* select=nullptr, const char* rangeName=nullptr, Int_t nStart=0, Int_t nStop=2000000000)  ;
0126   void loadValues(const RooAbsDataStore *tds, const RooFormulaVar* select=nullptr, const char* rangeName=nullptr,
0127       std::size_t nStart=0, std::size_t nStop = std::numeric_limits<std::size_t>::max()) override;
0128 
0129   void checkInit() const override;
0130 
0131   void setExternalWeightArray(const double* arrayWgt, const double* arrayWgtErrLo,
0132       const double* arrayWgtErrHi, const double* arraySumW2) override {
0133     _extWgtArray = arrayWgt ;
0134     _extWgtErrLoArray = arrayWgtErrLo ;
0135     _extWgtErrHiArray = arrayWgtErrHi ;
0136     _extSumW2Array = arraySumW2 ;
0137   }
0138 
0139   const RooArgSet& row() { return _varsww ; }
0140 
0141  private:
0142 
0143   friend class RooVectorDataStore ;
0144 
0145   RooArgSet varsNoWeight(const RooArgSet& allVars, const char* wgtName=nullptr) ;
0146   RooRealVar* weightVar(const RooArgSet& allVars, const char* wgtName=nullptr) ;
0147 
0148   void initialize();
0149   void attachCache(const RooAbsArg* newOwner, const RooArgSet& cachedVars) override ;
0150 
0151   // TTree Branch buffer size control
0152   void setBranchBufferSize(Int_t size) { _defTreeBufSize = size ; }
0153   Int_t getBranchBufferSize() const { return _defTreeBufSize ; }
0154 
0155   std::string makeTreeName() const;
0156 
0157   static Int_t _defTreeBufSize ;
0158 
0159   void createTree(RooStringView name, RooStringView title) ;
0160   TTree *_tree = nullptr;                 // TTree holding the data points
0161   TTree *_cacheTree = nullptr;            //! TTree holding the cached function values
0162   const RooAbsArg* _cacheOwner = nullptr; //! Object owning cache contents
0163   mutable bool _defCtor = false;        //! Was object constructed with default ctor?
0164 
0165   RooArgSet _varsww ;
0166   RooRealVar* _wgtVar = nullptr;     // Pointer to weight variable (if set)
0167 
0168   const double* _extWgtArray{nullptr};         ///<! External weight array
0169   const double* _extWgtErrLoArray{nullptr};    ///<! External weight array - low error
0170   const double* _extWgtErrHiArray{nullptr};    ///<! External weight array - high error
0171   const double* _extSumW2Array{nullptr};       ///<! External sum of weights array
0172   mutable std::unique_ptr<std::vector<double>> _weightBuffer; //! Buffer for weights in case a batch of values is requested.
0173 
0174   mutable double  _curWgt = 1.0;      ///< Weight of current event
0175   mutable double  _curWgtErrLo = 0.0; ///< Weight of current event
0176   mutable double  _curWgtErrHi = 0.0; ///< Weight of current event
0177   mutable double  _curWgtErr = 0.0;   ///< Weight of current event
0178 
0179   RooArgSet _attachedBuffers ; ///<! Currently attached buffers (if different from _varsww)
0180 
0181   ClassDefOverride(RooTreeDataStore, 2) // TTree-based Data Storage class
0182 };
0183 
0184 
0185 #endif