// @(#)root/tmva $Id$
// Author: Tancredi Carli, Dominik Dannheim, Alexander Voigt

/**********************************************************************************
 * Project: TMVA - a Root-integrated toolkit for multivariate data analysis       *
 * Package: TMVA                                                                  *
 * Classes: PDEFoamDiscriminantDensity                                            *
 * Web    : http://tmva.sourceforge.net                                           *
 *                                                                                *
 * Description:                                                                   *
 *      The TFDSITR class provides an interface between the Binary search tree    *
 *      and the PDEFoam object.  In order to build-up the foam one needs to       *
 *      calculate the density of events at a given point (sampling during         *
 *      Foam build-up).  The function PDEFoamDiscriminantDensity::Density() does this job.  It  *
 *      uses a binary search tree, filled with training events, in order to       *
 *      provide this density.                                                     *
 *                                                                                *
 * Authors (alphabetical):                                                        *
 *      Tancredi Carli   - CERN, Switzerland                                      *
 *      Dominik Dannheim - CERN, Switzerland                                      *
 *      S. Jadach        - Institute of Nuclear Physics, Cracow, Poland           *
 *      Alexander Voigt  - TU Dresden, Germany                                    *
 *      Peter Speckmayer - CERN, Switzerland                                      *
 *                                                                                *
 * Copyright (c) 2008, 2010:                                                      *
 *      CERN, Switzerland                                                         *
 *      MPI-K Heidelberg, Germany                                                 *
 *                                                                                *
 * Redistribution and use in source and binary forms, with or without             *
 * modification, are permitted according to the terms listed in LICENSE           *
 * (http://tmva.sourceforge.net/LICENSE)                                          *
 **********************************************************************************/

//_____________________________________________________________________
//
// PDEFoamDiscriminantDensity
//
// This is a concrete implementation of PDEFoam.  Density(...)
// estimates the discriminant density at a given phase-space point
// using range-searching.  The discriminant D is defined as
//
//    D = #events with given class / total number of events
// _____________________________________________________________________

#include <cmath>

#ifndef ROOT_TMVA_PDEFoamDiscriminantDensity
#include "TMVA/PDEFoamDiscriminantDensity.h"
#endif

ClassImp(TMVA::PDEFoamDiscriminantDensity)

//_____________________________________________________________________
TMVA::PDEFoamDiscriminantDensity::PDEFoamDiscriminantDensity()
   : PDEFoamDensityBase()
   , fClass(0)
{}

//_____________________________________________________________________
TMVA::PDEFoamDiscriminantDensity::PDEFoamDiscriminantDensity(std::vector<Double_t> box, UInt_t cls)
   : PDEFoamDensityBase(box)
   , fClass(cls)
{
   // User construcor:
   //
   // Parameters:
   //
   // - box - size of the range-searching box (n-dimensional
   //   std::vector)
   //
   // - cls - event class used for the range-searching
}

//_____________________________________________________________________
TMVA::PDEFoamDiscriminantDensity::PDEFoamDiscriminantDensity(const PDEFoamDiscriminantDensity &distr)
   : PDEFoamDensityBase(distr)
   , fClass(distr.fClass)
{
   // Copy constructor
}

//_____________________________________________________________________
Double_t TMVA::PDEFoamDiscriminantDensity::Density(std::vector<Double_t> &xev, Double_t &event_density)
{
   // This function is needed during the foam buildup.  It returns the
   // average number density of events of type fClass within the
   // range-searching volume (specified by fBox).
   //
   // Parameters:
   //
   // - xev - event vector (in [fXmin,fXmax]) to place the box at
   //
   // - event_density - here the event density is stored
   //
   // Returns:
   //
   // Number of events (event weights) of type fClass, which were
   // found in the range-searching volume at point 'xev', divided by
   // the box volume.

   if (!fBst)
      Log() << kFATAL << "<PDEFoamDiscriminantDensity::Density()> Binary tree not set!" << Endl;

   //create volume around point to be found
   std::vector<Double_t> lb(GetBox().size());
   std::vector<Double_t> ub(GetBox().size());

   // probevolume relative to hypercube with edge length 1:
   const Double_t probevolume_inv = 1.0 / GetBoxVolume();

   // set upper and lower bound for search volume
   for (UInt_t idim = 0; idim < GetBox().size(); ++idim) {
      lb[idim] = xev[idim] - GetBox().at(idim) / 2.0;
      ub[idim] = xev[idim] + GetBox().at(idim) / 2.0;
   }

   TMVA::Volume volume(&lb, &ub);                        // volume to search in
   std::vector<const TMVA::BinarySearchTreeNode*> nodes; // BST nodes found

   // do range searching
   const Double_t sumOfWeights = fBst->SearchVolume(&volume, &nodes);

   // store density based on total number of events
   event_density = nodes.size() * probevolume_inv;

   Double_t n_sig = 0;           // number of signal events found
   // calc number of signal events in nodes
   for (std::vector<const TMVA::BinarySearchTreeNode*>::const_iterator it = nodes.begin();
        it != nodes.end(); ++it) {
      if ((*it)->GetClass() == fClass) // signal node
         n_sig += (*it)->GetWeight();
   }

   // return:  (n_sig/n_total) / (cell_volume)
   return (n_sig / (sumOfWeights + 0.1)) * probevolume_inv;
}
 PDEFoamDiscriminantDensity.cxx:1
 PDEFoamDiscriminantDensity.cxx:2
 PDEFoamDiscriminantDensity.cxx:3
 PDEFoamDiscriminantDensity.cxx:4
 PDEFoamDiscriminantDensity.cxx:5
 PDEFoamDiscriminantDensity.cxx:6
 PDEFoamDiscriminantDensity.cxx:7
 PDEFoamDiscriminantDensity.cxx:8
 PDEFoamDiscriminantDensity.cxx:9
 PDEFoamDiscriminantDensity.cxx:10
 PDEFoamDiscriminantDensity.cxx:11
 PDEFoamDiscriminantDensity.cxx:12
 PDEFoamDiscriminantDensity.cxx:13
 PDEFoamDiscriminantDensity.cxx:14
 PDEFoamDiscriminantDensity.cxx:15
 PDEFoamDiscriminantDensity.cxx:16
 PDEFoamDiscriminantDensity.cxx:17
 PDEFoamDiscriminantDensity.cxx:18
 PDEFoamDiscriminantDensity.cxx:19
 PDEFoamDiscriminantDensity.cxx:20
 PDEFoamDiscriminantDensity.cxx:21
 PDEFoamDiscriminantDensity.cxx:22
 PDEFoamDiscriminantDensity.cxx:23
 PDEFoamDiscriminantDensity.cxx:24
 PDEFoamDiscriminantDensity.cxx:25
 PDEFoamDiscriminantDensity.cxx:26
 PDEFoamDiscriminantDensity.cxx:27
 PDEFoamDiscriminantDensity.cxx:28
 PDEFoamDiscriminantDensity.cxx:29
 PDEFoamDiscriminantDensity.cxx:30
 PDEFoamDiscriminantDensity.cxx:31
 PDEFoamDiscriminantDensity.cxx:32
 PDEFoamDiscriminantDensity.cxx:33
 PDEFoamDiscriminantDensity.cxx:34
 PDEFoamDiscriminantDensity.cxx:35
 PDEFoamDiscriminantDensity.cxx:36
 PDEFoamDiscriminantDensity.cxx:37
 PDEFoamDiscriminantDensity.cxx:38
 PDEFoamDiscriminantDensity.cxx:39
 PDEFoamDiscriminantDensity.cxx:40
 PDEFoamDiscriminantDensity.cxx:41
 PDEFoamDiscriminantDensity.cxx:42
 PDEFoamDiscriminantDensity.cxx:43
 PDEFoamDiscriminantDensity.cxx:44
 PDEFoamDiscriminantDensity.cxx:45
 PDEFoamDiscriminantDensity.cxx:46
 PDEFoamDiscriminantDensity.cxx:47
 PDEFoamDiscriminantDensity.cxx:48
 PDEFoamDiscriminantDensity.cxx:49
 PDEFoamDiscriminantDensity.cxx:50
 PDEFoamDiscriminantDensity.cxx:51
 PDEFoamDiscriminantDensity.cxx:52
 PDEFoamDiscriminantDensity.cxx:53
 PDEFoamDiscriminantDensity.cxx:54
 PDEFoamDiscriminantDensity.cxx:55
 PDEFoamDiscriminantDensity.cxx:56
 PDEFoamDiscriminantDensity.cxx:57
 PDEFoamDiscriminantDensity.cxx:58
 PDEFoamDiscriminantDensity.cxx:59
 PDEFoamDiscriminantDensity.cxx:60
 PDEFoamDiscriminantDensity.cxx:61
 PDEFoamDiscriminantDensity.cxx:62
 PDEFoamDiscriminantDensity.cxx:63
 PDEFoamDiscriminantDensity.cxx:64
 PDEFoamDiscriminantDensity.cxx:65
 PDEFoamDiscriminantDensity.cxx:66
 PDEFoamDiscriminantDensity.cxx:67
 PDEFoamDiscriminantDensity.cxx:68
 PDEFoamDiscriminantDensity.cxx:69
 PDEFoamDiscriminantDensity.cxx:70
 PDEFoamDiscriminantDensity.cxx:71
 PDEFoamDiscriminantDensity.cxx:72
 PDEFoamDiscriminantDensity.cxx:73
 PDEFoamDiscriminantDensity.cxx:74
 PDEFoamDiscriminantDensity.cxx:75
 PDEFoamDiscriminantDensity.cxx:76
 PDEFoamDiscriminantDensity.cxx:77
 PDEFoamDiscriminantDensity.cxx:78
 PDEFoamDiscriminantDensity.cxx:79
 PDEFoamDiscriminantDensity.cxx:80
 PDEFoamDiscriminantDensity.cxx:81
 PDEFoamDiscriminantDensity.cxx:82
 PDEFoamDiscriminantDensity.cxx:83
 PDEFoamDiscriminantDensity.cxx:84
 PDEFoamDiscriminantDensity.cxx:85
 PDEFoamDiscriminantDensity.cxx:86
 PDEFoamDiscriminantDensity.cxx:87
 PDEFoamDiscriminantDensity.cxx:88
 PDEFoamDiscriminantDensity.cxx:89
 PDEFoamDiscriminantDensity.cxx:90
 PDEFoamDiscriminantDensity.cxx:91
 PDEFoamDiscriminantDensity.cxx:92
 PDEFoamDiscriminantDensity.cxx:93
 PDEFoamDiscriminantDensity.cxx:94
 PDEFoamDiscriminantDensity.cxx:95
 PDEFoamDiscriminantDensity.cxx:96
 PDEFoamDiscriminantDensity.cxx:97
 PDEFoamDiscriminantDensity.cxx:98
 PDEFoamDiscriminantDensity.cxx:99
 PDEFoamDiscriminantDensity.cxx:100
 PDEFoamDiscriminantDensity.cxx:101
 PDEFoamDiscriminantDensity.cxx:102
 PDEFoamDiscriminantDensity.cxx:103
 PDEFoamDiscriminantDensity.cxx:104
 PDEFoamDiscriminantDensity.cxx:105
 PDEFoamDiscriminantDensity.cxx:106
 PDEFoamDiscriminantDensity.cxx:107
 PDEFoamDiscriminantDensity.cxx:108
 PDEFoamDiscriminantDensity.cxx:109
 PDEFoamDiscriminantDensity.cxx:110
 PDEFoamDiscriminantDensity.cxx:111
 PDEFoamDiscriminantDensity.cxx:112
 PDEFoamDiscriminantDensity.cxx:113
 PDEFoamDiscriminantDensity.cxx:114
 PDEFoamDiscriminantDensity.cxx:115
 PDEFoamDiscriminantDensity.cxx:116
 PDEFoamDiscriminantDensity.cxx:117
 PDEFoamDiscriminantDensity.cxx:118
 PDEFoamDiscriminantDensity.cxx:119
 PDEFoamDiscriminantDensity.cxx:120
 PDEFoamDiscriminantDensity.cxx:121
 PDEFoamDiscriminantDensity.cxx:122
 PDEFoamDiscriminantDensity.cxx:123
 PDEFoamDiscriminantDensity.cxx:124
 PDEFoamDiscriminantDensity.cxx:125
 PDEFoamDiscriminantDensity.cxx:126
 PDEFoamDiscriminantDensity.cxx:127
 PDEFoamDiscriminantDensity.cxx:128
 PDEFoamDiscriminantDensity.cxx:129
 PDEFoamDiscriminantDensity.cxx:130
 PDEFoamDiscriminantDensity.cxx:131
 PDEFoamDiscriminantDensity.cxx:132
 PDEFoamDiscriminantDensity.cxx:133
 PDEFoamDiscriminantDensity.cxx:134
 PDEFoamDiscriminantDensity.cxx:135
 PDEFoamDiscriminantDensity.cxx:136