TTTT Analysis  0.1
MVA_Creation.cpp
Go to the documentation of this file.
1 
34 #include <iostream>
35 #include <vector>
36 #include <utility>
37 #include <numeric>
38 #include <limits>
39 
40 #include "filval/filval.hpp"
41 #include "filval_root/filval_root.hpp"
42 
43 #include "MiniTreeDataSet.hpp"
44 #include <TSystem.h>
45 
46 #define PI 3.14159
47 #define W_MASS 80.385 // GeV/c^2
48 #define Z_MASS 91.188 // GeV/c^2
49 #define T_MASS 172.44 // GeV/c^2
50 
51 using namespace std;
52 using namespace fv;
53 using namespace fv::root;
54 
55 void enable_branches(MiniTreeDataSet& mt){
56  mt.track_branch<int>("nLepGood");
57  mt.track_branch<int>("nJet");
58  mt.track_branch<int>("nGenPart");
59 
60  mt.track_branch<int>("nBJetLoose40");
61  mt.track_branch<int>("nBJetMedium40");
62  mt.track_branch<int>("nBJetTight40");
63 }
64 #define MVA_DTYPES int, int, int, int, int
65 void declare_values(MiniTreeDataSet& mt){
66 
67  auto event_number = mt.get_current_event_number();
68  auto is_training = fv::apply(fv::GenFunction::register_function<bool(int)>("is_odd",
69  FUNC(([](int n){
70  return (n%2) == 1;
71  }))), fv::tuple(event_number));
72 
73  auto is_signal = fv::bound(fv::GenFunction::register_function<bool()>("is_signal",
74  FUNC(([mt=&mt](){
75  const std::string& label = mt->get_current_event_label();
76  return label == "signal";
77  }))), "is_signal");
78 
79  auto weight = fv::constant<double>("1", 1);
80 
81  auto mva_data = fv::root::mva_data<MVA_DTYPES>(is_training, is_signal, weight,
82  {"nJet", lookup<int>("nJet")},
83  {"nBJetLoose40", lookup<int>("nBJetLoose40")},
84  {"nBJetMedium40", lookup<int>("nBJetMedium40")},
85  {"nBJetTight40", lookup<int>("nBJetTight40")},
86  {"nLepGood", lookup<int>("nLepGood")}
87  );
88  GenValue::alias("mva_data", mva_data);
89 
90 }
91 
92 void declare_containers(MiniTreeDataSet& mt){
93 
94  auto mva_data = (MVAData<MVA_DTYPES>*)lookup<MVAData<MVA_DTYPES>::type>("mva_data");
95  auto mva = mt.register_container<MVA<MVA_DTYPES>>("my_mva", mva_data);
96  mva->add_method("KNN", "H:nkNN=20:ScaleFrac=0.8:SigmaFact=1.0:Kernel=Gaus:UseKernel=F:UseWeight=T:!Trim");
97 }
98 
99 
100 void create_mva(const std::string& output_filename, const std::vector<std::string>& sig_files, const std::vector<std::string>& bg_files, bool silent){
101  gSystem->Load("libfilval.so");
102  auto replace_suffix = [](const std::string& input, const std::string& new_suffix){
103  return input.substr(0, input.find_last_of(".")) + new_suffix;
104  };
105  string log_filename = replace_suffix(output_filename, ".log");
106  fv::util::Log::init_logger(log_filename, fv::util::LogPriority::kLogDebug);
107 
108  std::map<std::string, std::string> filenames_with_labels;
109  for (const std::string& fname : sig_files){
110  filenames_with_labels[fname] = "signal";
111  }
112  for (const std::string& fname : bg_files){
113  filenames_with_labels[fname] = "background";
114  }
115 
116  MiniTreeDataSet mt(output_filename, filenames_with_labels);
117 
118  enable_branches(mt);
119  declare_values(mt);
120  declare_containers(mt);
121 
122  mt.process(silent);
123  mt.save_all();
124 }
125 
126 int main(int argc, char * argv[])
127 {
128  fv::util::ArgParser args(argc, argv);
129  if(args.cmdOptionExists("-h")) {
130  cout << "Usage: ./mva (-s) -out outfile.root -sig [signal_minitree.root]+ -bg [background_minitree.root]+" << endl;
131  return 0;
132  }
133  bool silent = args.cmdOptionExists("-s");
134  string output_filename = args.getCmdOption("-out");
135  std::vector<std::string> sig_files;
136  std::vector<std::string> bg_files;
137  std::vector<std::string>* cur_flist = nullptr;
138 
139  for(int i=1; i<argc; i++){
140  if (!strncmp(argv[i], "-sig", 4)){
141  cur_flist = &sig_files;
142  }
143  else if (!strncmp(argv[i], "-bg", 3)){
144  cur_flist = &bg_files;
145  continue;
146  }
147  else if (!strncmp(argv[i], "-out", 4)){
148  cur_flist = nullptr;
149  }
150  else if (cur_flist != nullptr){
151  cur_flist->push_back(argv[i]);
152  }
153  }
154  create_mva(output_filename, sig_files, bg_files, silent);
155 }
STL namespace.
The namespace containing all filval classes and functions.
Definition: api.hpp:46
Definition: api.hpp:8