diff --git a/tmva/doc/v528/index.html b/tmva/doc/v528/index.html index 3eef2d58ef841cffc233eaffa393dc51bf4c4e3b..2975f3a33a0c1ed73af10534803f936d346a36ca 100644 --- a/tmva/doc/v528/index.html +++ b/tmva/doc/v528/index.html @@ -2,3 +2,18 @@ <hr> <a name="tmva"></a> <h3>TMVA</h3> + + TMVA version 4.1.0 is included in this root release. + + <h4>Framework</h4> + + <ul> + <li> <b><em>New TMVA event vector building.</em></b> The code + for splitting the input data into training and test samples for + all classes and the mixing of those samples to one training and + one test sample has been rewritten completely. The new code is + more performant and has a clearer structure. This should fix + several bugs which have been reported by + + </ul> + diff --git a/tmva/inc/MethodBase.h b/tmva/inc/MethodBase.h index 10e92ccb377067fd49fe1fddf24bd2973b57c78f..f2dc5ee1fe0979a25f9dd59ef4fd0fa5ad15cff7 100644 --- a/tmva/inc/MethodBase.h +++ b/tmva/inc/MethodBase.h @@ -616,7 +616,6 @@ inline const TMVA::Event* TMVA::MethodBase::GetEvent( const TMVA::Event* ev ) co return GetTransformationHandler().Transform(ev); } -//_______________________________________________________________________ inline const TMVA::Event* TMVA::MethodBase::GetEvent() const { if(fTmpEvent) @@ -625,28 +624,24 @@ inline const TMVA::Event* TMVA::MethodBase::GetEvent() const return GetTransformationHandler().Transform(Data()->GetEvent()); } -//_______________________________________________________________________ inline const TMVA::Event* TMVA::MethodBase::GetEvent( Long64_t ievt ) const { assert(fTmpEvent==0); return GetTransformationHandler().Transform(Data()->GetEvent(ievt)); } -//_______________________________________________________________________ inline const TMVA::Event* TMVA::MethodBase::GetEvent( Long64_t ievt, Types::ETreeType type ) const { assert(fTmpEvent==0); return GetTransformationHandler().Transform(Data()->GetEvent(ievt, type)); } -//_______________________________________________________________________ inline const TMVA::Event* TMVA::MethodBase::GetTrainingEvent( Long64_t ievt ) const { assert(fTmpEvent==0); return GetEvent(ievt, Types::kTraining); } -//_______________________________________________________________________ inline const TMVA::Event* TMVA::MethodBase::GetTestingEvent( Long64_t ievt ) const { assert(fTmpEvent==0); diff --git a/tmva/inc/VariableNormalizeTransform.h b/tmva/inc/VariableNormalizeTransform.h index 49e06bc7b879ab6fb6a89e143056929d93cb2e1d..805c9f043b03c7355b1d1757e6577fb67a5621a2 100644 --- a/tmva/inc/VariableNormalizeTransform.h +++ b/tmva/inc/VariableNormalizeTransform.h @@ -49,10 +49,7 @@ namespace TMVA { class VariableNormalizeTransform : public VariableTransformBase { public: - - typedef std::vector<Float_t> FloatVector; - typedef std::vector< FloatVector > VectorOfFloatVectors; - + VariableNormalizeTransform( DataSetInfo& dsi ); virtual ~VariableNormalizeTransform( void ); @@ -83,8 +80,8 @@ namespace TMVA { // mutable Event* fTransformedEvent; - VectorOfFloatVectors fMin; //! Min of source range - VectorOfFloatVectors fMax; //! Max of source range + std::vector< std::vector<Float_t> > fMin; //! Min of source range + std::vector< std::vector<Float_t> > fMax; //! Max of source range ClassDef(VariableNormalizeTransform,0) // Variable transformation: normalization }; diff --git a/tmva/inc/VariablePCATransform.h b/tmva/inc/VariablePCATransform.h index 5ba2224c74d1907cc3759e43a4f67915dad7f700..93e5983beda3e4505dc79808d97149385521f614 100644 --- a/tmva/inc/VariablePCATransform.h +++ b/tmva/inc/VariablePCATransform.h @@ -71,8 +71,7 @@ namespace TMVA { private: void CalculatePrincipalComponents( const std::vector<Event*>& ); - void X2P( std::vector<Float_t>&, const std::vector<Float_t>&, Int_t cls ) const; - void P2X( std::vector<Float_t>&, const std::vector<Float_t>&, Int_t cls ) const; + std::vector<Float_t> X2P( const std::vector<Float_t>&, Int_t cls ) const; // mutable Event* fTransformedEvent; diff --git a/tmva/inc/VariableTransformBase.h b/tmva/inc/VariableTransformBase.h index f318bb6cc54d5db1726d1287cc722f5e771da3f2..2712064172819953f49509c8f6313d418a31b0d2 100644 --- a/tmva/inc/VariableTransformBase.h +++ b/tmva/inc/VariableTransformBase.h @@ -68,10 +68,6 @@ namespace TMVA { public: - typedef std::vector<std::pair<Char_t,UInt_t> > VectorOfCharAndInt; - typedef VectorOfCharAndInt::iterator ItVarTypeIdx; - typedef VectorOfCharAndInt::const_iterator ItVarTypeIdxConst; - VariableTransformBase( DataSetInfo& dsi, Types::EVariableTransform tf, const TString& trfName ); virtual ~VariableTransformBase( void ); @@ -87,12 +83,6 @@ namespace TMVA { Bool_t IsCreated() const { return fCreated; } Bool_t IsNormalised() const { return fNormalise; } - // variable selection - virtual void SelectInput( const TString& inputVariables ); - virtual void GetInput ( const Event* event, std::vector<Float_t>& input ) const; - virtual void SetOutput( Event* event, std::vector<Float_t>& output, const Event* oldEvent = 0 ) const; - virtual void CountVariableTypes( UInt_t& nvars, UInt_t& ntgts, UInt_t& nspcts ); - void SetUseSignalTransform( Bool_t e=kTRUE) { fUseSignalTransform = e; } Bool_t UseSignalTransform() const { return fUseSignalTransform; } @@ -102,14 +92,14 @@ namespace TMVA { virtual void WriteTransformationToStream ( std::ostream& o ) const = 0; virtual void ReadTransformationFromStream( std::istream& istr, const TString& classname="" ) = 0; - virtual void AttachXMLTo(void* parent); - virtual void ReadFromXML( void* trfnode ); + virtual void AttachXMLTo(void* parent) = 0; + virtual void ReadFromXML( void* trfnode ) = 0; Types::EVariableTransform GetVariableTransform() const { return fVariableTransform; } // writer of function code virtual void MakeFunction( std::ostream& fout, const TString& fncName, Int_t part, - UInt_t trCounter, Int_t cls ); + UInt_t trCounter, Int_t cls ) = 0; // provides string vector giving explicit transformation virtual std::vector<TString>* GetTransformationStrings( Int_t cls ) const; @@ -130,9 +120,8 @@ namespace TMVA { void SetNVariables( UInt_t i ) { fNVars = i; } void SetName( const TString& c ) { fTransformName = c; } - UInt_t GetNVariables() const { return fDsi.GetNVariables(); } - UInt_t GetNTargets() const { return fDsi.GetNTargets(); } - UInt_t GetNSpectators() const { return fDsi.GetNSpectators(); } + UInt_t GetNVariables() const { return fDsi.GetNVariables(); } + UInt_t GetNTargets() const { return fDsi.GetNTargets(); } DataSetInfo& fDsi; @@ -144,10 +133,6 @@ namespace TMVA { mutable Event* fTransformedEvent; // holds the current transformed event mutable Event* fBackTransformedEvent; // holds the current back-transformed event - // variable selection - VectorOfCharAndInt fGet; // get variables/targets/spectators - - private: Types::EVariableTransform fVariableTransform; // Decorrelation, PCA, etc. @@ -163,7 +148,6 @@ namespace TMVA { std::vector<TMVA::VariableInfo> fVariables; // event variables [saved to weight file] std::vector<TMVA::VariableInfo> fTargets; // event targets [saved to weight file --> TODO ] - protected: TMVAVersion_t fTMVAVersion; diff --git a/tmva/src/Configurable.cxx b/tmva/src/Configurable.cxx index e5b9b2b882f6a43a6f8a381492f658eca9951232..81b59e017ff629a4ee523aa7bc3f7701386f03d6 100644 --- a/tmva/src/Configurable.cxx +++ b/tmva/src/Configurable.cxx @@ -190,7 +190,7 @@ void TMVA::Configurable::ParseOptions() else { // since we don't know what else is comming we just put everthing into a map if (!decOpt->SetValue(optval, idx)) - Log() << kFATAL << "Index " << idx << " too large (" << optval << ") for option " << decOpt->TheName() + Log() << kFATAL << "Index " << idx << " too large for option " << decOpt->TheName() << ", allowed range is [0," << decOpt->GetArraySize()-1 << "]" << Endl; } } diff --git a/tmva/src/Event.cxx b/tmva/src/Event.cxx index 1f958db4534694f8ff051ae99875d774b92178e8..b096a997bbddf1de5dafef42a9e8e0b5b4f2e9f4 100644 --- a/tmva/src/Event.cxx +++ b/tmva/src/Event.cxx @@ -177,11 +177,6 @@ void TMVA::Event::CopyVarValues( const Event& other ) { // copies only the variable values fValues = other.fValues; - fTargets = other.fTargets; - fSpectators = other.fSpectators; - - fVariableArrangement = other.fVariableArrangement; - fClass = other.fClass; fWeight = other.fWeight; fBoostWeight = other.fBoostWeight; diff --git a/tmva/src/Factory.cxx b/tmva/src/Factory.cxx index 83970f55fc44765b858b98e0fe48436ac4db892d..22ab70ff322e0c842d3982c426e3036a8f58119f 100644 --- a/tmva/src/Factory.cxx +++ b/tmva/src/Factory.cxx @@ -105,7 +105,7 @@ TMVA::Factory::Factory( TString jobName, TFile* theTargetFile, TString theOption fgTargetFile = theTargetFile; -// DataSetManager::CreateInstance(*fDataInputHandler); // DSMTEST removed + // DataSetManager::CreateInstance(*fDataInputHandler); // DSMTEST removed fDataSetManager = new DataSetManager( *fDataInputHandler ); // DSMTEST @@ -709,7 +709,7 @@ TMVA::MethodBase* TMVA::Factory::BookMethod( TString theMethodName, TString meth MethodBoost* methBoost = dynamic_cast<MethodBoost*>(im); // DSMTEST divided into two lines methBoost->SetBoostedMethodName( theMethodName ); // DSMTEST divided into two lines if( !methBoost ) // DSMTEST - Log() << kERROR << "Method with type kBoost cannot be casted to MethodCategory. /Factory" << Endl; // DSMTEST + Log() << kERROR << "Method with type kBoost cannot be casted to MethodCategory. /Factory" << Endl; // DSMTEST methBoost->fDataSetManager = fDataSetManager; // DSMTEST } @@ -866,63 +866,26 @@ void TMVA::Factory::WriteDataInformation() } delete trClsList; - TString variables = "_V_"; - - VariableTransformBase* transformation = NULL; if (trName=='I') { - transformation = new VariableIdentityTransform ( DefaultDataSetInfo() ); + trfs.back()->AddTransformation( new VariableIdentityTransform ( DefaultDataSetInfo() ), idxCls ); identityTrHandler = trfs.back(); - } - else if (trName == "D" || trName == "Deco" || trName == "Decorrelate"){ - if( variables.Length() == 0 ) - variables = "_V_"; - transformation = new VariableDecorrTransform( DefaultDataSetInfo()); - } - else if (trName == "P" || trName == "PCA"){ - if( variables.Length() == 0 ) - variables = "_V_"; - transformation = new VariablePCATransform ( DefaultDataSetInfo()); - } - else if (trName == "G" || trName == "Gauss"){ - if( variables.Length() == 0 ) - variables = "_V_,_T_"; - transformation = new VariableGaussTransform ( DefaultDataSetInfo()); - } - else if (trName == "N" || trName == "Norm" || trName == "Normalise" || trName == "Normalize") - { - if( variables.Length() == 0 ) - variables = "_V_,_T_"; - transformation = new VariableNormalizeTransform( DefaultDataSetInfo()); - } - else - Log() << kFATAL << "<ProcessOptions> Variable transform '" - << trName << "' unknown." << Endl; - - if( transformation ){ - transformation->SelectInput( "_V_" ); - trfs.back()->AddTransformation(transformation, idxCls); - } - -// if (trName=='I') { -// trfs.back()->AddTransformation( new VariableIdentityTransform ( DefaultDataSetInfo() ), idxCls ); -// identityTrHandler = trfs.back(); -// } -// else if (trName=='D') { -// trfs.back()->AddTransformation( new VariableDecorrTransform ( DefaultDataSetInfo() ), idxCls ); -// } -// else if (trName=='P') { -// trfs.back()->AddTransformation( new VariablePCATransform ( DefaultDataSetInfo() ), idxCls ); -// } -// else if (trName=='G') { -// trfs.back()->AddTransformation( new VariableGaussTransform ( DefaultDataSetInfo() ), idxCls ); -// } -// else if (trName=='N') { -// trfs.back()->AddTransformation( new VariableNormalizeTransform( DefaultDataSetInfo() ), idxCls ); -// } -// else { -// Log() << kINFO << "The transformation " << *trfsDefIt << " definition is not valid, the \n" -// << "transformation " << trName << " is not known!" << Endl; -// } + } + else if (trName=='D') { + trfs.back()->AddTransformation( new VariableDecorrTransform ( DefaultDataSetInfo() ), idxCls ); + } + else if (trName=='P') { + trfs.back()->AddTransformation( new VariablePCATransform ( DefaultDataSetInfo() ), idxCls ); + } + else if (trName=='G') { + trfs.back()->AddTransformation( new VariableGaussTransform ( DefaultDataSetInfo() ), idxCls ); + } + else if (trName=='N') { + trfs.back()->AddTransformation( new VariableNormalizeTransform( DefaultDataSetInfo() ), idxCls ); + } + else { + Log() << kINFO << "The transformation " << *trfsDefIt << " definition is not valid, the \n" + << "transformation " << trName << " is not known!" << Endl; + } } } @@ -1015,7 +978,10 @@ void TMVA::Factory::TrainAllMethods() Log() << Endl; if (RECREATE_METHODS) { - Log() << kINFO << "=== Destroy and recreate all methods via weight files for testing ===" << Endl << Endl;; + Log() << kINFO << "=== Destroy and recreate all methods via weight files for testing ===" << Endl << Endl; + + RootBaseDir()->cd(); + // iterate through all booked methods for (UInt_t i=0; i<fMethods.size(); i++) { @@ -1035,7 +1001,13 @@ void TMVA::Factory::TrainAllMethods() m = dynamic_cast<MethodBase*>( ClassifierFactory::Instance() .Create( std::string(Types::Instance().GetMethodName(methodType)), dataSetInfo, weightfile ) ); - + if( m->GetMethodType() == Types::kCategory ){ + MethodCategory *methCat = (dynamic_cast<MethodCategory*>(m)); + if( !methCat ) Log() << kFATAL << "Method with type kCategory cannot be casted to MethodCategory. /Factory" << Endl; + else methCat->fDataSetManager = fDataSetManager; + } + //ToDo, Do we need to fill the DataSetManager of MethodBoost here too? + m->SetupMethod(); m->ReadStateFromFile(); m->SetTestvarName(testvarName); @@ -1043,7 +1015,6 @@ void TMVA::Factory::TrainAllMethods() // replace trained method by newly created one (from weight file) in methods vector fMethods[i] = m; } - } } @@ -1236,10 +1207,9 @@ void TMVA::Factory::EvaluateAllMethods( void ) theMethod->WriteEvaluationHistosToFile(Types::kTesting); theMethod->WriteEvaluationHistosToFile(Types::kTraining); } else if (theMethod->DoMulticlass()) { - doMulticlass = kTRUE; + doMulticlass = kTRUE; Log() << kINFO << "Evaluate multiclass classification method: " << theMethod->GetMethodName() << Endl; - - theMethod->TestMulticlass(); + theMethod->TestMulticlass(); } else { Log() << kINFO << "Evaluate classifier: " << theMethod->GetMethodName() << Endl; diff --git a/tmva/src/MethodANNBase.cxx b/tmva/src/MethodANNBase.cxx index 122b26dff9158e2a7dacdd8ebb1c8334be7cece6..6e9b395a2d868fce9ad7598ea1d6afb3572050b5 100644 --- a/tmva/src/MethodANNBase.cxx +++ b/tmva/src/MethodANNBase.cxx @@ -731,7 +731,7 @@ void TMVA::MethodANNBase::ReadWeightsFromXML( void* wghtnode ) void* xmlLayout = NULL; try{ xmlLayout = gTools().GetChild(wghtnode, "Layout"); - }catch( std::logic_error& excpt ){ + } catch( std::logic_error& ) { xmlLayout = wghtnode; } if( !xmlLayout ){ @@ -792,7 +792,7 @@ void TMVA::MethodANNBase::ReadWeightsFromXML( void* wghtnode ) void* xmlInvHessian = NULL; try{ xmlInvHessian = gTools().GetChild(wghtnode, "InverseHessian"); - }catch( std::logic_error& excpt ){ + } catch ( std::logic_error& ){ // no inverse hessian available return; // ------------------ return from subroutine } diff --git a/tmva/src/MethodBase.cxx b/tmva/src/MethodBase.cxx index e06276eeb75bb468a02ef9a5fa13649d03aaa649..bfc236b46ed587b05c214c56742a3fbb19bacdb9 100644 --- a/tmva/src/MethodBase.cxx +++ b/tmva/src/MethodBase.cxx @@ -448,86 +448,7 @@ void TMVA::MethodBase::ProcessBaseOptions() //_______________________________________________________________________ void TMVA::MethodBase::CreateVariableTransforms(const TString& trafoDefinition ) { - // create variable transformations - - if (trafoDefinition == "None") // no transformations - return; - - if( trafoDefinition.Contains("+") || trafoDefinition.Contains("(") ) { // new format - TList* trList = gTools().ParseFormatLine( trafoDefinition, "+" ); - TListIter trIt(trList); - while (TObjString* os = (TObjString*)trIt()) { - TString tdef = os->GetString(); - Int_t idxCls = -1; - - TString variables = "_V_"; - if( tdef.Contains("(") ) { // contains selection of variables - Ssiz_t parStart = tdef.Index( "(" ); - Ssiz_t parLen = tdef.Index( ")", parStart )-parStart+1; - - variables = tdef(parStart,parLen); - tdef.Remove(parStart,parLen); - variables.Remove(parLen-1,1); - variables.Remove(0,1); - } - - TList* trClsList = gTools().ParseFormatLine( tdef, "_" ); // split entry to get trf-name and class-name - TListIter trClsIt(trClsList); - const TString& trName = ((TObjString*)trClsList->At(0))->GetString(); - - if (trClsList->GetEntries() > 1) { - TString trCls = "AllClasses"; - ClassInfo *ci = NULL; - trCls = ((TObjString*)trClsList->At(1))->GetString(); - if (trCls != "AllClasses") { - ci = DataInfo().GetClassInfo( trCls ); - if (ci == NULL) - Log() << kFATAL << "Class " << trCls << " not known for variable transformation " - << trName << ", please check." << Endl; - else - idxCls = ci->GetNumber(); - } - } - - - VariableTransformBase* transformation = NULL; - if (trName == "D" || trName == "Deco" || trName == "Decorrelate"){ - if( variables.Length() == 0 ) - variables = "_V_"; - transformation = new VariableDecorrTransform( DataInfo()); - } - else if (trName == "P" || trName == "PCA"){ - if( variables.Length() == 0 ) - variables = "_V_"; - transformation = new VariablePCATransform ( DataInfo()); - } - else if (trName == "G" || trName == "Gauss"){ - if( variables.Length() == 0 ) - variables = "_V_,_T_"; - transformation = new VariableGaussTransform ( DataInfo()); - } - else if (trName == "N" || trName == "Norm" || trName == "Normalise" || trName == "Normalize") - { - if( variables.Length() == 0 ) - variables = "_V_,_T_"; - transformation = new VariableNormalizeTransform( DataInfo()); - } - else - Log() << kFATAL << "<ProcessOptions> Variable transform '" - << trName << "' unknown." << Endl; - - if( transformation ){ - transformation->SelectInput( variables ); - GetTransformationHandler().AddTransformation(transformation, idxCls); - } - } - - - - return; - } - - if (trafoDefinition != "None") { // old format + if (trafoDefinition != "None") { TList* trList = gTools().ParseFormatLine( trafoDefinition, "," ); TListIter trIt(trList); while (TObjString* os = (TObjString*)trIt()) { @@ -551,32 +472,23 @@ void TMVA::MethodBase::CreateVariableTransforms(const TString& trafoDefinition ) } } - VariableTransformBase* transformation = NULL; - TString variables = "_V_"; if (trName == "D" || trName == "Deco" || trName == "Decorrelate") - transformation = new VariableDecorrTransform( DataInfo()); + GetTransformationHandler().AddTransformation( new VariableDecorrTransform ( DataInfo()) , idxCls ); else if (trName == "P" || trName == "PCA") - transformation = new VariablePCATransform ( DataInfo()); + GetTransformationHandler().AddTransformation( new VariablePCATransform ( DataInfo()), idxCls ); else if (trName == "G" || trName == "Gauss") - transformation = new VariableGaussTransform ( DataInfo()); - else if (trName == "N" || trName == "Norm" || trName == "Normalise" || trName == "Normalize"){ - variables = "_V_,_T_"; - transformation = new VariableNormalizeTransform( DataInfo()); - } + GetTransformationHandler().AddTransformation( new VariableGaussTransform ( DataInfo()), idxCls ); + else if (trName == "N" || trName == "Norm" || trName == "Normalise" || trName == "Normalize") + GetTransformationHandler().AddTransformation( new VariableNormalizeTransform( DataInfo()), idxCls ); else Log() << kFATAL << "<ProcessOptions> Variable transform '" << trName << "' unknown." << Endl; - ClassInfo* clsInfo = DataInfo().GetClassInfo(idxCls); if( clsInfo ) Log() << kINFO << " create Transformation " << trName << " with reference class " <<clsInfo->GetName() << "=("<< idxCls <<")"<<Endl; else Log() << kINFO << " create Transformation " << trName << " with events of all classes." << Endl; - if( transformation ){ - transformation->SelectInput( variables ); - GetTransformationHandler().AddTransformation(transformation, idxCls); - } } } } @@ -1367,6 +1279,9 @@ void TMVA::MethodBase::ReadStateFromXML( void* methodNode ) Log().SetSource( GetName() ); Log() << kINFO << "Read method \"" << GetMethodName() << "\" of type \"" << GetMethodTypeName() << "\"" << Endl; + // after the method name is read, the testvar can be set + SetTestvarName(); + TString nodeName(""); void* ch = gTools().GetChild(methodNode); while (ch!=0) { diff --git a/tmva/src/MethodCuts.cxx b/tmva/src/MethodCuts.cxx index 814fec1930d07d594df0d81501e686a54751e059..03e871c18a583ff0e5e730d4e844898a22bda131 100644 --- a/tmva/src/MethodCuts.cxx +++ b/tmva/src/MethodCuts.cxx @@ -595,6 +595,7 @@ void TMVA::MethodCuts::Train( void ) // this is important: reset the branch addresses of the training tree to the current event fEffBvsSLocal = new TH1F( GetTestvarName() + "_effBvsSLocal", TString(GetName()) + " efficiency of B vs S", fNbins, 0.0, 1.0 ); + fEffBvsSLocal->SetDirectory(0); // it's local // init for (Int_t ibin=1; ibin<=fNbins; ibin++) fEffBvsSLocal->SetBinContent( ibin, -0.1 ); @@ -995,7 +996,7 @@ void TMVA::MethodCuts::GetEffsfromSelection( Double_t* cutMin, Double_t* cutMax, // for given cut sample Float_t nTotS = 0, nTotB = 0; Float_t nSelS = 0, nSelB = 0; - + Volume* volume = new Volume( cutMin, cutMax, GetNvar() ); // search for all events lying in the volume, and add up their weights @@ -1526,9 +1527,9 @@ Double_t TMVA::MethodCuts::GetEfficiency( const TString& theString, Types::ETree // treat signal and background one must decide which transformation type shall // be used: our default is signal-type fBinaryTreeS = new BinarySearchTree(); - fBinaryTreeS->Fill( Data()->GetEventCollection(Types::kTesting), fSignalClass ); + fBinaryTreeS->Fill( GetEventCollection(Types::kTesting), fSignalClass ); fBinaryTreeB = new BinarySearchTree(); - fBinaryTreeB->Fill( Data()->GetEventCollection(Types::kTesting), fBackgroundClass ); + fBinaryTreeB->Fill( GetEventCollection(Types::kTesting), fBackgroundClass ); // there is no really good equivalent to the fEffS; fEffB (efficiency vs cutvalue) // for the "Cuts" method (unless we had only one cut). Maybe later I might add here @@ -1582,7 +1583,6 @@ Double_t TMVA::MethodCuts::GetEfficiency( const TString& theString, Types::ETree // create splines for histogram fSpleffBvsS = new TSpline1( "effBvsS", tmpBvsS ); - //fSpleffBvsS = new TSpline1( "effBvsS", new TGraph( eff_BvsS ) ); for (Int_t bini=1; bini<=fNbins; bini++) { Double_t effS = (bini - 0.5)/Float_t(fNbins); Double_t effB = fSpleffBvsS->Eval( effS ); diff --git a/tmva/src/MethodFDA.cxx b/tmva/src/MethodFDA.cxx index 65e6e6d9cdcaa82fabd3329ee61c1ed931586c01..01a0188562f7ded6c76218504d07e4bf233ac475 100644 --- a/tmva/src/MethodFDA.cxx +++ b/tmva/src/MethodFDA.cxx @@ -249,6 +249,7 @@ void TMVA::MethodFDA::ProcessOptions() if (pmin > pmax) Log() << kFATAL << "<ProcessOptions> max > min in interval for parameter: [" << ipar << "] : [" << pmin << ", " << pmax << "] " << Endl; + Log() << kINFO << "Create parameter interval for parameter " << ipar << " : [" << pmin << "," << pmax << "]" << Endl; fParRange[ipar] = new Interval( pmin, pmax ); } delete parList; @@ -264,7 +265,7 @@ void TMVA::MethodFDA::ProcessOptions() if( DoMulticlass() ) fOutputDimensions = DataInfo().GetNClasses(); - for( Int_t dim = 0; dim < fOutputDimensions; ++dim ){ + for( Int_t dim = 1; dim < fOutputDimensions; ++dim ){ for( UInt_t par = 0; par < fNPars; ++par ){ fParRange.push_back( fParRange.at(par) ); } @@ -598,7 +599,7 @@ void TMVA::MethodFDA::ReadWeightsFromXML( void* wghtnode ) try { gTools().ReadAttr( wghtnode, "NDim" , fOutputDimensions ); - }catch( std::logic_error& excpt ){ + } catch ( std::logic_error& ){ // attribute could not be read, it probably does not exist because the weight file has been written with an older version fOutputDimensions = 1; } diff --git a/tmva/src/MethodMLP.cxx b/tmva/src/MethodMLP.cxx index d447def8765f2d6cac1be24743c80ecd760aecd5..513e839df3c625062f4e65d2837304c42a1b1bb1 100644 --- a/tmva/src/MethodMLP.cxx +++ b/tmva/src/MethodMLP.cxx @@ -179,7 +179,7 @@ void TMVA::MethodMLP::DeclareOptions() "Number of steps (without improvement) required for convergence (<0 means automatic convergence check is turned off)"); DeclareOptionRef(fUseRegulator=kTRUE, "UseRegulator", - "Use regulator to avoid over-training (bayesian neural network technique)"); //zjh + "Use regulator to avoid over-training"); //zjh DeclareOptionRef(fUpdateLimit=10, "UpdateLimit", "Number of updates for regulator before stop training"); //zjh } diff --git a/tmva/src/Reader.cxx b/tmva/src/Reader.cxx index 8949a4ec6b21eb37be0f6ce291424f87f0f681b2..afd2e4975d63e4fe12c5bc7c790b8b3f4c369b97 100644 --- a/tmva/src/Reader.cxx +++ b/tmva/src/Reader.cxx @@ -111,6 +111,7 @@ #include "TMVA/ClassifierFactory.h" #include "TMVA/IMethod.h" #include "TMVA/MethodCuts.h" +#include "TMVA/MethodCategory.h" #include "TMVA/DataSetManager.h" ClassImp(TMVA::Reader) @@ -351,6 +352,12 @@ TMVA::IMethod* TMVA::Reader::BookMVA( const TString& methodTag, const TString& w MethodBase* method = dynamic_cast<MethodBase*>(this->BookMVA( Types::Instance().GetMethodType(methodType), weightfile ) ); + if( method->GetMethodType() == Types::kCategory ){ + MethodCategory *methCat = (dynamic_cast<MethodCategory*>(method)); + if( !methCat ) + Log() << kERROR << "Method with type kCategory cannot be casted to MethodCategory. /Reader" << Endl; + methCat->fDataSetManager = fDataSetManager; + } return fMethodMap[methodTag] = method; } @@ -364,6 +371,13 @@ TMVA::IMethod* TMVA::Reader::BookMVA( TMVA::Types::EMVA methodType, const TStrin MethodBase *method = (dynamic_cast<MethodBase*>(im)); + if( method->GetMethodType() == Types::kCategory ){ + MethodCategory *methCat = (dynamic_cast<MethodCategory*>(method)); + if( !methCat ) + Log() << kERROR << "Method with type kCategory cannot be casted to MethodCategory. /Reader" << Endl; + methCat->fDataSetManager = fDataSetManager; + } + if (method==0) return im; method->SetupMethod(); @@ -394,6 +408,13 @@ TMVA::IMethod* TMVA::Reader::BookMVA( TMVA::Types::EMVA methodType, const char* MethodBase *method = (dynamic_cast<MethodBase*>(im)); + if( method->GetMethodType() == Types::kCategory ){ + MethodCategory *methCat = (dynamic_cast<MethodCategory*>(method)); + if( !methCat ) + Log() << kERROR << "Method with type kCategory cannot be casted to MethodCategory. /Reader" << Endl; + methCat->fDataSetManager = fDataSetManager; + } + method->SetupMethod(); // when reading older weight files, they could include options diff --git a/tmva/src/VariableNormalizeTransform.cxx b/tmva/src/VariableNormalizeTransform.cxx index fe4aa71303da37bf9013d5147f5ddf620fef12c0..168fc3d960159347ceb0fcad1050c12f0a237f84 100644 --- a/tmva/src/VariableNormalizeTransform.cxx +++ b/tmva/src/VariableNormalizeTransform.cxx @@ -13,7 +13,7 @@ * Authors (alphabetical): * * Andreas Hoecker <Andreas.Hocker@cern.ch> - CERN, Switzerland * * Joerg Stelzer <Joerg.Stelzer@cern.ch> - CERN, Switzerland * - * Peter Speckmayer <Peter.Speckmayer@cern.ch> - CERN, Switzerland * + * Peter Speckmayer <Peter:Speckmayer@cern.ch> - CERN, Switzerland * * Helge Voss <Helge.Voss@cern.ch> - MPI-K Heidelberg, Germany * * * * Copyright (c) 2005: * @@ -27,7 +27,6 @@ #include <iostream> #include <iomanip> -#include <stdexcept> #include "TVectorF.h" #include "TVectorD.h" @@ -65,18 +64,18 @@ void TMVA::VariableNormalizeTransform::Initialize() { // initialization of the normalization transformation - UInt_t inputSize = fGet.size(); - + UInt_t nvar = Variables().size(); + UInt_t ntgts = Targets().size(); Int_t numC = GetNClasses()+1; if (GetNClasses() <= 1 ) numC = 1; fMin.resize( numC ); fMax.resize( numC ); for (Int_t i=0; i<numC; i++) { - fMin.at(i).resize(inputSize); - fMax.at(i).resize(inputSize); - fMin.at(i).assign(inputSize, 0); - fMax.at(i).assign(inputSize, 0); + fMin.at(i).resize(nvar+ntgts); + fMax.at(i).resize(nvar+ntgts); + fMin.at(i).assign(nvar+ntgts, 0); + fMax.at(i).assign(nvar+ntgts, 0); } } @@ -101,7 +100,7 @@ Bool_t TMVA::VariableNormalizeTransform::PrepareTransformation( const std::vecto const TMVA::Event* TMVA::VariableNormalizeTransform::Transform( const TMVA::Event* const ev, Int_t cls ) const { - // apply the normalization transformation + // apply the decorrelation transformation if (!IsCreated()) Log() << kFATAL << "Transformation not yet created" << Endl; // if cls (the class chosen by the user) not existing, @@ -110,36 +109,43 @@ const TMVA::Event* TMVA::VariableNormalizeTransform::Transform( const TMVA::Even if (GetNClasses() > 1 ) cls = GetNClasses(); else cls = (fMin.size()==1?0:2); } - - FloatVector input; // will be filled with the selected variables, targets, (spectators) - FloatVector output; // will be filled with the selected variables, targets, (spectators) - GetInput( ev, input ); + const UInt_t nvars = GetNVariables(); + const UInt_t ntgts = ev->GetNTargets(); + if (nvars != ev->GetNVariables()) { + Log() << kFATAL << "Transformation defined for a different number of variables (defined for: " << GetNVariables() + << ", event contains: " << ev->GetNVariables() << ")" << Endl; + } + if (ntgts != ev->GetNTargets()) { + Log() << kFATAL << "Transformation defined for a different number of targets (defined for: " << GetNTargets() + << ", event contains: " << ev->GetNTargets() << ")" << Endl; + } if (fTransformedEvent==0) fTransformedEvent = new Event(); Float_t min,max; - - const FloatVector& minVector = fMin.at(cls); - const FloatVector& maxVector = fMax.at(cls); - - UInt_t iidx = 0; - for ( std::vector<Float_t>::iterator itInp = input.begin(), itInpEnd = input.end(); itInp != itInpEnd; ++itInp) { // loop over input variables - Float_t val = (*itInp); - - min = minVector.at(iidx); - max = maxVector.at(iidx); - + for (Int_t ivar=nvars-1; ivar>=0; ivar--) { + min = fMin.at(cls).at(ivar); + max = fMax.at(cls).at(ivar); Float_t offset = min; Float_t scale = 1.0/(max-min); - Float_t valnorm = (val-offset)*scale * 2 - 1; - output.push_back( valnorm ); - - ++iidx; + Float_t valnorm = (ev->GetValue(ivar)-offset)*scale * 2 - 1; + fTransformedEvent->SetVal(ivar,valnorm); } + for (Int_t itgt=ntgts-1; itgt>=0; itgt--) { + min = fMin.at(cls).at(nvars+itgt); + max = fMax.at(cls).at(nvars+itgt); + Float_t offset = min; + Float_t scale = 1.0/(max-min); - SetOutput( fTransformedEvent, output, ev ); - + Float_t original = ev->GetTarget(itgt); + Float_t valnorm = (original-offset)*scale * 2 - 1; + fTransformedEvent->SetTarget(itgt,valnorm); + } + + fTransformedEvent->SetWeight ( ev->GetWeight() ); + fTransformedEvent->SetBoostWeight( ev->GetBoostWeight() ); + fTransformedEvent->SetClass ( ev->GetClass() ); return fTransformedEvent; } @@ -150,41 +156,43 @@ const TMVA::Event* TMVA::VariableNormalizeTransform::InverseTransform( const TMV if (!IsCreated()) Log() << kFATAL << "Transformation not yet created" << Endl; // if cls (the class chosen by the user) not existing, - // assume that user wants to have the transformation for all classes together. + // assume that user wants to have the matrix for all classes together. if (cls < 0 || cls > GetNClasses()) { if (GetNClasses() > 1 ) cls = GetNClasses(); else cls = 0; } - FloatVector input; // will be filled with the selected variables, targets, (spectators) - FloatVector output; // will be filled with the output - GetInput( ev, input ); + const UInt_t nvars = GetNVariables(); + const UInt_t ntgts = GetNTargets(); + if (nvars != ev->GetNVariables()) { + Log() << kFATAL << "Transformation defined for a different number of variables " << GetNVariables() << " " << ev->GetNVariables() + << Endl; + } if (fBackTransformedEvent==0) fBackTransformedEvent = new Event( *ev ); Float_t min,max; + for (Int_t ivar=nvars-1; ivar>=0; ivar--) { + min = fMin.at(cls).at(ivar); + max = fMax.at(cls).at(ivar); + Float_t offset = min; + Float_t scale = 1.0/(max-min); - const FloatVector& minVector = fMin.at(cls); - const FloatVector& maxVector = fMax.at(cls); - - UInt_t iidx = 0; - for ( std::vector<Float_t>::iterator itInp = input.begin(), itInpEnd = input.end(); itInp != itInpEnd; ++itInp) { // loop over input variables - Float_t val = (*itInp); - - min = minVector.at(iidx); - max = maxVector.at(iidx); + Float_t valnorm = offset+((ev->GetValue(ivar)+1)/(scale * 2)); + fBackTransformedEvent->SetVal(ivar,valnorm); + } + for (Int_t itgt=ntgts-1; itgt>=0; itgt--) { + min = fMin.at(cls).at(nvars+itgt); + max = fMax.at(cls).at(nvars+itgt); Float_t offset = min; Float_t scale = 1.0/(max-min); - Float_t valnorm = offset+((val+1)/(scale * 2)); - output.push_back( valnorm ); - - ++iidx; + Float_t original = ev->GetTarget(itgt); + Float_t valnorm = offset+((original+1.0)/(scale * 2)); + fBackTransformedEvent->SetTarget(itgt,valnorm); } - SetOutput( fBackTransformedEvent, output, ev ); - return fBackTransformedEvent; } @@ -194,57 +202,49 @@ void TMVA::VariableNormalizeTransform::CalcNormalizationParams( const std::vecto // compute offset and scale from min and max if (events.size() <= 1) Log() << kFATAL << "Not enough events (found " << events.size() << ") to calculate the normalization" << Endl; + + UInt_t nvars = GetNVariables(); + UInt_t ntgts = GetNTargets(); - FloatVector input; // will be filled with the selected variables, targets, (spectators) - - UInt_t inputSize = fGet.size(); // number of input variables - - const UInt_t nCls = GetNClasses(); - Int_t numC = nCls+1; // prepare the min and max values for each of the classes and additionally for all classes (if more than one) - Int_t all = nCls; // at idx the min and max values for "all" classes are stored - if (nCls <= 1 ) { - numC = 1; - all = 0; - } + Int_t numC = GetNClasses()+1; + if (GetNClasses() <= 1 ) numC = 1; - for (UInt_t iinp=0; iinp<inputSize; ++iinp) { + for (UInt_t ivar=0; ivar<nvars+ntgts; ivar++) { for (Int_t ic = 0; ic < numC; ic++) { - fMin.at(ic).at(iinp) = FLT_MAX; - fMax.at(ic).at(iinp) = -FLT_MAX; + fMin.at(ic).at(ivar) = FLT_MAX; + fMax.at(ic).at(ivar) = -FLT_MAX; } } + const Int_t all = GetNClasses(); std::vector<Event*>::const_iterator evIt = events.begin(); - for (;evIt!=events.end();evIt++) { // loop over all events - TMVA::Event* event = (*evIt); // get the event + for (;evIt!=events.end();evIt++) { + for (UInt_t ivar=0; ivar<nvars; ivar++) { + Float_t val = (*evIt)->GetValue(ivar); + UInt_t cls = (*evIt)->GetClass(); - UInt_t cls = (*evIt)->GetClass(); // get the class of this event - - FloatVector& minVector = fMin.at(cls); - FloatVector& maxVector = fMax.at(cls); + if (fMin.at(cls).at(ivar) > val) fMin.at(cls).at(ivar) = val; + if (fMax.at(cls).at(ivar) < val) fMax.at(cls).at(ivar) = val; - FloatVector& minVectorAll = fMin.at(all); - FloatVector& maxVectorAll = fMax.at(all); - - GetInput(event,input); // select the input variables for the transformation and get them from the event - UInt_t iidx = 0; - for ( std::vector<Float_t>::iterator itInp = input.begin(), itInpEnd = input.end(); itInp != itInpEnd; ++itInp) { // loop over input variables - Float_t val = (*itInp); + if (GetNClasses() != 1) { + if (fMin.at(all).at(ivar) > val) fMin.at(all).at(ivar) = val; + if (fMax.at(all).at(ivar) < val) fMax.at(all).at(ivar) = val; + } + } + for (UInt_t itgt=0; itgt<ntgts; itgt++) { + Float_t val = (*evIt)->GetTarget(itgt); + UInt_t cls = (*evIt)->GetClass(); - if( minVector.at(iidx) > val ) minVector.at(iidx) = val; - if( maxVector.at(iidx) < val ) maxVector.at(iidx) = val; + if (fMin.at(cls).at(nvars+itgt) > val) fMin.at(cls).at(nvars+itgt) = val; + if (fMax.at(cls).at(nvars+itgt) < val) fMax.at(cls).at(nvars+itgt) = val; - if (nCls != 1) { // in case more than one class exists, compute min and max as well for all classes together - if (minVectorAll.at(iidx) > val) minVectorAll.at(iidx) = val; - if (maxVectorAll.at(iidx) < val) maxVectorAll.at(iidx) = val; + if (GetNClasses() != 1) { + if (fMin.at(all).at(nvars+itgt) > val) fMin.at(all).at(nvars+itgt) = val; + if (fMax.at(all).at(nvars+itgt) < val) fMax.at(all).at(nvars+itgt) = val; } - - ++iidx; } } - PrintTransformation( std::cout ); - return; } @@ -257,43 +257,21 @@ std::vector<TString>* TMVA::VariableNormalizeTransform::GetTransformationStrings // have the matrix for all classes together. if (cls < 0 || cls > GetNClasses()) cls = GetNClasses(); - Float_t min, max; - - const UInt_t size = fGet.size(); - std::vector<TString>* strVec = new std::vector<TString>(size); - - UInt_t iinp = 0; - for( ItVarTypeIdxConst itGet = fGet.begin(), itGetEnd = fGet.end(); itGet != itGetEnd; ++itGet ) { - min = fMin.at(cls).at(iinp); - max = fMax.at(cls).at(iinp); - - Char_t type = (*itGet).first; - UInt_t idx = (*itGet).second; + const UInt_t nvar = GetNVariables(); + std::vector<TString>* strVec = new std::vector<TString>(nvar); + Float_t min, max; + for (Int_t ivar=nvar-1; ivar>=0; ivar--) { + min = fMin.at(cls).at(ivar); + max = fMax.at(cls).at(ivar); Float_t offset = min; Float_t scale = 1.0/(max-min); TString str(""); - VariableInfo& varInfo = (type=='v'?fDsi.GetVariableInfo(idx):(type=='t'?fDsi.GetTargetInfo(idx):fDsi.GetSpectatorInfo(idx))); - - if (offset < 0) str = Form( "2*%g*([%s] + %g) - 1", scale, varInfo.GetLabel().Data(), -offset ); - else str = Form( "2*%g*([%s] - %g) - 1", scale, varInfo.GetLabel().Data(), offset ); - (*strVec)[iinp] = str; - - ++iinp; + if (offset < 0) str = Form( "2*%g*([%s] + %g) - 1", scale, Variables()[ivar].GetLabel().Data(), -offset ); + else str = Form( "2*%g*([%s] - %g) - 1", scale, Variables()[ivar].GetLabel().Data(), offset ); + (*strVec)[ivar] = str; } -// Float_t min, max; -// for (Int_t ivar=nvar-1; ivar>=0; ivar--) { -// min = fMin.at(cls).at(ivar); -// max = fMax.at(cls).at(ivar); -// Float_t offset = min; -// Float_t scale = 1.0/(max-min); -// TString str(""); -// if (offset < 0) str = Form( "2*%g*([%s] + %g) - 1", scale, Variables()[ivar].GetLabel().Data(), -offset ); -// else str = Form( "2*%g*([%s] - %g) - 1", scale, Variables()[ivar].GetLabel().Data(), offset ); -// (*strVec)[ivar] = str; -// } - return strVec; } @@ -325,25 +303,31 @@ void TMVA::VariableNormalizeTransform::WriteTransformationToStream( std::ostream void TMVA::VariableNormalizeTransform::AttachXMLTo(void* parent) { // create XML description of Normalize transformation - void* trfxml = gTools().AddChild(parent, "Transform"); - gTools().AddAttr(trfxml, "Name", "Normalize"); - - VariableTransformBase::AttachXMLTo( trfxml ); - Int_t numC = (GetNClasses()<= 1)?1:GetNClasses()+1; + UInt_t nvars = GetNVariables(); + UInt_t ntgts = GetNTargets(); + void* trfxml = gTools().AddChild(parent, "Transform"); + gTools().AddAttr(trfxml, "Name", "Normalize"); + gTools().AddAttr(trfxml, "NVariables", nvars); + gTools().AddAttr(trfxml, "NTargets", ntgts); for( Int_t icls=0; icls<numC; icls++ ) { void* clsxml = gTools().AddChild(trfxml, "Class"); gTools().AddAttr(clsxml, "ClassIndex", icls); - void* inpxml = gTools().AddChild(clsxml, "Ranges"); - UInt_t iinp = 0; - for( ItVarTypeIdx itGet = fGet.begin(), itGetEnd = fGet.end(); itGet != itGetEnd; ++itGet ) { - void* mmxml = gTools().AddChild(inpxml, "Range"); - gTools().AddAttr(mmxml, "Index", iinp); - gTools().AddAttr(mmxml, "Min", fMin.at(icls).at(iinp) ); - gTools().AddAttr(mmxml, "Max", fMax.at(icls).at(iinp) ); - ++iinp; + void* varsxml = gTools().AddChild(clsxml, "Variables"); + for (UInt_t ivar=0; ivar<nvars; ivar++) { + void* varxml = gTools().AddChild(varsxml, "Variable"); + gTools().AddAttr(varxml, "VarIndex", ivar); + gTools().AddAttr(varxml, "Min", fMin.at(icls).at(ivar) ); + gTools().AddAttr(varxml, "Max", fMax.at(icls).at(ivar) ); + } + void* tgtsxml = gTools().AddChild(clsxml, "Targets"); + for (UInt_t itgt=0; itgt<ntgts; itgt++) { + void* tgtxml = gTools().AddChild(tgtsxml, "Target"); + gTools().AddAttr(tgtxml, "TargetIndex", itgt); + gTools().AddAttr(tgtxml, "Min", fMin.at(icls).at(nvars+itgt) ); + gTools().AddAttr(tgtxml, "Max", fMax.at(icls).at(nvars+itgt) ); } } } @@ -352,72 +336,11 @@ void TMVA::VariableNormalizeTransform::AttachXMLTo(void* parent) void TMVA::VariableNormalizeTransform::ReadFromXML( void* trfnode ) { // Read the transformation matrices from the xml node - - Bool_t newFormat = kFALSE; - - void* inpnode = NULL; - try{ - inpnode = gTools().GetChild(trfnode, "Input"); // new xml format - newFormat = kTRUE; - }catch( std::logic_error& excpt ){ - newFormat = kFALSE; // old xml format - } - if( newFormat ){ - // ------------- new format -------------------- - // read input - VariableTransformBase::ReadFromXML( inpnode ); - - // read transformation information - - UInt_t size = fGet.size(); - UInt_t classindex, idx; - - void* ch = gTools().GetChild( trfnode ); - while(ch) { - Int_t ci = 0; - gTools().ReadAttr(ch, "ClassIndex", ci); - classindex = UInt_t(ci); - - fMin.resize(classindex+1); - fMax.resize(classindex+1); - - fMin[classindex].resize(size,Float_t(0)); - fMax[classindex].resize(size,Float_t(0)); - - void* clch = gTools().GetChild( ch ); - while(clch) { - TString nodeName(gTools().GetName(clch)); - if(nodeName=="Ranges") { - void* varch = gTools().GetChild( clch ); - while(varch) { - gTools().ReadAttr(varch, "Index", idx); - gTools().ReadAttr(varch, "Min", fMin[classindex][idx]); - gTools().ReadAttr(varch, "Max", fMax[classindex][idx]); - varch = gTools().GetNextChild( varch ); - } - } - clch = gTools().GetNextChild( clch ); - } - ch = gTools().GetNextChild( ch ); - } - - SetCreated(); - return; - } - - // ------------- old format -------------------- UInt_t classindex, varindex, tgtindex, nvars, ntgts; gTools().ReadAttr(trfnode, "NVariables", nvars); gTools().ReadAttr(trfnode, "NTargets", ntgts); - for( UInt_t ivar = 0; ivar < nvars; ++ivar ){ - fGet.push_back(std::make_pair<Char_t,UInt_t>('v',ivar)); - } - for( UInt_t itgt = 0; itgt < ntgts; ++itgt ){ - fGet.push_back(std::make_pair<Char_t,UInt_t>('t',itgt)); - } - void* ch = gTools().GetChild( trfnode ); while(ch) { gTools().ReadAttr(ch, "ClassIndex", classindex); @@ -491,14 +414,6 @@ void TMVA::VariableNormalizeTransform::ReadTransformationFromStream( std::istrea UInt_t nvars = GetNVariables(); UInt_t ntgts = GetNTargets(); - - for( UInt_t ivar = 0; ivar < nvars; ++ivar ){ - fGet.push_back(std::make_pair<Char_t,UInt_t>('v',ivar)); - } - for( UInt_t itgt = 0; itgt < ntgts; ++itgt ){ - fGet.push_back(std::make_pair<Char_t,UInt_t>('t',itgt)); - } - char buf[512]; char buf2[512]; istr.getline(buf,512); @@ -530,26 +445,23 @@ void TMVA::VariableNormalizeTransform::ReadTransformationFromStream( std::istrea } //_______________________________________________________________________ -void TMVA::VariableNormalizeTransform::PrintTransformation( ostream& ) +void TMVA::VariableNormalizeTransform::PrintTransformation( ostream& o ) { // prints the transformation ranges Int_t numC = GetNClasses()+1; if (GetNClasses() <= 1 ) numC = 1; + UInt_t nvars = GetNVariables(); + UInt_t ntgts = GetNTargets(); for (Int_t icls = 0; icls < numC; icls++ ) { - Log() << "Transformation for class " << icls << " based on these ranges:" << Endl; - - UInt_t iinp = 0; - for( ItVarTypeIdxConst itGet = fGet.begin(), itGetEnd = fGet.end(); itGet != itGetEnd; ++itGet ){ - Char_t type = (*itGet).first; - UInt_t idx = (*itGet).second; - - TString typeString = (type=='v'?"Variable: ": (type=='t'?"Target : ":"Spectator : ") ); - Log() << typeString.Data() << std::setw(20) << fMin[icls][idx] << std::setw(20) << fMax[icls][idx] << Endl; - - ++iinp; - } + Log() << kINFO << "Transformation for class " << icls << " based on these ranges:" << Endl; + Log() << kINFO << "Variables:" << Endl; + for (UInt_t ivar=0; ivar<nvars; ivar++) + o << std::setw(20) << fMin[icls][ivar] << std::setw(20) << fMax[icls][ivar] << std::endl; + Log() << kINFO << "Targets:" << Endl; + for (UInt_t itgt=0; itgt<ntgts; itgt++) + o << std::setw(20) << fMin[icls][nvars+itgt] << std::setw(20) << fMax[icls][nvars+itgt] << std::endl; } } @@ -558,12 +470,12 @@ void TMVA::VariableNormalizeTransform::MakeFunction( std::ostream& fout, const T Int_t part, UInt_t trCounter, Int_t ) { // creates a normalizing function - + // TODO include target-transformation into makefunction UInt_t numC = fMin.size(); if (part==1) { fout << std::endl; - fout << " double fMin_"<<trCounter<<"["<<numC<<"]["<<fGet.size()<<"];" << std::endl; - fout << " double fMax_"<<trCounter<<"["<<numC<<"]["<<fGet.size()<<"];" << std::endl; + fout << " double fMin_"<<trCounter<<"["<<numC<<"]["<<GetNVariables()<<"];" << std::endl; + fout << " double fMax_"<<trCounter<<"["<<numC<<"]["<<GetNVariables()<<"];" << std::endl; } if (part==2) { @@ -571,7 +483,6 @@ void TMVA::VariableNormalizeTransform::MakeFunction( std::ostream& fout, const T fout << "//_______________________________________________________________________" << std::endl; fout << "inline void " << fcncName << "::InitTransform_"<<trCounter<<"()" << std::endl; fout << "{" << std::endl; - for (UInt_t ivar=0; ivar<GetNVariables(); ivar++) { Float_t min = FLT_MAX; Float_t max = -FLT_MAX; diff --git a/tmva/src/VariablePCATransform.cxx b/tmva/src/VariablePCATransform.cxx index a2ea4977d29b2d1870a0d1a800be758b4ab60177..5ca06f947348178fd7eab9d367adebd7f565b530 100644 --- a/tmva/src/VariablePCATransform.cxx +++ b/tmva/src/VariablePCATransform.cxx @@ -12,7 +12,6 @@ * * * Authors (alphabetical): * * Andreas Hoecker <Andreas.Hocker@cern.ch> - CERN, Switzerland * - * Peter Speckmayer <Peter.Speckmayer@cern.ch> - CERN, Switzerland * * Joerg Stelzer <Joerg.Stelzer@cern.ch> - CERN, Switzerland * * Helge Voss <Helge.Voss@cern.ch> - MPI-K Heidelberg, Germany * * * @@ -80,17 +79,15 @@ Bool_t TMVA::VariablePCATransform::PrepareTransformation( const std::vector<Even Log() << kINFO << "Preparing the Principle Component (PCA) transformation..." << Endl; - UInt_t inputSize = fGet.size(); - - SetNVariables(inputSize); + SetNVariables(events[0]->GetNVariables()); // TPrincipal doesn't support PCA transformation for 1 or less variables - if (inputSize <= 1) { + if (GetNVariables() <= 1) { Log() << kINFO << "Cannot perform PCA transformation for " << GetNVariables() << " variable only" << Endl; return kFALSE; } - if (inputSize > 200) { + if (GetNVariables() > 200) { Log() << kINFO << "----------------------------------------------------------------------------" << Endl; Log() << kINFO @@ -113,40 +110,41 @@ const TMVA::Event* TMVA::VariablePCATransform::Transform( const Event* const ev, // apply the principal component analysis if (!IsCreated()) return 0; - const Int_t inputSize = fGet.size(); - const UInt_t nCls = GetNClasses(); - //UInt_t evCls = ev->GetClass(); - + const Int_t nvar = ev->GetNVariables(); // if we have more than one class, take the last PCA analysis where all classes are combined if // the cls parameter is outside the defined classes // If there is only one class, then no extra class for all events of all classes has to be created - if (cls < 0 || UInt_t(cls) > nCls) cls = (fMeanValues.size()==1?0:2);//( GetNClasses() == 1 ? 0 : 1 ); ; + if (cls < 0 || cls > GetNClasses()) cls = (fMeanValues.size()==1?0:2);//( GetNClasses() == 1 ? 0 : 1 ); ; // Perform PCA and put it into PCAed events tree - if (fTransformedEvent==0 ) { + if (fTransformedEvent==0 || fTransformedEvent->GetNVariables()!=ev->GetNVariables()) { + if(fTransformedEvent!=0) delete fTransformedEvent; fTransformedEvent = new Event(); } - std::vector<Float_t> input; - std::vector<Float_t> principalComponents; // set the variable values const std::vector<UInt_t>* varArrange = ev->GetVariableArrangement(); - if(!varArrange) { - - GetInput( ev, input ); - X2P( principalComponents, input, cls ); - SetOutput( fTransformedEvent, principalComponents, ev ); + if(!varArrange) { + std::vector<Float_t> rv = X2P( ev->GetValues(), cls ); + for (Int_t ivar=0; ivar<nvar; ++ivar) + fTransformedEvent->SetVal(ivar, rv[ivar]); } else { - // TODO: check what has to be done here - std::vector<Float_t> rv(inputSize); - for (Int_t ivar=0; ivar<inputSize; ++ivar) + std::vector<Float_t> rv(nvar); + for (Int_t ivar=0; ivar<nvar; ++ivar) rv[ivar] = ev->GetValue(ivar); - X2P( principalComponents, input, cls ); - for (Int_t ivar=0; ivar<inputSize; ++ivar) + rv = X2P( rv, cls ); + for (Int_t ivar=0; ivar<nvar; ++ivar) fTransformedEvent->SetVal(ivar, rv[ivar]); } + // set the targets + for (UInt_t itgt=0; itgt<ev->GetNTargets(); itgt++) + fTransformedEvent->SetTarget( itgt, ev->GetTarget(itgt) ); + // and the rest + fTransformedEvent->SetWeight ( ev->GetWeight() ); + fTransformedEvent->SetBoostWeight( ev->GetBoostWeight() ); + fTransformedEvent->SetClass ( ev->GetClass() ); return fTransformedEvent; } @@ -156,43 +154,28 @@ const TMVA::Event* TMVA::VariablePCATransform::InverseTransform( const Event* co { // apply the principal component analysis // TODO: implementation of inverse transformation -// Log() << kFATAL << "Inverse transformation for PCA transformation not yet implemented. Hence, this transformation cannot be applied together with regression. Please contact the authors if necessary." << Endl; + Log() << kFATAL << "Inverse transformation for PCA transformation not yet implemented. Hence, this transformation cannot be applied together with regression. Please contact the authors if necessary." << Endl; if (!IsCreated()) return 0; - - - const Int_t inputSize = fGet.size(); - const UInt_t nCls = GetNClasses(); - //UInt_t evCls = ev->GetClass(); + const Int_t nvar = ev->GetNVariables(); // if we have more than one class, take the last PCA analysis where all classes are combined if // the cls parameter is outside the defined classes // If there is only one class, then no extra class for all events of all classes has to be created - if (cls < 0 || UInt_t(cls) > nCls) cls = (fMeanValues.size()==1?0:2);//( GetNClasses() == 1 ? 0 : 1 ); ; - // Perform PCA and put it into PCAed events tree - - if (fBackTransformedEvent==0 ) fBackTransformedEvent = new Event(); + if (cls < 0 || cls > GetNClasses()) cls = ( GetNClasses() == 1 ? 0 : 1 ); - std::vector<Float_t> principalComponents; - std::vector<Float_t> output; - // set the variable values - const std::vector<UInt_t>* varArrange = ev->GetVariableArrangement(); - if(!varArrange) { - - GetInput( ev, principalComponents ); - P2X( output, principalComponents, cls ); - SetOutput( fBackTransformedEvent, output, ev ); + // Perform PCA and put it into PCAed events tree + std::vector<Float_t> rv = X2P( ev->GetValues(), cls ); - } else { - // TODO: check what has to be done here - std::vector<Float_t> rv(inputSize); - for (Int_t ivar=0; ivar<inputSize; ++ivar) - rv[ivar] = ev->GetValue(ivar); - X2P( principalComponents, output, cls ); - for (Int_t ivar=0; ivar<inputSize; ++ivar) - fBackTransformedEvent->SetVal(ivar, rv[ivar]); + if (fBackTransformedEvent==0 || fBackTransformedEvent->GetNVariables()!=ev->GetNVariables()) { + if(fBackTransformedEvent!=0) delete fBackTransformedEvent; + fBackTransformedEvent = new Event( *ev ); } + for (Int_t ivar=0; ivar<nvar; ivar++) fBackTransformedEvent->SetVal(ivar, rv[ivar]); + fBackTransformedEvent->SetClass ( ev->GetClass() ); + fBackTransformedEvent->SetWeight ( ev->GetWeight() ); + fBackTransformedEvent->SetBoostWeight( ev->GetBoostWeight() ); return fBackTransformedEvent; } @@ -203,45 +186,25 @@ void TMVA::VariablePCATransform::CalculatePrincipalComponents( const std::vector // calculate the principal components for the signal and the background data // it uses the MakePrincipal method of ROOT's TPrincipal class - UInt_t nvars = 0, ntgts = 0, nspcts = 0; - CountVariableTypes( nvars, ntgts, nspcts ); - if( nvars>0 && ntgts>0 ) - Log() << kFATAL << "Variables and targets cannot be mixed in PCA transformation." << Endl; - - const Int_t inputSize = fGet.size(); + const Int_t nvar = GetNVariables(); // if we have more than one class, add another PCA analysis which combines all classes - const UInt_t nCls = GetNClasses(); - const UInt_t maxPCA = (nCls<=1) ? nCls : nCls+1; + const UInt_t maxPCA = (GetNClasses()<=1) ? GetNClasses() : GetNClasses()+1; // PCA [signal/background/class x/class y/... /all classes] std::vector<TPrincipal*> pca(maxPCA); - for (UInt_t i=0; i<maxPCA; i++) pca[i] = new TPrincipal(nvars,""); + for (UInt_t i=0; i<maxPCA; i++) pca[i] = new TPrincipal(nvar,""); - // !! Not normalizing and not storing input data, for performance reasons. Should perhaps restore normalization. - // But this can be done afterwards by adding a normalisation transformation (user defined) + // !! Not normalizing and not storing input data, for performance reasons. Should perhaps restore normalization. Long64_t ievt, entries = events.size(); - Double_t *dvec = new Double_t[inputSize]; - + Double_t *dvec = new Double_t[nvar]; - std::vector<Float_t> input; for (ievt=0; ievt<entries; ievt++) { Event* ev = events[ievt]; - UInt_t cls = ev->GetClass(); - - GetInput( ev, input ); - - UInt_t iinp = 0; - for( std::vector<Float_t>::iterator itInp = input.begin(), itInpEnd = input.end(); itInp != itInpEnd; ++itInp ) - { - Float_t value = (*itInp); - dvec[iinp] = (Double_t)value; - ++iinp; - } - - pca.at(cls)->AddRow( dvec ); - if (nCls > 1) pca.at(maxPCA-1)->AddRow( dvec ); + for (Int_t i = 0; i < nvar; i++) dvec[i] = (Double_t) ev->GetValue(i); + pca.at(ev->GetClass())->AddRow( dvec ); + if (GetNClasses() > 1) pca.at(maxPCA-1)->AddRow( dvec ); } // delete possible leftovers @@ -263,39 +226,22 @@ void TMVA::VariablePCATransform::CalculatePrincipalComponents( const std::vector } //_______________________________________________________________________ -void TMVA::VariablePCATransform::X2P( std::vector<Float_t>& pc, const std::vector<Float_t>& x, Int_t cls ) const +std::vector<Float_t> TMVA::VariablePCATransform::X2P( const std::vector<Float_t>& x, Int_t cls ) const { // Calculate the principal components from the original data vector // x, and return it in p (function extracted from TPrincipal::X2P) // It's the users responsibility to make sure that both x and p are // of the right size (i.e., memory must be allocated for p) - const Int_t nInput = x.size(); - pc.assign(nInput,0); + const Int_t nvar = x.size(); + std::vector<Float_t> p(nvar,0); - for (Int_t i = 0; i < nInput; i++) { + for (Int_t i = 0; i < nvar; i++) { Double_t pv = 0; - for (Int_t j = 0; j < nInput; j++) + for (Int_t j = 0; j < nvar; j++) pv += (((Double_t)x.at(j)) - (*fMeanValues.at(cls))(j)) * (*fEigenVectors.at(cls))(j,i); - pc[i] = pv; - } -} - -//_______________________________________________________________________ -void TMVA::VariablePCATransform::P2X( std::vector<Float_t>& x, const std::vector<Float_t>& pc, Int_t cls ) const -{ - // Perform the back-transformation from the principal components - // pc, and return x - // It's the users responsibility to make sure that both x and pc are - // of the right size (i.e., memory must be allocated for p) - const Int_t nInput = pc.size(); - x.assign(nInput,0); - - for (Int_t i = 0; i < nInput; i++) { - Double_t xv = 0; - for (Int_t j = 0; j < nInput; j++) - xv += (((Double_t)pc.at(j)) * (*fEigenVectors.at(cls))(i,j) ) + (*fMeanValues.at(cls))(j); - x[i] = xv; + p[i] = pv; } + return p; } //_______________________________________________________________________ diff --git a/tmva/src/VariableTransformBase.cxx b/tmva/src/VariableTransformBase.cxx index d2b252319178f3e9a3e55d0422140c4260a2c2b7..75119090ddc97e1cf5c12fd8baef7ad20294e036 100644 --- a/tmva/src/VariableTransformBase.cxx +++ b/tmva/src/VariableTransformBase.cxx @@ -12,7 +12,6 @@ * * * Authors (alphabetical): * * Andreas Hoecker <Andreas.Hocker@cern.ch> - CERN, Switzerland * - * Peter Speckmayer <Peter.Speckmayer@cern.ch> - CERN, Switzerland * * Joerg Stelzer <Joerg.Stelzer@cern.ch> - CERN, Switzerland * * Helge Voss <Helge.Voss@cern.ch> - MPI-K Heidelberg, Germany * * * @@ -26,9 +25,6 @@ **********************************************************************************/ #include <iomanip> -#include <algorithm> -#include <exception> -#include <stdexcept> #include "TMath.h" #include "TVectorD.h" @@ -83,214 +79,6 @@ TMVA::VariableTransformBase::~VariableTransformBase() delete fLogger; } -//_______________________________________________________________________ -void TMVA::VariableTransformBase::SelectInput( const TString& _inputVariables ) -{ - // select the variables/targets/spectators which serve as input to the transformation - TString inputVariables = _inputVariables; - - - // unselect all variables first - fGet.clear(); - - UInt_t nvars = GetNVariables(); - UInt_t ntgts = GetNTargets(); - UInt_t nspcts = GetNSpectators(); - - if (inputVariables == "") // default is all variables and all targets - { // (the default can be changed by decorating this member function in the implementations) - inputVariables = "_V_,_T_"; - } - - TList* inList = gTools().ParseFormatLine( inputVariables, "," ); - TListIter inIt(inList); - while (TObjString* os = (TObjString*)inIt()) { - TString variables = os->GetString(); - - if( variables.BeginsWith("_") && variables.EndsWith("_") ) { // special symbol (keyword) - variables.Remove( 0,1); // remove first "_" - variables.Remove( variables.Length()-1,1 ); // remove last "_" - - if( variables.BeginsWith("V") ) { // variables - variables.Remove(0,1); // remove "V" - if( variables.Length() == 0 ){ - for( UInt_t ivar = 0; ivar < nvars; ++ivar ) { - fGet.push_back( std::make_pair<Char_t,UInt_t>('v',ivar) ); - } - } else { - UInt_t idx = variables.Atoi(); - if( idx >= nvars ) - Log() << kFATAL << "You selected variable with index : " << idx << " of only " << nvars << " variables." << Endl; - fGet.push_back( std::make_pair<Char_t,UInt_t>('v',idx) ); - } - }else if( variables.BeginsWith("T") ) { // targets - variables.Remove(0,1); // remove "T" - if( variables.Length() == 0 ){ - for( UInt_t itgt = 0; itgt < ntgts; ++itgt ) { - fGet.push_back( std::make_pair<Char_t,UInt_t>('t',itgt) ); - } - } else { - UInt_t idx = variables.Atoi(); - if( idx >= ntgts ) - Log() << kFATAL << "You selected target with index : " << idx << " of only " << ntgts << " targets." << Endl; - fGet.push_back( std::make_pair<Char_t,UInt_t>('t',idx) ); - } - }else if( variables.BeginsWith("S") ) { // spectators - variables.Remove(0,1); // remove "S" - if( variables.Length() == 0 ){ - for( UInt_t ispct = 0; ispct < nspcts; ++ispct ) { - fGet.push_back( std::make_pair<Char_t,UInt_t>('s',ispct) ); - } - } else { - UInt_t idx = variables.Atoi(); - if( idx >= nspcts ) - Log() << kFATAL << "You selected spectator with index : " << idx << " of only " << nspcts << " spectators." << Endl; - fGet.push_back( std::make_pair<Char_t,UInt_t>('s',idx) ); - } - } - }else{ // no keyword - for( UInt_t ivar = 0; ivar < nvars; ++ivar ) { // search all variables - if( fDsi.GetVariableInfo( ivar ).GetLabel() == variables ) { - fGet.push_back( std::make_pair<Char_t,UInt_t>('v',ivar) ); - break; - } - } - for( UInt_t itgt = 0; itgt < ntgts; ++itgt ) { // search all targets - if( fDsi.GetTargetInfo( itgt ).GetLabel() == variables ) { - fGet.push_back( std::make_pair<Char_t,UInt_t>('t',itgt) ); - break; - } - } - for( UInt_t ispct = 0; ispct < nspcts; ++ispct ) { // search all spectators - if( fDsi.GetSpectatorInfo( ispct ).GetLabel() == variables ) { - fGet.push_back( std::make_pair<Char_t,UInt_t>('s',ispct) ); - break; - } - } - } - - } - - Log() << kINFO << "Transformation, Variable selection : " << Endl; - - ItVarTypeIdx itGet = fGet.begin(), itGetEnd = fGet.end(); - for( ; itGet != itGetEnd; ++itGet ) { - TString inputType = "?"; - - Char_t type = (*itGet).first; - Int_t inputIdx = (*itGet).second; - - TString label = "NOT FOUND"; - if( type == 'v' ) { - label = fDsi.GetVariableInfo( inputIdx ).GetLabel(); - inputType = "variable"; - } - else if( type == 't' ){ - label = fDsi.GetTargetInfo( inputIdx ).GetLabel(); - inputType = "target"; - } - else if( type == 's' ){ - label = fDsi.GetSpectatorInfo( inputIdx ).GetLabel(); - inputType = "spectator"; - } - - Log() << kINFO << "Input : " << inputType.Data() << " '" << label.Data() << "' (index=" << inputIdx << ")." << Endl; - } - -} - - -//_______________________________________________________________________ -void TMVA::VariableTransformBase::GetInput( const Event* event, std::vector<Float_t>& input ) const -{ - // select the values from the event - input.clear(); - for( ItVarTypeIdxConst itEntry = fGet.begin(), itEntryEnd = fGet.end(); itEntry != itEntryEnd; ++itEntry ) { - Char_t type = (*itEntry).first; - Int_t idx = (*itEntry).second; - - switch( type ) { - case 'v': - input.push_back( event->GetValue(idx) ); - break; - case 't': - input.push_back( event->GetTarget(idx) ); - break; - case 's': - input.push_back( event->GetSpectator(idx) ); - break; - default: - Log() << kFATAL << "VariableTransformBase/GetInput : unknown type '" << type << "'." << Endl; - } - } -} - -//_______________________________________________________________________ -void TMVA::VariableTransformBase::SetOutput( Event* event, std::vector<Float_t>& output, const Event* oldEvent ) const -{ - // select the values from the event - - std::vector<Float_t>::iterator itOutput = output.begin(); - - if( oldEvent ) - event->CopyVarValues( *oldEvent ); - - try { - - for( ItVarTypeIdxConst itEntry = fGet.begin(), itEntryEnd = fGet.end(); itEntry != itEntryEnd; ++itEntry ) { - Char_t type = (*itEntry).first; - Int_t idx = (*itEntry).second; - - Float_t value = (*itOutput); - - switch( type ) { - case 'v': - event->SetVal( idx, value ); - break; - case 't': - event->SetTarget( idx, value ); - break; - case 's': - event->SetSpectator( idx, value ); - break; - default: - Log() << kFATAL << "VariableTransformBase/GetInput : unknown type '" << type << "'." << Endl; - } - ++itOutput; - } - }catch( std::exception& except ){ - Log() << kFATAL << "VariableTransformBase/SetOutput : exception/" << except.what() << Endl; - throw; - } -} - - -//_______________________________________________________________________ -void TMVA::VariableTransformBase::CountVariableTypes( UInt_t& nvars, UInt_t& ntgts, UInt_t& nspcts ) -{ - // count variables, targets and spectators - nvars = ntgts = nspcts = 0; - - for( ItVarTypeIdxConst itEntry = fGet.begin(), itEntryEnd = fGet.end(); itEntry != itEntryEnd; ++itEntry ) { - Char_t type = (*itEntry).first; - - switch( type ) { - case 'v': - nvars++; - break; - case 't': - ntgts++; - break; - case 's': - nspcts++; - break; - default: - Log() << kFATAL << "VariableTransformBase/GetVariableTypeNumbers : unknown type '" << type << "'." << Endl; - } - } -} - - //_______________________________________________________________________ void TMVA::VariableTransformBase::CalcNorm( const std::vector<Event*>& events ) { @@ -409,128 +197,3 @@ void TMVA::VariableTransformBase::UpdateNorm ( Int_t ivar, Double_t x ) } } - - -//_______________________________________________________________________ -void TMVA::VariableTransformBase::AttachXMLTo(void* parent) -{ - // create XML description the transformation (write out info of selected variables) - - void* inpxml = gTools().AddChild(parent, "Input"); - gTools().AddAttr(inpxml, "NInputs", fGet.size() ); - - for( ItVarTypeIdx itGet = fGet.begin(), itGetEnd = fGet.end(); itGet != itGetEnd; ++itGet ) { - UInt_t idx = (*itGet).second; - Char_t type = (*itGet).first; - - TString label = ""; - TString typeString = ""; - switch( type ){ - case 'v': - typeString = "Variable"; - label = fDsi.GetVariableInfo( idx ).GetLabel(); - break; - case 't': - typeString = "Target"; - label = fDsi.GetTargetInfo( idx ).GetLabel(); - break; - case 's': - typeString = "Spectator"; - label = fDsi.GetSpectatorInfo( idx ).GetLabel(); - break; - default: - Log() << kFATAL << "VariableTransformBase/AttachXMLTo unknown variable type '" << type << "'." << Endl; - } - - void* idxxml = gTools().AddChild(inpxml, "Input"); -// gTools().AddAttr(idxxml, "Index", idx); - gTools().AddAttr(idxxml, "Type", typeString); - gTools().AddAttr(idxxml, "Label", label); - - } -} - -//_______________________________________________________________________ -void TMVA::VariableTransformBase::ReadFromXML( void* inpnode ) -{ - // Read the input variables from the XML node - fGet.clear(); - - UInt_t nvars = GetNVariables(); - UInt_t ntgts = GetNTargets(); - UInt_t nspcts = GetNSpectators(); - - UInt_t nInputs = 0; - gTools().ReadAttr(inpnode, "NInputs", nInputs); - - void* ch = gTools().GetChild( inpnode ); - while(ch) { - TString typeString = ""; - TString label = ""; - - gTools().ReadAttr(ch, "Type", typeString); - gTools().ReadAttr(ch, "Label", label); - - if( typeString == "Variable" ){ - for( UInt_t ivar = 0; ivar < nvars; ++ivar ) { // search all variables - if( fDsi.GetVariableInfo( ivar ).GetLabel() == label ) { - fGet.push_back( std::make_pair<Char_t,UInt_t>('v',ivar) ); - break; - } - } - }else if( typeString == "Target" ){ - for( UInt_t itgt = 0; itgt < ntgts; ++itgt ) { // search all targets - if( fDsi.GetTargetInfo( itgt ).GetLabel() == label ) { - fGet.push_back( std::make_pair<Char_t,UInt_t>('t',itgt) ); - break; - } - } - }else if( typeString == "Spectator" ){ - for( UInt_t ispct = 0; ispct < nspcts; ++ispct ) { // search all spectators - if( fDsi.GetSpectatorInfo( ispct ).GetLabel() == label ) { - fGet.push_back( std::make_pair<Char_t,UInt_t>('s',ispct) ); - break; - } - } - }else{ - Log() << kFATAL << "VariableTransformationBase/ReadFromXML : unknown type '" << typeString << "'." << Endl; - } - ch = gTools().GetNextChild( ch ); - } -} - - -//_______________________________________________________________________ -void TMVA::VariableTransformBase::MakeFunction( std::ostream& /*fout*/, const TString& /*fncName*/, Int_t part, - UInt_t /*trCounter*/, Int_t /*cls*/ ) -{ - // getinput and setoutput equivalent - if( part == 0 ){ // getinput equivalent -// fout << std::endl; -// fout << " std::vector<double> input; " << std::endl; -// // select the values from the event -// input.clear(); -// for( ItVarTypeIdxConst itEntry = fGet.begin(), itEntryEnd = fGet.end(); itEntry != itEntryEnd; ++itEntry ) { -// Char_t type = (*itEntry).first; -// Int_t idx = (*itEntry).second; - -// switch( type ) { -// case 'v': -// input.push_back( event->GetValue(idx) ); -// break; -// case 't': -// input.push_back( event->GetTarget(idx) ); -// break; -// case 's': -// input.push_back( event->GetSpectator(idx) ); -// break; -// default: -// Log() << kFATAL << "VariableTransformBase/GetInput : unknown type '" << type << "'." << Endl; -// } -// } - - - }else if( part == 1){ // setoutput equivalent - } -} - diff --git a/tmva/test/TMVAMulticlass.C b/tmva/test/TMVAMulticlass.C new file mode 100644 index 0000000000000000000000000000000000000000..240a8f6e108e468e020985e41c37546afe0f2867 --- /dev/null +++ b/tmva/test/TMVAMulticlass.C @@ -0,0 +1,86 @@ + +#include <cstdlib> +#include <iostream> +#include <map> +#include <string> + +#include "TChain.h" +#include "TFile.h" +#include "TTree.h" +#include "TString.h" +#include "TObjString.h" +#include "TSystem.h" +#include "TROOT.h" +#include "TPluginManager.h" + +#include "TMVAGui.C" + +#ifndef __CINT__ +#include "TMVA/Tools.h" +#include "TMVA/Factory.h" +#endif + +using namespace TMVA; + +void TMVAMulticlass(){ + TString outfileName = "TMVAMulticlass.root"; + TFile* outputFile = TFile::Open( outfileName, "RECREATE" ); + TMVA::Factory *factory = new TMVA::Factory( "TMVAClassification", outputFile, + "!V:!Silent:Color:DrawProgressBar:Transformations=I;D;P;G,D:AnalysisType=multiclass" ); + factory->AddVariable( "var0", 'F' ); + factory->AddVariable( "var1", 'F' ); + TFile *input(0); + TString fname = "./data.root"; + if (!gSystem->AccessPathName( fname )) { + // first we try to find data.root in the local directory + std::cout << "--- TMVAMulticlass : Accessing " << fname << std::endl; + input = TFile::Open( fname ); + } + else { + gROOT->LoadMacro( "./createData.C"); + create_multiclassdata(20000); + cout << " created data.root for tests of the multiclass features"<<endl; + input = TFile::Open( fname ); + } + if (!input) { + std::cout << "ERROR: could not open data file" << std::endl; + exit(1); + } + TTree *tree = (TTree*)input->Get("TreeR"); + + gROOT->cd( outfileName+TString(":/") ); + factory->AddTree ( tree, "Signal1", 1. , "cls==0" ); + factory->AddTree ( tree, "Signal2", 1. , "cls==1" ); + factory->AddTree ( tree, "Background", 1., "cls==2" ); + factory->PrepareTrainingAndTestTree( "", "SplitMode=Random:NormMode=NumEvents:!V" ); + + factory->BookMethod( TMVA::Types::kBDT, "BDT", "!H:!V:NTrees=1000:BoostType=Grad:Shrinkage=0.30:UseBaggedGrad:GradBaggingFraction=0.6:SeparationType=GiniIndex:nCuts=20:NNodesMax=5"); + factory->BookMethod( TMVA::Types::kMLP, "MLP", "!H:!V:NeuronType=tanh:VarTransform=N:NCycles=100:HiddenLayers=N+5,3:TestRate=5"); // testing vartransforms + factory->BookMethod( TMVA::Types::kMLP, "MLP2", "!H:!V:NeuronType=tanh:NCycles=100:HiddenLayers=N+5,3:TestRate=5"); + factory->BookMethod( TMVA::Types::kFDA, "FDA_GA", + "H:!V:Formula=(0)+(1)*x0+(2)*x1+(3)*x0*x1:ParRanges=(-1,1);(-10,10);(-10,10);(-10,10):FitMethod=GA:PopSize=300:Cycles=3:Steps=20:Trim=True:SaveBestGen=1" ); + + // Train MVAs using the set of training events + factory->TrainAllMethods(); + + // ---- Evaluate all MVAs using the set of test events + factory->TestAllMethods(); + + // ----- Evaluate and compare performance of all configured MVAs + factory->EvaluateAllMethods(); + + // -------------------------------------------------------------- + + // Save the output + outputFile->Close(); + + std::cout << "==> Wrote root file: " << outputFile->GetName() << std::endl; + std::cout << "==> TMVAClassification is done!" << std::endl; + + delete factory; + + // Launch the GUI for the root macros + if (!gROOT->IsBatch()) TMVAGui( outfileName ); + + +}