Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
RooDataHist.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * Project: RooFit *
3  * Package: RooFitCore *
4  * File: $Id: RooDataHist.h,v 1.37 2007/05/11 09:11:30 verkerke Exp $
5  * Authors: *
6  * WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu *
7  * DK, David Kirkby, UC Irvine, dkirkby@uci.edu *
8  * *
9  * Copyright (c) 2000-2005, Regents of the University of California *
10  * and Stanford University. All rights reserved. *
11  * *
12  * Redistribution and use in source and binary forms, *
13  * with or without modification, are permitted according to the terms *
14  * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
15  *****************************************************************************/
16 #ifndef ROO_DATA_HIST
17 #define ROO_DATA_HIST
18 
19 #include "RooAbsData.h"
20 #include "RooDirItem.h"
21 #include "RooArgSet.h"
22 #include "RooNameSet.h"
23 #include "RooCacheManager.h"
24 
25 #include <map>
26 #include <vector>
27 #include <string>
28 #include <utility>
29 
30 class TObject ;
31 class RooAbsArg;
32 class RooAbsReal ;
33 class RooAbsCategory ;
34 class Roo1DTable ;
35 class RooPlot;
36 class RooArgSet ;
37 class RooLinkedList ;
38 class RooAbsLValue ;
39 
40 class RooDataHist : public RooAbsData, public RooDirItem {
41 public:
42 
43  // Constructors, factory methods etc.
44  RooDataHist() ;
45  RooDataHist(const char *name, const char *title, const RooArgSet& vars, const char* binningName=0) ;
46  RooDataHist(const char *name, const char *title, const RooArgSet& vars, const RooAbsData& data, Double_t initWgt=1.0) ;
47  RooDataHist(const char *name, const char *title, const RooArgList& vars, const TH1* hist, Double_t initWgt=1.0) ;
48  RooDataHist(const char *name, const char *title, const RooArgList& vars, RooCategory& indexCat, std::map<std::string,TH1*> histMap, Double_t initWgt=1.0) ;
49  RooDataHist(const char *name, const char *title, const RooArgList& vars, RooCategory& indexCat, std::map<std::string,RooDataHist*> dhistMap, Double_t wgt=1.0) ;
50  //RooDataHist(const char *name, const char *title, const RooArgList& vars, Double_t initWgt=1.0) ;
51  RooDataHist(const char *name, const char *title, const RooArgList& vars, const RooCmdArg& arg1, const RooCmdArg& arg2=RooCmdArg(), const RooCmdArg& arg3=RooCmdArg(),
52  const RooCmdArg& arg4=RooCmdArg(),const RooCmdArg& arg5=RooCmdArg(),const RooCmdArg& arg6=RooCmdArg(),const RooCmdArg& arg7=RooCmdArg(),const RooCmdArg& arg8=RooCmdArg()) ;
53  RooDataHist& operator=(const RooDataHist&) = delete;
54 
55  RooDataHist(const RooDataHist& other, const char* newname = 0) ;
56  virtual TObject* Clone(const char* newname="") const {
57  return new RooDataHist(*this, newname && newname[0] != '\0' ? newname : GetName());
58  }
59  virtual ~RooDataHist() ;
60 
61  virtual RooAbsData* emptyClone(const char* newName=0, const char* newTitle=0, const RooArgSet*vars=0, const char* /*wgtVarName*/=0) const {
62  // Return empty clone of this RooDataHist
63  return new RooDataHist(newName?newName:GetName(),newTitle?newTitle:GetTitle(),vars?*vars:*get()) ;
64  }
65 
66  // Add one ore more rows of data
67  virtual void add(const RooArgSet& row, Double_t wgt=1.0) {
68  // Increment weight of bin enclosing coordinate stored in row by wgt
69  add(row,wgt,-1.) ;
70  }
71  virtual void add(const RooArgSet& row, Double_t weight, Double_t sumw2) ;
72  void set(Double_t weight, Double_t wgtErr=-1) ;
73  void set(const RooArgSet& row, Double_t weight, Double_t wgtErr=-1) ;
74  void set(const RooArgSet& row, Double_t weight, Double_t wgtErrLo, Double_t wgtErrHi) ;
75 
76  void add(const RooAbsData& dset, const RooFormulaVar* cutVar=0, Double_t weight=1.0 ) ;
77  void add(const RooAbsData& dset, const char* cut, Double_t weight=1.0 ) ;
78 
79  virtual const RooArgSet* get() const {
80  // Return set with coordinates of center of current bin
81  return &_vars ;
82  }
83  virtual const RooArgSet* get(Int_t masterIdx) const ;
84  virtual const RooArgSet* get(const RooArgSet& coord) const ;
85  virtual Int_t numEntries() const ;
86  virtual Double_t sumEntries() const ;
87  virtual Double_t sumEntries(const char* cutSpec, const char* cutRange=0) const ;
88  virtual Bool_t isWeighted() const {
89  // Return true as all histograms have in principle events weight != 1
90  return kTRUE ;
91  }
92  virtual Bool_t isNonPoissonWeighted() const ;
93 
94  virtual RooSpan<const double> getWeightBatch(std::size_t, std::size_t) const {
95  //TODO
96  std::cerr << "Retrieving weights in batches not yet implemented for RooDataHist." << std::endl;
97  assert(false);
98 
99  return {};
100  }
101 
102  Double_t sum(Bool_t correctForBinSize, Bool_t inverseCorr=kFALSE) const ;
103  Double_t sum(const RooArgSet& sumSet, const RooArgSet& sliceSet, Bool_t correctForBinSize, Bool_t inverseCorr=kFALSE) ;
104  Double_t sum(const RooArgSet& sumSet, const RooArgSet& sliceSet, Bool_t correctForBinSize, Bool_t inverseCorr, const std::map<const RooAbsArg*, std::pair<Double_t, Double_t> >& ranges);
105 
106  virtual Double_t weight() const {
107  // Return weight of current bin
108  return get_curWeight();
109  }
110  Double_t weightSquared() const ;
111  Double_t weight(const RooArgSet& bin, Int_t intOrder=1, Bool_t correctForBinSize=kFALSE, Bool_t cdfBoundaries=kFALSE, Bool_t oneSafe=kFALSE) ;
112  Double_t binVolume() const { return _curVolume ; }
113  Double_t binVolume(const RooArgSet& bin) ;
114  virtual Bool_t valid() const ;
115 
116  TIterator* sliceIterator(RooAbsArg& sliceArg, const RooArgSet& otherArgs) ;
117 
118  virtual void weightError(Double_t& lo, Double_t& hi, ErrorType etype=Poisson) const ;
119  virtual Double_t weightError(ErrorType etype=Poisson) const {
120  // Return symmetric error on current bin calculated either from Poisson statistics or from SumOfWeights
121  Double_t lo,hi ;
122  weightError(lo,hi,etype) ;
123  return (lo+hi)/2 ;
124  }
125 
126  using RooAbsData::plotOn ;
127  virtual RooPlot *plotOn(RooPlot *frame, PlotOpt o) const;
128 
129  virtual void reset() ;
130  void dump2() ;
131 
132  virtual void printMultiline(std::ostream& os, Int_t content, Bool_t verbose=kFALSE, TString indent="") const ;
133  virtual void printArgs(std::ostream& os) const ;
134  virtual void printValue(std::ostream& os) const ;
135 
136  void SetName(const char *name) ;
137  void SetNameTitle(const char *name, const char* title) ;
138 
139  Int_t getIndex(const RooArgSet& coord, Bool_t fast=kFALSE) ;
140 
141  void removeSelfFromDir() { removeFromDir(this) ; }
142 
143 protected:
144 
145  friend class RooAbsCachedPdf ;
146  friend class RooAbsCachedReal ;
147  friend class RooDataHistSliceIter ;
148  friend class RooAbsOptTestStatistic ;
149 
150  Int_t calcTreeIndex() const ;
151  void cacheValidEntries() ;
152 
153  void setAllWeights(Double_t value) ;
154 
155  void initialize(const char* binningName=0,Bool_t fillTree=kTRUE) ;
156  RooDataHist(const char* name, const char* title, RooDataHist* h, const RooArgSet& varSubset,
157  const RooFormulaVar* cutVar, const char* cutRange, Int_t nStart, Int_t nStop, Bool_t copyCache) ;
158  RooAbsData* reduceEng(const RooArgSet& varSubset, const RooFormulaVar* cutVar, const char* cutRange=0,
159  Int_t nStart=0, Int_t nStop=2000000000, Bool_t copyCache=kTRUE) ;
160  Double_t interpolateDim(RooRealVar& dim, const RooAbsBinning* binning, Double_t xval, Int_t intOrder, Bool_t correctForBinSize, Bool_t cdfBoundaries) ;
161  void calculatePartialBinVolume(const RooArgSet& dimSet) const ;
162  void checkBinBounds() const;
163 
164  void adjustBinning(const RooArgList& vars, const TH1& href, Int_t* offset=0) ;
165  void importTH1(const RooArgList& vars, const TH1& histo, Double_t initWgt, Bool_t doDensityCorrection) ;
166  void importTH1Set(const RooArgList& vars, RooCategory& indexCat, std::map<std::string,TH1*> hmap, Double_t initWgt, Bool_t doDensityCorrection) ;
167  void importDHistSet(const RooArgList& vars, RooCategory& indexCat, std::map<std::string,RooDataHist*> dmap, Double_t initWgt) ;
168 
169  virtual RooAbsData* cacheClone(const RooAbsArg* newCacheOwner, const RooArgSet* newCacheVars, const char* newName=0) ;
170 
171  Double_t get_wgt(const Int_t &idx) const { return _wgt[idx]; }
172  Double_t get_errLo(const Int_t &idx) const { return _errLo[idx]; }
173  Double_t get_errHi(const Int_t &idx) const { return _errHi[idx]; }
174  Double_t get_sumw2(const Int_t &idx) const { return _sumw2[idx]; }
175 
176  Double_t get_curWeight() const { return _curWeight; }
177  Double_t get_curWgtErrLo() const { return _curWgtErrLo; }
178  Double_t get_curWgtErrHi() const { return _curWgtErrHi; }
179  Double_t get_curSumW2() const { return _curSumW2; }
180 
181  Int_t get_curIndex() const { return _curIndex; }
182 
183  Int_t _arrSize ; // Size of the weight array
184  std::vector<Int_t> _idxMult ; // Multiplier jump table for index calculation
185 
186  Double_t* _wgt ; //[_arrSize] Weight array
187  Double_t* _errLo ; //[_arrSize] Low-side error on weight array
188  Double_t* _errHi ; //[_arrSize] High-side error on weight array
189  Double_t* _sumw2 ; //[_arrSize] Sum of weights^2
190  Double_t* _binv ; //[_arrSize] Bin volume array
191 
192  RooArgSet _realVars ; // Real dimensions of the dataset
193  Bool_t* _binValid ; //! Valid bins with current range definition
194 
195  mutable Double_t _curWeight{0.}; // Weight associated with the current coordinate
196  mutable Double_t _curWgtErrLo{0.}; // Error on weight associated with the current coordinate
197  mutable Double_t _curWgtErrHi{0.}; // Error on weight associated with the current coordinate
198  mutable Double_t _curSumW2{0.}; // Current sum of weights^2
199  mutable Double_t _curVolume{0.}; // Volume of bin enclosing current coordinate
200  mutable Int_t _curIndex{0}; // Current index
201 
202  mutable std::vector<Double_t>* _pbinv ; //! Partial bin volume array
203  mutable RooCacheManager<std::vector<Double_t> > _pbinvCacheMgr ; //! Cache manager for arrays of partial bin volumes
204  std::vector<RooAbsLValue*> _lvvars ; //! List of observables casted as RooAbsLValue
205  std::vector<const RooAbsBinning*> _lvbins ; //! List of used binnings associated with lvalues
206  mutable std::vector<std::vector<Double_t> > _binbounds; //! list of bin bounds per dimension
207 
208  mutable Int_t _cache_sum_valid{0}; //! Is cache sum valid
209  mutable Double_t _cache_sum{0.}; //! Cache for sum of entries ;
210 
211 
212 private:
213  void _adjustBinning(RooRealVar &theirVar, const TAxis &axis, RooRealVar *ourVar, Int_t *offset);
214 
215  ClassDef(RooDataHist,4) // Binned data set
216 };
217 
218 #endif
219