TTTT_Analysis.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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_pdgid_filter = GenFunction::register_function<bool(Particle)>("b_pdgid_filter",
  64. FUNC(([](const Particle& j){
  65. return j.genpart.pdgId == 5 || j.genpart.pdgId==-5;
  66. })));
  67. auto& w_mass_filter = GenFunction::register_function<bool(Particle, Particle)>("w_mass_filter",
  68. FUNC(([win_l=W_MASS-10, win_h=W_MASS+10](const Particle& j1, const Particle& j2){
  69. float inv_mass = (j1.v + j2.v).M();
  70. return inv_mass > win_l && inv_mass < win_h;
  71. })));
  72. auto& dup_filter = GenFunction::register_function<bool(std::tuple<Particle,Particle>,Particle)>("dup_filter",
  73. FUNC(([](const std::tuple<Particle,Particle>& w, const Particle& b){
  74. int j0 = b.idx;
  75. int j1 = std::get<0>(w).idx;
  76. int j2 = std::get<1>(w).idx;
  77. return (j0 != j1) && (j0 != j2) && (j1 != j2);
  78. })));
  79. auto& qg_id_filter = GenFunction::register_function<bool(Particle, Particle)>("qg_id_filter",
  80. FUNC(([](const Particle& j1, const Particle& j2){
  81. // require both particles be either quarks(not Top) or gluons
  82. int id1 = abs(j1.genpart.pdgId);
  83. int id2 = abs(j2.genpart.pdgId);
  84. return ((id1 >=1 && id1 <= 5) || id1 == 21) &&
  85. ((id2 >=1 && id2 <= 5) || id2 == 21);
  86. })));
  87. // Here is the calculation of the Top Reconstructed Mass from Particle
  88. auto jets = lookup<std::vector<Particle>>("jets");
  89. auto b_jets = lookup<std::vector<Particle>>("b_jets");
  90. auto w_dijets = tup_filter<Particle,Particle>(w_mass_filter, combinations<Particle,2>(jets, "reco_dijets"));
  91. auto top_cands = cart_product<std::tuple<Particle,Particle>, Particle>(w_dijets, b_jets);
  92. top_cands = tup_filter(dup_filter, top_cands);
  93. auto& t_mass = GenFunction::register_function<float(std::tuple<Particle,Particle>,Particle)>("t_mass",
  94. FUNC(([](const std::tuple<Particle,Particle>& w, const Particle& b){
  95. return (std::get<0>(w).v+std::get<1>(w).v+b.v).M();
  96. })));
  97. fv::map(t_mass, top_cands, "reco_top_mass");
  98. // Here is the calculation of the Top Reconstructed Mass from Generator-Level objects
  99. jets = lookup<std::vector<Particle>>("mc_jets");
  100. b_jets = filter(b_pdgid_filter, jets);
  101. w_dijets = tup_filter(qg_id_filter, combinations<Particle,2>(jets));
  102. w_dijets = tup_filter(w_mass_filter, w_dijets);
  103. top_cands = cart_product<std::tuple<Particle,Particle>, Particle>(w_dijets, b_jets);
  104. top_cands = tup_filter(dup_filter, top_cands);
  105. fv::map(t_mass, top_cands, "mc_top_mass");
  106. // calculation of di-jet inv-mass spectrum
  107. auto& inv_mass2 = GenFunction::register_function<float(Particle, Particle)>("inv_mass2",
  108. FUNC(([] (const Particle& j1, const Particle& j2){
  109. TLorentzVector sum = j1.v + j2.v;
  110. return (float)sum.M();
  111. })));
  112. fv::map(inv_mass2, lookup<std::vector<std::tuple<Particle,Particle>>>("reco_dijets"), "dijet_inv_mass");
  113. count<float>(GenFunction::register_function<bool(float)>("bJet_Selection",
  114. FUNC(([](float x){
  115. return x>0;
  116. }))), "Jet_btagCMVA", "b_jet_count");
  117. auto &is_electron = GenFunction::register_function<bool(int)>("is_electron",
  118. FUNC(([](int pdgId) {
  119. return abs(pdgId) == 11;
  120. })));
  121. auto &is_muon = GenFunction::register_function<bool(int)>("is_muon",
  122. FUNC(([](int pdgId) {
  123. return abs(pdgId) == 13;
  124. })));
  125. auto &is_lepton = GenFunction::register_function<bool(int)>("is_lepton",
  126. FUNC(([ie=&is_electron, im=&is_muon](int pdgId) {
  127. return (*ie)(pdgId) || (*im)(pdgId);
  128. })));
  129. count<int>(is_electron, "GenPart_pdgId", "genEle_count");
  130. count<int>(is_muon, "GenPart_pdgId", "genMu_count");
  131. count<int>(is_lepton, "GenPart_pdgId", "genLep_count");
  132. count<int>(is_electron, "LepGood_pdgId", "recEle_count");
  133. count<int>(is_muon, "LepGood_pdgId", "recMu_count");
  134. count<int>(is_lepton, "LepGood_pdgId", "recLep_count");
  135. fv::pair<int, int>("genEle_count", "recEle_count", "genEle_count_v_recEle_count");
  136. fv::pair<int, int>("genMu_count", "recMu_count", "genMu_count_v_recMu_count");
  137. fv::pair<int, int>("genLep_count", "recLep_count", "genLep_count_v_recLep_count");
  138. /* auto& sum = GenFunction::register_function<float(std::vector<float>)>("sum", */
  139. /* FUNC(([](const std::vector<float>& v){ */
  140. /* return std::accumulate(v.begin(), v.end(), 0); */
  141. /* }))); */
  142. /* auto sum_jet_pt = fv::apply(sum, lookup<std::vector<float>>("Jet_pt")); */
  143. obs_filter("trilepton", FUNC(([nLepGood=lookup<int>("nLepGood")]()
  144. {
  145. return dynamic_cast<Value<int>*>(nLepGood)->get_value() == 3;
  146. })));
  147. obs_filter("os-dilepton", FUNC(([LepGood_charge=lookup<vector<int>>("LepGood_charge")]()
  148. {
  149. auto& charges = LepGood_charge->get_value();
  150. return charges.size()==2 && (charges[0] != charges[1]);
  151. })));
  152. obs_filter("ss-dilepton", FUNC(([LepGood_charge=lookup<vector<int>>("LepGood_charge")]()
  153. {
  154. auto& charges = LepGood_charge->get_value();
  155. return charges.size()==2 && (charges[0] == charges[1]);
  156. })));
  157. }
  158. void declare_containers(MiniTreeDataSet& mt){
  159. mt.register_container<ContainerTH1<int>>("lepton_count", "Lepton Multiplicity", lookup<int>("nLepGood"), 8, 0, 8);
  160. mt.register_container<ContainerTH1Many<float>>("Jet_pt_dist", "Jet P_T",
  161. lookup<vector<float>>("Jet_pt"), 50, 0, 500,
  162. "Jet P_T");
  163. mt.register_container<ContainerTH1Many<float>>("Jet_eta_dist", "Jet Eta",
  164. lookup<vector<float>>("Jet_eta"), 50, -3, 3,
  165. "Jet Eta");
  166. mt.register_container<ContainerTH1Many<float>>("Jet_phi_dist", "Jet Phi",
  167. lookup<vector<float>>("Jet_phi"), 20, -PI, PI,
  168. "Jet Phi");
  169. mt.register_container<ContainerTH1Many<float>>("Jet_mass_dist", "Jet Mass",
  170. lookup<vector<float>>("Jet_mass"), 50, 0, 200,
  171. "Jet Mass");
  172. mt.register_container<ContainerTH1Many<float>>("dijet_inv_mass", "Di-Jet Inv. Mass - All",
  173. lookup<vector<float>>("dijet_inv_mass"), 100, 0, 500,
  174. "Di-Jet Mass");
  175. mt.register_container<ContainerTH1Many<float>>("dijet_inv_mass_osdilepton", "Di-Jet Inv. Mass - OS Dilepton",
  176. lookup<vector<float>>("dijet_inv_mass"), 100, 0, 500,
  177. "Di-Jet Mass")->add_filter(lookup_obs_filter("os-dilepton"));
  178. mt.register_container<ContainerTH1Many<float>>("dijet_inv_mass_ssdilepton", "Di-Jet Inv. Mass - SS Dilepton",
  179. lookup<vector<float>>("dijet_inv_mass"), 100, 0, 500,
  180. "Di-Jet Mass")->add_filter(lookup_obs_filter("ss-dilepton"));
  181. mt.register_container<ContainerTH1Many<float>>("dijet_inv_mass_trilepton", "Di-Jet Inv. Mass - Trilepton",
  182. lookup<vector<float>>("dijet_inv_mass"), 100, 0, 500,
  183. "Di-Jet Mass")->add_filter(lookup_obs_filter("trilepton"));
  184. mt.register_container<ContainerTH1Many<float>>("reco_top_mass", "Reconstructed Top mass",
  185. lookup<vector<float>>("reco_top_mass"), 100, 0, 500,
  186. "Tri-jet invarient mass");
  187. /* mt.register_container<ContainerTH1Many<float>>("reco_top_pt", "Reconstructed Top Pt", */
  188. /* lookup<vector<float>>("reco_top_pt"), 100, 0, 500, */
  189. /* "Tri-jet Pt"); */
  190. mt.register_container<ContainerTH1Many<float>>("mc_top_mass", "Reconstructed MC Top mass",
  191. lookup<vector<float>>("mc_top_mass"), 100, 0, 500,
  192. "Tri-jet invarient mass");
  193. mt.register_container<ContainerTH2<int>>("nLepvsnJet", "Number of Leptons vs Number of Jets",
  194. fv::pair<int, int>("nLepGood", "nJet"),
  195. 7, 0, 7, 20, 0, 20,
  196. "Number of Leptons", "Number of Jets");
  197. mt.register_container<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<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<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<ContainerTH1<int>>("b_jet_count", "B-Jet Multiplicity", lookup<int>("b_jet_count"), 10, 0, 10);
  210. mt.register_container<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<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<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<CounterMany<int>>("GenPart_pdgId_counter", lookup<vector<int>>("GenPart_pdgId"));
  217. mt.register_container<Vector<std::vector< int >>>("GenPart_pdgId", lookup<std::vector< int >>("GenPart_pdgId"));
  218. mt.register_container<Vector<std::vector< int >>>("GenPart_motherIndex", lookup<std::vector< int >>("GenPart_motherIndex"));
  219. mt.register_container<Vector<std::vector< int >>>("GenPart_motherId", lookup<std::vector< int >>("GenPart_motherId"));
  220. mt.register_container<Vector<std::vector<float>>>("GenPart_pt", lookup<std::vector<float>>("GenPart_pt"));
  221. mt.register_container<Vector<std::vector<float>>>("GenPart_eta", lookup<std::vector<float>>("GenPart_eta"));
  222. mt.register_container<Vector<std::vector<float>>>("GenPart_phi", lookup<std::vector<float>>("GenPart_phi"));
  223. mt.register_container<Vector<std::vector<float>>>("GenPart_mass", lookup<std::vector<float>>("GenPart_mass"));
  224. mt.register_container<Vector<std::vector<float>>>("GenPart_energy", lookup<std::vector<float>>("GenPart_energy"));
  225. mt.register_container<Vector<std::vector< int >>>("GenPart_status", lookup<std::vector< int >>("GenPart_status"));
  226. mt.register_container<Vector<vector< int >>>("LepGood_mcMatchPdgId", lookup<vector< int >>("LepGood_mcMatchPdgId"));
  227. mt.register_container<Vector< int >>("run", lookup< int >("run") );
  228. mt.register_container<Vector< int >>("lumi", lookup< int >("lumi"));
  229. mt.register_container<Vector< int >>("evt", lookup< int >("evt") );
  230. mt.register_container<Vector<float>>("xsec", lookup<float>("xsec"));
  231. mt.register_container<Vector<std::vector< int >>>("Jet_mcMatchFlav", lookup<std::vector< int >>("Jet_mcMatchFlav"));
  232. mt.register_container<Vector<std::vector<float>>>("Jet_pt", lookup<std::vector<float>>("Jet_pt"));
  233. mt.register_container<Vector<std::vector<float>>>("Jet_eta", lookup<std::vector<float>>("Jet_eta"));
  234. mt.register_container<Vector<std::vector<float>>>("Jet_phi", lookup<std::vector<float>>("Jet_phi"));
  235. mt.register_container<Vector<std::vector<float>>>("Jet_mass", lookup<std::vector<float>>("Jet_mass"));
  236. mt.register_container<PassCount>("SR4j_count", SR4j);
  237. mt.register_container<PassCount>("SR5j_count", SR5j);
  238. mt.register_container<PassCount>("SR6j_count", SR6j);
  239. }
  240. void run_analysis(const std::string& input_filename, bool silent){
  241. gSystem->Load("libfilval.so");
  242. auto replace_suffix = [](const std::string& input, const std::string& new_suffix){
  243. return input.substr(0, input.find_last_of(".")) + new_suffix;
  244. };
  245. string log_filename = replace_suffix(input_filename, "_result.log");
  246. fv::util::Log::init_logger(log_filename, fv::util::LogPriority::kLogDebug);
  247. string output_filename = replace_suffix(input_filename, "_result.root");
  248. MiniTreeDataSet mt(output_filename, input_filename);
  249. create_all_common_values(mt);
  250. enable_extra_branches(mt);
  251. declare_values(mt);
  252. init_selection();
  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. }