45using std::cout, std::endl, std::string, std::vector, std::pair, std::map;
71 TString options,
double rho,
double nSigma,
bool rotate,
bool sortInput)
72 :
RooAbsPdf(
name, title), _varList(
"varList",
"List of variables", this),
73 _rhoList(
"rhoList",
"List of rho parameters", this), _options(options), _widthFactor(rho),
74 _nSigma(nSigma), _rotate(rotate), _sortInput(sortInput), _nAdpt(1)
85 TString options,
double rho,
double nSigma,
bool rotate,
bool sortInput)
86 :
RooAbsPdf(
name, title), _varList(
"varList",
"List of variables", this),
87 _rhoList(
"rhoList",
"List of rho parameters", this),
88 _options(options), _widthFactor(rho), _nSigma(nSigma), _rotate(rotate),
89 _sortInput(sortInput), _nAdpt(1)
100 const TVectorD &rho,
TString options,
double nSigma,
bool rotate,
bool sortInput)
101 :
RooAbsPdf(
name, title), _varList(
"varList",
"List of variables", this),
102 _rhoList(
"rhoList",
"List of rho parameters", this), _options(options), _widthFactor(-1.0),
103 _nSigma(nSigma), _rotate(rotate), _sortInput(sortInput), _nAdpt(1)
109 coutE(InputArguments)
110 <<
"ERROR: RooNDKeysPdf::RooNDKeysPdf() : The vector-size of rho is different from that of varList."
111 <<
"Unable to create the PDF." << endl;
131 const RooArgList &rhoList,
TString options,
double nSigma,
bool rotate,
bool sortInput)
132 :
RooAbsPdf(
name, title), _varList(
"varList",
"List of variables", this),
133 _rhoList(
"rhoList",
"List of rho parameters", this), _options(options), _widthFactor(-1.0),
134 _nSigma(nSigma), _rotate(rotate), _sortInput(sortInput), _nAdpt(1)
140 for (
unsigned int i=0; i < rhoList.
size(); ++i) {
141 const auto rho = rhoList.
at(i);
143 coutE(InputArguments) <<
"RooNDKeysPdf::ctor(" <<
GetName() <<
") ERROR: parameter " << rho->
GetName()
144 <<
" is not of type RooRealVar" << endl;
153 coutE(InputArguments) <<
"ERROR: RooNDKeysPdf::RooNDKeysPdf() : The size of rhoList is different from varList."
154 <<
"Unable to create the PDF." << endl;
169 const RooArgList &rhoList,
TString options,
double nSigma,
bool rotate,
bool sortInput)
170 :
RooAbsPdf(
name, title), _varList(
"varList",
"List of variables", this),
171 _rhoList(
"rhoList",
"List of rho parameters", this),
172 _options(options), _widthFactor(-1), _nSigma(nSigma), _rotate(rotate), _sortInput(sortInput),
180 for (
unsigned int i=0; i < rhoList.
size(); ++i) {
181 const auto rho = rhoList.
at(i);
183 coutE(InputArguments) <<
"RooNDKeysPdf::ctor(" <<
GetName() <<
") ERROR: parameter " << rho->
GetName()
184 <<
" is not of type RooRealVar" << endl;
192 coutE(InputArguments) <<
"ERROR: RooNDKeysPdf::RooNDKeysPdf() : The size of rhoList is different from varList."
193 <<
"Unable to create the PDF." << endl;
208 double rho,
double nSigma,
bool rotate,
bool sortInput)
209 :
RooAbsPdf(
name, title), _varList(
"varList",
"List of variables", this),
210 _rhoList(
"rhoList",
"List of rho parameters", this), _options(
"a"), _widthFactor(rho),
211 _nSigma(nSigma), _rotate(rotate), _sortInput(sortInput), _nAdpt(1)
217 coutW(InputArguments) <<
"RooNDKeysPdf::RooNDKeysPdf() : Warning : asymmetric mirror(s) no longer supported."
231 TString options,
double rho,
double nSigma,
bool rotate,
bool sortInput)
232 :
RooAbsPdf(
name, title), _varList(
"varList",
"List of variables", this),
233 _rhoList(
"rhoList",
"List of rho parameters", this), _options(options), _widthFactor(rho),
234 _nSigma(nSigma), _rotate(rotate), _sortInput(sortInput), _nAdpt(1)
246 _varList(
"varList", this, other._varList),
247 _rhoList(
"rhoList", this, other._rhoList),
248 _options(other._options),
249 _widthFactor(other._widthFactor),
250 _nSigma(other._nSigma),
251 _fixedShape(other._fixedShape),
252 _mirror(other._mirror),
253 _debug(other._debug),
254 _verbose(other._verbose),
256 _nEvents(other._nEvents),
257 _nEventsM(other._nEventsM),
258 _nEventsW(other._nEventsW),
261 _dataPts(other._dataPts),
262 _dataPtsR(other._dataPtsR),
263 _weights0(other._weights0),
264 _weights1(other._weights1),
265 _weights(_options.Contains(
"a") ? &_weights1 : &_weights0),
266 _sortTVIdcs(other._sortTVIdcs),
273 _sigma(other._sigma),
274 _xDatLo(other._xDatLo),
275 _xDatHi(other._xDatHi),
276 _xDatLo3s(other._xDatLo3s),
277 _xDatHi3s(other._xDatHi3s),
278 _netFluxZ(other._netFluxZ),
279 _nEventsBW(other._nEventsBW),
280 _nEventsBMSW(other._nEventsBMSW),
281 _xVarLo(other._xVarLo),
282 _xVarHi(other._xVarHi),
283 _xVarLoM3s(other._xVarLoM3s),
284 _xVarLoP3s(other._xVarLoP3s),
285 _xVarHiM3s(other._xVarHiM3s),
286 _xVarHiP3s(other._xVarHiP3s),
287 _bpsIdcs(other._bpsIdcs),
288 _ibNoSort(other._ibNoSort),
289 _sIdcs(other._sIdcs),
290 _bIdcs(other._bIdcs),
291 _bmsIdcs(other._bmsIdcs),
292 _rangeBoxInfo(other._rangeBoxInfo),
293 _fullBoxInfo(other._fullBoxInfo),
295 _minWeight(other._minWeight),
296 _maxWeight(other._maxWeight),
300 _rotMat(new
TMatrixD(*other._rotMat)),
301 _sigmaR(new
TVectorD(*other._sigmaR)),
303 _sigmaAvgR(other._sigmaAvgR),
304 _rotate(other._rotate),
305 _sortInput(other._sortInput),
306 _nAdpt(other._nAdpt),
307 _tracker(other._tracker != nullptr ? new
RooChangeTracker(*other._tracker) : nullptr)
382 cxcoutD(InputArguments) <<
"RooNDKeysPdf::setOptions() options = " <<
_options
385 <<
"\n\tdebug = " <<
_debug
390 coutW(InputArguments) <<
"RooNDKeysPdf::setOptions() : Warning : nSigma = " <<
_nSigma <<
" < 2.0. "
391 <<
"Calculated normalization could be too large."
418 coutE(InputArguments) <<
"ERROR: RooNDKeysPdf::initialize() : The observable list is empty. "
419 <<
"Unable to begin generating the PDF." << endl;
424 coutE(InputArguments) <<
"ERROR: RooNDKeysPdf::initialize() : The input data set is empty. "
425 <<
"Unable to begin generating the PDF." << endl;
429 _d =
static_cast<double>(
_nDim);
431 std::vector<double> dummy(
_nDim,0.);
494 vector<RooRealVar*> dVars(
_nDim);
505 std::vector<double>& point =
_dataPts[i];
508 double myweight =
data.weight();
514 mat(j,k) += dVars[j]->getVal() * dVars[k]->getVal() * myweight;
518 point[j] = pointV[j] = dVars[j]->getVal();
520 _x0[j] += 1. * myweight;
521 _x1[j] += point[j] * myweight ;
522 _x2[j] += point[j] * point[j] * myweight ;
523 if (
_x2[j]!=
_x2[j]) exit(3);
556 for (
Int_t j=0; j<
_nDim; j++) { sigmaRraw[j] = sqrt(sigmaRraw[j]); }
595 coutI(Contents) <<
"RooNDKeysPdf::loadDataSet(" <<
this <<
")"
596 <<
"\n Number of events in dataset: " <<
_nEvents
597 <<
"\n Weighted number of events in dataset: " <<
_nEventsW << endl;
617 vector<double> dummy(
_nDim,0.);
624 vector<vector<double> > mpoints(
size,dummy);
625 vector<vector<Int_t> > mjdcs(
size);
630 vector<Int_t>& mjdxK = mjdcs[0];
631 vector<double>& mpointK = mpoints[0];
643 vector<Int_t>& mjdx0 = mjdcs[0];
645 if (
size==1 && mjdx0.empty())
continue;
649 vector<Int_t>& mjdx = mjdcs[0];
650 vector<double>& mpoint = mpoints[0];
653 Int_t eMir = 1 << mjdx.size();
654 vector<vector<double> > epoints(eMir,
x);
661 epoints[
l] = epoints[
l-size1];
663 vector<double>& epoint = epoints[
l];
664 epoint[mjdx[
Int_t(mjdx.size()-1)-
m]] = mpoint[mjdx[
Int_t(mjdx.size()-1)-
m]];
670 epoints.erase(epoints.begin());
679 for (
Int_t j=0; j<
_nDim; j++) { pointR[j] = (epoints[
m])[j]; }
702 double myweight =
data.weight();
708 coutI(Contents) <<
"RooNDKeysPdf::loadWeightSet(" <<
this <<
") : Number of weighted events : " <<
_wMap.size() << endl;
735 for (
const auto& wMapItr :
_wMap) {
736 Int_t i = wMapItr.first;
739 bool inVarRange(
true);
740 bool inVarRangePlusShell(
true);
745 inVarRange = inVarRange &&
true;
746 }
else { inVarRange = inVarRange &&
false; }
749 inVarRangePlusShell = inVarRangePlusShell &&
true;
750 }
else { inVarRangePlusShell = inVarRangePlusShell &&
false; }
755 bi->
bIdcs.push_back(i);
759 if (inVarRangePlusShell) {
769 if (inShell) bi->
sIdcs.push_back(i);
776 coutI(Contents) <<
"RooNDKeysPdf::calculateShell() : "
777 <<
"\n Events in shell " << bi->
sIdcs.size()
778 <<
"\n Events in box " << bi->
bIdcs.size()
779 <<
"\n Events in box and shell " << bi->
bpsIdcs.size()
797 cxcoutD(Eval) <<
"RooNDKeysPdf::calculatePreNorm() : "
811 for (
unsigned int i = 0; i <
_dataPtsR.size(); ++i) {
818 vector<TVectorD>::iterator dpRItr =
_dataPtsR.begin();
823 itrVecR.push_back(
itPair(i, dpRItr));
826 itrVecR.push_back(
itPair(i, dpRItr));
832 sort(itrVecR.begin(), itrVecR.end(), [=](
const itPair&
a,
const itPair&
b) {
833 return (*a.second)[j] < (*b.second)[j];
839 cxcoutD(Eval) <<
"RooNDKeysPdf::sortDataIndices() : Number of sorted events : " <<
_sortTVIdcs[j].size() << endl;
847 cxcoutD(Eval) <<
"RooNDKeysPdf::calculateBandWidth()" << endl;
858 cxcoutD(Eval) <<
"RooNDKeysPdf::calculateBandWidth() Using static bandwidth." << endl;
865 weight[j] =
_n * (*_sigmaR)[j];
872 cxcoutD(Eval) <<
"RooNDKeysPdf::calculateBandWidth() Using adaptive bandwidth." << endl;
874 double sqrt12 = sqrt(12.);
877 vector<double> dummy(
_nDim, 0.);
880 std::vector<std::vector<double>> *weights_prev(
nullptr);
881 std::vector<std::vector<double>> *weights_new(
nullptr);
902 vector<double> &weight = (*weights_new)[i];
904 double norm = (
_n * (*_sigmaR)[j]) / sqrtSigmaAvgR;
905 weight[j] = norm *
f / sqrt12;
924 std::vector<int> indices;
935 indices.push_back(ibMapItr.first);
939 for (
const auto& i : indices) {
946 const vector<double> &point =
_dataPts[i];
947 const vector<double> &weight = weights[
_idx[i]];
950 (*_dx)[j] =
x[j] - point[j];
958 double r = (*_dx)[j];
959 double c = 1. / (2. * weight[j] * weight[j]);
963 g *= exp(-
c *
r *
r);
964 g *= 1. / (sqrt2pi * weight[j]);
981 xRm[j] = xRp[j] =
x[j];
989 xRm[j] -=
_nSigma * (
_n * (*_sigmaR)[j]);
990 xRp[j] +=
_nSigma * (
_n * (*_sigmaR)[j]);
993 std::vector<TVectorD> xvecRm(1,xRm);
994 std::vector<TVectorD> xvecRp(1,xRp);
997 std::vector<Int_t> ibMapRT;
1002 return (*
a.second)[j] < (*
b.second)[j];
1014 auto&
m =
_nDim==1 ? ibMap : ibMapRT;
1015 m.reserve(std::distance(lo,
hi));
1016 for (it=lo; it!=
hi; ++it) {
1017 m.push_back(it->first);
1020 std::sort(
m.begin(),
m.end());
1026 for (it=lo; it!=
hi; ++it) {
1028 auto found = std::lower_bound(ibMapRT.begin(), ibMapRT.end(), it->first);
1029 if (found != ibMapRT.end() && !(it->first < *found)) {
1030 ibMap.push_back(it->first);
1034 std::sort(ibMap.begin(), ibMap.end());
1037 if (j!=
_nDim-1) { ibMapRT = std::move(ibMap); }
1045 vector<bool> doInt(
_nDim,
true);
1068 bi->
xVarLo[j] = var->getMin(rangeName);
1069 bi->
xVarHi[j] = var->getMax(rangeName);
1071 bi->
xVarLo[j] = var->getVal() ;
1072 bi->
xVarHi[j] = var->getVal() ;
1107 if (rangeName)
return 0 ;
1122 cxcoutD(Eval) <<
"Calling RooNDKeysPdf::analyticalIntegral(" <<
GetName() <<
") with code " << code
1123 <<
" and rangeName " << (rangeName?rangeName:
"<none>") << endl;
1129 vector<bool> doInt(
_nDim,
true);
1135 string rangeNameStr(rangeName) ;
1145 bool newBounds(
false);
1148 if ((var->getMin(rangeName)-bi->
xVarLo[j]!=0) ||
1149 (var->getMax(rangeName)-bi->
xVarHi[j]!=0)) {
1156 cxcoutD(Eval) <<
"RooNDKeysPdf::analyticalIntegral() : Found new boundaries ... " << (rangeName?rangeName:
"<none>") << endl;
1161 if (!bi->
filled || newBounds) {
1174 cxcoutD(Eval) <<
"RooNDKeysPdf::analyticalIntegral() : Using mirrored normalization : " << bi->
nEventsBW << endl;
1181 if (norm<0.) norm=0.;
1186 const vector<double>& weight = (*_weights)[
_idx[bi->
sIdcs[i]]];
1188 vector<double> chi(
_nDim,100.);
1191 if(!doInt[j])
continue;
1194 chi[j] = (
x[j] - bi->
xVarLo[j]) / weight[j];
1196 chi[j] = (bi->
xVarHi[j] -
x[j]) / weight[j];
1200 prob *= (0.5 + std::erf(std::abs(chi[j]) / sqrt(2.)) / 2.);
1202 prob *= (0.5 - std::erf(std::abs(chi[j]) / sqrt(2.)) / 2.);
1209 cxcoutD(Eval) <<
"RooNDKeysPdf::analyticalIntegral() : Final normalization : " << norm <<
" " << bi->
nEventsBW << endl;
1220 std::vector<RooRealVar *> varVec;
1222 for (
const auto var : varList) {
1224 coutE(InputArguments) <<
"RooNDKeysPdf::createDatasetFromHist(" <<
GetName() <<
") WARNING: variable "
1225 << var->
GetName() <<
" is not of type RooRealVar. Skip." << endl;
1228 varVec.push_back(
static_cast<RooRealVar *
>(var));
1232 unsigned int histndim(0);
1233 std::string classname = hist.
ClassName();
1234 if (classname.find(
"TH1") == 0) {
1236 }
else if (classname.find(
"TH2") == 0) {
1238 }
else if (classname.find(
"TH3") == 0) {
1241 assert(histndim == varVec.size());
1243 if (histndim > 3 || histndim <= 0) {
1244 coutE(InputArguments) <<
"RooNDKeysPdf::createDatasetFromHist(" <<
GetName()
1245 <<
") ERROR: input histogram dimension not between [1-3]: " << histndim << endl;
1253 for (
int i = 1; i <= hist.
GetXaxis()->GetNbins(); ++i) {
1257 varVec[0]->setVal(xval);
1259 if (varVec.size() == 1) {
1261 dataFromHist->
add(varSet, fval);
1264 for (
int j = 1; j <= hist.
GetYaxis()->GetNbins(); ++j) {
1266 varVec[1]->setVal(yval);
1268 if (varVec.size() == 2) {
1270 dataFromHist->
add(varSet, fval);
1273 for (
int k = 1; k <= hist.
GetZaxis()->GetNbins(); ++k) {
1275 varVec[2]->setVal(zval);
1278 dataFromHist->
add(varSet, fval);
1285 return dataFromHist;
1297 cxcoutD(Eval) <<
"RooNDKeysPdf::getWeights() Return evaluated weights." << endl;
1305 const vector<double>& weight = (*_weights)[i];
1306 mref(i,
_nDim) = weight[k];
1324 covMatRho(j, k) = (*_covMat)(j, k) *
_rho[j] *
_rho[k];
1331 (*_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
#define R__ASSERT(e)
Checks condition e and reports a fatal error if it's false.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
TMatrixTSym< Double_t > TMatrixDSym
TMatrixT< Double_t > TMatrixD
TVectorT< Double_t > TVectorD
const char * GetName() const override
Returns name of object.
Storage_t::size_type size() const
bool addTyped(const RooAbsCollection &list, bool silent=false)
Adds elements of a given RooAbsCollection to the container if they match the specified type.
RooAbsArg * find(const char *name) const
Find object with given name in list.
Abstract interface for all probability density functions.
const RooArgSet * nset() const
Abstract base class for objects that represent a real value that may appear on the left hand side of ...
virtual double getMin(const char *name=nullptr) const
Get minimum of currently defined range.
Abstract base class for objects that represent a real value and implements functionality common to al...
double getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
bool matchArgs(const RooArgSet &allDeps, RooArgSet &numDeps, const RooArgProxy &a) const
Utility function for use in getAnalyticalIntegral().
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...
bool hasChanged(bool clearState)
Returns true if state has changed since last call with clearState=true.
bool add(const RooAbsArg &var, bool valueServer, bool shapeServer, bool silent)
Overloaded RooCollection_t::add() method insert object into set and registers object as server to own...
Container class to hold unbinned data.
void add(const RooArgSet &row, double weight, double weightError)
Add one ore more rows of data.
Generic N-dimensional implementation of a kernel estimation p.d.f.
std::map< Int_t, bool > _ibNoSort
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
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()
void loadDataSet(bool firstCall, RooDataSet const &data)
copy the dataset and calculate some useful variables
std::vector< TVectorD > _dataPtsR
std::vector< double > _mean
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 > _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
void loadWeightSet(RooDataSet const &data)
std::vector< itVec > _sortTVIdcs
Weights to be used. Points either to _weights0 or _weights1.
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< std::vector< double > > _weights1
std::vector< double > _xDatHi3s
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
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.
void Print(Option_t *name="") const override
Print the matrix as a table of elements.
TMatrixT< Element > & T()
const char * GetName() const override
Returns name of object.
virtual const char * ClassName() const
Returns name of class to which the object belongs.
void ToLower()
Change string to lower-case.
const char * Data() const
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
TVectorT< Element > & Zero()
Set vector elements to zero.
void Print(Option_t *option="") const override
Print the vector as a list of elements.
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