obj_types.hpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. * Define some common data structures used throughout the analysis and methods
  33. * to make them available.
  34. */
  35. #ifndef OBJ_TYPES_HPP
  36. #define OBJ_TYPES_HPP
  37. #include "TLorentzVector.h"
  38. #include "filval/filval.hpp"
  39. #include "analysis/MiniTreeDataSet.hpp"
  40. struct Jet{
  41. float b_cmva;
  42. int mcMatchFlav;
  43. };
  44. struct Lepton {
  45. int pdg_id;
  46. int mcMatchPdgId;
  47. int charge;
  48. float relIso;
  49. };
  50. struct GenPart{
  51. int pdgId;
  52. int motherIndex;
  53. int motherId;
  54. int status;
  55. };
  56. struct Particle{
  57. enum{JET,
  58. LEPTON,
  59. GENPART,
  60. VOID,
  61. } tag;
  62. int idx;
  63. TLorentzVector v;
  64. union
  65. {
  66. Jet jet;
  67. Lepton lepton;
  68. GenPart genpart;
  69. };
  70. static Particle
  71. Jet(int idx, TLorentzVector v, Jet&& jet){
  72. Particle p = {Particle::JET, idx, v, {}};
  73. p.jet = jet;
  74. return p;
  75. }
  76. static Particle
  77. Lepton(int idx, TLorentzVector v, Lepton&& lepton){
  78. Particle p = {Particle::LEPTON, idx, v, {}};
  79. p.lepton = lepton;
  80. return p;
  81. }
  82. static Particle
  83. GenPart(int idx, TLorentzVector v, GenPart&& genpart){
  84. Particle p = {Particle::GENPART, idx, v, {}};
  85. p.genpart = genpart;
  86. return p;
  87. }
  88. static Particle
  89. Void(){
  90. Particle p;
  91. p.tag = Particle::VOID;
  92. return p;
  93. }
  94. };
  95. Function<Particle(std::vector<Particle>)>& leading_particle = GenFunction::reg_func<Particle(std::vector<Particle>)>("leading_particle",
  96. FUNC(([](const std::vector<Particle>& particles){
  97. if(particles.size() == 0) return Particle::Void();
  98. Particle leading_particle = particles[0];
  99. for (auto particle : particles){
  100. if(particle.tag != Particle::VOID && particle.v.Pt() > leading_particle.v.Pt()){
  101. leading_particle = particle;
  102. }
  103. }
  104. return leading_particle;
  105. })));
  106. Function<float(Particle)>& particle_pt = GenFunction::reg_func<float(Particle)>("particle_pt",
  107. FUNC(([](const Particle& p){
  108. return p.v.Pt();
  109. })));
  110. Function<float(Particle)>& particle_eta = GenFunction::reg_func<float(Particle)>("particle_eta",
  111. FUNC(([](const Particle& p){
  112. return p.v.Eta();
  113. })));
  114. Function<float(Particle)>& lepton_relIso = GenFunction::reg_func<float(Particle)>("lepton_relIso",
  115. FUNC(([](const Particle& p){
  116. return p.lepton.relIso;
  117. })));
  118. decltype(auto)
  119. construct_jets_value(MiniTreeDataSet& mt){
  120. mt.track_branch<int>("nJet");
  121. mt.track_branch_vec<float>("nJet", "Jet_pt");
  122. mt.track_branch_vec<float>("nJet", "Jet_eta");
  123. mt.track_branch_vec<float>("nJet", "Jet_phi");
  124. mt.track_branch_vec<float>("nJet", "Jet_mass");
  125. auto Jet_4v = lorentz_vectors("Jet_pt", "Jet_eta", "Jet_phi", "Jet_mass", "Jet_4v");
  126. auto jets = apply(GenFunction::reg_func<std::vector<Particle>(std::vector<TLorentzVector>,
  127. std::vector<float>,
  128. std::vector< int >
  129. )>("build_reco_jets",
  130. FUNC(([](const std::vector<TLorentzVector>& vs,
  131. const std::vector<float>& b_cmvas,
  132. const std::vector< int >& mcMatchFlavs
  133. ){
  134. std::vector<Particle> jets;
  135. for(int i=0; i<vs.size(); i++){
  136. Particle p = Particle::Jet(i, vs[i], {b_cmvas[i],
  137. mcMatchFlavs[i]
  138. });
  139. jets.push_back(p);
  140. }
  141. return jets;
  142. }))), fv::tuple(Jet_4v,
  143. mt.track_branch_vec<float>("nJet", "Jet_btagCMVA"),
  144. mt.track_branch_vec< int >("nJet", "Jet_mcMatchFlav")
  145. ), "jets");
  146. return jets;
  147. }
  148. decltype(auto)
  149. construct_b_jets_value(MiniTreeDataSet& mt){
  150. auto b_jets = fv::filter(GenFunction::reg_func<bool(Particle)>("is_b_jet",
  151. FUNC(([](const Particle& p){
  152. if(p.tag != Particle::JET) return false;
  153. return p.jet.b_cmva > 0;
  154. }))), lookup<std::vector<Particle>>("jets"), "b_jets");
  155. return b_jets;
  156. }
  157. decltype(auto)
  158. construct_leptons_value(MiniTreeDataSet& mt){
  159. mt.track_branch<int>("nLepGood");
  160. mt.track_branch_vec<float>("nLepGood", "LepGood_pt");
  161. mt.track_branch_vec<float>("nLepGood", "LepGood_eta");
  162. mt.track_branch_vec<float>("nLepGood", "LepGood_phi");
  163. mt.track_branch_vec<float>("nLepGood", "LepGood_mass");
  164. auto LepGood_4v = lorentz_vectors("LepGood_pt", "LepGood_eta", "LepGood_phi", "LepGood_mass", "LepGood_4v");
  165. auto leptons = apply(GenFunction::reg_func<std::vector<Particle>(std::vector<TLorentzVector>,
  166. std::vector<int>,
  167. std::vector<int>,
  168. std::vector<int>,
  169. std::vector<float>)>("build_reco_leptons",
  170. FUNC(([](const std::vector<TLorentzVector>& vs,
  171. const std::vector<int>& pdgIds,
  172. const std::vector<int>& mcMatchPdgIds,
  173. const std::vector<int>& charges,
  174. const std::vector<float>& relIso
  175. ){
  176. std::vector<Particle> leptons;
  177. for(int i=0; i<vs.size(); i++){
  178. Particle p = Particle::Lepton(i, vs[i], {pdgIds[i],
  179. mcMatchPdgIds[i],
  180. charges[i],
  181. relIso[i]});
  182. leptons.push_back(p);
  183. }
  184. return leptons;
  185. }))), fv::tuple(LepGood_4v,
  186. mt.track_branch_vec< int >("nLepGood", "LepGood_pdgId"),
  187. mt.track_branch_vec< int >("nLepGood", "LepGood_mcMatchPdgId"),
  188. mt.track_branch_vec< int >("nLepGood", "LepGood_charge"),
  189. mt.track_branch_vec<float>("nLepGood", "LepGood_miniRelIso")
  190. ), "leptons");
  191. return leptons;
  192. }
  193. decltype(auto)
  194. construct_mc_jets_value(MiniTreeDataSet& mt){
  195. mt.track_branch<int>("nGenPart");
  196. mt.track_branch_vec<float>("nGenPart", "GenPart_pt");
  197. mt.track_branch_vec<float>("nGenPart", "GenPart_eta");
  198. mt.track_branch_vec<float>("nGenPart", "GenPart_phi");
  199. mt.track_branch_vec<float>("nGenPart", "GenPart_mass");
  200. auto Jet_4v = lorentz_vectors("GenPart_pt", "GenPart_eta", "GenPart_phi", "GenPart_mass", "GenPart_4v");
  201. energies(Jet_4v, "GenPart_energy");
  202. auto mc_jets = apply(GenFunction::reg_func<std::vector<Particle>(std::vector<TLorentzVector>,
  203. std::vector<int>,
  204. std::vector<int>,
  205. std::vector<int>,
  206. std::vector<int>)>("build_mc_jets",
  207. FUNC(([](const std::vector<TLorentzVector>& vs,
  208. const std::vector<int>& pdgIds,
  209. const std::vector<int>& motherIndices,
  210. const std::vector<int>& motherIds,
  211. const std::vector<int>& statuses){
  212. std::vector<Particle> mc_jets;
  213. for(int i=0; i<vs.size(); i++){
  214. Particle p = Particle::GenPart(i, vs[i], {pdgIds[i],
  215. motherIndices[i],
  216. motherIds[i],
  217. statuses[i]});
  218. mc_jets.push_back(p);
  219. }
  220. return mc_jets;
  221. }))), fv::tuple(Jet_4v,
  222. mt.track_branch_vec<int>("nGenPart", "GenPart_pdgId"),
  223. mt.track_branch_vec<int>("nGenPart", "GenPart_motherIndex"),
  224. mt.track_branch_vec<int>("nGenPart", "GenPart_motherId"),
  225. mt.track_branch_vec<int>("nGenPart", "GenPart_status")
  226. ),"mc_jets");
  227. return mc_jets;
  228. }
  229. void create_all_common_values(MiniTreeDataSet& mt){
  230. construct_jets_value(mt);
  231. construct_b_jets_value(mt);
  232. construct_mc_jets_value(mt);
  233. construct_leptons_value(mt);
  234. }
  235. #endif