本文整理汇总了C++中tmva::Factory::SetSignalWeightExpression方法的典型用法代码示例。如果您正苦于以下问题:C++ Factory::SetSignalWeightExpression方法的具体用法?C++ Factory::SetSignalWeightExpression怎么用?C++ Factory::SetSignalWeightExpression使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tmva::Factory
的用法示例。
在下文中一共展示了Factory::SetSignalWeightExpression方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: TMVAtest
void TMVAtest(){
//gSystem->Load("../lib/slc5_amd64_gcc462/libTAMUWWMEPATNtuple.so");
gSystem->Load("libPhysics");
//gSystem->Load("EvtTreeForAlexx_h.so");
gSystem->Load("libTMVA.1");
gSystem->Load("AutoDict_vector_TLorentzVector__cxx.so");
TMVA::Tools::Instance();
TFile* outputFile = TFile::Open("TMVA1.root", "RECREATE");
TMVA::Factory *factory = new TMVA::Factory( "TMVAClassification",outputFile,"V=true:Color:DrawProgressBar");// ":Transformations=I;D;P;G,D" );
TFile* signal = TFile::Open("/uscms_data/d2/aperloff/Spring12ME7TeV/MEResults/microNtuples_oldStructure/microWW_EPDv01.root");
TFile* bkg = TFile::Open("/uscms_data/d2/aperloff/Spring12ME7TeV/MEResults/microNtuples_oldStructure/microWJets_EPDv01.root");
TTree* stree = (TTree*)signal->Get("METree");
TTree* btree = (TTree*)bkg->Get("METree");
factory->AddSignalTree(stree,1.0);
factory->AddBackgroundTree(btree,1.0);
factory->SetSignalWeightExpression("1.0");
factory->SetBackgroundWeightExpression("1.0");
factory->AddVariable("tEventProb[0]");
factory->AddVariable("tEventProb[1]");
factory->AddVariable("tEventProb[2]");
//factory->AddVariable("tEventProb0 := tEventProb[0]",'F');
//factory->AddVariable("tEventProb1 := tEventProb[1]",'F');
//factory->AddVariable("tEventProb2 := tEventProb[2]",'F');
TCut test("Entry$>-2 && jLV[1].Pt()>30");
TCut mycuts (test);
factory->PrepareTrainingAndTestTree(mycuts,mycuts,"nTrain_Signal=0:nTrain_Background=0:nTest_Signal=0:nTest_Background=0:SplitMode=Random:NormMode=None:V=true:VerboseLevel=DEBUG");
factory->BookMethod( TMVA::Types::kBDT, "BDT","!H:!V:NTrees=400:nEventsMin=400:MaxDepth=3:BoostType=AdaBoost:SeparationType=GiniIndex:nCuts=20:PruneMethod=NoPruning" );
factory->TrainAllMethods();
factory->TestAllMethods();
factory->EvaluateAllMethods();
outputFile->Close();
}
示例2: TMVA_stop
//.........这里部分代码省略.........
// variable definition, but simply compute the expression before adding the event
//
// // --- begin ----------------------------------------------------------
// std::vector<Double_t> vars( 4 ); // vector has size of number of input variables
// Float_t treevars[4], weight;
//
// // Signal
// for (UInt_t ivar=0; ivar<4; ivar++) signal->SetBranchAddress( Form( "var%i", ivar+1 ), &(treevars[ivar]) );
// for (UInt_t i=0; i<signal->GetEntries(); i++) {
// signal->GetEntry(i);
// for (UInt_t ivar=0; ivar<4; ivar++) vars[ivar] = treevars[ivar];
// // add training and test events; here: first half is training, second is testing
// // note that the weight can also be event-wise
// if (i < signal->GetEntries()/2.0) factory->AddSignalTrainingEvent( vars, signalWeight );
// else factory->AddSignalTestEvent ( vars, signalWeight );
// }
//
// // Background (has event weights)
// background->SetBranchAddress( "weight", &weight );
// for (UInt_t ivar=0; ivar<4; ivar++) background->SetBranchAddress( Form( "var%i", ivar+1 ), &(treevars[ivar]) );
// for (UInt_t i=0; i<background->GetEntries(); i++) {
// background->GetEntry(i);
// for (UInt_t ivar=0; ivar<4; ivar++) vars[ivar] = treevars[ivar];
// // add training and test events; here: first half is training, second is testing
// // note that the weight can also be event-wise
// if (i < background->GetEntries()/2) factory->AddBackgroundTrainingEvent( vars, backgroundWeight*weight );
// else factory->AddBackgroundTestEvent ( vars, backgroundWeight*weight );
// }
// // --- end ------------------------------------------------------------
//
// --- end of tree registration
// Set individual event weights (the variables must exist in the original TTree)
factory->SetSignalWeightExpression ("mini_weight");
factory->SetBackgroundWeightExpression("mini_weight");
/*
if( doMultipleOutputs ){
multifactory->AddTree(signal,"Signal");
multifactory->SetSignalWeightExpression ("event_scale1fb");
multifactory->SetBackgroundWeightExpression("event_scale1fb");
multifactory->SetWeightExpression("event_scale1fb");
if( includeBkg["ww"] ){
TTree* ww = (TTree*) chww;
multifactory->AddTree(ww,"WW");
cout << "Added WW to multi-MVA" << endl;
}
if( includeBkg["wjets"] ){
TTree* wjets = (TTree*) chwjets;
multifactory->AddTree(wjets,"WJets");
cout << "Added W+jets to multi-MVA" << endl;
}
if( includeBkg["tt"] ){
TTree* tt = (TTree*) chtt;
multifactory->AddTree(tt,"tt");
cout << "Added ttbar multi-MVA" << endl;
}
}
*/
// Apply additional cuts on the signal and background samples (can be different)
TCut mycuts = sel0; // for example: TCut mycuts = "abs(var1)<0.5 && abs(var2-0.5)<1";
TCut mycutb = sel0; // for example: TCut mycutb = "abs(var1)<0.5";
// Tell the factory how to use the training and testing events
示例3: main
//.........这里部分代码省略.........
std::cerr << "Warning, file " << filename << " could not be opened." << std::endl;
} else {
sfiles[sigfiles[i]] = tmp;
}
}
TTree *signal[sigfiles.size()];
for (unsigned i = 0; i < bkgfiles.size(); ++i) {
std::string f = bkgfiles[i];
if (tfiles[f]){
background[i] = (TTree*)tfiles[f]->Get("TmvaInputTree");
//if (f.find("QCD-Pt")!=f.npos){
//}
Double_t backgroundWeight = 1.0;
factory->AddBackgroundTree(background[i],backgroundWeight);
factory->SetBackgroundWeightExpression("total_weight");
}//if file exist
else {
std::cout << " Cannot find background file " << f << std::endl;
}
}//loop on files
for (unsigned i = 0; i < sigfiles.size(); ++i) {
std::string f = sigfiles[i];
if (sfiles[f]){
signal[i] = (TTree*)sfiles[f]->Get("TmvaInputTree");
//if (f.find("QCD-Pt")!=f.npos){
//}
Double_t signalWeight = 1.0;
factory->AddSignalTree(signal[i],signalWeight);
factory->SetSignalWeightExpression("total_weight");
}//if file exist
else {
std::cout << " Cannot find signal file " << f << std::endl;
}
}//loop on files
// Apply additional cuts on the signal and background samples (can be different)
TCut mycuts = "";//dijet_deta>3.8 && dijet_M > 1100 && met > 100 && met_significance>5";
TCut mycutb = "";//dijet_deta>3.8 && dijet_M > 1100 && met > 100 && met_significance>5";
factory->PrepareTrainingAndTestTree( mycuts, mycutb,
"nTrain_Signal=0:nTrain_Background=0:SplitMode=Random:NormMode=NumEvents:!V" );
// Likelihood ("naive Bayes estimator")
//factory->BookMethod( TMVA::Types::kLikelihood, "Likelihood",
//"H:!V:!TransformOutput:PDFInterpol=Spline2:NSmoothSig[0]=20:NSmoothBkg[0]=20:NSmoothBkg[1]=10:NSmooth=1:NAvEvtPerBin=50" );
// Linear discriminant (same as Fisher discriminant)
//factory->BookMethod( TMVA::Types::kLD, "LD", "H:!V:VarTransform=None:CreateMVAPdfs:PDFInterpolMVAPdf=Spline2:NbinsMVAPdf=50:NsmoothMVAPdf=10" );
// Fisher discriminant (same as LD)
factory->BookMethod( TMVA::Types::kFisher, "Fisher", "H:!V:Fisher:CreateMVAPdfs:PDFInterpolMVAPdf=Spline2:NbinsMVAPdf=50:NsmoothMVAPdf=10" );
// TMVA ANN: MLP (recommended ANN) -- all ANNs in TMVA are Multilayer Perceptrons
//factory->BookMethod( TMVA::Types::kMLP, "MLP", "H:!V:NeuronType=tanh:VarTransform=N:NCycles=60:HiddenLayers=N+5:TestRate=5:!UseRegulator" );
// Boosted Decision Trees
// Gradient Boost
示例4: TMVAClassification
//.........这里部分代码省略.........
//
// // --- begin ----------------------------------------------------------
// std::vector<Double_t> vars( 4 ); // vector has size of number of input variables
// Float_t treevars[4];
// for (Int_t ivar=0; ivar<4; ivar++) signal->SetBranchAddress( Form( "var%i", ivar+1 ), &(treevars[ivar]) );
// for (Int_t i=0; i<signal->GetEntries(); i++) {
// signal->GetEntry(i);
// for (Int_t ivar=0; ivar<4; ivar++) vars[ivar] = treevars[ivar];
// // add training and test events; here: first half is training, second is testing
// // note that the weight can also be event-wise
// if (i < signal->GetEntries()/2) factory->AddSignalTrainingEvent( vars, signalWeight );
// else factory->AddSignalTestEvent ( vars, signalWeight );
// }
//
// for (Int_t ivar=0; ivar<4; ivar++) background->SetBranchAddress( Form( "var%i", ivar+1 ), &(treevars[ivar]) );
// for (Int_t i=0; i<background->GetEntries(); i++) {
// background->GetEntry(i);
// for (Int_t ivar=0; ivar<4; ivar++) vars[ivar] = treevars[ivar];
// // add training and test events; here: first half is training, second is testing
// // note that the weight can also be event-wise
// if (i < background->GetEntries()/2) factory->AddBackgroundTrainingEvent( vars, backgroundWeight );
// else factory->AddBackgroundTestEvent ( vars, backgroundWeight );
// }
// // --- end ------------------------------------------------------------
//
// ====== end of register trees ==============================================
}
// This would set individual event weights (the variables defined in the
// expression need to exist in the original TTree)
// for signal : factory->SetSignalWeightExpression("weight1*weight2");
// for background: factory->SetBackgroundWeightExpression("weight1*weight2");
factory->SetBackgroundWeightExpression("Eweight*XS*BR*LUM*(1/NGE)*(B2/B3)*CUT");
factory->SetSignalWeightExpression("Eweight*XS*BR*LUM*(1/NGE)*(B2/B3)*CUT");
// Apply additional cuts on the signal and background samples (can be different)
TCut mycuts = "(CUT>2)";
TCut mycutb = "(CUT>2)";
// tell the factory to use all remaining events in the trees after training for testing:
factory->PrepareTrainingAndTestTree( mycuts, "SplitMode=random:!V" );
// "nTrain_Signal=0:nTrain_Background=0:SplitMode=Random:NormMode=NumEvents:!V" );
// If no numbers of events are given, half of the events in the tree are used for training, and
// the other half for testing:
// factory->PrepareTrainingAndTestTree( mycut, "SplitMode=random:!V" );
// To also specify the number of testing events, use:
// factory->PrepareTrainingAndTestTree( mycut,
// "NSigTrain=3000:NBkgTrain=3000:NSigTest=3000:NBkgTest=3000:SplitMode=Random:!V" );
// ---- Book MVA methods
//
// please lookup the various method configuration options in the corresponding cxx files, eg:
// src/MethoCuts.cxx, etc, or here: http://tmva.sourceforge.net/optionRef.html
// it is possible to preset ranges in the option string in which the cut optimisation should be done:
// "...:CutRangeMin[2]=-1:CutRangeMax[2]=1"...", where [2] is the third input variable
// Cut optimisation
if (Use["Cuts"])
factory->BookMethod( TMVA::Types::kCuts, "Cuts",
"!H:!V:FitMethod=MC:EffSel:SampleSize=200000:VarProp=FSmart" );
if (Use["CutsD"])
factory->BookMethod( TMVA::Types::kCuts, "CutsD",
"!H:!V:FitMethod=MC:EffSel:SampleSize=200000:VarProp=FSmart:VarTransform=Decorrelate" );
示例5: TMVAKaggleHiggs
//.........这里部分代码省略.........
// // --- begin ----------------------------------------------------------
// std::vector<Double_t> vars( 4 ); // vector has size of number of input variables
// Float_t treevars[4], weight;
//
// // Signal
// for (UInt_t ivar=0; ivar<4; ivar++) signal->SetBranchAddress( Form( "var%i", ivar+1 ), &(treevars[ivar]) );
// for (UInt_t i=0; i<signal->GetEntries(); i++) {
// signal->GetEntry(i);
// for (UInt_t ivar=0; ivar<4; ivar++) vars[ivar] = treevars[ivar];
// // add training and test events; here: first half is training, second is testing
// // note that the weight can also be event-wise
// if (i < signal->GetEntries()/2.0) factory->AddSignalTrainingEvent( vars, signalWeight );
// else factory->AddSignalTestEvent ( vars, signalWeight );
// }
//
// // Background (has event weights)
// background->SetBranchAddress( "weight", &weight );
// for (UInt_t ivar=0; ivar<4; ivar++) background->SetBranchAddress( Form( "var%i", ivar+1 ), &(treevars[ivar]) );
// for (UInt_t i=0; i<background->GetEntries(); i++) {
// background->GetEntry(i);
// for (UInt_t ivar=0; ivar<4; ivar++) vars[ivar] = treevars[ivar];
// // add training and test events; here: first half is training, second is testing
// // note that the weight can also be event-wise
// if (i < background->GetEntries()/2) factory->AddBackgroundTrainingEvent( vars, backgroundWeight*weight );
// else factory->AddBackgroundTestEvent ( vars, backgroundWeight*weight );
// }
// --- end ------------------------------------------------------------
//
// --- end of tree registration
// Set individual event weights (the variables must exist in the original TTree)
// for signal : factory->SetSignalWeightExpression ("weight1*weight2");
// for background: factory->SetBackgroundWeightExpression("weight1*weight2");
factory->SetSignalWeightExpression( "Weight" );
factory->SetBackgroundWeightExpression( "Weight" );
// Apply additional cuts on the signal and background samples (can be different)
TCut mycuts = "Label==1"; // for example: TCut mycuts = "abs(var1)<0.5 && abs(var2-0.5)<1";
TCut mycutb = "Label==0"; // for example: TCut mycutb = "abs(var1)<0.5";
// Tell the factory how to use the training and testing events
//
// If no numbers of events are given, half of the events in the tree are used
// for training, and the other half for testing:
// factory->PrepareTrainingAndTestTree( mycut, "SplitMode=random:!V" );
// To also specify the number of testing events, use:
// factory->PrepareTrainingAndTestTree( mycut,
// "NSigTrain=3000:NBkgTrain=3000:NSigTest=3000:NBkgTest=3000:SplitMode=Random:!V" );
factory->PrepareTrainingAndTestTree( mycuts, mycutb,
"nTrain_Signal=0:nTrain_Background=0:nTest_Signal=0:nTest_Background=0:SplitMode=Random:NormMode=NumEvents:!V" );
// factory->PrepareTrainingAndTestTree( mycuts, mycutb,
// "nTrain_Signal=5000:nTrain_Background=5000:nTest_Signal=5000:nTest_Background=5000:SplitMode=Random:NormMode=NumEvents:!V" );
// ---- Book MVA methods
//
// Please lookup the various method configuration options in the corresponding cxx files, eg:
// src/MethoCuts.cxx, etc, or here: http://tmva.sourceforge.net/optionRef.html
// it is possible to preset ranges in the option string in which the cut optimisation should be done:
// "...:CutRangeMin[2]=-1:CutRangeMax[2]=1"...", where [2] is the third input variable
// Cut optimisation
if (Use["Cuts"])
factory->BookMethod( TMVA::Types::kCuts, "Cuts",
"!H:!V:FitMethod=MC:EffSel:SampleSize=200000:VarProp=FSmart" );
if (Use["CutsD"])
示例6: Reg
void Reg(){
TMVA::Tools::Instance();
std::cout << "==> Start TMVARegression" << std::endl;
ifstream myfile;
myfile.open("99per.txt");
ostringstream xcS,xcH,xcP,xcC,xcN;
double xS,xH,xC,xN,xP;
if(myfile.is_open()){
while(!myfile.eof()){
myfile>>xS>>xH>>xC>>xN>>xP;
}
}
xcS<<xS;
xcH<<xH;
xcC<<xC;
xcN<<xN;
xcP<<xP;
//Output file
TString outfileName( "Ex1out_FullW_def.root" );
TFile* outputFile = TFile::Open( outfileName, "RECREATE" );
//Declaring the factory
TMVA::Factory *factory = new TMVA::Factory( "TMVAClassification", outputFile,
"!V:!Silent:Color:DrawProgressBar" );
//Declaring Input Varibles
factory->AddVariable( "Sieie",'F');
factory->AddVariable( "ToE", 'F' );
factory->AddVariable( "isoC",'F' );
factory->AddVariable( "isoN",'F' );
factory->AddVariable( "isoP",'F' );
TString fname = "../../CutTMVATrees_Barrel.root";
input = TFile::Open( fname );
// --- Register the regression tree
TTree *signal = (TTree*)input->Get("t_S");
TTree *background = (TTree*)input->Get("t_B");
//Just Some more settings
Double_t signalWeight = 1.0;
Double_t backgroundWeight = 1.0;
// You can add an arbitrary number of regression trees
factory->AddSignalTree( signal, signalWeight );
factory->AddBackgroundTree( background , backgroundWeight );
TCut mycuts ="";
TCut mycutb ="";
// factory->PrepareTrainingAndTestTree(mycuts,mycutb,"nTrain_Signal=9000:nTrain_Background=9000:nTest_Signal=10000:nTest_Background=10000");
factory->SetBackgroundWeightExpression("weightPT*weightXS");
factory->SetSignalWeightExpression("weightPT*weightXS");
TString methodName = "Cuts_FullsampleW_def";
TString methodOptions ="!H:!V:FitMethod=GA:EffMethod=EffSEl";
methodOptions +=":VarProp[0]=FMin:VarProp[1]=FMin:VarProp[2]=FMin:VarProp[3]=FMin:VarProp[4]=FMin";
methodOptions +=":CutRangeMax[0]="+xcS.str();
methodOptions +=":CutRangeMax[1]="+xcH.str();
methodOptions +=":CutRangeMax[2]="+xcC.str();
methodOptions +=":CutRangeMax[3]="+xcN.str();
methodOptions +=":CutRangeMax[4]="+xcP.str();
//************
factory->BookMethod(TMVA::Types::kCuts,methodName,methodOptions);
factory->TrainAllMethods();
factory->TestAllMethods();
factory->EvaluateAllMethods();
// --------------------------------------------------------------
// Save the output
outputFile->Close();
std::cout << "==> Wrote root file: " << outputFile->GetName() << std::endl;
std::cout << "==> TMVARegression is done!" << std::endl;
delete factory;
}
示例7: tmvaClassifier
//.........这里部分代码省略.........
// --- Register the training and test trees
TChain *signal = new TChain("ewkzp2j");
TChain *background = new TChain("ewkzp2j");
TSystemDirectory dir(inputDir,inputDir);
TList *files = dir.GetListOfFiles();
if (files) {
TSystemFile *file;
TString fname;
TIter next(files);
while ((file=(TSystemFile*)next())) {
fname = file->GetName();
if(!fname.EndsWith("_summary.root")) continue;
if(fname.Contains("Data")) continue;
if(!fname.Contains("DY")) continue;
bool isSignal(false);
if(fname.Contains("JJ")) { signal->Add(fname); isSignal=true; }
else if(fname.Contains("50toInf") && fname.Contains("DY")) background->Add(fname);
cout << fname << " added as " << (isSignal ? "signal" : "background") << endl;
}
}else{
cout << "[Error] no files found in " << inputDir << endl;
}
cout << "Signal has " << signal->GetEntries() << " raw events" << endl
<< "Background has " << background->GetEntries() << " raw events"<< endl;
// global event weights per tree
Double_t signalWeight = 1.0;
Double_t backgroundWeight = 1.0;
factory->AddSignalTree ( signal, signalWeight );
factory->AddBackgroundTree( background, backgroundWeight );
// event-per-event weights per tree
factory->SetBackgroundWeightExpression( "weight/cnorm" );
factory->SetSignalWeightExpression( "weight/cnorm" );
//define variables for the training
if(minimalTrain)
{
factory->AddVariable( "mjj", "M_{jj}" "GeV", 'F' );
factory->AddVariable( "detajj", "#Delta#eta_{jj}", "", 'F' );
factory->AddVariable( "spt", "#Delta_{rel}", "GeV", 'F' );
}
else
{
factory->AddVariable( "mjj", "M_{jj}" "GeV", 'F' );
factory->AddVariable( "detajj", "#Delta#eta_{jj}", "", 'F' );
factory->AddVariable( "setajj", "#Sigma#eta_{j}", "", 'F' );
factory->AddVariable( "eta1", "#eta(1)", "", 'F' );
factory->AddVariable( "eta2", "#eta(2)", "", 'F' );
factory->AddVariable( "pt1", "p_{T}(1)", "GeV", 'F' );
factory->AddVariable( "pt2", "p_{T}(2)", "GeV", 'F' );
factory->AddVariable( "spt", "#Delta_{rel}", "GeV", 'F' );
if(useQG) factory->AddVariable( "qg1", "q/g(1)", "", 'F' );
if(useQG) factory->AddVariable( "qg2", "q/g(2)", "", 'F' );
}
// Apply additional cuts on the signal and background samples (can be different)
TCut mycuts = ""; // for example: TCut mycuts = "abs(var1)<0.5 && abs(var2-0.5)<1";
TCut mycutb = ""; // for example: TCut mycutb = "abs(var1)<0.5";
factory->PrepareTrainingAndTestTree( mycuts, mycutb,
"nTrain_Signal=0:nTrain_Background=0:SplitMode=Random:NormMode=NumEvents:!V" );
// ---- Book MVA methods
//
// Please lookup the various method configuration options in the corresponding cxx files, eg:
示例8: main
//.........这里部分代码省略.........
std::vector<TFile*> SignalSamples;
for(unsigned int sigIter=0;sigIter<signallist.size();++sigIter){
SignalSamples.push_back(TFile::Open((folder+signallist.at(sigIter)+"_mt_2012.root").c_str()));
}
std::vector<TTree*> backgroundTrees;
for(unsigned int iter2=0;iter2<BackgroundSamples.size();++iter2){
backgroundTrees.push_back(dynamic_cast<TTree*>(BackgroundSamples.at(iter2)->Get("ntuple")));
}
std::vector<TTree*> signalTrees;
for(unsigned int sigIter2=0;sigIter2<SignalSamples.size();++sigIter2){
signalTrees.push_back(dynamic_cast<TTree*>(SignalSamples.at(sigIter2)->Get("ntuple")));
}
TFile *outfile = new TFile((output_folder+output_name).c_str(),"RECREATE");
TMVA::Factory *factory = new TMVA::Factory(classname,outfile,"!V:!Silent:Color:DrawProgressBar:Transformations=I;D;P;G,D:AnalysisType=Classification");
std::vector<std::string> vars;
std::ifstream parafile(paramfile2.c_str());
std::cout<<paramfile2.c_str()<<std::endl;
string line;
while(getline(parafile,line)){
vars.push_back(line);
}
parafile.close();
std::cout<<(vars.at(0)).c_str()<<std::endl;
std::vector<float> var2;
for(unsigned int variter=0;variter<vars.size();++variter){
var2.push_back(::atof((vars.at(variter)).c_str()));
}
for(unsigned int variter=0;variter<vars.size();++variter){
factory->AddVariable((vars.at(variter)).c_str(),(vars.at(variter)).c_str(),"",'F');
}
factory->AddSpectator("mt_1","mt_1","",'F');
factory->AddSpectator("n_prebjets","n_prebjets","",'I');
factory->AddSpectator("prebjetbcsv_1","prebjetbcsv_1","",'F');
factory->AddSpectator("prebjetbcsv_2","prebjetbcsv_2","",'F');
double weightval_=0;
ParseParamFile(paramfile);
for(unsigned int bckgit=0;bckgit<backgroundTrees.size();++bckgit){
auto it = sample_info_.find(bckglist.at(bckgit).c_str());
if(it!=sample_info_.end()){
double evt = it->second.first;
double xs = it->second.second;
weightval_=(double) xs/evt;
std::cout<<weightval_<<std::endl;
}
factory->AddBackgroundTree(backgroundTrees.at(bckgit),weightval_);
}
for(unsigned int sgit=0;sgit<signalTrees.size();++sgit){
auto it = sample_info_.find(signallist.at(sgit).c_str());
if(it!=sample_info_.end()){
double evt = it->second.first;
double xs=it->second.second;
weightval_=(Double_t) xs/evt;
}
std::cout<<weightval_<<std::endl;
factory->AddSignalTree(signalTrees.at(sgit),weightval_);
}
factory->SetBackgroundWeightExpression("wt");
factory->SetSignalWeightExpression("wt");
TCut mycutb, mycuts;
if(twotag){
mycutb="n_prebjets>1&&mt_1<30&&prebjetbcsv_1>0.679&&prebjetbcsv_2>0.679";
mycuts="n_prebjets>1&&mt_1<30&&prebjetbcsv_1>0.679&&prebjetbcsv_2>0.679";
}
else if(onetag){
mycutb="n_prebjets>1&&mt_1<30&&prebjetbcsv_1>0.679&&prebjetbcsv_2<0.679";
mycuts="n_prebjets>1&&mt_1<30&&prebjetbcsv_1>0.679&&prebjetbcsv_2<0.679";
}
else{
mycutb="n_prebjets>1&&mt_1<30";
mycuts="n_prebjets>1&&mt_1<30";
}
//TCut mycutb="";
//TCut mycuts="";
factory->PrepareTrainingAndTestTree( mycuts, mycutb,"SplitMode=Random:!V");
factory->BookMethod( TMVA::Types::kBDT, "BDT","!H:!V:NTrees=850:nEventsMin=150:MaxDepth=3:BoostType=AdaBoost:AdaBoostBeta=0.5:SeparationType=GiniIndex:nCuts=20:PruneMethod=NoPruning" );
factory->TrainAllMethods();
factory->TestAllMethods();
factory->EvaluateAllMethods();
outfile->Close();
delete factory;
return 0;
}
示例9: test2
void test2(){
//---------------------------------------------------------------
// This loads the library
TMVA::Tools::Instance();
TString outfileName( "trainingBDT_tZq.root" );
TFile* outputFile = TFile::Open( outfileName, "RECREATE" );
TMVA::Factory *factory = new TMVA::Factory( "BDT_trainning_tzq", outputFile,"!V:!Silent:Color:DrawProgressBar:Transformations=I;D;P;G,D:AnalysisType=Classification" );
TFile *input_sig = TFile::Open( "../TreeReader/outputroot/histofile_tZq.root" );
TFile *input_wz = TFile::Open( "../TreeReader/outputroot/histofile_WZ.root" );
TTree *signal = (TTree*)input_sig->Get("Ttree_tZq");
TTree *background = (TTree*)input_wz->Get("Ttree_WZ");
factory->AddSignalTree ( signal, 1.);
factory->AddBackgroundTree( background, 1.);
std::vector<TString > varList;
varList.push_back("tree_cosThetaStar");;
varList.push_back("tree_topMass");
varList.push_back("tree_totMass");
varList.push_back("tree_deltaPhilb");
varList.push_back("tree_deltaRlb");
varList.push_back("tree_deltaRTopZ");
varList.push_back("tree_asym");
varList.push_back("tree_Zpt");
varList.push_back("tree_ZEta");
varList.push_back("tree_topPt");
varList.push_back("tree_topEta");
varList.push_back("tree_NJets");
varList.push_back("tree_NBJets");
varList.push_back("tree_deltaRZl");
varList.push_back("tree_deltaPhiZmet");
varList.push_back("tree_btagDiscri");
varList.push_back("tree_totPt");
varList.push_back("tree_totEta");
varList.push_back("tree_leptWPt");
varList.push_back("tree_leptWEta");
varList.push_back("tree_leadJetPt");
varList.push_back("tree_leadJetEta");
varList.push_back("tree_deltaRZleptW");
varList.push_back("tree_deltaPhiZleptW");
varList.push_back("tree_met" );
varList.push_back("tree_mTW" );
for(unsigned int i=0; i< varList.size() ; i++) factory->AddVariable( varList[i].Data(), 'F');
factory->SetSignalWeightExpression ("tree_EvtWeight");
factory->SetBackgroundWeightExpression("tree_EvtWeight");
// Apply additional cuts on the signal and background samples (can be different)
TCut mycuts = ""; // for example: TCut mycuts = "abs(var1)<0.5 && abs(var2-0.5)<1";
TCut mycutb = ""; // for example: TCut mycutb = "abs(var1)<0.5";
factory->PrepareTrainingAndTestTree( mycuts, mycutb,
"nTrain_Signal=0:nTrain_Background=0:SplitMode=Random:NormMode=NumEvents:!V" );
//factory->BookMethod( TMVA::Types::kBDT, "BDT", "!H:!V:NTrees=400:nEventsMin=400:MaxDepth=3:BoostType=AdaBoost:SeparationType=GiniIndex:nCuts=20:PruneMethod=NoPruning:VarTransform=Decorrelate" );
// factory->BookMethod( TMVA::Types::kBDT, "BDT", "!H:!V:NTrees=100:nEventsMin=100:MaxDepth=3:BoostType=AdaBoost:SeparationType=GiniIndex:nCuts=20:PruneMethod=NoPruning:VarTransform=Decorrelate" );
factory->BookMethod( TMVA::Types::kBDT, "BDT", "!H:!V:NTrees=100:nEventsMin=100:MaxDepth=3:BoostType=AdaBoost:SeparationType=GiniIndex:nCuts=20:PruneMethod=NoPruning:VarTransform=Decorrelate" );
// 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 );
//.........这里部分代码省略.........
示例10: TMVAClassification
//.........这里部分代码省略.........
for (UInt_t ivar=0; ivar<nvars-nvarsWithInt; ivar++) vars[ivar] = treevars[ivar];
for (UInt_t ivar=nvars-nvarsWithInt; ivar<nvars; ivar++) vars[ivar] = treevars2[ivar];
// add training and test events; here: first half is training, second is testing
// note that the weight can also be event-wise
//for(int ij=0; ij<nvars; ij++) cout << ij << " " << vars[ij] << endl;
if(isMC && (abs(vars[3])==4)) {
if (i%2==0) factory->AddSignalTrainingEvent( vars, weight );
else factory->AddSignalTestEvent ( vars, weight );
}
}
//
// // Background (has event weights)
background->SetBranchAddress( "weight", &weight );
for (UInt_t ivar=0; ivar<nvars-nvarsWithInt; ivar++) background->SetBranchAddress( variables[ivar].c_str(), &(treevars[ivar]) );
for (UInt_t ivar=nvars-nvarsWithInt; ivar<nvars; ivar++) background->SetBranchAddress( variables[ivar].c_str(), &(treevars2[ivar]) );
for (UInt_t i=0; i<background->GetEntries(); i++) {
background->GetEntry(i);
for (UInt_t ivar=0; ivar<nvars-nvarsWithInt; ivar++) vars[ivar] = treevars[ivar];
for (UInt_t ivar=nvars-nvarsWithInt; ivar<nvars; ivar++) vars[ivar] = treevars2[ivar];
// add training and test events; here: first half is training, second is testing
// note that the weight can also be event-wise
if(isMC && (abs(vars[3])==5)) {
if (i%2==0) factory->AddBackgroundTrainingEvent( vars, weight );
else factory->AddBackgroundTestEvent ( vars, weight );
}
}
// --- end ------------------------------------------------------------
//
// --- end of tree registration
// Set individual event weights (the variables must exist in the original TTree)
// for signal : factory->SetSignalWeightExpression ("weight1*weight2");
// for background: factory->SetBackgroundWeightExpression("weight1*weight2");
factory->SetSignalWeightExpression("weight");
factory->SetBackgroundWeightExpression( "weight" );
// Apply additional cuts on the signal and background samples (can be different)
TCut mycuts = ""; // for example: TCut mycuts = "abs(var1)<0.5 && abs(var2-0.5)<1";
TCut mycutb = ""; // for example: TCut mycutb = "abs(var1)<0.5";
// Tell the factory how to use the training and testing events
//
// If no numbers of events are given, half of the events in the tree are used
// for training, and the other half for testing:
// factory->PrepareTrainingAndTestTree( mycut, "SplitMode=random:!V" );
// To also specify the number of testing events, use:
// factory->PrepareTrainingAndTestTree( mycut,
// "NSigTrain=3000:NBkgTrain=3000:NSigTest=3000:NBkgTest=3000:SplitMode=Random:!V" );
factory->PrepareTrainingAndTestTree( mycuts, mycutb,
"nTrain_Signal=0:nTrain_Background=0:SplitMode=Random:NormMode=NumEvents:!V" );
// ---- Book MVA methods
//
// Please lookup the various method configuration options in the corresponding cxx files, eg:
// src/MethoCuts.cxx, etc, or here: http://tmva.sourceforge.net/optionRef.html
// it is possible to preset ranges in the option string in which the cut optimisation should be done:
// "...:CutRangeMin[2]=-1:CutRangeMax[2]=1"...", where [2] is the third input variable
// Cut optimisation
if (Use["Cuts"])
factory->BookMethod( TMVA::Types::kCuts, "Cuts",
"!H:!V:FitMethod=MC:EffSel:SampleSize=200000:VarProp=FSmart" );
if (Use["CutsD"])
factory->BookMethod( TMVA::Types::kCuts, "CutsD",
"!H:!V:FitMethod=MC:EffSel:SampleSize=200000:VarProp=FSmart:VarTransform=Decorrelate" );
示例11: TMVAClassification
//.........这里部分代码省略.........
// std::vector<Double_t> vars( 4 ); // vector has size of number of input variables
// Float_t treevars[4], weight;
//
// // Signal
// for (UInt_t ivar=0; ivar<4; ivar++) signal->SetBranchAddress( Form( "var%i", ivar+1 ), &(treevars[ivar]) );
// for (UInt_t i=0; i<signal->GetEntries(); i++) {
// signal->GetEntry(i);
// for (UInt_t ivar=0; ivar<4; ivar++) vars[ivar] = treevars[ivar];
// // add training and test events; here: first half is training, second is testing
// // note that the weight can also be event-wise
// if (i < signal->GetEntries()/2.0) factory->AddSignalTrainingEvent( vars, signalWeight );
// else factory->AddSignalTestEvent ( vars, signalWeight );
// }
//
// // Background (has event weights)
// background->SetBranchAddress( "weight", &weight );
// for (UInt_t ivar=0; ivar<4; ivar++) background->SetBranchAddress( Form( "var%i", ivar+1 ), &(treevars[ivar]) );
// for (UInt_t i=0; i<background->GetEntries(); i++) {
// background->GetEntry(i);
// for (UInt_t ivar=0; ivar<4; ivar++) vars[ivar] = treevars[ivar];
// // add training and test events; here: first half is training, second is testing
// // note that the weight can also be event-wise
// if (i < background->GetEntries()/2) factory->AddBackgroundTrainingEvent( vars, backgroundWeight*weight );
// else factory->AddBackgroundTestEvent ( vars, backgroundWeight*weight );
// }
// --- end ------------------------------------------------------------
//
// --- end of tree registration
// Set individual event weights (the variables must exist in the original TTree)
// for signal : factory->SetSignalWeightExpression ("weight1*weight2");
// for background: factory->SetBackgroundWeightExpression("weight1*weight2");
factory->SetBackgroundWeightExpression( "w" );
factory->SetSignalWeightExpression( "w");
// Apply additional cuts on the signal and background samples (can be different)
// for example: TCut mycuts = "abs(var1)<0.5 && abs(var2-0.5)<1";
TCut mycuts = "";
if(mode==1){ //Optimisation inside Z peak
mycuts = "(llType==0 || llType==1) && abs(mll-91.2)<10 && nCJets>=2 \
&& j_pt[0]>20 && j_isC20[0] && Alt$(j_pt[1],0) && Alt$(j_isC20[1],0)";
/* \&& mEff>250 && met/mEff>0.3"; */
}
else if(mode==2){
mycuts = "(llType==0 || llType==1) && abs(mll-91.2)<10 && nCJets>=2 \
&& j_pt[0]>20 && j_isC20[0] && Alt$(j_pt[1],0) && Alt$(j_isC20[1],0) \
&& mT2jj>60";
}
else if(mode==3){
mycuts = "llType==1 && nBJets==0 &&nFJets==0 ";
}
else if(mode==4){
mycuts = "llType==2";
}
/*
//Optimation close to diagonal
TCut mycuts = "(llType==0 || llType==1) && mll<50 && nCJets>=2 \
&& j_pt[0]>20 && j_isC20[0] && Alt$(j_pt[1],0) && Alt$(j_isC20[1],0)";
*/
// for example: TCut mycutb = "abs(var1)<0.5";
TCut mycutb = mycuts;
// Tell the factory how to use the training and testing events
示例12: tmstr
std::pair<TString,TString> TMVAClassification (
TString infilename,
AnalysisType analysisType = AnalysisType::DIRECT,
TString additionalRootFileName = "")
{
TMVA::Tools::Instance();
std::string tmstr (now ());
TString tmstmp (tmstr.c_str ());
std::cout << "==> Start TMVAClassification" << std::endl;
std::cout << "-------------------- open input file ---------------- " << std::endl;
TString fname = infilename; //pathToData + infilename + TString (".root");
if (analysisType != AnalysisType::TRANSFORMED)
fname = pathToData + infilename + TString (".root");
std::cout << "open file " << std::endl << fname.Data () << std::endl;
std::cout << "-------------------- get tree ---------------- " << std::endl;
TString treeName = "data";
if (analysisType == AnalysisType::TRANSFORMED)
treeName = "transformed";
std::cout << "-------------------- create tchain with treeName ---------------- " << std::endl;
std::cout << treeName << std::endl;
TChain* tree = new TChain (treeName);
std::cout << "add file" << std::endl;
std::cout << fname << std::endl;
tree->Add (fname);
TChain* treeFriend (NULL);
if (additionalRootFileName.Length () > 0)
{
std::cout << "-------------------- add additional input file ---------------- " << std::endl;
std::cout << additionalRootFileName << std::endl;
treeFriend = new TChain (treeName);
treeFriend->Add (additionalRootFileName);
tree->AddFriend (treeFriend,"p");
}
// tree->Draw ("mass:prediction");
// return std::make_pair(TString("hallo"),TString ("nix"));
TString outfileName;
if (analysisType == AnalysisType::BACKGROUND)
{
outfileName = TString ("BACK_" + infilename) + tmstmp + TString (".root");
}
else
outfileName += TString ( "TMVA__" ) + tmstmp + TString (".root");
std::cout << "-------------------- open output file ---------------- " << std::endl;
TFile* outputFile = TFile::Open( outfileName, "RECREATE" );
std::cout << "-------------------- prepare factory ---------------- " << std::endl;
TMVA::Factory *factory = new TMVA::Factory( "TMVAClassification", outputFile,
"AnalysisType=Classification:Transformations=I:!V" );
std::cout << "-------------------- add variables ---------------- " << std::endl;
for (auto varname : variableNames)
{
factory->AddVariable (varname.c_str (), 'F');
}
for (auto varname : spectatorNames)
{
factory->AddSpectator (varname.c_str (), 'F');
}
std::cout << "-------------------- add trees ---------------- " << std::endl;
TCut signalCut ("signal==1");
TCut backgroundCut ("signal==0");
if (analysisType == AnalysisType::TRANSFORMED)
{
signalCut = "(signal_original==1 && signal_in==0)";
backgroundCut = "(signal_original==0 && signal_in==0)";
}
if (analysisType == AnalysisType::BACKGROUND)
{
signalCut = TString("(signal==0) * (prediction > 0.7)");
backgroundCut = TString("(signal==0) * (prediction < 0.4)");
}
//tree->Draw ("prediction",signalCut);
//return std::make_pair(TString("hallo"),TString ("nix"));
factory->AddTree(tree, "Signal", 1.0, baseCut + signalCut, "TrainingTesting");
factory->AddTree(tree, "Background", 1.0, baseCut + backgroundCut, "TrainingTesting");
TCut mycuts = ""; // for example: TCut mycuts = "abs(var1)<0.5 && abs(var2-0.5)<1";
TCut mycutb = ""; // for example: TCut mycutb = "abs(var1)<0.5";
/* // Set individual event weights (the variables must exist in the original TTree) */
if (analysisType == AnalysisType::BACKGROUND)
{
factory->SetSignalWeightExpression ("prediction");
factory->SetBackgroundWeightExpression ("1");
}
//.........这里部分代码省略.........