// @(#)root/tmva \$Id\$
// Author: Peter Speckmayer

/**********************************************************************************
* Project: TMVA - a Root-integrated toolkit for multivariate data analysis       *
* Package: TMVA                                                                  *
* Class  : TMVA::Interval                                                        *
* Web    : http://tmva.sourceforge.net                                           *
*                                                                                *
* Description:                                                                   *
*      Implementation (see header for description)                               *
*                                                                                *
* Authors (alphabetical):                                                        *
*      Peter Speckmayer <speckmay@mail.cern.ch>  - CERN, Switzerland             *
*                                                                                *
*      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           *
*                                                                                *
* File and Version Information:                                                  *
**********************************************************************************/

//////////////////////////////////////////////////////////////////////////////
//                                                                          //
// Interval                                                                 //
//                                                                          //
// Interval definition, continuous and discrete                             //
//                                                                          //
// Interval(min,max)  : a continous interval [min,max]                      //
// Interval(min,max,n): a "discrete interval" [min,max], i.e the n numbers: //
//          min, min+step, min+2*step,...., min+(n-1)*step, min+n*step=max  //
//   e.g.: Interval(1,5,5)=1,2,3,4,5                                        //
//         Interval(.5,1.,6)= .5, .6., .7, .8, .9, 1.0                      //
//                                                                          //
//  Note: **bin** counting starts from ZERO unlike in ROOT histograms       //
//////////////////////////////////////////////////////////////////////////////
/* Begin_Html
<center><h2>the TMVA::Interval Class</h2></center>

<ul>
<li> Interval definition, continuous and discrete
<ul>
<li>  Interval(min,max)  : a continous interval [min,max]
<li>  Interval(min,max,n): a "discrete interval" [min,max], i.e the n numbers:<br>
min, min+step, min+2*step,...., min+(n-1)*step=max <br>
e.g.: Interval(1,5,5)=1,2,3,4,5                    <br>
Interval(.5,1.,6)= .5, .6., .7, .8, .9, 1.0        <br>

</ul>
</ul>
<pre>

Example:   Interval(.5,1.,6)

[ min                           max ]
------------------------------------------------------------
|     |     |     |     |     |
.5    .6    .7    .8    .9    1.0

bin    0     1     2     3     4     5

</pre>
End_Html */

#include "TMath.h"
#include "TRandom3.h"

#include "TMVA/Interval.h"
#include "TMVA/MsgLogger.h"

ClassImp(TMVA::Interval)

//_______________________________________________________________________
TMVA::Interval::Interval( Double_t min, Double_t max, Int_t nbins ) :
fMin(min),
fMax(max),
fNbins(nbins)
{
// defines minimum and maximum of an interval
// when nbins > 0, interval describes a discrete distribution (equally distributed in the interval)
// when nbins == 0, interval describes a continous interval
//
if (fMax - fMin < 0) Log() << kFATAL << "maximum lower than minimum" << Endl;
if (nbins < 0) {
Log() << kFATAL << "nbins < 0" << Endl;
return;
}
else if (nbins == 1) {
Log() << kFATAL << "interval has to have at least 2 bins if discrete" << Endl;
return;
}
}

TMVA::Interval::Interval( const Interval& other ) :
fMin  ( other.fMin ),
fMax  ( other.fMax ),
fNbins( other.fNbins )
{
}

//_______________________________________________________________________
TMVA::Interval::~Interval()
{
// destructor
}

//_______________________________________________________________________
Double_t TMVA::Interval::GetElement( Int_t bin ) const
{
// calculates the value of the "number" bin in a discrete interval.
// Parameters:
//        Double_t position
//
if (fNbins <= 0) {
Log() << kFATAL << "GetElement only defined for discrete value Intervals" << Endl;
return 0.0;
}
else if (bin < 0 || bin >= fNbins) {
Log() << kFATAL << "bin " << bin << " out of range: interval *bins* count from 0 to " << fNbins-1  << Endl;
return 0.0;
}
return fMin + ( (Double_t(bin)/(fNbins-1)) *(fMax - fMin) );
}

//_______________________________________________________________________
Double_t TMVA::Interval::GetStepSize( Int_t iBin )  const
{
// retuns the step size between the numbers of a "discrete Interval"
if (fNbins <= 0) {
Log() << kFATAL << "GetElement only defined for discrete value Intervals" << Endl;
}
if (iBin<0) {
Log() << kFATAL << "You asked for iBin=" << iBin
<<" in interval .. and.. sorry, I cannot let this happen.."<<Endl;
}
return (fMax-fMin)/(Double_t)(fNbins-1);
}

//_______________________________________________________________________
Double_t TMVA::Interval::GetRndm( TRandom3& rnd )  const
{
// get uniformely distributed number within interval
return rnd.Rndm()*(fMax - fMin) + fMin;
}

Double_t TMVA::Interval::GetWidth() const
{
return fMax - fMin;
}
Double_t TMVA::Interval::GetMean()  const
{
return (fMax + fMin)/2;
}

void TMVA::Interval::Print(std::ostream &os) const
{
for (Int_t i=0; i<GetNbins(); i++){
os << "| " << GetElement(i)<<" |" ;
}
}

TMVA::MsgLogger& TMVA::Interval::Log() const {
return logger;
}
Interval.cxx:1
Interval.cxx:2
Interval.cxx:3
Interval.cxx:4
Interval.cxx:5
Interval.cxx:6
Interval.cxx:7
Interval.cxx:8
Interval.cxx:9
Interval.cxx:10
Interval.cxx:11
Interval.cxx:12
Interval.cxx:13
Interval.cxx:14
Interval.cxx:15
Interval.cxx:16
Interval.cxx:17
Interval.cxx:18
Interval.cxx:19
Interval.cxx:20
Interval.cxx:21
Interval.cxx:22
Interval.cxx:23
Interval.cxx:24
Interval.cxx:25
Interval.cxx:26
Interval.cxx:27
Interval.cxx:28
Interval.cxx:29
Interval.cxx:30
Interval.cxx:31
Interval.cxx:32
Interval.cxx:33
Interval.cxx:34
Interval.cxx:35
Interval.cxx:36
Interval.cxx:37
Interval.cxx:38
Interval.cxx:39
Interval.cxx:40
Interval.cxx:41
Interval.cxx:42
Interval.cxx:43
Interval.cxx:44
Interval.cxx:45
Interval.cxx:46
Interval.cxx:47
Interval.cxx:48
Interval.cxx:49
Interval.cxx:50
Interval.cxx:51
Interval.cxx:52
Interval.cxx:53
Interval.cxx:54
Interval.cxx:55
Interval.cxx:56
Interval.cxx:57
Interval.cxx:58
Interval.cxx:59
Interval.cxx:60
Interval.cxx:61
Interval.cxx:62
Interval.cxx:63
Interval.cxx:64
Interval.cxx:65
Interval.cxx:66
Interval.cxx:67
Interval.cxx:68
Interval.cxx:69
Interval.cxx:70
Interval.cxx:71
Interval.cxx:72
Interval.cxx:73
Interval.cxx:74
Interval.cxx:75
Interval.cxx:76
Interval.cxx:77
Interval.cxx:78
Interval.cxx:79
Interval.cxx:80
Interval.cxx:81
Interval.cxx:82
Interval.cxx:83
Interval.cxx:84
Interval.cxx:85
Interval.cxx:86
Interval.cxx:87
Interval.cxx:88
Interval.cxx:89
Interval.cxx:90
Interval.cxx:91
Interval.cxx:92
Interval.cxx:93
Interval.cxx:94
Interval.cxx:95
Interval.cxx:96
Interval.cxx:97
Interval.cxx:98
Interval.cxx:99
Interval.cxx:100
Interval.cxx:101
Interval.cxx:102
Interval.cxx:103
Interval.cxx:104
Interval.cxx:105
Interval.cxx:106
Interval.cxx:107
Interval.cxx:108
Interval.cxx:109
Interval.cxx:110
Interval.cxx:111
Interval.cxx:112
Interval.cxx:113
Interval.cxx:114
Interval.cxx:115
Interval.cxx:116
Interval.cxx:117
Interval.cxx:118
Interval.cxx:119
Interval.cxx:120
Interval.cxx:121
Interval.cxx:122
Interval.cxx:123
Interval.cxx:124
Interval.cxx:125
Interval.cxx:126
Interval.cxx:127
Interval.cxx:128
Interval.cxx:129
Interval.cxx:130
Interval.cxx:131
Interval.cxx:132
Interval.cxx:133
Interval.cxx:134
Interval.cxx:135
Interval.cxx:136
Interval.cxx:137
Interval.cxx:138
Interval.cxx:139
Interval.cxx:140
Interval.cxx:141
Interval.cxx:142
Interval.cxx:143
Interval.cxx:144
Interval.cxx:145
Interval.cxx:146
Interval.cxx:147
Interval.cxx:148
Interval.cxx:149
Interval.cxx:150
Interval.cxx:151
Interval.cxx:152
Interval.cxx:153
Interval.cxx:154
Interval.cxx:155
Interval.cxx:156
Interval.cxx:157
Interval.cxx:158
Interval.cxx:159
Interval.cxx:160
Interval.cxx:161
Interval.cxx:162
Interval.cxx:163
Interval.cxx:164
Interval.cxx:165
Interval.cxx:166
Interval.cxx:167
Interval.cxx:168
Interval.cxx:169
Interval.cxx:170
Interval.cxx:171