|
Meerkat
v1r3
Multidimensional kernel density estimation package
|
Go to the documentation of this file.
5 #include "Compression.h"
20 std::vector<TString> &vars,
21 std::vector<UInt_t> &binning,
22 std::vector<Double_t> &width,
28 init(thePhaseSpace, tree, vars, binning, width, d, toyEvents, maxEvents, skipEvents);
43 std::vector<TString> vars;
44 std::vector<UInt_t> binning;
45 std::vector<Double_t> width;
47 vars.push_back(TString(vars1));
48 binning.push_back(bins1);
49 width.push_back(width1);
51 init(thePhaseSpace, tree, vars, binning, width, d, toyEvents, maxEvents, skipEvents);
67 std::vector<TString> vars;
68 std::vector<UInt_t> binning;
69 std::vector<Double_t> width;
71 vars.push_back(TString(vars1));
72 vars.push_back(TString(weight));
73 binning.push_back(bins1);
74 width.push_back(width1);
76 init(thePhaseSpace, tree, vars, binning, width, d, toyEvents, maxEvents, skipEvents);
82 const char *vars1, const char *vars2,
83 UInt_t bins1, UInt_t bins2,
84 Double_t width1, Double_t width2,
91 std::vector<TString> vars;
92 std::vector<UInt_t> binning;
93 std::vector<Double_t> width;
95 vars.push_back(TString(vars1));
96 vars.push_back(TString(vars2));
97 binning.push_back(bins1);
98 binning.push_back(bins2);
99 width.push_back(width1);
100 width.push_back(width2);
102 init(thephaseSpace, tree, vars, binning, width, d, toyEvents, maxEvents, skipEvents);
108 const char *vars1, const char *vars2,
110 UInt_t bins1, UInt_t bins2,
111 Double_t width1, Double_t width2,
118 std::vector<TString> vars;
119 std::vector<UInt_t> binning;
120 std::vector<Double_t> width;
122 vars.push_back(TString(vars1));
123 vars.push_back(TString(vars2));
124 vars.push_back(TString(weight));
125 binning.push_back(bins1);
126 binning.push_back(bins2);
127 width.push_back(width1);
128 width.push_back(width2);
130 init(thephaseSpace, tree, vars, binning, width, d, toyEvents, maxEvents, skipEvents);
136 const char *vars1, const char *vars2, const char *vars3,
137 UInt_t bins1, UInt_t bins2, UInt_t bins3,
138 Double_t width1, Double_t width2, Double_t width3,
145 std::vector<TString> vars;
146 std::vector<UInt_t> binning;
147 std::vector<Double_t> width;
149 vars.push_back(TString(vars1));
150 vars.push_back(TString(vars2));
151 vars.push_back(TString(vars3));
152 binning.push_back(bins1);
153 binning.push_back(bins2);
154 binning.push_back(bins3);
155 width.push_back(width1);
156 width.push_back(width2);
157 width.push_back(width3);
159 init(thephaseSpace, tree, vars, binning, width, d, toyEvents, maxEvents, skipEvents);
165 const char *vars1, const char *vars2, const char *vars3,
167 UInt_t bins1, UInt_t bins2, UInt_t bins3,
168 Double_t width1, Double_t width2, Double_t width3,
175 std::vector<TString> vars;
176 std::vector<UInt_t> binning;
177 std::vector<Double_t> width;
179 vars.push_back(TString(vars1));
180 vars.push_back(TString(vars2));
181 vars.push_back(TString(vars3));
182 vars.push_back(TString(weight));
183 binning.push_back(bins1);
184 binning.push_back(bins2);
185 binning.push_back(bins3);
186 width.push_back(width1);
187 width.push_back(width2);
188 width.push_back(width3);
190 init(thephaseSpace, tree, vars, binning, width, d, toyEvents, maxEvents, skipEvents);
196 const char *vars1, const char *vars2, const char *vars3, const char *vars4,
197 UInt_t bins1, UInt_t bins2, UInt_t bins3, UInt_t bins4,
198 Double_t width1, Double_t width2, Double_t width3, Double_t width4,
205 std::vector<TString> vars;
206 std::vector<UInt_t> binning;
207 std::vector<Double_t> width;
209 vars.push_back(TString(vars1));
210 vars.push_back(TString(vars2));
211 vars.push_back(TString(vars3));
212 vars.push_back(TString(vars4));
213 binning.push_back(bins1);
214 binning.push_back(bins2);
215 binning.push_back(bins3);
216 binning.push_back(bins4);
217 width.push_back(width1);
218 width.push_back(width2);
219 width.push_back(width3);
220 width.push_back(width4);
222 init(thephaseSpace, tree, vars, binning, width, d, toyEvents, maxEvents, skipEvents);
228 const char *vars1, const char *vars2, const char *vars3, const char *vars4,
230 UInt_t bins1, UInt_t bins2, UInt_t bins3, UInt_t bins4,
231 Double_t width1, Double_t width2, Double_t width3, Double_t width4,
238 std::vector<TString> vars;
239 std::vector<UInt_t> binning;
240 std::vector<Double_t> width;
242 vars.push_back(TString(vars1));
243 vars.push_back(TString(vars2));
244 vars.push_back(TString(vars3));
245 vars.push_back(TString(vars4));
246 vars.push_back(TString(weight));
247 binning.push_back(bins1);
248 binning.push_back(bins2);
249 binning.push_back(bins3);
250 binning.push_back(bins4);
251 width.push_back(width1);
252 width.push_back(width2);
253 width.push_back(width3);
254 width.push_back(width4);
256 init(thephaseSpace, tree, vars, binning, width, d, toyEvents, maxEvents, skipEvents);
262 const char *vars1, const char *vars2, const char *vars3, const char *vars4, const char *vars5,
263 UInt_t bins1, UInt_t bins2, UInt_t bins3, UInt_t bins4, UInt_t bins5,
264 Double_t width1, Double_t width2, Double_t width3, Double_t width4, Double_t width5,
271 std::vector<TString> vars;
272 std::vector<UInt_t> binning;
273 std::vector<Double_t> width;
275 vars.push_back(TString(vars1));
276 vars.push_back(TString(vars2));
277 vars.push_back(TString(vars3));
278 vars.push_back(TString(vars4));
279 vars.push_back(TString(vars5));
280 binning.push_back(bins1);
281 binning.push_back(bins2);
282 binning.push_back(bins3);
283 binning.push_back(bins4);
284 binning.push_back(bins5);
285 width.push_back(width1);
286 width.push_back(width2);
287 width.push_back(width3);
288 width.push_back(width4);
289 width.push_back(width5);
291 init(thephaseSpace, tree, vars, binning, width, d, toyEvents, maxEvents, skipEvents);
297 const char *vars1, const char *vars2, const char *vars3, const char *vars4, const char *vars5,
299 UInt_t bins1, UInt_t bins2, UInt_t bins3, UInt_t bins4, UInt_t bins5,
300 Double_t width1, Double_t width2, Double_t width3, Double_t width4, Double_t width5,
307 std::vector<TString> vars;
308 std::vector<UInt_t> binning;
309 std::vector<Double_t> width;
311 vars.push_back(TString(vars1));
312 vars.push_back(TString(vars2));
313 vars.push_back(TString(vars3));
314 vars.push_back(TString(vars4));
315 vars.push_back(TString(vars5));
316 vars.push_back(TString(weight));
317 binning.push_back(bins1);
318 binning.push_back(bins2);
319 binning.push_back(bins3);
320 binning.push_back(bins4);
321 binning.push_back(bins5);
322 width.push_back(width1);
323 width.push_back(width2);
324 width.push_back(width3);
325 width.push_back(width4);
326 width.push_back(width5);
328 init(thephaseSpace, tree, vars, binning, width, d, toyEvents, maxEvents, skipEvents);
338 std::vector<TString> &vars,
339 std::vector<UInt_t> &binning,
340 std::vector<Double_t> &width,
358 Logger::print(0, "%20.20s ERROR: Dimensionality of phase space (%d) does not match binning vector size (%d)\n",
364 Logger::print(2, "%20.20s ERROR: Dimensionality of phase space (%d) does not match kernel width vector size (%d)\n",
370 std::vector<UInt_t>::iterator i;
376 if (size > 20000000) {
395 for (n= m_dim-1; n>=0; n--) {
396 if (n==(Int_t) m_dim-1) {
408 std::vector<UInt_t> initBin( m_dim);
409 std::vector<UInt_t> finalBin( m_dim);
410 std::vector<Double_t> lowLimit( m_dim);
411 std::vector<Double_t> coeff( m_dim);
413 UInt_t size = map.size();
417 for (n=0; n<(Int_t) m_dim; n++) {
418 Double_t x1 = point[n] - m_width[n];
419 Double_t x2 = point[n] + m_width[n];
423 Int_t i1 = (int)TMath::Ceil( (x1-low)/(up-low)*( m_binning[n]-1) );
426 Int_t i2 = (int)TMath::Floor( (x2-low)/(up-low)*( m_binning[n]-1) );
431 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);
440 lowLimit[n] -= point[n];
442 coeff[n] /= m_width[n];
443 lowLimit[n] /= m_width[n];
447 std::vector<UInt_t> iter( m_dim);
448 for (n=0; n<(Int_t) m_dim; n++) iter[n] = initBin[n];
455 Logger::print(2, "%20.20s ERROR: index (%d) is larger than array size (%d)\n", m_name, index, size);
459 for (n=0; n<(Int_t) m_dim; n++) {
464 Double_t dx = lowLimit[n] + (Double_t)iter[n]*coeff[n];
465 if (fabs(dx) < 1.) sqsum += dx*dx;
467 if (sqsum < 1.) map[index] += weight*(1.-sqsum);
473 for (n=0; n<(Int_t) m_dim; n++) {
474 if (iter[n] < finalBin[n]) {
479 iter[n] = initBin[n];
490 if (vars.size() != m_dim && vars.size() != m_dim + 1) {
491 Logger::print(2, "%20.20s ERROR: Number of TTree variables (%d) in tree \"%s\" does not correspond to phase space dimensionality (%d)\n",
496 if (vars.size() == m_dim + 1) {
497 Logger::print(0, "%20.20s INFO: Using variable \"%s\" as weight\n",
501 UInt_t nvars = vars.size();
503 tree->ResetBranchAddresses();
505 Long64_t nentries = tree->GetEntries();
506 if (maxEvents > 0 && skipEvents + maxEvents < nentries) nentries = skipEvents + maxEvents;
508 Logger::print(0, "%20.20s INFO: Will read %lld events (skipping first %d)\n",
509 m_name, nentries-skipEvents, skipEvents );
513 std::vector<Float_t> varArray(nvars);
516 for (n=0; n < (Int_t)nvars; n++) {
517 Logger::print(0, "%20.20s INFO: Will read branch \"%s\" from tree \"%s\"\n", m_name, vars[n].Data(), tree->GetName());
518 Int_t status = tree->SetBranchAddress(vars[n], &( varArray[n] ));
520 if (n+1 == (Int_t)nvars) {
521 Logger::print(1, "%20.20s WARNING: Weight branch \"%s\" not found, reverting to unweighted mode!\n", m_name, vars[n].Data() );
530 std::vector<Double_t> point( m_dim);
536 for(i=skipEvents; i<nentries; i++) {
538 for (n=0; n<(Int_t) m_dim; n++) {
539 point[n] = varArray[n];
541 Double_t weight = 1.;
542 if ( m_dim + 1 == nvars)
543 weight = varArray[ m_dim];
557 Logger::print(0, "%20.20s INFO: Read %lld/%lld events (%f%%), %lld out\n", m_name, i-skipEvents, nentries-skipEvents,
558 100.* float(i-skipEvents)/ float(nentries-skipEvents), nout);
562 Logger::print(0, "%20.20s INFO: %lld events read in from \"%s\", %lld out\n", m_name, nentries-nout, tree->GetName(), nout );
568 std::vector<Double_t> x( m_dim);
571 if (theDensity == 0) {
577 if (toyEvents == 0) {
579 Logger::print(0, "%20.20s INFO: Convolution of approx. density using rectangular grid\n", m_name);
582 std::vector<UInt_t> iter( m_dim);
584 for (j=0; j<(Int_t) m_dim; j++) {
594 for (j= m_dim-1; j>=0; j--) {
597 x[j] = low + (Double_t)iter[j]/((Double_t) m_binning[j]-1)*(up-low);
601 if (theDensity) e = theDensity-> density(x);
610 for (j=0; j<(Int_t) m_dim; j++) {
627 Logger::print(0, "%20.20s INFO: Convolution of approx. density using MC with %d events\n", m_name, toyEvents);
629 std::vector<Double_t> lower( m_dim);
630 std::vector<Double_t> coeff( m_dim);
631 for (i=0; i<(Int_t) m_dim; i++) {
637 for (i=0; i<(Int_t)toyEvents; i++) {
645 for (var = 0; var < m_dim; var++) {
646 x[var] = lower[var] + m_rnd.Rndm()*coeff[var];
656 Logger::print(1, "%20.20s WARNING: failed to generate a point within phase space after %d tries\n", m_name, m_maxTries);
661 if (theDensity) e = theDensity-> density(x);
664 Logger::print(0, "%20.20s INFO: toy event %d/%d (%f%%), density=%f\n", m_name, i, toyEvents, 100* float(i)/ float(toyEvents), e);
674 if (TString(filename).EndsWith( ".root") ) {
684 Logger::print(0, "%20.20s INFO: Writing binned density to text file \"%s\"\n", m_name, filename );
686 FILE* file = fopen(filename, "w+");
687 fprintf(file, "%d\n", m_dim);
690 for (j=0; j<(Int_t) m_dim; j++) {
695 std::vector<Double_t> x( m_dim);
696 std::vector<UInt_t> iter( m_dim);
697 for (j=0; j<(Int_t) m_dim; j++) {
701 UInt_t size = m_map.size();
707 for (j= m_dim-1; j>=0; j--) {
710 x[j] = low + (Double_t)iter[j]/((Double_t) m_binning[j]-1)*(up-low);
711 if (j==(Int_t) m_dim-1) {
721 Logger::print(2, "%20.20s ERROR: index (%d) is larger than array size (%d)\n", m_name, index, size);
728 for (j=0; j<(Int_t) m_dim; j++) {
746 Logger::print(0, "%20.20s INFO: Writing binned density to ROOT file \"%s\"\n", m_name, filename );
748 TDirectory* curr_dir = gDirectory;
750 TFile* file = TFile::Open(filename, "RECREATE");
751 file->SetCompressionSettings(ROOT::CompressionSettings(ROOT::kLZMA, 9));
752 TTree dimTree( "DimTree", "DimTree");
755 dimTree.Branch( "bins",&bins, "bins/I");
758 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();
773 TTree mapTree( "MapTree", "MapTree");
777 mapTree.Branch( "dens", &dens, "dens/F");
778 mapTree.Branch( "inphsp",&inphsp, "inphsp/B");
784 for (j= m_dim-1; j>=0; j--) {
787 x[j] = low + (Double_t)iter[j]/((Double_t) m_binning[j]-1)*(up-low);
788 if (j==(Int_t) m_dim-1) {
798 Logger::print(2, "%20.20s ERROR: index (%d) is larger than array size (%d)\n", m_name, index, size);
807 for (j=0; j<(Int_t) m_dim; j++) {
823 gDirectory = curr_dir;
835 std::vector<Double_t> x( m_dim);
836 std::vector<UInt_t> iter( m_dim);
837 UInt_t size = m_map.size();
841 for (j=0; j<(Int_t) m_dim; j++) {
849 for (j= m_dim-1; j>=0; j--) {
852 x[j] = low + (Double_t)iter[j]/((Double_t) m_binning[j]-1)*(up-low);
853 if (j==(Int_t) m_dim-1) {
863 Logger::print(2, "%20.20s ERROR: index (%d) is larger than array size (%d)\n", m_name, index, size);
873 for (j=0; j<(Int_t) m_dim; j++) {
885 sum /= (Double_t)num;
887 Logger::print(0, "%20.20s INFO: Average PDF value before normalisation is %f\n", m_name, sum);
890 for (j=0; j<(Int_t)size; j++) m_map[j] /= sum;
898 std::vector<UInt_t> ivect( m_dim);
900 for (j=0; j<(Int_t) m_dim; j++) {
904 if (xj < low || xj > up) {
907 Int_t ij = (Int_t)floor((xj-low)/(up-low)*( m_binning[j]-1));
917 std::vector<UInt_t> iter( m_dim);
919 for (j=0; j<(Int_t) m_dim; j++) {
928 for (j=0; j<(Int_t) m_dim; j++) {
932 Double_t xj1 = low + ((Double_t)ivect[j] )/((Double_t) m_binning[j]-1.)*(up-low);
933 Double_t xj2 = low + ((Double_t)ivect[j]+1.)/((Double_t) m_binning[j]-1.)*(up-low);
941 fweight = 1. - (x[j]-xj1)/(xj2-xj1);
943 fweight = (x[j]-xj1)/(xj2-xj1);
961 for (j= m_dim-1; j>=0; j--) {
962 Int_t ij = ivect[j] + iter[j];
963 if (j==(Int_t) m_dim-1) {
970 e += weight*map[index];
977 for (j=0; j<(Int_t) m_dim; j++) {
const char * name(void) Return the name of the PDF.
void writeToRootFile(const char *fileName) Write the binned density into a ROOT file.
AbsPhaseSpace * m_phaseSpace Reference to phase space.
std::vector< Double_t > m_width Vector of kernel widths.
virtual Double_t upperLimit(UInt_t var)=0 Return upper allowed limit of the variable.
Abstract class which defines probability density interface.
std::vector< Double_t > m_map Bin map of estimated PDF.
BinnedKernelDensity(const char *pdfName, AbsPhaseSpace *thePhaseSpace, TTree *tree, const char *vars1, UInt_t bins1, Double_t width1, AbsDensity *approx=0, UInt_t toyEvents=0, UInt_t maxEvents=0, UInt_t skipEvents=0) Constructor for 1-dimensional kernel PDF with binned interpolation from the sample of points in an NT...
void print(int level, const char *format,...)
char m_name[256] PDF name.
void normalise(void) Normalise the PDF such that the average PDF value over the allowed phase space equals to 1...
Bool_t m_fractionalMode Fractional mode flag.
std::vector< UInt_t > m_binning Vector of bin numbers in each variable.
TRandom3 m_rnd Random number generator.
UInt_t iterToIndex(std::vector< UInt_t > &iter) Convert an N-dimensional iterator vector into a linear bin index in the bin map.
Double_t density(std::vector< Double_t > &x) Calculate PDF density at the point. Multilinear binned interpolation is used which is calculated in t...
Abstract class which defines phase space interface.
virtual UInt_t dimensionality()=0 Get dimensionality of phase space.
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.
void fillMapFromTree(TTree *tree, std::vector< TString > &vars, UInt_t maxEvents=0, UInt_t skipEvents=0)
int timer(int secs) Return 1 if more than a certain number of seconds have passed since the last call of this function or...
void addToMap(std::vector< Double_t > &map, std::vector< Double_t > &point, Double_t weight=1.) Add a kernel density of a single data point to the binned map.
virtual ~BinnedKernelDensity() Destructor.
void init(AbsPhaseSpace *thePhaseSpace, TTree *tree, std::vector< TString > &vars, std::vector< UInt_t > &binning, std::vector< Double_t > &width, AbsDensity *approx=0, UInt_t toyEvents=0, UInt_t maxEvents=0, UInt_t skipEvents=0) Common initialise method used by all constructors.
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...
virtual Bool_t withinLimits(std::vector< Double_t > &x)=0 Check if the point is within phase space limits.
virtual Double_t density(std::vector< Double_t > &x)=0 Calculate PDF value at the given point.
void writeToTextFile(const char *fileName) Write the binned PDF to a text file.
void writeToFile(const char *fileName) Write density map to a file, ROOT or text depending on extension.
void fillMapFromDensity(AbsDensity *density, UInt_t toyEvents=0)
AbsPhaseSpace * phaseSpace() Return the phase space.
UInt_t m_dim Cached dimensionality of the phase space.
std::vector< Double_t > m_approxMap Bin map of approximation PDF convolved with the kernel.
AbsDensity * m_approxDensity Reference to approximation PDF.
|