TTTT_Analysis.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 <limits>
  41. #include "filval/filval.hpp"
  42. #include "filval_root/filval_root.hpp"
  43. #include "MiniTreeDataSet.hpp"
  44. #include <TSystem.h>
  45. #define PI 3.14159
  46. #define W_MASS 80.385 // GeV/c^2
  47. #define Z_MASS 91.188 // GeV/c^2
  48. #define T_MASS 172.44 // GeV/c^2
  49. using namespace std;
  50. using namespace fv;
  51. using namespace fv::root;
  52. void enable_branches(MiniTreeDataSet& mt){
  53. mt.fChain->SetBranchStatus("*", false);
  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<int>("nGenPart");
  73. mt.track_branch_vec< int >("nGenPart", "GenPart_pdgId");
  74. mt.track_branch_vec< int >("nGenPart", "GenPart_motherIndex");
  75. mt.track_branch_vec< int >("nGenPart", "GenPart_motherId");
  76. mt.track_branch_vec<float>("nGenPart", "GenPart_pt");
  77. mt.track_branch_vec<float>("nGenPart", "GenPart_eta");
  78. mt.track_branch_vec<float>("nGenPart", "GenPart_phi");
  79. mt.track_branch_vec<float>("nGenPart", "GenPart_mass");
  80. mt.track_branch_vec< int >("nGenPart", "GenPart_status");
  81. mt.track_branch<int>("nBJetLoose40");
  82. mt.track_branch<int>("nBJetMedium40");
  83. mt.track_branch<int>("nBJetTight40");
  84. mt.track_branch<int>("nVert");
  85. mt.track_branch< int >("run" );
  86. mt.track_branch< int >("lumi");
  87. mt.track_branch< int >("evt" );
  88. mt.track_branch<float>("xsec");
  89. }
  90. void declare_values(MiniTreeDataSet& mt){
  91. energies(lorentz_vectors("LepGood_pt", "LepGood_eta", "LepGood_phi", "LepGood_mass", "LepGood_4v"), "LepGood_energy");
  92. energies(lorentz_vectors("GenPart_pt", "GenPart_eta", "GenPart_phi", "GenPart_mass", "GenPart_4v"), "GenPart_energy");
  93. energies(lorentz_vectors("Jet_pt", "Jet_eta", "Jet_phi", "Jet_mass", "Jet_4v" ), "Jet_energy");
  94. auto dijet_4v = cart_product<TLorentzVector, TLorentzVector>("Jet_4v", "Jet_4v", "di-jet_4v");
  95. auto& inv_mass = GenFunction::register_function<float(TLorentzVector, TLorentzVector)>("inv_mass",
  96. FUNC(([] (const TLorentzVector& v1, const TLorentzVector& v2){
  97. TLorentzVector sum = v1 + v2;
  98. return (float)sum.M();
  99. })));
  100. fv::map(inv_mass, dijet_4v, "di-jet_inv_mass");
  101. fv::pair<vector<float>,vector<float>>("LepGood_energy", "LepGood_pt", "LepGood_energy_LepGood_pt");
  102. fv::pair<vector<float>,vector<float>>("Jet_energy", "Jet_eta", "Jet_energy_vs_Jet_eta");
  103. max<float>("LepGood_energy", "LepGood_energy_max");
  104. min<float>("LepGood_energy", "LepGood_energy_min");
  105. range<float>("LepGood_energy", "LepGood_energy_range");
  106. mean<float>("LepGood_energy", "LepGood_energy_mean");
  107. count<float>(GenFunction::register_function<bool(float)>("bJet_Selection", FUNC(([](float x){return x>0;}))),
  108. "Jet_btagCMVA", "b_jet_count");
  109. auto &is_electron = GenFunction::register_function<bool(int)>("is_electron", FUNC(([](int pdgId)
  110. {
  111. return abs(pdgId) == 11;
  112. })));
  113. auto &is_muon = GenFunction::register_function<bool(int)>("is_muon", FUNC(([](int pdgId)
  114. {
  115. return abs(pdgId) == 13;
  116. })));
  117. auto &is_lepton = GenFunction::register_function<bool(int)>("is_lepton", FUNC(([ie=&is_electron, im=&is_muon](int pdgId)
  118. {
  119. return (*ie)(pdgId) || (*im)(pdgId);
  120. })));
  121. count<int>(is_electron, "GenPart_pdgId", "genEle_count");
  122. count<int>(is_muon, "GenPart_pdgId", "genMu_count");
  123. count<int>(is_lepton, "GenPart_pdgId", "genLep_count");
  124. count<int>(is_electron, "LepGood_pdgId", "recEle_count");
  125. count<int>(is_muon, "LepGood_pdgId", "recMu_count");
  126. count<int>(is_lepton, "LepGood_pdgId", "recLep_count");
  127. fv::pair<int, int>("genEle_count", "recEle_count", "genEle_count_v_recEle_count");
  128. fv::pair<int, int>("genMu_count", "recMu_count", "genMu_count_v_recMu_count");
  129. fv::pair<int, int>("genLep_count", "recLep_count", "genLep_count_v_recLep_count");
  130. obs_filter("trilepton", FUNC(([nLepGood=lookup<int>("nLepGood")]()
  131. {
  132. return dynamic_cast<Value<int>*>(nLepGood)->get_value() == 3;
  133. })));
  134. obs_filter("os-dilepton", FUNC(([LepGood_charge=lookup<vector<int>>("LepGood_charge")]()
  135. {
  136. auto& charges = LepGood_charge->get_value();
  137. return charges.size()==2 && (charges[0] != charges[1]);
  138. })));
  139. obs_filter("ss-dilepton", FUNC(([LepGood_charge=lookup<vector<int>>("LepGood_charge")]()
  140. {
  141. auto& charges = LepGood_charge->get_value();
  142. return charges.size()==2 && (charges[0] == charges[1]);
  143. })));
  144. }
  145. void declare_containers(MiniTreeDataSet& mt){
  146. mt.register_container(new ContainerTH1<int>("lepton_count", "Lepton Multiplicity", lookup<int>("nLepGood"), 8, 0, 8));
  147. mt.register_container(new ContainerTH1Many<float>("LepGood_energy_all", "Lepton Energy - All",
  148. lookup<vector<float>>("LepGood_energy"), 50, 0, 500));
  149. mt.register_container(new ContainerTH1<float>("LepGood_energy_max", "Lepton Energy - Max",
  150. lookup<float>("LepGood_energy_max"), 50, 0, 500,
  151. "Lepton Energy Max(GeV)"));
  152. mt.register_container(new ContainerTH1<float>("LepGood_energy_min", "Lepton Energy - Min",
  153. lookup<float>("LepGood_energy_min"), 50, 0, 500,
  154. "Lepton Energy Min(GeV)"));
  155. mt.register_container(new ContainerTH1<float>("LepGood_energy_range", "Lepton Energy - Range",
  156. lookup<float>("LepGood_energy_range"), 50, 0, 500,
  157. "Lepton Energy Range(GeV)"));
  158. mt.register_container(new ContainerTH1Many<float>("Jet_pt", "Jet P_T",
  159. lookup<vector<float>>("Jet_pt"), 50, 0, 500,
  160. "Jet P_T"));
  161. mt.register_container(new ContainerTH1Many<float>("Jet_eta", "Jet Eta",
  162. lookup<vector<float>>("Jet_eta"), 50, -3, 3,
  163. "Jet Eta"));
  164. mt.register_container(new ContainerTH1Many<float>("Jet_phi", "Jet Phi",
  165. lookup<vector<float>>("Jet_phi"), 20, -PI, PI,
  166. "Jet Phi"));
  167. mt.register_container(new ContainerTH1Many<float>("Jet_mass", "Jet Mass",
  168. lookup<vector<float>>("Jet_mass"), 50, 0, 200,
  169. "Jet Mass"));
  170. mt.register_container(new ContainerTH1Many<float>("dijet_inv_mass", "Di-Jet Inv. Mass - All",
  171. lookup<vector<float>>("di-jet_inv_mass"), 100, 0, 500,
  172. "Di-Jet Mass"));
  173. mt.register_container(new ContainerTH1Many<float>("dijet_inv_mass_osdilepton", "Di-Jet Inv. Mass - OS Dilepton",
  174. lookup<vector<float>>("di-jet_inv_mass"), 100, 0, 500,
  175. "Di-Jet Mass"))->add_filter(lookup_obs_filter("os-dilepton"));
  176. mt.register_container(new ContainerTH1Many<float>("dijet_inv_mass_ssdilepton", "Di-Jet Inv. Mass - SS Dilepton",
  177. lookup<vector<float>>("di-jet_inv_mass"), 100, 0, 500,
  178. "Di-Jet Mass"))->add_filter(lookup_obs_filter("ss-dilepton"));
  179. mt.register_container(new ContainerTH1Many<float>("dijet_inv_mass_trilepton", "Di-Jet Inv. Mass - Trilepton",
  180. lookup<vector<float>>("di-jet_inv_mass"), 100, 0, 500,
  181. "Di-Jet Mass"))->add_filter(lookup_obs_filter("trilepton"));
  182. mt.register_container(new ContainerTH1Many<float>("Jet_energy", "Jet Energy",
  183. lookup<vector<float>>("Jet_energy"), 100, 0, 400,
  184. "Jet Energy"));
  185. mt.register_container(new ContainerTH2Many<float>("Jet_energy_vs_Jet_eta", "Jet Energy vs Jet Eta",
  186. lookup<std::pair<vector<float>, vector<float>>>("Jet_energy_vs_Jet_eta"),
  187. 100, 0, 400, 50, -3, 3,
  188. "Jet Energy", "Jet Eta"));
  189. mt.register_container(new ContainerTH2<int>("nLepvsnJet", "Number of Leptons vs Number of Jets",
  190. fv::pair<int, int>("nLepGood", "nJet"),
  191. 7, 0, 7, 20, 0, 20,
  192. "Number of Leptons", "Number of Jets"));
  193. mt.register_container(new ContainerTH2Many<float>("LepGood_energy_vs_pt", "Lepton Energy vs Lepton Pt",
  194. lookup<std::pair<std::vector<float>,std::vector<float>>>("LepGood_energy_LepGood_pt"),
  195. 50, 0, 500, 50, 0, 500,
  196. "lepton energy","lepton Pt"));
  197. mt.register_container(new ContainerTH2<int>("genEle_count_v_recEle_count", "Number of Generated Electrons v. Number of Reconstructed Electrons",
  198. lookup<std::pair<int,int>>("genEle_count_v_recEle_count"),
  199. 10, 0, 10, 10, 0, 10,
  200. "Generated Electrons","Reconstructed Electrons"));
  201. mt.register_container(new ContainerTH2<int>("genMu_count_v_recMu_count", "Number of Generated Muons v. Number of Reconstructed Muons",
  202. lookup<std::pair<int,int>>("genMu_count_v_recMu_count"),
  203. 10, 0, 10, 10, 0, 10,
  204. "Generated Muons","Reconstructed Muons"));
  205. mt.register_container(new ContainerTH2<int>("genLep_count_v_recLep_count", "Number of Generated Leptons v. Number of Reconstructed Leptons (e & mu only)",
  206. lookup<std::pair<int,int>>("genLep_count_v_recLep_count"),
  207. 10, 0, 10, 10, 0, 10,
  208. "Generated Leptons","Reconstructed Leptons"));
  209. mt.register_container(new ContainerTH1<int>("b_jet_count", "B-Jet Multiplicity", lookup<int>("b_jet_count"), 10, 0, 10));
  210. mt.register_container(new ContainerTH1<int>("jet_count_os_dilepton", "Jet Multiplicity - OS Dilepton Events",
  211. lookup<int>("nJet"), 14, 0, 14))->add_filter(lookup_obs_filter("os-dilepton"));
  212. mt.register_container(new ContainerTH1<int>("jet_count_ss_dilepton", "Jet Multiplicity - SS Dilepton Events",
  213. lookup<int>("nJet"), 14, 0, 14))->add_filter(lookup_obs_filter("ss-dilepton"));
  214. mt.register_container(new ContainerTH1<int>("jet_count_trilepton", "Jet Multiplicity - Trilepton Events",
  215. lookup<int>("nJet"), 14, 0, 14))->add_filter(lookup_obs_filter("trilepton"));
  216. mt.register_container(new ContainerTH1<int>("nVert", "Number of Primary Vertices", lookup<int>("nVert"), 50, 0, 50));
  217. mt.register_container(new CounterMany<int>("GenPart_pdgId_counter", lookup<vector<int>>("GenPart_pdgId")));
  218. mt.register_container(new Vector<vector< int >>("GenPart_pdgId", lookup<vector< int >>("GenPart_pdgId")));
  219. mt.register_container(new Vector<vector< int >>("GenPart_motherIndex", lookup<vector< int >>("GenPart_motherIndex")));
  220. mt.register_container(new Vector<vector< int >>("GenPart_motherId", lookup<vector< int >>("GenPart_motherId")));
  221. mt.register_container(new Vector<vector<float>>("GenPart_pt", lookup<vector<float>>("GenPart_pt")));
  222. mt.register_container(new Vector<vector<float>>("GenPart_energy", lookup<vector<float>>("GenPart_energy")));
  223. mt.register_container(new Vector<vector< int >>("GenPart_status", lookup<vector< int >>("GenPart_status")));
  224. mt.register_container(new Vector<vector< int >>("LepGood_mcMatchId", lookup<vector< int >>("LepGood_mcMatchId")));
  225. mt.register_container(new Vector<vector< int >>("LepGood_mcMatchPdgId", lookup<vector< int >>("LepGood_mcMatchPdgId")));
  226. mt.register_container(new Vector< int >("run", lookup< int >("run") ));
  227. mt.register_container(new Vector< int >("lumi", lookup< int >("lumi")));
  228. mt.register_container(new Vector< int >("evt", lookup< int >("evt") ));
  229. mt.register_container(new Vector<float>("xsec", lookup<float>("xsec")));
  230. }
  231. void run_analysis(const std::string& input_filename, bool silent){
  232. gSystem->Load("libfilval.so");
  233. auto replace_suffix = [](const std::string& input, const std::string& new_suffix){
  234. return input.substr(0, input.find_last_of(".")) + new_suffix;
  235. };
  236. string log_filename = replace_suffix(input_filename, "_result.log");
  237. fv::util::Log::init_logger(log_filename, fv::util::LogPriority::kLogDebug);
  238. string output_filename = replace_suffix(input_filename, "_result.root");
  239. MiniTreeDataSet mt(input_filename, output_filename);
  240. enable_branches(mt);
  241. declare_values(mt);
  242. declare_containers(mt);
  243. mt.process(silent);
  244. mt.save_all();
  245. }
  246. int main(int argc, char * argv[])
  247. {
  248. fv::util::ArgParser args(argc, argv);
  249. if(!args.cmdOptionExists("-f")) {
  250. cout << "Usage: ./main -f input_minitree.root" << endl;
  251. return -1;
  252. }
  253. bool silent = args.cmdOptionExists("-s");
  254. string input_filename = args.getCmdOption("-f");
  255. run_analysis(input_filename, silent);
  256. }