FD_NCID_ADA.C
Go to the documentation of this file.
1 /**********************************************************************************
2  * Project : TMVA - a ROOT-integrated toolkit for multivariate data analysis *
3  * Package : TMVA *
4  * Root Macro: Modified from TMVAClassification *
5  * *
6  * This macro provides examples for the training and testing of the *
7  * TMVA classifiers to use CAF files *
8  * *
9  * The methods to be used can be switched on and off by means of booleans, or *
10  * via the prompt command, for example: *
11  * *
12  * root -l ./TMVAClassification.C\(\"BDT\"\) *
13  * cafe TMVAClassification.C\(\"BDT\"\) *
14  * (note that the backslashes are mandatory) *
15  * If no method given, a default set of classifiers is used. *
16  * *
17  * The output file "CafeMVA.root" can be analysed with the use of dedicated *
18  * macros (simply say: root -l <macro.C>), which can be conveniently *
19  * invoked through a GUI that will appear at the end of the run of this macro. *
20  * Launch the GUI via the command: *
21  * *
22  * root -l ./TMVAGui.C *
23  * *
24  **********************************************************************************/
25 
26 #include <cstdlib>
27 #include <iostream>
28 #include <map>
29 #include <string>
30 #include "TChain.h"
31 #include "TFile.h"
32 #include "TTree.h"
33 #include "TString.h"
34 #include "TObjString.h"
35 #include "TSystem.h"
36 #include "TROOT.h"
37 #include "TMVAGui.C"
38 #if not defined(__CINT__) || defined(__MAKECINT__)
39 // needs to be included when makecint runs (ACLIC)
40 #include "TMVA/Factory.h"
41 #include "TMVA/Tools.h"
42 #endif
43 
44 //using namespace ana;
45 void FD_ADA_B( TString myMethodList = "" )
46 {
47  // This loads the library
48  TMVA::Tools::Instance();
49  // Default MVA methods to be trained + tested
50  std::map<std::string,int> Use;
51  // --- Boosted Decision Trees
52  Use["BDTA"] = 1; // uses Adaptive Boost
53  //---------------------------------------------------------------------------------------------------------------------------------------//
55  std::cout << "=============================================> Start Classification <=========================================" << std::endl;
56  // Select methods (don't look at this code - not of interest)
57  if (myMethodList != "") {
58  for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) it->second = 0;
59  std::vector<TString> mlist = TMVA::gTools().SplitString( myMethodList, ',' );
60  for (UInt_t i=0; i<mlist.size(); i++) {
61  std::string regMethod(mlist[i]);
62  if (Use.find(regMethod) == Use.end()) {
63  std::cout << "Method \"" << regMethod << "\" not known in TMVA under this name. Choose among the following:" << std::endl;
64  for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) std::cout << it->first << " ";
65  std::cout << std::endl;
66  return;
67  }
68  Use[regMethod] = 1;
69  }
70  }
71  // Create a ROOT output file where TMVA will store ntuples, histograms, etc.
72  TString outfileName("FD_ADA_B_One_One_C.root");
73  TFile* outputFile = TFile::Open(outfileName, "RECREATE");
74  //TMVA::Factory *factory = new TMVA::Factory("SK", outputFile,"V:AnalysisType=Classification" ); //One
75  TMVA::Factory *factory = new TMVA::Factory("FD_B_One_One", outputFile,"V:AnalysisType=Classification"); //Two
76  //TMVA::Factory *factory = new TMVA::Factory("SK", outputFile,"!V:!Silent:Color:DrawProgressBar:Transformations=I;G:AnalysisType=Classification" ); //
77  //TMVA::Factory *factory = new TMVA::Factory("SK", outputFile,"!V:!Silent:Color:DrawProgressBar:Transformations=I;G:AnalysisType=Classification"); //
78  //TMVA::Factory *factory = new TMVA::Factory("SK", outputFile,"!V:!Silent:Color:DrawProgressBar:Transformations=I;D;P;G,D:AnalysisType=Classification");//
79  // Getting the input file and grabing the TTree
80  TString fInput = "/nova/ana/users/yangs9/NCPID/S17-10-30_XGB/TRA/FD/ROOT/TRA_FD_CC.root";
81  TFile *input = TFile::Open(fInput);
82  //Get the signal and background trees from TFile source(s)
83  TTree *sigTree = (TTree*)input->Get("STree");
84  TTree *bkgTree = (TTree*)input->Get("BTree");
85  //Set the event weights per tree
86  Double_t sigWeight = 1.0;
87  Double_t bkgWeight = 1.0;
88  //Register the trees
89  factory->AddSignalTree (sigTree, sigWeight);
90  factory->AddBackgroundTree (bkgTree, bkgWeight);
91  ///Adding Variables into factory
92  factory->AddVariable( "ncalhit", "Number of Calibrated Hits in the slice", 'F' );
93  factory->AddVariable( "nmiphit", "Number of MIP Hits in the slice", 'F' );
94  factory->AddVariable( "boxminy", "Minimum Y in the Box", 'F' );
95  factory->AddVariable( "boxmaxy", "Maximum Y in the Box", 'F' );
96  factory->AddVariable( "sumtx", "Summerized X View Hits", 'F' );
97  factory->AddVariable( "ewsumtx", "Energy Weighted Summerized X View Hits", 'F' );
98  factory->AddVariable( "sumty", "Summerized Y View Hits", 'F' );
99  factory->AddVariable( "ewsumty", "Energy Weighted Summerized Y View Hits", 'F' );
100  factory->AddVariable( "fuzzyknpng", "Number of FuzzyK Prongs", 'F' );
101  factory->AddVariable( "fuzzykpngdirz", "Leading Prong Z Direction", 'F' );
102  factory->AddVariable( "fuzzykpngCalE", "Leading Prong Cal Energy", 'F' );
103  factory->AddVariable( "fuzzykpnglen", "Leading Prong Length", 'F' );
104  factory->AddVariable( "fuzzykpngnhit", "Number of hits of Leading Prong", 'F' );
105  factory->AddVariable( "fuzzykpngnhitx", "X View Number of hits of Leading Prong", 'F' );
106  factory->AddVariable( "fuzzykpngnhity", "Y View Number of hits of Leading Prong", 'F' );
107  factory->AddVariable( "ncid", "CVN NC ID", 'F' );
108  //factory->AddVariable( "fuzzyknpng2d", "Number of 2D FuzzyK Prongs", 'F' );
109  //factory->AddVariable( "fuzzykntot", "Number of total FuzzyK Prongs", 'F' );
110  //factory->AddVariable( "ncellsfromedge", "Number of Cells frome the edge", 'F' );
111  //factory->AddVariable( "angsumtx", "Angule of Summerized X View Hits", 'F' );
112  //factory->AddVariable( "angewsumtx", "Angule of EW Summerized X View Hits", 'F' );
113  //factory->AddVariable( "cossumtx", "Cos Ang of Summerized X View Hits", 'F' );
114  //factory->AddVariable( "cosewsumtx", "Cos Ang EW Summerized X View Hits", 'F' );
115  //factory->AddVariable( "angsumty", "Angule of Summerized Y View Hits", 'F' );
116  //factory->AddVariable( "angewsumty", "Angule of EW Summerized Y View Hits", 'F' );
117  //factory->AddVariable( "cossumty", "Cos Ang of Summerized Y View Hits", 'F' );
118  //factory->AddVariable( "cosewsumty", "Cos Ang EW Summerized Y View Hits", 'F' );
119  //factory->AddVariable( "fuzzykpngdirx", "Leading Prong X Direction", 'F' );
120  //factory->AddVariable( "fuzzykpngdiry", "Leading Prong Y Direction", 'F' );
121 
122  TCut preselectionCut="";
123  factory->PrepareTrainingAndTestTree( "", "SplitMode=random" );
124  // ---- Book MVA methods *
125  if (Use["BDTA"])
126  factory->BookMethod(TMVA::Types::kBDT, "BDTA",
127  "H:V:NTrees=1000:InverseBoostNegWeights::BoostType=RealAdaBoost:AdaBoostBeta=0.1:nCuts=40:MaxDepth=5:MinNodeSize=2%:NodePurityLimit=0.9"); // One
128  //"H:V:NTrees=1000:InverseBoostNegWeights::BoostType=RealAdaBoost:AdaBoostBeta=0.01:nCuts=20:MaxDepth=8:MinNodeSize=2%:NodePurityLimit=0.9"); // Two
129  //"!H:!V:NTrees=850:nEventsMin=150:MaxDepth=5:BoostType=AdaBoost:AdaBoostBeta=0.5:SeparationType=GiniIndex:nCuts=20:PruneMethod=NoPruning"); //
130  //"!H:!V:NTrees=200:nEventsMin=10:MaxDepth=3:BoostType=AdaBoost:AdaBoostBeta=0.5:SeparationType=GiniIndex:nCuts=20:PruneMethod=NoPruning"); //
131  //"H:V:NTrees=1000:InverseBoostNegWeights::BoostType=RealAdaBoost:Shrinkage=0.1:nCuts=40:MaxDepth=5:MinNodeSize=2%:NodePurityLimit=0.9");//Kirk's setting
132  // Train MVAs using the set of training events
133  factory->TrainAllMethods();
134  // ---- Evaluate all MVAs using the set of test events
135  factory->TestAllMethods();
136  // ----- Evaluate and compare performance of all configured MVAs
137  factory->EvaluateAllMethods();
138  // Save the output
139  outputFile->Close();
140  std::cout << "==> Wrote root file: " << outputFile->GetName() << std::endl;
141  std::cout << "==> TMVAClassification is done!" << std::endl;
142  delete factory;
143  // Launch the GUI for the root macros
144  if (!gROOT->IsBatch()) TMVAGui( outfileName );
145 }
set< int >::iterator it
OStream cout
Definition: OStream.cxx:6
void FD_ADA_B(TString myMethodList="")
Definition: FD_NCID_ADA.C:45
enum BeamMode string