CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
TH1Keys.cc
Go to the documentation of this file.
1 #include "../interface/TH1Keys.h"
2 #include <RooBinning.h>
3 #include <RooMsgService.h>
4 
5 #include <stdexcept>
6 #include <vector>
7 
9  x_(0),
10  dataset_(0),
11  underflow_(0.0), overflow_(0.0),
12  globalScale_(1.0),
13  cache_(0),
14  isCacheGood_(false)
15 {
16 }
17 
18 TH1Keys::TH1Keys(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_t xup, TString options, Double_t rho) :
19  TH1(name,title,nbinsx,xlow,xup),
20  min_(xlow), max_(xup),
21  x_(new RooRealVar("x", "x", min_, max_)),
22  w_(new RooRealVar("w", "w", 1.0)),
23  point_(*x_),
24  dataset_(new RooDataSet(name, title, RooArgSet(*x_, *w_), "w")),
25  underflow_(0.0), overflow_(0.0),
26  globalScale_(1.0),
27  options_(options),
28  rho_(rho),
29  cache_(new TH1F("",title,nbinsx,xlow,xup)),
30  isCacheGood_(true)
31 {
32  cache_->SetDirectory(0);
33  fDimension = 1;
34  x_->setBins(nbinsx);
35 }
36 
37 TH1Keys::TH1Keys(const char *name,const char *title,Int_t nbinsx,const Float_t *xbins, TString options, Double_t rho) :
38  TH1(name,title,nbinsx,xbins),
39  min_(xbins[0]), max_(xbins[nbinsx]),
40  x_(new RooRealVar("x", "x", min_, max_)),
41  w_(new RooRealVar("w", "w", 1.0)),
42  point_(*x_),
43  dataset_(new RooDataSet(name, title, RooArgSet(*x_, *w_), "w")),
44  underflow_(0.0), overflow_(0.0),
45  globalScale_(1.0),
46  options_(options),
47  rho_(rho),
48  cache_(new TH1F("",title,nbinsx,xbins)),
49  isCacheGood_(true)
50 {
51  cache_->SetDirectory(0);
52  fDimension = 1;
53  std::vector<Double_t> boundaries(nbinsx+1);
54  for (Int_t i = 0; i <= nbinsx; ++i) boundaries[i] = xbins[i];
55  x_->setBinning(RooBinning(nbinsx, &boundaries[0]));
56 }
57 
58 TH1Keys::TH1Keys(const char *name,const char *title,Int_t nbinsx,const Double_t *xbins, TString options, Double_t rho) :
59  TH1(name,title,nbinsx,xbins),
60  min_(xbins[0]), max_(xbins[nbinsx]),
61  x_(new RooRealVar("x", "x", min_, max_)),
62  w_(new RooRealVar("w", "w", 1.0)),
63  point_(*x_),
64  dataset_(new RooDataSet(name, title, RooArgSet(*x_, *w_), "w")),
65  underflow_(0.0), overflow_(0.0),
66  globalScale_(1.0),
67  options_(options),
68  rho_(rho),
69  cache_(new TH1F("",title,nbinsx,xbins)),
70  isCacheGood_(true)
71 {
72  cache_->SetDirectory(0);
73  fDimension = 1;
74  x_->setBinning(RooBinning(nbinsx, xbins));
75 }
76 
77 
78 TH1Keys::TH1Keys(const TH1Keys &other) :
79  TH1(other),
80  min_(other.min_), max_(other.max_),
81  x_(new RooRealVar("x", "x", min_, max_)),
82  w_(new RooRealVar("w", "w", 1.0)),
83  point_(*x_),
84  dataset_(new RooDataSet(other.GetName(), other.GetTitle(), RooArgSet(*x_, *w_), "w")),
85  underflow_(other.underflow_), overflow_(other.overflow_),
86  globalScale_(other.globalScale_),
87  options_(other.options_),
88  rho_(other.rho_),
89  cache_((TH1*)other.cache_->Clone()),
90  isCacheGood_(other.isCacheGood_)
91 {
92  fDimension = 1;
93  x_->setBinning(other.x_->getBinning());
94 }
95 
96 
98 {
99  delete cache_;
100  delete dataset_;
101  delete x_;
102 }
103 
104 Int_t TH1Keys::Fill(Double_t x, Double_t w)
105 {
106  isCacheGood_ = false;
107  if (x >= max_) overflow_ += w;
108  else if (x < min_) underflow_ += w;
109  else {
110  x_->setVal(x);
111  dataset_->add(point_, w);
112  return 1;
113  }
114  return -1;
115 }
116 
117 void TH1Keys::FillN(Int_t ntimes, const Double_t *x, const Double_t *w, Int_t stride)
118 {
119  isCacheGood_ = false;
120  for (Int_t i = 0; i < ntimes; i += stride) {
121  Fill(x[i], w[i]);
122  }
123 }
124 
125 void TH1Keys::Add(const TH1 *h1, Double_t c1)
126 {
127  if (c1 != 1.0) dont("Add with constant != 1");
128  const TH1Keys *other = dynamic_cast<const TH1Keys *>(h1);
129  if (other == 0) dont("Add with a non TH1Keys");
130  dataset_->append(const_cast<RooDataSet&>(*other->dataset_));
131  isCacheGood_ = false;
132 }
133 
134 void TH1Keys::Scale(Double_t c1, Option_t *option)
135 {
136  globalScale_ *= c1;
137  if (cache_) cache_->Scale(c1);
138 }
139 
140 void TH1Keys::Reset(Option_t *option) {
141  dataset_->reset();
142  overflow_ = underflow_ = 0.0;
143  globalScale_ = 1.0;
144  cache_->Reset();
145  isCacheGood_ = true;
146 }
147 
148 // ------------------------------------------------------------
149 
150 void TH1Keys::FillH1() const
151 {
152  if (dataset_->numEntries() == 0) {
153  cache_->Reset(); // make sure it's empty
154  } else {
155  RooFit::MsgLevel gKill = RooMsgService::instance().globalKillBelow();
156  RooMsgService::instance().setGlobalKillBelow(RooFit::FATAL);
157  delete cache_;
158  RooNDKeysPdf pdf("","",*x_,*dataset_,options_,rho_);
159  cache_ = pdf.createHistogram(GetName(), *x_);
160  if (cache_->Integral()) cache_->Scale(1.0/cache_->Integral());
161  cache_->SetBinContent(0, underflow_);
162  cache_->SetBinContent(cache_->GetNbinsX()+1, overflow_);
163  cache_->Scale(dataset_->sumEntries() * globalScale_);
164  RooMsgService::instance().setGlobalKillBelow(gKill);
165  }
166  isCacheGood_ = true;
167 }
168 
169 void TH1Keys::dont(const char *msg) const {
170  TObject::Error("TH1Keys",msg);
171  throw std::runtime_error(std::string("Error in TH1Keys: ")+msg);
172 }
int i
Definition: DBlmapReader.cc:9
TH1 * cache_
Definition: TH1Keys.h:61
virtual Int_t Fill(Double_t x)
Definition: TH1Keys.h:22
const double xbins[]
RooDataSet * dataset_
Definition: TH1Keys.h:54
static PFTauRenderPlugin instance
Definition: DDAxes.h:10
RooArgSet point_
Definition: TH1Keys.h:53
virtual void FillN(Int_t ntimes, const Double_t *x, const Double_t *w, Int_t stride=1)
Definition: TH1Keys.cc:117
virtual void Reset(Option_t *option="")
Definition: TH1Keys.cc:140
bool isCacheGood_
Definition: TH1Keys.h:62
void dont(const char *) const
Definition: TH1Keys.cc:169
Double_t underflow_
Definition: TH1Keys.h:55
virtual void Add(const TH1 *h1, Double_t c1=1)
Definition: TH1Keys.cc:125
Double_t rho_
Definition: TH1Keys.h:59
virtual ~TH1Keys()
Definition: TH1Keys.cc:97
Double_t overflow_
Definition: TH1Keys.h:55
virtual void Scale(Double_t c1=1, Option_t *option="")
Definition: TH1Keys.cc:134
Definition: DDAxes.h:10
TH1Keys()
Definition: TH1Keys.cc:8
void FillH1() const
Definition: TH1Keys.cc:150
ClassDef(TH1Keys, 1) private RooRealVar * x_
Definition: TH1Keys.h:48
T w() const
TString options_
Definition: TH1Keys.h:58
Double_t globalScale_
Definition: TH1Keys.h:56