コード例 #1
0
ファイル: main.cpp プロジェクト: petitcactusorange/BAE
int reader_wrapper::bookReader( TString xml_file_name) {
  m_reader = new TMVA::Reader("!Color:Silent");
  for (auto var : m_spectators) {
    m_reader->AddSpectator(var.formula, &var.value);
  }
  for (auto& var : m_variables) {
    m_reader->AddVariable(var.formula, &var.value);
  }
  m_reader->BookMVA( m_methodName, xml_file_name );
  return 0;
}
コード例 #2
0
void makeclassification() {
  
  Float_t *vars = new Float_t[10];
  
  //initialize TMVA Reader (example here is diphoton mva from higgs->gamma gamma mva analysis)
  TMVA::Reader* tmva = new TMVA::Reader();
  tmva->AddVariable("masserrsmeared/mass",            &vars[0]);
  tmva->AddVariable("masserrsmearedwrongvtx/mass",    &vars[1]);
  tmva->AddVariable("vtxprob",                        &vars[2]);
  tmva->AddVariable("ph1.pt/mass",                    &vars[3]);
  tmva->AddVariable("ph2.pt/mass",                    &vars[4]);
  tmva->AddVariable("ph1.eta",                        &vars[5]);
  tmva->AddVariable("ph2.eta",                        &vars[6]);
  tmva->AddVariable("TMath::Cos(ph1.phi-ph2.phi)"   , &vars[7]);
  tmva->AddVariable("ph1.idmva",                      &vars[8]);
  tmva->AddVariable("ph2.idmva",                      &vars[9]);
  
  tmva->BookMVA("BDTG","/afs/cern.ch/user/b/bendavid/cmspublic/diphotonmvaApr1/HggBambu_SMDipho_Jan16_BDTG.weights.xml");
  //tmva->BookMVA("BDTG","/scratch/bendavid/root/HggBambu_SMDipho_Jan16_BDTG.weights.xml");
  
  TMVA::MethodBDT *bdt = dynamic_cast<TMVA::MethodBDT*>(tmva->FindMVA("BDTG"));

 
  //enable root i/o for objects with reflex dictionaries in standalone root mode
  ROOT::Cintex::Cintex::Enable();   

  
  //open output root file
  TFile *fout = new TFile("gbrtest.root","RECREATE");
  
  //create GBRForest from tmva object
  GBRForest *gbr = new GBRForest(bdt);  
  
  //write to file
  fout->WriteObject(gbr,"gbrtest");

  fout->Close();
  
  
}
コード例 #3
0
ファイル: tmva.c プロジェクト: dbarge/tauTo3mu
void TMVAPredict()
{
  std::ofstream outfile ("baseline_c.csv");
  outfile << "id,prediction\n";

  TMVA::Tools::Instance();

  std::cout << "==> Start TMVAPredict" << std::endl;
  TMVA::Reader *reader = new TMVA::Reader( "!Color:!Silent" );  
  string variables_name[3] = {"LifeTime",
                           "FlightDistance",
                           "pt"}
  Float_t variables[3];
  for (int i=0; i < 3; i++){
    reader->AddVariable(variables_name[i].c_str(), &variables[i]);
    variables[i] = 0.0;
  }

  TString dir    = "weights/";
  TString prefix = "TMVAClassification";
  TString method_name = "GBDT";
  TString weightfile = dir + prefix + TString("_") + method_name + TString(".weights.xml");
  reader->BookMVA( method_name, weightfile ); 

  TFile *input(0);
  input = TFile::Open("../tau_data/test.root");
  TTree* tree = (TTree*)input->Get("data");
  
  Int_t ids;
  Float_t prediction;
  tree->SetBranchAddress("id", &ids);

  for (int i=0; i < 3; i++){
    tree->SetBranchAddress(variables_name[i].c_str(), &variables[i]);
  }
 
  for (Long64_t ievt=0; ievt < tree->GetEntries(); ievt++) {
    tree->GetEntry(ievt);
    prediction = reader->EvaluateMVA(method_name);
    outfile << ids << "," << (prediction + 1.) / 2. << "\n";
  }

  outfile.close();
  input->Close();
  delete reader;
}
コード例 #4
0
void TMVAClassificationApplication_tW(TString signal = "data") 
{   
#ifdef __CINT__
  gROOT->ProcessLine( ".O0" ); // turn off optimization in CINT
#endif
  
  //---------------------------------------------------------------
  // This loads the library
  TMVA::Tools::Instance();
  // --------------------------------------------------------------------------------------------------
  
  // --- Create the Reader object
  
  TMVA::Reader *reader = new TMVA::Reader( "!Color:!Silent" );    
  
  // Create a set of variables and declare them to the reader
  // - the variable names MUST corresponds in name and type to those given in the weight file(s) used
  
  Float_t jetpt;
  Float_t jeteta;
  Float_t jetphi;
  Float_t metpt;
  Float_t metpro;
  Float_t lep0pt;
  Float_t lep1pt;
  Float_t lep0eta;
  Float_t lep1eta;
  Float_t lep0phi;
  Float_t lep1phi;
  Float_t ptsys;
  Float_t ht;
  Float_t oblateness;
  Float_t sphericity;
  Float_t aplanarity;
  Float_t njetw;
  Float_t sqrts;
  Float_t deltarleps;
  Float_t deltaphileps;
  Float_t deltaetaleps;
  Float_t philepmetclose;
  Float_t philepmetfar;
  Float_t rlepmetclose;
  Float_t rlepmetfar;
  Float_t philepjetclose;
  Float_t philepjetfar;
  Float_t rlepjetclose;
  Float_t rlepjetfar;
  Float_t phijetmet;
  Float_t rjetmet;
  Float_t mll;
  Float_t htnomet;
  Float_t ptsysnomet;
  Float_t metphi;
  Float_t metminusptsysnomet;
  
  reader->AddVariable ("jetpt", &jetpt);
  reader->AddVariable ("jeteta", &jeteta);
  reader->AddVariable ("jetphi", &jetphi);
  reader->AddVariable ("metpt", &metpt);
  reader->AddVariable ("metpro",&metpro);
  reader->AddVariable ("lep0pt",&lep0pt);
  reader->AddVariable ("lep1pt",&lep1pt);
  reader->AddVariable ("lep0eta",&lep0eta);
  reader->AddVariable ("lep1eta",&lep1eta);
  reader->AddVariable ("lep0phi",&lep0phi);
  reader->AddVariable ("lep1phi",&lep1phi);
  reader->AddVariable ("ptsys",&ptsys);
  reader->AddVariable ("ht",&ht);
  reader->AddVariable ("oblateness", &oblateness);
  reader->AddVariable ("sphericity", &sphericity);
  reader->AddVariable ("aplanarity", &aplanarity);
  reader->AddVariable ("njetw", &njetw);
  reader->AddVariable ("sqrts", &sqrts);
  reader->AddVariable ("deltarleps", &deltarleps);
  reader->AddVariable ("deltaphileps", &deltaphileps);
  reader->AddVariable ("deltaetaleps", &deltaetaleps);
  reader->AddVariable ("philepmetclose", &philepmetclose);
  reader->AddVariable ("philepmetfar", &philepmetfar);
  reader->AddVariable ("rlepmetclose", &rlepmetclose);
  reader->AddVariable ("rlepmetfar", &rlepmetfar);
  reader->AddVariable ("philepjetclose", &philepjetclose);
  reader->AddVariable ("philepjetfar", &philepjetfar);
  reader->AddVariable ("rlepjetclose", &rlepjetclose);
  reader->AddVariable ("rlepjetfar", &rlepjetfar);
  reader->AddVariable ("phijetmet", &phijetmet);
  reader->AddVariable ("rjetmet", &rjetmet);
  reader->AddVariable ("mll", &mll);
  reader->AddVariable ("htnomet", &htnomet);
  reader->AddVariable ("ptsysnomet", &ptsysnomet);
  reader->AddVariable ("metphi", &metphi);
  reader->AddVariable ("metminusptsysnomet", &metminusptsysnomet);
  
  // *************************************************
  
  // --- Book the MVA methods
  
  TString dir    = "weights/";
  
  TString prefix = "test_tw_00";
  TString name = "BDT_"+prefix;
  std::cout<<"********* name = "<<name<<std::endl;
  
  //
  // book the MVA methods
  //
  
  reader->BookMVA( "BDT method", dir + prefix + "_BDT.weights.xml" );   
  
  // book output histograms
  Int_t nbin = 100;
  histBdt   = new TH1F( "MVA_BDT",           "MVA_BDT",           nbin, -1.0, 1.0 );
  
  // book example histogram for probability (the other methods are done similarly)
  probHistBDT   = new TH1F( "Probability_MVA_BDT", "Probability_MVA_BDT", nbin, -1, 1);
  rarityHistBDT = new TH1F( "Rarity_MVA_BDT", "Rarity_MVA_BDT", nbin, -1, 1);
  
  // Prepare input tree (this must be replaced by your data source)
  // in this example, there is a toy tree with signal and one with background events
  // we'll later on use only the "signal" events for the test in this example.
  //   
  TFile *input(0);
  
  TString folder = "rootfiles/";
  
  if(signal == "data") { TString fname = folder+"tmva_test_0_data.root"; }
  
  if(signal == "tw"){ TString fname = folder+"tmva_test_0_twdr.root"; }
  if(signal == "ww"){ TString fname = folder+"tmva_test_0_ww.root"; }
  if(signal == "qcd"){ TString fname = folder+"tmva_test_0_qcd_mu.root"; }
  if(signal == "wz"){ TString fname = folder+"tmva_test_0_wz.root"; }
  if(signal == "zz"){ TString fname = folder+"tmva_test_0_zz.root"; }
  if(signal == "st"){ TString fname = folder+"tmva_test_0_st.root"; }
  if(signal == "tt"){ TString fname = folder+"tmva_test_0_tt.root"; }
  if(signal == "wjets"){ TString fname = folder+"tmva_test_0_wjets.root"; }
  if(signal == "zjets"){ TString fname = folder+"tmva_test_0_zjets.root"; }
  if(signal == "di"){TString fname = folder + "tmva_test_0_di.root";}
  
  input = TFile::Open( fname,"UPDATE");   
  
  if (!input) {
    cout << "ERROR: could not open data file: " << fname << endl;
    exit(1);
  }
  
  // --- Event loop
  
  // Prepare the event tree
  // - here the variable names have to corresponds to your tree
  // - you can use the same variables as above which is slightly faster,
  //   but of course you can use different ones and copy the values inside the event loop
  //
  
  input->cd();
  TTree* theTree = (TTree*)input->Get("myTree");
  cout << "--- Select signal sample" << endl;
  
  double userjetpt;
  double userjeteta;
  double userjetphi;
  double usermetpt;
  double usermetpro;
  double userlep0pt;
  double userlep1pt;
  double userlep0eta;
  double userlep1eta;
  double userlep0phi;
  double userlep1phi;
  double userptsys;
  double userht;
  double useroblateness;
  double usersphericity;
  double useraplanarity;
  double usernjetw;
  double usersqrts;
  double userdeltarleps;
  double userdeltaphileps;
  double userdeltaetaleps;
  double userphilepmetclose;
  double userphilepmetfar;
  double userrlepmetclose;
  double userrlepmetfar;
  double userphilepjetclose;
  double userphilepjetfar;
  double userrlepjetclose;
  double userrlepjetfar;
  double userphijetmet;
  double userrjetmet;
  double userweight;
  double userweightnopu;
  double usermll;
  double userhtnomet;
  double userptsysnomet;
  double usermetphi;
  double usermetminusptsysnomet;
  
  theTree->SetBranchAddress ("savetheweight", &userweight);
  theTree->SetBranchAddress ("weightnopu", &userweightnopu);
  theTree->SetBranchAddress ("jetpt", &userjetpt);
  theTree->SetBranchAddress ("jeteta", &userjeteta);
  theTree->SetBranchAddress ("jetphi", &userjetphi);
  theTree->SetBranchAddress ("metpt", &usermetpt);
  theTree->SetBranchAddress ("metpro",&usermetpro);
  theTree->SetBranchAddress ("lep0pt",&userlep0pt);
  theTree->SetBranchAddress ("lep1pt",&userlep1pt);
  theTree->SetBranchAddress ("lep0eta",&userlep0eta);
  theTree->SetBranchAddress ("lep1eta",&userlep1eta);
  theTree->SetBranchAddress ("lep0phi",&userlep0phi);
  theTree->SetBranchAddress ("lep1phi",&userlep1phi);
  theTree->SetBranchAddress ("ptsys",&userptsys);
  theTree->SetBranchAddress ("ht",&userht);
  theTree->SetBranchAddress ("oblateness", &useroblateness);
  theTree->SetBranchAddress ("sphericity", &usersphericity);
  theTree->SetBranchAddress ("aplanarity", &useraplanarity);
  theTree->SetBranchAddress ("njetw", &usernjetw);
  theTree->SetBranchAddress ("sqrts", &usersqrts);
  theTree->SetBranchAddress ("deltarleps", &userdeltarleps);
  theTree->SetBranchAddress ("deltaphileps", &userdeltaphileps);
  theTree->SetBranchAddress ("deltaetaleps", &userdeltaetaleps);
  theTree->SetBranchAddress ("philepmetclose", &userphilepmetclose);
  theTree->SetBranchAddress ("philepmetfar", &userphilepmetfar);
  theTree->SetBranchAddress ("rlepmetclose", &userrlepmetclose);
  theTree->SetBranchAddress ("rlepmetfar", &userrlepmetfar);
  theTree->SetBranchAddress ("philepjetclose", &userphilepjetclose);
  theTree->SetBranchAddress ("philepjetfar", &userphilepjetfar);
  theTree->SetBranchAddress ("rlepjetclose", &userrlepjetclose);
  theTree->SetBranchAddress ("rlepjetfar", &userrlepjetfar);
  theTree->SetBranchAddress ("phijetmet", &userphijetmet);
  theTree->SetBranchAddress ("rjetmet", &userrjetmet);
  theTree->SetBranchAddress ("mll", &usermll);
  theTree->SetBranchAddress ("htnomet", &userhtnomet);
  theTree->SetBranchAddress ("ptsysnomet", &userptsysnomet);
  theTree->SetBranchAddress ("metphi", &usermetphi);
  theTree->SetBranchAddress ("metminusptsysnomet", &usermetminusptsysnomet);
  
  Double_t tBDT;    
  TTree* BDTTree = new TTree(name,"");
  BDTTree->Branch("BDT",&tBDT,"BDT/D");
  
  double tweight = 1;
    
  std::cout<<" ... opening file : "<<fname<<std::endl;
  cout << "--- Processing: " << theTree->GetEntries() << " events" << endl;
  
  TH1F *hBDT = new TH1F("hBDT","",100,-1.0,1.0);
  
  TStopwatch sw;
  sw.Start();
  for (Long64_t ievt=0; ievt<theTree->GetEntries();ievt++) {
    
    if (ievt%1000 == 0) cout << "--- ... Processing event: " << ievt << endl;
    
    theTree->GetEntry(ievt);
    
    tweight = userweight;
    
    jetpt = userjetpt;
    jeteta = userjeteta;
    jetphi = userjetphi;
    metpt = usermetpt;
    metpro = usermetpro;
    lep0pt = userlep0pt;
    lep1pt = userlep1pt;
    lep0eta = userlep0eta;
    lep1eta = userlep1eta;
    lep0phi = userlep0phi;
    lep1phi = userlep1phi;
    ptsys = userptsys;
    ht = userht;
    oblateness = useroblateness;
    sphericity = usersphericity;
    aplanarity = useraplanarity;
    njetw = usernjetw;
    sqrts = usersqrts;
    deltarleps = userdeltarleps;
    deltaphileps = userdeltaphileps;
    deltaetaleps = userdeltaetaleps;
    philepmetclose = userphilepmetclose;
    philepmetfar = userphilepmetfar;
    rlepmetclose = userrlepmetclose;
    rlepmetfar = userrlepmetfar;
    philepjetclose = userphilepjetclose;
    philepjetfar = userphilepjetfar;
    rlepjetclose = userrlepjetclose;
    rlepjetfar = userrlepjetfar;
    phijetmet = userphijetmet;
    rjetmet = userrjetmet;
    mll = usermll;
    htnomet = userhtnomet;
    ptsysnomet = userptsysnomet;
    metphi = usermetphi;
    metminusptsysnomet = usermetminusptsysnomet;
    
    double bdt         = reader->EvaluateMVA("BDT method");
    
    tBDT = bdt;
    BDTTree->Fill();
    
    
    if (signal == "data") tweight = 1;
    
    hBDT->Fill(bdt,tweight);
    
    double bdt_     = reader->EvaluateMVA("BDT method");
    histBdt   ->Fill(bdt_);	
  
  }

  // Get elapsed time
  sw.Stop();
  std::cout << "--- End of event loop: "; sw.Print();
  
  input->cd();
  hBDT->Write();
  BDTTree->Write();
  
  input->Close();
  
  
  // --- Write histograms
  
  TFile *target  = new TFile( "TMVApp.root","RECREATE" );
  
  histBdt    ->Write();
  
  std::cout << "--- Created root file: \"TMVApp.root\" containing the MVA output histograms" << std::endl;
  
  
  delete reader;
  
  std::cout << "==> TMVAClassificationApplication is done!" << endl << std::endl;
} 
コード例 #5
0
ファイル: apply.C プロジェクト: martinamalberti/ProjectPUPPI
void apply(std::string iName="train/OutputTmp.root") { 
  TMVA::Tools::Instance();
  TMVA::Reader *reader = new TMVA::Reader( "!Color:!Silent" );    

  float lPt        = 0; reader->AddVariable("pt"                 , &lPt);
  //float lEta       = 0; reader->AddVariable("eta"                , &lEta);
  //float lDR        = 0; reader->AddVariable("dR"                 , &lDR);
  //float lPtc       = 0; reader->AddVariable("ptc"                , &lPtc);
  // float lPtdR      = 0; reader->AddVariable("ptdR"               , &lPtdR);
  //float lPuppi     = 0; reader->AddVariable("puppi"              , &lPuppi);
  float lPtODR     = 0; reader->AddVariable("ptodR"              , &lPtODR);
  //float lPtODRS    = 0; reader->AddVariable("ptodRS"             , &lPtODRS);
  float lPtODRSO   = 0; reader->AddVariable("ptodRSO"            , &lPtODRSO);
  //float lDRLV      = 0; reader->AddVariable("dR_lv"              , &lDRLV);
  //float lPtcLV     = 0; reader->AddVariable("ptc_lv"             , &lPtcLV);
  //float lPtdRLV    = 0; reader->AddVariable("ptdR_lv"            , &lPtdRLV);
  //float lPuppiLV   = 0; reader->AddVariable("puppi_lv"           , &lPuppiLV);
  float lPtODRLV   = 0; reader->AddVariable("ptodR_lv"           , &lPtODRLV);
  //float lPtODRSLV  = 0; reader->AddVariable("ptodRS_lv"          , &lPtODRSLV);
  float lPtODRSOLV = 0; reader->AddVariable("ptodRSO_lv"         , &lPtODRSOLV);
  //float lDRPU      = 0; reader->AddVariable("dR_pu"              , &lDRPU);
  //float lPtcPU     = 0; reader->AddVariable("pt_pu"              , &lPtcPU);
  //float lPtdRPU    = 0; reader->AddVariable("ptdR_pu"            , &lPtdRPU);
  //float lPuppiPU   = 0; reader->AddVariable("puppi_pu"           , &lPuppiPU);
  //float lPtODRPU   = 0; reader->AddVariable("ptodR_pu"           , &lPtODRPU);
  //float lPtODRSPU  = 0; reader->AddVariable("ptodRS_pu"          , &lPtODRSPU);
  //float lPtODRSOPU = 0; reader->AddVariable("ptodRSO_pu"         , &lPtODRSOPU);
  
  std::string lJetName = "BDT";
  reader->BookMVA(lJetName .c_str(),(std::string("weights/TMVAClassificationCategory_PUDisc_v1")+std::string(".weights.xml")).c_str());
  
  TFile *lFile = new TFile(iName.c_str());
  TTree *lTree = (TTree*) lFile->Get("tree");
   lTree->SetBranchAddress("pt"                 , &lPt);
  //lTree->SetBranchAddress("eta"                , &lEta);
   //lTree->SetBranchAddress("dR"                 , &lDR);
   //lTree->SetBranchAddress("ptc"                , &lPtc);
   //lTree->SetBranchAddress("ptdR"               , &lPtdR);
  //lTree->SetBranchAddress("puppi"              , &lPuppi);
  lTree->SetBranchAddress("ptodR"              , &lPtODR);
  //lTree->SetBranchAddress("ptodRS"             , &lPtODRS);
  lTree->SetBranchAddress("ptodRSO"            , &lPtODRSO);
  //lTree->SetBranchAddress("dR_lv"              , &lDRLV);
  // lTree->SetBranchAddress("ptc_lv"             , &lPtcLV);
  //lTree->SetBranchAddress("ptdR_lv"            , &lPtdRLV);
  //lTree->SetBranchAddress("puppi_lv"           , &lPuppiLV);
  lTree->SetBranchAddress("ptodR_lv"           , &lPtODRLV);
  //lTree->SetBranchAddress("ptodRS_lv"          , &lPtODRSLV);
  lTree->SetBranchAddress("ptodRSO_lv"         , &lPtODRSOLV);
  //lTree->SetBranchAddress("dR_pu"              , &lDRPU);
  //lTree->SetBranchAddress("pt_pu"              , &lPtcPU);
  //lTree->SetBranchAddress("ptdR_pu"            , &lPtdRPU);
  //lTree->SetBranchAddress("puppi_pu"           , &lPuppiPU);
  //lTree->SetBranchAddress("ptodR_pu"           , &lPtODRPU);
  //lTree->SetBranchAddress("ptodRS_pu"          , &lPtODRSPU);
  //lTree->SetBranchAddress("ptodRSO_pu"         , &lPtODRSOPU);
    
  int lNEvents = lTree->GetEntries();
  TFile *lOFile = new TFile("Output.root","RECREATE");
  TTree *lOTree = lTree->CloneTree(0);
  float lMVA    = 0; lOTree->Branch("bdt"     ,&lMVA ,"lMVA/F");
  for (Long64_t i0=0; i0<lNEvents;i0++) {
    if (i0 % 10000 == 0) std::cout << "--- ... Processing event: " << double(i0)/double(lNEvents) << std::endl;
    lTree->GetEntry(i0);
    lMVA      = float(reader->EvaluateMVA(lJetName.c_str()));
    lOTree->Fill();
  }
  lOTree->Write();
  lOFile->Close();
  delete reader;
}
コード例 #6
0
void TMVAReader(){
#ifdef __CINT__
    gROOT->ProcessLine( ".O0" ); // turn off optimization in CINT
#endif

    TMVA::Tools::Instance(); // loads libraries

    //######################################
    // Fat jet variables
    //######################################
    float Jet_pt;
    float Jet_eta;
    float Jet_phi;
    float Jet_mass;
    float Jet_massGroomed;
    float Jet_flavour;
    float Jet_nbHadrons;
    float Jet_JP;
    float Jet_JBP;
    float Jet_CSV;
    float Jet_CSVIVF;
    float Jet_tau1;
    float Jet_tau2;

    // CSV TaggingVariables
    // per jet
    float TagVarCSV_jetNTracks;                           // tracks associated to jet
    float TagVarCSV_jetNTracksEtaRel;                     // tracks associated to jet for which trackEtaRel is calculated
    float TagVarCSV_trackSumJetEtRatio;                   // ratio of track sum transverse energy over jet energy
    float TagVarCSV_trackSumJetDeltaR;                    // pseudoangular distance between jet axis and track fourvector sum
    float TagVarCSV_trackSip2dValAboveCharm;              // track 2D signed impact parameter of first track lifting mass above charm
    float TagVarCSV_trackSip2dSigAboveCharm;              // track 2D signed impact parameter significance of first track lifting mass above charm
    float TagVarCSV_trackSip3dValAboveCharm;              // track 3D signed impact parameter of first track lifting mass above charm
    float TagVarCSV_trackSip3dSigAboveCharm;              // track 3D signed impact parameter significance of first track lifting mass above charm
    float TagVarCSV_vertexCategory;                       // category of secondary vertex (Reco, Pseudo, No)
    float TagVarCSV_jetNSecondaryVertices;                // number of reconstructed possible secondary vertices in jet
    float TagVarCSV_vertexMass;                           // mass of track sum at secondary vertex
    float TagVarCSV_vertexNTracks;                        // number of tracks at secondary vertex
    float TagVarCSV_vertexEnergyRatio;                    // ratio of energy at secondary vertex over total energy
    float TagVarCSV_vertexJetDeltaR;                      // pseudoangular distance between jet axis and secondary vertex direction
    float TagVarCSV_flightDistance2dVal;                  // transverse distance between primary and secondary vertex
    float TagVarCSV_flightDistance2dSig;                  // transverse distance significance between primary and secondary vertex
    float TagVarCSV_flightDistance3dVal;                  // distance between primary and secondary vertex
    float TagVarCSV_flightDistance3dSig;                  // distance significance between primary and secondary vertex
    // per jet per track
    float TagVarCSV_trackSip2dSig_0;                      // highest track 2D signed IP of tracks belonging to a given jet   
    float TagVarCSV_trackSip2dSig_1;                      // second highest track 2D signed IP of tracks belonging to a given jet
    float TagVarCSV_trackSip2dSig_2;                      // third highest track 2D signed IP of tracks belonging to a given jet
    float TagVarCSV_trackSip2dSig_3;                      // fourth highest track 2D signed IP of tracks belonging to a given jet
    float TagVarCSV_trackSip2dSig_4;                      // fifth highest track 2D signed IP of tracks belonging to a given jet
    float TagVarCSV_trackSip2dSig_5;                      // sixth highest track 2D signed IP of tracks belonging to a given jet
    float TagVarCSV_trackSip3dSig_0;                      // highest track 3D signed IP of tracks belonging to a given jet   
    float TagVarCSV_trackSip3dSig_1;                      // second highest track 3D signed IP of tracks belonging to a given jet
    float TagVarCSV_trackSip3dSig_2;                      // third highest track 3D signed IP of tracks belonging to a given jet
    float TagVarCSV_trackSip3dSig_3;                      // fourth highest track 3D signed IP of tracks belonging to a given jet
    float TagVarCSV_trackSip3dSig_4;                      // fifth highest track 3D signed IP of tracks belonging to a given jet
    float TagVarCSV_trackSip3dSig_5;                      // sixth highest track 3D signed IP of tracks belonging to a given jet
    float TagVarCSV_trackPtRel_0;                         // highest track transverse momentum, relative to the jet axis
    float TagVarCSV_trackPtRel_1;                         // second highest track transverse momentum, relative to the jet axis
    float TagVarCSV_trackPtRel_2;                         // third highest track transverse momentum, relative to the jet axis
    float TagVarCSV_trackPtRel_3;                         // fourth highest track transverse momentum, relative to the jet axis
    float TagVarCSV_trackPtRel_4;                         // fifth highest track transverse momentum, relative to the jet axis
    float TagVarCSV_trackPtRel_5;                         // sixth highest track transverse momentum, relative to the jet axis
    // per jet per etaRel track
    float TagVarCSV_trackEtaRel_0;                        // lowest track eta relative to jet axis
    float TagVarCSV_trackEtaRel_1;                        // second lowest track eta relative to jet axis
    float TagVarCSV_trackEtaRel_2;                        // third lowest track eta relative to jet axis

    //######################################
    // Subjet1 variables
    //######################################
    float SubJet1_pt;
    float SubJet1_eta;
    float SubJet1_phi;
    float SubJet1_mass;
    float SubJet1_flavour;
    float SubJet1_nbHadrons;
    float SubJet1_JP;
    float SubJet1_JBP;
    float SubJet1_CSV;
    float SubJet1_CSVIVF;

    // CSV TaggingVariables
    // per jet
    float TagVarCSV1_jetNTracks;                           // tracks associated to jet
    float TagVarCSV1_jetNTracksEtaRel;                     // tracks associated to jet for which trackEtaRel is calculated
    float TagVarCSV1_trackSumJetEtRatio;                   // ratio of track sum transverse energy over jet energy
    float TagVarCSV1_trackSumJetDeltaR;                    // pseudoangular distance between jet axis and track fourvector sum
    float TagVarCSV1_trackSip2dValAboveCharm;              // track 2D signed impact parameter of first track lifting mass above charm
    float TagVarCSV1_trackSip2dSigAboveCharm;              // track 2D signed impact parameter significance of first track lifting mass above charm
    float TagVarCSV1_trackSip3dValAboveCharm;              // track 3D signed impact parameter of first track lifting mass above charm
    float TagVarCSV1_trackSip3dSigAboveCharm;              // track 3D signed impact parameter significance of first track lifting mass above charm
    float TagVarCSV1_vertexCategory;                       // category of secondary vertex (Reco, Pseudo, No)
    float TagVarCSV1_jetNSecondaryVertices;                // number of reconstructed possible secondary vertices in jet
    float TagVarCSV1_vertexMass;                           // mass of track sum at secondary vertex
    float TagVarCSV1_vertexNTracks;                        // number of tracks at secondary vertex
    float TagVarCSV1_vertexEnergyRatio;                    // ratio of energy at secondary vertex over total energy
    float TagVarCSV1_vertexJetDeltaR;                      // pseudoangular distance between jet axis and secondary vertex direction
    float TagVarCSV1_flightDistance2dVal;                  // transverse distance between primary and secondary vertex
    float TagVarCSV1_flightDistance2dSig;                  // transverse distance significance between primary and secondary vertex
    float TagVarCSV1_flightDistance3dVal;                  // distance between primary and secondary vertex
    float TagVarCSV1_flightDistance3dSig;                  // distance significance between primary and secondary vertex
    // per jet per etaRel track
    float TagVarCSV1_trackEtaRel_0;                        // lowest track eta relative to jet axis
    float TagVarCSV1_trackEtaRel_1;                        // second lowest track eta relative to jet axis
    float TagVarCSV1_trackEtaRel_2;                        // third lowest track eta relative to jet axis

    //######################################
    // Subjet2 variables
    //######################################
    float SubJet2_pt;
    float SubJet2_eta;
    float SubJet2_phi;
    float SubJet2_mass;
    float SubJet2_flavour;
    float SubJet2_nbHadrons;
    float SubJet2_JP;
    float SubJet2_JBP;
    float SubJet2_CSV;
    float SubJet2_CSVIVF;

    // CSV TaggingVariables
    // per jet
    float TagVarCSV2_jetNTracks;                           // tracks associated to jet
    float TagVarCSV2_jetNTracksEtaRel;                     // tracks associated to jet for which trackEtaRel is calculated
    float TagVarCSV2_trackSumJetEtRatio;                   // ratio of track sum transverse energy over jet energy
    float TagVarCSV2_trackSumJetDeltaR;                    // pseudoangular distance between jet axis and track fourvector sum
    float TagVarCSV2_trackSip2dValAboveCharm;              // track 2D signed impact parameter of first track lifting mass above charm
    float TagVarCSV2_trackSip2dSigAboveCharm;              // track 2D signed impact parameter significance of first track lifting mass above charm
    float TagVarCSV2_trackSip3dValAboveCharm;              // track 3D signed impact parameter of first track lifting mass above charm
    float TagVarCSV2_trackSip3dSigAboveCharm;              // track 3D signed impact parameter significance of first track lifting mass above charm
    float TagVarCSV2_vertexCategory;                       // category of secondary vertex (Reco, Pseudo, No)
    float TagVarCSV2_jetNSecondaryVertices;                // number of reconstructed possible secondary vertices in jet
    float TagVarCSV2_vertexMass;                           // mass of track sum at secondary vertex
    float TagVarCSV2_vertexNTracks;                        // number of tracks at secondary vertex
    float TagVarCSV2_vertexEnergyRatio;                    // ratio of energy at secondary vertex over total energy
    float TagVarCSV2_vertexJetDeltaR;                      // pseudoangular distance between jet axis and secondary vertex direction
    float TagVarCSV2_flightDistance2dVal;                  // transverse distance between primary and secondary vertex
    float TagVarCSV2_flightDistance2dSig;                  // transverse distance significance between primary and secondary vertex
    float TagVarCSV2_flightDistance3dVal;                  // distance between primary and secondary vertex
    float TagVarCSV2_flightDistance3dSig;                  // distance significance between primary and secondary vertex
    // per jet per etaRel track
    float TagVarCSV2_trackEtaRel_0;                        // lowest track eta relative to jet axis
    float TagVarCSV2_trackEtaRel_1;                        // second lowest track eta relative to jet axis
    float TagVarCSV2_trackEtaRel_2;                        // third lowest track eta relative to jet axis


    TMVA::Reader *reader = new TMVA::Reader( "!Color" );
    
    reader->AddVariable("TagVarCSV_vertexCategory",&TagVarCSV_vertexCategory);
    reader->AddVariable("TagVarCSV_jetNTracks",&TagVarCSV_jetNTracks);
    //reader->AddVariable("TagVarCSV_trackSip2dSig_0",&TagVarCSV_trackSip2dSig_0);
    //reader->AddVariable("TagVarCSV_trackSip2dSig_1",&TagVarCSV_trackSip2dSig_1);
    //reader->AddVariable("TagVarCSV_trackSip2dSig_2",&TagVarCSV_trackSip2dSig_2);
    //reader->AddVariable("TagVarCSV_trackSip2dSig_3",&TagVarCSV_trackSip2dSig_3);
    reader->AddVariable("TagVarCSV_trackSip3dSig_0",&TagVarCSV_trackSip3dSig_0);
    reader->AddVariable("TagVarCSV_trackSip3dSig_1",&TagVarCSV_trackSip3dSig_1);
    reader->AddVariable("TagVarCSV_trackSip3dSig_2",&TagVarCSV_trackSip3dSig_2);
    reader->AddVariable("TagVarCSV_trackSip3dSig_3",&TagVarCSV_trackSip3dSig_3);
    //reader->AddVariable("TagVarCSV_trackPtRel_0",&TagVarCSV_trackPtRel_0);
    //reader->AddVariable("TagVarCSV_trackPtRel_1",&TagVarCSV_trackPtRel_1);
    //reader->AddVariable("TagVarCSV_trackPtRel_2",&TagVarCSV_trackPtRel_2);
    //reader->AddVariable("TagVarCSV_trackPtRel_3",&TagVarCSV_trackPtRel_3);
    reader->AddVariable("TagVarCSV_trackSip2dSigAboveCharm",&TagVarCSV_trackSip2dSigAboveCharm);
    //reader->AddVariable("TagVarCSV_trackSip3dSigAboveCharm",&TagVarCSV_trackSip3dSigAboveCharm);
    //reader->AddVariable("TagVarCSV_trackSumJetEtRatio",&TagVarCSV_trackSumJetEtRatio);
    //reader->AddVariable("TagVarCSV_trackSumJetDeltaR",&TagVarCSV_trackSumJetDeltaR);
    reader->AddVariable("TagVarCSV_jetNTracksEtaRel",&TagVarCSV_jetNTracksEtaRel);
    reader->AddVariable("TagVarCSV_trackEtaRel_0",&TagVarCSV_trackEtaRel_0);
    reader->AddVariable("TagVarCSV_trackEtaRel_1",&TagVarCSV_trackEtaRel_1);
    reader->AddVariable("TagVarCSV_trackEtaRel_2",&TagVarCSV_trackEtaRel_2);
    reader->AddVariable("TagVarCSV_jetNSecondaryVertices",&TagVarCSV_jetNSecondaryVertices);
    reader->AddVariable("TagVarCSV_vertexMass",&TagVarCSV_vertexMass);
    reader->AddVariable("TagVarCSV_vertexNTracks",&TagVarCSV_vertexNTracks);
    reader->AddVariable("TagVarCSV_vertexEnergyRatio",&TagVarCSV_vertexEnergyRatio);
    reader->AddVariable("TagVarCSV_vertexJetDeltaR",&TagVarCSV_vertexJetDeltaR);
    reader->AddVariable("TagVarCSV_flightDistance2dSig",&TagVarCSV_flightDistance2dSig);
    //reader->AddVariable("TagVarCSV_flightDistance3dSig",&TagVarCSV_flightDistance3dSig);
    
    reader->AddSpectator("Jet_pt", &Jet_pt);
    reader->AddSpectator("Jet_eta", &Jet_eta);
    reader->AddSpectator("Jet_phi", &Jet_phi);
    reader->AddSpectator("Jet_mass", &Jet_mass);
    reader->AddSpectator("Jet_massGroomed", &Jet_massGroomed);
    reader->AddSpectator("Jet_flavour", &Jet_flavour);
    reader->AddSpectator("Jet_nbHadrons", &Jet_nbHadrons);
    reader->AddSpectator("Jet_JP", &Jet_JP);
    reader->AddSpectator("Jet_JBP", &Jet_JBP);
    reader->AddSpectator("Jet_CSV", &Jet_CSV);
    reader->AddSpectator("Jet_CSVIVF", &Jet_CSVIVF);
    reader->AddSpectator("Jet_tau1", &Jet_tau1);
    reader->AddSpectator("Jet_tau2", &Jet_tau2);

    reader->AddSpectator("SubJet1_CSVIVF", &SubJet1_CSVIVF);
    reader->AddSpectator("SubJet2_CSVIVF", &SubJet2_CSVIVF);

    reader->BookMVA( "BDTG_T1000D3_fat_BBvsQCD method", "weights/TMVATrainer_BDTG_T1000D3_fat_BBvsQCD.weights.xml" );

    // histograms
    TH1F* hBDTGDiscSig = new TH1F("hBDTGDiscSig","",1000,-5,5);
    TH1F* hBDTGDiscBkg = new TH1F("hBDTGDiscBkg","",1000,-5,5);

    TH1F* hFatCSVIVFDiscSig = new TH1F("hFatCSVIVFDiscSig","",1000,-5,5);
    TH1F* hFatCSVIVFDiscBkg = new TH1F("hFatCSVIVFDiscBkg","",1000,-5,5);

    TH1F* hSubCSVIVFDiscSig = new TH1F("hSubCSVIVFDiscSig","",1000,-5,5);
    TH1F* hSubCSVIVFDiscBkg = new TH1F("hSubCSVIVFDiscBkg","",1000,-5,5);

    hBDTGDiscSig->GetXaxis()->SetTitle("BDTG Discriminant");
    hBDTGDiscBkg->GetXaxis()->SetTitle("BDTG Discriminant");

    hFatCSVIVFDiscSig->GetXaxis()->SetTitle("CSV Discriminant");
    hFatCSVIVFDiscBkg->GetXaxis()->SetTitle("CSV Discriminant");

    hSubCSVIVFDiscSig->GetXaxis()->SetTitle("CSV Discriminant");
    hSubCSVIVFDiscBkg->GetXaxis()->SetTitle("CSV Discriminant");
    
    // background input tree
    TString infilenameBkg="QCD_Pt-300to470_TuneZ2star_8TeV_pythia6_JetTaggingVariables_evaluation.root";
    TFile inBkg(infilenameBkg);
    TTree* intree = (TTree*)inBkg.Get("tagVars/ttree");

    // set the branches to point to address of the variables declared above
    //######################################
    // Fat jet variables
    //######################################
    intree->SetBranchAddress("Jet_pt"          ,&Jet_pt          );
    intree->SetBranchAddress("Jet_eta"         ,&Jet_eta         );
    intree->SetBranchAddress("Jet_phi"         ,&Jet_phi         );
    intree->SetBranchAddress("Jet_mass"        ,&Jet_mass        );
    intree->SetBranchAddress("Jet_massGroomed" ,&Jet_massGroomed );
    intree->SetBranchAddress("Jet_flavour"     ,&Jet_flavour     );
    intree->SetBranchAddress("Jet_nbHadrons"   ,&Jet_nbHadrons   );
    intree->SetBranchAddress("Jet_JP"          ,&Jet_JP          );
    intree->SetBranchAddress("Jet_JBP"         ,&Jet_JBP         );
    intree->SetBranchAddress("Jet_CSV"         ,&Jet_CSV         );
    intree->SetBranchAddress("Jet_CSVIVF"      ,&Jet_CSVIVF      );
    intree->SetBranchAddress("Jet_tau1"        ,&Jet_tau1        );
    intree->SetBranchAddress("Jet_tau2"        ,&Jet_tau2        );
    //--------------------------------------
    // CSV TaggingVariables
    //--------------------------------------
    intree->SetBranchAddress("TagVarCSV_jetNTracks"               ,&TagVarCSV_jetNTracks              );
    intree->SetBranchAddress("TagVarCSV_jetNTracksEtaRel"         ,&TagVarCSV_jetNTracksEtaRel        );
    intree->SetBranchAddress("TagVarCSV_trackSumJetEtRatio"       ,&TagVarCSV_trackSumJetEtRatio      );
    intree->SetBranchAddress("TagVarCSV_trackSumJetDeltaR"        ,&TagVarCSV_trackSumJetDeltaR       );
    intree->SetBranchAddress("TagVarCSV_trackSip2dValAboveCharm"  ,&TagVarCSV_trackSip2dValAboveCharm );
    intree->SetBranchAddress("TagVarCSV_trackSip2dSigAboveCharm"  ,&TagVarCSV_trackSip2dSigAboveCharm );
    intree->SetBranchAddress("TagVarCSV_trackSip3dValAboveCharm"  ,&TagVarCSV_trackSip3dValAboveCharm );
    intree->SetBranchAddress("TagVarCSV_trackSip3dSigAboveCharm"  ,&TagVarCSV_trackSip3dSigAboveCharm );
    intree->SetBranchAddress("TagVarCSV_vertexCategory"           ,&TagVarCSV_vertexCategory          );
    intree->SetBranchAddress("TagVarCSV_jetNSecondaryVertices"    ,&TagVarCSV_jetNSecondaryVertices   );
    intree->SetBranchAddress("TagVarCSV_vertexMass"               ,&TagVarCSV_vertexMass              );
    intree->SetBranchAddress("TagVarCSV_vertexNTracks"            ,&TagVarCSV_vertexNTracks           );
    intree->SetBranchAddress("TagVarCSV_vertexEnergyRatio"        ,&TagVarCSV_vertexEnergyRatio       );
    intree->SetBranchAddress("TagVarCSV_vertexJetDeltaR"          ,&TagVarCSV_vertexJetDeltaR         );
    intree->SetBranchAddress("TagVarCSV_flightDistance2dVal"      ,&TagVarCSV_flightDistance2dVal     );
    intree->SetBranchAddress("TagVarCSV_flightDistance2dSig"      ,&TagVarCSV_flightDistance2dSig     );
    intree->SetBranchAddress("TagVarCSV_flightDistance3dVal"      ,&TagVarCSV_flightDistance3dVal     );
    intree->SetBranchAddress("TagVarCSV_flightDistance3dSig"      ,&TagVarCSV_flightDistance3dSig     );
    intree->SetBranchAddress("TagVarCSV_trackSip2dSig_0"          ,&TagVarCSV_trackSip2dSig_0         );
    intree->SetBranchAddress("TagVarCSV_trackSip2dSig_1"          ,&TagVarCSV_trackSip2dSig_1         );
    intree->SetBranchAddress("TagVarCSV_trackSip2dSig_2"          ,&TagVarCSV_trackSip2dSig_2         );
    intree->SetBranchAddress("TagVarCSV_trackSip2dSig_3"          ,&TagVarCSV_trackSip2dSig_3         );
    intree->SetBranchAddress("TagVarCSV_trackSip2dSig_4"          ,&TagVarCSV_trackSip2dSig_4         );
    intree->SetBranchAddress("TagVarCSV_trackSip2dSig_5"          ,&TagVarCSV_trackSip2dSig_5         );
    intree->SetBranchAddress("TagVarCSV_trackSip3dSig_0"          ,&TagVarCSV_trackSip3dSig_0         );
    intree->SetBranchAddress("TagVarCSV_trackSip3dSig_1"          ,&TagVarCSV_trackSip3dSig_1         );
    intree->SetBranchAddress("TagVarCSV_trackSip3dSig_2"          ,&TagVarCSV_trackSip3dSig_2         );
    intree->SetBranchAddress("TagVarCSV_trackSip3dSig_3"          ,&TagVarCSV_trackSip3dSig_3         );
    intree->SetBranchAddress("TagVarCSV_trackSip3dSig_4"          ,&TagVarCSV_trackSip3dSig_4         );
    intree->SetBranchAddress("TagVarCSV_trackSip3dSig_5"          ,&TagVarCSV_trackSip3dSig_5         );
    intree->SetBranchAddress("TagVarCSV_trackPtRel_0"             ,&TagVarCSV_trackPtRel_0            );
    intree->SetBranchAddress("TagVarCSV_trackPtRel_1"             ,&TagVarCSV_trackPtRel_1            );
    intree->SetBranchAddress("TagVarCSV_trackPtRel_2"             ,&TagVarCSV_trackPtRel_2            );
    intree->SetBranchAddress("TagVarCSV_trackPtRel_3"             ,&TagVarCSV_trackPtRel_3            );
    intree->SetBranchAddress("TagVarCSV_trackPtRel_4"             ,&TagVarCSV_trackPtRel_4            );
    intree->SetBranchAddress("TagVarCSV_trackPtRel_5"             ,&TagVarCSV_trackPtRel_5            );
    intree->SetBranchAddress("TagVarCSV_trackEtaRel_0"            ,&TagVarCSV_trackEtaRel_0           );
    intree->SetBranchAddress("TagVarCSV_trackEtaRel_1"            ,&TagVarCSV_trackEtaRel_1           );
    intree->SetBranchAddress("TagVarCSV_trackEtaRel_2"            ,&TagVarCSV_trackEtaRel_2           );
    //######################################
    // Subjet1 variables
    //######################################
    intree->SetBranchAddress("SubJet1_pt"          ,&SubJet1_pt          );
    intree->SetBranchAddress("SubJet1_eta"         ,&SubJet1_eta         );
    intree->SetBranchAddress("SubJet1_phi"         ,&SubJet1_phi         );
    intree->SetBranchAddress("SubJet1_mass"        ,&SubJet1_mass        );
    intree->SetBranchAddress("SubJet1_flavour"     ,&SubJet1_flavour     );
    intree->SetBranchAddress("SubJet1_nbHadrons"   ,&SubJet1_nbHadrons   );
    intree->SetBranchAddress("SubJet1_JP"          ,&SubJet1_JP          );
    intree->SetBranchAddress("SubJet1_JBP"         ,&SubJet1_JBP         );
    intree->SetBranchAddress("SubJet1_CSV"         ,&SubJet1_CSV         );
    intree->SetBranchAddress("SubJet1_CSVIVF"      ,&SubJet1_CSVIVF      );
    //--------------------------------------
    // CSV TaggingVariables
    //--------------------------------------
    intree->SetBranchAddress("TagVarCSV1_jetNTracks"               ,&TagVarCSV1_jetNTracks              );
    intree->SetBranchAddress("TagVarCSV1_jetNTracksEtaRel"         ,&TagVarCSV1_jetNTracksEtaRel        );
    intree->SetBranchAddress("TagVarCSV1_trackSumJetEtRatio"       ,&TagVarCSV1_trackSumJetEtRatio      );
    intree->SetBranchAddress("TagVarCSV1_trackSumJetDeltaR"        ,&TagVarCSV1_trackSumJetDeltaR       );
    intree->SetBranchAddress("TagVarCSV1_trackSip2dValAboveCharm"  ,&TagVarCSV1_trackSip2dValAboveCharm );
    intree->SetBranchAddress("TagVarCSV1_trackSip2dSigAboveCharm"  ,&TagVarCSV1_trackSip2dSigAboveCharm );
    intree->SetBranchAddress("TagVarCSV1_trackSip3dValAboveCharm"  ,&TagVarCSV1_trackSip3dValAboveCharm );
    intree->SetBranchAddress("TagVarCSV1_trackSip3dSigAboveCharm"  ,&TagVarCSV1_trackSip3dSigAboveCharm );
    intree->SetBranchAddress("TagVarCSV1_vertexCategory"           ,&TagVarCSV1_vertexCategory          );
    intree->SetBranchAddress("TagVarCSV1_jetNSecondaryVertices"    ,&TagVarCSV1_jetNSecondaryVertices   );
    intree->SetBranchAddress("TagVarCSV1_vertexMass"               ,&TagVarCSV1_vertexMass              );
    intree->SetBranchAddress("TagVarCSV1_vertexNTracks"            ,&TagVarCSV1_vertexNTracks           );
    intree->SetBranchAddress("TagVarCSV1_vertexEnergyRatio"        ,&TagVarCSV1_vertexEnergyRatio       );
    intree->SetBranchAddress("TagVarCSV1_vertexJetDeltaR"          ,&TagVarCSV1_vertexJetDeltaR         );
    intree->SetBranchAddress("TagVarCSV1_flightDistance2dVal"      ,&TagVarCSV1_flightDistance2dVal     );
    intree->SetBranchAddress("TagVarCSV1_flightDistance2dSig"      ,&TagVarCSV1_flightDistance2dSig     );
    intree->SetBranchAddress("TagVarCSV1_flightDistance3dVal"      ,&TagVarCSV1_flightDistance3dVal     );
    intree->SetBranchAddress("TagVarCSV1_flightDistance3dSig"      ,&TagVarCSV1_flightDistance3dSig     );
    intree->SetBranchAddress("TagVarCSV1_trackEtaRel_0"            ,&TagVarCSV1_trackEtaRel_0           );
    intree->SetBranchAddress("TagVarCSV1_trackEtaRel_1"            ,&TagVarCSV1_trackEtaRel_1           );
    intree->SetBranchAddress("TagVarCSV1_trackEtaRel_2"            ,&TagVarCSV1_trackEtaRel_2           );
    //######################################
    // Subjet2 variables
    //######################################
    intree->SetBranchAddress("SubJet2_pt"          ,&SubJet2_pt          );
    intree->SetBranchAddress("SubJet2_eta"         ,&SubJet2_eta         );
    intree->SetBranchAddress("SubJet2_phi"         ,&SubJet2_phi         );
    intree->SetBranchAddress("SubJet2_mass"        ,&SubJet2_mass        );
    intree->SetBranchAddress("SubJet2_flavour"     ,&SubJet2_flavour     );
    intree->SetBranchAddress("SubJet2_nbHadrons"   ,&SubJet2_nbHadrons   );
    intree->SetBranchAddress("SubJet2_JP"          ,&SubJet2_JP          );
    intree->SetBranchAddress("SubJet2_JBP"         ,&SubJet2_JBP         );
    intree->SetBranchAddress("SubJet2_CSV"         ,&SubJet2_CSV         );
    intree->SetBranchAddress("SubJet2_CSVIVF"      ,&SubJet2_CSVIVF      );
    //--------------------------------------
    // CSV TaggingVariables
    //--------------------------------------
    intree->SetBranchAddress("TagVarCSV2_jetNTracks"               ,&TagVarCSV2_jetNTracks              );
    intree->SetBranchAddress("TagVarCSV2_jetNTracksEtaRel"         ,&TagVarCSV2_jetNTracksEtaRel        );
    intree->SetBranchAddress("TagVarCSV2_trackSumJetEtRatio"       ,&TagVarCSV2_trackSumJetEtRatio      );
    intree->SetBranchAddress("TagVarCSV2_trackSumJetDeltaR"        ,&TagVarCSV2_trackSumJetDeltaR       );
    intree->SetBranchAddress("TagVarCSV2_trackSip2dValAboveCharm"  ,&TagVarCSV2_trackSip2dValAboveCharm );
    intree->SetBranchAddress("TagVarCSV2_trackSip2dSigAboveCharm"  ,&TagVarCSV2_trackSip2dSigAboveCharm );
    intree->SetBranchAddress("TagVarCSV2_trackSip3dValAboveCharm"  ,&TagVarCSV2_trackSip3dValAboveCharm );
    intree->SetBranchAddress("TagVarCSV2_trackSip3dSigAboveCharm"  ,&TagVarCSV2_trackSip3dSigAboveCharm );
    intree->SetBranchAddress("TagVarCSV2_vertexCategory"           ,&TagVarCSV2_vertexCategory          );
    intree->SetBranchAddress("TagVarCSV2_jetNSecondaryVertices"    ,&TagVarCSV2_jetNSecondaryVertices   );
    intree->SetBranchAddress("TagVarCSV2_vertexMass"               ,&TagVarCSV2_vertexMass              );
    intree->SetBranchAddress("TagVarCSV2_vertexNTracks"            ,&TagVarCSV2_vertexNTracks           );
    intree->SetBranchAddress("TagVarCSV2_vertexEnergyRatio"        ,&TagVarCSV2_vertexEnergyRatio       );
    intree->SetBranchAddress("TagVarCSV2_vertexJetDeltaR"          ,&TagVarCSV2_vertexJetDeltaR         );
    intree->SetBranchAddress("TagVarCSV2_flightDistance2dVal"      ,&TagVarCSV2_flightDistance2dVal     );
    intree->SetBranchAddress("TagVarCSV2_flightDistance2dSig"      ,&TagVarCSV2_flightDistance2dSig     );
    intree->SetBranchAddress("TagVarCSV2_flightDistance3dVal"      ,&TagVarCSV2_flightDistance3dVal     );
    intree->SetBranchAddress("TagVarCSV2_flightDistance3dSig"      ,&TagVarCSV2_flightDistance3dSig     );
    intree->SetBranchAddress("TagVarCSV2_trackEtaRel_0"            ,&TagVarCSV2_trackEtaRel_0           );
    intree->SetBranchAddress("TagVarCSV2_trackEtaRel_1"            ,&TagVarCSV2_trackEtaRel_1           );
    intree->SetBranchAddress("TagVarCSV2_trackEtaRel_2"            ,&TagVarCSV2_trackEtaRel_2           );
    
    std::cout << "Now looping over " << intree->GetEntries() << " entries..." << std::endl;
    for(Long64_t iEntry = 0; iEntry < intree->GetEntries(); iEntry++){
	    if (iEntry % 1000 == 0) std::cout << "Processing Entry #" << iEntry << std::endl;
	    intree->GetEntry(iEntry); // all variables now filled!

	    bool isBkg = ( Jet_massGroomed>80 && Jet_massGroomed<150 );
	    float BDTG_Disc = reader->EvaluateMVA("BDTG_T1000D3_fat_BBvsQCD method");

	    if (isBkg) {
		    hBDTGDiscBkg->Fill(BDTG_Disc);
		    hFatCSVIVFDiscBkg->Fill(Jet_CSVIVF);
		    hSubCSVIVFDiscBkg->Fill(std::min(SubJet1_CSVIVF,SubJet2_CSVIVF));
	    }
    }

    // signal input tree
    TString infilenameSig="RadionToHH_4b_M-800_TuneZ2star_8TeV-Madgraph_pythia6_JetTaggingVariables_evaluation.root";
    TFile inSig(infilenameSig);
    intree = (TTree*)inSig.Get("tagVars/ttree");

    // set the branches to point to address of the variables declared above
    //######################################
    // Fat jet variables
    //######################################
    intree->SetBranchAddress("Jet_pt"          ,&Jet_pt          );
    intree->SetBranchAddress("Jet_eta"         ,&Jet_eta         );
    intree->SetBranchAddress("Jet_phi"         ,&Jet_phi         );
    intree->SetBranchAddress("Jet_mass"        ,&Jet_mass        );
    intree->SetBranchAddress("Jet_massGroomed" ,&Jet_massGroomed );
    intree->SetBranchAddress("Jet_flavour"     ,&Jet_flavour     );
    intree->SetBranchAddress("Jet_nbHadrons"   ,&Jet_nbHadrons   );
    intree->SetBranchAddress("Jet_JP"          ,&Jet_JP          );
    intree->SetBranchAddress("Jet_JBP"         ,&Jet_JBP         );
    intree->SetBranchAddress("Jet_CSV"         ,&Jet_CSV         );
    intree->SetBranchAddress("Jet_CSVIVF"      ,&Jet_CSVIVF      );
    intree->SetBranchAddress("Jet_tau1"        ,&Jet_tau1        );
    intree->SetBranchAddress("Jet_tau2"        ,&Jet_tau2        );
    //--------------------------------------
    // CSV TaggingVariables
    //--------------------------------------
    intree->SetBranchAddress("TagVarCSV_jetNTracks"               ,&TagVarCSV_jetNTracks              );
    intree->SetBranchAddress("TagVarCSV_jetNTracksEtaRel"         ,&TagVarCSV_jetNTracksEtaRel        );
    intree->SetBranchAddress("TagVarCSV_trackSumJetEtRatio"       ,&TagVarCSV_trackSumJetEtRatio      );
    intree->SetBranchAddress("TagVarCSV_trackSumJetDeltaR"        ,&TagVarCSV_trackSumJetDeltaR       );
    intree->SetBranchAddress("TagVarCSV_trackSip2dValAboveCharm"  ,&TagVarCSV_trackSip2dValAboveCharm );
    intree->SetBranchAddress("TagVarCSV_trackSip2dSigAboveCharm"  ,&TagVarCSV_trackSip2dSigAboveCharm );
    intree->SetBranchAddress("TagVarCSV_trackSip3dValAboveCharm"  ,&TagVarCSV_trackSip3dValAboveCharm );
    intree->SetBranchAddress("TagVarCSV_trackSip3dSigAboveCharm"  ,&TagVarCSV_trackSip3dSigAboveCharm );
    intree->SetBranchAddress("TagVarCSV_vertexCategory"           ,&TagVarCSV_vertexCategory          );
    intree->SetBranchAddress("TagVarCSV_jetNSecondaryVertices"    ,&TagVarCSV_jetNSecondaryVertices   );
    intree->SetBranchAddress("TagVarCSV_vertexMass"               ,&TagVarCSV_vertexMass              );
    intree->SetBranchAddress("TagVarCSV_vertexNTracks"            ,&TagVarCSV_vertexNTracks           );
    intree->SetBranchAddress("TagVarCSV_vertexEnergyRatio"        ,&TagVarCSV_vertexEnergyRatio       );
    intree->SetBranchAddress("TagVarCSV_vertexJetDeltaR"          ,&TagVarCSV_vertexJetDeltaR         );
    intree->SetBranchAddress("TagVarCSV_flightDistance2dVal"      ,&TagVarCSV_flightDistance2dVal     );
    intree->SetBranchAddress("TagVarCSV_flightDistance2dSig"      ,&TagVarCSV_flightDistance2dSig     );
    intree->SetBranchAddress("TagVarCSV_flightDistance3dVal"      ,&TagVarCSV_flightDistance3dVal     );
    intree->SetBranchAddress("TagVarCSV_flightDistance3dSig"      ,&TagVarCSV_flightDistance3dSig     );
    intree->SetBranchAddress("TagVarCSV_trackSip2dSig_0"          ,&TagVarCSV_trackSip2dSig_0         );
    intree->SetBranchAddress("TagVarCSV_trackSip2dSig_1"          ,&TagVarCSV_trackSip2dSig_1         );
    intree->SetBranchAddress("TagVarCSV_trackSip2dSig_2"          ,&TagVarCSV_trackSip2dSig_2         );
    intree->SetBranchAddress("TagVarCSV_trackSip2dSig_3"          ,&TagVarCSV_trackSip2dSig_3         );
    intree->SetBranchAddress("TagVarCSV_trackSip2dSig_4"          ,&TagVarCSV_trackSip2dSig_4         );
    intree->SetBranchAddress("TagVarCSV_trackSip2dSig_5"          ,&TagVarCSV_trackSip2dSig_5         );
    intree->SetBranchAddress("TagVarCSV_trackSip3dSig_0"          ,&TagVarCSV_trackSip3dSig_0         );
    intree->SetBranchAddress("TagVarCSV_trackSip3dSig_1"          ,&TagVarCSV_trackSip3dSig_1         );
    intree->SetBranchAddress("TagVarCSV_trackSip3dSig_2"          ,&TagVarCSV_trackSip3dSig_2         );
    intree->SetBranchAddress("TagVarCSV_trackSip3dSig_3"          ,&TagVarCSV_trackSip3dSig_3         );
    intree->SetBranchAddress("TagVarCSV_trackSip3dSig_4"          ,&TagVarCSV_trackSip3dSig_4         );
    intree->SetBranchAddress("TagVarCSV_trackSip3dSig_5"          ,&TagVarCSV_trackSip3dSig_5         );
    intree->SetBranchAddress("TagVarCSV_trackPtRel_0"             ,&TagVarCSV_trackPtRel_0            );
    intree->SetBranchAddress("TagVarCSV_trackPtRel_1"             ,&TagVarCSV_trackPtRel_1            );
    intree->SetBranchAddress("TagVarCSV_trackPtRel_2"             ,&TagVarCSV_trackPtRel_2            );
    intree->SetBranchAddress("TagVarCSV_trackPtRel_3"             ,&TagVarCSV_trackPtRel_3            );
    intree->SetBranchAddress("TagVarCSV_trackPtRel_4"             ,&TagVarCSV_trackPtRel_4            );
    intree->SetBranchAddress("TagVarCSV_trackPtRel_5"             ,&TagVarCSV_trackPtRel_5            );
    intree->SetBranchAddress("TagVarCSV_trackEtaRel_0"            ,&TagVarCSV_trackEtaRel_0           );
    intree->SetBranchAddress("TagVarCSV_trackEtaRel_1"            ,&TagVarCSV_trackEtaRel_1           );
    intree->SetBranchAddress("TagVarCSV_trackEtaRel_2"            ,&TagVarCSV_trackEtaRel_2           );
    //######################################
    // Subjet1 variables
    //######################################
    intree->SetBranchAddress("SubJet1_pt"          ,&SubJet1_pt          );
    intree->SetBranchAddress("SubJet1_eta"         ,&SubJet1_eta         );
    intree->SetBranchAddress("SubJet1_phi"         ,&SubJet1_phi         );
    intree->SetBranchAddress("SubJet1_mass"        ,&SubJet1_mass        );
    intree->SetBranchAddress("SubJet1_flavour"     ,&SubJet1_flavour     );
    intree->SetBranchAddress("SubJet1_nbHadrons"   ,&SubJet1_nbHadrons   );
    intree->SetBranchAddress("SubJet1_JP"          ,&SubJet1_JP          );
    intree->SetBranchAddress("SubJet1_JBP"         ,&SubJet1_JBP         );
    intree->SetBranchAddress("SubJet1_CSV"         ,&SubJet1_CSV         );
    intree->SetBranchAddress("SubJet1_CSVIVF"      ,&SubJet1_CSVIVF      );
    //--------------------------------------
    // CSV TaggingVariables
    //--------------------------------------
    intree->SetBranchAddress("TagVarCSV1_jetNTracks"               ,&TagVarCSV1_jetNTracks              );
    intree->SetBranchAddress("TagVarCSV1_jetNTracksEtaRel"         ,&TagVarCSV1_jetNTracksEtaRel        );
    intree->SetBranchAddress("TagVarCSV1_trackSumJetEtRatio"       ,&TagVarCSV1_trackSumJetEtRatio      );
    intree->SetBranchAddress("TagVarCSV1_trackSumJetDeltaR"        ,&TagVarCSV1_trackSumJetDeltaR       );
    intree->SetBranchAddress("TagVarCSV1_trackSip2dValAboveCharm"  ,&TagVarCSV1_trackSip2dValAboveCharm );
    intree->SetBranchAddress("TagVarCSV1_trackSip2dSigAboveCharm"  ,&TagVarCSV1_trackSip2dSigAboveCharm );
    intree->SetBranchAddress("TagVarCSV1_trackSip3dValAboveCharm"  ,&TagVarCSV1_trackSip3dValAboveCharm );
    intree->SetBranchAddress("TagVarCSV1_trackSip3dSigAboveCharm"  ,&TagVarCSV1_trackSip3dSigAboveCharm );
    intree->SetBranchAddress("TagVarCSV1_vertexCategory"           ,&TagVarCSV1_vertexCategory          );
    intree->SetBranchAddress("TagVarCSV1_jetNSecondaryVertices"    ,&TagVarCSV1_jetNSecondaryVertices   );
    intree->SetBranchAddress("TagVarCSV1_vertexMass"               ,&TagVarCSV1_vertexMass              );
    intree->SetBranchAddress("TagVarCSV1_vertexNTracks"            ,&TagVarCSV1_vertexNTracks           );
    intree->SetBranchAddress("TagVarCSV1_vertexEnergyRatio"        ,&TagVarCSV1_vertexEnergyRatio       );
    intree->SetBranchAddress("TagVarCSV1_vertexJetDeltaR"          ,&TagVarCSV1_vertexJetDeltaR         );
    intree->SetBranchAddress("TagVarCSV1_flightDistance2dVal"      ,&TagVarCSV1_flightDistance2dVal     );
    intree->SetBranchAddress("TagVarCSV1_flightDistance2dSig"      ,&TagVarCSV1_flightDistance2dSig     );
    intree->SetBranchAddress("TagVarCSV1_flightDistance3dVal"      ,&TagVarCSV1_flightDistance3dVal     );
    intree->SetBranchAddress("TagVarCSV1_flightDistance3dSig"      ,&TagVarCSV1_flightDistance3dSig     );
    intree->SetBranchAddress("TagVarCSV1_trackEtaRel_0"            ,&TagVarCSV1_trackEtaRel_0           );
    intree->SetBranchAddress("TagVarCSV1_trackEtaRel_1"            ,&TagVarCSV1_trackEtaRel_1           );
    intree->SetBranchAddress("TagVarCSV1_trackEtaRel_2"            ,&TagVarCSV1_trackEtaRel_2           );
    //######################################
    // Subjet2 variables
    //######################################
    intree->SetBranchAddress("SubJet2_pt"          ,&SubJet2_pt          );
    intree->SetBranchAddress("SubJet2_eta"         ,&SubJet2_eta         );
    intree->SetBranchAddress("SubJet2_phi"         ,&SubJet2_phi         );
    intree->SetBranchAddress("SubJet2_mass"        ,&SubJet2_mass        );
    intree->SetBranchAddress("SubJet2_flavour"     ,&SubJet2_flavour     );
    intree->SetBranchAddress("SubJet2_nbHadrons"   ,&SubJet2_nbHadrons   );
    intree->SetBranchAddress("SubJet2_JP"          ,&SubJet2_JP          );
    intree->SetBranchAddress("SubJet2_JBP"         ,&SubJet2_JBP         );
    intree->SetBranchAddress("SubJet2_CSV"         ,&SubJet2_CSV         );
    intree->SetBranchAddress("SubJet2_CSVIVF"      ,&SubJet2_CSVIVF      );
    //--------------------------------------
    // CSV TaggingVariables
    //--------------------------------------
    intree->SetBranchAddress("TagVarCSV2_jetNTracks"               ,&TagVarCSV2_jetNTracks              );
    intree->SetBranchAddress("TagVarCSV2_jetNTracksEtaRel"         ,&TagVarCSV2_jetNTracksEtaRel        );
    intree->SetBranchAddress("TagVarCSV2_trackSumJetEtRatio"       ,&TagVarCSV2_trackSumJetEtRatio      );
    intree->SetBranchAddress("TagVarCSV2_trackSumJetDeltaR"        ,&TagVarCSV2_trackSumJetDeltaR       );
    intree->SetBranchAddress("TagVarCSV2_trackSip2dValAboveCharm"  ,&TagVarCSV2_trackSip2dValAboveCharm );
    intree->SetBranchAddress("TagVarCSV2_trackSip2dSigAboveCharm"  ,&TagVarCSV2_trackSip2dSigAboveCharm );
    intree->SetBranchAddress("TagVarCSV2_trackSip3dValAboveCharm"  ,&TagVarCSV2_trackSip3dValAboveCharm );
    intree->SetBranchAddress("TagVarCSV2_trackSip3dSigAboveCharm"  ,&TagVarCSV2_trackSip3dSigAboveCharm );
    intree->SetBranchAddress("TagVarCSV2_vertexCategory"           ,&TagVarCSV2_vertexCategory          );
    intree->SetBranchAddress("TagVarCSV2_jetNSecondaryVertices"    ,&TagVarCSV2_jetNSecondaryVertices   );
    intree->SetBranchAddress("TagVarCSV2_vertexMass"               ,&TagVarCSV2_vertexMass              );
    intree->SetBranchAddress("TagVarCSV2_vertexNTracks"            ,&TagVarCSV2_vertexNTracks           );
    intree->SetBranchAddress("TagVarCSV2_vertexEnergyRatio"        ,&TagVarCSV2_vertexEnergyRatio       );
    intree->SetBranchAddress("TagVarCSV2_vertexJetDeltaR"          ,&TagVarCSV2_vertexJetDeltaR         );
    intree->SetBranchAddress("TagVarCSV2_flightDistance2dVal"      ,&TagVarCSV2_flightDistance2dVal     );
    intree->SetBranchAddress("TagVarCSV2_flightDistance2dSig"      ,&TagVarCSV2_flightDistance2dSig     );
    intree->SetBranchAddress("TagVarCSV2_flightDistance3dVal"      ,&TagVarCSV2_flightDistance3dVal     );
    intree->SetBranchAddress("TagVarCSV2_flightDistance3dSig"      ,&TagVarCSV2_flightDistance3dSig     );
    intree->SetBranchAddress("TagVarCSV2_trackEtaRel_0"            ,&TagVarCSV2_trackEtaRel_0           );
    intree->SetBranchAddress("TagVarCSV2_trackEtaRel_1"            ,&TagVarCSV2_trackEtaRel_1           );
    intree->SetBranchAddress("TagVarCSV2_trackEtaRel_2"            ,&TagVarCSV2_trackEtaRel_2           );
    
    std::cout << "Now looping over " << intree->GetEntries() << " entries..." << std::endl;
    for(Long64_t iEntry = 0; iEntry < intree->GetEntries(); iEntry++){
	    if (iEntry % 1000 == 0) std::cout << "Processing Entry #" << iEntry << std::endl;
	    intree->GetEntry(iEntry); // all variables now filled!

	    bool isSig = ( Jet_massGroomed>80 && Jet_massGroomed<150 );
	    float BDTG_Disc = reader->EvaluateMVA("BDTG_T1000D3_fat_BBvsQCD method");

	    if (isSig) {
		    hBDTGDiscSig->Fill(BDTG_Disc);
		    hFatCSVIVFDiscSig->Fill(Jet_CSVIVF);
		    hSubCSVIVFDiscSig->Fill(std::min(SubJet1_CSVIVF,SubJet2_CSVIVF));
	    }
    }
    
    TString outname = "BDTG_vs_CSVv2IVF_fat_BBvsQCD.root";
    TFile out(outname,"RECREATE");

    hBDTGDiscSig->Write();
    hBDTGDiscBkg->Write();

    hFatCSVIVFDiscSig->Write();
    hFatCSVIVFDiscBkg->Write();
    hSubCSVIVFDiscSig->Write();
    hSubCSVIVFDiscBkg->Write();

    out.Close();

    delete reader;
    delete hBDTGDiscSig;
    delete hBDTGDiscBkg;
    delete hFatCSVIVFDiscSig;
    delete hFatCSVIVFDiscBkg;
    delete hSubCSVIVFDiscSig;
    delete hSubCSVIVFDiscBkg;

    std::cout << "Done analyzing!" << std::endl;

}
コード例 #7
0
void TMVAClassificationApplicationLambda( TString myMethodList = "" ) 
{   
#ifdef __CINT__
   gROOT->ProcessLine( ".O0" ); // turn off optimization in CINT
#endif

   //---------------------------------------------------------------

   // This loads the library
   TMVA::Tools::Instance();

   // Default MVA methods to be trained + tested
   std::map<std::string,int> Use;

   // --- Cut optimisation
   Use["Cuts"]            = 1;
   Use["CutsD"]           = 1;
   Use["CutsPCA"]         = 0;
   Use["CutsGA"]          = 0;
   Use["CutsSA"]          = 0;
   // 
   // --- 1-dimensional likelihood ("naive Bayes estimator")
   Use["Likelihood"]      = 1;
   Use["LikelihoodD"]     = 0; // the "D" extension indicates decorrelated input variables (see option strings)
   Use["LikelihoodPCA"]   = 1; // the "PCA" extension indicates PCA-transformed input variables (see option strings)
   Use["LikelihoodKDE"]   = 0;
   Use["LikelihoodMIX"]   = 0;
   //
   // --- Mutidimensional likelihood and Nearest-Neighbour methods
   Use["PDERS"]           = 1;
   Use["PDERSD"]          = 0;
   Use["PDERSPCA"]        = 0;
   Use["PDEFoam"]         = 1;
   Use["PDEFoamBoost"]    = 0; // uses generalised MVA method boosting
   Use["KNN"]             = 1; // k-nearest neighbour method
   //
   // --- Linear Discriminant Analysis
   Use["LD"]              = 1; // Linear Discriminant identical to Fisher
   Use["Fisher"]          = 0;
   Use["FisherG"]         = 0;
   Use["BoostedFisher"]   = 0; // uses generalised MVA method boosting
   Use["HMatrix"]         = 0;
   //
   // --- Function Discriminant analysis
   Use["FDA_GA"]          = 1; // minimisation of user-defined function using Genetics Algorithm
   Use["FDA_SA"]          = 0;
   Use["FDA_MC"]          = 0;
   Use["FDA_MT"]          = 0;
   Use["FDA_GAMT"]        = 0;
   Use["FDA_MCMT"]        = 0;
   //
   // --- Neural Networks (all are feed-forward Multilayer Perceptrons)
   Use["MLP"]             = 0; // Recommended ANN
   Use["MLPBFGS"]         = 0; // Recommended ANN with optional training method
   Use["MLPBNN"]          = 1; // Recommended ANN with BFGS training method and bayesian regulator
   Use["CFMlpANN"]        = 0; // Depreciated ANN from ALEPH
   Use["TMlpANN"]         = 0; // ROOT's own ANN
   //
   // --- Support Vector Machine 
   Use["SVM"]             = 1;
   // 
   // --- Boosted Decision Trees
   Use["BDT"]             = 1; // uses Adaptive Boost
   Use["BDTG"]            = 0; // uses Gradient Boost
   Use["BDTB"]            = 0; // uses Bagging
   Use["BDTD"]            = 0; // decorrelation + Adaptive Boost
   // 
   // --- Friedman's RuleFit method, ie, an optimised series of cuts ("rules")
   Use["RuleFit"]         = 1;
   // ---------------------------------------------------------------
   Use["Plugin"]          = 0;
   Use["Category"]        = 0;
   Use["SVM_Gauss"]       = 0;
   Use["SVM_Poly"]        = 0;
   Use["SVM_Lin"]         = 0;

   std::cout << std::endl;
   std::cout << "==> Start TMVAClassificationApplication" << std::endl;

   // Select methods (don't look at this code - not of interest)
   if (myMethodList != "") {
      for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) it->second = 0;

      std::vector<TString> mlist = gTools().SplitString( myMethodList, ',' );
      for (UInt_t i=0; i<mlist.size(); i++) {
         std::string regMethod(mlist[i]);

         if (Use.find(regMethod) == Use.end()) {
            std::cout << "Method \"" << regMethod 
                      << "\" not known in TMVA under this name. Choose among the following:" << std::endl;
            for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) {
               std::cout << it->first << " ";
            }
            std::cout << std::endl;
            return;
         }
         Use[regMethod] = 1;
      }
   }

   // --------------------------------------------------------------------------------------------------

   // --- Create the Reader object

   TMVA::Reader *reader = new TMVA::Reader( "!Color:!Silent" );    

   // Create a set of variables and declare them to the reader
   // - the variable names MUST corresponds in name and type to those given in the weight file(s) used
   //Float_t var1, var2;
   //Float_t var3, var4;

   Float_t la_agl, la_dlos, la_dau1_dzos, la_dau1_dxyos, la_dau2_dzos, la_dau2_dxyos;
   Float_t la_vtxChi2, la_dau1_nhit, la_dau2_nhit;

   reader->AddVariable( "la_agl", &la_agl );
   reader->AddVariable( "la_dlos", &la_dlos );
   reader->AddVariable( "la_dau1_dzos", &la_dau1_dzos );
   reader->AddVariable( "la_dau2_dzos",&la_dau2_dzos);
   reader->AddVariable( "la_dau1_dxyos",                &la_dau1_dxyos );
   
   reader->AddVariable( "la_dau2_dxyos",&la_dau2_dxyos);
   reader->AddVariable( "la_vtxChi2",&la_vtxChi2);
   reader->AddVariable( "la_dau1_nhit",&la_dau1_nhit);
   reader->AddVariable( "la_dau2_nhit",&la_dau2_nhit);


   // Spectator variables declared in the training have to be added to the reader, too
   Float_t la_mass;
   reader->AddSpectator( "la_mass",   &la_mass );
   //reader->AddSpectator( "spec2 := var1*3",   &spec2 );
/*
   Float_t Category_cat1, Category_cat2, Category_cat3;
   if (Use["Category"]){
      // Add artificial spectators for distinguishing categories
      reader->AddSpectator( "Category_cat1 := var3<=0",             &Category_cat1 );
      reader->AddSpectator( "Category_cat2 := (var3>0)&&(var4<0)",  &Category_cat2 );
      reader->AddSpectator( "Category_cat3 := (var3>0)&&(var4>=0)", &Category_cat3 );
   }
*/
   // --- Book the MVA methods

   TString dir    = "weights/";
   TString prefix = "TMVAClassification";

   // Book method(s)
   for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) {
      if (it->second) {
         TString methodName = TString(it->first) + TString(" method");
         TString weightfile = dir + prefix + TString("_") + TString(it->first) + TString(".weights.xml");
         reader->BookMVA( methodName, weightfile ); 
      }
   }
   
   // Book output histograms
   UInt_t nbin = 100;
   TH1F   *histLk(0), *histLkD(0), *histLkPCA(0), *histLkKDE(0), *histLkMIX(0), *histPD(0), *histPDD(0);
   TH1F   *histPDPCA(0), *histPDEFoam(0), *histPDEFoamErr(0), *histPDEFoamSig(0), *histKNN(0), *histHm(0);
   TH1F   *histFi(0), *histFiG(0), *histFiB(0), *histLD(0), *histNn(0),*histNnbfgs(0),*histNnbnn(0);
   TH1F   *histNnC(0), *histNnT(0), *histBdt(0), *histBdtG(0), *histBdtD(0), *histRf(0), *histSVMG(0);
   TH1F   *histSVMP(0), *histSVML(0), *histFDAMT(0), *histFDAGA(0), *histCat(0), *histPBdt(0);

   if (Use["Likelihood"])    histLk      = new TH1F( "MVA_Likelihood",    "MVA_Likelihood",    nbin, -1, 1 );
   if (Use["LikelihoodD"])   histLkD     = new TH1F( "MVA_LikelihoodD",   "MVA_LikelihoodD",   nbin, -1, 0.9999 );
   if (Use["LikelihoodPCA"]) histLkPCA   = new TH1F( "MVA_LikelihoodPCA", "MVA_LikelihoodPCA", nbin, -1, 1 );
   if (Use["LikelihoodKDE"]) histLkKDE   = new TH1F( "MVA_LikelihoodKDE", "MVA_LikelihoodKDE", nbin,  -0.00001, 0.99999 );
   if (Use["LikelihoodMIX"]) histLkMIX   = new TH1F( "MVA_LikelihoodMIX", "MVA_LikelihoodMIX", nbin,  0, 1 );
   if (Use["PDERS"])         histPD      = new TH1F( "MVA_PDERS",         "MVA_PDERS",         nbin,  0, 1 );
   if (Use["PDERSD"])        histPDD     = new TH1F( "MVA_PDERSD",        "MVA_PDERSD",        nbin,  0, 1 );
   if (Use["PDERSPCA"])      histPDPCA   = new TH1F( "MVA_PDERSPCA",      "MVA_PDERSPCA",      nbin,  0, 1 );
   if (Use["KNN"])           histKNN     = new TH1F( "MVA_KNN",           "MVA_KNN",           nbin,  0, 1 );
   if (Use["HMatrix"])       histHm      = new TH1F( "MVA_HMatrix",       "MVA_HMatrix",       nbin, -0.95, 1.55 );
   if (Use["Fisher"])        histFi      = new TH1F( "MVA_Fisher",        "MVA_Fisher",        nbin, -4, 4 );
   if (Use["FisherG"])       histFiG     = new TH1F( "MVA_FisherG",       "MVA_FisherG",       nbin, -1, 1 );
   if (Use["BoostedFisher"]) histFiB     = new TH1F( "MVA_BoostedFisher", "MVA_BoostedFisher", nbin, -2, 2 );
   if (Use["LD"])            histLD      = new TH1F( "MVA_LD",            "MVA_LD",            nbin, -2, 2 );
   if (Use["MLP"])           histNn      = new TH1F( "MVA_MLP",           "MVA_MLP",           nbin, -1.25, 1.5 );
   if (Use["MLPBFGS"])       histNnbfgs  = new TH1F( "MVA_MLPBFGS",       "MVA_MLPBFGS",       nbin, -1.25, 1.5 );
   if (Use["MLPBNN"])        histNnbnn   = new TH1F( "MVA_MLPBNN",        "MVA_MLPBNN",        nbin, -1.25, 1.5 );
   if (Use["CFMlpANN"])      histNnC     = new TH1F( "MVA_CFMlpANN",      "MVA_CFMlpANN",      nbin,  0, 1 );
   if (Use["TMlpANN"])       histNnT     = new TH1F( "MVA_TMlpANN",       "MVA_TMlpANN",       nbin, -1.3, 1.3 );
   if (Use["BDT"])           histBdt     = new TH1F( "MVA_BDT",           "MVA_BDT",           nbin, -0.8, 0.8 );
   if (Use["BDTD"])          histBdtD    = new TH1F( "MVA_BDTD",          "MVA_BDTD",          nbin, -0.8, 0.8 );
   if (Use["BDTG"])          histBdtG    = new TH1F( "MVA_BDTG",          "MVA_BDTG",          nbin, -1.0, 1.0 );
   if (Use["RuleFit"])       histRf      = new TH1F( "MVA_RuleFit",       "MVA_RuleFit",       nbin, -2.0, 2.0 );
   if (Use["SVM_Gauss"])     histSVMG    = new TH1F( "MVA_SVM_Gauss",     "MVA_SVM_Gauss",     nbin,  0.0, 1.0 );
   if (Use["SVM_Poly"])      histSVMP    = new TH1F( "MVA_SVM_Poly",      "MVA_SVM_Poly",      nbin,  0.0, 1.0 );
   if (Use["SVM_Lin"])       histSVML    = new TH1F( "MVA_SVM_Lin",       "MVA_SVM_Lin",       nbin,  0.0, 1.0 );
   if (Use["FDA_MT"])        histFDAMT   = new TH1F( "MVA_FDA_MT",        "MVA_FDA_MT",        nbin, -2.0, 3.0 );
   if (Use["FDA_GA"])        histFDAGA   = new TH1F( "MVA_FDA_GA",        "MVA_FDA_GA",        nbin, -2.0, 3.0 );
   if (Use["Category"])      histCat     = new TH1F( "MVA_Category",      "MVA_Category",      nbin, -2., 2. );
   if (Use["Plugin"])        histPBdt    = new TH1F( "MVA_PBDT",          "MVA_BDT",           nbin, -0.8, 0.8 );

   // PDEFoam also returns per-event error, fill in histogram, and also fill significance
   if (Use["PDEFoam"]) {
      histPDEFoam    = new TH1F( "MVA_PDEFoam",       "MVA_PDEFoam",              nbin,  0, 1 );
      histPDEFoamErr = new TH1F( "MVA_PDEFoamErr",    "MVA_PDEFoam error",        nbin,  0, 1 );
      histPDEFoamSig = new TH1F( "MVA_PDEFoamSig",    "MVA_PDEFoam significance", nbin,  0, 10 );
   }

   // Book example histogram for probability (the other methods are done similarly)
   TH1F *probHistFi(0), *rarityHistFi(0);
   if (Use["Fisher"]) {
      probHistFi   = new TH1F( "MVA_Fisher_Proba",  "MVA_Fisher_Proba",  nbin, 0, 1 );
      rarityHistFi = new TH1F( "MVA_Fisher_Rarity", "MVA_Fisher_Rarity", nbin, 0, 1 );
   }

   // Prepare input tree (this must be replaced by your data source)
   // in this example, there is a toy tree with signal and one with background events
   // we'll later on use only the "signal" events for the test in this example.
   //   
   TFile *input(0);
   TString fname = "./tmva_example.root";   
   if (!gSystem->AccessPathName( fname )) 
      input = TFile::Open( fname ); // check if file in local directory exists
   else    
      input = TFile::Open( "~/2014Research/ROOT_file/V0reco_PbPb/MCPbPb_central1.root" ); // if not: download from ROOT server
   
   if (!input) {
      std::cout << "ERROR: could not open data file" << std::endl;
      exit(1);
   }
   std::cout << "--- TMVAClassificationApp    : Using input file: " << input->GetName() << std::endl;
   
   // --- Event loop

   // Prepare the event tree
   // - here the variable names have to corresponds to your tree
   // - you can use the same variables as above which is slightly faster,
   //   but of course you can use different ones and copy the values inside the event loop
   //
   std::cout << "--- Select signal sample" << std::endl;
   TTree* theTree = (TTree*)input->Get("ana/v0_Lambda");
   //Float_t userVar1, userVar2;
   theTree->SetBranchAddress( "la_agl", &la_agl );
   theTree->SetBranchAddress( "la_dlos", &la_dlos );
   theTree->SetBranchAddress( "la_dau1_dzos", &la_dau1_dzos );
   theTree->SetBranchAddress( "la_dau1_dxyos",                &la_dau1_dxyos );
   theTree->SetBranchAddress( "la_dau2_dzos",&la_dau2_dzos);
   theTree->SetBranchAddress( "la_dau2_dxyos",&la_dau2_dxyos);
   theTree->SetBranchAddress( "la_vtxChi2",&la_vtxChi2);
   theTree->SetBranchAddress( "la_dau1_nhit",&la_dau1_nhit);
   theTree->SetBranchAddress( "la_dau2_nhit",&la_dau2_nhit);

   theTree->SetBranchAddress( "la_mass", &la_mass );




   // Efficiency calculator for cut method
   Int_t    nSelCutsGA = 0;
   Double_t effS       = 0.7;

   std::vector<Float_t> vecVar(4); // vector for EvaluateMVA tests

   std::cout << "--- Processing: " << theTree->GetEntries() << " events" << std::endl;
   TStopwatch sw;
   sw.Start();

   TFile *target  = new TFile( "TMVAppLambda.root","RECREATE" );

   TNtuple *n1 =  new TNtuple( "n1","n1","Lam_mass:MVA");
   
   for (Long64_t ievt=0; ievt<theTree->GetEntries();ievt++) {

      if (ievt%1000 == 0) std::cout << "--- ... Processing event: " << ievt << std::endl;

      theTree->GetEntry(ievt);
      
      // --- Return the MVA outputs and fill into histograms

      if (Use["CutsGA"]) {
         // Cuts is a special case: give the desired signal efficienciy
         Bool_t passed = reader->EvaluateMVA( "CutsGA method", effS );
         if (passed) nSelCutsGA++;
      }

      if (Use["Likelihood"   ])   histLk     ->Fill( reader->EvaluateMVA( "Likelihood method"    ) );
      if (Use["LikelihoodD"  ])   histLkD    ->Fill( reader->EvaluateMVA( "LikelihoodD method"   ) );
      if (Use["LikelihoodPCA"])   histLkPCA  ->Fill( reader->EvaluateMVA( "LikelihoodPCA method" ) );
      if (Use["LikelihoodKDE"])   histLkKDE  ->Fill( reader->EvaluateMVA( "LikelihoodKDE method" ) );
      if (Use["LikelihoodMIX"])   histLkMIX  ->Fill( reader->EvaluateMVA( "LikelihoodMIX method" ) );
      if (Use["PDERS"        ])   histPD     ->Fill( reader->EvaluateMVA( "PDERS method"         ) );
      if (Use["PDERSD"       ])   histPDD    ->Fill( reader->EvaluateMVA( "PDERSD method"        ) );
      if (Use["PDERSPCA"     ])   histPDPCA  ->Fill( reader->EvaluateMVA( "PDERSPCA method"      ) );
      if (Use["KNN"          ])   histKNN    ->Fill( reader->EvaluateMVA( "KNN method"           ) );
      if (Use["HMatrix"      ])   histHm     ->Fill( reader->EvaluateMVA( "HMatrix method"       ) );
      if (Use["Fisher"       ])   histFi     ->Fill( reader->EvaluateMVA( "Fisher method"        ) );
      if (Use["FisherG"      ])   histFiG    ->Fill( reader->EvaluateMVA( "FisherG method"       ) );
      if (Use["BoostedFisher"])   histFiB    ->Fill( reader->EvaluateMVA( "BoostedFisher method" ) );
      if (Use["LD"           ])   histLD     ->Fill( reader->EvaluateMVA( "LD method"            ) );
      if (Use["MLP"          ])   histNn     ->Fill( reader->EvaluateMVA( "MLP method"           ) );
      if (Use["MLPBFGS"      ])   histNnbfgs ->Fill( reader->EvaluateMVA( "MLPBFGS method"       ) );
      if (Use["MLPBNN"       ])   histNnbnn  ->Fill( reader->EvaluateMVA( "MLPBNN method"        ) );
      if (Use["CFMlpANN"     ])   histNnC    ->Fill( reader->EvaluateMVA( "CFMlpANN method"      ) );
      if (Use["TMlpANN"      ])   histNnT    ->Fill( reader->EvaluateMVA( "TMlpANN method"       ) );
      
      if (Use["BDT"          ]) {  

          double Lam_mass = la_mass;
          
          histBdt    ->Fill( reader->EvaluateMVA( "BDT method"           ) );
          

          double MVA = 0.0;
          MVA = reader->EvaluateMVA("BDT method");


          n1->Fill(Lam_mass,MVA);
          
          
         // cout << "la mass: " << temp << endl
       }

      if (Use["BDTD"         ])  {
  
          double Lam_mass = la_mass;
          
          histBdtD    ->Fill( reader->EvaluateMVA( "BDTD method"           ) );
          

          double MVA = 0.0;
          MVA = reader->EvaluateMVA("BDTD method");


          n1->Fill(Lam_mass,MVA);

      }

      if (Use["BDTG"         ]) {

          double Lam_mass = la_mass;
          
          histBdtG    ->Fill( reader->EvaluateMVA( "BDTG method"           ) );
          

          double MVA = 0.0;
          MVA = reader->EvaluateMVA("BDTG method");


          n1->Fill(Lam_mass,MVA);



      }

      if (Use["RuleFit"      ])   histRf     ->Fill( reader->EvaluateMVA( "RuleFit method"       ) );
      if (Use["SVM_Gauss"    ])   histSVMG   ->Fill( reader->EvaluateMVA( "SVM_Gauss method"     ) );
      if (Use["SVM_Poly"     ])   histSVMP   ->Fill( reader->EvaluateMVA( "SVM_Poly method"      ) );
      if (Use["SVM_Lin"      ])   histSVML   ->Fill( reader->EvaluateMVA( "SVM_Lin method"       ) );
      if (Use["FDA_MT"       ])   histFDAMT  ->Fill( reader->EvaluateMVA( "FDA_MT method"        ) );
      if (Use["FDA_GA"       ])   histFDAGA  ->Fill( reader->EvaluateMVA( "FDA_GA method"        ) );
      if (Use["Category"     ])   histCat    ->Fill( reader->EvaluateMVA( "Category method"      ) );
      if (Use["Plugin"       ])   histPBdt   ->Fill( reader->EvaluateMVA( "P_BDT method"         ) );

      // Retrieve also per-event error
      if (Use["PDEFoam"]) {
         Double_t val = reader->EvaluateMVA( "PDEFoam method" );
         Double_t err = reader->GetMVAError();
         histPDEFoam   ->Fill( val );
         histPDEFoamErr->Fill( err );         
         if (err>1.e-50) histPDEFoamSig->Fill( val/err );
      }         

      // Retrieve probability instead of MVA output
      if (Use["Fisher"])   {
         probHistFi  ->Fill( reader->GetProba ( "Fisher method" ) );
         rarityHistFi->Fill( reader->GetRarity( "Fisher method" ) );
      }
   }

   // Get elapsed time
   sw.Stop();
   std::cout << "--- End of event loop: "; sw.Print();

   // Get efficiency for cuts classifier
   if (Use["CutsGA"]) std::cout << "--- Efficiency for CutsGA method: " << double(nSelCutsGA)/theTree->GetEntries()
                                << " (for a required signal efficiency of " << effS << ")" << std::endl;

   if (Use["CutsGA"]) {

      // test: retrieve cuts for particular signal efficiency
      // CINT ignores dynamic_casts so we have to use a cuts-secific Reader function to acces the pointer  
      TMVA::MethodCuts* mcuts = reader->FindCutsMVA( "CutsGA method" ) ;

      if (mcuts) {      
         std::vector<Double_t> cutsMin;
         std::vector<Double_t> cutsMax;
         mcuts->GetCuts( 0.7, cutsMin, cutsMax );
         std::cout << "--- -------------------------------------------------------------" << std::endl;
         std::cout << "--- Retrieve cut values for signal efficiency of 0.7 from Reader" << std::endl;
         for (UInt_t ivar=0; ivar<cutsMin.size(); ivar++) {
            std::cout << "... Cut: " 
                      << cutsMin[ivar] 
                      << " < \"" 
                      << mcuts->GetInputVar(ivar)
                      << "\" <= " 
                      << cutsMax[ivar] << std::endl;
         }
         std::cout << "--- -------------------------------------------------------------" << std::endl;
      }
   }

   // --- Write histograms



   

   if (Use["Likelihood"   ])   histLk     ->Write();
   if (Use["LikelihoodD"  ])   histLkD    ->Write();
   if (Use["LikelihoodPCA"])   histLkPCA  ->Write();
   if (Use["LikelihoodKDE"])   histLkKDE  ->Write();
   if (Use["LikelihoodMIX"])   histLkMIX  ->Write();
   if (Use["PDERS"        ])   histPD     ->Write();
   if (Use["PDERSD"       ])   histPDD    ->Write();
   if (Use["PDERSPCA"     ])   histPDPCA  ->Write();
   if (Use["KNN"          ])   histKNN    ->Write();
   if (Use["HMatrix"      ])   histHm     ->Write();
   if (Use["Fisher"       ])   histFi     ->Write();
   if (Use["FisherG"      ])   histFiG    ->Write();
   if (Use["BoostedFisher"])   histFiB    ->Write();
   if (Use["LD"           ])   histLD     ->Write();
   if (Use["MLP"          ])   histNn     ->Write();
   if (Use["MLPBFGS"      ])   histNnbfgs ->Write();
   if (Use["MLPBNN"       ])   histNnbnn  ->Write();
   if (Use["CFMlpANN"     ])   histNnC    ->Write();
   if (Use["TMlpANN"      ])   histNnT    ->Write();
   
   if (Use["BDT"          ]){   
      histBdt    ->Write();
      n1->Write();
    }

   if (Use["BDTD"         ]){
      histBdtD   ->Write();
      n1->Write();

   }

   if (Use["BDTG"         ])  {

      histBdtG   ->Write();
      n1->Write();
   } 
   if (Use["RuleFit"      ])   histRf     ->Write();
   if (Use["SVM_Gauss"    ])   histSVMG   ->Write();
   if (Use["SVM_Poly"     ])   histSVMP   ->Write();
   if (Use["SVM_Lin"      ])   histSVML   ->Write();
   if (Use["FDA_MT"       ])   histFDAMT  ->Write();
   if (Use["FDA_GA"       ])   histFDAGA  ->Write();
   if (Use["Category"     ])   histCat    ->Write();
   if (Use["Plugin"       ])   histPBdt   ->Write();

   // Write also error and significance histos
   if (Use["PDEFoam"]) { histPDEFoam->Write(); histPDEFoamErr->Write(); histPDEFoamSig->Write(); }

   // Write also probability hists
   if (Use["Fisher"]) { if (probHistFi != 0) probHistFi->Write(); if (rarityHistFi != 0) rarityHistFi->Write(); }
   target->Close();

   std::cout << "--- Created root file: \"TMVApp.root\" containing the MVA output histograms" << std::endl;
  
   delete reader;
    
   std::cout << "==> TMVAClassificationApplication is done!" << endl << std::endl;
} 
コード例 #8
0
void TMVAClassificationApplication_new(TString myMethodList = "" , TString iFileName = "", TString bkgSample = "", TString sampleLocation = "", TString massPoint = "", TString oFileLocation = "") 
{   
#ifdef __CINT__
   gROOT->ProcessLine( ".O0" ); // turn off optimization in CINT
#endif

   //---------------------------------------------------------------

   // This loads the library
   TMVA::Tools::Instance();

   // Default MVA methods to be trained + tested
   std::map<std::string,int> Use;

   // --- Cut optimisation
   Use["Cuts"]            = 0;
   Use["CutsD"]           = 0;
   Use["CutsPCA"]         = 0;
   Use["CutsGA"]          = 0;
   Use["CutsSA"]          = 0;
   // 
   // 
   // --- Boosted Decision Trees
   Use["BDT"]             = 1; // uses Adaptive Boost
   std::cout << std::endl;
   std::cout << "==> Start TMVAClassificationApplication" << std::endl;

   // Select methods (don't look at this code - not of interest)
   if (myMethodList != "") {
      for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) it->second = 0;

      std::vector<TString> mlist = gTools().SplitString( myMethodList, ',' );
      for (UInt_t i=0; i<mlist.size(); i++) {
         std::string regMethod(mlist[i]);

         if (Use.find(regMethod) == Use.end()) {
            std::cout << "Method \"" << regMethod 
                      << "\" not known in TMVA under this name. Choose among the following:" << std::endl;
            for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) {
               std::cout << it->first << " ";
            }
            std::cout << std::endl;
            return;
         }
         Use[regMethod] = 1;
      }
   }

   // --------------------------------------------------------------------------------------------------

   // --- Create the Reader object

   TMVA::Reader *reader = new TMVA::Reader("!Color:!Silent" );   
    TString weightTail = "_";
    weightTail = weightTail + massPoint;
   // Create a set of variables and declare them to the reader
   // - the variable names MUST corresponds in name and type to those given in the weight file(s) used
   Float_t var1, var2, var3, var4, var5, var6, var7, var8, var9, var10, var11, var12, var13, var14, var15, var16, var17, var18;
   reader->AddVariable( "svMass", &var1);
   reader->AddVariable( "dRTauTau", &var3 );
   reader->AddVariable( "dRJJ", &var4 );
//    reader->AddVariable( "svPt", &var5 );
//    reader->AddVariable( "dRhh", &var6 );
   reader->AddVariable( "met", &var7 );
   reader->AddVariable( "mJJ", &var8 );
//    reader->AddVariable( "metTau1DPhi", &var9 );
//    reader->AddVariable( "metTau2DPhi", &var10);
//    reader->AddVariable( "metJ1DPhi", &var11);
//    reader->AddVariable( "metJ2DPhi", &var12 );
//    reader->AddVariable( "metTauPairDPhi", &var13 );
//    reader->AddVariable( "metSvTauPairDPhi", &var14 );
//    reader->AddVariable( "metJetPairDPhi", &var15 );
//    reader->AddVariable( "CSVJ1", &var16 );
//    reader->AddVariable( "CSVJ2", &var17 );
   reader->AddVariable( "fMassKinFit", &var2 );
   reader->AddVariable( "chi2KinFit2", &var18 );


   // Spectator variables declared in the training have to be added to the reader, too
//    Float_t spec1,spec2;
//    reader->AddSpectator( "spec1 := var1*2",   &spec1 );
//    reader->AddSpectator( "spec2 := var1*3",   &spec2 );

//    Float_t Category_cat1, Category_cat2, Category_cat3;
//    if (Use["Category"]){
//       // Add artificial spectators for distinguishing categories
//       reader->AddSpectator( "Category_cat1 := var3<=0",             &Category_cat1 );
//       reader->AddSpectator( "Category_cat2 := (var3>0)&&(var4<0)",  &Category_cat2 );
//       reader->AddSpectator( "Category_cat3 := (var3>0)&&(var4>=0)", &Category_cat3 );
//    }

   // --- Book the MVA methods
   // Book method(s)
  TString weightFileName = "/nfs_scratch/zmao/test/CMSSW_5_3_15/src/TMVA-v4.2.0/test/weights/TMVAClassification_BDT.weights_";
  weightFileName += bkgSample;
  weightFileName += weightTail;
  reader->BookMVA("BDT method", weightFileName+".xml" ); 

   
   // Book output histograms
   UInt_t nbin = 200;
   TH1F   *histBdt(0);
   histBdt = new TH1F( "MVA_BDT",  "MVA_BDT", nbin, -1.0, 1.0);

   // Prepare input tree (this must be replaced by your data source)
   // in this example, there is a toy tree with signal and one with background events
   // we'll later on use only the "signal" events for the test in this example.
   //   
   TFile *input(0);
   TString fileName = iFileName;
   TString fname = sampleLocation;
   fname += fileName;
   TString oFileName = oFileLocation;
   oFileName += "ClassApp_" + bkgSample;
   oFileName += "_";
   oFileName += fileName;

   if (!gSystem->AccessPathName( fname )) 
      input = TFile::Open(fname); // check if file in local directory exists
   if (!input) {
      std::cout << "ERROR: could not open data file" << std::endl;
      exit(1);
   }
   std::cout << "--- TMVAClassificationApp    : Using input file: " << input->GetName() << std::endl;

   // --- Event loop

   // Prepare the event tree
   // - here the variable names have to corresponds to your tree
   // - you can use the same variables as above which is slightly faster,
   //   but of course you can use different ones and copy the values inside the event loop
   //
   std::cout << "--- Select signal sample" << std::endl;
   TTree* theTree = (TTree*)input->Get("eventTree");
   TFile *target  = new TFile( oFileName,"RECREATE" );
   TTree *newTree = theTree->CloneTree();
   Float_t BDT;
   TBranch *branchBDT = newTree->Branch("BDT_"+bkgSample,&BDT,"BDT/F");
   std::vector<Double_t> *vecVar1;
   std::vector<Double_t> *vecVar5;
   std::vector<Double_t> *vecVar7;
   theTree->SetBranchAddress( "svMass", &vecVar1);
   theTree->SetBranchAddress( "dRTauTau", &var3);
   theTree->SetBranchAddress( "dRJJ", &var4 );
//    theTree->SetBranchAddress( "svPt", &vecVar5 );
//    theTree->SetBranchAddress( "dRhh", &var6 );
   theTree->SetBranchAddress( "met", &vecVar7 );
   theTree->SetBranchAddress( "mJJ", &var8 );
//    theTree->SetBranchAddress( "metTau1DPhi", &var9 );
//    theTree->SetBranchAddress( "metTau2DPhi", &var10);
//    theTree->SetBranchAddress( "metJ1DPhi", &var11);
//    theTree->SetBranchAddress( "metJ2DPhi", &var12 );
//    theTree->SetBranchAddress( "metTauPairDPhi", &var13 );
//    theTree->SetBranchAddress( "metSvTauPairDPhi", &var14 );
//    theTree->SetBranchAddress( "metJetPairDPhi", &var15 );
//    theTree->SetBranchAddress( "CSVJ1", &var16 );
//    theTree->SetBranchAddress( "CSVJ2", &var17 );
   theTree->SetBranchAddress( "fMassKinFit", &var2);
   theTree->SetBranchAddress( "chi2KinFit2", &var18);

   //to get initial pre-processed events
   TH1F* cutFlow = (TH1F*)input->Get("preselection");

   // Efficiency calculator for cut method
   Int_t    nSelCutsGA = 0;
   Double_t effS       = 0.7;

   std::vector<Float_t> vecVar(4); // vector for EvaluateMVA tests

   std::cout << "--- Processing: " << theTree->GetEntries() << " events" << std::endl;
   TStopwatch sw;
   sw.Start();
   for (Long64_t ievt=0; ievt<theTree->GetEntries();ievt++) {

      if (ievt%1000 == 0) std::cout << "--- ... Processing event: " << ievt << std::endl;
      theTree->GetEntry(ievt);
      var1 = vecVar1->at(0);
//       var5 = vecVar5->at(0);
      var7 = vecVar7->at(0);
      // --- Return the MVA outputs and fill into histograms

      if (Use["CutsGA"]) {
         // Cuts is a special case: give the desired signal efficienciy
         Bool_t passed = reader->EvaluateMVA( "CutsGA method", effS );
         if (passed) nSelCutsGA++;
      }
      BDT = reader->EvaluateMVA( "BDT method");
      histBdt->Fill(BDT);
      branchBDT->Fill();
   }

   // Get elapsed time
   sw.Stop();
   std::cout << "--- End of event loop: "; sw.Print();

   // Get efficiency for cuts classifier
   if (Use["CutsGA"]) std::cout << "--- Efficiency for CutsGA method: " << double(nSelCutsGA)/theTree->GetEntries()
                                << " (for a required signal efficiency of " << effS << ")" << std::endl;

   if (Use["CutsGA"]) {

      // test: retrieve cuts for particular signal efficiency
      // CINT ignores dynamic_casts so we have to use a cuts-secific Reader function to acces the pointer  
      TMVA::MethodCuts* mcuts = reader->FindCutsMVA( "CutsGA method" ) ;

      if (mcuts) {      
         std::vector<Double_t> cutsMin;
         std::vector<Double_t> cutsMax;
         mcuts->GetCuts( 0.7, cutsMin, cutsMax );
         std::cout << "--- -------------------------------------------------------------" << std::endl;
         std::cout << "--- Retrieve cut values for signal efficiency of 0.7 from Reader" << std::endl;
         for (UInt_t ivar=0; ivar<cutsMin.size(); ivar++) {
            std::cout << "... Cut: " 
                      << cutsMin[ivar] 
                      << " < \"" 
                      << mcuts->GetInputVar(ivar)
                      << "\" <= " 
                      << cutsMax[ivar] << std::endl;
         }
         std::cout << "--- -------------------------------------------------------------" << std::endl;
      }
   }

   // --- Write histograms

   histBdt->Write();
   cutFlow->Write();
   newTree->Write();
   target->Close();

   std::cout << "--- Created root file: \""<<oFileName<<"\" containing the MVA output histograms" << std::endl;
  
   delete reader;
    
   std::cout << "==> TMVAClassificationApplication is done!" << endl << std::endl;
} 
コード例 #9
0
void TMVARegressionApplication( int wMs,int wM, string st,string st2,string option="",TString myMethodList = "" ) 
{
   //---------------------------------------------------------------
   // This loads the library
   TMVA::Tools::Instance();

   // Default MVA methods to be trained + tested
   std::map<std::string,int> Use;

   // --- Mutidimensional likelihood and Nearest-Neighbour methods
   Use["PDERS"]           = 0;
   Use["PDEFoam"]         = 0; 
   Use["KNN"]             = 0;
   // 
   // --- Linear Discriminant Analysis
   Use["LD"]		        = 0;
   // 
   // --- Function Discriminant analysis
   Use["FDA_GA"]          = 0;
   Use["FDA_MC"]          = 0;
   Use["FDA_MT"]          = 0;
   Use["FDA_GAMT"]        = 0;
   // 
   // --- Neural Network
   Use["MLP"]             = 0; 
   // 
   // --- Support Vector Machine 
   Use["SVM"]             = 0;
   // 
   // --- Boosted Decision Trees
   Use["BDT"]             = 0;
   Use["BDTG"]            = 1;
   // ---------------------------------------------------------------

   std::cout << std::endl;
   std::cout << "==> Start TMVARegressionApplication" << std::endl;

   // Select methods (don't look at this code - not of interest)
   if (myMethodList != "") {
      for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) it->second = 0;

      std::vector<TString> mlist = gTools().SplitString( myMethodList, ',' );
      for (UInt_t i=0; i<mlist.size(); i++) {
         std::string regMethod(mlist[i]);

         if (Use.find(regMethod) == Use.end()) {
            std::cout << "Method \"" << regMethod << "\" not known in TMVA under this name. Choose among the following:" << std::endl;
            for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) std::cout << it->first << " ";
            std::cout << std::endl;
            return;
         }
         Use[regMethod] = 1;
      }
   }

   // --------------------------------------------------------------------------------------------------

   // --- Create the Reader object

   TMVA::Reader *reader = new TMVA::Reader( "!Color:!Silent" );    

   // Create a set of variables and declare them to the reader
   // - the variable names MUST corresponds in name and type to those given in the weight file(s) used
   //Float_t var1, var2;
   //reader->AddVariable( "var1", &var1 );
   //reader->AddVariable( "var2", &var2 );
   Float_t pt_AK8MatchedToHbb,eta_AK8MatchedToHbb,nsv_AK8MatchedToHbb,sv0mass_AK8MatchedToHbb,sv1mass_AK8MatchedToHbb,
   nch_AK8MatchedToHbb,nmu_AK8MatchedToHbb,nel_AK8MatchedToHbb,muenfr_AK8MatchedToHbb,emenfr_AK8MatchedToHbb;
   reader->AddVariable( "pt_AK8MatchedToHbb", &pt_AK8MatchedToHbb );
   reader->AddVariable( "eta_AK8MatchedToHbb", &eta_AK8MatchedToHbb );
   reader->AddVariable( "nsv_AK8MatchedToHbb", &nsv_AK8MatchedToHbb );
   reader->AddVariable( "sv0mass_AK8MatchedToHbb", &sv0mass_AK8MatchedToHbb );
   reader->AddVariable( "sv1mass_AK8MatchedToHbb", &sv1mass_AK8MatchedToHbb );
   reader->AddVariable( "nch_AK8MatchedToHbb", &nch_AK8MatchedToHbb );
   reader->AddVariable( "nmu_AK8MatchedToHbb", &nmu_AK8MatchedToHbb );
   reader->AddVariable( "nel_AK8MatchedToHbb", &nel_AK8MatchedToHbb );
   reader->AddVariable( "muenfr_AK8MatchedToHbb", &muenfr_AK8MatchedToHbb );
   reader->AddVariable( "emenfr_AK8MatchedToHbb", &emenfr_AK8MatchedToHbb );

   
   // Spectator variables declared in the training have to be added to the reader, too
   Float_t spec1,spec2;
    reader->AddSpectator( "spec1:=n_pv",  &spec1 );
   reader->AddSpectator( "spec2:=msoftdrop_AK8MatchedToHbb",  &spec2 );

   // --- Book the MVA methods

   TString dir    = "weights/";
   TString prefix = "TMVARegression";

   // Book method(s)
   for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) {
      if (it->second) {
         TString methodName = it->first + " method";
         TString weightfile = dir + prefix + "_" + TString(it->first) + ".weights.xml";
         reader->BookMVA( methodName, weightfile ); 
      }
   }
   
     TH1* hists[100];
   Int_t nhists = -1;
      for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) {
         TH1* h = new TH1F( it->first.c_str(), TString(it->first) + " method", 100, -100, 600 );
         if (it->second) hists[++nhists] = h;
      }
      nhists++;
   
  //1=signal ,0=QCD ,2=data
	int nameRoot=1;
	if((st2.find("QCD")!= std::string::npos)||
	(st2.find("bGen")!= std::string::npos)||
	(st2.find("bEnriched")!= std::string::npos))nameRoot=0;
	if(st2.find("data")!= std::string::npos)nameRoot=2;
	cout<<"nameRoot = "<<nameRoot<<endl;
	
	//option-----------------------------------------------------------
	
	int JESOption=0;
	
   
   // Prepare input tree (this must be replaced by your data source)
   // in this example, there is a toy tree with signal and one with background events
   // we'll later on use only the "signal" events for the test in this example.
   //   
   TFile *f;
	TTree *tree;
	int nPass[20]={0};
	int total=0;
	double fixScaleNum[2]={0};
	
	TH1D* th1=new TH1D("a","a",150,50,200);	
	
	string massName[nMass]={"Thea","HCorr","Reg"};
	string catName[nCat]={"PP","PF","FP","FF"};
	string tau21Name[2]={"withTau21","woTau21"};
	
	string catNameShort[nCat]={"P","F"};
	string looseTight[2]={"loose","tight"};
	TF1 *fa[nMass][2][2][2];
	
	for(int i=0;i<nMass;i++){
		for(int j=0;j<2;j++){
			for(int k=0;k<2;k++){
				for(int w=0;w<2;w++){
					fa[i][j][k][w] = new TF1("fa","[0]+[1]*x+[2]*x*x+[3]*pow(x,3)",-3,3);
					ifstream myfile (Form("PFRatio/%s_%s_%s_%s.txt",looseTight[w].data(),massName[i].data(),catNameShort[j].data(),tau21Name[k].data()));
					double para[4];
					for(int m=0;m<4;m++){
						myfile>>para[m];
					}
					fa[i][j][k][w]->SetParameters(para[0],para[1],para[2],para[3]);
				}
			}
		}
	}
	
	/*
	TH1D* th2[nMass][nCat][2];
	TH1D* th3[nMass][nCat][2];
	for(int i=0;i<nMass;i++){
		for(int j=0;j<nCat;j++){
			for(int k=0;k<2;k++){
				th2[i][j][k]=(TH1D*)th1->Clone(Form("loose_%s_%s_%s",massName[i].data(),catName[j].data(),tau21Name[k].data()));
				th3[i][j][k]=(TH1D*)th1->Clone(Form("tight_%s_%s_%s",massName[i].data(),catName[j].data(),tau21Name[k].data()));
				
				th2[i][j][k]->Sumw2();
				th3[i][j][k]->Sumw2();
			}
		}
	}
	*/
	TH1D* th2d[14];
	
	th2d[0]=new TH1D("0a","0a",4000,1000,5000);	
	th2d[1]=new TH1D("0c","0c",4000,1000,5000);
	th2d[2]=new TH1D("1a","1a",4000,1000,5000);	
	th2d[3]=new TH1D("1c","1c",4000,1000,5000);	
	th2d[4]=new TH1D("2a","2a",4000,1000,5000);	
	th2d[5]=new TH1D("2b","2b",4000,1000,5000);	
	th2d[6]=new TH1D("2d","2d",4000,1000,5000);	
	
	
	th2d[7]=new TH1D("0aL","0aL",4000,1000,5000);		
	th2d[8]=new TH1D("0cL","0cL",4000,1000,5000);		
	th2d[9]=new TH1D("1aL","1aL",4000,1000,5000);		
	th2d[10]=new TH1D("1cL","1cL",4000,1000,5000);		
	th2d[11]=new TH1D("2aL","2aL",4000,1000,5000);	
	th2d[12]=new TH1D("2bL","2bL",4000,1000,5000);	
	th2d[13]=new TH1D("2dL","2dL",4000,1000,5000);	
		
	
		
	
	//int nWidth=5,nBmin=11;
	 int width [nWidth]={25,30,35,40};
	 int bmin[nBmin]={100,105,110,115};
	 
	
	 TH1D* th3d[14][nWidth][nBmin][2];
	 TH1D* th3f[14][nWidth][nBmin][2];
	 TH1D* th3v[14][nWidth][nBmin][2];
	 
	 for(int i=0;i<nWidth;i++){
		 for(int j=0;j<nBmin;j++){
			 for(int k=0;k<2;k++){
				  for(int l=0;l<14;l++){
					  th3d[l][i][j][k]=(TH1D*) th2d[l]->Clone(Form("%s_%d_%d_%s",th2d[l]->GetTitle(),bmin[j],width[i]+bmin[j],tau21Name[k].data()));
					  th3f[l][i][j][k]=(TH1D*) th2d[l]->Clone(Form("fill_%s_%d_%d_%s",th2d[l]->GetTitle(),bmin[j],width[i]+bmin[j],tau21Name[k].data()));
					  th3v[l][i][j][k]=(TH1D*) th2d[l]->Clone(Form("valid_%s_%d_%d_%s",th2d[l]->GetTitle(),bmin[j],width[i]+bmin[j],tau21Name[k].data()));
					   
					  th3d[l][i][j][k]->Sumw2();
					  th3f[l][i][j][k]->Sumw2();
					  th3v[l][i][j][k]->Sumw2();
				  }
			 }
		 }
	 }
   
   for (int w=wMs;w<wM;w++){
		if(w%20==0)cout<<w<<endl;
		
		if (nameRoot!=1)f = TFile::Open(Form("%s%d.root",st.data(),w));
		else f = TFile::Open(st.data());
		if (!f || !f->IsOpen())continue;
		
		TDirectory * dir;
		if (nameRoot!=1)dir = (TDirectory*)f->Get(Form("%s%d.root:/tree",st.data(),w));
		else dir = (TDirectory*)f->Get(Form("%s:/tree",st.data()));
		
		dir->GetObject("treeMaker",tree);
		
		//tree=(TTree*)f->Get("treeMaker");
		TreeReader data(tree);
		total+=data.GetEntriesFast();
		for(Long64_t jEntry=0; jEntry<data.GetEntriesFast() ;jEntry++){
			data.GetEntry(jEntry);
			
			
			
			Int_t nVtx        = data.GetInt("nVtx");
			//0. has a good vertex
			if(nVtx<1)continue;
			nPass[0]++;
			
			//1.trigger
			std::string* trigName = data.GetPtrString("hlt_trigName");
		 	vector<bool> &trigResult = *((vector<bool>*) data.GetPtr("hlt_trigResult"));
			bool passTrigger=false;
			for(int it=0; it< data.GetPtrStringSize();it++){
				std::string thisTrig= trigName[it];
				bool results = trigResult[it];
				if( ((thisTrig.find("HLT_PFHT800")!= std::string::npos||
					thisTrig.find("HLT_AK8DiPFJet300_200_TrimMass30_BTagCSV_p20")!= std::string::npos
						) && results==1)){
					passTrigger=true;
					break;
				}
			}
			if(!passTrigger && nameRoot==2)continue;
			nPass[1]++;

			const int nAK8Jet=data.GetInt("AK8PuppinJet");
			//2.nJets
			if(nAK8Jet<2)continue;nPass[2]++;
			int* AK8PuppinSubSDJet=data.GetPtrInt("AK8PuppinSubSDJet");
			if(AK8PuppinSubSDJet[0]!=2||AK8PuppinSubSDJet[1]!=2)continue;
			TClonesArray* AK8PuppijetP4 = (TClonesArray*) data.GetPtrTObject("AK8PuppijetP4");
			float*  AK8PuppijetCorrUncUp = data.GetPtrFloat("AK8PuppijetCorrUncUp"); 
			float*  AK8PuppijetCorrUncDown = data.GetPtrFloat("AK8PuppijetCorrUncDown"); 
			TLorentzVector* thisJet ,* thatJet;
			
			
			thisJet=(TLorentzVector*)AK8PuppijetP4->At(0);
			thatJet=(TLorentzVector*)AK8PuppijetP4->At(1);
			
			//3. Pt 
			if(thisJet->Pt()>99998 ||thatJet->Pt()>99998 )continue;
			if(thisJet->Pt()<300)continue;
			if(thatJet->Pt()<300)continue;
			nPass[3]++;
			//4tightId-----------------------------------------
			vector<bool>    &AK8PuppijetPassIDTight = *((vector<bool>*) data.GetPtr("AK8PuppijetPassIDTight"));
			if(AK8PuppijetPassIDTight[0]==0)continue;
			if(AK8PuppijetPassIDTight[1]==0)continue;
			Float_t*  AK8PuppijetCEmEF = data.GetPtrFloat("AK8PuppijetCEmEF");
			Float_t*  AK8PuppijetMuoEF = data.GetPtrFloat("AK8PuppijetMuoEF");
			if(AK8PuppijetMuoEF[0]>0.8)continue;
			if(AK8PuppijetCEmEF[0]>0.9)continue;
			if(AK8PuppijetMuoEF[1]>0.8)continue;
			if(AK8PuppijetCEmEF[1]>0.9)continue;
			nPass[4]++;
			//5. Eta-----------------------------------------
			if(fabs(thisJet->Eta())>2.4)continue;
			if(fabs(thatJet->Eta())>2.4)continue;
			nPass[5]++;
			//6. DEta-----------------------------------------
			float dEta = fabs(thisJet->Eta()-thatJet->Eta());
			if(dEta>1.3)continue;
			nPass[6]++;
			//7. Mjj-----------------------------------------
			//float mjjRed = (*thisJet+*thatJet).M()+250-thisJet->M()-thatJet->M();
			//if(mjjRed<1000)continue;
			nPass[7]++;
			//8. fatjetPRmassL2L3Corr-----------------------------------------
			nPass[8]++;
			//9.-----------------------------------------
		
			
    
			Float_t*  AK8Puppijet_DoubleSV = data.GetPtrFloat("AK8Puppijet_DoubleSV");
			
			
			int looseStat=-1;
			int tightStat=-1;
			
			if(AK8Puppijet_DoubleSV[0]>0.3 && AK8Puppijet_DoubleSV[1]>0.3)looseStat=0;
			else if(AK8Puppijet_DoubleSV[0]>0.3 && AK8Puppijet_DoubleSV[1]<0.3)looseStat=1;
			else if(AK8Puppijet_DoubleSV[0]<0.3 && AK8Puppijet_DoubleSV[1]>0.3)looseStat=2;
			else looseStat=3;
			
			if(AK8Puppijet_DoubleSV[0]>0.8 && AK8Puppijet_DoubleSV[1]>0.8)tightStat=0;
			else if(AK8Puppijet_DoubleSV[0]>0.8 && AK8Puppijet_DoubleSV[1]<0.8)tightStat=1;
			else if(AK8Puppijet_DoubleSV[0]<0.3 && AK8Puppijet_DoubleSV[1]>0.8)tightStat=2;
			else if(AK8Puppijet_DoubleSV[0]<0.3 && AK8Puppijet_DoubleSV[1]<0.8)tightStat=3;
			else tightStat=-1;
			
			
			double varTemp[2];
			
			Float_t*  AK8PuppijetSDmass = data.GetPtrFloat("AK8PuppijetSDmass");
			
			if(AK8PuppijetSDmass[0]<50||AK8PuppijetSDmass[1]<50)continue;
			
			Int_t* AK8Puppijet_nSV=data.GetPtrInt("AK8Puppijet_nSV");
			vector<float>   *AK8Puppijet_SVMass  =  data.GetPtrVectorFloat("AK8Puppijet_SVMass");
			int nEle= data.GetInt("nEle");
			int nMu=data.GetInt("nMu");
			Float_t*  AK8PuppijetEleEF = data.GetPtrFloat("AK8PuppijetEleEF");
			//Float_t*  AK8PuppijetMuoEF = data.GetPtrFloat("AK8PuppijetMuoEF");
			Int_t* AK8PuppijetCMulti=data.GetPtrInt("AK8PuppijetCMulti");
			Int_t* AK8PuppijetEleMulti=data.GetPtrInt("AK8PuppijetEleMulti");
			Int_t* AK8PuppijetMuoMulti=data.GetPtrInt("AK8PuppijetMuoMulti");
			
			for(int i=0; i<2;i++){
		
				TLorentzVector* thisAK8Jet ;
				
				if(i==1)thisAK8Jet=thatJet;
				else thisAK8Jet=thisJet;
				
				
				pt_AK8MatchedToHbb=thisAK8Jet->Pt();
				eta_AK8MatchedToHbb=thisAK8Jet->Eta();
				nsv_AK8MatchedToHbb=AK8Puppijet_nSV[i];
				sv0mass_AK8MatchedToHbb=AK8Puppijet_SVMass[i][0];
				sv1mass_AK8MatchedToHbb=AK8Puppijet_SVMass[i][1];
				nmu_AK8MatchedToHbb=AK8PuppijetMuoMulti[i];
				nel_AK8MatchedToHbb=AK8PuppijetEleMulti[i];
				muenfr_AK8MatchedToHbb=AK8PuppijetMuoEF[i];
				nch_AK8MatchedToHbb=AK8PuppijetCMulti[i];
				emenfr_AK8MatchedToHbb=AK8PuppijetEleEF[i];
				spec1=nVtx;
				spec2=AK8PuppijetSDmass[i];
				Float_t val ;
				for (Int_t ih=0; ih<nhists; ih++) {
				TString title = hists[ih]->GetTitle();
				val= (reader->EvaluateRegression( title ))[0];
				}
				varTemp[i]=val;
			}
			
			double PUPPIweight[2]={0};
			PUPPIweight[0]=getPUPPIweight(thisJet->Pt(),thisJet->Eta());
			PUPPIweight[1]=getPUPPIweight(thatJet->Pt(),thatJet->Eta());
			
			double PUPPIweightThea[2]={0};
			PUPPIweightThea[0]=getPUPPIweight_o(thisJet->Pt(),thisJet->Eta());
			PUPPIweightThea[1]=getPUPPIweight_o(thatJet->Pt(),thatJet->Eta());
	
			double Mjja= ((*thisJet)+(*thatJet)).M()+250
									-((*thisJet)).M()-((*thatJet)).M();
									
			TLorentzVector  thisJetReg, thatJetReg;
			thisJetReg=(*thisJet)*varTemp[0];
			thatJetReg=(*thatJet)*varTemp[1];
			
			double Mjjb= (thisJetReg+thatJetReg).M()+250
									-(thisJetReg).M()-(thatJetReg).M();
			
			double PUPPIweightOnRegressed[2]={0};			
			PUPPIweightOnRegressed[0]=getPUPPIweightOnRegressed(thisJetReg.Pt(),thisJetReg.Eta());
			PUPPIweightOnRegressed[1]=getPUPPIweightOnRegressed(thatJetReg.Pt(),thatJetReg.Eta());
			
			vector<float>   *subjetSDPx  =  data.GetPtrVectorFloat("AK8PuppisubjetSDPx");
			vector<float>   *subjetSDPy  =  data.GetPtrVectorFloat("AK8PuppisubjetSDPy");
			vector<float>   *subjetSDPz  =  data.GetPtrVectorFloat("AK8PuppisubjetSDPz");
			vector<float>   *subjetSDE   =  data.GetPtrVectorFloat("AK8PuppisubjetSDE");
			vector<float>   *AK8PuppisubjetSDRawFactor =  data.GetPtrVectorFloat("AK8PuppisubjetSDRawFactor");
			
			TLorentzVector thisSDJet, thatSDJet;
			TLorentzVector* subjetP4[2][2];
			for(int i=0;i<2;i++){
				for(int j=0;j<2;j++){
					subjetP4[i][j]=new TLorentzVector(0,0,0,0);
					subjetP4[i][j]->SetPxPyPzE(subjetSDPx[i][j],subjetSDPy[i][j],subjetSDPz[i][j],subjetSDE[i][j]);
				//	subjetP4[i][j]*=AK8PuppisubjetSDRawFactor[i][j];
				}
			}
			thisSDJet=(*subjetP4[0][0])*AK8PuppisubjetSDRawFactor[0][0]+(*subjetP4[0][1])*AK8PuppisubjetSDRawFactor[0][1];
			thatSDJet=(*subjetP4[1][0])*AK8PuppisubjetSDRawFactor[1][0]+(*subjetP4[1][1])*AK8PuppisubjetSDRawFactor[1][1];
			//thatSDJet=(*subjetP4[1][0])+(*subjetP4[1][1]);
			TLorentzVector thisSDJetReg, thatSDJetReg;			
			thisSDJetReg=	thisSDJet*varTemp[0]*PUPPIweightOnRegressed[0];			
			thatSDJetReg=	thatSDJet*varTemp[1]*PUPPIweightOnRegressed[1];			
			
			
			//double Mjjc= ((thisSDJet)+(thatSDJet)).M()+250
			//						-((thisSDJet)).M()-((thatSDJet)).M();
			
			
			double Mjjd= ((thisSDJet)+(thatSDJet)).M()+250
									-((thisSDJet)).M()-((thatSDJet)).M();
			
			
			Float_t*  AK8PuppijetTau1 = data.GetPtrFloat("AK8PuppijetTau1");
			Float_t*  AK8PuppijetTau2 = data.GetPtrFloat("AK8PuppijetTau2");
			double puppiTau21[2];
			puppiTau21[0]=(AK8PuppijetTau2[0]/AK8PuppijetTau1[0]),puppiTau21[1]=(AK8PuppijetTau2[1]/AK8PuppijetTau1[1]);
			
			double mass_j0,mass_j1,MjjLoop;
			int massCat;
			for(int k=0;k<7;k++){
				
				if(k==0||k==2||k==4){
					if(thisJet->Pt()<300)continue;
					if(thatJet->Pt()<300)continue;
				}
				else if (k==1){
					if((thisSDJet*PUPPIweightThea[0]).Pt()<300)continue;
					if((thatSDJet*PUPPIweightThea[1]).Pt()<300)continue;
				}
				else if (k==3){
					if((thisSDJet*PUPPIweight[0]).Pt()<300)continue;
					if((thatSDJet*PUPPIweight[1]).Pt()<300)continue;
				}
				else if (k==5){
					if(thisJetReg.Pt()<300)continue;
					if(thatJetReg.Pt()<300)continue;
				}
				else{
					if(thisSDJetReg.Pt()<300)continue;
					if(thatSDJetReg.Pt()<300)continue;
				}
				
				
				if(k==0||k==1){
					mass_j0=AK8PuppijetSDmass[0]*PUPPIweightThea[0];
					mass_j1=AK8PuppijetSDmass[1]*PUPPIweightThea[1];
					massCat=0;
				}
				else if (k==2||k==3){
					mass_j0=AK8PuppijetSDmass[0]*PUPPIweight[0];
					mass_j1=AK8PuppijetSDmass[1]*PUPPIweight[1];
					massCat=1;
				}
				
				else{
					mass_j0=AK8PuppijetSDmass[0]*varTemp[0]*PUPPIweightOnRegressed[0];
					mass_j1=AK8PuppijetSDmass[1]*varTemp[1]*PUPPIweightOnRegressed[1];
					massCat=2;
				} 
				
				
				
				if(k==0||k==2||k==4)MjjLoop=Mjja;
				else if (k==1)MjjLoop=((thisSDJet)*PUPPIweightThea[0]+(thatSDJet)*PUPPIweightThea[1]).M()+250-((thisSDJet)*PUPPIweightThea[0]).M()-((thatSDJet)*PUPPIweightThea[1]).M();
				else if (k==3)MjjLoop=((thisSDJet)*PUPPIweight[0]+(thatSDJet)*PUPPIweight[1]).M()+250-((thisSDJet)*PUPPIweight[0]).M()-((thatSDJet)*PUPPIweight[1]).M();
				else if (k==5)MjjLoop=Mjjb;
				else MjjLoop=Mjjd;
				
				
				//cout<<mass_j0<<","<<mass_j1<<",k="<<k<<endl;
				for(int i=0;i<nWidth;i++){
					for(int j=0;j<nBmin;j++){
						if(mass_j0<bmin[j] ||mass_j0>width[i]+bmin[j]
						||mass_j1<bmin[j] ||mass_j1>width[i]+bmin[j] )continue;
						
						for(int m=0;m<2;m++){
							if(m==0 && (puppiTau21[0]>0.6 || puppiTau21[1]>0.6))continue;
							double tightPFRatio=0,loosePFRatio=0;
							tightPFRatio=fa[massCat][0][m][1]->Eval(mass_j0);
							loosePFRatio=fa[massCat][0][m][0]->Eval(mass_j0);
							if(tightStat==0){
								th3d[k][i][j][m]->Fill(MjjLoop);
							}
							else if (tightStat==1){
								th3f[k][i][j][m]->Fill(MjjLoop);
							}
							else if(tightStat==3){
								tightPFRatio=fa[massCat][1][m][1]->Eval(mass_j0);
								th3v[k][i][j][m]->Fill(MjjLoop,tightPFRatio);
							}
							
							if(looseStat==0 && tightStat!=0){
								th3d[k+7][i][j][m]->Fill(MjjLoop);
							}
							else if (looseStat==1){
								th3f[k+7][i][j][m]->Fill(MjjLoop);
							}
							else if(looseStat==3){
								loosePFRatio=fa[massCat][1][m][0]->Eval(mass_j0);
								th3v[k+7][i][j][m]->Fill(MjjLoop,loosePFRatio);
							}
						}
						
					}
				}
			}
			
			
			
		}
	}	
	
	for(int i=0;i<10;i++)cout<<"npass["<<i<<"]="<<nPass[i]<<endl;
   

	TFile* outFile;//= new TFile(Form("PFRatio/%s.root",st2.data()),"recreate");
	outFile= new TFile(Form("MjjVC/%s.root",st2.data()),"recreate");
	for(int i=0;i<nWidth;i++){
		 for(int j=0;j<nBmin;j++){
			 for(int k=0;k<2;k++){
				  for(int l=0;l<14;l++){
					  th3d[l][i][j][k]->Write();
					  th3f[l][i][j][k]->Write();
					  th3v[l][i][j][k]->Write();
					  
				  }
			 }
		 }
	 }
	outFile->Close();
	
	for(int i=0;i<nWidth;i++){
		 for(int j=0;j<nBmin;j++){
			 for(int k=0;k<2;k++){
				  for(int l=0;l<14;l++){
					  delete th3d[l][i][j][k];
					  delete th3f[l][i][j][k];
					  delete th3v[l][i][j][k];
					  
				  }
			 }
		 }
	 }
  
   delete reader;
    
   
}
コード例 #10
0
int main(int argc, char** argv) {
 
 if(argc != 2)
 {
  std::cerr << ">>>>> analysis.cpp::usage: " << argv[0] << " configFileName" << std::endl ;
  return 1;
 }
 
 // Parse the config file                                                                                                                                                          
 parseConfigFile (argv[1]) ;
 
 std::string treeName  = gConfigParser -> readStringOption("Input::treeName");
 std::string fileSamples = gConfigParser -> readStringOption("Input::fileSamples");
 std::string inputDirectory = gConfigParser -> readStringOption("Input::inputDirectory");
 
 std::string inputBeginningFile = "out_NtupleProducer_"; 
 try {
  inputBeginningFile = gConfigParser -> readStringOption("Input::inputBeginningFile");
 }
 catch (char const* exceptionString){
  std::cerr << " exception = " << exceptionString << std::endl;
 }
 std::cout << ">>>>> Input::inputBeginningFile  " << inputBeginningFile  << std::endl;  
 
 
 //==== list of methods
 std::string              MVADirectory           = gConfigParser -> readStringOption    ("Options::MVADirectory");
 std::vector<std::string> vectorMyMethodList     = gConfigParser -> readStringListOption("Options::MVAmethods");
 std::vector<std::string> vectorMyMethodMassList = gConfigParser -> readStringListOption("Options::MVAmethodsMass");
 
 std::string outputDirectory = gConfigParser -> readStringOption("Output::outputDirectory");
  
 //---- variables 
 float jetpt1;
 float jetpt2;
 float mjj;
 float detajj;
 float dphilljetjet;
 
 float pt1;
 float pt2;
 float mll;
 float dphill;
 float mth;
 
 float dphillmet;
 float mpmet;
 
 float channel;
 
 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 TFile* outputRootFile[500];
 TTree* cloneTreeJetLepVect[500];
 
 
 TTree *treeJetLepVect[500];
 TFile* file[500];
 
 char *nameSample[1000];
 char *nameHumanReadable[1000];
 char* xsectionName[1000];
 
 char nameFileIn[1000];
 sprintf(nameFileIn,"%s",fileSamples.c_str());
 
 int numberOfSamples = ReadFile(nameFileIn, nameSample, nameHumanReadable, xsectionName);
 
 double Normalization[1000];
 double xsection[1000];
 
 for (int iSample=0; iSample<numberOfSamples; iSample++){ 
  char nameFile[20000];
  sprintf(nameFile,"%s/%s%s.root",inputDirectory.c_str(),inputBeginningFile.c_str(),nameSample[iSample]);  
  
  file[iSample] = new TFile(nameFile, "READ");
  
  treeJetLepVect[iSample] = (TTree*) file[iSample]->Get(treeName.c_str());
  char nameTreeJetLep[100];
  sprintf(nameTreeJetLep,"treeJetLep_%d",iSample); 
  treeJetLepVect[iSample]->SetName(nameTreeJetLep);
  
  treeJetLepVect[iSample] -> SetBranchAddress("jetpt1",  &jetpt1);
  treeJetLepVect[iSample] -> SetBranchAddress("jetpt2",  &jetpt2);
  treeJetLepVect[iSample] -> SetBranchAddress("mjj",   &mjj);
  treeJetLepVect[iSample] -> SetBranchAddress("detajj", &detajj);
  treeJetLepVect[iSample] -> SetBranchAddress("dphilljetjet", &dphilljetjet);
  treeJetLepVect[iSample] -> SetBranchAddress("pt1", &pt1);
  treeJetLepVect[iSample] -> SetBranchAddress("pt2", &pt2);
  treeJetLepVect[iSample] -> SetBranchAddress("mll", &mll);
  treeJetLepVect[iSample] -> SetBranchAddress("dphill", &dphill);
  treeJetLepVect[iSample] -> SetBranchAddress("mth", &mth);
  treeJetLepVect[iSample] -> SetBranchAddress("dphillmet", &dphillmet);
  treeJetLepVect[iSample] -> SetBranchAddress("mpmet", &mpmet);
  treeJetLepVect[iSample] -> SetBranchAddress("channel", &channel);
  
  
  sprintf(nameFile,"%s/%s%s.root",outputDirectory.c_str(),inputBeginningFile.c_str(),nameSample[iSample]);    
  outputRootFile[iSample] = new TFile ( nameFile, "RECREATE") ;
  outputRootFile[iSample] -> cd () ;
  cloneTreeJetLepVect[iSample] = treeJetLepVect[iSample] -> CloneTree (0) ;
 }
 
 
 /**
  * cycle on MVA (method-mass)
  * * cycle on samples
  * * * cycle on events
 */
 
 for (int iMVA = 0; iMVA < vectorMyMethodList.size(); iMVA++) {
  std::cout << " vectorMyMethodList[" << iMVA << "] = " << vectorMyMethodList.at(iMVA) << std::endl;
  TString myMethodList = Form ("%s",vectorMyMethodList.at(iMVA).c_str());
  for (int iMVAMass = 0; iMVAMass < vectorMyMethodMassList.size(); iMVAMass++) {
   std::cout << " vectorMyMethodMassList[" << iMVAMass << "] = " << vectorMyMethodMassList.at(iMVAMass) << std::endl;
   
   TMVA::Reader *TMVAreader = new TMVA::Reader( "!Color:!Silent" );
   
//    TMVAreader->AddVariable("jetpt1",       &jetpt1);
//    TMVAreader->AddVariable("jetpt2",       &jetpt2);
//    TMVAreader->AddVariable("mjj",          &mjj);
//    TMVAreader->AddVariable("detajj",       &detajj);
//    TMVAreader->AddVariable("dphilljetjet", &dphilljetjet);
//    TMVAreader->AddVariable("pt1",          &pt1);
//    TMVAreader->AddVariable("pt2",          &pt2);
//    TMVAreader->AddVariable("mll",          &mll);
//    TMVAreader->AddVariable("dphill",       &dphill);
//    TMVAreader->AddVariable("mth",          &mth);
//    TMVAreader->AddVariable("dphillmet",    &dphillmet);
//    TMVAreader->AddVariable("mpmet",        &mpmet);

   Float_t input_variables[1000];
//    float input_variables[1000];
   
//    TMVAreader->AddVariable("jetpt1",       &(input_variables[0]));
//    TMVAreader->AddVariable("jetpt2",       &(input_variables[1]));
//    TMVAreader->AddVariable("mjj",          &(input_variables[2]));
//    TMVAreader->AddVariable("detajj",       &(input_variables[3]));
//    TMVAreader->AddVariable("dphilljetjet", &(input_variables[4]));
//    TMVAreader->AddVariable("pt1",          &(input_variables[5]));
//    TMVAreader->AddVariable("pt2",          &(input_variables[6]));
//    TMVAreader->AddVariable("mll",          &(input_variables[7]));
//    TMVAreader->AddVariable("dphill",       &(input_variables[8]));
//    TMVAreader->AddVariable("mth",          &(input_variables[9]));
//    TMVAreader->AddVariable("dphillmet",    &(input_variables[10]));
//    TMVAreader->AddVariable("mpmet",        &(input_variables[11]));
   
   TMVAreader->AddVariable("jetpt1",       &input_variables[0]);
   TMVAreader->AddVariable("jetpt2",       &input_variables[1]);
   TMVAreader->AddVariable("mjj",          &input_variables[2]);
   TMVAreader->AddVariable("detajj",       &input_variables[3]);
   TMVAreader->AddVariable("dphilljetjet", &input_variables[4]);
   TMVAreader->AddVariable("pt1",          &input_variables[5]);
   TMVAreader->AddVariable("pt2",          &input_variables[6]);
   TMVAreader->AddVariable("mll",          &input_variables[7]);
   TMVAreader->AddVariable("dphill",       &input_variables[8]);
   TMVAreader->AddVariable("mth",          &input_variables[9]);
   TMVAreader->AddVariable("dphillmet",    &input_variables[10]);
   TMVAreader->AddVariable("mpmet",        &input_variables[11]);
   TMVAreader->AddSpectator("channel",     &input_variables[12]);
   
 
   TString myMethodMassList = Form ("%s",vectorMyMethodMassList.at(iMVAMass).c_str());
   TString weightfile = Form ("%s/weights_%s_testVariables/TMVAMulticlass_%s.weights.xml",MVADirectory.c_str(),myMethodMassList.Data(),myMethodList.Data());
   
   std::cout << " myMethodList = " << myMethodList.Data() << std::endl;
   std::cout << " weightfile   = " << weightfile.Data()   << std::endl;
   
//    TString myMethodListBook = Form ("%s",vectorMyMethodList.at(iMVA).c_str());
   
//    TMVAreader->BookMVA( myMethodListBook, weightfile );
   TMVAreader->BookMVA( myMethodList, weightfile );
   
   
   for (int iSample=0; iSample<numberOfSamples; iSample++){ 
    std::cout << " iSample = " << iSample << " :: " << numberOfSamples << std::endl;
    file[iSample] -> cd();
    Double_t MVA_Value;
    TBranch *newBranch;
    
    TString methodName4Tree = Form ("%s_%s_MVAHiggs",myMethodList.Data(),myMethodMassList.Data());
    TString methodName4Tree2 =  Form ("%s_%s_MVAHiggs/D",myMethodList.Data(),myMethodMassList.Data());
    newBranch = cloneTreeJetLepVect[iSample]->Branch(methodName4Tree,&MVA_Value,methodName4Tree2);
//     newBranch = treeJetLepVect[iSample]->Branch(methodName4Tree,&MVA_Value,methodName4Tree2);
    
    
    ///==== loop ====
    Long64_t nentries = treeJetLepVect[iSample]->GetEntries();
    
    for (Long64_t iEntry = 0; iEntry < nentries; iEntry++){
     if((iEntry%1000) == 0) std::cout << ">>>>> analysis::GetEntry " << iEntry << " : " << nentries << std::endl;   
     
     treeJetLepVect[iSample]->GetEntry(iEntry);
     
     input_variables[0]  = static_cast<Float_t>(jetpt1);
     input_variables[1]  = static_cast<Float_t>(jetpt2);
     input_variables[2]  = static_cast<Float_t>(mjj);
     input_variables[3]  = static_cast<Float_t>(detajj);
     input_variables[4]  = static_cast<Float_t>(dphilljetjet);
     input_variables[5]  = static_cast<Float_t>(pt1);
     input_variables[6]  = static_cast<Float_t>(pt2);
     input_variables[7]  = static_cast<Float_t>(mll);
     input_variables[8]  = static_cast<Float_t>(dphill);
     input_variables[9]  = static_cast<Float_t>(mth);
     input_variables[10] = static_cast<Float_t>(dphillmet);
     input_variables[11] = static_cast<Float_t>(mpmet);
     input_variables[12] = static_cast<Float_t>(channel);
     
     int num = TMVAreader->EvaluateMulticlass(myMethodList).size();
     double max = -1e9;
     double tempmax;
     int numsel = -1;
     for (int inum = 0; inum<(num-2); inum++) { // il -2 è dovuto a Sig e Bkg che mi salva il training! Uffi!
      tempmax = (TMVAreader->EvaluateMulticlass(myMethodList))[inum];
      if (tempmax > max) {
       max = tempmax;
       numsel = inum;
      }
     }
     MVA_Value = max + 3*numsel;
     
     //      newBranch -> Fill();
     cloneTreeJetLepVect[iSample] -> Fill () ; 
    }
   }
  }
 }
 
 
 for (int iSample=0; iSample<numberOfSamples; iSample++){ 
  // save only the new version of the tree
  //   treeJetLepVect[iSample]->Write("", TObject::kOverwrite);
  cloneTreeJetLepVect[iSample] -> SetName (treeName.c_str());
  cloneTreeJetLepVect[iSample] -> AutoSave () ;
  outputRootFile[iSample] -> Close () ;
 }
     
  
}
コード例 #11
0
void ZTMVAClassificationApplication( string filename, TString myMethodList = "" ) 
{   
#ifdef __CINT__
  gROOT->ProcessLine( ".O0" ); // turn off optimization in CINT
#endif
  //---------------------------------------------------------------

  // This loads the library
  TMVA::Tools::Instance();

  // Default MVA methods to be trained + tested
  std::map<std::string,int> Use;

  // --- Cut optimisation
  Use["Cuts"]            = 0;
  Use["CutsD"]           = 0;
  Use["CutsPCA"]         = 0;
  Use["CutsGA"]          = 0;
  Use["CutsSA"]          = 0;
  // 
  // --- 1-dimensional likelihood ("naive Bayes estimator")
  Use["Likelihood"]      = 0;
  Use["LikelihoodD"]     = 0; // the "D" extension indicates decorrelated input variables (see option strings)
  Use["LikelihoodPCA"]   = 0; // the "PCA" extension indicates PCA-transformed input variables (see option strings)
  Use["LikelihoodKDE"]   = 0;
  Use["LikelihoodMIX"]   = 0;
  //
  // --- Mutidimensional likelihood and Nearest-Neighbour methods
  Use["PDERS"]           = 0;
  Use["PDERSD"]          = 0;
  Use["PDERSPCA"]        = 0;
  Use["PDEFoam"]         = 0;
  Use["PDEFoamBoost"]    = 0; // uses generalised MVA method boosting
  Use["KNN"]             = 0; // k-nearest neighbour method
  //
  // --- Linear Discriminant Analysis
  Use["LD"]              = 0; // Linear Discriminant identical to Fisher
  Use["Fisher"]          = 0;
  Use["FisherG"]         = 0;
  Use["BoostedFisher"]   = 0; // uses generalised MVA method boosting
  Use["HMatrix"]         = 0;
  //
  // --- Function Discriminant analysis
  Use["FDA_GA"]          = 0; // minimisation of user-defined function using Genetics Algorithm
  Use["FDA_SA"]          = 0;
  Use["FDA_MC"]          = 0;
  Use["FDA_MT"]          = 0;
  Use["FDA_GAMT"]        = 0;
  Use["FDA_MCMT"]        = 0;
  //
  // --- Neural Networks (all are feed-forward Multilayer Perceptrons)
  Use["MLP"]             = 1; // Recommended ANN
  Use["MLPBFGS"]         = 0; // Recommended ANN with optional training method
  Use["MLPBNN"]          = 0; // Recommended ANN with BFGS training method and bayesian regulator
  Use["CFMlpANN"]        = 0; // Depreciated ANN from ALEPH
  Use["TMlpANN"]         = 0; // ROOT's own ANN
  //
  // --- Support Vector Machine 
  Use["SVM"]             = 0;
  // 
  // --- Boosted Decision Trees
  Use["BDT"]             = 1; // uses Adaptive Boost
  Use["BDTG"]            = 1; // uses Gradient Boost
  Use["BDTB"]            = 0; // uses Bagging
  Use["BDTD"]            = 1; // decorrelation + Adaptive Boost
  // 
  // --- Friedman's RuleFit method, ie, an optimised series of cuts ("rules")
  Use["RuleFit"]         = 0;
  // ---------------------------------------------------------------
  Use["Plugin"]          = 0;
  Use["Category"]        = 0;
  Use["SVM_Gauss"]       = 0;
  Use["SVM_Poly"]        = 0;
  Use["SVM_Lin"]         = 0;

  std::cout << std::endl;
  std::cout << "==> Start TMVAClassificationApplication" << std::endl;

  // Select methods (don't look at this code - not of interest)
  if (myMethodList != "") {
    for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) it->second = 0;

    std::vector<TString> mlist = gTools().SplitString( myMethodList, ',' );
    for (UInt_t i=0; i<mlist.size(); i++) {
       std::string regMethod(mlist[i]);

       if (Use.find(regMethod) == Use.end()) {
          std::cout << "Method \"" << regMethod 
                    << "\" not known in TMVA under this name. Choose among the following:" << std::endl;
          for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) {
             std::cout << it->first << " ";
          }
          std::cout << std::endl;
          return;
       }
       Use[regMethod] = 1;
    }
  }

  // --------------------------------------------------------------------------------------------------

  // --- Create the Reader object

  TMVA::Reader *reader = new TMVA::Reader( "!Color:!Silent" );    

  Float_t B_s0_ln_FDCHI2; reader->AddVariable("B_s0_ln_FDCHI2", &B_s0_ln_FDCHI2 );
  Float_t B_s0_ln_IPCHI2; reader->AddVariable("B_s0_ln_IPCHI2", &B_s0_ln_IPCHI2 );
  Float_t B_s0_ln_EVCHI2; reader->AddVariable("B_s0_ln_EVCHI2", &B_s0_ln_EVCHI2 );
  Float_t B_s0_PT_fiveGeV;reader->AddVariable("B_s0_PT_fiveGeV",&B_s0_PT_fiveGeV);
  Float_t B_s0_Eta;       reader->AddVariable("B_s0_Eta",       &B_s0_Eta       );
  Float_t minK_PT_GeV;    reader->AddVariable("minK_PT_GeV",    &minK_PT_GeV    );
  Float_t minK_ln_IPCHI2; reader->AddVariable("minK_ln_IPCHI2", &minK_ln_IPCHI2 );
  
  Float_t Category_cat1, Category_cat2, Category_cat3;
  if (Use["Category"]){
    // Add artificial spectators for distinguishing categories
    reader->AddSpectator( "Category_cat1 := var3<=0",             &Category_cat1 );
    reader->AddSpectator( "Category_cat2 := (var3>0)&&(var4<0)",  &Category_cat2 );
    reader->AddSpectator( "Category_cat3 := (var3>0)&&(var4>=0)", &Category_cat3 );
  }

  // --- Book the MVA methods

  TString dir    = "weights/";
  TString prefix = "TMVAClassification";

  // Book method(s)
  for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) {
    if (it->second) {
       TString methodName = TString(it->first) + TString(" method");
       TString weightfile = dir + prefix + TString("_") + TString(it->first) + TString(".weights.xml");
       reader->BookMVA( methodName, weightfile ); 
    }
  }
  
  // Book output histograms
  UInt_t nbin = 100;
  TH1F   *histLk(0), *histLkD(0), *histLkPCA(0), *histLkKDE(0), *histLkMIX(0), *histPD(0), *histPDD(0);
  TH1F   *histPDPCA(0), *histPDEFoam(0), *histPDEFoamErr(0), *histPDEFoamSig(0), *histKNN(0), *histHm(0);
  TH1F   *histFi(0), *histFiG(0), *histFiB(0), *histLD(0), *histNn(0),*histNnbfgs(0),*histNnbnn(0);
  TH1F   *histNnC(0), *histNnT(0), *histBdt(0), *histBdtG(0), *histBdtD(0), *histRf(0), *histSVMG(0);
  TH1F   *histSVMP(0), *histSVML(0), *histFDAMT(0), *histFDAGA(0), *histCat(0), *histPBdt(0);

  if (Use["Likelihood"])    histLk      = new TH1F( "MVA_Likelihood",    "MVA_Likelihood",    nbin, -1, 1 );
  if (Use["LikelihoodD"])   histLkD     = new TH1F( "MVA_LikelihoodD",   "MVA_LikelihoodD",   nbin, -1, 0.9999 );
  if (Use["LikelihoodPCA"]) histLkPCA   = new TH1F( "MVA_LikelihoodPCA", "MVA_LikelihoodPCA", nbin, -1, 1 );
  if (Use["LikelihoodKDE"]) histLkKDE   = new TH1F( "MVA_LikelihoodKDE", "MVA_LikelihoodKDE", nbin,  -0.00001, 0.99999 );
  if (Use["LikelihoodMIX"]) histLkMIX   = new TH1F( "MVA_LikelihoodMIX", "MVA_LikelihoodMIX", nbin,  0, 1 );
  if (Use["PDERS"])         histPD      = new TH1F( "MVA_PDERS",         "MVA_PDERS",         nbin,  0, 1 );
  if (Use["PDERSD"])        histPDD     = new TH1F( "MVA_PDERSD",        "MVA_PDERSD",        nbin,  0, 1 );
  if (Use["PDERSPCA"])      histPDPCA   = new TH1F( "MVA_PDERSPCA",      "MVA_PDERSPCA",      nbin,  0, 1 );
  if (Use["KNN"])           histKNN     = new TH1F( "MVA_KNN",           "MVA_KNN",           nbin,  0, 1 );
  if (Use["HMatrix"])       histHm      = new TH1F( "MVA_HMatrix",       "MVA_HMatrix",       nbin, -0.95, 1.55 );
  if (Use["Fisher"])        histFi      = new TH1F( "MVA_Fisher",        "MVA_Fisher",        nbin, -4, 4 );
  if (Use["FisherG"])       histFiG     = new TH1F( "MVA_FisherG",       "MVA_FisherG",       nbin, -1, 1 );
  if (Use["BoostedFisher"]) histFiB     = new TH1F( "MVA_BoostedFisher", "MVA_BoostedFisher", nbin, -2, 2 );
  if (Use["LD"])            histLD      = new TH1F( "MVA_LD",            "MVA_LD",            nbin, -2, 2 );
  if (Use["MLP"])           histNn      = new TH1F( "MVA_MLP",           "MVA_MLP",           nbin, -1.25, 1.5 );
  if (Use["MLPBFGS"])       histNnbfgs  = new TH1F( "MVA_MLPBFGS",       "MVA_MLPBFGS",       nbin, -1.25, 1.5 );
  if (Use["MLPBNN"])        histNnbnn   = new TH1F( "MVA_MLPBNN",        "MVA_MLPBNN",        nbin, -1.25, 1.5 );
  if (Use["CFMlpANN"])      histNnC     = new TH1F( "MVA_CFMlpANN",      "MVA_CFMlpANN",      nbin,  0, 1 );
  if (Use["TMlpANN"])       histNnT     = new TH1F( "MVA_TMlpANN",       "MVA_TMlpANN",       nbin, -1.3, 1.3 );
  if (Use["BDT"])           histBdt     = new TH1F( "MVA_BDT",           "MVA_BDT",           nbin, -0.8, 0.8 );
  if (Use["BDTD"])          histBdtD    = new TH1F( "MVA_BDTD",          "MVA_BDTD",          nbin, -0.8, 0.8 );
  if (Use["BDTG"])          histBdtG    = new TH1F( "MVA_BDTG",          "MVA_BDTG",          nbin, -1.0, 1.0 );
  if (Use["RuleFit"])       histRf      = new TH1F( "MVA_RuleFit",       "MVA_RuleFit",       nbin, -2.0, 2.0 );
  if (Use["SVM_Gauss"])     histSVMG    = new TH1F( "MVA_SVM_Gauss",     "MVA_SVM_Gauss",     nbin,  0.0, 1.0 );
  if (Use["SVM_Poly"])      histSVMP    = new TH1F( "MVA_SVM_Poly",      "MVA_SVM_Poly",      nbin,  0.0, 1.0 );
  if (Use["SVM_Lin"])       histSVML    = new TH1F( "MVA_SVM_Lin",       "MVA_SVM_Lin",       nbin,  0.0, 1.0 );
  if (Use["FDA_MT"])        histFDAMT   = new TH1F( "MVA_FDA_MT",        "MVA_FDA_MT",        nbin, -2.0, 3.0 );
  if (Use["FDA_GA"])        histFDAGA   = new TH1F( "MVA_FDA_GA",        "MVA_FDA_GA",        nbin, -2.0, 3.0 );
  if (Use["Category"])      histCat     = new TH1F( "MVA_Category",      "MVA_Category",      nbin, -2., 2. );
  if (Use["Plugin"])        histPBdt    = new TH1F( "MVA_PBDT",          "MVA_BDT",           nbin, -0.8, 0.8 );

  // PDEFoam also returns per-event error, fill in histogram, and also fill significance
  if (Use["PDEFoam"]) {
    histPDEFoam    = new TH1F( "MVA_PDEFoam",       "MVA_PDEFoam",              nbin,  0, 1 );
    histPDEFoamErr = new TH1F( "MVA_PDEFoamErr",    "MVA_PDEFoam error",        nbin,  0, 1 );
    histPDEFoamSig = new TH1F( "MVA_PDEFoamSig",    "MVA_PDEFoam significance", nbin,  0, 10 );
  }

  // Book example histogram for probability (the other methods are done similarly)
  TH1F *probHistFi(0), *rarityHistFi(0);
  if (Use["Fisher"]) {
    probHistFi   = new TH1F( "MVA_Fisher_Proba",  "MVA_Fisher_Proba",  nbin, 0, 1 );
    rarityHistFi = new TH1F( "MVA_Fisher_Rarity", "MVA_Fisher_Rarity", nbin, 0, 1 );
  }

  //TFile * input = new TFile("../cloosepid.root"); // this is the signal 
  //TFile * input_Background = new TFile("Z4430Files/merged_ntuple_jpsi_s17.root"); // this is the background
  //TFile * input = new TFile("../output/MCBsphif0_after_transform.root"); // this is the signal
  TFile * input_Background; 
  input_Background = new TFile(filename.c_str());
  //std::cout << "--- TMVAClassificationApp    : Using input file: " << input->GetName() << std::endl;
  std::cout << "--- TMVAClassificationApp    : Using input file: " << input_Background->GetName() << std::endl;
  
  // --- Event loop



  //save the results here
  /*
  TTree* results= new TTree("results", "results");
  Float_t Method_Likelihood; Float_t bdt;
  results->Branch("Method_Likelihood",&Method_Likelihood ,"Method_Likelihood/D" );
  results->Branch("BDTG method", &bdt, "BDTG method" );       
  */

 

  std::cout << "--- Select signal sample" << std::endl;
  //TTree* theTree = (TTree*)input->Get("MCtree");
  TTree* theTree = (TTree*)input_Background->Get("DecayTree");
 
  //if(mode<3) TCut cut = TCut("time1>0. && abs(phi_mass-1019.455)<15");
//   else TCut cut = TCut("time1>0. && abs(phi_mass-1019.455)<12 && abs(phi1_mass-1019.455)<12");
  //else TCut cut = TCut("time1>0. && abs(phi_mass-1019.455)<15 && abs(phi1_mass-1019.455)<15");
  //TFile* f_out  =new TFile("../output/MCBsphif0_after_bdt.root","RECREATE");
  TFile* f_out;
  string oldLabel="mvaVars_vetoes";
  string newLabel="mva";
  filename.replace(filename.find(oldLabel), oldLabel.length(), newLabel);
  f_out = new TFile(filename.c_str(),"RECREATE");

  //TTree* smalltree = theTree->CopyTree(cut);
  TTree*  newtree = theTree->CloneTree(-1);
  //TTree* smalltree = theTree->CloneTree(-1);
  //TTree*  newtree = theTree->CloneTree(-1);
  float bdtg;
  TBranch*  b_bdtg = newtree->Branch("bdtg", &bdtg,"bdtg/F");  
  float bdt;
  TBranch*  b_bdt = newtree->Branch("bdt", &bdt,"bdt/F");  
  float bdtd;
  TBranch*  b_bdtd = newtree->Branch("bdtd", &bdtd,"bdtd/F");  
  float mlp;
  TBranch*  b_mlp = newtree->Branch("mlp", &mlp,"mlp/F");  
 

  //   Float_t userptsum, userpionpt, userptj, userdmj , uservchi2dof;
  // Float_t usermaxdphi; Float_t userptAsym;
  theTree->SetBranchAddress("B_s0_ln_FDCHI2", &B_s0_ln_FDCHI2 );
  theTree->SetBranchAddress("B_s0_ln_IPCHI2", &B_s0_ln_IPCHI2 );
  theTree->SetBranchAddress("B_s0_ln_EVCHI2", &B_s0_ln_EVCHI2 );
  theTree->SetBranchAddress("B_s0_PT_fiveGeV",&B_s0_PT_fiveGeV);
  theTree->SetBranchAddress("B_s0_Eta",       &B_s0_Eta       );
  theTree->SetBranchAddress("minK_PT_GeV",    &minK_PT_GeV    );
  theTree->SetBranchAddress("minK_ln_IPCHI2", &minK_ln_IPCHI2 );
  // Efficiency calculator for cut method
  Int_t    nSelCutsGA = 0;
  Double_t effS       = 0.7;

  std::vector<Float_t> vecVar(4); // vector for EvaluateMVA tests
  Int_t num_entries = newtree->GetEntries();
  std::cout << "--- Processing: " << num_entries << " events" << std::endl;
  TStopwatch sw;
  sw.Start();
  for (Long64_t ievt=0; ievt<num_entries;ievt++) {

    // if (ievt%10000 == 0) std::cout << "--- ... Processing event: " << ievt << std::endl;

    newtree->GetEntry(ievt);
   
    //    var1 = userVar1 + userVar2;
    //       var2 = userVar1 - userVar2;
    
    // --- Return the MVA outputs and fill into histograms

    if (Use["CutsGA"]) {
       // Cuts is a special case: give the desired signal efficienciy
       Bool_t passed = reader->EvaluateMVA( "CutsGA method", effS );
       if (passed) nSelCutsGA++;
    }

    if (Use["Likelihood"   ])   {

	histLk     ->Fill( reader->EvaluateMVA( "Likelihood method"    ) );
	//	Method_Likelihood =  reader->EvaluateMVA( "Likelihood method"    ) ;
	//std::cout << Method_Likelihood << std::endl;
	//	results->Fill();
    }
    if (Use["LikelihoodD"  ])   histLkD    ->Fill( reader->EvaluateMVA( "LikelihoodD method"   ) );
    if (Use["LikelihoodPCA"])   histLkPCA  ->Fill( reader->EvaluateMVA( "LikelihoodPCA method" ) );
    if (Use["LikelihoodKDE"])   histLkKDE  ->Fill( reader->EvaluateMVA( "LikelihoodKDE method" ) );
    if (Use["LikelihoodMIX"])   histLkMIX  ->Fill( reader->EvaluateMVA( "LikelihoodMIX method" ) );
    if (Use["PDERS"        ])   histPD     ->Fill( reader->EvaluateMVA( "PDERS method"         ) );
    if (Use["PDERSD"       ])   histPDD    ->Fill( reader->EvaluateMVA( "PDERSD method"        ) );
    if (Use["PDERSPCA"     ])   histPDPCA  ->Fill( reader->EvaluateMVA( "PDERSPCA method"      ) );
    if (Use["KNN"          ])   histKNN    ->Fill( reader->EvaluateMVA( "KNN method"           ) );
    if (Use["HMatrix"      ])   histHm     ->Fill( reader->EvaluateMVA( "HMatrix method"       ) );
    if (Use["Fisher"       ])   histFi     ->Fill( reader->EvaluateMVA( "Fisher method"        ) );
    if (Use["FisherG"      ])   histFiG    ->Fill( reader->EvaluateMVA( "FisherG method"       ) );
    if (Use["BoostedFisher"])   histFiB    ->Fill( reader->EvaluateMVA( "BoostedFisher method" ) );
    if (Use["LD"           ])   histLD     ->Fill( reader->EvaluateMVA( "LD method"            ) );
    if (Use["MLP"          ]) {
	mlp =  reader->EvaluateMVA( "MLP method"           ) ;
      b_mlp->Fill();
      histNn     ->Fill( reader->EvaluateMVA( "MLP method"           ) );
    }
    if (Use["MLPBFGS"      ])   histNnbfgs ->Fill( reader->EvaluateMVA( "MLPBFGS method"       ) );
    if (Use["MLPBNN"       ])   histNnbnn  ->Fill( reader->EvaluateMVA( "MLPBNN method"        ) );
    if (Use["CFMlpANN"     ])   histNnC    ->Fill( reader->EvaluateMVA( "CFMlpANN method"      ) );
    if (Use["TMlpANN"      ])   histNnT    ->Fill( reader->EvaluateMVA( "TMlpANN method"       ) );
    if (Use["BDT"          ])  {
       histBdt    ->Fill( reader->EvaluateMVA( "BDT method"           ) );
       bdt = reader->EvaluateMVA( "BDT method"           ) ;
 	b_bdt->Fill();
    }
    if (Use["BDTD"         ])  {
      histBdtD   ->Fill( reader->EvaluateMVA( "BDTD method"          ) );
      bdtd =  reader->EvaluateMVA( "BDTD method"          );
	b_bdtd->Fill();
    }
    if (Use["BDTG"         ])  {
      histBdtG   ->Fill( reader->EvaluateMVA( "BDTG method"          ) );
      bdtg =  reader->EvaluateMVA( "BDTG method"        );
	b_bdtg->Fill();
	//cout <<  reader->EvaluateMVA( "BDTG method" )  <<endl;
    }
    if (Use["RuleFit"      ])   histRf     ->Fill( reader->EvaluateMVA( "RuleFit method"       ) );
    if (Use["SVM_Gauss"    ])   histSVMG   ->Fill( reader->EvaluateMVA( "SVM_Gauss method"     ) );
    if (Use["SVM_Poly"     ])   histSVMP   ->Fill( reader->EvaluateMVA( "SVM_Poly method"      ) );
    if (Use["SVM_Lin"      ])   histSVML   ->Fill( reader->EvaluateMVA( "SVM_Lin method"       ) );
    if (Use["FDA_MT"       ])   histFDAMT  ->Fill( reader->EvaluateMVA( "FDA_MT method"        ) );
    if (Use["FDA_GA"       ])   histFDAGA  ->Fill( reader->EvaluateMVA( "FDA_GA method"        ) );
    if (Use["Category"     ])   histCat    ->Fill( reader->EvaluateMVA( "Category method"      ) );
    if (Use["Plugin"       ])   histPBdt   ->Fill( reader->EvaluateMVA( "P_BDT method"         ) );

    // Retrieve also per-event error
    if (Use["PDEFoam"]) {
       Double_t val = reader->EvaluateMVA( "PDEFoam method" );
       Double_t err = reader->GetMVAError();
       histPDEFoam   ->Fill( val );
       histPDEFoamErr->Fill( err );         
       if (err>1.e-50) histPDEFoamSig->Fill( val/err );
    }         

    // Retrieve probability instead of MVA output
    if (Use["Fisher"])   {
       probHistFi  ->Fill( reader->GetProba ( "Fisher method" ) );
       rarityHistFi->Fill( reader->GetRarity( "Fisher method" ) );
    }
  }

  // Get elapsed time
  sw.Stop();
  std::cout << "--- End of event loop: "; sw.Print();

  // Get efficiency for cuts classifier
  if (Use["CutsGA"]) std::cout << "--- Efficiency for CutsGA method: " << double(nSelCutsGA)/theTree->GetEntries()
                              << " (for a required signal efficiency of " << effS << ")" << std::endl;

  if (Use["CutsGA"]) {

    // test: retrieve cuts for particular signal efficiency
    // CINT ignores dynamic_casts so we have to use a cuts-secific Reader function to acces the pointer  
    TMVA::MethodCuts* mcuts = reader->FindCutsMVA( "CutsGA method" ) ;

    if (mcuts) {      
       std::vector<Double_t> cutsMin;
       std::vector<Double_t> cutsMax;
       mcuts->GetCuts( 0.7, cutsMin, cutsMax );
       std::cout << "--- -------------------------------------------------------------" << std::endl;
       std::cout << "--- Retrieve cut values for signal efficiency of 0.7 from Reader" << std::endl;
       for (UInt_t ivar=0; ivar<cutsMin.size(); ivar++) {
          std::cout << "... Cut: " 
                    << cutsMin[ivar] 
                    << " < \"" 
                    << mcuts->GetInputVar(ivar)
                    << "\" <= " 
                    << cutsMax[ivar] << std::endl;
       }
       std::cout << "--- -------------------------------------------------------------" << std::endl;
    }
  }

 newtree->Write();
  f_out->Close();  


  // --- Write histograms

  TFile *target  = new TFile( "TMVApp.root","RECREATE" );
  if (Use["Likelihood"   ])   histLk     ->Write();
  if (Use["LikelihoodD"  ])   histLkD    ->Write();
  if (Use["LikelihoodPCA"])   histLkPCA  ->Write();
  if (Use["LikelihoodKDE"])   histLkKDE  ->Write();
  if (Use["LikelihoodMIX"])   histLkMIX  ->Write();
  if (Use["PDERS"        ])   histPD     ->Write();
  if (Use["PDERSD"       ])   histPDD    ->Write();
  if (Use["PDERSPCA"     ])   histPDPCA  ->Write();
  if (Use["KNN"          ])   histKNN    ->Write();
  if (Use["HMatrix"      ])   histHm     ->Write();
  if (Use["Fisher"       ])   histFi     ->Write();
  if (Use["FisherG"      ])   histFiG    ->Write();
  if (Use["BoostedFisher"])   histFiB    ->Write();
  if (Use["LD"           ])   histLD     ->Write();
  if (Use["MLP"          ])   histNn     ->Write();
  if (Use["MLPBFGS"      ])   histNnbfgs ->Write();
  if (Use["MLPBNN"       ])   histNnbnn  ->Write();
  if (Use["CFMlpANN"     ])   histNnC    ->Write();
  if (Use["TMlpANN"      ])   histNnT    ->Write();
  if (Use["BDT"          ])   histBdt    ->Write();
  if (Use["BDTD"         ])   histBdtD   ->Write();
  if (Use["BDTG"         ])   histBdtG   ->Write(); 
  if (Use["RuleFit"      ])   histRf     ->Write();
  if (Use["SVM_Gauss"    ])   histSVMG   ->Write();
  if (Use["SVM_Poly"     ])   histSVMP   ->Write();
  if (Use["SVM_Lin"      ])   histSVML   ->Write();
  if (Use["FDA_MT"       ])   histFDAMT  ->Write();
  if (Use["FDA_GA"       ])   histFDAGA  ->Write();
  if (Use["Category"     ])   histCat    ->Write();
  if (Use["Plugin"       ])   histPBdt   ->Write();


  // results->Write();
  // Write also error and significance histos
  if (Use["PDEFoam"]) { histPDEFoam->Write(); histPDEFoamErr->Write(); histPDEFoamSig->Write(); }

  // Write also probability hists
  if (Use["Fisher"]) { if (probHistFi != 0) probHistFi->Write(); if (rarityHistFi != 0) rarityHistFi->Write(); }
  target->Close();

  std::cout << "--- Created_prob.root file: \"TMVApp.root\" containing the MVA output histograms" << std::endl;
  delete reader;
  
  std::cout << "==> TMVAClassificationApplication is done!" << endl << std::endl;
} 
コード例 #12
0
int main(int argc, char** argv) {
 
 if(argc != 2)
 {
  std::cerr << ">>>>> analysis.cpp::usage: " << argv[0] << " configFileName" << std::endl ;
  return 1;
 }
 
 std::cout << " " << std::endl;
 std::cout << " " << std::endl;
 std::cout << "      " << std::endl;
 std::cout << "        \\  | \\ \\     /  \\            \\         |      |     \\ \\     /            _)         |      |           ___ \\       |     \\ \\     /  __ )   ____|  " << std::endl;
 std::cout << "       |\\/ |  \\ \\   /  _ \\          _ \\     _` |   _` |      \\ \\   /  _` |   __|  |   _` |  __ \\   |   _ \\        ) |      |      \\ \\   /   __ \\   |      " << std::endl;     
 std::cout << "       |   |   \\ \\ /  ___ \\        ___ \\   (   |  (   |       \\ \\ /  (   |  |     |  (   |  |   |  |   __/       __/   \\   |       \\ \\ /    |   |  __|    " << std::endl;
 std::cout << "      _|  _|    \\_/ _/    _\\     _/    _\\ \\__,_| \\__,_|        \\_/  \\__,_| _|    _| \\__,_| _.__/  _| \\___|     _____| \\___/         \\_/    ____/  _|      " << std::endl;
 std::cout << "                                                                                                                                                          " << std::endl;
 std::cout << " " << std::endl;
 std::cout << " " << std::endl;
 std::cout << " " << std::endl;
 
 
 
 // Parse the config file                                                                                                                                                          
 parseConfigFile (argv[1]) ;
 
 std::string treeName  = gConfigParser -> readStringOption("Input::treeName");
 std::string fileSamples = gConfigParser -> readStringOption("Input::fileSamples");
 std::string inputDirectory = gConfigParser -> readStringOption("Input::inputDirectory");
 
 std::string inputBeginningFile = "out_NtupleProducer_"; 
 try {
  inputBeginningFile = gConfigParser -> readStringOption("Input::inputBeginningFile");
 }
 catch (char const* exceptionString){
  std::cerr << " exception = " << exceptionString << std::endl;
 }
 std::cout << ">>>>> Input::inputBeginningFile  " << inputBeginningFile  << std::endl;  
 
 
 //==== list of methods
 std::string              MVADirectory           = gConfigParser -> readStringOption    ("Options::MVADirectory");
 std::vector<std::string> vectorMyMethodList     = gConfigParser -> readStringListOption("Options::MVAmethods");
 std::vector<std::string> vectorMyMethodMassList = gConfigParser -> readStringListOption("Options::MVAmethodsMass");
 
 std::string outputDirectory = gConfigParser -> readStringOption("Output::outputDirectory");
 
 //---- variables 
 
 //-- by YY --
 float pt1;
 float pt2;
 float dphill;
 float drll;
 float mll;
 float channel;
 float mth;
 float mjj;
 float detajj;
 float jeteta1;
 
 
 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 TFile* outputRootFile[500];
 TTree* cloneTreeJetLepVect[500];
 
 
 TTree *treeJetLepVect[500];
 TFile* file[500];
 
 char *nameSample[1000];
 char *nameHumanReadable[1000];
 char* xsectionName[1000];
 
 char nameFileIn[1000];
 sprintf(nameFileIn,"%s",fileSamples.c_str());
 
 int numberOfSamples = ReadFile(nameFileIn, nameSample, nameHumanReadable, xsectionName);
 
 double Normalization[1000];
 double xsection[1000];
 
 for (int iSample=0; iSample<numberOfSamples; iSample++){ 
  char nameFile[20000];
  sprintf(nameFile,"%s/%s%s.root",inputDirectory.c_str(),inputBeginningFile.c_str(),nameSample[iSample]);  
  
  file[iSample] = new TFile(nameFile, "READ");
  
  treeJetLepVect[iSample] = (TTree*) file[iSample]->Get(treeName.c_str());
  char nameTreeJetLep[100];
  sprintf(nameTreeJetLep,"treeJetLep_%d",iSample); 
  treeJetLepVect[iSample]->SetName(nameTreeJetLep);
  
  
  
  treeJetLepVect[iSample] -> SetBranchAddress("pt1",  &pt1);
  treeJetLepVect[iSample] -> SetBranchAddress("pt2",  &pt2);
  treeJetLepVect[iSample] -> SetBranchAddress("dphill",   &dphill);
  treeJetLepVect[iSample] -> SetBranchAddress("drll", &drll);
  treeJetLepVect[iSample] -> SetBranchAddress("mll", &mll);
  treeJetLepVect[iSample] -> SetBranchAddress("channel", &channel);
  treeJetLepVect[iSample] -> SetBranchAddress("mth", &mth);
  treeJetLepVect[iSample] -> SetBranchAddress("mjj", &mjj);
  treeJetLepVect[iSample] -> SetBranchAddress("detajj", &detajj);
  treeJetLepVect[iSample] -> SetBranchAddress("jeteta1", &jeteta1);
  
  
  sprintf(nameFile,"%s/%s%s.root",outputDirectory.c_str(),inputBeginningFile.c_str(),nameSample[iSample]);    
  outputRootFile[iSample] = new TFile ( nameFile, "RECREATE") ;
  outputRootFile[iSample] -> cd () ;
  cloneTreeJetLepVect[iSample] = treeJetLepVect[iSample] -> CloneTree (0) ;
 }
 
 
 
 for (int iSample=0; iSample<numberOfSamples; iSample++){ 
  file[iSample] -> cd();
  ///==== loop ====
  Long64_t nentries = treeJetLepVect[iSample]->GetEntries();
  for (Long64_t iEntry = 0; iEntry < nentries; iEntry++){
   treeJetLepVect[iSample]->GetEntry(iEntry);
   cloneTreeJetLepVect[iSample] -> Fill () ; 
  }
 }
 
 
 /**
  * cycle on MVA (method-mass)
  * * cycle on samples
  * * * cycle on events
  */
 
 for (int iMVA = 0; iMVA < vectorMyMethodList.size(); iMVA++) {
  std::cout << " vectorMyMethodList[" << iMVA << "] = " << vectorMyMethodList.at(iMVA) << std::endl;
  TString myMethodList = Form ("%s",vectorMyMethodList.at(iMVA).c_str());
  for (int iMVAMass = 0; iMVAMass < vectorMyMethodMassList.size(); iMVAMass++) {
   std::cout << " vectorMyMethodMassList[" << iMVAMass << "] = " << vectorMyMethodMassList.at(iMVAMass) << std::endl;
   
   TMVA::Reader *TMVAreader = new TMVA::Reader( "!Color:!Silent" );
   Float_t input_variables[1000];
      
   TMVAreader->AddVariable("lep1pt",    &input_variables[0]);
   TMVAreader->AddVariable("lep2pt",    &input_variables[1]);
   TMVAreader->AddVariable("dPhi",      &input_variables[2]);
   TMVAreader->AddVariable("dR",        &input_variables[3]);
   TMVAreader->AddVariable("dilmass",   &input_variables[4]);
   TMVAreader->AddVariable("type",      &input_variables[5]);
   TMVAreader->AddVariable("mt",        &input_variables[6]);
   TMVAreader->AddVariable("mjj",       &input_variables[7]);
   TMVAreader->AddVariable("detajj",    &input_variables[8]);   
   TMVAreader->AddVariable("jet1eta",   &input_variables[9]);
   
   TString myMethodMassList = Form ("%s",vectorMyMethodMassList.at(iMVAMass).c_str());
   TString weightfile = Form ("%s/weights_%s_testVariables/TMVAClassification_%s.weights.xml",MVADirectory.c_str(),myMethodMassList.Data(),myMethodList.Data());
   
   std::cout << " myMethodList = " << myMethodList.Data() << std::endl;
   std::cout << " weightfile   = " << weightfile.Data()   << std::endl;
   TMVAreader->BookMVA( myMethodList, weightfile );
   
   
   for (int iSample=0; iSample<numberOfSamples; iSample++){ 
    std::cout << " iSample = " << iSample << " :: " << numberOfSamples << std::endl;
    file[iSample] -> cd();
    Double_t MVA_Value;
    TBranch *newBranch;
    
    TString methodName4Tree = Form ("%s_%s_MVAHiggs",myMethodList.Data(),myMethodMassList.Data());
    TString methodName4Tree2 =  Form ("%s_%s_MVAHiggs_2JVBF/D",myMethodList.Data(),myMethodMassList.Data());
    newBranch = cloneTreeJetLepVect[iSample]->Branch(methodName4Tree,&MVA_Value,methodName4Tree2);
    
    
    ///==== loop ====
    Long64_t nentries = treeJetLepVect[iSample]->GetEntries();
    
    for (Long64_t iEntry = 0; iEntry < nentries; iEntry++){
     if((iEntry%1000) == 0) std::cout << ">>>>> analysis::GetEntry " << iEntry << " : " << nentries << std::endl;   
     
     treeJetLepVect[iSample]->GetEntry(iEntry);
     
     input_variables[0]  = static_cast<Float_t>(pt1);
     input_variables[1]  = static_cast<Float_t>(pt2);
     input_variables[2]  = static_cast<Float_t>(dphill);
     input_variables[3]  = static_cast<Float_t>(drll);
     input_variables[4]  = static_cast<Float_t>(mll);
     
     float smurfChannel = -1;
     if ( channel == 0 ) smurfChannel = 0 ;
     if ( channel == 1 ) smurfChannel = 3 ;
     if ( channel == 2 ) smurfChannel = 2 ;
     if ( channel == 3 ) smurfChannel = 1 ;
     input_variables[5]  = static_cast<Float_t>(smurfChannel);
     
     input_variables[6]  = static_cast<Float_t>(mth);
     input_variables[7]  = static_cast<Float_t>(mjj);
     input_variables[8]  = static_cast<Float_t>(detajj);
     input_variables[9]  = static_cast<Float_t>(jeteta1);
     
     MVA_Value = TMVAreader->EvaluateMVA(myMethodList);
     
     newBranch -> Fill();
     
    }
   }
  }
 }
 
 
 for (int iSample=0; iSample<numberOfSamples; iSample++){ 
  outputRootFile[iSample]->cd();
  
  // save only the new version of the tree
  //   treeJetLepVect[iSample]->Write("", TObject::kOverwrite);
  cloneTreeJetLepVect[iSample] -> SetName (treeName.c_str());
  //   cloneTreeJetLepVect[iSample] -> AutoSave () ;
  
  cloneTreeJetLepVect[iSample]->Write(treeName.c_str(),TObject::kOverwrite);
  
  outputRootFile[iSample] -> Close () ;
 }
 
 
}
コード例 #13
0
void TMVAClassificationApplication( TString SampleName = "" ) 
{   
#ifdef __CINT__
   gROOT->ProcessLine( ".O0" ); // turn off optimization in CINT
#endif

   //---------------------------------------------------------------

   // This loads the library
   TMVA::Tools::Instance();

   // Default MVA methods to be trained + tested
   std::map<std::string,int> Use;

   // --- Cut optimisation
   Use["Cuts"]            = 0;
   Use["CutsD"]           = 1;
   Use["CutsPCA"]         = 0;
   Use["CutsGA"]          = 0;
   Use["CutsSA"]          = 0;
   // 
   // --- 1-dimensional likelihood ("naive Bayes estimator")
   Use["Likelihood"]      = 0;
   Use["LikelihoodD"]     = 1; // the "D" extension indicates decorrelated input variables (see option strings)
   Use["LikelihoodPCA"]   = 0; // the "PCA" extension indicates PCA-transformed input variables (see option strings)
   Use["LikelihoodKDE"]   = 0;
   Use["LikelihoodMIX"]   = 0;
   //
   // --- Mutidimensional likelihood and Nearest-Neighbour methods
   Use["PDERS"]           = 0;
   Use["PDERSD"]          = 0;
   Use["PDERSPCA"]        = 0;
   Use["PDEFoam"]         = 0;
   Use["PDEFoamBoost"]    = 0; // uses generalised MVA method boosting
   Use["KNN"]             = 0; // k-nearest neighbour method
   //
   // --- Linear Discriminant Analysis
   Use["LD"]              = 0; // Linear Discriminant identical to Fisher
   Use["Fisher"]          = 0;
   Use["FisherG"]         = 0;
   Use["BoostedFisher"]   = 0; // uses generalised MVA method boosting
   Use["HMatrix"]         = 0;
   //
   // --- Function Discriminant analysis
   Use["FDA_GA"]          = 0; // minimisation of user-defined function using Genetics Algorithm
   Use["FDA_SA"]          = 0;
   Use["FDA_MC"]          = 0;
   Use["FDA_MT"]          = 0;
   Use["FDA_GAMT"]        = 0;
   Use["FDA_MCMT"]        = 0;
   //
   // --- Neural Networks (all are feed-forward Multilayer Perceptrons)
   Use["MLP"]             = 1; // Recommended ANN
   Use["MLPBFGS"]         = 1; // Recommended ANN with optional training method
   Use["MLPBNN"]          = 1; // Recommended ANN with BFGS training method and bayesian regulator
   Use["CFMlpANN"]        = 0; // Depreciated ANN from ALEPH
   Use["TMlpANN"]         = 0; // ROOT's own ANN
   //
   // --- Support Vector Machine 
   Use["SVM"]             = 0;
   // 
   // --- Boosted Decision Trees
   Use["BDT"]             = 1; // uses Adaptive Boost
   Use["BDTG"]            = 1; // uses Gradient Boost
   Use["BDTB"]            = 0; // uses Bagging
   Use["BDTD"]            = 1; // decorrelation + Adaptive Boost
   // 
   // --- Friedman's RuleFit method, ie, an optimised series of cuts ("rules")
   Use["RuleFit"]         = 0;
   // ---------------------------------------------------------------
   Use["Plugin"]          = 0;
   Use["Category"]        = 0;
   Use["SVM_Gauss"]       = 0;
   Use["SVM_Poly"]        = 0;
   Use["SVM_Lin"]         = 0;

   std::cout << std::endl;
   std::cout << "==> Start TMVAClassificationApplication" << std::endl;

   // --- Create the Reader object

   TMVA::Reader *reader = new TMVA::Reader( "!Color:!Silent" );    

   // Create a set of variables and declare them to the reader
   // - the variable names MUST corresponds in name and type to those given in the weight file(s) used
   Float_t var1, var2;
   Float_t var3, var4;
   reader->AddVariable( "jet_csv[0]", &var1 );
   reader->AddVariable( "jet_csv[1]", &var2 );
   reader->AddVariable( "jet_csv[2]", &var3 );
   reader->AddVariable( "jet_csv[3]", &var4 );
   // --- Book the MVA methods

   TString dir    = "weights/";
   TString prefix = "TMVAClassification";

   // Book method(s)
   for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) {
      if (it->second) {
         TString methodName = TString(it->first) + TString(" method");
         TString weightfile = dir + prefix + TString("_") + TString(it->first) + TString(".weights.xml");
         reader->BookMVA( methodName, weightfile ); 
      }
   }
   
   // Prepare input tree (this must be replaced by your data source)
   // in this example, there is a toy tree with signal and one with background events
   // we'll later on use only the "signal" events for the test in this example.
   //   
   TFile *input(0);
   TString fname = Form("file:/cms/home/youngjo/CATools/ntuple_v741/result4/result_%s.root",SampleName.Data());   
   if (!gSystem->AccessPathName( fname )) 
      input = TFile::Open( fname ); // check if file in local directory exists
//   else    
//      input = TFile::Open( "http://root.cern.ch/files/tmva_class_example.root" ); // if not: download from ROOT server
   
   if (!input) {
      std::cout << "ERROR: could not open data file" << fname.Data() << std::endl;
      exit(1);
   }
   TH1F *htotevents = (TH1F*) input->Get("hNEvent");
   std::cout << "--- TMVAClassificationApp    : Using input file: " << input->GetName() << std::endl;
   
   // --- Event loop

   // Prepare the event tree
   // - here the variable names have to corresponds to your tree
   // - you can use the same variables as above which is slightly faster,
   //   but of course you can use different ones and copy the values inside the event loop
   //
   std::cout << "--- Select signal sample" << std::endl;
   TTree* theTree = (TTree*)input->Get("myresult2");

   TFile *target  = new TFile( Form("result_TMVA_%s.root",SampleName.Data()),"RECREATE" );
   TTree *tree_ = new TTree(Form("myresult3"),"");
   FlatTree* fevent_ = new FlatTree();
   FlatTree* fevent2_ = new FlatTree();
   fevent_->book(tree_);
   fevent2_->setBranch(theTree);

   // Efficiency calculator for cut method
   Int_t    nSelCutsGA = 0;
   Double_t effS       = 0.7;

   std::cout << "--- Processing: " << theTree->GetEntries() << " events" << std::endl;
   TStopwatch sw;
   sw.Start();
   int counter=0;
   for (Long64_t ievt=0; ievt<theTree->GetEntries();ievt++) {

      if (ievt%1000 == 0) std::cout << "--- ... Processing event: " << ievt << std::endl;
      
      fevent_->clear();
      theTree->GetEntry(ievt);
      fevent_->copy(fevent2_);
      counter++;
      //if(counter>2000) break;
      var1 = fevent_->jet_csv_->at(0);
      var2 = fevent_->jet_csv_->at(1);
      var3 = fevent_->jet_csv_->at(2);
      var4 = fevent_->jet_csv_->at(3);

      //fevent_->CutsD_     = (float) 99.;
      fevent_->LikelihoodD_= (float) reader->EvaluateMVA( "LikelihoodD method"   ) ;
      fevent_->MLP_        = (float) reader->EvaluateMVA( "MLP method"     ) ;
      fevent_->MLPBFGS_    = (float) reader->EvaluateMVA( "MLPBFGS method" ) ;
      fevent_->MLPBNN_     = (float) reader->EvaluateMVA( "MLPBNN method"  ) ;
      fevent_->BDT_        = (float) reader->EvaluateMVA( "BDT method"     ) ;
      fevent_->BDTD_       = (float) reader->EvaluateMVA( "BDTD method"    ) ;
      fevent_->BDTG_       = (float) reader->EvaluateMVA( "BDTG method"    ) ;

      tree_->Fill();
   }

   // Get elapsed time
   sw.Stop();
   std::cout << "--- End of event loop: "; sw.Print();


   // --- Write histograms
   tree_->Write();
   htotevents->Write();
   target->Close();
   std::cout << "--- Created root file: \"result_TMVA_"+SampleName+".root\" containing the MVA output histograms" << std::endl;
  
   delete reader;
   std::cout << "==> TMVAClassificationApplication is done!" << endl << std::endl;
} 
コード例 #14
0
void tmva_reader(const int type = 1){
// type = 1 -> Signal
// type = 2 -> Continuum
// type = 0 -> Data
/////////////////
// TMVA Reader //
/////////////////
  rooksfw* ksfw  = new rooksfw("ksfw0", ksfw0_alpha, ksfw0_sigpdf, ksfw0_bkgpdf);
  TMVA::Reader* reader  = new TMVA::Reader("!Color:!Silent:V");
//  TMVA::Reader* reader1 = new TMVA::Reader("!Color:!Silent:V");

  string fname;
  if(type == 1)       fname = string("/home/vitaly/B0toDh0/Tuples/fil_b2dh_sigmc.root");
  else if(type == 2 ) fname = string("/home/vitaly/B0toDh0/Tuples/fil_b2dh_cont.root");
  else if(type == 0)  fname = string("/home/vitaly/B0toDh0/Tuples/fil_b2dh_data.root");
  else{
      cout << "Wrong type " << type << endl;
      return;
  }
  stinrg treename;
  if(type) treename = string("TEventEx");
  else     treename = string("TEvent");

  TFile *ifile = TFile::Open(fname.c_str());
  TTree *tree = (TTree*)ifile->Get(treename.c_str());

  if(type == 1)       fname = string("fil_b2dh_sig.root");
  else if(type == 2) fname = string("fil_b2dh_cont.root");
  else if(type == 0)  fname = string("fil_b2dh_data.root");
  TFile ofile(fname.c_str(),"RECREATE");
  TTree *TEvent = new TTree("TEvent","TEvent");

  double k0vars[17];

  Double_t cos_b0,p_ks,p_pp,p_pm,p_pi0,chi2_ndf_D0,chi2_ndf_B0,chi2_tag_vtx,cos_thr,thr_sig,thr_oth;
  Double_t k0mm2,k0et,k0hso00,k0hso02,k0hso04,k0hso10,k0hso12,k0hso14,k0hso20,k0hso22,k0hso24,k0hoo0,k0hoo1,k0hoo2,k0hoo3,k0hoo4;
  Double_t k1mm2,k1et,k1hso00,k1hso02,k1hso04,k1hso10,k1hso12,k1hso14,k1hso20,k1hso22,k1hso24,k1hoo0,k1hoo1,k1hoo2,k1hoo3,k1hoo4;
  Double_t pi0_chi2,egamma,ptgamma;
  Int_t ndf_tag_vtx;

  Double_t mbc,de,mp,mm,dz,dt,atckpi_max,mpi0,mk,md;
  Double_t bdt,bdtg,bdts,bdtgs,lh,bdtlh,bdtglh,bdtlhs,bdtglhs;
  Double_t ks_dr,ks_dz,ks_dphi,ks_fl,tag_LH,tag_LH_err,dzerr;
  Int_t phsp,bin,exp,run,evtn;

  Float_t m_cos_b0,m_p_ks,m_p_pp,m_p_pm,m_p_pi0,m_chi2_ndf_D0,m_chi2_ndf_B0,m_chi2_ndf_tag_vtx,m_cos_thr,m_thr_sig,m_thr_oth;
  Float_t m_ks_dr,m_ks_dz,m_ks_dphi,m_ks_fl,m_tag_LH_err,m_dzerr;
  Float_t m_pi0_chi2,m_egamma,m_ptgamma,m_lh;
  Float_t m_k1mm2,m_k1et,m_k1hso00,m_k1hso02,m_k1hso04,m_k1hso10,m_k1hso12,m_k1hso14,m_k1hso20,m_k1hso22,m_k1hso24,m_k1hoo0,m_k1hoo1,m_k1hoo2,m_k1hoo3,m_k1hoo4;

  Double_t mp_mc,mm_mc;
  Double_t t_mc,d0_t_mc;
  Double_t dt_mc,dz_mc;
  Int_t bin_mc;
  Int_t flv_mc,d0_flv_mc;
  Int_t mcflag,d0_mcflag;
  Int_t b0f,d0f;

  tree->SetBranchAddress("mp_mc",&mp_mc);
  tree->SetBranchAddress("mm_mc",&mm_mc);
  tree->SetBranchAddress("t_mc",&t_mc);
  tree->SetBranchAddress("d0_t_mc",&d0_t_mc);
  tree->SetBranchAddress("dt_mc",&dt_mc);
  tree->SetBranchAddress("dz_mc",&dz_mc);
  tree->SetBranchAddress("bin_mc",&bin_mc);
  tree->SetBranchAddress("flv_mc",&flv_mc);
  tree->SetBranchAddress("d0_flv_mc",&d0_flv_mc);
//  tree->SetBranchAddress("mcflag",&mcflag);
//  tree->SetBranchAddress("d0_mcflag",&d0_mcflag);
  tree->SetBranchAddress("b0f",&b0f);
  tree->SetBranchAddress("d0f",&d0f);

  tree->SetBranchAddress("exp",&exp);
  tree->SetBranchAddress("run",&run);
  tree->SetBranchAddress("evtn",&evtn);
  tree->SetBranchAddress("mbc",&mbc);
  tree->SetBranchAddress("de",&de);
  tree->SetBranchAddress("mp",&mp);
  tree->SetBranchAddress("mm",&mm);
  tree->SetBranchAddress("bin",&bin);
  tree->SetBranchAddress("dz",&dz);
  tree->SetBranchAddress("dt",&dt);
  tree->SetBranchAddress("atckpi_max",&atckpi_max);
  tree->SetBranchAddress("phsp",&phsp);
  tree->SetBranchAddress("mpi0_raw",&mpi0);
  tree->SetBranchAddress("mks_raw",&mk);
  tree->SetBranchAddress("md0_raw",&md);

  tree->SetBranchAddress("cos_b0",&cos_b0);
  tree->SetBranchAddress("p_ks",&p_ks);
//  tree->SetBranchAddress("p_pp",&p_pp);
//  tree->SetBranchAddress("p_pm",&p_pm);
//  tree->SetBranchAddress("p_pi0",&p_pi0);
  tree->SetBranchAddress("chi2_ndf_D0",&chi2_ndf_D0);
  tree->SetBranchAddress("chi2_ndf_B0",&chi2_ndf_B0);
  tree->SetBranchAddress("chi2_tag_vtx",&chi2_tag_vtx);
  tree->SetBranchAddress("ndf_tag_vtx",&ndf_tag_vtx);
  tree->SetBranchAddress("cos_thr",&cos_thr);
  tree->SetBranchAddress("thr_sig",&thr_sig);
  tree->SetBranchAddress("thr_oth",&thr_oth);
  tree->SetBranchAddress("tag_LH",&tag_LH);
  tree->SetBranchAddress("tag_LH_err",&tag_LH_err);
  tree->SetBranchAddress("dzerr",&dzerr);

  tree->SetBranchAddress("pi0_chi2",&pi0_chi2);
  tree->SetBranchAddress("egamma",&egamma);
//  tree->SetBranchAddress("ptgamma",&ptgamma);
//  tree->SetBranchAddress("lh",&m_lh);

  tree->SetBranchAddress("ks_dr",&ks_dr);
  tree->SetBranchAddress("ks_dz",&ks_dz);
  tree->SetBranchAddress("ks_dphi",&ks_dphi);
  tree->SetBranchAddress("ks_fl",&ks_fl);

  tree->SetBranchAddress("k0mm2",&k0mm2);
  tree->SetBranchAddress("k0et",&k0et);
  tree->SetBranchAddress("k0hso00",&k0hso00);
  tree->SetBranchAddress("k0hso02",&k0hso02);
  tree->SetBranchAddress("k0hso04",&k0hso04);
  tree->SetBranchAddress("k0hso10",&k0hso10);
  tree->SetBranchAddress("k0hso12",&k0hso12);
  tree->SetBranchAddress("k0hso14",&k0hso14);
  tree->SetBranchAddress("k0hso20",&k0hso20);
  tree->SetBranchAddress("k0hso22",&k0hso22);
  tree->SetBranchAddress("k0hso24",&k0hso24);
  tree->SetBranchAddress("k0hoo0",&k0hoo0);
  tree->SetBranchAddress("k0hoo1",&k0hoo1);
  tree->SetBranchAddress("k0hoo2",&k0hoo2);
  tree->SetBranchAddress("k0hoo3",&k0hoo3);
  tree->SetBranchAddress("k0hoo4",&k0hoo4);

  tree->SetBranchAddress("k1mm2",&k1mm2);
  tree->SetBranchAddress("k1et",&k1et);
  tree->SetBranchAddress("k1hso00",&k1hso00);
  tree->SetBranchAddress("k1hso02",&k1hso02);
  tree->SetBranchAddress("k1hso04",&k1hso04);
  tree->SetBranchAddress("k1hso10",&k1hso10);
  tree->SetBranchAddress("k1hso12",&k1hso12);
  tree->SetBranchAddress("k1hso14",&k1hso14);
  tree->SetBranchAddress("k1hso20",&k1hso20);
  tree->SetBranchAddress("k1hso22",&k1hso22);
  tree->SetBranchAddress("k1hso24",&k1hso24);
  tree->SetBranchAddress("k1hoo0",&k1hoo0);
  tree->SetBranchAddress("k1hoo1",&k1hoo1);
  tree->SetBranchAddress("k1hoo2",&k1hoo2);
  tree->SetBranchAddress("k1hoo3",&k1hoo3);
  tree->SetBranchAddress("k1hoo4",&k1hoo4);

  TEvent->Branch("mp_mc",&mp_mc,"mp_mc/D");
  TEvent->Branch("mm_mc",&mm_mc,"mm_mc/D");
  TEvent->Branch("t_mc",&t_mc,"t_mc/D");
  TEvent->Branch("d0_t_mc",&d0_t_mc,"d0_t_mc/D");
  TEvent->Branch("dt_mc",&dt_mc,"dt_mc/D");
  TEvent->Branch("dz_mc",&dz_mc,"dz_mc/D");
  TEvent->Branch("bin_mc",&bin_mc,"bin_mc/I");
  TEvent->Branch("flv_mc",&flv_mc,"flv_mc/I");
  TEvent->Branch("d0_flv_mc",&d0_flv_mc,"d0_flv_mc/I");
  TEvent->Branch("mcflag",&mcflag,"mcflag/I");
  TEvent->Branch("d0_mcflag",&d0_mcflag,"d0_mcflag/I");

  TEvent->Branch("exp",&exp,"exp/I");
  TEvent->Branch("run",&run,"run/I");
  TEvent->Branch("evtn",&evtn,"evtn/I");

  TEvent->Branch("b0f",&b0f,"b0f/I");
  TEvent->Branch("d0f",&d0f,"d0f/I");

  TEvent->Branch("mbc",&mbc,"mbc/D");
  TEvent->Branch("de",&de,"de/D");
  TEvent->Branch("bdt",&bdt,"bdt/D");
  TEvent->Branch("bdtg",&bdtg,"bdtg/D");
  TEvent->Branch("bdts",&bdts,"bdts/D");
  TEvent->Branch("bdtgs",&bdtgs,"bdtgs/D");
  TEvent->Branch("bdtlh",&bdtlh,"bdtlh/D");
  TEvent->Branch("bdtglh",&bdtglh,"bdtglh/D");
  TEvent->Branch("bdtlhs",&bdtlhs,"bdtlhs/D");
  TEvent->Branch("bdtglhs",&bdtglhs,"bdtglhs/D");
  TEvent->Branch("lh",&lh,"lh/D");
  
  TEvent->Branch("mp",&mp,"mp/D");
  TEvent->Branch("mm",&mm,"mm/D");
  TEvent->Branch("bin",&bin,"bin/I");
  
  TEvent->Branch("dz",&dz,"dz/D");
  TEvent->Branch("dt",&dt,"dt/D");
  
  TEvent->Branch("tag_LH",&tag_LH,"tag_LH/D");
  TEvent->Branch("tag_LH_err",&tag_LH_err,"tag_LH_err/D");

  TEvent->Branch("atckpi_max",&atckpi_max,"atckpi_max/D");
  TEvent->Branch("mpi0",&mpi0,"mpi0/D");
  TEvent->Branch("mk",&mk,"mk/D");
  TEvent->Branch("md",&md,"md/D");

  reader->AddVariable("abs(cos_b0)",&m_cos_b0);
//  reader->AddVariable("p_ks",&m_p_ks);
  reader->AddVariable("log(chi2_ndf_D0)",&m_chi2_ndf_D0);
//  reader->AddVariable("log(chi2_ndf_B0)",&m_chi2_ndf_B0);
//  reader->AddVariable("log(chi2_tag_vtx/ndf_tag_vtx)",&m_chi2_ndf_tag_vtx);
  reader->AddVariable("abs(cos_thr)",&m_cos_thr);
  reader->AddVariable("abs(thr_sig-0.885)",&m_thr_sig);
  reader->AddVariable("thr_oth",&m_thr_oth);
  reader->AddVariable("log(tag_LH_err)",&m_tag_LH_err);
  reader->AddVariable("log(dzerr)",&m_dzerr);
  reader->AddVariable("log(pi0_chi2)",&m_pi0_chi2);
  reader->AddVariable("log(egamma)",&m_egamma);

  reader->AddVariable("k1mm2",&m_k1mm2);
  reader->AddVariable("k1et",&m_k1et);
  reader->AddVariable("k1hso00",&m_k1hso00);
  reader->AddVariable("k1hso02",&m_k1hso02);
  reader->AddVariable("k1hso04",&m_k1hso04);
  reader->AddVariable("k1hso10",&m_k1hso10);
  reader->AddVariable("k1hso12",&m_k1hso12);
  reader->AddVariable("k1hso14",&m_k1hso14);
  reader->AddVariable("k1hso20",&m_k1hso20);
  reader->AddVariable("k1hso22",&m_k1hso22);
  reader->AddVariable("k1hso24",&m_k1hso24);
  reader->AddVariable("k1hoo0",&m_k1hoo0);
  reader->AddVariable("k1hoo1",&m_k1hoo1);
  reader->AddVariable("k1hoo2",&m_k1hoo2);
  reader->AddVariable("k1hoo3",&m_k1hoo3);
  reader->AddVariable("k1hoo4",&m_k1hoo4);

  reader->BookMVA("BDT","weights/MVAnalysis_BDT.weights.xml");
  reader->BookMVA("BDTG","weights/MVAnalysis_BDTG.weights.xml");
  reader->BookMVA("BDTs","weights/MVAnalysis_sig_BDT.weights.xml");
  reader->BookMVA("BDTGs","weights/MVAnalysis_sig_BDTG.weights.xml");

  reader1->AddVariable("abs(cos_b0)",&m_cos_b0);
  reader1->AddVariable("p_ks",&m_p_ks);
  reader1->AddVariable("log(chi2_ndf_D0)",&m_chi2_ndf_D0);
  reader1->AddVariable("log(chi2_ndf_B0)",&m_chi2_ndf_B0);
  reader1->AddVariable("log(chi2_tag_vtx/ndf_tag_vtx)",&m_chi2_ndf_tag_vtx);
  reader1->AddVariable("abs(cos_thr)",&m_cos_thr);
  reader1->AddVariable("abs(thr_sig-0.885)",&m_thr_sig);
  reader1->AddVariable("thr_oth",&m_thr_oth);
  reader1->AddVariable("log(tag_LH_err)",&m_tag_LH_err);
  reader1->AddVariable("log(dzerr)",&m_dzerr);
  reader1->AddVariable("log(pi0_chi2)",&m_pi0_chi2);
  reader1->AddVariable("log(egamma)",&m_egamma);
  reader1->AddVariable("lh",&m_lh);

  reader1->BookMVA("BDTlh","weights/MVAnalysis_lh_BDT.weights.xml");
  reader1->BookMVA("BDTGlh","weights/MVAnalysis_lh_BDTG.weights.xml");
  reader1->BookMVA("BDTlhs","weights/MVAnalysis_lh_sig_BDT.weights.xml");
  reader1->BookMVA("BDTGlhs","weights/MVAnalysis_lh_sig_BDTG.weights.xml");

  const int NTot = tree->GetEntries();
  for(int i=0; i<NTot; i++){
    if(!(i%10000)) cout << i << " events" << endl;
    tree->GetEvent(i);
//    if(!phsp) continue;
//    if(chi2_ndf_B0>1000) continue;

    m_cos_b0      = (float)TMath::Abs(cos_b0);
    m_p_ks        = (float)p_ks;
    m_p_pp        = (float)p_pp;
    m_p_pm        = (float)p_pm;
    m_p_pi0       = (float)p_pi0;
    m_chi2_ndf_D0 = (float)TMath::Log(chi2_ndf_D0);
    m_chi2_ndf_B0 = (float)TMath::Log(chi2_ndf_B0);
    m_chi2_ndf_tag_vtx = (float)TMath::Log(chi2_tag_vtx/ndf_tag_vtx);
    m_cos_thr     = (float)TMath::Abs(cos_thr);
    m_thr_sig     = (float)TMath::Abs(thr_sig-0.885);
    m_thr_oth     = (float)thr_oth;
    m_dzerr       = (float)TMath::Log(dzerr);
    m_ks_dr       = (float)ks_dr;
    m_ks_dz       = (float)ks_dz;
    m_ks_dphi     = (float)ks_dphi;
    m_ks_fl       = (float)ks_fl;
    m_pi0_chi2    = (float)TMath::Log(pi0_chi2);
    m_egamma      = (float)TMath::Log(egamma);
    m_ptgamma     = (float)ptgamma;
    m_k1mm2       = (float)k1mm2;
    m_k1et        = (float)k1et;
    m_k1hso00     = (float)k1hso00;
    m_k1hso02     = (float)k1hso02;
    m_k1hso04     = (float)k1hso04;
    m_k1hso10     = (float)k1hso10;
    m_k1hso12     = (float)k1hso12;
    m_k1hso14     = (float)k1hso14;
    m_k1hso20     = (float)k1hso20;
    m_k1hso22     = (float)k1hso22;
    m_k1hso24     = (float)k1hso24;
    m_k1hoo0      = (float)k1hoo0;
    m_k1hoo1      = (float)k1hoo1;
    m_k1hoo2      = (float)k1hoo2;
    m_k1hoo3      = (float)k1hoo3;
    m_k1hoo4      = (float)k1hoo4;
    m_lh          = 0;//(float)lh;

    k0vars[0]  = k0et;
    k0vars[1]  = k0hso00;
    k0vars[2]  = k0hso10;
    k0vars[3]  = k0hso20;
    k0vars[4]  = 0;//k0hso01;
    k0vars[5]  = k0hso02;
    k0vars[6]  = k0hso12;
    k0vars[7]  = k0hso22;
    k0vars[8]  = 0;//k0hso03;
    k0vars[9]  = k0hso04;
    k0vars[10] = k0hso14;
    k0vars[11] = k0hso24;
    k0vars[12] = k0hoo0;
    k0vars[13] = k0hoo1;
    k0vars[14] = k0hoo2;
    k0vars[15] = k0hoo3;
    k0vars[16] = k0hoo4;

    ksfw->input(k0mm2, k0vars);
    if(ksfw->ls() + ksfw->lb() > 0){
      lh = ksfw->ls()/(ksfw->ls()+ksfw->lb());
    } else{
      lh = -1;
    }

    bdt     = reader->EvaluateMVA("BDT");
    bdtg    = reader->EvaluateMVA("BDTG");
    bdts    = reader->EvaluateMVA("BDTs");
    bdtgs   = reader->EvaluateMVA("BDTGs");
    bdtlh   = reader1->EvaluateMVA("BDTlh");
    bdtglh  = reader1->EvaluateMVA("BDTGlh");
    bdtlhs  = reader1->EvaluateMVA("BDTlhs");
    bdtglhs = reader1->EvaluateMVA("BDTGlhs");

    TEvent->Fill();
  }

  TEvent->Write();
  ofile.Write();
  ofile.Close();
}
void TMVAClassificationApplication_cc1pcoh_bdt_ver3noveractFFFSI( TString myMethodList = "", TString fname)
{
#ifdef __CINT__
    gROOT->ProcessLine( ".O0" ); // turn off optimization in CINT
#endif
    
    //---------------------------------------------------------------
    
    // This loads the library
    TMVA::Tools::Instance();
    
    // Default MVA methods to be trained + tested
    std::map<std::string,int> Use;
    //
    // --- Boosted Decision Trees
    Use["BDT"]             = 1; // uses Adaptive Boost
    
    
    std::cout << std::endl;
    std::cout << "==> Start TMVAClassificationApplication" << std::endl;
    
    // Select methods (don't look at this code - not of interest)
    if (myMethodList != "") {
        for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) it->second = 0;
        
        std::vector<TString> mlist = gTools().SplitString( myMethodList, ',' );
        for (UInt_t i=0; i<mlist.size(); i++) {
            std::string regMethod(mlist[i]);
            
            if (Use.find(regMethod) == Use.end()) {
                std::cout << "Method \"" << regMethod
                << "\" not known in TMVA under this name. Choose among the following:" << std::endl;
                for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) {
                    std::cout << it->first << " ";
                }
                std::cout << std::endl;
                return;
            }
            Use[regMethod] = 1;
        }
    }
    
    // --------------------------------------------------------------------------------------------------
    
    // --- Create the Reader object
    
    TMVA::Reader *reader = new TMVA::Reader( "!Color:!Silent" );
    
    // Create a set of variables and declare them to the reader
    // - the variable names MUST corresponds in name and type to those given in the weight file(s) used
    Float_t mumucl, pmucl;
    Float_t pang_t, muang_t;
    Float_t veract;
    Float_t ppe, mupe;
    Float_t range, coplanarity;
    Float_t opening;//newadd
    
    reader->AddVariable( "mumucl", &mumucl );
    reader->AddVariable( "pmucl", &pmucl );
    reader->AddVariable( "pang_t", &pang_t );
    reader->AddVariable( "muang_t", &muang_t );
    //reader->AddVariable( "veract", &veract );
    reader->AddVariable( "ppe", &ppe);
    reader->AddVariable( "mupe", &mupe);
    reader->AddVariable( "range", &range);
    reader->AddVariable( "coplanarity", &coplanarity);
    reader->AddVariable( "opening", &opening);//newadd
    
    // Spectator variables declared in the training have to be added to the reader, too
    Int_t fileIndex, inttype;
    Float_t nuE, norm, totcrsne;
    reader->AddSpectator( "fileIndex", &fileIndex );
    reader->AddSpectator( "nuE", &nuE );
    reader->AddSpectator( "inttype", &inttype );
    reader->AddSpectator( "norm", &norm );
    reader->AddSpectator( "totcrsne", &totcrsne );
    reader->AddSpectator( "veract", &veract );
    
    // --- Book the MVA methods
    
    TString dir    = "weights/";
    TString prefix = "TMVAClassification_ver3noveractFFFSI";//newchange
    
    // Book method(s)
    for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) {
        if (it->second) {
            TString methodName = TString(it->first) + TString(" method");
            TString weightfile = dir + prefix + TString("_") + TString(it->first) + TString(".weights.xml");
            reader->BookMVA( methodName, weightfile );
        }
    }
    
    // Prepare input tree (this must be replaced by your data source)
    // in this example, there is a toy tree with signal and one with background events
    // we'll later on use only the "signal" events for the test in this example.
    //
    

    
    
    //for prange/pang_t corrected and other information included
    
    //TString fname = "/home/cvson/scraid2/cc1picoh/FIT/datafsi/datamc_merged_ccqe_addpidFFnew.root";
    //TString fname = "/home/cvson/scraid2/cc1picoh/FIT/datafsi/datamc_genie_merged_ccqe_addpidFFnew.root";
    //TString fname = "/home/cvson/scraid2/cc1picoh/FIT/datafsi/mc_merged_ccqe_addpidFFnew.root";
    //TString fname = "/home/cvson/scraid2/cc1picoh/FIT/datafsi/mcgenie_merged_ccqe_tot_addpidFFnew.root";
    //TString fname = "/home/cvson/scraid2/cc1picoh/FIT/datafsi/merged_ccqe_forResponseFunction.root";
   //TString fname = "/home/cvson/scraid2/cc1picoh/FIT/datafsi/pmnue_merged_ccqe_tot_addpidFFnew.root"; 
    std::cout << "--- Selecting data sample" << std::endl;
    TFile *pfile = new TFile(fname,"update");
    TTree* theTree = (TTree*)pfile->Get("tree");
    theTree->SetBranchAddress( "mumucl", &mumucl );
    theTree->SetBranchAddress( "pmucl", &pmucl );
    theTree->SetBranchAddress( "pang_t", &pang_t );
    theTree->SetBranchAddress( "muang_t", &muang_t );
    theTree->SetBranchAddress( "veract", &veract );
    theTree->SetBranchAddress( "ppe", &ppe);
    theTree->SetBranchAddress( "mupe", &mupe);
    theTree->SetBranchAddress( "range", &range);
    theTree->SetBranchAddress( "coplanarity", &coplanarity);
    theTree->SetBranchAddress( "opening", &opening);
    
 
    
    Int_t Ntrack;
    theTree->SetBranchAddress( "Ntrack", &Ntrack );
    
    
    Float_t pidfsi;
    TBranch *bpidfsi = theTree->Branch("pidfsi",&pidfsi,"pidfsi/F");
    
    
    
    std::vector<Float_t> vecVar(9); // vector for EvaluateMVA tests
    
    Long64_t nentries = theTree->GetEntriesFast();
    Long64_t iprintProcess = Long64_t(nentries/100.);
    
    std::cout << "--- Processing: " << nentries << " events" << std::endl;
    
    TStopwatch sw;
    sw.Start();
    for (Long64_t ievt=0; ievt<nentries;ievt++) {
        
        if (ievt%iprintProcess == 0) cout<<"Processing "<<int(ievt*100./nentries)<<"% of events"<<endl;
        theTree->GetEntry(ievt);
        Float_t pidfsi_tem;
        if (Use["BDT"])      {
            //if (Ntrack!=2) pidfsi_tem = -999;//changehere
            if (Ntrack<2) pidfsi_tem = -999;
            else pidfsi_tem = reader->EvaluateMVA("BDT method");
        }
        pidfsi = pidfsi_tem;
        bpidfsi->Fill();
        
    }
    theTree->Write();
    delete pfile;
    // Get elapsed time
    sw.Stop();
    std::cout << "--- End of event loop: "; sw.Print();
    
    delete reader;
    
    std::cout << "==> TMVAClassificationApplication is done!" << endl << std::endl;
} 
コード例 #16
0
void TMVAClassificationApplication( TString weightFile = "TMVAClassificationPtOrd_qqH115vsWZttQCD_Cuts.weights.xml",
                                    Double_t effS_ = 0.5 )
{

    TMVA::Tools::Instance();

    TMVA::Reader *reader = new TMVA::Reader( "!Color:!Silent" );

    Float_t pt1, pt2;
    Float_t Deta, Mjj;
    Float_t eta1,eta2;

    reader->AddVariable( "pt1", &pt1);
    reader->AddVariable( "pt2", &pt2);
    reader->AddVariable( "Deta",&Deta);
    reader->AddVariable( "Mjj", &Mjj);

    reader->AddSpectator("eta1",&eta1);
    reader->AddSpectator("eta2",&eta2);

    reader->BookMVA( "Cuts", TString("weights/")+weightFile );



    // mu+tau iso, OS, Mt<40
    TString fSignalName              = "/data_CMS/cms/lbianchini/VbfJetsStudy/looseSelection/nTupleVBFH115-PU-L.root";
    //TString fSignalName              = "/data_CMS/cms/lbianchini/VbfJetsStudy/nTupleVbf.root";
    // mu+tau iso, OS, Mt<40
    //TString fBackgroundNameDYJets    = "/data_CMS/cms/lbianchini/VbfJetsStudy/looseSelection/nTupleDYJets-madgraph-50-PU-L.root";
    TString fBackgroundNameDYJets    = "/data_CMS/cms/lbianchini/VbfJetsStudy/nTupleZJets.root";
    // Mt<40
    TString fBackgroundNameWJets     = "/data_CMS/cms/lbianchini/VbfJetsStudy/looseSelection/nTupleWJets-madgraph-PU-L.root";
    // Mt<40
    TString fBackgroundNameQCD       = "/data_CMS/cms/lbianchini/VbfJetsStudy/looseSelection/nTupleQCD-pythia-PU-L.root";
    // Mt<40
    TString fBackgroundNameTTbar     = "/data_CMS/cms/lbianchini/VbfJetsStudy/looseSelection/nTupleTT-madgraph-PU-L.root";


    TFile *fSignal(0);
    TFile *fBackgroundDYJets(0);
    TFile *fBackgroundWJets(0);
    TFile *fBackgroundQCD(0);
    TFile *fBackgroundTTbar(0);

    fSignal           = TFile::Open( fSignalName );
    fBackgroundDYJets = TFile::Open( fBackgroundNameDYJets );
    fBackgroundWJets  = TFile::Open( fBackgroundNameWJets );
    fBackgroundQCD    = TFile::Open( fBackgroundNameQCD );
    fBackgroundTTbar  = TFile::Open( fBackgroundNameTTbar );

    if(!fSignal || !fBackgroundDYJets || !fBackgroundWJets || !fBackgroundQCD || !fBackgroundTTbar) {
        std::cout << "ERROR: could not open files" << std::endl;
        exit(1);
    }

    TString tree = "outTreePtOrd";

    TTree *signal           = (TTree*)fSignal->Get(tree);
    TTree *backgroundDYJets = (TTree*)fBackgroundDYJets->Get(tree);
    TTree *backgroundWJets  = (TTree*)fBackgroundWJets->Get(tree);
    TTree *backgroundQCD    = (TTree*)fBackgroundQCD->Get(tree);
    TTree *backgroundTTbar  = (TTree*)fBackgroundTTbar->Get(tree);

    TCut mycuts = "pt1>0 && abs(eta1*eta2)/eta1/eta2<0";
    TCut mycutb = "pt1>0 && abs(eta1*eta2)/eta1/eta2<0";


    std::map<std::string,TTree*> tMap;
    tMap["qqH115"]=signal;
    tMap["Zjets"]=backgroundDYJets;
    tMap["Wjets"]=backgroundWJets;
    tMap["QCD"]=backgroundQCD;
    tMap["TTbar"]=backgroundTTbar;

    Double_t pt1_, pt2_;
    Double_t Deta_, Mjj_;

    for(std::map<std::string,TTree*>::iterator it = tMap.begin(); it != tMap.end(); it++) {

        TFile* dummy = new TFile("dummy.root","RECREATE");
        TTree* currentTree = (TTree*)(it->second)->CopyTree(mycuts);

        Int_t counter = 0;

        currentTree->SetBranchAddress( "pt1", &pt1_ );
        currentTree->SetBranchAddress( "pt2", &pt2_ );
        currentTree->SetBranchAddress( "Deta",&Deta_ );
        currentTree->SetBranchAddress( "Mjj", &Mjj_ );

        for (Long64_t ievt=0; ievt<currentTree->GetEntries(); ievt++) {
            currentTree->GetEntry(ievt);
            pt1  = pt1_;
            pt2  = pt2_;
            Deta = Deta_;
            Mjj  = Mjj_;
            if (ievt%1000000 == 0) {
                std::cout << "--- ... Processing event: " << ievt << std::endl;
                //cout << pt1 << ", " << pt2 << ", " << Deta << ", " << Mjj << endl;
            }
            if(reader->EvaluateMVA( "Cuts", effS_ )) counter++;
        }

        cout<< "Sample " << it->first << " has a rejection factor " << (Float_t)counter/(Float_t)currentTree->GetEntries() << "+/-" <<
            TMath::Sqrt((Float_t)counter/currentTree->GetEntries()*(1-counter/currentTree->GetEntries())/currentTree->GetEntries())
            << " for effS="<< effS_ << endl;
    }

    delete reader;

    std::cout << "==> TMVAClassificationApplication is done!" << endl << std::endl;

}
コード例 #17
0
int main(){
  TMVA::Tools::Instance();
  std::cout<<"Hello world"<<std::endl;

  TFile* OutputFile = TFile::Open("Outputfile.root","RECREATE");

  TMVA::Factory *factory = new TMVA::Factory( "TMVAClassification", OutputFile,
					      "!V:!Silent:Color:DrawProgressBar:Transformations=I;D;P;G,D:AnalysisType=Classification" );

  std::vector<VMVariable*> Variables;
  MVariable* Var3= new MVariable("var3",F,none);
  MVariable* Var4 = new MVariable("var4",F,none);
  Variables.push_back(Var3);
  Variables.push_back(Var4);
  MVariable* Var1 = new MVariable("var1",F,none);
  MVariable* Var2 = new MVariable("var2",F,none);

  MultiVariable* MyVar1 = new MultiVariable("Var1+Var2",sum);
  MyVar1->AddVariable(Var1);
  MyVar1->AddVariable(Var2);
  Variables.push_back(MyVar1);

  MultiVariable* MyVar2 = new MultiVariable("Minus",subtract);
  MyVar2->AddVariable(Var1);
  MyVar2->AddVariable(Var2);
  Variables.push_back(MyVar2);
  std::string InputName= "./tmva_class_exampleD.root";
  
  TFile *input = TFile::Open("./tmva_class_exampleD.root" );
  
  TTree *signal = (TTree*)input->Get("TreeS");
  TTree *background=(TTree*)input->Get("TreeB");

  Double_t signalWeight     = 1.0;
  Double_t backgroundWeight = 1.0;

  factory->AddSignalTree    ( signal,     signalWeight     );
  factory->AddBackgroundTree( background, backgroundWeight );

  for(auto v:Variables){
    factory->AddVariable(v->GetFactoryName(),v->GetType());
  }
  
  factory->SetBackgroundWeightExpression( "weight" );
  
  TCut mycuts = "";
  TCut mycutb = "";
  
  factory->PrepareTrainingAndTestTree( mycuts, mycutb,
				       "nTrain_Signal=0:nTrain_Background=0:SplitMode=Random:NormMode=NumEvents:!V" );
  
  std::vector<MClassifier*> Classifiers;
  
  Classifiers.push_back(new MClassifier(TMVA::Types::kBDT, "BDT",
					"!H:!V:NTrees=850:MinNodeSize=2.5%:MaxDepth=3:BoostType=AdaBoost:AdaBoostBeta=0.5:UseBaggedBoost:BaggedSampleFraction=0.5:SeparationType=GiniIndex:nCuts=20"));
  
  for(auto C:Classifiers){
    if(!(C->AddMethodToFactory(factory))){
      std::cout<<"Booking classifier failed"<<std::endl;
      return 1;
    }
  }

  factory->TrainAllMethods();
  
  factory->TestAllMethods();
  
  factory->EvaluateAllMethods();
  
  OutputFile->Close();
  
  delete factory;
  
  TMVA::Reader *reader = new TMVA::Reader( "!Color:!Silent" );    
  
  for(auto v: Variables){
    reader->AddVariable(v->GetFactoryName(),v->GetReaderAddress());
  }
  
  for(auto C:Classifiers){
    if(!(C->AddMethodToReader(reader,"./weights/","TMVAClassification"))){
      std::cout<<"Failed adding classifer to reader"<<std::endl;
      return 1;
    }
  }

  TFile* Input =  TFile::Open("./tmva_class_exampleD.root");
  TTree* TreeToEvaluate= (TTree*)Input->Get("TreeS");
  
  TFile* AppliedFile =  new TFile("AppliedFile.root","RECREATE");
  TTree* AppliedTree=TreeToEvaluate->CloneTree(0);
  
  for(auto C:Classifiers){
    if(!(C->MakeBranch(AppliedTree)))return 1;
  }
  
  for(auto Var:Variables){
    if(!(Var->SetBA(TreeToEvaluate))){
      std::cout<<"Problem Setting Branch addresses"<<std::endl;
      return 1;
    }
  }
  
  Long64_t N=TreeToEvaluate->GetEntries();
  LoopTimer LT(0.05);
  int vetoedeventcounter=0;
  double StartEntry=0.0;
  double LastEntry=0.0;
  Long64_t iStart=0;
  Long64_t iEnd=N;

  for(Long64_t i=iStart;i<iEnd;++i){
    LT.DeclareLoopStart(iEnd-iStart);
    TreeToEvaluate->GetEntry(i);
    bool useevent=true;
    for(auto Var:Variables){
      useevent=Var->DoOperation();
    }
    if(!useevent){
      vetoedeventcounter++;
      continue;
    }
    
    for(auto C:Classifiers){
      if(!(C->Apply(reader)))return 1;
    }
    
    AppliedTree->Fill();
  }

  AppliedTree->Write();
  AppliedFile->Close();
  std::cout<<"Got here"<<std::endl;
  // Compare Applied file from here with applied file from TMVA tests.
  TFile* ReadAppliedFile =  TFile::Open("AppliedFile.root");

  TTree* AppliedTreeRead=(TTree*)ReadAppliedFile->Get("TreeS");
  if(!AppliedTreeRead)std::cout<<"NUll pointer to tree"<<std::endl;
  double BDTResponse; AppliedTreeRead->SetBranchAddress("BDT_response",&BDTResponse);
  
  TFile* ReadTMVATestFile = TFile::Open("/home/tw/root-v5-34/tmva/test/TreeFile.root");
  if(!ReadTMVATestFile)std::cout<<"File open faild"<<std::endl;
  TTree* TMVATestTree=(TTree*)ReadTMVATestFile->Get("AppliedTree");
  if(!TMVATestTree)std::cout<<"NUll pointer to tree"<<std::endl;
  double TestBDTResponse; TMVATestTree->SetBranchAddress("BDT_response",&TestBDTResponse);
  Long64_t ATRN=AppliedTreeRead->GetEntries();
  Long64_t TTTN=TMVATestTree->GetEntries();
  std::cout<<"Entries in my tree= "<<ATRN<<std::endl;
  std::cout<<"Entries in TMVA tree= "<<TTTN<<std::endl;
  if(ATRN!=TTTN)std::cout<<"SOMETHING WRONG EVENTS NOT EQUAL"<<std::endl;
  std::vector<double> ATRValues;
  std::vector<double> TTTValues;
  for(int i=0;i<ATRN;++i){
    TMVATestTree->GetEntry(i);
    AppliedTreeRead->GetEntry(i);
    ATRValues.push_back(BDTResponse);
    TTTValues.push_back(TestBDTResponse);
    //    std::cout<<" MYTree = "<<BDTResponse<<" TMVATREE= "<<TestBDTResponse<<std::endl;
  }
  std::sort(ATRValues.begin(),ATRValues.end());
  std::sort(TTTValues.begin(),TTTValues.end());

  for(int i=0;i<TTTN;++i){
    std::cout<<" MY Value= "<<ATRValues.at(i)<<" TTT Value = "<<TTTValues.at(i)<<std::endl;
  }
  
}
コード例 #18
0
ファイル: GrowTree.C プロジェクト: swang373/VHbbUF
////////////////////////////////////////////////////////////////////////////////
/// Main                                                                     ///
////////////////////////////////////////////////////////////////////////////////
void GrowTree(TString process, std::string regMethod="BDTG", Long64_t beginEntry=0, Long64_t endEntry=-1)
{
    gROOT->SetBatch(1);
    TH1::SetDefaultSumw2(1);
    gROOT->LoadMacro("HelperFunctions.h");  //< make functions visible to TTreeFormula

    if (!TString(gROOT->GetVersion()).Contains("5.34")) {
        std::cout << "INCORRECT ROOT VERSION! Please use 5.34:" << std::endl;
        std::cout << "source /uscmst1/prod/sw/cms/slc5_amd64_gcc462/lcg/root/5.34.02-cms/bin/thisroot.csh" << std::endl;
        std::cout << "Return without doing anything." << std::endl;
        return;
    }

    const TString indir   = "/afs/cern.ch/work/d/degrutto/public/MiniAOD/ZnnHbb_Phys14_PU20bx25/skimV11/";
    const TString outdir  = "/afs/cern.ch/work/d/degrutto/public/MiniAOD/ZnnHbb_Phys14_PU20bx25/skimV11/step3/";
    const TString prefix  = "skim_";
    const TString suffix  = ".root";

    TFile *input = TFile::Open(indir + prefix + process + suffix);
    if (!input) {
        std::cout << "ERROR: Could not open input file." << std::endl;
        exit(1);
    }
    /// Make output directory if it doesn't exist
    if (gSystem->AccessPathName(outdir))
        gSystem->mkdir(outdir);

    std::cout << "--- GrowTree                 : Using input file: " << input->GetName() << std::endl;
    TTree *inTree = (TTree *) input->Get("tree");
    TH1F  *hcount = (TH1F *) input->Get("Count");
    TFile *output(0);
    if (beginEntry == 0 && endEntry == -1)
        output = TFile::Open(outdir + "Step3_" + process + suffix, "RECREATE");
    else
        output = TFile::Open(outdir + "Step3_" + process + TString::Format("_%Li_%Li", beginEntry, endEntry) + suffix, "RECREATE");
    TTree *outTree = inTree->CloneTree(0); // Do no copy the data yet
    /// The clone should not delete any shared i/o buffers.
    ResetDeleteBranches(outTree);
    


    ///-- Set branch addresses -------------------------------------------------
    EventInfo EVENT;
    double hJet_pt[MAXJ], hJet_eta[MAXJ], hJet_phi[MAXJ], hJet_m[MAXJ], hJet_ptRaw[MAXJ], hJet_genPt[MAXJ];
    int hJCidx[2];

    inTree->SetBranchStatus("*", 1);

    inTree->SetBranchStatus("hJCidx",1);
    inTree->SetBranchStatus("Jet_*",1);
    inTree->SetBranchAddress("hJCidx", &hJCidx);
    inTree->SetBranchAddress("Jet_pt", &hJet_pt);

    inTree->SetBranchAddress("Jet_eta", &hJet_eta);
    inTree->SetBranchAddress("Jet_phi", &hJet_phi);
    inTree->SetBranchAddress("Jet_mass", &hJet_m);
    inTree->SetBranchAddress("Jet_rawPt", &hJet_ptRaw);

    inTree->SetBranchAddress("Jet_mcPt", &hJet_genPt);


    ///-- Make new branches ----------------------------------------------------
    int EVENT_run, EVENT_event;  // set these as TTree index?
    float lumi_ = lumi, efflumi, efflumi_old, 
        efflumi_UEPS_up, efflumi_UEPS_down;
    float hJet_ptReg[2];
    float HptNorm, HptGen, HptReg;
    float HmassNorm, HmassGen, HmassReg;

    outTree->Branch("EVENT_run", &EVENT_run, "EVENT_run/I");
    outTree->Branch("EVENT_event", &EVENT_event, "EVENT_event/I");
    outTree->Branch("lumi", &lumi_, "lumi/F");
    outTree->Branch("efflumi", &efflumi, "efflumi/F");
    outTree->Branch("efflumi_old", &efflumi_old, "efflumi_old/F");
    outTree->Branch("efflumi_UEPS_up", &efflumi_UEPS_up, "efflumi_UEPS_up/F");
    outTree->Branch("efflumi_UEPS_down", &efflumi_UEPS_down, "efflumi_UEPS_down/F");
    outTree->Branch("hJet_ptReg", &hJet_ptReg, "hJet_ptReg[2]/F");
    
    outTree->Branch("HptNorm", &HptNorm, "HptNorm/F");
    outTree->Branch("HptGen", &HptGen, "HptGen/F");
    outTree->Branch("HptReg", &HptReg, "HptReg/F");
    outTree->Branch("HmassNorm", &HmassNorm, "HmassNorm/F");
    outTree->Branch("HmassGen", &HmassGen, "HmassGen/F");
    outTree->Branch("HmassReg", &HmassReg, "HmassReg/F");

    /// Get effective lumis
    std::map < std::string, float > efflumis = GetLumis();
    efflumi = efflumis[process.Data()];
    assert(efflumi > 0);
    efflumi_old       = efflumi;
    efflumi_UEPS_up   = efflumi * hcount->GetBinContent(2) / hcount->GetBinContent(3);
    efflumi_UEPS_down = efflumi * hcount->GetBinContent(2) / hcount->GetBinContent(4);



    TTreeFormula* ttf_lheweight = new TTreeFormula("ttf_lheweight", Form("%f", efflumi), inTree);
#ifdef STITCH
    std::map < std::string, std::string > lheweights = GetLHEWeights();
    TString process_lhe = process;
    if (process_lhe.BeginsWith("WJets") && process_lhe != "WJetsHW")
        process_lhe = "WJets";
    else if (process_lhe.BeginsWith("ZJets") && process_lhe != "ZJetsHW")
        process_lhe = "ZJets";
    else 
        process_lhe = "";
    TString lheweight = lheweights[process_lhe.Data()];
    if (lheweight != "") {
        delete ttf_lheweight;
        
        // Bug fix for ZJetsPtZ100
        if (process == "ZJetsPtZ100")
            lheweight.ReplaceAll("lheV_pt", "999");
        std::cout << "BUGFIX: " << lheweight << std::endl;
        ttf_lheweight = new TTreeFormula("ttf_lheweight", lheweight, inTree);
    }
#endif
    ttf_lheweight->SetQuickLoad(1);

    // regression stuff here

    
    ///-- Setup TMVA Reader ----------------------------------------------------
    TMVA::Tools::Instance();  //< This loads the library
    TMVA::Reader * reader = new TMVA::Reader("!Color:!Silent");

    /// Get the variables
    const std::vector < std::string > & inputExpressionsReg = GetInputExpressionsReg();
    
   const UInt_t nvars = inputExpressionsReg.size();
   
    Float_t readerVars[nvars];
    int idx_rawpt = -1, idx_pt = -1, idx_et = -1, idx_mt = -1;
   
    for (UInt_t iexpr = 0; iexpr < nvars; iexpr++) {
        const TString& expr = inputExpressionsReg.at(iexpr);
        reader->AddVariable(expr, &readerVars[iexpr]);
        if      (expr.BeginsWith("breg_rawptJER := "))  idx_rawpt = iexpr;
        else if (expr.BeginsWith("breg_pt := "))        idx_pt = iexpr;
        else if (expr.BeginsWith("breg_et := "))        idx_et = iexpr;
        else if (expr.BeginsWith("breg_mt := "))        idx_mt = iexpr;
    }
    //    assert(idx_rawpt!=-1 && idx_pt!=-1 && idx_et!=-1 && idx_mt!=-1);
    assert(idx_rawpt!=-1 && idx_pt!=-1 );

    /// Setup TMVA regression inputs
    const std::vector < std::string > & inputExpressionsReg0 = GetInputExpressionsReg0();
    const std::vector < std::string > & inputExpressionsReg1 = GetInputExpressionsReg1();
    assert(inputExpressionsReg0.size() == nvars);
    assert(inputExpressionsReg1.size() == nvars);

    /// Load TMVA weights
    TString weightdir  = "weights/";
    TString weightfile = weightdir + "TMVARegression_" + regMethod + ".testweights.xml";
    reader->BookMVA(regMethod + " method", weightfile);
    
    TStopwatch sw;
    sw.Start();


    /// Create TTreeFormulas
    TTreeFormula *ttf = 0;
    std::vector < TTreeFormula * >::const_iterator formIt, formItEnd;
    std::vector < TTreeFormula * > inputFormulasReg0;
    std::vector < TTreeFormula * > inputFormulasReg1;
    std::vector < TTreeFormula * > inputFormulasFJReg0;
    std::vector < TTreeFormula * > inputFormulasFJReg1;
    std::vector < TTreeFormula * > inputFormulasFJReg2;

    
    for (UInt_t iexpr = 0; iexpr < nvars; iexpr++) {
        ttf = new TTreeFormula(Form("ttfreg%i_0", iexpr), inputExpressionsReg0.at(iexpr).c_str(), inTree);
        ttf->SetQuickLoad(1);
        inputFormulasReg0.push_back(ttf);
        ttf = new TTreeFormula(Form("ttfreg%i_1", iexpr), inputExpressionsReg1.at(iexpr).c_str(), inTree);
        ttf->SetQuickLoad(1);
        inputFormulasReg1.push_back(ttf);
    }
 


    ///-- Loop over events -----------------------------------------------------
    Int_t curTree = inTree->GetTreeNumber();
    const Long64_t nentries = inTree->GetEntries();
    if (endEntry < 0)  endEntry = nentries;

    Long64_t ievt = 0;
    for (ievt=TMath::Max(ievt, beginEntry); ievt<TMath::Min(nentries, endEntry); ievt++) {
        if (ievt % 2000 == 0)
            std::cout << "--- ... Processing event: " << ievt << std::endl;
    
        const Long64_t local_entry = inTree->LoadTree(ievt);  // faster, but only for TTreeFormula
        if (local_entry < 0)  break;
        inTree->GetEntry(ievt);  // same event as received by LoadTree()

        if (inTree->GetTreeNumber() != curTree) {
            curTree = inTree->GetTreeNumber();

            for (formIt=inputFormulasReg0.begin(), formItEnd=inputFormulasReg0.end(); formIt!=formItEnd; formIt++)
                (*formIt)->UpdateFormulaLeaves();  // if using TChain
            for (formIt=inputFormulasReg1.begin(), formItEnd=inputFormulasReg1.end(); formIt!=formItEnd; formIt++)
                (*formIt)->UpdateFormulaLeaves();  // if using TChain
            for (formIt=inputFormulasFJReg0.begin(), formItEnd=inputFormulasFJReg0.end(); formIt!=formItEnd; formIt++)
                (*formIt)->UpdateFormulaLeaves();  // if using TChain
            for (formIt=inputFormulasFJReg1.begin(), formItEnd=inputFormulasFJReg1.end(); formIt!=formItEnd; formIt++)
                (*formIt)->UpdateFormulaLeaves();  // if using TChain
            for (formIt=inputFormulasFJReg2.begin(), formItEnd=inputFormulasFJReg2.end(); formIt!=formItEnd; formIt++)
                (*formIt)->UpdateFormulaLeaves();  // if using TChain

            ttf_lheweight->UpdateFormulaLeaves();
        }


        /// These need to be called when arrays of variable size are used in TTree.
        for (formIt=inputFormulasReg0.begin(), formItEnd=inputFormulasReg0.end(); formIt!=formItEnd; formIt++)
            (*formIt)->GetNdata();
        for (formIt=inputFormulasReg1.begin(), formItEnd=inputFormulasReg1.end(); formIt!=formItEnd; formIt++)
            (*formIt)->GetNdata();
        for (formIt=inputFormulasFJReg0.begin(), formItEnd=inputFormulasFJReg0.end(); formIt!=formItEnd; formIt++)
            (*formIt)->GetNdata();
        for (formIt=inputFormulasFJReg1.begin(), formItEnd=inputFormulasFJReg1.end(); formIt!=formItEnd; formIt++)
            (*formIt)->GetNdata();
        for (formIt=inputFormulasFJReg2.begin(), formItEnd=inputFormulasFJReg2.end(); formIt!=formItEnd; formIt++)
            (*formIt)->GetNdata();

        ttf_lheweight->GetNdata();
        /// Fill branches
        EVENT_run = EVENT.run;
        EVENT_event = EVENT.event;



#ifdef STITCH        
        efflumi           = ttf_lheweight->EvalInstance();
	//        efflumi_UEPS_up   = efflumi * hcount->GetBinContent(2) / hcount->GetBinContent(3);
        //efflumi_UEPS_down = efflumi * hcount->GetBinContent(2) / hcount->GetBinContent(4);
#endif
    
        bool verbose = false;
 
	for (Int_t ihj = 0; ihj < 2; ihj++) {

   
            /// Evaluate TMVA regression output
            for (UInt_t iexpr = 0; iexpr < nvars; iexpr++) {
                if (ihj==0) {
                    readerVars[iexpr] = inputFormulasReg0.at(iexpr)->EvalInstance();

                } else if (ihj==1) {
                    readerVars[iexpr] = inputFormulasReg1.at(iexpr)->EvalInstance();
                }
            }

	    hJet_ptReg[ihj]               = (reader->EvaluateRegression(regMethod + " method"))[0];
            if (verbose)  std::cout << readerVars[idx_pt] << " " << readerVars[idx_rawpt] <<  " " << hJet_pt[ihj] << " " << hJet_ptReg[ihj] << " " << hJet_genPt[ihj] << std::endl;
        const TLorentzVector p4Zero                     = TLorentzVector(0., 0., 0., 0.);
	//	int idx =  hJCidx[0] ;
	//	std::cout << "the regressed pt for jet 0 is " << hJet_ptReg[0] << "; the hJCidx is " << hJCidx[0] << ", hence the origianl pt is " <<  hJet_pt[idx] << std::endl;

	
       
        const TLorentzVector& hJet_p4Norm_0             = makePtEtaPhiM(hJet_pt[hJCidx[0]]                , hJet_pt[hJCidx[0]], hJet_eta[hJCidx[0]], hJet_phi[hJCidx[0]], hJet_m[hJCidx[0]]);
        const TLorentzVector& hJet_p4Norm_1             = makePtEtaPhiM(hJet_pt[hJCidx[1]]                , hJet_pt[hJCidx[1]], hJet_eta[hJCidx[1]], hJet_phi[hJCidx[1]], hJet_m[hJCidx[1]]);
        const TLorentzVector& hJet_p4Gen_0              = hJet_genPt[hJCidx[0]] > 0 ? 
                                                          makePtEtaPhiM(hJet_genPt[hJCidx[0]]             , hJet_pt[hJCidx[0]], hJet_eta[hJCidx[0]], hJet_phi[hJCidx[0]], hJet_m[hJCidx[0]]) : p4Zero;
        const TLorentzVector& hJet_p4Gen_1              = hJet_genPt[hJCidx[1]] > 0 ? 
                                                          makePtEtaPhiM(hJet_genPt[hJCidx[1]]             , hJet_pt[hJCidx[1]], hJet_eta[hJCidx[1]], hJet_phi[hJCidx[1]], hJet_m[hJCidx[1]]) : p4Zero;
        const TLorentzVector& hJet_p4Reg_0              = makePtEtaPhiM(hJet_ptReg[0]             , hJet_pt[hJCidx[0]], hJet_eta[hJCidx[0]], hJet_phi[hJCidx[0]], hJet_m[hJCidx[0]]);
        const TLorentzVector& hJet_p4Reg_1              = makePtEtaPhiM(hJet_ptReg[1]             , hJet_pt[hJCidx[1]], hJet_eta[hJCidx[1]], hJet_phi[hJCidx[1]], hJet_m[hJCidx[1]]);
        HptNorm             = (hJet_p4Norm_0             + hJet_p4Norm_1            ).Pt();
        HptGen              = (hJet_p4Gen_0              + hJet_p4Gen_1             ).Pt();
        HptReg              = (hJet_p4Reg_0              + hJet_p4Reg_1             ).Pt();
        HmassNorm             = (hJet_p4Norm_0             + hJet_p4Norm_1            ).M();
        HmassGen              = (hJet_p4Gen_0              + hJet_p4Gen_1             ).M();
        HmassReg              = (hJet_p4Reg_0              + hJet_p4Reg_1             ).M();
	//        std::cout << "HmassReg is " << HmassReg << std::endl; 
	
	}
        outTree->Fill();  // fill it!
    }  // end loop over TTree entries

    /// Get elapsed time
    sw.Stop();
    std::cout << "--- End of event loop: ";
    sw.Print();

    output->cd();
    outTree->Write();
    output->Close();
    input->Close();

    delete input;
    delete output;
    for (formIt=inputFormulasReg0.begin(), formItEnd=inputFormulasReg0.end(); formIt!=formItEnd; formIt++)
        delete *formIt;
    for (formIt=inputFormulasReg1.begin(), formItEnd=inputFormulasReg1.end(); formIt!=formItEnd; formIt++)
        delete *formIt;
    for (formIt=inputFormulasFJReg0.begin(), formItEnd=inputFormulasFJReg0.end(); formIt!=formItEnd; formIt++)
        delete *formIt;
    for (formIt=inputFormulasFJReg1.begin(), formItEnd=inputFormulasFJReg1.end(); formIt!=formItEnd; formIt++)
        delete *formIt;
    for (formIt=inputFormulasFJReg2.begin(), formItEnd=inputFormulasFJReg2.end(); formIt!=formItEnd; formIt++)
        delete *formIt;

    delete ttf_lheweight;

    std::cout << "==> GrowTree is done!" << std::endl << std::endl;
    return;
}
コード例 #19
0
void TMVARegressionApplication( TString myMethodList = "" ) 
{
   //---------------------------------------------------------------
   // This loads the library
   TMVA::Tools::Instance();

   // Default MVA methods to be trained + tested
   std::map<std::string,int> Use;

   // --- Mutidimensional likelihood and Nearest-Neighbour methods
   Use["PDERS"]           = 0;
   Use["PDEFoam"]         = 1; 
   Use["KNN"]             = 1;
   // 
   // --- Linear Discriminant Analysis
   Use["LD"]		        = 1;
   // 
   // --- Function Discriminant analysis
   Use["FDA_GA"]          = 1;
   Use["FDA_MC"]          = 0;
   Use["FDA_MT"]          = 0;
   Use["FDA_GAMT"]        = 0;
   // 
   // --- Neural Network
   Use["MLP"] = 1;
   Use["DNN_CPU"] = 0;
   // 
   // --- Support Vector Machine 
   Use["SVM"]             = 0;
   // 
   // --- Boosted Decision Trees
   Use["BDT"]             = 0;
   Use["BDTG"]            = 1;
   // ---------------------------------------------------------------

   std::cout << std::endl;
   std::cout << "==> Start TMVARegressionApplication" << std::endl;

   // Select methods (don't look at this code - not of interest)
   if (myMethodList != "") {
      for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) it->second = 0;

      std::vector<TString> mlist = gTools().SplitString( myMethodList, ',' );
      for (UInt_t i=0; i<mlist.size(); i++) {
         std::string regMethod(mlist[i]);

         if (Use.find(regMethod) == Use.end()) {
            std::cout << "Method \"" << regMethod << "\" not known in TMVA under this name. Choose among the following:" << std::endl;
            for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) std::cout << it->first << " ";
            std::cout << std::endl;
            return;
         }
         Use[regMethod] = 1;
      }
   }

   // --------------------------------------------------------------------------------------------------

   // --- Create the Reader object

   TMVA::Reader *reader = new TMVA::Reader( "!Color:!Silent" );    

   // Create a set of variables and declare them to the reader
   // - the variable names MUST corresponds in name and type to those given in the weight file(s) used
   Float_t var1, var2;
   reader->AddVariable( "var1", &var1 );
   reader->AddVariable( "var2", &var2 );

   // Spectator variables declared in the training have to be added to the reader, too
   Float_t spec1,spec2;
   reader->AddSpectator( "spec1:=var1*2",  &spec1 );
   reader->AddSpectator( "spec2:=var1*3",  &spec2 );

   // --- Book the MVA methods

   TString dir    = "dataset/weights/";
   TString prefix = "TMVARegression";

   // Book method(s)
   for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) {
      if (it->second) {
         TString methodName = it->first + " method";
         TString weightfile = dir + prefix + "_" + TString(it->first) + ".weights.xml";
         reader->BookMVA( methodName, weightfile ); 
      }
   }
   
   // Book output histograms
   TH1* hists[100];
   Int_t nhists = -1;
   for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) {
      TH1* h = new TH1F( it->first.c_str(), TString(it->first) + " method", 100, -100, 600 );
      if (it->second) hists[++nhists] = h;
   }
   nhists++;
   
   // Prepare input tree (this must be replaced by your data source)
   // in this example, there is a toy tree with signal and one with background events
   // we'll later on use only the "signal" events for the test in this example.
   //
   TFile *input(0);
   TString fname = "./tmva_reg_example.root";
   if (!gSystem->AccessPathName( fname )) {
      input = TFile::Open( fname ); // check if file in local directory exists
   }
   else {
      TFile::SetCacheFileDir(".");
      input = TFile::Open("http://root.cern.ch/files/tmva_reg_example.root", "CACHEREAD"); // if not: download from ROOT server
   }
   if (!input) {
      std::cout << "ERROR: could not open data file" << std::endl;
      exit(1);
   }
   std::cout << "--- TMVARegressionApp        : Using input file: " << input->GetName() << std::endl;

   // --- Event loop

   // Prepare the tree
   // - here the variable names have to corresponds to your tree
   // - you can use the same variables as above which is slightly faster,
   //   but of course you can use different ones and copy the values inside the event loop
   //
   TTree* theTree = (TTree*)input->Get("TreeR");
   std::cout << "--- Select signal sample" << std::endl;
   theTree->SetBranchAddress( "var1", &var1 );
   theTree->SetBranchAddress( "var2", &var2 );

   std::cout << "--- Processing: " << theTree->GetEntries() << " events" << std::endl;
   TStopwatch sw;
   sw.Start();
   for (Long64_t ievt=0; ievt<theTree->GetEntries();ievt++) {

      if (ievt%1000 == 0) {
         std::cout << "--- ... Processing event: " << ievt << std::endl;
      }

      theTree->GetEntry(ievt);

      // Retrieve the MVA target values (regression outputs) and fill into histograms
      // NOTE: EvaluateRegression(..) returns a vector for multi-target regression

      for (Int_t ih=0; ih<nhists; ih++) {
         TString title = hists[ih]->GetTitle();
         Float_t val = (reader->EvaluateRegression( title ))[0];
         hists[ih]->Fill( val );    
      }
   }
   sw.Stop();
   std::cout << "--- End of event loop: "; sw.Print();

   // --- Write histograms

   TFile *target  = new TFile( "TMVARegApp.root","RECREATE" );
   for (Int_t ih=0; ih<nhists; ih++) hists[ih]->Write();
   target->Close();

   std::cout << "--- Created root file: \"" << target->GetName() 
             << "\" containing the MVA output histograms" << std::endl;
  
   delete reader;
    
   std::cout << "==> TMVARegressionApplication is done!" << std::endl << std::endl;
}
コード例 #20
0
ファイル: MVA.C プロジェクト: cedricpri/AnalysisCMS
//------------------------------------------------------------------------------
// MVARead
//------------------------------------------------------------------------------
void MVARead(TString MVA_id, TString signal, TString filename)
{

  cout << "\n\n\n" << filename << "\n\n\n" << endl; 

  
  //----- TMVA::Reader   

  TMVA::Reader* reader   = new TMVA::Reader("!Color:!Silent");   

	float newdarkpt       ;
	//float topRecoW     ;
	//float lep1eta      ; 
	//float lep1phi      ;  
	//float lep1mass     ;
	//float lep2pt       ; 
	//float lep2eta      ;
	//float lep2phi      ; 
	//float lep2mass     ;
	//float jet1pt       ;
	//float jet1eta      ;
	//float jet1phi      ;
	//float jet1mass     ;
	//float jet2pt       ;
	//float jet2eta      ;
	//float jet2phi      ;
	//float jet2mass     ;
	float metPfType1   ;
	//float metPfType1Phi;
	//float m2l          ;
	float mt2ll        ;
	//float mt2lblb      ;
	//float mtw1         ;
	//float mtw2         ;
	//float ht           ;
	//float htjets       ;
	//float htnojets     ;
	//float njet         ;
	//float nbjet30csvv2l;
	//float nbjet30csvv2m;
	//float nbjet30csvv2t;  
	//float dphijet1met  ;
	//float dphijet2met  ;
	//float dphijj       ;
	//float dphijjmet    ;
	//float dphill       ;
	//float dphilep1jet1 ;
	//float dphilep1jet2 ;
	//float dphilep2jet1 ;
	//float dphilep2jet2 ;
	//float dphilmet1    ;
	//float dphilmet2    ;
	float dphillmet    ;
	//float sphericity   ;
	//float alignment    ;
	//float planarity    ;

	reader->AddVariable( "newdarkpt"       , &newdarkpt        );
	//reader->AddVariable( "topRecoW"     , &topRecoW      );
	//reader->AddVariable( "lep1eta"      , &lep1eta       );
	//reader->AddVariable( "lep1phi "     , &lep1phi       );
	//reader->AddVariable( "lep1mass"     , &lep1mass      );
	//reader->AddVariable( "lep2pt "      , &lep2pt        );
	//reader->AddVariable( "lep2eta"      , &lep2eta       );
	//reader->AddVariable( "lep2phi "     , &lep2phi       );
	//reader->AddVariable( "lep2mass"     , &lep2mass      );
	//reader->AddVariable( "jet1pt "      , &jet1pt        );
	//reader->AddVariable( "jet1eta"      , &jet1eta       );
	//reader->AddVariable( "jet1phi"      , &jet1phi       );
	//reader->AddVariable( "jet1mass"     , &jet1mass      );
	//reader->AddVariable( "jet2pt"       , &jet2pt        );
	//reader->AddVariable( "jet2eta"      , &jet2eta       );
	//reader->AddVariable( "jet2phi"      , &jet2phi       );
	//reader->AddVariable( "jet2mass"     , &jet2mass      );
	reader->AddVariable( "metPfType1"   , &metPfType1    );
	//reader->AddVariable( "metPfType1Phi", &metPfType1Phi );
	//reader->AddVariable( "m2l"          , &m2l           );
	reader->AddVariable( "mt2ll"        , &mt2ll         );
	//reader->AddVariable( "mt2lblb"      , &mt2lblb       );
	//reader->AddVariable( "mtw1"         , &mtw1          );
	//reader->AddVariable( "mtw2"         , &mtw2          );
	//reader->AddVariable( "ht"           , &ht            );
	//reader->AddVariable( "htjets"       , &htjets        );
	//reader->AddVariable( "htnojets"     , &htnojets      );
	//reader->AddVariable( "njet"         , &njet          );
	//reader->AddVariable( "nbjet30csvv2l", &nbjet30csvv2l );
	//reader->AddVariable( "nbjet30csvv2m", &nbjet30csvv2m );
	//reader->AddVariable( "nbjet30csvv2t", &nbjet30csvv2t );
	//reader->AddVariable( "dphijet1met"  , &dphijet1met   );
	//reader->AddVariable( "dphijet2met"  , &dphijet2met   );
	//reader->AddVariable( "dphijj"       , &dphijj        );
	//reader->AddVariable( "dphijjmet"    , &dphijjmet     );
	//reader->AddVariable( "dphill"       , &dphill        );
	//reader->AddVariable( "dphilep1jet1" , &dphilep1jet1  );
	//reader->AddVariable( "dphilep1jet2" , &dphilep1jet2  );
	//reader->AddVariable( "dphilep2jet1" , &dphilep2jet1  );
	//reader->AddVariable( "dphilep2jet2" , &dphilep2jet2  );
	//reader->AddVariable( "dphilmet1"    , &dphilmet1     );
	//reader->AddVariable( "dphilmet2"    , &dphilmet2     );
	reader->AddVariable( "dphillmet"    , &dphillmet     );
	//reader->AddVariable( "sphericity"   , &sphericity    );
	//reader->AddVariable( "alignment"    , &alignment     );
	//reader->AddVariable( "planarity"    , &planarity     );



  // Get MVA response
  //----------------------------------------------------------------------------

  TFile* input = TFile::Open(inputdir + filename + ".root", "update");

  TTree* theTree = (TTree*)input->Get("latino");

	
  //----- read 

	theTree->SetBranchAddress( "newdarkpt"       , &newdarkpt        );
	//theTree->SetBranchAddress( "topRecoW"     , &topRecoW      );
	//theTree->SetBranchAddress( "lep1eta"      , &lep1eta       );
	//theTree->SetBranchAddress( "lep1phi "     , &lep1phi       );
	//theTree->SetBranchAddress( "lep1mass"     , &lep1mass      );
	//theTree->SetBranchAddress( "lep2pt "      , &lep2pt        );
	//theTree->SetBranchAddress( "lep2eta"      , &lep2eta       );
	//theTree->SetBranchAddress( "lep2phi "     , &lep2phi       );
	//theTree->SetBranchAddress( "lep2mass"     , &lep2mass      );
	//theTree->SetBranchAddress( "jet1pt "      , &jet1pt        );
	//theTree->SetBranchAddress( "jet1eta"      , &jet1eta       );
	//theTree->SetBranchAddress( "jet1phi"      , &jet1phi       );
	//theTree->SetBranchAddress( "jet1mass"     , &jet1mass      );
	//theTree->SetBranchAddress( "jet2pt"       , &jet2pt        );
	//theTree->SetBranchAddress( "jet2eta"      , &jet2eta       );
	//theTree->SetBranchAddress( "jet2phi"      , &jet2phi       );
	//theTree->SetBranchAddress( "jet2mass"     , &jet2mass      );
	theTree->SetBranchAddress( "metPfType1"   , &metPfType1    );
	//theTree->SetBranchAddress( "metPfType1Phi", &metPfType1Phi );
	//theTree->SetBranchAddress( "m2l"          , &m2l           );
	theTree->SetBranchAddress( "mt2ll"        , &mt2ll         );
	//theTree->SetBranchAddress( "mt2lblb"      , &mt2lblb       );
	//theTree->SetBranchAddress( "mtw1"         , &mtw1          );
	//theTree->SetBranchAddress( "mtw2"         , &mtw2          );
	//theTree->SetBranchAddress( "ht"           , &ht            );
	//theTree->SetBranchAddress( "htjets"       , &htjets        );
	//theTree->SetBranchAddress( "htnojets"     , &htnojets      );
	//theTree->SetBranchAddress( "njet"         , &njet          );
	//theTree->SetBranchAddress( "nbjet30csvv2l", &nbjet30csvv2l );
	//theTree->SetBranchAddress( "nbjet30csvv2m", &nbjet30csvv2m );
	//theTree->SetBranchAddress( "nbjet30csvv2t", &nbjet30csvv2t );
	//theTree->SetBranchAddress( "dphijet1met"  , &dphijet1met   );
	//theTree->SetBranchAddress( "dphijet2met"  , &dphijet2met   );
	//theTree->SetBranchAddress( "dphijj"       , &dphijj        );
	//theTree->SetBranchAddress( "dphijjmet"    , &dphijjmet     );
	//theTree->SetBranchAddress( "dphill"       , &dphill        );
	//theTree->SetBranchAddress( "dphilep1jet1" , &dphilep1jet1  );
	//theTree->SetBranchAddress( "dphilep1jet2" , &dphilep1jet2  );
	//theTree->SetBranchAddress( "dphilep2jet1" , &dphilep2jet1  );
	//theTree->SetBranchAddress( "dphilep2jet2" , &dphilep2jet2  );
	//theTree->SetBranchAddress( "dphilmet1"    , &dphilmet1     );
	//theTree->SetBranchAddress( "dphilmet2"    , &dphilmet2     );
	theTree->SetBranchAddress( "dphillmet"    , &dphillmet     );
	//theTree->SetBranchAddress( "sphericity"   , &sphericity    );
	//theTree->SetBranchAddress( "alignment"    , &alignment     );
	//theTree->SetBranchAddress( "planarity"    , &planarity     );

  //----- write 

  	float mva01; 
  	//float mva02; 
  	//float mva03;
  	//float mva04;
  	//float mva05;

	//TBranch* b_delete = theTree->GetBranch( "xxx" );
  	//theTree -> GetListOfBranches() -> Remove( b_delete );
   	//theTree -> Write();
 
 	TBranch* b_mva01 = theTree->Branch("ANN_" + MVA_id + "_" + signal, &mva01, "mva/F" );
  	//TBranch* b_mva02 = theTree->Branch("mva02_" + signal, &mva02, "mva/F" );
  	//TBranch* b_mva03 = theTree->Branch("mva03_" + signal, &mva03, "mva/F" );
  	//TBranch* b_mva04 = theTree->Branch("mva04_" + signal, &mva04, "mva/F" );
  	//TBranch* b_mva05 = theTree->Branch("mva05_" + signal, &mva05, "mva/F" );



  // Book MVA methods
  //----------------------------------------------------------------------------
  reader->BookMVA("01", weightsdir + signal + "_MLP01.weights.xml");
  //reader->BookMVA("02", weightsdir + signal + "_MLP02.weights.xml");
  //reader->BookMVA("03", weightsdir + signal + "_MLP03.weights.xml");
  //reader->BookMVA("04", weightsdir + signal + "_BDT04.weights.xml");
  //reader->BookMVA("05", weightsdir + signal + "_BDT05.weights.xml");


  Long64_t nentries = theTree->GetEntries();



  for (Long64_t ievt=0; ievt<nentries; ievt++){

    theTree->GetEntry(ievt);

    mva01 = reader->EvaluateMVA("01");
   //mva02 = reader->EvaluateMVA("02");  
    //mva03 = reader->EvaluateMVA("03");
    //mva04 = reader->EvaluateMVA("04");
    //mva05 = reader->EvaluateMVA("05");

    b_mva01->Fill();	
    //b_mva02->Fill();
    //b_mva03->Fill();
    //b_mva04->Fill();
    //b_mva05->Fill();

  } 

  // Save
  //----------------------------------------------------------------------------
  theTree->Write("", TObject::kOverwrite);

  input->Close();

  delete reader;

}
コード例 #21
0
ファイル: TMVA_apply.C プロジェクト: stopsnt/SingleLepton2012
void TMVAClassificationApplication( TString myMethodList = "" ) 
{   
#ifdef __CINT__
   gROOT->ProcessLine( ".O0" ); // turn off optimization in CINT
#endif

   //---------------------------------------------------------------

   // This loads the library
   TMVA::Tools::Instance();

   // Default MVA methods to be trained + tested
   std::map<std::string,int> Use;

   Use["MLP"]             = 0; // Recommended ANN
   Use["BDT"]             = 1; // uses Adaptive Boost

   std::cout << std::endl;
   std::cout << "==> Start TMVAClassificationApplication" << std::endl;

   // --- Create the Reader object

   TMVA::Reader *reader = new TMVA::Reader( "!Color:!Silent" );    

   // Create a set of variables and declare them to the reader
   // - the variable names MUST corresponds in name and type to those given in the weight file(s) used
   Float_t var1, var2;
   Float_t var3, var4;
   reader->AddVariable( "myvar1 := var1+var2", &var1 );
   reader->AddVariable( "myvar2 := var1-var2", &var2 );
   reader->AddVariable( "var3",                &var3 );
   reader->AddVariable( "var4",                &var4 );

   // Spectator variables declared in the training have to be added to the reader, too
   Float_t spec1,spec2;
   reader->AddSpectator( "spec1 := var1*2",   &spec1 );
   reader->AddSpectator( "spec2 := var1*3",   &spec2 );

   Float_t Category_cat1, Category_cat2, Category_cat3;
   if (Use["Category"]){
      // Add artificial spectators for distinguishing categories
      reader->AddSpectator( "Category_cat1 := var3<=0",             &Category_cat1 );
      reader->AddSpectator( "Category_cat2 := (var3>0)&&(var4<0)",  &Category_cat2 );
      reader->AddSpectator( "Category_cat3 := (var3>0)&&(var4>=0)", &Category_cat3 );
   }

   // --- Book the MVA methods

   TString dir    = "weights/";
   TString prefix = "TMVAClassification";

   // Book method(s)
   for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) {
      if (it->second) {
         TString methodName = TString(it->first) + TString(" method");
         TString weightfile = dir + prefix + TString("_") + TString(it->first) + TString(".weights.xml");
         reader->BookMVA( methodName, weightfile ); 
      }
   }
   
   // Book output histograms
   UInt_t nbin = 100;
   TH1F   *histLk(0), *histLkD(0), *histLkPCA(0), *histLkKDE(0), *histLkMIX(0), *histPD(0), *histPDD(0);
   TH1F   *histPDPCA(0), *histPDEFoam(0), *histPDEFoamErr(0), *histPDEFoamSig(0), *histKNN(0), *histHm(0);
   TH1F   *histFi(0), *histFiG(0), *histFiB(0), *histLD(0), *histNn(0),*histNnbfgs(0),*histNnbnn(0);
   TH1F   *histNnC(0), *histNnT(0), *histBdt(0), *histBdtG(0), *histBdtD(0), *histRf(0), *histSVMG(0);
   TH1F   *histSVMP(0), *histSVML(0), *histFDAMT(0), *histFDAGA(0), *histCat(0), *histPBdt(0);

   if (Use["Likelihood"])    histLk      = new TH1F( "MVA_Likelihood",    "MVA_Likelihood",    nbin, -1, 1 );
   if (Use["LikelihoodD"])   histLkD     = new TH1F( "MVA_LikelihoodD",   "MVA_LikelihoodD",   nbin, -1, 0.9999 );
   if (Use["LikelihoodPCA"]) histLkPCA   = new TH1F( "MVA_LikelihoodPCA", "MVA_LikelihoodPCA", nbin, -1, 1 );
   if (Use["LikelihoodKDE"]) histLkKDE   = new TH1F( "MVA_LikelihoodKDE", "MVA_LikelihoodKDE", nbin,  -0.00001, 0.99999 );
   if (Use["LikelihoodMIX"]) histLkMIX   = new TH1F( "MVA_LikelihoodMIX", "MVA_LikelihoodMIX", nbin,  0, 1 );
   if (Use["PDERS"])         histPD      = new TH1F( "MVA_PDERS",         "MVA_PDERS",         nbin,  0, 1 );
   if (Use["PDERSD"])        histPDD     = new TH1F( "MVA_PDERSD",        "MVA_PDERSD",        nbin,  0, 1 );
   if (Use["PDERSPCA"])      histPDPCA   = new TH1F( "MVA_PDERSPCA",      "MVA_PDERSPCA",      nbin,  0, 1 );
   if (Use["KNN"])           histKNN     = new TH1F( "MVA_KNN",           "MVA_KNN",           nbin,  0, 1 );
   if (Use["HMatrix"])       histHm      = new TH1F( "MVA_HMatrix",       "MVA_HMatrix",       nbin, -0.95, 1.55 );
   if (Use["Fisher"])        histFi      = new TH1F( "MVA_Fisher",        "MVA_Fisher",        nbin, -4, 4 );
   if (Use["FisherG"])       histFiG     = new TH1F( "MVA_FisherG",       "MVA_FisherG",       nbin, -1, 1 );
   if (Use["BoostedFisher"]) histFiB     = new TH1F( "MVA_BoostedFisher", "MVA_BoostedFisher", nbin, -2, 2 );
   if (Use["LD"])            histLD      = new TH1F( "MVA_LD",            "MVA_LD",            nbin, -2, 2 );
   if (Use["MLP"])           histNn      = new TH1F( "MVA_MLP",           "MVA_MLP",           nbin, -1.25, 1.5 );
   if (Use["MLPBFGS"])       histNnbfgs  = new TH1F( "MVA_MLPBFGS",       "MVA_MLPBFGS",       nbin, -1.25, 1.5 );
   if (Use["MLPBNN"])        histNnbnn   = new TH1F( "MVA_MLPBNN",        "MVA_MLPBNN",        nbin, -1.25, 1.5 );
   if (Use["CFMlpANN"])      histNnC     = new TH1F( "MVA_CFMlpANN",      "MVA_CFMlpANN",      nbin,  0, 1 );
   if (Use["TMlpANN"])       histNnT     = new TH1F( "MVA_TMlpANN",       "MVA_TMlpANN",       nbin, -1.3, 1.3 );
   if (Use["BDT"])           histBdt     = new TH1F( "MVA_BDT",           "MVA_BDT",           nbin, -0.8, 0.8 );
   if (Use["BDTD"])          histBdtD    = new TH1F( "MVA_BDTD",          "MVA_BDTD",          nbin, -0.8, 0.8 );
   if (Use["BDTG"])          histBdtG    = new TH1F( "MVA_BDTG",          "MVA_BDTG",          nbin, -1.0, 1.0 );
   if (Use["RuleFit"])       histRf      = new TH1F( "MVA_RuleFit",       "MVA_RuleFit",       nbin, -2.0, 2.0 );
   if (Use["SVM_Gauss"])     histSVMG    = new TH1F( "MVA_SVM_Gauss",     "MVA_SVM_Gauss",     nbin,  0.0, 1.0 );
   if (Use["SVM_Poly"])      histSVMP    = new TH1F( "MVA_SVM_Poly",      "MVA_SVM_Poly",      nbin,  0.0, 1.0 );
   if (Use["SVM_Lin"])       histSVML    = new TH1F( "MVA_SVM_Lin",       "MVA_SVM_Lin",       nbin,  0.0, 1.0 );
   if (Use["FDA_MT"])        histFDAMT   = new TH1F( "MVA_FDA_MT",        "MVA_FDA_MT",        nbin, -2.0, 3.0 );
   if (Use["FDA_GA"])        histFDAGA   = new TH1F( "MVA_FDA_GA",        "MVA_FDA_GA",        nbin, -2.0, 3.0 );
   if (Use["Category"])      histCat     = new TH1F( "MVA_Category",      "MVA_Category",      nbin, -2., 2. );
   if (Use["Plugin"])        histPBdt    = new TH1F( "MVA_PBDT",          "MVA_BDT",           nbin, -0.8, 0.8 );

   // PDEFoam also returns per-event error, fill in histogram, and also fill significance
   if (Use["PDEFoam"]) {
      histPDEFoam    = new TH1F( "MVA_PDEFoam",       "MVA_PDEFoam",              nbin,  0, 1 );
      histPDEFoamErr = new TH1F( "MVA_PDEFoamErr",    "MVA_PDEFoam error",        nbin,  0, 1 );
      histPDEFoamSig = new TH1F( "MVA_PDEFoamSig",    "MVA_PDEFoam significance", nbin,  0, 10 );
   }

   // Book example histogram for probability (the other methods are done similarly)
   TH1F *probHistFi(0), *rarityHistFi(0);
   if (Use["Fisher"]) {
      probHistFi   = new TH1F( "MVA_Fisher_Proba",  "MVA_Fisher_Proba",  nbin, 0, 1 );
      rarityHistFi = new TH1F( "MVA_Fisher_Rarity", "MVA_Fisher_Rarity", nbin, 0, 1 );
   }

   // Prepare input tree (this must be replaced by your data source)
   // in this example, there is a toy tree with signal and one with background events
   // we'll later on use only the "signal" events for the test in this example.
   //   
   TFile *input(0);
   TString fname = "./tmva_example.root";   
   if (!gSystem->AccessPathName( fname )) 
      input = TFile::Open( fname ); // check if file in local directory exists
   else    
      input = TFile::Open( "http://root.cern.ch/files/tmva_class_example.root" ); // if not: download from ROOT server
   
   if (!input) {
      std::cout << "ERROR: could not open data file" << std::endl;
      exit(1);
   }
   std::cout << "--- TMVAClassificationApp    : Using input file: " << input->GetName() << std::endl;
   
   // --- Event loop

   // Prepare the event tree
   // - here the variable names have to corresponds to your tree
   // - you can use the same variables as above which is slightly faster,
   //   but of course you can use different ones and copy the values inside the event loop
   //
   std::cout << "--- Select signal sample" << std::endl;
   TTree* theTree = (TTree*)input->Get("TreeS");
   Float_t userVar1, userVar2;
   theTree->SetBranchAddress( "var1", &userVar1 );
   theTree->SetBranchAddress( "var2", &userVar2 );
   theTree->SetBranchAddress( "var3", &var3 );
   theTree->SetBranchAddress( "var4", &var4 );

   // Efficiency calculator for cut method
   Int_t    nSelCutsGA = 0;
   Double_t effS       = 0.7;

   std::vector<Float_t> vecVar(4); // vector for EvaluateMVA tests

   std::cout << "--- Processing: " << theTree->GetEntries() << " events" << std::endl;
   TStopwatch sw;
   sw.Start();
   for (Long64_t ievt=0; ievt<theTree->GetEntries();ievt++) {

      if (ievt%1000 == 0) std::cout << "--- ... Processing event: " << ievt << std::endl;

      theTree->GetEntry(ievt);

      var1 = userVar1 + userVar2;
      var2 = userVar1 - userVar2;

      // --- Return the MVA outputs and fill into histograms

      if (Use["CutsGA"]) {
         // Cuts is a special case: give the desired signal efficienciy
         Bool_t passed = reader->EvaluateMVA( "CutsGA method", effS );
         if (passed) nSelCutsGA++;
      }

      if (Use["Likelihood"   ])   histLk     ->Fill( reader->EvaluateMVA( "Likelihood method"    ) );
      if (Use["LikelihoodD"  ])   histLkD    ->Fill( reader->EvaluateMVA( "LikelihoodD method"   ) );
      if (Use["LikelihoodPCA"])   histLkPCA  ->Fill( reader->EvaluateMVA( "LikelihoodPCA method" ) );
      if (Use["LikelihoodKDE"])   histLkKDE  ->Fill( reader->EvaluateMVA( "LikelihoodKDE method" ) );
      if (Use["LikelihoodMIX"])   histLkMIX  ->Fill( reader->EvaluateMVA( "LikelihoodMIX method" ) );
      if (Use["PDERS"        ])   histPD     ->Fill( reader->EvaluateMVA( "PDERS method"         ) );
      if (Use["PDERSD"       ])   histPDD    ->Fill( reader->EvaluateMVA( "PDERSD method"        ) );
      if (Use["PDERSPCA"     ])   histPDPCA  ->Fill( reader->EvaluateMVA( "PDERSPCA method"      ) );
      if (Use["KNN"          ])   histKNN    ->Fill( reader->EvaluateMVA( "KNN method"           ) );
      if (Use["HMatrix"      ])   histHm     ->Fill( reader->EvaluateMVA( "HMatrix method"       ) );
      if (Use["Fisher"       ])   histFi     ->Fill( reader->EvaluateMVA( "Fisher method"        ) );
      if (Use["FisherG"      ])   histFiG    ->Fill( reader->EvaluateMVA( "FisherG method"       ) );
      if (Use["BoostedFisher"])   histFiB    ->Fill( reader->EvaluateMVA( "BoostedFisher method" ) );
      if (Use["LD"           ])   histLD     ->Fill( reader->EvaluateMVA( "LD method"            ) );
      if (Use["MLP"          ])   histNn     ->Fill( reader->EvaluateMVA( "MLP method"           ) );
      if (Use["MLPBFGS"      ])   histNnbfgs ->Fill( reader->EvaluateMVA( "MLPBFGS method"       ) );
      if (Use["MLPBNN"       ])   histNnbnn  ->Fill( reader->EvaluateMVA( "MLPBNN method"        ) );
      if (Use["CFMlpANN"     ])   histNnC    ->Fill( reader->EvaluateMVA( "CFMlpANN method"      ) );
      if (Use["TMlpANN"      ])   histNnT    ->Fill( reader->EvaluateMVA( "TMlpANN method"       ) );
      if (Use["BDT"          ])   histBdt    ->Fill( reader->EvaluateMVA( "BDT method"           ) );
      if (Use["BDTD"         ])   histBdtD   ->Fill( reader->EvaluateMVA( "BDTD method"          ) );
      if (Use["BDTG"         ])   histBdtG   ->Fill( reader->EvaluateMVA( "BDTG method"          ) );
      if (Use["RuleFit"      ])   histRf     ->Fill( reader->EvaluateMVA( "RuleFit method"       ) );
      if (Use["SVM_Gauss"    ])   histSVMG   ->Fill( reader->EvaluateMVA( "SVM_Gauss method"     ) );
      if (Use["SVM_Poly"     ])   histSVMP   ->Fill( reader->EvaluateMVA( "SVM_Poly method"      ) );
      if (Use["SVM_Lin"      ])   histSVML   ->Fill( reader->EvaluateMVA( "SVM_Lin method"       ) );
      if (Use["FDA_MT"       ])   histFDAMT  ->Fill( reader->EvaluateMVA( "FDA_MT method"        ) );
      if (Use["FDA_GA"       ])   histFDAGA  ->Fill( reader->EvaluateMVA( "FDA_GA method"        ) );
      if (Use["Category"     ])   histCat    ->Fill( reader->EvaluateMVA( "Category method"      ) );
      if (Use["Plugin"       ])   histPBdt   ->Fill( reader->EvaluateMVA( "P_BDT method"         ) );

      // Retrieve also per-event error
      if (Use["PDEFoam"]) {
         Double_t val = reader->EvaluateMVA( "PDEFoam method" );
         Double_t err = reader->GetMVAError();
         histPDEFoam   ->Fill( val );
         histPDEFoamErr->Fill( err );         
         if (err>1.e-50) histPDEFoamSig->Fill( val/err );
      }         

      // Retrieve probability instead of MVA output
      if (Use["Fisher"])   {
         probHistFi  ->Fill( reader->GetProba ( "Fisher method" ) );
         rarityHistFi->Fill( reader->GetRarity( "Fisher method" ) );
      }
   }

   // Get elapsed time
   sw.Stop();
   std::cout << "--- End of event loop: "; sw.Print();

   // Get efficiency for cuts classifier
   if (Use["CutsGA"]) std::cout << "--- Efficiency for CutsGA method: " << double(nSelCutsGA)/theTree->GetEntries()
                                << " (for a required signal efficiency of " << effS << ")" << std::endl;

   if (Use["CutsGA"]) {

      // test: retrieve cuts for particular signal efficiency
      // CINT ignores dynamic_casts so we have to use a cuts-secific Reader function to acces the pointer  
      TMVA::MethodCuts* mcuts = reader->FindCutsMVA( "CutsGA method" ) ;

      if (mcuts) {      
         std::vector<Double_t> cutsMin;
         std::vector<Double_t> cutsMax;
         mcuts->GetCuts( 0.7, cutsMin, cutsMax );
         std::cout << "--- -------------------------------------------------------------" << std::endl;
         std::cout << "--- Retrieve cut values for signal efficiency of 0.7 from Reader" << std::endl;
         for (UInt_t ivar=0; ivar<cutsMin.size(); ivar++) {
            std::cout << "... Cut: " 
                      << cutsMin[ivar] 
                      << " < \"" 
                      << mcuts->GetInputVar(ivar)
                      << "\" <= " 
                      << cutsMax[ivar] << std::endl;
         }
         std::cout << "--- -------------------------------------------------------------" << std::endl;
      }
   }

   // --- Write histograms

   TFile *target  = new TFile( "TMVApp.root","RECREATE" );
   if (Use["Likelihood"   ])   histLk     ->Write();
   if (Use["LikelihoodD"  ])   histLkD    ->Write();
   if (Use["LikelihoodPCA"])   histLkPCA  ->Write();
   if (Use["LikelihoodKDE"])   histLkKDE  ->Write();
   if (Use["LikelihoodMIX"])   histLkMIX  ->Write();
   if (Use["PDERS"        ])   histPD     ->Write();
   if (Use["PDERSD"       ])   histPDD    ->Write();
   if (Use["PDERSPCA"     ])   histPDPCA  ->Write();
   if (Use["KNN"          ])   histKNN    ->Write();
   if (Use["HMatrix"      ])   histHm     ->Write();
   if (Use["Fisher"       ])   histFi     ->Write();
   if (Use["FisherG"      ])   histFiG    ->Write();
   if (Use["BoostedFisher"])   histFiB    ->Write();
   if (Use["LD"           ])   histLD     ->Write();
   if (Use["MLP"          ])   histNn     ->Write();
   if (Use["MLPBFGS"      ])   histNnbfgs ->Write();
   if (Use["MLPBNN"       ])   histNnbnn  ->Write();
   if (Use["CFMlpANN"     ])   histNnC    ->Write();
   if (Use["TMlpANN"      ])   histNnT    ->Write();
   if (Use["BDT"          ])   histBdt    ->Write();
   if (Use["BDTD"         ])   histBdtD   ->Write();
   if (Use["BDTG"         ])   histBdtG   ->Write(); 
   if (Use["RuleFit"      ])   histRf     ->Write();
   if (Use["SVM_Gauss"    ])   histSVMG   ->Write();
   if (Use["SVM_Poly"     ])   histSVMP   ->Write();
   if (Use["SVM_Lin"      ])   histSVML   ->Write();
   if (Use["FDA_MT"       ])   histFDAMT  ->Write();
   if (Use["FDA_GA"       ])   histFDAGA  ->Write();
   if (Use["Category"     ])   histCat    ->Write();
   if (Use["Plugin"       ])   histPBdt   ->Write();

   // Write also error and significance histos
   if (Use["PDEFoam"]) { histPDEFoam->Write(); histPDEFoamErr->Write(); histPDEFoamSig->Write(); }

   // Write also probability hists
   if (Use["Fisher"]) { if (probHistFi != 0) probHistFi->Write(); if (rarityHistFi != 0) rarityHistFi->Write(); }
   target->Close();

   std::cout << "--- Created root file: \"TMVApp.root\" containing the MVA output histograms" << std::endl;
  
   delete reader;
    
   std::cout << "==> TMVAClassificationApplication is done!" << endl << std::endl;
} 
コード例 #22
0
ファイル: TMVAapply_double.C プロジェクト: chernyavskaya/Hbb
void TMVAapply_double::Loop(TString inputfile, TString output_dir,  int sample_type)
{
   if (fChain == 0) return;
   Long64_t nentries = fChain->GetEntriesFast();
   Long64_t nbytes = 0, nb = 0;

	Int_t presel=0;
	Int_t loopJet_max;
	TFile *output = new TFile("main_tmva_v13_Data_4_"+output_dir+".root","recreate");
	TTree *tree = fChain->CloneTree(0);
	float BDT_VBF;
	TBranch *branchBDT_VBF = tree->Branch("BDT_VBF",&BDT_VBF,"BDT_VBF/F");
	TString weightfile = "../weights/TMVAClassification_BDTG_double_all_4.weights.xml";
   TMVA::Reader *reader = new TMVA::Reader("Silent");
	float var1,var2,var3,var4,var5,var6,var7,var8,var9,var10, var11, var12;
	reader->AddVariable("Mqq",&var1);
	reader->AddVariable("DeltaEtaQQ",&var2);
	reader->AddVariable("DeltaPhiQQ",&var3);
	reader->AddVariable("SoftN5",&var4);
	reader->AddVariable("HTsoft",&var5);
	reader->AddVariable("CSV1",&var6);
   reader->AddVariable( "CSV2", &var7 );
	reader->AddVariable( "cosOqqbb", &var8 );
   reader->AddVariable( "DeltaEtaQB1", &var9 );
   reader->AddVariable( "DeltaEtaQB2", &var10 );
  	reader->AddVariable("qgl1",&var11);
   reader->AddVariable("qgl2",&var12);
	reader->BookMVA("BDTG", weightfile);


	TFile *input_file = new TFile(inputfile);
	TH1F*	Count = (TH1F*)input_file->Get("Count");
	TH1F*	CountPosWeight = (TH1F*)input_file->Get("CountPosWeight");
	TH1F*	CountNegWeight =(TH1F*)input_file->Get("CountNegWeight");
		

	int events_saved=0;
   for (Long64_t jentry=0; jentry<nentries;jentry++) {
      Long64_t ientry = LoadTree(jentry);
      nb = fChain->GetEntry(jentry);   nbytes += nb;
		
		int btag_max1_number = -1;
		int btag_max2_number = -1;
		int pt_max1_number = -1;
		int pt_max2_number = -1;
		TLorentzVector Bjet1;
		TLorentzVector Bjet2;
		TLorentzVector Qjet1;
		TLorentzVector Qjet2;
		TLorentzVector qq;

		if (preselection_double(nJet, Jet_pt,Jet_eta, Jet_phi, Jet_mass, Jet_btagCSV, Jet_id, btag_max1_number, btag_max2_number, pt_max1_number, pt_max2_number, HLT_BIT_HLT_QuadPFJet_DoubleBTagCSV_VBF_Mqq200_v, Bjet1, Bjet2, Qjet1, Qjet2, qq) !=0) continue;

		Float_t Mqq = qq.M();
		Float_t bbDeltaPhi = TMath::Abs(Bjet1.DeltaPhi(Bjet2));
		Double_t qqDeltaPhi = TMath::Abs(Qjet1.DeltaPhi(Qjet2));
		Float_t qqDeltaEta = TMath::Abs(Qjet1.Eta()-Qjet2.Eta());
		TLorentzVector bb;
		bb = Bjet1+Bjet2;
		Float_t Mbb = bb.M();
		TLorentzVector bbqq;
		bbqq = Bjet1 + Bjet2 + Qjet1 + Qjet2;
		Float_t cosOqqbb =TMath::Cos( ( ( Bjet1.Vect() ).Cross(Bjet2.Vect()) ).Angle( ( Qjet1.Vect() ).Cross(Qjet2.Vect()) ) );	


		Float_t EtaBQ1;
	 	Float_t EtaBQ2;
		Float_t PhiBQ1; 	
		Float_t PhiBQ2;
		 if (Qjet1.Eta() >= Qjet2.Eta()) {
			if (Bjet1.Eta() >= Bjet2.Eta())  {
				EtaBQ1 = Qjet1.Eta()-Bjet1.Eta();
				PhiBQ1 = TMath::Abs(Bjet1.DeltaPhi(Qjet1));		
			}
			else {
				EtaBQ1 = Qjet1.Eta()-Bjet2.Eta();
				PhiBQ1 = TMath::Abs(Bjet2.DeltaPhi(Qjet1));	
			}	
		} else if (Bjet1.Eta() >= Bjet2.Eta()) {
				EtaBQ1 = Qjet2.Eta()-Bjet1.Eta();
				PhiBQ1 = TMath::Abs(Bjet1.DeltaPhi(Qjet2));	
				
				}
			else {
				EtaBQ1 = Qjet2.Eta()-Bjet2.Eta();
				PhiBQ1 = TMath::Abs(Bjet2.DeltaPhi(Qjet2));	
			}


		 if (Qjet1.Eta() <= Qjet2.Eta()) {
			if (Bjet1.Eta() <= Bjet2.Eta())  {
				EtaBQ2 = Qjet1.Eta()-Bjet1.Eta();
				PhiBQ2 = TMath::Abs(Bjet1.DeltaPhi(Qjet1));		
			}
			else {
				EtaBQ2 = Qjet1.Eta()-Bjet2.Eta();
				PhiBQ2 = TMath::Abs(Bjet2.DeltaPhi(Qjet1));	
			}	
		} else if (Bjet1.Eta() <= Bjet2.Eta()) {
				EtaBQ2 = Qjet2.Eta()-Bjet1.Eta();
				PhiBQ2 = TMath::Abs(Bjet1.DeltaPhi(Qjet2));	
				
				}
			else {
				EtaBQ2 = Qjet2.Eta()-Bjet2.Eta();
				PhiBQ2 = TMath::Abs(Bjet2.DeltaPhi(Qjet2));	
			}
		


		Float_t Etot = Bjet1.E()+Bjet2.E()+Qjet1.E()+Qjet2.E();
		Float_t PzTot = Bjet1.Pz()+Bjet2.Pz()+Qjet1.Pz()+Qjet2.Pz();
		Float_t PxTot = Bjet1.Px()+Bjet2.Px()+Qjet1.Px()+Qjet2.Px();
		Float_t PyTot = Bjet1.Py()+Bjet2.Py()+Qjet1.Py()+Qjet2.Py();
	
		Float_t x1 = 0.;
		Float_t x2 = 0.;
		x1 = (Etot + PzTot)/2./13000.;
		x2 = (Etot - PzTot)/2./13000.;

		TLorentzVector q1,q2,q1_after,q2_after, VB1, VB2;
		q1.SetPxPyPzE(0.,0.,13000./2.*x1,13000./2.*x1);
		q2.SetPxPyPzE(0.,0.,-13000./2.*x2,13000./2.*x2);
		q1_after.SetPxPyPzE(Qjet1.Px()/Qjet1.Beta(),Qjet1.Py()/Qjet1.Beta(),Qjet1.Pz()/Qjet1.Beta(),Qjet1.E());
		q2_after.SetPxPyPzE(Qjet2.Px()/Qjet2.Beta(),Qjet2.Py()/Qjet2.Beta(),Qjet2.Pz()/Qjet2.Beta(),Qjet2.E());
		if (q1_after.Eta()>=0.) {
			VB1 = -q1_after+q1;
			VB2 = -q2_after+q2;
		} else {
			VB1 = -q2_after+q1;
			VB2 = -q1_after+q2;
		} 
		Float_t VB1_mass, VB2_mass;
		VB1_mass = TMath::Abs(VB1.M());
		VB2_mass = TMath::Abs(VB2.M());

		for (int i=0;i<nJet;i++){
			if (Jet_btagCSV[i]>1) Jet_btagCSV[i]=1.;
			if (Jet_btagCSV[i]<0) Jet_btagCSV[i]=0.;
		}

		var1= Mqq;
		var6= Jet_btagCSV[btag_max1_number];	
		var7= Jet_btagCSV[btag_max2_number];	
		var2= qqDeltaEta;
		var3= qqDeltaPhi;
		var4= softActivity_njets5;
		var5= softActivity_HT;
		var9= EtaBQ1;
		var10= EtaBQ2;
		var8= cosOqqbb;
		var11=Jet_qgl[pt_max1_number];
		var12 = Jet_qgl[pt_max2_number];
	
		BDT_VBF = reader->EvaluateMVA("BDTG");


	tree->Fill();
	//	events_saved++;		
//		if (sample_type==2) 
//			if (events_saved>=108767) break; //for 500-700

	}
	delete reader;
	output->cd();
	tree->AutoSave();
	Count->Write();
	CountPosWeight->Write();
	CountNegWeight->Write();
	output->Close();
}
コード例 #23
0
ファイル: Classify.C プロジェクト: cmstas/SingleLepton2012
void Classify_HWW( TString myMethodList = "" ) 
{   
#ifdef __CINT__
  gROOT->ProcessLine( ".O0" ); // turn off optimization in CINT
#endif

  //--------------------------------------------------------------------
  // path to weights dir (this is where MVA training info is stored)
  // output root file will be stored at [path]/output
  //--------------------------------------------------------------------

  TString path   = "Trainings/v5/H160_WW_10vars_dphi10/";
  //TString path   = "./";

  //-----------------------------------
  // select samples to run over
  //-----------------------------------

  char* babyPath = "/tas/cerati/HtoWWmvaBabies/latest";
  int mH         = 160;  // choose Higgs mass

  vector<char*> samples;
  samples.push_back("WWTo2L2Nu");
  samples.push_back("GluGluToWWTo4L");
  samples.push_back("WZ");
  samples.push_back("ZZ");
  samples.push_back("TTJets");
  samples.push_back("tW");
  samples.push_back("WJetsToLNu");
  samples.push_back("DY");
  //samples.push_back("WJetsFO3");

  if     ( mH == 130 ) samples.push_back("Higgs130");
  else if( mH == 160 ) samples.push_back("Higgs160");
  else if( mH == 200 ) samples.push_back("Higgs200");
  else{
    cout << "Error, unrecognized Higgs mass " << mH << " GeV, quitting" << endl;
    exit(0);
  }

  //--------------------------------------------------------------------------------
  // IMPORTANT: set the following variables to the same set used for MVA training!!!
  //--------------------------------------------------------------------------------
  
  std::map<std::string,int> mvaVar;
  mvaVar[ "lephard_pt" ]        = 1;
  mvaVar[ "lepsoft_pt" ]        = 1;
  mvaVar[ "dil_dphi" ]          = 1;
  mvaVar[ "dil_mass" ]          = 1;
  mvaVar[ "event_type" ]        = 0;
  mvaVar[ "met_projpt" ]        = 1;
  mvaVar[ "met_pt" ]            = 0;
  mvaVar[ "mt_lephardmet" ]     = 1;
  mvaVar[ "mt_lepsoftmet" ]     = 1;
  mvaVar[ "mthiggs" ]           = 1;
  mvaVar[ "dphi_lephardmet" ]   = 1;
  mvaVar[ "dphi_lepsoftmet" ]   = 1;
  mvaVar[ "lepsoft_fbrem" ]     = 0;
  mvaVar[ "lepsoft_eOverPIn" ]  = 0;
  mvaVar[ "lepsoft_qdphi" ]     = 0;

  //---------------------------------------------------------------

  // This loads the library
  TMVA::Tools::Instance();

  // Default MVA methods to be trained + tested
  std::map<std::string,int> Use;

  // --- Cut optimisation
  Use["Cuts"]            = 1;
  Use["CutsD"]           = 1;
  Use["CutsPCA"]         = 0;
  Use["CutsGA"]          = 0;
  Use["CutsSA"]          = 0;
  // 
  // --- 1-dimensional likelihood ("naive Bayes estimator")
  Use["Likelihood"]      = 1;
  Use["LikelihoodD"]     = 0; // the "D" extension indicates decorrelated input variables (see option strings)
  Use["LikelihoodPCA"]   = 1; // the "PCA" extension indicates PCA-transformed input variables (see option strings)
  Use["LikelihoodKDE"]   = 0;
  Use["LikelihoodMIX"]   = 0;
  //
  // --- Mutidimensional likelihood and Nearest-Neighbour methods
  Use["PDERS"]           = 1;
  Use["PDERSD"]          = 0;
  Use["PDERSPCA"]        = 0;
  Use["PDEFoam"]         = 1;
  Use["PDEFoamBoost"]    = 0; // uses generalised MVA method boosting
  Use["KNN"]             = 1; // k-nearest neighbour method
  //
  // --- Linear Discriminant Analysis
  Use["LD"]              = 1; // Linear Discriminant identical to Fisher
  Use["Fisher"]          = 0;
  Use["FisherG"]         = 0;
  Use["BoostedFisher"]   = 0; // uses generalised MVA method boosting
  Use["HMatrix"]         = 0;
  //
  // --- Function Discriminant analysis
  Use["FDA_GA"]          = 1; // minimisation of user-defined function using Genetics Algorithm
  Use["FDA_SA"]          = 0;
  Use["FDA_MC"]          = 0;
  Use["FDA_MT"]          = 0;
  Use["FDA_GAMT"]        = 0;
  Use["FDA_MCMT"]        = 0;
  //
  // --- Neural Networks (all are feed-forward Multilayer Perceptrons)
  Use["MLP"]             = 0; // Recommended ANN
  Use["MLPBFGS"]         = 0; // Recommended ANN with optional training method
  Use["MLPBNN"]          = 1; // Recommended ANN with BFGS training method and bayesian regulator
  Use["CFMlpANN"]        = 0; // Depreciated ANN from ALEPH
  Use["TMlpANN"]         = 0; // ROOT's own ANN
  //
  // --- Support Vector Machine 
  Use["SVM"]             = 1;
  // 
  // --- Boosted Decision Trees
  Use["BDT"]             = 1; // uses Adaptive Boost
  Use["BDTG"]            = 0; // uses Gradient Boost
  Use["BDTB"]            = 0; // uses Bagging
  Use["BDTD"]            = 0; // decorrelation + Adaptive Boost
  // 
  // --- Friedman's RuleFit method, ie, an optimised series of cuts ("rules")
  Use["RuleFit"]         = 1;
  // ---------------------------------------------------------------
  Use["Plugin"]          = 0;
  Use["Category"]        = 0;
  Use["SVM_Gauss"]       = 0;
  Use["SVM_Poly"]        = 0;
  Use["SVM_Lin"]         = 0;

  std::cout << std::endl;
  std::cout << "==> Start TMVAClassificationApplication" << std::endl;

  // Select methods (don't look at this code - not of interest)
  if (myMethodList != "") {
    for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) it->second = 0;

    std::vector<TString> mlist = gTools().SplitString( myMethodList, ',' );
    for (UInt_t i=0; i<mlist.size(); i++) {
      std::string regMethod(mlist[i]);

      if (Use.find(regMethod) == Use.end()) {
        std::cout << "Method \"" << regMethod 
                  << "\" not known in TMVA under this name. Choose among the following:" << std::endl;
        for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) {
          std::cout << it->first << " ";
        }
        std::cout << std::endl;
        return;
      }
      Use[regMethod] = 1;
    }
  }

  // --------------------------------------------------------------------------------------------------

  const unsigned int nsamples = samples.size();
  
  for( unsigned int i = 0 ; i < nsamples ; ++i ){

    // --- Create the Reader object

    TMVA::Reader *reader = new TMVA::Reader( "!Color:!Silent" );    

    // Create a set of variables and declare them to the reader
    // - the variable names MUST corresponds in name and type to those given in the weight file(s) used
    //    Float_t var1, var2;
    //    Float_t var3, var4;
    //    reader->AddVariable( "myvar1 := var1+var2", &var1 );
    //    reader->AddVariable( "myvar2 := var1-var2", &var2 );
    //    reader->AddVariable( "var3",                &var3 );
    //    reader->AddVariable( "var4",                &var4 );

    Float_t lephard_pt;
    Float_t lepsoft_pt;
    Float_t dil_dphi;
    Float_t dil_mass;
    Float_t event_type;
    Float_t met_projpt;
    Float_t met_pt;
    Float_t mt_lephardmet;
    Float_t mt_lepsoftmet;
    Float_t mthiggs;
    Float_t dphi_lephardmet;
    Float_t dphi_lepsoftmet;
    Float_t lepsoft_fbrem;
    Float_t lepsoft_eOverPIn;
    Float_t lepsoft_qdphi;

    if( mvaVar["lephard_pt"])       reader->AddVariable( "lephard_pt"                  ,   &lephard_pt        ); 
    if( mvaVar["lepsoft_pt"])       reader->AddVariable( "lepsoft_pt"                  ,   &lepsoft_pt        ); 
    if( mvaVar["dil_dphi"])         reader->AddVariable( "dil_dphi"                    ,   &dil_dphi          ); 
    if( mvaVar["dil_mass"])         reader->AddVariable( "dil_mass"                    ,   &dil_mass          ); 
    if( mvaVar["event_type"])       reader->AddVariable( "event_type"                  ,   &event_type        );
    if( mvaVar["met_projpt"])       reader->AddVariable( "met_projpt"                  ,   &met_pt            );
    if( mvaVar["met_pt"])           reader->AddVariable( "met_pt"                      ,   &met_pt            );
    if( mvaVar["mt_lephardmet"])    reader->AddVariable( "mt_lephardmet"               ,   &mt_lephardmet     );
    if( mvaVar["mt_lepsoftmet"])    reader->AddVariable( "mt_lepsoftmet"               ,   &mt_lepsoftmet     );
    if( mvaVar["mthiggs"])          reader->AddVariable( "mthiggs"                     ,   &mthiggs           );  
    if( mvaVar["dphi_lephardmet"])  reader->AddVariable( "dphi_lephardmet"             ,   &dphi_lephardmet   );
    if( mvaVar["dphi_lepsoftmet"])  reader->AddVariable( "dphi_lepsoftmet"             ,   &dphi_lepsoftmet   );
    if( mvaVar["lepsoft_fbrem"])    reader->AddVariable( "lepsoft_fbrem"               ,   &lepsoft_fbrem     );
    if( mvaVar["lepsoft_eOverPIn"]) reader->AddVariable( "lepsoft_eOverPIn"            ,   &lepsoft_eOverPIn  );
    if( mvaVar["lepsoft_qdphi"])    reader->AddVariable( "lepsoft_q * lepsoft_dPhiIn"  ,   &lepsoft_qdphi     );
 

    // Spectator variables declared in the training have to be added to the reader, too
    //    Float_t spec1,spec2;
    //    reader->AddSpectator( "spec1 := var1*2",   &spec1 );
    //    reader->AddSpectator( "spec2 := var1*3",   &spec2 );

    Float_t Category_cat1, Category_cat2, Category_cat3;
    if (Use["Category"]){
      // Add artificial spectators for distinguishing categories
      //       reader->AddSpectator( "Category_cat1 := var3<=0",             &Category_cat1 );
      //       reader->AddSpectator( "Category_cat2 := (var3>0)&&(var4<0)",  &Category_cat2 );
      //       reader->AddSpectator( "Category_cat3 := (var3>0)&&(var4>=0)", &Category_cat3 );
    }

    // --- Book the MVA methods

    //--------------------------------------------------------------------------------------
    // tell Classify_HWW where to find the weights dir, which contains the trained MVA's. 
    // In this example, the weights dir is located at [path]/[dir]
    // and the output root file is written to [path]/[output]
    //--------------------------------------------------------------------------------------

    TString dir    = path + "weights/";
    TString outdir = path + "output/";
    TString prefix = "TMVAClassification";

    // Book method(s)
    for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) {
      if (it->second) {
        TString methodName = TString(it->first) + TString(" method");
        TString weightfile = dir + prefix + TString("_") + TString(it->first) + TString(".weights.xml");
        reader->BookMVA( methodName, weightfile ); 
      }
    }
   
    // Book output histograms
    UInt_t nbin = 1000;
    TH1F   *histLk(0), *histLkD(0), *histLkPCA(0), *histLkKDE(0), *histLkMIX(0), *histPD(0), *histPDD(0);
    TH1F   *histPDPCA(0), *histPDEFoam(0), *histPDEFoamErr(0), *histPDEFoamSig(0), *histKNN(0), *histHm(0);
    TH1F   *histFi(0), *histFiG(0), *histFiB(0), *histLD(0), *histNn(0),*histNnbfgs(0),*histNnbnn(0);
    TH1F   *histNnC(0), *histNnT(0), *histBdt(0), *histBdtG(0), *histBdtD(0), *histRf(0), *histSVMG(0);
    TH1F   *histSVMP(0), *histSVML(0), *histFDAMT(0), *histFDAGA(0), *histCat(0), *histPBdt(0);

    if (Use["Likelihood"])    histLk      = new TH1F( "MVA_Likelihood",    "MVA_Likelihood",    nbin, -1, 1 );               
    if (Use["LikelihoodD"])   histLkD     = new TH1F( "MVA_LikelihoodD",   "MVA_LikelihoodD",   nbin, -1, 0.9999 );
    if (Use["LikelihoodPCA"]) histLkPCA   = new TH1F( "MVA_LikelihoodPCA", "MVA_LikelihoodPCA", nbin, -1, 1 );
    if (Use["LikelihoodKDE"]) histLkKDE   = new TH1F( "MVA_LikelihoodKDE", "MVA_LikelihoodKDE", nbin,  -0.00001, 0.99999 );
    if (Use["LikelihoodMIX"]) histLkMIX   = new TH1F( "MVA_LikelihoodMIX", "MVA_LikelihoodMIX", nbin,  0, 1 );
    if (Use["PDERS"])         histPD      = new TH1F( "MVA_PDERS",         "MVA_PDERS",         nbin,  0, 1 );
    if (Use["PDERSD"])        histPDD     = new TH1F( "MVA_PDERSD",        "MVA_PDERSD",        nbin,  0, 1 );
    if (Use["PDERSPCA"])      histPDPCA   = new TH1F( "MVA_PDERSPCA",      "MVA_PDERSPCA",      nbin,  0, 1 );
    if (Use["KNN"])           histKNN     = new TH1F( "MVA_KNN",           "MVA_KNN",           nbin,  0, 1 );
    if (Use["HMatrix"])       histHm      = new TH1F( "MVA_HMatrix",       "MVA_HMatrix",       nbin, -0.95, 1.55 );
    if (Use["Fisher"])        histFi      = new TH1F( "MVA_Fisher",        "MVA_Fisher",        nbin, -4, 4 );
    if (Use["FisherG"])       histFiG     = new TH1F( "MVA_FisherG",       "MVA_FisherG",       nbin, -1, 1 );
    if (Use["BoostedFisher"]) histFiB     = new TH1F( "MVA_BoostedFisher", "MVA_BoostedFisher", nbin, -2, 2 );
    if (Use["LD"])            histLD      = new TH1F( "MVA_LD",            "MVA_LD",            nbin, -2, 2 );
    if (Use["MLP"])           histNn      = new TH1F( "MVA_MLP",           "MVA_MLP",           nbin, -1.25, 1.5 );
    if (Use["MLPBFGS"])       histNnbfgs  = new TH1F( "MVA_MLPBFGS",       "MVA_MLPBFGS",       nbin, -1.25, 1.5 );
    if (Use["MLPBNN"])        histNnbnn   = new TH1F( "MVA_MLPBNN",        "MVA_MLPBNN",        nbin, -1.25, 1.5 );
    if (Use["CFMlpANN"])      histNnC     = new TH1F( "MVA_CFMlpANN",      "MVA_CFMlpANN",      nbin,  0, 1 );
    if (Use["TMlpANN"])       histNnT     = new TH1F( "MVA_TMlpANN",       "MVA_TMlpANN",       nbin, -1.3, 1.3 );
    if (Use["BDT"])           histBdt     = new TH1F( "MVA_BDT",           "MVA_BDT",           nbin, -1. , 1. );
    if (Use["BDTD"])          histBdtD    = new TH1F( "MVA_BDTD",          "MVA_BDTD",          nbin, -0.8, 0.8 );
    if (Use["BDTG"])          histBdtG    = new TH1F( "MVA_BDTG",          "MVA_BDTG",          nbin, -1.0, 1.0 );
    if (Use["RuleFit"])       histRf      = new TH1F( "MVA_RuleFit",       "MVA_RuleFit",       nbin, -2.0, 2.0 );
    if (Use["SVM_Gauss"])     histSVMG    = new TH1F( "MVA_SVM_Gauss",     "MVA_SVM_Gauss",     nbin,  0.0, 1.0 );
    if (Use["SVM_Poly"])      histSVMP    = new TH1F( "MVA_SVM_Poly",      "MVA_SVM_Poly",      nbin,  0.0, 1.0 );
    if (Use["SVM_Lin"])       histSVML    = new TH1F( "MVA_SVM_Lin",       "MVA_SVM_Lin",       nbin,  0.0, 1.0 );
    if (Use["FDA_MT"])        histFDAMT   = new TH1F( "MVA_FDA_MT",        "MVA_FDA_MT",        nbin, -2.0, 3.0 );
    if (Use["FDA_GA"])        histFDAGA   = new TH1F( "MVA_FDA_GA",        "MVA_FDA_GA",        nbin, -2.0, 3.0 );
    if (Use["Category"])      histCat     = new TH1F( "MVA_Category",      "MVA_Category",      nbin, -2., 2. );
    if (Use["Plugin"])        histPBdt    = new TH1F( "MVA_PBDT",          "MVA_BDT",           nbin, -0.8, 0.8 );

    if (Use["Likelihood"])    histLk      ->Sumw2();
    if (Use["LikelihoodD"])   histLkD     ->Sumw2();
    if (Use["LikelihoodPCA"]) histLkPCA   ->Sumw2();
    if (Use["LikelihoodKDE"]) histLkKDE   ->Sumw2();
    if (Use["LikelihoodMIX"]) histLkMIX   ->Sumw2();
    if (Use["PDERS"])         histPD      ->Sumw2();
    if (Use["PDERSD"])        histPDD     ->Sumw2();
    if (Use["PDERSPCA"])      histPDPCA   ->Sumw2();
    if (Use["KNN"])           histKNN     ->Sumw2();
    if (Use["HMatrix"])       histHm      ->Sumw2();
    if (Use["Fisher"])        histFi      ->Sumw2();
    if (Use["FisherG"])       histFiG     ->Sumw2();
    if (Use["BoostedFisher"]) histFiB     ->Sumw2();
    if (Use["LD"])            histLD      ->Sumw2();
    if (Use["MLP"])           histNn      ->Sumw2();
    if (Use["MLPBFGS"])       histNnbfgs  ->Sumw2();
    if (Use["MLPBNN"])        histNnbnn   ->Sumw2();
    if (Use["CFMlpANN"])      histNnC     ->Sumw2();
    if (Use["TMlpANN"])       histNnT     ->Sumw2();
    if (Use["BDT"])           histBdt     ->Sumw2();
    if (Use["BDTD"])          histBdtD    ->Sumw2();
    if (Use["BDTG"])          histBdtG    ->Sumw2();
    if (Use["RuleFit"])       histRf      ->Sumw2();
    if (Use["SVM_Gauss"])     histSVMG    ->Sumw2();
    if (Use["SVM_Poly"])      histSVMP    ->Sumw2();
    if (Use["SVM_Lin"])       histSVML    ->Sumw2();
    if (Use["FDA_MT"])        histFDAMT   ->Sumw2();
    if (Use["FDA_GA"])        histFDAGA   ->Sumw2();
    if (Use["Category"])      histCat     ->Sumw2();
    if (Use["Plugin"])        histPBdt    ->Sumw2();

    // PDEFoam also returns per-event error, fill in histogram, and also fill significance
    if (Use["PDEFoam"]) {
      histPDEFoam    = new TH1F( "MVA_PDEFoam",       "MVA_PDEFoam",              nbin,  0, 1 );
      histPDEFoamErr = new TH1F( "MVA_PDEFoamErr",    "MVA_PDEFoam error",        nbin,  0, 1 );
      histPDEFoamSig = new TH1F( "MVA_PDEFoamSig",    "MVA_PDEFoam significance", nbin,  0, 10 );
    }

    // Book example histogram for probability (the other methods are done similarly)
    TH1F *probHistFi(0), *rarityHistFi(0);
    if (Use["Fisher"]) {
      probHistFi   = new TH1F( "MVA_Fisher_Proba",  "MVA_Fisher_Proba",  nbin, 0, 1 );
      rarityHistFi = new TH1F( "MVA_Fisher_Rarity", "MVA_Fisher_Rarity", nbin, 0, 1 );
    }

    // Prepare input tree (this must be replaced by your data source)
    // in this example, there is a toy tree with signal and one with background events
    // we'll later on use only the "signal" events for the test in this example.
    //   

 
    TChain *ch = new TChain("Events");

    if( strcmp( samples.at(i) , "DY" ) == 0 ){
      ch -> Add( Form("%s/DYToMuMuM20_PU_testFinal_baby.root",babyPath) );
      ch -> Add( Form("%s/DYToMuMuM10To20_PU_testFinal_baby.root",babyPath) );
      ch -> Add( Form("%s/DYToEEM20_PU_testFinal_baby.root",babyPath) );
      ch -> Add( Form("%s/DYToEEM10To20_PU_testFinal_baby.root",babyPath) );
      ch -> Add( Form("%s/DYToTauTauM20_PU_testFinal_baby.root",babyPath) );
      ch -> Add( Form("%s/DYToTauTauM10To20_PU_testFinal_baby.root",babyPath) );
    }
    if( strcmp( samples.at(i) , "WJetsFO3" ) == 0 ){
      ch -> Add( Form("%s/WJetsToLNu_FOv3_PU_testFinal_baby.root",babyPath) );
      ch -> Add( Form("%s/WToLNu_FOv3_testFinal_baby.root",babyPath) );
    }
    else if( strcmp( samples.at(i) , "Higgs130" ) == 0 ){
      ch -> Add( Form("%s/HToWWTo2L2NuM130_PU_testFinal_baby.root",babyPath) );
      ch -> Add( Form("%s/HToWWToLNuTauNuM130_PU_testFinal_baby.root",babyPath) );
      ch -> Add( Form("%s/HToWWTo2Tau2NuM130_PU_testFinal_baby.root",babyPath) );
    }
    else if( strcmp( samples.at(i) , "Higgs160" ) == 0 ){
      ch -> Add( Form("%s/HToWWTo2L2NuM160_PU_testFinal_baby.root",babyPath) );
      ch -> Add( Form("%s/HToWWToLNuTauNuM160_PU_testFinal_baby.root",babyPath) );
      ch -> Add( Form("%s/HToWWTo2Tau2NuM160_PU_testFinal_baby.root",babyPath) );
    }
    else if( strcmp( samples.at(i) , "Higgs200" ) == 0 ){
      ch -> Add( Form("%s/HToWWTo2L2NuM200_PU_testFinal_baby.root",babyPath) );
      ch -> Add( Form("%s/HToWWToLNuTauNuM200_PU_testFinal_baby.root",babyPath) );
      ch -> Add( Form("%s/HToWWTo2Tau2NuM200_PU_testFinal_baby.root",babyPath) );
    }
    else{
      ch -> Add( Form("%s/%s_PU_testFinal_baby.root",babyPath,samples.at(i)) );
    }

    // --- Event loop

    // Prepare the event tree
    // - here the variable names have to corresponds to your tree
    // - you can use the same variables as above which is slightly faster,
    //   but of course you can use different ones and copy the values inside the event loop
    //
  
    TTree *theTree     = (TTree*) ch;

    std::cout << "--- Using input files: -------------------" <<  std::endl;

    TObjArray *listOfFiles = ch->GetListOfFiles();
    TIter fileIter(listOfFiles);
    TChainElement* currentFile = 0;
    
    while((currentFile = (TChainElement*)fileIter.Next())) {
      std::cout << currentFile->GetTitle() << std::endl;
    }

    Float_t lephard_pt_;
    Float_t lepsoft_pt_;
    Float_t lepsoft_fr_;
    Float_t dil_dphi_;
    Float_t dil_mass_;
    Float_t event_type_;
    Float_t met_projpt_;
    Int_t   jets_num_;
    Int_t   extralep_num_;
    Int_t   lowptbtags_num_;
    Int_t   softmu_num_;
    Float_t event_scale1fb_;
    Float_t met_pt_;
    Int_t   lepsoft_passTighterId_;
    Float_t mt_lephardmet_;
    Float_t mt_lepsoftmet_;
    Float_t mthiggs_;
    Float_t dphi_lephardmet_;
    Float_t dphi_lepsoftmet_;
    Float_t lepsoft_fbrem_;
    Float_t lepsoft_eOverPIn_;
    Float_t lepsoft_q_;
    Float_t lepsoft_dPhiIn_;

    theTree->SetBranchAddress( "lephard_pt_"             ,   &lephard_pt_              ); 
    theTree->SetBranchAddress( "lepsoft_pt_"             ,   &lepsoft_pt_              ); 
    theTree->SetBranchAddress( "lepsoft_fr_"             ,   &lepsoft_fr_              ); 
    theTree->SetBranchAddress( "dil_dphi_"               ,   &dil_dphi_                ); 
    theTree->SetBranchAddress( "dil_mass_"               ,   &dil_mass_                ); 
    theTree->SetBranchAddress( "event_type_"             ,   &event_type_              ); 
    theTree->SetBranchAddress( "met_projpt_"             ,   &met_projpt_              ); 
    theTree->SetBranchAddress( "jets_num_"               ,   &jets_num_                ); 
    theTree->SetBranchAddress( "extralep_num_"           ,   &extralep_num_            ); 
    theTree->SetBranchAddress( "lowptbtags_num_"         ,   &lowptbtags_num_          ); 
    theTree->SetBranchAddress( "softmu_num_"             ,   &softmu_num_              ); 
    theTree->SetBranchAddress( "event_scale1fb_"         ,   &event_scale1fb_          ); 
    theTree->SetBranchAddress( "lepsoft_passTighterId_"  ,   &lepsoft_passTighterId_   );
    theTree->SetBranchAddress( "met_pt_"                 ,   &met_pt_                  );
    theTree->SetBranchAddress( "mt_lephardmet_"          ,   &mt_lephardmet_           );
    theTree->SetBranchAddress( "mt_lepsoftmet_"          ,   &mt_lepsoftmet_           );
    theTree->SetBranchAddress( "mthiggs_"                ,   &mthiggs_                 );
    theTree->SetBranchAddress( "dphi_lephardmet_"        ,   &dphi_lephardmet_         );
    theTree->SetBranchAddress( "dphi_lepsoftmet_"        ,   &dphi_lepsoftmet_         );
    theTree->SetBranchAddress( "lepsoft_fbrem_"          ,   &lepsoft_fbrem_           );
    theTree->SetBranchAddress( "lepsoft_eOverPIn_"       ,   &lepsoft_eOverPIn_        );
    theTree->SetBranchAddress( "lepsoft_q_"              ,   &lepsoft_q_               );
    theTree->SetBranchAddress( "lepsoft_dPhiIn_"         ,   &lepsoft_dPhiIn_          );

    // Efficiency calculator for cut method
    Int_t    nSelCutsGA = 0;
    Double_t effS       = 0.7;

    std::vector<Float_t> vecVar(4); // vector for EvaluateMVA tests

    std::cout << "--- Processing: " << theTree->GetEntries() << " events" << std::endl;
    TStopwatch sw;
    sw.Start();

    int npass   = 0;
    float yield = 0.;
    
    for (Long64_t ievt=0; ievt<theTree->GetEntries();ievt++) {

      if (ievt%1000 == 0) std::cout << "--- ... Processing event: " << ievt << std::endl;

      theTree->GetEntry(ievt);

      //-------------------------------------------------------
      // event selection
      //-------------------------------------------------------

      if( dil_dphi_ > 1. ) continue;

      //em
      if( event_type_ > 0.5 && event_type_ < 2.5 ){
        if( met_projpt_ < 20. )   continue;
      }
      //ee/mm
      if( event_type_ < 0.5 || event_type_ > 2.5 ){
        if( met_projpt_ < 35. )   continue;
      }
      if( lephard_pt_ < 20.           )             continue;
      if( jets_num_ > 0               )             continue;
      if( extralep_num_ > 0           )             continue;
      if( lowptbtags_num_ > 0         )             continue;
      if( softmu_num_ > 0             )             continue;
      if( dil_mass_ < 12.             )             continue;
      if( lepsoft_passTighterId_ == 0 )             continue;
      //if( event_type_ < 1.5    )                    continue;
      //if( event_type > 1.5 && lepsoft_pt_ < 15. )   continue;

      //mH-dependent selection
      if( mH == 130 ){
        if( lepsoft_pt_ < 10.    )                  continue;      
        if( dil_mass_   > 90.    )                  continue;     
      }
      else if( mH == 160 ){
        if( lepsoft_pt_ < 20.    )                  continue;      
        if( dil_mass_   > 100.   )                  continue;     
      }
      else if( mH == 200 ){
        if( lepsoft_pt_ < 20.    )                  continue;      
        if( dil_mass_   > 130.   )                  continue;     
      }

      float weight = event_scale1fb_ * lepsoft_fr_ * 0.5;

      //--------------------------------------------------------
      // important: here we associate branches to MVA variables
      //--------------------------------------------------------

      lephard_pt        = lephard_pt_;
      lepsoft_pt        = lepsoft_pt_;
      dil_mass          = dil_mass_;
      dil_dphi          = dil_dphi_;
      event_type        = event_type_;
      met_pt            = met_pt_;
      met_projpt        = met_projpt_;
      mt_lephardmet     = mt_lephardmet_;
      mt_lepsoftmet     = mt_lepsoftmet_;
      mthiggs           = mthiggs_;
      dphi_lephardmet   = dphi_lephardmet_;
      dphi_lepsoftmet   = dphi_lepsoftmet_;
      lepsoft_fbrem     = lepsoft_fbrem_;
      lepsoft_eOverPIn  = lepsoft_eOverPIn_;
      lepsoft_qdphi     = lepsoft_q_ * lepsoft_dPhiIn_;

      npass++;
      yield+=weight;

      //       var1 = userVar1 + userVar2;
      //       var2 = userVar1 - userVar2;

      // --- Return the MVA outputs and fill into histograms

      if (Use["CutsGA"]) {
        // Cuts is a special case: give the desired signal efficienciy
        Bool_t passed = reader->EvaluateMVA( "CutsGA method", effS );
        if (passed) nSelCutsGA++;
      }

      if (Use["Likelihood"   ])   histLk     ->Fill( reader->EvaluateMVA( "Likelihood method"    ) , weight);
      if (Use["LikelihoodD"  ])   histLkD    ->Fill( reader->EvaluateMVA( "LikelihoodD method"   ) , weight);
      if (Use["LikelihoodPCA"])   histLkPCA  ->Fill( reader->EvaluateMVA( "LikelihoodPCA method" ) , weight);
      if (Use["LikelihoodKDE"])   histLkKDE  ->Fill( reader->EvaluateMVA( "LikelihoodKDE method" ) , weight);
      if (Use["LikelihoodMIX"])   histLkMIX  ->Fill( reader->EvaluateMVA( "LikelihoodMIX method" ) , weight);
      if (Use["PDERS"        ])   histPD     ->Fill( reader->EvaluateMVA( "PDERS method"         ) , weight);
      if (Use["PDERSD"       ])   histPDD    ->Fill( reader->EvaluateMVA( "PDERSD method"        ) , weight);
      if (Use["PDERSPCA"     ])   histPDPCA  ->Fill( reader->EvaluateMVA( "PDERSPCA method"      ) , weight);
      if (Use["KNN"          ])   histKNN    ->Fill( reader->EvaluateMVA( "KNN method"           ) , weight);
      if (Use["HMatrix"      ])   histHm     ->Fill( reader->EvaluateMVA( "HMatrix method"       ) , weight);
      if (Use["Fisher"       ])   histFi     ->Fill( reader->EvaluateMVA( "Fisher method"        ) , weight);
      if (Use["FisherG"      ])   histFiG    ->Fill( reader->EvaluateMVA( "FisherG method"       ) , weight);
      if (Use["BoostedFisher"])   histFiB    ->Fill( reader->EvaluateMVA( "BoostedFisher method" ) , weight);
      if (Use["LD"           ])   histLD     ->Fill( reader->EvaluateMVA( "LD method"            ) , weight);
      if (Use["MLP"          ])   histNn     ->Fill( reader->EvaluateMVA( "MLP method"           ) , weight);
      if (Use["MLPBFGS"      ])   histNnbfgs ->Fill( reader->EvaluateMVA( "MLPBFGS method"       ) , weight);
      if (Use["MLPBNN"       ])   histNnbnn  ->Fill( reader->EvaluateMVA( "MLPBNN method"        ) , weight);
      if (Use["CFMlpANN"     ])   histNnC    ->Fill( reader->EvaluateMVA( "CFMlpANN method"      ) , weight);
      if (Use["TMlpANN"      ])   histNnT    ->Fill( reader->EvaluateMVA( "TMlpANN method"       ) , weight);
      if (Use["BDT"          ])   histBdt    ->Fill( reader->EvaluateMVA( "BDT method"           ) , weight);
      if (Use["BDTD"         ])   histBdtD   ->Fill( reader->EvaluateMVA( "BDTD method"          ) , weight);
      if (Use["BDTG"         ])   histBdtG   ->Fill( reader->EvaluateMVA( "BDTG method"          ) , weight);
      if (Use["RuleFit"      ])   histRf     ->Fill( reader->EvaluateMVA( "RuleFit method"       ) , weight);
      if (Use["SVM_Gauss"    ])   histSVMG   ->Fill( reader->EvaluateMVA( "SVM_Gauss method"     ) , weight);
      if (Use["SVM_Poly"     ])   histSVMP   ->Fill( reader->EvaluateMVA( "SVM_Poly method"      ) , weight);
      if (Use["SVM_Lin"      ])   histSVML   ->Fill( reader->EvaluateMVA( "SVM_Lin method"       ) , weight);
      if (Use["FDA_MT"       ])   histFDAMT  ->Fill( reader->EvaluateMVA( "FDA_MT method"        ) , weight);
      if (Use["FDA_GA"       ])   histFDAGA  ->Fill( reader->EvaluateMVA( "FDA_GA method"        ) , weight);
      if (Use["Category"     ])   histCat    ->Fill( reader->EvaluateMVA( "Category method"      ) , weight);
      if (Use["Plugin"       ])   histPBdt   ->Fill( reader->EvaluateMVA( "P_BDT method"         ) , weight);

      // Retrieve also per-event error
      if (Use["PDEFoam"]) {
        Double_t val = reader->EvaluateMVA( "PDEFoam method" );
        Double_t err = reader->GetMVAError();
        histPDEFoam   ->Fill( val );
        histPDEFoamErr->Fill( err );         
        if (err>1.e-50) histPDEFoamSig->Fill( val/err , weight);
      }         

      // Retrieve probability instead of MVA output
      if (Use["Fisher"])   {
        probHistFi  ->Fill( reader->GetProba ( "Fisher method" ) , weight);
        rarityHistFi->Fill( reader->GetRarity( "Fisher method" ) , weight);
      }
    }

    std::cout << npass << " events passing selection, yield " << yield << std::endl;
 
    // Get elapsed time
    sw.Stop();
    std::cout << "--- End of event loop: "; sw.Print();

    // Get efficiency for cuts classifier
    if (Use["CutsGA"]) std::cout << "--- Efficiency for CutsGA method: " << double(nSelCutsGA)/theTree->GetEntries()
                                 << " (for a required signal efficiency of " << effS << ")" << std::endl;

    if (Use["CutsGA"]) {

      // test: retrieve cuts for particular signal efficiency
      // CINT ignores dynamic_casts so we have to use a cuts-secific Reader function to acces the pointer  
      TMVA::MethodCuts* mcuts = reader->FindCutsMVA( "CutsGA method" ) ;

      if (mcuts) {      
        std::vector<Double_t> cutsMin;
        std::vector<Double_t> cutsMax;
        mcuts->GetCuts( 0.7, cutsMin, cutsMax );
        std::cout << "--- -------------------------------------------------------------" << std::endl;
        std::cout << "--- Retrieve cut values for signal efficiency of 0.7 from Reader" << std::endl;
        for (UInt_t ivar=0; ivar<cutsMin.size(); ivar++) {
          std::cout << "... Cut: " 
                    << cutsMin[ivar] 
                    << " < \"" 
                    << mcuts->GetInputVar(ivar)
                    << "\" <= " 
                    << cutsMax[ivar] << std::endl;
        }
        std::cout << "--- -------------------------------------------------------------" << std::endl;
      }
    }

    // --- Write histograms
    cout << "dir " << dir << endl;
    char* mydir = outdir;
    TFile *target  = new TFile( Form("%s/%s.root",mydir,samples.at(i) ) ,"RECREATE" );
    cout << "Writing to file " << Form("%s/%s.root",mydir,samples.at(i) ) << endl;

    if (Use["Likelihood"   ])   histLk     ->Write();
    if (Use["LikelihoodD"  ])   histLkD    ->Write();
    if (Use["LikelihoodPCA"])   histLkPCA  ->Write();
    if (Use["LikelihoodKDE"])   histLkKDE  ->Write();
    if (Use["LikelihoodMIX"])   histLkMIX  ->Write();
    if (Use["PDERS"        ])   histPD     ->Write();
    if (Use["PDERSD"       ])   histPDD    ->Write();
    if (Use["PDERSPCA"     ])   histPDPCA  ->Write();
    if (Use["KNN"          ])   histKNN    ->Write();
    if (Use["HMatrix"      ])   histHm     ->Write();
    if (Use["Fisher"       ])   histFi     ->Write();
    if (Use["FisherG"      ])   histFiG    ->Write();
    if (Use["BoostedFisher"])   histFiB    ->Write();
    if (Use["LD"           ])   histLD     ->Write();
    if (Use["MLP"          ])   histNn     ->Write();
    if (Use["MLPBFGS"      ])   histNnbfgs ->Write();
    if (Use["MLPBNN"       ])   histNnbnn  ->Write();
    if (Use["CFMlpANN"     ])   histNnC    ->Write();
    if (Use["TMlpANN"      ])   histNnT    ->Write();
    if (Use["BDT"          ])   histBdt    ->Write();
    if (Use["BDTD"         ])   histBdtD   ->Write();
    if (Use["BDTG"         ])   histBdtG   ->Write(); 
    if (Use["RuleFit"      ])   histRf     ->Write();
    if (Use["SVM_Gauss"    ])   histSVMG   ->Write();
    if (Use["SVM_Poly"     ])   histSVMP   ->Write();
    if (Use["SVM_Lin"      ])   histSVML   ->Write();
    if (Use["FDA_MT"       ])   histFDAMT  ->Write();
    if (Use["FDA_GA"       ])   histFDAGA  ->Write();
    if (Use["Category"     ])   histCat    ->Write();
    if (Use["Plugin"       ])   histPBdt   ->Write();

    // Write also error and significance histos
    if (Use["PDEFoam"]) { histPDEFoam->Write(); histPDEFoamErr->Write(); histPDEFoamSig->Write(); }

    // Write also probability hists
    if (Use["Fisher"]) { if (probHistFi != 0) probHistFi->Write(); if (rarityHistFi != 0) rarityHistFi->Write(); }
    target->Close();

    delete reader;
    
    std::cout << "==> TMVAClassificationApplication is done with sample " << samples.at(i) << endl << std::endl;
  } 
}
コード例 #24
0
void PlotDecisionBoundary( TString weightFile = "weights/TMVAClassification_BDT.weights.xml",TString v0="var0", TString v1="var1", TString dataFileName = "/home/hvoss/TMVA/TMVA_data/data/data_circ.root") 
{   
   //---------------------------------------------------------------
   // default MVA methods to be trained + tested

   // this loads the library
   TMVA::Tools::Instance();

   std::cout << std::endl;
   std::cout << "==> Start TMVAClassificationApplication" << std::endl;


   //
   // create the Reader object
   //
   TMVA::Reader *reader = new TMVA::Reader( "!Color:!Silent" );    

   // create a set of variables and declare them to the reader
   // - the variable names must corresponds in name and type to 
   // those given in the weight file(s) that you use
   Float_t var0, var1;
   reader->AddVariable( v0,                &var0 );
   reader->AddVariable( v1,                &var1 );

   //
   // book the MVA method
   //
   reader->BookMVA( "M1", weightFile ); 
   
   TFile *f = new TFile(dataFileName);
   TTree *signal     = (TTree*)f->Get("TreeS");
   TTree *background = (TTree*)f->Get("TreeB");


//Declaration of leaves types
   Float_t         svar0;
   Float_t         svar1;
   Float_t         bvar0;
   Float_t         bvar1;
   Float_t         sWeight=1.0; // just in case you have weight defined, also set these branchaddresses
   Float_t         bWeight=1.0*signal->GetEntries()/background->GetEntries(); // just in case you have weight defined, also set these branchaddresses

   // Set branch addresses.
   signal->SetBranchAddress(v0,&svar0);
   signal->SetBranchAddress(v1,&svar1);
   background->SetBranchAddress(v0,&bvar0);
   background->SetBranchAddress(v1,&bvar1);


   UInt_t nbin = 50;
   Float_t xmax = signal->GetMaximum(v0.Data());
   Float_t xmin = signal->GetMinimum(v0.Data());
   Float_t ymax = signal->GetMaximum(v1.Data());
   Float_t ymin = signal->GetMinimum(v1.Data());
 
   xmax = TMath::Max(xmax,background->GetMaximum(v0.Data()));  
   xmin = TMath::Min(xmin,background->GetMinimum(v0.Data()));
   ymax = TMath::Max(ymax,background->GetMaximum(v1.Data()));
   ymin = TMath::Min(ymin,background->GetMinimum(v1.Data()));


   TH2D *hs=new TH2D("hs","",nbin,xmin,xmax,nbin,ymin,ymax);   
   TH2D *hb=new TH2D("hb","",nbin,xmin,xmax,nbin,ymin,ymax);   
   hs->SetXTitle(v0);
   hs->SetYTitle(v1);
   hb->SetXTitle(v0);
   hb->SetYTitle(v1);
   hs->SetMarkerColor(4);
   hb->SetMarkerColor(2);


   TH2F * hist = new TH2F( "MVA",    "MVA",    nbin,xmin,xmax,nbin,ymin,ymax);

   // Prepare input tree (this must be replaced by your data source)
   // in this example, there is a toy tree with signal and one with background events
   // we'll later on use only the "signal" events for the test in this example.

   Float_t MinMVA=10000, MaxMVA=-100000;
   for (Int_t ibin=1; ibin<nbin+1; ibin++){
      for (Int_t jbin=1; jbin<nbin+1; jbin++){
         var0 = hs->GetXaxis()->GetBinCenter(ibin);
         var1 = hs->GetYaxis()->GetBinCenter(jbin);
         Float_t mvaVal=reader->EvaluateMVA( "M1" ) ;
         if (MinMVA>mvaVal) MinMVA=mvaVal;
         if (MaxMVA<mvaVal) MaxMVA=mvaVal;
         hist->SetBinContent(ibin,jbin, mvaVal);
      }
   }

   // creating a fine histograms containing the error rate
   const Int_t nValBins=100;
   Double_t    sum = 0.;

   TH1F *mvaS= new TH1F("mvaS","",nValBins,MinMVA,MaxMVA);
   TH1F *mvaB= new TH1F("mvaB","",nValBins,MinMVA,MaxMVA);
   TH1F *mvaSC= new TH1F("mvaSC","",nValBins,MinMVA,MaxMVA);
   TH1F *mvaBC= new TH1F("mvaBC","",nValBins,MinMVA,MaxMVA);

   Long64_t nentries;
   nentries = TreeS->GetEntries();
   for (Long64_t is=0; is<nentries;is++) {
      signal->GetEntry(is);
      sum +=sWeight;
      var0 = svar0;
      var1 = svar1;
      Float_t mvaVal=reader->EvaluateMVA( "M1" ) ;
      hs->Fill(svar0,svar1);
      mvaS->Fill(mvaVal,sWeight);
   }
   nentries = TreeB->GetEntries();
   for (Long64_t ib=0; ib<nentries;ib++) {
      background->GetEntry(ib);
      sum +=bWeight;
      var0 = bvar0;
      var1 = bvar1;
      Float_t mvaVal=reader->EvaluateMVA( "M1" ) ;
      hb->Fill(bvar0,bvar1);
      mvaB->Fill(mvaVal,bWeight);
   }

   //SeparationBase *sepGain = new MisClassificationError();
   //SeparationBase *sepGain = new GiniIndex();
   SeparationBase *sepGain = new CrossEntropy();

   Double_t sTot = mvaS->GetSum();
   Double_t bTot = mvaB->GetSum();

   mvaSC->SetBinContent(1,mvaS->GetBinContent(1));
   mvaBC->SetBinContent(1,mvaB->GetBinContent(1));
   Double_t sSel=mvaSC->GetBinContent(1);
   Double_t bSel=mvaBC->GetBinContent(1);
   Double_t separationGain=sepGain->GetSeparationGain(sSel,bSel,sTot,bTot);
   Double_t mvaCut=mvaSC->GetBinCenter(1);
   Double_t mvaCutOrientation=1; // 1 if mva > mvaCut --> Signal and -1 if mva < mvaCut (i.e. mva*-1 > mvaCut*-1) --> Signal
   for (Int_t ibin=2;ibin<nValBins;ibin++){ 
      mvaSC->SetBinContent(ibin,mvaS->GetBinContent(ibin)+mvaSC->GetBinContent(ibin-1));
      mvaBC->SetBinContent(ibin,mvaB->GetBinContent(ibin)+mvaBC->GetBinContent(ibin-1));
    
      sSel=mvaSC->GetBinContent(ibin);
      bSel=mvaBC->GetBinContent(ibin);

      if (separationGain < sepGain->GetSeparationGain(sSel,bSel,sTot,bTot) && mvaSC->GetBinCenter(ibin)<0){
         separationGain = sepGain->GetSeparationGain(sSel,bSel,sTot,bTot);
         mvaCut=mvaSC->GetBinCenter(ibin);
         if (sSel/bSel > (sTot-sSel)/(bTot-bSel)) mvaCutOrientation=-1;
         else                                     mvaCutOrientation=1;
     }
   }
   

   cout << "Min="<<MinMVA << " Max=" << MaxMVA 
        << " sTot=" << sTot
        << " bTot=" << bTot
        << " sepGain="<<separationGain
        << " cut=" << mvaCut
        << " cutOrientation="<<mvaCutOrientation
        << endl;


   delete reader;

   gStyle->SetPalette(1);

  
   plot(hs,hb,hist     ,v0,v1,mvaCut);


   TCanvas *cm=new TCanvas ("cm","",900,1200);
   cm->cd();
   cm->Divide(1,2);
   cm->cd(1);
   mvaS->SetLineColor(4);
   mvaB->SetLineColor(2);
   mvaS->Draw();
   mvaB->Draw("same");

   cm->cd(2);
   mvaSC->SetLineColor(4);
   mvaBC->SetLineColor(2);
   mvaBC->Draw();
   mvaSC->Draw("same");

   // TH1F *add=(TH1F*)mvaBC->Clone("add");
   // add->Add(mvaSC);

   // add->Draw();

   // errh->Draw("same");

   //
   // write histograms
   //
   TFile *target  = new TFile( "TMVAPlotDecisionBoundary.root","RECREATE" );

   hs->Write();
   hb->Write();

   hist->Write();

   target->Close();

} 
コード例 #25
0
void TMVAClassificationApplication( TString myMethodList = "" ) 
{   
   //---------------------------------------------------------------
   // default MVA methods to be trained + tested

   // this loads the library
   TMVA::Tools::Instance();

   std::map<std::string,int> Use;

   Use["CutsGA"]          = 0; // other "Cuts" methods work identically
   // ---
   Use["Likelihood"]      = 1;
   Use["LikelihoodD"]     = 0; // the "D" extension indicates decorrelated input variables (see option strings)
   Use["LikelihoodPCA"]   = 0; // the "PCA" extension indicates PCA-transformed input variables (see option strings)
   Use["LikelihoodKDE"]   = 0;
   Use["LikelihoodMIX"]   = 0;
   // ---
   Use["PDERS"]           = 0;
   Use["PDERSD"]          = 0;
   Use["PDERSPCA"]        = 0;
   Use["PDERSkNN"]        = 0; // depreciated until further notice
   Use["PDEFoam"]         = 0;
   // --
   Use["KNN"]             = 0;
   // ---
   Use["HMatrix"]         = 0;
   Use["Fisher"]          = 0;
   Use["FisherG"]         = 0;
   Use["BoostedFisher"]   = 0;
   Use["LD"]              = 0;
   // ---
   Use["FDA_GA"]          = 0;
   Use["FDA_SA"]          = 0;
   Use["FDA_MC"]          = 0;
   Use["FDA_MT"]          = 0;
   Use["FDA_GAMT"]        = 0;
   Use["FDA_MCMT"]        = 0;
   // ---
   Use["MLP"]             = 0; // this is the recommended ANN
   Use["MLPBFGS"]         = 0; // recommended ANN with optional training method
   Use["MLPBNN"]          = 0;  // 
   Use["CFMlpANN"]        = 0; // *** missing
   Use["TMlpANN"]         = 0; 
   // ---
   Use["SVM"]             = 0;
   // ---
   Use["BDT"]             = 1;
   Use["BDTD"]            = 0;
   Use["BDTG"]            = 0;
   Use["BDTB"]            = 0;
   // ---
   Use["RuleFit"]         = 0;
   // ---
   Use["Category"]        = 0;
   // ---
   Use["Plugin"]          = 0;
   // ---------------------------------------------------------------

   std::cout << std::endl;
   std::cout << "==> Start TMVAClassificationApplication" << std::endl;

   if (myMethodList != "") {
      for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) it->second = 0;

      std::vector<TString> mlist = gTools().SplitString( myMethodList, ',' );
      for (UInt_t i=0; i<mlist.size(); i++) {
         std::string regMethod(mlist[i]);

         if (Use.find(regMethod) == Use.end()) {
            std::cout << "Method \"" << regMethod << "\" not known in TMVA under this name. Choose among the following:" << std::endl;
            for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) std::cout << it->first << " ";
            std::cout << std::endl;
            return;
         }
         Use[regMethod] = 0;
      }
   }

   //
   // create the Reader object
   //
   TMVA::Reader *reader = new TMVA::Reader( "!Color:!Silent" );    

Float_t Z_rapidity_z;
reader->AddVariable("Z_rapidity_z",&Z_rapidity_z);

Float_t THRUST_2D;
reader->AddVariable("THRUST_2D",&THRUST_2D);

Float_t L1_L2_cosangle;
reader->AddVariable("L1_L2_cosangle",&L1_L2_cosangle);

Float_t TransMass_ZH150_uncl;
reader->AddVariable("TransMass_ZH150_uncl",&TransMass_ZH150_uncl);

Float_t TransMass_ZH150;
reader->AddVariable("TransMass_ZH150",&TransMass_ZH150);

Float_t DeltaPhi_ZH;
reader->AddVariable("DeltaPhi_ZH",&DeltaPhi_ZH);

Float_t DeltaPhi_ZH_uncl;
reader->AddVariable("DeltaPhi_ZH_uncl",&DeltaPhi_ZH_uncl);

Float_t CMAngle;
reader->AddVariable("CMAngle",&CMAngle);

Float_t CS_cosangle;
reader->AddVariable("CS_cosangle",&CS_cosangle);

   // create a set of variables and declare them to the reader
   // - the variable names must corresponds in name and type to 
   // those given in the weight file(s) that you use
   Float_t var1, var2;
   Float_t var3, var4;
//   reader->AddVariable( "myvar1 := var1+var2", &var1 );
//   reader->AddVariable( "myvar2 := var1-var2", &var2 );
//   reader->AddVariable( "var3",                &var3 );
//   reader->AddVariable( "var4",                &var4 );

   //Spectator variables declared in the training have to be added to the reader, too
   Float_t spec1,spec2;
//   reader->AddSpectator( "spec1 := var1*2",   &spec1 );

 float nonsense =0;
//   reader->AddSpectator( "spec2 := var1*3",   &spec2 );

 float nonsense =0;

   Float_t Category_cat1, Category_cat2, Category_cat3;
   if (Use["Category"]){
      // add artificial spectators for distinguishing categories
//      reader->AddSpectator( "Category_cat1 := var3<=0",             &Category_cat1 );

 float nonsense =0;
//      reader->AddSpectator( "Category_cat2 := (var3>0)&&(var4<0)",  &Category_cat2 );

 float nonsense =0;
//      reader->AddSpectator( "Category_cat3 := (var3>0)&&(var4>=0)", &Category_cat3 );

 float nonsense =0;
   }
   //
   // book the MVA methods
   //
   TString dir    = "weights/";
   TString prefix = "TMVAClassification";

   // book method(s)
   for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) {
      if (it->second) {
         TString methodName = it->first + " method";
         TString weightfile = dir + prefix + "_" + TString(it->first) + ".weights.xml";
         reader->BookMVA( methodName, weightfile ); 
      }
   }
   
   // example how to use your own method as plugin
   if (Use["Plugin"]) {
      // the weight file contains a line 
      // Method         : MethodName::InstanceName

      // if MethodName is not a known TMVA method, it is assumed to be
      // a user implemented method which has to be loaded via the
      // plugin mechanism
      
      // for user implemented methods the line in the weight file can be
      // Method         : PluginName::InstanceName
      // where PluginName can be anything

      // before usage the plugin has to be defined, which can happen
      // either through the following line in .rootrc:
      // # plugin handler          plugin       class            library        constructor format
      // Plugin.TMVA@@MethodBase:  PluginName   MethodClassName  UserPackage    "MethodName(DataSet&,TString)"
      //  
      // or by telling the global plugin manager directly
      gPluginMgr->AddHandler("TMVA@@MethodBase", "PluginName", "MethodClassName", "UserPackage", "MethodName(DataSet&,TString)");
      // the class is then looked for in libUserPackage.so

      // now the method can be booked like any other
      reader->BookMVA( "User method", dir + prefix + "_User.weights.txt" );
   }

   // book output histograms
   UInt_t nbin = 100;
   TH1F *histLk(0), *histLkD(0), *histLkPCA(0), *histLkKDE(0), *histLkMIX(0), *histPD(0), *histPDD(0);
   TH1F *histPDPCA(0), *histPDEFoam(0), *histPDEFoamErr(0), *histPDEFoamSig(0), *histKNN(0), *histHm(0);
   TH1F *histFi(0), *histFiG(0), *histFiB(0), *histLD(0), *histNn(0),*histNnbfgs(0),*histNnbnn(0), *histNnC(0), *histNnT(0), *histBdt(0), *histBdtG(0), *histBdtD(0);
   TH1F *histRf(0), *histSVMG(0), *histSVMP(0), *histSVML(0), *histFDAMT(0), *histFDAGA(0), *histCat(0), *histPBdt(0);

   if (Use["Likelihood"])    histLk      = new TH1F( "MVA_Likelihood",    "MVA_Likelihood",    nbin, -1, 1 );
   if (Use["LikelihoodD"])   histLkD     = new TH1F( "MVA_LikelihoodD",   "MVA_LikelihoodD",   nbin, -1, 0.9999 );
   if (Use["LikelihoodPCA"]) histLkPCA   = new TH1F( "MVA_LikelihoodPCA", "MVA_LikelihoodPCA", nbin, -1, 1 );
   if (Use["LikelihoodKDE"]) histLkKDE   = new TH1F( "MVA_LikelihoodKDE", "MVA_LikelihoodKDE", nbin,  -0.00001, 0.99999 );
   if (Use["LikelihoodMIX"]) histLkMIX   = new TH1F( "MVA_LikelihoodMIX", "MVA_LikelihoodMIX", nbin,  0, 1 );
   if (Use["PDERS"])         histPD      = new TH1F( "MVA_PDERS",         "MVA_PDERS",         nbin,  0, 1 );
   if (Use["PDERSD"])        histPDD     = new TH1F( "MVA_PDERSD",        "MVA_PDERSD",        nbin,  0, 1 );
   if (Use["PDERSPCA"])      histPDPCA   = new TH1F( "MVA_PDERSPCA",      "MVA_PDERSPCA",      nbin,  0, 1 );
   if (Use["KNN"])           histKNN     = new TH1F( "MVA_KNN",           "MVA_KNN",           nbin,  0, 1 );
   if (Use["HMatrix"])       histHm      = new TH1F( "MVA_HMatrix",       "MVA_HMatrix",       nbin, -0.95, 1.55 );
   if (Use["Fisher"])        histFi      = new TH1F( "MVA_Fisher",        "MVA_Fisher",        nbin, -4, 4 );
   if (Use["FisherG"])       histFiG     = new TH1F( "MVA_FisherG",       "MVA_FisherG",       nbin, -1, 1 );
   if (Use["BoostedFisher"]) histFiB     = new TH1F( "MVA_BoostedFisher", "MVA_BoostedFisher", nbin, -2, 2 );
   if (Use["LD"])            histLD      = new TH1F( "MVA_LD",            "MVA_LD",            nbin, -2, 2 );
   if (Use["MLP"])           histNn      = new TH1F( "MVA_MLP",           "MVA_MLP",           nbin, -1.25, 1.5 );
   if (Use["MLPBFGS"])       histNnbfgs  = new TH1F( "MVA_MLPBFGS",           "MVA_MLPBFGS",           nbin, -1.25, 1.5 );
   if (Use["MLPBNN"])        histNnbnn   = new TH1F( "MVA_MLPBNN",           "MVA_MLPBNN",           nbin, -1.25, 1.5 );
   if (Use["CFMlpANN"])      histNnC     = new TH1F( "MVA_CFMlpANN",      "MVA_CFMlpANN",      nbin,  0, 1 );
   if (Use["TMlpANN"])       histNnT     = new TH1F( "MVA_TMlpANN",       "MVA_TMlpANN",       nbin, -1.3, 1.3 );
   if (Use["BDT"])           histBdt     = new TH1F( "MVA_BDT",           "MVA_BDT",           nbin, -0.8, 0.8 );
   if (Use["BDTD"])          histBdtD    = new TH1F( "MVA_BDTD",          "MVA_BDTD",          nbin, -0.8, 0.8 );
   if (Use["BDTG"])          histBdtG    = new TH1F( "MVA_BDTG",          "MVA_BDTG",          nbin, -1.0, 1.0 );
   if (Use["RuleFit"])       histRf      = new TH1F( "MVA_RuleFit",       "MVA_RuleFit",       nbin, -2.0, 2.0 );
   if (Use["SVM_Gauss"])     histSVMG    = new TH1F( "MVA_SVM_Gauss",     "MVA_SVM_Gauss",     nbin,  0.0, 1.0 );
   if (Use["SVM_Poly"])      histSVMP    = new TH1F( "MVA_SVM_Poly",      "MVA_SVM_Poly",      nbin,  0.0, 1.0 );
   if (Use["SVM_Lin"])       histSVML    = new TH1F( "MVA_SVM_Lin",       "MVA_SVM_Lin",       nbin,  0.0, 1.0 );
   if (Use["FDA_MT"])        histFDAMT   = new TH1F( "MVA_FDA_MT",        "MVA_FDA_MT",        nbin, -2.0, 3.0 );
   if (Use["FDA_GA"])        histFDAGA   = new TH1F( "MVA_FDA_GA",        "MVA_FDA_GA",        nbin, -2.0, 3.0 );
   if (Use["Category"])      histCat     = new TH1F( "MVA_Category",      "MVA_Category",      nbin, -2., 2. );
   if (Use["Plugin"])        histPBdt    = new TH1F( "MVA_PBDT",          "MVA_BDT",           nbin, -0.8, 0.8 );

   // PDEFoam also returns per-event error, fill in histogram, and also fill significance
   if (Use["PDEFoam"]) {
      histPDEFoam    = new TH1F( "MVA_PDEFoam",       "MVA_PDEFoam",              nbin,  0, 1 );
      histPDEFoamErr = new TH1F( "MVA_PDEFoamErr",    "MVA_PDEFoam error",        nbin,  0, 1 );
      histPDEFoamSig = new TH1F( "MVA_PDEFoamSig",    "MVA_PDEFoam significance", nbin,  0, 10 );
   }

   // book example histogram for probability (the other methods are done similarly)
   TH1F *probHistFi(0), *rarityHistFi(0);
   if (Use["Fisher"]) {
      probHistFi   = new TH1F( "MVA_Fisher_Proba",  "MVA_Fisher_Proba",  nbin, 0, 1 );
      rarityHistFi = new TH1F( "MVA_Fisher_Rarity", "MVA_Fisher_Rarity", nbin, 0, 1 );
   }

   // Prepare input tree (this must be replaced by your data source)
   // in this example, there is a toy tree with signal and one with background events
   // we'll later on use only the "signal" events for the test in this example.
   //   
   TFile *input(0);
   TString fname = "/tmp/chasco/ORIGINAL//Data_MuEG2011B_1.root";   
   if (!gSystem->AccessPathName( fname )) {
      input = TFile::Open( fname ); // check if file in local directory exists
   } 
   else { 
      input = TFile::Open( "http://root.cern.ch/files/tmva_class_example.root" ); // if not: download from ROOT server
   }
   
   if (!input) {
      std::cout << "ERROR: could not open data file" << std::endl;
      exit(1);
   }
   std::cout << "--- TMVAClassificationApp    : Using input file: " << input->GetName() << std::endl;
   
   //
   // prepare the tree
   // - here the variable names have to corresponds to your tree
   // - you can use the same variables as above which is slightly faster,
   //   but of course you can use different ones and copy the values inside the event loop
   //
   TTree* BigTree = (TTree*)input->Get("data");

TFile *tmp  = new TFile( "tmp.root","RECREATE" );

TTree* theTree = BigTree->CopyTree("((cat == 1) + (cat == 2))*(ln==0)*(Cosmic==0)*(fabs(Mass_Z - 91.18)<10)*(Pt_Z>30)*(DeltaPhi_metjet>0.5)*(Pt_J1 < 30)*(pfMEToverPt_Z > 0.4)*(pfMEToverPt_Z < 1.8)*((Pt_Jet_btag_CSV_max > 20)*(btag_CSV_max < 0.244) + (1-(Pt_Jet_btag_CSV_max > 20)))*(sqrt(pow(dilepPROJLong + 1.25*recoilPROJLong + 0.0*uncertPROJLong,2)*(dilepPROJLong + 1.25*recoilPROJLong + 0.0*uncertPROJLong > 0) + 1.0*pow(dilepPROJPerp + 1.25*recoilPROJPerp + 0.0*uncertPROJPerp,2)*(dilepPROJPerp + 1.25*recoilPROJPerp + 0.0*uncertPROJPerp > 0)) > 45.0)");
   std::cout << "--- Select signal sample" << std::endl;
   Float_t userVar1, userVar2;
//   theTree->SetBranchAddress( "var1", &userVar1 );
//   theTree->SetBranchAddress( "var2", &userVar2 );
//   theTree->SetBranchAddress( "var3", &var3 );
//   theTree->SetBranchAddress( "var4", &var4 );

theTree->SetBranchAddress( " Z_rapidity_z", &Z_rapidity_z);

theTree->SetBranchAddress( " THRUST_2D", &THRUST_2D);

theTree->SetBranchAddress( " L1_L2_cosangle", &L1_L2_cosangle);

theTree->SetBranchAddress( " TransMass_ZH150_uncl", &TransMass_ZH150_uncl);

theTree->SetBranchAddress( " TransMass_ZH150", &TransMass_ZH150);

theTree->SetBranchAddress( " DeltaPhi_ZH", &DeltaPhi_ZH);

theTree->SetBranchAddress( " DeltaPhi_ZH_uncl", &DeltaPhi_ZH_uncl);

theTree->SetBranchAddress( " CMAngle", &CMAngle);

theTree->SetBranchAddress( " CS_cosangle", &CS_cosangle);

   // efficiency calculator for cut method
   Int_t    nSelCutsGA = 0;
   Double_t effS       = 0.7;

   std::vector<Float_t> vecVar(9); // vector for EvaluateMVA tests

   std::cout << "--- Processing: " << theTree->GetEntries() << " events" << std::endl;
   TStopwatch sw;
   sw.Start();
   for (Long64_t ievt=0; ievt<theTree->GetEntries();ievt++) {

      if (ievt%1000 == 0){
         std::cout << "--- ... Processing event: " << ievt << std::endl;
      }

      theTree->GetEntry(ievt);

      var1 = userVar1 + userVar2;
      var2 = userVar1 - userVar2;

      if (ievt <20){
         // test the twodifferent Reader::EvaluateMVA functions 
         // access via registered variables compared to access via vector<float>
//         vecVar[0]=var1;
//         vecVar[1]=var2;
//         vecVar[2]=var3;
//         vecVar[3]=var4;      

vecVar[0]=Z_rapidity_z;

vecVar[1]=THRUST_2D;

vecVar[2]=L1_L2_cosangle;

vecVar[3]=TransMass_ZH150_uncl;

vecVar[4]=TransMass_ZH150;

vecVar[5]=DeltaPhi_ZH;

vecVar[6]=DeltaPhi_ZH_uncl;

vecVar[7]=CMAngle;

vecVar[8]=CS_cosangle;
         for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) {
            if (it->second) {
               TString mName = it->first + " method";
               Double_t mva1 = reader->EvaluateMVA( mName); 
               Double_t mva2 = reader->EvaluateMVA( vecVar, mName); 
               if (mva1 != mva2) {
                  std::cout << "++++++++++++++ ERROR in "<< mName <<", comparing different EvaluateMVA results val1=" << mva1 << " val2="<<mva2<<std::endl;
               }
            }
         }
         // now test that the inputs do matter
         TRandom3 rand(0);
//         vecVar[0]=rand.Rndm();
//         vecVar[1]=rand.Rndm();
//         vecVar[2]=rand.Rndm();
//         vecVar[3]=rand.Rndm();

vecVar[0]=rand.Rndm();

vecVar[1]=rand.Rndm();

vecVar[2]=rand.Rndm();

vecVar[3]=rand.Rndm();

vecVar[4]=rand.Rndm();

vecVar[5]=rand.Rndm();

vecVar[6]=rand.Rndm();

vecVar[7]=rand.Rndm();

vecVar[8]=rand.Rndm();
         for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) {
            if (it->second) {
               TString mName = it->first + " method";
               Double_t mva1 = reader->EvaluateMVA( mName); 
               Double_t mva2 = reader->EvaluateMVA( vecVar, mName); 
               if (mva1 == mva2) {
                  std::cout << "++++++++++++++ ERROR in "<< mName <<", obtaining idnetical output for different inputs" <<std::endl;
               }
            }
         }
      }
      // 
      // return the MVAs and fill to histograms
      // 
      if (Use["CutsGA"]) {
         // Cuts is a special case: give the desired signal efficienciy
         Bool_t passed = reader->EvaluateMVA( "CutsGA method", effS );
         if (passed) nSelCutsGA++;
      }

      if (Use["Likelihood"   ])   histLk     ->Fill( reader->EvaluateMVA( "Likelihood method"    ) );
      if (Use["LikelihoodD"  ])   histLkD    ->Fill( reader->EvaluateMVA( "LikelihoodD method"   ) );
      if (Use["LikelihoodPCA"])   histLkPCA  ->Fill( reader->EvaluateMVA( "LikelihoodPCA method" ) );
      if (Use["LikelihoodKDE"])   histLkKDE  ->Fill( reader->EvaluateMVA( "LikelihoodKDE method" ) );
      if (Use["LikelihoodMIX"])   histLkMIX  ->Fill( reader->EvaluateMVA( "LikelihoodMIX method" ) );
      if (Use["PDERS"        ])   histPD     ->Fill( reader->EvaluateMVA( "PDERS method"         ) );
      if (Use["PDERSD"       ])   histPDD    ->Fill( reader->EvaluateMVA( "PDERSD method"        ) );
      if (Use["PDERSPCA"     ])   histPDPCA  ->Fill( reader->EvaluateMVA( "PDERSPCA method"      ) );
      if (Use["KNN"          ])   histKNN    ->Fill( reader->EvaluateMVA( "KNN method"           ) );
      if (Use["HMatrix"      ])   histHm     ->Fill( reader->EvaluateMVA( "HMatrix method"       ) );
      if (Use["Fisher"       ])   histFi     ->Fill( reader->EvaluateMVA( "Fisher method"        ) );
      if (Use["FisherG"      ])   histFiG    ->Fill( reader->EvaluateMVA( "FisherG method"       ) );
      if (Use["BoostedFisher"])   histFiB    ->Fill( reader->EvaluateMVA( "BoostedFisher method" ) );
      if (Use["LD"           ])   histLD     ->Fill( reader->EvaluateMVA( "LD method"            ) );
      if (Use["MLP"          ])   histNn     ->Fill( reader->EvaluateMVA( "MLP method"           ) );
      if (Use["MLPBFGS"          ])   histNnbfgs ->Fill( reader->EvaluateMVA( "MLPBFGS method"           ) );
      if (Use["MLPBNN"          ])   histNnbnn ->Fill( reader->EvaluateMVA( "MLPBNN method"           ) );
      if (Use["CFMlpANN"     ])   histNnC    ->Fill( reader->EvaluateMVA( "CFMlpANN method"      ) );
      if (Use["TMlpANN"      ])   histNnT    ->Fill( reader->EvaluateMVA( "TMlpANN method"       ) );
      if (Use["BDT"          ])   histBdt    ->Fill( reader->EvaluateMVA( "BDT method"           ) );
      if (Use["BDTD"         ])   histBdtD   ->Fill( reader->EvaluateMVA( "BDTD method"          ) );
      if (Use["BDTG"         ])   histBdtG   ->Fill( reader->EvaluateMVA( "BDTG method"          ) );
      if (Use["RuleFit"      ])   histRf     ->Fill( reader->EvaluateMVA( "RuleFit method"       ) );
      if (Use["SVM_Gauss"    ])   histSVMG   ->Fill( reader->EvaluateMVA( "SVM_Gauss method"     ) );
      if (Use["SVM_Poly"     ])   histSVMP   ->Fill( reader->EvaluateMVA( "SVM_Poly method"      ) );
      if (Use["SVM_Lin"      ])   histSVML   ->Fill( reader->EvaluateMVA( "SVM_Lin method"       ) );
      if (Use["FDA_MT"       ])   histFDAMT  ->Fill( reader->EvaluateMVA( "FDA_MT method"        ) );
      if (Use["FDA_GA"       ])   histFDAGA  ->Fill( reader->EvaluateMVA( "FDA_GA method"        ) );
      if (Use["Category"     ])   histCat    ->Fill( reader->EvaluateMVA( "Category method"      ) );
      if (Use["Plugin"       ])   histPBdt   ->Fill( reader->EvaluateMVA( "P_BDT method"         ) );

      // retrieve also per-event error
      if (Use["PDEFoam"]) {
         Double_t val = reader->EvaluateMVA( "PDEFoam method" );
         Double_t err = reader->GetMVAError();
         histPDEFoam   ->Fill( val );
         histPDEFoamErr->Fill( err );         
         histPDEFoamSig->Fill( val/err );
      }         

      // retrieve probability instead of MVA output
      if (Use["Fisher"])   {
         probHistFi  ->Fill( reader->GetProba ( "Fisher method" ) );
         rarityHistFi->Fill( reader->GetRarity( "Fisher method" ) );
      }
   }
   // get elapsed time
   sw.Stop();
   std::cout << "--- End of event loop: "; sw.Print();

   // get efficiency for cuts classifier
   if (Use["CutsGA"]) std::cout << "--- Efficiency for CutsGA method: " << double(nSelCutsGA)/theTree->GetEntries()
                                << " (for a required signal efficiency of " << effS << ")" << std::endl;

   if (Use["CutsGA"]) {

      // test: retrieve cuts for particular signal efficiency
      // CINT ignores dynamic_casts so we have to use a cuts-secific Reader function to acces the pointer  
      TMVA::MethodCuts* mcuts = reader->FindCutsMVA( "CutsGA method" ) ;

      if (mcuts) {      
         std::vector<Double_t> cutsMin;
         std::vector<Double_t> cutsMax;
         mcuts->GetCuts( 0.7, cutsMin, cutsMax );
         std::cout << "--- -------------------------------------------------------------" << std::endl;
         std::cout << "--- Retrieve cut values for signal efficiency of 0.7 from Reader" << std::endl;
         for (UInt_t ivar=0; ivar<cutsMin.size(); ivar++) {
            std::cout << "... Cut: " 
                      << cutsMin[ivar] 
                      << " < \"" 
                      << mcuts->GetInputVar(ivar)
                      << "\" <= " 
                      << cutsMax[ivar] << std::endl;
         }
         std::cout << "--- -------------------------------------------------------------" << std::endl;
      }
   }

   //
   // write histograms
   //
   TFile *target  = new TFile( "TMVApp.root","RECREATE" );
   if (Use["Likelihood"   ])   histLk     ->Write();
   if (Use["LikelihoodD"  ])   histLkD    ->Write();
   if (Use["LikelihoodPCA"])   histLkPCA  ->Write();
   if (Use["LikelihoodKDE"])   histLkKDE  ->Write();
   if (Use["LikelihoodMIX"])   histLkMIX  ->Write();
   if (Use["PDERS"        ])   histPD     ->Write();
   if (Use["PDERSD"       ])   histPDD    ->Write();
   if (Use["PDERSPCA"     ])   histPDPCA  ->Write();
   if (Use["KNN"          ])   histKNN    ->Write();
   if (Use["HMatrix"      ])   histHm     ->Write();
   if (Use["Fisher"       ])   histFi     ->Write();
   if (Use["FisherG"      ])   histFiG    ->Write();
   if (Use["BoostedFisher"])   histFiB    ->Write();
   if (Use["LD"           ])   histLD     ->Write();
   if (Use["MLP"          ])   histNn     ->Write();
   if (Use["MLPBFGS"      ])   histNnbfgs ->Write();
   if (Use["MLPBNN"       ])   histNnbnn  ->Write();
   if (Use["CFMlpANN"     ])   histNnC    ->Write();
   if (Use["TMlpANN"      ])   histNnT    ->Write();
   if (Use["BDT"          ])   histBdt    ->Write();
   if (Use["BDTD"         ])   histBdtD   ->Write();
   if (Use["BDTG"         ])   histBdtG   ->Write(); 
   if (Use["RuleFit"      ])   histRf     ->Write();
   if (Use["SVM_Gauss"    ])   histSVMG   ->Write();
   if (Use["SVM_Poly"     ])   histSVMP   ->Write();
   if (Use["SVM_Lin"      ])   histSVML   ->Write();
   if (Use["FDA_MT"       ])   histFDAMT  ->Write();
   if (Use["FDA_GA"       ])   histFDAGA  ->Write();
   if (Use["Category"     ])   histCat    ->Write();
   if (Use["Plugin"       ])   histPBdt   ->Write();

   // write also error and significance histos
   if (Use["PDEFoam"]) { histPDEFoam->Write(); histPDEFoamErr->Write(); histPDEFoamSig->Write(); }

   // write also probability hists
   if (Use["Fisher"]) { if (probHistFi != 0) probHistFi->Write(); if (rarityHistFi != 0) rarityHistFi->Write(); }
   target->Close();

   std::cout << "--- Created root file: \"TMVApp.root\" containing the MVA output histograms" << std::endl;
  
   delete reader;
    
   std::cout << "==> TMVAClassificationApplication is done!" << endl << std::endl;
} 
コード例 #26
0
void TMVAMulticlassApplication( TString myMethodList = "" )
{

   TMVA::Tools::Instance();
   
   //---------------------------------------------------------------
   // default MVA methods to be trained + tested
   std::map<std::string,int> Use;
   Use["MLP"]             = 1;
   Use["BDTG"]            = 1;
   Use["FDA_GA"]          = 0;
   Use["PDEFoam"]         = 0;
   //---------------------------------------------------------------
  
   std::cout << std::endl;
   std::cout << "==> Start TMVAMulticlassApplication" << std::endl; 
   if (myMethodList != "") {
      for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) it->second = 0;

      std::vector<TString> mlist = gTools().SplitString( myMethodList, ',' );
      for (UInt_t i=0; i<mlist.size(); i++) {
         std::string regMethod(mlist[i]);

         if (Use.find(regMethod) == Use.end()) {
            std::cout << "Method \"" << regMethod << "\" not known in TMVA under this name. Choose among the following:" << std::endl;
            for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) std::cout << it->first << " " << std::endl;
            std::cout << std::endl;
            return;
         }
         Use[regMethod] = 1;
      }
   }

   
   // create the Reader object
   TMVA::Reader *reader = new TMVA::Reader( "!Color:!Silent" );    

   // create a set of variables and declare them to the reader
   // - the variable names must corresponds in name and type to 
   // those given in the weight file(s) that you use
   Float_t var1, var2, var3, var4;
   reader->AddVariable( "var1", &var1 );
   reader->AddVariable( "var2", &var2 );
   reader->AddVariable( "var3", &var3 );
   reader->AddVariable( "var4", &var4 );

   // book the MVA methods
   TString dir    = "dataset/weights/";
   TString prefix = "TMVAMulticlass";
   
   for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) {
      if (it->second) {
        TString methodName = TString(it->first) + TString(" method");
        TString weightfile = dir + prefix + TString("_") + TString(it->first) + TString(".weights.xml"); 
        reader->BookMVA( methodName, weightfile ); 
      }
   }

   // book output histograms
   UInt_t nbin = 100;
   TH1F *histMLP_signal(0), *histBDTG_signal(0), *histFDAGA_signal(0), *histPDEFoam_signal(0);
   if (Use["MLP"])    
      histMLP_signal    = new TH1F( "MVA_MLP_signal",    "MVA_MLP_signal",    nbin, 0., 1.1 );
   if (Use["BDTG"])
      histBDTG_signal  = new TH1F( "MVA_BDTG_signal",   "MVA_BDTG_signal",   nbin, 0., 1.1 );
   if (Use["FDA_GA"])
      histFDAGA_signal = new TH1F( "MVA_FDA_GA_signal", "MVA_FDA_GA_signal", nbin, 0., 1.1 );
   if (Use["PDEFoam"])
      histPDEFoam_signal = new TH1F( "MVA_PDEFoam_signal", "MVA_PDEFoam_signal", nbin, 0., 1.1 );


   TFile *input(0); 
   TString fname = "./tmva_example_multiple_background.root";
   if (!gSystem->AccessPathName( fname )) {
      input = TFile::Open( fname ); // check if file in local directory exists
   }
   if (!input) {
      std::cout << "ERROR: could not open data file, please generate example data first!" << std::endl;
      exit(1);
   }
   std::cout << "--- TMVAMulticlassApp : Using input file: " << input->GetName() << std::endl;
   
   // prepare the tree
   // - here the variable names have to corresponds to your tree
   // - you can use the same variables as above which is slightly faster,
   //   but of course you can use different ones and copy the values inside the event loop
  
   TTree* theTree = (TTree*)input->Get("TreeS");
   std::cout << "--- Select signal sample" << std::endl;
   theTree->SetBranchAddress( "var1", &var1 );
   theTree->SetBranchAddress( "var2", &var2 );
   theTree->SetBranchAddress( "var3", &var3 );
   theTree->SetBranchAddress( "var4", &var4 );

   std::cout << "--- Processing: " << theTree->GetEntries() << " events" << std::endl;
   TStopwatch sw;
   sw.Start();

   for (Long64_t ievt=0; ievt<theTree->GetEntries();ievt++) {
      if (ievt%1000 == 0){
         std::cout << "--- ... Processing event: " << ievt << std::endl;
      }
      theTree->GetEntry(ievt);
      
      if (Use["MLP"])
         histMLP_signal->Fill((reader->EvaluateMulticlass( "MLP method" ))[0]);
      if (Use["BDTG"])
         histBDTG_signal->Fill((reader->EvaluateMulticlass( "BDTG method" ))[0]);
      if (Use["FDA_GA"])
         histFDAGA_signal->Fill((reader->EvaluateMulticlass( "FDA_GA method" ))[0]);
      if (Use["PDEFoam"])
         histPDEFoam_signal->Fill((reader->EvaluateMulticlass( "PDEFoam method" ))[0]);
      
   }
   
   // get elapsed time
   sw.Stop();
   std::cout << "--- End of event loop: "; sw.Print();
   
   TFile *target  = new TFile( "TMVAMulticlassApp.root","RECREATE" );
   if (Use["MLP"])
      histMLP_signal->Write();
   if (Use["BDTG"])
      histBDTG_signal->Write(); 
   if (Use["FDA_GA"])
      histFDAGA_signal->Write();
   if (Use["PDEFoam"])
      histPDEFoam_signal->Write();

   target->Close();
   std::cout << "--- Created root file: \"TMVMulticlassApp.root\" containing the MVA output histograms" << std::endl;

   delete reader;
   
   std::cout << "==> TMVAClassificationApplication is done!" << std::endl << std::endl;
}
コード例 #27
0
void TMVAClassificationApplication_TX(TString myMethodList = "" , TString iFileName = "", TString sampleLocation = "", TString outputLocation = "") 
{   
#ifdef __CINT__
   gROOT->ProcessLine( ".O0" ); // turn off optimization in CINT
#endif

   //---------------------------------------------------------------

   // This loads the library
   TMVA::Tools::Instance();

   // Default MVA methods to be trained + tested
   std::map<std::string,int> Use;

   // --- Cut optimisation
   Use["Cuts"]            = 0;
   Use["CutsD"]           = 0;
   Use["CutsPCA"]         = 0;
   Use["CutsGA"]          = 0;
   Use["CutsSA"]          = 0;
   // 
   // 
   // --- Boosted Decision Trees
   Use["BDT"]             = 1; // uses Adaptive Boost
   std::cout << std::endl;
   std::cout << "==> Start TMVAClassificationApplication" << std::endl;

   // Select methods (don't look at this code - not of interest)
   if (myMethodList != "") {
      for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) it->second = 0;

      std::vector<TString> mlist = gTools().SplitString( myMethodList, ',' );
      for (UInt_t i=0; i<mlist.size(); i++) {
         std::string regMethod(mlist[i]);

         if (Use.find(regMethod) == Use.end()) {
            std::cout << "Method \"" << regMethod 
                      << "\" not known in TMVA under this name. Choose among the following:" << std::endl;
            for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) {
               std::cout << it->first << " ";
            }
            std::cout << std::endl;
            return;
         }
         Use[regMethod] = 1;
      }
   }

   // --------------------------------------------------------------------------------------------------

   // --- Create the Reader object

   TMVA::Reader *reader = new TMVA::Reader("!Color:!Silent" );    
   // Create a set of variables and declare them to the reader
   // - the variable names MUST corresponds in name and type to those given in the weight file(s) used
   Float_t var1, var2, var3, var4, var5, var6, var7, var8, var9, var10, var11, var12, var13, var14, var15, var16, var17, var18, var19, var20, var21, var22, var23, var24, var25, var26, var27, var28, var29;
   //reader->AddVariable( "Alt$(jet_pt_singleLepCalc[0],0)", &var1);
   //reader->AddVariable( "Alt$(jet_pt_singleLepCalc[1],0)", &var2 );
   //reader->AddVariable( "Alt$(jet_pt_singleLepCalc[2],0)", &var3 );
   reader->AddVariable( "Alt$(bJetPt_CATopoCalc[0],0)", &var4 );
   reader->AddVariable( "Alt$(bJetPt_CATopoCalc[1],0)", &var5 );
   //reader->AddVariable( "corr_met_singleLepCalc", &var6 );
   //reader->AddVariable( "muon_1_pt_singleLepCalc", &var7 );
   //reader->AddVariable( "nBJets_CATopoCalc", &var8 );
   //reader->AddVariable( "nSelJets_CommonCalc", &var9 );
   //reader->AddVariable( "LeptonJet_DeltaR_LjetsTopoCalcNew", &var10);
   reader->AddVariable( "Mevent_LjetsTopoCalcNew", &var11);
   //reader->AddVariable( "W_Pt_LjetsTopoCalcNew", &var12 );
   reader->AddVariable( "Jet1Jet2_Pt_LjetsTopoCalcNew", &var13 );
   //reader->AddVariable( "BestTop_LjetsTopoCalcNew", &var14 );
   //reader->AddVariable( "BTagTopMass_LjetsTopoCalcNew", &var15 );
   //reader->AddVariable( "Alt$(CAHEPTopJetMass_JetSubCalc[0],0)", &var16 );
   //reader->AddVariable( "Alt$(CAWCSVMSubJets_JetSubCalc[0],0)", &var17 );
   //reader->AddVariable( "Alt$(CAWCSVLSubJets_JetSubCalc[0],0)", &var18 );
   reader->AddVariable( "Alt$(CAWJetPt_JetSubCalc[0],0)", &var19 );
   reader->AddVariable( "Alt$(CAWJetMass_JetSubCalc[0],0)", &var20 );
   //reader->AddVariable( "Alt$(CAHEPTopJetMass_JetSubCalc[1],0)", &var21 );
   //reader->AddVariable( "Hz_LjetsTopoCalcNew", &var22 );
   //reader->AddVariable( "Centrality_LjetsTopoCalcNew", &var23 );
   reader->AddVariable( "SqrtsT_LjetsTopoCalcNew", &var24 );
   reader->AddVariable( "CAMindrBMass_CATopoCalc", &var28 );
   reader->AddVariable( "minDRCAtoB_CATopoCalc",  &var29 );
   //reader->AddVariable( "HT2prime_LjetsTopoCalcNew", &var25 );
   reader->AddVariable( "HT2_LjetsTopoCalcNew", &var26 );
   //reader->AddVariable( "dphiLepMet_LjetsTopoCalcNew", &var27 );
     


   // Spectator variables declared in the training have to be added to the reader, too
//    Float_t spec1,spec2;
//    reader->AddSpectator( "spec1 := var1*2",   &spec1 );
//    reader->AddSpectator( "spec2 := var1*3",   &spec2 );

//    Float_t Category_cat1, Category_cat2, Category_cat3;
//    if (Use["Category"]){
//       // Add artificial spectators for distinguishing categories
//       reader->AddSpectator( "Category_cat1 := var3<=0",             &Category_cat1 );
//       reader->AddSpectator( "Category_cat2 := (var3>0)&&(var4<0)",  &Category_cat2 );
//       reader->AddSpectator( "Category_cat3 := (var3>0)&&(var4>=0)", &Category_cat3 );
//    }

   // --- Book the MVA methods
   // Book method(s)
  TString weightFileName = "weights/TMVAClassification_BDT.weights";
  reader->BookMVA("BDT method", weightFileName+".xml" ); 

   
   // Book output histograms
   UInt_t nbin = 100;
   TH1F   *histBdt(0);
   histBdt = new TH1F( "MVA_BDT_TX",  "MVA_BDT_TX", nbin, -1.0, 1.0);

   // Prepare input tree (this must be replaced by your data source)
   // in this example, there is a toy tree with signal and one with background events
   // we'll later on use only the "signal" events for the test in this example.
   //   
   TFile *input(0);
   TString fileName = iFileName;
   TString fname = sampleLocation+"/";
   fname += fileName;
   TString oFileName = fileName;

   if (!gSystem->AccessPathName( fname )) 
      input = TFile::Open( fname ); // check if file in local directory exists
   if (!input) {
      std::cout << "ERROR: could not open data file" << std::endl;
      exit(1);
   }
   std::cout << "--- TMVAClassificationApp    : Using input file: " << input->GetName() << std::endl;

   // --- Event loop

   // Prepare the event tree
   // - here the variable names have to corresponds to your tree
   // - you can use the same variables as above which is slightly faster,
   //   but of course you can use different ones and copy the values inside the event loop
   //
   std::cout << "--- Select signal sample" << std::endl;
   TTree* theTree = (TTree*)input->Get("ljmet");
   gSystem->mkdir( outputLocation );
   TFile *target  = new TFile( outputLocation+"/"+oFileName,"RECREATE" );
   TTree *newTree = theTree->CloneTree();
   Float_t BDT;
   TBranch *branchBDT = newTree->Branch("__BDT_TX__",&BDT,"__BDT_TX__/F");
   std::vector<Double_t> *vecVar1;
   std::vector<Double_t> *vecVar4;
   std::vector<Double_t> *vecVar16;
   std::vector<Int_t> *vecVar17;
   std::vector<Int_t> *vecVar18;
   std::vector<Double_t> *vecVar19;
   std::vector<Double_t> *vecVar20;
   Int_t *intVar5, *intVar8, *intVar9;
   Double_t *dVar2, *dVar3, *dVar6, *dVar7, *dVar10, *dVar11, *dVar12, *dVar13, *dVar14, *dVar15, *dVar22, *dVar23, *dVar24, dVar25, *dVar26, *dVar27, *dVar28, *dVar29;
   theTree->SetBranchAddress( "jet_pt_singleLepCalc", &vecVar1);
   theTree->SetBranchAddress( "bJetPt_CATopoCalc", &vecVar4 );
   theTree->SetBranchAddress( "corr_met_singleLepCalc", &dVar6 );
   theTree->SetBranchAddress( "muon_1_pt_singleLepCalc", &dVar7 );
   theTree->SetBranchAddress( "nBJets_CATopoCalc", &intVar8 );
   theTree->SetBranchAddress( "nSelJets_CommonCalc", &intVar9 );
   theTree->SetBranchAddress( "LeptonJet_DeltaR_LjetsTopoCalcNew", &dVar10);
   theTree->SetBranchAddress( "Mevent_LjetsTopoCalcNew", &dVar11);
   theTree->SetBranchAddress( "W_Pt_LjetsTopoCalcNew", &dVar12 );
   theTree->SetBranchAddress( "Jet1Jet2_Pt_LjetsTopoCalcNew", &dVar13 );
   theTree->SetBranchAddress( "BestTop_LjetsTopoCalcNew", &dVar14 );
   theTree->SetBranchAddress( "BTagTopMass_LjetsTopoCalcNew", &dVar15 );
   theTree->SetBranchAddress( "CAHEPTopJetMass_JetSubCalc", &vecVar16 );
   theTree->SetBranchAddress( "CAWCSVMSubJets_JetSubCalc", &vecVar17 );
   theTree->SetBranchAddress( "CAWCSVLSubJets_JetSubCalc", &vecVar18 );
   theTree->SetBranchAddress( "CAWJetPt_JetSubCalc", &vecVar19 );
   theTree->SetBranchAddress( "CAWJetMass_JetSubCalc", &vecVar20 );
   theTree->SetBranchAddress( "Hz_LjetsTopoCalcNew", &dVar22 );
   theTree->SetBranchAddress( "Centrality_LjetsTopoCalcNew", &dVar23 );
   theTree->SetBranchAddress( "SqrtsT_LjetsTopoCalcNew", &dVar24 );
   theTree->SetBranchAddress( "HT2prime_LjetsTopoCalcNew", &dVar25 );
   theTree->SetBranchAddress( "HT2_LjetsTopoCalcNew", &dVar26 );
   theTree->SetBranchAddress( "dphiLepMet_LjetsTopoCalcNew", &dVar27 );
   theTree->SetBranchAddress( "CAMindrBMass_CATopoCalc", &dVar28 );
   theTree->SetBranchAddress( "minDRCAtoB_CATopoCalc", &dVar29 );


   // Efficiency calculator for cut method
   Int_t    nSelCutsGA = 0;
   Double_t effS       = 0.7;

   std::vector<Float_t> vecVar(4); // vector for EvaluateMVA tests

   std::cout << "--- Processing: " << theTree->GetEntries() << " events" << std::endl;
   TStopwatch sw;
   sw.Start();
   for (Long64_t ievt=0; ievt<theTree->GetEntries();ievt++) {

      if (ievt%1000 == 0) std::cout << "--- ... Processing event: " << ievt << std::endl;
      theTree->GetEntry(ievt);
      if(vecVar1->size()>0){
      	var1 = vecVar1->at(0);
      }
      if(vecVar1->size()>1){
      	var2 = vecVar1->at(1);
      }
      if(vecVar1->size()>2){
      	var3 = vecVar1->at(2);
      }
      if(vecVar4->size()>0){
      	var4 = vecVar4->at(0);
      }
      if(vecVar4->size()>1){
      	var5 = vecVar4->at(1);
      }      
      var6 = dVar6;
      var7 = dVar7;
      var8 = intVar8;
      var9 = intVar9;
      var10 = dVar10;
      var11 = dVar11;
      var12 = dVar12;
      var13 = dVar13;
      var14 = dVar14;
      var15 = dVar15;
      if(vecVar16->size()>0){
      	var16 = vecVar16->at(0);
      }
      else{
      	var16 = 0;
      }
      if(vecVar17->size()>0){
      	var17 = vecVar17->at(0);
      }
      else{
      	var18 = 0;
      }
      if(vecVar19->size()>0){
      	var19 = vecVar19->at(0);
      }
      else{
      	var19 = 0;
      }     
      if(vecVar20->size()>0){
      	var20 = vecVar20->at(0);
      }
      else{
      	var20 = 0;
      }
      if(vecVar16->size()>1){
	  	var21 = vecVar16->at(1);
	  }
	  else{
	  	var21 = 0;
	  }
	  var22 = dVar22;
	  var23 = dVar23;
	  var24 = dVar24;
	  var25 = dVar25;
	  var26 = dVar26;
	  var27 = dVar27;
	  var28 = dVar28;
	  var29 = dVar29;      // --- Return the MVA outputs and fill into histograms

      if (Use["CutsGA"]) {
         // Cuts is a special case: give the desired signal efficienciy
         Bool_t passed = reader->EvaluateMVA( "CutsGA method", effS );
         if (passed) nSelCutsGA++;
      }
      BDT = reader->EvaluateMVA( "BDT method");
      histBdt->Fill(BDT);
      branchBDT->Fill();
   }

   // Get elapsed time
   sw.Stop();
   std::cout << "--- End of event loop: "; sw.Print();

   // Get efficiency for cuts classifier
   if (Use["CutsGA"]) std::cout << "--- Efficiency for CutsGA method: " << double(nSelCutsGA)/theTree->GetEntries()
                                << " (for a required signal efficiency of " << effS << ")" << std::endl;

   if (Use["CutsGA"]) {

      // test: retrieve cuts for particular signal efficiency
      // CINT ignores dynamic_casts so we have to use a cuts-secific Reader function to acces the pointer  
      TMVA::MethodCuts* mcuts = reader->FindCutsMVA( "CutsGA method" ) ;

      if (mcuts) {      
         std::vector<Double_t> cutsMin;
         std::vector<Double_t> cutsMax;
         mcuts->GetCuts( 0.7, cutsMin, cutsMax );
         std::cout << "--- -------------------------------------------------------------" << std::endl;
         std::cout << "--- Retrieve cut values for signal efficiency of 0.7 from Reader" << std::endl;
         for (UInt_t ivar=0; ivar<cutsMin.size(); ivar++) {
            std::cout << "... Cut: " 
                      << cutsMin[ivar] 
                      << " < \"" 
                      << mcuts->GetInputVar(ivar)
                      << "\" <= " 
                      << cutsMax[ivar] << std::endl;
         }
         std::cout << "--- -------------------------------------------------------------" << std::endl;
      }
   }

   // --- Write histograms

   newTree->Write("",TObject::kOverwrite);
   target->Close();

   std::cout << "--- Created root file: \""<<oFileName<<"\" containing the MVA output histograms" << std::endl;
  
   delete reader;
    
   std::cout << "==> TMVAClassificationApplication is done!" << endl << std::endl;
} 
コード例 #28
0
ファイル: DYPtZ_HF_BDTCut.C プロジェクト: rbartek/usercode
void DYPtZ_HF_BDTCut( TString myMethodList = "" ) 
{   
#ifdef __CINT__
   gROOT->ProcessLine( ".O0" ); // turn off optimization in CINT
#endif

   //---------------------------------------------------------------

   // This loads the library
   TMVA::Tools::Instance();

   // Default MVA methods to be trained + tested
   std::map<std::string,int> Use;

   // --- Cut optimisation
   Use["Cuts"]            = 0;
   Use["CutsD"]           = 0;
   Use["CutsPCA"]         = 0;
   Use["CutsGA"]          = 0;
   Use["CutsSA"]          = 0;
   // 
   // --- 1-dimensional likelihood ("naive Bayes estimator")
   Use["Likelihood"]      = 0;
   Use["LikelihoodD"]     = 0; // the "D" extension indicates decorrelated input variables (see option strings)
   Use["LikelihoodPCA"]   = 0; // the "PCA" extension indicates PCA-transformed input variables (see option strings)
   Use["LikelihoodKDE"]   = 0;
   Use["LikelihoodMIX"]   = 0;
   //
   // --- Mutidimensional likelihood and Nearest-Neighbour methods
   Use["PDERS"]           = 0;
   Use["PDERSD"]          = 0;
   Use["PDERSPCA"]        = 0;
   Use["PDEFoam"]         = 0;
   Use["PDEFoamBoost"]    = 0; // uses generalised MVA method boosting
   Use["KNN"]             = 0; // k-nearest neighbour method
   //
   // --- Linear Discriminant Analysis
   Use["LD"]              = 0; // Linear Discriminant identical to Fisher
   Use["Fisher"]          = 0;
   Use["FisherG"]         = 0;
   Use["BoostedFisher"]   = 0; // uses generalised MVA method boosting
   Use["HMatrix"]         = 0;
   //
   // --- Function Discriminant analysis
   Use["FDA_GA"]          = 0; // minimisation of user-defined function using Genetics Algorithm
   Use["FDA_SA"]          = 0;
   Use["FDA_MC"]          = 0;
   Use["FDA_MT"]          = 0;
   Use["FDA_GAMT"]        = 0;
   Use["FDA_MCMT"]        = 0;
   //
   // --- Neural Networks (all are feed-forward Multilayer Perceptrons)
   Use["MLP"]             = 0; // Recommended ANN
   Use["MLPBFGS"]         = 0; // Recommended ANN with optional training method
   Use["MLPBNN"]          = 0; // Recommended ANN with BFGS training method and bayesian regulator
   Use["CFMlpANN"]        = 0; // Depreciated ANN from ALEPH
   Use["TMlpANN"]         = 0; // ROOT's own ANN
   //
   // --- Support Vector Machine 
   Use["SVM"]             = 0;
   // 
   // --- Boosted Decision Trees using this
   Use["BDT"]             = 1; // uses Adaptive Boost
   Use["BDTG"]            = 0; // uses Gradient Boost
   Use["BDTB"]            = 0; // uses Bagging
   Use["BDTD"]            = 0; // decorrelation + Adaptive Boost
   // 
   // --- Friedman's RuleFit method, ie, an optimised series of cuts ("rules")
   Use["RuleFit"]         = 0;
   // ---------------------------------------------------------------
   Use["Plugin"]          = 0;
   Use["Category"]        = 0;
   Use["SVM_Gauss"]       = 0;
   Use["SVM_Poly"]        = 0;
   Use["SVM_Lin"]         = 0;

   std::cout << std::endl;
   std::cout << "==> Start DYPtZ_HF_BDTCut" << std::endl;

   // Select methods (don't look at this code - not of interest)
   if (myMethodList != "") {
      for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) it->second = 0;

      std::vector<TString> mlist = gTools().SplitString( myMethodList, ',' );
      for (UInt_t i=0; i<mlist.size(); i++) {
         std::string regMethod(mlist[i]);

         if (Use.find(regMethod) == Use.end()) {
            std::cout << "Method \"" << regMethod 
                      << "\" not known in TMVA under this name. Choose among the following:" << std::endl;
            for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) {
               std::cout << it->first << " ";
            }
            std::cout << std::endl;
            return;
         }
         Use[regMethod] = 1;
      }
   }

   // --------------------------------------------------------------------------------------------------

   // --- Create the Reader object

   TMVA::Reader *reader = new TMVA::Reader( "!Color:!Silent" );    

   // Create a set of variables and declare them to the reader
   // - the variable names MUST corresponds in name and type to those given in the weight file(s) used
   Float_t Hmass, Emumass;
	Float_t Hpt, Zpt;
	Float_t CSV0, CSV1;
	Float_t DeltaPhiHV, DetaJJ;
	Int_t  nJets, eventFlavor, Naj; 
 	Float_t BDTvalue, Trigweight, B2011PUweight, A2011PUweight, btag2CSF, MET;
	Float_t alpha_j, qtb1, jetPhi0, jetPhi1, jetEta0, jetEta1, Zphi, Hphi;
	Float_t Ht, EvntShpCircularity, jetCHF0, jetCHF1;
	Float_t EtaStandDev, lep_pfCombRelIso0, lep_pfCombRelIso1, EvntShpIsotropy;
	Float_t lep0pt, lep1pt, UnweightedEta, DphiJJ, RMS_eta, EvntShpSphericity;
	Float_t PtbalZH, EventPt, AngleEMU, Centrality, EvntShpAplanarity;
	 Float_t UnweightedEta, lep0pt;
	 	Int_t naJets, nSV;
	 Float_t Mte, Mtmu, dPhiHMET, Dphiemu, delRjj, delRemu, DphiZMET, DeltaPhijetMETmin;
	 Float_t MassEleb0, DphiEleMET, PtbalMETH,dphiEleMET, dEtaJJ, dphiZMET, ScalarSumPt;
	Float_t ZmassSVD, AngleHemu, ProjVisT, topMass, topPt, ZmassSVDnegSol, Mt, Zmass, ZmassNegInclu;
	
	Float_t dphiEMU, dphiZMET;
	
	reader->AddVariable( "Hmass", &Hmass );
	//reader->AddVariable( "Naj", &Naj );
	reader->AddVariable( "CSV0", &CSV0 );
	reader->AddVariable( "Emumass", &Emumass );
	reader->AddVariable( "DeltaPhiHV", &DeltaPhiHV );
	reader->AddVariable( "Mt", &Mt );
	reader->AddVariable( "dPhiHMET", &dPhiHMET );
	reader->AddVariable( "dphiEMU := abs(Dphiemu)", &dphiEMU );
	reader->AddVariable( "dphiZMET:=abs(DphiZMET)", &dphiZMET );
	reader->AddVariable( "PtbalMETH", &PtbalMETH );
	reader->AddVariable( "EtaStandDev", &EtaStandDev );
	reader->AddVariable( "jetCHF0", &jetCHF0 );
	reader->AddVariable( "ProjVisT", &ProjVisT );

	
   // Spectator variables declared in the training have to be added to the reader, too
 
//   reader->AddSpectator( "UnweightedEta",   &UnweightedEta );
  // reader->AddSpectator( "lep0pt",   &lep0pt );

/*   Float_t Category_cat1, Category_cat2, Category_cat3;
   if (Use["Category"]){
      // Add artificial spectators for distinguishing categories
      reader->AddSpectator( "Category_cat1 := var3<=0",             &Category_cat1 );
      reader->AddSpectator( "Category_cat2 := (var3>0)&&(var4<0)",  &Category_cat2 );
      reader->AddSpectator( "Category_cat3 := (var3>0)&&(var4>=0)", &Category_cat3 );
   }
*/
   // --- Book the MVA methods

   TString dir    = "weights/";
   TString prefix = "TMVAClassification";

   // Book method(s)
   for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) {
      if (it->second) {
         TString methodName = TString(it->first) + TString(" method");
         TString weightfile = dir + prefix + TString("_") + TString(it->first) + TString(".weights.xml");
         reader->BookMVA( methodName, weightfile ); 
      }
   }
   
	// Book output histograms
   	TH1F* hCHFb0_OpenSelection= new TH1F		("hCHFb0_OpenSelection", "charged Hadron Energy Fraction b1", 40, 0.0, 1.2);
   	TH1F* hCHFb1_OpenSelection= new TH1F		("hCHFb1_OpenSelection", "charged Hadron Energy Fraction b2", 40, 0.0, 1.2);
 	TH1F* hPtjj_OpenSelection= new TH1F		("hPtjj_OpenSelection","Pt of two b jets with highest CSV ", 50, 0.0, 400);
 	TH1F* hPtmumu_OpenSelection= new TH1F	("hPtmumu_OpenSelection","Pt of two muons with highest pt ", 50, 0.0, 400);
	TH1F* hPtbalZH_OpenSelection= new TH1F	("hPtbalZH_OpenSelection", "Pt balance of Z and H", 40, -80, 80);
	TH1F* hPtmu0_OpenSelection= new TH1F		("hPtmu0_OpenSelection","Pt of muon with highest pt ", 30, 0.0, 300);
	TH1F* hPtmu1_OpenSelection= new TH1F		("hPtmu1_OpenSelection","Pt of muon with second highest pt ", 30, 0.0, 300);
	TH1F* hPFRelIsomu0_OpenSelection= new TH1F		("hPFRelIsomu0_OpenSelection", "PF Rel Iso of muon with highest Pt", 40, 0., 0.2);
	TH1F* hPFRelIsomu1_OpenSelection= new TH1F		("hPFRelIsomu1_OpenSelection", "PF Rel Iso of muon with second highest Pt", 40, 0., 0.2);
	TH1F* hCSV0_OpenSelection= new TH1F		("hCSV0_OpenSelection","Jet with highest CSV ",			40, 0, 1.5);
	TH1F* hCSV1_OpenSelection= new TH1F		("hCSV1_OpenSelection","Jet with second highest CSV ",		40, 0, 1.5);
	TH1F* hdphiVH_OpenSelection= new TH1F	("hdphiVH_OpenSelection","Delta phi between Z and Higgs ", 50, -0.1, 4.5);
	TH1F* hdetaJJ_OpenSelection= new TH1F	("hdetaJJ_OpenSelection","Delta eta between two jets ", 60, -4, 4);
	TH1F* hNjets_OpenSelection= new TH1F	("hNjets_OpenSelection", "Number of Jets",		13, -2.5, 10.5);
	TH1F* hMjj_OpenSelection	= new TH1F	("hMjj_OpenSelection",  "Invariant Mass of two Jets ",		50, 0, 300);
	TH1F* hMmumu_OpenSelection	= new TH1F	("hMmumu_OpenSelection",  "Invariant Mass of two muons ",	75, 0, 200);
    TH1F* hRMSeta_OpenSelection= new TH1F	("hRMSeta_OpenSelection", "RMS Eta",		30, 0, 3);
    TH1F* hStaDeveta_OpenSelection= new TH1F	("hStaDeveta_OpenSelection", "Standard Deviation Eta",		30, 0, 3);
	TH1F* hUnweightedEta_OpenSelection= new TH1F	("hUnweightedEta_OpenSelection",  "Unweighted Eta ",		50, 0, 15);		
	TH1F* hdphiJJ_vect_OpenSelection= new TH1F	("hdphiJJ_vect_OpenSelection", "Delta phi between two jets",  30, -3.5, 4);
	TH1F* hCircularity_OpenSelection= new TH1F("hCircularity_OpenSelection","EventShapeVariables circularity", 30, 0.0, 1.2);
	TH1F* hHt_OpenSelection= new TH1F("hHt_OpenSelection","scalar sum of pt of four particles", 50, 0.0, 500);
    TH1F* hCentrality_OpenSelection= new TH1F	("hCentrality_OpenSelection", "Centrality", 40, 0.0, 0.8);
    TH1F* hEventPt_OpenSelection= new TH1F	("hEventPt_OpenSelection", "Pt of HV system", 50, 0.0, 100);
    TH1F* hAngleEMU_OpenSelection= new TH1F	("hAngleEMU_OpenSelection", "AngleEMU between H and Z", 45, 0, 4.5);
    TH1F* hSphericity_OpenSelection= new TH1F	("hSphericity_OpenSelection", "EventShapeVariables sphericity", 50, 0.0, 1);
    TH1F* hAplanarity_OpenSelection= new TH1F	("hAplanarity_OpenSelection", "EventShapeVariables Aplanarity", 50, -0.1, .4);
    TH1F* hIsotropy_OpenSelection= new TH1F	("hIsotropy_OpenSelection",  "EventShapeVariables isotropy", 30, 0.0, 1.3);
    TH2F* hDphiDetajj_OpenSelection= new TH2F	("hDphiDetajj_OpenSelection", "#Delta#phi vs #Delta#eta JJ", 25, -5, 5, 25, -5, 5);
	TH1F* hMtmu_OpenSelection= new TH1F ("hMtmu_OpenSelection", "Mt with respect to Muon", 101, -0.1, 200);
	TH1F* hMte_OpenSelection= new TH1F ("hMte_OpenSelection", "Mt with respect to Electron", 101, -0.1, 200);
	TH1F* hdPhiHMET_OpenSelection= new TH1F ("hdPhiHMET_OpenSelection", "Delta phi between MET and Higgs", 50, -0.1, 4.5);
	TH1F* hDphiemu_OpenSelection= new TH1F ("hDphiemu_OpenSelection", "Delta phi between e and muon", 50, -3.5, 4.5);
	TH1F* hdelRjj_OpenSelection= new TH1F ("hdelRjj_OpenSelection", "Delta R jj", 55, 0, 5.5);
	TH1F* hdelRemu_OpenSelection= new TH1F ("hdelRemu_OpenSelection", "Delta R emu", 55, 0, 5.5);
	TH1F* hDphiZMET_OpenSelection= new TH1F ("hDphiZMET_OpenSelection", "Delta phi between Z and MET",  71, -3.5, 4);
	TH1F* hDeltaPhijetMETmin_OpenSelection= new TH1F ("hDeltaPhijetMETmin_OpenSelection", "Delta phi between MET and nearest jet", 50, -0.1, 4.5);
	TH1F* hAngleHemu_OpenSelection= new TH1F ("hAngleHemu_OpenSelection", "Angle between H and Z", 30, 0, 3.5);
	TH1F* hProjVisT_OpenSelection= new TH1F ("hProjVisT_OpenSelection", "Transverse componenet of Projection of Z onto bisector", 80, 0, 200);
	TH1F* htopMass_OpenSelection= new TH1F ("htopMass_OpenSelection", "Top Mass single lepton", 100, 75, 375);
	TH1F* htopPt_OpenSelection= new TH1F ("htopPt_OpenSelection", "Pt of Top", 125, 0, 250);
	TH1F* hVMt_OpenSelection= new TH1F ("hVMt_OpenSelection", "VMt", 75, 0, 150);
	TH1F* hZmassSVD_OpenSelection= new TH1F ("hZmassSVD_OpenSelection", "Invariant Mass of two Leptons corrected SVD", 75, 0, 150);
	TH1F* hZmassSVDnegSol_OpenSelection= new TH1F ("hZmassSVDnegSol_OpenSelection", "Invariant Mass of two Leptons corrected SVD", 100, -50, 200);
	TH1F* hZmass_OpenSelection= new TH1F ("hZmass_OpenSelection", "Zmass ", 5, 0, 150);
	TH1F* hZmassNegInclu_OpenSelection= new TH1F ("hZmassNegInclu_OpenSelection", "Invariant Mass of two Leptons corrected SVD", 100, -50, 200);
	
	
	TTree *treeWithBDT = new TTree("treeWithBDT","Tree wiht BDT output");
	treeWithBDT->SetDirectory(0);
	treeWithBDT->Branch("nJets",&nJets, "nJets/I");
	treeWithBDT->Branch("naJets",&naJets, "naJets/I");
	treeWithBDT->Branch("eventFlavor",&eventFlavor, "eventFlavor/I");
	treeWithBDT->Branch("CSV0",&CSV0, "CSV0/F");
	treeWithBDT->Branch("CSV1",&CSV1, "CSV1/F");
	treeWithBDT->Branch("Emumass",&Emumass, "Emumass/F");
	treeWithBDT->Branch("Hmass",&Hmass, "Hmass/F");
	treeWithBDT->Branch("DeltaPhiHV",&DeltaPhiHV, "DeltaPhiHV/F");
	treeWithBDT->Branch("Hpt",&Hpt, "Hpt/F");
	treeWithBDT->Branch("Zpt",&Zpt, "Zpt/F");
	treeWithBDT->Branch("lep0pt",&lep0pt, "lep0pt/F");
	treeWithBDT->Branch("Ht",&Ht, "Ht/F");
	treeWithBDT->Branch("EtaStandDev",&EtaStandDev, "EtaStandDev/F");
	treeWithBDT->Branch("UnweightedEta",&UnweightedEta, "UnweightedEta/F");
	treeWithBDT->Branch("EvntShpCircularity",&EvntShpCircularity, "EvntShpCircularity/F");
	treeWithBDT->Branch("alpha_j",&alpha_j, "alpha_j/F");
	treeWithBDT->Branch("qtb1",&qtb1, "qtb1/F");
	treeWithBDT->Branch("nSV",&nSV, "nSV/I");
	treeWithBDT->Branch("Trigweight",&Trigweight, "Trigweight/F");
	treeWithBDT->Branch("B2011PUweight",&B2011PUweight, "B2011PUweight/F");
	treeWithBDT->Branch("A2011PUweight",&A2011PUweight, "A2011PUweight/F");
	treeWithBDT->Branch("btag2CSF",&btag2CSF, "btag2CSF/F");
	treeWithBDT->Branch("DetaJJ",&DetaJJ, "DetaJJ/F");
	treeWithBDT->Branch("jetCHF0",&jetCHF0, "jetCHF0/F");
	treeWithBDT->Branch("jetCHF1",&jetCHF1, "jetCHF1/F");
	treeWithBDT->Branch("jetPhi0",&jetPhi0, "jetPhi0/F");
	treeWithBDT->Branch("jetPhi1",&jetPhi1, "jetPhi1/F");
	treeWithBDT->Branch("jetEta0",&jetEta0, "jetEta0/F");
	treeWithBDT->Branch("jetEta1",&jetEta1, "jetEta1/F");
	treeWithBDT->Branch("lep1pt",&lep1pt, "lep1pt/F");
	treeWithBDT->Branch("lep_pfCombRelIso0",&lep_pfCombRelIso0, "lep_pfCombRelIso0/F");
	treeWithBDT->Branch("lep_pfCombRelIso1",&lep_pfCombRelIso1, "lep_pfCombRelIso1/F");
	treeWithBDT->Branch("DphiJJ",&DphiJJ, "DphiJJ/F");
	treeWithBDT->Branch("RMS_eta",&RMS_eta, "RMS_eta/F");
	treeWithBDT->Branch("PtbalZH",&PtbalZH, "PtbalZH/F");
	treeWithBDT->Branch("EventPt",&EventPt, "EventPt/F");
	treeWithBDT->Branch("AngleEMU",&AngleEMU, "AngleEMU/F");
	treeWithBDT->Branch("Centrality",&Centrality, "Centrality/F");
	treeWithBDT->Branch("MET",&MET, "MET/F");
	treeWithBDT->Branch("EvntShpAplanarity",&EvntShpAplanarity, "EvntShpAplanarity/F");
	treeWithBDT->Branch("EvntShpSphericity",&EvntShpSphericity, "EvntShpSphericity/F");
	treeWithBDT->Branch("EvntShpIsotropy",&EvntShpIsotropy, "EvntShpIsotropy/F");
	treeWithBDT->Branch("Zphi",&Zphi, "Zphi/F");
	treeWithBDT->Branch("Hphi",&Hphi, "Hphi/F");
	treeWithBDT->Branch("Mte",&Mte, "Mte/F");
	treeWithBDT->Branch("Mtmu",&Mtmu, "Mtmu/F");
	treeWithBDT->Branch("dPhiHMET",&dPhiHMET, "dPhiHMET/F");
	treeWithBDT->Branch("Dphiemu",&Dphiemu, "Dphiemu/F");
	treeWithBDT->Branch("delRjj",&delRjj, "delRjj/F");
	treeWithBDT->Branch("delRemu",&delRemu, "delRemu/F");
	treeWithBDT->Branch("DphiZMET",&DphiZMET, "DphiZMET/F");
	treeWithBDT->Branch("DeltaPhijetMETmin",&DeltaPhijetMETmin, "DeltaPhijetMETmin/F");
	treeWithBDT->Branch("BDTvalue",&BDTvalue, "BDTvalue/F");
	treeWithBDT->Branch("AngleHemu",&AngleHemu, "AngleHemu/F");
	treeWithBDT->Branch("ProjVisT",&ProjVisT, "ProjVisT/F");
	treeWithBDT->Branch("topMass",&topMass, "topMass/F");
	treeWithBDT->Branch("topPt",&topPt, "topPt/F");
	treeWithBDT->Branch("ZmassSVD",&ZmassSVD, "ZmassSVD/F");
	treeWithBDT->Branch("ZmassSVDnegSol",&ZmassSVDnegSol, "ZmassSVDnegSol/F");
	treeWithBDT->Branch("Zmass",&Zmass, "Zmass/F");
	treeWithBDT->Branch("ZmassNegInclu",&ZmassNegInclu, "ZmassNegInclu/F");
	treeWithBDT->Branch("topMass",&topMass, "topMass/F");
	treeWithBDT->Branch("Mt",&Mt, "Mt/F");

	
   UInt_t nbin = 15;
   TH1F   *histLk(0), *histLkD(0), *histLkPCA(0), *histLkKDE(0), *histLkMIX(0), *histPD(0), *histPDD(0);
   TH1F   *histPDPCA(0), *histPDEFoam(0), *histPDEFoamErr(0), *histPDEFoamSig(0), *histKNN(0), *histHm(0);
   TH1F   *histFi(0), *histFiG(0), *histFiB(0), *histLD(0), *histNn(0),*histNnbfgs(0),*histNnbnn(0);
   TH1F   *histNnC(0), *histNnT(0), *histBdt(0), *histBdtG(0), *histBdtD(0), *histRf(0), *histSVMG(0);
   TH1F   *histSVMP(0), *histSVML(0), *histFDAMT(0), *histFDAGA(0), *histCat(0), *histPBdt(0);

   if (Use["Likelihood"])    histLk      = new TH1F( "MVA_Likelihood",    "MVA_Likelihood",    nbin, -1, 1 );
   if (Use["LikelihoodD"])   histLkD     = new TH1F( "MVA_LikelihoodD",   "MVA_LikelihoodD",   nbin, -1, 0.9999 );
   if (Use["LikelihoodPCA"]) histLkPCA   = new TH1F( "MVA_LikelihoodPCA", "MVA_LikelihoodPCA", nbin, -1, 1 );
   if (Use["LikelihoodKDE"]) histLkKDE   = new TH1F( "MVA_LikelihoodKDE", "MVA_LikelihoodKDE", nbin,  -0.00001, 0.99999 );
   if (Use["LikelihoodMIX"]) histLkMIX   = new TH1F( "MVA_LikelihoodMIX", "MVA_LikelihoodMIX", nbin,  0, 1 );
   if (Use["PDERS"])         histPD      = new TH1F( "MVA_PDERS",         "MVA_PDERS",         nbin,  0, 1 );
   if (Use["PDERSD"])        histPDD     = new TH1F( "MVA_PDERSD",        "MVA_PDERSD",        nbin,  0, 1 );
   if (Use["PDERSPCA"])      histPDPCA   = new TH1F( "MVA_PDERSPCA",      "MVA_PDERSPCA",      nbin,  0, 1 );
   if (Use["KNN"])           histKNN     = new TH1F( "MVA_KNN",           "MVA_KNN",           nbin,  0, 1 );
   if (Use["HMatrix"])       histHm      = new TH1F( "MVA_HMatrix",       "MVA_HMatrix",       nbin, -0.95, 1.55 );
   if (Use["Fisher"])        histFi      = new TH1F( "MVA_Fisher",        "MVA_Fisher",        nbin, -4, 4 );
   if (Use["FisherG"])       histFiG     = new TH1F( "MVA_FisherG",       "MVA_FisherG",       nbin, -1, 1 );
   if (Use["BoostedFisher"]) histFiB     = new TH1F( "MVA_BoostedFisher", "MVA_BoostedFisher", nbin, -2, 2 );
   if (Use["LD"])            histLD      = new TH1F( "MVA_LD",            "MVA_LD",            nbin, -2, 2 );
   if (Use["MLP"])           histNn      = new TH1F( "MVA_MLP",           "MVA_MLP",           nbin, -1.25, 1.5 );
   if (Use["MLPBFGS"])       histNnbfgs  = new TH1F( "MVA_MLPBFGS",       "MVA_MLPBFGS",       nbin, -1.25, 1.5 );
   if (Use["MLPBNN"])        histNnbnn   = new TH1F( "MVA_MLPBNN",        "MVA_MLPBNN",        nbin, -1.25, 1.5 );
   if (Use["CFMlpANN"])      histNnC     = new TH1F( "MVA_CFMlpANN",      "MVA_CFMlpANN",      nbin,  0, 1 );
   if (Use["TMlpANN"])       histNnT     = new TH1F( "MVA_TMlpANN",       "MVA_TMlpANN",       nbin, -1.3, 1.3 );
	if (Use["BDT"])     {
		histMattBdt     = new TH1F( "Matt_BDT",           "Matt_BDT",           15, -1.1, 0.35 );
		histTMVABdt     = new TH1F( "TMVA_BDT",           "TMVA_BDT",           36, -1.0, -0.1 );
	}
   if (Use["BDTD"])          histBdtD    = new TH1F( "MVA_BDTD",          "MVA_BDTD",          nbin, -0.8, 0.8 );
   if (Use["BDTG"])          histBdtG    = new TH1F( "MVA_BDTG",          "MVA_BDTG",          nbin, -1.0, 1.0 );
   if (Use["RuleFit"])       histRf      = new TH1F( "MVA_RuleFit",       "MVA_RuleFit",       nbin, -2.0, 2.0 );
   if (Use["SVM_Gauss"])     histSVMG    = new TH1F( "MVA_SVM_Gauss",     "MVA_SVM_Gauss",     nbin,  0.0, 1.0 );
   if (Use["SVM_Poly"])      histSVMP    = new TH1F( "MVA_SVM_Poly",      "MVA_SVM_Poly",      nbin,  0.0, 1.0 );
   if (Use["SVM_Lin"])       histSVML    = new TH1F( "MVA_SVM_Lin",       "MVA_SVM_Lin",       nbin,  0.0, 1.0 );
   if (Use["FDA_MT"])        histFDAMT   = new TH1F( "MVA_FDA_MT",        "MVA_FDA_MT",        nbin, -2.0, 3.0 );
   if (Use["FDA_GA"])        histFDAGA   = new TH1F( "MVA_FDA_GA",        "MVA_FDA_GA",        nbin, -2.0, 3.0 );
   if (Use["Category"])      histCat     = new TH1F( "MVA_Category",      "MVA_Category",      nbin, -2., 2. );
   if (Use["Plugin"])        histPBdt    = new TH1F( "MVA_PBDT",          "MVA_BDT",           nbin, -0.8, 0.8 );

   // PDEFoam also returns per-event error, fill in histogram, and also fill significance
   if (Use["PDEFoam"]) {
      histPDEFoam    = new TH1F( "MVA_PDEFoam",       "MVA_PDEFoam",              nbin,  0, 1 );
      histPDEFoamErr = new TH1F( "MVA_PDEFoamErr",    "MVA_PDEFoam error",        nbin,  0, 1 );
      histPDEFoamSig = new TH1F( "MVA_PDEFoamSig",    "MVA_PDEFoam significance", nbin,  0, 10 );
   }

   // Book example histogram for probability (the other methods are done similarly)
   TH1F *probHistFi(0), *rarityHistFi(0);
   if (Use["Fisher"]) {
      probHistFi   = new TH1F( "MVA_Fisher_Proba",  "MVA_Fisher_Proba",  nbin, 0, 1 );
      rarityHistFi = new TH1F( "MVA_Fisher_Rarity", "MVA_Fisher_Rarity", nbin, 0, 1 );
   }

   // Prepare input tree (this must be replaced by your data source)
   // in this example, there is a toy tree with signal and one with background events
   // we'll later on use only the "signal" events for the test in this example.
   //   
   TFile *input(0);
   TString fname = "/home/hep/wilken/taus/CMSSW_4_4_2_patch8/src/UserCode/wilken/V21/DY_PtZ.root"; 
	double lumi = 4.457;
	Double_t  DY_PtZ_weight = lumi/(lumiZJH/2.0); //WW_TuneZ2_7TeV_pythia6_tauola
  
   if (!gSystem->AccessPathName( fname )) 
      input = TFile::Open( fname ); // check if file in local directory exists
   else    
      input = TFile::Open( "http://root.cern.ch/files/tmva_class_example.root" ); // if not: download from ROOT server
   
   if (!input) {
      std::cout << "ERROR: could not open data file" << std::endl;
      exit(1);
   }
   std::cout << "--- TMVAClassificationApp    : Using input file: " << input->GetName() << std::endl;
   
   // --- Event loop

   // Prepare the event tree
   // - here the variable names have to corresponds to your tree
   // - you can use the same variables as above which is slightly faster,
   //   but of course you can use different ones and copy the values inside the event loop
   //
   std::cout << "--- Select signal sample" << std::endl;
   TTree* theTree = (TTree*)input->Get("BDT_btree");
   
    theTree->SetBranchAddress( "Hmass", &Hmass );
    theTree->SetBranchAddress( "Emumass", &Emumass );
    theTree->SetBranchAddress( "Hpt", &Hpt );
	theTree->SetBranchAddress( "Zpt", &Zpt );
	theTree->SetBranchAddress( "CSV0", &CSV0 );
	theTree->SetBranchAddress( "CSV1", &CSV1 );
	theTree->SetBranchAddress( "DeltaPhiHV", &DeltaPhiHV );
	theTree->SetBranchAddress( "DetaJJ", &DetaJJ );
	theTree->SetBranchAddress( "UnweightedEta", &UnweightedEta );
	theTree->SetBranchAddress( "lep0pt", &lep0pt );
	theTree->SetBranchAddress( "Ht", &Ht );
	theTree->SetBranchAddress( "EvntShpCircularity", &EvntShpCircularity );
	theTree->SetBranchAddress( "nJets", &nJets );
	theTree->SetBranchAddress( "naJets",&naJets);
	theTree->SetBranchAddress( "nSV", &nSV );
	theTree->SetBranchAddress( "lep1pt", &lep1pt );
	theTree->SetBranchAddress( "lep0pt", &lep0pt );
	theTree->SetBranchAddress( "EtaStandDev", &EtaStandDev );
	theTree->SetBranchAddress( "UnweightedEta", &UnweightedEta );
	theTree->SetBranchAddress( "jetCHF0", &jetCHF0 );
	theTree->SetBranchAddress( "jetCHF1", &jetCHF1 );
	theTree->SetBranchAddress( "lep_pfCombRelIso0", &lep_pfCombRelIso0 );
	theTree->SetBranchAddress( "lep_pfCombRelIso1", &lep_pfCombRelIso1 );
	theTree->SetBranchAddress( "DphiJJ", &DphiJJ );
	theTree->SetBranchAddress( "RMS_eta", &RMS_eta );
	theTree->SetBranchAddress( "PtbalZH", &PtbalZH );
	theTree->SetBranchAddress( "EventPt", &EventPt );
	theTree->SetBranchAddress( "AngleEMU", &AngleEMU );
	theTree->SetBranchAddress( "Centrality", &Centrality );
	theTree->SetBranchAddress( "EvntShpAplanarity", &EvntShpAplanarity );
	theTree->SetBranchAddress( "EvntShpSphericity", &EvntShpSphericity );
	theTree->SetBranchAddress( "EvntShpIsotropy", &EvntShpIsotropy );
	theTree->SetBranchAddress( "Trigweight", &Trigweight );
	theTree->SetBranchAddress( "B2011PUweight", &B2011PUweight );
	theTree->SetBranchAddress( "A2011PUweight", &A2011PUweight );
	theTree->SetBranchAddress( "btag2CSF", &btag2CSF );
	theTree->SetBranchAddress( "MET", &MET );
	theTree->SetBranchAddress( "Mte"      ,  &Mte     );
	theTree->SetBranchAddress( "Mtmu"      ,  &Mtmu     );
	theTree->SetBranchAddress( "dPhiHMET"      ,  &dPhiHMET     );
	theTree->SetBranchAddress( "DeltaPhijetMETmin"      ,  &DeltaPhijetMETmin     );
	theTree->SetBranchAddress( "delRjj"      ,  &delRjj     );
	theTree->SetBranchAddress( "delRemu"      ,  &delRemu     );
	theTree->SetBranchAddress( "DphiZMET"      ,  &DphiZMET     );
	theTree->SetBranchAddress( "Dphiemu"      ,  &Dphiemu     );
	theTree->SetBranchAddress( "DeltaPhijetMETmin"      ,  &DeltaPhijetMETmin     );
	theTree->SetBranchAddress("AngleHemu",&AngleHemu);
	theTree->SetBranchAddress("ProjVisT",&ProjVisT);
	theTree->SetBranchAddress("topMass",&topMass);
	theTree->SetBranchAddress("topPt",&topPt);
	theTree->SetBranchAddress("ZmassSVD",&ZmassSVD);
	theTree->SetBranchAddress("ZmassSVDnegSol",&ZmassSVDnegSol);
	theTree->SetBranchAddress("Zmass",&Zmass);
	theTree->SetBranchAddress("ZmassNegInclu",&ZmassNegInclu);
	theTree->SetBranchAddress("topMass",&topMass);
	theTree->SetBranchAddress("Mt",&Mt);
	
	theTree->SetBranchAddress( "eventFlavor", &eventFlavor );
	

   // Efficiency calculator for cut method
   Int_t    nSelCutsGA = 0;
   Double_t effS       = 0.7;

	TTree* TMVATree = (TTree*)input->Get("TMVA_tree");
	int NumInTree = theTree->GetEntries();
	int NumberTMVAtree = TMVATree->GetEntries();
	DY_PtZ_weight = DY_PtZ_weight* ( NumInTree /float(NumberTMVAtree)) ;
	

   std::vector<Float_t> vecVar(4); // vector for EvaluateMVA tests

   std::cout << "--- Processing: " << theTree->GetEntries() << " events" << std::endl;
   TStopwatch sw;
   sw.Start();
   int Nevents = 0, NpassBDT = 0;
   for (Long64_t ievt=0; ievt<theTree->GetEntries();ievt++) {
Nevents++;
      if (ievt%1000 == 0) std::cout << "--- ... Processing event: " << ievt << std::endl;

      theTree->GetEntry(ievt);

 //     var1 = userVar1 + userVar2;
   //   var2 = userVar1 - userVar2;

      // --- Return the MVA outputs and fill into histograms

      if (Use["CutsGA"]) {
         // Cuts is a special case: give the desired signal efficienciy
         Bool_t passed = reader->EvaluateMVA( "CutsGA method", effS );
         if (passed) nSelCutsGA++;
      }

      if (Use["Likelihood"   ])   histLk     ->Fill( reader->EvaluateMVA( "Likelihood method"    ) );
      if (Use["LikelihoodD"  ])   histLkD    ->Fill( reader->EvaluateMVA( "LikelihoodD method"   ) );
      if (Use["LikelihoodPCA"])   histLkPCA  ->Fill( reader->EvaluateMVA( "LikelihoodPCA method" ) );
      if (Use["LikelihoodKDE"])   histLkKDE  ->Fill( reader->EvaluateMVA( "LikelihoodKDE method" ) );
      if (Use["LikelihoodMIX"])   histLkMIX  ->Fill( reader->EvaluateMVA( "LikelihoodMIX method" ) );
      if (Use["PDERS"        ])   histPD     ->Fill( reader->EvaluateMVA( "PDERS method"         ) );
      if (Use["PDERSD"       ])   histPDD    ->Fill( reader->EvaluateMVA( "PDERSD method"        ) );
      if (Use["PDERSPCA"     ])   histPDPCA  ->Fill( reader->EvaluateMVA( "PDERSPCA method"      ) );
      if (Use["KNN"          ])   histKNN    ->Fill( reader->EvaluateMVA( "KNN method"           ) );
      if (Use["HMatrix"      ])   histHm     ->Fill( reader->EvaluateMVA( "HMatrix method"       ) );
      if (Use["Fisher"       ])   histFi     ->Fill( reader->EvaluateMVA( "Fisher method"        ) );
      if (Use["FisherG"      ])   histFiG    ->Fill( reader->EvaluateMVA( "FisherG method"       ) );
      if (Use["BoostedFisher"])   histFiB    ->Fill( reader->EvaluateMVA( "BoostedFisher method" ) );
      if (Use["LD"           ])   histLD     ->Fill( reader->EvaluateMVA( "LD method"            ) );
      if (Use["MLP"          ])   histNn     ->Fill( reader->EvaluateMVA( "MLP method"           ) );
      if (Use["MLPBFGS"      ])   histNnbfgs ->Fill( reader->EvaluateMVA( "MLPBFGS method"       ) );
      if (Use["MLPBNN"       ])   histNnbnn  ->Fill( reader->EvaluateMVA( "MLPBNN method"        ) );
      if (Use["CFMlpANN"     ])   histNnC    ->Fill( reader->EvaluateMVA( "CFMlpANN method"      ) );
      if (Use["TMlpANN"      ])   histNnT    ->Fill( reader->EvaluateMVA( "TMlpANN method"       ) );
	   if (Use["BDT"          ]) {
	   BDTvalue = reader->EvaluateMVA( "BDT method"           );
		   histMattBdt    ->Fill( BDTvalue,DY_PtZ_weight*Trigweight*B2011PUweight );
		   histTMVABdt    ->Fill( BDTvalue,DY_PtZ_weight*Trigweight*B2011PUweight );
	   }
      if (Use["BDTD"         ])   histBdtD   ->Fill( reader->EvaluateMVA( "BDTD method"          ) );
      if (Use["BDTG"         ])   histBdtG   ->Fill( reader->EvaluateMVA( "BDTG method"          ) );
      if (Use["RuleFit"      ])   histRf     ->Fill( reader->EvaluateMVA( "RuleFit method"       ) );
      if (Use["SVM_Gauss"    ])   histSVMG   ->Fill( reader->EvaluateMVA( "SVM_Gauss method"     ) );
      if (Use["SVM_Poly"     ])   histSVMP   ->Fill( reader->EvaluateMVA( "SVM_Poly method"      ) );
      if (Use["SVM_Lin"      ])   histSVML   ->Fill( reader->EvaluateMVA( "SVM_Lin method"       ) );
      if (Use["FDA_MT"       ])   histFDAMT  ->Fill( reader->EvaluateMVA( "FDA_MT method"        ) );
      if (Use["FDA_GA"       ])   histFDAGA  ->Fill( reader->EvaluateMVA( "FDA_GA method"        ) );
      if (Use["Category"     ])   histCat    ->Fill( reader->EvaluateMVA( "Category method"      ) );
      if (Use["Plugin"       ])   histPBdt   ->Fill( reader->EvaluateMVA( "P_BDT method"         ) );

      // Retrieve also per-event error
      if (Use["PDEFoam"]) {
         Double_t val = reader->EvaluateMVA( "PDEFoam method" );
         Double_t err = reader->GetMVAError();
         histPDEFoam   ->Fill( val );
         histPDEFoamErr->Fill( err );         
         if (err>1.e-50) histPDEFoamSig->Fill( val/err );
      }         

      // Retrieve probability instead of MVA output
      if (Use["Fisher"])   {
         probHistFi  ->Fill( reader->GetProba ( "Fisher method" ) );
         rarityHistFi->Fill( reader->GetRarity( "Fisher method" ) );
      }
   
	  // std::cout << "Ht is "<< Ht << endl;
	   if(BDTvalue>-1.50){
NpassBDT++;
hMjj_OpenSelection->Fill(Hmass,DY_PtZ_weight*Trigweight*B2011PUweight );
hMmumu_OpenSelection->Fill(Emumass,DY_PtZ_weight*Trigweight*B2011PUweight );
hPtjj_OpenSelection->Fill(Hpt,DY_PtZ_weight*Trigweight*B2011PUweight );
hPtmumu_OpenSelection->Fill(Zpt,DY_PtZ_weight*Trigweight*B2011PUweight );
hCSV0_OpenSelection->Fill(CSV0,DY_PtZ_weight*Trigweight*B2011PUweight );
hCSV1_OpenSelection->Fill(CSV1,DY_PtZ_weight*Trigweight*B2011PUweight );
hdphiVH_OpenSelection->Fill(DeltaPhiHV,DY_PtZ_weight*Trigweight*B2011PUweight );
hdetaJJ_OpenSelection->Fill(DetaJJ,DY_PtZ_weight*Trigweight*B2011PUweight );
hUnweightedEta_OpenSelection->Fill(UnweightedEta,DY_PtZ_weight*Trigweight*B2011PUweight );
hPtmu0_OpenSelection->Fill(lep0pt,DY_PtZ_weight*Trigweight*B2011PUweight );
	   hHt_OpenSelection->Fill(Ht,DY_PtZ_weight*Trigweight*B2011PUweight );
	   hCircularity_OpenSelection->Fill(EvntShpCircularity,DY_PtZ_weight*Trigweight*B2011PUweight );
	   hCHFb0_OpenSelection->Fill(jetCHF0, DY_PtZ_weight*Trigweight*B2011PUweight );
	   hCHFb1_OpenSelection->Fill(jetCHF1, DY_PtZ_weight*Trigweight*B2011PUweight );
	   hPtbalZH_OpenSelection->Fill(PtbalZH, DY_PtZ_weight*Trigweight*B2011PUweight );
	   hPtmu1_OpenSelection->Fill(lep1pt, DY_PtZ_weight*Trigweight*B2011PUweight );
	   hPFRelIsomu0_OpenSelection->Fill(lep_pfCombRelIso0, DY_PtZ_weight*Trigweight*B2011PUweight );
	   hPFRelIsomu1_OpenSelection->Fill(lep_pfCombRelIso1, DY_PtZ_weight*Trigweight*B2011PUweight );
	   hNjets_OpenSelection->Fill(nJets, DY_PtZ_weight*Trigweight*B2011PUweight );
	   hRMSeta_OpenSelection->Fill(RMS_eta, DY_PtZ_weight*Trigweight*B2011PUweight );
	   hStaDeveta_OpenSelection->Fill(EtaStandDev, DY_PtZ_weight*Trigweight*B2011PUweight );
	   hdphiJJ_vect_OpenSelection->Fill(DphiJJ, DY_PtZ_weight*Trigweight*B2011PUweight );
	   hCentrality_OpenSelection->Fill(Centrality, DY_PtZ_weight*Trigweight*B2011PUweight );
	   hEventPt_OpenSelection->Fill(EventPt, DY_PtZ_weight*Trigweight*B2011PUweight );
	   hAngleEMU_OpenSelection->Fill(AngleEMU, DY_PtZ_weight*Trigweight*B2011PUweight );
	   hSphericity_OpenSelection->Fill(EvntShpSphericity, DY_PtZ_weight*Trigweight*B2011PUweight );
	   hAplanarity_OpenSelection->Fill(EvntShpAplanarity, DY_PtZ_weight*Trigweight*B2011PUweight );
	   hIsotropy_OpenSelection->Fill(EvntShpIsotropy, DY_PtZ_weight*Trigweight*B2011PUweight );
		   hDphiDetajj_OpenSelection->Fill(DphiJJ, DetaJJ, DY_PtZ_weight*Trigweight*B2011PUweight );
		   hMte_OpenSelection->Fill(Mte, DY_PtZ_weight*Trigweight*B2011PUweight );
		   hMtmu_OpenSelection->Fill(Mtmu, DY_PtZ_weight*Trigweight*B2011PUweight );
		   hdPhiHMET_OpenSelection->Fill(dPhiHMET, DY_PtZ_weight*Trigweight*B2011PUweight );
		   hDphiemu_OpenSelection->Fill(DeltaPhijetMETmin, DY_PtZ_weight*Trigweight*B2011PUweight );
		   hdelRjj_OpenSelection->Fill(delRjj, DY_PtZ_weight*Trigweight*B2011PUweight );
		   hdelRemu_OpenSelection->Fill(delRemu, DY_PtZ_weight*Trigweight*B2011PUweight );
		   hDphiZMET_OpenSelection->Fill(DphiZMET, DY_PtZ_weight*Trigweight*B2011PUweight );
		   hDphiemu_OpenSelection->Fill(Dphiemu, DY_PtZ_weight*Trigweight*B2011PUweight );
		   hDeltaPhijetMETmin_OpenSelection->Fill(DeltaPhijetMETmin, DY_PtZ_weight*Trigweight*B2011PUweight );
		   hAngleHemu_OpenSelection->Fill(AngleHemu, DY_PtZ_weight*Trigweight*B2011PUweight );
		   hProjVisT_OpenSelection->Fill(ProjVisT, DY_PtZ_weight*Trigweight*B2011PUweight );
		   htopMass_OpenSelection->Fill(topMass, DY_PtZ_weight*Trigweight*B2011PUweight );
		   htopPt_OpenSelection->Fill(topPt, DY_PtZ_weight*Trigweight*B2011PUweight );
		   hVMt_OpenSelection->Fill(Mt, DY_PtZ_weight*Trigweight*B2011PUweight );
		   hZmassSVD_OpenSelection->Fill(ZmassSVD, DY_PtZ_weight*Trigweight*B2011PUweight );
		   hZmassSVDnegSol_OpenSelection->Fill(ZmassSVDnegSol, DY_PtZ_weight*Trigweight*B2011PUweight );
		   hZmass_OpenSelection->Fill(Zmass, DY_PtZ_weight*Trigweight*B2011PUweight );
		   hZmassNegInclu_OpenSelection->Fill(ZmassNegInclu, DY_PtZ_weight*Trigweight*B2011PUweight );

	   }
	   treeWithBDT->Fill();
	   
   }//end event loop
   
   // Get elapsed time
   sw.Stop();
   std::cout << "--- End of event loop: "; sw.Print();
std::cout << "Number of Events: "<< Nevents << " Events passed BDT " << NpassBDT<< endl;
   // Get efficiency for cuts classifier
   
   if (Use["CutsGA"]) std::cout << "--- Efficiency for CutsGA method: " << double(nSelCutsGA)/theTree->GetEntries()
                                << " (for a required signal efficiency of " << effS << ")" << std::endl;

   if (Use["CutsGA"]) {

      // test: retrieve cuts for particular signal efficiency
      // CINT ignores dynamic_casts so we have to use a cuts-secific Reader function to acces the pointer  
      TMVA::MethodCuts* mcuts = reader->FindCutsMVA( "CutsGA method" ) ;

      if (mcuts) {      
         std::vector<Double_t> cutsMin;
         std::vector<Double_t> cutsMax;
         mcuts->GetCuts( 0.7, cutsMin, cutsMax );
         std::cout << "--- -------------------------------------------------------------" << std::endl;
         std::cout << "--- Retrieve cut values for signal efficiency of 0.7 from Reader" << std::endl;
         for (UInt_t ivar=0; ivar<cutsMin.size(); ivar++) {
            std::cout << "... Cut: " 
                      << cutsMin[ivar] 
                      << " < \"" 
                      << mcuts->GetInputVar(ivar)
                      << "\" <= " 
                      << cutsMax[ivar] << std::endl;
         }
         std::cout << "--- -------------------------------------------------------------" << std::endl;
      }
   }
	

   // --- Write histograms
   TFile *target  = new TFile( "BDTCut_DYPtZ_HF.root","RECREATE" );
   if (Use["Likelihood"   ])   histLk     ->Write();
   if (Use["LikelihoodD"  ])   histLkD    ->Write();
   if (Use["LikelihoodPCA"])   histLkPCA  ->Write();
   if (Use["LikelihoodKDE"])   histLkKDE  ->Write();
   if (Use["LikelihoodMIX"])   histLkMIX  ->Write();
   if (Use["PDERS"        ])   histPD     ->Write();
   if (Use["PDERSD"       ])   histPDD    ->Write();
   if (Use["PDERSPCA"     ])   histPDPCA  ->Write();
   if (Use["KNN"          ])   histKNN    ->Write();
   if (Use["HMatrix"      ])   histHm     ->Write();
   if (Use["Fisher"       ])   histFi     ->Write();
   if (Use["FisherG"      ])   histFiG    ->Write();
   if (Use["BoostedFisher"])   histFiB    ->Write();
   if (Use["LD"           ])   histLD     ->Write();
   if (Use["MLP"          ])   histNn     ->Write();
   if (Use["MLPBFGS"      ])   histNnbfgs ->Write();
   if (Use["MLPBNN"       ])   histNnbnn  ->Write();
   if (Use["CFMlpANN"     ])   histNnC    ->Write();
   if (Use["TMlpANN"      ])   histNnT    ->Write();
	if (Use["BDT"          ])  {
		histMattBdt    ->Write();
		histTMVABdt    ->Write();
	}
   if (Use["BDTD"         ])   histBdtD   ->Write();
   if (Use["BDTG"         ])   histBdtG   ->Write(); 
   if (Use["RuleFit"      ])   histRf     ->Write();
   if (Use["SVM_Gauss"    ])   histSVMG   ->Write();
   if (Use["SVM_Poly"     ])   histSVMP   ->Write();
   if (Use["SVM_Lin"      ])   histSVML   ->Write();
   if (Use["FDA_MT"       ])   histFDAMT  ->Write();
   if (Use["FDA_GA"       ])   histFDAGA  ->Write();
   if (Use["Category"     ])   histCat    ->Write();
   if (Use["Plugin"       ])   histPBdt   ->Write();

   // Write also error and significance histos
   if (Use["PDEFoam"]) { histPDEFoam->Write(); histPDEFoamErr->Write(); histPDEFoamSig->Write(); }

   // Write also probability hists
   if (Use["Fisher"]) { if (probHistFi != 0) probHistFi->Write(); if (rarityHistFi != 0) rarityHistFi->Write(); }
   
   hCHFb0_OpenSelection->Write();
   hCHFb1_OpenSelection->Write();
   hPtbalZH_OpenSelection->Write();
   hPtmu1_OpenSelection->Write();
   hPFRelIsomu0_OpenSelection->Write();
   hPFRelIsomu1_OpenSelection->Write();
   hMjj_OpenSelection->Write();
   hMmumu_OpenSelection->Write();
   hPtjj_OpenSelection->Write();
   hPtmumu_OpenSelection->Write();
   hCSV0_OpenSelection->Write();
   hCSV1_OpenSelection->Write();
   hdphiVH_OpenSelection->Write();
   hdetaJJ_OpenSelection->Write();
   hUnweightedEta_OpenSelection->Write();
   hPtmu0_OpenSelection->Write();
	hCircularity_OpenSelection->Write();
	hHt_OpenSelection->Write();
	hNjets_OpenSelection->Write();
	hRMSeta_OpenSelection->Write();
hStaDeveta_OpenSelection->Write();
hdphiJJ_vect_OpenSelection->Write();
hCentrality_OpenSelection->Write();
hEventPt_OpenSelection->Write();
hAngleEMU_OpenSelection->Write();
hSphericity_OpenSelection->Write();
hAplanarity_OpenSelection->Write();
hIsotropy_OpenSelection->Write();
hDphiDetajj_OpenSelection->Write();
hMtmu_OpenSelection->Write();
hMte_OpenSelection->Write();
hdPhiHMET_OpenSelection->Write();
hDphiemu_OpenSelection->Write();
hdelRjj_OpenSelection->Write();
hdelRemu_OpenSelection->Write();
hDphiZMET_OpenSelection->Write();
hDeltaPhijetMETmin_OpenSelection->Write();
hAngleHemu_OpenSelection->Write();
hProjVisT_OpenSelection->Write();
htopMass_OpenSelection->Write();
htopPt_OpenSelection->Write();
hVMt_OpenSelection->Write();
hZmassSVD_OpenSelection->Write();
hZmassSVDnegSol_OpenSelection->Write();
hZmass_OpenSelection->Write();
hZmassNegInclu_OpenSelection->Write();

treeWithBDT->Write();

   
   target->Close();

  
   delete reader;

	hCHFb0_OpenSelection->Delete();
	hCHFb1_OpenSelection->Delete();
	hPtbalZH_OpenSelection->Delete();
	hPtmu1_OpenSelection->Delete();
	hPFRelIsomu0_OpenSelection->Delete();
	hPFRelIsomu1_OpenSelection->Delete();
	hMjj_OpenSelection->Delete();
	hMmumu_OpenSelection->Delete();
	hPtjj_OpenSelection->Delete();
	hPtmumu_OpenSelection->Delete();
	hCSV0_OpenSelection->Delete();
	hCSV1_OpenSelection->Delete();
	hdphiVH_OpenSelection->Delete();
	hdetaJJ_OpenSelection->Delete();
	hUnweightedEta_OpenSelection->Delete();
	hPtmu0_OpenSelection->Delete();
	hCircularity_OpenSelection->Delete();
	hHt_OpenSelection->Delete();
	hNjets_OpenSelection->Delete();
	hRMSeta_OpenSelection->Delete();
	hStaDeveta_OpenSelection->Delete();
	hdphiJJ_vect_OpenSelection->Delete();
	hCentrality_OpenSelection->Delete();
	hEventPt_OpenSelection->Delete();
	hAngleEMU_OpenSelection->Delete();
	hSphericity_OpenSelection->Delete();
	hAplanarity_OpenSelection->Delete();
	hIsotropy_OpenSelection->Delete();
	hDphiDetajj_OpenSelection->Delete();
	hMtmu_OpenSelection->Delete();
	hMte_OpenSelection->Delete();
	hdPhiHMET_OpenSelection->Delete();
	hDphiemu_OpenSelection->Delete();
	hdelRjj_OpenSelection->Delete();
	hdelRemu_OpenSelection->Delete();
	hDphiZMET_OpenSelection->Delete();
	hDeltaPhijetMETmin_OpenSelection->Delete();
	hAngleHemu_OpenSelection->Delete();
	hProjVisT_OpenSelection->Delete();
	htopMass_OpenSelection->Delete();
	htopPt_OpenSelection->Delete();
	hVMt_OpenSelection->Delete();
	hZmassSVD_OpenSelection->Delete();
	hZmassSVDnegSol_OpenSelection->Delete();
	hZmass_OpenSelection->Delete();
	hZmassNegInclu_OpenSelection->Delete();
	

	if (Use["BDT"          ])  {
		histMattBdt    ->Delete();
		histTMVABdt    ->Delete();
	}
	
	

treeWithBDT->Delete();

	
    
   std::cout << "==> DYPtZ_HF_BDTCut is done!" << endl << std::endl;
   
   gROOT->ProcessLine(".q");
} 
コード例 #29
0
ファイル: MVA.C プロジェクト: amanjong/AnalysisCMS
//------------------------------------------------------------------------------
// MVARead
//------------------------------------------------------------------------------
void MVARead(TString signal, TString filename)
{
  TMVA::Reader* reader = new TMVA::Reader("!Color:!Silent");   

  //  float channel;
  float metPfType1;
  float m2l;
  //  float njet;
  //  float nbjet20cmvav2l;
  float lep1pt;
  float lep2pt;
  //  float jet1pt;
  float jet2pt;
  float mtw1;
  float dphill;
  float dphilep1jet1;
  //  float dphilep1jet2;
  //  float dphilmet1;
  //  float dphilep2jet1;
  //  float dphilep2jet2;
  //  float dphilmet2;
  //  float dphijj;
  //  float dphijet1met;
  //  float dphijet2met;
  float dphillmet;
  float eventW;
  float mva; 

  //  reader->AddVariable("channel",        &channel);
  reader->AddVariable("metPfType1",     &metPfType1);
  reader->AddVariable("m2l",            &m2l);
  //  reader->AddVariable("njet",           &njet);
  //  reader->AddVariable("nbjet20cmvav2l", &nbjet20cmvav2l);
  reader->AddVariable("lep1pt",         &lep1pt);
  reader->AddVariable("lep2pt",         &lep2pt);
  //  reader->AddVariable("jet1pt",         &jet1pt);
  reader->AddVariable("jet2pt",         &jet2pt);
  reader->AddVariable("mtw1",           &mtw1);
  reader->AddVariable("dphill",         &dphill);
  reader->AddVariable("dphilep1jet1",   &dphilep1jet1);
  //  reader->AddVariable("dphilep1jet2",   &dphilep1jet2);
  //  reader->AddVariable("dphilmet1",      &dphilmet1);
  //  reader->AddVariable("dphilep2jet1",   &dphilep2jet1);
  //  reader->AddVariable("dphilep2jet2",   &dphilep2jet2);
  //  reader->AddVariable("dphilmet2",      &dphilmet2);
  //  reader->AddVariable("dphijj",         &dphijj);
  //  reader->AddVariable("dphijet1met",    &dphijet1met);
  //  reader->AddVariable("dphijet2met",    &dphijet2met);
  reader->AddVariable("dphillmet",      &dphillmet);


  // Book MVA methods
  //----------------------------------------------------------------------------
  reader->BookMVA("MLP", weightsdir + signal + "_MLP.weights.xml");

  // Get MVA response
  //----------------------------------------------------------------------------
  TH1D* h_mva = new TH1D("h_mva_" + signal, "", 100, -0.05, 1.05);

  TFile* input = TFile::Open(inputdir + filename + ".root", "update");

  TTree* theTree = (TTree*)input->Get("latino");

  TBranch* b_mva = theTree->Branch("mva_" + signal, &mva, "mva/F" );

  //  theTree->SetBranchAddress("channel",        &channel);
  theTree->SetBranchAddress("metPfType1",     &metPfType1);
  theTree->SetBranchAddress("m2l",            &m2l);
  //  theTree->SetBranchAddress("njet",           &njet);
  //  theTree->SetBranchAddress("nbjet20cmvav2l", &nbjet20cmvav2l);
  theTree->SetBranchAddress("lep1pt",         &lep1pt);
  theTree->SetBranchAddress("lep2pt",         &lep2pt);
  //  theTree->SetBranchAddress("jet1pt",         &jet1pt);
  theTree->SetBranchAddress("jet2pt",         &jet2pt);
  theTree->SetBranchAddress("mtw1",           &mtw1);
  theTree->SetBranchAddress("dphill",         &dphill);
  theTree->SetBranchAddress("dphilep1jet1",   &dphilep1jet1);
  //  theTree->SetBranchAddress("dphilep1jet2",   &dphilep1jet2);
  //  theTree->SetBranchAddress("dphilmet1",      &dphilmet1);
  //  theTree->SetBranchAddress("dphilep2jet1",   &dphilep2jet1);
  //  theTree->SetBranchAddress("dphilep2jet2",   &dphilep2jet2);
  //  theTree->SetBranchAddress("dphilmet2",      &dphilmet2);
  //  theTree->SetBranchAddress("dphijj",         &dphijj);
  //  theTree->SetBranchAddress("dphijet1met",    &dphijet1met);
  //  theTree->SetBranchAddress("dphijet2met",    &dphijet2met);
  theTree->SetBranchAddress("dphillmet",      &dphillmet);
  theTree->SetBranchAddress("eventW",         &eventW);

  Long64_t nentries = theTree->GetEntries();

  for (Long64_t ievt=0; ievt<nentries; ievt++) {

    theTree->GetEntry(ievt);

    mva = reader->EvaluateMVA("MLP");

    b_mva->Fill();

    // Comentado por Alberto Manjon
    //if (njet > 1) h_mva->Fill(mva, eventW);
    h_mva->Fill(mva, eventW);
  }


  // Save
  //----------------------------------------------------------------------------
  theTree->Write("", TObject::kOverwrite);

  input->Close();

  TFile* target = TFile::Open(applicationdir + signal + "__" + filename + ".root", "recreate");

  h_mva->Write();
  
  target->Close();

  h_mva->Delete();

  delete reader;
}
コード例 #30
0
ファイル: computeBDT.C プロジェクト: dimatteo/MitMonoJet
void computeBDT(std::string iName="../samples/boostedv-v8_s12-zll-ptz100-v7c_noskim_flatntuple.root",
              std::string iWeightFile="weights/TMVAClassificationCategory_BDT_simple_alpha.weights.xml") { 
  TMVA::Tools::Instance();
  TMVA::Reader *reader = new TMVA::Reader( "!Color:!Silent" );    

  float jet1QGtagComb = 0.; reader->AddVariable("2*fjet1QGtagSub2+fjet1QGtagSub1",&jet1QGtagComb);
  float fjet1QGtagSub1 = 0.; reader->AddVariable("fjet1QGtagSub1",&fjet1QGtagSub1);
  float fjet1QGtagSub2 = 0.; reader->AddVariable("fjet1QGtagSub2",&fjet1QGtagSub2);
  float fjet1QGtag = 0.; reader->AddVariable("fjet1QGtag",&fjet1QGtag);
  float fjet1PullAngle = 0.; reader->AddVariable("fjet1PullAngle",&fjet1PullAngle);
  float fjet1Pull = 0.; reader->AddVariable("fjet1Pull",&fjet1Pull);
  float fjet1MassTrimmed = 0.; reader->AddVariable("fjet1MassTrimmed",&fjet1MassTrimmed);
  float fjet1MassPruned = 0.; reader->AddVariable("fjet1MassPruned",&fjet1MassPruned);
  float fjet1MassSDbm1 = 0.; reader->AddVariable("fjet1MassSDbm1",&fjet1MassSDbm1);
  float fjet1MassSDb2 = 0.; reader->AddVariable("fjet1MassSDb2",&fjet1MassSDb2);
  float fjet1MassSDb0 = 0.; reader->AddVariable("fjet1MassSDb0",&fjet1MassSDb0);
  float fjet1QJetVol = 0.; reader->AddVariable("fjet1QJetVol",&fjet1QJetVol);
  float fjet1C2b2 = 0.; reader->AddVariable("fjet1C2b2",&fjet1C2b2);
  float fjet1C2b1 = 0.; reader->AddVariable("fjet1C2b1",&fjet1C2b1);
  float fjet1C2b0p5 = 0.; reader->AddVariable("fjet1C2b0p5",&fjet1C2b0p5);
  float fjet1C2b0p2 = 0.; reader->AddVariable("fjet1C2b0p2",&fjet1C2b0p2);
  float fjet1C2b0 = 0.; reader->AddVariable("fjet1C2b0",&fjet1C2b0);
  float fjet1Tau2 = 0.; reader->AddVariable("fjet1Tau2",&fjet1Tau2);
  float fjet1Tau1 = 0.; reader->AddVariable("fjet1Tau1",&fjet1Tau1);
  float tau2tau1 = 0.; reader->AddVariable("fjet1Tau2/fjet1Tau1",&tau2tau1);

  std::string lJetName = "BDT";
  reader->BookMVA(lJetName .c_str(),iWeightFile.c_str());
  
  TFile *lFile = new TFile(iName.c_str());
  TTree *lTree = (TTree*) lFile->FindObjectAny("DMSTree");
  TString pExpress0 = "2*fjet1QGtagSub2+fjet1QGtagSub1";
  TString pExpr0(pExpress0);
  TTreeFormula* lFVars0 = new TTreeFormula(pExpr0,pExpr0,lTree);
  TString pExpress1 = "fjet1QGtagSub1";
  TString pExpr1(pExpress1);
  TTreeFormula* lFVars1 = new TTreeFormula(pExpr1,pExpr1,lTree);
  TString pExpress2 = "fjet1QGtagSub2";
  TString pExpr2(pExpress2);
  TTreeFormula* lFVars2 = new TTreeFormula(pExpr2,pExpr2,lTree);
  TString pExpress3 = "fjet1QGtag";
  TString pExpr3(pExpress3);
  TTreeFormula* lFVars3 = new TTreeFormula(pExpr3,pExpr3,lTree);
  TString pExpress4 = "fjet1PullAngle";
  TString pExpr4(pExpress4);
  TTreeFormula* lFVars4 = new TTreeFormula(pExpr4,pExpr4,lTree);
  TString pExpress5 = "fjet1Pull";
  TString pExpr5(pExpress5);
  TTreeFormula* lFVars5 = new TTreeFormula(pExpr5,pExpr5,lTree);
  TString pExpress6 = "fjet1MassTrimmed";
  TString pExpr6(pExpress6);
  TTreeFormula* lFVars6 = new TTreeFormula(pExpr6,pExpr6,lTree);
  TString pExpress7 = "fjet1MassPruned";
  TString pExpr7(pExpress7);
  TTreeFormula* lFVars7 = new TTreeFormula(pExpr7,pExpr7,lTree);
  TString pExpress8 = "fjet1MassSDbm1";
  TString pExpr8(pExpress8);
  TTreeFormula* lFVars8 = new TTreeFormula(pExpr8,pExpr8,lTree);
  TString pExpress9 = "fjet1MassSDb2";
  TString pExpr9(pExpress9);
  TTreeFormula* lFVars9 = new TTreeFormula(pExpr9,pExpr9,lTree);
  TString pExpress10 = "fjet1MassSDb0";
  TString pExpr10(pExpress10);
  TTreeFormula* lFVars10 = new TTreeFormula(pExpr10,pExpr10,lTree);
  TString pExpress11 = "fjet1QJetVol";
  TString pExpr11(pExpress11);
  TTreeFormula* lFVars11 = new TTreeFormula(pExpr11,pExpr11,lTree);
  TString pExpress12 = "fjet1C2b2";
  TString pExpr12(pExpress12);
  TTreeFormula* lFVars12 = new TTreeFormula(pExpr12,pExpr12,lTree);
  TString pExpress13 = "fjet1C2b1";
  TString pExpr13(pExpress13);
  TTreeFormula* lFVars13 = new TTreeFormula(pExpr13,pExpr13,lTree);
  TString pExpress14 = "fjet1C2b0p5";
  TString pExpr14(pExpress14);
  TTreeFormula* lFVars14 = new TTreeFormula(pExpr14,pExpr14,lTree);
  TString pExpress15 = "fjet1C2b0p2";
  TString pExpr15(pExpress15);
  TTreeFormula* lFVars15 = new TTreeFormula(pExpr15,pExpr15,lTree);
  TString pExpress16 = "fjet1C2b0";
  TString pExpr16(pExpress16);
  TTreeFormula* lFVars16 = new TTreeFormula(pExpr16,pExpr16,lTree);
  TString pExpress17 = "fjet1Tau2";
  TString pExpr17(pExpress17);
  TTreeFormula* lFVars17 = new TTreeFormula(pExpr17,pExpr17,lTree);
  TString pExpress18 = "fjet1Tau1";
  TString pExpr18(pExpress18);
  TTreeFormula* lFVars18 = new TTreeFormula(pExpr18,pExpr18,lTree);
  TString pExpress19 = "fjet1Tau2/fjet1Tau1";
  TString pExpr19(pExpress19);
  TTreeFormula* lFVars19 = new TTreeFormula(pExpr19,pExpr19,lTree);

  //lTree->SetBranchAddress( "jet1mprune"         , &lJP);
  //lTree->SetBranchAddress( iVar1.c_str()           , &lJT1);
  //if(iVar1 != iVar2) lTree->SetBranchAddress( iVar2.c_str()           , &lJT2); 

  int lNEvents = lTree->GetEntries();
  TFile *lOFile = new TFile("Output.root","RECREATE");
  TTree *lOTree = new TTree("DMSTree","DMSTree");
  float lMVA    = 0; lOTree->Branch("bdt_all",&lMVA ,"bdt_all/F");
  for (Long64_t i0=0; i0<lNEvents;i0++) {
    if (i0 % 10000 == 0) std::cout << "--- ... Processing event: " << double(i0)/double(lNEvents) << std::endl;
    lTree->GetEntry(i0);
    jet1QGtagComb = lFVars0->EvalInstance();
    fjet1QGtagSub1 = lFVars1->EvalInstance();
    fjet1QGtagSub2 = lFVars2->EvalInstance();
    fjet1QGtag = lFVars3->EvalInstance();
    fjet1PullAngle = lFVars4->EvalInstance();
    fjet1Pull = lFVars5->EvalInstance();
    fjet1MassTrimmed = lFVars6->EvalInstance();
    fjet1MassPruned = lFVars7->EvalInstance();
    fjet1MassSDbm1 = lFVars8->EvalInstance();
    fjet1MassSDb2 = lFVars9->EvalInstance();
    fjet1MassSDb0 = lFVars10->EvalInstance();
    fjet1QJetVol = lFVars11->EvalInstance();
    fjet1C2b2 = lFVars12->EvalInstance();
    fjet1C2b1 = lFVars13->EvalInstance();
    fjet1C2b0p5 = lFVars14->EvalInstance();
    fjet1C2b0p2 = lFVars15->EvalInstance();
    fjet1C2b0 = lFVars16->EvalInstance();
    fjet1Tau2 = lFVars17->EvalInstance();
    fjet1Tau1 = lFVars18->EvalInstance();
    tau2tau1 = lFVars19->EvalInstance();

    lMVA      = float(reader->EvaluateMVA(lJetName.c_str()));
    lOTree->Fill();
  }
  lOTree->Write();
  lOFile->Close();
  delete reader;
}