Esempio n. 1
0
Float_t hhMVA::GetBDTValue(Float_t mTT, Float_t ptTT, Float_t mBB, Float_t ptBB,
			   Float_t mHH, Float_t ptHH, Float_t mt2,
			   Float_t dRbb, Float_t dRtt, Float_t dRhh) {
  
  if (!fTMVAReader) {
    cout << "TMVA reader not initialized properly" << endl;
    return -999;
  }

  fMVAVar_mTT   =mTT;
  fMVAVar_ptTT  =ptTT;
  fMVAVar_mBB1  =mBB;
  fMVAVar_ptBB1 =ptBB;
  fMVAVar_mHH   =mHH;
  fMVAVar_ptHH  =ptHH;
  fMVAVar_mt2   =mt2;
  fMVAVar_dRbb  =dRbb;
  fMVAVar_dRtt  =dRtt;
  fMVAVar_dRhh  =dRhh;

  TMVA::Reader *reader = 0;
  reader = fTMVAReader;
  
  return reader->EvaluateMVA("BDT method");
  
}
Esempio n. 2
0
int reader_wrapper::GetEntry(Long64_t e) {
  /// don't care about spectators here
  for (auto b: m_branches) {
    b->GetEntry(e);
  }
  for (auto& v : m_variables) {
    v.value = v.ttreeformula->EvalInstance();
  }
  m_response = m_reader->EvaluateMVA(m_methodName.Data());
  m_responseBranch->Fill();
  return 0;
}
Esempio n. 3
0
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;
}
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;

}
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;
} 
Esempio n. 6
0
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;
  } 
}
Esempio n. 7
0
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;
}
Esempio n. 8
0
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;
}
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();

} 
Esempio n. 10
0
void TMVAReaderPracticeDT0818() {
	
  bool isTT=0;
    
	string  masspoint[13]={"600","800","1000","1200","1400","1600","1800","2000","2500","3000","3500","4000","4500"};

	//for (int massP=0;massP<13;massP++){
	 for (int massP=0;massP<1;massP++){
		
		//Sig
	   //  TString endfix =Form("treeV4DT/signal-%s.root",masspoint[massP].data());for(int w=1;w<2;w++){f = TFile::Open(Form("/data2/syu/13TeV/ZprimeZhbb/ZprimeToZhToZlephbb_narrow_M-%s_13TeV-madgraph.root",masspoint[massP].data()));if (!f || !f->IsOpen())continue;TDirectory * dir = (TDirectory*)f->Get(Form("/data2/syu/13TeV/ZprimeZhbb/ZprimeToZhToZlephbb_narrow_M-%s_13TeV-madgraph.root:/tree",masspoint[massP].data()));dir->GetObject("treeMaker",tree);


	  //DY100-200
  
	   //for(int w=1;w<90;w++){ f = TFile::Open(Form("/data7/khurana/NCUGlobalTuples/SPRING15/DYJetsHTBins25nsSamples/DYJetsToLL_M-50_HT-100to200_TuneCUETP8M1_13TeV-madgraphMLM-pythia8/crab_DYJetsToLL_M-50_HT-100to200_TuneCUETP8M1_13TeV-madgraphMLM-pythia8_0803/150812_162742/0000/NCUGlobalTuples_%d.root",w));if (!f || !f->IsOpen())continue;TDirectory * dir = (TDirectory*)f->Get(Form("/data7/khurana/NCUGlobalTuples/SPRING15/DYJetsHTBins25nsSamples/DYJetsToLL_M-50_HT-100to200_TuneCUETP8M1_13TeV-madgraphMLM-pythia8/crab_DYJetsToLL_M-50_HT-100to200_TuneCUETP8M1_13TeV-madgraphMLM-pythia8_0803/150812_162742/0000/NCUGlobalTuples_%d.root:/tree",w));   dir->GetObject("treeMaker",tree);TString endfix =Form("treeV4DT/DYHT100-%d.root",w);

    //DY200-400
 
	 
	   //  for(int w=1;w<45;w++){f = TFile::Open(Form("/data7/khurana/NCUGlobalTuples/SPRING15/DYJetsHTBins25nsSamples/DYJetsToLL_M-50_HT-200to400_TuneCUETP8M1_13TeV-madgraphMLM-pythia8/crab_DYJetsToLL_M-50_HT-200to400_TuneCUETP8M1_13TeV-madgraphMLM-pythia8_0803/150812_162821/0000/NCUGlobalTuples_%d.root",w));if (!f || !f->IsOpen())continue;TDirectory * dir = (TDirectory*)f->Get(Form("/data7/khurana/NCUGlobalTuples/SPRING15/DYJetsHTBins25nsSamples/DYJetsToLL_M-50_HT-200to400_TuneCUETP8M1_13TeV-madgraphMLM-pythia8/crab_DYJetsToLL_M-50_HT-200to400_TuneCUETP8M1_13TeV-madgraphMLM-pythia8_0803/150812_162821/0000/NCUGlobalTuples_%d.root:/tree",w));   dir->GetObject("treeMaker",tree);TString endfix =Form("treeV4DT/DYHT200-%d.root",w);


    //DY400-600

	   //  for(int w=1;w<45;w++){f = TFile::Open(Form("/data7/khurana/NCUGlobalTuples/SPRING15/DYJetsHTBins25nsSamples/DYJetsToLL_M-50_HT-400to600_TuneCUETP8M1_13TeV-madgraphMLM-pythia8/crab_DYJetsToLL_M-50_HT-400to600_TuneCUETP8M1_13TeV-madgraphMLM-pythia8_0803/150812_162858/0000/NCUGlobalTuples_%d.root",w));if (!f || !f->IsOpen())continue;TDirectory * dir = (TDirectory*)f->Get(Form("/data7/khurana/NCUGlobalTuples/SPRING15/DYJetsHTBins25nsSamples/DYJetsToLL_M-50_HT-400to600_TuneCUETP8M1_13TeV-madgraphMLM-pythia8/crab_DYJetsToLL_M-50_HT-400to600_TuneCUETP8M1_13TeV-madgraphMLM-pythia8_0803/150812_162858/0000/NCUGlobalTuples_%d.root:/tree",w));   dir->GetObject("treeMaker",tree);TString endfix =Form("treeV4DT/DYHT400-%d.root",w);


    //DY600-inf
   
	   //  for(int w=1;w<48;w++){f = TFile::Open(Form("/data7/khurana/NCUGlobalTuples/SPRING15/DYJetsHTBins25nsSamples/DYJetsToLL_M-50_HT-600toInf_TuneCUETP8M1_13TeV-madgraphMLM-pythia8/crab_DYJetsToLL_M-50_HT-600toInf_TuneCUETP8M1_13TeV-madgraphMLM-pythia8_0803/150812_162937/0000/NCUGlobalTuples_%d.root",w));if (!f || !f->IsOpen())continue;TDirectory * dir = (TDirectory*)f->Get(Form("/data7/khurana/NCUGlobalTuples/SPRING15/DYJetsHTBins25nsSamples/DYJetsToLL_M-50_HT-600toInf_TuneCUETP8M1_13TeV-madgraphMLM-pythia8/crab_DYJetsToLL_M-50_HT-600toInf_TuneCUETP8M1_13TeV-madgraphMLM-pythia8_0803/150812_162937/0000/NCUGlobalTuples_%d.root:/tree",w));   dir->GetObject("treeMaker",tree);TString endfix =Form("treeV4DT/DYHT600-%d.root",w);


//tt
	     isTT=1; for (int w=1;w<174;w++){f = TFile::Open(Form("/data7/khurana/NCUGlobalTuples/SPRING15/crab_TT_TuneCUETP8M1_13TeV-powheg-pythia8_0803/150803_175618/0000/NCUGlobalTuples_%d.root",w));  if (!f || !f->IsOpen())continue;TDirectory * dir = (TDirectory*)f->Get(Form("/data7/khurana/NCUGlobalTuples/SPRING15/crab_TT_TuneCUETP8M1_13TeV-powheg-pythia8_0803/150803_175618/0000/NCUGlobalTuples_%d.root:/tree",w));    dir->GetObject("treeMaker",tree);TString endfix =Form("treeV4DT/TT-%d.root",w);


      // TFile *fw;
      // TTree* treeP;
      // if(w==1){fw = new TFile("tree/DY.root","recreate");
      // 	 treeP=new TTree("treeP","treeP");
      // 	}
      // else {fw = new TFile("tree/DY.root","update");
      // 	treeP =(TTree*)fw->FindObjectAny("treeP");}
		   
       TFile *fw=new TFile(endfix.Data(),"recreate");
       
      float fatPt;
      float fatCSV;
      float sub1Pt;
      
      float sub1CSV;
      float sub2Pt;
     
      float sub2CSV;
      float delta_R;
      float tau21;
      float tau1;
      float tau2;
      
      cout<<massP<<","<<w<<endl;
      TreeReader data(tree);
      //data.Print();
      Long64_t nentries = data.GetEntriesFast();

      TMVA::Reader *reader = new TMVA::Reader( "!Color:!Silent" );
      reader->AddVariable( "fatPt", &fatPt );
      reader->AddVariable( "fatCSV", &fatCSV );
      reader->AddVariable( "sub1Pt", &sub1Pt );
      reader->AddVariable( "sub1CSV", &sub1CSV );
      reader->AddVariable( "sub2Pt", &sub2Pt );
      reader->AddVariable( "sub2CSV", &sub2CSV );
      reader->AddVariable( "delta_R", &delta_R );
      reader->AddVariable( "tau21", &tau21 );
      reader->AddVariable( "tau1", &tau1 );
      reader->AddVariable( "tau2", &tau2 );

      reader->BookMVA("BDT method","weights/DT.xml"); 
      TH1F *th1 =new TH1F("BDT","BDT",100,-0.8,0.8);
		  
		  for (Long64_t jentry=0; jentry<nentries;jentry++) {
		    data.GetEntry(jentry);
		    Int_t FATnJet=data.GetInt("FATnJet");
		    if(FATnJet==0)continue;

		    TClonesArray* eleP4 = (TClonesArray*) data.GetPtrTObject("eleP4");
		    TClonesArray* muP4 = (TClonesArray*) data.GetPtrTObject("muP4");
		    vector<bool> eleIsPassVeto=  *((vector<bool>*) data.GetPtr("eleIsPassVeto"));
		    vector<bool> isLooseMuon=  *((vector<bool>*) data.GetPtr("isLooseMuon"));
		    Int_t nMu=data.GetInt("nMu");
		    Int_t nEle=data.GetInt("nEle");
		    vector<int> mus,eles;
		    for(int i=0;i<nEle;i++){
		      TLorentzVector* thisEle =(TLorentzVector*)eleP4->At(i) ;
		      if(thisEle->Pt()<10 ||fabs(thisEle->Eta())>2.5 || eleIsPassVeto[i]==0 )continue;
		      eles.push_back(i);
		    }
		    
		     for(int i=0;i<nMu;i++){
		       TLorentzVector* thisMu =(TLorentzVector*)muP4->At(i) ;
		       if(thisMu->Pt()<10 ||fabs(thisMu->Eta())>2.4 || isLooseMuon[i]==0 )continue;
		       mus.push_back(i);
		     }
		      
		    
		    TClonesArray* FATjetP4 = (TClonesArray*) data.GetPtrTObject("FATjetP4");
		    Float_t*  FATjetSDmass = data.GetPtrFloat("FATjetSDmass");
		    Float_t*  FATjetCISVV2 = data.GetPtrFloat("FATjetCISVV2");


		    //if(>1 )continue;    
		    
		    int FATi=0;
		    bool isFAT=0;
		    TLorentzVector* FATjetP4_1;
		    //cout<<FATnJet<<endl;
		    for (FATi=0;FATi<FATnJet;FATi++){
		      //cout<<"FATi="<<FATi<<endl;
		      if(FATjetCISVV2[FATi]<0 ||FATjetCISVV2[FATi]>1 )continue;
		      FATjetP4_1 = (TLorentzVector*)FATjetP4->At(FATi);
		      bool isOverlap=0;
		      for(int i=0;i<mus.size();i++){
			TLorentzVector* thisMu =(TLorentzVector*)muP4->At(mus[i]) ;
			if(FATjetP4_1->DeltaR(*thisMu)<0.8){
			  isOverlap=1;
			  break;
			}
		      }
		      if(!isOverlap){
			for(int i=0;i<eles.size();i++){
			  TLorentzVector* thisEle =(TLorentzVector*)eleP4->At(eles[i]) ;
			  if(FATjetP4_1->DeltaR(*thisEle)<0.8){
			    isOverlap=1;
			    break;
			  }
			}
			
		      }
		      if(isOverlap)continue;
		      
		      isFAT=1;
		    break;
		    }
		    if(!isFAT)continue;
		    
		    if(FATjetP4_1->Pt()<200)continue;
		    
		    Int_t* FATnSubSDJet=data.GetPtrInt("FATnSubSDJet");
		    
		    if(FATnSubSDJet[FATi]<2)continue;
		    
		    int nsub=FATnSubSDJet[FATi];
		    if(nsub!=2)cout<<"nsub="<<nsub<<endl;if(nsub!=2)cout<<"nsub="<<nsub<<endl;
		    //
		    
		    float* FATjetTau1=data.GetPtrFloat("FATjetTau1");
		    float* FATjetTau2=data.GetPtrFloat("FATjetTau2");
		    vector<float>   *FATsubjetSDCSV =  data.GetPtrVectorFloat("FATsubjetSDCSV");
		    vector<float>   *FATsubjetSDPx =  data.GetPtrVectorFloat("FATsubjetSDPx");
		    vector<float>   *FATsubjetSDPy =  data.GetPtrVectorFloat("FATsubjetSDPy");
		    vector<float>   *FATsubjetSDPz =  data.GetPtrVectorFloat("FATsubjetSDPz");
		    vector<float>   *FATsubjetSDCE =  data.GetPtrVectorFloat("FATsubjetSDCE");
		    
		 	

		    int subi=0,subj=0;
		    bool isSubi=0,isSubj=0;
		    
	         
		    for(int subij=0;subij<FATnSubSDJet[FATi];subij++){
		      if(FATsubjetSDCSV[FATi][subij]>1 ||FATsubjetSDCSV[FATi][subij]<0 )continue;
		      if(!isSubi && !isSubj){
			subi=subij;isSubi=1;continue;
		      }
		      if(isSubi && !isSubj){
			subj=subij;isSubj=1;break;
		      }
		    }
		    //
		    if (!isSubi || !isSubj)continue;
		    
		    TLorentzVector  FATsubjet_1,FATsubjet_2;
		    
		    
		    FATsubjet_1.SetPxPyPzE(FATsubjetSDPx[FATi][subi],FATsubjetSDPy[FATi][subi],FATsubjetSDPz[FATi][subi],FATsubjetSDCE[FATi][subi]);
	            FATsubjet_2.SetPxPyPzE(FATsubjetSDPx[FATi][subj],FATsubjetSDPy[FATi][subj],FATsubjetSDPz[FATi][subj],FATsubjetSDCE[FATi][subj]);
	           
		    
		    fatPt=FATjetP4_1->Pt();//BfatPt->Fill();
		    fatCSV=FATjetCISVV2[FATi];//BfatCSV->Fill();
		    sub1Pt=FATsubjet_1.Pt();//Bsub1Pt->Fill();
		    //sub1Eta=FATsubjet_1.Eta();//Bsub1Eta->Fill();
		    sub1CSV=FATsubjetSDCSV[FATi][subi];//Bsub1CSV->Fill();
		    sub2Pt=FATsubjet_2.Pt();//Bsub2Pt->Fill();
		    //sub2Eta=FATsubjet_2.Eta();//Bsub2Eta->Fill();
		    sub2CSV=FATsubjetSDCSV[FATi][subj];//Bsub2CSV->Fill();
		    delta_R=FATsubjet_1.DeltaR(FATsubjet_2);//BdeltaR->Fill();
		    tau21=FATjetTau2[FATi]/FATjetTau1[FATi];
		    tau1=FATjetTau1[FATi];
		    tau2=FATjetTau2[FATi];
		    
		    
		    th1 ->Fill( reader->EvaluateMVA("BDT method"));
		   
		  }
		  th1->Write();
		  
		  fw->Close();
		  
		}	
		
		
    }	    
    
}
void cutFlowStudyMu( TString weightFile = "TMVAClassificationPtOrd_qqH115vsWZttQCD_Cuts.weights.xml",
		     Double_t effS_ = 0.3) 
{
  
  ofstream out("cutFlow-MuTauStream.txt");
  out.precision(4);

  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("/home/llr/cms/lbianchini/CMSSW_3_9_9/src/Bianchi/TauTauStudies/test/Macro/weights/")+weightFile ); 
 
  TFile *fFullSignalVBF           = new TFile("/data_CMS/cms/lbianchini//MuTauStream2011/treeMuTauStream_VBFH115-Mu-powheg-PUS1.root","READ"); 
  TFile *fFullSignalGGH           = new TFile("/data_CMS/cms/lbianchini//MuTauStream2011/treeMuTauStream_GGFH115-Mu-powheg-PUS1.root","READ");  
  TFile *fFullBackgroundDYTauTau  = new TFile("/data_CMS/cms/lbianchini//MuTauStream2011/treeMuTauStream_DYToTauTau-Mu-20-PUS1.root","READ"); 
  TFile *fFullBackgroundDYMuMu    = new TFile("/data_CMS/cms/lbianchini//MuTauStream2011/treeMuTauStream_DYToMuMu-20-PUS1.root","READ"); 
  TFile *fFullBackgroundWJets     = new TFile("/data_CMS/cms/lbianchini//MuTauStream2011/treeMuTauStream_WJets-Mu-madgraph-PUS1.root","READ"); 
  TFile *fFullBackgroundQCD       = new TFile("/data_CMS/cms/lbianchini//MuTauStream2011/treeMuTauStream_QCDmu.root","READ"); 
  TFile *fFullBackgroundTTbar     = new TFile("/data_CMS/cms/lbianchini//MuTauStream2011/treeMuTauStream_TTJets-Mu-madgraph-PUS1.root","READ"); 
  TFile *fFullBackgroundDiBoson   = new TFile("/data_CMS/cms/lbianchini//MuTauStream2011/treeMuTauStream_DiBoson-Mu.root","READ"); 

  // OpenNTuples
  TString fSignalNameVBF           = "/data_CMS/cms/lbianchini/VbfJetsStudy/OpenNtuples/MuTauStream2011/v2/nTupleVBFH115-Mu-powheg-PUS1_Open_MuTauStream.root";
  TString fSignalNameGGH           = "/data_CMS/cms/lbianchini/VbfJetsStudy/OpenNtuples/MuTauStream2011/v2/nTupleGGFH115-Mu-powheg-PUS1_Open_MuTauStream.root";
  TString fBackgroundNameDYTauTau  = "/data_CMS/cms/lbianchini/VbfJetsStudy/OpenNtuples/MuTauStream2011/v2/nTupleDYToTauTau-Mu-20-PUS1_Open_MuTauStream.root";
  TString fBackgroundNameDYMuMu  = "/data_CMS/cms/lbianchini/VbfJetsStudy/OpenNtuples/MuTauStream2011/v2/nTupleDYToMuMu-20-PUS1_Open_MuTauStream.root";
  TString fBackgroundNameWJets     = "/data_CMS/cms/lbianchini/VbfJetsStudy/OpenNtuples/MuTauStream2011/v2/nTupleWJets-Mu-madgraph-PUS1_Open_MuTauStream.root";
  TString fBackgroundNameQCD       = "/data_CMS/cms/lbianchini/VbfJetsStudy/OpenNtuples/MuTauStream2011/v2/nTupleQCDmu_Open_MuTauStream.root";
  TString fBackgroundNameTTbar     = "/data_CMS/cms/lbianchini/VbfJetsStudy/OpenNtuples/MuTauStream2011/v2/nTupleTTJets-Mu-madgraph-PUS1_Open_MuTauStream.root";
  TString fBackgroundNameDiBoson   = "/data_CMS/cms/lbianchini/VbfJetsStudy/OpenNtuples/MuTauStream2011/v2/nTupleDiBoson-Mu_Open_MuTauStream.root";

  TFile *fSignalVBF(0); 
  TFile *fSignalGGH(0); 
  TFile *fBackgroundDYTauTau(0);
  TFile *fBackgroundDYMuMu(0);
  TFile *fBackgroundWJets(0);
  TFile *fBackgroundQCD(0);
  TFile *fBackgroundTTbar(0);
  TFile *fBackgroundDiBoson(0);
 
  fSignalVBF          = TFile::Open( fSignalNameVBF ); 
  fSignalGGH          = TFile::Open( fSignalNameGGH ); 
  fBackgroundDYTauTau = TFile::Open( fBackgroundNameDYTauTau ); 
  fBackgroundDYMuMu   = TFile::Open( fBackgroundNameDYMuMu ); 
  fBackgroundWJets    = TFile::Open( fBackgroundNameWJets ); 
  fBackgroundQCD      = TFile::Open( fBackgroundNameQCD ); 
  fBackgroundTTbar    = TFile::Open( fBackgroundNameTTbar ); 
  fBackgroundDiBoson  = TFile::Open( fBackgroundNameDiBoson ); 

  if(!fSignalVBF || !fBackgroundDYTauTau || !fBackgroundWJets || !fBackgroundQCD || !fBackgroundTTbar ||
     !fSignalGGH || !fBackgroundDYMuMu || !fBackgroundDiBoson ){
    std::cout << "ERROR: could not open files" << std::endl;
    exit(1);
  }

  TString tree = "outTreePtOrd";

  TTree *signalVBF           = (TTree*)fSignalVBF->Get(tree);
  TTree *signalGGH           = (TTree*)fSignalGGH->Get(tree);
  TTree *backgroundDYTauTau  = (TTree*)fBackgroundDYTauTau->Get(tree);
  TTree *backgroundDYMuMu    = (TTree*)fBackgroundDYMuMu->Get(tree);
  TTree *backgroundWJets     = (TTree*)fBackgroundWJets->Get(tree);
  TTree *backgroundQCD       = (TTree*)fBackgroundQCD->Get(tree);
  TTree *backgroundTTbar     = (TTree*)fBackgroundTTbar->Get(tree);
  TTree *backgroundDiBoson   = (TTree*)fBackgroundDiBoson->Get(tree);

  // here I define the map between a sample name and its tree
  std::map<std::string,TTree*> tMap;
  tMap["ggH115"]=signalGGH;
  tMap["qqH115"]=signalVBF;
  tMap["Ztautau"]=backgroundDYTauTau;
  tMap["Zmumu"]=backgroundDYMuMu;
  tMap["Wjets"]=backgroundWJets;
  tMap["QCD"]=backgroundQCD;
  tMap["TTbar"]=backgroundTTbar;
  tMap["DiBoson"]=backgroundDiBoson;

  std::map<std::string,TTree*>::iterator jt;

  Float_t pt1_, pt2_;
  Float_t Deta_, Mjj_;
  Float_t Dphi,diTauSVFitPt,diTauSVFitEta,diTauVisMass,diTauSVFitMass,ptL1,ptL2,etaL1,etaL2,diTauCharge,MtLeg1,numPV,combRelIsoLeg1,sampleWeight,ptVeto,HLT;
  Int_t tightestHPSWP;

  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////



  // here I choose the order in the stack
  std::vector<string> samples;
  samples.push_back("ggH115");
  samples.push_back("qqH115");
  samples.push_back("DiBoson");
  samples.push_back("TTbar");
  samples.push_back("Wjets");
  samples.push_back("Zmumu");
  samples.push_back("Ztautau");
  samples.push_back("QCD");

  std::map<std::string,float> crossSec;
  crossSec["ggH115"]=( 7.65e-02 * 18.13 );
  crossSec["qqH115"]=( 0.1012);
  crossSec["DiBoson"]=( -1  );
  crossSec["TTbar"]=( 157.5 );
  crossSec["Wjets"]=( 31314.0);
  crossSec["Zmumu"]=( 1666  );
  crossSec["Ztautau"]=( 1666  );
  crossSec["QCD"]=( 296600000*0.0002855 );

  float Lumi = 1000;


  // here I choose the order in the stack
  std::vector<string> filters;
  filters.push_back("total");
  filters.push_back("vertex");
  filters.push_back("1-mu");
  filters.push_back("0-e");
  filters.push_back("mu-ID");
  filters.push_back("tau-ID");
  filters.push_back("Delta R mu-tau");
  filters.push_back("mu-iso");
  filters.push_back("tau-iso");
  filters.push_back("Mt");
  filters.push_back("OS");
  filters.push_back("2-jets");
  filters.push_back("VBF cuts");
  filters.push_back("jet-veto");
  filters.push_back("HLT");

  // here I define the map between a sample name and its file ptr
  std::map<std::string,TFile*> fullMap;
  fullMap["ggH115"]   = fFullSignalGGH;
  fullMap["qqH115"]   = fFullSignalVBF;
  fullMap["Ztautau"]  = fFullBackgroundDYTauTau;
  fullMap["Zmumu"]    = fFullBackgroundDYMuMu;
  fullMap["Wjets"]    = fFullBackgroundWJets;
  fullMap["QCD"]      = fFullBackgroundQCD;
  fullMap["TTbar"]    = fFullBackgroundTTbar;
  fullMap["DiBoson"]  = fFullBackgroundDiBoson;

  std::map<std::string,TFile*>::iterator it;

  std::map<std::string,float> cutMap_allEventsFilter;
  std::map<std::string,float> cutMap_allEventsFilterE;
  for(it = fullMap.begin(); it != fullMap.end(); it++){
    TH1F* allEvents = (TH1F*)(it->second)->Get("allEventsFilter/totalEvents");
    float totalEvents = allEvents->GetBinContent(1);
    float totalEquivalentEvents = allEvents->GetEffectiveEntries();
    float tot = totalEvents;
    if(crossSec[it->first]>0) tot *= (Lumi/ (totalEvents/crossSec[it->first])  ) ;
    cutMap_allEventsFilter[it->first] = tot;
    cutMap_allEventsFilterE[it->first] = totalEquivalentEvents>0 ? sqrt(totalEquivalentEvents)*(tot/totalEquivalentEvents) : 0;
  }
  std::map<std::string,float> cutMap_vertexScrapingFilter;
  std::map<std::string,float> cutMap_vertexScrapingFilterE;
  for(it = fullMap.begin(); it != fullMap.end(); it++){
    TH1F* allEvents = (TH1F*)(it->second)->Get("allEventsFilter/totalEvents");
    float totalEvents = allEvents->GetBinContent(1);
    allEvents = (TH1F*)(it->second)->Get("vertexScrapingFilter/totalEvents");
    float tot =  allEvents->GetBinContent(1);
    float totalEquivalentEvents = allEvents->GetEffectiveEntries();
    if(crossSec[it->first]>0){
      tot *= (Lumi/ (totalEvents/crossSec[it->first])  ) ;
      //totalEquivalentEvents *= (Lumi/ (totalEvents/crossSec[it->first])  ) ;
    }
    cutMap_vertexScrapingFilter[it->first] = tot;
    cutMap_vertexScrapingFilterE[it->first] = totalEquivalentEvents>0 ? sqrt(totalEquivalentEvents)*(tot/totalEquivalentEvents) : 0;
  }
  std::map<std::string,float> cutMap_oneElectronFilter;
  std::map<std::string,float> cutMap_oneElectronFilterE;
  for(it = fullMap.begin(); it != fullMap.end(); it++){
    TH1F* allEvents = (TH1F*)(it->second)->Get("allEventsFilter/totalEvents");
    float totalEvents = allEvents->GetBinContent(1);
    allEvents = (TH1F*)(it->second)->Get("oneMuonFilter/totalEvents");
    float tot =  allEvents->GetBinContent(1);
    float totalEquivalentEvents = allEvents->GetEffectiveEntries();
    if(crossSec[it->first]>0){
      tot *= (Lumi/ (totalEvents/crossSec[it->first])  ) ;
      //totalEquivalentEvents *= (Lumi/ (totalEvents/crossSec[it->first])  ) ;
    }
    cutMap_oneElectronFilter[it->first] = tot;
    cutMap_oneElectronFilterE[it->first] = totalEquivalentEvents>0 ? sqrt(totalEquivalentEvents)*(tot/totalEquivalentEvents) : 0;
  }
  std::map<std::string,float> cutMap_noMuonFilter;
  std::map<std::string,float> cutMap_noMuonFilterE;
  for(it = fullMap.begin(); it != fullMap.end(); it++){
    TH1F* allEvents = (TH1F*)(it->second)->Get("allEventsFilter/totalEvents");
    float totalEvents = allEvents->GetBinContent(1);
    allEvents = (TH1F*)(it->second)->Get("noElecFilter/totalEvents");
    float tot =  allEvents->GetBinContent(1);
    float totalEquivalentEvents = allEvents->GetEffectiveEntries();
    if(crossSec[it->first]>0){
      tot *= (Lumi/ (totalEvents/crossSec[it->first])  ) ;
      //totalEquivalentEvents *= (Lumi/ (totalEvents/crossSec[it->first])  ) ;
    }
    cutMap_noMuonFilter[it->first] = tot;
    cutMap_noMuonFilterE[it->first] = totalEquivalentEvents>0 ? sqrt(totalEquivalentEvents)*(tot/totalEquivalentEvents) : 0;
  }
  std::map<std::string,float> cutMap_electronLegFilter;
  std::map<std::string,float> cutMap_electronLegFilterE;
  for(it = fullMap.begin(); it != fullMap.end(); it++){
    TH1F* allEvents = (TH1F*)(it->second)->Get("allEventsFilter/totalEvents");
    float totalEvents = allEvents->GetBinContent(1);
    allEvents = (TH1F*)(it->second)->Get("muonLegFilter/totalEvents");
    float tot =  allEvents->GetBinContent(1);
    float totalEquivalentEvents = allEvents->GetEffectiveEntries();
    if(crossSec[it->first]>0){
      tot *= (Lumi/ (totalEvents/crossSec[it->first])  ) ;
      //totalEquivalentEvents *= (Lumi/ (totalEvents/crossSec[it->first])  ) ;
    }
    cutMap_electronLegFilter[it->first] = tot;
    cutMap_electronLegFilterE[it->first] = totalEquivalentEvents>0 ? sqrt(totalEquivalentEvents)*(tot/totalEquivalentEvents) : 0;
  }
  std::map<std::string,float> cutMap_tauLegFilter;
  std::map<std::string,float> cutMap_tauLegFilterE;
  for(it = fullMap.begin(); it != fullMap.end(); it++){
    TH1F* allEvents = (TH1F*)(it->second)->Get("allEventsFilter/totalEvents");
    float totalEvents = allEvents->GetBinContent(1);
    allEvents = (TH1F*)(it->second)->Get("tauLegFilter/totalEvents");
    float tot =  allEvents->GetBinContent(1);
    float totalEquivalentEvents = allEvents->GetEffectiveEntries();
    if(crossSec[it->first]>0){
      tot *= (Lumi/ (totalEvents/crossSec[it->first])  ) ;
      //totalEquivalentEvents *= (Lumi/ (totalEvents/crossSec[it->first])  ) ;
    }
    cutMap_tauLegFilter[it->first] = tot;
    cutMap_tauLegFilterE[it->first] = totalEquivalentEvents>0 ? sqrt(totalEquivalentEvents)*(tot/totalEquivalentEvents) : 0;
  }
  std::map<std::string,float> cutMap_atLeastOneDiTauFilter;
  std::map<std::string,float> cutMap_atLeastOneDiTauFilterE;
  for(it = fullMap.begin(); it != fullMap.end(); it++){
    TH1F* allEvents = (TH1F*)(it->second)->Get("allEventsFilter/totalEvents");
    float totalEvents = allEvents->GetBinContent(1);
    allEvents = (TH1F*)(it->second)->Get("atLeastOneDiTauFilter/totalEvents");
    float tot = allEvents->GetBinContent(1);
    float totalEquivalentEvents = allEvents->GetEffectiveEntries();
    if(crossSec[it->first]>0){
      tot *= (Lumi/ (totalEvents/crossSec[it->first])  ) ;
      //totalEquivalentEvents *= (Lumi/ (totalEvents/crossSec[it->first])  ) ;
    }
    cutMap_atLeastOneDiTauFilter[it->first] = tot;
    cutMap_atLeastOneDiTauFilterE[it->first] = totalEquivalentEvents>0 ? sqrt(totalEquivalentEvents)*(tot/totalEquivalentEvents) : 0;
  }
  std::map<std::string,float> cutMap_ElecIso;
  std::map<std::string,float> cutMap_ElecIsoE;
  for(it = fullMap.begin(); it != fullMap.end(); it++){
    cout<<it->first<<endl;
    TH1F* allEvents = (TH1F*)(it->second)->Get("allEventsFilter/totalEvents");
    float totalEvents = allEvents->GetBinContent(1);
    TH1F* h1 = new TH1F("h1","",1,-10,10); 
    TCut cut =  (crossSec[it->first]>0) ?  "(chIsoLeg1+nhIsoLeg1+phIsoLeg1)/diTauLegsP4[0].Pt()<0.1"
      : "weight*((chIsoLeg1+nhIsoLeg1+phIsoLeg1)/diTauLegsP4[0].Pt()<0.1)";
    ((TTree*) (it->second->Get("muTauStreamAnalyzer/tree")) )->Draw("diTauLegsP4[0].Eta()>>h1",cut);
    float tot = h1->Integral();
    float totalEquivalentEvents = h1->GetEffectiveEntries();
    if(crossSec[it->first]>0){
      tot *= (Lumi/ (totalEvents/crossSec[it->first])  ) ;
      //totalEquivalentEvents *= (Lumi/ (totalEvents/crossSec[it->first])  ) ;
    }
    cutMap_ElecIso[it->first] = tot;
    cutMap_ElecIsoE[it->first] = totalEquivalentEvents>0 ? sqrt(totalEquivalentEvents)*(tot/totalEquivalentEvents) : 0;
    delete h1;
  }
  std::map<std::string,float> cutMap_TauIso;
  std::map<std::string,float> cutMap_TauIsoE;
  for(it = fullMap.begin(); it != fullMap.end(); it++){
    cout<<it->first<<endl;
    TH1F* allEvents = (TH1F*)(it->second)->Get("allEventsFilter/totalEvents");
    float totalEvents = allEvents->GetBinContent(1);
    TH1F* h1 = new TH1F("h1","",1,-10,10); 
    TCut cut =  (crossSec[it->first]>0) ?  "(chIsoLeg1+nhIsoLeg1+phIsoLeg1)/diTauLegsP4[0].Pt()<0.1 && tightestHPSWP>0"
      : "weight*((chIsoLeg1+nhIsoLeg1+phIsoLeg1)/diTauLegsP4[0].Pt()<0.1 && tightestHPSWP>0)";
    ((TTree*) (it->second->Get("muTauStreamAnalyzer/tree")) )->Draw("diTauLegsP4[0].Eta()>>h1",cut);
    float tot = h1->Integral();
    float totalEquivalentEvents = h1->GetEffectiveEntries();
    if(crossSec[it->first]>0){
      tot *= (Lumi/ (totalEvents/crossSec[it->first])  ) ;
      //totalEquivalentEvents *= (Lumi/ (totalEvents/crossSec[it->first])  ) ;
    }
    cutMap_TauIso[it->first] = tot;
    cutMap_TauIsoE[it->first] = totalEquivalentEvents>0 ? sqrt(totalEquivalentEvents)*(tot/totalEquivalentEvents) : 0;
    delete h1;
  }
  std::map<std::string,float> cutMap_Mt;
  std::map<std::string,float> cutMap_MtE;
  for(it = fullMap.begin(); it != fullMap.end(); it++){
    cout<<it->first<<endl;
    TH1F* allEvents = (TH1F*)(it->second)->Get("allEventsFilter/totalEvents");
    float totalEvents = allEvents->GetBinContent(1);
    TH1F* h1 = new TH1F("h1","",1,-10,10); 
    TCut cut =  (crossSec[it->first]>0) ?  "(chIsoLeg1+nhIsoLeg1+phIsoLeg1)/diTauLegsP4[0].Pt()<0.1 && tightestHPSWP>0 && MtLeg1<40"
      : "weight*((chIsoLeg1+nhIsoLeg1+phIsoLeg1)/diTauLegsP4[0].Pt()<0.1 && tightestHPSWP>0 && MtLeg1<40)";
    ((TTree*) (it->second->Get("muTauStreamAnalyzer/tree")) )->Draw("diTauLegsP4[0].Eta()>>h1",cut);
    float tot = h1->Integral();
    float totalEquivalentEvents = h1->GetEffectiveEntries();
    if(crossSec[it->first]>0){
      tot *= (Lumi/ (totalEvents/crossSec[it->first])  ) ;
      //totalEquivalentEvents *= (Lumi/ (totalEvents/crossSec[it->first])  ) ;
    }
    cutMap_Mt[it->first] = tot;
    cutMap_MtE[it->first] = totalEquivalentEvents>0 ? sqrt(totalEquivalentEvents)*(tot/totalEquivalentEvents) : 0;
    delete h1;
  }
  std::map<std::string,float> cutMap_OS;
  std::map<std::string,float> cutMap_OSE;
  for(it = fullMap.begin(); it != fullMap.end(); it++){
    cout<<it->first<<endl;
    TH1F* allEvents = (TH1F*)(it->second)->Get("allEventsFilter/totalEvents");
    float totalEvents = allEvents->GetBinContent(1);
    TH1F* h1 = new TH1F("h1","",1,-10,10); 
    TCut cut =  (crossSec[it->first]>0) ?  "(chIsoLeg1+nhIsoLeg1+phIsoLeg1)/diTauLegsP4[0].Pt()<0.1 && tightestHPSWP>0 && diTauCharge==0 && MtLeg1<40"
      : "weight*((chIsoLeg1+nhIsoLeg1+phIsoLeg1)/diTauLegsP4[0].Pt()<0.1 && tightestHPSWP>0 && diTauCharge==0 && MtLeg1<40)";
    ((TTree*) (it->second->Get("muTauStreamAnalyzer/tree")) )->Draw("diTauLegsP4[0].Eta()>>h1",cut);
    float tot = h1->Integral();
    float totalEquivalentEvents = h1->GetEffectiveEntries();
    if(crossSec[it->first]>0){
      tot *= (Lumi/ (totalEvents/crossSec[it->first])  ) ;
      //totalEquivalentEvents *= (Lumi/ (totalEvents/crossSec[it->first])  ) ;
    }
    cutMap_OS[it->first] = tot;
    cutMap_OSE[it->first] = totalEquivalentEvents>0 ? sqrt(totalEquivalentEvents)*(tot/totalEquivalentEvents) : 0;
    delete h1;
  }
  std::map<std::string,float> cutMap_VBFPre;
  std::map<std::string,float> cutMap_VBFPreE;
  for(jt = tMap.begin(); jt != tMap.end(); jt++){
    cout<<jt->first<<endl;
    TH1F* h1 = new TH1F("h1","",1,-10,10); 
    TCut cut =  "sampleWeight*(pt1>0 && combRelIsoLeg1<0.1 && tightestHPSWP>0 && diTauCharge==0 && MtLeg1<40)"; 
    jt->second->Draw("etaL1>>h1",cut);
    float tot = h1->Integral();
    float totalEquivalentEvents = h1->GetEffectiveEntries();
    cutMap_VBFPre[jt->first] = tot;
    cutMap_VBFPreE[jt->first] = totalEquivalentEvents>0 ? sqrt(totalEquivalentEvents)*(tot/totalEquivalentEvents) : 0;
    delete h1;
  }
  std::map<std::string,float> cutMap_VBF;
  std::map<std::string,float> cutMap_VBFE;
  std::map<std::string,float> cutMap_JetVeto;
  std::map<std::string,float> cutMap_JetVetoE;
  std::map<std::string,float> cutMap_HLT;
  std::map<std::string,float> cutMap_HLTE;
  for(jt = tMap.begin(); jt != tMap.end(); jt++){
    cout<<jt->first<<endl;
    TCut cut =  "(pt1>0 && combRelIsoLeg1<0.1 && tightestHPSWP>0 && diTauCharge==0 && MtLeg1<40)"; 

    TFile* dummy = new TFile("dummy.root","RECREATE");  
    TTree* currentTree = (TTree*)(jt->second)->CopyTree(cut);
    float tot = 0;
    int counter = 0;
    float tot2 = 0;
    int counter2 = 0;
    float tot3 = 0;
    int counter3 = 0;

    currentTree->SetBranchAddress( "pt1", &pt1_ );
    currentTree->SetBranchAddress( "pt2", &pt2_ );
    currentTree->SetBranchAddress( "Deta",&Deta_ );
    currentTree->SetBranchAddress( "Mjj", &Mjj_ );
    currentTree->SetBranchAddress( "diTauSVFitPt",&diTauSVFitPt);
    //currentTree->SetBranchAddress( "diTauSVFitEta",&diTauSVFitEta);
    currentTree->SetBranchAddress( "diTauSVFitMass",&diTauSVFitMass);
    currentTree->SetBranchAddress( "diTauVisMass",&diTauVisMass);
    currentTree->SetBranchAddress( "ptL1", &ptL1 );
    currentTree->SetBranchAddress( "ptL2",  &ptL2 );
    currentTree->SetBranchAddress( "etaL1", &etaL1 );
    currentTree->SetBranchAddress( "etaL2", &etaL2 );
    currentTree->SetBranchAddress( "combRelIsoLeg1",&combRelIsoLeg1);
    currentTree->SetBranchAddress( "tightestHPSWP",&tightestHPSWP);
    currentTree->SetBranchAddress( "diTauCharge",&diTauCharge);
    currentTree->SetBranchAddress( "MtLeg1",&MtLeg1);
    currentTree->SetBranchAddress( "numPV",&numPV);
    currentTree->SetBranchAddress( "sampleWeight",&sampleWeight);
    currentTree->SetBranchAddress( "ptVeto",&ptVeto);
    currentTree->SetBranchAddress( "HLT",&HLT);

    for (Long64_t ievt=0; ievt<currentTree->GetEntries();ievt++) {

      currentTree->GetEntry(ievt);

      if (ievt%10000 == 0){
	std::cout << (jt->first) << " ---> processing event: " << ievt << " ..." <<std::endl;
      }

      pt1  = pt1_;
      pt2  = pt2_;
      Deta = Deta_;
      Mjj  = Mjj_;

      bool pass = effS_>0 ? reader->EvaluateMVA( "Cuts", effS_ ) : (pt1>0);

      if(pass){
	tot+=sampleWeight;
	counter++;
	if(ptVeto<20){
	  tot2+=sampleWeight;
	  counter2++;
	  if(HLT>0.5 && HLT<1.5){
	    tot3+=sampleWeight;
	    counter3++;
	  }
	}
      }
    
    }// end loop   

    cutMap_VBF[jt->first] = tot;
    cutMap_VBFE[jt->first] = counter>0 ? sqrt(counter)*tot/counter : 0;
    cutMap_JetVeto[jt->first] = tot2;
    cutMap_JetVetoE[jt->first] = counter2>0 ? sqrt(counter2)*tot2/counter2 : 0;
    cutMap_HLT[jt->first] = tot3;
    cutMap_HLTE[jt->first] = counter3>0 ? sqrt(counter3)*tot3/counter3 : 0;
  }


  
 

  std::vector< std::map<std::string,float> > allFilters;
  allFilters.push_back(cutMap_allEventsFilter);
  allFilters.push_back(cutMap_vertexScrapingFilter);
  allFilters.push_back(cutMap_oneElectronFilter);
  allFilters.push_back(cutMap_noMuonFilter);
  allFilters.push_back(cutMap_electronLegFilter);
  allFilters.push_back(cutMap_tauLegFilter);
  allFilters.push_back(cutMap_atLeastOneDiTauFilter);
  allFilters.push_back(cutMap_ElecIso);
  allFilters.push_back(cutMap_TauIso);
  allFilters.push_back(cutMap_Mt);
  allFilters.push_back(cutMap_OS);
  allFilters.push_back(cutMap_VBFPre);
  allFilters.push_back(cutMap_VBF);
  allFilters.push_back(cutMap_JetVeto);
  allFilters.push_back(cutMap_HLT);

  std::vector< std::map<std::string,float> > allFiltersE;
  allFiltersE.push_back(cutMap_allEventsFilterE);
  allFiltersE.push_back(cutMap_vertexScrapingFilterE);
  allFiltersE.push_back(cutMap_oneElectronFilterE);
  allFiltersE.push_back(cutMap_noMuonFilterE);
  allFiltersE.push_back(cutMap_electronLegFilterE);
  allFiltersE.push_back(cutMap_tauLegFilterE);
  allFiltersE.push_back(cutMap_atLeastOneDiTauFilterE);
  allFiltersE.push_back(cutMap_ElecIsoE);
  allFiltersE.push_back(cutMap_TauIsoE);
  allFiltersE.push_back(cutMap_MtE);
  allFiltersE.push_back(cutMap_OSE);
  allFiltersE.push_back(cutMap_VBFPreE);
  allFiltersE.push_back(cutMap_VBFE);
  allFiltersE.push_back(cutMap_JetVetoE);
  allFiltersE.push_back(cutMap_HLTE);

  //out<<"\\begin{center}"<<endl;
  out<<"\\begin{tabular}[!htbp]{|c";
  for(int k = 0 ; k < samples.size(); k++) out<<"|c";
  out<<"|} \\hline"<<endl;
  out<< "Cut & ";
  for(int k = 0 ; k < samples.size(); k++){
    out << (fullMap.find(samples[k]))->first;
    if(k!=samples.size()-1) out <<" & " ;
    else out << " \\\\ " << endl;
  }
  out <<  " \\hline" << endl;

  
  for(int i = 0; i < allFilters.size(); i++){
    out << filters[i] << " & ";
    for(int k = 0 ; k < samples.size(); k++){
      out << (allFilters[i].find(samples[k]))->second << " $\\pm$ " << (allFiltersE[i].find(samples[k]))->second;
      if(k!=samples.size()-1) out <<" & " ;
      else out << " \\\\ " << endl;
    }
    out <<  " \\hline" << endl;

  }
  
  out<<"\\end{tabular}"<<endl;
  //out<<"\\end{center}"<<endl;
 

  return;

}
void rezamyTMVAClassificationApplication1systematic( 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
   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 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 ptphoton,etaphoton,ptmuon,etamuon,ptjet,etajet,masstop,mtw,deltaRphotonjet,deltaRphotonmuon,ht,costopphoton,deltaphiphotonmet,cvsdiscriminant;
Float_t jetmultiplicity,bjetmultiplicity,leptoncharge;
   // 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
  reader->AddVariable ("ptphoton", &ptphoton);
//  reader->AddVariable ("etaphoton", &etaphoton);
  reader->AddVariable ("ptmuon", &ptmuon);
//  reader->AddVariable ("etamuon", &etamuon);
  reader->AddVariable ("ptjet", &ptjet);
//  reader->AddVariable ("etajet", &etajet);
//  reader->AddVariable ("masstop", &masstop);
//  reader->AddVariable ("mtw", &mtw);
  reader->AddVariable ("deltaRphotonjet", &deltaRphotonjet);
  reader->AddVariable ("deltaRphotonmuon", &deltaRphotonmuon);
//  reader->AddVariable ("ht", &ht);
//  reader->AddVariable ("photonmuonmass", &photonmuonmass);
  reader->AddVariable ("costopphoton", &costopphoton);
//  reader->AddVariable ("topphotonmass", &topphotonmass);
//reader->AddVariable ("pttop", &pttop);
//reader->AddVariable ("etatop", &etatop);
  reader->AddVariable ("jetmultiplicity", &jetmultiplicity);
//  reader->AddVariable ("bjetmultiplicity", &bjetmultiplicity);
  reader->AddVariable ("deltaphiphotonmet", &deltaphiphotonmet);
  reader->AddVariable ("cvsdiscriminant", &cvsdiscriminant);
//  reader->AddVariable ("leptoncharge", &leptoncharge);


/*
   // Spectator variables declared in the training have to be added to the reader, too
   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 = "TMVA";

   // 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 = 40;
   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);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
std::vector<string> samples_;
std::vector<string> datasamples_;
std::vector<TH1F*> datahists;
std::vector<TH1F*> revDATAhists;

float scales[] = {0.0978,1.491,0.0961,0.0253,0.0224,0.0145,0.0125,0.0160,0.0158,0.0341,0.0341,0.0341,0.020,0.0017,0.0055,0.0032,0.00084,0.02,19.145*0.0169,19.145*0.0169,19.145*0.0169,19.145*0.0169,19.145*0.0169};
//samples_.push_back("WJET.root");
samples_.push_back("ZJET.root");
samples_.push_back("PHJET200400.root");
samples_.push_back("WPHJET.root");
samples_.push_back("T-W-CH.root");
samples_.push_back("TBAR-W-CH.root");
samples_.push_back("T-S-CH.root");
samples_.push_back("TBAR-S-CH.root");
samples_.push_back("T-T-CH.root");
samples_.push_back("TBAR-T-CH.root");
samples_.push_back("TTBAR1.root");
samples_.push_back("TTBAR2.root");
samples_.push_back("TTBAR3.root");
samples_.push_back("TTG.root");
samples_.push_back("WWG.root");
samples_.push_back("WW.root");
samples_.push_back("WZ.root");
samples_.push_back("ZZ.root");
samples_.push_back("ZGAMMA.root");
samples_.push_back("SIGNAL.root");
samples_.push_back("SIGNAL.root");
samples_.push_back("SIGNAL.root");
samples_.push_back("SIGNAL.root");
samples_.push_back("SIGNAL.root");


datasamples_.push_back("REALDATA1.root");
datasamples_.push_back("REALDATA2.root");
datasamples_.push_back("REALDATA3.root");

std::vector<string> datasamplesreverse_;
datasamplesreverse_.push_back("etarev/REALDATA1.root");
datasamplesreverse_.push_back("etarev/REALDATA2.root");
datasamplesreverse_.push_back("etarev/REALDATA3.root");

std::vector<string> systematics;
//systematics.push_back("__JES__plus");
//systematics.push_back("__JES__minus");
//systematics.push_back("__JER__plus");
//systematics.push_back("__JER__minus");
systematics.push_back("__PU__plus");
systematics.push_back("__PU__minus");
systematics.push_back("__TRIG__plus");
systematics.push_back("__TRIG__minus");
systematics.push_back("__BTAG__plus");
systematics.push_back("__BTAG__minus");
systematics.push_back("__MISSTAG__plus");
systematics.push_back("__MISSTAG__minus");
systematics.push_back("__MUON__plus");
systematics.push_back("__MUON__minus");
systematics.push_back("__PHOTON__plus");
systematics.push_back("__PHOTON__minus");
systematics.push_back("");

map<string, double> eventwight;
TList* hList = new TList();      // list of histograms to store
std::vector<TFile*> files;
for(unsigned int idx=0; idx<samples_.size(); ++idx){
files.push_back(new TFile(samples_[idx].c_str()));
}

std::vector<TFile*> datafiles;
for(unsigned int idx=0; idx<datasamples_.size(); ++idx){
datafiles.push_back(new TFile(datasamples_[idx].c_str()));
}


for(unsigned int phi=0; phi<systematics.size(); ++phi){
std::vector<TH1F*> hists;

TH1F   *wphjethist(0), *zjethist(0) , *phjethist(0), *wjethist(0), *twchhist(0), *tbarwhist(0),  *tschhist(0), *tbarschhist(0), *ttchhist(0), *tbartchhist(0), *tt1hist(0) ,*tt2hist(0), *tt3hist(0), *ttphhist(0), *wwphhist(0), *wwhist(0), *wzhist(0), *zzhist(0), *zgammahist(0), *signalhist5(0) , *signalhist10(0), *signalhist20(0), *signalhist30(0), *signalhist40(0);

TH1F *data1histrev(0), *data2histrev(0) ,*data3histrev(0);

wphjethist = new TH1F( std::string("BDT__wphjethist").append(systematics[phi]).c_str(), std::string("BDT__wphjethist").append(systematics[phi]).c_str()  , nbin,  -1, 1    );
zjethist = new TH1F( std::string("BDT__zjethist").append(systematics[phi]).c_str(), std::string("BDT__zjethist").append(systematics[phi]).c_str(),  nbin,  -1, 1    );
phjethist = new TH1F( std::string("BDT__phjethist").append(systematics[phi]).c_str(),std::string("BDT__phjethist").append(systematics[phi]).c_str() , nbin,  -1, 1    );
//wjethist = new TH1F( std::string("BDT__wjethist").append(systematics[phi]).c_str(),std::string("BDT__wjethist").append(systematics[phi]).c_str() , nbin, -0.8, 0.8 );
twchhist = new TH1F( std::string("BDT__twchhist").append(systematics[phi]).c_str(),std::string("BDT__twchhist").append(systematics[phi]).c_str() ,nbin,  -1, 1    );
tbarwhist = new TH1F( std::string("BDT__tbarwhist").append(systematics[phi]).c_str(),std::string("BDT__tbarwhist").append(systematics[phi]).c_str() ,nbin,  -1, 1    );
tschhist = new TH1F( std::string("BDT__tschhist").append(systematics[phi]).c_str(), std::string("BDT__tschhist").append(systematics[phi]).c_str(), nbin,  -1, 1    );
tbarschhist = new TH1F( std::string("BDT__tbarschhist").append(systematics[phi]).c_str(),std::string("BDT__tbarschhist").append(systematics[phi]).c_str(), nbin,  -1, 1    );
ttchhist = new TH1F( std::string("BDT__ttchhist").append(systematics[phi]).c_str(),std::string("BDT__ttchhist").append(systematics[phi]).c_str(), nbin,  -1, 1    );
tbartchhist = new TH1F( std::string("BDT__tbartchhist").append(systematics[phi]).c_str(), std::string("BDT__tbartchhist").append(systematics[phi]).c_str(), nbin,  -1, 1    );
tt1hist = new TH1F( std::string("BDT__tt1hist").append(systematics[phi]).c_str(), std::string("BDT__tt1hist").append(systematics[phi]).c_str(), nbin,  -1, 1    );
tt2hist = new TH1F( std::string("BDT__tt2hist").append(systematics[phi]).c_str(), std::string("BDT__tt2hist").append(systematics[phi]).c_str(), nbin,  -1, 1    );
tt3hist = new TH1F( std::string("BDT__tt3hist").append(systematics[phi]).c_str(),std::string("BDT__tt3hist").append(systematics[phi]).c_str() , nbin,  -1, 1    );
ttphhist = new TH1F( std::string("BDT__ttphhist").append(systematics[phi]).c_str(),std::string("BDT__ttphhist").append(systematics[phi]).c_str() ,nbin,  -1, 1    );
wwphhist = new TH1F( std::string("BDT__wwphhist").append(systematics[phi]).c_str(),std::string("BDT__wwphhist").append(systematics[phi]).c_str(), nbin,  -1, 1    );
wwhist = new TH1F( std::string("BDT__wwhist").append(systematics[phi]).c_str(),std::string("BDT__wwhist").append(systematics[phi]).c_str()  ,nbin,  -1, 1    );
wzhist = new TH1F( std::string("BDT__wzhist").append(systematics[phi]).c_str(),std::string("BDT__wzhist").append(systematics[phi]).c_str(), nbin,  -1, 1    );
zzhist = new TH1F( std::string("BDT__zzhist").append(systematics[phi]).c_str(),std::string("BDT__zzhist").append(systematics[phi]).c_str() ,nbin,  -1, 1    );
zgammahist = new TH1F( std::string("BDT__zgammahist").append(systematics[phi]).c_str(),std::string("BDT__zgammahist").append(systematics[phi]).c_str() ,nbin,  -1, 1    );
signalhist5 = new TH1F( std::string("BDT__signal5").append(systematics[phi]).c_str(),std::string("BDT__signal5").append(systematics[phi]).c_str()  ,nbin,  -1, 1    );
signalhist10 = new TH1F( std::string("BDT__signal10").append(systematics[phi]).c_str(),std::string("BDT__signal10").append(systematics[phi]).c_str() ,nbin,  -1, 1    );
signalhist20 = new TH1F( std::string("BDT__signal20").append(systematics[phi]).c_str(),std::string("BDT__signal20").append(systematics[phi]).c_str() ,nbin,  -1, 1    );
signalhist30 = new TH1F( std::string("BDT__signal30").append(systematics[phi]).c_str(),std::string("BDT__signal30").append(systematics[phi]).c_str() ,nbin, -1, 1   );
signalhist40 = new TH1F( std::string("BDT__signal40").append(systematics[phi]).c_str(),std::string("BDT__signal40").append(systematics[phi]).c_str() ,nbin, -1, 1   );



hists.push_back(zjethist);
hists.push_back(phjethist);
hists.push_back(wphjethist);
//hists.push_back(wjethist);
hists.push_back(twchhist);
hists.push_back(tbarwhist);
hists.push_back(tschhist);
hists.push_back(tbarschhist);
hists.push_back(ttchhist);
hists.push_back(tbartchhist);
hists.push_back(tt1hist);
hists.push_back(tt2hist);
hists.push_back(tt3hist);
hists.push_back(ttphhist);
hists.push_back(wwphhist);
hists.push_back(wwhist);
hists.push_back(wzhist);
hists.push_back(zzhist);
hists.push_back(zgammahist);
hists.push_back(signalhist5);
hists.push_back(signalhist10);
hists.push_back(signalhist20);
hists.push_back(signalhist30);
hists.push_back(signalhist40);


for(unsigned int idx=0; idx<samples_.size(); ++idx){
   TFile *input(0);

   TString fname =samples_[idx];
   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 corres[1]ponds 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
   //
  //Double_t  myptphoton,myetaphoton,myptmuon,myetamuon,myptjet,myetajet,mymasstop,mymtw,mydeltaRphotonjet,mydeltaRphotonmuon,myht,mycostopphoton,mydeltaphiphotonmet,mycvsdiscriminant,myjetmultiplicity,mybjetmultiplicity,myleptoncharge;


std::vector<double> *myptphoton=0;
std::vector<double> *myetaphoton=0;
std::vector<double> *myptmuon=0;
std::vector<double> *myetamuon=0;
std::vector<double> *myptjet=0;
std::vector<double> *myetajet=0;
std::vector<double> *mymasstop=0;
//std::vector<double> *mymtw=0;
std::vector<double> *mydeltaRphotonjet=0;
std::vector<double> *mydeltaRphotonmuon=0;
//std::vector<double> *myht=0;
std::vector<double> *mycostopphoton=0;
std::vector<double> *mydeltaphiphotonmet=0;
std::vector<double> *mycvsdiscriminant=0;
std::vector<double> *myjetmultiplicity=0;
//std::vector<double> *mybjetmultiplicity=0;
//std::vector<double> *myleptoncharge=0;
std::vector<double> *myweight=0;
std::vector<double> *mybtagSF=0;
std::vector<double> *mybtagSFup=0;
std::vector<double> *mybtagSFdown=0;
std::vector<double> *mymistagSFup=0;
std::vector<double> *mymistagSFdown=0;
std::vector<double> *mytriggerSF=0;
std::vector<double> *mytriggerSFup=0;
std::vector<double> *mytriggerSFdown=0;
std::vector<double> *myphotonSF=0;
std::vector<double> *myphotonSFup=0;
std::vector<double> *myphotonSFdown=0;
std::vector<double> *mypileupSF=0;
std::vector<double> *mypileupSFup=0;
std::vector<double> *mypileupSFdown=0;
std::vector<double> *mymuonSFup=0;
std::vector<double> *mymuonSFdown=0;
std::vector<double> *mymuonSF=0;

   std::cout << "--- Select signal sample" << std::endl;
   TTree* theTree = (TTree*)input->Get("analyzestep2/atq");
//   Int_t myjetmultiplicity, mybjetmultiplicity , myleptoncharge;
//   Float_t userVar1, userVar2;

   theTree->SetBranchAddress("ptphoton", &myptphoton  );
   theTree->SetBranchAddress( "etaphoton", &myetaphoton );
   theTree->SetBranchAddress( "ptmuon", &myptmuon );
   theTree->SetBranchAddress( "etamuon", &myetamuon );
   theTree->SetBranchAddress( "ptjet", &myptjet );
   theTree->SetBranchAddress( "etajet", &myetajet );
   theTree->SetBranchAddress( "masstop", &mymasstop );
//   theTree->SetBranchAddress( "mtw", &mymtw );
   theTree->SetBranchAddress( "deltaRphotonjet", &mydeltaRphotonjet );
   theTree->SetBranchAddress( "deltaRphotonmuon", &mydeltaRphotonmuon );
//   theTree->SetBranchAddress( "ht", &myht );
   theTree->SetBranchAddress( "costopphoton", &mycostopphoton );
   theTree->SetBranchAddress( "jetmultiplicity", &myjetmultiplicity );
//   theTree->SetBranchAddress( "bjetmultiplicity", &mybjetmultiplicity );
   theTree->SetBranchAddress( "deltaphiphotonmet", &mydeltaphiphotonmet );
   theTree->SetBranchAddress( "cvsdiscriminant", &mycvsdiscriminant );
//   theTree->SetBranchAddress( "leptoncharge", &myleptoncharge );
   theTree->SetBranchAddress( "weight", &myweight);
   theTree->SetBranchAddress( "btagSF", &mybtagSF);
   theTree->SetBranchAddress( "btagSFup", &mybtagSFup);
   theTree->SetBranchAddress( "btagSFdown", &mybtagSFdown);
   theTree->SetBranchAddress( "mistagSFup", &mymistagSFup);
   theTree->SetBranchAddress( "mistagSFdown", &mymistagSFdown);
   theTree->SetBranchAddress( "triggerSF", &mytriggerSF);
   theTree->SetBranchAddress( "triggerSFup", &mytriggerSFup);
   theTree->SetBranchAddress( "triggerSFdown", &mytriggerSFdown);
   theTree->SetBranchAddress( "photonSF", &myphotonSF);
   theTree->SetBranchAddress( "photonSFup", &myphotonSFup);
   theTree->SetBranchAddress( "photonSFdown", &myphotonSFdown);
   theTree->SetBranchAddress( "muonSF", &mymuonSF);
   theTree->SetBranchAddress( "muonSFup", &mymuonSFup);
   theTree->SetBranchAddress( "muonSFdown", &mymuonSFdown);
   theTree->SetBranchAddress( "pileupSF", &mypileupSF);
   theTree->SetBranchAddress( "pileupSFup", &mypileupSFup);
   theTree->SetBranchAddress( "pileupSFdown", &mypileupSFdown);



 // 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++) {
//   std::cout << "--- ... Processing event: " << ievt << std::endl;
double finalweight;

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

      theTree->GetEntry(ievt);
//for (int l=0;l<sizeof(myptphoton);l++){
//std::cout << "--- ... reza: " << myptphoton[l] <<std::endl;
//}
//std::cout << "--- ......................."<< (*mycvsdiscriminant)[0]<<std::endl;
      // --- Return the MVA outputs and fill into histograms
ptphoton=(float)(*myptphoton)[0];
etaphoton=(float)(*myetaphoton )[0];
ptmuon=(float)(*myptmuon )[0];
etamuon=(float)(*myetamuon )[0];
ptjet=(float)(*myptjet )[0];
etajet=(float)(*myetajet )[0];
masstop=(float)(*mymasstop )[0];
//mtw=(float)(*mymtw )[0];
deltaRphotonjet=(float)(*mydeltaRphotonjet )[0];
deltaRphotonmuon=(float)(*mydeltaRphotonmuon )[0];
//ht=(float)(*myht )[0];
costopphoton=(float)(*mycostopphoton )[0];
jetmultiplicity=(float)(*myjetmultiplicity )[0];
//bjetmultiplicity=(float)(*mybjetmultiplicity )[0];
deltaphiphotonmet=(float)(*mydeltaphiphotonmet )[0];
cvsdiscriminant=(float)(*mycvsdiscriminant)[0];
//leptoncharge=(float)(*myleptoncharge )[0];
finalweight=(*myweight)[0];
//cout<<(*myweight)[0]<<endl;
eventwight["__PU__plus"]=(*myweight)[0]*(*mypileupSFup)[0]/(*mypileupSF)[0];
eventwight["__PU__minus"]=(*myweight)[0]*(*mypileupSFdown)[0]/(*mypileupSF)[0];
eventwight["__TRIG__plus"]=(*myweight)[0]*(*mytriggerSFup)[0]/(*mytriggerSF)[0];
eventwight["__TRIG__minus"]=(*myweight)[0]*(*mytriggerSFdown)[0]/(*mytriggerSF)[0];
eventwight["__BTAG__plus"]=(*myweight)[0]*(*mybtagSFup)[0]/(*mybtagSF)[0];
eventwight["__BTAG__minus"]=(*myweight)[0]*(*mybtagSFdown)[0]/(*mybtagSF)[0];
eventwight["__MISSTAG__plus"]=(*myweight)[0]*(*mymistagSFup)[0]/(*mybtagSF)[0];
eventwight["__MISSTAG__minus"]=(*myweight)[0]*(*mymistagSFdown)[0]/(*mybtagSF)[0];
eventwight["__MUON__plus"]=(*myweight)[0]*(*mymuonSFup)[0]/(*mymuonSF)[0];
eventwight["__MUON__minus"]=(*myweight)[0]*(*mymuonSFdown)[0]/(*mymuonSF)[0];
eventwight["__PHOTON__plus"]=(*myweight)[0]*(*myphotonSFup)[0]/(*myphotonSF)[0];
eventwight["__PHOTON__minus"]=(*myweight)[0]*(*myphotonSFdown)[0]/(*myphotonSF)[0];
eventwight[""]=(*myweight)[0];

finalweight=eventwight[systematics[phi].c_str()];
if (samples_[idx]=="SIGNAL.root") finalweight=(*myweight)[0];
//if (samples_[idx]=="WPHJET")  finalweight=(*mypileupSF)[0]*(*mytriggerSF)[0]*(*mybtagSF)[0]*(*mymuonSF)[0]*(*myphotonSF)[0];
//if (finalweight<0) finalweight=30;
//cout<<"negative event weight"<<finalweight<<"            "<<ptphoton<<endl;
      if (Use["CutsGA"]) {
         // Cuts is a special case: give the desired signal efficienciy
         Bool_t passed = reader->EvaluateMVA( "CutsGA method", effS );
         if (passed) nSelCutsGA++;
      }

hists[idx] ->Fill( reader->EvaluateMVA( "BDT method"           ),finalweight* scales[idx] );

//cout<<reader->EvaluateMVA( "BDT method")<<"                            "<<finalweight<<endl;   
//cout<<(*myweight)[0]<<endl;
      // 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" ) );
      }
//delete finalweight;
   }

   // 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;
      }
   }

delete myptphoton;
delete myetaphoton;
delete myptmuon;
delete myetamuon;
delete myptjet;
delete myetajet;
delete mymasstop;
//delete mymtw;
delete mydeltaRphotonjet;
delete mydeltaRphotonmuon;
//delete myht;
delete mycostopphoton;
delete mydeltaphiphotonmet;
delete mycvsdiscriminant;
delete myjetmultiplicity;
//delete mybjetmultiplicity;
//delete myleptoncharge;
//delete myplot;
delete mybtagSF;
delete mybtagSFup;
delete mybtagSFdown;
delete mymistagSFup;
delete mytriggerSF;
delete mytriggerSFup;
delete mytriggerSFdown;
delete myphotonSF;
delete myphotonSFup;
delete myphotonSFdown;
delete mypileupSF;
delete mypileupSFup;
delete mypileupSFdown;
delete input;
if (idx==samples_.size()-5) hists[idx]->Scale(5/hists[idx]->Integral());
if (idx==samples_.size()-4) hists[idx]->Scale(10/hists[idx]->Integral());
if (idx==samples_.size()-3) hists[idx]->Scale(20/hists[idx]->Integral());
if (idx==samples_.size()-2) hists[idx]->Scale(30/hists[idx]->Integral());
if (idx==samples_.size()-1) hists[idx]->Scale(40/hists[idx]->Integral());

if (samples_[idx]=="WPHJET.root") hists[idx]->Scale(3173/hists[idx]->Integral());
if (samples_[idx]=="TTBAR2.root") hists[idx]->Add(hists[idx-1]);
if (samples_[idx]=="TTBAR3.root") hists[idx]->Add(hists[idx-1]);
if (!(samples_[idx]=="TTBAR1.root" || samples_[idx]=="TTBAR2.root")) hList->Add(hists[idx]);
}
}

TH1F *data1hist(0), *data2hist(0) ,*data3hist(0);
data1hist = new TH1F( "mu_BDT__data1hist",           "mu_BDT__data1hist",           nbin, -1, 1 );
data2hist = new TH1F( "mu_BDT__data2hist",           "mu_BDT__data2hist",           nbin, -1, 1 );
data3hist = new TH1F( "BDT__DATA",           "BDT__DATA",           nbin, -1, 1 );

datahists.push_back(data1hist);
datahists.push_back(data2hist);
datahists.push_back(data3hist);



for(unsigned int idx=0; idx<datasamples_.size(); ++idx){
   TFile *input(0);
   TString fname =datasamples_[idx];
   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 corres[1]ponds 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::vector<double> *myptphoton=0;
std::vector<double> *myetaphoton=0;
std::vector<double> *myptmuon=0;
std::vector<double> *myetamuon=0;
std::vector<double> *myptjet=0;
std::vector<double> *myetajet=0;
std::vector<double> *mymasstop=0;
//std::vector<double> *mymtw=0;
std::vector<double> *mydeltaRphotonjet=0;
std::vector<double> *mydeltaRphotonmuon=0;
//std::vector<double> *myht=0;
std::vector<double> *mycostopphoton=0;
std::vector<double> *mydeltaphiphotonmet=0;
std::vector<double> *mycvsdiscriminant=0;
std::vector<double> *myjetmultiplicity=0;
//std::vector<double> *mybjetmultiplicity=0;
//std::vector<double> *myleptoncharge=0;

   std::cout << "--- Select signal sample" << std::endl;
   TTree* theTree = (TTree*)input->Get("analyzestep2/atq");
//   Int_t myjetmultiplicity, mybjetmultiplicity , myleptoncharge;
//   Float_t userVar1, userVar2;

   theTree->SetBranchAddress("ptphoton", &myptphoton  );
   theTree->SetBranchAddress( "etaphoton", &myetaphoton );
   theTree->SetBranchAddress( "ptmuon", &myptmuon );
   theTree->SetBranchAddress( "etamuon", &myetamuon );
   theTree->SetBranchAddress( "ptjet", &myptjet );
   theTree->SetBranchAddress( "etajet", &myetajet );
   theTree->SetBranchAddress( "masstop", &mymasstop );
//   theTree->SetBranchAddress( "mtw", &mymtw );
   theTree->SetBranchAddress( "deltaRphotonjet", &mydeltaRphotonjet );
   theTree->SetBranchAddress( "deltaRphotonmuon", &mydeltaRphotonmuon );
//   theTree->SetBranchAddress( "ht", &myht );
   theTree->SetBranchAddress( "costopphoton", &mycostopphoton );
   theTree->SetBranchAddress( "jetmultiplicity", &myjetmultiplicity );
//   theTree->SetBranchAddress( "bjetmultiplicity", &mybjetmultiplicity );
   theTree->SetBranchAddress( "deltaphiphotonmet", &mydeltaphiphotonmet );
   theTree->SetBranchAddress( "cvsdiscriminant", &mycvsdiscriminant );
//   theTree->SetBranchAddress( "leptoncharge", &myleptoncharge );


 // 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++) {
//   std::cout << "--- ... Processing event: " << ievt << std::endl;

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

      theTree->GetEntry(ievt);
//for (int l=0;l<sizeof(myptphoton);l++){
//std::cout << "--- ... reza: " << myptphoton[l] <<std::endl;
//}
//std::cout << "--- ......................."<< (*mycvsdiscriminant)[0]<<std::endl;
      // --- Return the MVA outputs and fill into histograms
ptphoton=(float)(*myptphoton)[0];
etaphoton=(float)(*myetaphoton )[0];
ptmuon=(float)(*myptmuon )[0];
etamuon=(float)(*myetamuon )[0];
ptjet=(float)(*myptjet )[0];
etajet=(float)(*myetajet )[0];
masstop=(float)(*mymasstop )[0];
//mtw=(float)(*mymtw )[0];
deltaRphotonjet=(float)(*mydeltaRphotonjet )[0];
deltaRphotonmuon=(float)(*mydeltaRphotonmuon )[0];
//ht=(float)(*myht )[0];
costopphoton=(float)(*mycostopphoton )[0];
jetmultiplicity=(float)(*myjetmultiplicity )[0];
//bjetmultiplicity=(float)(*mybjetmultiplicity )[0];
deltaphiphotonmet=(float)(*mydeltaphiphotonmet )[0];
cvsdiscriminant=(float)(*mycvsdiscriminant)[0];
//leptoncharge=(float)(*myleptoncharge )[0];




      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"         ) );

datahists[idx] ->Fill( reader->EvaluateMVA( "BDT method"           ) );
   }
   // Get elapsed time
   sw.Stop();
   std::cout << "--- End of event loop: "; sw.Print();

delete myptphoton;
delete myetaphoton;
delete myptmuon;
delete myetamuon;
delete myptjet;
delete myetajet;
delete mymasstop;
//delete mymtw;
delete mydeltaRphotonjet;
delete mydeltaRphotonmuon;
//delete myht;
delete mycostopphoton;
delete mydeltaphiphotonmet;
delete mycvsdiscriminant;
delete myjetmultiplicity;
//delete mybjetmultiplicity;
//delete myleptoncharge;
//delete myplot;
delete input;
}

for(unsigned int idx=1; idx<datasamples_.size(); ++idx){
datahists[idx]->Add(datahists[idx-1]);
}
hList->Add(datahists[2]);


TH1F *data1histrev(0), *data2histrev(0) ,*data3histrev(0);

data1histrev = new TH1F( "BDT__data1histrev",           "BDT__data1histrev",           nbin, -1, 1 );
data2histrev = new TH1F( "BDT__data2histrev",           "BDT__data2histrev",           nbin, -1, 1 );
data3histrev = new TH1F( "BDT__wjet",           "BDT__wjet",           nbin, -1, 1 );

revDATAhists.push_back(data1histrev);
revDATAhists.push_back(data2histrev);
revDATAhists.push_back(data3histrev);


for(unsigned int idx=0; idx<datasamplesreverse_.size(); ++idx){
   TFile *input(0);

   TString fname =datasamplesreverse_[idx];
   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 corres[1]ponds 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
   //                //
//   Double_t  myptphoton,myetaphoton,myptmuon,myetamuon,myptjet,myetajet,mymasstop,mymtw,mydeltaRphotonjet,mydeltaRphotonmuon,myht,mycostopphoton,mydeltaphiphotonmet,mycvsdiscriminant,myjetmultiplicity,mybjetmultiplicity,myleptoncharge;
   
std::vector<double> *myptphoton=0;
std::vector<double> *myetaphoton=0;
 std::vector<double> *myptmuon=0;
 std::vector<double> *myetamuon=0;
 std::vector<double> *myptjet=0;
 std::vector<double> *myetajet=0;
 std::vector<double> *mymasstop=0;
 //std::vector<double> *mymtw=0;
 std::vector<double> *mydeltaRphotonjet=0;
std::vector<double> *mydeltaRphotonmuon=0;
 //std::vector<double> *myht=0;
 std::vector<double> *mycostopphoton=0;
 std::vector<double> *mydeltaphiphotonmet=0;
 std::vector<double> *mycvsdiscriminant=0;
 std::vector<double> *myjetmultiplicity=0;
 //std::vector<double> *mybjetmultiplicity=0;
 //std::vector<double> *myleptoncharge=0;
   TTree* theTree = (TTree*)input->Get("analyzestep2/atq");
   theTree->SetBranchAddress("ptphoton", &myptphoton  );
   theTree->SetBranchAddress( "etaphoton", &myetaphoton );
   theTree->SetBranchAddress( "ptmuon", &myptmuon );
   theTree->SetBranchAddress( "etamuon", &myetamuon );
   theTree->SetBranchAddress( "ptjet", &myptjet );
   theTree->SetBranchAddress( "etajet", &myetajet );
   theTree->SetBranchAddress( "masstop", &mymasstop );
//   theTree->SetBranchAddress( "mtw", &mymtw );
   theTree->SetBranchAddress( "deltaRphotonjet", &mydeltaRphotonjet );
   theTree->SetBranchAddress( "deltaRphotonmuon", &mydeltaRphotonmuon );
//   theTree->SetBranchAddress( "ht", &myht );
    theTree->SetBranchAddress( "costopphoton", &mycostopphoton );
    theTree->SetBranchAddress( "jetmultiplicity", &myjetmultiplicity );
//   theTree->SetBranchAddress( "bjetmultiplicity", &mybjetmultiplicity );
    theTree->SetBranchAddress( "deltaphiphotonmet", &mydeltaphiphotonmet );
    theTree->SetBranchAddress( "cvsdiscriminant", &mycvsdiscriminant );
//   theTree->SetBranchAddress( "leptoncharge", &myleptoncharge );
 // 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++) {
                       //   std::cout << "--- ... Processing event: " << ievt << std::endl;
                             if (ievt%1000 == 0) std::cout << "--- ... Processing event: " << ievt << std::endl;
 
                                   theTree->GetEntry(ievt);
                                   //for (int l=0;l<sizeof(myptphoton);l++){
                                   //std::cout << "--- ... reza: " << myptphoton[l] <<std::endl;
                                   //}
      //                             std::cout << "--- ......................."<< (*mycvsdiscriminant)[0]<<std::endl;
                                         // --- Return the MVA outputs and fill into histograms
                                         ptphoton=(float)(*myptphoton)[0];
                                         etaphoton=(float)(*myetaphoton )[0];
                                         ptmuon=(float)(*myptmuon )[0];
                                         etamuon=(float)(*myetamuon )[0];
                                         ptjet=(float)(*myptjet )[0];
                                         etajet=(float)(*myetajet )[0];
                                        masstop=(float)(*mymasstop )[0];
                                        //mtw=(float)(*mymtw )[0];
                                         deltaRphotonjet=(float)(*mydeltaRphotonjet )[0];
                                        deltaRphotonmuon=(float)(*mydeltaRphotonmuon )[0];
                                         //ht=(float)(*myht )[0];
                                        costopphoton=(float)(*mycostopphoton )[0];
                                         jetmultiplicity=(float)(*myjetmultiplicity )[0];
                                         //bjetmultiplicity=(float)(*mybjetmultiplicity )[0];
                                         deltaphiphotonmet=(float)(*mydeltaphiphotonmet )[0];
                                         cvsdiscriminant=(float)(*mycvsdiscriminant)[0];
                                         //leptoncharge=(float)(*myleptoncharge )[0];
      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"         ) );

revDATAhists[idx]->Fill( reader->EvaluateMVA( "BDT method"           ) );}
   sw.Stop();
   std::cout << "--- End of event loop: "; sw.Print();

delete myptphoton;
delete myetaphoton;
delete myptmuon;
delete myetamuon;
delete myptjet;
delete myetajet;
delete mymasstop;
//delete mymtw;
delete mydeltaRphotonjet;
delete mydeltaRphotonmuon;
//delete myht;
delete mycostopphoton;
delete mydeltaphiphotonmet;
delete mycvsdiscriminant;
delete myjetmultiplicity;
////delete mybjetmultiplicity;
////delete myleptoncharge;
////delete myplot;
//
delete input;
}
for(unsigned int idx=1; idx<datasamplesreverse_.size(); ++idx){
revDATAhists[idx]->Add(revDATAhists[idx-1]);
 } 
revDATAhists[2]->Scale(620.32/revDATAhists[2]->Integral());
hList->Add(revDATAhists[2]);
cout<<revDATAhists[2]->Integral()<<endl;
TFile *target  = new TFile( "MVApp.root","RECREATE" );
  hList->Write();
   target->Close();

} 
Esempio n. 13
0
/*
void allBranches(TTree* inTree){
  
  inTree->SetBranchAddress("fullpmet",&fullpmet);
  inTree->SetBranchAddress("trkpmet",&trkpmet);
  inTree->SetBranchAddress("ratioMet",&ratioMet);
  inTree->SetBranchAddress("ptll",&ptll);
  inTree->SetBranchAddress("mth",&mth);
  inTree->SetBranchAddress("jetpt1",&jetpt1);
  inTree->SetBranchAddress("ptWW",&ptWW);
  inTree->SetBranchAddress("dphilljet",&dphilljet);
  inTree->SetBranchAddress("dphillmet",&dphillmet);
  inTree->SetBranchAddress("dphijet1met",&dphijet1met);
  inTree->SetBranchAddress("nvtx",&nvtx);

  inTree->SetBranchAddress("baseW",&baseW);        
}

void createOutput(TTree* outTree){

  outTree->Branch("fullpmet",&fullpmet);
  outTree->Branch("trkpmet",&trkpmet);
  outTree->Branch("ratioMet",&ratioMet);
  outTree->Branch("ptll",&ptll);
  outTree->Branch("mth",&mth);
  outTree->Branch("jetpt1",&jetpt1);
  outTree->Branch("ptWW",&ptWW);
  outTree->Branch("dphilljet",&dphilljet);
  outTree->Branch("dphillmet",&dphillmet);
  outTree->Branch("dphijet1met",&dphijet1met);
  outTree->Branch("nvtx",&nvtx);

  outTree->Branch("baseW",&baseW);        

}
*/
void read(TString sampleName = "WW")
{
  //calling the reader of the MVA analysis
  TMVA::Reader* reader = new TMVA::Reader("");
  
  reader->AddVariable("fullpmet",&fullpmet);
  reader->AddVariable("trkpmet",&trkpmet);
  reader->AddVariable("ratioMet",&ratioMet);
  reader->AddVariable("ptll",&ptll);
  reader->AddVariable("mth",&mth);
  reader->AddVariable("jetpt1",&jetpt1);
  reader->AddVariable("ptWW",&ptWW);
  reader->AddVariable("dphilljet",&dphilljet);
  reader->AddVariable("dphillmet",&dphillmet);
  reader->AddVariable("dphijet1met",&dphijet1met);
  reader->AddVariable("nvtx",&nvtx);
  
  //reader->BookMVA("Fisher", "weights/MVAnalysis_Fisher.weights.xml");
  reader->BookMVA("BDT", "weights/" + sampleName + "_BDT.weights.xml");
  
  /*
  //Calling WW Signal File and Tree and Creating Output Signal Trees 
  TFile *MySigFile = new TFile("../rootFiles/AllJet/OF/WW.root","READ");
  TTree* MySigTree = (TTree*)MySigFile->Get("nt");

  MySigTree->SetBranchAddress("fullpmet",&fullpmet);
  MySigTree->SetBranchAddress("trkpmet",&trkpmet);
  MySigTree->SetBranchAddress("ratioMet",&ratioMet);
  MySigTree->SetBranchAddress("ptll",&ptll);
  MySigTree->SetBranchAddress("mth",&mth);
  MySigTree->SetBranchAddress("jetpt1",&jetpt1);
  MySigTree->SetBranchAddress("ptWW",&ptWW);
  MySigTree->SetBranchAddress("dphilljet",&dphilljet);
  MySigTree->SetBranchAddress("dphillmet",&dphillmet);
  MySigTree->SetBranchAddress("dphijet1met",&dphijet1met);
  MySigTree->SetBranchAddress("nvtx",&nvtx);

  TTree *outSigTree = new TTree ("Out","Out");
  outSigTree -> SetDirectory(0);
  createOutput(outSigTree);
  */
  const int nProcesses = 2;

  enum{iWW, iDY};

  TFile *MyFile[nProcesses];
  TTree *MyTree[nProcesses];
  TTree *outTree[nProcesses];

  TString MyName[nProcesses];

  MyName[iWW]    = "WW";
  MyName[iDY]    = "DY";

  Float_t pt1;
  Float_t pt2;
  Float_t ptll;
  Float_t mll;
  Float_t mth;
  Float_t pfType1Met;
  Float_t drll;
  Float_t dphill;
  Float_t dphilljet;
  Float_t dphillmet;
  Float_t trkMet;
  Float_t Mt1;
  Float_t Mt2;
  Float_t mpmet;
  Float_t Mc;
  Float_t ptWW;
  Float_t Ht;

  Float_t cutpt1        = 20;
  Float_t cutpt2        = 20;
  Float_t cutptll       = 0;
  Float_t cutmll        = 0;
  Float_t cutmth        = 0;
  Float_t cutpfType1Met = 0;
  Float_t cutdrll       = 0;
  Float_t cutdphill     = 0;
  Float_t cutdphilljet  = 0;
  Float_t cutdphillmet  = 0;
  Float_t cuttrkMet     = 0;
  Float_t cutMt1        = 0;
  Float_t cutMt2        = 0;
  Float_t cutmpmet      = 0;
  Float_t cutMc         = 0;
  Float_t cutptWW       = 0;
  Float_t cutHt         = 0;
  Float_t cutValue      = 0.022;

  Float_t value         = 0;

  //Loop Over All Processes
  for (int i = 0; i < nProcesses; ++i){
    MyFile[i] = new TFile("../rootFiles/SF/MediumIDTighterIP/" + MyName[i] + ".root","READ");
    MyTree[i] = (TTree*)MyFile[i]->Get("nt");
    
    //Calling Processes Trees 
    MyTree[i]->SetBranchAddress("fullpmet",&fullpmet);
    MyTree[i]->SetBranchAddress("trkpmet",&trkpmet);
    MyTree[i]->SetBranchAddress("ratioMet",&ratioMet);
    MyTree[i]->SetBranchAddress("ptll",&ptll);
    MyTree[i]->SetBranchAddress("mth",&mth);
    MyTree[i]->SetBranchAddress("jetpt1",&jetpt1);
    MyTree[i]->SetBranchAddress("ptWW",&ptWW);
    MyTree[i]->SetBranchAddress("dphilljet",&dphilljet);
    MyTree[i]->SetBranchAddress("dphillmet",&dphillmet);
    MyTree[i]->SetBranchAddress("dphijet1met",&dphijet1met);
    MyTree[i]->SetBranchAddress("nvtx",&nvtx);

    MyTree[i]->SetBranchAddress("pt1",&pt1);        
    MyTree[i]->SetBranchAddress("pt2",&pt2);        
    MyTree[i]->SetBranchAddress("ptll",&ptll);       
    MyTree[i]->SetBranchAddress("mll",&mll);        
    MyTree[i]->SetBranchAddress("mth",&mth);        
    MyTree[i]->SetBranchAddress("pfType1Met",&pfType1Met); 
    MyTree[i]->SetBranchAddress("drll",&drll);       
    MyTree[i]->SetBranchAddress("dphill",&dphill);     
    MyTree[i]->SetBranchAddress("dphilljet",&dphilljet);  
    MyTree[i]->SetBranchAddress("dphillmet",&dphillmet);  
    MyTree[i]->SetBranchAddress("trkMet",&trkMet);     
    //MyTree[i]->SetBranchAddress("Mt1",&Mt1);        
    //MyTree[i]->SetBranchAddress("Mt2",&Mt2);        
    MyTree[i]->SetBranchAddress("mpmet",&mpmet);      
    //MyTree[i]->SetBranchAddress("Mc",&Mc);         
    MyTree[i]->SetBranchAddress("ptWW",&ptWW);       
    MyTree[i]->SetBranchAddress("Ht",&Ht);         

    //Creating Output Trees
    outTree[i] = new TTree (MyName[i],MyName[i]);
    outTree[i] -> SetDirectory(0);

    outTree[i] -> Branch("fullpmet",&fullpmet);
    outTree[i] -> Branch("trkpmet",&trkpmet);
    outTree[i] -> Branch("ratioMet",&ratioMet);
    outTree[i] -> Branch("ptll",&ptll);
    outTree[i] -> Branch("mth",&mth);
    outTree[i] -> Branch("jetpt1",&jetpt1);
    outTree[i] -> Branch("ptWW",&ptWW);
    outTree[i] -> Branch("dphilljet",&dphilljet);
    outTree[i] -> Branch("dphillmet",&dphillmet);
    outTree[i] -> Branch("dphijet1met",&dphijet1met);
    outTree[i] -> Branch("nvtx",&nvtx);  
    outTree[i] -> Branch("baseW",&baseW);        
  
    outTree[i] -> Branch("pt1",&pt1);        
    outTree[i] -> Branch("pt2",&pt2);        
    outTree[i] -> Branch("ptll",&ptll);       
    outTree[i] -> Branch("mll",&mll);        
    outTree[i] -> Branch("mth",&mth);        
    outTree[i] -> Branch("pfType1Met",&pfType1Met); 
    outTree[i] -> Branch("drll",&drll);       
    outTree[i] -> Branch("dphill",&dphill);     
    outTree[i] -> Branch("dphilljet",&dphilljet);  
    outTree[i] -> Branch("dphillmet",&dphillmet);  
    outTree[i] -> Branch("trkMet",&trkMet);     
    //outTree[i] -> Branch("Mt1",&Mt1);        
    //outTree[i] -> Branch("Mt2",&Mt2);        
    outTree[i] -> Branch("mpmet",&mpmet);      
    //outTree[i] -> Branch("Mc",&Mc);         
    outTree[i] -> Branch("ptWW",&ptWW);       
    outTree[i] -> Branch("Ht",&Ht);         

    //Applying Selections
    Int_t cont = 0;
    for (int j = 0; j < MyTree[i]->GetEntries(); ++j){
      if (j == 0) cout<<MyName[i]<<": "<<MyTree[i]->GetEntries()<<endl;
      MyTree[i]->GetEntry(j);
      
      if (pt1        < cutpt1)        continue;
      if (pt2        < cutpt2)        continue;
      if (ptll       < cutptll)       continue;
      if (mll        < cutmll)        continue;
      if (mth        < cutmth)        continue;
      if (pfType1Met < cutpfType1Met) continue;
      if (drll       < cutdrll)       continue;
      if (dphill     < cutdphill)     continue;
      if (dphilljet  < cutdphilljet)  continue;
      if (dphillmet  < cutdphillmet)  continue;
      if (trkMet     < cuttrkMet)     continue;
      //      if (Mt1        < cutMt1)        continue;
      //if (Mt2        < cutMt2)        continue;
      if (mpmet      < cutmpmet)      continue;
      //if (Mc         < cutMc)         continue;
      if (ptWW       < cutptWW)       continue;
      if (Ht         < cutHt)         continue;
      
      value = reader->EvaluateMVA("BDT");
      if(value       < cutValue)      continue;
      
      ++cont;
      outTree[i]->Fill();
    }
    cout<<MyName[i]<<" survived: "<<cont<<" ("<<100 * cont / MyTree[i]->GetEntries()<<"%)"<<endl;  
  }

  /*
  //applying selections on ZH sample
  Int_t contZH = 0;
  for (int i = 0; i < ZHTree->GetEntries(); ++i){
    if (i == 0) cout<<"ZH Entries : "<<ZHTree->GetEntries()<<endl;
    ZHTree->GetEntry(i);

    if (pt1        < cutpt1)        continue;
    if (pt2        < cutpt2)        continue;
    if (ptll       < cutptll)       continue;
    if (mll        < cutmll)        continue;
    if (mth        < cutmth)        continue;
    if (pfType1Met < cutpfType1Met) continue;
    if (drll       < cutdrll)       continue;
    if (dphill     < cutdphill)     continue;
    if (dphilljet  < cutdphilljet)  continue;
    if (dphillmet  < cutdphillmet)  continue;
    if (trkMet     < cuttrkMet)     continue;
    if (Mt1        < cutMt1)        continue;
    if (Mt2        < cutMt2)        continue;
    if (mpmet      < cutmpmet)      continue;
    if (Mc         < cutMc)         continue;
    if (ptWW       < cutptWW)       continue;
    if (Ht         < cutHt)         continue;

    value = reader->EvaluateMVA("BDT");
    if(value       < cutValue)      continue;

    ++contZH;
    outZHTree->Fill();
  }
    
  cout<<"ZH survived: "<<contZH<<endl;  

  //applying selections on HWW sample
  Int_t contHWW = 0;
  for (int i = 0; i < HWWTree->GetEntries(); ++i){
    if (i == 0) cout<<"HWW Entries : "<<HWWTree->GetEntries()<<endl;
    HWWTree->GetEntry(i);

    if (pt1        < cutpt1)        continue;
    if (pt2        < cutpt2)        continue;
    if (ptll       < cutptll)       continue;
    if (mll        < cutmll)        continue;
    if (mth        < cutmth)        continue;
    if (pfType1Met < cutpfType1Met) continue;
    if (drll       < cutdrll)       continue;
    if (dphill     < cutdphill)     continue;
    if (dphilljet  < cutdphilljet)  continue;
    if (dphillmet  < cutdphillmet)  continue;
    if (trkMet     < cuttrkMet)     continue;
    if (Mt1        < cutMt1)        continue;
    if (Mt2        < cutMt2)        continue;
    if (mpmet      < cutmpmet)      continue;
    if (Mc         < cutMc)         continue;
    if (ptWW       < cutptWW)       continue;
    if (Ht         < cutHt)         continue;

    value = reader->EvaluateMVA("BDT");
    if(value       < cutValue)      continue;

    ++contHWW;
    outHWWTree->Fill();
  }

  cout<<"HWW survived: "<<contHWW<<endl;
  
  //applying selections on WW sample
  Int_t contWW = 0;
  for (int i = 0; i < WWTree->GetEntries(); ++i){
    if (i == 0) cout<<"WW Entries : "<<WWTree->GetEntries()<<endl;
    WWTree->GetEntry(i);

    if (pt1        < cutpt1)        continue;
    if (pt2        < cutpt2)        continue;
    if (ptll       < cutptll)       continue;
    if (mll        < cutmll)        continue;
    if (mth        < cutmth)        continue;
    if (pfType1Met < cutpfType1Met) continue;
    if (drll       < cutdrll)       continue;
    if (dphill     < cutdphill)     continue;
    if (dphilljet  < cutdphilljet)  continue;
    if (dphillmet  < cutdphillmet)  continue;
    if (trkMet     < cuttrkMet)     continue;
    if (Mt1        < cutMt1)        continue;
    if (Mt2        < cutMt2)        continue;
    if (mpmet      < cutmpmet)      continue;
    if (Mc         < cutMc)         continue;
    if (ptWW       < cutptWW)       continue;
    if (Ht         < cutHt)         continue;

    value = reader->EvaluateMVA("BDT");
    if(value             < cutValue)           continue;
  
    ++contWW;
    outWWTree->Fill();
  }
  
  cout<<"WW survived: "<<contWW<<endl;
*/
  //saving trees
  TFile *outMVA = new TFile("outMVA" + sampleName + ".root","RECREATE");
  outMVA -> cd();
  for (int y = 0; y < nProcesses; ++y)
    outTree[y] -> Write();
  outMVA -> Close();
}
Esempio n. 14
0
//_____________________________________________________________________________
void dumpCats(bool debug, TString fileName, TString dirName,
              bool smearMassError) {
 
  TFile* file = TFile::Open(fileName.Data());
  TDirectory* theDir = (TDirectory*) file->FindObjectAny(dirName.Data());
  TTree* theTree = (TTree*) theDir->Get("hPhotonTree");

  UInt_t run, lumi, evt;
  float rho, mass;
  Int_t tth, vhLep, vhMet, vhHad, vbf, cat;


  theTree->SetBranchAddress("run", &run);
  theTree->SetBranchAddress("lumi",&lumi);
  theTree->SetBranchAddress("evt", &evt);

  theTree->SetBranchAddress("mass",&mass);
  theTree->SetBranchAddress("rho",&rho);

  theTree->SetBranchAddress("tthTag",&tth);
  theTree->SetBranchAddress("VHLepTag",&vhLep);
  theTree->SetBranchAddress("VHHadTag",&vhHad);
  theTree->SetBranchAddress("vbfTag",&vbf);
  
  float ph1e, ph1pt, ph1eerr, ph1eerrsmeared, teta1, phi1;
  float ph2e, ph2pt, ph2eerr, ph2eerrsmeared, teta2, phi2;

  theTree->SetBranchAddress("ph1.pt",&ph1pt);
  theTree->SetBranchAddress("ph1.e",&ph1e);
  theTree->SetBranchAddress("ph1.eerr",&ph1eerr);
  theTree->SetBranchAddress("ph1.eerrsmeared",&ph1eerrsmeared);
  theTree->SetBranchAddress("ph1.eta",&teta1);
  theTree->SetBranchAddress("ph1.phi",&phi1);

  theTree->SetBranchAddress("ph2.pt",&ph2pt);
  theTree->SetBranchAddress("ph2.e",&ph2e);
  theTree->SetBranchAddress("ph2.eerr",&ph2eerr);
  theTree->SetBranchAddress("ph2.eerrsmeared",&ph2eerrsmeared);
  theTree->SetBranchAddress("ph2.eta",&teta2);
  theTree->SetBranchAddress("ph2.phi",&phi2);

  Float_t ele1_pt, ele1_eta;
  Float_t mu1_pt, mu1_eta;
  
  theTree->SetBranchAddress("elePt"  , &ele1_pt );
  theTree->SetBranchAddress("eleEta" , &ele1_eta);
  theTree->SetBranchAddress("muonPt" , &mu1_pt  );
  theTree->SetBranchAddress("muonEta", &mu1_eta );
  
  float jet1pt, jet1eta, jet1phi;
  float jet2pt, jet2eta, jet2phi;

  theTree->SetBranchAddress("jet1pt",&jet1pt);
  theTree->SetBranchAddress("jet1eta",&jet1eta);
  theTree->SetBranchAddress("jet1phi",&jet1phi);
  theTree->SetBranchAddress("jet2pt",&jet2pt);
  theTree->SetBranchAddress("jet2eta",&jet2eta);
  theTree->SetBranchAddress("jet2phi",&jet2phi);

  float masserr, masserrwvtx, masserr_ns, masserrwvtx_ns, vtxprob, idmva_1, idmva_2;
  theTree->SetBranchAddress("masserrsmeared",&masserr);
  theTree->SetBranchAddress("masserrsmearedwrongvtx",&masserrwvtx);
  theTree->SetBranchAddress("masserr",&masserr_ns);
  theTree->SetBranchAddress("masserrwrongvtx",&masserrwvtx_ns);
  theTree->SetBranchAddress("vtxprob",&vtxprob);
  theTree->SetBranchAddress("ph1.idmva",&idmva_1);
  theTree->SetBranchAddress("ph2.idmva",&idmva_2);

  // MET tag stuff
  float corrpfmet, corrpfmetphi, pfmet, pfmetphi;
  theTree->SetBranchAddress("corrpfmet",&corrpfmet);
  theTree->SetBranchAddress("corrpfmetphi",&corrpfmetphi);
  theTree->SetBranchAddress("pfmet",&pfmet);
  theTree->SetBranchAddress("pfmetphi",&pfmetphi);

  float phigg, jetleadNoIDpt, jetleadNoIDphi, jetleadNoIDeta;
  float ph1sceta, ph1scphi;
  float ph2sceta, ph2scphi;

  theTree->SetBranchAddress("phigg",&phigg);
  theTree->SetBranchAddress("jetleadNoIDpt",&jetleadNoIDpt);
  theTree->SetBranchAddress("jetleadNoIDphi",&jetleadNoIDphi);
  theTree->SetBranchAddress("jetleadNoIDeta",&jetleadNoIDeta);

  theTree->SetBranchAddress("ph1.sceta",&ph1sceta);
  theTree->SetBranchAddress("ph1.scphi",&ph1scphi);

  theTree->SetBranchAddress("ph2.sceta",&ph2sceta);
  theTree->SetBranchAddress("ph2.scphi",&ph2scphi);
 

  // Setup the diphoton BDT
  Float_t rVtxSigmaMoM, wVtxSigmaMoM, cosDPhi;
  Float_t pho1_ptOverM;
  Float_t pho2_ptOverM;
  Float_t diphoMVA;
  
  TMVA::Reader* reader = new TMVA::Reader("Silent");
  reader->AddVariable("masserrsmeared/mass"        , &rVtxSigmaMoM);
  reader->AddVariable("masserrsmearedwrongvtx/mass", &wVtxSigmaMoM);
  reader->AddVariable("vtxprob"                    , &vtxprob     );
  reader->AddVariable("ph1.pt/mass"                , &pho1_ptOverM);
  reader->AddVariable("ph2.pt/mass"                , &pho2_ptOverM);
  reader->AddVariable("ph1.eta"                    , &teta1       );
  reader->AddVariable("ph2.eta"                    , &teta2       );
  reader->AddVariable("TMath::Cos(ph1.phi-ph2.phi)", &cosDPhi     );
  reader->AddVariable("ph1.idmva"                  , &idmva_1     );
  reader->AddVariable("ph2.idmva"                  , &idmva_2     );
  const char *diphotonWeights = (
    "/home/veverka/cms/cmssw/031/CMSSW_5_3_10_patch1/src/MitPhysics/data/"
    "HggBambu_SMDipho_Oct01_redqcdweightallsigevenbkg_BDTG.weights.xml"
    );
  reader->BookMVA("BDTG", diphotonWeights);

  TRandom3 rng(0);

  int eventCounter=0;

  // Loop over the entries.
  std::cout << "Looping over " << theTree->GetEntries() << " entries." << std::endl;
  for (int i=0; i < theTree->GetEntries(); ++i) {
   
    if (eventCounter > 9 && debug ) break;
    if (debug) {
      cout << "Processing entry " << i << " :" << endl
           << "    mass:   " << mass << endl
           << "    ph1pt:  " << ph1pt << endl
           << "    ph2pt:  " << ph2pt << endl
           << "    idmva_1:" << idmva_1 << endl
           << "    idmva_2:" << idmva_2 << endl;
    }
    
    theTree->GetEntry(i);

    // MET category
    vhMet = 0;
    double dEtaJPh1 = ph1sceta - jetleadNoIDeta;
    double dPhiJPh1 = TMath::ACos(TMath::Cos(ph1scphi - jetleadNoIDphi));
    double dRJPh1 = TMath::Sqrt(TMath::Power(dEtaJPh1, 2) +
                                TMath::Power(dPhiJPh1, 2));
    double dEtaJPh2 = ph2sceta - jetleadNoIDeta;
    double dPhiJPh2 = TMath::ACos(TMath::Cos(ph2scphi - jetleadNoIDphi));
    double dRJPh2 = TMath::Sqrt(TMath::Power(dEtaJPh2, 2) +
                                TMath::Power(dPhiJPh2, 2));
    double dPhiMetGG = TMath::ACos(TMath::Cos(phigg - corrpfmetphi));
    double dPhiMetJet = TMath::ACos(
             TMath::Cos(TMath::Abs(jetleadNoIDphi - corrpfmetphi))
             );
    if (TMath::Abs(ph1sceta) < 1.4442 &&
        TMath::Abs(ph2sceta) < 1.4442 &&
        corrpfmet > 70. &&
        ph1pt/mass > 45./120. &&
        dPhiMetGG > 2.1 &&
        (
          jetleadNoIDpt < 50. ||
          dRJPh1 < 0.5 ||
          dRJPh2 < 0.5 ||
          dPhiMetJet < 2.7
        ) &&
        ph2pt > mass/4) {
       vhMet = 1;
    }

    // Calculate needed variables for the diphoMVA
    if (smearMassError) {
      rVtxSigmaMoM = masserr / mass;          // with smearing
      wVtxSigmaMoM = masserrwvtx / mass;      // with smearing
    } else {
      rVtxSigmaMoM = masserr_ns / mass;       // no smearing
      wVtxSigmaMoM = masserrwvtx_ns / mass;   // no smearing
    }
    cosDPhi = TMath::Cos(phi1 - phi2);
    pho1_ptOverM = ph1pt / mass;
    pho2_ptOverM = ph2pt / mass;
    diphoMVA = reader->EvaluateMVA("BDTG");

    bool passPreselection = (mass > 100 &&
                             mass < 180 &&
                             ph1pt > mass/3 &&
                             ph2pt > mass/4 &&
                             idmva_1 > -0.2 &&
                             idmva_2 > -0.2);

    if (passPreselection == false) {
      if (debug) {
        cout << "    passPreselection: " << passPreselection << endl;
      }
      continue;
    }

    if (debug) {
      cout << "    ... passed preselection." << endl;
    }

    eventCounter++;
    if      (tth   == 1) tth = 2;
    else if (tth   == 2) tth = 1;

    if      (vhHad == 2) vhHad = 1;

    cat = kIncl0;
    if      (tth   == 2) cat = kTTHLep;
    else if (vhLep == 2) cat = kVHLepTight;
    else if (vhLep == 1) cat = kVHLepLoose;
    else if (vbf   >  0) cat = kDijet0;
    else if (vhMet == 1) cat = kVHMet;
    else if (tth   == 1) cat = kTTHHad;
    else if (vhHad == 1) cat = kVHHad;

    // if (cat == kIncl0 && diphoMVA < -0.4) continue;
    
    // Event Variables
    dumpVar("run"                    , run                    ); //  1
    dumpVar("lumi"                   , lumi                   ); //  2
    dumpVar("event"                  , evt                    ); //  3

    dumpVar("cat"                    , cat                    );
    dumpVar("tth"                    , tth                    );
    dumpVar("vhLep"                  , vhLep                  );
    dumpVar("vhMet"                  , vhMet                  );
    dumpVar("vhHad"                  , vhHad                  );
    dumpVar("vbf"                    , vbf                    );


    // Leading Photon Variables
    dumpVar("pho1_e"                 , ph1e                   ); // 10
    dumpVar("pho1_eErr"              , ph1eerr                ); // 11
    dumpVar("pho1_eta"               , teta1                  ); //  8
    dumpVar("pho1_phi"               , phi1                   ); //  9
    dumpVar("pho1_idMVA"             , idmva_1                );

    // Trailing Photon Variables
    dumpVar("pho2_e"                 , ph2e                   ); // 36
    dumpVar("pho2_eErr"              , ph2eerr                ); // 37
    dumpVar("pho2_eta"               , teta2                  ); // 34
    dumpVar("pho2_phi"               , phi2                   ); // 35
    dumpVar("pho2_idMVA"             , idmva_2                );

    // Diphoton Variables
    dumpVar("mass"                   , mass                   );
    dumpVar("met"                    , corrpfmet              );
    dumpVar("met_phi"                , corrpfmetphi           );
    dumpVar("uncorrMet"              , pfmet                  );
    dumpVar("uncorrMet_phi"          , pfmetphi               );
    dumpVar("diphoMVA"               , diphoMVA               );

    // Muon Variables
    if (mu1_pt < 0) {
      mu1_pt = -999;
      mu1_eta = -999;
    }
    dumpVar("mu1_pt"                , mu1_pt                 );
    dumpVar("mu1_eta"               , mu1_eta                );

    // Electron Variables
    if (ele1_pt < 0) {
      ele1_pt = -999;
      ele1_eta = -999;
    }
    dumpVar("ele1_pt"                , ele1_pt                 );
    dumpVar("ele1_eta"               , ele1_eta                );

    
    // Leading Jet Variables
    if (jet1pt < 0) {
      jet1pt = -999;
      jet1eta = -999;
    }
    dumpVar("jet1_pt"                , jet1pt                 ); // 69
    dumpVar("jet1_eta"               , jet1eta                ); // 70
    dumpVar("jet1_phi"               , jet1phi                ); // 70

    // Trailing Jet Variables
    if (jet2pt < 0) {
      jet2pt = -999;
      jet2eta = -999;
    }
    dumpVar("jet2_pt"                , jet2pt                 ); // 72
    dumpVar("jet2_eta"               , jet2eta                ); // 73
    dumpVar("jet2_phi"               , jet2phi                ); // 70

    std::cout << std::endl;
  } // Loop over the tree entries.
  
  return;

} // void dumpMvaInputs(bool debug, TString fileName)
Esempio n. 15
0
int testPyKerasRegression(){
   // Get data file
   std::cout << "Get test data..." << std::endl;
   TString fname = "./tmva_reg_example.root";
   if (gSystem->AccessPathName(fname))  // file does not exist in local directory
      gSystem->Exec("curl -O http://root.cern.ch/files/tmva_reg_example.root");
   TFile *input = TFile::Open(fname);

   // Build model from python file
   std::cout << "Generate keras model..." << std::endl;
   UInt_t ret;
   ret = gSystem->Exec("echo '"+pythonSrc+"' > generateKerasModelRegression.py");
   if(ret!=0){
       std::cout << "[ERROR] Failed to write python code to file" << std::endl;
       return 1;
   }
   ret = gSystem->Exec("python generateKerasModelRegression.py");
   if(ret!=0){
       std::cout << "[ERROR] Failed to generate model using python" << std::endl;
       return 1;
   }

   // Setup PyMVA and factory
   std::cout << "Setup TMVA..." << std::endl;
   TMVA::PyMethodBase::PyInitialize();
   TFile* outputFile = TFile::Open("ResultsTestPyKerasRegression.root", "RECREATE");
   TMVA::Factory *factory = new TMVA::Factory("testPyKerasRegression", outputFile,
      "!V:Silent:Color:!DrawProgressBar:AnalysisType=Regression");

   // Load data
   TMVA::DataLoader *dataloader = new TMVA::DataLoader("datasetTestPyKerasRegression");

   TTree *tree = (TTree*)input->Get("TreeR");
   dataloader->AddRegressionTree(tree);

   dataloader->AddVariable("var1");
   dataloader->AddVariable("var2");
   dataloader->AddTarget("fvalue");

   dataloader->PrepareTrainingAndTestTree("",
      "SplitMode=Random:NormMode=NumEvents:!V");

   // Book and train method
   factory->BookMethod(dataloader, TMVA::Types::kPyKeras, "PyKeras",
      "!H:!V:VarTransform=D,G:FilenameModel=kerasModelRegression.h5:FilenameTrainedModel=trainedKerasModelRegression.h5:NumEpochs=10:BatchSize=32:SaveBestOnly=false:Verbose=0");
   std::cout << "Train model..." << std::endl;
   factory->TrainAllMethods();

   // Clean-up
   delete factory;
   delete dataloader;
   delete outputFile;

   // Setup reader
   UInt_t numEvents = 100;
   std::cout << "Run reader and estimate target of " << numEvents << " events..." << std::endl;
   TMVA::Reader *reader = new TMVA::Reader("!Color:Silent");
   Float_t vars[3];
   reader->AddVariable("var1", vars+0);
   reader->AddVariable("var2", vars+1);
   reader->BookMVA("PyKeras", "datasetTestPyKerasRegression/weights/testPyKerasRegression_PyKeras.weights.xml");

   // Get mean squared error on events
   tree->SetBranchAddress("var1", vars+0);
   tree->SetBranchAddress("var2", vars+1);
   tree->SetBranchAddress("fvalue", vars+2);

   Float_t meanMvaError = 0;
   for(UInt_t i=0; i<numEvents; i++){
      tree->GetEntry(i);
      meanMvaError += std::pow(vars[2]-reader->EvaluateMVA("PyKeras"),2);
   }
   meanMvaError = meanMvaError/float(numEvents);

   // Check whether the response is obviously better than guessing
   std::cout << "Mean squared error: " << meanMvaError << std::endl;
   if(meanMvaError > 30.0){
      std::cout << "[ERROR] Mean squared error is " << meanMvaError << " (>30.0)" << std::endl;
      return 1;
   }

   return 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;
} 
Esempio n. 17
0
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();
}
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;
} 
Esempio n. 19
0
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");
} 
Esempio n. 20
0
//------------------------------------------------------------------------------
// 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;

}
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;
} 
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;
} 
Esempio n. 23
0
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;
} 
Esempio n. 24
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;

}
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;
} 
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;
} 
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;
} 
Esempio n. 28
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();
}
Esempio n. 29
0
//------------------------------------------------------------------------------
// 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;
}
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 () ;
 }
 
 
}