45using std::string, std::vector, std::pair, std::map;
70 TString options,
double rho,
double nSigma,
bool rotate,
bool sortInput)
84 TString options,
double rho,
double nSigma,
bool rotate,
bool sortInput)
86 _rhoList(
"rhoList",
"List of rho parameters", this),
99 const TVectorD &rho,
TString options,
double nSigma,
bool rotate,
bool sortInput)
108 coutE(InputArguments)
109 <<
"ERROR: RooNDKeysPdf::RooNDKeysPdf() : The vector-size of rho is different from that of varList."
110 <<
"Unable to create the PDF." << std::endl;
111 R__ASSERT(int(_varList.size()) == rho.GetNrows());
130 const RooArgList &rhoList,
TString options,
double nSigma,
bool rotate,
bool sortInput)
139 for (
unsigned int i=0; i < rhoList.
size(); ++i) {
140 const auto rho = rhoList.
at(i);
142 coutE(InputArguments) <<
"RooNDKeysPdf::ctor(" <<
GetName() <<
") ERROR: parameter " << rho->GetName()
143 <<
" is not of type RooRealVar" << std::endl;
152 coutE(InputArguments) <<
"ERROR: RooNDKeysPdf::RooNDKeysPdf() : The size of rhoList is different from varList."
153 <<
"Unable to create the PDF." << std::endl;
154 assert(_varList.size() == _rhoList.size());
168 const RooArgList &rhoList,
TString options,
double nSigma,
bool rotate,
bool sortInput)
170 _rhoList(
"rhoList",
"List of rho parameters", this),
179 for (
unsigned int i=0; i < rhoList.
size(); ++i) {
180 const auto rho = rhoList.
at(i);
182 coutE(InputArguments) <<
"RooNDKeysPdf::ctor(" <<
GetName() <<
") ERROR: parameter " << rho->GetName()
183 <<
" is not of type RooRealVar" << std::endl;
191 coutE(InputArguments) <<
"ERROR: RooNDKeysPdf::RooNDKeysPdf() : The size of rhoList is different from varList."
192 <<
"Unable to create the PDF." << std::endl;
193 assert(_varList.size() == _rhoList.size());
207 double rho,
double nSigma,
bool rotate,
bool sortInput)
216 coutW(InputArguments) <<
"RooNDKeysPdf::RooNDKeysPdf() : Warning : asymmetric mirror(s) no longer supported."
230 TString options,
double rho,
double nSigma,
bool rotate,
bool sortInput)
381 cxcoutD(InputArguments) <<
"RooNDKeysPdf::setOptions() options = " <<
_options
382 <<
"\n\tbandWidthType = " <<
_options.Contains(
"a")
384 <<
"\n\tdebug = " <<
_debug
389 coutW(InputArguments) <<
"RooNDKeysPdf::setOptions() : Warning : nSigma = " <<
_nSigma <<
" < 2.0. "
390 <<
"Calculated normalization could be too large."
417 coutE(InputArguments) <<
"ERROR: RooNDKeysPdf::initialize() : The observable list is empty. "
418 <<
"Unable to begin generating the PDF." << std::endl;
423 coutE(InputArguments) <<
"ERROR: RooNDKeysPdf::initialize() : The input data set is empty. "
424 <<
"Unable to begin generating the PDF." << std::endl;
428 _d =
static_cast<double>(
_nDim);
430 std::vector<double> dummy(
_nDim,0.);
465 for(
unsigned int j=0; j <
_varList.size(); ++j) {
493 vector<RooRealVar*> dVars(
_nDim);
504 std::vector<double>& point =
_dataPts[i];
507 double myweight = data.weight();
513 mat(j,k) += dVars[j]->getVal() * dVars[k]->getVal() * myweight;
517 point[j] = pointV[j] = dVars[j]->getVal();
519 _x0[j] += 1. * myweight;
520 _x1[j] += point[j] * myweight ;
521 _x2[j] += point[j] * point[j] * myweight ;
522 if (
_x2[j]!=
_x2[j]) exit(3);
555 for (
Int_t j=0; j<
_nDim; j++) { sigmaRraw[j] = sqrt(sigmaRraw[j]); }
594 coutI(Contents) <<
"RooNDKeysPdf::loadDataSet(" <<
this <<
")"
595 <<
"\n Number of events in dataset: " <<
_nEvents
596 <<
"\n Weighted number of events in dataset: " <<
_nEventsW << std::endl;
616 vector<double> dummy(
_nDim,0.);
623 vector<vector<double> > mpoints(
size,dummy);
624 vector<vector<Int_t> > mjdcs(
size);
629 vector<Int_t>& mjdxK = mjdcs[0];
630 vector<double>& mpointK = mpoints[0];
642 vector<Int_t>& mjdx0 = mjdcs[0];
644 if (
size==1 && mjdx0.empty())
continue;
648 vector<Int_t>& mjdx = mjdcs[0];
649 vector<double>& mpoint = mpoints[0];
652 Int_t eMir = 1 << mjdx.size();
653 vector<vector<double> > epoints(eMir,
x);
660 epoints[
l] = epoints[
l-size1];
662 vector<double>& epoint = epoints[
l];
663 epoint[mjdx[
Int_t(mjdx.size()-1)-
m]] = mpoint[mjdx[
Int_t(mjdx.size()-1)-
m]];
669 epoints.erase(epoints.begin());
678 for (
Int_t j=0; j<
_nDim; j++) { pointR[j] = (epoints[
m])[j]; }
701 double myweight = data.weight();
707 coutI(Contents) <<
"RooNDKeysPdf::loadWeightSet(" <<
this <<
") : Number of weighted events : " <<
_wMap.size() << std::endl;
734 for (
const auto& wMapItr :
_wMap) {
735 Int_t i = wMapItr.first;
738 bool inVarRange(
true);
739 bool inVarRangePlusShell(
true);
744 inVarRange = inVarRange &&
true;
745 }
else { inVarRange = inVarRange &&
false; }
748 inVarRangePlusShell = inVarRangePlusShell &&
true;
749 }
else { inVarRangePlusShell = inVarRangePlusShell &&
false; }
754 bi->
bIdcs.push_back(i);
758 if (inVarRangePlusShell) {
768 if (inShell) bi->
sIdcs.push_back(i);
775 coutI(Contents) <<
"RooNDKeysPdf::calculateShell() : "
776 <<
"\n Events in shell " << bi->
sIdcs.size()
777 <<
"\n Events in box " << bi->
bIdcs.size()
778 <<
"\n Events in box and shell " << bi->
bpsIdcs.size()
796 cxcoutD(Eval) <<
"RooNDKeysPdf::calculatePreNorm() : "
810 for (
unsigned int i = 0; i <
_dataPtsR.size(); ++i) {
817 vector<TVectorD>::iterator dpRItr =
_dataPtsR.begin();
822 itrVecR.push_back(
itPair(i, dpRItr));
825 itrVecR.push_back(
itPair(i, dpRItr));
831 sort(itrVecR.begin(), itrVecR.end(), [=](
const itPair&
a,
const itPair&
b) {
832 return (*a.second)[j] < (*b.second)[j];
838 cxcoutD(Eval) <<
"RooNDKeysPdf::sortDataIndices() : Number of sorted events : " <<
_sortTVIdcs[j].size() << std::endl;
846 cxcoutD(Eval) <<
"RooNDKeysPdf::calculateBandWidth()" << std::endl;
848 const bool adaptive =
_options.Contains(
"a");
857 cxcoutD(Eval) <<
"RooNDKeysPdf::calculateBandWidth() Using static bandwidth." << std::endl;
864 weight[j] =
_n * (*_sigmaR)[j];
871 cxcoutD(Eval) <<
"RooNDKeysPdf::calculateBandWidth() Using adaptive bandwidth." << std::endl;
873 double sqrt12 = sqrt(12.);
876 vector<double> dummy(
_nDim, 0.);
879 std::vector<std::vector<double>> *weights_prev(
nullptr);
880 std::vector<std::vector<double>> *weights_new(
nullptr);
901 vector<double> &weight = (*weights_new)[i];
903 double norm = (
_n * (*_sigmaR)[j]) / sqrtSigmaAvgR;
904 weight[j] = norm *
f / sqrt12;
923 std::vector<int> indices;
934 indices.push_back(ibMapItr.first);
938 for (
const auto& i : indices) {
945 const vector<double> &point =
_dataPts[i];
946 const vector<double> &weight = weights[
_idx[i]];
949 (*_dx)[j] =
x[j] - point[j];
957 double r = (*_dx)[j];
958 double c = 1. / (2. * weight[j] * weight[j]);
962 g *= exp(-
c *
r *
r);
963 g *= 1. / (sqrt2pi * weight[j]);
980 xRm[j] = xRp[j] =
x[j];
988 xRm[j] -=
_nSigma * (
_n * (*_sigmaR)[j]);
989 xRp[j] +=
_nSigma * (
_n * (*_sigmaR)[j]);
992 std::vector<TVectorD> xvecRm(1,xRm);
993 std::vector<TVectorD> xvecRp(1,xRp);
996 std::vector<Int_t> ibMapRT;
1001 return (*
a.second)[j] < (*
b.second)[j];
1013 auto&
m =
_nDim==1 ? ibMap : ibMapRT;
1014 m.reserve(std::distance(lo,
hi));
1015 for (it=lo; it!=
hi; ++it) {
1016 m.push_back(it->first);
1019 std::sort(
m.begin(),
m.end());
1025 for (it=lo; it!=
hi; ++it) {
1027 auto found = std::lower_bound(ibMapRT.begin(), ibMapRT.end(), it->first);
1028 if (found != ibMapRT.end() && !(it->first < *found)) {
1029 ibMap.push_back(it->first);
1033 std::sort(ibMap.begin(), ibMap.end());
1036 if (j!=
_nDim-1) { ibMapRT = std::move(ibMap); }
1044 vector<bool> doInt(
_nDim,
true);
1064 for (
unsigned int j=0; j <
_varList.size(); ++j) {
1067 bi->
xVarLo[j] = var->getMin(rangeName);
1068 bi->
xVarHi[j] = var->getMax(rangeName);
1070 bi->
xVarLo[j] = var->getVal() ;
1071 bi->
xVarHi[j] = var->getVal() ;
1087 for (
unsigned int j=0; j <
_varList.size(); ++j) {
1089 _x[j] = var->getVal(nset);
1106 if (rangeName)
return 0 ;
1121 cxcoutD(Eval) <<
"Calling RooNDKeysPdf::analyticalIntegral(" <<
GetName() <<
") with code " << code
1122 <<
" and rangeName " << (rangeName?rangeName:
"<none>") << std::endl;
1128 vector<bool> doInt(
_nDim,
true);
1134 string rangeNameStr(rangeName) ;
1144 bool newBounds(
false);
1145 for (
unsigned int j=0; j <
_varList.size(); ++j) {
1147 if ((var->getMin(rangeName)-bi->
xVarLo[j]!=0) ||
1148 (var->getMax(rangeName)-bi->
xVarHi[j]!=0)) {
1155 cxcoutD(Eval) <<
"RooNDKeysPdf::analyticalIntegral() : Found new boundaries ... " << (rangeName?rangeName:
"<none>") << std::endl;
1160 if (!bi->
filled || newBounds) {
1173 cxcoutD(Eval) <<
"RooNDKeysPdf::analyticalIntegral() : Using mirrored normalization : " << bi->
nEventsBW << std::endl;
1180 if (norm<0.) norm=0.;
1185 const vector<double>& weight = (*_weights)[
_idx[bi->
sIdcs[i]]];
1187 vector<double> chi(
_nDim,100.);
1190 if(!doInt[j])
continue;
1193 chi[j] = (
x[j] - bi->
xVarLo[j]) / weight[j];
1195 chi[j] = (bi->
xVarHi[j] -
x[j]) / weight[j];
1199 prob *= (0.5 + std::erf(std::abs(chi[j]) / sqrt(2.)) / 2.);
1201 prob *= (0.5 - std::erf(std::abs(chi[j]) / sqrt(2.)) / 2.);
1208 cxcoutD(Eval) <<
"RooNDKeysPdf::analyticalIntegral() : Final normalization : " << norm <<
" " << bi->
nEventsBW << std::endl;
1219 std::vector<RooRealVar *> varVec;
1221 for (
const auto var : varList) {
1223 coutE(InputArguments) <<
"RooNDKeysPdf::createDatasetFromHist(" <<
GetName() <<
") WARNING: variable "
1224 << var->GetName() <<
" is not of type RooRealVar. Skip." << std::endl;
1227 varVec.push_back(
static_cast<RooRealVar *
>(var));
1231 unsigned int histndim(0);
1232 std::string classname = hist.
ClassName();
1233 if (classname.find(
"TH1") == 0) {
1235 }
else if (classname.find(
"TH2") == 0) {
1237 }
else if (classname.find(
"TH3") == 0) {
1240 assert(histndim == varVec.size());
1242 if (histndim > 3 || histndim <= 0) {
1243 coutE(InputArguments) <<
"RooNDKeysPdf::createDatasetFromHist(" <<
GetName()
1244 <<
") ERROR: input histogram dimension not between [1-3]: " << histndim << std::endl;
1252 for (
int i = 1; i <= hist.
GetXaxis()->GetNbins(); ++i) {
1256 varVec[0]->setVal(xval);
1258 if (varVec.size() == 1) {
1260 dataFromHist->
add(varSet, fval);
1263 for (
int j = 1; j <= hist.
GetYaxis()->GetNbins(); ++j) {
1265 varVec[1]->setVal(yval);
1267 if (varVec.size() == 2) {
1269 dataFromHist->
add(varSet, fval);
1272 for (
int k = 1; k <= hist.
GetZaxis()->GetNbins(); ++k) {
1274 varVec[2]->setVal(zval);
1277 dataFromHist->
add(varSet, fval);
1284 return dataFromHist;
1296 cxcoutD(Eval) <<
"RooNDKeysPdf::getWeights() Return evaluated weights." << std::endl;
1304 const vector<double>& weight = (*_weights)[i];
1305 mref(i,
_nDim) = weight[k];
1314 for (
unsigned int j = 0; j <
_rhoList.size(); ++j) {
1316 _rho[j] = rho->getVal();
1323 covMatRho(j, k) = (*_covMat)(j, k) *
_rho[j] *
_rho[k];
1330 (*_sigmaR)[j] = sqrt((*
_sigmaR)[j]);
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
std::vector< itPair > itVec
std::pair< Int_t, VecTVecDouble::iterator > itPair
int Int_t
Signed integer 4 bytes (int).
#define R__ASSERT(e)
Checks condition e and reports a fatal error if it's false.
TMatrixTSym< Double_t > TMatrixDSym
TMatrixT< Double_t > TMatrixD
TVectorT< Double_t > TVectorD
Storage_t::size_type size() const
RooAbsArg * find(const char *name) const
Find object with given name in list.
RooAbsPdf()
Default constructor.
Abstract base class for objects that represent a real value that may appear on the left hand side of ...
double getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
bool matchArgs(const RooArgSet &allDeps, RooArgSet &analDeps, const RooArgProxy &a, const Proxies &... proxies) const
RooArgList is a container object that can hold multiple RooAbsArg objects.
RooAbsArg * at(Int_t idx) const
Return object at given index, or nullptr if index is out of range.
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Meta object that tracks value changes in a given set of RooAbsArgs by registering itself as value cli...
void add(const RooArgSet &row, double weight, double weightError)
Add one ore more rows of data.
std::vector< double > _xVarLoM3s
std::map< Int_t, bool > _ibNoSort
std::vector< Int_t > _sIdcs
std::vector< std::vector< double > > _weights0
double analyticalIntegral(Int_t code, const char *rangeName=nullptr) const override
Implements the actual analytical integral(s) advertised by getAnalyticalIntegral.
void calculatePreNorm(BoxInfo *bi) const
bi->nEventsBMSW=0.; bi->nEventsBW=0.;
std::vector< double > _xDatHi
std::vector< std::vector< double > > * _weights
! Weights to be used. Points either to _weights0 or _weights1
void createPdf(bool firstCall, RooDataSet const &data)
evaluation order of constructor.
double evaluate() const override
Evaluate this PDF / function / constant. Needs to be overridden by all derived classes.
void loopRange(std::vector< double > &x, std::vector< Int_t > &indices) const
determine closest points to x, to loop over in evaluate()
std::vector< double > _xDatLo
void initialize(RooDataSet const &data)
initialization
std::map< Int_t, double > _wMap
void sortDataIndices(BoxInfo *bi=nullptr)
sort entries, as needed for loopRange()
std::vector< double > _xVarHi
void loadDataSet(bool firstCall, RooDataSet const &data)
copy the dataset and calculate some useful variables
std::vector< Int_t > _bIdcs
std::vector< TVectorD > _dataPtsR
std::vector< double > _mean
std::vector< double > _xVarLo
void calculateShell(BoxInfo *bi) const
determine points in +/- nSigma shell around the box determined by the variable ranges.
void calculateBandWidth()
std::vector< double > _xDatLo3s
std::vector< double > _x1
std::vector< std::vector< double > > _dataPts
std::vector< double > _xVarHiM3s
std::vector< double > _x0
std::vector< double > _x2
double gauss(std::vector< double > &x, std::vector< std::vector< double > > &weights) const
loop over all closest point to x, as determined by loopRange()
std::vector< double > _rho
std::vector< Int_t > _bmsIdcs
void loadWeightSet(RooDataSet const &data)
std::vector< itVec > _sortTVIdcs
!
void boxInfoInit(BoxInfo *bi, const char *rangeName, Int_t code) const
std::map< std::pair< std::string, int >, BoxInfo * > _rangeBoxInfo
std::vector< Int_t > _idx
Int_t getAnalyticalIntegral(RooArgSet &allVars, RooArgSet &analVars, const char *rangeName=nullptr) const override
Interface function getAnalyticalIntergral advertises the analytical integrals that are supported.
std::vector< double > _xVarLoP3s
std::vector< std::vector< double > > _weights1
std::vector< double > _xDatHi3s
std::vector< double > _xVarHiP3s
TMatrixD getWeights(const int &k) const
Return evaluated weights.
std::vector< double > _sigma
void mirrorDataSet()
determine mirror dataset.
void setOptions()
set the configuration
RooDataSet * createDatasetFromHist(const RooArgList &varList, const TH1 &hist) const
void checkInitWeights() const
std::map< Int_t, bool > _bpsIdcs
RooChangeTracker * _tracker
Variable that can be changed from the outside.
virtual Double_t GetBinCenter(Int_t bin) const
Return center of bin.
TH1 is the base class of all histogram classes in ROOT.
virtual Double_t GetBinContent(Int_t bin) const
Return content of bin number bin.
const TVectorD & GetEigenValues() const
const TMatrixD & GetEigenVectors() const
virtual TMatrixTBase< Element > & Zero()
Set matrix elements to zero.
const char * GetName() const override
Returns name of object.
virtual const char * ClassName() const
Returns name of class to which the object belongs.
void box(Int_t pat, Double_t x1, Double_t y1, Double_t x2, Double_t y2)
RooCmdArg WeightVar(const char *name="weight", bool reinterpretAsWeight=false)
constexpr Double_t TwoPi()
std::vector< double > xVarHiM3s
std::vector< Int_t > bIdcs
std::vector< double > xVarHiP3s
std::vector< double > xVarLo
std::vector< double > xVarHi
std::map< Int_t, bool > bpsIdcs
std::vector< Int_t > sIdcs
std::vector< double > xVarLoM3s
std::vector< double > xVarLoP3s
std::vector< Int_t > bmsIdcs