|
Meerkat
v1r3
Multidimensional kernel density estimation package
|
Go to the documentation of this file.
5 #include "Compression.h"
18 #define MAX_VECTOR_SIZE 20000000
23 std::vector<TString> &vars,
24 std::vector<UInt_t> &binning,
25 std::vector<Double_t> &width,
32 init(thePhaseSpace, tree, vars, binning, width, widthScale, approx, toyEvents, maxEvents, skipEvents);
48 std::vector<TString> vars;
49 std::vector<UInt_t> binning;
50 std::vector<Double_t> width;
52 vars.push_back(TString(vars1));
53 binning.push_back(bins1);
54 width.push_back(width1);
56 init(thePhaseSpace, tree, vars, binning, width, widthScale, approx, toyEvents, maxEvents, skipEvents);
73 std::vector<TString> vars;
74 std::vector<UInt_t> binning;
75 std::vector<Double_t> width;
77 vars.push_back(TString(vars1));
78 vars.push_back(TString(weight));
79 binning.push_back(bins1);
80 width.push_back(width1);
82 init(thePhaseSpace, tree, vars, binning, width, widthScale, approx, toyEvents, maxEvents, skipEvents);
88 const char *vars1, const char *vars2,
89 UInt_t bins1, UInt_t bins2,
90 Double_t width1, Double_t width2,
98 std::vector<TString> vars;
99 std::vector<UInt_t> binning;
100 std::vector<Double_t> width;
102 vars.push_back(TString(vars1));
103 vars.push_back(TString(vars2));
104 binning.push_back(bins1);
105 binning.push_back(bins2);
106 width.push_back(width1);
107 width.push_back(width2);
109 init(thePhaseSpace, tree, vars, binning, width, widthScale, approx, toyEvents, maxEvents, skipEvents);
115 const char *vars1, const char *vars2,
117 UInt_t bins1, UInt_t bins2,
118 Double_t width1, Double_t width2,
126 std::vector<TString> vars;
127 std::vector<UInt_t> binning;
128 std::vector<Double_t> width;
130 vars.push_back(TString(vars1));
131 vars.push_back(TString(vars2));
132 vars.push_back(TString(weight));
133 binning.push_back(bins1);
134 binning.push_back(bins2);
135 width.push_back(width1);
136 width.push_back(width2);
138 init(thePhaseSpace, tree, vars, binning, width, widthScale, approx, toyEvents, maxEvents, skipEvents);
144 const char *vars1, const char *vars2, const char *vars3,
145 UInt_t bins1, UInt_t bins2, UInt_t bins3,
146 Double_t width1, Double_t width2, Double_t width3,
154 std::vector<TString> vars;
155 std::vector<UInt_t> binning;
156 std::vector<Double_t> width;
158 vars.push_back(TString(vars1));
159 vars.push_back(TString(vars2));
160 vars.push_back(TString(vars3));
161 binning.push_back(bins1);
162 binning.push_back(bins2);
163 binning.push_back(bins3);
164 width.push_back(width1);
165 width.push_back(width2);
166 width.push_back(width3);
168 init(thePhaseSpace, tree, vars, binning, width, widthScale, approx, toyEvents, maxEvents, skipEvents);
174 const char *vars1, const char *vars2, const char *vars3,
176 UInt_t bins1, UInt_t bins2, UInt_t bins3,
177 Double_t width1, Double_t width2, Double_t width3,
185 std::vector<TString> vars;
186 std::vector<UInt_t> binning;
187 std::vector<Double_t> width;
189 vars.push_back(TString(vars1));
190 vars.push_back(TString(vars2));
191 vars.push_back(TString(vars3));
192 vars.push_back(TString(weight));
193 binning.push_back(bins1);
194 binning.push_back(bins2);
195 binning.push_back(bins3);
196 width.push_back(width1);
197 width.push_back(width2);
198 width.push_back(width3);
200 init(thePhaseSpace, tree, vars, binning, width, widthScale, approx, toyEvents, maxEvents, skipEvents);
206 const char *vars1, const char *vars2, const char *vars3, const char *vars4,
207 UInt_t bins1, UInt_t bins2, UInt_t bins3, UInt_t bins4,
208 Double_t width1, Double_t width2, Double_t width3, Double_t width4,
216 std::vector<TString> vars;
217 std::vector<UInt_t> binning;
218 std::vector<Double_t> width;
220 vars.push_back(TString(vars1));
221 vars.push_back(TString(vars2));
222 vars.push_back(TString(vars3));
223 vars.push_back(TString(vars4));
224 binning.push_back(bins1);
225 binning.push_back(bins2);
226 binning.push_back(bins3);
227 binning.push_back(bins4);
228 width.push_back(width1);
229 width.push_back(width2);
230 width.push_back(width3);
231 width.push_back(width4);
233 init(thePhaseSpace, tree, vars, binning, width, widthScale, approx, toyEvents, maxEvents, skipEvents);
239 const char *vars1, const char *vars2, const char *vars3, const char *vars4,
241 UInt_t bins1, UInt_t bins2, UInt_t bins3, UInt_t bins4,
242 Double_t width1, Double_t width2, Double_t width3, Double_t width4,
250 std::vector<TString> vars;
251 std::vector<UInt_t> binning;
252 std::vector<Double_t> width;
254 vars.push_back(TString(vars1));
255 vars.push_back(TString(vars2));
256 vars.push_back(TString(vars3));
257 vars.push_back(TString(vars4));
258 vars.push_back(TString(weight));
259 binning.push_back(bins1);
260 binning.push_back(bins2);
261 binning.push_back(bins3);
262 binning.push_back(bins4);
263 width.push_back(width1);
264 width.push_back(width2);
265 width.push_back(width3);
266 width.push_back(width4);
268 init(thePhaseSpace, tree, vars, binning, width, widthScale, approx, toyEvents, maxEvents, skipEvents);
274 const char *vars1, const char *vars2, const char *vars3, const char *vars4, const char *vars5,
275 UInt_t bins1, UInt_t bins2, UInt_t bins3, UInt_t bins4, UInt_t bins5,
276 Double_t width1, Double_t width2, Double_t width3, Double_t width4, Double_t width5,
284 std::vector<TString> vars;
285 std::vector<UInt_t> binning;
286 std::vector<Double_t> width;
288 vars.push_back(TString(vars1));
289 vars.push_back(TString(vars2));
290 vars.push_back(TString(vars3));
291 vars.push_back(TString(vars4));
292 vars.push_back(TString(vars5));
293 binning.push_back(bins1);
294 binning.push_back(bins2);
295 binning.push_back(bins3);
296 binning.push_back(bins4);
297 binning.push_back(bins5);
298 width.push_back(width1);
299 width.push_back(width2);
300 width.push_back(width3);
301 width.push_back(width4);
302 width.push_back(width5);
304 init(thePhaseSpace, tree, vars, binning, width, widthScale, approx, toyEvents, maxEvents, skipEvents);
310 const char *vars1, const char *vars2, const char *vars3, const char *vars4, const char *vars5,
312 UInt_t bins1, UInt_t bins2, UInt_t bins3, UInt_t bins4, UInt_t bins5,
313 Double_t width1, Double_t width2, Double_t width3, Double_t width4, Double_t width5,
321 std::vector<TString> vars;
322 std::vector<UInt_t> binning;
323 std::vector<Double_t> width;
325 vars.push_back(TString(vars1));
326 vars.push_back(TString(vars2));
327 vars.push_back(TString(vars3));
328 vars.push_back(TString(vars4));
329 vars.push_back(TString(vars5));
330 vars.push_back(TString(weight));
331 binning.push_back(bins1);
332 binning.push_back(bins2);
333 binning.push_back(bins3);
334 binning.push_back(bins4);
335 binning.push_back(bins5);
336 width.push_back(width1);
337 width.push_back(width2);
338 width.push_back(width3);
339 width.push_back(width4);
340 width.push_back(width5);
342 init(thePhaseSpace, tree, vars, binning, width, widthScale, approx, toyEvents, maxEvents, skipEvents);
354 std::vector<TString> &vars,
355 std::vector<UInt_t> &binning,
356 std::vector<Double_t> &width,
379 Logger::print(0, "%20.20s INFO: Creating binned adaptive kernel density over %dD phase space\n", m_name, m_dim );
382 Logger::print(2, "%20.20s ERROR: Dimensionality of phase space (%d) does not match binning vector size (%d)\n",
388 Logger::print(2, "%20.20s ERROR: Dimensionality of phase space (%d) does not match kernel width vector size (%d)\n",
394 std::vector<UInt_t>::iterator i;
419 for (n= m_dim-1; n>=0; n--) {
420 if ((UInt_t)n == m_dim-1) {
432 std::vector<UInt_t> initBin( m_dim);
433 std::vector<UInt_t> finalBin( m_dim);
434 std::vector<Double_t> lowLimit( m_dim);
435 std::vector<Double_t> coeff( m_dim);
437 UInt_t size = map.size();
444 Double_t corrWeight = weight;
446 for (n=0; n< m_dim; n++) {
447 Double_t width = m_width[n]*widthScale;
451 Double_t step = (up-low)/( m_binning[n]-1);
453 if ( step > 1.5*width) {
458 corrWeight /= widthScale;
460 Double_t x1 = point[n] - width;
461 Double_t x2 = point[n] + width;
463 Int_t i1 = (int)TMath::Ceil( (x1-low)/step );
466 Int_t i2 = (int)TMath::Floor( (x2-low)/step );
471 Logger::print(2, "%20.20s ERROR: i1(%d)>i2(%d) for n=%d, bin size is larger than kernel width!\n", m_name, i1, i2, n);
480 lowLimit[n] -= point[n];
483 lowLimit[n] /= width;
487 std::vector<UInt_t> iter(m_dim);
488 for (n=0; n< m_dim; n++) iter[n] = initBin[n];
495 Logger::print(2, "%20.20s ERROR: index (%d) is larger than array size (%d)\n", m_name, index, size);
499 for (n=0; n< m_dim; n++) {
504 Double_t dx = lowLimit[n] + (Double_t)iter[n]*coeff[n];
505 if (fabs(dx) < 1.) sqsum += dx*dx;
507 if (sqsum < 1.) map[index] += corrWeight*(1.-sqsum);
513 for (n=0; n< m_dim; n++) {
514 if (iter[n] < finalBin[n]) {
519 iter[n] = initBin[n];
529 UInt_t maxEvents, UInt_t skipEvents) {
531 if (vars.size() != m_dim && vars.size() != m_dim + 1) {
532 Logger::print(2, "%20.20s ERROR: Number of TTree variables (%d) in tree \"%s\" does not correspond to phase space dimensionality (%d)\n",
537 if (vars.size() == m_dim + 1) {
538 Logger::print(0, "%20.20s INFO: Using variable \"%s\" as weight\n",
542 UInt_t nvars = vars.size();
544 tree->ResetBranchAddresses();
546 Long64_t nentries = tree->GetEntries();
547 if (maxEvents > 0 && skipEvents + maxEvents < nentries) nentries = skipEvents + maxEvents;
549 Logger::print(0, "%20.20s INFO: Will read %lld events (skipping first %d)\n",
550 m_name, nentries-skipEvents, skipEvents );
554 std::vector<Float_t> varArray(nvars);
557 for (n=0; n < nvars; n++) {
558 Logger::print(0, "%20.20s INFO: Will read branch \"%s\" from tree \"%s\"\n", m_name, vars[n].Data(), tree->GetName());
559 Int_t status = tree->SetBranchAddress(vars[n], &( varArray[n] ));
562 Logger::print(1, "%20.20s WARNING: Weight branch \"%s\" not found, reverting to unweighted mode!\n", m_name, vars[n].Data() );
571 std::vector<Double_t> point( m_dim);
577 for(i=skipEvents; i<nentries; i++) {
579 for (n=0; n< m_dim; n++) {
580 point[n] = varArray[n];
582 Double_t weight = 1.;
583 if (m_dim + 1 == nvars)
584 weight = varArray[ m_dim];
595 Double_t width_scale;
601 width_scale = 1./TMath::Power(a, 1./(Double_t)m_dim);
606 Logger::print(0, "%20.20s INFO: Read %lld/%lld events (%f%%), %lld out\n", m_name, i-skipEvents, nentries-skipEvents,
607 100.*(Double_t)(i-skipEvents)/(Double_t)(nentries-skipEvents), nout);
611 Logger::print(0, "%20.20s INFO: %lld events read in from \"%s\", %lld out\n", m_name, nentries-nout, tree->GetName(), nout );
617 std::vector<Double_t> x( m_dim);
620 if (theDensity == 0) {
626 if (toyEvents == 0) {
628 Logger::print(0, "%20.20s INFO: Convolution of approx. density using rectangular grid\n", m_name);
631 std::vector<UInt_t> iter( m_dim);
633 for (j=0; j<(Int_t) m_dim; j++) {
643 for (j= m_dim-1; j>=0; j--) {
646 x[j] = low + (Double_t)iter[j]/((Double_t) m_binning[j]-1)*(up-low);
650 if (theDensity) e = theDensity-> density(x);
657 Double_t width_scale;
663 width_scale = 1./TMath::Power(a, 1./(Double_t) m_dim);
669 for (j=0; j<(Int_t) m_dim; j++) {
686 Logger::print(0, "%20.20s INFO: Convolution of approx. density using MC with %d events\n", m_name, toyEvents);
688 std::vector<Double_t> lower( m_dim);
689 std::vector<Double_t> coeff( m_dim);
690 for (i=0; i< m_dim; i++) {
696 for (i=0; i<toyEvents; i++) {
704 for (var = 0; var < m_dim; var++) {
705 x[var] = lower[var] + m_rnd.Rndm()*coeff[var];
715 Logger::print(1, "%20.20s WARNING: failed to generate a point within phase space after %d tries\n", m_name, m_maxTries);
720 if (theDensity) e = theDensity-> density(x);
723 Logger::print(0, "%20.20s INFO: toy event %d/%d (%f%%), density=%f\n", m_name, i, toyEvents, 100*(Double_t)(i)/(Double_t)(toyEvents), e);
726 Double_t width_scale;
732 width_scale = 1./TMath::Power(a, 1./(Double_t)m_dim);
741 Logger::print(0, "%20.20s INFO: Writing binned density to file \"%s\"\n", m_name, filename );
743 if (TString(filename).EndsWith( ".root") ) {
753 Logger::print(0, "%20.20s INFO: Writing binned density to text file \"%s\"\n", m_name, filename );
755 FILE* file = fopen(filename, "w+");
757 fprintf(file, "%d\n", m_dim);
760 for (j=0; j<(Int_t) m_dim; j++) {
765 std::vector<Double_t> x( m_dim);
766 std::vector<UInt_t> iter( m_dim);
767 for (j=0; j<(Int_t) m_dim; j++) {
771 UInt_t size = m_map.size();
777 for (j= m_dim-1; j>=0; j--) {
780 x[j] = low + (Double_t)iter[j]/((Double_t) m_binning[j]-1)*(up-low);
781 if (j==(Int_t) m_dim-1) {
791 Logger::print(2, "%20.20s ERROR: index (%d) is larger than array size (%d)\n", m_name, index, size);
798 for (j=0; j<(Int_t) m_dim; j++) {
816 Logger::print(0, "%20.20s INFO: Writing binned density to ROOT file \"%s\"\n", m_name, filename );
818 TDirectory* curr_dir = gDirectory;
820 TFile* file = TFile::Open(filename, "RECREATE");
821 file->SetCompressionSettings(ROOT::CompressionSettings(ROOT::kLZMA, 9));
822 TTree dimTree( "DimTree", "DimTree");
825 dimTree.Branch( "bins",&bins, "bins/I");
828 for (j=0; j<(Int_t) m_dim; j++) {
835 std::vector<Double_t> x( m_dim);
836 std::vector<UInt_t> iter( m_dim);
837 for (j=0; j<(Int_t) m_dim; j++) {
841 UInt_t size = m_map.size();
843 TTree mapTree( "MapTree", "MapTree");
847 mapTree.Branch( "dens", &dens, "dens/F");
848 mapTree.Branch( "inphsp",&inphsp, "inphsp/B");
854 for (j= m_dim-1; j>=0; j--) {
857 x[j] = low + (Double_t)iter[j]/((Double_t) m_binning[j]-1)*(up-low);
858 if (j==(Int_t) m_dim-1) {
868 Logger::print(2, "%20.20s ERROR: index (%d) is larger than array size (%d)\n", m_name, index, size);
877 for (j=0; j<(Int_t) m_dim; j++) {
893 gDirectory = curr_dir;
904 Double_t majorant = 0.;
907 std::vector<Double_t> x( m_dim);
908 std::vector<UInt_t> iter( m_dim);
909 UInt_t size = m_map.size();
913 for (j=0; j<(Int_t) m_dim; j++) {
921 for (j= m_dim-1; j>=0; j--) {
924 x[j] = low + (Double_t)iter[j]/((Double_t) m_binning[j]-1)*(up-low);
925 if (j==(Int_t) m_dim-1) {
935 Logger::print(2, "%20.20s ERROR: index (%d) is larger than array size (%d)\n", m_name, index, size);
940 if (d > majorant) majorant = d;
947 for (j=0; j<(Int_t) m_dim; j++) {
959 sum /= (Double_t)num;
961 Logger::print(0, "%20.20s INFO: Average PDF value before normalisation is %f\n", m_name, sum);
964 for (j=0; j<(Int_t)size; j++) m_map[j] /= sum;
974 std::vector<UInt_t> ivect( m_dim);
976 for (j=0; j<(Int_t) m_dim; j++) {
980 if (xj < low || xj > up) {
983 Int_t ij = (int)floor((xj-low)/(up-low)*( m_binning[j]-1));
993 std::vector<UInt_t> iter( m_dim);
995 for (j=0; j<(Int_t) m_dim; j++) {
1003 Double_t weight = 1;
1004 for (j=0; j<(Int_t) m_dim; j++) {
1008 Double_t xj1 = low + ((Double_t)ivect[j] )/((Double_t) m_binning[j]-1.)*(up-low);
1009 Double_t xj2 = low + ((Double_t)ivect[j]+1.)/((Double_t) m_binning[j]-1.)*(up-low);
1017 fweight = 1. - (x[j]-xj1)/(xj2-xj1);
1019 fweight = (x[j]-xj1)/(xj2-xj1);
1037 for (j= m_dim-1; j>=0; j--) {
1038 Int_t ij = ivect[j] + iter[j];
1039 if (j==(Int_t) m_dim-1) {
1046 e += weight*map[index];
1053 for (j=0; j<(Int_t) m_dim; j++) {
const char * name(void) Return the name of the PDF.
virtual Double_t upperLimit(UInt_t var)=0 Return upper allowed limit of the variable.
void normalise(void) Normalise the PDF such that the average PDF value over the allowed phase space equals to 1...
Abstract class which defines probability density interface.
void addToMap(std::vector< Double_t > &map, std::vector< Double_t > &point, Double_t widthScale=1., Double_t weight=1.) Add a kernel density of a single data point to the binned map.
AbsDensity * m_widthScaleDensity Reference to PDF used for kernel width scaling.
AbsPhaseSpace * phaseSpace() Return the phase space.
void print(int level, const char *format,...)
std::vector< Double_t > m_width Vector of kernel widths.
char m_name[256] PDF name.
void fillMapFromTree(TTree *tree, std::vector< TString > &vars, UInt_t maxEvents=0, UInt_t skipEvents=0)
std::vector< Double_t > m_approxMap Bin map of approximation PDF convolved with the kernel.
virtual ~AdaptiveKernelDensity() Destructor.
Double_t density(std::vector< Double_t > &x) Calculate PDF density at the point. Multilinear binned interpolation is used which is calculated in t...
TRandom3 m_rnd Random number generator.
Bool_t m_fractionalMode Fractional mode flag.
Double_t mapDensity(std::vector< Double_t > &map, std::vector< Double_t > &x) Calculate the raw density using the binned map (estimated or approximation) at a given point...
Abstract class which defines phase space interface.
virtual UInt_t dimensionality()=0 Get dimensionality of phase space.
void setMajorant(Double_t majorant) Set majorant for accept-reject method.
UInt_t m_maxTries Maximum number of tries for accept-reject method.
void setTimer(void) Reset time counter.
virtual Double_t lowerLimit(UInt_t var)=0 Return lower allowed limit of the variable.
int timer(int secs) Return 1 if more than a certain number of seconds have passed since the last call of this function or...
std::vector< Double_t > m_map Bin map of estimated PDF.
AbsPhaseSpace * m_phaseSpace Reference to phase space.
Double_t m_maxScale Maximum width scale factor.
UInt_t iterToIndex(std::vector< UInt_t > &iter) Convert an N-dimensional iterator vector into a linear bin index in the bin map.
AbsDensity * m_approxDensity Reference to approximation PDF.
void writeToTextFile(const char *fileName) Write the binned PDF to a text file.
void init(AbsPhaseSpace *thePhaseSpace, TTree *tree, std::vector< TString > &vars, std::vector< UInt_t > &binning, std::vector< Double_t > &width, AbsDensity *widthScale, AbsDensity *approx=0, UInt_t toyEvents=0, UInt_t maxEvents=0, UInt_t skipEvents=0) Common initialise method used by all constructors.
virtual Bool_t withinLimits(std::vector< Double_t > &x)=0 Check if the point is within phase space limits.
Double_t m_maxValue Maximum value of the width scale PDF to be used for scaling.
virtual Double_t density(std::vector< Double_t > &x)=0 Calculate PDF value at the given point.
void writeToRootFile(const char *fileName) Write the binned density into a ROOT file.
Double_t m_minScale Minimum width scale factor.
Double_t m_minValue Minimum value of the width scale PDF to be used for scaling.
void fillMapFromDensity(AbsDensity *theDensity, UInt_t toyEvents=0)
void writeToFile(const char *fileName) Write density map to file depending on extension.
std::vector< UInt_t > m_binning Vector of bin numbers in each variable.
UInt_t m_dim Cached dimensionality of the phase space.
AdaptiveKernelDensity(const char *pdfName, AbsPhaseSpace *thePhaseSpace, TTree *tree, const char *vars1, UInt_t bins1, Double_t width1, AbsDensity *widthScale, AbsDensity *approx=0, UInt_t toyEvents=0, UInt_t maxEvents=0, UInt_t skipEvents=0) Constructor for 1-dimensional adaptive kernel PDF from the sample of points in an NTuple...
|