96 Log() << kINFO <<
"Preparing the Gaussian transformation..." <<
Endl;
101 if (inputSize > 200) {
102 Log() << kWARNING <<
"----------------------------------------------------------------------------"
105 <<
": More than 200 variables, I hope you have enough memory!!!!" <<
Endl;
106 Log() << kWARNING <<
"----------------------------------------------------------------------------"
123 if (!
IsCreated())
Log() << kFATAL <<
"Transformation not yet created" <<
Endl;
135 std::vector<Float_t> input(0);
136 std::vector<Float_t> output(0);
138 std::vector<Char_t> mask;
141 std::vector<Char_t>::iterator itMask = mask.begin();
147 for (
UInt_t ivar=0; ivar<inputSize; ivar++) {
164 output.push_back( cumulant );
167 Double_t maxErfInvArgRange = 0.99999999;
192 if (!
IsCreated())
Log() << kFATAL <<
"Transformation not yet created" <<
Endl;
204 std::vector<Float_t> input(0);
205 std::vector<Float_t> output(0);
207 std::vector<Char_t> mask;
210 std::vector<Char_t>::iterator itMask = mask.begin();
216 for (
UInt_t ivar=0; ivar<inputSize; ivar++) {
224 invCumulant = input.at(ivar);
228 invCumulant = (
TMath::Erf(invCumulant/1.414213562)+1)/2.f;
234 Log() << kFATAL <<
"Inverse Uniform/Gauss transformation not implemented for TMVA versions before 4.1.0" <<
Endl;
236 output.push_back(invCumulant);
256 UInt_t nevt = events.size();
259 UInt_t numDist = nClasses+1;
265 std::list< TMVA::TMVAGaussPair > **listsForBinning =
new std::list<TMVA::TMVAGaussPair>* [numDist];
266 std::vector< Float_t > **vsForBinning =
new std::vector<Float_t>* [numDist];
267 for (
UInt_t i=0; i < numDist; i++) {
268 listsForBinning[i] =
new std::list<TMVA::TMVAGaussPair> [inputSize];
269 vsForBinning[i] =
new std::vector<Float_t> [inputSize];
270 nbins[i] =
new UInt_t[inputSize];
273 std::vector<Float_t> input;
274 std::vector<Char_t> mask;
280 for (
UInt_t i=0; i<numDist; i++) {
285 for (
UInt_t ievt=0; ievt < nevt; ievt++) {
286 const Event* ev= events[ievt];
289 sumOfWeights[cls] += eventWeight;
290 if (minWeight[cls] > eventWeight) minWeight[cls]=eventWeight;
291 if (maxWeight[cls] < eventWeight) maxWeight[cls]=eventWeight;
292 if (numDist>1) sumOfWeights[numDist-1] += eventWeight;
295 if( hasMaskedEntries ){
296 Log() << kWARNING <<
"Incomplete event" <<
Endl;
297 std::ostringstream oss;
300 Log() << kFATAL <<
"Targets or variables masked by transformation. Apparently (a) value(s) is/are missing in this event." <<
Endl;
305 for( std::vector<Float_t>::iterator itInput = input.begin(), itInputEnd = input.end(); itInput != itInputEnd; ++itInput ) {
308 if (numDist>1)listsForBinning[numDist-1][ivar].push_back(
TMVA::TMVAGaussPair(value,eventWeight));
313 for (
UInt_t icl=0; icl<numDist-1; icl++){
314 minWeight[numDist-1] =
TMath::Min(minWeight[icl],minWeight[numDist-1]);
315 maxWeight[numDist-1] =
TMath::Max(maxWeight[icl],maxWeight[numDist-1]);
321 const UInt_t nbinsmax=2000;
323 for (
UInt_t icl=0; icl< numDist; icl++){
324 for (
UInt_t ivar=0; ivar<inputSize; ivar++) {
325 listsForBinning[icl][ivar].sort();
326 std::list< TMVA::TMVAGaussPair >::iterator it;
327 Float_t sumPerBin = sumOfWeights[icl]/nbinsmax;
328 sumPerBin=
TMath::Max(minWeight[icl]*nevmin,sumPerBin);
330 Float_t ev_value=listsForBinning[icl][ivar].begin()->GetValue();
333 vsForBinning[icl][ivar].push_back(ev_value-eps);
334 vsForBinning[icl][ivar].push_back(ev_value);
336 for (it=listsForBinning[icl][ivar].begin(); it != listsForBinning[icl][ivar].end(); ++it){
337 sum+= it->GetWeight();
338 if (
sum >= sumPerBin) {
339 ev_value=it->GetValue();
340 if (ev_value>lastev_value) {
341 vsForBinning[icl][ivar].push_back(ev_value);
343 lastev_value=ev_value;
347 if (
sum!=0) vsForBinning[icl][ivar].push_back(listsForBinning[icl][ivar].back().GetValue());
348 nbins[icl][ivar] = vsForBinning[icl][ivar].size();
352 delete[] sumOfWeights;
358 for (
UInt_t icls = 0; icls < numDist; icls++) {
359 for (
UInt_t ivar=0; ivar < inputSize; ivar++){
362 for (
UInt_t k =0 ; k < nbins[icls][ivar]; k++){
363 binnings[k] = vsForBinning[icls][ivar][k];
371 nbins[icls][ivar] -1,
379 for (
UInt_t i=0; i<numDist; i++) {
380 delete [] listsForBinning[numDist-i-1];
381 delete [] vsForBinning[numDist-i-1];
382 delete [] nbins[numDist-i-1];
384 delete [] listsForBinning;
385 delete [] vsForBinning;
389 std::vector<Int_t> ic(numDist);
390 for (
UInt_t ievt=0; ievt<nevt; ievt++) {
392 const Event* ev= events[ievt];
399 for( std::vector<Float_t>::iterator itInput = input.begin(), itInputEnd = input.end(); itInput != itInputEnd; ++itInput ) {
402 if (numDist>1)
fCumulativeDist[ivar][numDist-1]->Fill(value,eventWeight);
414 for (
UInt_t ivar=0; ivar<inputSize; ivar++) {
416 for (
UInt_t icls=0; icls<numDist; icls++) {
422 if (val>0)
total += val;
426 if (val>0)
sum += val;
439 Log() << kFATAL <<
"VariableGaussTransform::WriteTransformationToStream is obsolete" <<
Endl;
446 if (opt ==
"ALL" || opt ==
"PDF"){
454 if (opt ==
"ALL" || opt ==
"Dist"){
474 for (
UInt_t ivar=0; ivar<nvar; ivar++) {
481 Log() << kFATAL <<
"Cumulative histograms for variable " << ivar <<
" don't exist, can't write it to weight file" <<
Endl;
505 void* inpnode = NULL;
511 void* varnode = NULL;
523 TString varname, histname, classname;
526 if(
gTools().HasAttr(varnode,
"Name") )
554 istr.getline(buf,512);
558 while (!(buf[0]==
'#'&& buf[1]==
'#')) {
560 while (*p==
' ' || *p==
'\t') p++;
561 if (*p==
'#' || *p==
'\0') {
562 istr.getline(buf,512);
565 std::stringstream sstr(buf);
568 if (strvar==
"CumulativeHistogram") {
570 TString devnullS(
""),hname(
"");
579 for (
Int_t ibin=0; ibin<nbins+1; ibin++) {
588 if ( histToRead !=0 )
delete histToRead;
590 histToRead =
new TH1F( hname, hname, nbins, Binnings );
595 for (
Int_t ibin=0; ibin<nbins; ibin++) {
609 if (strvar==
"Uniform") {
611 istr.getline(buf,512);
615 istr.getline(buf,512);
618 UInt_t classIdx=(classname==
"signal")?0:1;
643 x1 =
h->GetBinLowEdge(
TMath::Min(bin,
h->GetNbinsX())+1);
646 y1 =
h->GetBinContent(
TMath::Min(bin,
h->GetNbinsX()+1));
655 if (bin >
h->GetNbinsX()) {
659 if (bin ==
h->GetNbinsX()) {
666 cumulant = y0 + (y1-y0)*(
x-x0)/(x1-x0);
672 if (
x >=
h->GetBinLowEdge(
h->GetNbinsX()+1)){
684 Log() << kINFO <<
"I do not know yet how to print this... look in the weight file " << cls <<
":" <<
Endl;
698 for (
UInt_t icls=0; icls<numDist; icls++) {
699 for (
UInt_t ivar=0; ivar<nvar; ivar++) {
701 if (nbin > nBins) nBins=nbin;
708 fout <<
" int nvar;" << std::endl;
711 fout <<
" double cumulativeDist["<<nvar<<
"]["<<numDist<<
"]["<<nBins+1<<
"];"<<std::endl;
712 fout <<
" double X["<<nvar<<
"]["<<numDist<<
"]["<<nBins+1<<
"];"<<std::endl;
713 fout <<
" double xMin["<<nvar<<
"]["<<numDist<<
"];"<<std::endl;
714 fout <<
" double xMax["<<nvar<<
"]["<<numDist<<
"];"<<std::endl;
715 fout <<
" int nbins["<<nvar<<
"]["<<numDist<<
"];"<<std::endl;
719 fout <<
"#include \"math.h\"" << std::endl;
721 fout <<
"//_______________________________________________________________________" << std::endl;
722 fout <<
"inline void " << fcncName <<
"::InitTransform_"<<trCounter<<
"()" << std::endl;
723 fout <<
"{" << std::endl;
724 fout <<
" // Gauss/Uniform transformation, initialisation" << std::endl;
725 fout <<
" nvar=" << nvar <<
";" << std::endl;
726 for (
UInt_t icls=0; icls<numDist; icls++) {
727 for (
UInt_t ivar=0; ivar<nvar; ivar++) {
729 fout <<
" nbins["<<ivar<<
"]["<<icls<<
"]="<<nbin<<
";"<<std::endl;
736 for (
UInt_t icls=0; icls<numDist; icls++) {
737 for (
UInt_t ivar=0; ivar<nvar; ivar++) {
743 Log() << kWARNING <<
"MakeClass for the Gauss transformation works only for the transformation of variables. The transformation of targets/spectators is not implemented." <<
Endl;
745 }
catch( std::out_of_range &){
746 Log() << kWARNING <<
"MakeClass for the Gauss transformation searched for a non existing variable index (" << ivar <<
")" <<
Endl;
763 fout <<
"}" << std::endl;
765 fout <<
"//_______________________________________________________________________" << std::endl;
766 fout <<
"inline void " << fcncName <<
"::Transform_"<<trCounter<<
"( std::vector<double>& iv, int clsIn) const" << std::endl;
767 fout <<
"{" << std::endl;
768 fout <<
" // Gauss/Uniform transformation" << std::endl;
769 fout <<
" int cls=clsIn;" << std::endl;
770 fout <<
" if (cls < 0 || cls > "<<
GetNClasses()<<
") {"<< std::endl;
772 fout <<
" else cls = "<<(
fCumulativePDF[0].size()==1?0:2)<<
";"<< std::endl;
773 fout <<
" }"<< std::endl;
775 fout <<
" // copy the variables which are going to be transformed "<< std::endl;
777 fout <<
" static std::vector<double> dv; "<< std::endl;
778 fout <<
" dv.resize(nvar); "<< std::endl;
779 fout <<
" for (int ivar=0; ivar<nvar; ivar++) dv[ivar] = iv[indicesGet.at(ivar)]; "<< std::endl;
780 fout <<
" "<< std::endl;
781 fout <<
" bool FlatNotGauss = "<< (
fFlatNotGauss?
"true":
"false") <<
"; "<< std::endl;
782 fout <<
" double cumulant; "<< std::endl;
783 fout <<
" //const int nvar = "<<nvar<<
"; "<< std::endl;
784 fout <<
" for (int ivar=0; ivar<nvar; ivar++) { "<< std::endl;
785 fout <<
" int nbin = nbins[ivar][cls]; "<< std::endl;
786 fout <<
" int ibin=0; "<< std::endl;
787 fout <<
" while (dv[ivar] > X[ivar][cls][ibin]) ibin++; "<< std::endl;
788 fout <<
" "<< std::endl;
789 fout <<
" if (ibin<0) { ibin=0;} "<< std::endl;
790 fout <<
" if (ibin>=nbin) { ibin=nbin-1;} "<< std::endl;
791 fout <<
" int nextbin = ibin; "<< std::endl;
792 fout <<
" if ((dv[ivar] > X[ivar][cls][ibin] && ibin !=nbin-1) || ibin==0) "<< std::endl;
793 fout <<
" nextbin++; "<< std::endl;
794 fout <<
" else "<< std::endl;
795 fout <<
" nextbin--; "<< std::endl;
796 fout <<
" "<< std::endl;
797 fout <<
" double dx = X[ivar][cls][ibin]- X[ivar][cls][nextbin]; "<< std::endl;
798 fout <<
" double dy = cumulativeDist[ivar][cls][ibin] - cumulativeDist[ivar][cls][nextbin]; "<< std::endl;
799 fout <<
" cumulant = cumulativeDist[ivar][cls][ibin] + (dv[ivar] - X[ivar][cls][ibin])* dy/dx;"<< std::endl;
800 fout <<
" "<< std::endl;
801 fout <<
" "<< std::endl;
802 fout <<
" if (cumulant>1.-10e-10) cumulant = 1.-10e-10; "<< std::endl;
803 fout <<
" if (cumulant<10e-10) cumulant = 10e-10; "<< std::endl;
804 fout <<
" if (FlatNotGauss) dv[ivar] = cumulant; "<< std::endl;
805 fout <<
" else { "<< std::endl;
806 fout <<
" double maxErfInvArgRange = 0.99999999; "<< std::endl;
807 fout <<
" double arg = 2.0*cumulant - 1.0; "<< std::endl;
808 fout <<
" if (arg > maxErfInvArgRange) arg= maxErfInvArgRange; "<< std::endl;
809 fout <<
" if (arg < -maxErfInvArgRange) arg=-maxErfInvArgRange; "<< std::endl;
810 fout <<
" double inverf=0., stp=1. ; "<< std::endl;
811 fout <<
" while (stp >1.e-10){; "<< std::endl;
812 fout <<
" if (erf(inverf)>arg) inverf -=stp ; "<< std::endl;
813 fout <<
" else if (erf(inverf)<=arg && erf(inverf+stp)>=arg) stp=stp/5. ; "<< std::endl;
814 fout <<
" else inverf += stp; "<< std::endl;
815 fout <<
" } ; "<< std::endl;
816 fout <<
" //dv[ivar] = 1.414213562*TMath::ErfInverse(arg); "<< std::endl;
817 fout <<
" dv[ivar] = 1.414213562* inverf; "<< std::endl;
818 fout <<
" } "<< std::endl;
819 fout <<
" } "<< std::endl;
820 fout <<
" // copy the transformed variables back "<< std::endl;
821 fout <<
" for (int ivar=0; ivar<nvar; ivar++) iv[indicesPut.at(ivar)] = dv[ivar]; "<< std::endl;
822 fout <<
"} "<< std::endl;
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
int Int_t
Signed integer 4 bytes (int).
char Char_t
Character 1 byte (char).
unsigned int UInt_t
Unsigned integer 4 bytes (unsigned int).
bool Bool_t
Boolean (0=false, 1=true) (bool).
double Double_t
Double 8 bytes.
float Float_t
Float 4 bytes (float).
static unsigned int total
#define TMVA_VERSION(a, b, c)
TDirectory::TContext keeps track and restore the current directory.
1-D histogram with a float per channel (see TH1 documentation)
TH1 is the base class of all histogram classes in ROOT.
virtual void SetDirectory(TDirectory *dir)
By default, when a histogram is created, it is added to the list of histogram objects in the current ...
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...
Class that contains all the data information.
UInt_t GetNVariables() const
accessor to the number of variables
Double_t GetWeight() const
return the event weight - depending on whether the flag IgnoreNegWeightsInTraining is or not.
void Print(std::ostream &o) const
print method
PDF wrapper for histograms; uses user-defined spline interpolation.
const char * GetName() const override
Returns name of object.
void ReadXML(void *pdfnode)
XML file reading.
Singleton class for Global types used by TMVA.
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
MsgLogger & Endl(MsgLogger &ml)
Short_t Max(Short_t a, Short_t b)
Returns the largest of a and b.
Double_t Erf(Double_t x)
Computation of the error function erf(x).
Double_t ErfInverse(Double_t x)
Returns the inverse error function.
Short_t Min(Short_t a, Short_t b)
Returns the smallest of a and b.
static uint64_t sum(uint64_t i)