示例#1
0
int reader_wrapper::bookReader( TString xml_file_name) {
  m_reader = new TMVA::Reader("!Color:Silent");
  for (auto var : m_spectators) {
    m_reader->AddSpectator(var.formula, &var.value);
  }
  for (auto& var : m_variables) {
    m_reader->AddVariable(var.formula, &var.value);
  }
  m_reader->BookMVA( m_methodName, xml_file_name );
  return 0;
}
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 TMVARegressionApplication( int wMs,int wM, string st,string st2,string option="",TString myMethodList = "" ) 
{
   //---------------------------------------------------------------
   // This loads the library
   TMVA::Tools::Instance();

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

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

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

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

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

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

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

   // --- Create the Reader object

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

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

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

   // --- Book the MVA methods

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

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

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

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

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

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

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

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

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

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

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

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

   // --- Create the Reader object

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

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

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

   // --- Book the MVA methods

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

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

   // --- Event loop

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

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

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

      theTree->GetEntry(ievt);

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

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

   // --- Write histograms

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

   std::cout << "--- Created root file: \"" << target->GetName() 
             << "\" containing the MVA output histograms" << std::endl;
  
   delete reader;
    
   std::cout << "==> TMVARegressionApplication is done!" << std::endl << std::endl;
}
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( 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 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;
} 
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;
} 
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 TMVAClassificationApplication( TString myMethodList = "" ) 
{   
#ifdef __CINT__
   gROOT->ProcessLine( ".O0" ); // turn off optimization in CINT
#endif

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

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

   // set verbosity
   //TMVA::Tools::Instance().Log().SetMinType(kINFO);

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

   Use["BDT"]             = 1; // uses Adaptive Boost
   Use["Category"]        = 1;

   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" );    
   reader->SetMsgType(kINFO);


   // CMS STATS:
   //
   // 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 met;
   Float_t HT;
   Float_t minMLB;
   Float_t leptonJetsMETSum;
   reader->AddVariable( "met",              &met );
   reader->AddVariable( "HT",               &HT );
   reader->AddVariable( "minMLB",           &minMLB );
   reader->AddVariable( "leptonJetsMETSum", &leptonJetsMETSum );



   // CMS STATS:
   // *** VERY IMPORTANT! ***
   // TMVA notoriously has problems with integer and other non-float branches.
   // Better not to use them at all and convert them to Float_t. If you happen
   // to have integer branches that you need, as in this example, you should create
   // corresponding float spectator variables and assign them in the event loop.
   // 
   // Spectator variables declared in the training have to be added to the reader, too
   //
   // Note that the corresponding branches are integer, so we create floats too!
   Int_t nBTag;
   Int_t nJets;
   Int_t nLeptons;
   Int_t isMuon1;
   Int_t isMuon2;
   Int_t isMuon3;
   Int_t isMuon4;
   Float_t nBTagFloat;
   Float_t nJetsFloat;
   Float_t nLeptonsFloat;
   Float_t isMuon1Float;
   Float_t isMuon2Float;
   Float_t isMuon3Float;
   Float_t isMuon4Float;
   Float_t leptonSumMass;
   reader->AddSpectator( "nBTag", &nBTagFloat );
   reader->AddSpectator( "nJets", &nJetsFloat );
   reader->AddSpectator( "nLeptons", &nLeptonsFloat );
   reader->AddSpectator( "isMuon1", &isMuon1Float );
   reader->AddSpectator( "isMuon2", &isMuon2Float );
   reader->AddSpectator( "isMuon3", &isMuon3Float );
   reader->AddSpectator( "isMuon4", &isMuon4Float );
   reader->AddSpectator( "leptonSumMass", &leptonSumMass );



   // CMS STATS:
   // cut definitions. Define categories and overall selection.
#include "TMVA_tprime_cuts.C"



   // Add artificial spectators for distinguishing categories
   Float_t Category_mycat1, Category_mycat2, Category_mycat3, Category_mycat4;
   TString sCat1("Category_cat1:=");
   TString sCat2("Category_cat2:=");
   TString sCat3("Category_cat3:=");
   TString sCat4("Category_cat4:=");
   sCat1.Append(cut_os_cat1);
   sCat2.Append(cut_os_cat2);
   sCat3.Append(cut_ss);
   sCat4.Append(cut_tri);
   reader->AddSpectator( sCat1, &Category_mycat1 );
   reader->AddSpectator( sCat2, &Category_mycat2 );
   reader->AddSpectator( sCat3, &Category_mycat3 );
   reader->AddSpectator( sCat4, &Category_mycat4 );


   // --- 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 * histBdt(0);
   TH1F * histCat(0);

   if (Use["BDT"])           histBdt     = new TH1F( "MVA_BDT",           "MVA_BDT",           nbin, -0.8, 0.8 );
   if (Use["Category"])      histCat     = new TH1F( "MVA_Category",      "MVA_Category",      nbin, -2., 2. );


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


   // CMS STATS:
   // Specify files with data, for which you want to compute the classifier values
   TString fname = "./data/pass7_OS_test1/TTbar_MuMu/all.root";   
   //TString fname = "./data/pass7_TRI_test1/TTbar_MuMu/all.root";   


   if (!gSystem->AccessPathName( fname )) 
      input = TFile::Open( fname ); // check if file in local directory exists
   
   if (!input) {
      std::cout << "ERROR: could not open data file" << 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("MVA");
   //Float_t userVar1, userVar2;
   theTree->SetBranchAddress( "met",              &met );
   theTree->SetBranchAddress( "HT",               &HT );
   theTree->SetBranchAddress( "minMLB",           &minMLB );
   theTree->SetBranchAddress( "leptonJetsMETSum", &leptonJetsMETSum );


   // spectators
   theTree->SetBranchAddress( "leptonSumMass", &leptonSumMass );
   theTree->SetBranchAddress( "nJets", &nJets );
   theTree->SetBranchAddress( "nBTag", &nBTag );
   theTree->SetBranchAddress( "nLeptons", &nLeptons );
   theTree->SetBranchAddress( "isMuon1", &isMuon1 );
   theTree->SetBranchAddress( "isMuon2", &isMuon2 );
   theTree->SetBranchAddress( "isMuon3", &isMuon3 );
   theTree->SetBranchAddress( "isMuon4", &isMuon4 );


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


   // CMS STATS:
   //       
   //       A little trick: loop over only selected events

   // make event list
   theTree->Draw(">>EvtList", mycut);
   TEventList * pEvtList = (TEventList *)gROOT->FindObject("EvtList");
   long int nEvents = pEvtList->GetN();

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

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

      //theTree->GetEntry(ievt);
      theTree->GetEntry(pEvtList->GetEntry(ievt));


      // CMS STATS:
      // *** HERE we assign integer branches' values to float spectator variables
      //     Otherwise our category cuts would fail
      //
      nBTagFloat=(Float_t)nBTag;
      nJetsFloat=(Float_t)nJets;
      nLeptonsFloat=(Float_t)nLeptons;
      isMuon1Float  = (Float_t)isMuon1;
      isMuon2Float  = (Float_t)isMuon2;
      isMuon3Float  = (Float_t)isMuon3;
      isMuon4Float  = (Float_t)isMuon4;



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

      if (Use["BDT"          ])   histBdt    ->Fill( reader->EvaluateMVA( "BDT method"           ) );
      if (Use["Category"     ])   histCat    ->Fill( reader->EvaluateMVA( "Category method"      ) );

   }

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


   // --- Write histograms

   TFile *target  = new TFile( "TMVApp.root","RECREATE" );
   if (Use["BDT"          ])   histBdt    ->Write();
   if (Use["Category"     ])   histCat    ->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;
} 
void TMVAClassificationCategoryApplication()
{
   // ---------------------------------------------------------------
   // default MVA methods to be trained + tested
   std::map<std::string,int> Use;
   // ---
   Use["LikelihoodCat"] = 1;
   Use["FisherCat"]     = 1;
   // ---------------------------------------------------------------

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

   // --- Create the Reader object

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

   // Create a set of variables and spectators 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, eta;
   reader->AddVariable( "var1", &var1 );
   reader->AddVariable( "var2", &var2 );
   reader->AddVariable( "var3", &var3 );
   reader->AddVariable( "var4", &var4 );

   reader->AddSpectator( "eta", &eta );

   // --- Book the MVA methods

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

   // Book output histograms
   UInt_t nbin = 100;
   std::map<std::string,TH1*> hist;
   hist["LikelihoodCat"] = new TH1F( "MVA_LikelihoodCat",   "MVA_LikelihoodCat", nbin, -1, 0.9999 );
   hist["FisherCat"]     = new TH1F( "MVA_FisherCat",       "MVA_FisherCat",     nbin, -4, 4 );

   // 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.
   //
   TString fname = TString(gSystem->DirName(__FILE__) ) + "/data/";
   // if directory data not found try using tutorials dir
   if (gSystem->AccessPathName( fname )) {
      fname = TString(gROOT->GetTutorialsDir()) + "/tmva/data/";
   }
   if (UseOffsetMethod) fname += "toy_sigbkg_categ_offset.root";
   else                 fname += "toy_sigbkg_categ_varoff.root";
   std::cout << "--- TMVAClassificationApp    : Accessing " << fname << "!" << std::endl;
   TFile *input = TFile::Open(fname);
   if (!input) {
      std::cout << "ERROR: could not open data file: " << fname << std::endl;
      exit(1);
   }

   // --- Event loop

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

   theTree->SetBranchAddress( "eta",  &eta ); // spectator

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

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

      for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) {
         if (!it->second) continue;
         TString methodName = it->first + " method";
         hist[it->first]->Fill( reader->EvaluateMVA( methodName ) );         
      }

   }
   sw.Stop();
   std::cout << "--- End of event loop: "; sw.Print();

   // --- Write histograms

   TFile *target  = new TFile( "TMVApp.root","RECREATE" );
   for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++)
      if (it->second) hist[it->first]->Write();

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

   delete reader;
   std::cout << "==> TMVAClassificationApplication is done!" << std::endl << std::endl;
}
示例#14
0
TString TMVAPredict(TString method_name, EnumPredictMode predictMode = EnumPredictMode::FINAL)
{
    std::cout << "------------ predict with : " << method_name << " ------ " << std::endl;
    std::vector<std::string> inputNames = {"training","test","check_correlation","check_agreement"};
    std::map<std::string,std::vector<std::string>> varsForInput;

    std::vector<std::string> variableOrder = {"id", "signal", "mass", "min_ANNmuon", "prediction"};
    
    varsForInput["training"].emplace_back ("prediction");
    if (predictMode != EnumPredictMode::INTERMEDIATE)
    {
        varsForInput["training"].emplace_back ("id");
        varsForInput["training"].emplace_back ("signal");
        varsForInput["training"].emplace_back ("mass");
        varsForInput["training"].emplace_back ("min_ANNmuon");

        varsForInput["test"].emplace_back ("prediction");
        varsForInput["test"].emplace_back ("id");

        varsForInput["check_agreement"].emplace_back ("signal");
        varsForInput["check_agreement"].emplace_back ("weight");
        varsForInput["check_agreement"].emplace_back ("prediction");

        varsForInput["check_correlation"].emplace_back ("mass");
        varsForInput["check_correlation"].emplace_back ("prediction");
    }

    
    std::map<std::string,std::vector<std::string>> createForInput;
    createForInput["training"].emplace_back ("root");

    if (predictMode != EnumPredictMode::INTERMEDIATE)
    {
        createForInput["training"].emplace_back ("csv");
        createForInput["test"].emplace_back ("csv");
        createForInput["check_agreement"].emplace_back ("csv");
        createForInput["check_correlation"].emplace_back ("csv");
    }


    // -------- prepare the Reader ------
    TMVA::Tools::Instance();

    std::cout << "==> Start TMVAPredict" << std::endl;
    TMVA::Reader *reader = new TMVA::Reader( "!Color:!Silent" );  


    std::vector<Float_t> variables (variableNames.size ());
    auto itVar = begin (variables);
    for (auto varName : variableNames)
    {
	Float_t* pVar = &(*itVar);
        auto localVarName = varName;
        localVarName.substr(0,localVarName.find(":="));
	reader->AddVariable(varName.c_str(), pVar);
	(*itVar) = 0.0;
	++itVar;
    }

    // spectators not known for the reader (in test.csv)
    for (auto varName : spectatorNames)
    {
	Float_t spectator (0.0);
	reader->AddSpectator (varName.c_str(), &spectator);
	++itVar;
    }


    TString dir    = "weights/";
    TString prefix = "TMVAClassification";
    TString weightfile = dir + prefix + TString("_") + method_name + TString(".weights.xml");
    std::cout << "weightfile name : " << weightfile.Data () << std::endl;
    reader->BookMVA( method_name, weightfile ); 
  


    // --------- for each of the input files
    for (auto inputName : inputNames)
    {
        // --- define variables
	Int_t id;
	Float_t prediction;
	Float_t weight;
	Float_t min_ANNmuon;
	Float_t mass;
	Float_t signal;

       
        // --- open input file
	TFile *input(0);
        std::stringstream infilename;
        infilename << pathToData.Data () << inputName << ".root";
	std::cout << "infilename = " << infilename.str ().c_str () << std::endl;
	input = TFile::Open (infilename.str ().c_str ());
	TTree* tree = (TTree*)input->Get("data");


        // --- prepare branches on input file
	// id field if needed
	if (contains (varsForInput, inputName, "id"))
	    tree->SetBranchAddress("id", &id);

	// signal field if needed
	if (contains (varsForInput, inputName, "signal"))
	    tree->SetBranchAddress("signal", &signal);

	// min_ANNmuon field if needed
	if (contains (varsForInput, inputName, "min_ANNmuon"))
	    tree->SetBranchAddress("min_ANNmuon", &min_ANNmuon);

	// mass field if needed
	if (contains (varsForInput, inputName, "mass"))
	    tree->SetBranchAddress("mass", &mass);

	// weight field if needed
	if (contains (varsForInput, inputName, "weight"))
	    tree->SetBranchAddress("weight", &weight);

      
	// variables for prediction
	itVar = begin (variables);
	for (auto inputName : variableNames)
        {
	    Float_t* pVar = &(*itVar);
	    tree->SetBranchAddress(inputName.c_str(), pVar);
	    ++itVar;
        }
	

        // ---- make ROOT file
        TString rootFileName;
        TFile* outRootFile (NULL);
	TTree* outTree (NULL);
        if (contains (createForInput, inputName, "root"))
        {
            rootFileName = TString (inputName.c_str ()) + TString ("_prediction__") + method_name + TString (".root");
            outRootFile = new TFile (rootFileName.Data (), "RECREATE");
            outTree = new TTree("data","data");

            if (contains (varsForInput, inputName, "id"))
                outTree->Branch ("id", &id, "F");
            if (contains (varsForInput, inputName, "signal"))
                outTree->Branch ("signal", &signal, "F");
            if (contains (varsForInput, inputName, "min_ANNmuon"))
                outTree->Branch ("min_ANNmuon", &min_ANNmuon, "F");
            if (contains (varsForInput, inputName, "mass"))
                outTree->Branch ("mass", &mass, "F");
            if (contains (varsForInput, inputName, "weight"))
                outTree->Branch ("weight", &weight, "F");
            if (contains (varsForInput, inputName, "prediction"))
                outTree->Branch ("prediction", &prediction, "F");
        }

        // ---- prepare csv file
        std::ofstream outfile;
        if (contains (createForInput, inputName, "csv"))
        {
            std::stringstream outfilename;
            outfilename << inputName << "_prediction__" << method_name.Data () << ".csv";
            std::cout << outfilename.str () << std::endl; 
            /* return; */
      
            outfile.open (outfilename.str ());
            bool isFirst = true;
            for (auto varName : variableOrder)
            {
                if (contains (varsForInput, inputName, varName))
                {
                    if (!isFirst)
                        outfile << ",";
                    isFirst = false;
                    outfile << varName;
                }
            }
            outfile << "\n";
        }


        
        bool doCSV = contains (createForInput, inputName, "csv");            
        bool doROOT = contains (createForInput, inputName, "root");            
	for (Long64_t ievt=0; ievt < tree->GetEntries(); ievt++)
        {
	    tree->GetEntry(ievt);
	    // predict
	    prediction = reader->EvaluateMVA (method_name);
            prediction = std::max<double> (0.0, std::min<double> (1.0, prediction));
            //prediction = (prediction + 1.0)/2.0;
            if (doCSV)
            {
                for (auto varName : variableOrder)
                {
                    if (varName == "id" && contains (varsForInput, inputName, "id"))
                        outfile << id << ",";

                    if (varName == "signal" && contains (varsForInput, inputName, "signal"))
                        outfile << signal << ",";

                    if (varName == "min_ANNmuon" && contains (varsForInput, inputName, "min_ANNmuon"))
                        outfile << min_ANNmuon << ",";

                    if (varName == "mass" && contains (varsForInput, inputName, "mass"))
                        outfile << mass << ",";

                    if (varName == "weight" && contains (varsForInput, inputName, "weight"))
                        outfile << weight << ",";

                    if (varName == "prediction" && contains (varsForInput, inputName, "prediction"))
                        outfile << prediction;

                }
                outfile << "\n";
            }
            if (doROOT)
            {
                outTree->Fill ();
            }
        }

	outfile.close();
	input->Close();

        if (doROOT)
        {
            outRootFile->Write ();
        }
        if (predictMode == EnumPredictMode::INTERMEDIATE)
        {
            delete reader;
            std::cout << "DONE predict INTERMEDIATE" << std::endl;
            return rootFileName;
        }
    }
    delete reader;

    if (predictMode == EnumPredictMode::FINAL)
    {
        std::cout << "DONE predict FINAL" << std::endl;
        TString cmd (".! python tests.py ");
        cmd += method_name;
        gROOT->ProcessLine (cmd);
    }

    return method_name;
}
void PlotDecisionBoundary( TString weightFile = "weights/Zprime_vs_QCD_TMVAClassification_BDT.weights.xml",TString v0="lep_pt_ljet", TString v1="met_pt", TString dataFileNameS = "/nfs/dust/cms/user/karavdia/ttbar_semilep_13TeV/RunII_25ns_v1/test_03/uhh2.AnalysisModuleRunner.MC.Zp01w3000.root", TString dataFileNameB = "/nfs/dust/cms/user/karavdia/ttbar_semilep_13TeV/RunII_25ns_v1/test_03/uhh2.AnalysisModuleRunner.MC.QCD_EMEnriched.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 );
   Float_t lep_pt, lep_fbrem, MwT;
   Float_t log_ljet_pt, log_met_pt, log_lep_pt_ljet;
   Float_t log_dR_lep_cljet, log_dR_cljet_ljet;
   Float_t dPhi_lep_cljet;
   reader->AddVariable("lep_pt", &lep_pt);
   reader->AddVariable("lep_fbrem", & lep_fbrem);
   reader->AddVariable("MwT", &MwT);
   reader->AddVariable("log(ljet_pt)", &log_ljet_pt);
   reader->AddVariable("log(met_pt)",&log_met_pt);
   reader->AddVariable("log(lep_pt_ljet)",&log_lep_pt_ljet);
   reader->AddVariable("log(dR_lep_cljet)",&log_dR_lep_cljet_trans);
   reader->AddVariable("log(fabs((dR_cljet_ljet-3.14)/3.14))", &log_dR_cljet_ljet);
   reader->AddSpectator("dPhi_lep_cljet", &dPhi_lep_cljet);
   //
   // book the MVA method
   //
   reader->BookMVA( "BDT", weightFile ); 
   
   TFile *fS = new TFile(dataFileNameS);
   TTree *signal     = (TTree*)fS->Get("AnalysisTree");
   TFile *fB = new TFile(dataFileNameS);
   TTree *background = (TTree*)fB->Get("AnalysisTree");


//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,(Float_t)background->GetMaximum(v0.Data()));  
   xmin = TMath::Min(xmin,(Float_t)background->GetMinimum(v0.Data()));
   ymax = TMath::Max(ymax,(Float_t)background->GetMaximum(v1.Data()));
   ymin = TMath::Min(ymin,(Float_t)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 (UInt_t ibin=1; ibin<nbin+1; ibin++){
      for (UInt_t jbin=1; jbin<nbin+1; jbin++){
         var0 = hs->GetXaxis()->GetBinCenter(ibin);
         var1 = hs->GetYaxis()->GetBinCenter(jbin);
         Float_t mvaVal=reader->EvaluateMVA( "BDT" ) ;
         if (MinMVA>mvaVal) MinMVA=mvaVal;
         if (MaxMVA<mvaVal) MaxMVA=mvaVal;
         hist->SetBinContent(ibin,jbin, mvaVal);
      }
   }


   // now you need to try to find the MVA-value at which you cut for the plotting of the decision boundary
   // (Use the smallest number of misclassifications as criterion)
   const Int_t nValBins=100;
   Double_t    sum = 0.;
   TH1F *mvaS= new TH1F("mvaS","",nValBins,MinMVA,MaxMVA); mvaS->SetXTitle("MVA-ouput"); mvaS->SetYTitle("#entries");
   TH1F *mvaB= new TH1F("mvaB","",nValBins,MinMVA,MaxMVA); mvaB->SetXTitle("MVA-ouput"); mvaB->SetYTitle("#entries");
   TH1F *mvaSC= new TH1F("mvaSC","",nValBins,MinMVA,MaxMVA); mvaSC->SetXTitle("MVA-ouput"); mvaSC->SetYTitle("cummulation");
   TH1F *mvaBC= new TH1F("mvaBC","",nValBins,MinMVA,MaxMVA); mvaBC->SetXTitle("MVA-ouput"); mvaBC->SetYTitle("cummulation");

   Long64_t nentries;
   nentries = signal->GetEntries();
   for (Long64_t is=0; is<nentries;is++) {
      signal->GetEntry(is);
      sum +=sWeight;
      var0 = svar0;
      var1 = svar1;
      Float_t mvaVal=reader->EvaluateMVA( "BDT" ) ;
      hs->Fill(svar0,svar1);
      mvaS->Fill(mvaVal,sWeight);
   }
   nentries = background->GetEntries();
   for (Long64_t ib=0; ib<nentries;ib++) {
      background->GetEntry(ib);
      sum +=bWeight;
      var0 = bvar0;
      var1 = bvar1;
      Float_t mvaVal=reader->EvaluateMVA( "BDT" ) ;
      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 sSelBest=0;
   Double_t bSelBest=0;
   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 (UInt_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)){
         separationGain = sepGain->GetSeparationGain(sSel,bSel,sTot,bTot);
         mvaCut=mvaSC->GetBinCenter(ibin);
         if (sSel/bSel > (sTot-sSel)/(bTot-bSel)) mvaCutOrientation=-1;
         else                                     mvaCutOrientation=1;
         sSelBest=sSel;
         bSelBest=bSel;
     }
   }
   

   cout << "Min="<<MinMVA << " Max=" << MaxMVA 
        << " sTot=" << sTot
        << " bTot=" << bTot
        << " sSel=" << sSelBest
        << " bSel=" << bSelBest
        << " 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();

} 
示例#16
0
void applyBDT(std::string iName="/mnt/hscratch/dabercro/skims2/BDT_Signal.root",
              TString inputVariables = "trainingVars.txt",
              TString inputTree = "DMSTree",
              std::string iWeightFile="weights/TMVAClassificationCategory_BDT_simple_alpha.weights.xml",
              TString outName = "Output.root",
              TString fOutputName = "TMVA.root",
              TString fMethodName = "BDT",
              TString fUniformVariable = "fjet1MassTrimmed",
              TString fWeight = "f**k",
              Int_t NumBins = 10, Double_t VarMin = 10, Double_t VarMax = 190, Int_t NumMapPoints = 501) {

  Double_t binWidth = (VarMax - VarMin)/NumBins;
  Double_t VarVals[NumBins+1];
  for (Int_t i0 = 0; i0 < NumBins + 1; i0++)
    VarVals[i0] = VarMin + i0 * binWidth;

  std::vector<TGraph*> transformGraphs;
  TGraph *tempGraph;

  if (fUniformVariable != "") {
    // First scale the BDT to be uniform
    // Make the background shape

    TFile *TMVAFile = TFile::Open(fOutputName);
    TTree *BackgroundTree = (TTree*) TMVAFile->Get("TrainTree");

    mithep::PlotHists *HistPlotter = new mithep::PlotHists();
    HistPlotter->SetDefaultTree(BackgroundTree);
    HistPlotter->SetDefaultExpr(fMethodName);

    Double_t binWidth = 2.0/(NumMapPoints - 1);
    Double_t BDTBins[NumMapPoints];
    for (Int_t i0 = 0; i0 < NumMapPoints; i0++)
      BDTBins[i0] = i0 * binWidth - 1;

    for (Int_t iVarBin = 0; iVarBin < NumBins; iVarBin++) {
      TString BinCut = TString::Format("%s*(%s>=%f&&%s<%f)", fWeight.Data(),
                                       fUniformVariable.Data(), VarVals[iVarBin],
                                       fUniformVariable.Data(), VarVals[iVarBin+1]);
      HistPlotter->AddWeight(fWeight + TString("*(classID == 1 && ") + BinCut + ")");
    }

    std::vector<TH1D*> BDTHists = HistPlotter->MakeHists(NumMapPoints,-1,1);

    for (Int_t iVarBin = 0; iVarBin < NumBins; iVarBin++) {
      tempGraph = new TGraph(NumMapPoints);
      transformGraphs.push_back(tempGraph);
      Double_t FullIntegral = BDTHists[iVarBin]->Integral();
      for (Int_t iMapPoint = 0; iMapPoint < NumMapPoints; iMapPoint++) {
        transformGraphs[iVarBin]->SetPoint(iMapPoint, BDTBins[iMapPoint],
                                           BDTHists[iVarBin]->Integral(0,iMapPoint)/FullIntegral);
      }
    }

    for (UInt_t iHist = 0; iHist < BDTHists.size(); iHist++)
      delete BDTHists[iHist];

    TMVAFile->Close();
  }

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

  TString BDTName;
  ifstream configFile;
  configFile.open(inputVariables.Data());
  TString tempFormula;

  std::vector<float> Evaluated;
  std::vector<TString> Strings;
  TTreeFormula *Formulas[40];
  
  configFile >> BDTName;  // Is the name of the BDT

  while(!configFile.eof()){
    configFile >> tempFormula;
    if(tempFormula != ""){
      Evaluated.push_back(0.);
      Strings.push_back(tempFormula);
    }
  }
  
  TFile *lFile = new TFile(iName.c_str());
  TTree *lTree = (TTree*) lFile->FindObjectAny(inputTree);

  if(lTree->GetBranch(BDTName) == NULL){

    for(unsigned int i0 = 0;i0 < Strings.size();i0++){
      if (i0 == 0)
        reader->AddSpectator(Strings[i0],&Evaluated[i0]);
      else
        reader->AddVariable(Strings[i0],&Evaluated[i0]);
      Formulas[i0] = new TTreeFormula(Strings[i0],Strings[i0],lTree);
    }
    
    std::string lJetName = "BDT";
    reader->BookMVA(lJetName .c_str(),iWeightFile.c_str());
    
    int lNEvents = lTree->GetEntries();
    TFile *lOFile = new TFile(outName,"RECREATE");
    TTree *lOTree = new TTree(inputTree,inputTree);
    float lMVA    = 0; lOTree->Branch(BDTName,&lMVA ,BDTName+TString("/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);
      for(unsigned int i1 = 0;i1 < Strings.size();i1++){
        Evaluated[i1] = Formulas[i1]->EvalInstance();
      }
      
      lMVA      = float(reader->EvaluateMVA(lJetName.c_str()));

      if (fUniformVariable != "") {
        if (Evaluated[0] >= VarVals[0] && Evaluated[0] < VarVals[NumBins]) {
          for (Int_t iBin = 0; iBin < NumBins; iBin++) {
            if (Evaluated[0] < VarVals[iBin + 1]) {
              lMVA = Float_t(transformGraphs[iBin]->Eval(lMVA));
              break;
            }
          }
        }
      }

      lOTree->Fill();
    }
    lOTree->Write();
    lOFile->Close();
  }
  else std::cout << "Skipping " << iName.c_str() << std::endl;
  delete reader;

  for (UInt_t iGraph = 0; iGraph < transformGraphs.size(); iGraph++)
    delete transformGraphs[iGraph];
}