73 if (strcor==
"Uniform") {fFlatNotGauss =
kTRUE;
101 Log() <<
kINFO <<
"Preparing the Gaussian transformation..." <<
Endl;
106 if (inputSize > 200) {
107 Log() <<
kWARNING <<
"----------------------------------------------------------------------------" 110 <<
": More than 200 variables, I hope you have enough memory!!!!" <<
Endl;
111 Log() <<
kWARNING <<
"----------------------------------------------------------------------------" 140 std::vector<Float_t> input(0);
141 std::vector<Float_t>
output(0);
143 std::vector<Char_t> mask;
146 std::vector<Char_t>::iterator itMask = mask.begin();
152 for (
UInt_t ivar=0; ivar<inputSize; ivar++) {
169 output.push_back( cumulant );
172 Double_t maxErfInvArgRange = 0.99999999;
209 std::vector<Float_t> input(0);
210 std::vector<Float_t>
output(0);
212 std::vector<Char_t> mask;
215 std::vector<Char_t>::iterator itMask = mask.begin();
221 for (
UInt_t ivar=0; ivar<inputSize; ivar++) {
229 invCumulant = input.at(ivar);
233 invCumulant = (
TMath::Erf(invCumulant/1.414213562)+1)/2.
f;
239 Log() <<
kFATAL <<
"Inverse Uniform/Gauss transformation not implemented for TMVA versions before 4.1.0" <<
Endl;
241 output.push_back(invCumulant);
261 UInt_t nevt = events.size();
264 UInt_t numDist = nClasses+1;
270 std::list< TMVA::TMVAGaussPair > **listsForBinning =
new std::list<TMVA::TMVAGaussPair>* [numDist];
271 std::vector< Float_t > **vsForBinning =
new std::vector<Float_t>* [numDist];
272 for (
UInt_t i=0; i < numDist; i++) {
273 listsForBinning[i] =
new std::list<TMVA::TMVAGaussPair> [inputSize];
274 vsForBinning[i] =
new std::vector<Float_t> [inputSize];
275 nbins[i] =
new UInt_t[inputSize];
278 std::vector<Float_t> input;
279 std::vector<Char_t> mask;
285 for (
UInt_t i=0; i<numDist; i++) {
290 for (
UInt_t ievt=0; ievt < nevt; ievt++) {
291 const Event* ev= events[ievt];
294 sumOfWeights[cls] += eventWeight;
295 if (minWeight[cls] > eventWeight) minWeight[cls]=eventWeight;
296 if (maxWeight[cls] < eventWeight) maxWeight[cls]=eventWeight;
297 if (numDist>1) sumOfWeights[numDist-1] += eventWeight;
300 if( hasMaskedEntries ){
302 std::ostringstream oss;
305 Log() <<
kFATAL <<
"Targets or variables masked by transformation. Apparently (a) value(s) is/are missing in this event." <<
Endl;
310 for( std::vector<Float_t>::iterator itInput = input.begin(), itInputEnd = input.end(); itInput != itInputEnd; ++itInput ) {
313 if (numDist>1)listsForBinning[numDist-1][ivar].push_back(
TMVA::TMVAGaussPair(value,eventWeight));
318 for (
UInt_t icl=0; icl<numDist-1; icl++){
319 minWeight[numDist-1] =
TMath::Min(minWeight[icl],minWeight[numDist-1]);
320 maxWeight[numDist-1] =
TMath::Max(maxWeight[icl],maxWeight[numDist-1]);
326 const UInt_t nbinsmax=2000;
328 for (
UInt_t icl=0; icl< numDist; icl++){
329 for (
UInt_t ivar=0; ivar<inputSize; ivar++) {
330 listsForBinning[icl][ivar].sort();
331 std::list< TMVA::TMVAGaussPair >::iterator it;
332 Float_t sumPerBin = sumOfWeights[icl]/nbinsmax;
333 sumPerBin=
TMath::Max(minWeight[icl]*nevmin,sumPerBin);
335 Float_t ev_value=listsForBinning[icl][ivar].begin()->GetValue();
338 vsForBinning[icl][ivar].push_back(ev_value-eps);
339 vsForBinning[icl][ivar].push_back(ev_value);
341 for (it=listsForBinning[icl][ivar].begin(); it != listsForBinning[icl][ivar].end(); it++){
342 sum+= it->GetWeight();
343 if (sum >= sumPerBin) {
344 ev_value=it->GetValue();
345 if (ev_value>lastev_value) {
346 vsForBinning[icl][ivar].push_back(ev_value);
348 lastev_value=ev_value;
352 if (sum!=0) vsForBinning[icl][ivar].push_back(listsForBinning[icl][ivar].back().GetValue());
353 nbins[icl][ivar] = vsForBinning[icl][ivar].size();
357 delete[] sumOfWeights;
363 for (
UInt_t icls = 0; icls < numDist; icls++) {
364 for (
UInt_t ivar=0; ivar < inputSize; ivar++){
367 for (
UInt_t k =0 ; k < nbins[icls][ivar]; k++){
368 binnings[k] = vsForBinning[icls][ivar][k];
375 Form(
"Cumulative_Var%d_cls%d",ivar,icls),
376 nbins[icls][ivar] -1,
384 for (
UInt_t i=0; i<numDist; i++) {
385 delete [] listsForBinning[numDist-i-1];
386 delete [] vsForBinning[numDist-i-1];
387 delete [] nbins[numDist-i-1];
389 delete [] listsForBinning;
390 delete [] vsForBinning;
394 std::vector<Int_t> ic(numDist);
395 for (
UInt_t ievt=0; ievt<nevt; ievt++) {
397 const Event* ev= events[ievt];
404 for( std::vector<Float_t>::iterator itInput = input.begin(), itInputEnd = input.end(); itInput != itInputEnd; ++itInput ) {
407 if (numDist>1)
fCumulativeDist[ivar][numDist-1]->Fill(value,eventWeight);
419 for (
UInt_t ivar=0; ivar<inputSize; ivar++) {
421 for (
UInt_t icls=0; icls<numDist; icls++) {
427 if (val>0)
total += val;
431 if (val>0) sum += val;
444 Log() <<
kFATAL <<
"VariableGaussTransform::WriteTransformationToStream is obsolete" <<
Endl;
451 if (opt ==
"ALL" || opt ==
"PDF"){
459 if (opt ==
"ALL" || opt ==
"Dist"){
479 for (
UInt_t ivar=0; ivar<nvar; ivar++) {
486 Log() <<
kFATAL <<
"Cumulative histograms for variable " << ivar <<
" don't exist, can't write it to weight file" <<
Endl;
510 void* inpnode =
NULL;
516 void* varnode =
NULL;
528 TString varname, histname, classname;
531 if(
gTools().HasAttr(varnode,
"Name") )
560 istr.getline(buf,512);
564 while (!(buf[0]==
'#'&& buf[1]==
'#')) {
566 while (*p==
' ' || *p==
'\t') p++;
567 if (*p==
'#' || *p==
'\0') {
568 istr.getline(buf,512);
571 std::stringstream sstr(buf);
574 if (strvar==
"CumulativeHistogram") {
576 TString devnullS(
""),hname(
"");
585 for (
Int_t ibin=0; ibin<nbins+1; ibin++) {
594 if ( histToRead !=0 )
delete histToRead;
596 histToRead =
new TH1F( hname, hname, nbins, Binnings );
615 if (strvar==
"Uniform") {
617 istr.getline(buf,512);
621 istr.getline(buf,512);
625 UInt_t classIdx=(classname==
"signal")?0:1;
671 cumulant = y0 + (y1-y0)*(x-x0)/(x1-x0);
674 if (x <= h->GetBinLowEdge(1)){
691 Log() <<
kINFO <<
"I do not know yet how to print this... look in the weight file " << cls <<
":" <<
Endl;
705 for (
UInt_t icls=0; icls<numDist; icls++) {
706 for (
UInt_t ivar=0; ivar<nvar; ivar++) {
708 if (nbin > nBins) nBins=nbin;
715 fout <<
" int nvar;" << std::endl;
718 fout <<
" double cumulativeDist["<<nvar<<
"]["<<numDist<<
"]["<<nBins+1<<
"];"<<std::endl;
719 fout <<
" double X["<<nvar<<
"]["<<numDist<<
"]["<<nBins+1<<
"];"<<std::endl;
720 fout <<
" double xMin["<<nvar<<
"]["<<numDist<<
"];"<<std::endl;
721 fout <<
" double xMax["<<nvar<<
"]["<<numDist<<
"];"<<std::endl;
722 fout <<
" int nbins["<<nvar<<
"]["<<numDist<<
"];"<<std::endl;
726 fout <<
"#include \"math.h\"" << std::endl;
728 fout <<
"//_______________________________________________________________________" << std::endl;
729 fout <<
"inline void " << fcncName <<
"::InitTransform_"<<trCounter<<
"()" << std::endl;
730 fout <<
"{" << std::endl;
731 fout <<
" // Gauss/Uniform transformation, initialisation" << std::endl;
732 fout <<
" nvar=" << nvar <<
";" << std::endl;
733 for (
UInt_t icls=0; icls<numDist; icls++) {
734 for (
UInt_t ivar=0; ivar<nvar; ivar++) {
736 fout <<
" nbins["<<ivar<<
"]["<<icls<<
"]="<<nbin<<
";"<<std::endl;
743 for (
UInt_t icls=0; icls<numDist; icls++) {
744 for (
UInt_t ivar=0; ivar<nvar; ivar++) {
750 Log() <<
kWARNING <<
"MakeClass for the Gauss transformation works only for the transformation of variables. The transformation of targets/spectators is not implemented." <<
Endl;
752 }
catch( std::out_of_range
except ){
753 Log() <<
kWARNING <<
"MakeClass for the Gauss transformation searched for a non existing variable index (" << ivar <<
")" <<
Endl;
770 fout <<
"}" << std::endl;
772 fout <<
"//_______________________________________________________________________" << std::endl;
773 fout <<
"inline void " << fcncName <<
"::Transform_"<<trCounter<<
"( std::vector<double>& iv, int clsIn) const" << std::endl;
774 fout <<
"{" << std::endl;
775 fout <<
" // Gauss/Uniform transformation" << std::endl;
776 fout <<
" int cls=clsIn;" << std::endl;
777 fout <<
" if (cls < 0 || cls > "<<
GetNClasses()<<
") {"<< std::endl;
779 fout <<
" else cls = "<<(
fCumulativePDF[0].size()==1?0:2)<<
";"<< std::endl;
780 fout <<
" }"<< std::endl;
782 fout <<
" // copy the variables which are going to be transformed "<< std::endl;
784 fout <<
" static std::vector<double> dv; "<< std::endl;
785 fout <<
" dv.resize(nvar); "<< std::endl;
786 fout <<
" for (int ivar=0; ivar<nvar; ivar++) dv[ivar] = iv[indicesGet.at(ivar)]; "<< std::endl;
787 fout <<
" "<< std::endl;
788 fout <<
" bool FlatNotGauss = "<< (
fFlatNotGauss?
"true":
"false") <<
"; "<< std::endl;
789 fout <<
" double cumulant; "<< std::endl;
790 fout <<
" //const int nvar = "<<nvar<<
"; "<< std::endl;
791 fout <<
" for (int ivar=0; ivar<nvar; ivar++) { "<< std::endl;
792 fout <<
" int nbin = nbins[ivar][cls]; "<< std::endl;
793 fout <<
" int ibin=0; "<< std::endl;
794 fout <<
" while (dv[ivar] > X[ivar][cls][ibin]) ibin++; "<< std::endl;
795 fout <<
" "<< std::endl;
796 fout <<
" if (ibin<0) { ibin=0;} "<< std::endl;
797 fout <<
" if (ibin>=nbin) { ibin=nbin-1;} "<< std::endl;
798 fout <<
" int nextbin = ibin; "<< std::endl;
799 fout <<
" if ((dv[ivar] > X[ivar][cls][ibin] && ibin !=nbin-1) || ibin==0) "<< std::endl;
800 fout <<
" nextbin++; "<< std::endl;
801 fout <<
" else "<< std::endl;
802 fout <<
" nextbin--; "<< std::endl;
803 fout <<
" "<< std::endl;
804 fout <<
" double dx = X[ivar][cls][ibin]- X[ivar][cls][nextbin]; "<< std::endl;
805 fout <<
" double dy = cumulativeDist[ivar][cls][ibin] - cumulativeDist[ivar][cls][nextbin]; "<< std::endl;
806 fout <<
" cumulant = cumulativeDist[ivar][cls][ibin] + (dv[ivar] - X[ivar][cls][ibin])* dy/dx;"<< std::endl;
807 fout <<
" "<< std::endl;
808 fout <<
" "<< std::endl;
809 fout <<
" if (cumulant>1.-10e-10) cumulant = 1.-10e-10; "<< std::endl;
810 fout <<
" if (cumulant<10e-10) cumulant = 10e-10; "<< std::endl;
811 fout <<
" if (FlatNotGauss) dv[ivar] = cumulant; "<< std::endl;
812 fout <<
" else { "<< std::endl;
813 fout <<
" double maxErfInvArgRange = 0.99999999; "<< std::endl;
814 fout <<
" double arg = 2.0*cumulant - 1.0; "<< std::endl;
815 fout <<
" if (arg > maxErfInvArgRange) arg= maxErfInvArgRange; "<< std::endl;
816 fout <<
" if (arg < -maxErfInvArgRange) arg=-maxErfInvArgRange; "<< std::endl;
817 fout <<
" double inverf=0., stp=1. ; "<< std::endl;
818 fout <<
" while (stp >1.e-10){; "<< std::endl;
819 fout <<
" if (erf(inverf)>arg) inverf -=stp ; "<< std::endl;
820 fout <<
" else if (erf(inverf)<=arg && erf(inverf+stp)>=arg) stp=stp/5. ; "<< std::endl;
821 fout <<
" else inverf += stp; "<< std::endl;
822 fout <<
" } ; "<< std::endl;
823 fout <<
" //dv[ivar] = 1.414213562*TMath::ErfInverse(arg); "<< std::endl;
824 fout <<
" dv[ivar] = 1.414213562* inverf; "<< std::endl;
825 fout <<
" } "<< std::endl;
826 fout <<
" } "<< std::endl;
827 fout <<
" // copy the transformed variables back "<< std::endl;
828 fout <<
" for (int ivar=0; ivar<nvar; ivar++) iv[indicesPut.at(ivar)] = dv[ivar]; "<< std::endl;
829 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.
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.
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