MVA_Creation.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /**
  2. * @file
  3. * @author Caleb Fangmeier <caleb@fangmeier.tech>
  4. * @version 0.1
  5. *
  6. * @section LICENSE
  7. *
  8. *
  9. * MIT License
  10. *
  11. * Copyright (c) 2017 Caleb Fangmeier
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining a copy
  14. * of this software and associated documentation files (the "Software"), to deal
  15. * in the Software without restriction, including without limitation the rights
  16. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  17. * copies of the Software, and to permit persons to whom the Software is
  18. * furnished to do so, subject to the following conditions:
  19. *
  20. * The above copyright notice and this permission notice shall be included in all
  21. * copies or substantial portions of the Software.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  28. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  29. * SOFTWARE.
  30. *
  31. * @section DESCRIPTION
  32. * Main analysis routine file. This file declares the Histogram/Graph objects
  33. * that will end up in the final root file. It also declares the values that
  34. * are used to populate the histogram, as well as how these values are
  35. * calculated. See the Fil-Val documentation for how the system works.
  36. */
  37. #include <iostream>
  38. #include <vector>
  39. #include <utility>
  40. #include <numeric>
  41. #include <limits>
  42. #include "filval/filval.hpp"
  43. #include "filval_root/filval_root.hpp"
  44. #include "MiniTreeDataSet.hpp"
  45. #include <TSystem.h>
  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. using namespace std;
  51. using namespace fv;
  52. using namespace fv::root;
  53. void enable_branches(MiniTreeDataSet& mt){
  54. mt.track_branch<int>("nLepGood");
  55. /* mt.track_branch_vec< int >("nLepGood", "LepGood_pdgId"); */
  56. /* mt.track_branch_vec<float>("nLepGood", "LepGood_pt"); */
  57. /* mt.track_branch_vec<float>("nLepGood", "LepGood_eta"); */
  58. /* mt.track_branch_vec<float>("nLepGood", "LepGood_phi"); */
  59. /* mt.track_branch_vec<float>("nLepGood", "LepGood_mass"); */
  60. /* mt.track_branch_vec< int >("nLepGood", "LepGood_charge"); */
  61. /* mt.track_branch_vec< int >("nLepGood", "LepGood_mcMatchId"); */
  62. /* mt.track_branch_vec< int >("nLepGood", "LepGood_mcMatchPdgId"); */
  63. /* mt.track_branch_vec< int >("nLepGood", "LepGood_mcMatchAny"); */
  64. /* mt.track_branch_vec< int >("nLepGood", "LepGood_mcMatchTau"); */
  65. /* mt.track_branch_vec< int >("nLepGood", "LepGood_mcPt"); */
  66. mt.track_branch<int>("nJet");
  67. /* mt.track_branch_vec<float>("nJet", "Jet_pt"); */
  68. /* mt.track_branch_vec<float>("nJet", "Jet_eta"); */
  69. /* mt.track_branch_vec<float>("nJet", "Jet_phi"); */
  70. /* mt.track_branch_vec<float>("nJet", "Jet_mass"); */
  71. /* mt.track_branch_vec<float>("nJet", "Jet_btagCMVA"); */
  72. /* mt.track_branch_vec< int >("nJet", "Jet_mcMatchFlav"); */
  73. /* mt.track_branch_vec< int >("nJet", "Jet_mcMatchId"); */
  74. /* mt.track_branch_vec< int >("nJet", "Jet_mcFlavour"); */
  75. mt.track_branch<int>("nGenPart");
  76. /* mt.track_branch_vec< int >("nGenPart", "GenPart_pdgId"); */
  77. /* mt.track_branch_vec< int >("nGenPart", "GenPart_motherIndex"); */
  78. /* mt.track_branch_vec< int >("nGenPart", "GenPart_motherId"); */
  79. /* mt.track_branch_vec<float>("nGenPart", "GenPart_pt"); */
  80. /* mt.track_branch_vec<float>("nGenPart", "GenPart_eta"); */
  81. /* mt.track_branch_vec<float>("nGenPart", "GenPart_phi"); */
  82. /* mt.track_branch_vec<float>("nGenPart", "GenPart_mass"); */
  83. /* mt.track_branch_vec< int >("nGenPart", "GenPart_status"); */
  84. mt.track_branch<int>("nBJetLoose40");
  85. mt.track_branch<int>("nBJetMedium40");
  86. mt.track_branch<int>("nBJetTight40");
  87. /* mt.track_branch<int>("nVert"); */
  88. /* mt.track_branch< int >("run" ); */
  89. /* mt.track_branch< int >("lumi"); */
  90. /* mt.track_branch< int >("evt" ); */
  91. /* mt.track_branch<float>("xsec"); */
  92. }
  93. void declare_values(MiniTreeDataSet& mt){
  94. /* auto data = fv::tuple(lookup<int>("nJet"), lookup<int>("nBJetLoose40"), lookup<int>("nBJetMedium40"), lookup<int>("nBJetTight40"), lookup<int>("nLepGood")); */
  95. auto event_number = mt.get_current_event_number();
  96. auto is_training = fv::apply(fv::GenFunction::register_function<bool(int)>("is_odd",
  97. FUNC(([](int n){
  98. return (n%2) == 1;
  99. }))), fv::tuple(event_number));
  100. auto is_signal = fv::bound(fv::GenFunction::register_function<bool()>("is_signal",
  101. FUNC(([mt=&mt](){
  102. const std::string& label = mt->get_current_event_label();
  103. return label == "signal";
  104. }))), "is_signal");
  105. auto weight = fv::constant<double>("1", 1);
  106. /* auto mva_data = fv::root::mva_data(data, is_training, is_signal, weight, "mva_data"); */
  107. /* mva_data->enable_logging([](std::tuple<std::tuple<int,int,int,int,int>,bool,bool,double> t) */
  108. /* { */
  109. /* std::tuple<int,int,int,int,int> data; */
  110. /* bool is_training, is_signal; */
  111. /* double weight; */
  112. /* std::tie(data, is_training, is_signal, weight) = t; */
  113. /* int nJet, nBJetLoose40, nBJetMedium40, nBJetTight40, nLepGood; */
  114. /* std::tie(nJet, nBJetLoose40, nBJetMedium40, nBJetTight40, nLepGood) = data; */
  115. /* std::stringstream ss; */
  116. /* ss << "data("<<nJet<<","<<nBJetLoose40<<","<<nBJetMedium40<<","<<nBJetTight40<<","<<nLepGood<<")"<< std::endl */
  117. /* <<"\tis_training:" << is_training<< std::endl */
  118. /* <<"\tis_signal: " << is_signal << std::endl */
  119. /* <<"\tweight: " << weight << std::endl; */
  120. /* return ss.str(); */
  121. /* }); */
  122. auto mva_data = fv::root::mva_data<int,int,int,int,int>(is_training, is_signal, weight,
  123. {"nJet", lookup<int>("nJet")},
  124. {"nBJetLoose40", lookup<int>("nBJetLoose40")},
  125. {"nBJetMedium40", lookup<int>("nBJetMedium40")},
  126. {"nBJetTight40", lookup<int>("nBJetTight40")},
  127. {"nLepGood", lookup<int>("nLepGood")}
  128. );
  129. GenValue::alias("mva_data", mva_data);
  130. }
  131. void declare_containers(MiniTreeDataSet& mt){
  132. auto mva_data = (MVAData<int,int,int,int,int>*)lookup<MVAData<int,int,int,int,int>::type>("mva_data");
  133. auto mva = mt.register_container<MVA<int,int,int,int,int>>("my_mva", mva_data);
  134. mva->add_method("KNN", "H:nkNN=20:ScaleFrac=0.8:SigmaFact=1.0:Kernel=Gaus:UseKernel=F:UseWeight=T:!Trim");
  135. }
  136. void create_mva(const std::string& output_filename, const std::vector<std::string>& sig_files, const std::vector<std::string>& bg_files, bool silent){
  137. gSystem->Load("libfilval.so");
  138. auto replace_suffix = [](const std::string& input, const std::string& new_suffix){
  139. return input.substr(0, input.find_last_of(".")) + new_suffix;
  140. };
  141. string log_filename = replace_suffix(output_filename, ".log");
  142. fv::util::Log::init_logger(log_filename, fv::util::LogPriority::kLogDebug);
  143. std::map<std::string, std::string> filenames_with_labels;
  144. for (const std::string& fname : sig_files){
  145. filenames_with_labels[fname] = "signal";
  146. }
  147. for (const std::string& fname : bg_files){
  148. filenames_with_labels[fname] = "background";
  149. }
  150. MiniTreeDataSet mt(output_filename, filenames_with_labels);
  151. enable_branches(mt);
  152. declare_values(mt);
  153. declare_containers(mt);
  154. mt.process(silent);
  155. mt.save_all();
  156. }
  157. int main(int argc, char * argv[])
  158. {
  159. fv::util::ArgParser args(argc, argv);
  160. if(args.cmdOptionExists("-h")) {
  161. cout << "Usage: ./mva (-s) -out outfile.root -sig [signal_minitree.root]+ -bg [background_minitree.root]+" << endl;
  162. return 0;
  163. }
  164. bool silent = args.cmdOptionExists("-s");
  165. string output_filename = args.getCmdOption("-out");
  166. std::vector<std::string> sig_files;
  167. std::vector<std::string> bg_files;
  168. std::vector<std::string>* cur_flist = nullptr;
  169. for(int i=1; i<argc; i++){
  170. if (!strncmp(argv[i], "-sig", 4)){
  171. cur_flist = &sig_files;
  172. }
  173. else if (!strncmp(argv[i], "-bg", 3)){
  174. cur_flist = &bg_files;
  175. continue;
  176. }
  177. else if (!strncmp(argv[i], "-out", 4)){
  178. cur_flist = nullptr;
  179. }
  180. else if (cur_flist != nullptr){
  181. cur_flist->push_back(argv[i]);
  182. }
  183. }
  184. create_mva(output_filename, sig_files, bg_files, silent);
  185. }