TTTT_Analysis.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 "analysis/common/obj_types.hpp"
  45. #include "analysis/common/constants.hpp"
  46. #include "analysis/selection.hpp"
  47. #include "MiniTreeDataSet.hpp"
  48. #include <TSystem.h>
  49. using namespace std;
  50. using namespace fv;
  51. using namespace fv::root;
  52. void enable_extra_branches(MiniTreeDataSet& mt){
  53. mt.track_branch<int>("nBJetLoose40");
  54. mt.track_branch<int>("nBJetMedium40");
  55. mt.track_branch<int>("nBJetTight40");
  56. mt.track_branch< int >("run" );
  57. mt.track_branch< int >("lumi");
  58. mt.track_branch< int >("evt" );
  59. mt.track_branch<float>("xsec");
  60. }
  61. void declare_values(MiniTreeDataSet& mt){
  62. // Define a couple selections to be used in the top-mass reconstruction.
  63. auto& b_mva_filter = GenFunction::register_function<bool(Particle)>("b_mva_filter",
  64. FUNC(([cut=B_JET_WP](const Particle& j){
  65. return j.jet.b_cmva > cut;
  66. })));
  67. auto& b_pdgid_filter = GenFunction::register_function<bool(Particle)>("b_pdgid_filter",
  68. FUNC(([](const Particle& j){
  69. return j.genpart.pdgId == 5 || j.genpart.pdgId==-5;
  70. })));
  71. auto& w_mass_filter = GenFunction::register_function<bool(Particle, Particle)>("w_mass_filter",
  72. FUNC(([win_l=W_MASS-10, win_h=W_MASS+10](const Particle& j1, const Particle& j2){
  73. float inv_mass = (j1.v + j2.v).M();
  74. return inv_mass > win_l && inv_mass < win_h;
  75. })));
  76. auto& dup_filter = GenFunction::register_function<bool(std::tuple<Particle,Particle>,Particle)>("dup_filter",
  77. FUNC(([](const std::tuple<Particle,Particle>& w, const Particle& b){
  78. int j0 = b.idx;
  79. int j1 = std::get<0>(w).idx;
  80. int j2 = std::get<1>(w).idx;
  81. return (j0 != j1) && (j0 != j2) && (j1 != j2);
  82. })));
  83. auto& qg_id_filter = GenFunction::register_function<bool(Particle, Particle)>("qg_id_filter",
  84. FUNC(([](const Particle& j1, const Particle& j2){
  85. // require both particles be either quarks(not Top) or gluons
  86. int id1 = abs(j1.genpart.pdgId);
  87. int id2 = abs(j2.genpart.pdgId);
  88. return ((id1 >=1 && id1 <= 5) || id1 == 21) &&
  89. ((id2 >=1 && id2 <= 5) || id2 == 21);
  90. })));
  91. // Here is the calculation of the Top Reconstructed Mass from Particle
  92. auto jets = lookup<std::vector<Particle>>("jets");
  93. auto b_jets = filter(b_mva_filter, jets, "reco_b_jets");
  94. auto w_dijets = tup_filter<Particle,Particle>(w_mass_filter, combinations<Particle,2>(jets, "reco_dijets"));
  95. auto top_cands = cart_product<std::tuple<Particle,Particle>, Particle>(w_dijets, b_jets);
  96. top_cands = tup_filter(dup_filter, top_cands);
  97. auto& t_mass = GenFunction::register_function<float(std::tuple<Particle,Particle>,Particle)>("t_mass",
  98. FUNC(([](const std::tuple<Particle,Particle>& w, const Particle& b){
  99. return (std::get<0>(w).v+std::get<1>(w).v+b.v).M();
  100. })));
  101. fv::map(t_mass, top_cands, "reco_top_mass");
  102. // Here is the calculation of the Top Reconstructed Mass from Generator-Level objects
  103. jets = lookup<std::vector<Particle>>("mc_jets");
  104. b_jets = filter(b_pdgid_filter, jets);
  105. w_dijets = tup_filter(qg_id_filter, combinations<Particle,2>(jets));
  106. w_dijets = tup_filter(w_mass_filter, w_dijets);
  107. top_cands = cart_product<std::tuple<Particle,Particle>, Particle>(w_dijets, b_jets);
  108. top_cands = tup_filter(dup_filter, top_cands);
  109. fv::map(t_mass, top_cands, "mc_top_mass");
  110. // calculation of di-jet inv-mass spectrum
  111. auto& inv_mass2 = GenFunction::register_function<float(Particle, Particle)>("inv_mass2",
  112. FUNC(([] (const Particle& j1, const Particle& j2){
  113. TLorentzVector sum = j1.v + j2.v;
  114. return (float)sum.M();
  115. })));
  116. fv::map(inv_mass2, lookup<std::vector<std::tuple<Particle,Particle>>>("reco_dijets"), "dijet_inv_mass");
  117. count<float>(GenFunction::register_function<bool(float)>("bJet_Selection",
  118. FUNC(([](float x){
  119. return x>0;
  120. }))), "Jet_btagCMVA", "b_jet_count");
  121. auto &is_electron = GenFunction::register_function<bool(int)>("is_electron",
  122. FUNC(([](int pdgId) {
  123. return abs(pdgId) == 11;
  124. })));
  125. auto &is_muon = GenFunction::register_function<bool(int)>("is_muon",
  126. FUNC(([](int pdgId) {
  127. return abs(pdgId) == 13;
  128. })));
  129. auto &is_lepton = GenFunction::register_function<bool(int)>("is_lepton",
  130. FUNC(([ie=&is_electron, im=&is_muon](int pdgId) {
  131. return (*ie)(pdgId) || (*im)(pdgId);
  132. })));
  133. count<int>(is_electron, "GenPart_pdgId", "genEle_count");
  134. count<int>(is_muon, "GenPart_pdgId", "genMu_count");
  135. count<int>(is_lepton, "GenPart_pdgId", "genLep_count");
  136. count<int>(is_electron, "LepGood_pdgId", "recEle_count");
  137. count<int>(is_muon, "LepGood_pdgId", "recMu_count");
  138. count<int>(is_lepton, "LepGood_pdgId", "recLep_count");
  139. fv::pair<int, int>("genEle_count", "recEle_count", "genEle_count_v_recEle_count");
  140. fv::pair<int, int>("genMu_count", "recMu_count", "genMu_count_v_recMu_count");
  141. fv::pair<int, int>("genLep_count", "recLep_count", "genLep_count_v_recLep_count");
  142. /* auto& sum = GenFunction::register_function<float(std::vector<float>)>("sum", */
  143. /* FUNC(([](const std::vector<float>& v){ */
  144. /* return std::accumulate(v.begin(), v.end(), 0); */
  145. /* }))); */
  146. /* auto sum_jet_pt = fv::apply(sum, lookup<std::vector<float>>("Jet_pt")); */
  147. obs_filter("trilepton", FUNC(([nLepGood=lookup<int>("nLepGood")]()
  148. {
  149. return dynamic_cast<Value<int>*>(nLepGood)->get_value() == 3;
  150. })));
  151. obs_filter("os-dilepton", FUNC(([LepGood_charge=lookup<vector<int>>("LepGood_charge")]()
  152. {
  153. auto& charges = LepGood_charge->get_value();
  154. return charges.size()==2 && (charges[0] != charges[1]);
  155. })));
  156. obs_filter("ss-dilepton", FUNC(([LepGood_charge=lookup<vector<int>>("LepGood_charge")]()
  157. {
  158. auto& charges = LepGood_charge->get_value();
  159. return charges.size()==2 && (charges[0] == charges[1]);
  160. })));
  161. }
  162. void declare_containers(MiniTreeDataSet& mt){
  163. mt.register_container<ContainerTH1<int>>("lepton_count", "Lepton Multiplicity", lookup<int>("nLepGood"), 8, 0, 8);
  164. mt.register_container<ContainerTH1Many<float>>("Jet_pt_dist", "Jet P_T",
  165. lookup<vector<float>>("Jet_pt"), 50, 0, 500,
  166. "Jet P_T");
  167. mt.register_container<ContainerTH1Many<float>>("Jet_eta_dist", "Jet Eta",
  168. lookup<vector<float>>("Jet_eta"), 50, -3, 3,
  169. "Jet Eta");
  170. mt.register_container<ContainerTH1Many<float>>("Jet_phi_dist", "Jet Phi",
  171. lookup<vector<float>>("Jet_phi"), 20, -PI, PI,
  172. "Jet Phi");
  173. mt.register_container<ContainerTH1Many<float>>("Jet_mass_dist", "Jet Mass",
  174. lookup<vector<float>>("Jet_mass"), 50, 0, 200,
  175. "Jet Mass");
  176. mt.register_container<ContainerTH1Many<float>>("dijet_inv_mass", "Di-Jet Inv. Mass - All",
  177. lookup<vector<float>>("dijet_inv_mass"), 100, 0, 500,
  178. "Di-Jet Mass");
  179. mt.register_container<ContainerTH1Many<float>>("dijet_inv_mass_osdilepton", "Di-Jet Inv. Mass - OS Dilepton",
  180. lookup<vector<float>>("dijet_inv_mass"), 100, 0, 500,
  181. "Di-Jet Mass")->add_filter(lookup_obs_filter("os-dilepton"));
  182. mt.register_container<ContainerTH1Many<float>>("dijet_inv_mass_ssdilepton", "Di-Jet Inv. Mass - SS Dilepton",
  183. lookup<vector<float>>("dijet_inv_mass"), 100, 0, 500,
  184. "Di-Jet Mass")->add_filter(lookup_obs_filter("ss-dilepton"));
  185. mt.register_container<ContainerTH1Many<float>>("dijet_inv_mass_trilepton", "Di-Jet Inv. Mass - Trilepton",
  186. lookup<vector<float>>("dijet_inv_mass"), 100, 0, 500,
  187. "Di-Jet Mass")->add_filter(lookup_obs_filter("trilepton"));
  188. mt.register_container<ContainerTH1Many<float>>("reco_top_mass", "Reconstructed Top mass",
  189. lookup<vector<float>>("reco_top_mass"), 100, 0, 500,
  190. "Tri-jet invarient mass");
  191. /* mt.register_container<ContainerTH1Many<float>>("reco_top_pt", "Reconstructed Top Pt", */
  192. /* lookup<vector<float>>("reco_top_pt"), 100, 0, 500, */
  193. /* "Tri-jet Pt"); */
  194. mt.register_container<ContainerTH1Many<float>>("mc_top_mass", "Reconstructed MC Top mass",
  195. lookup<vector<float>>("mc_top_mass"), 100, 0, 500,
  196. "Tri-jet invarient mass");
  197. mt.register_container<ContainerTH2<int>>("nLepvsnJet", "Number of Leptons vs Number of Jets",
  198. fv::pair<int, int>("nLepGood", "nJet"),
  199. 7, 0, 7, 20, 0, 20,
  200. "Number of Leptons", "Number of Jets");
  201. mt.register_container<ContainerTH2<int>>("genEle_count_v_recEle_count", "Number of Generated Electrons v. Number of Reconstructed Electrons",
  202. lookup<std::pair<int,int>>("genEle_count_v_recEle_count"),
  203. 10, 0, 10, 10, 0, 10,
  204. "Generated Electrons","Reconstructed Electrons");
  205. mt.register_container<ContainerTH2<int>>("genMu_count_v_recMu_count", "Number of Generated Muons v. Number of Reconstructed Muons",
  206. lookup<std::pair<int,int>>("genMu_count_v_recMu_count"),
  207. 10, 0, 10, 10, 0, 10,
  208. "Generated Muons","Reconstructed Muons");
  209. mt.register_container<ContainerTH2<int>>("genLep_count_v_recLep_count", "Number of Generated Leptons v. Number of Reconstructed Leptons (e & mu only)",
  210. lookup<std::pair<int,int>>("genLep_count_v_recLep_count"),
  211. 10, 0, 10, 10, 0, 10,
  212. "Generated Leptons","Reconstructed Leptons");
  213. mt.register_container<ContainerTH1<int>>("b_jet_count", "B-Jet Multiplicity", lookup<int>("b_jet_count"), 10, 0, 10);
  214. mt.register_container<ContainerTH1<int>>("jet_count_os_dilepton", "Jet Multiplicity - OS Dilepton Events",
  215. lookup<int>("nJet"), 14, 0, 14)->add_filter(lookup_obs_filter("os-dilepton"));
  216. mt.register_container<ContainerTH1<int>>("jet_count_ss_dilepton", "Jet Multiplicity - SS Dilepton Events",
  217. lookup<int>("nJet"), 14, 0, 14)->add_filter(lookup_obs_filter("ss-dilepton"));
  218. mt.register_container<ContainerTH1<int>>("jet_count_trilepton", "Jet Multiplicity - Trilepton Events",
  219. lookup<int>("nJet"), 14, 0, 14)->add_filter(lookup_obs_filter("trilepton"));
  220. mt.register_container<CounterMany<int>>("GenPart_pdgId_counter", lookup<vector<int>>("GenPart_pdgId"));
  221. mt.register_container<Vector<std::vector< int >>>("GenPart_pdgId", lookup<std::vector< int >>("GenPart_pdgId"));
  222. mt.register_container<Vector<std::vector< int >>>("GenPart_motherIndex", lookup<std::vector< int >>("GenPart_motherIndex"));
  223. mt.register_container<Vector<std::vector< int >>>("GenPart_motherId", lookup<std::vector< int >>("GenPart_motherId"));
  224. mt.register_container<Vector<std::vector<float>>>("GenPart_pt", lookup<std::vector<float>>("GenPart_pt"));
  225. mt.register_container<Vector<std::vector<float>>>("GenPart_eta", lookup<std::vector<float>>("GenPart_eta"));
  226. mt.register_container<Vector<std::vector<float>>>("GenPart_phi", lookup<std::vector<float>>("GenPart_phi"));
  227. mt.register_container<Vector<std::vector<float>>>("GenPart_mass", lookup<std::vector<float>>("GenPart_mass"));
  228. mt.register_container<Vector<std::vector<float>>>("GenPart_energy", lookup<std::vector<float>>("GenPart_energy"));
  229. mt.register_container<Vector<std::vector< int >>>("GenPart_status", lookup<std::vector< int >>("GenPart_status"));
  230. mt.register_container<Vector<vector< int >>>("LepGood_mcMatchPdgId", lookup<vector< int >>("LepGood_mcMatchPdgId"));
  231. mt.register_container<Vector< int >>("run", lookup< int >("run") );
  232. mt.register_container<Vector< int >>("lumi", lookup< int >("lumi"));
  233. mt.register_container<Vector< int >>("evt", lookup< int >("evt") );
  234. mt.register_container<Vector<float>>("xsec", lookup<float>("xsec"));
  235. mt.register_container<Vector<std::vector< int >>>("Jet_mcMatchFlav", lookup<std::vector< int >>("Jet_mcMatchFlav"));
  236. mt.register_container<Vector<std::vector<float>>>("Jet_pt", lookup<std::vector<float>>("Jet_pt"));
  237. mt.register_container<Vector<std::vector<float>>>("Jet_eta", lookup<std::vector<float>>("Jet_eta"));
  238. mt.register_container<Vector<std::vector<float>>>("Jet_phi", lookup<std::vector<float>>("Jet_phi"));
  239. mt.register_container<Vector<std::vector<float>>>("Jet_mass", lookup<std::vector<float>>("Jet_mass"));
  240. }
  241. void run_analysis(const std::string& input_filename, bool silent){
  242. gSystem->Load("libfilval.so");
  243. auto replace_suffix = [](const std::string& input, const std::string& new_suffix){
  244. return input.substr(0, input.find_last_of(".")) + new_suffix;
  245. };
  246. string log_filename = replace_suffix(input_filename, "_result.log");
  247. fv::util::Log::init_logger(log_filename, fv::util::LogPriority::kLogDebug);
  248. string output_filename = replace_suffix(input_filename, "_result.root");
  249. MiniTreeDataSet mt(output_filename, input_filename);
  250. create_all_common_values(mt);
  251. enable_extra_branches(mt);
  252. declare_values(mt);
  253. declare_containers(mt);
  254. mt.process(silent);
  255. mt.save_all();
  256. }
  257. int main(int argc, char * argv[])
  258. {
  259. fv::util::ArgParser args(argc, argv);
  260. if(!args.cmdOptionExists("-f")) {
  261. cout << "Usage: ./main (-s) -f input_minitree.root" << endl;
  262. return -1;
  263. }
  264. bool silent = args.cmdOptionExists("-s");
  265. string input_filename = args.getCmdOption("-f");
  266. run_analysis(input_filename, silent);
  267. }