79 CleanUpCumulativeArrays();
95 if (!IsEnabled() || IsCreated())
return kTRUE;
97 Log() << kINFO <<
"Preparing the Gaussian transformation..." <<
Endl;
99 UInt_t inputSize = fGet.size();
100 SetNVariables(inputSize);
102 if (inputSize > 200) {
103 Log() << kWARNING <<
"----------------------------------------------------------------------------"
106 <<
": More than 200 variables, I hope you have enough memory!!!!" <<
Endl;
107 Log() << kWARNING <<
"----------------------------------------------------------------------------"
112 GetCumulativeDist( events );
124 if (!IsCreated()) Log() << kFATAL <<
"Transformation not yet created" <<
Endl;
130 if (cls <0 || cls >= (
int) fCumulativePDF[0].
size()) cls = fCumulativePDF[0].size()-1;
134 UInt_t inputSize = fGet.size();
136 std::vector<Float_t>
input(0);
137 std::vector<Float_t>
output(0);
139 std::vector<Char_t>
mask;
142 std::vector<Char_t>::iterator itMask =
mask.begin();
148 for (
UInt_t ivar=0; ivar<inputSize; ivar++) {
155 if (0 != fCumulativePDF[ivar][cls]) {
158 cumulant = (fCumulativePDF[ivar][cls])->GetVal(
input.at(ivar));
160 cumulant = OldCumulant(
input.at(ivar), fCumulativePDF[ivar][cls]->GetOriginalHist() );
165 output.push_back( cumulant );
168 Double_t maxErfInvArgRange = 0.99999999;
178 if (fTransformedEvent==0 || fTransformedEvent->GetNVariables()!=ev->
GetNVariables()) {
179 if (fTransformedEvent!=0) {
delete fTransformedEvent; fTransformedEvent = 0; }
180 fTransformedEvent =
new Event();
183 SetOutput( fTransformedEvent,
output,
mask, ev );
185 return fTransformedEvent;
193 if (!IsCreated()) Log() << kFATAL <<
"Transformation not yet created" <<
Endl;
199 if (cls <0 || cls >= (
int) fCumulativePDF[0].
size()) cls = fCumulativePDF[0].size()-1;
203 UInt_t inputSize = fGet.size();
205 std::vector<Float_t>
input(0);
206 std::vector<Float_t>
output(0);
208 std::vector<Char_t>
mask;
211 std::vector<Char_t>::iterator itMask =
mask.begin();
217 for (
UInt_t ivar=0; ivar<inputSize; ivar++) {
224 if (0 != fCumulativePDF[ivar][cls]) {
225 invCumulant =
input.at(ivar);
229 invCumulant = (
TMath::Erf(invCumulant/1.414213562)+1)/2.f;
233 invCumulant = (fCumulativePDF[ivar][cls])->GetValInverse(invCumulant,
kTRUE);
235 Log() << kFATAL <<
"Inverse Uniform/Gauss transformation not implemented for TMVA versions before 4.1.0" <<
Endl;
237 output.push_back(invCumulant);
241 if (fBackTransformedEvent==0) fBackTransformedEvent =
new Event( *ev );
245 return fBackTransformedEvent;
253 const UInt_t inputSize = fGet.size();
257 UInt_t nevt = events.size();
259 const UInt_t nClasses = GetNClasses();
260 UInt_t numDist = nClasses+1;
262 if (GetNClasses() == 1 ) numDist = nClasses;
266 std::list< TMVA::TMVAGaussPair > **listsForBinning =
new std::list<TMVA::TMVAGaussPair>* [numDist];
267 std::vector< Float_t > **vsForBinning =
new std::vector<Float_t>* [numDist];
268 for (
UInt_t i=0; i < numDist; i++) {
269 listsForBinning[i] =
new std::list<TMVA::TMVAGaussPair> [inputSize];
270 vsForBinning[i] =
new std::vector<Float_t> [inputSize];
271 nbins[i] =
new UInt_t[inputSize];
274 std::vector<Float_t>
input;
275 std::vector<Char_t>
mask;
281 for (
UInt_t i=0; i<numDist; i++) {
286 for (
UInt_t ievt=0; ievt < nevt; ievt++) {
287 const Event* ev= events[ievt];
290 sumOfWeights[cls] += eventWeight;
291 if (minWeight[cls] > eventWeight) minWeight[cls]=eventWeight;
292 if (maxWeight[cls] < eventWeight) maxWeight[cls]=eventWeight;
293 if (numDist>1) sumOfWeights[numDist-1] += eventWeight;
296 if( hasMaskedEntries ){
297 Log() << kWARNING <<
"Incomplete event" <<
Endl;
298 std::ostringstream oss;
301 Log() << kFATAL <<
"Targets or variables masked by transformation. Apparently (a) value(s) is/are missing in this event." <<
Endl;
306 for( std::vector<Float_t>::iterator itInput =
input.begin(), itInputEnd =
input.end(); itInput != itInputEnd; ++itInput ) {
314 for (
UInt_t icl=0; icl<numDist-1; icl++){
315 minWeight[numDist-1] =
TMath::Min(minWeight[icl],minWeight[numDist-1]);
316 maxWeight[numDist-1] =
TMath::Max(maxWeight[icl],maxWeight[numDist-1]);
322 const UInt_t nbinsmax=2000;
324 for (
UInt_t icl=0; icl< numDist; icl++){
325 for (
UInt_t ivar=0; ivar<inputSize; ivar++) {
326 listsForBinning[icl][ivar].sort();
327 std::list< TMVA::TMVAGaussPair >::iterator it;
328 Float_t sumPerBin = sumOfWeights[icl]/nbinsmax;
329 sumPerBin=
TMath::Max(minWeight[icl]*nevmin,sumPerBin);
331 Float_t ev_value=listsForBinning[icl][ivar].begin()->GetValue();
334 vsForBinning[icl][ivar].push_back(ev_value-eps);
335 vsForBinning[icl][ivar].push_back(ev_value);
337 for (it=listsForBinning[icl][ivar].begin(); it != listsForBinning[icl][ivar].end(); ++it){
338 sum+= it->GetWeight();
339 if (
sum >= sumPerBin) {
340 ev_value=it->GetValue();
341 if (ev_value>lastev_value) {
342 vsForBinning[icl][ivar].push_back(ev_value);
344 lastev_value=ev_value;
348 if (
sum!=0) vsForBinning[icl][ivar].push_back(listsForBinning[icl][ivar].back().GetValue());
349 nbins[icl][ivar] = vsForBinning[icl][ivar].size();
353 delete[] sumOfWeights;
358 fCumulativeDist.resize(inputSize);
359 for (
UInt_t icls = 0; icls < numDist; icls++) {
360 for (
UInt_t ivar=0; ivar < inputSize; ivar++){
363 for (
UInt_t k =0 ; k < nbins[icls][ivar]; k++){
364 binnings[k] = vsForBinning[icls][ivar][k];
366 fCumulativeDist[ivar].resize(numDist);
367 if (fCumulativeDist[ivar][icls] ) {
368 delete fCumulativeDist[ivar][icls];
370 fCumulativeDist[ivar][icls] =
new TH1F(
TString::Format(
"Cumulative_Var%d_cls%d",ivar,icls),
372 nbins[icls][ivar] -1,
374 fCumulativeDist[ivar][icls]->SetDirectory(
nullptr);
380 for (
UInt_t i=0; i<numDist; i++) {
381 delete [] listsForBinning[numDist-i-1];
382 delete [] vsForBinning[numDist-i-1];
383 delete [] nbins[numDist-i-1];
385 delete [] listsForBinning;
386 delete [] vsForBinning;
390 std::vector<Int_t> ic(numDist);
391 for (
UInt_t ievt=0; ievt<nevt; ievt++) {
393 const Event* ev= events[ievt];
400 for( std::vector<Float_t>::iterator itInput =
input.begin(), itInputEnd =
input.end(); itInput != itInputEnd; ++itInput ) {
402 fCumulativeDist[ivar][cls]->Fill(
value,eventWeight);
403 if (numDist>1) fCumulativeDist[ivar][numDist-1]->Fill(
value,eventWeight);
410 CleanUpCumulativeArrays(
"PDF");
414 fCumulativePDF.resize(inputSize);
415 for (
UInt_t ivar=0; ivar<inputSize; ivar++) {
417 for (
UInt_t icls=0; icls<numDist; icls++) {
418 (fCumulativeDist[ivar][icls])->Smooth();
421 for (
Int_t ibin=1, ibinEnd=fCumulativeDist[ivar][icls]->GetNbinsX(); ibin <=ibinEnd ; ibin++){
422 Float_t val = (fCumulativeDist[ivar][icls])->GetBinContent(ibin);
423 if (val>0)
total += val;
425 for (
Int_t ibin=1, ibinEnd=fCumulativeDist[ivar][icls]->GetNbinsX(); ibin <=ibinEnd ; ibin++){
426 Float_t val = (fCumulativeDist[ivar][icls])->GetBinContent(ibin);
427 if (val>0)
sum += val;
428 (fCumulativeDist[ivar][icls])->SetBinContent(ibin,
sum/
total);
440 Log() << kFATAL <<
"VariableGaussTransform::WriteTransformationToStream is obsolete" <<
Endl;
447 if (opt ==
"ALL" || opt ==
"PDF"){
448 for (
UInt_t ivar=0; ivar<fCumulativePDF.size(); ivar++) {
449 for (
UInt_t icls=0; icls<fCumulativePDF[ivar].size(); icls++) {
450 if (0 != fCumulativePDF[ivar][icls])
delete fCumulativePDF[ivar][icls];
453 fCumulativePDF.clear();
455 if (opt ==
"ALL" || opt ==
"Dist"){
456 for (
UInt_t ivar=0; ivar<fCumulativeDist.size(); ivar++) {
457 for (
UInt_t icls=0; icls<fCumulativeDist[ivar].size(); icls++) {
458 if (0 != fCumulativeDist[ivar][icls])
delete fCumulativeDist[ivar][icls];
461 fCumulativeDist.clear();
470 gTools().
AddAttr(trfxml,
"FlatOrGauss", (fFlatNotGauss?
"Flat":
"Gauss") );
474 UInt_t nvar = fGet.size();
475 for (
UInt_t ivar=0; ivar<nvar; ivar++) {
480 if ( fCumulativePDF[ivar][0]==0 ||
481 (fCumulativePDF[ivar].
size()>1 && fCumulativePDF[ivar][1]==0 ))
482 Log() << kFATAL <<
"Cumulative histograms for variable " << ivar <<
" don't exist, can't write it to weight file" <<
Endl;
484 for (
UInt_t icls=0; icls<fCumulativePDF[ivar].size(); icls++){
486 (fCumulativePDF[ivar][icls])->AddXMLTo(pdfxml);
496 CleanUpCumulativeArrays();
501 if (FlatOrGauss ==
"Flat") fFlatNotGauss =
kTRUE;
502 else fFlatNotGauss =
kFALSE;
506 void* inpnode = NULL;
512 void* varnode = NULL;
524 TString varname, histname, classname;
527 if(
gTools().HasAttr(varnode,
"Name") )
538 fCumulativePDF.resize( ivar+1 );
539 fCumulativePDF[ivar].push_back(pdfToRead);
556 istr.getline(buf,512);
560 while (!(buf[0]==
'#'&& buf[1]==
'#')) {
562 while (*
p==
' ' || *
p==
'\t')
p++;
563 if (*
p==
'#' || *
p==
'\0') {
564 istr.getline(buf,512);
567 std::stringstream sstr(buf);
570 if (strvar==
"CumulativeHistogram") {
572 TString devnullS(
""),hname(
"");
576 sstr >>
type >> ivar >> hname >> nbins >> fElementsperbin;
581 for (
Int_t ibin=0; ibin<nbins+1; ibin++) {
586 if(ivar>=fCumulativeDist.size()) fCumulativeDist.resize(ivar+1);
587 if(
type>=fCumulativeDist[ivar].
size()) fCumulativeDist[ivar].resize(
type+1);
589 TH1F * histToRead = fCumulativeDist[ivar][
type];
590 if ( histToRead !=0 )
delete histToRead;
592 histToRead =
new TH1F( hname, hname, nbins, Binnings );
594 fCumulativeDist[ivar][
type]=histToRead;
597 for (
Int_t ibin=0; ibin<nbins; ibin++) {
604 fCumulativePDF.resize(ivar+1);
605 fCumulativePDF[ivar].resize(
type+1);
606 fCumulativePDF[ivar][
type] = pdf;
611 if (strvar==
"Uniform") {
612 sstr >> fFlatNotGauss;
613 istr.getline(buf,512);
617 istr.getline(buf,512);
621 UInt_t classIdx=(classname==
"signal")?0:1;
622 for(
UInt_t ivar=0; ivar<fCumulativePDF.size(); ++ivar) {
623 PDF*
src = fCumulativePDF[ivar][classIdx];
658 if (bin >
h->GetNbinsX()) {
662 if (bin ==
h->GetNbinsX()) {
669 cumulant = y0 + (
y1-y0)*(
x-x0)/(
x1-x0);
672 if (x <= h->GetBinLowEdge(1)){
675 if (
x >=
h->GetBinLowEdge(
h->GetNbinsX()+1)){
687 Log() << kINFO <<
"I do not know yet how to print this... look in the weight file " << cls <<
":" <<
Endl;
698 const UInt_t nvar = fGet.size();
699 UInt_t numDist = GetNClasses() + 1;
701 for (
UInt_t icls=0; icls<numDist; icls++) {
702 for (
UInt_t ivar=0; ivar<nvar; ivar++) {
703 Int_t nbin=(fCumulativePDF[ivar][icls])->GetGraph()->GetN();
704 if (nbin > nBins) nBins=nbin;
711 fout <<
" int nvar;" << std::endl;
714 fout <<
" double cumulativeDist["<<nvar<<
"]["<<numDist<<
"]["<<nBins+1<<
"];"<<std::endl;
715 fout <<
" double X["<<nvar<<
"]["<<numDist<<
"]["<<nBins+1<<
"];"<<std::endl;
716 fout <<
" double xMin["<<nvar<<
"]["<<numDist<<
"];"<<std::endl;
717 fout <<
" double xMax["<<nvar<<
"]["<<numDist<<
"];"<<std::endl;
718 fout <<
" int nbins["<<nvar<<
"]["<<numDist<<
"];"<<std::endl;
722 fout <<
"#include \"math.h\"" << std::endl;
724 fout <<
"//_______________________________________________________________________" << std::endl;
725 fout <<
"inline void " << fcncName <<
"::InitTransform_"<<trCounter<<
"()" << std::endl;
726 fout <<
"{" << std::endl;
727 fout <<
" // Gauss/Uniform transformation, initialisation" << std::endl;
728 fout <<
" nvar=" << nvar <<
";" << std::endl;
729 for (
UInt_t icls=0; icls<numDist; icls++) {
730 for (
UInt_t ivar=0; ivar<nvar; ivar++) {
731 Int_t nbin=(fCumulativePDF[ivar][icls])->GetGraph()->GetN();
732 fout <<
" nbins["<<ivar<<
"]["<<icls<<
"]="<<nbin<<
";"<<std::endl;
739 for (
UInt_t icls=0; icls<numDist; icls++) {
740 for (
UInt_t ivar=0; ivar<nvar; ivar++) {
746 Log() << kWARNING <<
"MakeClass for the Gauss transformation works only for the transformation of variables. The transformation of targets/spectators is not implemented." <<
Endl;
748 }
catch( std::out_of_range &){
749 Log() << kWARNING <<
"MakeClass for the Gauss transformation searched for a non existing variable index (" << ivar <<
")" <<
Endl;
754 Double_t xmn = (fCumulativePDF[ivar][icls])->GetGraph()->GetX()[0];
755 Double_t xmx = (fCumulativePDF[ivar][icls])->GetGraph()->GetX()[(fCumulativePDF[ivar][icls])->GetGraph()->GetN()-1];
759 for (
Int_t ibin=0; ibin<(fCumulativePDF[ivar][icls])->GetGraph()->GetN(); ibin++) {
760 fout <<
" cumulativeDist[" << ivar <<
"]["<< icls<<
"]["<<ibin<<
"]="<<
gTools().
StringFromDouble((fCumulativePDF[ivar][icls])->GetGraph()->GetY()[ibin])<<
";"<<std::endl;
761 fout <<
" X[" << ivar <<
"]["<< icls<<
"]["<<ibin<<
"]="<<
gTools().
StringFromDouble((fCumulativePDF[ivar][icls])->GetGraph()->GetX()[ibin])<<
";"<<std::endl;
766 fout <<
"}" << std::endl;
768 fout <<
"//_______________________________________________________________________" << std::endl;
769 fout <<
"inline void " << fcncName <<
"::Transform_"<<trCounter<<
"( std::vector<double>& iv, int clsIn) const" << std::endl;
770 fout <<
"{" << std::endl;
771 fout <<
" // Gauss/Uniform transformation" << std::endl;
772 fout <<
" int cls=clsIn;" << std::endl;
773 fout <<
" if (cls < 0 || cls > "<<GetNClasses()<<
") {"<< std::endl;
774 fout <<
" if ("<<GetNClasses()<<
" > 1 ) cls = "<<GetNClasses()<<
";"<< std::endl;
775 fout <<
" else cls = "<<(fCumulativePDF[0].size()==1?0:2)<<
";"<< std::endl;
776 fout <<
" }"<< std::endl;
778 fout <<
" // copy the variables which are going to be transformed "<< std::endl;
780 fout <<
" static std::vector<double> dv; "<< std::endl;
781 fout <<
" dv.resize(nvar); "<< std::endl;
782 fout <<
" for (int ivar=0; ivar<nvar; ivar++) dv[ivar] = iv[indicesGet.at(ivar)]; "<< std::endl;
783 fout <<
" "<< std::endl;
784 fout <<
" bool FlatNotGauss = "<< (fFlatNotGauss?
"true":
"false") <<
"; "<< std::endl;
785 fout <<
" double cumulant; "<< std::endl;
786 fout <<
" //const int nvar = "<<nvar<<
"; "<< std::endl;
787 fout <<
" for (int ivar=0; ivar<nvar; ivar++) { "<< std::endl;
788 fout <<
" int nbin = nbins[ivar][cls]; "<< std::endl;
789 fout <<
" int ibin=0; "<< std::endl;
790 fout <<
" while (dv[ivar] > X[ivar][cls][ibin]) ibin++; "<< std::endl;
791 fout <<
" "<< std::endl;
792 fout <<
" if (ibin<0) { ibin=0;} "<< std::endl;
793 fout <<
" if (ibin>=nbin) { ibin=nbin-1;} "<< std::endl;
794 fout <<
" int nextbin = ibin; "<< std::endl;
795 fout <<
" if ((dv[ivar] > X[ivar][cls][ibin] && ibin !=nbin-1) || ibin==0) "<< std::endl;
796 fout <<
" nextbin++; "<< std::endl;
797 fout <<
" else "<< std::endl;
798 fout <<
" nextbin--; "<< std::endl;
799 fout <<
" "<< std::endl;
800 fout <<
" double dx = X[ivar][cls][ibin]- X[ivar][cls][nextbin]; "<< std::endl;
801 fout <<
" double dy = cumulativeDist[ivar][cls][ibin] - cumulativeDist[ivar][cls][nextbin]; "<< std::endl;
802 fout <<
" cumulant = cumulativeDist[ivar][cls][ibin] + (dv[ivar] - X[ivar][cls][ibin])* dy/dx;"<< std::endl;
803 fout <<
" "<< std::endl;
804 fout <<
" "<< std::endl;
805 fout <<
" if (cumulant>1.-10e-10) cumulant = 1.-10e-10; "<< std::endl;
806 fout <<
" if (cumulant<10e-10) cumulant = 10e-10; "<< std::endl;
807 fout <<
" if (FlatNotGauss) dv[ivar] = cumulant; "<< std::endl;
808 fout <<
" else { "<< std::endl;
809 fout <<
" double maxErfInvArgRange = 0.99999999; "<< std::endl;
810 fout <<
" double arg = 2.0*cumulant - 1.0; "<< std::endl;
811 fout <<
" if (arg > maxErfInvArgRange) arg= maxErfInvArgRange; "<< std::endl;
812 fout <<
" if (arg < -maxErfInvArgRange) arg=-maxErfInvArgRange; "<< std::endl;
813 fout <<
" double inverf=0., stp=1. ; "<< std::endl;
814 fout <<
" while (stp >1.e-10){; "<< std::endl;
815 fout <<
" if (erf(inverf)>arg) inverf -=stp ; "<< std::endl;
816 fout <<
" else if (erf(inverf)<=arg && erf(inverf+stp)>=arg) stp=stp/5. ; "<< std::endl;
817 fout <<
" else inverf += stp; "<< std::endl;
818 fout <<
" } ; "<< std::endl;
819 fout <<
" //dv[ivar] = 1.414213562*TMath::ErfInverse(arg); "<< std::endl;
820 fout <<
" dv[ivar] = 1.414213562* inverf; "<< std::endl;
821 fout <<
" } "<< std::endl;
822 fout <<
" } "<< std::endl;
823 fout <<
" // copy the transformed variables back "<< std::endl;
824 fout <<
" for (int ivar=0; ivar<nvar; ivar++) iv[indicesPut.at(ivar)] = dv[ivar]; "<< std::endl;
825 fout <<
"} "<< std::endl;
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
static unsigned int total
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void input
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t mask
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
Option_t Option_t TPoint TPoint const char x1
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t src
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
Option_t Option_t TPoint TPoint const char y1
#define TMVA_VERSION(a, b, c)
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 ...
static void AddDirectory(Bool_t add=kTRUE)
Sets the flag controlling the automatic add of histograms in memory.
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...
static Bool_t AddDirectoryStatus()
Static function: cannot be inlined on Windows/NT.
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.
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)