CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10_patch1/src/HiggsAnalysis/CombinedLimit/src/TH1Keys.cc

Go to the documentation of this file.
00001 #include "../interface/TH1Keys.h"
00002 #include <RooBinning.h>
00003 #include <RooMsgService.h>
00004 
00005 #include <stdexcept>
00006 #include <vector>
00007 
00008 TH1Keys::TH1Keys() :
00009     x_(0),
00010     dataset_(0),
00011     underflow_(0.0), overflow_(0.0),
00012     globalScale_(1.0),
00013     cache_(0),
00014     isCacheGood_(false)
00015 {
00016 }
00017 
00018 TH1Keys::TH1Keys(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_t xup, TString options, Double_t rho) :
00019     TH1(name,title,nbinsx,xlow,xup),
00020     min_(xlow), max_(xup),
00021     x_(new RooRealVar("x", "x", min_, max_)),
00022     w_(new RooRealVar("w", "w", 1.0)),
00023     point_(*x_),
00024     dataset_(new RooDataSet(name, title, RooArgSet(*x_, *w_), "w")),
00025     underflow_(0.0), overflow_(0.0),
00026     globalScale_(1.0),
00027     options_(options),
00028     rho_(rho),
00029     cache_(new TH1F("",title,nbinsx,xlow,xup)),
00030     isCacheGood_(true)
00031 {
00032     cache_->SetDirectory(0);
00033     fDimension = 1;
00034     x_->setBins(nbinsx);
00035 }
00036 
00037 TH1Keys::TH1Keys(const char *name,const char *title,Int_t nbinsx,const Float_t  *xbins, TString options, Double_t rho) :
00038     TH1(name,title,nbinsx,xbins),
00039     min_(xbins[0]), max_(xbins[nbinsx]),
00040     x_(new RooRealVar("x", "x", min_, max_)),
00041     w_(new RooRealVar("w", "w", 1.0)),
00042     point_(*x_),
00043     dataset_(new RooDataSet(name, title, RooArgSet(*x_, *w_), "w")),
00044     underflow_(0.0), overflow_(0.0),
00045     globalScale_(1.0),
00046     options_(options),
00047     rho_(rho),
00048     cache_(new TH1F("",title,nbinsx,xbins)),
00049     isCacheGood_(true)
00050 {
00051     cache_->SetDirectory(0);
00052     fDimension = 1;
00053     std::vector<Double_t> boundaries(nbinsx+1);
00054     for (Int_t i = 0; i <= nbinsx; ++i) boundaries[i] = xbins[i];
00055     x_->setBinning(RooBinning(nbinsx, &boundaries[0]));
00056 }
00057 
00058 TH1Keys::TH1Keys(const char *name,const char *title,Int_t nbinsx,const Double_t *xbins, TString options, Double_t rho) :
00059     TH1(name,title,nbinsx,xbins),
00060     min_(xbins[0]), max_(xbins[nbinsx]),
00061     x_(new RooRealVar("x", "x", min_, max_)),
00062     w_(new RooRealVar("w", "w", 1.0)),
00063     point_(*x_),
00064     dataset_(new RooDataSet(name, title, RooArgSet(*x_, *w_), "w")),
00065     underflow_(0.0), overflow_(0.0),
00066     globalScale_(1.0),
00067     options_(options),
00068     rho_(rho),
00069     cache_(new TH1F("",title,nbinsx,xbins)),
00070     isCacheGood_(true)
00071 {
00072     cache_->SetDirectory(0);
00073     fDimension = 1;
00074     x_->setBinning(RooBinning(nbinsx, xbins));
00075 }
00076 
00077 
00078 TH1Keys::TH1Keys(const TH1Keys &other)  :
00079     TH1(other),
00080     min_(other.min_), max_(other.max_),
00081     x_(new RooRealVar("x", "x", min_, max_)),
00082     w_(new RooRealVar("w", "w", 1.0)),
00083     point_(*x_),
00084     dataset_(new RooDataSet(other.GetName(), other.GetTitle(), RooArgSet(*x_, *w_), "w")),
00085     underflow_(other.underflow_), overflow_(other.overflow_),
00086     globalScale_(other.globalScale_),
00087     options_(other.options_),
00088     rho_(other.rho_),
00089     cache_((TH1*)other.cache_->Clone()),
00090     isCacheGood_(other.isCacheGood_)
00091 {
00092     fDimension = 1;
00093     x_->setBinning(other.x_->getBinning());
00094 }
00095 
00096 
00097 TH1Keys::~TH1Keys() 
00098 {
00099     delete cache_;
00100     delete dataset_;
00101     delete x_;
00102 }
00103 
00104 Int_t TH1Keys::Fill(Double_t x, Double_t w)
00105 {
00106     isCacheGood_ = false;
00107     if (x >= max_) overflow_ += w;
00108     else if (x < min_) underflow_ += w;
00109     else {
00110         x_->setVal(x);
00111         dataset_->add(point_, w);
00112         return 1;
00113     } 
00114     return -1;
00115 }
00116 
00117 void TH1Keys::FillN(Int_t ntimes, const Double_t *x, const Double_t *w, Int_t stride)
00118 {
00119     isCacheGood_ = false;
00120     for (Int_t i = 0; i < ntimes; i += stride) {
00121         Fill(x[i], w[i]);
00122     }
00123 }
00124 
00125 void TH1Keys::Add(const TH1 *h1, Double_t c1) 
00126 {
00127     if (c1 != 1.0) dont("Add with constant != 1");
00128     const TH1Keys *other = dynamic_cast<const TH1Keys *>(h1);
00129     if (other == 0) dont("Add with a non TH1Keys");
00130     dataset_->append(const_cast<RooDataSet&>(*other->dataset_));
00131     isCacheGood_ = false;
00132 }
00133 
00134 void TH1Keys::Scale(Double_t c1, Option_t *option)
00135 {
00136     globalScale_ *= c1;
00137     if (cache_) cache_->Scale(c1);
00138 }
00139 
00140 void TH1Keys::Reset(Option_t *option) {
00141     dataset_->reset();
00142     overflow_ = underflow_ = 0.0;
00143     globalScale_ = 1.0;
00144     cache_->Reset();
00145     isCacheGood_ = true;
00146 }
00147 
00148 // ------------------------------------------------------------
00149 
00150 void TH1Keys::FillH1() const
00151 {
00152     if (dataset_->numEntries() == 0) {
00153         cache_->Reset(); // make sure it's empty
00154     } else {
00155         RooFit::MsgLevel gKill = RooMsgService::instance().globalKillBelow();
00156         RooMsgService::instance().setGlobalKillBelow(RooFit::FATAL);
00157         delete cache_;
00158         RooNDKeysPdf pdf("","",*x_,*dataset_,options_,rho_);
00159         cache_ = pdf.createHistogram(GetName(), *x_);
00160         if (cache_->Integral()) cache_->Scale(1.0/cache_->Integral());
00161         cache_->SetBinContent(0,                     underflow_);
00162         cache_->SetBinContent(cache_->GetNbinsX()+1, overflow_);
00163         cache_->Scale(dataset_->sumEntries() * globalScale_);
00164         RooMsgService::instance().setGlobalKillBelow(gKill);
00165     }
00166     isCacheGood_ = true;
00167 }
00168 
00169 void TH1Keys::dont(const char *msg) const {
00170     TObject::Error("TH1Keys",msg);
00171     throw std::runtime_error(std::string("Error in TH1Keys: ")+msg);
00172 }