#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <assert.h>
#include "TMVA/Event.h"
#include "TMVA/Tools.h"
#include "TMVA/PDEFoam.h"
#include "TMVA/MsgLogger.h"
#include "TMVA/Types.h"
#include "TStyle.h"
#include "TObject.h"
#include "TH1.h"
#include "TMath.h"
#include "TVectorT.h"
#include "TRandom3.h"
#include "TRefArray.h"
#include "TMethodCall.h"
#include "G__ci.h"
#include "TColor.h"
#include "TSystem.h"
ClassImp(TMVA::PDEFoam)
static const Float_t gHigh= FLT_MAX;
static const Float_t gVlow=-FLT_MAX;
using namespace std;
#define SW2 setprecision(7) << setw(12)
TMVA::PDEFoam::PDEFoam() :
fLogger(new MsgLogger("PDEFoam"))
{
fDim = 0;
fNoAct = 0;
fNCells = 0;
fRNmax = 0;
fMaskDiv = 0;
fInhiDiv = 0;
fXdivPRD = 0;
fCells = 0;
fAlpha = 0;
fCellsAct = 0;
fPrimAcu = 0;
fHistEdg = 0;
fHistWt = 0;
fHistDbg = 0;
fMCMonit = 0;
fRho = 0;
fMCvect = 0;
fRvec = 0;
fPseRan = 0;
fXmin = 0;
fXmax = 0;
fNElements = 0;
fCutNmin = kTRUE;
fNmin = 100;
fCutRMSmin = kFALSE;
fRMSmin = 1.0;
SetPDEFoamVolumeFraction(-1.);
fSignalClass = -1;
fBackgroundClass = -1;
fDistr = new TFDISTR();
fDistr->SetSignalClass( fSignalClass );
fDistr->SetBackgroundClass( fBackgroundClass );
fTimer = new Timer(fNCells, "PDEFoam", kTRUE);
fVariableNames = new TObjArray();
}
TMVA::PDEFoam::PDEFoam(const TString& Name) :
fLogger(new MsgLogger("PDEFoam"))
{
if(strlen(Name) >129) {
Log() << kFATAL << "Name too long " << Name.Data() << Endl;
}
fName=Name;
fDate=" Release date: 2005.04.10";
fVersion= "1.02M";
fMaskDiv = 0;
fInhiDiv = 0;
fXdivPRD = 0;
fCells = 0;
fAlpha = 0;
fCellsAct = 0;
fPrimAcu = 0;
fHistEdg = 0;
fHistWt = 0;
fHistDbg = 0;
fDim = 0;
fNCells = 1000;
fNSampl = 200;
fOptPRD = 0;
fOptDrive = 2;
fChat = 1;
fOptRej = 1;
fNBin = 8;
fEvPerBin =25;
fNCalls = 0;
fNEffev = 0;
fLastCe =-1;
fNoAct = 0;
fWtMin = gHigh;
fWtMax = gVlow;
fMaxWtRej =1.10;
fPseRan = 0;
fMCMonit = 0;
fRho = 0;
fMethodCall=0;
fXmin = 0;
fXmax = 0;
fCutNmin = kFALSE;
fCutRMSmin = kFALSE;
SetPDEFoamVolumeFraction(-1.);
fSignalClass = -1;
fBackgroundClass = -1;
fDistr = new TFDISTR();
fDistr->SetSignalClass( fSignalClass );
fDistr->SetBackgroundClass( fBackgroundClass );
fTimer = new Timer(fNCells, "PDEFoam", kTRUE);
fVariableNames = new TObjArray();
Log().SetSource( "PDEFoam" );
}
TMVA::PDEFoam::~PDEFoam()
{
delete fVariableNames;
delete fTimer;
delete fDistr;
if (fXmin) delete [] fXmin; fXmin=0;
if (fXmax) delete [] fXmax; fXmax=0;
Int_t i;
if(fCells!= 0) {
for(i=0; i<fNCells; i++) delete fCells[i];
delete [] fCells;
}
delete [] fRvec;
delete [] fAlpha;
delete [] fMCvect;
delete [] fPrimAcu;
delete [] fMaskDiv;
delete [] fInhiDiv;
if( fXdivPRD!= 0) {
for(i=0; i<fDim; i++) delete fXdivPRD[i];
delete [] fXdivPRD;
}
delete fMCMonit;
delete fHistWt;
delete fLogger;
}
TMVA::PDEFoam::PDEFoam(const PDEFoam &From):
TObject(From),
fLogger( new MsgLogger("PDEFoam"))
{
Log() << kFATAL << "COPY CONSTRUCTOR NOT IMPLEMENTED" << Endl;
}
void TMVA::PDEFoam::Initialize(Bool_t CreateCellElements)
{
Bool_t addStatus = TH1::AddDirectoryStatus();
TH1::AddDirectory(kFALSE);
Int_t i;
if(fPseRan==0) Log() << kFATAL << "Random number generator not set" << Endl;
if(fRho==0 && fMethodCall==0 ) Log() << kFATAL << "Distribution function not set" << Endl;
if(fDim==0) Log() << kFATAL << "Zero dimension not allowed" << Endl;
fRNmax= fDim+1;
fRvec = new Double_t[fRNmax];
if(fRvec==0) Log() << kFATAL << "Cannot initialize buffer fRvec" << Endl;
if(fDim>0){
fAlpha = new Double_t[fDim];
if(fAlpha==0) Log() << kFATAL << "Cannot initialize buffer fAlpha" << Endl;
}
fMCvect = new Double_t[fDim];
if(fMCvect==0) Log() << kFATAL << "Cannot initialize buffer fMCvect" << Endl;
if(fInhiDiv == 0){
fInhiDiv = new Int_t[fDim];
for(i=0; i<fDim; i++) fInhiDiv[i]=0;
}
if(fMaskDiv == 0){
fMaskDiv = new Int_t[fDim];
for(i=0; i<fDim; i++) fMaskDiv[i]=1;
}
if(fXdivPRD == 0){
fXdivPRD = new PDEFoamVect*[fDim];
for(i=0; i<fDim; i++) fXdivPRD[i]=0;
}
fHistWt = new TH1D("HistWt","Histogram of MC weight",100,0.0, 1.5*fMaxWtRej);
fHistEdg = new TObjArray(fDim);
TString hname;
TString htitle;
for(i=0;i<fDim;i++){
hname=fName+TString("_HistEdge_");
hname+=i;
htitle=TString("Edge Histogram No. ");
htitle+=i;
(*fHistEdg)[i] = new TH1D(hname.Data(),htitle.Data(),fNBin,0.0, 1.0);
((TH1D*)(*fHistEdg)[i])->Sumw2();
}
fHistDbg = new TObjArray(fDim);
for(i=0;i<fDim;i++){
hname=fName+TString("_HistDebug_");
hname+=i;
htitle=TString("Debug Histogram ");
htitle+=i;
(*fHistDbg)[i] = new TH1D(hname.Data(),htitle.Data(),fNBin,0.0, 1.0);
}
InitCells(CreateCellElements);
Grow();
MakeActiveList();
fSumWt = 0.0;
fSumWt2 = 0.0;
fSumOve = 0.0;
fNevGen = 0.0;
fWtMax = gVlow;
fWtMin = gHigh;
fMCresult=fCells[0]->GetIntg();
fMCresult=fCells[0]->GetIntg();
fMCerror =fCells[0]->GetIntg();
fMCMonit = new PDEFoamMaxwt(5.0,1000);
if(fChat==2) PrintCells();
TH1::AddDirectory(addStatus);
}
void TMVA::PDEFoam::Initialize(TRandom3*PseRan, PDEFoamIntegrand *fun )
{
SetPseRan(PseRan);
SetRho(fun);
Initialize(kFALSE);
}
void TMVA::PDEFoam::InitCells(Bool_t CreateCellElements)
{
Int_t i;
fLastCe =-1;
if(fCells!= 0) {
for(i=0; i<fNCells; i++) delete fCells[i];
delete [] fCells;
}
fCells = new PDEFoamCell*[fNCells];
for(i=0;i<fNCells;i++){
fCells[i]= new PDEFoamCell(fDim);
fCells[i]->SetSerial(i);
}
if(fCells==0) Log() << kFATAL << "Cannot initialize CELLS" << Endl;
if (CreateCellElements)
ResetCellElements(true);
CellFill(1, 0);
for(Long_t iCell=0; iCell<=fLastCe; iCell++){
Explore( fCells[iCell] );
}
}
Int_t TMVA::PDEFoam::CellFill(Int_t Status, PDEFoamCell *parent)
{
PDEFoamCell *cell;
if (fLastCe==fNCells){
Log() << kFATAL << "Too many cells" << Endl;
}
fLastCe++;
if (Status==1) fNoAct++;
cell = fCells[fLastCe];
cell->Fill(Status, parent, 0, 0);
cell->SetBest( -1);
cell->SetXdiv(0.5);
Double_t xInt2,xDri2;
if(parent!=0){
xInt2 = 0.5*parent->GetIntg();
xDri2 = 0.5*parent->GetDriv();
cell->SetIntg(xInt2);
cell->SetDriv(xDri2);
}else{
cell->SetIntg(0.0);
cell->SetDriv(0.0);
}
return fLastCe;
}
void TMVA::PDEFoam::Explore(PDEFoamCell *cell)
{
Double_t wt, dx, xBest, yBest;
Double_t intOld, driOld;
Long_t iev;
Double_t nevMC;
Int_t i, j, k;
Int_t nProj, kBest;
Double_t ceSum[5], xproj;
Double_t event_density = 0;
Double_t totevents = 0;
Double_t toteventsOld = 0;
PDEFoamVect cellSize(fDim);
PDEFoamVect cellPosi(fDim);
cell->GetHcub(cellPosi,cellSize);
PDEFoamCell *parent;
Double_t *xRand = new Double_t[fDim];
Double_t *volPart=0;
cell->CalcVolume();
dx = cell->GetVolume();
intOld = cell->GetIntg();
driOld = cell->GetDriv();
if (CutNmin())
toteventsOld = GetCellEvents(cell);
ceSum[0]=0;
ceSum[1]=0;
ceSum[2]=0;
ceSum[3]=gHigh;
ceSum[4]=gVlow;
for (i=0;i<fDim;i++) ((TH1D *)(*fHistEdg)[i])->Reset();
fHistWt->Reset();
Double_t nevEff=0.;
if (fNSampl==0) {
Double_t *cellPosiarr = new Double_t[Int_t(fDim)];
Double_t *cellSizearr = new Double_t[Int_t(fDim)];
for (Int_t idim=0; idim<fDim; idim++) {
cellPosiarr[idim]=cellPosi[idim];
cellSizearr[idim]=cellSize[idim];
}
delete[] cellPosiarr;
delete[] cellSizearr;
}
else {
for (iev=0;iev<fNSampl;iev++){
MakeAlpha();
if (fDim>0){
for (j=0; j<fDim; j++)
xRand[j]= cellPosi[j] +fAlpha[j]*(cellSize[j]);
}
wt = dx*Eval(xRand, event_density);
totevents += dx*event_density;
nProj = 0;
if (fDim>0) {
for (k=0; k<fDim; k++) {
xproj =fAlpha[k];
((TH1D *)(*fHistEdg)[nProj])->Fill(xproj,wt);
nProj++;
}
}
fNCalls++;
ceSum[0] += wt;
ceSum[1] += wt*wt;
ceSum[2]++;
if (ceSum[3]>wt) ceSum[3]=wt;
if (ceSum[4]<wt) ceSum[4]=wt;
nevEff = ceSum[0]*ceSum[0]/ceSum[1];
if ( nevEff >= fNBin*fEvPerBin) break;
}
totevents /= fNSampl;
}
if (cell==fCells[0] && ceSum[0]<=0.0){
if (ceSum[0]==0.0)
Log() << kFATAL << "No events were found during exploration of "
<< "root cell. Please check PDEFoam parameters nSampl "
<< "and VolFrac." << Endl;
else
Log() << kWARNING << "Negative number of events found during "
<< "exploration of root cell" << Endl;
}
for (k=0; k<fDim;k++){
fMaskDiv[k] =1;
if ( fInhiDiv[k]==1) fMaskDiv[k] =0;
}
kBest=-1;
Double_t rmin,rmax,rdiv;
if (fOptPRD) {
for (k=0; k<fDim; k++) {
rmin= cellPosi[k];
rmax= cellPosi[k] +cellSize[k];
if ( fXdivPRD[k] != 0) {
Int_t n= (fXdivPRD[k])->GetDim();
for (j=0; j<n; j++) {
rdiv=(*fXdivPRD[k])[j];
if ( (rmin +1e-99 <rdiv) && (rdiv< rmax -1e-99)) {
kBest=k;
xBest= (rdiv-cellPosi[k])/cellSize[k] ;
goto ee05;
}
}
}
}
}
ee05:
fNEffev += (Long_t)nevEff;
nevMC = ceSum[2];
Double_t intTrue = ceSum[0]/(nevMC+0.000001);
Double_t intDriv=0.;
Double_t intPrim=0.;
switch(fOptDrive){
case 1:
if (kBest == -1) Varedu(ceSum,kBest,xBest,yBest);
if (CutRMSmin())
intDriv =sqrt( ceSum[1]/nevMC -intTrue*intTrue );
else
intDriv =sqrt(ceSum[1]/nevMC) -intTrue;
intPrim =sqrt(ceSum[1]/nevMC);
break;
case 2:
if (kBest == -1) Carver(kBest,xBest,yBest);
intDriv =ceSum[4] -intTrue;
intPrim =ceSum[4];
break;
default:
Log() << kFATAL << "Wrong fOptDrive = " << Endl;
}
cell->SetBest(kBest);
cell->SetXdiv(xBest);
cell->SetIntg(intTrue);
cell->SetDriv(intDriv);
cell->SetPrim(intPrim);
if (CutNmin())
SetCellElement(cell, 0, totevents);
Double_t parIntg, parDriv;
for (parent = cell->GetPare(); parent!=0; parent = parent->GetPare()){
parIntg = parent->GetIntg();
parDriv = parent->GetDriv();
parent->SetIntg( parIntg +intTrue -intOld );
parent->SetDriv( parDriv +intDriv -driOld );
if (CutNmin())
SetCellElement( parent, 0, GetCellEvents(parent) + totevents - toteventsOld);
}
delete [] volPart;
delete [] xRand;
}
void TMVA::PDEFoam::Varedu(Double_t ceSum[5], Int_t &kBest, Double_t &xBest, Double_t &yBest)
{
Double_t nent = ceSum[2];
Double_t swAll = ceSum[0];
Double_t sswAll = ceSum[1];
Double_t ssw = sqrt(sswAll)/sqrt(nent);
Double_t swIn,swOut,sswIn,sswOut,xLo,xUp;
kBest =-1;
xBest =0.5;
yBest =1.0;
Double_t maxGain=0.0;
for(Int_t kProj=0; kProj<fDim; kProj++) {
if( fMaskDiv[kProj]) {
Double_t sigmIn =0.0; Double_t sigmOut =0.0;
Double_t sswtBest = gHigh;
Double_t gain =0.0;
Double_t xMin=0.0; Double_t xMax=0.0;
for(Int_t jLo=1; jLo<=fNBin; jLo++) {
Double_t aswIn=0; Double_t asswIn=0;
for(Int_t jUp=jLo; jUp<=fNBin;jUp++) {
aswIn += ((TH1D *)(*fHistEdg)[kProj])->GetBinContent(jUp);
asswIn += Sqr(((TH1D *)(*fHistEdg)[kProj])->GetBinError( jUp));
xLo=(jLo-1.0)/fNBin;
xUp=(jUp*1.0)/fNBin;
swIn = aswIn/nent;
swOut = (swAll-aswIn)/nent;
sswIn = sqrt(asswIn) /sqrt(nent*(xUp-xLo)) *(xUp-xLo);
sswOut= sqrt(sswAll-asswIn)/sqrt(nent*(1.0-xUp+xLo)) *(1.0-xUp+xLo);
if( (sswIn+sswOut) < sswtBest) {
sswtBest = sswIn+sswOut;
gain = ssw-sswtBest;
sigmIn = sswIn -swIn;
sigmOut = sswOut-swOut;
xMin = xLo;
xMax = xUp;
}
}
}
Int_t iLo = (Int_t) (fNBin*xMin);
Int_t iUp = (Int_t) (fNBin*xMax);
if(gain>=maxGain) {
maxGain=gain;
kBest=kProj;
xBest=xMin;
yBest=xMax;
if(iLo == 0) xBest=yBest;
if(iUp == fNBin) yBest=xBest;
}
}
}
if( (kBest >= fDim) || (kBest<0) ) Log() << kFATAL << "Something wrong with kBest" << Endl;
}
void TMVA::PDEFoam::Carver(Int_t &kBest, Double_t &xBest, Double_t &yBest)
{
Int_t kProj,iBin;
Double_t carve,carvTot,carvMax,carvOne,binMax,binTot,primTot,primMax;
Int_t jLow,jUp,iLow,iUp;
Double_t theBin;
Int_t jDivi;
Int_t j;
Double_t *bins = new Double_t[fNBin];
if(bins==0) Log() << kFATAL << "Cannot initialize buffer Bins " << Endl;
kBest =-1;
xBest =0.5;
yBest =1.0;
carvMax = gVlow;
primMax = gVlow;
for(kProj=0; kProj<fDim; kProj++)
if( fMaskDiv[kProj] ){
binMax = gVlow;
for(iBin=0; iBin<fNBin;iBin++){
bins[iBin]= ((TH1D *)(*fHistEdg)[kProj])->GetBinContent(iBin+1);
binMax = TMath::Max( binMax, bins[iBin]);
}
if(binMax < 0 ) {
delete [] bins;
return;
}
carvTot = 0.0;
binTot = 0.0;
for(iBin=0;iBin<fNBin;iBin++){
carvTot = carvTot + (binMax-bins[iBin]);
binTot +=bins[iBin];
}
primTot = binMax*fNBin;
jLow =0;
jUp =fNBin-1;
carvOne = gVlow;
Double_t yLevel = gVlow;
for(iBin=0; iBin<fNBin;iBin++) {
theBin = bins[iBin];
iLow = iBin;
for(j=iBin; j>-1; j-- ) {
if(theBin< bins[j]) break;
iLow = j;
}
iUp = iBin;
for(j=iBin; j<fNBin; j++) {
if(theBin< bins[j]) break;
iUp = j;
}
carve = (iUp-iLow+1)*(binMax-theBin);
if( carve > carvOne) {
carvOne = carve;
jLow = iLow;
jUp = iUp;
yLevel = theBin;
}
}
if( carvTot > carvMax) {
carvMax = carvTot;
primMax = primTot;
kBest = kProj;
xBest = ((Double_t)(jLow))/fNBin;
yBest = ((Double_t)(jUp+1))/fNBin;
if(jLow == 0 ) xBest = yBest;
if(jUp == fNBin-1) yBest = xBest;
jDivi = jLow;
if(jLow == 0 ) jDivi=jUp+1;
}
for(iBin=0; iBin<fNBin; iBin++)
((TH1D *)(*fHistDbg)[kProj])->SetBinContent(iBin+1,binMax);
for(iBin=jLow; iBin<jUp+1; iBin++)
((TH1D *)(*fHistDbg)[kProj])->SetBinContent(iBin+1,yLevel);
}
if( (kBest >= fDim) || (kBest<0) ) Log() << kFATAL << "Something wrong with kBest" << Endl;
delete [] bins;
}
void TMVA::PDEFoam::MakeAlpha()
{
Int_t k;
if(fDim<1) return;
fPseRan->RndmArray(fDim,fRvec);
for(k=0; k<fDim; k++) fAlpha[k] = fRvec[k];
}
Long_t TMVA::PDEFoam::PeekMax()
{
Long_t iCell = -1;
Long_t i;
Double_t drivMax, driv;
Bool_t bCutNmin = kTRUE;
Bool_t bCutRMS = kTRUE;
drivMax = 0;
for(i=0; i<=fLastCe; i++) {
if( fCells[i]->GetStat() == 1 ) {
driv = TMath::Abs( fCells[i]->GetDriv());
if (CutRMSmin()){
bCutRMS = driv > GetRMSmin() ;
Log() << kINFO << "rms[cell "<<i<<"]=" << driv << Endl;
}
if (CutNmin())
bCutNmin = GetCellEvents(fCells[i]) > GetNmin();
if(driv > drivMax && bCutNmin && bCutRMS) {
drivMax = driv;
iCell = i;
}
}
}
if (iCell == -1){
if (!bCutNmin)
Log() << kVERBOSE << "Warning: No cell with more than " << GetNmin() << " events found!" << Endl;
else if (!bCutRMS)
Log() << kVERBOSE << "Warning: No cell with RMS/mean > " << GetRMSmin() << " found!" << Endl;
else
Log() << kWARNING << "Warning: PDEFoam::PeekMax: no more candidate cells (drivMax>0) found for further splitting." << Endl;
}
return(iCell);
}
Int_t TMVA::PDEFoam::Divide(PDEFoamCell *cell)
{
Double_t xdiv;
Int_t kBest;
if(fLastCe+1 >= fNCells) Log() << kFATAL << "Buffer limit is reached, fLastCe=fnBuf" << Endl;
cell->SetStat(0);
fNoAct--;
xdiv = cell->GetXdiv();
kBest = cell->GetBest();
if( kBest<0 || kBest>=fDim ) Log() << kFATAL << "Wrong kBest" << Endl;
Int_t d1 = CellFill(1, cell);
Int_t d2 = CellFill(1, cell);
cell->SetDau0((fCells[d1]));
cell->SetDau1((fCells[d2]));
Explore( (fCells[d1]) );
Explore( (fCells[d2]) );
return 1;
}
void TMVA::PDEFoam::MakeActiveList()
{
Long_t n, iCell;
Double_t sum;
if(fPrimAcu != 0) delete [] fPrimAcu;
if(fCellsAct != 0) delete fCellsAct;
fCellsAct = new TRefArray();
fPrime = 0.0; n = 0;
for(iCell=0; iCell<=fLastCe; iCell++) {
if (fCells[iCell]->GetStat()==1) {
fPrime += fCells[iCell]->GetPrim();
fCellsAct->Add(fCells[iCell]);
n++;
}
}
if(fNoAct != n) Log() << kFATAL << "Wrong fNoAct " << Endl;
if(fPrime == 0.) Log() << kFATAL << "Integrand function is zero" << Endl;
fPrimAcu = new Double_t[fNoAct];
if( fCellsAct==0 || fPrimAcu==0 ) Log() << kFATAL << "Cant allocate fCellsAct or fPrimAcu" << Endl;
sum =0.0;
for(iCell=0; iCell<fNoAct; iCell++) {
sum = sum + ( (PDEFoamCell *) (fCellsAct->At(iCell)) )->GetPrim()/fPrime;
fPrimAcu[iCell]=sum;
}
}
void TMVA::PDEFoam::GenerCel2(PDEFoamCell *&pCell)
{
Long_t lo, hi, hit;
Double_t fhit, flo, fhi;
Double_t random;
random=fPseRan->Rndm();
lo = 0; hi =fNoAct-1;
flo = fPrimAcu[lo]; fhi=fPrimAcu[hi];
while(lo+1<hi) {
hit = lo + (Int_t)( (hi-lo)*(random-flo)/(fhi-flo)+0.5);
if (hit<=lo)
hit = lo+1;
else if(hit>=hi)
hit = hi-1;
fhit=fPrimAcu[hit];
if (fhit>random) {
hi = hit;
fhi = fhit;
} else {
lo = hit;
flo = fhit;
}
}
if (fPrimAcu[lo]>random)
pCell = (PDEFoamCell *) fCellsAct->At(lo);
else
pCell = (PDEFoamCell *) fCellsAct->At(hi);
}
Double_t TMVA::PDEFoam::Eval(Double_t *xRand, Double_t &event_density)
{
Double_t result = 0;
if(!fRho) {
Log() << kFATAL << "No binary tree given!" << Endl;
} else {
result=fRho->Density(fDim,xRand,event_density);
}
return result;
}
void TMVA::PDEFoam::Grow()
{
fTimer->Init(fNCells);
Long_t iCell;
PDEFoamCell* newCell;
while ( (fLastCe+2) < fNCells ) {
iCell = PeekMax();
if ( (iCell<0) || (iCell>fLastCe) ) {
Log() << kVERBOSE << "Break: "<< fLastCe+1 << " cells created" << Endl;
for (Long_t jCell=fLastCe+1; jCell<fNCells; jCell++)
delete fCells[jCell];
fNCells = fLastCe+1;
break;
}
newCell = fCells[iCell];
OutputGrow();
if ( Divide( newCell )==0) break;
}
OutputGrow( kTRUE );
CheckAll(1);
Log() << kINFO << GetNActiveCells() << " active cells created" << Endl;
}
void TMVA::PDEFoam::MakeEvent(void)
{
Int_t j;
Double_t wt,dx,mcwt;
PDEFoamCell *rCell;
ee0:
GenerCel2(rCell);
MakeAlpha();
PDEFoamVect cellPosi(fDim); PDEFoamVect cellSize(fDim);
rCell->GetHcub(cellPosi,cellSize);
for(j=0; j<fDim; j++)
fMCvect[j]= cellPosi[j] +fAlpha[j]*cellSize[j];
dx = rCell->GetVolume();
wt=dx*Eval(fMCvect);
mcwt = wt / rCell->GetPrim();
fNCalls++;
fMCwt = mcwt;
fSumWt += mcwt;
fSumWt2 += mcwt*mcwt;
fNevGen++;
fWtMax = TMath::Max(fWtMax, mcwt);
fWtMin = TMath::Min(fWtMin, mcwt);
fMCMonit->Fill(mcwt);
fHistWt->Fill(mcwt,1.0);
if(fOptRej == 1) {
Double_t random;
random=fPseRan->Rndm();
if( fMaxWtRej*random > fMCwt) goto ee0;
if( fMCwt<fMaxWtRej ) {
fMCwt = 1.0;
} else {
fMCwt = fMCwt/fMaxWtRej;
fSumOve += fMCwt-fMaxWtRej;
}
}
}
void TMVA::PDEFoam::GetMCvect(Double_t *MCvect)
{
for ( Int_t k=0 ; k<fDim ; k++) *(MCvect +k) = fMCvect[k];
}
Double_t TMVA::PDEFoam::GetMCwt(void)
{
return(fMCwt);
}
void TMVA::PDEFoam::GetMCwt(Double_t &mcwt)
{
mcwt=fMCwt;
}
Double_t TMVA::PDEFoam::MCgenerate(Double_t *MCvect)
{
MakeEvent();
GetMCvect(MCvect);
return(fMCwt);
}
void TMVA::PDEFoam::GetIntegMC(Double_t &mcResult, Double_t &mcError)
{
Double_t mCerelat;
mcResult = 0.0;
mCerelat = 1.0;
if (fNevGen>0) {
mcResult = fPrime*fSumWt/fNevGen;
mCerelat = sqrt( fSumWt2/(fSumWt*fSumWt) - 1/fNevGen);
}
mcError = mcResult *mCerelat;
}
void TMVA::PDEFoam::GetIntNorm(Double_t& IntNorm, Double_t& Errel )
{
if(fOptRej == 1) {
Double_t intMC,errMC;
GetIntegMC(intMC,errMC);
IntNorm = intMC;
Errel = errMC;
} else {
IntNorm = fPrime;
Errel = 0;
}
}
void TMVA::PDEFoam::GetWtParams(Double_t eps, Double_t &aveWt, Double_t &wtMax, Double_t &sigma)
{
Double_t mCeff, wtLim;
fMCMonit->GetMCeff(eps, mCeff, wtLim);
wtMax = wtLim;
aveWt = fSumWt/fNevGen;
sigma = sqrt( fSumWt2/fNevGen -aveWt*aveWt );
}
void TMVA::PDEFoam::Finalize(Double_t& IntNorm, Double_t& Errel)
{
GetIntNorm(IntNorm,Errel);
Double_t mcResult,mcError;
GetIntegMC(mcResult,mcError);
}
void TMVA::PDEFoam::ResetPseRan(TRandom3*PseRan)
{
if(fPseRan) {
Info("ResetPseRan", "Resetting random number generator \n");
delete fPseRan;
}
SetPseRan(PseRan);
}
void TMVA::PDEFoam::SetRho(PDEFoamIntegrand *fun)
{
if (fun)
fRho=fun;
else
Log() << kFATAL << "Bad function" << Endl;
}
void TMVA::PDEFoam::ResetRho(PDEFoamIntegrand *fun)
{
if(fRho) {
Info("ResetRho", "!!! Resetting distribution function !!!\n");
delete fRho;
}
SetRho(fun);
}
void TMVA::PDEFoam::SetRhoInt(void *fun)
{
const Char_t *namefcn = G__p2f2funcname(fun);
if(namefcn) {
fMethodCall=new TMethodCall();
fMethodCall->InitWithPrototype(namefcn, "Int_t, Double_t *");
}
fRho=0;
}
void TMVA::PDEFoam::SetInhiDiv(Int_t iDim, Int_t InhiDiv)
{
if(fDim==0) Log() << kFATAL << "SetInhiDiv: fDim=0" << Endl;
if(fInhiDiv == 0) {
fInhiDiv = new Int_t[ fDim ];
for(Int_t i=0; i<fDim; i++) fInhiDiv[i]=0;
}
if( ( 0<=iDim) && (iDim<fDim)) {
fInhiDiv[iDim] = InhiDiv;
} else
Log() << kFATAL << "Wrong iDim" << Endl;
}
void TMVA::PDEFoam::SetXdivPRD(Int_t iDim, Int_t len, Double_t xDiv[])
{
Int_t i;
if(fDim<=0) Log() << kFATAL << "fDim=0" << Endl;
if( len<1 ) Log() << kFATAL << "len<1" << Endl;
if(fXdivPRD == 0) {
fXdivPRD = new PDEFoamVect*[fDim];
for(i=0; i<fDim; i++) fXdivPRD[i]=0;
}
if( ( 0<=iDim) && (iDim<fDim)) {
fOptPRD =1;
if( fXdivPRD[iDim] != 0)
Log() << kFATAL << "Second allocation of XdivPRD not allowed" << Endl;
fXdivPRD[iDim] = new PDEFoamVect(len);
for(i=0; i<len; i++) {
(*fXdivPRD[iDim])[i]=xDiv[i];
}
} else {
Log() << kFATAL << "Wrong iDim" << Endl;
}
Log()<<" SetXdivPRD, idim= "<<iDim<<" len= "<<len<<" "<<Endl;
for(i=0; i<len; i++) {
Log()<< (*fXdivPRD[iDim])[i] <<" ";
}
Log()<<Endl;
for(i=0; i<len; i++) Log()<< xDiv[i] <<" ";
Log()<<Endl;
}
void TMVA::PDEFoam::CheckAll(Int_t level)
{
Int_t errors, warnings;
PDEFoamCell *cell;
Long_t iCell;
errors = 0; warnings = 0;
if (level==1) Log() << "Performing consistency checks for created foam" << Endl;
for(iCell=1; iCell<=fLastCe; iCell++) {
cell = fCells[iCell];
if( ((cell->GetDau0()==0) && (cell->GetDau1()!=0) ) ||
((cell->GetDau1()==0) && (cell->GetDau0()!=0) ) ) {
errors++;
if (level==1) Log() << kFATAL << "ERROR: Cell's no %d has only one daughter " << iCell << Endl;
}
if( (cell->GetDau0()==0) && (cell->GetDau1()==0) && (cell->GetStat()==0) ) {
errors++;
if (level==1) Log() << kFATAL << "ERROR: Cell's no %d has no daughter and is inactive " << iCell << Endl;
}
if( (cell->GetDau0()!=0) && (cell->GetDau1()!=0) && (cell->GetStat()==1) ) {
errors++;
if (level==1) Log() << kFATAL << "ERROR: Cell's no %d has two daughters and is active " << iCell << Endl;
}
if( (cell->GetPare())!=fCells[0] ) {
if ( (cell != cell->GetPare()->GetDau0()) && (cell != cell->GetPare()->GetDau1()) ) {
errors++;
if (level==1) Log() << kFATAL << "ERROR: Cell's no %d parent not pointing to this cell " << iCell << Endl;
}
}
if(cell->GetDau0()!=0) {
if(cell != (cell->GetDau0())->GetPare()) {
errors++;
if (level==1) Log() << kFATAL << "ERROR: Cell's no %d daughter 0 not pointing to this cell " << iCell << Endl;
}
}
if(cell->GetDau1()!=0) {
if(cell != (cell->GetDau1())->GetPare()) {
errors++;
if (level==1) Log() << kFATAL << "ERROR: Cell's no %d daughter 1 not pointing to this cell " << iCell << Endl;
}
}
if(cell->GetVolume()<1E-50) {
errors++;
if(level==1) Log() << kFATAL << "ERROR: Cell no. %d has Volume of <1E-50" << iCell << Endl;
}
}
for(iCell=0; iCell<=fLastCe; iCell++) {
cell = fCells[iCell];
if( (cell->GetStat()==1) && (cell->GetVolume()<1E-11) ) {
errors++;
if(level==1) Log() << kFATAL << "ERROR: Cell no. " << iCell << " is active but Volume is 0 " << Endl;
}
}
if(level==1){
Log() << kINFO << "Check has found " << errors << " errors and " << warnings << " warnings." << Endl;
}
if(errors>0){
Info("CheckAll","Check - found total %d errors \n",errors);
}
}
void TMVA::PDEFoam::PrintCells(void)
{
Long_t iCell;
for(iCell=0; iCell<=fLastCe; iCell++) {
Log()<<"Cell["<<iCell<<"]={ ";
Log()<<" "<< fCells[iCell]<<" ";
Log()<<Endl;
fCells[iCell]->Print("1");
Log()<<"}"<<Endl;
}
}
void TMVA::PDEFoam::LinkCells()
{
Info("LinkCells", "VOID function for backward compatibility \n");
return;
}
Double_t TMVA::PDEFoam::GetSumCellElements(UInt_t i)
{
Double_t intg = 0;
for (Long_t iCell=0; iCell<=fLastCe; iCell++) {
if (fCells[iCell]->GetStat())
intg += GetCellElement(fCells[iCell], i);
}
return intg;
}
Double_t TMVA::PDEFoam::GetSumCellIntg()
{
Double_t intg = 0;
for (Long_t iCell=0; iCell<=fLastCe; iCell++) {
if (fCells[iCell]->GetStat())
intg += fCells[iCell]->GetIntg();
}
return intg;
}
UInt_t TMVA::PDEFoam::GetNActiveCells()
{
UInt_t count = 0;
for (Long_t iCell=0; iCell<=fLastCe; iCell++) {
if (fCells[iCell]->GetStat())
count++;
}
return count;
}
void TMVA::PDEFoam::RemoveEmptyCell( Int_t iCell )
{
Double_t volume = fCells[iCell]->GetVolume();
if (!fCells[iCell]->GetStat() || volume>0){
Log() << "<RemoveEmptyCell>: cell " << iCell
<< "is not active or has volume>0 ==> doesn't need to be removed" << Endl;
return;
}
PDEFoamCell *pCell = fCells[iCell]->GetPare();
PDEFoamCell *ppCell = fCells[iCell]->GetPare()->GetPare();
PDEFoamCell *sCell;
if (pCell->GetDau0() == fCells[iCell])
sCell = pCell->GetDau1();
else
sCell = pCell->GetDau0();
if (pCell->GetIntg() != sCell->GetIntg())
Log() << kWARNING << "<RemoveEmptyCell> Error: cell integrals are not equal!"
<< " Intg(parent cell)=" << pCell->GetIntg()
<< " Intg(sister cell)=" << sCell->GetIntg()
<< Endl;
if (ppCell->GetDau0() == pCell)
ppCell->SetDau0(sCell);
else
ppCell->SetDau1(sCell);
sCell->SetPare(ppCell);
fCells[iCell]->SetStat(0);
pCell->SetStat(0);
}
void TMVA::PDEFoam::CheckCells( Bool_t remove_empty_cells )
{
for (Long_t iCell=0; iCell<=fLastCe; iCell++) {
if (!fCells[iCell]->GetStat())
continue;
Double_t volume = fCells[iCell]->GetVolume();
if (volume<1e-10){
if (volume<=0){
Log() << kWARNING << "Critical: cell volume negative or zero! volume="
<< volume << " cell number: " << iCell << Endl;
if (remove_empty_cells){
Log() << kWARNING << "Remove cell " << iCell << Endl;
RemoveEmptyCell(iCell);
}
}
else {
Log() << kWARNING << "Cell volume close to zero! volume="
<< volume << " cell number: " << iCell << Endl;
}
}
}
}
void TMVA::PDEFoam::PrintCellElements()
{
for (Long_t iCell=0; iCell<=fLastCe; iCell++) {
if (!fCells[iCell]->GetStat()) continue;
std::cout << ">>> cell[" << iCell << "] elements: ";
for (UInt_t i=0; i<GetNElements(); i++)
std::cout << "[" << i << "; " << GetCellElement(fCells[iCell], i) << "] ";
std::cout << std::endl;
}
}
UInt_t TMVA::PDEFoam::GetNInActiveCells()
{
return GetNCells()-GetNActiveCells();
}
UInt_t TMVA::PDEFoam::GetNCells()
{
return fNCells;
}
Int_t TMVA::PDEFoam::GetSumCellMemory( ECellType ct )
{
UInt_t count = 0;
for (Long_t iCell=0; iCell<fNCells; iCell++) {
Int_t cellsize = sizeof(*(fCells[iCell]));
if (ct==kAll)
count += cellsize;
else if (ct==kActive && fCells[iCell]->GetStat() && iCell<fLastCe)
count += cellsize;
else if (ct==kInActive &&
((!(fCells[iCell]->GetStat()) && iCell<fLastCe) || iCell>=fLastCe) )
count += cellsize;
}
return count;
}
TMVA::PDEFoamCell* TMVA::PDEFoam::GetRootCell()
{
return fCells[0];
}
void TMVA::PDEFoam::ResetCellElements(Bool_t allcells)
{
if (!fCells || GetNElements()==0) return;
Log() << kVERBOSE << "Delete old cell elements" << Endl;
for(Long_t iCell=0; iCell<fNCells; iCell++) {
if (fCells[iCell]->GetElement() != 0){
delete dynamic_cast<TVectorD*>(fCells[iCell]->GetElement());
fCells[iCell]->SetElement(0);
}
}
if (allcells){
Log() << kVERBOSE << "Reset new cell elements to "
<< GetNElements() << " value(s) per cell" << Endl;
for(Long_t iCell=0; iCell<fNCells; iCell++) {
TVectorD *elem = new TVectorD(GetNElements());
for (UInt_t i=0; i<GetNElements(); i++)
(*elem)(i) = 0.;
fCells[iCell]->SetElement(elem);
}
} else {
Log() << kVERBOSE << "Reset active cell elements to "
<< GetNElements() << " value(s) per cell" << Endl;
for(Long_t iCell=0; iCell<=fLastCe; iCell++) {
if (!(fCells[iCell]->GetStat()))
continue;
TVectorD *elem = new TVectorD(GetNElements());
for (UInt_t i=0; i<GetNElements(); i++)
(*elem)(i) = 0.;
fCells[iCell]->SetElement(elem);
}
}
}
void TMVA::PDEFoam::DisplayCellContent()
{
Double_t total_counts = 0;
Double_t max = GetCellEntries(fCells[0]);
Double_t min = GetCellEntries(fCells[0]);
for (Long_t iCell=0; iCell<=fLastCe; iCell++) {
total_counts += GetCellEntries(fCells[iCell]);
if (GetCellEntries(fCells[iCell]) < min)
min = GetCellEntries(fCells[iCell]);
if (GetCellEntries(fCells[iCell]) > max)
max = GetCellEntries(fCells[iCell]);
}
Log() << kVERBOSE << "DEBUG: Total Events in Foam: " << total_counts << Endl;
Log() << kVERBOSE << "DEBUG: min cell entry: " << min << Endl;
Log() << kVERBOSE << "DEBUG: max cell entry: " << max << Endl;
}
void TMVA::PDEFoam::CalcCellTarget()
{
for (Long_t iCell=0; iCell<=fLastCe; iCell++) {
if (!(fCells[iCell]->GetStat()))
continue;
Double_t N_ev = GetCellElement(fCells[iCell], 0);
Double_t tar = GetCellElement(fCells[iCell], 1);
if (N_ev > 1e-10){
SetCellElement(fCells[iCell], 0, tar/N_ev);
SetCellElement(fCells[iCell], 1, 0.0 );
}
else {
SetCellElement(fCells[iCell], 0, 0.0 );
SetCellElement(fCells[iCell], 1, 1. );
}
}
}
void TMVA::PDEFoam::CalcCellDiscr()
{
for (Long_t iCell=0; iCell<=fLastCe; iCell++) {
if (!(fCells[iCell]->GetStat()))
continue;
Double_t N_sig = GetCellElement(fCells[iCell], 0);
Double_t N_bg = GetCellElement(fCells[iCell], 1);
if (N_sig<0.) {
Log() << kWARNING << "Negative number of signal events in cell " << iCell
<< ": " << N_sig << ". Set to 0." << Endl;
N_sig=0.;
}
if (N_bg<0.) {
Log() << kWARNING << "Negative number of background events in cell " << iCell
<< ": " << N_bg << ". Set to 0." << Endl;
N_bg=0.;
}
if (N_sig+N_bg > 1e-10){
SetCellElement(fCells[iCell], 0, N_sig/(N_sig+N_bg));
SetCellElement(fCells[iCell], 1, TMath::Sqrt( TMath::Power ( N_sig/TMath::Power(N_sig+N_bg,2),2)*N_sig +
TMath::Power ( N_bg /TMath::Power(N_sig+N_bg,2),2)*N_bg ) );
}
else {
SetCellElement(fCells[iCell], 0, 0.5);
SetCellElement(fCells[iCell], 1, 1. );
}
}
}
Double_t TMVA::PDEFoam::GetCellDiscr( std::vector<Float_t> xvec, EKernel kernel )
{
Double_t result = 0.;
std::vector<Float_t> txvec = VarTransform(xvec);
PDEFoamCell *cell= FindCell(txvec);
if (!cell) return -999.;
if (kernel == kNone) result = GetCellDiscr(cell);
else if (kernel == kGaus) {
Double_t norm = 0.;
for (Long_t iCell=0; iCell<=fLastCe; iCell++) {
if (!(fCells[iCell]->GetStat())) continue;
Double_t cell_discr = GetCellDiscr(fCells[iCell]);
Double_t gau = WeightGaus(fCells[iCell], txvec);
result += gau * cell_discr;
norm += gau;
}
result /= norm;
}
else if (kernel == kLinN) {
result = WeightLinNeighbors(txvec, kDiscriminator);
}
else {
Log() << kFATAL << "GetCellDiscr: ERROR: wrong kernel!" << Endl;
result = -999.0;
}
return result;
}
Double_t TMVA::PDEFoam::GetCellDiscrError( std::vector<Float_t> xvec )
{
PDEFoamCell *cell= FindCell(VarTransform(xvec));
if (!cell) return -999.;
return GetCellDiscrError(cell);
}
void TMVA::PDEFoam::FillFoamCells(const Event* ev, EFoamType ft, Bool_t NoNegWeights)
{
std::vector<Float_t> values = ev->GetValues();
std::vector<Float_t> targets = ev->GetTargets();
Float_t weight = ev->GetOriginalWeight();
if(NoNegWeights && weight<=0)
return;
if (ft == kMultiTarget)
values.insert(values.end(), targets.begin(), targets.end());
PDEFoamCell *cell = FindCell(VarTransform(values));
if (!cell) {
Log() << kFATAL << "<PDEFoam::FillFoamCells>: No cell found!" << Endl;
return;
}
if (ft == kSeparate || ft == kMultiTarget){
SetCellElement(cell, 0, GetCellElement(cell, 0) + weight);
SetCellElement(cell, 1, GetCellElement(cell, 1) + weight*weight);
} else if (ft == kDiscr){
if (ev->IsSignal())
SetCellElement(cell, 0, GetCellElement(cell, 0) + weight);
else
SetCellElement(cell, 1, GetCellElement(cell, 1) + weight);
} else if (ft == kMonoTarget){
SetCellElement(cell, 0, GetCellElement(cell, 0) + weight);
SetCellElement(cell, 1, GetCellElement(cell, 1) + weight*targets.at(0));
}
}
Double_t TMVA::PDEFoam::GetCellMean( std::vector<Float_t> xvec )
{
PDEFoamCell *cell = FindCell(VarTransform(xvec));
if (!cell) {
Log() << kFATAL << "<GetCellMean> ERROR: No cell found! " << Endl;
return -999.;
}
return GetCellMean(cell);
}
Double_t TMVA::PDEFoam::GetCellRMS( std::vector<Float_t> xvec )
{
PDEFoamCell *cell = FindCell(VarTransform(xvec));
if (!cell) {
Log() << kFATAL << "<GetCellRMS> ERROR: No cell found! " << Endl;
return -999.;
}
return GetCellRMS(cell);
}
Double_t TMVA::PDEFoam::GetCellRegValue0( std::vector<Float_t> xvec, EKernel kernel )
{
Double_t result = 0.;
std::vector<Float_t> txvec = VarTransform(xvec);
PDEFoamCell *cell = FindCell(txvec);
if (!cell) {
Log() << kFATAL << "<GetCellRegValue0> ERROR: No cell found!" << Endl;
return -999.;
}
if (kernel == kNone){
result = GetCellRegValue0(cell);
}
else if (kernel == kGaus){
Double_t norm = 0.;
for (Long_t iCell=0; iCell<=fLastCe; iCell++) {
if (!(fCells[iCell]->GetStat())) continue;
Double_t cell_val = GetCellRegValue0(fCells[iCell]);
Double_t gau = WeightGaus(fCells[iCell], txvec);
result += gau * cell_val;
norm += gau;
}
result /= norm;
}
else if (kernel == kLinN) {
result = WeightLinNeighbors(txvec, kTarget0);
}
else {
Log() << kFATAL << "ERROR: unknown kernel!" << Endl;
return -999.;
}
return result;
}
Double_t TMVA::PDEFoam::GetCellTarget( UInt_t target_number, std::vector<Float_t> tvals, ETargetSelection ts )
{
Double_t result = 0.;
Double_t norm = 0.;
Double_t max_dens = 0.;
UInt_t cell_count = 0;
const Double_t xsmall = 1.e-7;
if (ts==kMpv)
result = fXmin[target_number+tvals.size()];
for (Long_t iCell=0; iCell<=fLastCe; iCell++) {
if (!(fCells[iCell]->GetStat())) continue;
PDEFoamVect cellPosi(GetTotDim()), cellSize(GetTotDim());
fCells[iCell]->GetHcub(cellPosi, cellSize);
Bool_t CellOK = kTRUE;
for (UInt_t k=0; k<tvals.size(); k++){
if (!( tvals.at(k) > cellPosi[k]-xsmall &&
tvals.at(k) <= cellPosi[k]+cellSize[k]+xsmall )){
CellOK = kFALSE;
break;
}
}
if (!CellOK)
continue;
cell_count++;
Double_t cell_density = GetCellDensity(fCells[iCell]);
if (ts==kMean){
result += cell_density * VarTransformInvers(target_number+tvals.size(),
cellPosi[target_number+tvals.size()]
+0.5*cellSize[target_number+tvals.size()]);
norm += cell_density;
} else {
if (cell_density > max_dens){
max_dens = cell_density;
result = VarTransformInvers(target_number+tvals.size(),
cellPosi[target_number+tvals.size()]
+0.5*cellSize[target_number+tvals.size()]);
}
}
}
if (cell_count<1 || (ts==kMean && norm<1.0e-15))
return (fXmax[target_number+tvals.size()]-fXmin[target_number+tvals.size()])/2.;
if (ts==kMean)
result /= norm;
return result;
}
Double_t TMVA::PDEFoam::GetProjectedRegValue( UInt_t target_number, std::vector<Float_t> vals,
EKernel kernel, ETargetSelection ts )
{
if (target_number+vals.size() > UInt_t(GetTotDim())){
Log() << kFATAL << "ERROR: wrong dimension given!" << Endl;
return 0;
}
const Double_t xsmall = 1.e-7;
for (UInt_t l=0; l<vals.size(); l++) {
if (vals.at(l) <= fXmin[l]){
vals.at(l) = fXmin[l] + xsmall;
}
else if (vals.at(l) >= fXmax[l]){
vals.at(l) = fXmax[l] - xsmall;
}
}
std::vector<Float_t> txvec = VarTransform(vals);
Double_t result = 0.;
if (kernel == kNone)
result = GetCellTarget(target_number, txvec, ts);
else if (kernel == kGaus){
Double_t norm = 0.;
for (Long_t ice=0; ice<=fLastCe; ice++) {
if (!(fCells[ice]->GetStat())) continue;
Double_t gau = WeightGaus(fCells[ice], txvec, vals.size());
PDEFoamVect cellPosi(GetTotDim()), cellSize(GetTotDim());
fCells[ice]->GetHcub(cellPosi, cellSize);
std::vector<Float_t> new_vec;
for (UInt_t k=0; k<txvec.size(); k++)
new_vec.push_back(cellPosi[k] + 0.5*cellSize[k]);
Double_t val = GetCellTarget(target_number, new_vec, ts);
result += gau * val;
norm += gau;
}
if (norm<1.0e-20){
Log() << kWARNING << "Warning: norm too small!" << Endl;
return 0.;
}
result /= norm;
}
else {
Log() << kFATAL << "<GetProjectedRegValue> ERROR: unsupported kernel!" << Endl;
result = 0.;
}
return result;
}
Double_t TMVA::PDEFoam::GetCellEntries( std::vector<Float_t> xvec )
{
PDEFoamCell *cell= FindCell(VarTransform(xvec));
if (!cell) {
Log() << kFATAL << "<GetCellEntries> No cell found! " << Endl;
return -999.;
}
return GetCellEntries(cell);
}
Double_t TMVA::PDEFoam::GetCellDensity( PDEFoamCell *cell )
{
Double_t volume = cell->GetVolume();
Double_t entries = GetCellEntries(cell);
if (entries <= 0)
return 0.;
if (volume > 1.0e-10 ){
return GetCellEntries(cell)/volume;
}
else {
if (volume<=0){
cell->Print("1");
Log() << kWARNING << "<GetCellDensity(cell)>: ERROR: cell volume negative or zero!"
<< " ==> return cell density 0!"
<< " cell volume=" << volume
<< " cell entries=" << GetCellEntries(cell) << Endl;
return 0;
}
else {
Log() << kWARNING << "<GetCellDensity(cell)>: WARNING: cell volume close to zero!"
<< " cell volume: " << volume << Endl;
}
}
return GetCellEntries(cell)/volume;
}
Double_t TMVA::PDEFoam::GetCellDensity( std::vector<Float_t> xvec, EKernel kernel )
{
Double_t result = 0;
std::vector<Float_t> txvec = VarTransform(xvec);
PDEFoamCell *cell = FindCell(txvec);
if (!cell) {
Log() << kFATAL << "<GetCellDensity(event)> ERROR: No cell found!" << Endl;
return -999.;
}
if (kernel == kNone){
return GetCellDensity(cell);
}
else if (kernel == kGaus){
Double_t norm = 0.;
for (Long_t iCell=0; iCell<=fLastCe; iCell++) {
if (!(fCells[iCell]->GetStat())) continue;
Double_t cell_dens = GetCellDensity(fCells[iCell]);
Double_t gau = WeightGaus(fCells[iCell], txvec);
result += gau * cell_dens;
norm += gau;
}
result /= norm;
}
else if (kernel == kLinN){
result = WeightLinNeighbors(txvec, kDensity);
}
else {
Log() << kFATAL << "<GetCellDensity(event)> ERROR: unknown kernel!" << Endl;
return -999.;
}
return result;
}
Double_t TMVA::PDEFoam::GetCellRegValue0( PDEFoamCell* cell )
{
return GetCellElement(cell, 0);
}
Double_t TMVA::PDEFoam::GetCellDiscr( PDEFoamCell* cell )
{
return GetCellElement(cell, 0);
}
Double_t TMVA::PDEFoam::GetCellDiscrError( PDEFoamCell* cell )
{
return GetCellElement(cell, 1);
}
Double_t TMVA::PDEFoam::GetCellMean( PDEFoamCell* cell )
{
return cell->GetIntg();
}
Double_t TMVA::PDEFoam::GetCellRMS( PDEFoamCell* cell )
{
return cell->GetDriv();
}
Double_t TMVA::PDEFoam::GetCellRMSovMean( PDEFoamCell* cell )
{
if (GetCellMean(cell)!=0)
return GetCellRMS(cell)/GetCellMean(cell);
else
return 0;
}
Double_t TMVA::PDEFoam::GetCellEntries( PDEFoamCell* cell )
{
return GetCellElement(cell, 0);
}
Double_t TMVA::PDEFoam::GetCellEvents( PDEFoamCell* cell )
{
return GetCellElement(cell, 0);
}
Double_t TMVA::PDEFoam::WeightLinNeighbors( std::vector<Float_t> txvec, ECellValue cv )
{
Double_t result = 0.;
const Double_t xoffset = 1.e-6;
if (txvec.size() != UInt_t(GetTotDim()))
Log() << kFATAL << "Wrong dimension of event variable!" << Endl;
PDEFoamCell *cell= FindCell(txvec);
PDEFoamVect cellSize(GetTotDim());
PDEFoamVect cellPosi(GetTotDim());
cell->GetHcub(cellPosi, cellSize);
for (Int_t dim=0; dim<GetTotDim(); dim++) {
std::vector<Float_t> ntxvec = txvec;
Double_t mindist;
PDEFoamCell *mindistcell = 0;
mindist = (txvec[dim]-cellPosi[dim])/cellSize[dim];
if (mindist<0.5) {
ntxvec[dim] = cellPosi[dim]-xoffset;
mindistcell = FindCell(ntxvec);
} else {
mindist=1-mindist;
ntxvec[dim] = cellPosi[dim]+cellSize[dim]+xoffset;
mindistcell = FindCell(ntxvec);
}
Double_t cellval = 0;
Double_t mindistcellval = 0;
if (cv==kDiscriminator){
cellval = GetCellDiscr(cell);
mindistcellval = GetCellDiscr(mindistcell);
} else if (cv==kTarget0){
cellval = GetCellRegValue0(cell);
mindistcellval = GetCellRegValue0(mindistcell);
} else if (cv==kDensity){
cellval = GetCellDensity(cell);
mindistcellval = GetCellDensity(mindistcell);
} else if (cv==kNev){
cellval = GetCellEntries(cell);
mindistcellval = GetCellEntries(mindistcell);
} else if (cv==kMeanValue){
cellval = GetCellMean(cell);
mindistcellval = GetCellMean(mindistcell);
} else if (cv==kRms){
cellval = GetCellRMS(cell);
mindistcellval = GetCellRMS(mindistcell);
} else if (cv==kRmsOvMean){
cellval = GetCellRMSovMean(cell);
mindistcellval = GetCellRMSovMean(mindistcell);
} else {
Log() << kFATAL << "<WeightLinNeighbors>: unsupported option" << Endl;
}
result += cellval * (0.5 + mindist);
result += mindistcellval * (0.5 - mindist);
}
return result/GetTotDim();
}
Double_t TMVA::PDEFoam::WeightGaus( PDEFoamCell* cell, std::vector<Float_t> txvec, UInt_t dim )
{
PDEFoamVect cellSize(GetTotDim());
PDEFoamVect cellPosi(GetTotDim());
cell->GetHcub(cellPosi, cellSize);
UInt_t dims;
if (dim == 0)
dims = GetTotDim();
else if (dim <= UInt_t(GetTotDim()))
dims = dim;
else {
Log() << kFATAL << "ERROR: too many given dimensions for Gaus weight!" << Endl;
return 0.;
}
std::vector<Float_t> cell_center;
for (UInt_t i=0; i<dims; i++){
if (txvec[i]<0.) txvec[i]=0.;
if (txvec[i]>1.) txvec[i]=1.;
if (cellPosi[i] > txvec.at(i))
cell_center.push_back(cellPosi[i]);
else if (cellPosi[i]+cellSize[i] < txvec.at(i))
cell_center.push_back(cellPosi[i]+cellSize[i]);
else
cell_center.push_back(txvec.at(i));
}
Double_t distance = 0.;
for (UInt_t i=0; i<dims; i++)
distance += TMath::Power(txvec.at(i)-cell_center.at(i), 2);
distance = TMath::Sqrt(distance);
Double_t width = 1./GetPDEFoamVolumeFraction();
if (width < 1.0e-10)
Log() << kWARNING << "Warning: wrong volume fraction: " << GetPDEFoamVolumeFraction() << Endl;
return TMath::Gaus(distance, 0, width, kFALSE);
}
TMVA::PDEFoamCell* TMVA::PDEFoam::FindCell( std::vector<Float_t> xvec )
{
PDEFoamVect cellPosi0(GetTotDim()), cellSize0(GetTotDim());
PDEFoamCell *cell, *cell0;
cell=fCells[0];
Int_t idim=0;
while (cell->GetStat()!=1) {
idim=cell->GetBest();
cell0=cell->GetDau0();
cell0->GetHcub(cellPosi0,cellSize0);
if (xvec.at(idim)<=cellPosi0[idim]+cellSize0[idim])
cell=cell0;
else
cell=(cell->GetDau1());
}
return cell;
}
TH1D* TMVA::PDEFoam::Draw1Dim( const char *opt, Int_t nbin )
{
if (!(strcmp(opt,"mean")==0 ||
strcmp(opt,"nev")==0 ||
strcmp(opt,"rms")==0 ||
strcmp(opt,"rms_ov_mean")==0 ||
strcmp(opt,"discr")==0 ||
strcmp(opt,"discr_error")==0 ||
strcmp(opt,"MonoTargetRegression")==0 ||
strcmp(opt,"MultiTargetRegression")==0) ||
GetTotDim()!=1 )
return 0;
char hname[100]; char htit[100];
sprintf(htit,"1-dimensional Foam: %s", opt);
sprintf(hname,"h%s",opt);
TH1D* h1=(TH1D*)gDirectory->Get(hname);
if (h1) delete h1;
h1= new TH1D(hname, htit, nbin, fXmin[0], fXmax[0]);
if (!h1) Log() << kFATAL << "ERROR: Can not create histo" << hname << Endl;
std::vector<Float_t> xvec(GetTotDim(), 0.);
for (Int_t ibinx=1; ibinx<=nbin; ibinx++) {
xvec.at(0) = h1->GetBinCenter(ibinx);
std::vector<Float_t> txvec = VarTransform(xvec);
for (Long_t iCell=0; iCell<=fLastCe; iCell++) {
if (!(fCells[iCell]->GetStat())) continue;
PDEFoamVect cellPosi(GetTotDim()), cellSize(GetTotDim());
fCells[iCell]->GetHcub(cellPosi,cellSize);
const Double_t xsmall = 1.e-10;
if (!( (txvec.at(0)>cellPosi[0]-xsmall) &&
(txvec.at(0)<=cellPosi[0]+cellSize[0]+xsmall) ) )
continue;
Double_t vol = fCells[iCell]->GetVolume();
if (vol<1e-10) {
Log() << kWARNING << "Project: ERROR: Volume too small!" << Endl;
continue;
}
Double_t var = 0.;
if (strcmp(opt,"nev")==0 || strcmp(opt,"MultiTargetRegression")==0)
var = GetCellEntries(fCells[iCell]);
else if (strcmp(opt,"mean")==0)
var = GetCellMean(fCells[iCell]);
else if (strcmp(opt,"rms")==0)
var = GetCellRMS(fCells[iCell]);
else if (strcmp(opt,"rms_ov_mean")==0)
var = GetCellRMSovMean(fCells[iCell]);
else if (strcmp(opt,"discr")==0)
var = GetCellDiscr(fCells[iCell]);
else if (strcmp(opt,"discr_error")==0)
var = GetCellDiscrError(fCells[iCell]);
else if (strcmp(opt,"MonoTargetRegression")==0)
var = GetCellRegValue0(fCells[iCell]);
else
Log() << kFATAL << "Project: ERROR: unknown option: " << opt << Endl;
h1->SetBinContent(ibinx, var + h1->GetBinContent(ibinx));
}
}
return h1;
}
TH2D* TMVA::PDEFoam::Project2( Int_t idim1, Int_t idim2, const char *opt, const char *ker, UInt_t maxbins )
{
if (!(
strcmp(opt,"nev")==0 ||
strcmp(opt,"rms")==0 ||
strcmp(opt,"rms_ov_mean")==0 ||
strcmp(opt,"discr")==0 ||
strcmp(opt,"MonoTargetRegression")==0 ||
strcmp(opt,"MultiTargetRegression")==0) ||
(idim1>=GetTotDim()) || (idim1<0) ||
(idim2>=GetTotDim()) || (idim2<0) ||
(idim1==idim2) )
return 0;
EKernel kernel = kNone;
if (!strcmp(ker, "kNone"))
kernel = kNone;
else if (!strcmp(ker, "kGaus"))
kernel = kGaus;
else
Log() << kWARNING << "Warning: wrong kernel! using kNone instead" << Endl;
char name[]="PDEFoam::Project: ";
const Bool_t debug = kFALSE;
char hname[100]; char htit[100];
const Double_t foam_area = (fXmax[idim1]-fXmin[idim1])*(fXmax[idim2]-fXmin[idim2]);
Double_t bin_width = 1.;
for (Long_t iCell=0; iCell<=fLastCe; iCell++) {
if (!(fCells[iCell]->GetStat())) continue;
PDEFoamVect cellPosi(GetTotDim()), cellSize(GetTotDim());
fCells[iCell]->GetHcub(cellPosi,cellSize);
for (Int_t d1=0; d1<GetTotDim(); d1++)
if (cellSize[d1]<bin_width && cellSize[d1]>0)
bin_width = cellSize[d1];
}
UInt_t nbin = UInt_t(1./bin_width);
if (maxbins>0 && nbin>maxbins)
nbin = maxbins;
if (nbin>1000){
Log() << kWARNING << "Warning: number of bins too big: " << nbin
<< "! Using 1000 bins for each dimension instead." << Endl;
nbin = 1000;
}
sprintf(htit,"%s var%d vs var%d",opt,idim1,idim2);
sprintf(hname,"h%s_%d_vs_%d",opt,idim1,idim2);
if (debug) {
Log() << kInfo <<name<<" Project var"<<idim1<<" and var"<<idim2<<" to histo: "<< Endl;
Log() << kVERBOSE <<name<<hname<<" nbin="<<nbin
<<" fXmin["<<idim1<<"]="<<fXmin[idim1]<<" fXmax["<<idim1<<"]="<<fXmax[idim1]
<<" fXmin["<<idim2<<"]="<<fXmin[idim2]<<" fXmax["<<idim2<<"]="<<fXmax[idim2]
<< Endl;
}
TH2D* h1=(TH2D*)gDirectory->Get(hname);
if (h1) delete h1;
h1= new TH2D(hname, htit, nbin, fXmin[idim1], fXmax[idim1], nbin, fXmin[idim2], fXmax[idim2]);
if (!h1) Log() << kFATAL <<name<<"ERROR: Can not create histo"<<hname<< Endl;
else if (debug) h1->Print();
if (debug) Log() << kVERBOSE << "histogram created/ loaded" << Endl;
UInt_t cell_count = 0;
for (Long_t iCell=0; iCell<=fLastCe; iCell++) {
if (!(fCells[iCell]->GetStat())) continue;
cell_count++;
PDEFoamVect cellPosi(GetTotDim()), cellSize(GetTotDim());
fCells[iCell]->GetHcub(cellPosi,cellSize);
Double_t var = 0.;
if (strcmp(opt,"mean")==0 || strcmp(opt,"nev")==0 || strcmp(opt,"MultiTargetRegression")==0){
Double_t area = cellSize[idim1] * cellSize[idim2];
if (area<1e-20){
Log() << kWARNING << "PDEFoam::ProjectEvents: Warning, cell volume too small --> skiping cell!"
<< Endl;
continue;
}
if (strcmp(opt,"mean")==0)
var = GetCellMean(fCells[iCell])/(area*foam_area);
else
var = GetCellEntries(fCells[iCell])/(area*foam_area);
if (debug)
Log() << kVERBOSE << "Project2: Cell=" << iCell << " projected density=" << var
<< " area=" << area*foam_area
<< " entries="<< GetCellEntries(fCells[iCell]) << Endl;
}
else if (strcmp(opt,"rms")==0){
var = GetCellRMS(fCells[iCell]);
}
else if (strcmp(opt,"rms_ov_mean")==0){
var = GetCellRMSovMean(fCells[iCell]);
}
else if (strcmp(opt,"discr")==0){
Double_t area_cell = 1.;
for (Int_t d1=0; d1<GetTotDim(); d1++){
if ((d1!=idim1) && (d1!=idim2))
area_cell *= cellSize[d1];
}
if (area_cell<1e-20){
Log() << kWARNING << "ProjectEvents: Warning, cell volume too small --> skiping cell!" << Endl;
continue;
}
var = GetCellDiscr(fCells[iCell])*area_cell;
}
else if (strcmp(opt,"discr_error")==0){
var = GetCellDiscrError(fCells[iCell]);
}
else if (strcmp(opt,"MonoTargetRegression")==0){
var = GetCellRegValue0(fCells[iCell]);
}
else
Log() << kFATAL << "Project: ERROR: unknown option: " << opt << Endl;
const Double_t xsmall = (1.e-20)*cellSize[idim1];
const Double_t ysmall = (1.e-20)*cellSize[idim2];
Double_t x1 = VarTransformInvers( idim1, cellPosi[idim1]+xsmall );
Double_t y1 = VarTransformInvers( idim2, cellPosi[idim2]+ysmall );
Double_t x2 = VarTransformInvers( idim1, cellPosi[idim1]+cellSize[idim1]-xsmall );
Double_t y2 = VarTransformInvers( idim2, cellPosi[idim2]+cellSize[idim2]-ysmall );
Int_t xbin_start = h1->GetXaxis()->FindBin(x1);
Int_t xbin_stop = h1->GetXaxis()->FindBin(x2);
Int_t ybin_start = h1->GetYaxis()->FindBin(y1);
Int_t ybin_stop = h1->GetYaxis()->FindBin(y2);
for (Int_t ibinx=xbin_start; ibinx<xbin_stop; ibinx++) {
for (Int_t ibiny=ybin_start; ibiny<ybin_stop; ibiny++) {
if (kernel != kNone){
Double_t result = 0.;
Double_t norm = 0.;
Double_t x_curr = VarTransform( idim1, ((x2-x1)*ibinx - x2*xbin_start + x1*xbin_stop)/(xbin_stop-xbin_start) );
Double_t y_curr = VarTransform( idim2, ((y2-y1)*ibiny - y2*ybin_start + y1*ybin_stop)/(ybin_stop-ybin_start) );
for (Long_t ice=0; ice<=fLastCe; ice++) {
if (!(fCells[ice]->GetStat())) continue;
Double_t cell_var = 0.;
if (strcmp(opt,"mean")==0 || strcmp(opt,"nev")==0 || strcmp(opt,"MultiTargetRegression")==0){
PDEFoamVect _cellPosi(GetTotDim()), _cellSize(GetTotDim());
fCells[ice]->GetHcub(_cellPosi, _cellSize);
Double_t cellarea = _cellSize[idim1] * _cellSize[idim2];
cell_var = GetCellEntries(fCells[ice])/(cellarea*foam_area);
}
else if (strcmp(opt,"MonoTargetRegression")==0){
cell_var = GetCellRegValue0(fCells[ice]);
}
else if (strcmp(opt,"rms")==0)
cell_var = GetCellRMS(fCells[ice]);
else if (strcmp(opt,"rms_ov_mean")==0)
cell_var = GetCellRMSovMean(fCells[ice]);
else if (strcmp(opt,"discr")==0){
PDEFoamVect _cellPosi(GetTotDim()), _cellSize(GetTotDim());
fCells[ice]->GetHcub(_cellPosi, _cellSize);
Double_t cellarea = 1.;
for (Int_t d1=0; d1<GetTotDim(); d1++){
if ((d1!=idim1) && (d1!=idim2))
cellarea *= _cellSize[d1];
}
cell_var = GetCellDiscr(fCells[ice])*cellarea;
}
else if (strcmp(opt,"discr_error")==0)
cell_var = GetCellDiscrError(fCells[ice]);
else {
Log() << kFATAL << "ERROR: unsupported option for kernel plot!" << Endl;
return 0;
}
std::vector<Float_t> coor;
for (Int_t i=0; i<GetTotDim(); i++) {
if (i == idim1)
coor.push_back(x_curr);
else if (i == idim2)
coor.push_back(y_curr);
else
coor.push_back(cellPosi[i] + 0.5*cellSize[i]);
}
Double_t weight_ = 0;
if (kernel == kGaus)
weight_ = WeightGaus(fCells[ice], coor);
else
weight_ = 1.;
result += weight_ * cell_var;
norm += weight_;
}
var = result/norm;
}
h1->SetBinContent(ibinx, ibiny, var + h1->GetBinContent(ibinx, ibiny));
if (debug) {
Log() << kVERBOSE << " ibinx=" << ibinx << " ibiny=" << ibiny
<< " cellID=" << iCell
<< " [x1,y1]=[" << x1 << ", " << y1 <<"]"
<< " [x2,y2]=[" << x2 << ", " << y2 <<"] value=" << var << Endl;
}
}
}
}
Log() << kVERBOSE << "Info: Foam has " << cell_count << " active cells" << Endl;
return h1;
}
TH2D* TMVA::PDEFoam::ProjectMC( Int_t idim1, Int_t idim2, Int_t nevents, Int_t nbin )
{
Bool_t debug = kFALSE;
char hname[100]; char htit[100];
sprintf(htit,"MC projection var%d vs var%d", idim1, idim2);
sprintf(hname,"hMCProjections_%d_vs_%d", idim1, idim2);
TH2D* h1 = (TH2D*)gDirectory->Get(hname);
if (h1) delete h1;
h1= new TH2D(hname, htit, nbin, fXmin[idim1], fXmax[idim1], nbin, fXmin[idim2], fXmax[idim2]);
Double_t *MCvect = new Double_t[GetTotDim()];
Double_t wt=0.;
if (debug)
Log() << kVERBOSE << "frho=" << fRho << " fPseRan=" << fPseRan << Endl;
for (Long_t loop=0; loop<nevents; loop++){
MakeEvent();
GetMCvect(MCvect);
GetMCwt(wt);
h1->Fill(MCvect[idim1], MCvect[idim2], wt);
}
return h1;
}
TVectorD* TMVA::PDEFoam::GetCellElements( std::vector<Float_t> xvec )
{
if (unsigned(GetTotDim()) != xvec.size()){
Log() << kFATAL << "ERROR: dimension of foam not equal to cell vector!" << Endl;
return 0;
}
PDEFoamCell *cell= FindCell(VarTransform(xvec));
if (!cell){
Log() << kFATAL << "ERROR: cell not found!" << Endl;
return 0;
}
return dynamic_cast<TVectorD*>(cell->GetElement());
}
Double_t TMVA::PDEFoam::GetCellElement( PDEFoamCell *cell, UInt_t i )
{
if (i >= GetNElements()){
Log() << kFATAL << "PDEFoam: ERROR: Index out of range" << Endl;
return 0.;
}
TVectorD *vec = dynamic_cast<TVectorD*>(cell->GetElement());
if (!vec){
Log() << kFATAL << "<GetCellElement> ERROR: cell element is not a TVectorD*" << Endl;
return 0.;
}
return (*vec)(i);
}
void TMVA::PDEFoam::SetCellElement( PDEFoamCell *cell, UInt_t i, Double_t value )
{
if (i >= GetNElements()){
Log() << kFATAL << "ERROR: Index out of range" << Endl;
return;
}
TVectorD *vec = dynamic_cast<TVectorD*>(cell->GetElement());
if (!vec){
Log() << kFATAL << "<SetCellElement> ERROR: cell element is not a TVectorD*" << Endl;
return;
}
(*vec)(i) = value;
}
void TMVA::PDEFoam::OutputGrow( Bool_t finished )
{
if (finished) {
Log() << kINFO << "Elapsed time: " + fTimer->GetElapsedTime() + " " << Endl;
return;
}
Int_t modulo = 1;
if (fNCells >= 100) modulo = Int_t(fNCells/100);
if (fLastCe%modulo == 0) fTimer->DrawProgressBar( fLastCe );
}
void TMVA::PDEFoam::RootPlot2dim( const TString& filename, std::string what,
Bool_t CreateCanvas, Bool_t colors, Bool_t log_colors )
{
Bool_t plotmean = kFALSE;
Bool_t plotnevents = kFALSE;
Bool_t plotdensity = kFALSE;
Bool_t plotrms = kFALSE;
Bool_t plotrmsovmean = kFALSE;
Bool_t plotcellnumber = kFALSE;
Bool_t plotdiscr = kFALSE;
Bool_t plotdiscrerr = kFALSE;
Bool_t plotmonotarget = kFALSE;
Bool_t fillcells = kTRUE;
if (what == "mean")
plotmean = kTRUE;
else if (what == "nevents")
plotnevents = kTRUE;
else if (what == "density")
plotdensity = kTRUE;
else if (what == "rms")
plotrms = kTRUE;
else if (what == "rms_ov_mean")
plotrmsovmean = kTRUE;
else if (what == "cellnumber")
plotcellnumber = kTRUE;
else if (what == "discr")
plotdiscr = kTRUE;
else if (what == "discrerr")
plotdiscrerr = kTRUE;
else if (what == "monotarget")
plotmonotarget = kTRUE;
else if (what == "nofill") {
plotcellnumber = kTRUE;
fillcells = kFALSE;
}
else {
plotmean = kTRUE;
Log() << kWARNING << "Unknown option, plotting mean!" << Endl;
}
std::ofstream outfile(filename, std::ios::out);
Double_t x1,y1,x2,y2,x,y;
Long_t iCell;
Double_t offs =0.01;
Double_t lpag =1-2*offs;
outfile<<"{" << std::endl;
if (!colors) {
outfile << "TColor *graycolors[100];" << std::endl;
outfile << "for (Int_t i=0.; i<100; i++)" << std::endl;
outfile << " graycolors[i]=new TColor(1000+i, 1-(Float_t)i/100.,1-(Float_t)i/100.,1-(Float_t)i/100.);"<< std::endl;
}
if (CreateCanvas)
outfile<<"cMap = new TCanvas(\""<< fName <<"\",\"Cell Map for "<< fName <<"\",600,600);"<<std::endl;
outfile<<"TBox*a=new TBox();"<<std::endl;
outfile<<"a->SetFillStyle(0);"<<std::endl;
outfile<<"a->SetLineWidth(4);"<<std::endl;
outfile<<"TBox *b1=new TBox();"<<std::endl;
if (fillcells) {
outfile << (colors ? "gStyle->SetPalette(1, 0);" : "gStyle->SetPalette(0);") <<std::endl;
outfile <<"b1->SetFillStyle(1001);"<<std::endl;
outfile<<"TBox *b2=new TBox();"<<std::endl;
outfile <<"b2->SetFillStyle(0);"<<std::endl;
}
else {
outfile <<"b1->SetFillStyle(0);"<<std::endl;
}
Int_t lastcell = fLastCe;
if (fillcells)
(colors ? gStyle->SetPalette(1, 0) : gStyle->SetPalette(0) );
Double_t zmin = 1E8;
Double_t zmax = -1E8;
Double_t value=0.;
for (iCell=1; iCell<=lastcell; iCell++) {
if ( fCells[iCell]->GetStat() == 1) {
if (plotmean)
value = GetCellMean(fCells[iCell]);
else if (plotnevents)
value = GetCellEntries(fCells[iCell]);
else if (plotdensity)
value = GetCellDensity(fCells[iCell]);
else if (plotrms)
value = GetCellRMS(fCells[iCell]);
else if (plotrmsovmean)
value = GetCellRMSovMean(fCells[iCell]);
else if (plotdiscr)
value = GetCellDiscr(fCells[iCell]);
else if (plotdiscrerr)
value = GetCellDiscrError(fCells[iCell]);
else if (plotmonotarget)
value = GetCellRegValue0(fCells[iCell]);
else if (plotcellnumber)
value = iCell;
if (value<zmin)
zmin=value;
if (value>zmax)
zmax=value;
}
}
outfile << "// observed minimum and maximum of distribution: " << std::endl;
outfile << "// Double_t zmin = "<< zmin << ";" << std::endl;
outfile << "// Double_t zmax = "<< zmax << ";" << std::endl;
if (log_colors) {
if (zmin<1)
zmin=1;
zmin=TMath::Log(zmin);
zmax=TMath::Log(zmax);
outfile << "// logarthmic color scale used " << std::endl;
} else
outfile << "// linear color scale used " << std::endl;
outfile << "// used minimum and maximum of distribution (taking into account log scale if applicable): " << std::endl;
outfile << "Double_t zmin = "<< zmin << ";" << std::endl;
outfile << "Double_t zmax = "<< zmax << ";" << std::endl;
Int_t ncolors = colors ? gStyle->GetNumberOfColors() : 100;
Double_t dz = zmax - zmin;
Double_t scale = (ncolors-1)/dz;
if (GetTotDim()==2) {
PDEFoamVect cellPosi(GetTotDim()); PDEFoamVect cellSize(GetTotDim());
outfile << "// =========== Rectangular cells ==========="<< std::endl;
for (iCell=1; iCell<=lastcell; iCell++) {
if ( fCells[iCell]->GetStat() == 1) {
fCells[iCell]->GetHcub(cellPosi,cellSize);
x1 = offs+lpag*(cellPosi[0]); y1 = offs+lpag*(cellPosi[1]);
x2 = offs+lpag*(cellPosi[0]+cellSize[0]); y2 = offs+lpag*(cellPosi[1]+cellSize[1]);
value = 0;
if (fillcells) {
if (plotmean) value = GetCellMean(fCells[iCell]);
else if (plotnevents) value = GetCellEntries(fCells[iCell]);
else if (plotdensity) value = GetCellDensity(fCells[iCell]);
else if (plotrms) value = GetCellRMS(fCells[iCell]);
else if (plotrmsovmean) value = GetCellRMSovMean(fCells[iCell]);
else if (plotdiscr) value = GetCellDiscr(fCells[iCell]);
else if (plotdiscrerr) value = GetCellDiscrError(fCells[iCell]);
else if (plotmonotarget) value = GetCellRegValue0(fCells[iCell]);
else if (plotcellnumber) value = iCell;
if (log_colors) {
if (value<1.) value=1;
value = TMath::Log(value);
}
Int_t color;
if (colors)
color = gStyle->GetColorPalette(Int_t((value-zmin)*scale));
else
color = 1000+(Int_t((value-zmin)*scale));
outfile << "b1->SetFillColor(" << color << ");" << std::endl;
}
outfile<<"b1->DrawBox("<<x1<<","<<y1<<","<<x2<<","<<y2<<");"<<std::endl;
if (fillcells)
outfile<<"b2->DrawBox("<<x1<<","<<y1<<","<<x2<<","<<y2<<");"<<std::endl;
if (lastcell<=250) {
x = offs+lpag*(cellPosi[0]+0.5*cellSize[0]); y = offs+lpag*(cellPosi[1]+0.5*cellSize[1]);
}
}
}
outfile<<"// ============== End Rectangles ==========="<< std::endl;
}
outfile << "}" << std::endl;
outfile.flush();
outfile.close();
}
void TMVA::PDEFoam::SetVolumeFraction( Double_t vfr )
{
fDistr->SetVolumeFraction(vfr);
SetPDEFoamVolumeFraction(vfr);
}
void TMVA::PDEFoam::FillBinarySearchTree( const Event* ev, EFoamType ft, Bool_t NoNegWeights )
{
fDistr->FillBinarySearchTree(ev, ft, NoNegWeights);
}
void TMVA::PDEFoam::Create(Bool_t CreateCellElements)
{
SetRho(fDistr);
Initialize(CreateCellElements);
};
void TMVA::PDEFoam::Init()
{
fDistr->Initialize(GetTotDim());
}
void TMVA::PDEFoam::SetFoamType( EFoamType ft )
{
if (ft==kDiscr)
fDistr->SetDensityCalc(kDISCRIMINATOR);
else if (ft==kMonoTarget)
fDistr->SetDensityCalc(kTARGET);
else
fDistr->SetDensityCalc(kEVENT_DENSITY);
}
void TMVA::PDEFoam::PrintDensity(){
fDistr->PrintDensity();
}
ostream& TMVA::operator<< ( ostream& os, const TMVA::PDEFoam& pdefoam )
{
pdefoam.PrintStream(os);
return os;
}
istream& TMVA::operator>> ( istream& istr, TMVA::PDEFoam& pdefoam )
{
pdefoam.ReadStream(istr);
return istr;
}
void TMVA::PDEFoam::ReadStream( istream & istr )
{
istr >> fLastCe;
istr >> fNCells;
istr >> fDim;
Double_t vfr = -1.;
istr >> vfr;
SetPDEFoamVolumeFraction(vfr);
Log() << kVERBOSE << "Foam dimension: " << GetTotDim() << Endl;
if (fXmin) delete [] fXmin;
if (fXmax) delete [] fXmax;
fXmin = new Double_t[GetTotDim()];
fXmax = new Double_t[GetTotDim()];
for (Int_t i=0; i<GetTotDim(); i++)
istr >> fXmin[i];
for (Int_t i=0; i<GetTotDim(); i++)
istr >> fXmax[i];
}
void TMVA::PDEFoam::PrintStream( ostream & ostr ) const
{
ostr << fLastCe << std::endl;
ostr << fNCells << std::endl;
ostr << fDim << std::endl;
ostr << GetPDEFoamVolumeFraction() << std::endl;
for (Int_t i=0; i<GetTotDim(); i++)
ostr << fXmin[i] << std::endl;
for (Int_t i=0; i<GetTotDim(); i++)
ostr << fXmax[i] << std::endl;
}
void TMVA::PDEFoam::AddXMLTo( void* parent ){
void *variables = gTools().xmlengine().NewChild( parent, 0, "Variables" );
gTools().AddAttr( variables, "LastCe", fLastCe );
gTools().AddAttr( variables, "nCells", fNCells );
gTools().AddAttr( variables, "Dim", fDim );
gTools().AddAttr( variables, "VolumeFraction", GetPDEFoamVolumeFraction() );
void *xmin_wrap;
for (Int_t i=0; i<GetTotDim(); i++){
xmin_wrap = gTools().xmlengine().NewChild( variables, 0, "Xmin" );
gTools().AddAttr( xmin_wrap, "Index", i );
gTools().AddAttr( xmin_wrap, "Value", fXmin[i] );
}
void *xmax_wrap;
for (Int_t i=0; i<GetTotDim(); i++){
xmax_wrap = gTools().xmlengine().NewChild( variables, 0, "Xmax" );
gTools().AddAttr( xmax_wrap, "Index", i );
gTools().AddAttr( xmax_wrap, "Value", fXmax[i] );
}
}
void TMVA::PDEFoam::ReadXML( void* parent ){
void *variables = gTools().xmlengine().GetChild( parent );
gTools().ReadAttr( variables, "LastCe", fLastCe );
gTools().ReadAttr( variables, "nCells", fNCells );
gTools().ReadAttr( variables, "Dim", fDim );
Float_t volfr;
gTools().ReadAttr( variables, "VolumeFraction", volfr );
SetPDEFoamVolumeFraction( volfr );
if (fXmin) delete [] fXmin;
if (fXmax) delete [] fXmax;
fXmin = new Double_t[GetTotDim()];
fXmax = new Double_t[GetTotDim()];
void *xmin_wrap = gTools().xmlengine().GetChild( variables );
for (Int_t counter=0; counter<fDim; counter++) {
Int_t i=0;
gTools().ReadAttr( xmin_wrap , "Index", i );
if (i >= GetTotDim() || i<0)
Log() << kFATAL << "dimension index out of range:" << i << Endl;
gTools().ReadAttr( xmin_wrap , "Value", fXmin[i] );
xmin_wrap = gTools().xmlengine().GetNext( xmin_wrap );
}
void *xmax_wrap = xmin_wrap;
for (Int_t counter=0; counter<fDim; counter++) {
Int_t i=0;
gTools().ReadAttr( xmax_wrap , "Index", i );
if (i >= GetTotDim() || i<0)
Log() << kFATAL << "dimension index out of range:" << i << Endl;
gTools().ReadAttr( xmax_wrap , "Value", fXmax[i] );
xmax_wrap = gTools().xmlengine().GetNext( xmax_wrap );
}
}
TMVA::TFDISTR::TFDISTR() :
fDim(-1),
fXmin(0),
fXmax(0),
fVolFrac(-1.),
fBst(NULL),
fDensityCalc(kEVENT_DENSITY),
fSignalClass(1),
fBackgroundClass(0),
fLogger( new MsgLogger("TFDISTR"))
{}
TMVA::TFDISTR::~TFDISTR() {
if (fBst) delete fBst;
if (fXmin) delete [] fXmin; fXmin=0;
if (fXmax) delete [] fXmax; fXmax=0;
delete fLogger;
}
void TMVA::TFDISTR::PrintDensity()
{
char name[]={"EventfromDensity::PrintDensity"};
Log() << kINFO << name<<" Volume fraction to search for events: "<<fVolFrac<< Endl;
Log() << kINFO << name<<" Binary Tree: "<<fBst<< Endl;
if (!fBst) Log() << kINFO <<name<<" Binary tree not found ! "<< Endl;
Log() << kINFO <<name<<" Volfraction= "<<fVolFrac<< Endl;
for (Int_t idim=0; idim<fDim; idim++){
Log() << kINFO <<name<<idim<<" fXmin["<<idim<<"]= "<<fXmin[idim]
<<" fXmax["<<idim<<"]= "<<fXmax[idim]<< Endl;
}
}
void TMVA::TFDISTR::Initialize( Int_t ndim )
{
std::string name = "EventfromDensity::PreInitialize:";
this->SetDim(ndim);
if (fBst) delete fBst;
fBst = new TMVA::BinarySearchTree();
if (!fBst){
Log() << kFATAL << name << "ERROR: an not create binary tree !" << Endl;
return;
}
fBst->SetPeriode(fDim);
}
void TMVA::TFDISTR::FillBinarySearchTree( const Event* ev, EFoamType ft, Bool_t NoNegWeights )
{
if(NoNegWeights && ev->GetWeight()<=0)
return;
TMVA::Event *event = new TMVA::Event(*ev);
event->SetSignalClass( fSignalClass );
if (ft==kSeparate || ft==kDiscr){
event->SetClass(ev->IsSignal() ? fSignalClass : fBackgroundClass);
} else if (ft==kMultiTarget){
std::vector<Float_t> targets = ev->GetTargets();
for (UInt_t i = 0; i < targets.size(); i++)
event->SetVal(i+ev->GetValues().size(), targets.at(i));
event->GetTargets().clear();
event->SetClass(fSignalClass);
}
fBst->Insert(event);
}
void TMVA::TFDISTR::FillEdgeHist( Int_t nDim, TObjArray *myfHistEdg,
Double_t *cellPosi, Double_t *cellSize, Double_t *ceSum, Double_t ceVol )
{
Double_t wt;
std::vector<Double_t> lb(nDim+1);
std::vector<Double_t> ub(nDim+1);
Double_t *valuesarr = new Double_t[Int_t(nDim)];
for (Int_t idim=0; idim<nDim; idim++){
lb[idim]=fXmin[idim]+(fXmax[idim]-fXmin[idim])*cellPosi[idim];
ub[idim]=fXmin[idim]+(fXmax[idim]-fXmin[idim])*(cellPosi[idim]+cellSize[idim]);
}
lb[nDim] = -1.e19;
ub[nDim] = +1.e19;
TMVA::Volume volume(&lb, &ub);
std::vector<const TMVA::BinarySearchTreeNode*> nodes;
if (!fBst) {
Log() << kFATAL <<"Binary tree not found ! "<< Endl;
exit (1);
}
fBst->SearchVolume(&volume, &nodes);
for (std::vector<const TMVA::BinarySearchTreeNode*>::iterator inode = nodes.begin(); inode != nodes.end(); inode++) {
const std::vector<Float_t>& values = (*inode)->GetEventV();
for (Int_t idim=0; idim<nDim; idim++) {
valuesarr[idim]=(values[idim]-fXmin[idim])/(fXmax[idim]-fXmin[idim]);
}
Double_t event_density = 0;
wt = (TFDISTR::Density(nDim, valuesarr, event_density)) * ceVol;
for (Int_t idim=0; idim<nDim; idim++)
((TH1D *)(*myfHistEdg)[idim])->Fill((values[idim]-fXmin[idim])/(fXmax[idim]-fXmin[idim]), wt);
ceSum[0] += wt;
ceSum[1] += wt*wt;
ceSum[2]++;
if (ceSum[3]>wt) ceSum[3]=wt;
if (ceSum[4]<wt) ceSum[4]=wt;
}
delete[] valuesarr;
}
UInt_t TMVA::TFDISTR::GetNEvents( PDEFoamCell* cell )
{
PDEFoamVect cellSize(fDim);
PDEFoamVect cellPosi(fDim);
cell->GetHcub(cellPosi,cellSize);
std::vector<Float_t> lb;
std::vector<Float_t> ub;
for (Int_t idim=0; idim<fDim; idim++) {
lb.push_back(VarTransformInvers(idim, cellPosi[idim]));
ub.push_back(VarTransformInvers(idim, cellPosi[idim] + cellSize[idim]));
}
TMVA::Volume volume(&lb, &ub);
std::vector<const TMVA::BinarySearchTreeNode*> nodes;
if (!fBst) {
Log() << kFATAL << "<TFDISTR::GetNEvents> Binary tree not found!" << Endl;
exit (1);
}
fBst->SearchVolume(&volume, &nodes);
return nodes.size();
}
Bool_t TMVA::TFDISTR::CellRMSCut( PDEFoamCell* cell, Float_t rms_cut, Int_t nbin )
{
PDEFoamVect cellSize(fDim);
PDEFoamVect cellPosi(fDim);
cell->GetHcub(cellPosi, cellSize);
std::vector<Float_t> lb;
std::vector<Float_t> ub;
for (Int_t idim=0; idim<fDim; idim++) {
lb.push_back(VarTransformInvers(idim, cellPosi[idim]));
ub.push_back(VarTransformInvers(idim, cellPosi[idim] + cellSize[idim]));
}
TMVA::Volume volume(&lb, &ub);
std::vector<const TMVA::BinarySearchTreeNode*> nodes;
if (!fBst) {
Log() << kFATAL << "<TFDISTR::CellRMSCut> Binary tree not found!" << Endl;
return 0.;
}
fBst->SearchVolume(&volume, &nodes);
if (nodes.size() <= 1)
return kFALSE;
std::vector<TH1F*> histo;
for (Int_t i=0; i<fDim; i++){
std::stringstream title (std::stringstream::in | std::stringstream::out);
title << "proj" << i;
histo.push_back(new TH1F(title.str().c_str(), title.str().c_str(), nbin, lb.at(i), ub.at(i)));
}
for (UInt_t i=0; i<nodes.size(); i++){
for (Int_t d=0; d<fDim; d++)
(histo.at(d))->Fill((nodes.at(i)->GetEventV()).at(d), nodes.at(i)->GetWeight());
}
std::vector<Float_t> rms_mean, rms_err_mean;
for (Int_t i=0; i<fDim; i++){
std::vector<Float_t> bin_val;
for (Int_t k=1; k<=nbin; k++){
bin_val.push_back( histo.at(i)->GetBinContent(k) );
}
Float_t mean = 0;
for (UInt_t k=0; k<bin_val.size(); k++)
mean += bin_val.at(k);
mean /= bin_val.size();
Float_t rms = 0;
for (UInt_t k=0; k<bin_val.size(); k++)
rms += TMath::Power( bin_val.at(k) - mean, 2 );
rms /= (bin_val.size()-1);
rms = TMath::Sqrt(rms)/mean;
Float_t rms_err = 0;
rms_err = (1+rms*TMath::Sqrt(2))*rms/TMath::Sqrt(2*bin_val.size());
if (TMath::Abs(mean) > 1.0e-15){
rms_mean.push_back( rms );
rms_err_mean.push_back( rms_err );
}
else {
rms_mean.push_back(0.);
rms_err_mean.push_back(1.);
}
}
Bool_t result = kFALSE;
for (Int_t i=0; i<fDim; i++){
result = result || (rms_mean.at(i) > rms_cut);
}
for (Int_t i=0; i<fDim; i++)
delete histo.at(i);
return result;
}
Double_t TMVA::TFDISTR::Density( Int_t nDim, Double_t *Xarg, Double_t &event_density )
{
assert(nDim == fDim);
Bool_t simplesearch=true;
Bool_t usegauss=true;
Int_t targetevents=0;
char name[]={" DensityFromEvents: "};
Bool_t debug0 = false;
Bool_t debug = false;
Double_t volscale=1.;
Double_t lasthighvolscale=0.;
Double_t lastlowvolscale=0.;
Int_t nit=0;
Double_t probevolume_inv;
static Int_t nev=0; nev++;
if (debug) for (Int_t idim=0; idim<nDim; idim++)
Log() << kVERBOSE << nev << name << idim <<" unscaled in: Xarg= "<< Xarg[idim] << Endl;
Double_t wbin;
for (Int_t idim=0; idim<nDim; idim++){
if (debug)
std::cerr << "fXmin[" << idim << "] = " << fXmin[idim]
<< ", fXmax[" << idim << "] = " << fXmax[idim] << std::endl;
wbin=fXmax[idim]-fXmin[idim];
Xarg[idim]=fXmin[idim]+wbin*Xarg[idim];
}
if (debug0) for (Int_t idim=0; idim<nDim; idim++)
Log() << kVERBOSE <<nev<<name<<idim<<" scaled in: Xarg= "<<Xarg[idim]<< Endl;
std::vector<Double_t> lb(nDim);
std::vector<Double_t> ub(nDim);
volumesearchstart:
probevolume_inv = pow((fVolFrac/2/volscale), nDim);
for (Int_t idim = 0; idim < fDim; idim++) {
lb[idim] = Xarg[idim];
ub[idim] = Xarg[idim];
}
for (Int_t idim = 0; idim < nDim; idim++) {
Double_t volsize=(fXmax[idim] - fXmin[idim]) / fVolFrac * volscale;
lb[idim] -= volsize;
ub[idim] += volsize;
if (debug) {
std::cerr << "lb[" << idim << "]=" << lb[idim] << std::endl;
std::cerr << "ub[" << idim << "]=" << ub[idim] << std::endl;
}
}
TMVA::Volume volume(&lb, &ub);
std::vector<const TMVA::BinarySearchTreeNode*> nodes;
if (!fBst) {
Log() << kFATAL <<name<<" Binary tree not found ! "<< Endl;
}
fBst->SearchVolume(&volume, &nodes);
Double_t count=(Double_t) (nodes.size());
event_density = count * probevolume_inv;
if (targetevents>0 && nit<25) {
nit++;
if (count<targetevents) {
lastlowvolscale=volscale;
if (lasthighvolscale>0.)
volscale+=(lasthighvolscale-volscale)/2;
else
volscale*=1.5;
goto volumesearchstart;
}
}
if (targetevents>0 && count<targetevents)
Log() << kWARNING << "WARNING: Number of target events not reached within 25 iterations. count=="
<< count << Endl;
Double_t N_sig = 0;
Double_t N_tar = 0;
Double_t weighted_count = 0.;
for (UInt_t j=0; j<nodes.size(); j++)
weighted_count += (nodes.at(j))->GetWeight();
if (FillDiscriminator()){
N_sig = 0;
for (Int_t j=0; j<count; j++){
N_sig += ((nodes.at(j))->IsSignal()?1.:0.) * (nodes.at(j))->GetWeight();
}
}
else if (FillTarget0()){
N_tar = 0;
for (Int_t j=0; j<count; j++) {
if (((nodes.at(j))->GetTargets()).size() < 1)
Log() << kFATAL << "ERROR: No targets for node " << j << Endl;
N_tar += ((nodes.at(j))->GetTargets()).at(0) * ((nodes.at(j))->GetWeight());
}
}
if (simplesearch){
if (FillDiscriminator())
return (N_sig/(weighted_count+0.1))*probevolume_inv;
else if (FillTarget0())
return (N_tar/(weighted_count+0.1))*probevolume_inv;
else
return ((weighted_count+0.1)*probevolume_inv);
}
Double_t density = 0.;
Double_t normalised_distance = 0;
if (count==0)
return (0.1*probevolume_inv);
unsigned usednodes;
for (usednodes = 0; usednodes < nodes.size(); usednodes++) {
const std::vector<Float_t>& values = nodes[usednodes]->GetEventV();
for (Int_t idim = 0; idim < nDim; idim++)
normalised_distance += pow( ( values[idim]-Xarg[idim] )/ (ub[idim]-lb[idim]) * 2 , 2);
normalised_distance = sqrt(normalised_distance/nDim);
if (usegauss)
density+=TMath::Gaus(normalised_distance, 0, 0.3, kFALSE);
else
density+=(1-normalised_distance);
}
density /= usednodes;
if (FillDiscriminator())
density = (N_sig/density)*probevolume_inv;
else if (FillTarget0())
density = (N_tar/density)*probevolume_inv;
else
density *= probevolume_inv;
return density;
}
TH2D* TMVA::TFDISTR::MakeHistogram( Int_t nbinsx, Int_t nbinsy )
{
const Bool_t debug = kFALSE;
TH2D *foamhist = new TH2D("foamhist", "Distributon",
nbinsx, fXmin[0], fXmax[0],
nbinsy, fXmin[1], fXmax[1]);
if (debug)
std::cerr << "TFDIST::MakeHistogram, nbinsx=" << nbinsx
<< ", nbinsy=" << nbinsy
<< ", fXmin[0]=" << fXmin[0]
<< ", fXmax[0]=" << fXmax[0]
<< ", fXmin[1]=" << fXmin[1]
<< ", fXmax[1]=" << fXmax[1]
<< std::endl;
Double_t xvec[2];
for (Int_t i = 1; i < nbinsx+1; i++) {
for (Int_t j = 1; j < nbinsy+1; j++) {
xvec[0] = foamhist->GetXaxis()->GetBinCenter(i);
xvec[1] = foamhist->GetYaxis()->GetBinCenter(j);
xvec[0] = (xvec[0] - fXmin[0]) / (fXmax[0] - fXmin[0]);
xvec[1] = (xvec[1] - fXmin[1]) / (fXmax[1] - fXmin[1]);
if (debug)
std::cerr << "xvec[0]=" << xvec[0]
<< ", xvec[1]=" << xvec[1]
<< std::endl;
Int_t ibin = foamhist->GetBin(i, j);
Double_t var = this->Density(2, xvec);
foamhist->SetBinContent(ibin, var);
if (debug)
std::cerr << "i=" << i
<< ", j=" << j
<< ", var=" << var
<< ", ibin=" << ibin
<< std::endl;
}
}
return foamhist;
}
ClassImp(TMVA::TFDISTR)
ClassImp(TMVA::PDEFoamIntegrand);
TMVA::PDEFoamIntegrand::PDEFoamIntegrand(){};
ClassImp(TMVA::PDEFoamCell)
TMVA::PDEFoamCell::PDEFoamCell()
{
fParent = 0;
fDaught0 = 0;
fDaught1 = 0;
fElement = 0;
}
TMVA::PDEFoamCell::PDEFoamCell(Int_t kDim)
{
if ( kDim >0) {
fDim = kDim;
fStatus = 1;
fParent = 0;
fDaught0 = 0;
fDaught1 = 0;
fXdiv = 0.0;
fBest = 0;
fVolume = 0.0;
fIntegral = 0.0;
fDrive = 0.0;
fPrimary = 0.0;
fElement = 0;
} else
Error( "PDEFoamCell", "Dimension has to be >0" );
}
TMVA::PDEFoamCell::PDEFoamCell(PDEFoamCell &From): TObject(From)
{
Error( "PDEFoamCell", "+++++ NEVER USE Copy constructor for PDEFoamCell");
fStatus = From.fStatus;
fParent = From.fParent;
fDaught0 = From.fDaught0;
fDaught1 = From.fDaught1;
fXdiv = From.fXdiv;
fBest = From.fBest;
fVolume = From.fVolume;
fIntegral = From.fIntegral;
fDrive = From.fDrive;
fPrimary = From.fPrimary;
fElement = From.fElement;
}
TMVA::PDEFoamCell::~PDEFoamCell()
{
}
TMVA::PDEFoamCell& TMVA::PDEFoamCell::operator=(const PDEFoamCell &From)
{
Info("PDEFoamCell", "operator=\n ");
if (&From == this) return *this;
fStatus = From.fStatus;
fParent = From.fParent;
fDaught0 = From.fDaught0;
fDaught1 = From.fDaught1;
fXdiv = From.fXdiv;
fBest = From.fBest;
fVolume = From.fVolume;
fIntegral = From.fIntegral;
fDrive = From.fDrive;
fPrimary = From.fPrimary;
fElement = From.fElement;
return *this;
}
void TMVA::PDEFoamCell::Fill(Int_t Status, PDEFoamCell *Parent, PDEFoamCell *Daugh1, PDEFoamCell *Daugh2)
{
fStatus = Status;
fParent = Parent;
fDaught0 = Daugh1;
fDaught1 = Daugh2;
}
void TMVA::PDEFoamCell::GetHcub( PDEFoamVect &cellPosi, PDEFoamVect &cellSize) const
{
if(fDim<1) return;
const PDEFoamCell *pCell,*dCell;
cellPosi = 0.0; cellSize=1.0;
dCell = this;
while(dCell != 0) {
pCell = dCell->GetPare();
if( pCell== 0) break;
Int_t kDiv = pCell->fBest;
Double_t xDivi = pCell->fXdiv;
if(dCell == pCell->GetDau0() ) {
cellSize[kDiv] *=xDivi;
cellPosi[kDiv] *=xDivi;
} else if( dCell == pCell->GetDau1() ) {
cellSize[kDiv] *=(1.0-xDivi);
cellPosi[kDiv] =cellPosi[kDiv]*(1.0-xDivi)+xDivi;
} else {
Error( "GetHcub ","Something wrong with linked tree \n");
}
dCell=pCell;
}
}
void TMVA::PDEFoamCell::GetHSize( PDEFoamVect &cellSize) const
{
if(fDim<1) return;
const PDEFoamCell *pCell,*dCell;
cellSize=1.0;
dCell = this;
while(dCell != 0) {
pCell = dCell->GetPare();
if( pCell== 0) break;
Int_t kDiv = pCell->fBest;
Double_t xDivi = pCell->fXdiv;
if(dCell == pCell->GetDau0() ) {
cellSize[kDiv]=cellSize[kDiv]*xDivi;
} else if(dCell == pCell->GetDau1() ) {
cellSize[kDiv]=cellSize[kDiv]*(1.0-xDivi);
} else {
Error( "GetHSize ","Something wrong with linked tree \n");
}
dCell=pCell;
}
}
void TMVA::PDEFoamCell::CalcVolume(void)
{
Int_t k;
Double_t volu=1.0;
if(fDim>0) {
PDEFoamVect cellSize(fDim);
GetHSize(cellSize);
for(k=0; k<fDim; k++) volu *= cellSize[k];
}
fVolume =volu;
}
void TMVA::PDEFoamCell::Print(Option_t *option) const
{
if (!option) Error( "Print", "No option set\n");
cout << " Status= "<< fStatus <<",";
cout << " Volume= "<< fVolume <<",";
cout << " TrueInteg= " << fIntegral <<",";
cout << " DriveInteg= "<< fDrive <<",";
cout << " PrimInteg= " << fPrimary <<",";
cout << endl;
cout << " Xdiv= "<<fXdiv<<",";
cout << " Best= "<<fBest<<",";
cout << " Parent= {"<< (GetPare() ? GetPare()->GetSerial() : -1) <<"} ";
cout << " Daught0= {"<< (GetDau0() ? GetDau0()->GetSerial() : -1 )<<"} ";
cout << " Daught1= {"<< (GetDau1() ? GetDau1()->GetSerial() : -1 )<<"} ";
cout << endl;
if (fDim>0 ) {
PDEFoamVect cellPosi(fDim); PDEFoamVect cellSize(fDim);
GetHcub(cellPosi,cellSize);
cout <<" Posi= "; cellPosi.Print("1"); cout<<","<< endl;
cout <<" Size= "; cellSize.Print("1"); cout<<","<< endl;
}
}
ClassImp(TMVA::PDEFoamMaxwt);
TMVA::PDEFoamMaxwt::PDEFoamMaxwt():
fLogger( new MsgLogger("PDEFoamMaxwt") )
{
fNent = 0;
fnBin = 0;
fWtHst1 = 0;
fWtHst2 = 0;
}
TMVA::PDEFoamMaxwt::PDEFoamMaxwt(Double_t wmax, Int_t nBin):
fLogger( new MsgLogger("PDEFoamMaxwt") )
{
fNent = 0;
fnBin = nBin;
fwmax = wmax;
fWtHst1 = new TH1D("PDEFoamMaxwt_hst_Wt1","Histo of weight ",nBin,0.0,wmax);
fWtHst2 = new TH1D("PDEFoamMaxwt_hst_Wt2","Histo of weight**2",nBin,0.0,wmax);
fWtHst1->SetDirectory(0);
fWtHst2->SetDirectory(0);
}
TMVA::PDEFoamMaxwt::PDEFoamMaxwt(PDEFoamMaxwt &From):
TObject(From),
fLogger( new MsgLogger("PDEFoamMaxwt") )
{
fnBin = From.fnBin;
fwmax = From.fwmax;
fWtHst1 = From.fWtHst1;
fWtHst2 = From.fWtHst2;
Error( "PDEFoamMaxwt","COPY CONSTRUCTOR NOT TESTED!");
}
TMVA::PDEFoamMaxwt::~PDEFoamMaxwt()
{
delete fWtHst1;
delete fWtHst2;
fWtHst1=0;
fWtHst2=0;
delete fLogger;
}
void TMVA::PDEFoamMaxwt::Reset()
{
fNent = 0;
fWtHst1->Reset();
fWtHst2->Reset();
}
TMVA::PDEFoamMaxwt& TMVA::PDEFoamMaxwt::operator=(const PDEFoamMaxwt &From)
{
if (&From == this) return *this;
fnBin = From.fnBin;
fwmax = From.fwmax;
fWtHst1 = From.fWtHst1;
fWtHst2 = From.fWtHst2;
return *this;
}
void TMVA::PDEFoamMaxwt::Fill(Double_t wt)
{
fNent = fNent+1.0;
fWtHst1->Fill(wt,1.0);
fWtHst2->Fill(wt,wt);
}
void TMVA::PDEFoamMaxwt::Make(Double_t eps, Double_t &MCeff)
{
Double_t wtLim,aveWt;
GetMCeff(eps, MCeff, wtLim);
aveWt = MCeff*wtLim;
Log()<< "00000000000000000000000000000000000000000000000000000000000000000000000"<<Endl;
Log()<< "00 -->wtLim: No_evt ="<<fNent<<" <Wt> = "<<aveWt<<" wtLim= "<<wtLim<<Endl;
Log()<< "00 -->wtLim: For eps = "<<eps <<" EFFICIENCY <Wt>/wtLim= "<<MCeff<<Endl;
Log()<< "00000000000000000000000000000000000000000000000000000000000000000000000"<<Endl;
}
void TMVA::PDEFoamMaxwt::GetMCeff(Double_t eps, Double_t &MCeff, Double_t &wtLim)
{
Int_t ib,ibX;
Double_t lowEdge,bin,bin1;
Double_t aveWt, aveWt1;
fWtHst1->Print();
fWtHst2->Print();
Double_t sum = 0.0;
Double_t sumWt = 0.0;
for(ib=0;ib<=fnBin+1;ib++) {
sum += fWtHst1->GetBinContent(ib);
sumWt += fWtHst2->GetBinContent(ib);
}
if( (sum == 0.0) || (sumWt == 0.0) ) {
Log()<<"PDEFoamMaxwt::Make: zero content of histogram !!!,sum,sumWt ="<<sum<<sumWt<<Endl;
}
aveWt = sumWt/sum;
for( ibX=fnBin+1; ibX>0; ibX--) {
lowEdge = (ibX-1.0)*fwmax/fnBin;
sum = 0.0;
sumWt = 0.0;
for( ib=0; ib<=fnBin+1; ib++) {
bin = fWtHst1->GetBinContent(ib);
bin1 = fWtHst2->GetBinContent(ib);
if(ib >= ibX) bin1=lowEdge*bin;
sum += bin;
sumWt += bin1;
}
aveWt1 = sumWt/sum;
if( TMath::Abs(1.0-aveWt1/aveWt) > eps ) break;
}
if(ibX == (fnBin+1) ) {
wtLim = 1.0e200;
MCeff = 0.0;
Log()<< "+++++ wtLim undefined. Higher uper limit in histogram"<<Endl;
} else if( ibX == 1) {
wtLim = 0.0;
MCeff =-1.0;
Log()<< "+++++ wtLim undefined. Lower uper limit or more bins "<<Endl;
} else {
wtLim= (ibX)*fwmax/fnBin;
MCeff = aveWt/wtLim;
}
}
ClassImp(TMVA::PDEFoamVect);
TMVA::PDEFoamVect::PDEFoamVect():
fLogger( new MsgLogger("PDEFoamVect") )
{
fDim =0;
fCoords =0;
fNext =0;
fPrev =0;
}
TMVA::PDEFoamVect::PDEFoamVect(Int_t n):
fLogger( new MsgLogger("PDEFoamVect") )
{
Int_t i;
fNext=0;
fPrev=0;
fDim=n;
fCoords = 0;
if (n>0) {
fCoords = new Double_t[fDim];
if(gDebug) {
if(fCoords == 0)
Error( "PDEFoamVect", "Constructor failed to allocate\n");
}
for (i=0; i<n; i++) *(fCoords+i)=0.0;
}
if(gDebug) Info("PDEFoamVect", "USER CONSTRUCTOR PDEFoamVect(const Int_t)\n ");
}
TMVA::PDEFoamVect::PDEFoamVect(const PDEFoamVect &Vect):
TObject(Vect),
fLogger( new MsgLogger("PDEFoamVect") )
{
fNext=0;
fPrev=0;
fDim=Vect.fDim;
fCoords = 0;
if(fDim>0) fCoords = new Double_t[fDim];
if(gDebug) {
if(fCoords == 0) {
Error( "PDEFoamVect", "Constructor failed to allocate fCoords\n");
}
}
for(Int_t i=0; i<fDim; i++)
fCoords[i] = Vect.fCoords[i];
Error( "PDEFoamVect","+++++ NEVER USE Copy constructor !!!!!\n ");
}
TMVA::PDEFoamVect::~PDEFoamVect()
{
if(gDebug) Info("PDEFoamVect"," DESTRUCTOR PDEFoamVect~ \n");
delete [] fCoords;
fCoords=0;
delete fLogger;
}
TMVA::PDEFoamVect& TMVA::PDEFoamVect::operator =(const PDEFoamVect& Vect)
{
Int_t i;
if (&Vect == this) return *this;
if( fDim != Vect.fDim )
Error( "PDEFoamVect","operator=Dims. are different: %d and %d \n ",fDim,Vect.fDim);
if( fDim != Vect.fDim ) {
delete [] fCoords;
fCoords = new Double_t[fDim];
}
fDim=Vect.fDim;
for(i=0; i<fDim; i++)
fCoords[i] = Vect.fCoords[i];
fNext=Vect.fNext;
fPrev=Vect.fPrev;
if(gDebug) Info("PDEFoamVect", "SUBSITUTE operator =\n ");
return *this;
}
Double_t &TMVA::PDEFoamVect::operator[](Int_t n)
{
if ((n<0) || (n>=fDim)) {
Error( "PDEFoamVect","operator[], out of range \n");
}
return fCoords[n];
}
TMVA::PDEFoamVect& TMVA::PDEFoamVect::operator*=(const Double_t &x)
{
for(Int_t i=0;i<fDim;i++)
fCoords[i] = fCoords[i]*x;
return *this;
}
TMVA::PDEFoamVect& TMVA::PDEFoamVect::operator+=(const PDEFoamVect& Shift)
{
if( fDim != Shift.fDim){
Error( "PDEFoamVect","operator+, different dimensions= %d %d \n",fDim,Shift.fDim);
}
for(Int_t i=0;i<fDim;i++)
fCoords[i] = fCoords[i]+Shift.fCoords[i];
return *this;
}
TMVA::PDEFoamVect& TMVA::PDEFoamVect::operator-=(const PDEFoamVect& Shift)
{
if( fDim != Shift.fDim) {
Error( "PDEFoamVect","operator+, different dimensions= %d %d \n",fDim,Shift.fDim);
}
for(Int_t i=0;i<fDim;i++)
fCoords[i] = fCoords[i]-Shift.fCoords[i];
return *this;
}
TMVA::PDEFoamVect TMVA::PDEFoamVect::operator+(const PDEFoamVect &p2)
{
PDEFoamVect temp(fDim);
temp = (*this);
temp += p2;
return temp;
}
TMVA::PDEFoamVect TMVA::PDEFoamVect::operator-(const PDEFoamVect &p2)
{
PDEFoamVect temp(fDim);
temp = (*this);
temp -= p2;
return temp;
}
TMVA::PDEFoamVect& TMVA::PDEFoamVect::operator =(Double_t Vect[])
{
Int_t i;
for(i=0; i<fDim; i++)
fCoords[i] = Vect[i];
return *this;
}
TMVA::PDEFoamVect& TMVA::PDEFoamVect::operator =(Double_t x)
{
if(fCoords != 0) {
for(Int_t i=0; i<fDim; i++)
fCoords[i] = x;
}
return *this;
}
void TMVA::PDEFoamVect::Print(Option_t *option) const
{
if(!option) Error( "Print ", "No option set \n");
Int_t i;
cout << "(";
for(i=0; i<fDim-1; i++) cout << SW2 << *(fCoords+i) << ",";
cout << SW2 << *(fCoords+fDim-1);
cout << ")";
}
void TMVA::PDEFoamVect::PrintList(void)
{
Long_t i=0;
if(this == 0) return;
PDEFoamVect *current=this;
while(current != 0) {
Log() << "vec["<<i<<"]=";
current->Print("1");
Log() << Endl;
current = current->fNext;
i++;
}
}