muonid_training.C
Go to the documentation of this file.
1 //................................................
2 // This script has made to train/test muon pid by
3 // proving 4 input variables such as:
4 // 1- dedxLL
5 // 2- ScatdedxLL
6 // 3- Average dedx in last 10cm
7 // 4 - Average dedx in last 40cm
8 //
9 // Author: Biswaranjan Behera
10 // Date: bbehera@fnal.gov
11 //...............................................
12 #include <cstdlib>
13 #include <iostream>
14 #include <map>
15 #include <string>
16 
17 #include "TChain.h"
18 #include "TFile.h"
19 #include "TTree.h"
20 #include "TString.h"
21 #include "TObjString.h"
22 #include "TSystem.h"
23 #include "TROOT.h"
24 
25 #include "TMVA/Tools.h"
26 #include "TMVA/Factory.h"
27 #include "TMVA/TMVAGui.h"
28 #include "TMVA/Reader.h"
29 #include "TMVA/DataLoader.h"
30 
31 using namespace TMVA;
32 
33 void muonid_training(string fTrain, string fTest, string foutput = "fMuonId.root")
34 {
35  std::cout << "Hello! Hello! Welcome to Train/Test Muon PID using TMVA" <<"\n"
36  << "......................................................." <<std::endl;
37 
38 
39  // Basic I/O
40  // Adding variables to Factory2 and also some spectator variables
41 
42  // Both trees are made of 20% of dataset
43  TFile *fTrainFile = new TFile(fTrain.c_str());
44  TFile *fTestFile = new TFile(fTest.c_str());
45 
46  // check your file is correct
47  if(!fTrainFile || fTrainFile->IsZombie())
48  throw runtime_error("File " + fTrain + " does not exist. Aborting...");
49  else
50  std::cout << "TMVA is ready to use " << fTrainFile->GetName() << " for training." << std::endl;
51 
52  if(!fTestFile || fTestFile->IsZombie())
53  throw runtime_error("File " + fTest + " does not exist. Aborting...");
54  else
55  std::cout << "TMVA is ready to use " << fTestFile->GetName() << " for testing." << std::endl;
56 
57  // Defining the output file
58  TFile* foutfile = TFile::Open(foutput.c_str(), "RECREATE" );
59 
60  // Declaring the factory and variables
61  TMVA::Factory *factory = new TMVA::Factory( "MuonID", foutfile,"V:Color:DrawProgressBar:AnalysisType=Classification");
62 
63  TMVA::DataLoader *dataloader=new TMVA::DataLoader("dataset");
64 
65  dataloader->AddVariable("DedxLL", "dE/dx LL", "", 'F');
66  dataloader->AddVariable("ScatLL", "Scatring LL", "", 'F');
67  dataloader->AddVariable("Avededxlast10cm", "Avededxlast10cm", "", 'F');
68  dataloader->AddVariable("Avededxlast40cm", "Avededxlast40cm", "", 'F');
69 
70  std::cout<<" Hurah!!... Variables and spectators are added"<<"\n"
71  <<".............................................."<<std::endl;
72 
73  // Register the training and test trees
74  TTree *sigTrainTree = (TTree*)fTrainFile->Get("sigTree");
75  TTree *bkgTrainTree = (TTree*)fTrainFile->Get("bkgTree");
76  TTree *sigTestTree = (TTree*)fTestFile ->Get("sigTree");
77  TTree *bkgTestTree = (TTree*)fTestFile ->Get("bkgTree");
78 
79  dataloader->AddSignalTree(sigTrainTree, 1.0, "Training");
80  dataloader->AddSignalTree(sigTestTree, 1.0, "Test");
81  dataloader->AddBackgroundTree(bkgTrainTree, 1.0, "Training");
82  dataloader->AddBackgroundTree(bkgTestTree, 1.0, "Test");
83 
84  std::cout<<"Added signal and background trees."<<std::endl;
85 
86  // Preparing the training and testing data
87  // .......................................
88  // If no numbers of events are given, half of the events in the tree are used
89  // for training, and the other half for testing:
90 
91  // So I THINK we don't need to do this since we already
92  // dataloader->PrepareTrainingAndTestTree( "", "SplitMode=random");
93 
94  // std::cout<<"Training and Test Tree prepared!"<<std::endl;
95 
96  // Booking TMVA methods
97  // Boosted Decision Trees (Grad Boost)
98  // ....................................
99 
100  factory->BookMethod(dataloader,TMVA::Types::kBDT, "BDTG","!H:!V:NTrees=1000:MinNodeSize=2.5%:BoostType=Grad:IgnoreNegWeightsInTraining:Shrinkage=0.50:UseBaggedBoost:BaggedSampleFraction=0.5:nCuts=20:MaxDepth=4");
101  std::cout<<"All Methods are booked!"<<std::endl;
102 
103  // Train MVAs using the set of training events
104  std::cout<<"Training All Methods!"<<std::endl;
105  factory->TrainAllMethods();
106 
107  // Evaluate all MVAs using the set of test events
108  std::cout<<"Testing All Methods!"<<std::endl;
109  factory->TestAllMethods();
110 
111  // Evaluate and compare performance of all configured MVAs
112  std::cout<<"Evaluating All Methods!"<<std::endl;
113  factory->EvaluateAllMethods();
114 
115  // Save the output
116  foutfile->Close();
117 
118  //Launch the GUI
119  // if (!gROOT->IsBatch()) TMVA::TMVAGui( foutfile->GetName() );
120 
121  std::cout << "Output file is " << foutfile->GetName() << std::endl;
122  std::cout << "Congratulation! Muon PID selection using TMVA is done sucessfully........ "<<"\n"
123  << "Next Step: Make plots quickly, Interesting stuffs will happen, Beleive me!"<<"\n"
124  << "-----------------------------xxx------------------------------"<<std::endl;
125 
126  std::cout << "Congratulations! Congratulations! Congratulations! Congratulations! Congratulations! Congratulations! " << std::endl;
127 
128 
129  delete factory;
130 
131  // ...................................................
132 
133 } // end of void
cout<< "Opened file "<< fin<< " ixs= "<< ixs<< endl;if(ixs==0) hhh=(TH1F *) fff-> Get("h1")
Definition: AddMC.C:8
void muonid_training(string fTrain, string fTest, string foutput="fMuonId.root")
OStream cout
Definition: OStream.cxx:6
Definition: tmvaglob.h:28