72 if (strcor==
"Uniform") {fFlatNotGauss =
kTRUE;
100 Log() << kINFO <<
"Preparing the Gaussian transformation..." <<
Endl;
105 if (inputSize > 200) {
106 Log() << kWARNING <<
"----------------------------------------------------------------------------" 109 <<
": More than 200 variables, I hope you have enough memory!!!!" <<
Endl;
110 Log() << kWARNING <<
"----------------------------------------------------------------------------" 127 if (!
IsCreated())
Log() << kFATAL <<
"Transformation not yet created" <<
Endl;
139 std::vector<Float_t> input(0);
140 std::vector<Float_t>
output(0);
142 std::vector<Char_t> mask;
145 std::vector<Char_t>::iterator itMask = mask.begin();
151 for (
UInt_t ivar=0; ivar<inputSize; ivar++) {
168 output.push_back( cumulant );
171 Double_t maxErfInvArgRange = 0.99999999;
196 if (!
IsCreated())
Log() << kFATAL <<
"Transformation not yet created" <<
Endl;
208 std::vector<Float_t> input(0);
209 std::vector<Float_t>
output(0);
211 std::vector<Char_t> mask;
214 std::vector<Char_t>::iterator itMask = mask.begin();
220 for (
UInt_t ivar=0; ivar<inputSize; ivar++) {
228 invCumulant = input.at(ivar);
232 invCumulant = (
TMath::Erf(invCumulant/1.414213562)+1)/2.
f;
238 Log() << kFATAL <<
"Inverse Uniform/Gauss transformation not implemented for TMVA versions before 4.1.0" <<
Endl;
240 output.push_back(invCumulant);
260 UInt_t nevt = events.size();
263 UInt_t numDist = nClasses+1;
269 std::list< TMVA::TMVAGaussPair > **listsForBinning =
new std::list<TMVA::TMVAGaussPair>* [numDist];
270 std::vector< Float_t > **vsForBinning =
new std::vector<Float_t>* [numDist];
271 for (
UInt_t i=0; i < numDist; i++) {
272 listsForBinning[i] =
new std::list<TMVA::TMVAGaussPair> [inputSize];
273 vsForBinning[i] =
new std::vector<Float_t> [inputSize];
274 nbins[i] =
new UInt_t[inputSize];
277 std::vector<Float_t> input;
278 std::vector<Char_t> mask;
284 for (
UInt_t i=0; i<numDist; i++) {
289 for (
UInt_t ievt=0; ievt < nevt; ievt++) {
290 const Event* ev= events[ievt];
293 sumOfWeights[cls] += eventWeight;
294 if (minWeight[cls] > eventWeight) minWeight[cls]=eventWeight;
295 if (maxWeight[cls] < eventWeight) maxWeight[cls]=eventWeight;
296 if (numDist>1) sumOfWeights[numDist-1] += eventWeight;
299 if( hasMaskedEntries ){
300 Log() << kWARNING <<
"Incomplete event" <<
Endl;
301 std::ostringstream oss;
304 Log() << kFATAL <<
"Targets or variables masked by transformation. Apparently (a) value(s) is/are missing in this event." <<
Endl;
309 for( std::vector<Float_t>::iterator itInput = input.begin(), itInputEnd = input.end(); itInput != itInputEnd; ++itInput ) {
312 if (numDist>1)listsForBinning[numDist-1][ivar].push_back(
TMVA::TMVAGaussPair(value,eventWeight));
317 for (
UInt_t icl=0; icl<numDist-1; icl++){
318 minWeight[numDist-1] =
TMath::Min(minWeight[icl],minWeight[numDist-1]);
319 maxWeight[numDist-1] =
TMath::Max(maxWeight[icl],maxWeight[numDist-1]);
325 const UInt_t nbinsmax=2000;
327 for (
UInt_t icl=0; icl< numDist; icl++){
328 for (
UInt_t ivar=0; ivar<inputSize; ivar++) {
329 listsForBinning[icl][ivar].sort();
330 std::list< TMVA::TMVAGaussPair >::iterator it;
331 Float_t sumPerBin = sumOfWeights[icl]/nbinsmax;
332 sumPerBin=
TMath::Max(minWeight[icl]*nevmin,sumPerBin);
334 Float_t ev_value=listsForBinning[icl][ivar].begin()->GetValue();
337 vsForBinning[icl][ivar].push_back(ev_value-eps);
338 vsForBinning[icl][ivar].push_back(ev_value);
340 for (it=listsForBinning[icl][ivar].begin(); it != listsForBinning[icl][ivar].end(); it++){
341 sum+= it->GetWeight();
342 if (sum >= sumPerBin) {
343 ev_value=it->GetValue();
344 if (ev_value>lastev_value) {
345 vsForBinning[icl][ivar].push_back(ev_value);
347 lastev_value=ev_value;
351 if (sum!=0) vsForBinning[icl][ivar].push_back(listsForBinning[icl][ivar].back().GetValue());
352 nbins[icl][ivar] = vsForBinning[icl][ivar].size();
356 delete[] sumOfWeights;
362 for (
UInt_t icls = 0; icls < numDist; icls++) {
363 for (
UInt_t ivar=0; ivar < inputSize; ivar++){
366 for (
UInt_t k =0 ; k < nbins[icls][ivar]; k++){
367 binnings[k] = vsForBinning[icls][ivar][k];
374 Form(
"Cumulative_Var%d_cls%d",ivar,icls),
375 nbins[icls][ivar] -1,
383 for (
UInt_t i=0; i<numDist; i++) {
384 delete [] listsForBinning[numDist-i-1];
385 delete [] vsForBinning[numDist-i-1];
386 delete [] nbins[numDist-i-1];
388 delete [] listsForBinning;
389 delete [] vsForBinning;
393 std::vector<Int_t> ic(numDist);
394 for (
UInt_t ievt=0; ievt<nevt; ievt++) {
396 const Event* ev= events[ievt];
403 for( std::vector<Float_t>::iterator itInput = input.begin(), itInputEnd = input.end(); itInput != itInputEnd; ++itInput ) {
406 if (numDist>1)
fCumulativeDist[ivar][numDist-1]->Fill(value,eventWeight);
418 for (
UInt_t ivar=0; ivar<inputSize; ivar++) {
420 for (
UInt_t icls=0; icls<numDist; icls++) {
426 if (val>0)
total += val;
430 if (val>0) sum += val;
443 Log() << kFATAL <<
"VariableGaussTransform::WriteTransformationToStream is obsolete" <<
Endl;
450 if (opt ==
"ALL" || opt ==
"PDF"){
458 if (opt ==
"ALL" || opt ==
"Dist"){
478 for (
UInt_t ivar=0; ivar<nvar; ivar++) {
485 Log() << kFATAL <<
"Cumulative histograms for variable " << ivar <<
" don't exist, can't write it to weight file" <<
Endl;
509 void* inpnode =
NULL;
515 void* varnode =
NULL;
527 TString varname, histname, classname;
530 if(
gTools().HasAttr(varnode,
"Name") )
559 istr.getline(buf,512);
563 while (!(buf[0]==
'#'&& buf[1]==
'#')) {
565 while (*p==
' ' || *p==
'\t') p++;
566 if (*p==
'#' || *p==
'\0') {
567 istr.getline(buf,512);
570 std::stringstream sstr(buf);
573 if (strvar==
"CumulativeHistogram") {
575 TString devnullS(
""),hname(
"");
584 for (
Int_t ibin=0; ibin<nbins+1; ibin++) {
593 if ( histToRead !=0 )
delete histToRead;
595 histToRead =
new TH1F( hname, hname, nbins, Binnings );
614 if (strvar==
"Uniform") {
616 istr.getline(buf,512);
620 istr.getline(buf,512);
624 UInt_t classIdx=(classname==
"signal")?0:1;
672 cumulant = y0 + (y1-y0)*(x-x0)/(x1-x0);
675 if (x <= h->GetBinLowEdge(1)){
690 Log() << kINFO <<
"I do not know yet how to print this... look in the weight file " << cls <<
":" <<
Endl;
704 for (
UInt_t icls=0; icls<numDist; icls++) {
705 for (
UInt_t ivar=0; ivar<nvar; ivar++) {
707 if (nbin > nBins) nBins=nbin;
714 fout <<
" int nvar;" << std::endl;
717 fout <<
" double cumulativeDist["<<nvar<<
"]["<<numDist<<
"]["<<nBins+1<<
"];"<<std::endl;
718 fout <<
" double X["<<nvar<<
"]["<<numDist<<
"]["<<nBins+1<<
"];"<<std::endl;
719 fout <<
" double xMin["<<nvar<<
"]["<<numDist<<
"];"<<std::endl;
720 fout <<
" double xMax["<<nvar<<
"]["<<numDist<<
"];"<<std::endl;
721 fout <<
" int nbins["<<nvar<<
"]["<<numDist<<
"];"<<std::endl;
725 fout <<
"#include \"math.h\"" << std::endl;
727 fout <<
"//_______________________________________________________________________" << std::endl;
728 fout <<
"inline void " << fcncName <<
"::InitTransform_"<<trCounter<<
"()" << std::endl;
729 fout <<
"{" << std::endl;
730 fout <<
" // Gauss/Uniform transformation, initialisation" << std::endl;
731 fout <<
" nvar=" << nvar <<
";" << std::endl;
732 for (
UInt_t icls=0; icls<numDist; icls++) {
733 for (
UInt_t ivar=0; ivar<nvar; ivar++) {
735 fout <<
" nbins["<<ivar<<
"]["<<icls<<
"]="<<nbin<<
";"<<std::endl;
742 for (
UInt_t icls=0; icls<numDist; icls++) {
743 for (
UInt_t ivar=0; ivar<nvar; ivar++) {
749 Log() << kWARNING <<
"MakeClass for the Gauss transformation works only for the transformation of variables. The transformation of targets/spectators is not implemented." <<
Endl;
751 }
catch( std::out_of_range
except ){
752 Log() << kWARNING <<
"MakeClass for the Gauss transformation searched for a non existing variable index (" << ivar <<
")" <<
Endl;
769 fout <<
"}" << std::endl;
771 fout <<
"//_______________________________________________________________________" << std::endl;
772 fout <<
"inline void " << fcncName <<
"::Transform_"<<trCounter<<
"( std::vector<double>& iv, int clsIn) const" << std::endl;
773 fout <<
"{" << std::endl;
774 fout <<
" // Gauss/Uniform transformation" << std::endl;
775 fout <<
" int cls=clsIn;" << std::endl;
776 fout <<
" if (cls < 0 || cls > "<<
GetNClasses()<<
") {"<< std::endl;
778 fout <<
" else cls = "<<(
fCumulativePDF[0].size()==1?0:2)<<
";"<< std::endl;
779 fout <<
" }"<< std::endl;
781 fout <<
" // copy the variables which are going to be transformed "<< std::endl;
783 fout <<
" static std::vector<double> dv; "<< std::endl;
784 fout <<
" dv.resize(nvar); "<< std::endl;
785 fout <<
" for (int ivar=0; ivar<nvar; ivar++) dv[ivar] = iv[indicesGet.at(ivar)]; "<< std::endl;
786 fout <<
" "<< std::endl;
787 fout <<
" bool FlatNotGauss = "<< (
fFlatNotGauss?
"true":
"false") <<
"; "<< std::endl;
788 fout <<
" double cumulant; "<< std::endl;
789 fout <<
" //const int nvar = "<<nvar<<
"; "<< std::endl;
790 fout <<
" for (int ivar=0; ivar<nvar; ivar++) { "<< std::endl;
791 fout <<
" int nbin = nbins[ivar][cls]; "<< std::endl;
792 fout <<
" int ibin=0; "<< std::endl;
793 fout <<
" while (dv[ivar] > X[ivar][cls][ibin]) ibin++; "<< std::endl;
794 fout <<
" "<< std::endl;
795 fout <<
" if (ibin<0) { ibin=0;} "<< std::endl;
796 fout <<
" if (ibin>=nbin) { ibin=nbin-1;} "<< std::endl;
797 fout <<
" int nextbin = ibin; "<< std::endl;
798 fout <<
" if ((dv[ivar] > X[ivar][cls][ibin] && ibin !=nbin-1) || ibin==0) "<< std::endl;
799 fout <<
" nextbin++; "<< std::endl;
800 fout <<
" else "<< std::endl;
801 fout <<
" nextbin--; "<< std::endl;
802 fout <<
" "<< std::endl;
803 fout <<
" double dx = X[ivar][cls][ibin]- X[ivar][cls][nextbin]; "<< std::endl;
804 fout <<
" double dy = cumulativeDist[ivar][cls][ibin] - cumulativeDist[ivar][cls][nextbin]; "<< std::endl;
805 fout <<
" cumulant = cumulativeDist[ivar][cls][ibin] + (dv[ivar] - X[ivar][cls][ibin])* dy/dx;"<< std::endl;
806 fout <<
" "<< std::endl;
807 fout <<
" "<< std::endl;
808 fout <<
" if (cumulant>1.-10e-10) cumulant = 1.-10e-10; "<< std::endl;
809 fout <<
" if (cumulant<10e-10) cumulant = 10e-10; "<< std::endl;
810 fout <<
" if (FlatNotGauss) dv[ivar] = cumulant; "<< std::endl;
811 fout <<
" else { "<< std::endl;
812 fout <<
" double maxErfInvArgRange = 0.99999999; "<< std::endl;
813 fout <<
" double arg = 2.0*cumulant - 1.0; "<< std::endl;
814 fout <<
" if (arg > maxErfInvArgRange) arg= maxErfInvArgRange; "<< std::endl;
815 fout <<
" if (arg < -maxErfInvArgRange) arg=-maxErfInvArgRange; "<< std::endl;
816 fout <<
" double inverf=0., stp=1. ; "<< std::endl;
817 fout <<
" while (stp >1.e-10){; "<< std::endl;
818 fout <<
" if (erf(inverf)>arg) inverf -=stp ; "<< std::endl;
819 fout <<
" else if (erf(inverf)<=arg && erf(inverf+stp)>=arg) stp=stp/5. ; "<< std::endl;
820 fout <<
" else inverf += stp; "<< std::endl;
821 fout <<
" } ; "<< std::endl;
822 fout <<
" //dv[ivar] = 1.414213562*TMath::ErfInverse(arg); "<< std::endl;
823 fout <<
" dv[ivar] = 1.414213562* inverf; "<< std::endl;
824 fout <<
" } "<< std::endl;
825 fout <<
" } "<< std::endl;
826 fout <<
" // copy the transformed variables back "<< std::endl;
827 fout <<
" for (int ivar=0; ivar<nvar; ivar++) iv[indicesPut.at(ivar)] = dv[ivar]; "<< std::endl;
828 fout <<
"} "<< std::endl;
virtual Int_t FindBin(Double_t x, Double_t y=0, Double_t z=0)
Return Global bin number corresponding to x,y,z.
Double_t ErfInverse(Double_t x)
returns the inverse error function x must be <-1<x<1
static long int sum(long int i)
MsgLogger & Endl(MsgLogger &ml)
void ReadXML(void *pdfnode)
XML file reading.
Singleton class for Global types used by TMVA.
virtual void SetDirectory(TDirectory *dir)
By default when an histogram is created, it is added to the list of histogram objects in the current ...
THist< 1, float, THistStatContent, THistStatUncertainty > TH1F
virtual Double_t GetBinContent(Int_t bin) const
Return content of bin number bin.
static Bool_t AddDirectoryStatus()
Static function: cannot be inlined on Windows/NT.
tomato 1-D histogram with a float per channel (see TH1 documentation)}
Short_t Min(Short_t a, Short_t b)
virtual Double_t GetBinLowEdge(Int_t bin) const
Return bin lower edge for 1D histogram.
static void AddDirectory(Bool_t add=kTRUE)
Sets the flag controlling the automatic add of histograms in memory.
Class that contains all the data information.
PDF wrapper for histograms; uses user-defined spline interpolation.
Double_t GetWeight() const
return the event weight - depending on whether the flag IgnoreNegWeightsInTraining is or not...
const char * GetName() const
Returns name of object.
Double_t Erf(Double_t x)
Computation of the error function erf(x).
virtual void SetBinContent(Int_t bin, Double_t content)
Set bin content see convention for numbering bins in TH1::GetBin In case the bin number is greater th...
char * Form(const char *fmt,...)
void Print(std::ostream &o) const
print method
UInt_t GetNVariables() const
accessor to the number of variables
static unsigned int total
#define TMVA_VERSION(a, b, c)
static const double x1[5]
static RooMathCoreReg dummy
static std::shared_ptr< std::function< double(double)> > Gauss
you should not use this method at all Int_t Int_t Double_t Double_t Double_t e
Abstract ClassifierFactory template that handles arbitrary types.
Short_t Max(Short_t a, Short_t b)
TString()
TString default ctor.
virtual Int_t GetNbinsX() const